Shop OBEX P1 Docs P2 Docs Learn Events
PID help tuning and approach — Parallax Forums

PID help tuning and approach

grasshoppergrasshopper Posts: 438
edited 2008-08-21 12:24 in Propeller 1
My problem is that I can not tune the PID to run correctly. Do I need to read the temperature less or more? Do I need to provide a cushion for example if the temp is 20.03 +/- .02 so that the temp would range from 20.05 to 20.01

Suggestions please

My Code so far

pid.init(5.0, 1.0, 5.10,TargetT, 30, 10.0, -10.0)

Stash := A2D.GetTemp(1) 'Gets temp 
Drive := pid.calculate(Stash) ' Calculates PID output

  IF (Drive > 1) ' Drive temp up Heat
                                         
         outa[noparse][[/noparse]INNA]~~
         outa[noparse][[/noparse]INNB]~
         MOS.SetDuty(Drive)
                                                         
  IF (Drive < 0) ' Drive temp down cool

         drive := (drive * -1) ' make the neg number positive to pwm
         outa[noparse][[/noparse]INNA]~
         outa[noparse][[/noparse]INNB]~~         
         MOS.SetDuty(Drive)




Some plots of the runs at various P.I.D. constants will come soon
«1

Comments

  • Graham StablerGraham Stabler Posts: 2,510
    edited 2008-07-03 22:33
    You don't tell us in what way it does not run correctly.
  • grasshoppergrasshopper Posts: 438
    edited 2008-07-04 06:05
    Sorry Graham and others

    It runs but not good enough for my needs. I just think that i need pointers on tuning the thing. If I use just the proportional i get great oscillation for example. If i use to much derivative i get weird spikes and so on.

    For now any ideas how i can begin to tune this correctly where do i start?
  • Graham StablerGraham Stabler Posts: 2,510
    edited 2008-07-04 08:45
    What are you controlling the temperature of? What is doing the heating/cooling? Are you controlling a heat pump?

    I doubt very much you need any D at all because generally things take time to heat so there is unlikely to be any rapid changes that need to be fought.

    I would start with P and try reducing the gain to make it less sensitive.

    Graham
  • Paul MPaul M Posts: 95
    edited 2008-07-04 08:56
    There is plenty of information on the web regarding tuning, not all easy to understand. If your system oscillates with P only then the gain is probably too high. As a rule of thumb, apply increasing·Proportional gain(no I or D) until the system oscillates then set the P gain to half this value. Now apply some Integral gain in steps to·reduce the bias error.·Too much I gain will likely cause the systrem too oscillate again. As Graham says you probably don't need any Derivative gain and·any noise can cause more problems than it cures if you use D.
  • scottascotta Posts: 168
    edited 2008-07-04 13:47
    Grasshopper,

    The time constant of your process is one of the
    biggest variables in the equation. I would suspect
    that it takes a long time for a step input to cause
    a full swing in temperature change. If your loop
    is being run too fast, relitave to this time constant,
    you will have all kinds of issues with oscillations.

    Start with I and D set to zero. Increase P until
    you see oscillations in the output, then back P
    down. After that you can do the same to I.

    Remember that I is best for removing persistent
    errors that P will not correct. Too much I will
    cause oscillations too.

    You will probably not need D. Its the least understood
    off the three. D is very dependent on your process,
    and will cause noise on the output if your loop is running
    too fast.

    Lastly, you might need to filter the input signal. A noisy
    input will cause noise on the output. Especially when
    using D.

    Check my last post to this forum, there was a sample
    first order IIR filter written in Spin. Its smooth like
    butter.

    Scott
  • Paul MPaul M Posts: 95
    edited 2008-07-04 16:30
    scatta said...
    If your loopis being run too fast, relitave to this time constant,
    you will have all kinds of issues with oscillations
    The reverse is more likely to be true. If the update rate is too slow there will be large changes in control value and associated large changes in the output which can lead to oscillations

    The P term = Gain * Error·is clearly·independent of update rate.
    The I term takes into account the time between updates·and is therfore also independent of the update period#
    Similarly for the D term.

    We seem to agree on how to tune the loop.
    The D term is well understood; it is just diffucult to apply in the real world due to noise. Fortunately, many systems can operate without D.
    As Graham said, if the process·has a long time constant, the output will change slowly, the D term will be small, and hence probably not needed.··
  • grasshoppergrasshopper Posts: 438
    edited 2008-07-04 19:08
    Thanks all for the nice suggestions

    - "Graham" - To clear this up a bit I am controlling temperature inside a small box that uses a heating / cooling element. I am reading the temperature in the order of +/- .02°c that ranges from -50°c to 150°c. The device is required to maintain a precision of the +/- .02 once the temperature is reached in other word "stay stable +/- .02". Some of you mentioned the notion to toss out the Derivate. I argue this and I could be very wrong, but what if the door to the small box is opened for a few moments. I want the device to react quickly. As far a noise I have not seen any. The oscillations are derived from my over control and programming woes.

    - "Scotta" I noticed the problem you mentioned and have since corrected the loop by making it a little bit slower.
    - "Paul M" I did noted your point as well. It sounds funny, but I solved this by reading more decimal places ( i.e. started out at 2 now I read 6 )

    As far as tuning the whole thing I will do as you all suggest and begin with the P-Gain and so on.

    I will keep you all posted. i have great plots that i need to post here and more questions i am sure.
  • RaymanRayman Posts: 14,242
    edited 2008-07-04 23:38
    wikipedia gives some really good tips...
  • evanhevanh Posts: 15,545
    edited 2008-07-05 02:15
    grasshopper said...
    I am reading the temperature in the order of +/- .02°c that ranges from -50°c to 150°c. The device is required to maintain a precision of the +/- .02 once the temperature is reached in other word "stay stable +/- .02".
    That's a pretty impressive level of precision required. You are wanting a maximum control deviation, once stabilsed, of 200 ppm. That means you would be wanting a resolution of 1 ppm which is a 20 bit sampler!

    Is that what you are using to get temperature feedback?
  • Paul MPaul M Posts: 95
    edited 2008-07-05 09:31
    Grasshopper,

    What temperature sensor are you using that is accurate·to 0.02 deg C?
  • evanhevanh Posts: 15,545
    edited 2008-07-05 11:37
    Hehe, there's the trick, it don't have to be accurate at all. Only has to be stable and repeatable. There is a difference. One has to be wary of that distinction when reading about a sensor's accuracy in literature.

    Accuracy is a metricing term, it defines the sensor's ability to follow the ideal line. Accuracy does not say how precise the sensor is. For a lot of applications, repeatably precise is more than enough. The process parameters can be fiddled to produce the desired outcome.


    Evan
  • Paul MPaul M Posts: 95
    edited 2008-07-05 12:04
    I'm fully aware of the difference between accuracy and repeatability(aka precision) and you are quite right that accuracy is unimportant if one is only trying to maintain a constant temperature and not bothered about the actual temp.

    I find it hard to believe that a device that has such a wide temperature range requires such precision, but only Grasshopper can enlighten us on this.
  • DavidGregDavidGreg Posts: 38
    edited 2008-07-05 12:13
    The best PID article I've found on the web, and it includes source: http://www.embedded.com/2000/0010/0010feat3.htm

    I've coded the source in spin - not much to it really.
  • Paul MPaul M Posts: 95
    edited 2008-07-05 12:49
    DavidGreg - this routine will only work with a fixed update rate since the integral term does not include·the intersample time in its calculation. For the D term I assume that 'rate' is the rate of change of the output value since it is passed to the update routine. The traditional PID routine would apply the derivative gain to the rate of change of the error signal.·Using the output value·is often done to avoid large changes in the control value if the setpoint changes suddenly.
    There is also a minor bug in your code:
    iGain := fMath.Fmul(iState,iGain)
    

    ·Evanh·- how did you get to requiring a resolution of 1ppm? 1ppm in -50 to 150 is 0.0002
  • DavidGregDavidGreg Posts: 38
    edited 2008-07-05 14:57
    Yes, this routine only works properly if called at fixed intervals (using wait) - I choose to time my control loop and save a bit of calculation. Another downside is that the optimum gains will be a function of the update rate - change your update rate and you have to change the gains. Of course, adding a dt to the routine would be simple.

    Thanks for catching the bug!

    I was experimenting with a sensor that could tell me rate, and so I was not calculating the derivative in the routine. I've attached a version with the traditional derivative calculations.

    -David
  • evanhevanh Posts: 15,545
    edited 2008-07-05 15:15
    Paul M said...
    Evanh - how did you get to requiring a resolution of 1ppm? 1ppm in -50 to 150 is 0.0002
    It's just a rough number that hopefully gives a stable working range for the control function. If the dynamics are too coarse then the control function will keep bouncing out of spec on disturbances.


    Evan
  • grasshoppergrasshopper Posts: 438
    edited 2008-07-05 16:34
    Paul M : I am using an lm135AH in a voltage divider arraignment. Initially I considered a Wheatstone bridge but needed more channels on my A to D IC . This voltage drop is sent into a 16bit A to D converter that has a precision 5Volt reference and a precision 2Volt reference so in effect the 3 volts is divided by the 16 bits. I calculated that the LM would never go below 2.2 volts in my application and never above 4.5 volts. This is the reason I set the A to D from 2V to 5 Volts.

    The sensor itself is not accurate to +/-.02 per say, but I have a reference thermometer that is. Thus allowing me to calibrate my device towards the reference thermometer. Also I do not use the LM135AH as the data sheet suggest I am using it as a 2 pin configuration. As a side note * I am trying to replace the lm135 with a RTD sensor, but company politics and pride have insisted that I use the LM135.

    Please comment more on the subject. Be aware I still very new to electronics design and have only been working as such for 4 years. I have learned that I make good mistakes and often. [noparse]:)[/noparse]

    Sorry for the delay in posting the data plots but I am at home and wont return to work until Monday where I can finish tunning the PID. The good news is that i can read the temperature up to 4 decimal places and Its repeatable. The PID is working great it just needs to work better than great.

    Post Edited (grasshopper) : 7/5/2008 4:41:39 PM GMT
  • evanhevanh Posts: 15,545
    edited 2008-07-06 11:58
    Good to hear it's repeatable. Next step is to increase effective resolution. Best solution for that is to add a digital filter on top of the 16 bit sampler. To do that you actually need a little noise - called dither. I'm guessing a bit now but, given the high resolution of a 16 bit ADC, there will be natural noise that is probably good enough.

    I recommend adding a 16 or 32 word averaging (FIR) filter. This reduces the response time but hopefully you can increase the sampling rate to compensate.

    This will increase your effective resolution. You should notice an immediate improvement of control.
  • grasshoppergrasshopper Posts: 438
    edited 2008-07-07 15:19
    evanh:

    yea the plots below show what happens if you take no averages also i used no D or I

    dir2.JPG

    As i promised below is the charts that show the use of a derivative and no derivative. Perhaps as some of you mentioned i may not need to use the derivative


    der.JPG


    I must try to remove the over shooting when driving to the desired temperature. Perhaps too much integral.
  • Graham StablerGraham Stabler Posts: 2,510
    edited 2008-07-07 15:40
    One thing to consider is the time taken for the propagation of heat, if you put the temp sensor at one end of the box and the heat source at the other then by the time the sensor says too hot the heater may have stuck too much heat in, this heat propagates to the sensor causing overshoot meanwhile the cooling is starting getting one part of the system more than cool enough to get rid of the extra heat. This may be a cause of oscillation.

    Graham
  • grasshoppergrasshopper Posts: 438
    edited 2008-07-07 15:54
    Graham this is an interesting point, how would one go about compensating for this? Should I react a bit slower if so I would soot over the temperature for sure.

    Hum now things are getting more and more complex.
  • Shawn LoweShawn Lowe Posts: 635
    edited 2008-07-07 16:17
    Move the air in your box, that would help insure consistant temp.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Shawn Lowe


    When all else fails.....procrastinate!
  • grasshoppergrasshopper Posts: 438
    edited 2008-07-07 16:24
    Yea air movement would be nice but my box does not have much air. Mostly liquid and metal. The liquid is stirred and the box is around a cigarette lighter size.
  • Graham StablerGraham Stabler Posts: 2,510
    edited 2008-07-07 18:13
    Grasshopper,

    It is hard to know what to suggest without an idea of what you are trying to achieve, why and how it is implemented. What I would say is that if you have a volume to which is attached a heater/cooler it is not possible to make the whole volume the same temperature, at least it will be difficult, it needs to be very well insulated at least. There is bound to be a temperature gradient otherwise. I suspect however that there are ways of designing things to minimize these effects but I have no experience with this.

    You might try mounting your thermocouple directly to the heater to begin with, get that sorted and move on to the more demanding full application.

    Oh and there must be stuff about this in the library, at least a university one.

    Graham
  • evanhevanh Posts: 15,545
    edited 2008-07-07 22:28
    Ok, liquid is stired and you've got cooling control as well as heating right? Sounds like a sweet setup. And lifting the lid does not impact a great deal?

    Is the later part of the final graph smooth enough? It's a bit hard to tell just looking at it here.

    What happens without any Integral gain? Does the overshoot go away? If so then yes you have too strong a gain or it's being accumulated too early. Managing wind-up on step changes can be a pain.

    I'm late for work ...
  • grasshoppergrasshopper Posts: 438
    edited 2008-07-08 00:22
    Evanh:

    Integral gain helps as I expected tremendously. I noticed that some people zero out the integral, but adding the right upper and lower limit seems to be working great as of now. Without this upper and lower limit the temperature will definitely overshoot and with to little upper and lower limit it will never reach the temp. So all in all it is working pretty good, the later part of the graph is almost good enough. As far as taking the lid off I will have to get back to you on that.

    Great comments Ill post more plots as soon as I can

    Sorry Graham that I can not go into specific details about the unit itself. I am bound to a contract on this particular project.
  • Graham StablerGraham Stabler Posts: 2,510
    edited 2008-07-08 11:01
    Integral gets rid of steady state errors but are you saying it is helping overshoot?

    What are these upper and lower limits you mention?

    Graham
  • AleAle Posts: 2,363
    edited 2008-07-08 11:38
    You have to take in consideration the time it takes for the device to react to temperature change. If you heat and measure a while after you stopped heating, he heat reached probably (you have to determine) all the corners of your device, now you can measure the temperature, The position of the heater and the sensor also affect how the heat is distributed. The best would be to have a heater all around you "box" but it may not be feasible, otherwise you will have a gradient. A pair of sensors may help if you have partitioned heating, 2 zones or something like that. As others suggested, try to determine the response time heating vs temperature change at several points, that would help to determine appropriate correction factors depending where are you on the heating process. Remember that heat is transferred from the hot to the cold. So if the environment temp is too close to your end temperature... your response is going to be faster. From that, an adaptive correction may be necessary.

    I hope it helps.
  • grasshoppergrasshopper Posts: 438
    edited 2008-07-08 14:40
    Graham Stabler said...
    Integral gets rid of steady state errors but are you saying it is helping overshoot?

    What are these upper and lower limits you mention?

    Graham

    The upper and lower limits that I mentioned are part of the PID object that I am using.

    PUB init (_Kp, _Ki, _Kd, setPoint, offset, maxArea, minArea) 
    
    



    It clamps the total gain of the integral as far as I can tell. It seemed through my experiments that if the integral gets to large during the rise in temperature from a long span (i.e. rising from 0C - 50C) it would over heat. This is logical because the integral continues to grow until the desired temperature is reached and if not dealt with (zero it out when approaching the desired temperature, or put upper and lower limits as I am doing) the integral will be such a large number that it the "drive" will overshoot the desired temperature.

    This is probably not typical and is a desired effect in most P.I.D applications, but in mine i don't want to overshoot the temperature by much.
  • Graham StablerGraham Stabler Posts: 2,510
    edited 2008-07-08 14:59
    Overshoot is far from a desired effect in a lot of control, sometimes it is more important to get the the operating point quickly (like avoiding a pedestrian when driving) but often the wanted response is to be critically damped, that is to have no overshoot. I think your integral gain is probably too large, in most applications the rise to the setpoint will be so fast very little will be added to the intergral. Your system is slow but that is all relative, for a slow system you can expect to have low gains. To repeat what you probably already know, in the steady state a system without I may have a steadystate error, it might have no ripple but just be 0.1degree out, that small error can be accumulated over a relatively large amount of time to correct for it.

    By adding limits I think you are creating a different kind of controller.

    Is the PID object from the object exchange?

    Graham
Sign In or Register to comment.