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.
void MotorControl (unsigned char motor)
{
**
//The printfs in CalculateSpeed() give correct data.
**
int speed = CalculateSpeed(motor);
int output;
//printf("speed on motor %d = %d
", motor);
//printf("Speed Goal = %d
", 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
", motor, (int)currentOutput[motor]);
}
switch (motor)
{
case 0:
MOTOR_ONE = LimitInput(currentOutput[motor] + 127, 0, 255);
//printf("Motor one set during %d
", motor);
break;
case 1:
MOTOR_TWO = LimitInput(currentOutput[motor] + 127, 0, 255);
//printf("Motor one set during %d
", motor);
break;
}
}