cancel
Showing results for 
Search instead for 
Did you mean: 

Looking for resources to build a PID controller for dc motor speed control using STM32

xpp07
Senior

Hello,

I have a L432KC Nucleo. I would like to build a PID or PI software to control the speed of a DC motor. The closed loop element is a speed sensor to monitor RPM. I would appreciate if anyone has resources to share about developing this controller using STM32.

4 REPLIES 4
Andrew Neil
Evangelist III

Do you understand the principles of PID control?

They are long-established, well-documented and widely-used - not at all specific to STM32 ...

https://en.wikipedia.org/wiki/PID_controller

T J
Lead

how do you control the speed of the motor ?

did you decide on a motor ? which type ?

PID is easy enough to run, a little difficult to get a perfect result...

the RPM sensor would usually generate and interrupt or increment a counter...

I'm controlling a brushed DC motor via PWM. The hardware actually works, my project is just missing the software PID controller. I can manually control the duty cycle through CCR1 register. I'm measuring RPM using a reflective sensor and PWM input mode.

T J
Lead

did you calculate the RPM already ?

PID..

Current RPM 1000

Requested RPM 2000

PID has 3 elements to be added:

current error is targetRPM / RPM ;; 2000/1000 = 2

multiply that by a constant to get it to a usable number, lets try 1

Current Error = 2 ; // first element of PID // (double the RPM requested in this element)

Historical error, is the sum of all errors in the last 1 minute(ish)

lets say we use 30 readings for the Historical error;

Herror = Herror *29/30 + Cerror; // second element Herror

RateOFChange = Herror - LastHerror; // over the last reading

// you may want to use a few readings for better stability.

RateofChange, this is the most difficult to get right. // third element

if the Current RPM is not changing , the historical error will level out RateofChange will go to zero

you want to predict the next PWM setting...

set the PID to be a percentage.. ie, increase speed by 3%

Next PWM setting = CurrentPWM * ( 1+PID)