# Using PID to control dc encoder motor's position

Hi,I am studying Brett Beauregard's website's PID control tutorial. It said "If this PID is going into a microcontroller, a very good argument can be made for using an interrupt. SetSampleTime sets the interrupt frequency, then Compute() gets called when it’s time."

I don't know which method is right :using running time of the loop() to be the sample time or each interrupt interval time to be the sample time as he mentioned?

If I choose running time of the loop() to be the sample time , it probably get so many PID outputs already than interrupt method dose once.

Interrupt time as samle time:the wheel's speed decreases before reaching the target position, the interrupt freq also decreases by the time. So I need to change SetSampleTime in process,right? I guess the interrupt time interval is decided by wheel rotation, and I need to know the rough time interval between each interrupt.

Hope someone can correct me if I am wrong. But don,t correct my grammar , because English is not my native language, please.

Thanks.

When interrupts are involved.... then it usually means that the sampling period is equal to the time interval between adjacent interrupts. But this is all an 'ideal' situation.... since carrying out instructions within an interrupt routine takes time. As long as significant amounts of time are accounted for... then ok.

But don,t correct my grammar , because English is not my native language

You don't want free hints on how to improve it?

Southpark:
As long as significant amounts of time are accounted for... then ok.

Dose significant means approximation? Because the time interval between adjacent interrupts actually is not a integer number, it has numbers after decimal point(float number). I have to add a millis() in the interrupt function to get the time interval when every interrupt is called,and think a method to sub each other.....

And what is the loop mode sample time interrupt's defect if I use it?

jremington:
You don't want free hints on how to improve it?

Not now! Thank you!

Running from loop your timing accuracy is limited by the latency of the slowest piece of
code called from loop(). If interrupt-driven you are only affected by the latency of other
interrupt routines - typically that means much better regularity, which will make the PID
better behaved and more predicatable.

The actual frequency the PID is called is very important,
too slow and you'll lose control, too fast and the D-term may be quantized to one bit
which can only be fixed by an interpolated estimate of the rate-of-change

yangkai:
Dose significant means approximation?

I mean... if somebody asks you to push a button right now in your house. And it must be done right now.... immediately... otherwise it messes up somebody elses schedule and timing. But suppose you always need to get to the button...... by needing to walk over to it. Then that few seconds could be critical. This same thing applies in control systems.... timing... or accurate timing can be important. If a system requires a measured 'value' NOW.... then using up extra time to take the measurement and averaging stuff and carrying out mathematical processing and decision making can mean that you weren't able to provide that information 'NOW'..... or at least not immediately when requested.

I only know how to use external interrupt , where can I use a timer-like interrupt? When time is up, call the interrupt to do the PID?