sexy_example_latching.py
Go to the documentation of this file.
00001 #!/usr/bin/env python
00002 #
00003 # Copyright 2011 Shadow Robot Company Ltd.
00004 #
00005 # This program is free software: you can redistribute it and/or modify it
00006 # under the terms of the GNU General Public License as published by the Free
00007 # Software Foundation, either version 2 of the License, or (at your option)
00008 # any later version.
00009 #
00010 # This program is distributed in the hope that it will be useful, but WITHOUT
00011 # ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
00012 # FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
00013 # more details.
00014 #
00015 # You should have received a copy of the GNU General Public License along
00016 # with this program.  If not, see <http://www.gnu.org/licenses/>.
00017 #
00018 
00019 # This program is a simple script used for demos. It runs the hand
00020 # through different given poses.
00021 
00022 # It is a good example on how to use the latching feature of the
00023 # ros publisher to make sure a data is received even if we don't stream
00024 # the data.
00025 
00026 import roslib; roslib.load_manifest('sr_example')
00027 import rospy
00028 
00029 import time
00030 import math
00031 from std_msgs.msg import Float64
00032 
00033 class SexyExampleLatching(object):
00034     
00035     # type of controller that is running 
00036     # If you use the simulated hand (in gazebo) use the mixed controllers
00037     controller_type = "_mixed_position_velocity_controller"
00038     # If you use the real hand, generally use the position controller (comment the previous line and uncomment the following)
00039     #controller_type = "_position_controller"
00040     
00041     def __init__(self):
00042 
00043         self.hand_publishers = self.create_hand_publishers()
00044 
00045         self.sleep_time = 3.0
00046 
00047     def run(self):
00048         """
00049         Runs the hand through different predefined position in a given order.
00050         """
00051         start = {"FFJ0":0, "FFJ3":0, "FFJ4":0,
00052                  "MFJ0":0, "MFJ3":0, "MFJ4":0,
00053                  "RFJ0":0, "RFJ3":0, "RFJ4":0,
00054                  "LFJ0":0, "LFJ3":0, "LFJ4":0, "LFJ5":0,
00055                  "THJ1":0, "THJ2":0, "THJ3":0, "THJ4":0, "THJ5":0,
00056                  "WRJ1":0, "WRJ2":0 }
00057 
00058         fist = {"FFJ0":180, "FFJ3":90, "FFJ4":0,
00059                 "MFJ0":180, "MFJ3":90, "MFJ4":0,
00060                 "RFJ0":180, "RFJ3":90, "RFJ4":0,
00061                 "LFJ0":180, "LFJ3":90, "LFJ4":0, "LFJ5":0,
00062                 "THJ1":0, "THJ2":0, "THJ3":0, "THJ4":50, "THJ5":-50,
00063                 "WRJ1":0, "WRJ2":0 }
00064 
00065         victory = {"FFJ0":0, "FFJ3":0, "FFJ4":-20,
00066                    "MFJ0":0, "MFJ3":0, "MFJ4":20,
00067                    "RFJ0":180, "RFJ3":90, "RFJ4":-10,
00068                    "LFJ0":180, "LFJ3":90, "LFJ4":-10, "LFJ5":0,
00069                    "THJ1":40, "THJ2":20, "THJ3":0, "THJ4":50, "THJ5":35,
00070                    "WRJ1":0, "WRJ2":0 }
00071 
00072         wave_1 = {"WRJ2":-20}
00073         wave_2 = {"WRJ2":5}
00074 
00075 
00076         self.publish_pose(start)
00077         time.sleep(self.sleep_time)
00078         
00079         self.publish_pose(fist)
00080         time.sleep(self.sleep_time)
00081 
00082         self.publish_pose(start)
00083         time.sleep(self.sleep_time)
00084 
00085         self.publish_pose(victory)
00086         time.sleep(self.sleep_time)
00087 
00088         self.publish_pose(start)
00089         time.sleep(self.sleep_time)
00090 
00091         for i in range(0,4):
00092             self.publish_pose(wave_1)
00093             time.sleep(self.sleep_time)
00094             self.publish_pose(wave_2)
00095             time.sleep(self.sleep_time)
00096 
00097 
00098     def publish_pose(self, pose):
00099         """
00100         Publish a given pose.
00101         """
00102         for joint, pos in pose.iteritems():
00103             self.hand_publishers[joint].publish(math.radians(pos))
00104     
00105     def create_hand_publishers(self):
00106         """
00107         Creates a dictionary of publishers to send the targets to the controllers
00108         on /sh_??j?_mixed_position_velocity_controller/command
00109         or /sh_??j?_position_controller/command
00110         """
00111         hand_pub = {}
00112 
00113         for joint in ["FFJ0", "FFJ3", "FFJ4",
00114                       "MFJ0", "MFJ3", "MFJ4",
00115                       "RFJ0", "RFJ3", "RFJ4",
00116                       "LFJ0", "LFJ3", "LFJ4", "LFJ5",
00117                       "THJ1", "THJ2", "THJ3", "THJ4", "THJ5",
00118                       "WRJ1", "WRJ2" ]:
00119             # Here we initialize the publisher with the latch set to True.
00120             # this will ensure that the hand gets the message, even though we're
00121             # using the messages more as a service (we don't stream the data, we
00122             # just ask the hand to take a given position)
00123             hand_pub[joint] = rospy.Publisher('sh_'+joint.lower() + self.controller_type + '/command', Float64, latch=True)
00124 
00125         return hand_pub
00126 
00127 def main():
00128     rospy.init_node('sexy_example_latching', anonymous = True)
00129     sexy_example = SexyExampleLatching()
00130     sexy_example.run()
00131 
00132 if __name__ == '__main__':
00133     main()


sr_example
Author(s): Ugo Cupcic
autogenerated on Mon Oct 6 2014 07:49:30