-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy pathprobe_accuracy.py
277 lines (239 loc) · 8 KB
/
probe_accuracy.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
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
# Create a Python environment for this script. Use ssh to log in to the pi, and run the following:
#
# sudo apt install python3-venv
# python3 -m venv ~/plotly-env
# ~/plotly-env/bin/pip install -U plotly
# mkdir ~/probe_accuracy
#
# Download probe_accuracy.py and copy it to the pi into ~/probe_accuracy/ .
#
# To collect data, ssh into the pi and run the below command before doing TEST_PROBE_ACCURACY:
#
# ~/plotly-env/bin/python3 ~/probe_accuracy/probe_accuracy.py
#
# Leave that ssh session/window open for the duration of the test. After the test completes, the
# chart should be in /tmp/probe_accuracy.html. Copy that file from the pi to your local machine
# and open it.
#
# If you specify --plot-only the script will not collect data from Klipper, but instead plot an
# existing JSON data file pointed to by --data-file.
import argparse
import json
import os
import re
import socket
import time
from statistics import pstdev
import plotly.graph_objects as pgo
from plotly.subplots import make_subplots
home_dir = os.path.expanduser('~')
parser = argparse.ArgumentParser()
parser.add_argument('--klippy-uds', default=os.path.join(home_dir, 'printer_data/comms/klippy.sock'))
parser.add_argument('--data-file', default='/tmp/probe_accuracy.json')
parser.add_argument('--chart-file', default='/tmp/probe_accuracy.html')
parser.add_argument('--plot-only', action='store_true',
help='plot existing file specified by --data-file instead of collecting data from Klipper')
KLIPPY_KEY = 31415926
GCODE_SUBSCRIBE = {
'params': {'response_template': {'key': KLIPPY_KEY}},
'id': 42,
'method': 'gcode/subscribe_output'
}
TEST_END_MARKER = 'TEST_PROBE_ACCURACY: DONE'
BED_THERMISTOR_ID = 'B'
EXTRUDER_THERMISTOR_ID = 'T0'
START_RE = re.compile(r'// TEST_PROBE_ACCURACY: START')
# B:40.1 /40.0 PI:45.3 /0.0 T0:59.8 /60.0
TEMP_RE = re.compile(r'(?P<id>[\w-]+):(?P<temp>[0-9.]+)\s*/(?P<set>[0-9.]+)')
# // probe at 175.000,175.000 is z=2.027500
PROBE_RE = re.compile(r'^// probe at [0-9.,-]+ is z=(?P<z>[0-9.-]+)')
def get_klippy_output(klippy_uds: str):
sock = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
sock.connect(klippy_uds)
try:
sock.sendall(json.dumps(GCODE_SUBSCRIBE, separators=(',', ':')).encode() + b'\x03')
remainder = b''
while True:
data = sock.recv(4096)
parts = data.split(b'\x03')
parts[0] = remainder + parts[0]
remainder = parts.pop()
for part in parts:
line = part.decode()
if str(KLIPPY_KEY) not in line:
continue
if TEST_END_MARKER in line:
return
yield line
finally:
sock.close()
def parse_response(response: str) -> dict:
ts = time.time()
# Parse Z height output.
m = PROBE_RE.match(response)
if m:
d = {
'ts': ts,
'z': float(m.group('z'))
}
return d
# Parse thermistor output.
tmatches = list(TEMP_RE.finditer(response))
if tmatches:
d = {'ts': ts}
for m in tmatches:
if m.group('id') == BED_THERMISTOR_ID:
d['btemp'] = float(m.group('temp'))
d['bset'] = float(m.group('set'))
elif m.group('id') == EXTRUDER_THERMISTOR_ID:
d['etemp'] = float(m.group('temp'))
d['eset'] = float(m.group('set'))
else:
ad = {
'id': m.group('id'),
'temp': float(m.group('temp')),
'set': float(m.group('set'))
}
try:
d['atherms'].append(ad)
except KeyError:
d['atherms'] = [ad]
return d
def get_data(klippy_uds: str, data_file: str) -> list:
data = []
with open(data_file, 'w') as f:
for line in get_klippy_output(klippy_uds):
klippy_response = json.loads(line)
response = klippy_response['params']['response']
d = parse_response(response)
if d:
print("Datapoint: ", d)
data.append(d)
f.write(json.dumps(d, separators=(',', ':')) + '\n')
f.flush()
return data
def load_data(data_file: str) -> list:
with open(data_file, 'r') as f:
return [json.loads(line) for line in f]
def write_chart(data: list, output_file: str):
min_ts = data[0]['ts']
ztrace = pgo.Scatter(
x=[x['ts'] - min_ts for x in data if 'z' in x],
y=[x['z'] for x in data if 'z' in x],
name='Z',
mode='lines',
line={'color': 'black'},
yaxis='y2'
)
zstddevtrace = pgo.Scatter(
x=[ts for i, ts in enumerate(ztrace.x) if i >= 4],
y=[pstdev(ztrace.y[i-4:i+1]) * 1000 for i, ts in enumerate(ztrace.y) if i >= 4],
name='Z stddev',
mode='markers',
line={'color': 'gray'},
yaxis='y3'
)
btrace = pgo.Scatter(
x=[x['ts'] - min_ts for x in data if 'btemp' in x],
y=[x['btemp'] for x in data if 'btemp' in x],
name='bed temperature',
mode='lines',
line={'color': 'blue'}
)
bstrace = pgo.Scatter(
x=[x['ts'] - min_ts for x in data if 'bset' in x],
y=[x['bset'] for x in data if 'bset' in x],
showlegend=False,
mode='none',
fill='tozeroy',
fillcolor='rgba(128,128,255,0.3)'
)
etrace = pgo.Scatter(
x=[x['ts'] - min_ts for x in data if 'etemp' in x],
y=[x['etemp'] for x in data if 'etemp' in x],
name='extruder temperature',
mode='lines',
line={'color': 'red'}
)
estrace = pgo.Scatter(
x=[x['ts'] - min_ts for x in data if 'eset' in x],
y=[x['eset'] for x in data if 'eset' in x],
showlegend=False,
mode='none',
fill='tozeroy',
fillcolor='rgba(255,128,128,0.3)'
)
fig = pgo.Figure()
fig.add_trace(ztrace)
fig.add_trace(zstddevtrace)
fig.add_trace(btrace)
fig.add_trace(bstrace)
fig.add_trace(etrace)
fig.add_trace(estrace)
thermistors_xy = {}
for d in data:
if not 'atherms' in d:
continue
ts = d['ts'] - min_ts
for ad in d['atherms']:
therm_id = ad['id']
temp = ad['temp']
try:
thermistors_xy[therm_id]['x'].append(ts)
thermistors_xy[therm_id]['y'].append(temp)
except KeyError:
thermistors_xy[therm_id] = {
'x': [ts],
'y': [temp]
}
for therm_id, xy in thermistors_xy.items():
trace = pgo.Scatter(
x=xy['x'],
y=xy['y'],
name=f'{therm_id} temperature',
mode='lines'
)
fig.add_trace(trace)
fig.update_layout(
title=dict(
text='Probe Accuracy'
),
legend=dict(
x=1.1
),
xaxis=dict(
title='seconds',
domain=[0, 0.9]
),
yaxis=dict(
title='°C',
),
yaxis2=dict(
title='Z (mm)',
anchor='x',
overlaying='y',
side='right',
position=0.9
),
yaxis3=dict(
title='Z stddev (µm)',
rangemode='tozero',
anchor='free',
overlaying='y',
side='right',
position=1.0
),
)
fig.write_html(output_file)
def main():
args = parser.parse_args()
if args.plot_only:
data = load_data(args.data_file)
else:
print('Recording data, LEAVE THIS SESSION OPEN UNTIL THE SCRIPT SAYS "DONE"!')
print('You should see data printed each time the printer probes, otherwise something is wrong.')
data = get_data(args.klippy_uds, args.data_file)
write_chart(data, args.chart_file)
print(f'DONE, chart is in {args.chart_file}, chart data in {args.data_file}')
if __name__ == '__main__':
main()