rotate.py
Go to the documentation of this file.
1 #
2 # License: BSD
3 # https://raw.github.com/robotics-in-concert/rocon_concert/license/LICENSE
4 #
5 ##############################################################################
6 # Imports
7 ##############################################################################
8 
9 import rospy
10 import math
11 import std_msgs.msg as std_msgs
12 import geometry_msgs.msg as geometry_msgs
13 #import tf
14 
15 ##############################################################################
16 # Classes
17 ##############################################################################
18 
19 
20 class Rotate(object):
21  '''
22  implements a rotating motion.
23  '''
24  __slots__ = [
25  'yaw_absolute_rate',
26  'yaw_direction', # +1, or -1
27  '_stop',
28  '_cmd_vel_topic',
29  '_cmd_vel_publisher',
30  '_cmd_vel_frequency',
31  '_rate',
32  '_running',
33  '_twist',
34  '_listener',
35  '_initialise_pose_trigger' # tell the initial pose manager to grab an initialisation
36  ]
37  CLOCKWISE = -1
38  COUNTER_CLOCKWISE = 1
39 
40  def __init__(self, cmd_vel_topic):
41  self._cmd_vel_publisher = None
43  self._cmd_vel_topic = cmd_vel_topic
44  self._rate = rospy.Rate(self._cmd_vel_frequency)
45  self._stop = False
46  self._running = False
47  self._initialise_pose_trigger = rospy.Publisher('~initialise', std_msgs.Empty, queue_size=5)
48 
49  twist = geometry_msgs.Twist()
50  twist.linear.x = 0
51  twist.linear.y = 0
52  twist.linear.z = 0
53  twist.angular.x = 0
54  twist.angular.y = 0
55  twist.angular.z = 0
56  self._twist = twist
57  #self._listener = tf.TransformListener()
58  self.init()
59 
60  def init(self, yaw_absolute_rate=1.2, yaw_direction=CLOCKWISE):
61  '''
62  Initialise the direction and rate the robot should rotate at.
63  '''
64  self.yaw_absolute_rate = yaw_absolute_rate
65  self.yaw_direction = yaw_direction
66 
67  def shutdown(self):
68  self.stop()
69  while self._running:
70  rospy.sleep(0.01)
71 
72  def is_running(self):
73  return self._running
74 
75  def change_direction(self):
76  if self.yaw_direction == Rotate.CLOCKWISE:
77  self.yaw_direction = Rotate.COUNTER_CLOCKWISE
78  else:
79  self.yaw_direction = Rotate.CLOCKWISE
80 
81  def is_stopped(self):
82  return self._stop
83 
84  def stop(self):
85  self._stop = True
86 
87  def execute(self):
88  '''
89  Drop this into threading.Thread or QThread for execution
90  '''
91  if self._running:
92  rospy.logerr("AR Pair Search: already executing a motion, ignoring the request")
93  return
94  self._cmd_vel_publisher = rospy.Publisher(self._cmd_vel_topic, geometry_msgs.Twist, queue_size=10)
95  self._stop = False
96  self._running = True
97  start = rospy.get_rostime()
98  rospy.sleep(0.3)
99  result = False
100  twist = self._twist
101  while not self._stop and not rospy.is_shutdown():
102  update = self.yaw_direction * self.yaw_absolute_rate / 10.0
103  if math.fabs(twist.angular.z) < self.yaw_absolute_rate:
104  twist.angular.z = twist.angular.z + update
105  else:
106  # Make sure it is exact so the inequality in the while loop doesn't mess up next time around
107  twist.angular.z = self.yaw_direction * self.yaw_absolute_rate
108  now = rospy.get_rostime()
109  rospy.logdebug("AR Pair Search: rotate: %s rad/s [%ss]" % (twist.angular.z, str(now.secs - start.secs)))
110  try:
111  self._cmd_vel_publisher.publish(twist)
112  except rospy.ROSException: # shutdown
113  break
114  self._rate.sleep()
115  if not rospy.is_shutdown():
116  cmd = geometry_msgs.Twist()
117  cmd.angular.z = 0.0
118  self._cmd_vel_publisher.publish(cmd)
119  self._initialise_pose_trigger.publish(std_msgs.Empty())
120  result = True
121  self._cmd_vel_publisher.unregister()
122  self._cmd_vel_publisher = None
123  self._running = False
124  return result
def __init__(self, cmd_vel_topic)
Definition: rotate.py:40
def init(self, yaw_absolute_rate=1.2, yaw_direction=CLOCKWISE)
Definition: rotate.py:60


yocs_ar_pair_approach
Author(s): Daniel Stonier
autogenerated on Mon Jun 10 2019 15:53:27