2 # -*- coding: iso-8859-15 -*-
10 from i2c import i2c, i2c_write_reg, i2c_read_reg
12 from geometry_msgs.msg import Twist
13 from nav_msgs.msg import Odometry
14 from diagnostic_msgs.msg import DiagnosticArray, DiagnosticStatus, KeyValue
15 from sensor_msgs.msg import Imu, Range
16 from wild_thumper.msg import LedStripe
21 def __init__(self, bus, device, num_leds):
22 self.spi = spidev.SpiDev()
23 self.spi.open(bus, device)
25 self.spi.max_speed_hz=int(2e6)
26 self.num_leds = num_leds
28 self.l = [(0, 0, 0)] * num_leds
31 def set(self, i, red=0, green=0, blue=0):
32 if red > 127 or green > 127 or blue >> 127 or red < 0 or green < 0 or blue < 0:
33 raise Exception("Bad RGB Value")
34 self.l[i] = (red, green, blue)
37 self.spi.writebytes([0x0 for i in range((self.num_leds+31)/32)])
41 for i in range(self.num_leds):
42 red, green, blue = self.l[i]
43 l.append(0x80 | green)
46 self.spi.writebytes(l)
51 rospy.init_node('wild_thumper')
52 prctl.set_name("wild_thumper")
53 enable_odom_tf = rospy.get_param("~enable_odom_tf", True)
55 self.tf_broadcaster = tf.broadcaster.TransformBroadcaster()
57 self.tf_broadcaster = None
58 self.pub_odom = rospy.Publisher("odom", Odometry, queue_size=16)
59 self.pub_diag = rospy.Publisher("diagnostics", DiagnosticArray, queue_size=16)
60 self.pub_range_fwd = rospy.Publisher("range_forward", Range, queue_size=16)
61 self.pub_range_bwd = rospy.Publisher("range_backward", Range, queue_size=16)
62 self.pub_range_left = rospy.Publisher("range_left", Range, queue_size=16)
63 self.pub_range_right = rospy.Publisher("range_right", Range, queue_size=16)
66 rospy.loginfo("Init done")
67 i2c_write_reg(0x50, 0x90, struct.pack("BB", 1, 1)) # switch direction
68 self.handicap_last = (-1, -1)
69 self.pStripe = LPD8806(1, 0, 12)
70 rospy.Subscriber("cmd_vel_out", Twist, self.cmdVelReceived)
71 rospy.Subscriber("imu", Imu, self.imuReceived)
72 rospy.Subscriber("led_stripe", LedStripe, self.led_stripe_received)
76 rate = rospy.Rate(20.0)
77 sleep(3) # wait 3s for ros to register and establish all subscriber connections before sending reset diag
78 reset_val = self.get_reset()
79 rospy.loginfo("Reset Status: 0x%x" % reset_val)
81 while not rospy.is_shutdown():
82 #print struct.unpack(">B", i2c_read_reg(0x50, 0xA2, 1))[0] # count test
87 self.get_dist_forward()
90 self.get_dist_backward()
93 if self.cmd_vel != None:
94 self.set_speed(self.cmd_vel[0], self.cmd_vel[1])
98 def set_motor_handicap(self, front, aft): # percent
99 if front > 100: front = 100
100 if aft > 100: aft = 100
101 if self.handicap_last != (front, aft):
102 i2c_write_reg(0x50, 0x94, struct.pack(">bb", front, aft))
103 self.handicap_last = (front, aft)
105 def imuReceived(self, msg):
106 (roll, pitch, yaw) = tf.transformations.euler_from_quaternion(msg.orientation.__getstate__())
107 if pitch > 30*pi/180:
108 val = (100.0/60)*abs(pitch)*180/pi
109 self.set_motor_handicap(int(val), 0)
110 elif pitch < -30*pi/180:
111 val = (100.0/60)*abs(pitch)*180/pi
112 self.set_motor_handicap(0, int(val))
114 self.set_motor_handicap(0, 0)
117 reset = struct.unpack(">B", i2c_read_reg(0x50, 0xA0, 1))[0]
119 msg = DiagnosticArray()
120 msg.header.stamp = rospy.Time.now()
121 stat = DiagnosticStatus()
122 stat.name = "Reset reason"
123 stat.level = DiagnosticStatus.ERROR if reset & 0x0c else DiagnosticStatus.OK
124 stat.message = "0x%02x" % reset
126 wdrf = bool(reset & (1 << 3))
127 if wdrf: rospy.loginfo("Watchdog Reset")
128 borf = bool(reset & (1 << 2))
129 if borf: rospy.loginfo("Brown-out Reset Flag")
130 extrf = bool(reset & (1 << 1))
131 if extrf: rospy.loginfo("External Reset Flag")
132 porf = bool(reset & (1 << 0))
133 if porf: rospy.loginfo("Power-on Reset Flag")
134 stat.values.append(KeyValue("Watchdog Reset Flag", str(wdrf)))
135 stat.values.append(KeyValue("Brown-out Reset Flag", str(borf)))
136 stat.values.append(KeyValue("External Reset Flag", str(extrf)))
137 stat.values.append(KeyValue("Power-on Reset Flag", str(porf)))
139 msg.status.append(stat)
140 self.pub_diag.publish(msg)
144 def get_tle_err(self):
145 err = struct.unpack(">B", i2c_read_reg(0x50, 0xA1, 1))[0]
147 msg = DiagnosticArray()
148 msg.header.stamp = rospy.Time.now()
149 stat = DiagnosticStatus()
150 stat.name = "Motor: Error Status"
151 stat.level = DiagnosticStatus.ERROR if err else DiagnosticStatus.OK
152 stat.message = "0x%02x" % err
154 stat.values.append(KeyValue("aft left", str(bool(err & (1 << 0)))))
155 stat.values.append(KeyValue("front left", str(bool(err & (1 << 1)))))
156 stat.values.append(KeyValue("front right", str(bool(err & (1 << 2)))))
157 stat.values.append(KeyValue("aft right", str(bool(err & (1 << 3)))))
159 msg.status.append(stat)
160 self.pub_diag.publish(msg)
162 def get_voltage(self):
163 volt = struct.unpack(">h", i2c_read_reg(0x52, 0x09, 2))[0]/100.0
165 msg = DiagnosticArray()
166 msg.header.stamp = rospy.Time.now()
167 stat = DiagnosticStatus()
168 stat.name = "Voltage"
169 stat.level = DiagnosticStatus.ERROR if volt < 7 else DiagnosticStatus.OK
170 stat.message = "%.2fV" % volt
172 msg.status.append(stat)
173 self.pub_diag.publish(msg)
177 speed_trans, speed_rot, posx, posy, angle = struct.unpack(">fffff", i2c_read_reg(0x50, 0x38, 20))
178 current_time = rospy.Time.now()
180 # since all odometry is 6DOF we'll need a quaternion created from yaw
181 odom_quat = tf.transformations.quaternion_from_euler(0, 0, angle)
183 # first, we'll publish the transform over tf
184 if self.tf_broadcaster is not None:
185 self.tf_broadcaster.sendTransform((posx, posy, 0.0), odom_quat, current_time, "base_footprint", "odom")
187 # next, we'll publish the odometry message over ROS
189 odom.header.stamp = current_time
190 odom.header.frame_id = "odom"
193 odom.pose.pose.position.x = posx
194 odom.pose.pose.position.y = posy
195 odom.pose.pose.position.z = 0.0
196 odom.pose.pose.orientation.x = odom_quat[0]
197 odom.pose.pose.orientation.y = odom_quat[1]
198 odom.pose.pose.orientation.z = odom_quat[2]
199 odom.pose.pose.orientation.w = odom_quat[3]
200 odom.pose.covariance[0] = 1e-3 # x
201 odom.pose.covariance[7] = 1e-3 # y
202 odom.pose.covariance[14] = 1e6 # z
203 odom.pose.covariance[21] = 1e6 # rotation about X axis
204 odom.pose.covariance[28] = 1e6 # rotation about Y axis
205 odom.pose.covariance[35] = 0.03 # rotation about Z axis
208 odom.child_frame_id = "base_footprint"
209 odom.twist.twist.linear.x = speed_trans
210 odom.twist.twist.linear.y = 0.0
211 odom.twist.twist.angular.z = speed_rot
212 odom.twist.covariance[0] = 1e-3 # x
213 odom.twist.covariance[7] = 1e-3 # y
214 odom.twist.covariance[14] = 1e6 # z
215 odom.twist.covariance[21] = 1e6 # rotation about X axis
216 odom.twist.covariance[28] = 1e6 # rotation about Y axis
217 odom.twist.covariance[35] = 0.03 # rotation about Z axis
219 # publish the message
220 self.pub_odom.publish(odom)
223 def set_speed(self, trans, rot):
224 i2c_write_reg(0x50, 0x50, struct.pack(">ff", trans, rot))
226 def cmdVelReceived(self, msg):
227 self.cmd_vel = (msg.linear.x, msg.angular.z) # commit speed on next update cycle
229 # http://rn-wissen.de/wiki/index.php/Sensorarten#Sharp_GP2D12
230 def get_dist_ir(self, num):
232 s = struct.pack("B", num)
242 val = struct.unpack(">H", s)[0]
245 def start_dist_srf(self, num):
247 s = struct.pack("B", num)
251 def read_dist_srf(self, num):
252 return struct.unpack(">H", i2c_read_reg(0x52, num, 2))[0]/1000.0
254 def send_range(self, pub, frame_id, typ, dist, min_range, max_range, fov_deg):
256 msg.header.stamp = rospy.Time.now()
257 msg.header.frame_id = frame_id
258 msg.radiation_type = typ
259 msg.field_of_view = fov_deg*pi/180
260 msg.min_range = min_range
261 msg.max_range = max_range
265 def get_dist_left(self):
266 if self.pub_range_left.get_num_connections() > 0:
267 dist = self.get_dist_ir(0x1)
269 self.send_range(self.pub_range_left, "ir_left", Range.INFRARED, 30.553/(dist - -67.534), 0.04, 0.3, 1)
271 def get_dist_right(self):
272 if self.pub_range_right.get_num_connections() > 0:
273 dist = self.get_dist_ir(0x3)
275 self.send_range(self.pub_range_right, "ir_right", Range.INFRARED, 17.4/(dist - 69), 0.04, 0.3, 1)
277 def get_dist_forward(self):
278 if self.pub_range_fwd.get_num_connections() > 0:
279 dist = self.read_dist_srf(0x15)
280 self.send_range(self.pub_range_fwd, "sonar_forward", Range.ULTRASOUND, dist, 0.04, 6, 40)
281 self.start_dist_srf(0x5) # get next value
283 def get_dist_backward(self):
284 if self.pub_range_bwd.get_num_connections() > 0:
285 dist = self.read_dist_srf(0x17)
286 self.send_range(self.pub_range_bwd, "sonar_backward", Range.ULTRASOUND, dist, 0.04, 6, 40)
287 self.start_dist_srf(0x7) # get next value
289 def led_stripe_received(self, msg):
291 self.pStripe.set(led.num, red=led.red, green=led.green, blue=led.blue)
292 self.pStripe.update()
295 if __name__ == "__main__":