The classic PID controller uses floating point values in its calculations and this option is better suited for ESP_Easy (due to the peculiarities of the mathematics implemented in the firmware), but initially I wrote code for an 8-bit microcontroller, where memory is limited as well as core speed, so the implementation is such.
Another interesting solution I think is how the load management is implemented, this is the "Bresenham's" algorithm. of course, you can apply PWM control or phase-pulse control, it all depends on the task and your imagination.
Important point! The "PID" function must be called with the same time period!
All coefficients P, I, D are selected individually, on a real object where the regulator will work!
Code: Select all
On System#Boot do
let,1,27//scaling I
let,2,0//working variable "Bresenham's"
let,3,100//discreteness "Bresenham's"
let,4,5//P_gain
let,5,1//I_gain
let,6,0//D_gain
let,7,1//Gain_scaling
let,8,0//Feedback
let,9,280//Setpoint
let,10,0//PID_p
let,11,0//PID_i
let,12,0//PID_d
let,13,0//PID_px
let,14,0//PID_y
loopTimerSet,1,1//turn on the timer with a period of 1 second
endon
on Rules#Timer=1 do
let,8,[termo_1#temperature]//it's not 280, it's 28.0. But you need to write without a comma!
let,8,[var#8]*10//this is necessary in order not to lose the capacity of the temperature sensor.
let,9,[var#9#F]//removes everything after the comma
let,10,[int#9]-[int#8]//PID_p = Setpoint - Feedback
let,10,[var#10#F]
let,12,[int#10]-[int#13]//PID_d = PID_p - PID_px
let,12,[var#12#F]
let,13,[int#10]//PID_px = PID_p
let,13,[var#13#F]
let,14,[int#10]*[int#4]//PID_y = (PID_p * P_gain)
let,14,[var#14#F]
let,14,[int#14]+([int#11]*[int#5])/[int#1]//PID_y = PID_y + (PID_i * I_gain)/scaling I
let,14,[var#14#F]
let,14,[int#14]+([int#12]*[int#6])//PID_y = PID_y + (PID_d * D_gain)
let,14,[var#14#F]
//let,14,[int#14]+[int#12]//if you do not use the D component in the calculations, then you can write it like this
//let,14,[var#14#F]
let,14,[int#14]/[int#7]//PID_y = PID_y / Gain_scaling
let,14,[var#14#F]
if [int#14] > 100//control variable constraint. 100 because it is convenient to take for 100%
let,14,100
else
if [int#14] < 0//remove negative numbers
let,14,0
endif
let,11,[int#11]+[int#10]
let,11,[var#11#F]
endif
let,2,[int#2]+[int#14]//conversion of PID value value to power factor
if [int#2]>=[int#3]
let,2,[int#2]-[int#3]
gpio,0,0//active level
else
gpio,0,1//low level
endif
endon