The simple demonstration of using the intera_interface. Lights class.
Contents
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
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.
import argparse import rospy from intera_interface import Lights
This imports the intera interface for accessing the Lights
class.
def test_light_interface(light_name='head_green_light'):
"""Blinks a desired Light on then off."""
l = Lights()
rospy.loginfo("All available lights on this robot:\n{0}\n".format(
', '.join(l.list_all_lights())))
rospy.loginfo("Blinking Light: {0}".format(light_name))
on_off = lambda x: 'ON' if l.get_light_state(x) else 'OFF'
rospy.loginfo("Initial state: {0}".format(on_off(light_name)))
# turn on light
l.set_light_state(light_name, True)
rospy.sleep(1)
rospy.loginfo("New state: {0}".format(on_off(light_name)))
# turn off light
l.set_light_state(light_name, False)
rospy.sleep(1)
rospy.loginfo("New state: {0}".format(on_off(light_name)))
# turn on light
l.set_light_state(light_name, True)
rospy.sleep(1)
rospy.loginfo("New state: {0}".format(on_off(light_name)))
# reset output
l.set_light_state(light_name, False)
rospy.sleep(1)
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.
def main(): """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. """ epilog = """ Intera Interface Lights """ arg_fmt = argparse.RawDescriptionHelpFormatter parser = argparse.ArgumentParser(formatter_class=arg_fmt, description=main.__doc__, epilog=epilog) parser.add_argument( '-l', '--light_name', dest='light_name', default='head_green_light', help=('name of Light component to use' ' (default: head_green_light)') ) args = parser.parse_args(rospy.myargv()[1:])
The argument for light name, default light is 'head_green_light'.
rospy.init_node('sdk_lights_blink', anonymous=True) test_light_interface(args.light_name) if __name__ == '__main__': main()
The node is initialized and blink the desired light using the test_light_interface()
method as explained above.
Was this article helpful?
That’s Great!
Thank you for your feedback
Sorry! We couldn't be helpful
Thank you for your feedback
Feedback sent
We appreciate your effort and will try to fix the article