!pip install tclab --upgrade
TCLab Lab 1: Coding a relay controller
From https://github.com/jckantor/CBE30338
and this one more specifically: https://github.com/jckantor/cbe30338-book/blob/main/notebooks/03.04-Relay-Control.ipynb
The purpose of this first laboratory session is to verify that you can interface and interact with the TCLab hardware, and familiarize you with the TCLab library. The first exercise will be to code a rudimentary relay (also called ‘on-off’ or thermostat) controller for one of the two heaters.
Before you begin, you should be familiar with the following reading meterials:
- On-Off Control in Section 1.6 of Feedback Control for Scientists and Engineers
- 01_Understanding_TCLab
Exercise 1. Download and install TCLab.py
Execute the following cell to download and install the TCLab.py python library.
Exercise 2. Verify that your hardware and software are working correctly.
The following cell should cause the LED on the TCLab shield to light up to 100% maximum brightness.
from tclab import TCLab
with TCLab() as lab:
0) lab.LED(
TCLab version 1.0.0
Arduino Leonardo connected on port /dev/cu.usbmodem142101 at 115200 baud.
TCLab Firmware 3.0.0 Arduino Leonardo/Micro.
TCLab disconnected successfully.
Exercise 3. Turn on the heaters for 120 seconds and log temperature response.
For this exercise, write a code cell that turns on heater 1 at 100% power, then log the temperature response once per second for 120 seconds. The output of the cell should report the time, power level, and temperature for each measurement. You may wish to consult 01_Understanding_TCLab notebook for relevant code examples. You will need the clock
function from tclab
for this exercise.
from tclab import TCLab, clock, Historian, Plotter, setup
# TCLab = setup(connected=False, speedup=20)
# control parameters
= 0
U_min = 100
U_max = 40
T_SP
# time horizon and time step
= 250
t_final = 1
t_step
# perform experiment
with TCLab() as lab:
= 200
lab.P1 = Historian(lab.sources)
h = Plotter(h, t_final)
p for t in clock(t_final, t_step):
= lab.T1 # measure temperature
T1 if lab.T1 < T_SP:
= U_max
U1 else
= U_min
U1 # U1 = U_max if lab.T1 < T_SP else U_min # compute manipulated variable
# adjust power
lab.Q1(U1) # log results p.update(t)
TCLab disconnected successfully.
Exercise 4. Code an on-off controller.
Code an on-off controller for a setpoint of 40 degrees C using heater 1 as the manipulated variable, and temperature 1 as the measured variable. Operate the controller for at least 5 minutes (600 seconds), reporting time/power/temperature measurements every 2 seconds.
from tclab import TCLab, clock, Historian, Plotter, setup
= setup(connected=False, speedup=20)
TCLab
# control parameters
= 0
U_min = 100
U_max = 40
T_SP
# time horizon and time step
= 250
t_final = 1
t_step
# perform experiment
with TCLab() as lab:
= 200
lab.P1 = Historian(lab.sources)
h = Plotter(h, t_final)
p for t in clock(t_final, t_step):
= lab.T1 # measure temperature
T1 = U_max if lab.T1 < T_SP else U_min # compute manipulated variable
U1 # adjust power
lab.Q1(U1) # log results p.update(t)
Exercise 5. Analysis
Examine the results of the previous exercise and answer the following questions.
Approximately how much time elapses between power on and power off events?
What is the approximate duty cycle (i.e, fraction of time the heater is in the ‘on’ state) once the initial start-up period has passed.
What is the size of the oscillation around the setpoint? Why does this occur?