Lab 6: Closed Loop Control using PID


In this lab, I first improved my code structure to store chunks of data on the Artemis board and send data only when requested over BLE. Next, I drove the robot towards a wall and implemented PID control to stop the robot within 1 foot of the wall. I used the front-facing Time of Flight on the robot to collect data which was then used to calculate the error value for the PID controller.


In my main loop, I also added new logic for the storing and transmission of data. Previously, I would send data over Bluetooth right as I received it, which caused significant delay in my data collection process. My code now stores data in arrays on the Artemis board and only sends data when it is requested over Bluetooth. This is done by sending a GET_DATA command in my Python code; in my Arduino command handler, I set a flag which triggers data to be written to BLE in the main loop.




Using the above heuristic, I calculated the following parameter values which allowed my robot to quickly move towards the wall but stop before collision, adjusting to be around 304cm from the wall.


k_p = 0.08

k_d = 0.2

k_i = 0.125


In my pid function, I added deadband caps so that if the pwm being sent was too low for the motors to actually turn, I simply raised them to the minimum driving pwm. I also capped the maximum pwm value at 255 since that is the maximum duty cycle of the motors. Below is a compilation of videos I took throughout the process of adjusting the PID parameters. I start out by turning up the P and D terms, then decreasing them and adding the I term so that the robot can stop short of the wall and correct its position.


Distance PWM