Práctica 2: Follow Line

Description

  • Our goal with this practice is to get a Formula 1 car to go around a circuit in the shortest time possible. To help achieve this we have a red line from which the car has to guide itself to try and be on top or near the line at all times.
  • For this, I will use the JdeRobot platform, which provides maps to the gazebo simulator and all the base code to start running it.

System

  • We will use a reactive system (infinite loop) to which I have given different solutions to improve the stability and times of the car.
  • The cars main function: I follow the line and if I distance from it I will try to correct it by turning towards it again.

Hardware

  • We have a camera as a sensor, which we have to use to follow the line.
  • We also have the robots engines as actuators, and with those we will control the speed and rotation of the car.

Software

Here I will show how I have developed the program with different solutions.

Open CV

  • To begin with, I use cv2 to capture the image without processing it, filter it and apply a mask to the image. Later, I estimate the center of the mask (the center of the line in this case) and I would use that center to get the car to try and always be on or near it. Here you can see the image after applying a HSV filter.

Solution using cases

  • This is the first thing that came to me and the most basic one. Knowing the center, firts I gave it a little range where it won’t do anything, called range 0. If the car got passed that range, I would correct it turning it towards it.
  • If it went passed the range 0, it would enter the range 1 where it was turning little by little towards that direction and if it left this range it would go to the range 2 of error in which the turn increased, making it more abrupt. his last range is used more to stabilize or to make a turn in the curve. It’s pretty straightforward programming as a first idea.
  • The drawbacks of this solution is that the robot was zigzagging all the time in an unreal way, so it was not the best solution but a valid idea.

Solution using a controller P (Kp)

  • Starting to use a controller helped us calculate the error, which is equivalent to the distance of the center of the car with the distance with the center of the line and transform it into a new output would correct this error.
  • Fairly good stability improvement if compared to the other solution, due to the movement of the car being more natural in this simulation.
  • The drawback was the speed, since if it was very high it made it quite oscillating and sometimes this controller was not enough to calculate the necessary output to correct the error and it ends up colliding.

Solution using a controller PI (Kp | Ki)

  • Taking all of the above from the P controller, adding the I controller will not improve the stability too much but the rotation will be improved and with this we will achieve two things.
  • The main function of controller I is to gather the error so that it can be corrected faster.
  • The first improvement we see is that by gathering the error when making a turn, the error that is generated begins to be corrected faster since it is bigger than before when only using P.
  • The second improvement is that if we stop seeing the line, P controller will stop giving us an error, but having the error gathered in the controller I we will be able to correct it fast and make it see the line.
  • Even so, with only these two controllers we don’t get it to be able to go at a high speed, because even if it gathers the error it can not correct it quickly and it would crash.

Solution using a controller PD (Kp | Kd)

  • Adding the controller D to P we will see great improvements. In this practise it wouldn’t be necessary to use the I controller to get a good solution, but it would be more refined.
  • The function of the D controller is to compare the error in each turn of the loop and we will be able to see if the error is being corrected or not.
  • Only with these two controllers we can increase the speed a lot and take the curves quite well and it would be a fairly stable system.

Solution using a controller PID (Kp | Ki | Kd)

  • As I have been saying, this solution would be the best one, because it puts together the other two solutions. With the D we can anticipate the future knowing if the error is being corrected or not and with the I we will achieve that it goes better in the curves and that it does not get lost if it stops seeing the line.
  • With this solution we can greatly increase the constant speed respect to the other controllers (very similar to that of the PD) but the drawback is that the higher the speed the bigger the error, and this will create a great instability.
  • To correct this , the controller output we applied to the cars turn could also be applied to the speed, so if the car makes a 2 turn, it will also reduce the speed by 2. It is not the best solution but it does serve as a fix to be able to go faster in a straight line (when it is stable) and reduce it in a curve (when a greater error is generated).

Solution using a controller PID and Kv

  • As in previous solutions we have used the proportional constant (Kp), derivative constant (Kd) and the integrating constant (Ki), we add another which will be the decay constant (Kv)
  • With this constant and using a formula it is possible to calculate a speed that will change depending on the error and Kp.
  • Also, thanks to this formula we can set a maximum and minimum speed. Our car will change its speed within this range depending on the situation.

Conclusions

  • With this small review of the operation of each controller and the effect they had on this practice, we have been able to see the different solutions that can be given, how to optimize them little by little and achieve greater stability at high speeds.
  • Logically, the best controller is the PID, but in order to be able to conform it before, you have to know what is achieved with each one and gradually fine-tune the constants to achieve the greatest possible stability. If you start to design the controller in the order that I have explained, it would be possible to get a solution before applying a PID directly.
  • The computer that will be used can be limiting, since the simulator consumes many resources so cv2 and making the calculations will be slower, so it is very likely that the program will fail.

Final solution

  • In my case I decided to choose a PID controller although the integrator has a very low value since I use it mainly for recovery and sudden changes. To change the speed I am using the constant Kv. After refining all the variables my computer has allowed me to reach 34 seconds per lap in a fairly stable way.

--

--

Juan Carlos Manzanares Serrano
PRÁCTICAS ROBÓTICA MÓVIL

Estudiante de Ingeniería de Robótica Software en la Universidad Rey Juan Carlos.