-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTemp.py
123 lines (110 loc) · 3.62 KB
/
Temp.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
import serial # import Serial Library
import numpy # Import numpy
import matplotlib.pyplot as plt # import matplotlib library
from drawnow import *
from time import sleep
#import tkinter as Tk
#from tkinter.ttk import Frame
time = []
setpoint = []
temp =[]
output=[]
dataArray=[]
arduinoData = serial.Serial()
#keyboard Input
print('Press Enter without typing anything to get default values')
#Port
print('Select Port: \"3\" for com3 (default:3)')
keyIn = input()
try:
tryvar = float(keyIn)
arduinoData.port = 'com' + str(keyIn)
except ValueError:
arduinoData.port = 'com3'
#Setpoint
print('Setpoint in deg C (default:50)')
keyIn = input()
try:
tryvar = float(keyIn)
setSP = tryvar
except ValueError:
setSP = 50
#Kp
print('Kp (default 5)')
keyIn = input()
try:
tryvar = float(keyIn)
Kp = tryvar
except ValueError:
Kp = 5
#Ki
print('Ki (default 5)')
keyIn = input()
try:
tryvar = float(keyIn)
Ki = tryvar
except ValueError:
Ki = 5
#Kd
print('Kd (default 0)')
keyIn = input()
try:
tryvar = float(keyIn)
Kd = tryvar
except ValueError:
Kd = 0
#Try to connect Serial
print('Connecting Serial')
try:
arduinoData.open();
arduinoData.close();
arduinoData.open();
except ValueError:
print("Port Error")
exit()
while arduinoData.is_open == 0 :
continue
sleep(2)
strToSend = (str(setSP) + ',' + str(Kp) + ',' + str(Ki) + ',' + str(Kd) + "\r\n").encode('ascii')
print(strToSend)
arduinoData.write(strToSend)
arduinoData.flush()
sleep(1)
plt.ion() # Tell matplotlib you want interactive mode to plot live data
cnt = 0
fig = plt.figure()
def makeFig(): # Create a function that makes our desired plot
#plt.ylim(80, 90) # Set y min and max values
plot1 = fig.add_subplot(211)
plt.title('Temperature Control (Kp=' + str(curKp) + ',Ki=' + str(curKi) + ',Kd=' + str(curKd)+')') # Plot the title
plt.grid(True) # Turn the grid on
plt.ylabel('Temperature (deg C)') # Set ylabels
plt.plot(time, setpoint, 'r-', label='Setpoint') # plot the temperature
plt.plot(time, temp, 'b-', label='Temperature')
plt.legend(loc='best') # plot the legend
plot2 = fig.add_subplot(212, sharex=plot1)
plt.ylim(-260, 260) # Set limits of second y axis- adjust to readings you are getting
plt.plot(time, output, 'b-', label='PID Output') # plot pressure data
plt.ylabel('PWM DAC') # label second y axis
plt.ticklabel_format(useOffset=False) # Force matplotlib to NOT autoscale y axis
plt.legend(loc='best') # plot the legend
while True: # While loop that loops forever
while (arduinoData.inWaiting() == 0): # Wait here until there is data
pass # do nothing
arduinoString = arduinoData.readline().decode('utf-8') # read the line of text from the serial port
print(arduinoString)
dataArray = arduinoString.split(',') # Split it into an array called dataArray
curTime = float(dataArray[0]) # Convert first element to floating number and put in temp
curSP = float(dataArray[1]) # Convert second element to floating number and put in P
curTemp = float(dataArray[2])
curOutput = float(dataArray[3])
curKp = float(dataArray[4])
curKi = float(dataArray[5])
curKd = float(dataArray[6])
time.append(curTime) # Build our tempF array by appending temp readings
setpoint.append(curSP)
temp.append(curTemp)
output.append(curOutput)
drawnow(makeFig) # Call drawnow to update our live graph
plt.pause(.000001) # Pause Briefly. Important to keep drawnow from crashing
cnt = cnt + 1