Gripper Cuff Control Example

Revision as of 20:24, 16 January 2017 by Swang (Talk | contribs)

(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation , search

The Gripper Cuff Control example demonstrates basic cuff/gripper interaction on the robot. In this example, connects cuff buttons to gripper open/close commands: 'Circle' Button will open gripper, 'Dash' Button will close gripper, Cuff 'Squeeze' will turn on Nav lights.

Introduction

This program demonstrates the usage of gripper, cuff, lights interface to control the close and open actions of the gripper and navigation light. The main() function creates an instance of the GripperConnect class which connects the gripper buttons to various callback functions. If you would like to follow along with the actual source code for the example on GitHub, it can be found through this link for gripper cuff control example.

Usage

$ rosrun intera_examples gripper_cuff_control.py -h

Arguments

Important Arguments:

-g or --gripper : The gripper limb to control.

-n or --no-lights : Specify the argument will not trigger lights on cuff while grasping.

-v or --verbose : Specify the verbose will print the debug statements from rospy, default value is rospy.INFO

usage: gripper_cuff_control.py [-h] [-g {['right']}] [-n] [-v]

SDK Gripper Button Control Example

    Connects cuff buttons to gripper open/close commands:
        'Circle' Button    - open gripper
        'Dash' Button      - close gripper
        Cuff 'Squeeze'     - turn on Nav lights

    Run this example in the background or in another terminal
    to be able to easily control the grippers by hand while
    using the robot. Can be run in parallel with other code.
   

optional arguments:
  -h, --help            show this help message and exit
  -g {['right']}, --gripper {['right']}
                        gripper limb to control (default: both)
  -n, --no-lights       do not trigger lights on cuff grasp
  -v, --verbose         print debug statements

Code Walkthrough

Now, let's break down the code.

  1. import argparse
  2. import sys
  3.  
  4. import rospy
  5.  
  6. from intera_interface import (
  7.     Gripper,
  8.     Lights,
  9.     Cuff,
  10.     RobotParams,
  11. )

This imports the intera interface for accessing the Cuff, Lights and the Gripper class.

  1. class GripperConnect(object):
  2.     """
  3.    Connects wrist button presses to gripper open/close commands.
  4.    Uses the Navigator callback feature to make callbacks to connected
  5.    action functions when the button values change.
  6.    """
  7.  
  8.     def __init__(self, arm, lights=True):
  9.         """
  10.        @type arm: str
  11.        @param arm: arm of gripper to control
  12.        @type lights: bool
  13.        @param lights: if lights should activate on cuff grasp
  14.        """
  15.         self._arm = arm
  16.         # inputs
  17.         self._cuff = Cuff(limb=arm)
  18.         # connect callback fns to signals
  19.         self._lights = None
  20.         if lights:
  21.             self._lights = Lights()
  22.             self._cuff.register_callback(self._light_action,
  23.                                          '{0}_cuff'.format(arm))

Instances of the cuff and the lights are created.

  1.         try:
  2.             self._gripper = Gripper(arm)
  3.             if not (self._gripper.is_calibrated() or
  4.                     self._gripper.calibrate() == True):
  5.                 rospy.logerr("({0}_gripper) calibration failed.".format(
  6.                                self._gripper.name))
  7.                 raise
  8.             self._cuff.register_callback(self._close_action,
  9.                                          '{0}_button_upper'.format(arm))
  10.             self._cuff.register_callback(self._open_action,
  11.                                          '{0}_button_lower'.format(arm))
  12.             rospy.loginfo("{0} Cuff Control initialized...".format(
  13.                           self._gripper.name))
  14.         except:
  15.             self._gripper = None
  16.             msg = ("{0} Gripper is not connected to the robot."
  17.                    " Running cuff-light connection only.").format(arm.capitalize())
  18.             rospy.logwarn(msg)

If the gripper attached, create the instance of the gripper then verify if the gripper was already in a calibrated state, the cuff actions will be registered afterwards. Failed to calibrate the gripper will raise error. If the gripper is not attached, warning message will appear.

  1.     def _open_action(self, value):
  2.         if value and self._gripper.is_ready():
  3.             rospy.logdebug("gripper open triggered")
  4.             self._gripper.open()
  5.             if self._lights:
  6.                 self._set_lights('red', False)
  7.                 self._set_lights('green', True)

The method opens the gripper and set cuff light when button sends a True value.

  1.     def _close_action(self, value):
  2.         if value and self._gripper.is_ready():
  3.             rospy.logdebug("gripper close triggered")
  4.             self._gripper.close()
  5.             if self._lights:
  6.                 self._set_lights('green', False)
  7.                 self._set_lights('red', True)

The method closes the gripper and set cuff light when button sends a True value.

  1.     def _light_action(self, value):
  2.         if value:
  3.             rospy.logdebug("cuff grasp triggered")
  4.         else:
  5.             rospy.logdebug("cuff release triggered")
  6.         if self._lights:
  7.             self._set_lights('red', False)
  8.             self._set_lights('green', False)
  9.             self._set_lights('blue', value)

This method assigns the boolean value that was signalled to the light interface.

  1.     def _set_lights(self, color, value):
  2.         self._lights.set_light_state('head_{0}_light'.format(color), on=bool(value))
  3.         self._lights.set_light_state('{0}_hand_{1}_light'.format(self._arm, color),
  4.                                                                  on=bool(value))

Set light color according to the provided params.

  1. def main():
  2.     """SDK Gripper Button Control Example
  3.    Connects cuff buttons to gripper open/close commands:
  4.        'Circle' Button    - open gripper
  5.        'Dash' Button      - close gripper
  6.        Cuff 'Squeeze'     - turn on Nav lights
  7.    Run this example in the background or in another terminal
  8.    to be able to easily control the grippers by hand while
  9.    using the robot. Can be run in parallel with other code.
  10.    """
  11.     rp = RobotParams()
  12.     valid_limbs = rp.get_limb_names()
  13.     if not valid_limbs:
  14.         rp.log_message(("Cannot detect any limb parameters on this robot. "
  15.                         "Exiting."), "ERROR")
  16.         return
  17.     if len(valid_limbs) > 1:
  18.         valid_limbs.append("all_limbs")
  19.     arg_fmt = argparse.RawDescriptionHelpFormatter
  20.     parser = argparse.ArgumentParser(formatter_class=arg_fmt,
  21.                                      description=main.__doc__)
  22.     parser.add_argument('-g', '--gripper', dest='gripper', default=valid_limbs[0],
  23.                         choices=[valid_limbs],
  24.                         help='gripper limb to control (default: both)')
  25.     parser.add_argument('-n', '--no-lights', dest='lights',
  26.                         action='store_false',
  27.                         help='do not trigger lights on cuff grasp')
  28.     parser.add_argument('-v', '--verbose', dest='verbosity',
  29.                         action='store_const', const=rospy.DEBUG,
  30.                         default=rospy.INFO,
  31.                         help='print debug statements')
  32.     args = parser.parse_args(rospy.myargv()[1:])

The gripper option, the light and verbose options are parsed from the command line arguments, as entered by the user.

  1.     rospy.init_node('sdk_gripper_cuff_control_{0}'.format(args.gripper),
  2.                     log_level=args.verbosity)
  3.  
  4.     arms = (args.gripper,) if args.gripper != 'all_limbs' else valid_limbs[:-1]
  5.     grip_ctrls = [GripperConnect(arm, args.lights) for arm in arms]
  6.  
  7.     print("Press cuff buttons for gripper control. Spinning...")
  8.     rospy.spin()
  9.     print("Gripper Button Control Finished.")
  10.     return 0
  11.  
  12. if __name__ == '__main__':
  13.     sys.exit(main())

The node is initialized and an instance of the GripperConnect is created.