import tclab import numpy as np import time import matplotlib.pyplot as plt from APMonitor.apm import * from mpc_lib import * # Connect to Arduino a = tclab.TCLab() # Get Version print(a.version) # Turn LED on print('LED On') a.LED(100) # Run time in minutes run_time = 5.0 # Number of cycles loops = int(60.0 * run_time) tm = np.zeros(loops) # Temperature (K) T1 = np.ones(loops) * a.T1 # temperature (degC) Tsp1 = np.ones(loops) * 30.0 # set point (degC) # Set point changes Tsp1[100:] = 40.0 Tsp1[200:] = 35.0
Q[30:] = 35.0 Q[270:] = 70.0 Q[450:] = 10.0 Q[630:] = 60.0 Q[800:] = 0.0 # Create linear interpolation of the data versus time Qf = interp1d(t, Q) ############################################################################### # MEASURE DATA ############################################################################### try: df = pd.read_csv('tclab_data.csv') except FileNotFoundError: with tclab.TCLab() as lab: h = tclab.Historian(lab.sources) for i in tclab.clock(t_heat - 1, step=1, tol=0.5): lab.Q1(Q[int(i)]) h.update(i) print(f"Time: {i:.0f} sec") df = pd.DataFrame(h.log, columns=h.columns) df.to_csv('tclab_data.csv') y = df.T1 ############################################################################### # INITIAL CONDITIONS ###############################################################################
def prepare(): print("prepare") global tc tc=tclab.TCLab() print("arduino tclab ready")
import tclab import time a = tclab.TCLab() # connect to TCLab fid = open('data.csv', 'w') fid.write('Time,Q1,T1\n') fid.write('0,0,' + str(a.T1) + '\n') fid.close() for i in range(240): # 4 minute test time.sleep(1) T1 = a.T1 # temperature Q1 = 100 if T1 <= 40.0 else 0 # On/Off Control a.Q1(Q1) # set heater print('Time: ' + str(i) + ' Q1: ' + str(Q1) + ' T1 (SP=40): ' + str(T1)) fid = open('data.csv', 'a') fid.write(str(i) + ',' + str(Q1) + ',' + str(T1) + '\n') fid.close() a.close()