basic_move_controller.py
Go to the documentation of this file.
1 '''
2  basic_move_controller.py
3  LICENSE : BSD - https://raw.github.com/yujinrobot/yujin_ocs/license/LICENSE
4 '''
5 import math
6 import copy
7 import rospy
8 import tf
9 import nav_msgs.msg as nav_msgs
10 import geometry_msgs.msg as geometry_msgs
11 
12 class BasicMoveController(object):
13  def __init__(self, odom_topic='odom', cmd_vel_topic='cmd_vel'):
14  self._sub_odom = rospy.Subscriber(odom_topic, nav_msgs.Odometry, self.process_odometry)
15  self._pub_cmd_vel = rospy.Publisher(cmd_vel_topic, geometry_msgs.Twist, queue_size=5)
16 
17  self._odom = None
18 
19  def process_odometry(self, msg):
20  self._odom = copy.deepcopy(msg)
21 
22  def move_at(self, v, w, t):
23  vel = geometry_msgs.Twist()
24 
25  vel.linear.x = v
26  vel.angular.z = w
27  self._pub_cmd_vel.publish(vel)
28  rospy.sleep(t)
29 
30  def turn_clockwise(self):
31  self.move_at(0.0, -0.5, 0.1)
32 
34  self.move_at(0.0, 0.5, 0.1)
35 
36  def turn(self, angle):
37  raise NotImplementedError()
38 
39  def spin_clockwise(self):
40  yaw = self._get_odom_yaw()
41 
42  i = 0
43  while (self._get_odom_yaw() <= yaw) or i < 5:
44  self.turn_clockwise()
45  i = i + 1
46 
47  i = 0
48  while (self._get_odom_yaw() > yaw) or i < 5:
49  self.turn_clockwise()
50  i = i +1
51 
52  def _get_odom_yaw(self):
53  quaternion = (self._odom.pose.pose.orientation.x, self._odom.pose.pose.orientation.y, self._odom.pose.pose.orientation.z, self._odom.pose.pose.orientation.w)
54  roll, pitch, yaw = tf.transformations.euler_from_quaternion(quaternion)
55  return yaw
56 
57  def slow_forward(self):
58  self.move_at(0.1, 0.0, 0.1)
59 
60  def slow_backward(self):
61  self.move_at(-0.1, 0.0, 0.1)
62 
63  def forward(self, distance):
64  pos0 = copy.deepcopy(self._odom.pose.pose.position)
65 
66  while(self._distance2d(pos0, self._odom.pose.pose.position) < distance):
67  self.slow_forward()
68 
69  def backward(self, distance):
70  pos0 = copy.deepcopy(self._odom.pose.pose.position)
71 
72  while(self._distance2d(pos0, self._odom.pose.pose.position) < distance):
73  self.slow_backward()
74 
75  def _distance2d(self, p1, p2):
76  '''
77  computes distance between two points
78  '''
79  return math.sqrt(math.pow((p2.x - p1.x), 2) + math.pow((p2.y -p1.y), 2))
def __init__(self, odom_topic='odom', cmd_vel_topic='cmd_vel')


yocs_navigator
Author(s): Jihoon Lee, Jorge Simon Santos
autogenerated on Mon Jun 10 2019 15:53:58