I've wrote a simple PID controller routine (below). I haven't tested it yet (float's will need working to ints etc) but I was hoping to model it first and to that end I've created a spreadsheet which is attached.
I've noticed that the output is always positive, so even when there's no error the output is about +24 on the duty cycle. This seems to be related to the integralTotal which will only decay when the output has a positive error. All help appreciated!
I've noticed that the output is always positive, so even when there's no error the output is about +24 on the duty cycle. This seems to be related to the integralTotal which will only decay when the output has a positive error. All help appreciated!
Code:
float p_gain = 0.01;
float i_gain = 0.01;
float d_gain = 0.00;
int16_t PID (int16_t currentError, int16_t *previousError, uint8_t *integral) {
int16_t output = (p_gain * currentError) + (*integral * i_gain) + (d_gain * (currentError - *previousError));
*integral = *integral + currentError;
*previousError = currentError;
return output;
}