base_hands.py
Go to the documentation of this file.
00001 #!/usr/bin/env python
00002 
00003 # Software License Agreement (BSD License)
00004 #
00005 # Copyright (c) 2013, Tokyo Opensource Robotics Kyokai Association
00006 # All rights reserved.
00007 #
00008 # Redistribution and use in source and binary forms, with or without
00009 # modification, are permitted provided that the following conditions
00010 # are met:
00011 #
00012 #  * Redistributions of source code must retain the above copyright
00013 #    notice, this list of conditions and the following disclaimer.
00014 #  * Redistributions in binary form must reproduce the above
00015 #    copyright notice, this list of conditions and the following
00016 #    disclaimer in the documentation and/or other materials provided
00017 #    with the distribution.
00018 #  * Neither the name of Tokyo Opensource Robotics Kyokai Association. nor the
00019 #    names of its contributors may be used to endorse or promote products
00020 #    derived from this software without specific prior written permission.
00021 #
00022 # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
00023 # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
00024 # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
00025 # FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
00026 # COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
00027 # INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
00028 # BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
00029 # LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
00030 # CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
00031 # LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
00032 # ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
00033 # POSSIBILITY OF SUCH DAMAGE.
00034 #
00035 # Author: Isaac Isao Saito
00036 
00037 import rospy
00038 
00039 
00040 class BaseHands(object):
00041     '''
00042     This class provides methods that are generic for the hands of
00043     Kawada Industries' dual-arm robot called Nextage Open.
00044 
00045     DIO pin numbers are set in
00046     nextage_ros_bridge.abs_hand_command.AbsractHandCommand
00047     '''
00048     # TODO: Unittest is needed!!
00049 
00050     # Since NEXTAGE is expected to be dual-arm, arm indicator can be defined
00051     # at the top level of hierarchy.
00052     HAND_L = '1'  # '0' is expected to be "Both hands".
00053     HAND_R = '2'
00054 
00055     _DIO_ASSIGN_ON = 1
00056     _DIO_ASSIGN_OFF = 0
00057     _DIO_MASK = 0   # Masking value remains "0" regardless the design on the
00058                     # robot; masking logic is defined in hrpsys, while robot
00059                     # makers can decide DIO logic.
00060 
00061     def __init__(self, parent):
00062         '''
00063         Since this class operates requires an access to
00064         hrpsys.hrpsys_config.HrpsysConfigurator, valid 'parent' is a must.
00065         Otherwise __init__ returns without doing anything.
00066 
00067         @type parent: hrpsys.hrpsys_config.HrpsysConfigurator
00068         @param parent: derived class of HrpsysConfigurator.
00069         '''
00070         if not parent:
00071             return  # TODO: Replace with throwing exception
00072         self._parent = parent
00073 
00074     def _dio_writer(self, digitalout_indices, dio_assignments,
00075                     padding=_DIO_ASSIGN_OFF):
00076         '''
00077         This private method calls HrpsysConfigurator.writeDigitalOutputWithMask,
00078         which this class expects to be available via self._parent.
00079 
00080         According to the current (Oct 2013) hardware spec, numbering rule
00081         differs regarding 0 (numeric figure) in dout and mask as follows:
00082 
00083            * 0 is "OFF" in the digital output.
00084              * 2/1/2014 Assignment modified 0:ON --> 0:OFF
00085            * 0 is "masked" and not used in mask. Since using '0' is defined in
00086              hrpsys and not in the robots side, we'll always use '0' for
00087              masking.
00088 
00089         @type digitalout_indices: int[]
00090         @param digitalout_indices: Array of indices of digital output that NEED to be
00091                             flagged as 1.
00092                             eg. If you're targetting on 25 and 26th places in
00093                                 the DIO array but only 25th is 1, then the
00094                                 array becomes [24].
00095         @type dio_assignments: int[]
00096         @param dio_assignments: range(32). Also called as "masking bits" or
00097                                 just "mask". This number corresponds to the
00098                                assigned digital pin of the robot.
00099 
00100                                eg. If the target pins are 25 and 26,
00101                                    dio_assignments = [24, 25]
00102         @param padding: Either 0 or 1. DIO bit array will be populated with
00103                         this value.
00104                         Usually this method assumes to be called when turning
00105                         something "on". Therefore by default this value is ON.
00106         '''
00107 
00108         # 32 bit arrays used in write methods in hrpsys/hrpsys_config.py
00109         dout = []
00110         for i in range(32):
00111             dout.append(padding)
00112             # At the end of this loop, dout contains list of 32 'padding's.
00113             # eg. [ 0, 0,...,0] if padding == 0
00114         mask = []
00115         for i in range(32):
00116             mask.append(self._DIO_MASK)
00117             # At the end of this loop, mask contains list of 32 '0's.
00118 
00119         signal_alternate = self._DIO_ASSIGN_ON
00120         if padding == self._DIO_ASSIGN_ON:
00121             signal_alternate = self._DIO_ASSIGN_OFF
00122         rospy.logdebug('digitalout_indices={}'.format(digitalout_indices))
00123         # Assign commanded bits
00124         for i in digitalout_indices:
00125             dout[i - 1] = signal_alternate
00126 
00127         # Assign unmasked, effective bits
00128         for i in dio_assignments:
00129             # For masking, alternate symbol is always 1 regarless the design
00130             # on robot's side.
00131             mask[i - 1] = 1
00132 
00133         # BEGIN; For convenience only; to show array number.
00134         print_index = []
00135         for i in range(10):
00136             # For masking, alternate symbol is always 1.
00137             n = i + 1
00138             if 10 == n:
00139                 n = 0
00140             print_index.append(n)
00141         print_index.extend(print_index)
00142         print_index.extend(print_index)
00143         del print_index[-8:]
00144         # END; For convenience only; to show array number.
00145 
00146         # # For some reason rospy.loginfo not print anything.
00147         # # With this print formatting, you can copy the output and paste
00148         # # directly into writeDigitalOutputWithMask method if you wish.
00149         print('dout, mask:\n{},\n{}\n{}'.format(dout, mask, print_index))
00150 
00151         try:
00152             self._parent.writeDigitalOutputWithMask(dout, mask)
00153         except AttributeError as e:
00154             rospy.logerr('AttributeError from robot.\nTODO: Needs handled.')
00155             rospy.logerr('\t{}'.format("Device was not found. Maybe you're" +
00156                                        "on simulator?"))
00157 
00158     def init_dio(self):
00159         '''
00160         Initialize dio. All channels will be set '0' (off), EXCEPT for
00161         tool changers (channel 19 and 24) so that attached tools won't fall.
00162         '''
00163         # TODO: The behavior might not be optimized. Ask Hajime-san and
00164         #       Nagashima-san to take a look.
00165 
00166         # 10/24/2013 OUT19, 24 are alternated; When they turned to '1', they
00167         # are ON. So hands won't fall upon this function call.
00168         # 2/1/2014 Due to the change of DIO assingment, OUT19, 24 need to be
00169         # alternated;
00170 
00171         dout = mask = []
00172         # Use all slots from 17 to 32.
00173         for i in range(16, 32):
00174             mask.append(i)
00175 
00176         self._dio_writer(dout, mask, self._DIO_ASSIGN_ON)


nextage_ros_bridge
Author(s): Isaac Isao Saito
autogenerated on Mon Oct 6 2014 07:22:37