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

Thread Starter

Kevil

Joined Jun 28, 2020
179
How to implement in software hot plate control on MCU with known, measured PID parameters? It is 2nd Order system.

I will measure hot plate temperature each second and do not know how to calculate the PWM duty cycle (0 - 100%) for triac control to achive required reflow profile temperatures stored in the MCU. For example I will measure in second 120 temperature 56°C and the reflow temperature should be 62°C at this time. How to calculate the required PWM duty cycle for triac control for time period 120-121 second?

k 175.3352919
w 0.006332428
gi 0.25484488
theta 0.25484488

Kp 0.321286016
Ti 44
Td 309.830276
I 0.007301955
D 99.54413508
 

Attachments

MaxHeadRoom

Joined Jul 18, 2013
28,619
There is an app note for this application on the Picmicro site, I have a copy on another PC if needed.
A triac is generally controlled via phase angle control, rather than PWM
AN958
 

Thread Starter

Kevil

Joined Jun 28, 2020
179
I don't have any problems with programming of the MCU see picture bellow (ATtiny202). I need just the advice how to put the PID coefficients together (equation) to be able to control the duty cycle for the triac - Zero Cross Detect (ZCD).
 

Attachments

MrAl

Joined Jun 17, 2014
11,389
I don't have any problems with programming of the MCU see picture bellow (ATtiny202). I need just the advice how to put the PID coefficients together (equation) to be able to control the duty cycle for the triac - Zero Cross Detect (ZCD).
Hi,

What kind of temperature sensor are you using?
 

Sensacell

Joined Jun 19, 2012
3,432
Build the algorithm, it's really just a bunch of multiply and accumulate functions that get executed on a timed schedule.
Pay special attention to how you handle register over and underflows, you are processing signals here, and they do what they want.

Make the PID parameters easy to adjust.
Tune it up on a live system.

Start with Proportional gain only.
Add Derivative to damp oscillations and overshoot.
Add Integration to tighten up the setpoint tracking.

It's actually not that difficult.
 

Thread Starter

Kevil

Joined Jun 28, 2020
179
...just a bunch of multiply and accumulate functions that get executed on a timed schedule.
It's actually not that difficult.
That's what I am asking for. But not just words but real advice on how to combine coefficients and temperatures together in a real equation.
 

MrAl

Joined Jun 17, 2014
11,389
Ok thanks just wanted to make sure you are using feedback and not simply feedforward control which would not be PID.

Ok, so a direct implementation would be to integrate numerically, take the derivative numerically, and the proportional part is just a multiplication. Does that make sense to you? I could probably provide a simple example, but integration is just repeated summation, derivative is just subtraction divided by a time increment, and proportional is just a multiplication by a constant, and then you most likely sum the results of all three.

Integrate:
Err1=Ref-Measurement
Y=Ki*(Y+Err1*dt)
where dt is the clock time period (increment of time) and Y on the left is the total integration up to that point in time, Err1 is the current error measurement. Y on the right is the previous integration result often starting with zero at system startup.
Ref is the reference and Measurement is the current measurement (of the sensor).

First Derivative:
dX=Kd*(Err1-Err2)/dt
where dt is the clock time period, Err1 is current error measurement at time t2, and Err2 is the previous measurement error at time t1, and dt=t2-t1 the clock period. dX is change over time dt, the first derivative. There is a better formula however based on dx/dt=(f(x+h)-f(x-h))/(h+h) where h is the time increment.

Proportional:
P=Kp*Err1

Totat Drive Signal then is:
drive=Y+dX+P

then:
Err2=Err1
Wait time dt, then repeat from Err1=Ref-Measurement.

As mentioned, there are better formulas for derivative and also for integration although those simple ones can be used successfully with short clock periods.
 
Last edited:

Thread Starter

Kevil

Joined Jun 28, 2020
179
Perfect, thank you very much. That is exactly I was looking for. Maybe I will first test the calculations in the Excel sheet.
 

MrAl

