Designing an Embedded PI Controller for Speed Control

Thread Starter

Xavier Pacheco Paulino

Joined Oct 21, 2015
I will appreciate your guidance in this. I'm designing an embedded PI controller using a STM32 Nucleo board. In the PWM configuration, a 0 means 0% duty and 800 means 100% duty. I'm confused about the PI output and the duty cycle relationship. The objetive is to regulate the speed of a PMDC motor. This is what I have:

int error=0;
int target_RPM=0;
int duty_cycle=0;
int Kp = 0;
int integral = 0;
int current_RPM = 0;
int Ki = 0;
I plan to execute the following PID loop every 100 ms with an interrupt.

//Get current RPM
current_RPM = read_RPM();

//Get the error
error = target_RPM - current_RPM;

//Calculate the integral
integral = integral+error;

//Calculate the Control Variable
duty_cycle = (Kp*error) + (Ki*integral);

//Limit the Control Variable to within 0-800
    duty_cycle = 800;
else if (duty_cycle<0){
    duty_cycle = 0;
htim1.Instance->CCR1 = duty_cycle;
I still don't have the PI constants. First, I want to make sure that the algorithm is correct. Also, I know I need anti-wind up for the integral variable, but still not sure how to implement it.