Rodders
Joined: 04 Mar 2010 Posts: 12 Location: Birmingham
|
Scaled Integer Math |
Posted: Mon Apr 12, 2010 9:58 am |
|
|
Hi guys,
I have been writing a program whereby I need to use integers instead of floats because floating point calculation will take more time in a system I need to be processing data as quickly as possible.
I thought that the basic way of using scaled integers is that you scale them using a scale factor and then scale them down. I have written this code below and so far it's plain outright not working.
Note that the maximum possible value I can get is 124 and all the time it seems that the PIC is getting the maximum value regardless of the different situations and readings.
Code: |
SIGNED int scaleFactor = 256;
SIGNED int kp = 655; // actual value: 6.55
SIGNED int ki = 1; // actual value: 0.01
SIGNED int kd = 657; //actual value: 65.7
//scale the constants
kp = kp * scaleFactor;
ki = ki * scaleFactor;
kd = kd * scaleFactor;
error = testError (Port_A.sensors1, Port_A.sensors2, Port_A.sensors3, Port_A.sensors4, Port_A.sensors5, Port_A.sensors6);
integral = integral + error;
derivative = error - lastError;
//scale the variables
error = error * scaleFactor;
integral = integral * scaleFactor;
derivative = derivative * scaleFactor;
//scaling back
turnCorrection = ( (kp * error) / scaleFactor) + ( (kd * derivative) / scaleFactor) + ( (ki * integral) / scaleFactor);
|
Thanks in advance |
|