|
#1
|
||||
|
||||
|
I hate "C"
I'm having a very tedious coding problem and I know it has something to do with the types/type casting in C.
I was writing/testing some PID code. It worked fine(I was just tweaking gains), but I noticed some overflow, so I changed alot of my variables from ints to longs. Now when I try to print, nearly everything prints as zero. Here is the code in question: Code:
/*
* pid_output
*
* This function outputs a value using a PID control loop for the specified PID struct.
* Takes a measurement, maximum output, and index of the gains for this loop in EEPROM.
*/
int Pid_Output(Pid_Struct * pid, long measurement,int maxOutput,int eeprom_index)
{
int output = 0;
//Get smaller names for the gains.
int pn = EEPROM_Read(eeprom_index);
int pd = EEPROM_Read(eeprom_index+1);
int in = EEPROM_Read(eeprom_index+2);
int id = EEPROM_Read(eeprom_index+3);
int dn = EEPROM_Read(eeprom_index+4);
int dd = EEPROM_Read(eeprom_index+5);
long p, i, d;
pid->error = pid->setPoint-measurement;
if( ABS(pid->error) < (long)pid->tolerance )
pid->error = 0;
//Accumlate error into the error_i and set the last error.
pid->error_i+=pid->error;
pid->error_d = (pid->error - pid->error_last);
pid->error_last = pid->error;
//This is the PID part:
p =((pid->error * (long)pn) / (long)pd);
i= ((pid->error_i * (long)in) / (long)id);
d= (((long)pid->error_d * (long)dn) / (long)dd);
output = p + i + d;
//DEBUG UNCONVENTIONAL
printf("\rMeasure: %d Target: %d Err: %d Err_I: %d Err_d: %d Output: %d Max: %d",measurement,pid->setPoint,
(int)p,(int)i,(int)d, (int)output, (int)maxOutput );
//Make sure we're in bounds
if(ABS(output) > maxOutput )
output = maxOutput*SIGN(output);
output+=pid->offset;
return output;
}
so what is happening here? the value for "output" prints out fine, but everything else comes up as zero... pn, pd, etc are non zero... Maybe I'm just too tired/lazy to figure this out. |
| Thread Tools | |
| Display Modes | Rate This Thread |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| New NEMO White Papers! "Creating a Killer Packet" and "25 Ways to Sponsor" | Jessica Boucher | Team Organization | 0 | 10-08-2005 10:55 |
| "Thunderbirds" Vs. "Team America" Which one will rule the box office? | Elgin Clock | Chit-Chat | 3 | 07-09-2004 19:53 |
| Conflict between "Initialize_Tracker()" and "pwm13 & pwm15"? Kevin? | gnormhurst | Programming | 3 | 22-02-2004 02:55 |
| how tall is the ramp when in "up" and "balanced" position??? | archiver | 2001 | 1 | 24-06-2002 00:54 |
| I "hate" the new first site | Dima | General Forum | 11 | 06-01-2002 19:40 |