How to implement PID control on MCU with known PID parameters ?

MrAl

Joined Jun 17, 2014
11,389
Hi,

Please let me know which table format suits you the best, I can upload it for you accordingly.

The transfer function is:

T = 3.30758E-09 * t^4 - 7.70310E-06 * t^3 +4.17181E-03 * t^2 - 7.85164E-02 * t + 27.10683

I hope this can help.

Hello again,

Thank you for that it's very clear.
Correct me if i am wrong, but this looks like a function that starts out around 27.1 at t=0 and as time progresses to about t=10 seconds the temperature drops to around 26.7 and then as time progresses further the Temperature just keeps rising and as time progresses more it becomes apparent that the temperature increases even faster which indicates thermal runaway.

The question i have now then is, what happens if i apply power for 100 seconds (Temperature rises to about 53.6) and then suddenly disconnect the power completely, what happens to the Temperature then?
I ask because there seems to be no heat storage anywhere because there is no initial value for the Temperature after some heating has already occurred. It would be very strange if the Temperature dropped suddenly back to 27.1 degrees. Is that even possible? Normally we have some thermal time constant.
For example, for a different system we might see this:
Vout=E-(E-Vc)*e^(-t/(C*R))
where
Vout is the voltage across the capacitor after time increment t,
E is the driving voltage source level,
Vc is the initial voltage across the capacitor,
C is the capacitance,
R is the resistance.
In a thermal system, this could be:
Vout is the final temperature,
Vc is the initial temperature,
C is the thermal capacitance,
R is the thermal resistance.

The main point is that Vc is the initial capacitor voltage so if we drop the drive E to zero, the capacitor voltage does not suddenly drop to zero (which would be a sudden compete discharge) but gradually drops exponentially until the drive E again goes up and so the capacitance starts to charge again.

So what we need to know next is what happens when the power is disconnected after some heating has occurred in the past. There should be another function or something that describes this behavior.

Any thoughts on this?
 

Thread Starter

Kevil

Joined Jun 28, 2020
179
Just to explain the chart. I simply switched on the heat plate to full power and measured the temperature at the hot plate by the MAX6675 Module + K Type Thermocouple Sensor and ATtiny202 (RTC, SPI, USART) each second, that's the chart probabbly not the transfer function as you thought.

Slight temperature drop at the beginning is just a small error caused by the polynomial approximation of real measured temperature values.

When needed I can upload here the real measured temperature values for comparison with polynomial approximation.
 
Last edited:

MrAl

Joined Jun 17, 2014
11,389
Just to explain the chart. I simply switched on the heat plate to full power and measured the temperature at the hot plate by the MAX6675 Module + K Type Thermocouple Sensor and ATtiny202 (RTC, SPI, USART) each second, that's the chart probabbly not the transfer function as you thought.

Slight temperature drop at the beginning is just a small error caused by the polynomial approximation of real measured temperature values.

When needed I can upload here the real measured temperature values for comparison with polynomial approximation.
Hello again,

Ok then i guess that is all the info you have at this time but maybe that's ok if we go by the pure physical system that generated that response we may be able to pull this off.

First though i made a slight mistake when i quoted that the response dips then increases indefinitely. It does dip, but if we go past about t=490 seconds we start to see a decrease and i think it dips very very low. That could be because of the time response which is probably an approximation gleaned from a curve fitting.algorithm. That may be ok too because the pulse times will most likely be much shorter than 490 seconds.

Ok, so the time response you posted strictly speaking represents a system for four integrations and can be represented by the a set of four ODE's as follows:
dX4/dt=X3(t)
dX3/dt=X2(t)
dX2/dt=X1(t)
dX1/dt=Vin
Vout=Vin*A+X1(t)*B+X2(t)*C+X3(t)*D+X4(t)*E
where Vout is really T and Vin=1 (on) or Vin=0 (off) and some constants you will recognize.
X1 through X4 are the system state variables and dXN/dt are their derivatives.
A=27.10683
B=-0.0785164
C=0.00834362
D=-4.62186e-5
E=7.938192e-8

From those four ODE's we can obtain the exact response (according to the response you gave that is) and i believe we can get the initial values from that set also which gives us a solution represented by five equations with four initial values and that means we can use those to generate the function we need in order to simulate this either in some simulator or with a simple C (or other language) program.

If you would like to try to solve that set go right ahead. If not when i get back here again probably tomorrow or late tonight i'll post a solution.
 
Last edited:

MrAl

Joined Jun 17, 2014
11,389
Thank you for reply. I think I have all required parameters for PID control already available (Kp, Ti, Td, I, D) solved automatically on www.pidtuner.com
Oh i didnt think there was ever any question about that, i just thought you wanted to be able to simulate it yourself to make sure those PID constants really worked or not.
What constants did you pick?

