Lights Blinking Example

Jump to: navigation , search

The simple demonstration of using the intera_interface.Lights class.

Introduction

Run this example with default argument: head_green_light and watch the green light on the head blink on and off while the console echos the state. Use the light names from Lights.list_all_lights() to change lights to toggle. 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 lights blink example.

Usage

Run the example as following command:

$ rosrun intera_examples lights_blink.py

Pressing Control-C at any time will stop and exit the example.

Arguments

Important Arguments:

See the lights_blink.py available arguments on the command line by passing the -h, help argument:

$ rosrun intera_examples lights_blink.py -h
usage: lights_blink.py [-h] [-l]

Intera SDK Lights Example: Blink
    Toggles the Lights interface on then off again
    while printing the state at each step. Simple demonstration
    of using the intera_interface.Lights class.
    Run this example with default arguments and watch the green
    light on the head blink on and off while the console
    echos the state. Use the light names from Lights.list_all_lights()
    to change lights to toggle.
   

optional arguments:
  -h, --help            show this help message and exit
  -l, --light_name      name of Light component to use (default: head_green_light)

Code Walkthrough

Now, let's break down the code.

  1. import argparse
  2.  
  3. import rospy
  4.  
  5. from intera_interface import Lights

This imports the intera interface for accessing the Lights class.

  1. def test_light_interface(light_name='head_green_light'):
  2.     """Blinks a desired Light on then off."""
  3.     l = Lights()
  4.     rospy.loginfo("All available lights on this robot:\n{0}\n".format(
  5.                                                ', '.join(l.list_all_lights())))
  6.     rospy.loginfo("Blinking Light: {0}".format(light_name))
  7.     on_off = lambda x: 'ON' if l.get_light_state(x) else 'OFF'
  8.     rospy.loginfo("Initial state: {0}".format(on_off(light_name)))
  9.     # turn on light
  10.     l.set_light_state(light_name, True)
  11.     rospy.sleep(1)
  12.     rospy.loginfo("New state: {0}".format(on_off(light_name)))
  13.     # turn off light
  14.     l.set_light_state(light_name, False)
  15.     rospy.sleep(1)
  16.     rospy.loginfo("New state: {0}".format(on_off(light_name)))
  17.     # turn on light
  18.     l.set_light_state(light_name, True)
  19.     rospy.sleep(1)
  20.     rospy.loginfo("New state: {0}".format(on_off(light_name)))
  21.     # reset output
  22.     l.set_light_state(light_name, False)
  23.     rospy.sleep(1)
  24.     rospy.loginfo("Final state: {0}".format(on_off(light_name)))

The function list all available lights. Blink the desired light with the state of the light printed out in terminal.

  1. def main():
  2.     """Intera SDK Lights Example: Blink
  3.    Toggles the Lights interface on then off again
  4.    while printing the state at each step. Simple demonstration
  5.    of using the intera_interface.Lights class.
  6.    Run this example with default arguments and watch the green
  7.    light on the head blink on and off while the console
  8.    echos the state. Use the light names from Lights.list_all_lights()
  9.    to change lights to toggle.
  10.    """
  11.     epilog = """ Intera Interface Lights """
  12.     arg_fmt = argparse.RawDescriptionHelpFormatter
  13.     parser = argparse.ArgumentParser(formatter_class=arg_fmt,
  14.                                      description=main.__doc__,
  15.                                      epilog=epilog)
  16.     parser.add_argument(
  17.         '-l', '--light_name', dest='light_name',
  18.         default='head_green_light',
  19.         help=('name of Light component to use'
  20.               ' (default: head_green_light)')
  21.     )
  22.     args = parser.parse_args(rospy.myargv()[1:])

The argument for light name, default light is 'head_green_light'.

  1.     rospy.init_node('sdk_lights_blink', anonymous=True)
  2.     test_light_interface(args.light_name)
  3.  
  4. if __name__ == '__main__':
  5.     main()

The node is initialized and blink the desired light using the test_light_interface() method as explained above.