2
\$\begingroup\$

I have calculated the transfer function of my system to apply a PID to it. With MATLAB and the PID Tuner tool I got the Kp, Ki and Kd parameters of my PID controller. For example in my case they are 0.175, 371.22 and 0 respectively.

Now, I need to use this values in my DSP but the PID algorithm uses the Q15 fractional format for everything. I know how I should scale the input and output values of my PID:

  • If the max input value is x, divide it by x if it goes from -x to x, or do (x/2)-1 if it goes from 0 to x and then convert to Q15 fractional

  • For the output value just scale it to your needs

But how do I convert my PID parameters Kp, Ki and Kd to Q15 correctly? If I apply some factor to them like I do for the input and output values I'm actually changing the loop response so how is this done? I think this should be a fairly common thing to do when designing digital PIDs.

\$\endgroup\$
1
\$\begingroup\$

First at all the maximum representables values of the Q15 format are -1 and 0.9999. So you might have a problem with your Ki.

The most common solution for converting a double to a Q15 is to multiply it with 2^15.

I am usually using this macro:

#define Q15(x)(s16) ((x<0.0) ? (s16)(32768*(x) - 0.5):(s16)(32768*(x) + 0.5))

But because of your Ki is not working with the Q15 format you should use another Q-format. eg: Q6 or Q7.

For further information you could check wikipedia.

\$\endgroup\$
  • \$\begingroup\$ So there's just no way to do it, I have to keep my parameters below 1? \$\endgroup\$ – Andres Dec 16 '16 at 12:19
  • \$\begingroup\$ Or you could shift your parameters only 6 times. (=Q6) \$\endgroup\$ – user118405 Dec 16 '16 at 13:09
  • \$\begingroup\$ The code will keep working if it was designed to use with Q15 and I use Q6? \$\endgroup\$ – Andres Dec 16 '16 at 13:12
  • \$\begingroup\$ Just change the fraction length from 15 (32768) to 6 (64). \$\endgroup\$ – user118405 Dec 16 '16 at 13:25
0
\$\begingroup\$

You can use the internal matlab function: fi(v) to construct a fixed point numeric object: https://de.mathworks.com/help/fixedpoint/ref/fi.html

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.