The previous post would allow you to set up a simulation with the procedure outlined earlier. Doing that gives you confidence that the controller will actually work. The ODE's describe the system in a more complete way so as to allow simulation and the calculation of response. They can also be used to simulate in continuous time.
 

Thread Starter

Kevil

Joined Jun 28, 2020
179
Kp = 0.321286016254141
Ti = 44
Td = 309.83027598131656
I = 0.007301954914866849
D = 99.54413508495838

The problem si only that I don't know how to use the parameters to calculate the control signal (0%-100% power) base on preset temperature value and measured temperature value in time t .

Screenshot 2022-01-25 at 19-09-58 PID Tuner.png


Screenshot 2022-01-25 at 19-13-30 PID Tuner.png
 
Last edited:

MrAl

Joined Jun 17, 2014
11,389
Kp = 0.321286016254141
Ti = 44
Td = 309.83027598131656
I = 0.007301954914866849
D = 99.54413508495838

The problem si only that I don't know how to use the parameters to calculate the control signal (0%-100% power) base on preset temperature value and measured temperature value in time t .

View attachment 258676


View attachment 258677

Hello again,

That's interesting. Looks like they are using a 2nd order approximation though. I dont think it is a very good idea to blindly use a program though without knowing exactly what it is trying to accomplish and why it does it that way. That's why i like to simulate everything from the ground up, just to make sure.

Doing that, there seems to be a problem with the original transfer function specification. After solving the set of ODE's and doing a simple pulsed on/off simulation, i found that the model causes the plate to somehow be able to cool as well as heat up.
As i pointed out earlier, there is a 'dip' in the response between t=0 and t=20. There is also a large dip sometime after 500 seconds. Either of those dips means that there must be a cooling mechanism built into the system somewhere, and since there probably really isnt any, that means it does not model the physical system very well. In other words, if we have a heating coil and we power it with full (or even partial) power the temperature should always rise and possibly level off. If it dips down at any point even for a second, that means there is something else in the system that causes the plate to cool below room temperature which would be some sort of air conditioner or Peltier device or some unusual air current or something. This doesnt seem to be much of a problem, but the problem is that if the unit is turned off there is a tendency for the plate to cool down below room temperature which makes it unreal, and because it can cool down so far that means the model should be changed to something that has a more monotonic response. This should not be too hard to do.

This problem illustrates one of the pitfalls of using curve fitting software without carefully checking the resulting curve fit for unreal and problematic jumps that do not seem to appear during a simple plot simply because those problem points were never plotted. This is because some curve fitting with polynomials cause the curve to pass through points that are given, but also pass through points that were not give and those points are not part of the original response. Because of this sometimes we might see y axis points say 5 and 6 come out just fine, but instead of getting a between point of 5.5 it dips down to -1 or up to 9 or something like that, and we dont see it because we havent plotted it or presented that between point of 5.5 to the curve fit algorithm (such as the Method of Lease Squares).

Since in the case of the heating plate the plotted part of the response is nearly monotonic already, we should be able to easily modify the coefficients to provide a more realistic response, one that better matches a real life heating plate. It may have to take a different form however in order to ensure that the temperature NEVER dips as the 'on' time progresses.
A quick example, although probably not appropriate for this particular project, is an x^2 response. That means that at t=0 the plate can be 27 degrees, and as the on time progresses the temperature just keeps climbing up and up and up and never dips back down. I guess an x^4 type response can be similar if the x^3 term is left out, but we can look into these more carefully.

So what are your thoughts on this now?
 

Thread Starter

Kevil

Joined Jun 28, 2020
179
Hello again,

That's interesting. Looks like they are using a 2nd order approximation though. I dont think it is a very good idea to blindly use a program though without knowing exactly what it is trying to accomplish and why it does it that way. That's why i like to simulate everything from the ground up, just to make sure.
I know exactly that I need second order PID system for Hot Plate control based on similar systems.

I don't need that someone point me by to some small glitches in the Hot Plate temperature curve approximation, which doesn't have nothing with my problem, but I need real advice from someone who knows the problem well on how to calculate the control signal based on set and measured temperature nothing more.
 

Sensacell

Joined Jun 19, 2012
3,432
If your goal is to actually control a REAL hotplate with PID, all this simulation stuff is going to have you spending hours fixing problems that have almost NOTHING to do with the real task.
When you finally start working on the REAL hardware, it will be like starting over from zero while you learn what the actual, real problems are.

Get the hardware going, write the simplest on-off controller first, before drowning in all this theoretical stuff.
Observe the behavior of the simple system, gain real-world, intuitive experience that will make debugging the code so much easier.

