Beginners: please read this post and this post before posting to the forum.

0 Members and 1 Guest are viewing this topic.

while (1){ s = CalcSpeed(); if (s > targetSpeed) pwm = pwm - 1; else if (s < targetSpeed) pwm = pwm + 1; SetMotorSpeed(pwm);}

If I just use a simple feedback loop, wouldn't this be good enough?:Code: [Select]while (1){ s = CalcSpeed(); if (s > targetSpeed) pwm = pwm - 1; else if (s < targetSpeed) pwm = pwm + 1; SetMotorSpeed(pwm);}Mike

while (1){ error = setSpeed - CalcSpeed(); // if moving slower than wanted, positive error pwm = pTerm*error; SetMotorSpeed(pwm); // assumes SetMotorSpeed can handle negative numbers}

Actually, for the D term, the exact timing is not important. Consider this:...we can just absorb it into the gain, D, so we getT = P*error + D'(error1-error2)

You set the PWM based on a percentage of the requested output compared to the maximum output, you don't just set the PWM according to that requested output.

Then it seems that before you can implement PID you have to at least measure the max sensor value when PWM is at max?

if the sensor is x and the max. is y...you do x/y*255 to get the amount of time it should be on during a cycle.