22 SWITCHED_VELOCITY = VELOCITY * -1.0
25 SWITCH_DEAD_TIME = 2.0
34 from geometry_msgs.msg
import Twist,Point
35 from nav_msgs.msg
import Odometry
39 parser = argparse.ArgumentParser(description=
'Ubiquity Robotics Automatic velocity tester')
40 parser.add_argument(
'--loop_time', help=
'Loop time delay in sec)', default=
'0.05')
41 parser.add_argument(
'--velocity', help=
'Running velocity in M/Sec)', default=str(VELOCITY))
42 parser.add_argument(
'--angle', help=
'Angular velocity in Radians/Sec)', default=
'0.0')
43 parser.add_argument(
'--switch_time', help=
'Time till direction switch (0.0 = no switch)', default=
'0.0')
44 parser.add_argument(
'--switched_velocity', help=
'Velocity in switched 2nd period', default=str(SWITCHED_VELOCITY))
45 parser.add_argument(
'--switch_dead_time', help=
'Time stopped between velocity switches', default=
'2.0')
46 args = parser.parse_args()
48 LOOP_TIME = float(args.loop_time)
49 VELOCITY = float(args.velocity)
50 ANGLE = float(args.angle)
51 SWITCH_TIME = float(args.switch_time)
52 SWITCHED_VELOCITY = float(args.switched_velocity)
53 SWITCH_DEAD_TIME = float(args.switch_dead_time)
55 rospy.init_node(
'slow_motion', anonymous=
True)
62 print(
""" -------------------------------------------------------------
63 Auto-reverse velocity mode
64 -------------------------------------------------------------
67 print(
""" -------------------------------------------------------------
68 Odometer consistency check
69 -------------------------------------------------------------
71 print(
"Using velocity of " + str(VELOCITY))
81 now = rospy.Time.now().to_sec()
82 cur_pos = msg.pose.pose.position
84 if last_pos
and (SWITCH_TIME == 0.0):
85 pos_distance = ((cur_pos.x-last_pos.x)**2 + (cur_pos.y-last_pos.y)**2)**0.5
86 t_distance = VELOCITY*(now-last_t)
87 print(
"Velocity error: {}%".format(round(abs(msg.twist.twist.linear.x-VELOCITY)/VELOCITY*100,2)),\
88 " Position error: {}%".format(round(abs(pos_distance-t_distance)/t_distance*100,2)))
97 global SWITCH_DEAD_TIME
100 pub = rospy.Publisher(
'cmd_vel', Twist, queue_size=10)
102 rospy.Subscriber(
'odom',Odometry,odometry_callback)
104 vel_msg.linear.x = VELOCITY
105 vel_msg.angular.z = ANGLE
108 stop_msg.linear.x = 0.0
109 stop_msg.angular.z = 0.0
117 while not rospy.is_shutdown():
123 vel_msg.angular.z = 0.0
126 rospy.sleep(LOOP_TIME)
128 drive_time += LOOP_TIME
131 if ((SWITCH_TIME > 0.0)
and (drive_time > SWITCH_TIME)):
132 if drive_forward > 0:
133 print "Linear VELOCITY is being set to rate 1 of ", VELOCITY,
" M/sec for ", SWITCH_TIME,
" sec"
134 vel_msg.linear.x = VELOCITY
136 vel_msg.linear.x = SWITCHED_VELOCITY
137 print "Linear VELOCITY is being set to rate 2 of ", SWITCHED_VELOCITY,
" M/sec for ", SWITCH_TIME,
" sec"
138 drive_forward = drive_forward * -1
140 if SWITCH_DEAD_TIME > 0.0:
141 pub.publish(stop_msg)
142 print "Linear VELOCITY deadtime active for next ", SWITCH_DEAD_TIME,
" sec"
143 rospy.sleep(SWITCH_DEAD_TIME)
146 if __name__ ==
'__main__':
147 if abs(VELOCITY)<0.000001:
148 print(
"VELOCITY must be different from zero")
153 except rospy.ROSInterruptException: