PID, adjusting the sampling time, meaning of units

I've written some PID controller functions, and I'm trying to make my code more reusable for future applications.

I want to understand the effect of sampling time on a PID, and whether there is a clean relationship as to how sampling time affects the Ki and Kd constants.

I'm particularly perplexed by the "units" of a PID controller, if I could work these out it would make much more sense. I've been able to find by trial and error* that kp=2, ki=0.00001 and kd=100000 works well, but can't quite work out whether ki and kd would scale if the sampling time were varied.

The values involved seem so wildly different I have to wonder if these is something very odd going on wit units and if all would make more sense if relatively scaled by a sampling time.

Assuming I don't violate the nyquist condition by going too slowly is any smapling time appropriate? Should I be able to keep kp, ki and kd all the same when doing this or would they need retuning for any change to sampling time?

The term which Ki multiplies with looks to have units of (us) and Kd multiplies by a (us^-1) term, do the relative term sizes all come down to how fast the thing being controlled (motor shaft) tends to move** in units of (for a shaft)radians per us. us seemed easier than ms or s as I can keep to integer maths and not have the code sloed by too many float operations, hence although the shaft only rotate a few times a second I used us timings.

Thankss

**am I forgetting something obvious or do I just need to get some sleep
*6am in the morning here, trial and error took all night, apologies for any typos or stupid logical errors in this post

P.S. anywhere I can ask about PID for when things aren't actually properly proportionall

It depends on the controlled system. I'd stay with a fixed sampling time, eventually filter the input.

Find out what will result if you half or double the sample rate at a given signal (slope...). How will that affect the derivative and integral of the signal?

Perhaps if you showed the relevant part of your code?

1 Like

that's an interesting question

the following illustrates results from a PID simulation i have. i ran it with the same K parameters but with different sampling rates: 100 and 50 msec.

looks like the curves are not the same. the hash marks on the curves indicate the actual values being plotted. the one set of curves have points twice as often

looks live the results converge sooner when the sampling rate is faster

2 Likes

Hey, nice chart. It looks like the response to a stepped error.

And demonstrates that at least there is no formulaic answer, like if you double the sample rate you have to halve the constants, or whatever.

Halve. Don't get to use that word too often. :expressionless:

And as PID user but not writer, it doesn't surprise me that doubling the sample rate speeds convergence.

It seems that more likely is the case that doubling or halving (!) sample rates would run into other problems sooner, like pesky realities of computer maths.

Or as the OP suggests, just being too slow.

The PID loops I exploit run at 2-8 kHz, which just baffles me in a physical system that, it seems, cannot possibly be responding to changes to its operating parameters anywhere nearly that quickly.

But I do not argue with success, and so far, faster has consistently meant better.

a7

The PID algorithm knows nothing about units, as the input and output values are specific to sensors and actuators, indirectly related to a physical system.

To make sense out of PID K values, you must construct a complete system model, and the "units" will become apparent. That is covered in Control Theory, which is typically a one or two year course for advanced students in an engineering school. Advanced math skills are required.

If you like though, the units of Kp are (output actuator units)/(input sensor units).

1 Like

With the benefit of actually being concious and awake this time,

It seems that expressing a PID in the right way makes it reasonably able to handle any sampling time (so long as not excessives slow, and maybe not so good if so fast as to see single step changes in the measured variables on onlya fraction of runs of the loop) and still give pretty similar performance with the same Kp Ki and Kd values.

