None Notebook

This notebook contains material from cbe30338-2021; content is available on Github.

< 1.3 The Temperature Control Laboratory | Contents | Tag Index | 1.5 Assignment 1 >

Open in Colab

Download

1.4 The TCLab Python Package

The accompanying diagram shows how the code is organized to access the temperature contol laboratory using the TCLab library.

Jupyter notebooks and Python scripts: The highest level consists of the you code you write to implement control algorithms. This may be done in Jupyter/Python notebooks, or directly in Python using a development environment. This repository contains several examples, lessons, and student projects.

TCLab: TCLab consists of a Python library entitled tclab includes the TCLab() class that creates an object to access to the device, an iterator clock for synchronizing with a real time clock, Historian() class to create objects for data logging, and a Plotter() class to visualize data in real time.

TCLab-sketch: The TCLab-sketch github repository provides firmware to ensure intrisically safe operation of the Arduino board and shield. The sketch is downloaded to the Arduino using the Arduino IDE. Loading firmware to the Arduino is a one-time operation.

Arduino: The hardware platform for the Temperature Control Laboratory. The Python tools and libraries have been tested with the Arduino Leonardo boards.

1.4.1 Connecting to the Temperature Control Laboratory

1.4.1.1 Installing the tclab library

The TCLab package is installed from a terminal window (MacOS) or command window (PC) with the command

pip install tclab

Alternatively, the installation can be performed from within a Jupyter/Python notebook with the command

!pip install tclab

There are occasional updates to the library. These can be installed by appending a --upgrade to the above commands and demonstrated in the next cell.

1.4.1.2 Creating a TCLab instance

Once installed, the tclab package can be imported into Python and an instance created with the Python statements

from tclab import TCLab
lab = TCLab()
# do something
lab.close()

TCLab() attempts to find a device connected to a serial port and return a connection. An error is generated if no device is found. The connection must be closed when no longer in use.

1.4.1.3 Using the LED

The following cell demonstrates the process, and uses the tclab LED() function to flash the LED on the Temperature Control Lab for a period of 10 seconds at a 100% brightness level.

1.4.1.4 Using TCLab and Python's with statement

The Python with statement provides a convenient means of setting up and closing a connection to the Temperature Control Laboratory. In particular, the with statement establishes a context where a tclab instance is created, assigned to a variable, and automatically closed upon completion. The with statement is the preferred way to connect the Temperature Control Laboratory for most uses.

1.4.2 Reading Temperatures

Once a tclab instance is created and connected to a device the temperature sensors are acccessed with the attributes .T1 and .T2. Given an instance named lab, the temperatures are accessed as

T1 = lab.T1
T2 = lab.T2

Note that lab.T1 and lab.T2 are read-only properties. Attempt to assign a value will return a Python error.

1.4.3 Setting Heater Power

1.4.3.1 Setting maximum power with .P1 and .P2

Heater power is specified as a percentage of the maximum power available at each heater. The maximum power to each heater is determined by setting parameters .P1 and .P2 to number in the range 0 and 255. The default settings are

lab.P1 = 200
lab.P2 = 100

Based on laboratory measurements, the power delivered to each heater is approximately 14.5 mW per unit increase in .P1 and .P2. For heater 1 at the default setting of 200, the power is

$$ 200 \times 14.5 \text{mW} \times \frac{\text{1 watt}}{\text{1000 mW}} = 2.9\text{ watts}$$

For heater 2 at the default setting of 100, the power is

$$ 100 \times 14.5 \text{mW} \times \frac{\text{1 watt}}{\text{1000 mW}} = 1.45\text{ watts}$$

Note that the power delivered to the heaters for constant .P1 and .P2 is temperature dependent, and there will be some variation among units.

The default values for .P1 and .P2 were chosen to avoid unnecessarily high temperatures, and to include an asymmetric response between the two heaters.

1.4.3.2 Setting heater power with .Q1() and .Q2()

For legacy reasons, there are two ways to set the percentage of maximum power delivered to the heaters. The first way is to the functions.Q1() and .Q2() of a TCLab instance. For example, both heaters can be set to 100% power with the functions