You will get the project done so much faster.

I have written several PID controllers from scratch- the algorithm is dirt simple.
The hard part is fixing all the little edge condition problems, like "Integrator Windup" for example.
The little details are always system-specific, so spend your time on the system, not in simulation fantasyland.
 
Last edited:

MrAl

Joined Jun 17, 2014
11,389
I know exactly that I need second order PID system for Hot Plate control based on similar systems.

I don't need that someone point me by to some small glitches in the Hot Plate temperature curve approximation, which doesn't have nothing with my problem, but I need real advice from someone who knows the problem well on how to calculate the control signal based on set and measured temperature nothing more.
Hello again,

If you are looking for what to do with the Ki, Kd, and Kp i explained that already and had shown the procedure on how to do that so you can try whatever you want to try with that. Use the lines for integrator, differentiator, and constant.

Here are actual program lines that perform the controller which improves the response of the raw plant:
Code:
for k=1 to 30000 do
  t=k*dt
  iLvC=CalcLC(dt,U,iL,vC)
  iL=iLvC[1]
  vC=iLvC[2]
  Measurement=vC
  Err2=Ref-Measurement
  Y2=Ki*(Y1+Err2*dt)
  dX=Kd*(Err2-Err1)/dt
  P=Kp*Err2
  U=Y2+dX+P
  Err1=Err2
  Y1=Y2
  if k/1000=floor(k/1000) or k=10 then
    printf(1,"%f  %f  %f  %f\n",{t,iL,vC,Err2})
  end if
  --wait dt seconds
end for
The wait period is implied so there is just a rem statement no actually wait. The wait time is 'dt'.
The CalcLC() function calculates the next response, iL and vC take the two responses from that calculation to be used in the control algorithm that follows. vC is the node being controlled but because the system is clocked the iL signal is also necessary for the new calculation each time.
The printf() statement just prints out the time and responses every 1000^th clock time, no need to look at every single clock period in most cases once it is up and running successfully.
The response without the controller is oscillatory and drawn out over time, while the response with the controller is a little oscillatory but for a much much shorter time and then the response damps out to nearly zero error.
If you still are not sure what to use for Ki, Kd, and Kp, try some different values and plot the results that will give you a feel for what you need. Start with maybe Kp. Because there are so many integrators, Kd will probably do the most good, but try some numbers and see what you get.

However, I have a feeling you did not understand what i was trying to say later, but that's ok good luck with it i hope you find what you are looking for.
 
Last edited:

Thread Starter

Kevil

Joined Jun 28, 2020
179
Please don't post nonsense when you don't understand the problem.

I was looking for this equation which is working well.

PMATune.png
 

MrAl

Joined Jun 17, 2014
11,389
Please don't post nonsense when you don't understand the problem.

I was looking for this equation which is working well.

View attachment 259058
Wow, you come here asking for help then call it nonsense?
You should not mock what you dont understand. If you dont understand that program code then you just mocked many control system handbooks, which BTW you should definitely buy.
I've studied and worked in control theory and the industry for years so don tell me what is nonsense is and is not, got it bud?
 

Thread Starter

Kevil

Joined Jun 28, 2020
179
I was just asking for simply equation not the theory. My comment was probably too strong. Buddy sometimes we can go for a beer ;).

I am expert in data communication, I used to work for Motorola as IT manager. Programming anything from Intel I7 (c++/x64 ASM) to 8 bit MCU's isn't a problem for me.
 

MrAl

Joined Jun 17, 2014
11,389
I was just asking for simply equation not the theory. My comment was probably too strong. Buddy sometimes we can go for a beer ;).

I am expert in data communication, I used to work for Motorola as IT manager. Programming anything from Intel I7 (c++/x64 ASM) to 8 bit MCU's isn't a problem for me.
Ok that's great, but i have to say i like the part about the beer the best (har har) :) I used to go out about once a month to my favorite bar but they demolished it and built an apartment complex there instead, which some guy got mad about and burnt it down! They had to rebuild it a second time!

My technical library contains about 200 books on electronics, engineering, advanced mathematics, etc., and several on control theory. I was heavy into control theory some years ago :)
One of the control theory books i liked a lot was very into advanced concepts. State variable control schemes was the best part i think, along with observers, controllability theory (controllability gramian, special matrix structures, etc.).

Here is a link but i have to apologize if you dont need this or dont care about this particular concept:
https://en.wikipedia.org/wiki/Controllability_Gramian

Take care for now,
Al

If you are really into this stuff you could probably find a ton of articles on the web that could be interesting even if you already know a lot about this already.
 
Top