import numpy as np import pandas as pd import tclab import time import matplotlib.pyplot as plt # generate step test data on Arduino filename = 'tclab_2sec.csv' # heater steps Q1d = np.zeros(601) Q1d[10:100] = 80 Q1d[100:200] = 20 Q1d[200:300] = 70 Q1d[300:400] = 50 Q1d[400:500] = 100 Q1d[500:] = 0 Q2d = np.zeros(601) Q2d[50:150] = 35 Q2d[150:250] = 95 Q2d[250:350] = 25 Q2d[350:450] = 100 Q2d[450:550] = 45 Q2d[550:] = 0 # Connect to Arduino a = tclab.TCLab() fid = open(filename,'w') fid.write('Time,H1,H2,T1,T2\n') fid.close() # run step test (20 min) for i in range(601): # set heater values a.Q1(Q1d[i]) a.Q2(Q2d[i]) print('Time: ' + str(2*i) + \ ' H1: ' + str(Q1d[i]) + \ ' H2: ' + str(Q2d[i]) + \ ' T1: ' + str(a.T1) + \ ' T2: ' + str(a.T2)) # wait 2 seconds time.sleep(2) fid = open(filename,'a') fid.write(str(2*i)+','+str(Q1d[i])+','+str(Q2d[i])+',' \ +str(a.T1)+','+str(a.T2)+'\n') fid.close() # close connection to Arduino a.close() # read data file data = pd.read_csv(filename) # plot measurements plt.figure() plt.subplot(2,1,1) plt.plot(data['Time'],data['H1'],'r-',label='Heater 1') plt.plot(data['Time'],data['H2'],'b--',label='Heater 2') plt.ylabel('Heater (%)') plt.legend(loc='best') plt.subplot(2,1,2) plt.plot(data['Time'],data['T1'],'r.',label='Temperature 1') plt.plot(data['Time'],data['T2'],'b.',label='Temperature 2') plt.ylabel('Temperature (degC)') plt.legend(loc='best') plt.xlabel('Time (sec)') plt.savefig('tclab_dyn_2sec.png') plt.show()