TCLab Emulation for Offline Use

TCLabModel replaces TCLab for occasions where the TCLab hardware might not be available. To use, include the import

from tclab import TCLabModel as TCLab

The rest of your code will work without change. Be advised the underlying model used to approximate the behavior of the Temperature Control Laboratory is an approximation to the dynamics of the actual hardware.

[1]:
from tclab import TCLabModel as TCLab

with TCLab() as a:
    print("Temperature 1: {0:0.2f} °C".format(a.T1))
    print("Temperature 2: {0:0.2f} °C".format(a.T2))
TCLab version 0.4.5dev
Simulated TCLab
Temperature 1: 20.95 °C
Temperature 2: 20.95 °C
TCLab Model disconnected successfully.

Choosing Real or Emulation mode with setup()

The tclab.setup() function provides a choice of using actual hardware or an emulation of the TCLab device by changing a single line of code. When emulating TCLab, a second parameter speedup allows the emulation to run at a multiple of real time.

# connect to TCLab mounted on arduino
TCLab = tclab.setup(connected=True)

# Emulate the operation of TCLab using TCLabModel
TCLab = tclab.setup(connected=False)

# Emulate operation at 5× realtime
TCLab = tclab.setup(connected=False, speedup=5)

The next cell demonsrates emulation of the TCLab device at 5× real time.

[2]:
%matplotlib inline
import tclab

TCLab = tclab.setup(connected=False, speedup=5)

with TCLab() as lab:
    for t in tclab.clock(20):
        lab.Q1(100 if t < 10 else 0)
        print("t = {0:4.1f}    Q1 = {1:3.0f} %    T1 = {2:5.2f}".format(t, lab.Q1(), lab.T1))
TCLab version 0.4.5dev
Simulated TCLab
t =  0.0    Q1 = 100 %    T1 = 20.95
t =  1.0    Q1 = 100 %    T1 = 20.95
t =  2.0    Q1 = 100 %    T1 = 20.95
t =  3.0    Q1 = 100 %    T1 = 20.95
t =  4.0    Q1 = 100 %    T1 = 20.95
t =  5.0    Q1 = 100 %    T1 = 21.27
t =  6.1    Q1 = 100 %    T1 = 21.27
t =  7.0    Q1 = 100 %    T1 = 21.27
t =  8.1    Q1 = 100 %    T1 = 21.59
t =  9.0    Q1 = 100 %    T1 = 21.59
t = 10.1    Q1 =   0 %    T1 = 21.92
t = 11.2    Q1 =   0 %    T1 = 21.92
t = 12.2    Q1 =   0 %    T1 = 22.24
t = 13.2    Q1 =   0 %    T1 = 22.24
t = 14.0    Q1 =   0 %    T1 = 22.56
t = 15.2    Q1 =   0 %    T1 = 22.88
t = 16.2    Q1 =   0 %    T1 = 22.88
t = 17.2    Q1 =   0 %    T1 = 22.88
t = 18.2    Q1 =   0 %    T1 = 23.21
t = 19.0    Q1 =   0 %    T1 = 23.21
t = 20.1    Q1 =   0 %    T1 = 23.21
TCLab Model disconnected successfully.
[ ]:

[ ]: