Calculating Integral with Arduino

Hi,

I want to implement a PID control system for my projects. And in this topic I want to discuss calculating the integral term.

I have decided to integrate using the trapezoidal rule. Where

Integral += (sample2 + sample1) * 0.5 * delta_t_seconds

. sample1 and sample2 are two consecutive samples of the error; and delta_t_seconds is the time between samples in seconds. However I'm not sure about the sampling rate. Should I sample as fast as possible or should I have a pre defined time interval?

Thanks.

As I'm used to see it the I term is mostly a summing of the remaining error in order to adjust to the final result.
What is "sample1", "sample2"? Samples of what?

Railroader:
As I'm used to see it the I term is mostly a summing of the remaining error in order to adjust to the final result.
What is "sample1", "sample2"? Samples of what?

Samples of the error. Sorry I forgot to add the "+" symbol to show the integral is being accumulated.

Railroader:
As I'm used to see it the I term is mostly a summing of the remaining error in order to adjust to the final result.

That would be integration using the rectangle rule.

ningaman151:
I have decided to integrate using the trapezoidal rule. Where

Integral += (sample2 - sample1) * 0.5 * delta_t_seconds

. sample1 and sample2 are two consecutive samples of the error; and delta_t_seconds is the time between samples in seconds.

If you are trying to do the first term of this....
then it should be (sample2 + sample1).... not (sample2 - sample1).

Reference: Trapezoidal Rule -- from Wolfram MathWorld

DaveEvans:
If you are trying to do the first term of this…
then it should be (sample2 + sample1)… not (sample2 - sample1).

Reference: Trapezoidal Rule -- from Wolfram MathWorld

facepalm you are correct my bad; silly mistake. I have it written down correctly in my notes; it was a mistake during typing it out again.
Thank you for the correction.

The problem with this way of calculating is that P and I are separate.

Other alternative is

U[n+1] = U[n] + k{ [E(n)-E(n-1)] + 1/Ti * E(n) * (cycle duration between 2 cycles) }.

Averaging should not be necessary if your arduino is fast enough and your process slow (10 x factor).

Each cycle, the output of the P action of the previous run is substracted from the actual output U and the new value of the P factor action is added to the actual output U. Then the I action is calculated and also added to the output. Therefore if you limit U at +-100% you will not get into trouble otherwise you will if you have different factors for P and I. Integrator windup can be a very bad for your application if not managed properly.

Best Regards,
Johi.

I want to implement a PID control system for my projects.

Before going off on tangents like this, have you read up on the topic? PID has been extensively analyzed and discussed, for decades now, and dozens of textbooks are available.

And, the implementation is system-specific. You will often find that the I term is not needed.

jremington:
Before going off on tangents like this, have you read up on the topic? PID has been extensively analyzed and discussed, for decades now, and dozens of textbooks are available.

And, the implementation is system-specific. You will often find that the I term is not needed.

I've been learning about it for a while; mainly on YouTube. I found that a lot of people use the I term in the wrong way (when implemented on Arduino).

I remember my university lessons from 1973 regarding the technic using PID. Then the world was analog. Only a few years later lessons in digital filters, digital control, became the subject.
Now, 50 years later, how to use PID ought to be more than well established and debating "stone age" technic obsolete.

ningaman151:
I've been learning about it for a while; mainly on YouTube.

Eehhh... Youtube is polluted by too many self made theories, but okey, there are certainly some genuine knowing people being correct.

I found that a lot of people use the I term in the wrong way (when implemented on Arduino).

As I remember.... The I term is the last one to be used if the P and D (don't forget about kP, kD) does not get accurate enough.

You have to sample "fast enough" per Nyquist.

If you have a fixed sample rate you won't have to calculate "delta_t_seconds" each iteration which might result in less computation if that matters in the application.

The usual sampling rules of thumb with PID are to sample at least 10 times faster than the measured system response time constant, and that the sample rate be constant.

I found that a lot of people use the I term in the wrong way (when implemented on Arduino).

What else would you expect from hobbyists and amateurs, posting their poorly informed ideas and one-off experiments on YouTube?

jremington:
The usual sampling rules of thumb with PID are to sample at least 10 times faster than the measured system response time constant, and that the sample rate be constant.

Excuse my ignorance but what do you mean by measured system response time constant? It's been a while since I did any control.

ningaman151:
Excuse my ignorance but what do you mean by measured system response time constant?

Stepping in.... Suppose You apply a step, a significantly different control signal to f ex a heavy motor. Then it will take some time for the motor to react, respond, to that signal and enter a steady state.

what do you mean by measured system response time constant

You really, really need to do some reading.

Forget YouTube. Many of the “technical” (especially Arduino) videos are made by people who have no idea what they are doing.

@jremington

Agree. Git hub show projects made "against all specifications"....

I've been dabbling with PID lately and quickly found out the importance of sample rate and that this be held constant (as per reply#12). It's crazy, but adjusting the sample rate from say 2.5x to 10x the response (ultimate or critical period) of your system can actually be used as a control parameter affecting overshoot and ability to maintain tight control about the setpoint. Found some really good information in my research. First, check this neat graphic on how easy a system can be to control. You really can't miss flipping through the web pages here, and finally, you might find this interesting.

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.