CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to CCS Technical Support

Scaled Integer Math

 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
Rodders



Joined: 04 Mar 2010
Posts: 12
Location: Birmingham

View user's profile Send private message

Scaled Integer Math
PostPosted: Mon Apr 12, 2010 9:58 am     Reply with quote

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
bungee-



Joined: 27 Jun 2007
Posts: 206

View user's profile Send private message

PostPosted: Mon Apr 12, 2010 10:43 am     Reply with quote

SIGNED int kp = 655; // actual value: 6.55 <--- this will not work, because you are using 8 bit integers....

You should declare it like: SIGNED int16 kp = ......

So look at your other variables for this Wink
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group