Today
For Next Time
Abstract
As we go into the final project, you will want to add a few more debugging tools to your quiver. Today, I have created some self-guided exercises to help you get to know these new approaches.
Dynamic Reconfigure
You may remember back when we did the Neato Soccer in-class assignment that one of the debugging strategies we used was to create sliders to adjust the values that we passed to the cv2.inrange function. In our __init__ function we had this:
cv2.namedWindow('threshold_image') self.red_lower_bound = 0 cv2.createTrackbar('red lower bound', 'threshold_image', 0, 255, self.set_red_lower_bound)This code created a window with a slider that let us set the lower bound for the red channel in for the cv2.inrange call. The createTrackbar method specified a callback function that is invoked whenever the slider is moved:
def set_red_lower_bound(self, val): """ A callback function to handle the OpenCV slider to select the red lower bound """ self.red_lower_bound = valWhile this is a perfectly fine way to create a simple GUI to modify the behavior of a node, it has two key shortcomings:
ROS has a much better mechanism for this sort of thing called dynamic_reconfigure. The basic idea is that you define a special Python script with the suffix .cfg that defines the parameters you want to be able to set via a simple GUI. This Python script is invoked by the ROS build system to automatically create a simple GUI that can be used to set a node's behavior at run-time.
Exercise 1
(We'll do this together)
Create the .cfg file and configure catkin_make to run your .cfg to automatically by following this tutorial. You can put this .cfg file in an existing catkin package that you have created or use a new catkin package (as suggested in the tutorial).
Exercise 2
Create a Python node to handle a reconfigure request from the automatically generated GUI by following this tutorial. You should go through the whole tutorial so you actually see the GUI in action and verify that the callback function in your Python node is invoked whenever you change the parameters using the GUI.
Exercise 3
Modify a node that you wrote that uses OpenCV sliders for a GUI to instead use dynamic_reconfigure. If you don't have one that you want to modify, you can instead modify my neato_soccer node.
Exercise 4
You can save the parameters from a dynamically reconfigurable node and reload them on demand using the rosrun dynamic_reconfigure dynparam dump and rosrun dynamic_reconfigure dynparam load. See here for documentation.
Note: I ran into some trouble with this. I kept getting this error:
error updating parameters: don't know parameter: groupsI was able to solve it by manually editing the YAML file and deleting the portion that referenced groups:
groups: !!python/object/new:dynamic_reconfigure.encoding.Config dictitems: bool_param: true double_param: 0.32 groups: !!python/object/new:dynamic_reconfigure.encoding.Config state: [] id: 0 int_param: 72 name: Default parameters: !!python/object/new:dynamic_reconfigure.encoding.Config state: [] parent: 0 size: 1 state: true str_param: Hello World type: '' state: []This seems to be a bug (see this report).