Files
forgefirm/scripts/bench/pwm_stream_test.py
T
ScottW514 cd8a01a3d9 bench: every board-runnable tool ported to the bench page
The remaining bench diagnostics run from forgetest's #bench tab. The
tools that also run from a LAN host share scripts/bench/gfbench.py:
GF_HOST names a remote machine (host mode, sysfs through ssh, Grbl and
forgectrl over the LAN); unset, the tool runs on the board itself
(local mode, sysfs directly, everything on 127.0.0.1), which is how the
page runs them - with GF_HOST=127.0.0.1, the panel token in GF_TOKEN
and their data files under <data>/bench/ (FORGETEST_BENCH_DATA). The
helper also reads a machine setting from forgectrl, or from the settings
file on the board while forgectrl is stopped.

Ported: pwm_sweep / pwm_hold (scope = a takeover; the latch relocked,
the write refused if FIRE or LASER_ON reads active), pwm_stream_test
(PASS/FAIL exit), flow_characterize, flow_recheck_char,
flow_warm_validate and flow_matrix (takeovers: forgectrl owns the
thermal hardware, so the page's takeover replaces the tools' own
controller stop/restart, whose command line predated the supervisor;
results and logs in the bench data directory), flow_sustained,
fan_test, temp_calibrate (dry; watch bounded in seconds; the threshold
and the coolant conversion from the shared code), flow_escalate_drill
(cool_confirm_max_s shortened through forgectrl's settings for the
drill and restored; the setting's minimum is the default budget), and
live_fire_drills (<drill> [S] [F], all six drills, host from GF_HOST,
token from the board). flow_matrix joins the registry. What stays
unported cannot run against the machine at all: the two null-sink CI
harnesses and the .puls decoder.

Runner: a scope tool runs inside the takeover wrapper; the bench
environment above is passed to every tool. Tests: test_bench_registry
(registry <-> scripts/bench consistency, every ported tool builds its
command line, every script compiles, gfbench host/local modes) and the
server test (scope tool takeover, the environment reaching the tool).
Local mode smoke-run on the bench (temp_calibrate watch, setting, token)
from /tmp, removed after.

No catalog consequence: bench tools are not image components (dev-only
forgetest); the acceptance catalog is unchanged.
2026-08-16 16:28:39 -04:00

110 lines
3.7 KiB
Python

#!/usr/bin/env python3
"""LASER_PWM stream-path test - runs ON the board.
Streams POWER BYTES ONLY (bit 7 set) through /dev/glowforge and lets the
kernel pulse engine play them, so the scope on LASER_PWM verifies the
real power path the laser milestone will use, including two contract
rules (run-start duty reset; consecutive power bytes dropped).
Safety posture:
- The stream contains ZERO step bytes and ZERO FIRE (bit 4) bits.
- motor_lock=15 masks step output on all axes regardless.
- laser_latch re-asserted locked; lid closed; HV watchdog untouched.
- Position counters compared before/after - must be identical.
- streaming stays 0: end-of-data is a normal completion.
Runs with the controller and forgectrl stopped (the bench page's
takeover). Exit status 0 when the counters did not move, the run ended
in an idle state and no FIRE/emission was read back; 1 otherwise.
"""
import fcntl, mmap, os, struct, sys, time
TICK_HZ = 10000
PWM2_BASE = 0x02084000
def wr(attr, val):
with open('/sys/glowforge/' + attr, 'w') as f:
f.write(str(val))
def rd(attr):
with open('/sys/glowforge/' + attr) as f:
return f.read().strip()
def rd_pos():
with open('/sys/glowforge/cnc/position', 'rb') as f:
raw = f.read(32)
x, y, z, done, total = struct.unpack('<5i', raw[:20])
return x, y, z, done, total
def pwmsar():
with open('/dev/mem', 'rb') as f:
m = mmap.mmap(f.fileno(), 4096, mmap.MAP_SHARED, mmap.PROT_READ,
offset=PWM2_BASE)
sar = struct.unpack('<I', m[0x0C:0x10])[0]
m.close()
return sar
def power(v):
return bytes([0x80 | (v & 0x7F)])
PAD = b'\x00'
stream = (
PAD * TICK_HZ + # 1 s: shows the run-start 100% reset
power(64) + PAD * (2 * TICK_HZ) + # 50%
power(32) + power(96) + PAD * (2 * TICK_HZ) + # 25%; the 96 must be DROPPED
power(96) + PAD * (2 * TICK_HZ) + # 75% (applies after pads)
power(8) + PAD * (2 * TICK_HZ) + # ~6%
power(127) + PAD * (2 * TICK_HZ) # 100%
)
print('stream: %d bytes = %.1f s at %d Hz' % (len(stream), len(stream) / TICK_HZ, TICK_HZ))
print('pre: state=%s pos=%s laser_enable=%s PWMSAR=%d'
% (rd('cnc/state'), rd_pos(), rd('cnc/laser_enable'), pwmsar()))
wr('cnc/laser_latch', 1)
wr('cnc/motor_lock', 15)
wr('cnc/step_freq', TICK_HZ)
fd = os.open('/dev/glowforge', os.O_WRONLY)
try:
fcntl.flock(fd, fcntl.LOCK_EX)
os.lseek(fd, 1, os.SEEK_SET) # clear pulse data + byte counters
wr('cnc/enable', 1)
time.sleep(0.5)
os.write(fd, stream) # preload the whole program
pos_before = rd_pos()
print('run: state=%s, starting playback...' % rd('cnc/state'))
wr('cnc/run', 1)
t0 = time.time()
state = ''
while time.time() - t0 < 30:
state = rd('cnc/state')
if state != 'running':
break
time.sleep(0.2)
dt = time.time() - t0
print('done: state=%s after %.1f s' % (state, dt))
pos_after = rd_pos()
moved = pos_before[:3] != pos_after[:3]
print('post: pos before=%s after=%s MOVED=%s' % (pos_before, pos_after, moved))
en, on, sampled = rd('cnc/laser_enable'), rd('cnc/laser_on'), rd('cnc/laser_on_sampled')
print('post: laser_enable=%s laser_on=%s laser_on_sampled=%s faults=%s underruns=%s'
% (en, on, sampled, rd('cnc/faults'), rd('cnc/underruns')))
print('post: PWMSAR=%d (duty after end-of-data)' % pwmsar())
ok = (not moved) and state == 'idle' and en == '0' and on == '0' and sampled == '0'
finally:
fcntl.flock(fd, fcntl.LOCK_UN)
os.close(fd)
wr('cnc/disable', 1)
print('safe state restored: state=%s' % rd('cnc/state'))
print('RESULT %s' % ('PASS' if ok else 'FAIL'))
sys.exit(0 if ok else 1)