code wiki / (root) / nx_jam_detect_test.nx

nx_jam_detect_test.nx source

↩ module page · 44 lines · 2265 B

1// nx_jam_detect_test.nx -- KAT gate for the sovereign clog/jam detector. 2// commanded extrusion vs measured feed -> OK / hard-clog / grinding. expect_exit: 0. 3 4import "nx_syscalls.nx" 5import "nx_jam_detect.nx" 6 7func w(s: *u8) -> i64 { var n: i64 = 0; while s[n] != (0 as u8) { n = n + 1 } sys_write(1, s, n); return 0 } 8func wn(v: i64) -> i64 { 9 let b: *u8 = sys_mmap(28); var m: i64 = v 10 if m < 0 { m = 0 - m; sys_write(1, "-" as *u8, 1) } 11 let t: *u8 = sys_mmap(28); var k: i64 = 0 12 if m == 0 { t[0] = 48 as u8; k = 1 } 13 while m > 0 { t[k] = (48 + (m % 10)) as u8; m = m / 10; k = k + 1 } 14 var i: i64 = 0; while i < k { b[i] = t[k - 1 - i]; i = i + 1 } 15 sys_write(1, b, k); return 0 16} 17func chk(cond: i64, pass: *i64, fail: *i64, label: *u8) -> i64 { 18 if cond == 1 { pass[0] = pass[0] + 1; w(" ok " as *u8); w(label); w("\n" as *u8) } 19 else { fail[0] = fail[0] + 1; w(" XX " as *u8); w(label); w("\n" as *u8) } 20 return 0 21} 22 23func main() -> i64 { 24 let pass: *i64 = (sys_mmap(8)) as *i64 25 let fail: *i64 = (sys_mmap(8)) as *i64 26 pass[0] = 0 27 fail[0] = 0 28 let Q: i64 = NX_JAM_Q14 29 let TH: i64 = Q // 1 mm min commanded to judge 30 let R: i64 = 70 // require >= 70% feed/commanded 31 w("=== nx_jam_detect KAT (commanded-E vs measured-feed) ===\n" as *u8) 32 33 chk(jam_step(10 * Q, 0, TH, R) == JAM_CLOG, pass, fail, "commanded 10mm, fed 0 -> HARD CLOG" as *u8) 34 chk(jam_step(10 * Q, 2 * Q, TH, R) == JAM_GRIND, pass, fail, "commanded 10mm, fed 2mm (20%) -> GRINDING" as *u8) 35 chk(jam_step(10 * Q, 6 * Q, TH, R) == JAM_GRIND, pass, fail, "commanded 10mm, fed 6mm (60%) -> GRINDING" as *u8) 36 chk(jam_step(10 * Q, 7 * Q, TH, R) == JAM_OK, pass, fail, "commanded 10mm, fed 7mm (70%) -> OK (boundary)" as *u8) 37 chk(jam_step(10 * Q, 10 * Q, TH, R) == JAM_OK, pass, fail, "commanded 10mm, fed 10mm -> OK (healthy)" as *u8) 38 chk(jam_step(0, 0, TH, R) == JAM_OK, pass, fail, "no extrusion commanded -> OK (no false alarm)" as *u8) 39 chk(jam_step(Q / 2, 0, TH, R) == JAM_OK, pass, fail, "tiny move below threshold -> OK (ignored)" as *u8) 40 41 w("=== VERDICT pass=" as *u8); wn(pass[0]); w(" fail=" as *u8); wn(fail[0]); w(" ===\n" as *u8) 42 if fail[0] == 0 { return 0 } 43 return 1 44}