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 threading
11 import rospy
12 import math
13 from geometry_msgs.msg import Twist
14 from std_msgs.msg import String
15 
16 ##############################################################################
17 # Classes
18 ##############################################################################
19 
20 '''
21  implements a rotating motion.
22 '''
23 
24 class RotateTest(threading.Thread):
25  def __init__(self, cmd_vel_topic, log_topic, yaw_rate = 1.2):
26  threading.Thread.__init__(self)
27  self.pub_cmd = rospy.Publisher(cmd_vel_topic,Twist)
28  self.pub_log = rospy.Publisher(log_topic,String)
29 
30  freq = 5
31  self.rate = rospy.Rate(freq)
32  self.yaw_rate = yaw_rate
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.max_rotate_count = freq*int(3.14/ self.yaw_rate)
44 
45  self._stop = False
46 
47  def stop(self):
48  self._stop = True
49 
50  def run(self):
51  self._stop = False
52 
53  start = rospy.get_rostime()
54  rospy.sleep(0.5)
55  twist = self.twist
56  max_rotate_count = self.max_rotate_count
57  rotate_count = max_rotate_count
58  yaw_rate = self.yaw_rate
59 
60  while not rospy.is_shutdown() and not self._stop:
61  if rotate_count == max_rotate_count:
62  if twist.angular.z > 0:
63  mod = -1.0
64  else:
65  mod = 1.0
66  update = mod*yaw_rate/10.0
67 
68  while math.fabs(twist.angular.z) <= yaw_rate:
69  twist.angular.z = twist.angular.z + update
70  self.pub_cmd.publish(twist)
71  rospy.sleep(0.04)
72 
73  # Make sure it is exact so the inequality in the while loop doesn't mess up next time around
74  twist.angular.z = mod*yaw_rate
75  rotate_count = 0
76  else:
77  rotate_count += 1
78  now = rospy.get_rostime()
79  msg = "Rotate: " + str(now.secs - start.secs)
80  self.log(msg)
81  self.pub_cmd.publish(twist)
82  self.rate.sleep()
83 
84  def log(self,msg):
85  rospy.loginfo(msg)
86 
87  t = String(msg)
88  self.pub_log.publish(t)
89 
def __init__(self, cmd_vel_topic, log_topic, yaw_rate=1.2)
Definition: rotate.py:25


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