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.
This commit is contained in:
Phil Hord 2015-10-26 15:20:37 -04:00 committed by Markus Hitter
parent 887a4eedd9
commit 59610750dd
1 changed files with 57 additions and 0 deletions

View File

@ -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;