sr_move_hand_joint.py
Go to the documentation of this file.
1 #!/usr/bin/env python
2 
3 # Copyright 2019 Shadow Robot Company Ltd.
4 #
5 # This program is free software: you can redistribute it and/or modify it
6 # under the terms of the GNU General Public License as published by the Free
7 # Software Foundation version 2 of the License.
8 #
9 # This program is distributed in the hope that it will be useful, but WITHOUT
10 # ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 # FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
12 # more details.
13 #
14 # You should have received a copy of the GNU General Public License along
15 # with this program. If not, see <http://www.gnu.org/licenses/>.
16 
17 import rospy
18 from copy import deepcopy
19 from sr_robot_commander.sr_hand_commander import SrHandCommander
20 
21 
22 ####################
23 # POSE DEFINITIONS #
24 ####################
25 
26 joints = ["rh_THJ1", "rh_THJ2", "rh_THJ3", "rh_THJ4", "rh_THJ5",
27  "rh_FFJ1", "rh_FFJ2", "rh_FFJ3", "rh_FFJ4",
28  "rh_MFJ1", "rh_MFJ2", "rh_MFJ3", "rh_MFJ4",
29  "rh_RFJ1", "rh_RFJ2", "rh_RFJ3", "rh_RFJ4",
30  "rh_LFJ1", "rh_LFJ2", "rh_LFJ3", "rh_LFJ4", "rh_LFJ5",
31  "rh_WRJ1", "rh_WRJ2"]
32 
33 # starting position for the hand
34 start_pos = {"rh_THJ1": 0, "rh_THJ2": 0, "rh_THJ3": 0, "rh_THJ4": 0, "rh_THJ5": 0,
35  "rh_FFJ1": 0, "rh_FFJ2": 0, "rh_FFJ3": 0, "rh_FFJ4": 0,
36  "rh_MFJ1": 0, "rh_MFJ2": 0, "rh_MFJ3": 0, "rh_MFJ4": 0,
37  "rh_RFJ1": 0, "rh_RFJ2": 0, "rh_RFJ3": 0, "rh_RFJ4": 0,
38  "rh_LFJ1": 0, "rh_LFJ2": 0, "rh_LFJ3": 0, "rh_LFJ4": 0, "rh_LFJ5": 0,
39  "rh_WRJ1": 0, "rh_WRJ2": 0}
40 
41 # flex first finger
42 flex_ff = {"rh_FFJ1": 0, "rh_FFJ2": 0, "rh_FFJ3": 0, "rh_FFJ4": 0}
43 # extend first finger
44 ext_ff = {"rh_FFJ1": 0, "rh_FFJ2": 0, "rh_FFJ3": 0, "rh_FFJ4": 0}
45 
46 flex_mf = {"rh_MFJ1": 0, "rh_MFJ2": 0, "rh_MFJ3": 0, "rh_MFJ4": 0}
47 ext_mf = {"rh_MFJ1": 0, "rh_MFJ2": 0, "rh_MFJ3": 0, "rh_MFJ4": 0}
48 
49 flex_rf = {"rh_RFJ1": 0, "rh_RFJ2": 0, "rh_RFJ3": 0, "rh_RFJ4": 0}
50 ext_rf = {"rh_RFJ1": 0, "rh_RFJ2": 0, "rh_RFJ3": 0, "rh_RFJ4": 0}
51 
52 flex_lf = {"rh_LFJ1": 0, "rh_LFJ2": 0, "rh_LFJ3": 0, "rh_LFJ4": 0, "rh_LFJ5": 0}
53 ext_lf = {"rh_LFJ1": 0, "rh_LFJ2": 0, "rh_LFJ3": 0, "rh_LFJ4": 0, "rh_LFJ5": 0}
54 
55 flex_th = {"rh_THJ1": 0, "rh_THJ2": 0, "rh_THJ3": 0, "rh_THJ4": 0, "rh_THJ5": 0}
56 ext_th = {"rh_THJ1": 0, "rh_THJ2": 0, "rh_THJ3": 0, "rh_THJ4": 0, "rh_THJ5": 0}
57 
58 
60  flex = dict()
61  extend = dict()
62  finger = raw_input("select which finger to move (options: ff, mf, rf, lf, th default: ff): ")
63  if finger == "ff":
64  finger = "FF"
65  flex = deepcopy(flex_ff)
66  extend = deepcopy(ext_ff)
67  elif finger == "mf":
68  finger = "MF"
69  flex = deepcopy(flex_mf)
70  extend = deepcopy(ext_mf)
71  elif finger == "rf":
72  finger = "RF"
73  flex = deepcopy(flex_rf)
74  extend = deepcopy(ext_rf)
75  elif finger == "lf":
76  finger = "LF"
77  flex = deepcopy(flex_lf)
78  extend = deepcopy(ext_lf)
79  elif finger == "th":
80  finger = "TH"
81  flex = deepcopy(flex_th)
82  extend = deepcopy(ext_th)
83  else:
84  rospy.loginfo("Default finger 'ff' will be used.")
85  finger = "FF"
86  flex = deepcopy(flex_ff)
87  extend = deepcopy(ext_ff)
88 
89  correct_joint_number = False
90  while not correct_joint_number:
91  correct_joint_number = True
92  joint_number_str = raw_input("select joint number to move: ")
93  joint_number_int = int(joint_number_str)
94  if finger == "FF" or finger == "MF" or finger == "RF":
95  if joint_number_int not in range(5):
96  rospy.logerr("The finger you selected doesn't have joint {}".format(joint_number_int))
97  correct_joint_number = False
98  elif finger == "LF" or finger == "TH":
99  if joint_number_int not in range(6):
100  rospy.logerr("The finger you selected doesn't have joint {}".format(joint_number_int))
101  correct_joint_number = False
102 
103  return finger, flex, extend, joint_number_str, joint_number_int
104 
105 
107  rospy.sleep(1.2)
108  while True:
109  joints = list()
110  finger, flex, extend, joint_number_str, joint_number_int = select_finger()
111  if joint_number_int == 0:
112  joint_1 = "rh_" + finger + "J1"
113  joint_2 = "rh_" + finger + "J2"
114  else:
115  joint = "rh_" + finger + "J" + joint_number_str
116  flex_degrees = raw_input("Select flex position in degrees (default 180): ")
117  try:
118  if flex_degrees:
119  flex_degrees_float = float(flex_degrees)
120  if joint_number_int == 0:
121  flex[joint_1] = flex_degrees_float / 2.0
122  flex[joint_2] = flex_degrees_float / 2.0
123  else:
124  flex[joint] = flex_degrees_float
125  else:
126  if joint_number_int == 0:
127  flex[joint_1] = 90
128  flex[joint_2] = 90
129  else:
130  flex[joint] = 90
131  except ValueError:
132  rospy.logerr("You didn't give a valid value")
133  continue
134 
135  ext_degrees = raw_input("Select extension position in degrees (default 0): ")
136  try:
137  if ext_degrees:
138  ext_degrees_float = float(ext_degrees)
139  if joint_number_int == 0:
140  extend[joint_1] = ext_degrees_float / 2.0
141  extend[joint_2] = ext_degrees_float / 2.0
142  else:
143  extend[joint] = ext_degrees_float
144  else:
145  if joint_number_int == 0:
146  extend[joint_1] = 0
147  extend[joint_2] = 0
148  else:
149  extend[joint] = 0
150  except ValueError:
151  rospy.logerr("You didn't give a valid value")
152  continue
153 
154  time_raw = raw_input("Select the time for each movement in seconds (default 1.0): ")
155  try:
156  if time_raw:
157  time = float(time_raw)
158  else:
159  time = 1.0
160  except ValueError:
161  rospy.logerr("You didn't give a valid value")
162  continue
163 
164  while True:
165  hand_commander.move_to_joint_value_target_unsafe(flex, time, True, angle_degrees=True)
166  rospy.sleep(1.0)
167  hand_commander.move_to_joint_value_target_unsafe(extend, time, True, angle_degrees=True)
168 
169  user_input = raw_input(
170  "Press return to run again, 'change' to change parameters or 'exit' to exit the program: ")
171  if user_input == 'exit':
172  return
173  elif user_input == 'change':
174  break
175 
176 
177 if __name__ == "__main__":
178  rospy.init_node("right_hand_demo", anonymous=True)
179  hand_commander = SrHandCommander(name="right_hand")
180 
181  sequence_ff()
182  rospy.loginfo("Demo finished!")


sr_example
Author(s): Ugo Cupcic
autogenerated on Wed Oct 14 2020 04:05:12