Lab 5

PID and Closed Loop Control

In this lab,I used the TOF sensor to control the car to move from 6ft and stop within 1 ft of a wall using PID control.


Lab 5

Prelab

Setup

To prepare for this lab, I wrote up code that would allow for bluetooth communication with the robot. I added two new commands: START_CAR, START_PID, STOP_PID. START_CAR makes the car move at a chose PWM for 5 seconds straight. The START_PID controlls the PID controller and collects the data to send. I have another command called GET_PWM_TOF_DATA which sends the data collected in a pwm, time, and distance array to the python script. I used flag variables to setup when PID was happening. I also had a change gain command that would help me with debugging.

Lab 4 Hardware Lab 4 Hardware Lab 4 Hardware Lab 4 Hardware Lab 4 Hardware

PID Control

For this lab we used the PID control equation from class to control the position of our robot. I decided to use PI control. The PID equation had 3 constants Kp, Kd, and Ki. I had to tune the Kp and Ki constants. The proportional control adjusts the output based on the error to ensure steady state accuracy. Integral control accumulates error over time and maintains the correction even when the error can disappear. Derivative control responds to error rate changes and predicts future behaviour.

P Control

First I did P control and used trial and error and my printed graphs to find a good Kp value. I kept Ki and Kd at 0. I decided to cap my maximum speed at 100 so that the car wouldn't crash. Through trial and error my Kp value was 0.08 which seemed to work best. Below are my graphs for the PWM and the TOF over time and code.

Lab 2 Pitch Lab 2 Pitch Lab 2 Pitch Lab 2 Pitch

Below is a video of the P control car working but not very well and accurate.

PI Control

After ensuring that P control worked well enough I started on PI control. Unforunately I wasn't able to see much of a different in performance. The I value I eventually got was very small. I just tested values and got 0.002. Below is a video where the run was good and it stopped more accurately and no oscillations.

Lab 2 Pitch

Sampling Time and Extrapolation

Although the PI and the P control worked well somewhat, there was a lot of downtime when the TOF sensor data just wasn't ready and needed around 40 ms for a new value. It was not great so I had to do extrapolation to get the car working better. For when there were no TOF readings I implemented a linear extrapolation from the recent measurements to get a rough estimate of the robot's position. New TOF values would trigger a new PID calculation.

Lab 2 Pitch

5000 Level Tasks

Windup Protection

When there was no windup protection, the car would slam into the wall if it went very fast. I would have to set the PWM to be slightly above 40 to get it to work as seen in the videos. The integral error increased over time so wind up protection was implemented. If the value of Ki multipled by the time integrated error was greater than 250 or less than -250 the Ki Integral would be set to a constant 250 and does not change. The implementation is below. I noticed with a higher Ki values the worse the robot performed.

pitch gyro

I referenced Nila Narayan and Mikayla Lahr for implementation.