Using PID controller to control the speed of the DC motor!
Start scrolling!The purpose of this lab is to get experience with PID control. The lab is fairly open ended, you can pick whatever controller works best for your system. 4000-level students can choose between P, PI, PID, PD; 5000-level students can choose between PI and PID controllers. Your hand-in will be judged upon your demonstrated understanding of PID control and practical implementation constraints, and the quality of your solution.
Design the PID controller
Here below is the code of my PID controller, itself is very simple, but there is a detail:
Because the role of the Integrator is the feedback, so we accumulate the error to the parameter 'integral' only when the current speed and the current error are in the different -/+ regions.
The benefit of this operation is that it makes our car response much more rapidly when it exceeds the limit line (300mm in this task) since the parameter 'Integrator' is always 0 before that. So when the distance is less than 300mm, the parameter 'Integrator' will immidiately be negative and then slow down the speed to make the car brake.
Mapping the speed to the PWM
Because there exists differece between desired speed and the desired PWM, we should mapping these two parameters so that they have the same original point. As tested last Lab, when the PWM is 30, the car will start to move, so we should map the 0-speed point to the 30/-30 PWM point. Here below is the code:
Extrapolation
The frequency of our TOF data is only 10Hz, which is not enough to update the PID parameter and make fast response. So that we make this Extrapolation as shown below:
Using this method, we can predict the location of our car (the distance to the obstacle) in the interval that the TOF got no response. Because the response frequency of TOF is only 10Hz and our loop frequency is 200Hz, so this implement is very necessary to make fast response.
Anything goes: The goal is a working system. When you have a reasonable control setup working, you should feel free to add any “hacks” that will improve your robot performance in a reliable way. If you don’t have time to implement them, discussing what you imagine would help can still get you grading points.
By using the simple PID control, we found that when our initial speed is large or the accumulation distance is longer, the speed of the car when reaching the 30cm limit distance will be too fast to stop before hitting the obstacle.
So my idea is to make the car slow down before reaching the 30cm limit distance. My trick is adding one more limit distance at 80cm. The car will start to slow down when the distance is below 80cm, and when the current distance is below 60cm and the current speed is kind of slow, the target distance will switch to 30cm, which is our final target.
And the code is attached here:
As the code illustrates, we calculated the instant speed to decide whether the current speed of the car is appropriate to switch to the 30cm target.
There is a detail that we should clear our integral parameter of the PID controller since we have already changed the target, the accumulated error should be calculated based on the new target.
Tuning the Kp, Ki and Kd
For more convinent tuning, I wrote the program that I can change the PID parameters in the jupyter lab terminal so that we do not need to upload the code to the arduino board each time when changing the PID parameter.
Our final result is beyond my expectation since it can stop rapidly from a fast speed.
Here below is the data received in my computer and plotted as figures.
The first figure is the distance w.r.t time, which decreases from 1800mm to 150mm at least and then bounce back to about 300mm.
Our initial distance to the wall is over 2m, but the TOF distance is only 1.8m.
The second figure is the PWM vs Time, as shown below, to make our car run as soon as possible, we make the largest PWM nearly 255, which is the theorical maximum speed.
And one thing noticeable is that the PWM is bouncing between 255 and -255, this is because our Ki and Kd value are exteremely large, which are 0.1 and 10, making the fluctuation large but stop fast.
The final one is the instant speed, and we can see that in the interval when there is no TOF data, the extrapolated speed is very stable. When there is a TOF data, the instant speed will change fiercely.
Implement wind-up protection for your integrator. Argue for why this is necessary (you may for example demonstrate how your controller works reasonably independent of floor surface). Demonstrate your system with and without wind-up protection.
I left the car in the laboratory, and will implement this in Wednesday's lab