spiral_cmd.py
Go to the documentation of this file.
1 #!/usr/bin/env python
2 import rospy
3 from geometry_msgs.msg import Twist
4 import math
5 
6 step_size = 1.0
7 spiral_diam = 7
8 
9 # linear speed params
10 a_x = 1.17
11 v_x = 0.2
12 a_x_t = v_x/a_x
13 a_dist = a_x * a_x_t**2 / 2
14 # angular speed params
15 a_z = 2.02
16 v_z = 0.45
17 a_z_t = v_z/a_z
18 a_rad = a_z * a_z_t**2 / 2
19 
20 twist = Twist()
21 
22 
23 def travel_time(dist):
24  t = (dist - a_dist) / v_x + a_x_t
25  return t
26 
27 
28 def rotation_time(rad):
29  t = (rad - a_rad) / v_z + a_z_t
30  return t
31 
32 
33 def publish_cmd_vel(twist, t):
34  rospy.loginfo("publish cmd:\n" + str(twist))
35  end_time = rospy.rostime.get_rostime() + rospy.Duration(t)
36  rate = rospy.Rate(100)
37  while True:
38  pub.publish(twist)
39  rate.sleep()
40  rem_time = end_time - rospy.rostime.get_rostime()
41  if rem_time < rate.sleep_dur:
42  if rem_time.to_sec() > 0:
43  rospy.sleep(rem_time)
44  break
45 
46 
47 def move(dist):
48  twist.linear.x = v_x
49  twist.angular.z = 0
50  t = travel_time(dist)
51  rospy.loginfo("move " + str(dist) + " m for " + str(t) + " sec")
52  publish_cmd_vel(twist, t)
53 
54 
55 def rotate(rad):
56  twist.linear.x = 0
57  twist.angular.z = v_z
58  t = rotation_time(rad)
59  rospy.loginfo("rotate " + str(rad) + " rad for " + str(t) + " sec")
60  publish_cmd_vel(twist, t)
61 
62 
63 def stop():
64  twist.linear.x = 0
65  twist.angular.z = 0
66  rospy.loginfo("stop");
67  publish_cmd_vel(twist, 0.3)
68 
69 
70 if __name__ == '__main__':
71 
72  pub = rospy.Publisher('cmd_vel', Twist, queue_size = 1)
73  rospy.init_node('spiral_cmd', anonymous=True)
74 
75  try:
76  for i in range(1, spiral_diam):
77  dist = i * step_size
78  if i < spiral_diam:
79  move(dist)
80  stop()
81  rotate(math.pi/2)
82  stop()
83  move(dist)
84  stop()
85  rotate(math.pi/2)
86  stop()
87  else:
88  move(dist)
89  stop()
90  except rospy.ROSInterruptException:
91  pass
92 
def travel_time(dist)
Definition: spiral_cmd.py:23
def stop()
Definition: spiral_cmd.py:63
def move(dist)
Definition: spiral_cmd.py:47
def rotate(rad)
Definition: spiral_cmd.py:55
def publish_cmd_vel(twist, t)
Definition: spiral_cmd.py:33
def rotation_time(rad)
Definition: spiral_cmd.py:28


magni_nav
Author(s):
autogenerated on Tue Jun 1 2021 02:33:39