motion_rotate.py
Go to the documentation of this file.
1 #!/usr/bin/env python
2 #
3 # License: BSD
4 # https://raw.github.com/yujinrobot/kobuki/hydro-devel/kobuki_testsuite/LICENSE
5 #
6 ##############################################################################
7 # Imports
8 ##############################################################################
9 
10 import rospy
11 import math
12 from geometry_msgs.msg import Twist
13 from std_msgs.msg import String
14 
15 ##############################################################################
16 # Classes
17 ##############################################################################
18 
19 '''
20  implements a rotating motion.
21 '''
22 
23 class Rotate():
24  def __init__(self,cmd_vel_topic):
25  self.cmd_vel_publisher = rospy.Publisher(cmd_vel_topic,Twist, queue_size=10)
27  self._rate = rospy.Rate(self._cmd_vel_frequency)
28  self._yaw_rate = 1.2
29 
30  self._max_rotate_count = self._cmd_vel_frequency*int(3.14/ self._yaw_rate)
31  self._stop = False
32  self._running = False
33 
34  twist = Twist()
35  twist.linear.x = 0
36  twist.linear.y = 0
37  twist.linear.z = 0
38  twist.angular.x = 0
39  twist.angular.y = 0
40  twist.angular.z = 0
41  self.twist = twist
42 
43  self._number_of_turns = -1 # continuous
44 
45  def init(self, yaw_rate, number_of_turns=-1):
46  self._yaw_rate = yaw_rate
47  self._max_rotate_count = self._cmd_vel_frequency*int(3.14/ self._yaw_rate)
48 
49  def shutdown(self):
50  self.stop()
51  while self._running:
52  rospy.sleep(0.5)
53  self.cmd_vel_publisher.unregister() #This one creates an error for some reason, probably because others already unregister.
54 
55  def stop(self):
56  self._stop = True
57 
58  def execute(self):
59  '''
60  Drop this into threading.Thread or QThread for execution
61  '''
62  if self._running:
63  rospy.logerr("Kobuki TestSuite: already executing a motion, ignoring the request")
64  return
65  self._stop = False
66  self._running = True
67  start = rospy.get_rostime()
68  rospy.sleep(0.5)
69 
70  twist = self.twist
71  max_rotate_count = self._max_rotate_count
72  rotate_count = max_rotate_count
73 
74  while not self._stop and not rospy.is_shutdown():
75  if rotate_count == max_rotate_count:
76  if twist.angular.z > 0:
77  mod = -1.0
78  else:
79  mod = 1.0
80  update = mod*self._yaw_rate/10.0
81 
82  while math.fabs(twist.angular.z) <= self._yaw_rate:
83  twist.angular.z = twist.angular.z + update
84  #print "Command velocity: %s"%twist.angular.z
85  self.cmd_vel_publisher.publish(twist)
86  rospy.sleep(0.04)
87 
88  # Make sure it is exact so the inequality in the while loop doesn't mess up next time around
89  twist.angular.z = mod*self._yaw_rate
90  rotate_count = 0
91  else:
92  rotate_count += 1
93  now = rospy.get_rostime()
94  msg = "Rotate: " + str(now.secs - start.secs)
95  self.cmd_vel_publisher.publish(twist)
96  self._rate.sleep()
97  if not rospy.is_shutdown():
98  cmd = Twist()
99  cmd.angular.z = 0.0
100  self.cmd_vel_publisher.publish(cmd)
101  self._running = False
102 
def __init__(self, cmd_vel_topic)
def init(self, yaw_rate, number_of_turns=-1)


kobuki_testsuite
Author(s): Jorge Santos Simon
autogenerated on Mon Jun 10 2019 13:45:22