In certain situations, it helps to know when the PID has computed this bit will be true for one cycle after the pid calculation has occured.
What is meant by "one cycle after the pid calculation has occurred"? One cycle of what?
I want my PID to calculate a new value exactly once per loop().
I think I don't understand how the PID figures out when it should run a calculation. I want it to run a calculation exactly when I tell it to, so I set the SetSampleTime=1ms. So, when I run pid.Compute, I think the pid will calculate a new Output within 1ms.
I run pid.Compute(); but then I want to make sure the pid has finished computing the new Output before I write Output to my motor. I figure this might take up to a couple ms. I have this code:
pid.Compute();
while(!(pid.JustCalculated())){}; //make sure PID computes before proceeding
analogWrite(motor, Output);
I don't understand how JustCalculated works though, and I don't know if this technique will work. If pid.JustCalculated() returns a 1 after the library computes, then when does it get set back to zero? Will it still 1 the next time loop() runs?
I could be wrong but I was under the impression that a consistent PID sample update is required for the 'tuning values' of P,I&D terms to be most effective. If the update time is variable due to different paths taken in the main loop, then tuning and therefore control results might also be variable? Recall the the I and D terms are integrated over time.
You may be right. Like I said, I am confused about the whole setSampleTime phenomenon. It's not clear to me how the PID does its internal timing.
What I'm doing is I have a 6rpm slow-spinning motor with an encoder. The encoder spacing has only 13 pulses per rotation, so I cannot afford to wait till a lot of pulses go by and then divide to get an average speed. Instead what I do is every time the interrupt gets a pulse, it sets a flag and my main loop() only runs if it detects that. Then it calls micros() to compare with the last time to get the speed. Then I wanted to compare that speed with what I know the speed SHOULD be and adjust the motor output to match it. I wanted to use PID to do that, but I'm not sure it fits in with my scheme, since I don't understand how PID uses timing internally.
I suppose I can write a crude P control myself...the simplest would be
if (speed<desiredspeed){output++;}
else{output--;}
But this would be pretty slow-reacting.
int delta;
int p;
int speed desiredspeed;
...calculate speed...
delta=speed-desiredspeed;
output+=(int(delta*p));
analogWrite(motor, output);
I think that basically reinvents P control, but at least I could have it run when I want it to. Other ideas?