00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034 import roslib; roslib.load_manifest('nxt_controllers')
00035 import rospy
00036 import math
00037 import thread
00038 import tf
00039 from PyKDL import *
00040 from sensor_msgs.msg import JointState
00041 from nav_msgs.msg import Odometry
00042 from nxt_msgs.msg import Range, JointCommand
00043 from tf_conversions import posemath
00044
00045 PUBLISH_TF = False
00046
00047 class BaseOdometry:
00048 def __init__(self):
00049 self.initialized = False
00050
00051 self.ns =rospy.get_namespace() + 'base_parameters/'
00052
00053 self.l_joint = rospy.get_param(self.ns +'l_wheel_joint')
00054 self.r_joint = rospy.get_param(self.ns +'r_wheel_joint')
00055
00056 self.wheel_radius = rospy.get_param(self.ns +'wheel_radius', 0.022)
00057 self.wheel_basis = rospy.get_param(self.ns +'wheel_basis', 0.055)
00058
00059
00060 rospy.Subscriber('joint_states', JointState, self.jnt_state_cb)
00061
00062
00063 if PUBLISH_TF:
00064 self.br = tf.TransformBroadcaster()
00065
00066
00067 self.pub = rospy.Publisher('odom', Odometry)
00068
00069 self.initialized = False
00070
00071 def jnt_state_cb(self, msg):
00072
00073 position = {}
00074 for name, pos in zip(msg.name, msg.position):
00075 position[name] = pos
00076
00077
00078 if not self.initialized:
00079 self.r_pos = position[self.r_joint]
00080 self.l_pos = position[self.l_joint]
00081 self.pose = Frame()
00082 self.initialized = True
00083 else:
00084 delta_r_pos = position[self.r_joint] - self.r_pos
00085 delta_l_pos = position[self.l_joint] - self.l_pos
00086 delta_trans = (delta_r_pos + delta_l_pos)*self.wheel_radius/2.0
00087 delta_rot = (delta_r_pos - delta_l_pos)*self.wheel_radius/(2.0*self.wheel_basis)
00088 twist = Twist(Vector(delta_trans, 0, 0), Vector(0, 0, delta_rot))
00089 self.r_pos = position[self.r_joint]
00090 self.l_pos = position[self.l_joint]
00091 self.pose = addDelta(self.pose, self.pose.M * twist)
00092 if PUBLISH_TF:
00093 self.br.sendTransform(self.pose.p, self.pose.M.GetQuaternion(), rospy.Time.now(), 'base_link', 'odom')
00094
00095
00096 self.rot_covar = 1.0
00097 if delta_rot == 0:
00098 self.rot_covar = 0.00000000001
00099
00100 odom = Odometry()
00101 odom.header.stamp = rospy.Time.now()
00102 odom.pose.pose = posemath.toMsg(self.pose)
00103 odom.pose.covariance = [0.00001, 0, 0, 0, 0, 0,
00104 0, 0.00001, 0, 0, 0, 0,
00105 0, 0, 10.0000, 0, 0, 0,
00106 0, 0, 0, 1.00000, 0, 0,
00107 0, 0, 0, 0, 1.00000, 0,
00108 0, 0, 0, 0, 0, self.rot_covar]
00109 self.pub.publish(odom)
00110
00111 def main():
00112 rospy.init_node('nxt_base_odometry')
00113 base_odometry = BaseOdometry()
00114 rospy.spin()
00115
00116
00117
00118 if __name__ == '__main__':
00119 main()