![]() |
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) |
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. |
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.
|
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. |
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?
|
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 |
Re: Problem with negitive numbers
Quote:
Code:
void Motor(char motor, int speed); //Sets the goal for the PID calculationI'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 intDoes 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. |
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? |
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.
|
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.
|
Re: Problem with negitive numbers
Quote:
Quote:
Quote:
|
| All times are GMT -5. The time now is 23:51. |
Powered by vBulletin® Version 3.6.4
Copyright ©2000 - 2017, Jelsoft Enterprises Ltd.
Copyright © Chief Delphi