sr_robot_state_saver.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 from sys import argv
17 
18 import rospy
19 from moveit_msgs.srv import SaveRobotStateToWarehouse as SaveState
20 from sensor_msgs.msg import JointState
21 from moveit_msgs.msg import RobotState
22 from sr_arm_commander import SrArmCommander
23 from sr_hand_commander import SrHandCommander
24 from sr_robot_commander import SrRobotCommander
25 from sr_utilities.hand_finder import HandFinder
26 from controller_manager_msgs.srv import ListControllers
27 from control_msgs.msg import JointTrajectoryControllerState
28 from threading import Lock
29 
30 
31 class SrStateSaverUnsafe(object):
32  def __init__(self, name, hand_or_arm="both", hand_h=False, save_target=False):
33 
34  self._save = rospy.ServiceProxy(
35  'save_robot_state', SaveState)
36 
37  self._name = name
38  self._hand_h = hand_h
39  self._save_target = save_target
40 
41  if name is None or name == '':
42  raise ValueError("Cannot save with empty name.")
43 
44  if hand_or_arm == "arm":
45  self._commander = SrArmCommander()
46  if not self._commander.arm_found():
47  raise ValueError("'No arm found.'")
48 
49  elif hand_or_arm == 'hand':
50  self._commander = SrHandCommander()
51  else:
52  double_error = []
53  try:
54  self._commander = SrHandCommander()
55  except Exception as e:
56  double_error.append(str(e))
57 
58  self._arm_commander = SrArmCommander()
59 
60  if not self._arm_commander.arm_found():
61  double_error.append("'No arm found.'")
62 
63  if len(double_error) != 0:
64  raise ValueError(" ".join(double_error))
65 
66  self._save_hand = (hand_or_arm == "hand" or hand_or_arm == "both")
67  self._save_arm = (hand_or_arm == "arm" or hand_or_arm == "both")
68 
69  if save_target:
70  rospy.loginfo("Saving targets instead of current values")
71  self._mutex = Lock()
72  self._target_values = dict()
73  if self._save_hand:
74  prefix = "H1" if hand_h else "rh"
75  self._hand_subscriber = rospy.Subscriber("/" + prefix + "_trajectory_controller/state",
76  JointTrajectoryControllerState, self._target_cb)
77  if self._save_arm:
78  self._arm_subscriber = rospy.Subscriber("/ra_trajectory_controller/state",
79  JointTrajectoryControllerState, self._target_cb)
80 
81  self._hand_or_arm = hand_or_arm
82 
83  rs = RobotState()
84 
85  current_dict = {}
86 
87  rospy.loginfo("Getting current")
88 
89  if self._hand_or_arm == "both":
90  current_dict = self._commander.get_robot_state_bounded()
91  robot_name = self._commander.get_robot_name()
92  elif self._hand_or_arm == "arm" or self._hand_or_arm == "hand":
93  current_dict = self._commander.get_current_state_bounded()
94  robot_name = self._commander.get_robot_name()
95  else:
96  rospy.logfatal("Unknown save type")
97  exit(-1)
98 
99  if self._save_target:
100  rospy.loginfo("Getting targets")
101  waiting_for_targets = True
102  while waiting_for_targets and not rospy.is_shutdown():
103  self._mutex.acquire()
104  waiting_for_targets = False
105  for joint in current_dict:
106  if joint in self._target_values:
107  current_dict[joint] = self._target_values[joint]
108  else:
109  waiting_for_targets = True
110  rospy.loginfo("Still waiting for %s target" % joint)
111  self._mutex.release()
112  if waiting_for_targets:
113  rospy.loginfo(self._target_values)
114  rospy.sleep(1)
115  if rospy.is_shutdown():
116  exit(0)
117 
118  rospy.loginfo(current_dict)
119  rs.joint_state = JointState()
120  rs.joint_state.name = current_dict.keys()
121  rs.joint_state.position = current_dict.values()
122  rospy.logwarn(rs)
123  self._save(self._name, robot_name, rs)
124 
125  def _target_cb(self, data):
126  self._mutex.acquire()
127  for n, joint in enumerate(data.joint_names):
128  self._target_values[joint] = data.desired.positions[n]
129  self._mutex.release()
def __init__(self, name, hand_or_arm="both", hand_h=False, save_target=False)


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