View Single Post
  #7   Spotlight this post!  
Unread 02-02-2008, 22:45
EHaskins EHaskins is offline
Needs to change his user title.
AKA: Eric Haskins
no team (CARD #6 (SCOE))
Team Role: College Student
 
Join Date: Jan 2006
Rookie Year: 2006
Location: Elkhorn, WI USA
Posts: 998
EHaskins has a reputation beyond reputeEHaskins has a reputation beyond reputeEHaskins has a reputation beyond reputeEHaskins has a reputation beyond reputeEHaskins has a reputation beyond reputeEHaskins has a reputation beyond reputeEHaskins has a reputation beyond reputeEHaskins has a reputation beyond reputeEHaskins has a reputation beyond reputeEHaskins has a reputation beyond reputeEHaskins has a reputation beyond repute
Send a message via MSN to EHaskins
Re: Problem with negitive numbers

Quote:
Originally Posted by usbcd36 View Post
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.
Here are the prototypes for the other function involved.
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.
As I tried to explain, I have several printfs thoughout the different function, so I am confident that the values are correct until the first printf in the code in my first post.

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:
Originally Posted by Abwehr View Post
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.
It is defined here.
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
I was able to determine that the switching from high to low values is an overflow by examining the terminal info, but I still can't identify the primary issue.

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.
__________________
Eric Haskins KC9JVH