sr_robot_state_exporter.py
Go to the documentation of this file.
1 # Copyright 2019 Shadow Robot Company Ltd.
2 #
3 # This program is free software: you can redistribute it and/or modify it
4 # under the terms of the GNU General Public License as published by the Free
5 # Software Foundation version 2 of the License.
6 #
7 # This program is distributed in the hope that it will be useful, but WITHOUT
8 # ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
9 # FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
10 # more details.
11 #
12 # You should have received a copy of the GNU General Public License along
13 # with this program. If not, see <http://www.gnu.org/licenses/>.
14 
15 import rospy
16 import pprint
17 from copy import deepcopy
18 
19 from moveit_msgs.srv import CheckIfRobotStateExistsInWarehouse as HasState
20 from moveit_msgs.srv import GetRobotStateFromWarehouse as GetState
21 from moveit_msgs.srv import SaveRobotStateToWarehouse as SaveState
22 from moveit_msgs.srv import ListRobotStatesInWarehouse as ListState
23 from moveit_msgs.msg import RobotState
24 
25 
26 class SrRobotStateExporter(object):
27  def __init__(self, start_dictionary={}):
28  self._get_state = rospy.ServiceProxy("/get_robot_state", GetState)
29  self._has_state = rospy.ServiceProxy("/has_robot_state", HasState)
30  self._list_states = rospy.ServiceProxy("/list_robot_states", ListState)
31  self._save_state = rospy.ServiceProxy("/save_robot_state", SaveState)
32  self._dictionary = deepcopy(start_dictionary)
33 
34  def extract_list(self, list_of_states):
35  for name in list_of_states:
36  self.extract_one_state(name)
37 
38  def extract_one_state(self, name):
39  if self._has_state(name, '').exists:
40  state = self._get_state(name, '').state.joint_state
41  names = state.name
42  position = state.position
43  self._dictionary[name] = {name: position[n] for n, name in enumerate(names)}
44  else:
45  rospy.logerr("State %s not present in the warehouse." % name)
46 
47  def extract_from_trajectory(self, dictionary_trajectory):
48  for entry in dictionary_trajectory:
49  if 'name' in entry:
50  self.extract_one_state(entry['name'])
51 
52  def extract_all(self):
53  for state in self._list_states("", "").states:
54  self.extract_one_state(state)
55 
56  def output_module(self, file_name):
57  pp = pprint.PrettyPrinter()
58  with open(file_name, "w") as output:
59  output.write('warehouse_states = %s\n' % pp.pformat(self._dictionary))
60 
61  def convert_trajectory(self, named_trajectory):
62  new_trajectory = []
63  for entry in named_trajectory:
64  new_entry = deepcopy(entry)
65  if 'name' in new_entry:
66  if new_entry['name'] in self._dictionary:
67  new_entry['joint_angles'] = self._dictionary[new_entry['name']]
68  new_entry.pop('name')
69  else:
70  rospy.logwarn("Entry named %s not present in dictionary. Not replacing." % name)
71  new_trajectory.append(new_entry)
72  return new_trajectory
73 
75  for name in self._dictionary:
76  if self._has_state(name, '').exists:
77  rospy.logwarn("State named %s already in warehouse, not re-adding." % name)
78  else:
79  state = RobotState()
80  state.joint_state.name = self._dictionary[name].keys()
81  state.joint_state.position = self._dictionary[name].values()
82  self._save_state(name, '', state)


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