This file implements the line-following controller. It reads the reflectance sensor array, estimates line position using a centroid-based method, filters and bounds the result, then generates left and right wheel velocity commands to keep the robot centered on the track.
This file is the robot’s main path-tracking controller whenever line-follow mode is enabled. It turns raw reflectance readings into steering behavior.
| Name | Description |
|---|---|
sens | Line sensor array object used to read reflectance values. |
v_base | Base forward speed Share. |
vL_cmd, vR_cmd | Left and right wheel velocity command Shares. |
lfEnable | Share that enables or disables line-follow control. |
Kp, Ki, Kd | Controller gains for steering correction. |
u_max | Maximum steering correction magnitude. |
max_min, sum_min | Confidence thresholds used to reject weak line signals. |
pos_alpha | Low-pass filter coefficient for the line-position estimate. |
pos_deadband | Small deadband around zero position error. |
_last_valid_pos | Most recent valid line position, used when the signal weakens. |
| Method / Logic | Description |
|---|---|
_reset_pid() | Resets internal controller state when line-follow mode is disabled. |
| Centroid estimation | Computes an effective line position from the weighted sensor readings. |
| Weak-signal handling | Prevents the robot from immediately spinning when the line signal becomes unclear. |
| Wheel-command output | Uses the steering correction to adjust left and right speed commands around the base speed. |
if not self.lfEnable.get():
self._reset_pid()
yield self._state
continue
One of the strongest parts of this file is its weak-line handling. Instead of assuming the sensor signal is always perfect, the controller includes thresholds, filtering, and hold-last-valid logic so the robot behaves more gracefully when the line momentarily becomes less clear.
This file is what allows the robot to follow the printed track smoothly rather than simply reacting to a threshold crossing. It is a large part of why the robot appears controlled instead of jerky.