grasps_interpoler.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 
00020 class GraspInterpoler(object):
00021     def __init__(self, grasp_from, grasp_to="current"):
00022         """
00023         interpolate from one grasp to another
00024 
00025         Keyword arguments
00026         grasp_from -- goes from this grasp
00027         grasp_to -- to this grasp (if not defined, then consider
00028         the current position of the hand as a grasp)
00029         """
00030         self.percentage = 0
00031         self.grasp_from = grasp_from
00032         self.grasp_to = grasp_to
00033 
00034         # store the joints in common between the 2 grasps, with a table
00035         # [ starting_value, end_value ]
00036         self.from_grasp1_to_grasp2 = {}
00037 
00038         for joint in self.grasp_from.joints_and_positions.keys():
00039             # if joint in self.grasp_to.joints_and_positions.keys():
00040             self.from_grasp1_to_grasp2.update({
00041                 joint: [self.grasp_from.joints_and_positions.get(joint),
00042                         self.grasp_to.joints_and_positions.get(joint)]})
00043 
00044     def set_percentage_grasp1_to_grasp2(self, percentage):
00045         """
00046         move to a given percentage between grasp1 and grasp2
00047 
00048         Keyword arguments
00049         percentage -- must be between 0 and 100
00050         """
00051         self.percentage = percentage
00052         positions_to_send = {}
00053 
00054         for joint in self.from_grasp1_to_grasp2.iteritems():
00055             new_target = percentage / 100.0 * (joint[1][1] - joint[1][0])
00056             positions_to_send.update({joint[0]: new_target})
00057 
00058         return positions_to_send
00059 
00060     def interpolate(self, percentage):
00061         position_to_send = {}
00062         for name, position in self.grasp_to.joints_and_positions.items():
00063             if name in self.grasp_from.joints_and_positions.keys():
00064                 pos_from = self.grasp_from.joints_and_positions[name]
00065                 pos_to = position
00066                 position_to_send[name] = pos_from + (pos_to - pos_from) * (percentage / 100.0)
00067         return position_to_send


sr_hand
Author(s): Ugo Cupcic
autogenerated on Fri Aug 21 2015 12:24:55