motion_travel_forward.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 math
11 import rospy
12 from geometry_msgs.msg import Twist, Pose
13 from nav_msgs.msg import Odometry
14 from kobuki_msgs.msg import CliffEvent
15 # Local imports
16 import utils
17 
18 ##############################################################################
19 # Classes
20 ##############################################################################
21 
22 '''
23  Travels forward a set distance.
24 
25  API:
26  init(speed,distance) : (re)initialise parameters
27  stop() - stop.
28  execute() - pass this to a thread to run
29  shutdown() - cleanup
30 '''
31 class TravelForward(object):
32  '''
33  Initialise everything
34 
35  @param topic names
36  @type strings
37  '''
38  def __init__(self, cmd_vel_topic, odom_topic, cliff_sensor_topic):
39  self.odom_subscriber = rospy.Subscriber(odom_topic, Odometry, self.odometry_callback)
40  self.cmd_vel_publisher = rospy.Publisher(cmd_vel_topic, Twist, queue_size=10)
41  self.cliff_sensor_subscriber = rospy.Subscriber(cliff_sensor_topic, CliffEvent , self.cliff_sensor_callback)
42  self.speed = 0.7
43  self.distance = 1.0
44  self._current_pose = Pose()
45  self._starting_pose = Pose()
46  self._stop = False
47  self._running = False
48 
49  def init(self, speed, distance):
50  self.speed = speed
51  self.distance = distance
52 
53  def shutdown(self):
54  self.stop()
55  while self._running:
56  rospy.sleep(0.05)
57  self.cmd_vel_publisher.unregister()
58  self.odom_subscriber.unregister()
59 
60  def stop(self):
61  self._stop = True
62 
63  def execute(self):
64  '''
65  Drop this into threading.Thread or QThread for execution
66  '''
67  if self._running:
68  rospy.logerr("Kobuki TestSuite: already executing a motion, ignoring the request")
69  return
70  self._stop = False
71  self._running = True
72  rate = rospy.Rate(10)
73  self._current_speed = 0.0
74  current_distance_sq = 0.0
75  distance_sq = self.distance*self.distance
76  self._starting_pose = self._current_pose
77  while not self._stop and not rospy.is_shutdown():
78  if current_distance_sq > distance_sq:
79  break
80  else:
81  current_distance_sq = (self._current_pose.position.x - self._starting_pose.position.x)*(self._current_pose.position.x - self._starting_pose.position.x) + \
82  (self._current_pose.position.y - self._starting_pose.position.y)*(self._current_pose.position.y - self._starting_pose.position.y)
83  #current_distance_sq += 0.01 # uncomment this and comment above for debugging
84  print("Distance %s"%math.sqrt(current_distance_sq))
85  if self.speed > 0:
86  if self._current_speed < self.speed:
87  self._current_speed += 0.01
88  else:
89  if self._current_speed > self.speed:
90  self._current_speed -= 0.01
91  cmd = Twist()
92  cmd.linear.x = self._current_speed
93  self.cmd_vel_publisher.publish(cmd)
94  rate.sleep()
95  if not rospy.is_shutdown():
96  cmd = Twist()
97  cmd.linear.x = 0.0
98  self.cmd_vel_publisher.publish(cmd)
99  self._running = False
100 
101  ##########################################################################
102  # Ros Callbacks
103  ##########################################################################
104 
105  def cliff_sensor_callback(self, data):
106  rospy.loginfo("Kobuki Testsuite: cliff event on sensor [%s]"%str(data.sensor))
107  if data.state == CliffEvent.CLIFF:
108  if not rospy.is_shutdown():
109  cmd = Twist()
110  cmd.linear.x = 0.0
111  self.cmd_vel_publisher.publish(cmd)
112  self.stop()
113 
114  def odometry_callback(self, data):
115  self._current_pose = data.pose.pose
def __init__(self, cmd_vel_topic, odom_topic, cliff_sensor_topic)


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