PID loops and equations


Thread Starter



I have developed a relatively simple PID loop in a ucontroller. However I am a bit unsure about a few details:

1. In a lot of the standard equations I have seen they contain Ts/Td for example. What does it mean to divide your sampling period by the derivative time?

2. If I specify a derivative time Td, does this mean I should take a sample (error sample i.e SP-PV) at say t(0) then another sample at t(0+Td) and subtract from each other? In any of the equations I have seen or code I have looked at there doesn't appear to be a timer that is set to sample the error difference for Td, all I see is Ts/Td. This is where the confusion comes in. If the code is not sampling for Td then why bother setting a Td? How does Ts/Td effect the loop?

The same above applies for the integral.

My current understanding leads me to the following:
Use Interrupt to sample Analogue (PV) every say...
* 1ms (faster then any real possible changes in measured value), once the sample is taken then evaluate P, I and D and output to PWM.


* Sample every 1ms evaluate P output to PWM, sample every Ti for integral and evaluate I output to PWM, sample every Td for derivative and evaluate D output to PWM.

Any suggestions would be appreciated.


Bruce Durdle

A integral is a summation over time - so you need to multiply the current value of the error by the sample time to integrate. The weighting of the integral term in the overall output depends on the integral time Ti, and this since you are multiplying error (%) by time you need to divide by time to get the output as a %. Hence the integral term is usually written e x Ts/Ti.

Derivative is the opposite - de/dt. In this case you are measuring the change over the sample interval and dividing by the interval to get the rate of change. So the weighting factor for the derivative term has units of time - (e2 - e1) x Td/Ts.

With the derivative term in its simplest form, you are going to get a LOT of problems with noise - especially in a sampling system. You need to modify the D term to limit the upper frequency response or maximum gain. This can get complicated.

Another way to obtain an integral term is to use the reset feedback approach - which has a lot of advantages when it comes to automating the bells and whistles such as auto-manual transfer. With this, the output is obtained by adding the gain-weighted error to a signal derived from a transfer lag function running off the feedback input. The feedback can be taken directly off the controller output, off the output of an A/M module, or off other signals further downstream in the control loop.


Peter Nachtwey

>With the derivative term in its
>simplest form, you are going to get a
>LOT of problems with noise - especially
>in a sampling system. You need to
>modify the D term to limit the upper
>frequency response or maximum gain.
>This can get complicated.

This problem is reduced by using high resolution feedback and sampling using a FPGA so there is no sample jitter.
Sampling in an interrupt will introduce jitter depending on how long the interrupts are turned off. If one pays attention to the details a second derivative can be implemented using an observer or low pass output filter. These techniques work very well for the first order derivative.

It really bothers me when people say they don't use the derivative term because of "noise" instead of trying to fix the problem. In many cases the derivative and even the second derivative term is necessary to properly tune the system. A plant with two poles requires two gains other than the integrator ( the integrator comes with its own pole so it doesn't help ) to properly place the poles.

George Buckbee

Your question highlights some very important points about signal

There is an inter-relationship between the sample time, derivative time, filter time, and process deadtime that must be considered. If you sample too slowly, derivative will make erratic movements. If your filter is too large or too small, the derivative (and possibly gain & integral) action will not be right.

Keep in mind that there are many places where the instrument signal is filtered. This includes:
* At the instrument
* In the analog input card (hardware anti-aliasing)
* In the input processing
* at the PID algorithm

You can find some tips on tuning, filtering, and comparing times at the following links:
Time Comparison:

Hey Bruce,

Thanks for your reply. I am still a bit undecided about the integral. In my code basically I took the following steps:

Sample Input continuously (as regular as the state_machine (super loop) returned to the adc_sample point), I would at this stage evaluate the P variable and update the output. The integral would only be evaluated and added to P if a flag was set in a Timer interrupt and Ti had elapsed.
adcvalue = Adc_Read(0);
adcvalue = adcvalue & 0x3FF;

//Start PID
Yerror = Ysetpoint - adcvalue;
Yp = Kp * Yerror;

//iState Flag gets set in a Timer Interrupt if Ti has elapsed
if (iState_flag == 1)
iState = iState + Yerror;
if (iState > accerror_max)
{ iState = accerror_max; }
else if (iState < accerror_min)
{ iState = accerror_min; }
Yi = iState / Ki; //Ki = 0.1
iState_flag = 0;
Ypid = Yp + Yi;
The above seems completely incorrect!!?

I had always taken Ti as being the actual time that the integral was evaluated.

I plan to sample the input using the interrupt, then? Should I then evaluate P and I (i will look at D later) every time I take a sample? I am still have trouble with Ts/Ti, I do understand how it effects the integral weighting, but I don't get how it relates to the process time constant.

It also seems like I will have to make use of floating point maths.


Bruce Durdle

Hi Mike,

No, you don't wait for the IAT and then add in the integral - in a large process, Ti can be several hours.

An approach I have used is to sample the analogue value on a regular timed basis - this sample time needs to be fast compared to the significant time constants in the process.

For an incremental algorithm, the proportional term is given by

P = K x (e1 - e0) - the change in error during the last interval.

The integral term is given by

I = K x Ts/Ti x e1

The output value is then changed by adding the above terms to the old

V1 = V0 + P + I

But every scan you need to calculate the integral term and incorporate it else you are likely to get some big bumps.

You don't need floating point but you do need multiple precision - I have found it helps to keep track of the unused digits and every time you add in the integral term you retain the residual that was not used in the output calc. Add the new multi-precision integral term to this residual and then add to the output.


If the value of V is to 4 digits, but the integral is to 8:
V0 = vvvv ....
I = 1234 5678
V1 = (vvvv + 1234)
Residual is RI0 = 0000 5678
On the next scan the new integral is 0098 7654
Add the integral residual to this - 0099 3332
Use the 1st 4 digits to calculate the new output value
V1 = V0 + 0099
Save the rest as the new residual
RI1 = 0000 3322

Hope that makes sense...