]> defiant.homedns.org Git - ros_wild_thumper.git/blob - scripts/wt_node.py
umbmark correction
[ros_wild_thumper.git] / scripts / wt_node.py
1 #!/usr/bin/env python
2 # -*- coding: iso-8859-15 -*-
3
4 import rospy
5 import tf
6 import struct
7 from i2c import *
8 from math import *
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
13
14 WHEEL_DIST = 0.248
15
16 class MoveBase:
17         def __init__(self):
18                 rospy.init_node('wild_thumper')
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)
22                 if enable_odom_tf:
23                         self.tf_broadcaster = tf.broadcaster.TransformBroadcaster()
24                 else:
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)
32                 self.set_speed(0, 0)
33                 rospy.loginfo("Init done")
34                 i2c_write_reg(0x50, 0x90, struct.pack("BB", 1, 1)) # switch direction
35                 self.handicap_last = (-1, -1)
36                 self.run()
37         
38         def run(self):
39                 rate = rospy.Rate(20.0)
40                 reset_val = self.get_reset()
41                 rospy.loginfo("Reset Status: 0x%x" % reset_val)
42                 i = 0
43                 while not rospy.is_shutdown():
44                         #print struct.unpack(">B", i2c_read_reg(0x50, 0xA2, 1))[0] # count test
45                         self.get_tle_err()
46                         self.get_odom()
47                         self.get_voltage()
48                         if i % 2:
49                                 self.get_dist_forward()
50                                 self.get_dist_backward()
51                                 self.get_dist_left()
52                                 self.get_dist_right()
53                         i+=1
54                         rate.sleep()
55
56         def set_motor_handicap(self, front, aft): # percent
57                 if self.handicap_last != (front, aft):
58                         i2c_write_reg(0x50, 0x94, struct.pack(">bb", front, aft))
59                         self.handicap_last = (front, aft)
60
61         def imuReceived(self, msg):
62                 (roll, pitch, yaw) = tf.transformations.euler_from_quaternion(msg.orientation.__getstate__())
63                 if pitch > 30*pi/180:
64                         val = (100.0/65)*abs(pitch)*180/pi
65                         self.set_motor_handicap(0, int(val))
66                 elif pitch < -30*pi/180:
67                         val = (100.0/65)*abs(pitch)*180/pi
68                         self.set_motor_handicap(int(val), 0)
69                 else:
70                         self.set_motor_handicap(0, 0)
71
72         def get_reset(self):
73                 reset = struct.unpack(">B", i2c_read_reg(0x50, 0xA0, 1))[0]
74
75                 msg = DiagnosticArray()
76                 msg.header.stamp = rospy.Time.now()
77                 stat = DiagnosticStatus()
78                 stat.name = "Reset reason"
79                 stat.level = DiagnosticStatus.ERROR if reset & 0x0c else DiagnosticStatus.OK
80                 stat.message = "0x%02x" % reset
81
82                 stat.values.append(KeyValue("Watchdog Reset Flag", str(bool(reset & (1 << 3)))))
83                 stat.values.append(KeyValue("Brown-out Reset Flag", str(bool(reset & (1 << 2)))))
84                 stat.values.append(KeyValue("External Reset Flag", str(bool(reset & (1 << 1)))))
85                 stat.values.append(KeyValue("Power-on Reset Flag", str(bool(reset & (1 << 0)))))
86
87                 msg.status.append(stat)
88                 self.pub_diag.publish(msg)
89                 return reset
90
91
92         def get_tle_err(self):
93                 err = struct.unpack(">B", i2c_read_reg(0x50, 0xA1, 1))[0]
94                 
95                 msg = DiagnosticArray()
96                 msg.header.stamp = rospy.Time.now()
97                 stat = DiagnosticStatus()
98                 stat.name = "Motor: Error Status"
99                 stat.level = DiagnosticStatus.ERROR if err else DiagnosticStatus.OK
100                 stat.message = "0x%02x" % err
101
102                 stat.values.append(KeyValue("aft left", str(bool(err & (1 << 0)))))
103                 stat.values.append(KeyValue("front left", str(bool(err & (1 << 1)))))
104                 stat.values.append(KeyValue("front right", str(bool(err & (1 << 2)))))
105                 stat.values.append(KeyValue("aft right", str(bool(err & (1 << 3)))))
106
107                 msg.status.append(stat)
108                 self.pub_diag.publish(msg)
109         
110         def get_voltage(self):
111                 volt = struct.unpack(">h", i2c_read_reg(0x52, 0x09, 2))[0]/100.0
112
113                 msg = DiagnosticArray()
114                 msg.header.stamp = rospy.Time.now()
115                 stat = DiagnosticStatus()
116                 stat.name = "Voltage"
117                 stat.level = DiagnosticStatus.ERROR if volt < 7 else DiagnosticStatus.OK
118                 stat.message = "%.2fV" % volt
119
120                 msg.status.append(stat)
121                 self.pub_diag.publish(msg)
122
123
124         def get_odom(self):
125                 posx, posy, angle = struct.unpack(">fff", i2c_read_reg(0x50, 0x40, 12))
126                 speed_trans, speed_rot = struct.unpack(">ff", i2c_read_reg(0x50, 0x38, 8))
127                 current_time = rospy.Time.now()
128
129                 # since all odometry is 6DOF we'll need a quaternion created from yaw
130                 odom_quat = tf.transformations.quaternion_from_euler(0, 0, angle)
131
132                 # first, we'll publish the transform over tf
133                 if self.tf_broadcaster is not None:
134                         self.tf_broadcaster.sendTransform((posx, posy, 0.0), odom_quat, current_time, "base_footprint", "odom")
135
136                 # next, we'll publish the odometry message over ROS
137                 odom = Odometry()
138                 odom.header.stamp = current_time
139                 odom.header.frame_id = "odom"
140
141                 # set the position
142                 odom.pose.pose.position.x = posx
143                 odom.pose.pose.position.y = posy
144                 odom.pose.pose.position.z = 0.0
145                 odom.pose.pose.orientation.x = odom_quat[0]
146                 odom.pose.pose.orientation.y = odom_quat[1]
147                 odom.pose.pose.orientation.z = odom_quat[2]
148                 odom.pose.pose.orientation.w = odom_quat[3]
149                 odom.pose.covariance[0] = 1e-3 # x
150                 odom.pose.covariance[7] = 1e-3 # y
151                 odom.pose.covariance[14] = 1e6 # z
152                 odom.pose.covariance[21] = 1e6 # rotation about X axis
153                 odom.pose.covariance[28] = 1e6 # rotation about Y axis
154                 odom.pose.covariance[35] = 0.03 # rotation about Z axis
155
156                 # set the velocity
157                 odom.child_frame_id = "base_footprint"
158                 odom.twist.twist.linear.x = speed_trans
159                 odom.twist.twist.linear.y = 0.0
160                 odom.twist.twist.angular.z = speed_rot
161                 odom.twist.covariance[0] = 1e-3 # x
162                 odom.twist.covariance[7] = 1e-3 # y
163                 odom.twist.covariance[14] = 1e6 # z
164                 odom.twist.covariance[21] = 1e6 # rotation about X axis
165                 odom.twist.covariance[28] = 1e6 # rotation about Y axis
166                 odom.twist.covariance[35] = 0.03 # rotation about Z axis
167
168                 # publish the message
169                 self.pub_odom.publish(odom)
170
171         
172         def set_speed(self, trans, rot):
173                 i2c_write_reg(0x50, 0x50, struct.pack(">ff", trans, rot))
174
175         def cmdVelReceived(self, msg):
176                 trans = msg.linear.x
177                 rot = msg.angular.z # rad/s
178                 self.set_speed(trans, rot)
179
180         # http://rn-wissen.de/wiki/index.php/Sensorarten#Sharp_GP2D12
181         def get_dist_ir(self, num):
182                 dev = i2c(0x52)
183                 s = struct.pack("B", num)
184                 dev.write(s)
185                 dev.close()
186
187                 sleep(2e-6)
188
189                 dev = i2c(0x52)
190                 s = dev.read(2)
191                 dev.close()
192
193                 val = struct.unpack(">H", s)[0]
194                 return val
195         
196         def get_dist_srf(self, num):
197                 dev = i2c(0x52)
198                 s = struct.pack("B", num)
199                 dev.write(s)
200                 dev.close()
201
202                 sleep(50e-3)
203
204                 dev = i2c(0x52)
205                 s = dev.read(2)
206                 dev.close()
207
208                 return struct.unpack(">H", s)[0]/1000.0
209
210         def send_range(self, pub, frame_id, typ, dist, min_range, max_range, fov_deg):
211                 msg = Range()
212                 msg.header.stamp = rospy.Time.now()
213                 msg.header.frame_id = frame_id
214                 msg.radiation_type = typ
215                 msg.field_of_view = fov_deg*pi/180
216                 msg.min_range = min_range
217                 msg.max_range = max_range
218                 msg.range = dist
219                 pub.publish(msg)
220
221         def get_dist_left(self):
222                 if self.pub_range_left.get_num_connections() > 0:
223                         dist = 30.553/(self.get_dist_ir(0x1) - -67.534)
224                         self.send_range(self.pub_range_left, "ir_left", Range.INFRARED, dist, 0.04, 0.3, 5)
225
226         def get_dist_right(self):
227                 if self.pub_range_right.get_num_connections() > 0:
228                         dist = 17.4/(self.get_dist_ir(0x3) - 69)
229                         self.send_range(self.pub_range_right, "ir_right", Range.INFRARED, dist, 0.04, 0.3, 5)
230
231         def get_dist_forward(self):
232                 if self.pub_range_fwd.get_num_connections() > 0:
233                         dist = self.get_dist_srf(0x5)
234                         self.send_range(self.pub_range_fwd, "sonar_forward", Range.ULTRASOUND, dist, 0.04, 6, 60)
235
236         def get_dist_backward(self):
237                 if self.pub_range_bwd.get_num_connections() > 0:
238                         dist = self.get_dist_srf(0x7)
239                         self.send_range(self.pub_range_bwd, "sonar_backward", Range.ULTRASOUND, dist, 0.04, 6, 60)
240                 
241
242 if __name__ == "__main__":
243         MoveBase()