-
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathfire_active.py
203 lines (148 loc) · 5.37 KB
/
fire_active.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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
# -*- coding: utf-8 -*-
from flask import jsonify
import json
import os
import shutil
import fire
import io_temp
import settings
active = {}
phases = {}
duration = 0
currentSegment = 0
# startTemp = 0
def GetCurrentTemp():
global phases
return io_temp.GetTemp(phases['units'])
def GetEstimateTemp(units):
global phases
return io_temp.GetTemp(units)
def GetActivePath():
global active
return active['path']
# Convert the ramp, temp, hold schedules to phase segments
# |SEGMENT |SEGMENT |SEGMENT
# | | |
# | | | -
# | RAMP | HOLD | -/
# | |--------|/
# | -/| |
# | -/ | |
# | -/ | |
# |-/
# +-------------------------
def ConvertToSegments():
global phases
global active
# global startTemp
global duration
phases.clear()
phases['name'] = active['name']
phases['units'] = active['units']
phases['segments'] = []
# startTemp = 0
startTemp = GetCurrentTemp()
duration = 0.0
for i in range(len(active['segments'])):
# convert the ramp to segment
segmentRamp = {}
rampStartTemp = 0
# set the start temperature of the first segment the actual
# temperature of the kiln or the target temperature, whichever is lower
if i == 0:
rampStartTemp = min([active['segments'][i]['temp'], startTemp])
else:
rampStartTemp = active['segments'][i - 1]['temp']
segmentRamp['start'] = rampStartTemp
segmentRamp['end'] = active['segments'][i]['temp']
rampDuration = abs(segmentRamp['end'] - segmentRamp['start']) / active['segments'][i]['rate']
duration += rampDuration
segmentRamp['finished'] = duration
# convert the hold to segment
segmentHold = {}
segmentHold['start'] = active['segments'][i]['temp']
segmentHold['end'] = active['segments'][i]['temp']
# convert mins to hours
duration = duration + (active['segments'][i]['hold'] / 60.0)
segmentHold['finished'] = duration
# append the ramp and hold segments
phases['segments'].append(segmentRamp)
phases['segments'].append(segmentHold)
print("Phases:")
print(phases)
# used to show estimates of selected schedules before firing them
def GetTimeEstimate(filename):
print("getting schedule " + filename)
src_file = os.path.join('schedules', filename)
with open (src_file, "r") as scheduleData:
estimateSchedule = json.load(scheduleData)
print(estimateSchedule)
startEstimateTemp = GetEstimateTemp(estimateSchedule['units'])
estimateDuration = 0.0
for i in range(len(estimateSchedule['segments'])):
rampStartTemp = 0
# set the start temperature of the first segment the actual
# temperature of the kiln or the target temperature, whichever is lower
if i == 0:
rampStartTemp = min([estimateSchedule['segments'][i]['temp'], startEstimateTemp])
else:
rampStartTemp = estimateSchedule['segments'][i - 1]['temp']
segmentStart = rampStartTemp
segmentEnd = estimateSchedule['segments'][i]['temp']
rampDuration = abs(segmentEnd - segmentStart) / estimateSchedule['segments'][i]['rate']
estimateDuration += rampDuration
# convert mins to hours
estimateDuration += estimateSchedule['segments'][i]['hold'] / 60.0
return estimateDuration
def GetTargetTemp(timeHours):
global phases
global currentSegment
if timeHours > phases['segments'][len(phases['segments']) - 1]['finished']:
print("FINISHED")
return 0.0
# get the index of the segment
segmentIndex = 0
for i in range(len(phases['segments'])):
if timeHours <= phases['segments'][i]['finished']:
segmentIndex = i
break
print("index = " + str(segmentIndex) + ", hour = " + str(timeHours))
# get the start time of the segment
startTime = 0
if segmentIndex != 0:
startTime = phases['segments'][i - 1]['finished']
# convert time to a percentage of how far it is between the start and end times
percent = (timeHours - startTime) / (phases['segments'][segmentIndex]['finished'] - startTime)
# find the difference between the segment start and end temperatures,
# and then multiply that by the time percentage and add to the start temperature to get the target temperature
diff = phases['segments'][segmentIndex]['end'] - phases['segments'][segmentIndex]['start']
temp = phases['segments'][segmentIndex]['start'] + (diff * percent)
currentSegment = segmentIndex // 2
return temp
def StartFire(filename):
global active
# Copy selected schedule to active
print("getting schedule " + filename)
src_file = os.path.join('schedules', filename)
shutil.copyfile(src_file, "active.json")
print("READING ACTIVE.JSON")
with open ('active.json', "r") as activeData:
active = json.load(activeData)
print(active)
ConvertToSegments()
# fire.start_fire()
return jsonify(result=True)
def UpdateActiveCost(fireAmps):
global active
# convert amps used in fire to kilowatts multiplied by the cot per kilowatt
fireCost = ((fireAmps * settings.settings['volts']) / 1000.0) * settings.settings['cost']
print("Fire Cost: " + str(fireCost))
active['cost'] = fireCost
# get path to json file for active schedule
src_file = os.path.join('schedules', active['path'])
print(src_file)
# Write fire cost to json file
with open(src_file, 'w') as f:
json.dump(active, f, indent=4, separators=(',', ':'), sort_keys=True)
#add trailing newline for POSIX compatibility
f.write('\n')