NowPIDTime = micros();
PIDTimeDiff=NowPIDTime-PreviousPIDTime;
if(PIDTimeDiff>=SamplingTime){
   //various other stuff here

   MainTerm=KProp * error;
   IntegTerm=IntegTerm+((PIDTimeDiff * KInte * error));
   DiffTerm=KDiff * ((measured-PreviousMeasured)/PIDTimeDiff);

   //various stuff related to summing the terms and storing data for next run of loop

where PIDTimeDiff is a measured copy of the sampling time (so will vary the value if you sample ever so slightly too late as vs your desired sampling rate).

For my system this behaves the same if I sample at 2KHz (500us) through to 200Hz (5000us), haven't tested vastly shorter or longer sampling times yet.

Thanks everyone

P.S. I'd be interested to know any PID tricks for better control of a DC motor, particularly in regards to the way that it isn't properly proportional, at lower PWM values fed to it you get no motion until suddenly above some threshold the motor starts moving "quite fast" (approx an eighth of its maximum 255/255 speed). This makes the integration part particularly tricky and prone to overshooting, and also makes small changes in setpoint tought to handle. Thanks

With these values, you basically have a D controller for your motor. Maybe try something within this range to get PID control.

Regarding the effects of sample time, I would experiment with the PID Tuner online tool.

To compare the effects of using a different sample time, I would do several step response tests, both tests using the same kp, ki and kd values. Each test using a different sample time.

Generate some simple csv data (i.e.60 samples) for each test, in the format: time(sec), input, output. Copy/paste the data into the PID tuner table on the first screen. Select the complete time range.

On the last screen you can try different kp, ki, and kd to see its effect. Note that Ti represents 1/ki and Td represents 1/kd.

However, keeping kp, ki, and kd the same for both of your tests, you can compare the effect of using a higher or lower sample rate.

There is another parameter for time (tao) which also can be edited to see its effect (this is the time constant of your system ... how fast it responds).

Thanks for the pidtuner website and paper. The website looks really useful given its ability to take an import of my own measured data. I'll look in more detail at the paper, but while I did once take a physics degree which taught me most of the fundemantal maths involved, I've never learnt the notations used in control theory, I might not get that far with it unless there are some good resources online about getting a feel for that sort of notation starting from more typical calculus and perhaps fourier principles.

A rule of thumb with PID control systems is that the PID loop period should be 1/10 of the system response time constant, or somewhat faster. Little is gained by much shorter loop periods.

1 Like

P.S. I'd be interested to know any PID tricks for better control of a DC motor, particularly in regards to the way that it isn't properly proportional, at lower PWM values fed to it you get no motion until suddenly above some threshold the motor starts moving "quite fast" (approx an eighth of its maximum 255/255 speed). This makes the integration part particularly tricky and prone to overshooting, and also makes small changes in setpoint tought to handle. Thanks

Perhaps using limits on the integral output term to mitigate integral windup

Forgot to mention that on screen3 (3. Select Model), PID tuner will have selected the model it thinks best fits your data. You manually select different models to see how well the calculated response matches your data.

Agreed, this is a derivative controller, and might exhibit erratic behavior. Kp and Ki have little or no influence, and the controller output is dominated by the D term.

I suggest to start over, read some of the "PID tuning" tutorials, and try out one or more of the suggested general approaches.

I've been reading some more, seems there are also possibilites for "Proportional on measurement" PID controller algorithms. What I can't tell is whether they would be naturally better suited to running a DC motor for position control applications than the typical"Proportional on error" scheme would. Is this worth trying as a reimplementation? PoM does sound relevant, as PoE tends to, in many exmaples look like it is for processes where one has to keep an output at some higher or lower level to maintain a constant output level, whereas with a DC motor it is very much a case where you hold position by having the PWM output go back to 0% (or just a bit above or below 0% if holding against a weghted load).

It certainly does seem like PID algorithms were designed mainly for things which are not much like DC motors? Afterall with a DC motor for position control I have immediate control of motor speed, whereas the thing I'm actually working to achieve is a position, the integral of speed. A lot of PID examples seem to have an "actuating" output acting in the same "units" as the thing being controlled (like watts going in to a heater and temperature in a chamber, there might be some lag between them but temp will be strngly influenced by watts now, not just by the integral of joules provided so far). Is there a better type of control loop to look up for DC motor position control, one which has as many easily understandable introductory materials as the PID concept does?

I seem to be in a situation where I can get the DC motor to run about as fast as possible for most of the way between setpoints, then brake under the influence of Kd just hard enough to stop near the setpoint and use a slightly modified Ki to compensate for small remaining errors, this last part usually involving a second or two * of low-PWM barely-moving-at-all motor "humming" (default PWM freq is in the audible range) back and forth (including the waits for the integrator to rise enough to get the PWM value high enough to start moving) to compensate for the last few degrees of output shat angle inaccuracy before reaching the setpoint properly. I'm getting, randomly, between no overshoot at all, and overshoots of just a few degrees at the ends of motions (setpoint changes) in the 10 to 180 degree range.

Is that about as good as I can hope to get? Am I probably at the point here where mechanical inaccuracies would drown any attempt to make the PID better, particularly in terms of shortening the "humming time" without adding substantial overshoot.

Thanks

*compared to the time spen moving in the fast bit o the PID controleld motion this varies between lasting half as long as the ast bit to "humming" for 4 or 5 times te fast bit's time period

PID algorithms are a fine starting point for control of any system with a measurable property and a controllable means of affecting that measurable property. A DC motor is an example of such a system, and many people successfully use PID to control motors. Hobby servos are a popular example.

The main drawback of PID is that the system is assumed to be linear, and many systems are not. DC motor speed as a function of PWM is very nonlinear.

More advanced approaches have been proposed to take nonlinear systems into account, which is why Control Theory is an advanced topic. The method you mention may be one of those approaches, but I've never heard of it.

If your kd is non-zero, you need PoM - especially for a controller with a high update rate

At higher PWM frequencies, I've found it surprisingly linear (at least for a small “flying fader” motors I've tested), it's mainly the static friction that makes systems like this hard to control.

Power_Broker, can you explain that further please, it might be a really crucial clue for me to progress from. There isn't much online about "Proportional on measurement", so anything you can explain is useful. Thanks

Except for the rather large part of the plot where the speed is zero, regardless of the PWM value.

This is a more typical speed versus PWM plot:

I think that the proportional part should be based on the absolute value in case of speed, else on the displacement from the set position. With position holding the minimal motor speed has to be handled so that the motor certainly starts even with a small error.

This should be related to the question of units in PID calculations.