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.
30 import argparse
31
32 import rospy
33
34 from intera_interface import Lights
This imports the intera interface for accessing the Lights
class.
37 def test_light_interface(light_name='head_green_light'):
38 """Blinks a desired Light on then off."""
39 l = Lights()
40 rospy.loginfo("All available lights on this robot:\n{0}\n".format(
41 ', '.join(l.list_all_lights())))
42 rospy.loginfo("Blinking Light: {0}".format(light_name))
43 on_off = lambda x: 'ON' if l.get_light_state(x) else 'OFF'
44 rospy.loginfo("Initial state: {0}".format(on_off(light_name)))
45 # turn on light
46 l.set_light_state(light_name, True)
47 rospy.sleep(1)
48 rospy.loginfo("New state: {0}".format(on_off(light_name)))
49 # turn off light
50 l.set_light_state(light_name, False)
51 rospy.sleep(1)
52 rospy.loginfo("New state: {0}".format(on_off(light_name)))
53 # turn on light
54 l.set_light_state(light_name, True)
55 rospy.sleep(1)
56 rospy.loginfo("New state: {0}".format(on_off(light_name)))
57 # reset output
58 l.set_light_state(light_name, False)
59 rospy.sleep(1)
60 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.
63 def main():
64 """Intera SDK Lights Example: Blink
65 Toggles the Lights interface on then off again
66 while printing the state at each step. Simple demonstration
67 of using the intera_interface.Lights class.
68 Run this example with default arguments and watch the green
69 light on the head blink on and off while the console
70 echos the state. Use the light names from Lights.list_all_lights()
71 to change lights to toggle.
72 """
73 epilog = """ Intera Interface Lights """
74 arg_fmt = argparse.RawDescriptionHelpFormatter
75 parser = argparse.ArgumentParser(formatter_class=arg_fmt,
76 description=main.__doc__,
77 epilog=epilog)
78 parser.add_argument(
79 '-l', '--light_name', dest='light_name',
80 default='head_green_light',
81 help=('name of Light component to use'
82 ' (default: head_green_light)')
83 )
84 args = parser.parse_args(rospy.myargv()[1:])
The argument for light name, default light is 'head_green_light'.
88 rospy.init_node('sdk_lights_blink', anonymous=True)
89 test_light_interface(args.light_name)
90
91 if __name__ == '__main__':
92 main()
The node is initialized and blink the desired light using the test_light_interface()
method as explained above.