|
|
|
![]() |
|
|||||||
|
||||||||
![]() |
| Thread Tools | Rate Thread | Display Modes |
|
#1
|
||||
|
||||
|
Problem with negitive numbers
I've been working on a velocity PID for motor control, but I'm having trouble.
When the PID calculation returns a positive value the code runs fine, but when it return a negitice value the motor output begins jumping appearently randomly from very high(5 digit) to very low(-5 digit) values. Once this happens the PID reacts correctly, but due to the values that a far outside of normal it never recovers. Here is the code I think contains the issue, but I can't find it. I added some comments to help explain the issue. Code:
void MotorControl (unsigned char motor)
{
//The printfs in CalculateSpeed() give correct data.
int speed = CalculateSpeed(motor);
int output;
//printf("speed on motor %d = %d\r\n", motor);
//printf("Speed Goal = %d\r\n", speedGoal[motor]);
// Last line in PID prints the PID output, and it always appears correctly.
output = PID(speed, speedGoal[motor], &pidInfo[motor]);
currentOutput[motor] += output;
if (loopCount == 0)
{
//This is where the issue is obvious. It prints strange values within
//the same loop as PID() returns a negitive value, and then until reset.
printf("Output %d = %d\r\n\r\n\r\n", motor, (int)currentOutput[motor]);
}
switch (motor)
{
case 0:
MOTOR_ONE = LimitInput(currentOutput[motor] + 127, 0, 255);
//printf("Motor one set during %d\r\n", motor);
break;
case 1:
MOTOR_TWO = LimitInput(currentOutput[motor] + 127, 0, 255);
//printf("Motor one set during %d\r\n", motor);
break;
}
}
Last edited by EHaskins : 03-02-2008 at 12:58. |
|
#2
|
||||
|
||||
|
Re: Problem with negitive numbers
What do your functions do? Could the problem exist there?
What variable types are you using? Unsigned variables dropping below zero and incorrect casting can cause problems similar to what you are describing. |
|
#3
|
|||
|
|||
|
Re: Problem with negitive numbers
Sounds like arithmatic wrap/overflow in a byte value. Check all the variable sizes. The compiler will choose to keep arithmetic in byte value instead of promoting to integer like ansi standard.
|
|
#4
|
|||||
|
|||||
|
Re: Problem with negitive numbers
What type is "currentOutput[motor]"?
If currentOutput[motor] is, say, 0, and you try adding -5 to it, you will get overflow unless currentOutput[motor] is a signed type. |
|
#5
|
||||||
|
||||||
|
Re: Problem with negitive numbers
the maximum size for a signed integer is 2^15-1 or 32767. Since you talk about large 5 digit numbers, are you sure you aren't overflowing an int?
|
|
#6
|
|||
|
|||
|
Re: Problem with negitive numbers
When people say, "overflow", they mean the number goes from its lowest value it can represent to its highest value (or vice versa).
For example, and unsigned char can represent values from 0 to 255. If you add 1 to 255, you get 0. If you subtract 1 from 0, you get 255. Suppose in this expression: currentOutput[motor] + 127 currentOutput is declared something like this: unsigned char currentOutput[2]; Then if the value is more than 127, you are going to have trouble. I presume that isn't your case, but I used that example because there is something more interesting: when you add two things and assign to a larger thing, the promotion to the larger thing doesn't happen until the assignment. suppose the example above, plus int x; x=currentOutput[motor] + 127; now, since x is an int, you wouldn't (might not) expect the expression currentOutput[motor] + 127 to overflow just because currentOutput[motor] is, say 200, because 200 + 127 is just 327, and that will easily fit in an int. However, C, first it will add currentOutput[motor] + 127 using byte sized math, and THEN "widen" it to 16 bits to promote it to an integer type. By then, the overflow has occured, you wrapped back around positive infinity to zero, and you have the wrong value. (small caveat: If the compiler thinks of 127 as an "int" instead of as a "unsigned char" in the example above, then it will first promote both operands of the + to ints. I don't remember the rules for integer literals off hand, and this compiler may well bend them to save some space anyway. To make the example more precise, and applicable to any C compiler (including ANSI compliant ones), then I could have used two unsigned chars, like intX = ucY + ucZ; In this case, the addition of ucY + ucZ might overflow a byte). I apologize if I went overboard on the detail. This is the sort of thing that confuzles the kids on our team frequently, so I thought maybe some background would help. David Fort Team 1001 Hacksaw |
|
#7
|
||||
|
||||
|
Re: Problem with negitive numbers
Quote:
Code:
void Motor(char motor, int speed); //Sets the goal for the PID calculation void ProcessDrive(void); //Performs PID calculations for all motors, and sends outputs void MotorControl(unsigned char motor); //Performs PID calc for each motor. Called from ProcessDrive PIDInfo GetNewPIDInfo (void); //Creates a PIDInfo struct using default values. void InitializeDrive(void); //Performs drive system initialization. Called from UserInit int CalculateSpeed(unsigned char motor); //Calculates the speed of a given motor int PID (int input, int goal, PIDInfo *info); //Performs PID calc using the supplied params. I'm familiar with overflows, and have had them catch me before. I assumed that was the issue, but I couldn't where the it was happening. Quote:
Code:
int currentOutput[NUMBER_OF_MOTORS]; I'm confident the overflow is occuring in one of these two lines(comments added), since the printf at the end of PID has the correct value, and the printf immediatly following these lines shows the incorrect value. Code:
output = PID(speed, speedGoal[motor], &pidInfo[motor]); //returns int currentOutput[motor] += output; //These are both ints Does anyone see an issue with my logic here? What am I missing? If anyone want to see the full code I'll send it. I would post it, but I don't like posting large amounts of broken code. |
|
#8
|
||||
|
||||
|
Re: Problem with negitive numbers
check your PID loop... maybe you inadvertedly got rid of a typecast.
also, are you sure you dont have any longs defined in there? |
|
#9
|
|||
|
|||
|
Re: Problem with negitive numbers
If you suspect overflow in currentOutput, then bump it from int to short long type to see if the symptoms change.
|
|
#10
|
|||||
|
|||||
|
Re: Problem with negitive numbers
I don't see anything that defines the type of output. If it were an unsigned variable, it would give the behavior you describe.
|
|
#11
|
||||
|
||||
|
Re: Problem with negitive numbers
Quote:
Quote:
I will try that next time I have access to the robot. |
![]() |
| Thread Tools | |
| Display Modes | Rate This Thread |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Problem with Lofting | cooker52 | Inventor | 14 | 04-11-2007 15:00 |
| Problem With Footsteps | Popper99 | 3D Animation and Competition | 1 | 04-11-2007 09:34 |
| Problem with RoboEmu2? | Calvin | Programming | 1 | 12-02-2005 11:27 |
| Problem with communicating with STAMP through serial port | Skabana159 | Technical Discussion | 2 | 06-02-2003 21:10 |
| Problem with OI / RC | Jay Lundy | Technical Discussion | 2 | 29-03-2002 23:07 |