2 # -*- coding: iso-8859-15 -*-
9 from geometry_msgs.msg import Twist
10 from nav_msgs.msg import Odometry
11 from diagnostic_msgs.msg import DiagnosticArray, DiagnosticStatus, KeyValue
12 from sensor_msgs.msg import Imu, Range
18 rospy.init_node('wild_thumper_move_base')
19 rospy.Subscriber("cmd_vel", Twist, self.cmdVelReceived)
20 rospy.Subscriber("imu", Imu, self.imuReceived)
21 enable_odom_tf = rospy.get_param("~enable_odom_tf", True)
23 self.tf_broadcaster = tf.broadcaster.TransformBroadcaster()
25 self.tf_broadcaster = None
26 self.pub_odom = rospy.Publisher("odom", Odometry, queue_size=16)
27 self.pub_diag = rospy.Publisher("diagnostics", DiagnosticArray, queue_size=16)
28 self.pub_range_fwd = rospy.Publisher("range_forward", Range, queue_size=16)
29 self.pub_range_bwd = rospy.Publisher("range_backward", Range, queue_size=16)
30 self.pub_range_left = rospy.Publisher("range_left", Range, queue_size=16)
31 self.pub_range_right = rospy.Publisher("range_right", Range, queue_size=16)
33 rospy.loginfo("Init done")
34 i2c_write_reg(0x50, 0x90, struct.pack("BB", 1, 1)) # switch direction
35 self.handicap_last = (-1, -1)
39 rate = rospy.Rate(20.0)
40 reset_val = self.get_reset()
41 rospy.loginfo("Reset Status: 0x%x" % reset_val)
42 while not rospy.is_shutdown():
43 #print struct.unpack(">B", i2c_read_reg(0x50, 0xA2, 1))[0] # count test
47 self.get_dist_forward()
48 self.get_dist_backward()
53 def set_motor_handicap(self, front, aft): # percent
54 if self.handicap_last != (front, aft):
55 i2c_write_reg(0x50, 0x94, struct.pack(">bb", front, aft))
56 self.handicap_last = (front, aft)
58 def imuReceived(self, msg):
59 (roll, pitch, yaw) = tf.transformations.euler_from_quaternion(msg.orientation.__getstate__())
61 val = (100.0/65)*abs(pitch)*180/pi
62 self.set_motor_handicap(0, int(val))
63 elif pitch < -30*pi/180:
64 val = (100.0/65)*abs(pitch)*180/pi
65 self.set_motor_handicap(int(val), 0)
67 self.set_motor_handicap(0, 0)
70 reset = struct.unpack(">B", i2c_read_reg(0x50, 0xA0, 1))[0]
72 msg = DiagnosticArray()
73 msg.header.stamp = rospy.Time.now()
74 stat = DiagnosticStatus()
75 stat.name = "Reset reason"
76 stat.level = DiagnosticStatus.ERROR if reset & 0x0c else DiagnosticStatus.OK
77 stat.message = "0x%02x" % reset
79 stat.values.append(KeyValue("Watchdog Reset Flag", str(bool(reset & (1 << 3)))))
80 stat.values.append(KeyValue("Brown-out Reset Flag", str(bool(reset & (1 << 2)))))
81 stat.values.append(KeyValue("External Reset Flag", str(bool(reset & (1 << 1)))))
82 stat.values.append(KeyValue("Power-on Reset Flag", str(bool(reset & (1 << 0)))))
84 msg.status.append(stat)
85 self.pub_diag.publish(msg)
89 def get_tle_err(self):
90 err = struct.unpack(">B", i2c_read_reg(0x50, 0xA1, 1))[0]
92 msg = DiagnosticArray()
93 msg.header.stamp = rospy.Time.now()
94 stat = DiagnosticStatus()
95 stat.name = "Motor: Error Status"
96 stat.level = DiagnosticStatus.ERROR if err else DiagnosticStatus.OK
97 stat.message = "0x%02x" % err
99 stat.values.append(KeyValue("aft left", str(bool(err & (1 << 0)))))
100 stat.values.append(KeyValue("front left", str(bool(err & (1 << 1)))))
101 stat.values.append(KeyValue("front right", str(bool(err & (1 << 2)))))
102 stat.values.append(KeyValue("aft right", str(bool(err & (1 << 3)))))
104 msg.status.append(stat)
105 self.pub_diag.publish(msg)
107 def get_voltage(self):
108 volt = struct.unpack(">h", i2c_read_reg(0x52, 0x09, 2))[0]/100.0
110 msg = DiagnosticArray()
111 msg.header.stamp = rospy.Time.now()
112 stat = DiagnosticStatus()
113 stat.name = "Voltage"
114 stat.level = DiagnosticStatus.ERROR if volt < 7 else DiagnosticStatus.OK
115 stat.message = "%.2fV" % volt
117 msg.status.append(stat)
118 self.pub_diag.publish(msg)
122 posx, posy, angle = struct.unpack(">fff", i2c_read_reg(0x50, 0x40, 12))
123 speed_trans, speed_rot = struct.unpack(">ff", i2c_read_reg(0x50, 0x38, 8))
124 current_time = rospy.Time.now()
126 # since all odometry is 6DOF we'll need a quaternion created from yaw
127 odom_quat = tf.transformations.quaternion_from_euler(0, 0, angle)
129 # first, we'll publish the transform over tf
130 if self.tf_broadcaster is not None:
131 self.tf_broadcaster.sendTransform((posx, posy, 0.0), odom_quat, current_time, "base_footprint", "odom")
133 # next, we'll publish the odometry message over ROS
135 odom.header.stamp = current_time
136 odom.header.frame_id = "/odom"
139 odom.pose.pose.position.x = posx
140 odom.pose.pose.position.y = posy
141 odom.pose.pose.position.z = 0.0
142 odom.pose.pose.orientation.x = odom_quat[0]
143 odom.pose.pose.orientation.y = odom_quat[1]
144 odom.pose.pose.orientation.z = odom_quat[2]
145 odom.pose.pose.orientation.w = odom_quat[3]
146 odom.pose.covariance[0] = 1e-3 # x
147 odom.pose.covariance[7] = 1e-3 # y
148 odom.pose.covariance[14] = 1e6 # z
149 odom.pose.covariance[21] = 1e6 # rotation about X axis
150 odom.pose.covariance[28] = 1e6 # rotation about Y axis
151 odom.pose.covariance[35] = 0.1 # rotation about Z axis
154 odom.child_frame_id = "base_footprint"
155 odom.twist.twist.linear.x = speed_trans
156 odom.twist.twist.linear.y = 0.0
157 odom.twist.twist.angular.z = speed_rot
158 odom.twist.covariance[0] = 1e-3 # x
159 odom.twist.covariance[7] = 1e-3 # y
160 odom.twist.covariance[14] = 1e6 # z
161 odom.twist.covariance[21] = 1e6 # rotation about X axis
162 odom.twist.covariance[28] = 1e6 # rotation about Y axis
163 odom.twist.covariance[35] = 0.1 # rotation about Z axis
165 # publish the message
166 self.pub_odom.publish(odom)
169 def set_speed(self, trans, rot):
170 i2c_write_reg(0x50, 0x50, struct.pack(">ff", trans, rot))
172 def cmdVelReceived(self, msg):
174 rot = msg.angular.z # rad/s
175 self.set_speed(trans, rot)
177 # http://rn-wissen.de/wiki/index.php/Sensorarten#Sharp_GP2D12
178 def get_dist_ir(self, num):
180 s = struct.pack("B", num)
190 val = struct.unpack(">H", s)[0]
193 def get_dist_srf(self, num):
195 s = struct.pack("B", num)
205 return struct.unpack(">H", s)[0]/1000.0
207 def send_range(self, pub, frame_id, typ, dist, min_range, max_range, fov_deg):
209 msg.header.stamp = rospy.Time.now()
210 msg.header.frame_id = frame_id
211 msg.radiation_type = typ
212 msg.field_of_view = fov_deg*pi/180
213 msg.min_range = min_range
214 msg.max_range = max_range
218 def get_dist_left(self):
219 if self.pub_range_left.get_num_connections() > 0:
220 dist = 30.553/(self.get_dist_ir(0x1) - -67.534)
221 self.send_range(self.pub_range_left, "ir_left", Range.INFRARED, dist, 0.04, 0.3, 5)
223 def get_dist_right(self):
224 if self.pub_range_right.get_num_connections() > 0:
225 dist = 17.4/(self.get_dist_ir(0x3) - 69)
226 self.send_range(self.pub_range_right, "ir_right", Range.INFRARED, dist, 0.04, 0.3, 5)
228 def get_dist_forward(self):
229 if self.pub_range_fwd.get_num_connections() > 0:
230 dist = self.get_dist_srf(0x5)
231 self.send_range(self.pub_range_fwd, "sonar_forward", Range.ULTRASOUND, dist, 0.04, 6, 60)
233 def get_dist_backward(self):
234 if self.pub_range_bwd.get_num_connections() > 0:
235 dist = self.get_dist_srf(0x7)
236 self.send_range(self.pub_range_bwd, "sonar_backward", Range.ULTRASOUND, dist, 0.04, 6, 60)
239 if __name__ == "__main__":