PID - A simple position control simulator

Join Date
Apr 2002
Location
No income tax, no capital gains tax. Freedom!
Posts
8,389
The link below is to an Excel spreadsheet. I used Excel 2003. Hopefully those with out Excel can at least use the Excel viewer. I decided to use Excel because it is widely available. The spreadsheet below simulates a simple servo system with a PID. The PID gains are initially set to low values. The challenge is to tune this system so that the actual position matches the target position as closely as possible. Doing so reduces the ISE "integrated squared error" which should be used as a means of evaluating whether gain changes are making the system track closer or not.

The PID is a simple one that takes the error and provides a correcting control signal. The PID form that I like and will use for these examples is the incremental form. I like this form for two reasons:

1. It handle saturation well because there is no separate integrator to unwind.
2. Because it is incremental, one can move smoothly between openloop ( manual ) and closed loop ( automatic ) modes.

I reduced the update rate to 10 milliseconds to keep the spread sheets smaller. A later exercise will be to reduce the update period to 1 millisecond to see the difference.

The system could represent a well designed simple servo motor system.
The equation for the system is G/(s*(tau*s+1). I don't expect anyone to know what this means, yet. The point is that it has a time constant tau that has units of seconds and a system gain G which has units of inches per second per volt. ( Sorry about that metric people ) I have converted the system model to a difference equation using z transform tables which I will explain much later.

What I want you to do is to tune the system WITHOUT changing the system gain G and the time constant. The goal should be to get the integrated squared error down below 60. Better yet is to get the ISE below 59. Notice that right now the ISE is above 7000.

I cheated. I can calculate the gains so I know what is possible.

What I want you to notice:
1. Tuning is easy when you have an evaluation routine like the integrated squared error.
2. The output immediately jumps to 10 volts. There is nothing you can do about this. This is the result of the system instantly accelerating instead of ramping smoothly. In real practice this should be avoided. I left this "defect" in to show Norm and Friedrich how a PID should be able to handle staturation.
3. Notice the gains you end up with when you achieve the lowest integrated squared error ( ISE ). What does this say about how the integrator and differentiator should be used.

PID and Servo Simulator.

This is a confidence builder. I will cover other systems with more complicated models that will be more difficult to tune. I will cover many of the different types of systems so there will be many spreadsheets with slightly different characteristics.

Enjoy. Feel free to ask questions. I wonder who will have the lowest ISE.
 
Well I got to 58.958 after some futtzing around.

I'll wait to post my numbers as they are not close to the other two early birds.

I like the model peter.

Thomas Wrote:
>Peter is there another way?

You must be new around here. :D
Or else your $5 from Peter for the set up is in the mail... hehehehe

nOrM
 
Last edited:
Well Done!!

It seems that everyone found that the integrator should be much higher than the proportional gain. It also appears that everyone found the integrator very helpful. I chose this model because I knew it would be so. So much for the myths integrators and differentiators.

Hopefully you found the ISE very helpful. I came up with this idea own my own back in '93. I called it the sum of errors squared. Later someone pointed out that industry uses this technique already, but its common name is integrated squared error. Oh well, it is a good idea anyway.

The abrupt start at the beginning distorts things a little. Hopefully a real servo system would NEVER see such an abrupt start. It may cause a real servo system to trip on over current.

By trial and error. Peter is there another way?

There are many ways to calculate PID gains, but few can do better that what you have just done. I have posted a link to the same power point presentation in the past. To calculate the gains one just needs to have two things:

1. A model of the system.
2. A desired response.

I am going avoid the math for now. We have only one model so far that the formulas to calculate the gains changes from model to model so I will get back to the ISE approach.

In previous posts people have mentioned genetic algorithms where trial modifications are tested against the a standard. If the new modifications are better then they become the new standard. This is what you did only the changes where not random. I am sure that you found a procedure for modifying the gains that could easily be programmed into a computer. However, a computer could easily minimize the ISE in a few milliseconds if it has a model. Think how long this trial and error approach would be if you tried this on a temperature systems that took 15 minutes or a half hour to check one change of the the gains. The advantage of the ISE approach to tuning is that it always works. At least I haven't found a case where it doesn't.

Now for more fun. I picked the model and the sine wave target generator to be on-the-edge.

What happens if you change the system gain 'G' to 1? The gain will not be high enough.

What happens if you change the system gain 'G' to 3? The gains should be reduced by 2/3rds only.

What happens if you change the system time constant to .5 seconds.
There is not enough gain in controller to over come the sluggish response.

What happens if you change the system time constant to .05 seconds?
The ratio of the integrator gain and to the proportional gain will increase.

Excel uses 80 bit calculations and all these number are 'perfect' what happens if the position feed back is rounded to the nearest thousandths of an inch?

I made the sample time 10 milliseconds. What happens if the array lengths are expanded by a factor of ten and the sample time is decreased to every every millisecond?

What happens if the sample time is .001 seconds AND the positions are rounded to .001 inches?

It would save me time if some one could modify the spread sheet and report the results.

The PID I used is one that depends on the error between the target position and the actual position. What if I changed the PID to use the actual position ( PV ) like the PIDs in many temperature systems?
I will do this later.

I will do a velocity system next with just one time constant. It will be easier to tune than this one. It will not be as much fun. However, the gains will be adjusted differently. I will also have some that are much more difficult to tune. The point I am trying to make is that different models require different tuning 'rules'. The coming goal is to recognize a system just by how it responds to a step change in the control signal. Later we will excite a model with a step input. Then we will use the LINEST instruction to reconstruct the model.
 
I don't know what the lowest value is.

El Cid said:
151
1900
2.7

yields 58.816

Is there a lower value to be had?

The lower limit is limited by the disconinuity in speed when the system starts and the output is limited. Theoretically, should otherwise be able to get the ISE down to 0 if there are no discontinuities. There is really very little improvement in the overall tracking ability between 58.816 and 59. However, you have found a way reduce the error just a little quicker than the rest of us.

I can reach 58.767 when I set the desired response to be 3 real poles at 50 radians per second.

Kp 750
Ki 1200
Kd 14.5

but that is not really a valid answer. One can see the output is very erratic. A valid answer would be 3 real poles at 40 radians per second.


Kp 480
Ki 6400
Kd 11.5
ISE 58.982

So how do I do it? I have a model and I pick a response desired response.

Kp:= (3*l^2)/(G*a)
Ki:= (l^3)/(G*a)
Kd:= (3*l-a)/(G*a)

Where
a:=1/time constant. This has units of frequency in radians per second. This is also known as an exponential frequency.
l:= lambda. -l is the desired location for the three real poles. The farther the pole is away from 0 the faster the system will respond to changes in the target.
G is the system gain as stated above.

These equations apply ONLY for this model.

Notice that as the system gain 'G' increases, the PID gains decrease.
Notice that as the time constant increases the gains must increase.
Here is something to think about. If the desired response is slow then Kd can go negative! If the desired response 'l' is less than 1/3 the system frequency 'a', the numerator goes negative. In some cases negative gains work! If will find an example of that later.


There is one more thing to try. Replace the sinewave target generator with step changes in position. Make all target position from 0 and below 2.5 seconds +10 inches, and all positions afterward -10 inches. Try your optimal gains. Then try the PID gains when lambda ( l ) is equal to 20

Kp=120
Ki=800
Kd=5.5

You are now beginning to find that minimizing the ISE may not be optimal as the system may be tuned 'too hot'.

Notice the ISE does not work very well for step changes. In this case the ITAE, integared time absolute error works better. The ITAE is better for systems, like temperature systems, where step changes are made to the set point.


Same simulator as before with step changes, ITAE and calculator for PID gains

Norm and Friedrich, again notice how well the PID handles saturation without the windup and overshoot. Convinced?
 
Peter,

I think my original description of my problem (I still think it was the controller's problem) was not very clear.

What was happening was that the actuator was being commanded to move beyond its physical limit in one direction. Note that the controller kept separate gains and errors for each direction. Lets call my problem direction "down". As the system is being commanded down but physically can't move there, an error signal is building up. Then the system is commanded up (different error summer) and moves with no problem. When the system is commanded back down... BANG the previously accumulated error signal is added and since this was a 3000 lb. slide moving, people noticed :oops:

That said, the more I remember it, there were several problems with the setup. First was that this was a jog move (as defined at the controller) and should have been an open loop move!!!! Why the controller was building up an error on what should have been an open loop move, I don't know. The system was not concerned with following errors so that parameter was set very loose and allowed the error to build up. I won't mention names ;) but translations in the manual for the parameter descriptions were confusing us as we went along.

I have attached a version of your spreadsheet which sort of shows this behavior. The command Signal amplitude is set to 10 but the actual position is clampped at 6 in the positive direction. It is very hard to get ISE under 4000 even using negative kd. The control output spends most of it's time jumping from full max to full min to overcome the non-linear behavior of the actual position. I think if your spreadsheet kept separate values for the different directions, then you'd see exactly what was happeining. But even as is, I think it demonstrates what non-linearities can do to a system.

------------------

To everyone else (I know it's the weekend):

You are getting a gold mine here!!! Play with the sheets and read Peter's descriptions over and over. You are getting a preimum controls education. This is better than anything you could get at a seminar or even graduate level college (been there) for FREE!!! And customized to all of us here and where our questions lead Peter.

If any of us hasn't increased our PID control understanding after this summer, it our own fault!
 
ISE = 53.365

Still playing.


Next...

ISE = 25.178


Next...

ISE = 22.412


Next...

ISE = 4.918
The control signal is a little radical out of the gate but settles in within 1/4-cycle.
 
Last edited:
It works better with the gains I calculated.

Clamped with proper gains

The gains you had where strange. I said some PIDs will have a negative Kd, but not this one. As I said above, this will only happen when you specified poles where l is less than 1/3 of 'a'. See the formula above. I used the second set of gains I calculated above where Kp = 480. You can see the motion response using my gains work in an intuitive way.

Norm, your motion controller is probably not using 80 bit IEEE floating point like these spread sheets. 16 bit microcontrollers and DSP are limited to signed integers which limit the range of numbers to -32768 to 32767. If an overflow error occurs while summing the different terms then watch out. All I can say is that your motion controller probably had code in it written by rookies. I made that mistake once in the 80's. I was a rookie once. We quickly added checks for overflow error after summing each term in the PID to avoid overflow run aways. Now we use 32 bit floating point on our newer products so we don't need to waste time checking for overflows.

You should notice that there are extra fields in the upper right part of the second spread sheet. All you need to do is to specify where you desire the 3 real poles to be and the PID gains are calculated for you. A good conservative number is l=20 (l is my symbol for lambda since Phil's site doesn't do greek yet, hint). A more aggressive number is l=40.

One more point. Notice how helpful Norm's graph/spread sheet was in helping me diagnose his problem. A picture is worth a thousand words. A good motion controller should be able save the resulting motion profile so it can be e-mail to tech support. Better yet is to be able to access the motion controller over the internet. This is how we support our product. This is also why I choose the RSLogix500 software for doing the later test. I think we can learn much from sharing the trends/graphs.
 
Several Next's later...

ISE = .123

Still looking for zero!


OOOooops!

I just went back and re-read the ground rules...

I missed the part about keeping the same gain...

So... RESET, REBOOT, Square-one!
 
Last edited:
Terry Woods said:
Several Next's later...
ISE = .123
Still looking for zero!
OOOooops!
I just went back and re-read the ground rules...
I missed the part about keeping the same gain...
So... RESET, REBOOT, Square-one!

I have pointed out that you can only theoretically appoach zero. The inital jump from 0 to 15.708 inches per second will always cause an error between the target and actual position at the biginning. In these examples an ISE of 0 is not possible.

The spreadsheets don't have noise or quantizing errors. Both would make the ISE much worse.

Is anyone going to try rounding the actual position to 3 decimal points?
 
Peter,

I played around with my original clampped spreadsheet trying to minimize ISE and that's where those gains came from. Yes, the control was bouncing wildly but I was just seeing what ISE might be possible.

My best 2 point control (controller flipping from +max to -max) was with kp:700, ki:2900, kd:0. ISE about 18600.

Now a question on the ITAE. You are taking the error (Target-Actual) and multiplying it by the current time. Then you are summing all these values together. I have no books here to look this up but this just fells fishy. My inkling would be to multiply the current error by the time step and then sum them all up which is how it would be done for crude numerical integration of say a function. Am I missing something here?
 

Similar Topics

Been working with PLCs for a couple of decades, but almost 100% DirectLogic. Have a customer who wanted me to make a couple of simple changes to a...
Replies
3
Views
1,119
Hey All In my down time between projects i enjoy building different types of control systems. I already do it all day for work but like to tinker...
Replies
7
Views
1,695
Hi all. Simple question here. I've seen both sides of the spectrum here and no one can give me a concrete answer. On a AB CLX platform PID...
Replies
16
Views
6,690
So you thought you would take the weekend off! No way. The link below is saying download me! Give me a try! The link is to a spreadsheet that...
Replies
15
Views
23,496
Hello, has anybody done a "user block", that delivers PID functionality? This could have been done using simple control blocks available in...
Replies
1
Views
6,281
Back
Top Bottom