]> defiant.homedns.org Git - ros_wild_thumper.git/blob - scripts/move_base.py
Merge branch 'master' of ssh://vontaene/home/erik_alt/git/ros_wild_thumper
[ros_wild_thumper.git] / scripts / move_base.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
13 WHEEL_DIST = 0.248
14
15 class MoveBase:
16         def __init__(self):
17                 rospy.init_node('wild_thumper_move_base')
18                 rospy.Subscriber("cmd_vel", Twist, self.cmdVelReceived)
19                 self.tf_broadcaster = tf.broadcaster.TransformBroadcaster()
20                 self.pub_odom = rospy.Publisher("odom", Odometry, queue_size=16)
21                 self.pub_diag = rospy.Publisher("diagnostics", DiagnosticArray, queue_size=16)
22                 self.set_speed(0, 0)
23                 rospy.loginfo("Init done")
24                 i2c_write_reg(0x50, 0x90, struct.pack("BB", 1, 1)) # switch direction
25                 self.run()
26         
27         def run(self):
28                 rate = rospy.Rate(20.0)
29                 reset_val = self.get_reset()
30                 rospy.loginfo("Reset Status: 0x%x" % reset_val)
31                 while not rospy.is_shutdown():
32                         #print struct.unpack(">B", i2c_read_reg(0x50, 0xA2, 1))[0] # count test
33                         self.get_tle_err()
34                         self.get_odom()
35                         self.get_voltage()
36                         #self.get_dist_forward()
37                         #self.get_dist_backward()
38                         #self.get_dist_left()
39                         #self.get_dist_right()
40                         rate.sleep()
41
42         def get_reset(self):
43                 reset = struct.unpack(">B", i2c_read_reg(0x50, 0xA0, 1))[0]
44
45                 msg = DiagnosticArray()
46                 msg.header.stamp = rospy.Time.now()
47                 stat = DiagnosticStatus()
48                 stat.name = "Reset reason"
49                 stat.level = DiagnosticStatus.ERROR if reset & 0x0c else DiagnosticStatus.OK
50                 stat.message = "0x%02x" % reset
51
52                 stat.values.append(KeyValue("Watchdog Reset Flag", str(bool(reset & (1 << 3)))))
53                 stat.values.append(KeyValue("Brown-out Reset Flag", str(bool(reset & (1 << 2)))))
54                 stat.values.append(KeyValue("External Reset Flag", str(bool(reset & (1 << 1)))))
55                 stat.values.append(KeyValue("Power-on Reset Flag", str(bool(reset & (1 << 0)))))
56
57                 msg.status.append(stat)
58                 self.pub_diag.publish(msg)
59                 return reset
60
61
62         def get_tle_err(self):
63                 err = struct.unpack(">B", i2c_read_reg(0x50, 0xA1, 1))[0]
64                 
65                 msg = DiagnosticArray()
66                 msg.header.stamp = rospy.Time.now()
67                 stat = DiagnosticStatus()
68                 stat.name = "Motor: Error Status"
69                 stat.level = DiagnosticStatus.ERROR if err else DiagnosticStatus.OK
70                 stat.message = "0x%02x" % err
71
72                 stat.values.append(KeyValue("Motor 1", str(bool(err & (1 << 0)))))
73                 stat.values.append(KeyValue("Motor 2", str(bool(err & (1 << 1)))))
74                 stat.values.append(KeyValue("Motor 3", str(bool(err & (1 << 2)))))
75                 stat.values.append(KeyValue("Motor 4", str(bool(err & (1 << 3)))))
76
77                 msg.status.append(stat)
78                 self.pub_diag.publish(msg)
79         
80         def get_voltage(self):
81                 volt = struct.unpack(">h", i2c_read_reg(0x52, 0x09, 2))[0]/100.0
82
83                 msg = DiagnosticArray()
84                 msg.header.stamp = rospy.Time.now()
85                 stat = DiagnosticStatus()
86                 stat.name = "Voltage"
87                 stat.level = DiagnosticStatus.ERROR if volt < 6 else DiagnosticStatus.OK
88                 stat.message = "%.2fV" % volt
89
90                 msg.status.append(stat)
91                 self.pub_diag.publish(msg)
92
93
94         def get_odom(self):
95                 posx, posy, angle = struct.unpack(">fff", i2c_read_reg(0x50, 0x40, 12))
96                 speed_trans, speed_rot = struct.unpack(">ff", i2c_read_reg(0x50, 0x38, 8))
97                 current_time = rospy.Time.now()
98
99                 # since all odometry is 6DOF we'll need a quaternion created from yaw
100                 odom_quat = tf.transformations.quaternion_from_euler(0, 0, angle)
101
102                 # first, we'll publish the transform over tf
103                 self.tf_broadcaster.sendTransform((posx, posy, 0.0), odom_quat, current_time, "base_link", "odom")
104
105                 # next, we'll publish the odometry message over ROS
106                 odom = Odometry()
107                 odom.header.stamp = current_time
108                 odom.header.frame_id = "/odom"
109
110                 # set the position
111                 odom.pose.pose.position.x = posx
112                 odom.pose.pose.position.y = posy
113                 odom.pose.pose.position.z = 0.0
114                 odom.pose.pose.orientation.x = odom_quat[0]
115                 odom.pose.pose.orientation.y = odom_quat[1]
116                 odom.pose.pose.orientation.z = odom_quat[2]
117                 odom.pose.pose.orientation.w = odom_quat[3]
118
119                 # set the velocity
120                 odom.child_frame_id = "base_link"
121                 odom.twist.twist.linear.x = speed_trans
122                 odom.twist.twist.linear.y = 0.0
123                 odom.twist.twist.angular.z = speed_rot
124
125                 # publish the message
126                 self.pub_odom.publish(odom)
127
128         
129         def set_speed(self, trans, rot):
130                 i2c_write_reg(0x50, 0x50, struct.pack(">ff", trans, rot))
131
132         def cmdVelReceived(self, msg):
133                 trans = msg.linear.x
134                 rot = msg.angular.z # rad/s
135                 self.set_speed(trans, rot)
136
137         # http://rn-wissen.de/wiki/index.php/Sensorarten#Sharp_GP2D12
138         def get_dist_ir(self, num):
139                 dev = i2c(0x52)
140                 s = struct.pack("B", num)
141                 dev.write(s)
142                 dev.close()
143
144                 sleep(2e-6)
145
146                 dev = i2c(0x52)
147                 s = dev.read(2)
148                 dev.close()
149
150                 val = struct.unpack(">H", s)[0]
151                 return 15221/(val - -276.42)/100;
152         
153         def get_dist_srf(self, num):
154                 dev = i2c(0x52)
155                 s = struct.pack("B", num)
156                 dev.write(s)
157                 dev.close()
158
159                 sleep(50e-3)
160
161                 dev = i2c(0x52)
162                 s = dev.read(2)
163                 dev.close()
164
165                 return struct.unpack(">H", s)[0]/1000.0
166
167         def get_dist_left(self):
168                 dist = self.get_dist_ir(0x1)
169
170         def get_dist_right(self):
171                  dist = self.get_dist_ir(0x3)
172
173         def get_dist_forward(self):
174                 dist = self.get_dist_srf(0x5)
175
176         def get_dist_backward(self):
177                 dist = self.get_dist_srf(0x7)
178
179
180 if __name__ == "__main__":
181         MoveBase()