named_trajectory_services.py
Go to the documentation of this file.
1 #!/usr/bin/env python
2 # Copyright 2019 Shadow Robot Company Ltd.
3 #
4 # This program is free software: you can redistribute it and/or modify it
5 # under the terms of the GNU General Public License as published by the Free
6 # Software Foundation version 2 of the License.
7 #
8 # This program is distributed in the hope that it will be useful, but WITHOUT
9 # ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
10 # FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
11 # more details.
12 #
13 # You should have received a copy of the GNU General Public License along
14 # with this program. If not, see <http://www.gnu.org/licenses/>.
15 
16 import rospy
17 
18 from sr_robot_msgs.srv import PlanTrajectoryFromList as PlanFromList
19 from sr_robot_msgs.srv import PlanTrajectoryFromPrefix as PlanFromPrefix
20 from sr_robot_msgs.srv import ExecutePlannedTrajectory as ExecutePlan
21 from sr_robot_msgs.srv import PlanNamedTrajectory as PlanNamed
22 from sr_robot_msgs.srv import ListNamedTrajectories as ListNamed
23 from functools import partial
24 from copy import deepcopy
25 
26 
27 class WaypointNamedServices(object):
28  def __init__(self):
29 
30  rospy.init_node('waypoint_named_services')
31 
32  rospy.loginfo("Waiting for planning services...")
33  rospy.wait_for_service('plan_trajectory_from_list')
34  rospy.wait_for_service('plan_trajectory_from_prefix')
35  rospy.wait_for_service('execute_planned_trajectory')
36 
37  self.__from_list = rospy.ServiceProxy(
38  'plan_trajectory_from_list', PlanFromList)
39  self.__from_prefix = rospy.ServiceProxy(
40  'plan_trajectory_from_prefix', PlanFromPrefix)
41  self.__execute_plan = rospy.ServiceProxy(
42  'execute_planned_trajectory', PlanFromPrefix)
43  rospy.loginfo("Service proxies connected.")
44 
45  self.service_mapping = rospy.get_param("~service_mapping")
46  self.define_services()
47 
48  def __plan_named_trajectory(self, req):
49  mapping = [
50  x for x in self.service_mapping if x["name"] ==
51  req.name]
52  if len(mapping) > 1:
53  rospy.logfatal("Trajectory name is not unique")
54  elif len(mapping) is 0:
55  rospy.logfatal("Trajectory name does not exist")
56  else:
57  m = mapping[0]
58 
59  if "list" not in m.keys() and "prefix" not in m.keys():
60  rospy.logfatal("Service must specify either prefix " +
61  "or list for choosing waypoints")
62  elif "list" in m.keys() and "prefix" in m.keys():
63  rospy.logfatal("Service mapping has both list and " +
64  "prefix specified. Can only be one.")
65  else:
66  if "list" in m.keys():
67  return self.__from_list(m["list"]).success
68  else:
69  return self.__from_prefix(m["prefix"]).success
70 
71  return False
72 
73  def __list_named_trajectories(self, req):
74  return [map(lambda x:x["name"], self.service_mapping)]
75 
76  def define_services(self):
77  plan_service_name = rospy.get_param("~plan_named_trajectory_service")
78  list_service_name = rospy.get_param("~list_named_trajectories_service")
79 
80  self.__plan_server = rospy.Service(plan_service_name, PlanNamed,
82  self.__list_server = rospy.Service(list_service_name, ListNamed,
84 
85 if __name__ == "__main__":
87  rospy.spin()


sr_robot_commander
Author(s): Andriy Petlovanyy
autogenerated on Wed Oct 14 2020 04:05:30