-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathprogram.py
More file actions
64 lines (52 loc) · 1.88 KB
/
Copy pathprogram.py
File metadata and controls
64 lines (52 loc) · 1.88 KB
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
import ujson as json
from logger import Logger
log = Logger(__name__)
class Instruction:
def __init__(self, temp, time, **kwargs):
self.temp = temp # degrees
self.time = time # seconds
class Program:
def __init__(self, name=None):
self.name = name
self.instructions = None
if name is not None:
self.load(f"prog/{name}")
def load(self, name):
with open(name, "r") as file:
data = json.load(file)
self.name = data["name"]
self.instructions = [Instruction(**inst) for inst in data["instructions"]]
log.debug(f"Loaded program {name} with {len(self.instructions)} instructions")
def save(self, name):
with open(name, "w") as file:
json.dump(self, file, default=serialize, indent=0)
log.debug(f"Saved program {name} with {len(self.instructions)} instructions")
def get_setpoint(self, runtime=0):
if self.instructions is None:
return None
for i in range(len(self.instructions)):
inst = self.instructions[i]
if runtime > inst.time:
continue
if i == 0:
last = Instruction(0, 0)
else:
last = self.instructions[i - 1]
if last.temp == inst.temp:
# hold
return inst.temp
# Linear interpolation
delta_time = inst.time - last.time
delta_temp = inst.temp - last.temp
if delta_time == 0:
return last.temp
ratio = (runtime - last.time) / delta_time
return last.temp + ratio * delta_temp
return None
def serialize(obj):
if isinstance(obj, Program):
return {
"name": obj.name,
"instructions": [serialize(inst) for inst in obj.instructions],
}
return obj.__dict__