DEV Community

Cover image for F1 FollowLine + HSV filter + PID Controller
Jorge Martin
Jorge Martin

Posted on

F1 FollowLine + HSV filter + PID Controller

This is the blog of the P2-RM, where I programmed the behavior of a F1 Follow-Line car.

Objetive

The objective of this project is to program the behavior of an F1 car, so that it can safely and quickly complete a lap around a circuit. To achieve this, I will use an HSV filter to detect the line and a PID controller.

Coding

Libraries

  • This library provides functions related to time management, such as getting the current time or measuring elapsed time.
import time
Enter fullscreen mode Exit fullscreen mode
  • This library provides functions for working in domain of linear algebra, fourier transform, matrices and arrays.
import numpy as np
Enter fullscreen mode Exit fullscreen mode
  • This library is used for image and video processing, offering functions for tasks like object detection, filtering, and transformations in computer vision.
import cv2
Enter fullscreen mode Exit fullscreen mode

Implementation

HSV Filter

To create the HSV filter, we will need to generate two masks because the red color occupies two regions in the hue spectrum.
HSV
For each red value, a hue, saturation, and value are necessary.

lower_red_1 = np.array([0, 120, 70])
upper_red_1 = np.array([10, 255, 255])
lower_red_2 = np.array([170, 120, 70])
upper_red_2 = np.array([180, 255, 255])
Enter fullscreen mode Exit fullscreen mode

Centroids

At the beginning, I decided to place 3 centroids at three different points within a specific area of the red line.
Imagen dia 2
However, to make it simpler and more precise, I changed it to extract a single centroid from a specific area using image moments.

if moments['m00'] != 0:
       centroid_x = int(moments['m10'] / moments['m00'])
Enter fullscreen mode Exit fullscreen mode

Imagen dia 3

PID

To control the movement of the car, I have implemented a PID controller where:

P = Kp * error

integral += error * dt
I = Ki * integral

derivative = (error - previous_error) / dt 
D = Kd * derivative
Enter fullscreen mode Exit fullscreen mode

In this case, dt is the time difference between the current measurement and the last recorded one.

For each map, it was necessary to tweak the values of the Kp, Ki, and Kd components to optimize the car's movement.

# Values for Simple
Kp = 0.011
Ki = 0.0015
Kd = 0.0004
Enter fullscreen mode Exit fullscreen mode

Difficulties

  • The only major problem I've had has been with the PID controller, as even the slightest variation in any component caused the car to crash. It took quite some time to find the right values for each map.

Logbook

  • Day 1 (30/09/24): Implementation of the HSV filter (not tested on all maps).
  • Day 2 (2/10/24): Improvement of the HSV filter and analysis of the regions to be examined.
  • Day 3 (8/10/24): Change from 3 centroids in a line to 1 centroid in an area.
  • Day 4 (9/10/24): Implementation of the PID controller and adjustment of the components Kp, Ki and Kd.

Functionality

⚠️Warning⚠️: due to the constant change in the computer's performance, there are times when the times may decrease, increase, or even when the car may never complete the lap.

Simple: 31.46s

Simple Ackerman: 135s

Top comments (0)