Skip to content

Commit

Permalink
Prevent disable of linked steppers
Browse files Browse the repository at this point in the history
  • Loading branch information
thinkyhead committed Sep 19, 2021
1 parent d57f0e0 commit 497beaa
Show file tree
Hide file tree
Showing 3 changed files with 108 additions and 77 deletions.
30 changes: 12 additions & 18 deletions Marlin/src/gcode/control/M17_M18_M84.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,15 +34,12 @@
*/
void GcodeSuite::M17() {
if (parser.seen_axis()) {
#if HAS_EXTRUDERS && HAS_E_STEPPER_ENABLE
if (parser.seen('E')) {
const int8_t e = parser.has_value() ? parser.value_int() : -1;
if (e >= 0)
stepper.enable_e_stepper(e);
else
stepper.enable_e_steppers();
}
#endif
if (TERN0(HAS_EXTRUDERS, parser.seen('E'))) {
if (parser.has_value())
stepper.enable_e_stepper(parser.value_int());
else
stepper.enable_e_steppers();
}
LINEAR_AXIS_CODE(
if (parser.seen_test('X')) ENABLE_AXIS_X(),
if (parser.seen_test('Y')) ENABLE_AXIS_Y(),
Expand All @@ -69,15 +66,12 @@ void GcodeSuite::M18_M84() {
else {
if (parser.seen_axis()) {
planner.synchronize();
#if HAS_EXTRUDERS && HAS_E_STEPPER_ENABLE
if (parser.seen('E')) {
const int8_t e = parser.has_value() ? parser.value_int() : -1;
if (e >= 0)
stepper.disable_e_stepper(e);
else
stepper.disable_e_steppers();
}
#endif
if (TERN0(HAS_EXTRUDERS, parser.seen('E'))) {
if (parser.has_value())
stepper.disable_e_stepper(parser.value_int());
else
stepper.disable_e_steppers();
}
LINEAR_AXIS_CODE(
if (parser.seen_test('X')) DISABLE_AXIS_X(),
if (parser.seen_test('Y')) DISABLE_AXIS_Y(),
Expand Down
18 changes: 10 additions & 8 deletions Marlin/src/module/stepper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -483,15 +483,17 @@ xyze_int8_t Stepper::count_direction{0};
#define DIR_WAIT_AFTER()
#endif

void Stepper::enable_e_steppers() {
#define _ENA_E(N) ENABLE_AXIS_E##N();
REPEAT(E_STEPPERS, _ENA_E)
}
#if HAS_EXTRUDERS
void Stepper::enable_e_steppers() {
#define _ENA_E(N) ENABLE_AXIS_E##N();
REPEAT(E_STEPPERS, _ENA_E)
}

void Stepper::disable_e_steppers() {
#define _DIS_E(N) disable_e_stepper(N);
REPEAT(E_STEPPERS, _DIS_E)
}
void Stepper::disable_e_steppers() {
#define _DIS_E(N) disable_e_stepper(N);
REPEAT(E_STEPPERS, _DIS_E)
}
#endif

void Stepper::enable_all_steppers() {
TERN_(AUTO_POWER_CONTROL, powerManager.power_on());
Expand Down
137 changes: 86 additions & 51 deletions Marlin/src/module/stepper.h
Original file line number Diff line number Diff line change
Expand Up @@ -236,10 +236,47 @@
// Perhaps DISABLE_MULTI_STEPPING should be required with ADAPTIVE_STEP_SMOOTHING.
#define MIN_STEP_ISR_FREQUENCY (MAX_STEP_ISR_FREQUENCY_1X / 2)

// Axis flags type, for enabled state or other simple state
typedef struct {
bool LOGICAL_AXIS_LIST(E:1, X:1, Y:1, Z:1, I:1, J:1, K:1);
union {
uint16_t bits;
struct {
bool LOGICAL_AXIS_LIST(X:1, Y:1, Z:1, I:1, J:1, K:1);
#if HAS_EXTRUDERS
bool LIST_N(EXTRUDERS, E0:1, E1:1, E2:1, E3:1, E4:1, E5:1, E6:1, E7:1);
#endif
};
};
} axis_flags_t;

// Index of the axis or extruder element in a combined array
constexpr int8_t index_of_axis(const AxisEnum axis, const uint8_t eindex=0) {
return uint8_t(axis) + (axis < LINEAR_AXES ? 0 : eindex);
}

// All the stepper enable pins
constexpr pin_t ena_pins[] = {
LINEAR_AXIS_LIST(X_ENABLE_PIN, Y_ENABLE_PIN, Z_ENABLE_PIN, I_ENABLE_PIN, J_ENABLE_PIN, K_ENABLE_PIN),
LIST_N(EXTRUDERS, E0_ENABLE_PIN, E1_ENABLE_PIN, E2_ENABLE_PIN, E3_ENABLE_PIN, E4_ENABLE_PIN, E5_ENABLE_PIN, E6_ENABLE_PIN, E7_ENABLE_PIN)
};

// Bit mask for a matching enable pin, or 0
constexpr uint16_t ena_same(const uint8_t a, const uint8_t b) {
return (ena_pins[a] == ena_pins[b]) ? _BV(b) : 0;
}

// Recursively get the enable overlaps mask for a given linear axis or extruder
constexpr uint16_t ena_overlap(const AxisEnum axis, const uint8_t eindex=0, const uint16_t value=0, const uint8_t cmpindex=0) {
return value | (cmpindex < COUNT(ena_pins) ? ena_overlap(axis, eindex, ena_same(index_of_axis(axis, eindex), cmpindex), cmpindex + 1) : 0);
}

#define _OVERLAP(N) ena_overlap(AxisEnum(N)),
#define _E_OVERLAP(N) ena_overlap(E_AXIS, N),
constexpr uint16_t ena_overlaps[] = {
REPEAT(LINEAR_AXES, _OVERLAP)
REPEAT(EXTRUDERS, _E_OVERLAP)
};

//
// Stepper class definition
//
Expand Down Expand Up @@ -388,22 +425,6 @@ class Stepper {

#endif

// Return a mask of axes that overlap an axis ENABLE pin
static inline uint16_t ena_overlap(const AxisEnum axis, const uint8_t eindex=0) {
constexpr pin_t enalist[] = {
LINEAR_AXIS_LIST(X_ENABLE_PIN, Y_ENABLE_PIN, Z_ENABLE_PIN, I_ENABLE_PIN, J_ENABLE_PIN, K_ENABLE_PIN),
LIST_N(EXTRUDERS, E0_ENABLE_PIN, E1_ENABLE_PIN, E2_ENABLE_PIN, E3_ENABLE_PIN, E4_ENABLE_PIN, E5_ENABLE_PIN, E6_ENABLE_PIN, E7_ENABLE_PIN)
};

uint16_t overlap = 0;
for (uint8_t i = 0; i < COUNT(enalist); i++) {
const uint8_t a = TERN(HAS_EXTRUDERS, i <= E_AXIS ? i : E_AXIS, i);
if (a != axis && enalist[a] == enalist[axis]) SBI(overlap, a);
}

return overlap;
}

public:
// Initialize stepper hardware
static void init();
Expand Down Expand Up @@ -541,55 +562,69 @@ class Stepper {

static axis_flags_t axis_enabled; // Axis stepper ENABLED states

static inline void mark_axis_enabled(const AxisEnum axis, const uint8_t eindex=0) {
SBI(axis_enabled.bits, index_of_axis(axis, eindex));
}
static inline void mark_axis_disabled(const AxisEnum axis, const uint8_t eindex=0) {
CBI(axis_enabled.bits, index_of_axis(axis, eindex));
}
static inline bool can_axis_disable(const AxisEnum axis, const uint8_t eindex=0) {
uint8_t axis_index = index_of_axis(axis, eindex);
return ena_overlaps[axis_index] == _BV(axis_index) || !(ena_overlaps[axis_index] & axis_enabled.bits);
}

static inline void enable_axis(const AxisEnum axis) {
#define _CASE_ENABLE(N) case N##_AXIS: ENABLE_AXIS_##N(); axis_enabled.N = true; break;
#define _CASE_ENABLE(N) case N##_AXIS: ENABLE_AXIS_##N(); break;
switch (axis) {
LINEAR_AXIS_CODE(
_CASE_ENABLE(X),
_CASE_ENABLE(Y),
_CASE_ENABLE(Z),
_CASE_ENABLE(I),
_CASE_ENABLE(J),
_CASE_ENABLE(K)
_CASE_ENABLE(X), _CASE_ENABLE(Y), _CASE_ENABLE(Z),
_CASE_ENABLE(I), _CASE_ENABLE(J), _CASE_ENABLE(K)
);
default: break;
}
#undef _CASE_ENABLE
mark_axis_enabled(axis);
}

static inline void disable_axis(const AxisEnum axis) {
#define _CASE_DISABLE(N) case N##_AXIS: DISABLE_AXIS_##N(); axis_enabled.N = false; break;
switch (axis) {
LINEAR_AXIS_CODE(
_CASE_DISABLE(X),
_CASE_DISABLE(Y),
_CASE_DISABLE(Z),
_CASE_DISABLE(I),
_CASE_DISABLE(J),
_CASE_DISABLE(K)
);
default: break;
mark_axis_disabled(axis);
// If all the axes that share the enabled bit are disabled
if (can_axis_disable(axis)) {
#define _CASE_DISABLE(N) case N##_AXIS: DISABLE_AXIS_##N(); break;
switch (axis) {
LINEAR_AXIS_CODE(
_CASE_DISABLE(X), _CASE_DISABLE(Y), _CASE_DISABLE(Z),
_CASE_DISABLE(I), _CASE_DISABLE(J), _CASE_DISABLE(K)
);
default: break;
}
}
#undef _CASE_DISABLE
}

static void enable_all_steppers();
static void enable_e_steppers();
static inline void enable_e_stepper(const uint8_t e) {
#define _CASE_ENA_E(N) case N: ENABLE_AXIS_E##N(); break;
switch (e) {
REPEAT(E_STEPPERS, _CASE_ENA_E)
#if HAS_EXTRUDERS
static inline void enable_e_stepper(const uint8_t eindex) {
#define _CASE_ENA_E(N) case N: ENABLE_AXIS_E##N(); break;
switch (eindex) {
REPEAT(E_STEPPERS, _CASE_ENA_E)
}
}
}
static inline void disable_e_stepper(const uint8_t eindex) {
mark_axis_disabled(E_AXIS, eindex);
if (can_axis_disable(E_AXIS, eindex)) {
#define _CASE_DIS_E(N) case N: DISABLE_AXIS_E##N(); break;
switch (eindex) { REPEAT(E_STEPPERS, _CASE_DIS_E) }
}
}
static void enable_e_steppers();
static void disable_e_steppers();
#else
static inline void enable_e_stepper(const uint8_t) {}
static inline void disable_e_stepper(const uint8_t) {}
static inline void enable_e_steppers() {}
static inline void disable_e_steppers() {}
#endif

static void enable_all_steppers();
static void disable_all_steppers();
static void disable_e_steppers();
static inline void disable_e_stepper(const uint8_t e) {
#define _CASE_DIS_E(N) case N: DISABLE_AXIS_E##N(); break;
switch (e) {
REPEAT(E_STEPPERS, _CASE_DIS_E)
}
}

// Update direction states for all steppers
static void set_directions();
Expand Down

0 comments on commit 497beaa

Please sign in to comment.