src/yocs_localization_manager/localization_manager.py
Go to the documentation of this file.
1 #
2 # License: BSD
3 # https://raw.github.com/yujinrobot/yujin_ocs/license/LICENSE
4 #
5 
6 import threading
7 import actionlib
8 import dynamic_reconfigure.client
9 import rospy
10 import tf
11 from yocs_navigator import BasicMoveController
12 import std_msgs.msg as std_msgs
13 import yocs_msgs.msg as yocs_msgs
14 import geometry_msgs.msg as geometry_msgs
15 
16 class LocalizationManager(object):
17  """
18  Uses poses from a pose tracker to initialise the global robot pose
19  using a pose initialisation node such as fake_localization
20  """
21 
22  _localize_action_name = 'localize'
23 
24  def __init__(self):
25  self._initialise = False
26  self._thread = None
27  self._basic_move_controller = BasicMoveController()
28  self._init_params()
29 
30  if self.param['simulation']:
31  self.loginfo("Running in simulation mode.")
32  else:
33  self._client = dynamic_reconfigure.client.Client(rospy.get_param('~pose_tracker', 'ar_track_alvar'))
34  self._sub_tracked_poses = rospy.Subscriber('pose_tracker/poses', geometry_msgs.PoseWithCovarianceStamped, self._tracked_poses_callback)
35 
36  self._pub_init_pose = rospy.Publisher('initialpose', geometry_msgs.PoseWithCovarianceStamped, latch=True, queue_size=3)
37 
38  self._as_localize = actionlib.SimpleActionServer(self._localize_action_name, yocs_msgs.LocalizeAction, auto_start=False)
39  self._as_localize.register_goal_callback(self._process_localize_goal)
40  self._as_localize.register_preempt_callback(self._process_localize_preempt)
41 
42  def _init_params(self):
43  param = {}
44  param['sleeptime'] = rospy.Duration(1 / rospy.get_param('~spin_freq', 10))
45  param['simulation'] = rospy.get_param('~simulation', False)
46  param['ar_pair_baseline'] = rospy.get_param('ar_pair/baseline', 0.28)
47  param['ar_pair_target_offset'] = rospy.get_param('ar_pair/target_offset', 0.5)
48  param['timeout'] = rospy.get_param('~timeout', 10.0)
49 
50  # configurations for simulation
51  param['sim_global_frame'] = rospy.get_param('~simulation_global_frame','map')
52  param['sim_x'] = rospy.get_param('~simulation_x', 0.0)
53  param['sim_y'] = rospy.get_param('~simulation_y', 0.0)
54  param['sim_a'] = rospy.get_param('~simulation_a', 0.0)
55 
56  self.param = param
57 
58  def _tracked_poses_callback(self, msg):
59  if self._initialise:
60  # send pose to pose initialisation node
61  msg.header.stamp -= rospy.Duration(0.2) # TODO: get latest common time
62  cov = list(msg.pose.covariance)
63  cov[6 * 0 + 0] = self._distortion * self._distortion
64  cov[6 * 1 + 1] = self._distortion * self._distortion
65  msg.pose.covariance = tuple(cov)
66  self._pub_init_pose.publish(msg)
67  self.loginfo("localization done.")
68  self._initialise = False
69 
71  goal = self._as_localize.accept_new_goal()
72  self._distortion = goal.distortion
73  self.loginfo("Received Localize goal %s"%str(goal))
74 
75  if self._initialise:
76  message = 'robot is initialising already. Ignore the command'
77  self._send_result(False, message)
78  else:
79  if self.param['simulation']:
80  pose_msg = geometry_msgs.PoseWithCovarianceStamped()
81  pose_msg.header.frame_id = self.param['sim_global_frame']
82  pose_msg.header.stamp = rospy.Time.now() - rospy.Duration(0.2) # TODO: get latest common time
83  pose_msg.pose.pose.position.x = self.param['sim_x']
84  pose_msg.pose.pose.position.y = self.param['sim_y']
85  pose_msg.pose.pose.position.z = 0.0
86  quat = tf.transformations.quaternion_from_euler(0, 0, self.param['sim_a'])
87  pose_msg.pose.pose.orientation = geometry_msgs.Quaternion(*quat)
88  self._pub_init_pose.publish(pose_msg)
89  # send success right away
90  self._send_result(True,'Initialisation done in simulation.')
91  elif goal.command == goal.STAND_AND_LOCALIZE:
92  self._thread = threading.Thread(target=self._stand_and_localize)
93  self._thread.start()
94  elif goal.command == goal.SPIN_AND_LOCALIZE:
95  self._thread = threading.Thread(target=self._spin_and_localize)
96  self._thread.start()
97  else:
98  message = 'Invalid command %s'%str(goal.command)
99  self._send_result(False, message)
100 
102  self.logwarn('Received Preempt Request')
103  pass
104 
106  self.loginfo("Stand and Localization started.")
107 
108  # enable pose tracker
109  self._update_tracker(True)
110  self._initialise = True
111 
112  timeout = self.param['timeout']
113  sleeptime = self.param['sleeptime']
114  start_time = rospy.Time.now()
115  while not rospy.is_shutdown() and self._initialise:
116  current_time = rospy.Time.now()
117  dif = (current_time - start_time).to_sec()
118  if dif > timeout:
119  break
120  rospy.sleep(sleeptime)
121 
122  # disable the pose tracker
123  self._update_tracker(False)
124 
125 
126  if self._initialise: # Timeout
127  self._initialise = False
128  self._send_result(False, "couldn't localize in time %s"%str(dif))
129  else: # localized
130  self._send_result(True, "Localized")
131 
133  self.loginfo("Spin and localization started")
134 
135  self._update_tracker(True)
136  self._initialise = True
137 
138  self._basic_move_controller.spin_clockwise()
139 
140  self._update_tracker(False)
141  if self._initialise:
142  self._initialise = False
143  self._send_result(False, "couldn't localise after full spining")
144  else:
145  self._send_result(True, "Localised")
146 
147  def _send_result(self, success, message):
148  if success:
149  self.loginfo(str(message))
150  else:
151  self.logwarn(str(message))
152  r = yocs_msgs.LocalizeResult()
153  r.success = success
154  r.message = message
155  self._as_localize.set_succeeded(r)
156 
157  def _update_tracker(self, enabled):
158  params = { 'enabled' : enabled}
159  config = self._client.update_configuration(params)
160 
161  def loginfo(self, msg):
162  rospy.loginfo('Localization Manager : ' + str(msg))
163 
164  def logwarn(self, msg):
165  rospy.logwarn('Localization Manager : ' + str(msg))
166 
167  def spin(self):
168  sleeptime = self.param['sleeptime']
169  self._as_localize.start()
170  while not rospy.is_shutdown():
171  rospy.sleep(sleeptime)
172  if self._thread:
173  self._thread.join()


yocs_localization_manager
Author(s): Jihoon Lee, Daniel Stonier
autogenerated on Mon Jun 10 2019 15:53:38