This is simple to implement, in fact it is just one line of code in the following cell. Adjust Tsetpoint to a desired setpoint value, then run the cell.
from tclab import TCLab, clock, Historian# control parametersQmax =100Tsetpoint =40# time horizon and time steptfinal =300tstep =1# perform experimentwith TCLab() as a: h = Historian(a) h.initplot(tfinal)for t in clock(tfinal, tstep): T1 = a.T1 # measure temperature Q1 = Qmax if a.T1 < Tsetpoint else0# compute manipulated variable a.Q1(Q1) # adjust power h.update(t) # log results
TCLab disconnected successfully.
Relay Control with Hysteresis
One of the issues with simple relay control is the potential for ‘chattering’, which are situations where the manipulated variable (in this case heater power) rapid on-and-off switching. This can be caused by systems that are highly response to control inputs or where the sensor measurements carry significant noise.
The typical home thermostat used for furnace control incorporates a simple but highly effective solution to the chattering period. The idea is to intentially overshoot the setpoint. Then, after the control switches state, there will be at least a short period of time where no further control action should be necessary. The control algorithm can be written
where \(d\) is the tolerance or hysteresis. For home heating systems a typical value is in the range of 0.5 to 1 degree F. This image shows how hystersis was adjusted on a typical home thermostat in common usage in the late 20th century.
The furnance is turned on for temperatures below the range
and is turned for temperatures above the range. Within the range, however, the furnance may be on or off depending on what happened at the last decision point.
The following code implements relay control with hystersis.
from tclab import TCLab, clock, Historian# control parametersQmax =100Tsetpoint =50d =0.5# time horizon and time steptfinal =300tstep =1# perform experimentwith TCLab() as a: h = Historian(a) h.initplot(tfinal) Q1 = a.Q1()for t in clock(tfinal, tstep): T1 = a.T1if T1 <= Tsetpoint - d/2: Q1 = Qmaxif T1 >= Tsetpoint + d/2: Q1 =0 a.Q1(Q1) h.update()
Examining the closed-loop responses, it’s obvious that the heater is oversized for the purpose of control at 40 deg C. Try other values for \(Q^{\max}\) to see if you can improve closed-loop performance.
What is the effect of sample time on control performance? What happens if you make the controller sample time longer?
In a new cell, create a modification of the script to include a change in setpoint from 40 deg C to 50 deg C at the 300 second mark. Run the experiment for at least 10 minutes to see the full effect.
For a relay control with hystersis, try to sketch a graph of \(Q\) as a function of \(T\) assuming \(T_{Setpoint} = 50\) and \(h = 3\). Can you draw a unique function? Why not?