test_velocity.py
Go to the documentation of this file.
1 #!/usr/bin/env python3
2 
3 # This is a test tool to drive forever open-loop under computer control.
4 #
5 # By default a constant linear velocity is used till script is stopped.
6 # In constant mode velocity and position errors are printed
7 #
8 # The second mode will switch between two speeds with a deadtime between.
9 # This mode is enabled when the 'switched_time' parameter is non-zero.
10 # The switched_time is how long to run in a given direction.
11 # The normal velocity can be set and the second, switched_velocity can be set
12 # where each velocity can be positive or negative.
13 # The dead time can be 0 but if not we go to 0 for the time specified.
14 #
15 
16 # VELOCITY can be positive (driving forward) or negative (driving backward)
17 VELOCITY = 0.20
18 
19 # When SWITCH_TIME is non-zero we drive for this time then reverse direction
20 # This allows for evaluation of the PID parameters to go back and forth
21 SWITCH_TIME = 0.0
22 SWITCHED_VELOCITY = VELOCITY * -1.0
23 
24 # When doing reversals this is the dead time at zero velocity between reversals of direction
25 SWITCH_DEAD_TIME = 2.0
26 
27 # Initial turn angle (Z axis)
28 ANGLE = 0.0
29 
30 LOOP_TIME = 0.05
31 
32 
33 import rospy
34 from geometry_msgs.msg import Twist,Point
35 from nav_msgs.msg import Odometry
36 import argparse
37 
38 
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()
47 
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)
54 
55 rospy.init_node('slow_motion', anonymous=True)
56 
57 
58 last_t = None
59 last_pos = None
60 
61 if SWITCH_TIME > 0.0:
62  print(""" -------------------------------------------------------------
63  Auto-reverse velocity mode
64  -------------------------------------------------------------
65  """)
66 else:
67  print(""" -------------------------------------------------------------
68  Odometer consistency check
69  -------------------------------------------------------------
70  """)
71  print("Using velocity of " + str(VELOCITY))
72 
74 
75  global SWITCH_TIME
76 
77  # Calculate velocity error (%)
78  global last_t
79  global last_pos
80 
81  now = rospy.Time.now().to_sec()
82  cur_pos = msg.pose.pose.position
83 
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)))
89 
90  last_pos = cur_pos
91  last_t = now
92 
93 
95 
96  global SWITCH_TIME
97  global SWITCH_DEAD_TIME
98  global LOOP_TIME
99 
100  pub = rospy.Publisher('cmd_vel', Twist, queue_size=10)
101 
102  rospy.Subscriber('odom',Odometry,odometry_callback)
103  vel_msg = Twist()
104  vel_msg.linear.x = VELOCITY
105  vel_msg.angular.z = ANGLE
106 
107  stop_msg = Twist()
108  stop_msg.linear.x = 0.0
109  stop_msg.angular.z = 0.0
110 
111  initial = True
112 
113 
114  drive_time = 0.0
115  drive_forward = 1
116 
117  while not rospy.is_shutdown():
118 
119  # Publish the velocity
120  pub.publish(vel_msg)
121 
122  if initial:
123  vel_msg.angular.z = 0.0
124  initial = False
125 
126  rospy.sleep(LOOP_TIME)
127 
128  drive_time += LOOP_TIME
129 
130  # implement auto-reversal of driving linear direction if enabled
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
135  else:
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
139  drive_time = 0.0
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)
144 
145 
146 if __name__ == '__main__':
147  if abs(VELOCITY)<0.000001:
148  print("VELOCITY must be different from zero")
149  else:
150 
151  try:
152  slow_motion()
153  except rospy.ROSInterruptException:
154  pass
test_velocity.odometry_callback
def odometry_callback(msg)
Definition: test_velocity.py:73
test_velocity.slow_motion
def slow_motion()
Definition: test_velocity.py:94


ubiquity_motor
Author(s):
autogenerated on Thu Nov 16 2023 03:30:56