Hi Guys,
I used Tim Wescott's PID without a PhD document to come up with my controller loop. Now, my application is a light source that is being controlled with the duty cycle of a pwm - I have a current feedback loop that is telling me how much current is going through the LEDs. Now to get these values on the same scale, I just read what my ADC reads at maximum allowed current and that is 100%. And then my input (IRDrive) is controlling how intens the LEDs should be (again in %). My control loop looks like this: double UpdatePID(SPid * pid, double error, double position) { double pTerm, dTerm, iTerm; pTerm = pid->pGain * error; // calculate the proportional term // calculate the integral state with appropriate limiting pid->iState += error; if (pid->iState > pid->iMax) pid->iState = pid->iMax; else if (pid->iState < pid->iMin) pid->iState = pid->iMin; iTerm = pid->iGain * pid->iState; // calculate the integral term dTerm = pid->dGain * (position - pid->dState); pid->dState = position; return pTerm + iTerm - dTerm; } And I played around with various P,I and D gains but seem not to be able to get it ionto a steady state, it keeps flocking all over. The struct looks like this: typedef struct { double dState; // Last position input double iState; // Integrator state double iMax, iMin; // Maximum and minimum allowable integrator state double iGain, // integral gain pGain, // proportional gain dGain; // derivative gain } SPid; Before I implemented the PID controller, I simply had this: AVGfdbckprcnt = ((float)100/(float)MAX_CURRENT) * AVGfeedback; /* if (AVGfdbckprcnt < IRDrive){ if (CurrPWM < 255) CurrPWM++; PWM_WriteCompare(CurrPWM); } else if (AVGfdbckprcnt > IRDrive){ if (CurrPWM > 0) CurrPWM--; PWM_WriteCompare(CurrPWM); } else continue; PWM_WriteCompare(CurrPWM); Which worked fine and now i replaced this with: AVGfdbckprcnt = ((float)100/(float)MAX_CURRENT) * AVGfeedback; CurrPWM = UpdatePID(&Cntrl, AVGfdbckprcnt-IRDrive, AVGfdbckprcnt); PWM_WriteCompare(CurrPWM); which doesn't quite work yet... Any clues, hints or suggestions would be appreciated!
Thank you! roN