Menu

PID control Library

mmotte
2023-01-18
2023-02-05
  • mmotte

    mmotte - 2023-01-18

    Here is a implementation of a PID controller for 8 bit micros not using FP.

    I am using BT HC-05 to communicate so i can use my cellphone to change control parameters and plot the control.

     

    Last edit: mmotte 2023-01-18
  • mmotte

    mmotte - 2023-01-18

    It don't allow to attach more than one file. So here is the library file

     
  • mmotte

    mmotte - 2023-01-18

    Here is the controller starting up.
    Orange is output
    Red is PV the temperature C
    Green is setpoint
    Blue is error

     
  • Angel Mier

    Angel Mier - 2023-01-19

    Very nice!

    This is a very useful library and a must for a lot of projects; and you made it with only 80 lines of code, that's remarkable!

    Awesome work, Mike.

    Greetings.

    Angel

     
  • Anobium

    Anobium - 2023-01-19

    Nice. I will give this try.

    Evan

     
  • mmotte

    mmotte - 2023-01-26

    Up Date on PID developement

    I am controlling a Solid State Refrigerator (Peltier Module)used for pop storage to stabilize the receiver and LNA amplifiers on my Radio Astronomy equipment.

    The previous file was being developed as a heater (tail lightbulb) and DS18B20 Temp sensor. Now I needed to reverse the control because i am cooling instead of heating. It was as simple as reversing the error equation.

           error = Input -Setpoint    ' For cooling
          'error =  Setpoint -Input    ' for Heating
    

    But there was other learning.


    I had to shut off interrupts during the Sample temperature routine .

        IntOff
          SampleTemp        'Get current temperature
          IntOn
    

    This helped the strange error values I was getting. Solid error values now.


    Another thing was how HPWM works on the PIC16F1705. there is no PWM 2 but there is PWM 3and 4. Also a new Command PWMUpdate. Always things to learn.


    Also with the PID control parameters. I had to make them much more aggressive for use on the refrigerator. It has a very long lag time of 40 secs. this is the time from when you make an output change to the time you see the sensor start to respond.


    Then there was trying to make the above error calculation reversible with a parameter. I called the parameter HeatCool with the idea of it having two values: 1 and -1 . 1 -Heating and -1 cooling.

    error =  (Setpoint -Input) * HeatCool
    

    This did not work. I still don't know why?
    Here is the log of when I changed it to above error calc.
    The error went to -22044 and It should have been -19

    Error= -22044  Output= 593 P part = 593 + I term = 0 + I part = -2383 -  D part = 0
     5212 1124 2146 3168 4190
    
    # ** Here I reloaded the cooling calculation without the HeatCool Parameter
    # **  and I am setting the ouput so I can recover faster.**
    
    Error= -12852  Output= 1360 P part = 1102 + I term = 255 + I part = 255 -  D part = -3
     5212 1128 2150 3172 4194
    Error= -13  Output= 0 P part = -390 + I term = 0 + I part = -13 -  D part = 993
     4294930805 1129 2151 3173Manual =0  Auto =1  AutoMan=   1    > enter new num with ret at end
    Manual =0  Auto =1  AutoMan=   0
     4195Output = 0    > enter new num with ret at end
    Output = 61
     4195 4195 4195Manual =0  Auto =1  AutoMan=   0    > enter new num with ret at end
    Manual =0  Auto =1  AutoMan=   1
     4195
    
     # ** Now we are back into automatic and using the simpler equation for error**
    
    Error= -19  Output= 5511 P part = -570 + I term = 6081 + I part = -19 -  D part = 0
     17945 1125 2147 3169 4191
    Error= -19  Output= 5492 P part = -570 + I term = 6062 + I part = -19 -  D part = 0
     5213 1123 2145 3167 4189
    

    "Error= -19 Output= 5492 P part = -570 + I term = 6062 + I part = -19 - D part = 0"
    This is the debug for looking at the parts of the PID equation

    Output = (kp * error /10) + Iterm - (kd * dInput /10)
    

    Error = -19 because we are ranged times 100 , this means we are 0.19 degrees lower than the setpoint

    Output is exactly that , this is the value we send to the PWM but divided by 100. Actually I am using the 8 it range of 0-255, so 54.92 or 55 is sent to the PWM.

    P part is a simple ratio of error multiplied by a value kp to change the Output the quickest.

    Iterm is a remembered value also sometimes called "bias" which provides a fulcrum point for the P part

    I part (integral) is the calculated error times the ki that is added(or subtracted from the Iterm every scan in this case every 5 secs( samplrtime).

    D part is the derivative (rate of change) Oldinput-newinput. times the kd multiplyer. the is the worry wort that says , things are changing fast ,let's go faster and Things slowed down, let's go slower. Never use in a noisy environment . Set to 0 is off.

    "5213 1123 2145 3167 4189"
    are the scan times in millisecs. Sampletime is every 5 sec . looks like it calculated ate 5.213 sec.

    I'l post the current programs next

    Things are working!
    Mike

     

    Last edit: mmotte 2023-01-26
  • mmotte

    mmotte - 2023-01-26

    Latest programs

     

    Last edit: mmotte 2023-01-26
  • William Roth

    William Roth - 2023-02-02

    PID can be tricky to get right. There are lots of things to consider outside of the actual code. Not for the feint-of-heart or weak-of-mind.

    One problem have seen regularly is an oversized/undersized heaters for temperature control as well as not considering the effect of the ambient temperature.

    A lady called me to fix her fancy PID egg incubator that was running way too hot. She had moved the incubator from indoors (75F) to her back porch that got direct sunlight in the late afternoon. The sun heated it up so much that it cooked her eggs.

    It was all I could do to explain to her that even the most expensive incubator could not control the temperature to 99.5F if the ambient is 95F and the unit is sitting directly in the Texas sun

     
  • mmotte

    mmotte - 2023-02-03

    I have started to do an implementation of PID a couple times but recently I needed it for my project and more web searching brought me across the Brett Beauregard in depth explanation and generic code. I am not new to PID, I have programmed and tuned hundreds of PID loops for process industry but it was on Honeywell controllers or a DCS system, Brett's code was easy to implement in GCB EXCEPT no floats. error can be + or - and after multiplying the error times the kp constant it could exceed the range of the short integer we have to work with. I had to put limits on the error to keep it in the integer bounds. Or keep the tuning constants down.

         If error > 100 Then error =100
          If error < -100 Then error = -100
    

    When I was working on BME280, Theo sent me some "signed long integer" routines (2018?) and i have misplaced them. I only used a couple in the BME280 library file. When I deleted old GCB installs , they must have been lost too.

    I installed my "environmental chamber" today and it was controlling. But this is in an un- airconditioned building and this summer it will struggle. Like you say.

     
  • Fulvio _

    Fulvio _ - 2023-02-03

    I wrote a program that works on a midrange MCU and uses only word sized variable. But is not in GCbasic. Maybe not fine grained resolution and no tests on the real world.

    The variables are also a bit of tricky matter to have two separated controls. So the allocation is limited to the first memory bank.

    The translation may take a couple of day, if that would spring some interest. Besides that, I'll see my schedules. But I'm slooooow...

     
  • Bert

    Bert - 2023-02-03

    Peltier devices can heat or cool, so a incubation chamber that heats most of the time can switch to cooling mode if the ambient temperature gets too high by reversing the polarity. I made some incubators for a local zoo using this method some years ago, but not with an MCU; it used a special PWM chip and an H-bridge (TI UC3638). An MCU version is certainly possible, requiring for example 2 PWM outputs, or 1 PWM and a heat/cool polarity output. The thermal design is 6" fan on a heatsink on the outside, then a 3/4" aluminum block, then the peltier (2" square), and another smaller heatsink with fan on the inside. The peltier must be well sealed around the outer edge to prevent condensation and corrosion.

     

    Last edit: Bert 2023-02-03
  • SPostma

    SPostma - 2023-02-05

    Years ago I helped develop a programmable PID process controller with auto-tuning in C,
    and found that it helps to keep the integrator in reset (0.0) as long as
    abs(Setpoint - ActualValue) is > ProportionalBand.

    Resetting the integrator as soon as 100% output is given might work even better.
    The integrator only needs to regulate away the steady state error.

     

Log in to post a comment.