Analog IO Rampup - Code Walkthrough

From sdk-wiki
Revision as of 15:02, 29 July 2014 by Sdkuser (talk | contribs) (Protected "Analog IO Rampup - Code Walkthrough": Setting Permissions for (almost all) Pages - editable and moveable by all logged in users ([Edit=Allow only autoconfirmed users] (indefinite) [Move=Allow only autoconfirmed users] (indefinite)))
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search

Introduction

This example demonstrates the usage of the AnalogIO interface of Baxter. The main() function captures the user desired analog component on which the example is to be demonstrated. The test_interface() function creates an instance of the AnalogIO interface for that component, increases its value from 0 to 100, and ramps it down from 100 to 0, in a step size of 1.
Interfaces: AnalogIO::set_output(<double>), AnalogIO::state()

Code Walkthrough

Now, let's break down the code.

30 import argparse
31 
32 import rospy
33 
34 import baxter_interface.analog_io as AIO

The baxter interface for the Analog IO components is imported as AIO for convenience.

37 def test_interface(io_component='torso_fan'):
38     """Ramps an Analog component from 0 to 100, then back down to 0."""
39     rospy.loginfo("Ramping output of Analog IO component: %s", io_component)
40 
41     b = AIO.AnalogIO(io_component)
42     rate = rospy.Rate(2)
43 
44     # start: 0.0
45     print b.state()

The io_component is initialized to be as "torso_fan" by default. As discussed above AIO refers to baxter_interface.analog_io.
b is an instance of the AnalogIO class of the Baxter Interface and is initialized with the analog component under interest. state() method retrieves the latest value of the component.

48     for i in range(0, 101, 10):
49         b.set_output(i)
50         print i
51         rate.sleep()
52     # max: 100.0
53     print b.state()

set_output() sets the new state of the output of the calling component, to the value passed. Here, the value is ramped up from 0 to 100 in increments of 1. Then, the updated state is displayed.

56     for i in range(100, -1, -10):
57         b.set_output(i)
58         print i
59         rate.sleep()
60     # (fans off)
61     b.set_output(0)

As discussed above, the analog io component's output is ramped down in step size of 1.

64 def main():
65     """RSDK Analog IO Example: Ramp
66 
67     Ramps the output of an AnalogIO component from 0 to 100,
68     and then back down again. Demonstrates the use of the
69     baxter_interface.AnalogIO class.
70 
71     Run this example and listen to the fan as output changes.
72     """
73     epilog = """
74 ROS Parameters:
75   ~component_id        - name of AnalogIO component to use
76 
77 Baxter AnalogIO
78     Note that 'AnalogIO' components are only those that use
79     the custom ROS Messages baxter_core_msgs/AnalogIOState
80     and baxter_core_msgs/AnalogOutputCommand.
81 
82     AnalogIO component names can be found on the Wiki or by
83     echoing the names field of the analog_io_states topic:
84       $ rostopic echo -n 1 /robot/analog_io_states/names
85     """
86     arg_fmt = argparse.RawDescriptionHelpFormatter
87     parser = argparse.ArgumentParser(formatter_class=arg_fmt,
88                                      description=main.__doc__,
89                                      epilog=epilog)
90     parser.add_argument(
91         '-c', '--component', dest='component_id', default='torso_fan',
92         help='name of Analog IO component to use (default:= torso_fan)'
93     )
94     args = parser.parse_args(rospy.myargv()[1:])

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

 96     rospy.init_node('rsdk_analog_io_rampup', anonymous=True)
 97     io_component = rospy.get_param('~component_id', args.component_id)
 98     test_interface(io_component)
 99 
100 if __name__ == '__main__':
101     main()

The ros node is initialized and the test_interface() function is called which performs the ramp up and ramp down as explained above.