vinayakdabholkar
Advanced Member level 4
- Joined
- Nov 29, 2009
- Messages
- 114
- Helped
- 0
- Reputation
- 0
- Reaction score
- 0
- Trophy points
- 1,296
- Location
- goa
- Activity points
- 2,195
Introduction
Building a fast PID controller in software, with an overall accuracy of about 12 bits (dictated by the available hardware), I needed fast 24 bit math.
The general formula for a PID controller
PID = Kp * Error + Kd * (Error - PreviousError) + Ki * IntegralError
In this formula error terms are in the range [-12bits ... +12bits] = 13 bits and Kp, Kd, Ki are in the range of [-1 ... +1].
Integer math has the advantage of being faster then floating point math, and it's more easier to control the accuracy there where you need it. Now for integer math, it's more convenient to rewrite the general PID formula like this:
PID = ( 1 / Ko ) * ( Kp * Error + Kd * (Error - PreviousError) + Ki * IntegralError )
Where Ko is some nice factor (power of 2, so dividing becomes just shifting) large enough to deal with the maximum ratios of Kp, Kd, Ki. A first shot would be to make Ko 2^5 or 2^6, so the maximum ratio between K-factors can be as large as 32 or 64. For this moment we take Ko = 2^5.
Because all K-factors are in the range of [-1 ... +1], we need 5 bits plus a sign bit = 6 bits for the K-factors.
For PID * Ko, we have 3 terms (is 2 bits), each consisting of a multiplication of an errorterm of 13 bits and a K-factor of 6 bits, so in totally we need 21 bits (consisting of 20 bits magnitude and a sign bit).
Thus calculating in 24 bits is perfect suited for this problem.
Now we have to choose what number representation(s) we are going to use, and here are some possibilities:
two's complement
signed integer
unsigned integer with a separate sign bit
An interesting feature to notice is, because we're not using all the 24 available bits, we can recognize each number representation by the most left 2 bits.
0... This is a positive number in any of the 3 mentioned notations
10... This is a negative number in signed integer notation
11... This is a negative number in two's complement notation
http://mientki.ruhosting.nl/data_www/pic/jalcc/examples/example_pid_24bit.html
Building a fast PID controller in software, with an overall accuracy of about 12 bits (dictated by the available hardware), I needed fast 24 bit math.
The general formula for a PID controller
PID = Kp * Error + Kd * (Error - PreviousError) + Ki * IntegralError
In this formula error terms are in the range [-12bits ... +12bits] = 13 bits and Kp, Kd, Ki are in the range of [-1 ... +1].
Integer math has the advantage of being faster then floating point math, and it's more easier to control the accuracy there where you need it. Now for integer math, it's more convenient to rewrite the general PID formula like this:
PID = ( 1 / Ko ) * ( Kp * Error + Kd * (Error - PreviousError) + Ki * IntegralError )
Where Ko is some nice factor (power of 2, so dividing becomes just shifting) large enough to deal with the maximum ratios of Kp, Kd, Ki. A first shot would be to make Ko 2^5 or 2^6, so the maximum ratio between K-factors can be as large as 32 or 64. For this moment we take Ko = 2^5.
Because all K-factors are in the range of [-1 ... +1], we need 5 bits plus a sign bit = 6 bits for the K-factors.
For PID * Ko, we have 3 terms (is 2 bits), each consisting of a multiplication of an errorterm of 13 bits and a K-factor of 6 bits, so in totally we need 21 bits (consisting of 20 bits magnitude and a sign bit).
Thus calculating in 24 bits is perfect suited for this problem.
Now we have to choose what number representation(s) we are going to use, and here are some possibilities:
two's complement
signed integer
unsigned integer with a separate sign bit
An interesting feature to notice is, because we're not using all the 24 available bits, we can recognize each number representation by the most left 2 bits.
0... This is a positive number in any of the 3 mentioned notations
10... This is a negative number in signed integer notation
11... This is a negative number in two's complement notation
http://mientki.ruhosting.nl/data_www/pic/jalcc/examples/example_pid_24bit.html