airhand_command.py
Go to the documentation of this file.
1 #!/usr/bin/env python
2 
3 # Software License Agreement (BSD License)
4 #
5 # Copyright (c) 2013, Tokyo Opensource Robotics Kyokai Association
6 # All rights reserved.
7 #
8 # Redistribution and use in source and binary forms, with or without
9 # modification, are permitted provided that the following conditions
10 # are met:
11 #
12 # * Redistributions of source code must retain the above copyright
13 # notice, this list of conditions and the following disclaimer.
14 # * Redistributions in binary form must reproduce the above
15 # copyright notice, this list of conditions and the following
16 # disclaimer in the documentation and/or other materials provided
17 # with the distribution.
18 # * Neither the name of Tokyo Opensource Robotics Kyokai Association. nor the
19 # names of its contributors may be used to endorse or promote products
20 # derived from this software without specific prior written permission.
21 #
22 # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
23 # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
24 # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
25 # FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
26 # COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
27 # INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
28 # BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
29 # LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
30 # CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31 # LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
32 # ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
33 # POSSIBILITY OF SUCH DAMAGE.
34 #
35 # Author: Isaac Isao Saito
36 
37 import time
38 import threading
39 
40 import rospy
41 
42 from abs_hand_command import AbsractHandCommand
43 
44 
45 class AirhandReleaseThread(threading.Thread):
46  '''
47  With airhand, to release by blowing air needs to be stopped after certain
48  amount of time. Usually a few seconds, not too short for the air compressor
49  to have ample time for a reaction, is a good idea. This thread is used
50  in order to do so without letting the computer program halt.
51  '''
52  def __init__(self, command, sleeptime):
53  '''
54  @type command: AirhandCommand
55  '''
56  threading.Thread.__init__(self)
57  self._command = command
58  self._sleeptime = sleeptime
59 
60  def run(self):
61  time.sleep(self._sleeptime)
62  self._command.execute(self._command.AIRHAND_KEEP)
63 
64 
65 class AirhandCommand(AbsractHandCommand):
66  '''dio_writer
67  Following Command design pattern, this class represents command for
68  an Airhand of NEXTAGE OPEN.
69 
70  As of 2/1/2014, it's only implemented for a right arm (since there's no
71  testing environment for left arm).
72  '''
73  # TODO: Unittest is needed!!
74 
75  # For air hands
76  AIRHAND_DRAWIN = 'drawin'
77  AIRHAND_KEEP = 'keep'
78  AIRHAND_RELEASE = 'release'
79 
80  ## Might not be necessary. Maybe use only where you have to specify
81  ## dangerous situation.AIRHAND_KEEP
82  AIRHAND_DANGER = 'danger'
83 
84  def __init__(self, hands, hand, dio_pins):
85  '''
86  @see nextage_ros_bridge.command.abs_hand_command.AbsractHandCommand
87  @type hands: nextage_ros_bridge.base_hands.BaseHands
88  @type hand: str
89  @param hand: Side of hand. Variables that are defined in
90  nextage_ros_bridge.base_hands.BaseHands can be used { HAND_L, HAND_R }.
91  '''
92  super(AirhandCommand, self).__init__(hands, hand, dio_pins)
94 
95  def _assign_dio_names(self, dio_pins):
96  '''
97  @see abs_hand_command.AbsractHandCommand._assign_dio_names
98  '''
99  #DIO reassignment for the class-specific purpose
100  self._DIO_SUCTION_L_1 = dio_pins[0]
101  self._DIO_SUCTION_L_2 = dio_pins[1]
102  self._DIO_SUCTION_R_1 = dio_pins[2]
103  self._DIO_SUCTION_R_2 = dio_pins[3]
104 
105  def execute(self, operation):
106  '''
107  @see abs_hand_command.AbsractHandCommand.execute
108  '''
109  dout = []
110  mask = [] # Will be filled depending on the side of the hand.
111  mask_l = [self._DIO_SUCTION_L_1, self._DIO_SUCTION_L_2]
112  mask_r = [self._DIO_SUCTION_R_1, self._DIO_SUCTION_R_2]
113 
114  # Set masking value per hand.
115  if self._hands.HAND_L == self._hand:
116  mask = mask_l
117  elif self._hands.HAND_R == self._hand:
118  mask = mask_r
119  else:
120  raise RuntimeError('Make sure _hands object, _hand value are set.')
121 
122  if self.AIRHAND_DRAWIN == operation:
123  if self._hands.HAND_L == self._hand:
124  dout = [self._DIO_SUCTION_L_1]
125  elif self._hands.HAND_R == self._hand:
126  dout = [self._DIO_SUCTION_R_1]
127  elif self.AIRHAND_KEEP == operation:
128  if self._hands.HAND_L == self._hand:
129  pass # Do nothing since off for both pins.
130  elif self._hands.HAND_R == self._hand:
131  pass # Do nothing since off for both pins.
132  elif self.AIRHAND_RELEASE == operation:
133  if self._hands.HAND_L == self._hand:
134  dout = [self._DIO_SUCTION_L_2]
135  elif self._hands.HAND_R == self._hand:
136  dout = [self._DIO_SUCTION_R_2]
137 
138  # Create a thread to do KEEP action after the specified amount
139  # of time without stopping the program.
140  thread = AirhandReleaseThread(self, self._SLEEP_POST_RELEASE)
141  thread.start()
142  else:
143  # TODO: Might want to thrown exception?
144  rospy.logwarn('No gripper specified. Do nothing.')
145  return
146  return self._hands._dio_writer(dout, mask)


nextage_ros_bridge
Author(s): Isaac Isao Saito , Akio Ochiai
autogenerated on Wed Jun 17 2020 04:14:47