lab = TCLab()
lab.Q1(100)
lab.Q2(100)

The device firmware limits the heaters to a range of 0 to 100%. The current settiing may be accessed via

Q1 = lab.Q1()
Q2 = lab.Q2()

The LED on the temperature control laboratory will turns bright when either heater is on. Closing the TCLab instance turns the heaters off.

1.4.3.3 Setting heater power with .U1 and .U2

Alternatively, the percentage of maximum power delivered to the heaters can be set by assigning value to the .U1 and .U2 attributes of a TCLab instances. Getting the value of .U1 and .U2 retrieves the current settings.

1.4.4 Synchronizing with Real Time using clock

The tclab module includes clock for synchronizing calculations with real time. clock(t_period, t_step) generates a sequence of evenly spaced time step over a periodt_period seconds that are t_step seconds apart. If t_step is omitted then the default time step is set to 1 second.

There are some considerations to keep in mind when using clock. Most important, by its nature Python is not a real-time environment. clock makes a best effort to stay in sync with evenly spaced ticks of the real time clock. If, for some reason, the loop falls behind the real time clock, then the generator will skip over the event to get back in sync with the real time clock. Thus the total number of iterations may be less than expected. This behavior is demonstrated in the following cell.

1.4.4.1 Using clock with TCLab

The following cell demonstrates use of clock to perform a short experiment.

1.4.5 The TCLab Historian

The Historian class provides means for logging process data to a database.

Given a list sources of data sources and methods to access the data, Historian(sources) creates an historian that logs data to database on each call to .update(). Given an instance lab of a TCLab object, lab.sources is a default list of data sources and methods for logging temperatures lab.T1 and lab.T2 and power settings lab.U1 and lab.U2.

lab = TCLab()
h = Historian(lab.sources)

The historian automatically initializes a database to log the process data. The database is updated by issuing a command

h.update(t)

where t is variable containing the current time.

To demonstrate, the following cell logs 10 seconds of data with time varying power level applied to heater 1. When the experiment is over, h.to_csv saves the data to a file that be imported in python or a spreadsheet application.

Once saved, data can be read and plotted using the Pandas Data Analysis Library as demonstrated in this cell.

1.4.6 The TCLab Plotter

The Plotter class adds a real time plotting of experimental data. A plotter is created from an instance of an historian as follows

h = Historian(lab.sources)
p = Plotter(h)

Updating the plotter also updates the associated historian.

p.update(t)

The following example shows how this works.

1.4.7 Using TCLab Offline

The tclab library includes a simulation capability. This is useful for circumstances when it isn't possible to access the hardware. The followinig cell demonstrated the use of setup to use the library in simulation mode. The argument connected is set to True if the hardware is connected, otherwise False. Simulation mode allows the use of the speedup parameter to run experiments at some multiple of real time.

1.4.8 Running Diagnostics

1.4.9 Lab Assignment 1

Create a new Jupyter notebook (i.e, don't reuse this one, start with a fresh notebook!). Title the notebook "Lab Assignment 1" as first level header, and add your name as a second level header. When you are finished with the assignment, either export as pdf or print as a pdf file, and submit your work via GradeScope.

1.4.9.1 Exercise 1.

Run the diagnostic code and include the cell output in your notebook.

1.4.9.2 Exercise 2.

Using the Historian to save your data, and Plotter to display your data, perform a step change on Heater 1, raising the power input from 0% to 60% of full power. Collect at least 600 seconds of data for temperatures T1 and T2. Plot the results.

1.4.10 Exercise 3.

Examine the step test results for T1. Estimate

Use the estimated parameters to create a simulation of the heater temperature, and compare to the measured results.

1.4.11 Exercise 4.

Examine the data for the response of temperature T2 due to a step change in heater 1. Does that appear to be the response of a first-order linear system? How does it differ? Can you still estimate a gain?

< 1.3 The Temperature Control Laboratory | Contents | Tag Index | 1.5 Assignment 1 >

Open in Colab

Download