Pid temperature controller from the computer. Frequency converter and pid controller - general setup. Tilt and move the transform option

It is necessary to keep the temperature at a given certain level and change the task. There is a microcontroller to which a temperature meter is attached, and a triac for power control. We will not warm our heads on the TAU, nor, we will just take and make a PID controller “on the forehead”.

II. Theoretical introductory

How is a PID controller obtained? We take the difference between the current temperature and the desired one, multiply by the adjustable coefficient, we get the power that needs to be output at the moment. This is a proportional component, it works at the moment of occurrence of a mismatch - that is, it instantly responds both to a change in the setpoint and to the behavior of the object. Has it started heating up? Power starts to drop. Overheated? Turned off, or even gave a cooling signal. It's all good, it's just real life the effect of exposure manifests itself with a delay, and the object is affected not only by us, but also by the environment: a heated reactor is not only hot inside, but also cools down, giving off heat to the room, and therefore as soon as we turn off the power, it immediately starts to cool down. Therefore, a pure proportional controller oscillates around the maintenance point, and the more it oscillates, the higher the influence environment/ contents of the reactor.

To compensate for "external" effects on the reactor, an integral component is added to the circuit. All the mismatch that was in the system goes to the integrator (respectively, as soon as we overheated, the amount decreases, while it is underheated, the amount increases). And the accumulated integral, with its own coefficient, gives its own increase-decrease in power. As a result of this approach, in a stationary process, after some time, the integral selects such a contribution to the sum with power that compensates for the environmental losses, and the fluctuations disappear - the integral becomes stable, so the value of the output power becomes constant. Moreover, since the desired temperature is maintained, there is no mismatch, the proportional component does not work at all.

To compensate for the influence of delays between the impact and the response of the system, a differential component is added to the system. It’s just that the proportional regulator gives power all the time until the temperature reaches the desired point, the proportional-differential one begins to reduce the supplied power before it has warmed up to the desired point - since the mismatch decreases, there is a negative derivative that reduces the impact. This minimizes overheating during large transitions.

So, we figured out the physical meaning, let's move on to the basic implementation issues.

III. Who should use the regulator?

- Technicians.

What follows from this? It follows from this that technicians understand the physical component, and have experience in setting up hardware PID controllers. This means that the software implementation should proceed from the convenience of tuning by technicians - repeating the physical model. And this is extremely important! Very often, for the sake of simplifying the code, the coefficients are changed, for example, to inverse ones - in order to get rid of the division. As a result, tuning becomes hellish and a nightmare, and experience with tuning this particular regulator is required, instead of understanding the process. From here we get that our coefficients - the constant of integration and the constant of differentiation - must have the dimension of time, that is, they must be set in seconds, and not in “1 / s”, as they like to do.

IV. Area of ​​operation.

We are trying to make a universal regulator, which means that it should work both on small fast objects and powerful large furnaces. This means that one should proceed from the fact that the controlled temperature is limited in general by the meter. The most commonly used are XA(K) and XK(L). Their area of ​​applicability is somewhere up to 1200°C. Cooling requires more complex equipment (cryostats), control of additional cooling (fans and opening doors of heating cabinets) is also rarely required - which means that we exclude it from consideration for now. We get that the controlled temperature is from ~15°C to ~1200°C, only the power supply is controlled.

The control accuracy is determined in-1x by the measurement accuracy: calibration tables are given through 0.1 degrees; the linearity inside the tables is, in principle, decent, so the accuracy is limited primarily by the amplifier and current meter. In my case, I wanted to achieve an accuracy of maintaining 0.1 degrees, so the meter is set to 1/32 degrees: this gives ~ 3 quantums per 0.1 degrees, thus, having the normal “noise” of regulation + -1 quantum, we remain within the same 0.1 degrees. Using 1/32 allows you to work with a fixed point - 5 bits = fractional part, the rest is an integer. In 16 bits it turns out to represent from 0 to 2047 °. Instead of working with negative numbers, we will work in kelvins instead of celsius, so - 0 to 2047 °K is represented, which is equivalent to -273 to 1775 °C; in steps of 0.03125°.

V. Range of customization.

To control a microreactor with a powerful power plant, it may turn out that 1% of the power is enough to heat up by 10 degrees, while for a large inert furnace, 100% of the heating power is barely enough to heat up by a degree. (In real life, it looks like this - there are several manually controlled heaters - they are turned on by a separate switch and produce initial heating, further maintaining the operating point is provided by the thermostat, controlling another heater, which at full power gives a maximum of + 10 ° C to that that heated constantly included). Based on this, it is logical to assume 100% of power per 1 degree as the limiting coefficient of proportionality. It doesn't make sense anymore since we want 0.1 degrees of steering. The minimum, for simplicity, I took inverse - 1% power per 100 degrees.

The time factor ranges are calculated simply from our regulator operating conditions. Since we control the power through the triac by calculating the turn-on delay after passing through 0, the limiting frequency of the regulator is 50Hz. If we are sure that we are controlling the power of which it does not matter plus or minus, we can work at 100Hz, but this is not always the case, and therefore it is better to dose an equal amount of both positive and negative half-wave each time. To simplify life, I have reduced the operating time to 25Hz, thus any calculated impact will be valid for 4 half-waves, and during this time I will be able to calculate a new impact.

Thus, the time constants are set in 1/25 sec, from 0 to ~ 2000 sec (2000 * 25 = 50000, just fit in 16bit).

Well, we also have a minimum and maximum power limit, from 0 to 100%.

VI. Power management.

Starting from this moment, all theoretical calculations end, bitter practice begins, tied to a specific implementation.

So, we have already decided that we control the delay in opening the triac after passing through 0. Thus, a delay of 0 means 100% power, an infinite delay = 0% power.

Question: How accurate are we? Can power control? In general, with the accuracy of counting the time of our timer. On the other hand, how much power do you need? We calculate what% of power needs to be applied for 0.04sec. In principle, from experience, power control even with an accuracy of 1% at a frequency of 0.1 sec is enough to maintain a temperature of 1 degree. We have a control of 0.04sec (2.5 times faster). Therefore, it was decided to calculate the power table after 1/250 of the maximum (with a step of 0.4%). This allows the table to be not very large (500 bytes), and at the same time have an accuracy above 1%. If your case requires more accuracy, it is not so difficult to recalculate.

