Skip to content

Commit ded995d

Browse files
committed
Merge branch 'adq-logger'
2 parents 15c2dbe + 13f5493 commit ded995d

File tree

3 files changed

+122
-0
lines changed

3 files changed

+122
-0
lines changed

kiln-logger.py

Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
#!/usr/bin/env python
2+
3+
import websocket
4+
import json
5+
import time
6+
import csv
7+
import argparse
8+
import sys
9+
10+
11+
STD_HEADER = [
12+
'stamp',
13+
'runtime',
14+
'temperature',
15+
'target',
16+
'state',
17+
'heat',
18+
'totaltime',
19+
'profile',
20+
]
21+
22+
23+
PID_HEADER = [
24+
'pid_time',
25+
'pid_timeDelta',
26+
'pid_setpoint',
27+
'pid_ispoint',
28+
'pid_err',
29+
'pid_errDelta',
30+
'pid_p',
31+
'pid_i',
32+
'pid_d',
33+
'pid_kp',
34+
'pid_ki',
35+
'pid_kd',
36+
'pid_pid',
37+
'pid_out',
38+
]
39+
40+
41+
def logger(hostname, csvfile, noprofilestats, pidstats, stdout):
42+
status_ws = websocket.WebSocket()
43+
44+
csv_fields = []
45+
if not noprofilestats:
46+
csv_fields += STD_HEADER
47+
if pidstats:
48+
csv_fields += PID_HEADER
49+
50+
out = open(csvfile, 'w')
51+
csv_out = csv.DictWriter(out, csv_fields, extrasaction='ignore')
52+
csv_out.writeheader()
53+
54+
if stdout:
55+
csv_stdout = csv.DictWriter(sys.stdout, csv_fields, extrasaction='ignore', delimiter='\t')
56+
csv_stdout.writeheader()
57+
else:
58+
csv_stdout = None
59+
60+
while True:
61+
try:
62+
msg = json.loads(status_ws.recv())
63+
64+
except websocket.WebSocketException:
65+
try:
66+
status_ws.connect(f'ws://{hostname}/status')
67+
except Exception:
68+
time.sleep(5)
69+
70+
continue
71+
72+
if msg.get('type') == 'backlog':
73+
continue
74+
75+
if not noprofilestats:
76+
msg['stamp'] = time.time()
77+
if pidstats and 'pidstats' in msg:
78+
for k, v in msg.get('pidstats', {}).items():
79+
msg[f"pid_{k}"] = v
80+
81+
csv_out.writerow(msg)
82+
out.flush()
83+
84+
if stdout:
85+
for k in list(msg.keys()):
86+
v = msg[k]
87+
if isinstance(v, float):
88+
msg[k] = '{:5.3f}'.format(v)
89+
csv_stdout.writerow(msg)
90+
sys.stdout.flush()
91+
92+
93+
if __name__ == "__main__":
94+
parser = argparse.ArgumentParser(description='Log kiln data for analysis.')
95+
parser.add_argument('--hostname', type=str, default="localhost:8081", help="The kiln-controller hostname:port")
96+
parser.add_argument('--csvfile', type=str, default="/tmp/kilnstats.csv", help="Where to write the kiln stats to")
97+
parser.add_argument('--pidstats', action='store_true', help="Include PID stats")
98+
parser.add_argument('--noprofilestats', action='store_true', help="Do not store profile stats (default is to store them)")
99+
parser.add_argument('--stdout', action='store_true', help="Also print to stdout")
100+
args = parser.parse_args()
101+
102+
logger(args.hostname, args.csvfile, args.noprofilestats, args.pidstats, args.stdout)

lib/oven.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -273,6 +273,7 @@ def get_state(self):
273273
'kwh_rate': config.kwh_rate,
274274
'currency_type': config.currency_type,
275275
'profile': self.profile.name if self.profile else None,
276+
'pidstats': self.pid.pidstats,
276277
}
277278
return state
278279

@@ -462,6 +463,7 @@ def __init__(self, ki=1, kp=1, kd=1):
462463
self.lastNow = datetime.datetime.now()
463464
self.iterm = 0
464465
self.lastErr = 0
466+
self.pidstats = {}
465467

466468
# FIX - this was using a really small window where the PID control
467469
# takes effect from -1 to 1. I changed this to various numbers and
@@ -496,6 +498,23 @@ def compute(self, setpoint, ispoint):
496498

497499
output = float(output / window_size)
498500

501+
self.pidstats = {
502+
'time': time.mktime(now.timetuple()),
503+
'timeDelta': timeDelta,
504+
'setpoint': setpoint,
505+
'ispoint': ispoint,
506+
'err': error,
507+
'errDelta': dErr,
508+
'p': self.kp * error,
509+
'i': self.iterm,
510+
'd': self.kd * dErr,
511+
'kp': self.kp,
512+
'ki': self.ki,
513+
'kd': self.kd,
514+
'pid': out4logs,
515+
'out': output,
516+
}
517+
499518
if out4logs > 0:
500519
# log.info("pid percents pid=%0.2f p=%0.2f i=%0.2f d=%0.2f" % (out4logs,
501520
# ((self.kp * error)/out4logs)*100,

requirements.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,3 +6,4 @@ gevent-websocket
66
RPi.GPIO
77
Adafruit-MAX31855
88
Adafruit-GPIO
9+
websocket-client

0 commit comments

Comments
 (0)