From 59610750dd1e055d3d924679c106e4fb82cfa0b1 Mon Sep 17 00:00:00 2001 From: Phil Hord Date: Mon, 26 Oct 2015 15:20:37 -0400 Subject: [PATCH] Simulator: support endstop simulation Provide a simulated, simplified representation of the action of mechanical endstop switches which turn on and off at slightly different amounts of pressure. When any axis moves past -10, simulate endstop "on" condition. When the axis moves to 0, simulate the endstop "off" condition. This support allows the simulation of G28 (home) commands. --- simulator/simulator.c | 57 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 57 insertions(+) diff --git a/simulator/simulator.c b/simulator/simulator.c index 5784329..54b3c5f 100644 --- a/simulator/simulator.c +++ b/simulator/simulator.c @@ -329,6 +329,8 @@ bool _READ(pin_t pin) { return state[pin]; } +static void sim_endstop(int axis); + void _WRITE(pin_t pin, bool s) { bool old_state = state[pin]; uint64_t nseconds = sim_runtime_ns(); @@ -397,10 +399,65 @@ void _WRITE(pin_t pin, bool s) { pos[axis] += dir; record_pin(TRACE_POS + axis, pos[axis], nseconds); print_pos(); + + for (int a = X_AXIS; a < E_AXIS; a++) + sim_endstop(a); } } } +/** + Simulate min endstops. "on" at -10, "off" at 0. +*/ +static void sim_endstop( int axis ) { + bool on ; + + if (axis == AXIS_NONE) return; + else if (pos[axis] <= -10) on = true; + else if (pos[axis] >= 0) on = false; + else return ; + + const char * strstate = on ? "ON" : "OFF"; + int minpin; + switch (axis) { + case X_AXIS: + #ifdef X_INVERT_MIN + on = ! on; + #endif + minpin = X_MIN_PIN; + break; + case Y_AXIS: + #ifdef Y_INVERT_MIN + on = ! on; + #endif + minpin = Y_MIN_PIN; + break; + case Z_AXIS: + #ifdef Z_INVERT_MIN + on = ! on; + #endif + minpin = Z_MIN_PIN; + break; + default: + return; + } + + // No change + if (state[minpin] == on) return; + + // Change the endstop state and report it + state[minpin] = on; + record_pin(TRACE_PINS + minpin, on, sim_runtime_ns()); + bred(); + if (on) + sim_tick('A' + minpin); + else + sim_tick('a' + minpin); + fbreset(); + + sim_info("%c-Endstop: %s", "XYZE???"[axis], strstate); +} + void _SET_OUTPUT(pin_t pin) { sim_assert(pin < PIN_NB, "Pin number out of range"); direction[pin] = out;