Feedback Control
Module 1
Contents
Lectures
Topic | Checkpoints |
---|---|
Feedback Control [Lecture Video, Slides] | Follow Me (1D) |
Omnidrive Geometry [Lecture Video, Slides] | Follow Me (2D) |
Wall Following [Lecture Video, Slides] | Project 1 |
Guides & Tutorials
The following guides and tutorials will be helpful for completing the checkpoints for this module and Project 1.
- Robot Quick Start: Getting up and running using your robot. If this is the first time you are using your robot and it has not been set up, take a look at the setup guides in the MBot Overview.
- Installing VSCode: Setting up VSCode on your computer.
- GitHub: We will use GitHub to store all of the code for this course.
- Programming the Robot: You can program your robot from your computer using VSCode.
- The MBot Bridge API: You will use these functions to read data from your robot and control it.
Checkpoint: Follow Me!
In this checkpoint, you will program the robot using a feedback controller. This checkpoint will build foundational code that will help you complete Project 1: Wall Following.
Read these instructions for:
Prerequisites
Grab a robot with a fully charged battery. Before you begin, you will need to have followed these guides on your laptop:
- Make sure your robot is setup by following the Quick Start Guide
- Install VSCode (tutorial)
- Connect to the MBot with VSCode remote (tutorial)
Getting the Code
See the course website for the course at your home institution for links to the template and submission instructions for this checkpoint.
Once you have accepted the assignment, clone the repository you created on the robot. In a VSCode remote session connected to the robot (tutorial), open a new terminal. Then, type:
git clone <ADDRESS>
Substitute <ADDRESS> with the address for your repo, found on GitHub. You can get the address from your repo page on GitHub:
Open the folder of the repository you just cloned in VSCode by cliking "File" > "Open Folder…" then typing in the name of the repo you cloned:
Code Overview
We will use the MBot Bridge API to control the robot. Review the documentation for instructions on how to use the provided functions.
Compiling the Code
We’ll use a tool called CMake to compile the code. CMake finds all the code and external libraries we need and generates instructions to build the executable. To build the code, in a terminal, type:
cd ~/[my-code-dir]/build
cmake ..
make
Remember that the code should be cloned and compiled on the Raspberry Pi. This will fail on your computer!
You should replace [my-code-dir]
with the name of your Follow Me directoy.
You only need to run cmake ..
once. When you start writing code, you can compile it by typing make
in the build/
folder. (Calling CMake multiple times won’t hurt, though!)
Running the Code
When you run make
successfully, it will generate two executables: follow_1D
is the 1D Follow Me code from the file src/follow_1D.cpp
, and follow_2D
is the 2D Follow Me code from the file src/follow_2D.cpp
.
Run them from the command line, in the build/
folder, to test your code for the 1D or 2D follow me:
./follow_1D
./follow_2D
The template code contains two files:
follow_1D.py
for Part 1 (1D) of this checkpoint,follow_2D.py
for Part 2 (2D) of this checkpoint.
1D Follow Me
We will write a program to make the robot maintain a fixed distance from the object in front of it. Edit the file src/follow_1D.cpp
follow_1D.py
to implement a Bang-Bang controller.
You will use the MBot Bridge API to program the robot. Review the functions available before getting started.
-
Edit the file
src/follow_1D.cpp
follow_1D.py
to implement a Bang-Bang controller. Commit and push your changes to GitHub.
- Hint: Use the
robot.drive(vx, vy, wz)
function to move the robot.
2D Follow Me
For this part of the activity, you will be editing src/follow_2D.cpp
follow_2D.py
.
First, you will need to write the function findMinDist()
find_min_dist()
.
This function should take in a vector of Lidar range values, and return the index of the shortest ray in the scan.
This function should take in a vector of Lidar range values and angles, and return the length and angle of the shortest ray in the scan.
Invalid rays will have zero range. You should ignore any zeros when finding the shortest range.
-
Edit the function
findMinDist()
in the filesrc/follow_2D.cpp
to return the index of the shortest ray. Edit the functionfind_min_dist()
in the filefollow_2D.py
to return the length and angle of the shortest ray.
- Hint: Remember to ignore rays with zero range when finding the minimum range value. Rays with zero range are invalid. If you forget to check for invalid rays, the minimum range will always be zero.
Next, edit src/follow_2D.cpp
follow_2D.py
to maintain a setpoint distance to the nearest wall in any direction. You will use the function findMinDist()
find_min_dist()
to get the distance and angle to the nearest object. This is done for you in the code:
// Get the distance to the wall.
float min_idx = findMinDist(ranges);
float dist_to_wall = ranges[min_idx];
float angle_to_wall = thetas[min_idx];
# Get the distance and angle to the wall.
dist_to_wall, angle_to_wall = find_min_dist(ranges, thetas)
The variable dist_to_wall
will then contain the distance to the nearest wall, and angle_to_wall
will contain the angle to the wall. Your drive commands will now be in the direction of the nearest wall instead of just forward.
-
Edit the main function in file
src/follow_2D.cpp
follow_2D.py
to implement a Bang-Bang controller again, but this time maintaining the distance to the nearest wall.