Repository navigation
Expand file tree
/
Copy pathpose_imu_ang.py
More file actions
201 lines (181 loc) · 6.19 KB
/
Copy pathpose_imu_ang.py
File metadata and controls
201 lines (181 loc) · 6.19 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
import rospy
import time
import numpy as np
from std_msgs.msg import String
from sensor_msgs.msg import Imu
import matplotlib.pyplot as plt
import tf
from math import pi, cos, sin
import diagnostic_msgs
import diagnostic_updater
from geometry_msgs.msg import Quaternion, Twist
from nav_msgs.msg import Odometry
class NodoPos(object): # Crea clase
def __init__(self):
self.ekf_pub = rospy.Publisher('/ekf', Odometry, queue_size=10)
self.enc_start_flag = 0 #una variable con memoria de theta
self.enc_x=0
self.enc_y=0
self.enc_yaw=0
self.enc_N=100.0
self.enc_angle_l=0
self.enc_angle_l_1=0
self.enc_angle_r=0
self.enc_angle_r_1=0
self.enc_inc_r =0
self.enc_inc_l=0
self.enc_radio = 7.2/100.0
self.enc_yo = 0.6
self.enc_PPR = 6533.0
self.enc_wr =0
self.enc_wl =0
self.enc_vr=0
self.enc_vl=0
self.enc_VX = 0
self.enc_delta_z = 0
self.enc_Ts = 1/10.0
self.enc_X=[]
self.enc_Y=[]
self.enc_acum_yaw = []
self.enc_samples=0
# I M U
self.imu_samples = 0
self.imu_Ts = 1/100.0
self.imu_ax = 0
self.imu_ay = 0
self.imu_az = 0
self.imu_vx = 0
self.imu_vy = 0
self.imu_vz = 0
self.imu_x = 0
self.imu_y = 0
self.imu_z = 0
self.imu_mean_ax = -0.0628382861614
self.imu_mean_ay = -0.347958862782
self.imu_mean_az = 9.80008125305
self.imu_std_ax = 0
self.imu_std_ay = 0
self.imu_std_az = 0
self.imu_yaw= 0
self.imu_pitch = 0
self.imu_roll = 0
self.imu_acum_X = []
self.imu_acum_Y = []
self.imu_acum_Z = []
self.imu_acum_yaw = []
#rospy.loginfo("Starting node")
rospy.Subscriber('/encoders', String, self.calcEnc) #10 Hz
rospy.Subscriber('/imu_data', Imu, self.calcImu) #100 Hz
rospy.spin()
def calcEnc(self,data):
self.enc_samples+=1
data=str(data)
data = data.split(",")
angle_l = data[0].split(":")
self.enc_angle_l = float(angle_l[2])
angle_r = data[1].split(":")
angle_r = angle_r[1].strip('"')
self.enc_angle_r = float(angle_r)
self.enc_inc_l = self.enc_angle_l-self.enc_angle_l_1
self.enc_inc_r = self.enc_angle_r-self.enc_angle_r_1
self.enc_angle_l_1 = self.enc_angle_l
self.enc_angle_r_1 = self.enc_angle_r
self.enc_wl = (2.0*np.pi/self.enc_PPR)*(self.enc_inc_l/self.enc_Ts)
self.enc_wr = (2.0*np.pi/self.enc_PPR)*(self.enc_inc_r/self.enc_Ts)
self.enc_vr = self.enc_radio*self.enc_wr
self.enc_vl = self.enc_radio*self.enc_wl
self.enc_delta_z = (self.enc_vr - self.enc_vl)/(2.0*self.enc_yo)
if self.enc_start_flag == 0:
self.enc_start_flag = 1
else:
self.enc_VX = (self.enc_vr+self.enc_vl)/2.0
self.enc_yaw = self.enc_yaw + self.enc_delta_z*self.enc_Ts
self.enc_acum_yaw.append(self.enc_yaw)
#print str(self.enc_VX)+" "+str(self.enc_yaw)
self.enc_yaw = self.imu_yaw
self.enc_x=self.enc_x+np.cos(self.enc_yaw)*self.enc_VX*self.enc_Ts #-np.cos...
self.enc_y=self.enc_y+np.sin(self.enc_yaw)*self.enc_VX*self.enc_Ts
self.enc_X.append( self.enc_x)
self.enc_Y.append( self.enc_y)
self.publish_odom(self.enc_x, self.enc_y, self.enc_yaw, self.enc_VX, self.enc_delta_z)
def calcImu(self,data):
self.imu_samples +=1
qx = data.orientation.x
qy = data.orientation.y
qz = data.orientation.z
qw = data.orientation.w
q = np.array([qx,qy,qz,qw])
euler = tf.transformations.euler_from_quaternion(q)
self.imu_yaw = euler[2]
self.imu_ax = data.linear_acceleration.x - self.imu_mean_ax
self.imu_ay = data.linear_acceleration.y - self.imu_mean_ay
self.imu_az = data.linear_acceleration.z - self.imu_mean_az
R = np.matrix([ [1,0,0,0], [0,np.cos(self.imu_yaw),-np.sin(self.imu_yaw),0], [0,np.sin(self.imu_yaw),np.cos(self.imu_yaw),0],[0,0,0,1]])
Ain = np.matrix([[-self.imu_ay],[self.imu_ax],[self.imu_az],[1]])
Aout= np.matmul(R,Ain)
self.imu_ax = Aout[0]
self.imu_ay = Aout[1]
self.imu_az = Aout[2]
if self.imu_samples <3000:
self.imu_mean_ax += data.linear_acceleration.x
self.imu_mean_ay += data.linear_acceleration.y
self.imu_mean_az += data.linear_acceleration.z
if self.imu_samples == 3000:
self.imu_mean_ax = self.imu_mean_ax/3000.0
self.imu_mean_ay = self.imu_mean_ay/3000.0
self.imu_mean_az = self.imu_mean_az/3000.0
if self.imu_samples >3000:
self.imu_vx = self.imu_vx + self.imu_ax*self.imu_Ts
self.imu_vy = self.imu_vy + self.imu_ay*self.imu_Ts
self.imu_vz = self.imu_vz + self.imu_az*self.imu_Ts
self.imu_x = self.imu_x + self.imu_vx*self.imu_Ts
self.imu_y = self.imu_y + self.imu_vy*self.imu_Ts
self.imu_z = self.imu_z + self.imu_vz*self.imu_Ts
self.imu_acum_X.append(self.imu_x)
self.imu_acum_Y.append(self.imu_y)
self.imu_acum_Z.append(self.imu_z)
self.imu_acum_yaw.append(self.imu_yaw)
def publish_odom(self, cur_x, cur_y, cur_theta, vx, vth):
quat = tf.transformations.quaternion_from_euler(0, 0, cur_theta)
current_time = rospy.Time.now()
br = tf.TransformBroadcaster()
br.sendTransform((cur_x, cur_y, 0),
tf.transformations.quaternion_from_euler(0, 0, cur_theta),
current_time,
"base_link",
"odom")
odom = Odometry()
odom.header.stamp = current_time
odom.header.frame_id = 'odom'
odom.pose.pose.position.x = cur_x
odom.pose.pose.position.y = cur_y
odom.pose.pose.position.z = 0.0
odom.pose.pose.orientation = Quaternion(*quat)
odom.pose.covariance[0] = 0.01
odom.pose.covariance[7] = 0.01
odom.pose.covariance[14] = 99999
odom.pose.covariance[21] = 99999
odom.pose.covariance[28] = 99999
odom.pose.covariance[35] = 0.01
odom.child_frame_id = 'base_link'
odom.twist.twist.linear.x = vx
odom.twist.twist.linear.y = 0
odom.twist.twist.angular.z = vth
odom.twist.covariance = odom.pose.covariance
self.ekf_pub.publish(odom)
def main(self):
#ekf_pub = rospy.Publisher('/ekf', Odometry, queue_size=10)
#rospy.init_node("ekf_node")
rate = rospy.Rate(10) # 10hz
while not rospy.is_shutdown():
hello_str = "hello world %s" % rospy.get_time()
#rospy.loginfo(hello_str)
#pub.publish(hello_str)
rate.sleep()
if __name__ == '__main__':
try:
rospy.init_node("ekf_node")
NodoPos()
except rospy.ROSInterruptException:
pass
print "Exiting EKF"