# PID control

This past few days, Xavier and I worked on the PID for motor speed control. We haven’t quite finished debugging our code, but here is an update of what we are working on.

## A few words about PID controller

Our PID (Proportional, Integral, Derivative) controller is a control loop algorithm using the speed feedback from the optical sensor or the hall sensor to control the motor speed.

The code continuously calculates the error, i.e. the difference between the target speed (the speed at which we want the motor to turn) and the measured speed, its integral, and its derivative. It applies a correction to the command send by the ESC to the motor. The correction is computed according to three coefficients : KP (for proportional correction), KI (for integral correction), and KD (for derivative correction) as explained in the following diagram.

The proportional regulator (using KP) is used to improve the response time of the system. The integral regulator (using KI) aimed to suppress the final offset. And the derived regulator (using KD) is used to stabilized the system and suppress oscillations.

## State of play

As explained here, we had a setup using the optical sensor to measure the motor speed.

Our first idea was simply to simply compute the PID every time the timer raises an input capture interrupt – when there is a rising edge on the optical sensor output, which happens once per rotation. This PID computation includes setting a new throttle value for the Dshot, but the actual sending is done in parallel since the ESC expects to receive a Dshot frame every few milliseconds.

We then realised that, even though the speed is measured only once per rotation, it would be better to update the PID correction much more frequently to smooth the Dshot output. So we moved the PID computation to the much more frequent Dshot loop.

## Results so far

We haven’t accomplished much yet, except for wildly oscillating speeds and a hiccuping motor; we still have to tune the KP, KI and KD coefficients. On top of that, we have troubles when starting the motor : the motor takes some time to start rotating and this delay seems to cause an overcompensation in the throttle target. On top of that, when the motor turns too slowly for the timer (the timer counter overflows), or isn’t turning at all, we simply record a null speed, which adds an imprecision at startup time when the motor starts to spin up. We plan to try and improve the timer overflow handling in our speed feedback code.

To choose the coefficients KP, KI and KD, there are almost as many methodologies as papers on the subject. We will use the method that Alexis suggested to us :

• increase KP until the system oscillates. The final value for KP is ⅔ of this value.
• do the same with KD and then with KI.

We will keep you posted !