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
00035 import roslib; roslib.load_manifest('nxt_controllers')
00036 import rospy
00037 import math
00038 import thread
00039 from sensor_msgs.msg import JointState
00040 from geometry_msgs.msg import Twist
00041 from nxt_msgs.msg import Range, JointCommand
00042
00043 class BaseController:
00044 def __init__(self):
00045 self.initialized = False
00046 self.vel_rot_desi = 0
00047 self.vel_trans_desi = 0
00048 self.vel_trans = 0
00049 self.vel_rot = 0
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 self.wheel_radius = rospy.get_param(self.ns +'wheel_radius', 0.022)
00056 self.wheel_basis = rospy.get_param(self.ns +'wheel_basis', 0.055)
00057 self.vel_to_eff = rospy.get_param(self.ns +'vel_to_eff', 0.5)
00058 self.k_rot = 0.075/self.vel_to_eff
00059 self.k_trans = 0.055/self.vel_to_eff
00060
00061
00062 self.pub = rospy.Publisher('joint_command', JointCommand)
00063 rospy.Subscriber('joint_states', JointState, self.jnt_state_cb)
00064
00065
00066 rospy.Subscriber('cmd_vel', Twist, self.cmd_vel_cb)
00067
00068 def cmd_vel_cb(self, msg):
00069 self.vel_rot_desi = msg.angular.z
00070 self.vel_trans_desi = msg.linear.x
00071
00072 def jnt_state_cb(self, msg):
00073 velocity = {}
00074 for name, vel in zip(msg.name, msg.velocity):
00075 velocity[name] = vel
00076
00077
00078 self.vel_trans = 0.5*self.vel_trans + 0.5*(velocity[self.r_joint] + velocity[self.l_joint])*self.wheel_radius/2.0
00079 self.vel_rot = 0.5*self.vel_rot + 0.5*(velocity[self.r_joint] - velocity[self.l_joint])*self.wheel_radius/(2.0*self.wheel_basis)
00080
00081
00082 vel_trans = self.vel_trans_desi + self.k_trans*(self.vel_trans_desi - self.vel_trans)
00083 vel_rot = self.vel_rot_desi + self.k_rot*(self.vel_rot_desi - self.vel_rot)
00084
00085
00086 l_cmd = JointCommand()
00087 l_cmd.name = self.l_joint
00088 l_cmd.effort = self.vel_to_eff*(vel_trans/self.wheel_radius - vel_rot*self.wheel_basis/self.wheel_radius)
00089 self.pub.publish(l_cmd)
00090
00091 r_cmd = JointCommand()
00092 r_cmd.name = self.r_joint
00093 r_cmd.effort = self.vel_to_eff*(vel_trans/self.wheel_radius + vel_rot*self.wheel_basis/self.wheel_radius)
00094 self.pub.publish(r_cmd)
00095
00096
00097 def main():
00098 rospy.init_node('nxt_base_controller')
00099 base_controller = BaseController()
00100 rospy.spin()
00101
00102
00103
00104 if __name__ == '__main__':
00105 main()