test_velocity.py
Go to the documentation of this file.
1 #!/usr/bin/env python
2 
3 # VELOCITY can be positive (driving forward) or negative (driving backward)
4 VELOCITY = 0.2
5 
6 # Initial turn angle (Z axis)
7 ANGLE = 0.0
8 
9 
10 import rospy
11 from geometry_msgs.msg import Twist,Point
12 from nav_msgs.msg import Odometry
13 
14 rospy.init_node('slow_motion', anonymous=True)
15 
16 last_t = None
17 last_pos = None
18 
19 print ("""-------------------------------------------------------------
20 Odometer consistency check
21 -------------------------------------------------------------
22 """)
23 
25 
26  # Calculate velocity error (%)
27  global last_t
28  global last_pos
29 
30  now = rospy.Time.now().to_sec()
31  cur_pos = msg.pose.pose.position
32 
33  if last_pos:
34  pos_distance = ((cur_pos.x-last_pos.x)**2 + (cur_pos.y-last_pos.y)**2)**0.5
35  t_distance = VELOCITY*(now-last_t)
36  print "Velocity error: {}%".format(round(abs(msg.twist.twist.linear.x-VELOCITY)/VELOCITY*100,2)),\
37  " Position error: {}%".format(round(abs(pos_distance-t_distance)/t_distance*100,2))
38 
39  last_pos = cur_pos
40  last_t = now
41 
42 
44 
45 
46  pub = rospy.Publisher('cmd_vel', Twist, queue_size=10)
47 
48  rospy.Subscriber('odom',Odometry,odometry_callback)
49  vel_msg = Twist()
50 
51  initial = True
52 
53  vel_msg.linear.x = VELOCITY
54  vel_msg.angular.z = ANGLE
55 
56  while not rospy.is_shutdown():
57 
58  # Publish the velocity
59  pub.publish(vel_msg)
60 
61  if initial:
62  vel_msg.angular.z = 0.0
63  initial = False
64 
65  rospy.sleep(0.05)
66 
67 
68 
69 if __name__ == '__main__':
70  if abs(VELOCITY)<0.000001:
71  print ("VELOCITY must be different from zero")
72  else:
73 
74  try:
75  slow_motion()
76  except rospy.ROSInterruptException:
77  pass
def odometry_callback(msg)


ubiquity_motor
Author(s):
autogenerated on Mon Feb 28 2022 23:57:06