Joined Jun 17, 2014
11,389
Perfect, thank you very much. That is exactly I was looking for. Maybe I will first test the calculations in the Excel sheet.
You're welcome, and yes testing is a good idea both in simulation and in practice taking the system through as much of a dynamic range as it will see in the real life application. Testing, Testing, and more Testing, leads to success, success, and more success :)

I like to write up a little program to test these systems. it's very interesting to see these things work.
There's also the z transform approach which can be interesting too if you are into that.
 

Thread Starter

Kevil

Joined Jun 28, 2020
179
Unfortunately the implementation is not that easy. What I'm doing wrong in the table to get correct 'Drive' values (ZCD Triac PWM Control 0-100%)?

Measured values are just test values entered by hand.
 

Attachments

MrAl

Joined Jun 17, 2014
11,389
Unfortunately the implementation is not that easy. What I'm doing wrong in the table to get correct 'Drive' values (ZCD Triac PWM Control 0-100%)?

Measured values are just test values entered by hand.
Unfortunately i can not open that file maybe you can find another format to use to post that information.

I did a controller once that could not use integration, so Ki had to be zero. Only Kp and Kd were used and that worked very well. It can be a little tricky sometimes especially since it is often used in a sampled system which can be harder to stabilize sometimes.

LATER:
I found a way to convert it to pdf but it did not come out that well. Maybe you should post in pdf format.
From what i can see it looks like the system is unstable, and that usually means the factors are not correct but it could also mean that the clock period is too long.
 
Last edited:

MrAl

Joined Jun 17, 2014
11,389
The table is the Excel sheet *.xlsx and I can open the attachment easy.

See a screenshot of the table.
Yes i told you i was able to convert it and so i was about to view it.

From the numbers it looks like it is unstable. As i said, that usually means the constants have not yet been chosen correctly or else the clock period is too long.

I tested the procedure i posted on another system and i got comparable results to a non sampled system with the same set of coefficients. Perhaps you can try lowing Ki the constant for the integration.
I would presume you have a mathematical model for your system before the PID controller has been added. Can you post that system transfer function? I can try to reproduce your results and see what i can find out.
 

Thread Starter

Kevil

Joined Jun 28, 2020
179
That's great. Here you have measured data (full power) on my Hot Plate. "T poly" is the polynomial approximation of the measured curve (coefficients are in the right top corner).

I had to add column Input, requirement for the automatic tuning of PID parameters on pidtuner.com
Click on blue PID tuner button at the top, select the data from the the three columns in table, copy them to pidtuner and let him do the job.
 

Attachments

MrAl

Joined Jun 17, 2014
11,389
That's great. Here you have measured data (full power) on my Hot Plate. "T poly" is the polynomial approximation of the measured curve (coefficients are in the right top corner).

I had to add column Input, requirement for the automatic tuning of PID parameters on pidtuner.com
Click on blue PID tuner button at the top, select the data from the the three columns in table, copy them to pidtuner and let him do the job.

Hello again,

Ok let's try this again.
First, please dont post xlsx files they are too hard for me to read.

Second and most important, please just type out the transfer function. For example:
T=t*2+7

or whatever you have there. T is temperature of the plate, lower case 't' is time.
I would be better if you did not post any more files just type that out.

Also note that a graph will not work either because there are too many ways to interpret a graph. That involves curve fitting which i would not mind doing but if my curve fit is not the same as yours then we will be working with two different transfer functions which although may be very similar will not be exactly the same and we need to have the exact same transfer functions.
 

Thread Starter

Kevil

Joined Jun 28, 2020
179
Hello again,

Ok let's try this again.
First, please dont post xlsx files they are too hard for me to read.

Second and most important, please just type out the transfer function. For example:
T=t*2+7
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.
 

Thread Starter

Kevil

Joined Jun 28, 2020
179
Here is a link to an application note on PID control.
Thanks for the link, unfortunately it won't help me much. I need advice on which equation to use (to be able to program it on MCU) to calculate the control input value (0% - 100%) for 2nd order PID process feedback on the Hot Plate data I gave here when I know all the necessary parameters: setPoint, processValue, Kp, Ti, Td, I, D and sample time = 1 second.
 
Top