Electrical – Controlling motor speed with PID how to scale PWM to RPM and back

controlmicrocontrollermotor

I want to control the speed of a motor using PID controller. My problem is, the valid PWM range I can set in my microcontroller is from 0 to 199 but I measure the speed of the motor (using an encoder) in range of ~50 to 6000 RPM.

How can I meaningfully interchange these values?

Example:

I set the target speed to 6000 when motor is at 0RPM. Then the PID starts and at that moment the error is 6000…so the PID gives maximum output which will be 32767. But I only can set the timer PWM from 0~200.

I am using the code in AVR221 application note and there every variable is signed integer (no floating points). My own micro controller is stm32 that can handle floating points efficiently I beleive.

Best Answer

No need to scale. You need to limit your PID controller output wit 0% and 99%, and find out what factors work best. There is I*R, all kinds of interference, acceleration, so voltage is not strictly related to speed. Actually sometimes it may seem like there is no relation at all. So you do PI or PID, and tune it. Best practice is to also use SI units everywhere, but not mandatory.

By the way, and this is very important. You must build motion profile. Which in case of velocity control means you have to gradually raise the velocity according to acceleration. Otherwise you will get overshoots and very unstable behavior, or poor control if you will lower your gain to calm system down.