2_check_velocity.py
Go to the documentation of this file.
00001 #!/usr/bin/env python
00002 
00003 import rospy
00004 from math import sin, pi
00005 from nav_msgs.msg import Odometry
00006 from geometry_msgs.msg import Twist, Pose2D
00007 from tf.transformations import euler_from_quaternion
00008 
00009 #
00010 # reset robot pose and restart spur_contrller to start from origin
00011 #
00012 # use laser: roslaunch base_controller.launch use_base_odom:=false
00013 # use odom : roslaunch base_controller.launch use_base_odom:=true
00014 # 
00015 # find maximum velocity of the robot
00016 
00017 odom = {}
00018 curr_pose = None
00019 prev_pose = None
00020 tm_prev = None
00021 
00022 def odom_cb(msg):
00023     global odom
00024     odom[msg.header.frame_id] = msg
00025 def pose_cb(msg):
00026     global curr_pose
00027     curr_pose = msg
00028 
00029 def check_odom():
00030     global odom, curr_pose, prev_pose, tm_prev
00031     tm_curr = rospy.Time.now()
00032     t1_x = odom['map'].twist.twist.linear.x
00033     t1_y = odom['map'].twist.twist.linear.y
00034     t1_z = odom['map'].twist.twist.angular.z
00035     src = ""
00036     t2_x = t2_y = t2_z = 0
00037     if odom.has_key('odom'):
00038         t2_x = odom['odom'].twist.twist.linear.x
00039         t2_y = odom['odom'].twist.twist.linear.y
00040         t2_z = odom['odom'].twist.twist.angular.z
00041         src = "base "
00042     elif tm_prev and prev_pose:
00043         dt = (tm_curr - tm_prev).to_sec()
00044         t2_x = (curr_pose.x - prev_pose.x)/dt
00045         t2_y = (curr_pose.y - prev_pose.y)/dt
00046         t2_z = (curr_pose.theta  - prev_pose.theta)/dt
00047         src = "laser"
00048     tm_prev = tm_curr
00049     prev_pose = curr_pose
00050     print("ground_truth %7.3f %7.3f %7.3f, odom(%s) %7.3f %7.3f %7.3f, diff %7.3f %7.3f %7.3f" % (t1_x, t1_y, t1_z, src, t2_x, t2_y, t2_z, abs(t1_x-t2_x), abs(t1_y-t2_y), abs(t1_z-t2_z))) 
00051     return (t2_x, t2_y, t2_z)
00052     
00053 
00054 if __name__=="__main__":
00055     # first thing, init a node!
00056     rospy.init_node('test_odom')
00057 
00058     # publish to cmd_vel
00059     p = rospy.Publisher('spur/cmd_vel', Twist)
00060     rospy.Subscriber('odom', Odometry, odom_cb, queue_size=10)
00061     rospy.Subscriber('base_pose_ground_truth', Odometry, odom_cb, queue_size=10)
00062     rospy.Subscriber('pose2D', Pose2D, pose_cb, queue_size=10)
00063 
00064     # create a twist message, fill in the details
00065     twist = Twist()
00066     twist.linear.x = 0
00067     twist.linear.y = 0
00068     twist.linear.z = 0
00069     twist.angular.x = 0
00070     twist.angular.y = 0
00071     twist.angular.z = 0
00072 
00073     linear_x = 0.1
00074     inc_x = 0.05
00075     angular_z = 0.2
00076     inc_z = 0.05
00077     sign = 1
00078     try:
00079         twist.linear.x = sign*linear_x
00080         twist.angular.z = 0
00081         while not rospy.is_shutdown():
00082             p.publish(twist)
00083             rospy.sleep(1)
00084             # announce move, and publish the message
00085             rospy.loginfo("move forward for 2 sec at %f"%twist.linear.x)
00086             for i in range(4):
00087                 p.publish(twist)
00088                 rospy.sleep(0.5) # 10*0.2 = 2.0
00089                 diff = check_odom()
00090             print("diff %s"% str(diff))
00091             if abs(diff[0] - sign*linear_x) > 0.1:
00092                 linear_x = linear_x - inc_x
00093                 break
00094             linear_x = linear_x + inc_x
00095             sign = sign * -1
00096             twist.linear.x = sign*linear_x
00097         
00098         print("--- Maximum velocity is  %f"% linear_x)
00099 
00100         twist.linear.x = 0
00101         twist.angular.z = sign*angular_z
00102         while not rospy.is_shutdown() :
00103             p.publish(twist)
00104             rospy.sleep(1)
00105             # announce move, and publish the message
00106             rospy.loginfo("move forward for 2 sec at %f"%twist.linear.x)
00107             for i in range(4):
00108                 p.publish(twist)
00109                 rospy.sleep(0.5) # 10*0.2 = 2.0
00110                 diff = check_odom()
00111             print("diff %s"% str(diff))
00112             if abs(diff[2] - sign*angular_z) > 0.1:
00113                 angular_z = angular_z - inc_z
00114                 break
00115             angular_z = angular_z + inc_z
00116             sign = sign * -1
00117             twist.angular.z = sign*angular_z
00118 
00119         
00120         print("--- Maximum angular is  %f"% angular_z)
00121 
00122     except rospy.ROSInterruptException:
00123         # create a new message
00124         twist = Twist()
00125 
00126         # note: everything defaults to 0 in twist, if we don't fill it in, we stop!
00127         rospy.loginfo("Stopping!")
00128         p.publish(twist)
00129     
00130         # twist.linear.x = 0
00131         # twist.angular.z = 0.3
00132         # rospy.loginfo("rotate for 5 sec (90 deg)")
00133         # for i in range(50):
00134         #     p.publish(twist)
00135         #     rospy.sleep(0.1) # 50*0.1 = 5.0
00136         #     print_odom()
00137 
00138 


spur_controller
Author(s): Tokyo Opensource Robotics Programmer 534o <534o@opensource-robotics.tokyo.jp>, Isaac I. Y. Saito
autogenerated on Sat Jun 8 2019 19:44:12