sr_latching_example.py
Go to the documentation of this file.
1 #!/usr/bin/env python
2 #
3 # Copyright 2011 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 """
18 This program is a simple script that runs the hand through different given poses.
19 
20 It is a good example on how to use the latching feature of the
21 ros publisher to make sure a data is received even if we don't stream
22 the data.
23 
24 """
25 
26 import rospy
27 
28 import time
29 from std_msgs.msg import Float64
30 
31 
32 class LatchingExample(object):
33  # type of controller that is running
34  controller_type = "_position_controller"
35 
36  def __init__(self):
37 
38  # Prefix of hand, can be lh for left or rh for right
39  self.prefix = 'rh'
40 
41  self.keys = ['FFJ0', 'FFJ3', 'FFJ4',
42  'MFJ0', 'MFJ3', 'MFJ4',
43  'RFJ0', 'RFJ3', 'RFJ4',
44  'LFJ0', 'LFJ3', 'LFJ4', 'LFJ5',
45  'THJ1', 'THJ2', 'THJ3', 'THJ4', 'THJ5',
46  'WRJ1', 'WRJ2']
47 
48  self.keys_prefixed = ["{0}_{1}".format(self.prefix, joint)
49  for joint in self.keys]
50 
52 
53  self.sleep_time = 3.0
54 
55  def run(self):
56  """
57  Runs the hand through different predefined position in a given order.
58  """
59 
60  start = [0.00, 0.00, 0.00, 0.00,
61  0.00, 0.00, 0.00, 0.00,
62  0.00, 0.00, 0.00, 0.00,
63  0.00, 0.00, 0.00, 0.00, 0.00,
64  0.00, 0.00, 0.00, 0.00, 0.00,
65  0.00, 0.00]
66 
67  fist = [3.14, 1.57, 0.00,
68  3.14, 1.57, 0.00,
69  3.14, 1.57, 0.00,
70  3.4, 1.57, 0.00, 0.00,
71  1.33, 0.00, 1.15, 0.26,
72  0.00, 0.00]
73 
74  victory = [0.00, 0.00, -0.35,
75  0.00, 0.00, 0.35,
76  3.14, 1.57, -0.17,
77  3.14, 1.57, -0.17, 0.00,
78  1.05, 0.00, 0.87, 0.61,
79  0.00, 0.00]
80 
81  wave_1 = [-0.35]
82  wave_2 = [0.09]
83 
84  start_pose = dict(zip(self.keys_prefixed, start))
85  fist_pose = dict(zip(self.keys_prefixed, fist))
86  victory_pose = dict(zip(self.keys_prefixed, victory))
87  wave_1_pose = dict(zip(self.keys_prefixed[-1:], wave_1))
88  wave_2_pose = dict(zip(self.keys_prefixed[-1:], wave_2))
89 
90  # publish each pose and sleep for time 'sleep_time' between each command
91  self.publish_pose(start_pose)
92  time.sleep(self.sleep_time)
93 
94  self.publish_pose(fist_pose)
95  time.sleep(self.sleep_time)
96 
97  self.publish_pose(start_pose)
98  time.sleep(self.sleep_time)
99 
100  self.publish_pose(victory_pose)
101  time.sleep(self.sleep_time)
102 
103  self.publish_pose(start_pose)
104  time.sleep(self.sleep_time)
105 
106  for _ in range(4):
107  self.publish_pose(wave_1_pose)
108  time.sleep(self.sleep_time/2)
109  self.publish_pose(wave_2_pose)
110  time.sleep(self.sleep_time/2)
111 
112  def publish_pose(self, pose):
113  """
114  Publish a given pose.
115  """
116  for joint, pos in pose.iteritems():
117  self.hand_publishers[joint].publish(pos)
118 
119  def create_hand_publishers(self, keys_prefixed):
120  """
121  Creates a dictionary of publishers to send the targets to the controllers
122  on /sh_??_??j?_mixed_position_velocity_controller/command
123  or /sh_??_??j?_position_controller/command
124  """
125  hand_pub = {}
126 
127  for joint in keys_prefixed:
128  # Here we initialize the publisher with the latch set to True.
129  # this will ensure that the hand gets the message, even though we're
130  # using the messages more as a service (we don't stream the data, we
131  # just ask the hand to take a given position)
132  hand_pub[joint] = rospy.Publisher(
133  'sh_' + joint.lower() + self.controller_type + '/command', Float64,
134  latch=True, queue_size=1)
135 
136  return hand_pub
137 
138 
139 def main():
140  rospy.init_node('sr_latching_example', anonymous=True)
141  example = LatchingExample()
142  example.run()
143 
144 
145 if __name__ == '__main__':
146  main()
def create_hand_publishers(self, keys_prefixed)


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