Entry 2: P Controller, Centroid Calculation & D Controller

⚙️ P Controller

To control both the turning and linear speeds based on the reference line, I started by applying a proportional controller (P) to these variables. The error is obtained from the distance between the center of the image and the centroid of the detected line using the mask.

Initially, I applied control only to the turning speed, but extending it to the linear speed allows the car to slow down when taking curves. This makes it easier to achieve proper turning without veering off the reference line. To accomplish this, I defined two different proportional coefficients KP, one for turning control and another for speed reduction.

At this stage, with a maximum linear speed of 4.0 and a minimum of 2.0, and both proportional constants set to 0.005, the car completes the circuit in 400 seconds. However, I noticed significant oscillations in the curves. When printing the error, it fluctuates drastically, which could be related to the calculated centroid position, causing abrupt changes in the turning speed.

Change of the error prints

📍 Calculation of the centroid

To avoid such abrupt changes of the error, I proceed to calculate a centroid less close to the car and therefore allowing to predict the action of the car earlier. Currently the centroid is calculated with respect to the full image, resulting in a point too far down:

(In blue) centroid calculated on the total of the image.

To calculate a higher centroid (corresponding to a point further away from the camera) I will select a region of interest in the central strip of the image, on which the new centroid will be calculated. In this way it is possible to anticipate in advance the action to be performed. I tested several ROI sizes until I found a centroid that I considered optimal through several runs.

(In blue) centroid calculated over the region of interest.

As a result, I observed that the oscillations have significantly decreased (with the same parameters as in the previous test). However, the car still deviates considerably in curves, moving too far away from the reference line. The car completed the circuit in 333 seconds (with a maximum speed of 4.0 and both KP values at 0.005).

🛠️ D Controller

The next step was to implement a derivative (D) controller, for now, only applied to the angular velocity. As an initial test, I assigned a derivative constant of 0.003 to observe the behavior. The maximum linear speed remains at 4.0 for now.

The first noticeable improvement is that the oscillation observed in the previous execution is significantly reduced. In particular, I noticed that upon reaching a straight section, the car oscillates very briefly but does not cross the line in the opposite direction. Instead, it stabilizes quite quickly. However, it is important to note that the linear speed is still relatively low.

🚨 In case the line is not visible

Next, I tested increasing the maximum speed significantly (10.0) to see what happens. In the first curve, the car went off track because the turning was not fast enough compared to the linear speed at that moment.

When it lost sight of the line, the system was set up to slow down and turn until the line was detected again. However, in this case, the car turned in the opposite direction and continued its route in reverse. This showed me that it is necessary to force the car to turn in the same direction it was moving before losing the line.

To achieve this, I simply increased the angular velocity in the same direction (i.e., keeping the same sign) as the last registered velocity. This way, the car keeps turning until it detects the line again, ensuring that it maintains the correct circuit direction. Meanwhile, the linear speed remains at the minimum configured value.

With a speed of 10.0 and the same P and D parameters, the holonomic car completes the circuit in 188 seconds. However, it keeps "bouncing" against the road boundaries because it tends to deviate too much in the curves. For now, I will reduce the speed and start fine-tuning the parameters.

⬅ Back to P1 - Follow Line