sr_robot_state_combiner.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 from moveit_msgs.srv import SaveRobotStateToWarehouse as SaveState
18 from moveit_msgs.srv import GetRobotStateFromWarehouse as GetState
19 from moveit_msgs.srv import GetRobotStateFromWarehouseResponse as GetStateResp
20 
21 
22 class SrRobotStateCombiner(object):
23 
24  _arm_joints = ['shoulder_pan_joint', 'elbow_joint', 'shoulder_lift_joint',
25  'wrist_1_joint', 'wrist_2_joint', 'wrist_3_joint', 'WRJ2', 'WRJ1']
26 
27  def __init__(self, arm_state_name, hand_state_name, new_state_name, robot_name="ur10srh"):
28  self._save = rospy.ServiceProxy(
29  'save_robot_state', SaveState)
30 
31  self._read = rospy.ServiceProxy(
32  'get_robot_state', GetState)
33 
34  self._arm_state_name = arm_state_name
35  self._hand_state_name = hand_state_name
36  self._new_state_name = new_state_name
37  self._robot_name = robot_name
38 
39  def _filter_joints(self, state, remove_arm=True):
40  """
41  Filters out the joints that we don't want and their positions
42  :param state: initial state
43  :param remove_arm: If True we remove the arm joints, if False we remove the non-arm joints (hand joints)
44  :return: the filtered state
45  """
46  name = []
47  position = []
48  for n, pos in zip(state.state.joint_state.name, state.state.joint_state.position):
49  if (remove_arm and all(s not in n for s in self._arm_joints))\
50  or (not remove_arm and any(s in n for s in self._arm_joints)):
51  name.append(n)
52  position.append(pos)
53  state.state.joint_state.name = name
54  state.state.joint_state.position = position
55  return state
56 
57  def combine(self):
58  if self._arm_state_name == "NONE":
59  arm_state = GetStateResp()
60  else:
61  arm_state = self._read(self._arm_state_name, self._robot_name)
62 
63  if self._hand_state_name == "NONE":
64  hand_state = GetStateResp()
65  else:
66  hand_state = self._read(self._hand_state_name, self._robot_name)
67 
68  arm_state = self._filter_joints(arm_state, False)
69  hand_state = self._filter_joints(hand_state, True)
70 
71  combined_state = arm_state
72  combined_state.state.joint_state.name += hand_state.state.joint_state.name
73  combined_state.state.joint_state.position += hand_state.state.joint_state.position
74 
75  self._save(self._new_state_name, self._robot_name, combined_state.state)
def __init__(self, arm_state_name, hand_state_name, new_state_name, robot_name="ur10srh")


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