Now let's talk about the calculation of this very table. Firstly, it should be taken into account that there is moment when the zero crossing signal is triggered. In my case - 12V. That is, when the input voltage drops below 12V, I will get a pass through 0 signal.

This means that for 100% power start time = 12V transit time.

Let's solve the system of equations

; IntMoment:= 12V ; Max:= sqr(220*sqrt(2)) ; ( Sqr(Sin(Pi/2)*K) = Max ; ( Sqr(Sin(X)*K) = IntMoment ; ; 2*k/MaxCode = 1 - cos(T*Pi) ; cos(T*Pi) = 1-2*k/MaxCode ;T*Pi = arccos(1-2*k/MaxCode) ;T = arccos(1-2*k/MaxCode) / Pi

My processor runs at a frequency of 32786, the PLL is set to 384/2, the half-wave has 100Hz, from which we get that the code for loading the constant into the timer for time T looks like this:

65536-(T*(32768*384/2)/100.0 + 773)

We need to calculate the delay time, giving a uniform increase in the area of ​​the included part of the sinusoid. That is, we need to have time readings that give a uniform increase in power. The total power we deliver is the integral over the entire sinusoid. [Who knows how to insert formulas on Habré? no way? I write in maple notation then].

Max = int(sqr(sin(x)), x=0..Pi) int(sqr(sin(x)), x=0..T*Pi) = x/2 - sin(2*x)/ 4+C | 0..T*PI ​​= (T*Pi)/2 - sin(2*T*Pi)/4 (T*Pi)/2 - sin(2*T*Pi)/4 = Q*Pi/2

Thus, we need to go through all Q with a given accuracy, and for each of them find T.

I solved it for myself in this stupid way:

pearl generator

#!/usr/bin/perl # (T*Pi)/2 - sin(2*T*Pi)/4 = Q*Pi/2 use constant PI => 4 * atan2(1, 1); $t = 1; for($i = 250; $i >= 0; $i--) ( $int = $i*PI/2/250; $ev = ($T*PI)/2-sin(2*$T* PI)/4; while(abs($ev-$int) > 0.0005) ( $T -= 0.0001; $ev = ($T*PI)/2-sin(2*$T*PI)/4; ) #print $i."\t".$T."\n"; $code = 65536-($T*(32768*384/2)/100.0 + 773); printf "DB 0%02Xh, 0%02Xh ; %04Xh = $i/250 of power\n", $code%256, int($code/256), $code, $i; )

That's it, at the output we got a plate of 250 values ​​corresponding to the timer load constants until the moment of ignition after receiving a signal about passing through 0 (more precisely, through 12V, as I said above).

VII. Measurement of input data

I skip this question, because it deserves a separate large article. How I solved the issue with thermal resistance can be found in the archive of my blog that died in Bose.

The main thing we need to know is that we are measuring data with the frequency we need (in this case, 25Hz), and the desired accuracy (the output is a number from 0 to 2048 degrees Kelvin in 1/32 degrees). The data are assumed already normalized for all further calculations.

If anyone is interested - write in the comments, next time I will sign how it is done for thermocouples.

VIII. Impact calculation

And so it happened: we have all the data in order to finally produce what we started everything for: calculate how much power should be applied to the control element.

Recall once again the PID controller formula:

U = K * (Err + (1/Ti)*Int + Td*dErr)

  • U- the power to be delivered;
  • K- proportional coefficient (note - it is put out of brackets, why - I will describe a little lower);
  • Ti is the integration time constant. Please note - the reciprocal is used in the calculations;
  • Td- time constant of differentiation
  • Err- current mismatch (difference between the setpoint and the measured temperature
  • dErr- mismatch derivative (difference between current and past error)
  • int- the accumulated mismatch integral (the sum of all Err "s that we saw)

We come back to the point raised in Section III: This is what the technicians will use. Therefore, it is extremely important to avoid the classic error of all implementations - "the dimensions of the coefficients as it turns out." We do device for management physical process, which means that the model must match.

Let's output all dimensions. Partially looking ahead, I have already described in, but now we will reveal in more detail:

  • U- has a value in% power. Even more precisely - in 2/5 of% of the power, since our table goes through 1/250 of 100%.
  • Err- Mismatch, given in degrees. More precisely - through 1/32 degrees.
  • int- integral, is the sum of degrees in time - and therefore has the dimension of degrees * sec. More precisely - (1/32 degrees) * (1/25 sec)
  • Ti- set after 1/25 sec
  • (1/Ti)*Int- after calculation gives a contribution having the dimension (1/32 degrees).
  • dErr- derivative, has the dimension of degrees / sec, or rather (1/32 degrees) / (1/25 sec)
  • Td- set after 1/25 sec
  • Td*dErr- after the product brings the contribution to the dimension (1/32 degrees)
  • (...) - so, all the terms under the brackets are reduced to the dimension (1/32 degrees)
  • K- agrees U and (...) , which means it has the dimension of percent-per-degree, more precisely (2/5)% / (1/32 degrees)

Now you can clearly see why the proportional coefficient is taken out of brackets - this allows you to leave the differential and int coefficients simply as time constants, as a result, when setting up, the operator operates with simple and understandable numbers - percentage per degree for proportional and seconds for integral and differential coefficients.

And thanks to the convenient selection of the position of the points and the dimensions of time, as we will now see, all calculations are made almost "on the forehead".

Except one - we have a value Ti, and the calculation requires 1/Ti. The division operation of a large capacity is very expensive. The multiplication operation is several times cheaper, so we will use the excellent article Division by Invariant Integers using Multiplication . We have K / Ti / Td change extremely rarely, and therefore we can afford to pervert with them in any way after they change, the main thing is that the main calculation cycle works quickly.

Thus, instead of Ti for calculations, we decompose into a set Ti_m, Ti_sh1, Ti_sh2; and on each cycle we perform the calculation:
T1 = MULUH(Ti_m, Int) Q = SHR(T1+SHR(Int-T1, Ti_sh1), Ti_sh2)

Now we calculate the balance of bit depth. To do this, write the full formula step by step:

  1. Eo = E ; We need an error. Errors - 16 bits
  2. E = Y-X; We calculate a new mismatch. 16bit
  3. Int = Int + (E+Eo)/2 ; We integrate the error. In this case, we consider the half-sum of the difference (difference scheme). 32bit = 32bit + 16bit
  4. cI = Int * (1/Ti) ; We consider the integral contribution - 32bit * 32bit => 32bit
  5. cD = Td * (E-Eo) ; We count the differential contribution - 16*16 => 32bit
  6. PID = E + cI + cD ; Subrack; 16+32+32 => 32bit
  7. U = K*PID/256 ; coefficient; 32*16/8bit => 40bit.

In all calculations, the position of the point up to the 7th step remains at the 5th position from the right. At the last moment there is an interesting feint with the ears. K is set through 1/256, respectively, after multiplication, the point is shifted to the left to 5 + 8 = 13 places, so we must discard the lower 8 bits from the result. And the lowest byte of the result is the power we need after 2/5%. This is another reason why the power is aligned in steps of 1/250 - this allows the result to fit into one byte and get the desired result easily from the table.

Further, remember that we are only interested in power from 0 to 250 - therefore the 7th step of the calculation is very simple, as soon as we get a negative number - we immediately add uMin. As soon as we found out that any high byte is not zero, we immediately add uMax. And only if the power is in the range - we check for less than uMin or more than uMax.

In case anyone is interested:

full footcloth calculations

; PID control CalcMainEnd: ; Calculations, Go-Go. CalcPid: ; 1. Eo = E | 16bit Pid1: MOV Err0H, ErrH MOV Err0L, ErrL ; 2.E=Y-X | 16bit Pid2: CLR C MOV A, SettingL SUBB A, ThermoL MOV ErrL, A MOV A, SettingH SUBB A, ThermoH MOV ErrH, A JNB OV, Pid2Ov JB ACC.7, Pid2Max Pid2Min: MOV ErrL, #LOW(-500* 32) MOV ErrH, #HIGH(-500*32) SJMP Pid2End Pid2Max: MOV ErrL, #LOW(500*32) MOV ErrH, #HIGH(500*32) SJMP Pid2End Pid2Ov: JNB ACC.7, Pid2OvP Pid2OvN: ; Check for a downward constraint CLR C MOV A, ErrL SUBB A, #LOW(-500*32) MOV A, ErrH SUBB A, #HIGH(-500*32) JNC Pid2End ; If > -500 => OK SJMP Pid2Min Pid2OvP: CLR C MOV A, ErrL SUBB A, #LOW(500*32) MOV A, ErrH SUBB A, #HIGH(500*32) JNC Pid2Max ; If< 500 => everything is ok Pid2End: ; 3. Int = Int + (E+Eo)/2 | 32bit+16bit Pid3: JNB PowerReady, Pid3End ; If there is no network, we do not save the integral part MOV A, ErrL ADD A, Err0L MOV R0, A ; temporarily MOV A, ErrH ADDC A, Err0H MOV C, ACC.7 ; The half sum always fits into 16 bits, so the sign of RRC A must be preserved when shifting; Divide without loss of sign XCH A, R0 ; A= low part, R0 - high part of half-sum RRC A ; Added JNB IntS, Pid3IntPos ; Int is negative, change the sign for R0:A, so you can simply add to Int CLR C CPL A ADD A, #1 XCH A, R0 CPL A ADDC A, #0 XCH A, R0 Pid3IntPos: ; Int and R0:A now have signed signs, so we add in the usual way ADD A, IntLL MOV IntLL, A MOV A, IntLH ADDC A, R0 MOV IntLH, A MOV A, R0 JB ACC.7, Pid3Neg ; Did you add a negative difference? ; If the difference is positive, just propagate the JNC jPid3End carry; If a word was added and there was no transfer, we do not need to do anything. INC IntHL ; Extend carry above MOV A, IntHL JNZ Pid3End ; If the transfer did not go to the 4th byte, everything is fine INC IntHH ; Extend carry to MOST high byte MOV A, IntHH JNZ Pid3End ; If the carry hasn't gone even higher, everything is fine MOV IntHH, #0FFh ; If the transfer was higher - limit the integral to the ceiling MOV IntHL, #0FFh MOV IntLH, #0FFh MOV IntLL, #0FFh jPid3End: SJMP Pid3End Pid3Neg: ; If the difference is negative, then continue adding both times, but FFh MOV A, IntHL ADDC A, #0FFh MOV IntHL, A MOV A, IntHH ADDC A, #0FFh MOV IntHH, A JC Pid3End ; If there was a carry, then the sign of the integral has not changed CPL IntS ; If there was no transfer, then the sign of the integral has changed CPL C ; Reverse the sign of the resulting number MOV A, #0 SUBB A, IntLL MOV IntLL, A MOV A, #0 SUBB A, IntLH MOV IntLH, A MOV A, #0 SUBB A, IntHL MOV IntHL, A MOV A, #0 SUBB A , IntHH MOV IntHH, A ; since it has become negative, then the transfer will always be here Pid3End: ; 5. cI = Int*(1/Ti) | 32*32=>32bit Pid5: ; R3:R2:R1:R0 = Int*(1/Ti) JB Ti_sh1, Pid5Calc ; if Ti_sh1=0, then 1/Ti=1 or Ti=0. and do nothing MOV A, Ti_mLL ORL A, Ti_mLH ORL A, Ti_mHL ORL A, Ti_mHH JZ Pid5Zero MOV R0, IntLL MOV R1, IntLH MOV R2, IntHL MOV R3, IntHH AJMP Pid5End Pid5Zero: MOV A, #0 MOV R0 , A MOV R1, A MOV R2, A MOV R3, A MOV IntLL, A MOV IntLH, A MOV IntHL, A MOV IntHH, A AJMP Pid5End Pid5Calc: ; R7:R6:R5:R4[:R3] = MULUH(Int*Ti_m) // Count R3 as rounding part MOV R2, #0 ;; R7:R6 = IntHH*Ti_mHH MOV A, IntHH MOV B, Ti_mHH MUL AB MOV R7, B MOV R6, A ; R6:R5 += IntHL*Ti_mHH MOV A, IntHL MOV B, Ti_mHH MUL AB MOV R5, A MOV A, R6 ADD A, B MOV R6, A MOV A, R2 ; A=0 ADDC A, R7 MOV R7, A ; R5:R4 += IntLH*Ti_mHH MOV A, IntLH MOV B, Ti_mHH MUL AB MOV R4, A MOV A, R5 ADD A, B MOV R5, A MOV A, R2 ; A=0 ADDC A, R6 MOV R6, A MOV A, R2 ; A=0 ADDC A, R7 MOV R7, A ; R4:R3 += IntLL*Ti_mHH MOV A, IntLL MOV B, Ti_mHH MUL AB MOV R3, A MOV A, R4 ADD A, B MOV R4, A MOV A, R2 ; A=0 ADDC A, R5 MOV R5, A MOV A, R2 ; A=0 ADDC A, R6 MOV R6, A MOV A, R2 ; A=0 ADDC A, R7 MOV R7, A ;; R6:R5 += IntHH*Ti_mHL MOV A, IntHH MOV B, Ti_mHL MUL AB ADD A, R5 MOV R5, A MOV A, R6 ADDC A, B MOV R6, A MOV A, R2 ; A=0 ADDC A, R7 MOV R7, A ; R5:R4 += IntHL*Ti_mHL MOV A, IntHL MOV B, Ti_mHL MUL AB ADD A, R4 MOV R4, A MOV A, R5 ADDC A, B MOV R5, A MOV A, R2 ; A=0 ADDC A, R6 MOV R6, A MOV A, R2 ; A=0 ADDC A, R7 MOV R7, A ; R4:R3 += IntLH*Ti_mHL MOV A, IntLH MOV B, Ti_mHL MUL AB MOV A, R3 MOV R3, A MOV A, R4 ADDC A, B MOV R4, A MOV A, R2 ; A=0 ADDC A, R5 MOV R5, A MOV A, R2 ; A=0 ADDC A, R6 MOV R6, A MOV A, R2 ; A=0 ADDC A, R7 MOV R7, A ;; R5:R4 += IntHH*Ti_mLH MOV A, IntHH MOV B, Ti_mLH MUL AB ADD A, R4 MOV R4, A MOV A, R5 ADDC A, B MOV R5, A MOV A, R2 ; A=0 ADDC A, R6 MOV R6, A MOV A, R2 ; A=0 ADDC A, R7 MOV R7, A ; R4:R3 += IntHL*Ti_mLH MOV A, IntHL MOV B, Ti_mLH MUL AB ADD A, R3 MOV R3, A MOV A, R4 ADDC A, B MOV R4, A MOV A, R2 ; A=0 ADDC A, R5 MOV R5, A MOV A, R2 ; A=0 ADDC A, R6 MOV R6, A MOV A, R2 ; A=0 ADDC A, R7 MOV R7, A ;; R4:R3 += IntHH*Ti_mLL MOV A, IntHH MOV B, Ti_mLL MUL AB ADD A, R3 MOV R3, A MOV A, R4 ADDC A, B MOV R4, A MOV A, R2 ; A=0 ADDC A, R5 MOV R5, A MOV A, R2 ; A=0 ADDC A, R6 MOV R6, A MOV A, R2 ; A=0 ADDC A, R7 MOV R7, A ;;; If R3 > 7F -- MOV A, R3 JNB ACC. 7, Pid5Shift ; If R3<80 -- округление не надо ANL A, #7Fh JZ Pid5Round ; Если = 80 -- округляем до нечетного MOV A, #1 ADD A, R4 MOV R4, A MOV A, R2 ; A=0 ADDC A, R5 MOV R5, A MOV A, R2 ; A=0 ADDC A, R6 MOV R6, A MOV A, R2 ; A=0 ADDC A, R7 MOV R7, A SJMP Pid5Shift Pid5Round: MOV A, R4 ORL A, #01h MOV R4, A ;JMP Pid5Shift Pid5Shift: ; R3:R2:R1:R0 = (Int-R7:R6:R5:R4) > > 1 CLR C MOV A, IntLL SUBB A, R4 MOV R0, A MOV A, IntLH SUBB A, R5 MOV R1, A MOV A, IntHL SUBB A, R6 MOV R2, A MOV A, IntHH SUBB A, R7 RRC A ; >>1 without loss of carry MOV R3, A MOV A, R2 RRC A MOV R2, A MOV A, R1 RRC A MOV R1, A MOV A, R0 RRC A ;MOV R0, A ; R3:R2:R1:R0 += R7:R6:R5:R4 ;MOV A, R0 ADD A, R4 MOV R0, A MOV A, R1 ADDC A, R5 MOV R1, A MOV A, R2 ADDC A, R6 MOV R2, A MOV A, R3 ADDC A, R7 MOV R7, A ; Now shift right to sh2. ; sh2 can be up to 16 (because we have a 16-bit Ti; check if a 16-bit shift is necessary) MOV A, Ti_sh2 JNB ACC.4, Pid5ShiftUnder16 ; We need to shift >=16 -- 2 bytes will be shifted with mov "s MOV R0, 18h+2; R2, bank 3 MOV R1, 18h+3; R3, bank 3 MOV R2, #0 MOV R3, #0 Pid5ShiftUnder16: JNB ACC. 3, Pid5ShiftUnder8 ; It is necessary to shift by >=8 -- 1 byte is shifted with mov "ami MOV R0, 18h+1; R1, bank 3 MOV R1, 18h+2; R2, bank 3 MOV R2, 18h+3; R3, bank 3 MOV R3, #0 Pid5ShiftUnder8: ANL A, #07h JZ Pid5End ; If you don't need to move inside a byte, everything MOV R4, A SJMP Pid5ShiftRight Pid5NextShift: CLR C ; At this point, C still has a possibly significant high bit! Pid5ShiftRight: MOV A, R3 RRC A MOV R3, A MOV A, R2 RRC A MOV R2, A MOV A, R1 RRC A MOV R1, A MOV A, R0 RRC A MOV R0, A DJNZ R4, Pid5NextShift ; Everything, after all the shifts, got the result; Do not forget that the calculated in R3:R2:R1:R0 ; now the number is positive, and its sign is in IntS Pid5End: ; 4. PID += [ cD = Td * (E-Eo) ] | 16*16=>32bit Pid4: ; cD = R7:R6:R5:R4; ErrD = E-Eo CLR C MOV A, ErrL SUBB A, Err0L MOV DiffL, A MOV A, ErrH SUBB A, Err0H MOV DiffH, A MOV C, ACC.7 ; Take the sign of the result MOV DiffS, C ; Keep sign E-Eo JNC Pid4Mul ; Diff -- negative, invert sign MOV A, DiffL CPL A ADD A, #1 MOV DiffL, A MOV A, DiffH CPL A ADDC A, #0 MOV DiffH, A Pid4Mul: ; R7:R6 = DiffH*TdH ; MOV A, DiffH = anyway A=DiffH MOV B, TdH MUL AB MOV R6, A MOV R7, B ; R5:R4 = DiffL*TdL MOV A, DiffL MOV B, TdL MUL AB MOV R4, A MOV R5, B ; R6:R5 += DiffH*TdL MOV A, DiffH MOV B, TdL MUL AB ADD A, R5 MOV R5, A MOV A, R6 ADD A, B MOV R6, A MOV A, R7 ADDC A, #0 MOV R7, A ; R6:R5 += DiffL*TdH MOV A, DiffL MOV B, TdH MUL AB ADD A, R5 MOV R5, A MOV A, R6 ADD A, B MOV R6, A MOV A, R7 ADDC A, #0 MOV R7, A ; 6. PID = E + cI + cD | 32bit Pid6: ; R3:R2:R1:R0 is equal to cI, sign in IntS; ; R7:R6:R5:R4 = cD; sign in DiffS ; E in inverse two's complement JB IntS, ChkDiffN JNB DiffS, Pid6Add ; Int>0, Diff>0 => Add SJMP Pid6Sub ; Int>0, Diff<0 =>Sub ChkDiffN: JNB DiffS, Pid6Sub ; int<0, Diff>0 => sub ; int<0, Diff<0 =>Add Pid6Add: ; Same sign => add them with overflow check MOV A, R0 ADD A, R4 MOV R0, A MOV A, R1 ADDC A, R5 MOV R1, A MOV A, R2 ADDC A, R6 MOV R2, A MOV A, R3 ADDC A, R7 MOV R3, A JNC Pid6Err ; If there is no carry, there was no overflow as a result of addition MOV R3, #0FFh MOV R2, #0FFh MOV R1, #0FFh MOV R0, #0FFh SJMP Pid6Err Pid6Sub: ; Signs are different -- subtract one from the other and check the sign of the result CLR C MOV A, R4 SUBB A, R0 MOV R0, A MOV A, R5 SUBB A, R1 MOV R1, A MOV A, R6 SUBB A, R2 MOV R2, A MOV A, R7 SUBB A, R3 MOV R3, A JNC Pid6Err ; If there is no borrow -- the sign of the result is the sign of DiffS CPL DiffS ; If there was a borrow, DiffS and the result must be reversed MOV R6, #0 ; R6=0 MOV A, R0 CPL A ADDC A, R6 ; R6=0, C=1 => action +1 MOV R0, A MOV A, R1 CPL A ADDC A, R6 ; +transfer MOV R1, A MOV A, R2 CPL A ADDC A, R6 MOV R2, A MOV A, R3 CPL A ADDC A, R6 MOV R3, A Pid6Err: MOV R6, #0 ; R6=0 ; In R3:R2:R1:R0 -- lies cI+cD; sum sign in DiffS ; it is necessary to add/subtract Err written in the reverse code; Let's convert the sign of Err to DiffS MOV R4, ErrL MOV A, ErrH JB ACC.7, Pid6ChkDiffS JNB DiffS, Pid6SumErrNoInv ; Err>0, Diff>0 => NoInv SJMP Pid6SumErrInv Pid6ChkDiffS: JNB DiffS, Pid6SumErrNoInv ; Err<0, Diff>0 => NoInv Pid6SumErrInv: ; Err has a different sign than DiffS -- invert SETB C ; Unsure of status C MOV A, ErrL CPL A ADDC A, R6 ; A+=R6+C, R6=0 C=1 => A+=1 MOV R4, A ; R4=ErrL MOV A, ErrH CPL A ADDC A, R6 Pid6SumErrNoInv: MOV R5, A ; ErrH Pid6SumErr: ; So, R5:R4 contains Err, the sign of which is consistent with DiffS; but in back complement MOV A, R0 ADD A, R4 MOV R0, A MOV A, R5 CLR F0 JNB ACC.7, Pid6SubErrPos SETB F0 MOV R6, #0FFh ; Add negative => add FFs Pid6SubErrPos: ADDC A, R1 MOV R1, A MOV A, R2 ADDC A, R6 ; + extension MOV R2, A MOV A, R3 ADDC A, R6 ; +extension MOV R3, A MOV R6, #0 ; It is necessary to check whether there is a sign change of the total sum JNC Pid6ChkF0 JB F0, Pid7 ; Err<0, был перенос =>Sign not changed, no overflow SJMP Pid6SumOv ; Err>0, there was a transfer => overflow Pid6ChkF0: JNB F0, Pid7 ; Err>0, no transfer => no overflow; SJMP Pid6SumUf ; Err<0, небыло переноса =>sign changed Pid6SumUf: ; If Err<0 и небыло переноса =>sign changed CPL DiffS MOV A, R0 CPL A ADD A, #1 ; C=?, so we add 1 in the usual way MOV R0, A MOV A, R1 CPL A ADDC A, R6 MOV R1, A MOV A, R2 CPL A ADDC A, R6 MOV R2, A MOV A, R3 CPL A ADDC A, R6 MOV R3, A SJMP Pid7 ; The sign of the result and DiffS are normalized Pid6SumOv: ; There was an overflow => round up to the maximum MOV R0, #0FFh MOV R1, #0FFh MOV R2, #0FFh MOV R3, #0FFh ; 7.U=K*PID/256 | 32bit*16bit/8bit => 40bit, ; | which are truncated to 10bit ; | when calculating Pid7: ; R3:R2:R1:R0 contains the PID result, DiffS has its sign; We need to calculate K*PID/256, limiting the result to 10 bits; K is always positive, so if PID< 0 =>minimum JB DiffS, Pid7Umin ; since we can hard limit the top to 16 bits, ; then if R3 != ​​0 => set the maximum anyway MOV A, R3 JNZ Pid7Umax ; = ; calculate, taking into account that it should turn out R7=0 R6=0, ; otherwise, overflow, so we don't touch R7 and R6 at all; but check the result; R7:R6 = R2*KH MOV A, R2 JZ Pid7S1 MOV A, KH JNZ Pid7Umax ; If R2!=0 and KH!=0 => R7:R6>0 => overflow Pid7S1: ; R6:R5 = R2*KL MOV A, R2 MOV B, KL MUL AB MOV R5, A MOV A, B JNZ Pid7Umax ; If R6 > 0 => overflow; R6:R5 = R1*KH MOV A, R1 MOV B, KH MUL AB ADD A, R5 JC Pid7Umax ; If R6 > 0 => overflow MOV R5, A MOV A, B JNZ Pid7Umax ; If R6 > 0 => overflow; R5:R4 = R0*KH MOV A, R0 MOV B, KH MUL AB MOV R4, A MOV A, R5 ADD A, B JC Pid7Umax ; If R6 > 0 => overflow MOV R5, A ; R5:R4 = R1*KL MOV A, R1 MOV B, KL MUL AB ADD A, R4 MOV R4, A MOV A, R5 ADDC A, B JC Pid7Umax ; If R6 > 0 => overflow MOV R5, A ; R4:R3 = R0*KL MOV A, R0 MOV B, KL MUL AB RLC A ; C = R3>=0x80, Z=R3>0x80 MOV R3, #0FFh ; R3<>0x80 => nothing JNZ Pid7S2 MOV R3, #0FEh ; R3==0x80 => round to even Pid7S2: MOV A, R4 ADDC A, B ; Add multiplication, case, and carry-round ANL A, R3 ; And also if the rounding is even - we discard the least significant bit after MOV R4, A MOV A, R5 ADDC A, R6 ; We have had R6=0 for a long time, even though we did not add anything there during the multiplication JC Pid7Umax ; If R6 > 0 => overflow MOV R5, A ; R5:R4 => 16 bit limited result; Now we need to limit R5:R4 to Umax/Umin MOV A, UmaxL SUBB A, R4 ; C=0 currently MOV A, UmaxH SUBB A, R5 JC Pid7Umax ; If R5:R4>Umax => R5:R4 = Umax MOV A, UminL SUBB A, R4 ; C=0 currently MOV A, UminH SUBB A, R5 JNC Pid7Umin ; If R5:R4 R5:R4 = Umin ; Power calculated MOV UH, R5 MOV UL, R4 SETB UReady AJMP CalcExit Pid7Umax: ; Install maximum power MOV UH, UmaxH MOV UL, UmaxL SETB UReady AJMP CalcExit Pid7Umin: ; Set minimum power MOV UH, UminH MOV UL, UminL SETB UReady AJMP CalcExit

IX. Application of influence.

So, we have a calculated impact, and our task is to apply it. To do this, a common cycle of operation with a frequency of 50 Hz works. On an even cycle, measurement and calculation are performed, on an odd cycle, the impact is applied. In this way, general scheme it turns out: the power is set, measurement and calculation are performed through one sinusoid, and a new one is applied through another one.

X. Pitfalls.

Compared to the difference scheme, the direct scheme has very few pitfalls, here is a list of those that I saw:
  • Dimensional Accounting. The most important and most common mistake. You can't just take U=K*(Err+Ki*Int+Kd*Diff), without stipulating WHAT is K, Ki, Kd. And with what precision. It is especially important for the coefficient Ki, which has the dimension of the reciprocal of time - if the operation is in integers, you CANNOT simply multiply by it - since there must be a DIVISION, and the reciprocal number in integers is not representable.
  • Sign accounting. The second very important thing is to take into account the sign. All operations must be signed, the integral must accumulate signed - since it not only replaces the proportional component, but also allows you to resist external influences, for example, the release of heat from the mixture itself; and then its sign is negative.
  • Overflow accounting. It is important for us to get either the power from 0% to 100%, or the fact that the calculated power is greater than 100% or less than 0%. There is no need to do all the calculations if we got a negative parenthesis result, for example. But at the same time, it is important to take into account that an overflow can occur during the product-addition - and it must be taken into account as “more than 100%”, and in no way should the result be left after the overflow. This is fraught primarily with the lack of regulation when required - the object is below the required temperature, and power is not supplied
  • Calculation Time Accounting. The need for high-digit multiplications (with an implementation curve - also divisions) takes time, so it is extremely important to calculate the execution time of the worst version of calculations, and it should be less than the free time between measurements. Failure to comply with this condition leads to an unmanaged object, which “seems to work, but somehow doesn’t

XI. Conclusions.

As a result, the direct implementation scheme does not have the same problems as , but is more computationally intensive. However, when implemented correctly, the direct circuit is quite applicable even on cheap 8-bit microcontrollers, and gives more predictable results.

What is a PID controller? First of all, this is an algorithm that can be implemented both in software and in hardware. Today we will consider the PID controller as a complete device that can be used to build control and automation systems. As an example, let's take the device of the company "OWEN" TPM210. But first, a little theory...

What is a PID controller?

PID controller refers to. The abbreviation "PID" stands for "proportional-integral-derivative" (regulator) - these three words fully describe the principle of its operation. The general structural diagram of control looks like this:

The physical value measured by the sensor (temperature, humidity, etc.) is fed to the input of the controller, the controller, in accordance with its algorithm (implementing the conversion function), issues a control action. This causes a change in the controlled variable (eg temperature or humidity). At the next step, the controller again measures the controlled parameter and compares this value with the set value, calculating the control error. A new control action is formed taking into account the control error at each step. The value of the value to be maintained is set by the user.

The PID controller conversion function looks like this:

Where E is the control error (difference between the set value of the controlled variable and the actual value)

In this formula, as you may have guessed, there are three components: integral proportional and differential. Each of them has a corresponding coefficient (Kp, Ki, Kd). The larger the coefficient, the greater the contribution this component makes to the operation of the regulator. Now let's see what each of them is responsible for.

Proportional: "The more - the more, the less - the less"

Everything is simple here. The proportional term simply multiplies the error value by its factor. For example, the higher the set temperature compared to the current one, the more power the regulator will set on the heater.

Integral: "Let's take into account previous experience"

The integral component is necessary to take into account the previous experience of the controller and make the control more and more accurate over time. As you know, an integral is a sum. The controller sums up all previous values ​​of the control error and makes a correction for them. As soon as the system reaches the set mode (for example, it reaches the set temperature), the control error will be close to zero and the integral part will have less and less effect on the controller operation over time. talking plain language, the integral component strives to correct the control errors for the previous period.

Differential: "Take into account the rate of change"

This component takes the derivative of the measured value. physical meaning derivative is the rate of change of a physical quantity. For example, the faster the temperature in the system rises (or falls), the larger the corresponding derivative will be. The differential component allows the regulator to respond differently to sharp and smooth changes in the controlled value in the system, thereby avoiding "swinging" of this value.

TRM210: Functional diagram of the device

A brief excursion into the theory is over, let's return to practice and consider the TPM210 device that implements this algorithm.

Here is its functional diagram:

Information from the sensor is converted by the device using a scaling scale, filtered and corrected. This is necessary so that the PID algorithm receives the measured value in a convenient and understandable form for it.

The value of the measured value is displayed on the instrument display.

The control action of the regulator can be pulsed or analog. In the first case, the control action of the controller is to change the width of the pulses generated at the output. In the second case, the controller generates a unified voltage signal in the range of 0...10 V or current in the range of 4...20 mA. With these signals, you can control almost any device.

The TPM210 has an alarm block that reports on the output of a physical quantity beyond the specified limits by closing a discrete output, which, for example, can “light up” the “Emergency” lamp.

The device also has a recorder block that can transmit the measured value of a physical quantity to any other device or device using a 4 ... 20 mA current signal.

In addition to all of the above, the regulator has an RS-485 interface "on board", which allows reading from the device the values ​​of the measured value, the output power of the regulator and any configurable parameters. This can be useful if you need to transfer information about the operation of the device to the control room.

Usage example

Suppose you need to implement ventilation of the room as follows: the higher the temperature inside, the more you need to open the window. To do this, install a drive on the window, which will smoothly rotate to a given angle, and will be controlled by a current signal of 4 ... 20 mA (almost all such drives support such a control signal). That is, if a 4 mA signal is applied to the drive, it will completely close the window, and 20 mA will completely open it.

To measure the temperature, you can take any of the supported TPM210 - these are almost any thermocouples and any sensors with unified outputs of 0 ... 10 V and 4 ... 20 mA.

PID controller tuning

The TRM210 has an auto-tuning function. In this mode, the regulator itself simulates disturbing influences, monitors the system response and, based on these data, adjusts its coefficients. However, it is far from always possible to tune the controller in this way, since the controller does not know anything about the real system, and the test disturbances generated by it may not coincide with the real disturbances that arise in this system. In such cases, it is necessary to select the coefficients manually. We will tell you how to do it right in.

Goodbye! Read LAZY SMART.

1. Put the regulator in manual mode, wait until the process stabilizes and make a single change in the output signal (output to the valve) X, which will provide an acceptable response of the process variable Y (figure 1).
2. After receiving the response, return to the original value of the output parameter of the regulator signal. The process variable should also return to its original value. If the difference is significant, retry the response.
3. Determine the process gain (Kp=Y/X), the delay time d, and the time constant T by averaging the values ​​of the upper and lower responses.
4. Calculate the PID controller tuning coefficients according to the formulas presented in Table 1.
5. For more stable operation of the regulator, it may be necessary to increase the time constant closed circuit(E).

Figure 1. Process response to step action.


Table 1. Formulas for calculating coefficients for PID controllers

Where: X is the value of the change in the output action (in %);
Y is the value of the process variable change (in % of the scale);
Kp is the process amplification factor;
d is the delay time of the process reaction (in minutes);
T is the time constant of the process (in minutes);
E is the specified closed loop time constant (minutes). The minimum possible time during which the adjustable controller can bring the value of the process variable to the target.

Kp=Y/X
E=T+d

For more stable operation of the regulator, the value of E must be increased.
When tuning cascade controllers, first adjust the slave controller, then the master controller. Moreover, the time constant E of the master controller must be at least 5 times greater than the time constant E of the slave controller.

Tuning the PID controller using the maximum gain method (Method #2).

1. Switch the regulator to manual operation when the process is sufficiently stable and no sudden deviations from the setpoint are expected on the installation. Set Td (controller derivative constant) and K (controller proportional gain) to zero and Ti (controller integration constant) to the maximum value.
2. Remember the original position of the valve in case you need to return to it during the setup process. Switch the regulator to automatic mode.
3. Gradually increase the value of the proportional gain until oscillation begins. It is necessary to ensure that the oscillations are with a constant amplitude. If the oscillations are with increasing amplitude, then the proportional coefficient must be reduced. In case of strong buildup, it is necessary to switch the regulator to manual mode, set the value of the output to the valve that was memorized in step 2, reduce the proportional coefficient and try again. When uniform oscillations are obtained, measure the period of oscillation tc (time to work out one complete cycle) (see figure 2). The resulting proportional gain will be the maximum for this control system (Kmax).
4. Based on the obtained tc and Kmax, calculate the PID controller tuning coefficients using the formulas presented in Table 2.

Figure 2. Determination of the oscillation period


Table 2. Formulas for calculating coefficients for PID controllers

General rules for adjusting regulators:
The regulator will not operate properly if the valve is almost fully closed or almost fully open.
You need to adjust the regulator in that zone of the scale, presumably in which it will work.
Derivative action should not be used for flow controllers.
Do not use small values ​​of the integral component in the level controls.
Do not use the effect of the differential component in the level controls.
After adjusting the controller, it is necessary to check its stability by changing the reference value by a significant value. If buildup is observed, then it is necessary to reduce the gain.
It should be remembered that safe for the stability of the regulator are big values Ti and small values ​​of Td.
With noisy measurement results, the use of the differential component is usually not possible. Never set the derivative term to be larger than the integral term.
When setting up cascade controllers, the integration time of the master controller must be 4 times longer than the integration time of the slave controller.
Note: After adjusting the regulator according to method 1 or method 2, for more accurate operation of the regulator, you can adjust its coefficients based on Figure 3.

Figure 3. Graphs to refine the regulator settings.

WITH additional materials on setting up PID controllers, you can familiarize yourself with.

You can learn more about regulators and algorithms for the operation of regulators.

To consolidate the acquired knowledge, we suggest you use the program for simulating control loops

PID (from the English P-proportional, I-integral, D-derivative) - a controller is a device used in control loops equipped with a link feedback. These regulators are used to generate a control signal in automatic systems where it is necessary to achieve high requirements for the quality and accuracy of transients.

The control signal of the PID controller is obtained by adding three components: the first is proportional to the magnitude of the error signal, the second - to the integral of the error signal, the third - to its derivative. If any of these three components is not included in the addition process, then the controller will no longer be a PID, but simply proportional, proportional-derivative, or proportional-integral.

The first component is proportional

The output signal gives a proportional component. This signal leads to counteracting the current deviation of the input variable to be controlled from the set value. The greater the deviation, the greater the signal. When the input value is equal to the set value, the output signal becomes zero.

If you leave only this proportional component, and use only it, then the value of the quantity to be regulated will never stabilize at the correct value. There is always a static error equal to such a value of the deviation of the controlled variable that the output signal stabilizes at this value.

For example, a thermostat controls the power of a heating device. The output signal decreases as the desired object temperature approaches and the control signal stabilizes the power at the heat loss level. As a result, the temperature will not reach the set value, because the heating device will simply have to be turned off and will start to cool down (the power is zero).

More gain between the input and output - less static error, but if the gain (in fact, the proportionality factor) is too large, then, provided there are delays in the system (and they are often inevitable), self-oscillations will soon begin in it, and if you increase the coefficient is even greater - the system will simply lose stability.

Or an example of positioning a motor with a gearbox. With a small coefficient, the desired position of the working body is reached too slowly. Increase the coefficient - the reaction will be faster. But if you increase the coefficient further, then the motor will “overshoot” the correct position, and the system will not quickly move to the required position, as one would like to expect. If we now increase the proportionality coefficient further, then oscillations will begin around the desired point - the result will not be achieved again ...

The second component is integrating

The time integral of the mismatch value is the main part of the integrating component. It is proportional to this integral. The integrating component is used precisely to eliminate the static error, since the controller takes into account the static error over time.

In the absence of external disturbances, after some time the quantity to be controlled will stabilize at the correct value, when the proportional component will be equal to zero, and the accuracy of the output will be entirely ensured by the integrating component. But the integrating component can also generate oscillations around the positioning point if the coefficient is not chosen correctly.

The third component is differentiating

The rate of change in the deviation of the value to be controlled is proportional to the third - differentiating component. It is necessary in order to counteract deviations (caused by external influences or delays) from the correct position predicted in the future.

As you already understood, PID controllers are used to maintain the set value x0 of some one value, by changing the value u of another value. Is there a setting or set value x0, and there is a difference or discrepancy (mismatch) e = x0-x. If the system is linear and stationary (in practice, this is hardly possible), then the following formulas are valid for specifying u:

In this formula, you see the coefficients of proportionality for each of the three terms.

In practice, PID controllers use a different formula for tuning, where the gain is applied to all components at once:

The practical side of PID control

Practically, the theoretical analysis of PID-controlled systems is rarely used. The difficulty lies in the fact that the characteristics of the control object are unknown, and the system is almost always non-stationary and non-linear.

Really working PID controllers always have a limitation of the operating range from above and below, this fundamentally explains their non-linearity. The adjustment is therefore almost always and everywhere carried out experimentally when the control object is connected to the control system.

The use of the value generated by the software control algorithm has a number of specific nuances. If we are talking, for example, about temperature control, then often not one, but two devices at once are required: the first controls heating, the second controls cooling. The first supplies a heated coolant, the second - a refrigerant. Three options for practical solutions can be considered.

The first one is close to the theoretical description when the output is an analog and continuous value. The second is an output in the form of a set of pulses, for example, to control a stepper motor. The third - when the output from the regulator serves to set the pulse width.

Today, almost all automation systems are built, and PID controllers are special modules added to the control controller or generally implemented programmatically by loading libraries. To properly adjust the gains in such controllers, their developers provide special software.

Andrey Povny

Features of P, PI and PID control

The presence in the devices of the function of the PID control output device implies the possibility of implementing three types of control: P-, PI- and PID control.

P regulation. The output power is directly proportional to the control error. The larger the proportionality factor, the lower the output power for the same control error. Proportional control can be recommended for low-inertia systems with a large transmission coefficient. To adjust the proportional controller, you must first set the proportional coefficient to the maximum, while the output power of the controller will decrease to zero. After the measured value has stabilized, set the setpoint and gradually reduce the proportional gain, while the control error will decrease. When periodic oscillations occur in the system, the proportionality factor should be increased so that the control error is minimal, and periodic oscillations are reduced as much as possible.

PI regulation. The output power is equal to the sum of the proportional and integral components. The larger the proportionality factor, the lower the output power with the same control error, the larger the integration time constant, the slower the integral component accumulates. PI control ensures zero control error and is insensitive to measuring channel noise. The disadvantage of PI regulation is the slow response to disturbances. To adjust the PI controller, you must first set the integration time constant to zero, and the proportionality factor to the maximum. Then, as when setting up a proportional controller, by reducing the proportionality coefficient, it is necessary to achieve the appearance of undamped oscillations in the system. The value of the proportionality coefficient close to the optimal value will be twice that at which the oscillations occurred, and the value of the integration time constant close to the optimal value will be 20% less than the oscillation period.

PID regulation. The output power is equal to the sum of three components: proportional, integral and differential. The larger the proportionality coefficient, the lower the output power with the same control error, the larger the integration time constant, the slower the integral component accumulates, the larger the differentiation time constant, the stronger the system response to the disturbance. The PID controller is used in inertial systems with a relatively low noise level of the measuring channel. The advantage of the PID controller is a quick exit to the mode, accurate retention of the set temperature and a quick response to disturbing influences. Manual PID tuning is extremely difficult, so it is recommended to use the auto-tuning function.

Auto-tuning of PID control in PJSC “TERA” devices:

The main thing that determines the quality of a PID controller is its ability to accurately and quickly reach a given temperature, for which all modern PID controllers have an auto-tuning function. There are no standard PID auto-tuning algorithms; in practice, each manufacturer uses its own algorithm. Therefore, the user, purchasing the same product called “PID controller” from different manufacturers, can get completely different results of their application at his facility. The main advantages of the auto-tuning algorithm in the PID controllers of PJSC “TERA” are:

  • auto-tuning and control output without overshoot (for standard PID controllers, overshoot can reach 50-70% of the set temperature, which is technologically undesirable or even prohibited on some control objects)
  • the duration of auto-tuning is on average 2 times shorter than that of other manufacturers (an extremely important characteristic for control objects with frequently changing properties, especially for inertial objects)

Auto-tuning can be performed at any stable state of the regulated object. In addition, the greater the difference between the initial and set temperature, the more accurately the PID controller coefficients are determined. All PID coefficients are stored in the instrument's non-volatile memory.

Autotune must be repeated if:

  • the power of the actuator has changed
  • the physical properties of the regulated object have changed (mass, capacity, heat transfer, etc.)
  • the object of regulation is replaced by another non-identical
  • with a significant change in the set temperature