Digital IO Blink - Code Walkthrough

From sdk-wiki
Jump to: navigation, search

Introduction

This example demonstrates the use of DigitalIO interface of Baxter. The main() function captures the user desired digital component on which the example is to be demonstrated. The test_interface() function creates the DigitalIO interface for that component and toggles its output.
Interfaces: DigitalIO::set_output(<double>), DigitalIO::state()

Code Walkthrough

Now, let's break down the code.

30 import argparse
31 
32 import rospy
33 
34 import baxter_interface.digital_io as DIO

The baxter interface for the Digital IO components is imported as DIO for convenience.

37 def test_interface(io_component='left_itb_light_outer'):
38     """Blinks a Digital Output on then off."""
39     rospy.loginfo("Blinking Digital Output: %s", io_component)
40     b = DIO.DigitalIO(io_component)
41 
42     print "Initial state: ", b.state

The io_component is initialized to be as "left_itb_light_outer" by default. As discussed above DIO refers to baxter_interface.digital_io.
b is an instance of the DigitalIO class of the Baxter Interface and is initialized with the digital component under interest. state() method retrieves the latest value of the component.

45     b.set_output(True)
46     rospy.sleep(1)
47     print "New state: ", b.state
48 
49     # reset output
50     b.set_output(False)
51     rospy.sleep(1)
52     print "Final state:", b.state

The output of the digital io component is toggled between True and False, and the corresponding output is displayed using the state() function.

55 def main():
56     """RSDK Digital IO Example: Blink
57 
58     Turns the output of a DigitalIO component on then off again
59     while printing the state at each step. Simple demonstration
60     of using the baxter_interface.DigitalIO class.
61 
62     Run this example with default arguments and watch the light
63     on the left arm Navigator blink on and off while the console
64     echos the state. Use the component_id argument or ROS Parameter
65     to change the DigitalIO component used.
66     """
67     epilog = """
68 ROS Parameters:
69   ~component_id        - name of DigitalIO component to use
70 
71 Baxter DigitalIO
72     Note that 'DigitalIO' components are only those that use
73     the custom ROS Messages baxter_core_msgs/DigitalIOState
74     and baxter_core_msgs/DigitalOutputCommand.
75 
76     Component names can be found on the Wiki, or by echoing
77     the names field of the digital_io_states topic:
78       $ rostopic echo -n 1 /robot/digital_io_states/names
79     """
80     arg_fmt = argparse.RawDescriptionHelpFormatter
81     parser = argparse.ArgumentParser(formatter_class=arg_fmt,
82                                      description=main.__doc__,
83                                      epilog=epilog)
84     parser.add_argument(
85         '-c', '--component', dest='component_id',
86         default='left_itb_light_outer',
87         help=('name of Digital IO component to use'
88               ' (default: left_itb_light_outer)')
89     )
90     args = parser.parse_args(rospy.myargv()[1:])

The user entered digital io component's id is captured. By default, its value is initialized as "left_itb_light_outer".

92     rospy.init_node('rsdk_digital_io_blink', anonymous=True)
93     io_component = rospy.get_param('~component_id', args.component_id)
94     test_interface(io_component)
95 
96 if __name__ == '__main__':
97     main()

The ros node is initialized and the test_interface() function is called which performs the output toggle, as explained above.