class PI:
def __init__(self, kp=1, ki=0, MV=0):
self.kp = kp
self.ki = ki
self.e_prev = 0
self.MV = MV
def update(self, SP, PV):
= SP - PV
e self.e = e
self.MV += self.kp * (e - self.e_prev) + self.ki * e
self.MV = max(0, min(100, self.MV))
self.e_prev = e
return self.MV
Lab Assignment 4: PI Control
Implementation of a simple PI controller
Given a process variable \(PV\) and setpoint \(SP\), proportional-integral-derivative control determines the value of a manipulated variable MV by the equation
\[\begin{align} MV & = \bar{MV} + k_p\left(SP - PV\right) + k_i \int_0^t \left(SP-PV)\right)dt \end{align}\]
where \(k_p\) and \(k_i\) are the proportional and integral coefficients, respectively. The value \(\bar{MV}\) is a nominal or initial value of the manipulated variable.
The actual implementation of PI control is normally done by computing how much the \(MV\) should change at each time step. Defining the error at time \(k\) as
\[\begin{align} e_k & = SP_k - PV_k \end{align}\]
then consecutive values of \(MV\) are given by
\[\begin{align} MV_{k-1} & = \bar{MV} + k_p e_{k-1} + k_i \sum_{j=0}^{k-1} e_{j} \\ MV_{k} & = \bar{MV} + k_p e_{k} + k_i \sum_{j=0}^{k} e_{j} \end{align}\]
Taking differences gives a practical formula for updating the value of \(MV\) in response to measurements
\[\begin{align} MV_{k} & = MV_{k-1} + k_p(e_{k} - e_{k-1}) + k_i e_{k} \end{align}\]
The following code defines a Python object that implements this algorithm.
Exercise 1. Tune the PI control for the Temperature Control Lab
The following cell provides an initial implementation of PI control for heater T1. This is setup for testing with the off-line simulation mode of tclab. Experiment with the simulation to find appropriate values for \(k_p\) and \(k_i\). Your design goal is to achieve the setpoint and stay within a zone of +/- 2 degrees as quickly as possible.
from tclab import setup, clock, Historian, Plotter
= setup(connected=False, speedup = 20)
TCLab
= PI(kp=2, ki=0.1)
pi = 50
SP
with TCLab() as lab:
= Historian(lab.sources)
h = Plotter(h, 800)
p for t in clock(800):
= lab.T1 # measure the the process variable
PV = pi.update(SP, PV) # PI control to determine the MV
MV # set the heater power
lab.Q1(MV) # log data p.update(t)
TCLab Model disconnected successfully.
Exercise 2. Hardware testing the PI controller
Copy and paste the above code into the cell below. Connect the code to the tclab hardware by changing ‘connected’ to ‘True’. Adjust the experiment horizon to 1200 seconds to provide plenty of time for testing.
Test your controller. Does the performance match the simulation?
After the controller has achieved the setpoint, introduce a disturbance. An example of a disturbance would be to increase air flow around the device, or to touch the heater with something thermally conductive (be careful, don’t use your finger. 50 deg C is hot enough to burn your skin.)
Add a text cell below, and comment on your results. Do you see any shortcomings in this control implementation?
# put your code here
Write your comments in this cell.
Exercise 3. Multivariable PI control
The next exercise is to extend the system to control both heaters. You won’t have enough time in the lab to do this experimentally, so do this exercise using the simulation mode of tclab.
Copy and past the code from Exercise 1 into the cell below.
Add a second PI controller (rename the first pi_1, and call the second pi_2, for example). Adjust the setpoint for the first heater to 40 deg C, and the second to 35 deg C. Tune the controllers to achieve rapid acquisition of the setpoints.
# put your code here