Debugging, Proportional Control, and ROS Parameters
Today
- Robot debugging strategies
- ROS and threading
- Proportional Control and ROS Parameters
- Studio Time
For Next Time
- Work on the
the Warmup Project (due next Friday, the 15th).
Brainstorming Robot Debugging Strategies
You’ve probably learned about debugging strategies for software during your time at Olin. Many of these generic strategies carry over just fine to debugging robotics programs. Let’s take 10 minutes with the people around you to come up with some debugging strategies for writing robotis code. Once we you talk to folks around you, we will share out to teh class. As a motivating example, let’s consider the part of the warmup projection where you have to create a person follower.
Here are some areas to consider in the debugging / development lifecycle:
- How do you ensure your code is correct (implements the strategy you expected)?
- How do you test your approach to see if it performs the task effectively (e.g., follows a person)?
- How might you tune the parameters of your approach to make it perform as best possible?
ROS and threading
We’ll go over some points regarding how ROS2 deals with different threads of execution. In order to structure our work, we’re going to be looking at two pieces of sample code:
Proportional Control
So far we’ve programmed robots to choose between a small set of motor commands (move forward, stop, etc.) based on sensor readings. Today, we will be experimenting with setting the the motor command proportional to the error between the robot’s current position and the desired position.
To get the idea, program the Neato to adjust its position so that it is a specified (target) distance away from the wall immediately in front of it. The Neato’s forward velocity should be proportional to the error between the target distance and its current distance. Note: it’s tricky to get the sign correct, run through a few mental simulations to make sure the robot will move in the right direction.
Note: if you haven’t done this yet, you should add an upstream remote to the
comprobo23/class_activities_and_resourcesrepo.$ cd ~/ros2_ws/src/class_activities_and_resources $ git remote add upstream https://github.com/comprobo23/class_activities_and_resources $ git pull upstream main
To get started, create a package somewhere in your ros2_ws/src directory for your work. In this example, I’m going to put the package directly in ros2_ws/src/class_activities_and_resources directory
$ cd ~/ros2_ws/src/class_activities_and_resources
$ ros2 pkg create in_class_day04 --build-type ament_python --node-name wall_approach --dependencies rclpy std_msgs geometry_msgs sensor_msgs neato2_interfaces
This has been confusing in the past, but whenever you add a new ROS package (or a new ROS node), you’ll want to build using colcon and source the setup.bash file from your workspace.
$ cd ~/ros2_ws
$ colcon build --symlink-install
$ source ~/ros2_ws/install/setup.bash
You may have noticed at this point that ROS requires a certain amount of boiler-plate code to get going. If you are having trouble with this, or would rather skip ahead to the proportional control part, you can grab some starter code for wall_approach.py.
A helpful tool for visualizing the results of your program is to use the
$ rqt
Next, go to plugins -> visualization -> plot.
Type /scan/ranges[0] (if that is in fact what you used to calculate forward distance) into the topic field and then hit +. You can use this link to find a sample solution to this task.
Tip: to change the zoom in the plot, hold down the right mouse button and drag up or down on the body of the plot (it’s pretty finicky, but it does work).
Getting Fancy
To make your node more configurable, use (see the ros param command line tools documentation for more information) and code for accessing parameters in Python documentation from The Robotics BackEnd (I actually like this doc better than the official one). For instance, if you follow the documentation you can create a node similar to our sample solution that supports the following customization via the command line.
$ ros2 run in_class_day04_solutions wall_approach_fancy --ros-args -p target_distance:=1.5 -p Kp:=0.5
Here is a demo of our script, wall_approach_fancy.py that uses ROS parameters as well as the tool dynamic_reconfigure for easy manipulation of various node parameters. As linked above, you use the following link to find wall_approach_fancy. Note that in order to support dynamic_reconfigure you have to call add_on_set_parameters_callback and implement an appropriate callback function (see sample solution for more on this). Apologies that the demo below is out of date (you should get the main idea though).
.