PID Integration over not constant dt (∆time)

Is integration over not constant dt (∆time) a possible thing? Let's say you have a PID loop with differentiating frequency, can the integral part of it still work? (Assuming you know the dt from the last iteration)

Could I just use a variable dt (∆time) in my calculation and the PID principle would still function correctly?

• Are we talking about minor adjustments or large variations in sampling? As long as your sample frequency is high enough it doesn't really matter but it's easy to convince yourself that if there's a step function in the error and you delay the next sample by a large amount your controller will not respond the same way. Jul 28, 2014 at 3:45
• The dt is changing just about a couple milliseconds plus minus, I guess that is not a large change. Jul 28, 2014 at 11:04

3 Answers

Short answer: yes, as long as you adjust for the real dt you are fine

Longer answer: In reality, we are always using discrete time approximations of continuous time concepts of derivative and integral. So a varying dt just means the accuracy of your approximation is varying over time, but that's ok. Examples of situations where you might run into problems are:

• You are on the edge of stability and some of the slower timesteps are too slow
• You dt doesn't change randomly and happens to interact with important system frequencies
• Could you please explain further your second situation where I might run into a problem? How can that cause problems? Jul 27, 2014 at 20:02
• It's pretty much just a theoretical situation. Have you ever seen videos of the Tacoma Narrows bridge? The same thing can happen to controllers, but you have to be really unlucky for dt variation to be the cause. In any hobby controller, and most industry systems, periodic variation in dt is so far down the list of concerns it never gets checked. Jul 28, 2014 at 2:40

Normally, it would best to fix sampling time via hardware timer interrupt.

In case this cannot be done (like using software-only looping), it will work as long as you calculate,

normalized_error = (latest_reading - set_point) / (latest_time_stamp - previous_time_stamp)

• Could you please explain why is it best to keep a fixed dt (time sampling) and provide the negative consequences of using a variable one? Jul 27, 2014 at 20:04
• For 'professional' (real commercial product) hard real-time control systems, normally, the processor/MCU has hardware timer to produce periodic time tick interrupt in which it will be easily program to append PID sampling task to the system time tick task. Fixed sampling make the system more predictable and have best/precise/accurate fine-tuning results.
– EEd
Jul 27, 2014 at 21:00
• Do not worry too much if cannot do the above. 'Normalized' will still work, to a large extend.
– EEd
Jul 27, 2014 at 21:08

Indeed I d also go for an interrupt method. As this method allows you to execute code, do something etc.. on a very arbitrary base. Which I think youll ,due to the non constant dt

• Welcome to Robotics, Jacques. As it stands, your response doesn't add to the Q&A - could you extend your answer? Aug 6, 2014 at 17:54