2021-09-08 09:08 AM
Hi All,
Can someone please enlighten me on this:
Why is the integrator term, and the control function output scaled and shifted as shown in this code below?
Why would I need to do that in an embedded system such as STM32?
2021-09-08 07:50 PM
Decimal number involved in PID control part often. Ki is so small in most cases, such as 0.00053. And float tpye is not proper for some mcu. So we need to translate it to multiply and shift. 0.00053 = 34>>16.
2021-09-09 04:10 AM
Hi rpip.1 okay that makes sense. But, in this particular code it was not only the integral term that was scaled, the output was also scaled.
Also, can it always be a random number?
2021-09-09 07:28 PM
PID para is determined by calculation based on control theory or by test. It's related to motor para and PWM freq. Whatever, you don't want to set para just be an integer.
Even if para is not small enough, fraction part still involved in, such as 2.15.
Scale and shift can make more accurated result.
For examle,
If you want to set Kp to 2.15, it's moded by integer type to 2. Meaning a 1 step.
if you set Kp to 140902>>16, it's 2.1499. Meaning a 1>>16 step.
And in the latter case, you must multipy para with error first, then shifting.
Also Motor library need to be compatible to small para. If calculated pid para happen to be an integer or it's acceptable with integer type in your case. you can elide these scale and shift.
2021-09-16 04:58 AM
wow, Okay I dont fully understand this.
How do you do the math behind converting 2.15 to 140902>>16?
Let's say I have a random number like 3.3, how do I convert it to this format you just described (x>>y)