pr2_move_base.py
Go to the documentation of this file.
1 #! /usr/bin/python
2 #***********************************************************
3 #* Software License Agreement (BSD License)
4 #*
5 #* Copyright (c) 2009, Willow Garage, Inc.
6 #* All rights reserved.
7 #*
8 #* Redistribution and use in source and binary forms, with or without
9 #* modification, are permitted provided that the following conditions
10 #* are met:
11 #*
12 #* * Redistributions of source code must retain the above copyright
13 #* notice, this list of conditions and the following disclaimer.
14 #* * Redistributions in binary form must reproduce the above
15 #* copyright notice, this list of conditions and the following
16 #* disclaimer in the documentation and/or other materials provided
17 #* with the distribution.
18 #* * Neither the name of Willow Garage, Inc. nor the names of its
19 #* contributors may be used to endorse or promote products derived
20 #* from this software without specific prior written permission.
21 #*
22 #* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
23 #* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
24 #* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
25 #* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
26 #* COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
27 #* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
28 #* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
29 #* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
30 #* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31 #* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
32 #* ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
33 #* POSSIBILITY OF SUCH DAMAGE.
34 #*
35 #* Author: Eitan Marder-Eppstein
36 #***********************************************************
37 
38 PKG = "pr2_move_base"
39 
40 import roslib; roslib.load_manifest(PKG)
41 
42 import rospy
43 import actionlib
44 from std_msgs.msg import Bool
45 from pr2_msgs.msg import LaserTrajCmd
46 from pr2_msgs.msg import PowerState
47 from pr2_msgs.srv import SetLaserTrajCmd
48 from actionlib_msgs.msg import GoalStatus
49 from move_base_msgs.msg import MoveBaseAction, MoveBaseGoal, MoveBaseResult, MoveBaseFeedback
50 from pr2_controllers_msgs.msg import SingleJointPositionAction, SingleJointPositionGoal
51 from pr2_common_action_msgs.msg import TuckArmsAction, TuckArmsGoal
52 import dynamic_reconfigure.client
53 from pr2_controllers_msgs.msg import PointHeadAction, PointHeadGoal
54 
55 def feedback_cb(feedback):
56  server.publish_feedback(feedback)
57 
58 def set_tilt_profile(position, time_from_start):
59  cmd = LaserTrajCmd()
60  cmd.profile = 'blended_linear'
61  cmd.position = position
62  cmd.time_from_start = [rospy.Time.from_sec(x) for x in time_from_start]
63  cmd.max_velocity = 10
64  cmd.max_acceleration = 30
65  try:
66  tilt_profile_client.call(cmd)
67  except rospy.ServiceException, e:
68  rospy.logerr("Couldn't set the profile on the laser. Exception %s" % e)
69  return False
70  return True
71 
73  head_client = actionlib.SimpleActionClient('head_traj_controller/point_head_action', PointHeadAction)
74  head_client.wait_for_server()
75  point_head_goal = PointHeadGoal()
76  point_head_goal.target.header.frame_id = 'base_link'
77  point_head_goal.target.point.x = 3.0
78  point_head_goal.target.point.y = 0.0
79  point_head_goal.target.point.z = 1.0
80  point_head_goal.pointing_frame = 'head_tilt_link'
81  point_head_goal.pointing_axis.x = 1
82  point_head_goal.pointing_axis.y = 0
83  point_head_goal.pointing_axis.z = 0
84 
85  head_client.send_goal(point_head_goal)
86  head_client.wait_for_result(rospy.Duration(5.0))
87 
88 
90  #TODO: remove hack to get things working in gazebo
91  try:
92  rospy.wait_for_service('tilt_hokuyo_node/set_parameters', 1.0)
93  except rospy.exceptions.ROSException, e:
94  rospy.logerr("Couldn't set parameters %s" % e)
95  return
96  #end TODO
97 
98  client = dynamic_reconfigure.client.Client('tilt_hokuyo_node')
99  global old_config
100  old_config = client.get_configuration(2.0)
101  new_config = {'skip': 0, 'intensity': 0, 'min_ang': -1.57, 'max_ang': 1.57, 'calibrate_time': 1, 'cluster': 1, 'time_offset': 0.0}
102  rospy.loginfo('Setting laser to the navigation configuration: %s' % new_config)
103  client.update_configuration(new_config)
104 
106  #TODO: remove hack to get things working in gazebo
107  try:
108  rospy.wait_for_service('tilt_hokuyo_node/set_parameters', 1.0)
109  except rospy.exceptions.ROSException, e:
110  rospy.logerr("Couldn't set the profile on the laser. Exception %s" % e)
111  return
112  #end TODO
113 
114  rospy.loginfo('Setting laser back to this configuration: %s' % old_config)
115  client = dynamic_reconfigure.client.Client('tilt_hokuyo_node')
116  client.update_configuration(old_config)
117 
119  restore_laser()
120  set_tilt_profile([0.0, 0.0], [0.0, 1.0])
121 
122 def execute_cb(goal):
123  rospy.loginfo("Received a goal")
124 
125  # check for network cable
126  if network_connected:
127  server.set_aborted(None, "Dangerous to navigate with network cable connected.")
128  return
129 
130  # check for power cable
131  if ac_power_connected:
132  server.set_aborted(None, "Dangerous to navigate with AC power cable connected.")
133  return
134 
135  # check for power cable
136  # TODO
137 
138  # start the arms tucking
139  tuck_arms_client = actionlib.SimpleActionClient('tuck_arms', TuckArmsAction)
140  tuck_arms_client.wait_for_server()
141  tuck_goal = TuckArmsGoal()
142  tuck_goal.tuck_left=True
143  tuck_goal.tuck_right=True
144  tuck_arms_client.send_goal(tuck_goal)
145 
146  # start the torso lowering
147  torso_client = actionlib.SimpleActionClient('torso_controller/position_joint_action', SingleJointPositionAction)
148  torso_client.wait_for_server()
149  torso_down_goal = SingleJointPositionGoal()
150  torso_down_goal.position = 0
151  torso_client.send_goal(torso_down_goal)
152 
153  # configure the tilting laser
154  if not set_tilt_profile([1.05, -.7, 1.05], [0.0, 1.8, 2.0125 + .3]):
155  server.set_aborted(MoveBaseResult(), "Couldn't set the profile on the laser")
156  return
157 
160 
161  # wait for tuck_arms to finish. (Don't wait for torso to lower, that is a Nice To Have)
162  while not tuck_arms_client.wait_for_result(rospy.Duration(0.1)):
163  if server.is_preempt_requested():
164  if not server.is_new_goal_available():
165  tuck_arms_client.cancel_goal()
166 
167  tuck_state = tuck_arms_client.get_state()
168  if tuck_state != GoalStatus.SUCCEEDED:
169  if tuck_state == GoalStatus.PREEMPTED:
170  server.set_preempted(MoveBaseResult(), "Tuck-arms preempted")
171  elif tuck_state == GoalStatus.ABORTED:
172  server.set_aborted(MoveBaseResult(), "Tuck-arms aborted")
173  return
174 
175  # Now everything should be ready so send the navigation goal.
176  move_base_client.send_goal(goal, None, None, feedback_cb)
177 
178  while not move_base_client.wait_for_result(rospy.Duration(0.1)):
179  #in the unlikely event of network cable being plugged in while moving, stop moving.
180  if network_connected:
181  move_base_client.cancel_goal()
182  server.set_aborted(None, "Dangerous to navigate with network cable connected.")
183  return
184 
185  #in the unlikely event of ac power cable being plugged in while moving, stop moving.
186  if ac_power_connected:
187  move_base_client.cancel_goal()
188  server.set_aborted(None, "Dangerous to navigate with ac power cable connected.")
189  return
190 
191  if server.is_preempt_requested():
192  if not server.is_new_goal_available():
193  rospy.loginfo("Preempt requested without new goal, cancelling move_base goal.")
194  move_base_client.cancel_goal()
195 
196  server.set_preempted(MoveBaseResult(), "Got preempted by a new goal")
197  return
198 
199 
200  terminal_state = move_base_client.get_state()
201  result = move_base_client.get_result()
202  if terminal_state == GoalStatus.PREEMPTED:
203  server.set_preempted(result)
204  elif terminal_state == GoalStatus.SUCCEEDED:
205  server.set_succeeded(result)
206  elif terminal_state == GoalStatus.ABORTED:
207  server.set_aborted(result)
208  else:
209  server.set_aborted(result, "Unknown result from move_base")
210 
211 def handle_network_connected( connectedness_msg ):
212  global network_connected
213  network_connected = connectedness_msg.data
214 
215 def handle_power_state( power_state_msg ):
216  global ac_power_connected
217  ac_power_connected = (power_state_msg.AC_present > 0)
218 
219 if __name__ == '__main__':
220  name = 'pr2_move_base'
221  rospy.init_node(name)
222 
223  #listen for messages about the network cable being plugged in.
224  global network_connected
225  network_connected = False #default presumes we are fine if we never hear anything.
226  rospy.Subscriber("network/connected", Bool, handle_network_connected)
227 
228  #listen for messages about the power cable being plugged in.
229  global ac_power_connected
230  ac_power_connected = False #default presumes we are fine if we never hear anything.
231  rospy.Subscriber("power_state", PowerState, handle_power_state)
232 
233  #create the action client to move_base
234  move_base_client = actionlib.SimpleActionClient('move_base', MoveBaseAction)
235  move_base_client.wait_for_server()
236  move_base_goal = MoveBaseGoal()
237 
238  #create a client to the tilt laser
239  rospy.wait_for_service('laser_tilt_controller/set_traj_cmd')
240  tilt_profile_client = rospy.ServiceProxy('laser_tilt_controller/set_traj_cmd', SetLaserTrajCmd)
241 
242  server = actionlib.SimpleActionServer(name, MoveBaseAction, execute_cb)
243  rospy.loginfo('%s: Action server running', name)
244 
245  global old_config
246  old_config = {}
247 
248  #we'll set the tilt profile and configure the laser by default when we start up
249  #NOTE: IF YOU CHANGE THIS, ALSO MAKE SURE TO CHANGE THE tilt_lasers_filters.yaml FILE IN pr2_navigation_perception
250  set_tilt_profile([1.05, -.7, 1.05], [0.0, 1.8, 2.0125 + .3])
252 
253  rospy.on_shutdown(on_shutdown)
254 
255  rospy.spin()
def feedback_cb(feedback)
def configure_head()
def handle_power_state(power_state_msg)
def set_tilt_profile(position, time_from_start)
def handle_network_connected(connectedness_msg)
def configure_laser()
def execute_cb(goal)


pr2_move_base
Author(s): Eitan Marder-Eppstein
autogenerated on Mon Jun 10 2019 14:28:42