Lab B: Robot Line Follower


Today's Lab



Coding Exercise: Line Following with Turtlebot4


Please find a partner to work with for this exercise. While both you and your partner are expected to write you own code and make your own individual submission, this exercise is designed to work through together. You and your partner will help each other understand the starter code and the concepts behind the line follower, and you will be able to discuss and troubleshoot together. Also, you'll need to likely share some Turtlebots.

Getting Started


To get started on this exercise, clone the lab_b_line_follower git repository ROS2 package - the starter code that we'll be using for this lab.

$ cd ~/intro_robo_ws/src
$ git clone https://github.com/Intro-Robotics-UChicago-Fall-2025/lab_b_line_follower.git
$ cd ~/intro_robo_ws 
$ colcon build --symlink-install --packages-select lab_b_line_follower
$ source install/setup.bash      

Connect with a Turtlebot4, remembering to run:

$ set_robot_num [robot-number]

Then, to run the code, you can run:

$ ros2 run lab_b_line_follower line-follower

The starter code implements a helpful debugging window to help visualize the center of the computed orange pixels. Once you've correctly identified the center of the orange pixels, your window should look something like the following:

Red dot visualization

Understanding the Starter Code


Read through the starter code in line_follower.py with your partner. Discuss it together so that both of you understand what's going on. We encourage you to look up what certain OpenCV functions do to better understand what's going on. Make sure that you all discuss and understand what the following variables represent and what values they will hold: h, w, d, cx, cy.


Implementing the Line Follower


This programming exercise contains 2 main components:

  1. Defining the range for what you will consider a "orange" pixel in the image feed.
    • This task is best done using the HSV (Hue, Saturation, Value) color model, as it is more intuitive to use for defining color ranges than the additive RGB model. The HSV model is commonly used in color pickers; you may find it helpful to play around with one and observe how the HSV values change, then define a range of colors based on certain cutoffs.
    • OpenCV uses the following ranges for H, S, and V: H: 0-179, S: 0-255, V: 0-255. This means that any colors defined using the standard HSV ranges (H: 0-360, S: 0.0-1.0, V: 0.0-1.0) will need to have their components normalized accordingly.
      • For example, the HSV Wikipedia page contains swatches for certain hues. The standard HSV color \(H = 60^{\circ}, S = \frac{1}{4}, V = \frac{5}{8}\) converts to the OpenCV HSV values of \(H = \big(\frac{60^{\circ}}{360^{\circ}} \cdot 180 - 1\big) = 29, S = \big(\frac{1}{4} \cdot 256 - 1\big) = 63, V = \big(\frac{5}{8} \cdot 256 - 1\big) = 159\)
    • In the solution that we developed, we used the following two colors as our minimum and maximum HSV values to define "orange":
      light orange swatch
      dark orange swatch
  2. Implementing proportional control to enable the robot to follow the orange line. This will involve:
    • Setting up a ROS publisher to control the movement of the robot.
    • Computing an "error" term (the difference between the goal and the sensed reality). This most important part here is defining the "goal" and the "reality" comparison.
    • Using the error term to determine how the robot moves in accordance with the principles of proportional control.

To run your code:

$ ros2 run lab_b_line_follower line-follower

Once you've successfully implemented your proportional control line follower, it should look something like the following:

line follower demo

If you and your partner(s) finish early, feel free to use this time to work independently on your Warmup Project assignment.


Tips:



Lab B Deliverables


For your Lab B Deliverables, submit the following via Canvas/Gradescope by 2:00pm on Thursday, October 9th:


Acknowledgments


The line-following exercise and code was taken and modified from Gaitech EDU.