Skip to content

Commit 4194cdd

Browse files
committed
♻️ Refactor Linear / Logical / Distinct Axes (MarlinFirmware#21953)
* More patches supporting EXTRUDERS 0 * Extend types in prep for more axes
1 parent f5f999d commit 4194cdd

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

43 files changed

+1141
-787
lines changed

Marlin/src/core/serial.cpp

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -101,8 +101,11 @@ void print_bin(uint16_t val) {
101101
}
102102
}
103103

104-
void print_pos(const_float_t x, const_float_t y, const_float_t z, PGM_P const prefix/*=nullptr*/, PGM_P const suffix/*=nullptr*/) {
104+
void print_pos(
105+
LINEAR_AXIS_LIST(const_float_t x, const_float_t y, const_float_t z)
106+
, PGM_P const prefix/*=nullptr*/, PGM_P const suffix/*=nullptr*/
107+
) {
105108
if (prefix) serialprintPGM(prefix);
106-
SERIAL_ECHOPAIR_P(SP_X_STR, x, SP_Y_STR, y, SP_Z_STR, z);
109+
SERIAL_ECHOPAIR_P(LIST_N(DOUBLE(LINEAR_AXES), SP_X_STR, x, SP_Y_STR, y, SP_Z_STR, z));
107110
if (suffix) serialprintPGM(suffix); else SERIAL_EOL();
108111
}

Marlin/src/core/serial.h

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -310,10 +310,13 @@ void serialprint_truefalse(const bool tf);
310310
void serial_spaces(uint8_t count);
311311

312312
void print_bin(const uint16_t val);
313-
void print_pos(const_float_t x, const_float_t y, const_float_t z, PGM_P const prefix=nullptr, PGM_P const suffix=nullptr);
313+
void print_pos(
314+
LINEAR_AXIS_LIST(const_float_t x, const_float_t y, const_float_t z),
315+
PGM_P const prefix=nullptr, PGM_P const suffix=nullptr
316+
);
314317

315318
inline void print_pos(const xyz_pos_t &xyz, PGM_P const prefix=nullptr, PGM_P const suffix=nullptr) {
316-
print_pos(xyz.x, xyz.y, xyz.z, prefix, suffix);
319+
print_pos(LINEAR_AXIS_LIST(xyz.x, xyz.y, xyz.z), prefix, suffix);
317320
}
318321

319322
#define SERIAL_POS(SUFFIX,VAR) do { print_pos(VAR, PSTR(" " STRINGIFY(VAR) "="), PSTR(" : " SUFFIX "\n")); }while(0)

Marlin/src/core/types.h

Lines changed: 248 additions & 192 deletions
Large diffs are not rendered by default.

Marlin/src/core/utility.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,3 +76,11 @@ class restorer {
7676
// Converts from an uint8_t in the range of 0-255 to an uint8_t
7777
// in the range 0-100 while avoiding rounding artifacts
7878
constexpr uint8_t ui8_to_percent(const uint8_t i) { return (int(i) * 100 + 127) / 255; }
79+
80+
const xyze_char_t axis_codes LOGICAL_AXIS_ARRAY('E', 'X', 'Y', 'Z');
81+
82+
#if LINEAR_AXES <= XYZ
83+
#define AXIS_CHAR(A) ((char)('X' + A))
84+
#else
85+
#define AXIS_CHAR(A) axis_codes[A]
86+
#endif

Marlin/src/feature/encoder_i2c.cpp

Lines changed: 17 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -327,7 +327,7 @@ int32_t I2CPositionEncoder::get_raw_count() {
327327
}
328328

329329
bool I2CPositionEncoder::test_axis() {
330-
//only works on XYZ cartesian machines for the time being
330+
// Only works on XYZ Cartesian machines for the time being
331331
if (!(encoderAxis == X_AXIS || encoderAxis == Y_AXIS || encoderAxis == Z_AXIS)) return false;
332332

333333
const float startPosition = soft_endstop.min[encoderAxis] + 10,
@@ -344,11 +344,14 @@ bool I2CPositionEncoder::test_axis() {
344344
startCoord[encoderAxis] = startPosition;
345345
endCoord[encoderAxis] = endPosition;
346346

347-
planner.synchronize();
348-
startCoord.e = planner.get_axis_position_mm(E_AXIS);
349-
planner.buffer_line(startCoord, fr_mm_s, 0);
350347
planner.synchronize();
351348

349+
#if HAS_EXTRUDERS
350+
startCoord.e = planner.get_axis_position_mm(E_AXIS);
351+
planner.buffer_line(startCoord, fr_mm_s, 0);
352+
planner.synchronize();
353+
#endif
354+
352355
// if the module isn't currently trusted, wait until it is (or until it should be if things are working)
353356
if (!trusted) {
354357
int32_t startWaitingTime = millis();
@@ -357,7 +360,7 @@ bool I2CPositionEncoder::test_axis() {
357360
}
358361

359362
if (trusted) { // if trusted, commence test
360-
endCoord.e = planner.get_axis_position_mm(E_AXIS);
363+
TERN_(HAS_EXTRUDERS, endCoord.e = planner.get_axis_position_mm(E_AXIS));
361364
planner.buffer_line(endCoord, fr_mm_s, 0);
362365
planner.synchronize();
363366
}
@@ -402,7 +405,7 @@ void I2CPositionEncoder::calibrate_steps_mm(const uint8_t iter) {
402405
planner.synchronize();
403406

404407
LOOP_L_N(i, iter) {
405-
startCoord.e = planner.get_axis_position_mm(E_AXIS);
408+
TERN_(HAS_EXTRUDERS, startCoord.e = planner.get_axis_position_mm(E_AXIS));
406409
planner.buffer_line(startCoord, fr_mm_s, 0);
407410
planner.synchronize();
408411

@@ -411,7 +414,7 @@ void I2CPositionEncoder::calibrate_steps_mm(const uint8_t iter) {
411414

412415
//do_blocking_move_to(endCoord);
413416

414-
endCoord.e = planner.get_axis_position_mm(E_AXIS);
417+
TERN_(HAS_EXTRUDERS, endCoord.e = planner.get_axis_position_mm(E_AXIS));
415418
planner.buffer_line(endCoord, fr_mm_s, 0);
416419
planner.synchronize();
417420

@@ -497,9 +500,7 @@ void I2CPositionEncodersMgr::init() {
497500

498501
encoders[i].set_active(encoders[i].passes_test(true));
499502

500-
#if I2CPE_ENC_1_AXIS == E_AXIS
501-
encoders[i].set_homed();
502-
#endif
503+
TERN_(HAS_EXTRUDERS, if (I2CPE_ENC_1_AXIS == E_AXIS) encoders[i].set_homed());
503504
#endif
504505

505506
#if I2CPE_ENCODER_CNT > 1
@@ -528,9 +529,7 @@ void I2CPositionEncodersMgr::init() {
528529

529530
encoders[i].set_active(encoders[i].passes_test(true));
530531

531-
#if I2CPE_ENC_2_AXIS == E_AXIS
532-
encoders[i].set_homed();
533-
#endif
532+
TERN_(HAS_EXTRUDERS, if (I2CPE_ENC_2_AXIS == E_AXIS) encoders[i].set_homed());
534533
#endif
535534

536535
#if I2CPE_ENCODER_CNT > 2
@@ -557,11 +556,9 @@ void I2CPositionEncodersMgr::init() {
557556
encoders[i].set_ec_threshold(I2CPE_ENC_3_EC_THRESH);
558557
#endif
559558

560-
encoders[i].set_active(encoders[i].passes_test(true));
559+
encoders[i].set_active(encoders[i].passes_test(true));
561560

562-
#if I2CPE_ENC_3_AXIS == E_AXIS
563-
encoders[i].set_homed();
564-
#endif
561+
TERN_(HAS_EXTRUDERS, if (I2CPE_ENC_3_AXIS == E_AXIS) encoders[i].set_homed());
565562
#endif
566563

567564
#if I2CPE_ENCODER_CNT > 3
@@ -590,9 +587,7 @@ void I2CPositionEncodersMgr::init() {
590587

591588
encoders[i].set_active(encoders[i].passes_test(true));
592589

593-
#if I2CPE_ENC_4_AXIS == E_AXIS
594-
encoders[i].set_homed();
595-
#endif
590+
TERN_(HAS_EXTRUDERS, if (I2CPE_ENC_4_AXIS == E_AXIS) encoders[i].set_homed());
596591
#endif
597592

598593
#if I2CPE_ENCODER_CNT > 4
@@ -621,9 +616,7 @@ void I2CPositionEncodersMgr::init() {
621616

622617
encoders[i].set_active(encoders[i].passes_test(true));
623618

624-
#if I2CPE_ENC_5_AXIS == E_AXIS
625-
encoders[i].set_homed();
626-
#endif
619+
TERN_(HAS_EXTRUDERS, if (I2CPE_ENC_5_AXIS == E_AXIS) encoders[i].set_homed());
627620
#endif
628621

629622
#if I2CPE_ENCODER_CNT > 5
@@ -652,9 +645,7 @@ void I2CPositionEncodersMgr::init() {
652645

653646
encoders[i].set_active(encoders[i].passes_test(true));
654647

655-
#if I2CPE_ENC_6_AXIS == E_AXIS
656-
encoders[i].set_homed();
657-
#endif
648+
TERN_(HAS_EXTRUDERS, if (I2CPE_ENC_6_AXIS == E_AXIS) encoders[i].set_homed());
658649
#endif
659650
}
660651

Marlin/src/feature/tmc_util.cpp

Lines changed: 25 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -757,7 +757,10 @@
757757
}
758758
}
759759

760-
static void tmc_debug_loop(const TMC_debug_enum i, const bool print_x, const bool print_y, const bool print_z, const bool print_e) {
760+
static void tmc_debug_loop(
761+
const TMC_debug_enum i,
762+
LOGICAL_AXIS_LIST(const bool print_e, const bool print_x, const bool print_y, const bool print_z)
763+
) {
761764
if (print_x) {
762765
#if AXIS_IS_TMC(X)
763766
tmc_status(stepperX, i);
@@ -821,7 +824,10 @@
821824
SERIAL_EOL();
822825
}
823826

824-
static void drv_status_loop(const TMC_drv_status_enum i, const bool print_x, const bool print_y, const bool print_z, const bool print_e) {
827+
static void drv_status_loop(
828+
const TMC_drv_status_enum i,
829+
LOGICAL_AXIS_LIST(const bool print_e, const bool print_x, const bool print_y, const bool print_z)
830+
) {
825831
if (print_x) {
826832
#if AXIS_IS_TMC(X)
827833
tmc_parse_drv_status(stepperX, i);
@@ -889,9 +895,12 @@
889895
* M122 report functions
890896
*/
891897

892-
void tmc_report_all(const bool print_x/*=true*/, const bool print_y/*=true*/, const bool print_z/*=true*/, const bool print_e/*=true*/) {
893-
#define TMC_REPORT(LABEL, ITEM) do{ SERIAL_ECHOPGM(LABEL); tmc_debug_loop(ITEM, print_x, print_y, print_z, print_e); }while(0)
894-
#define DRV_REPORT(LABEL, ITEM) do{ SERIAL_ECHOPGM(LABEL); drv_status_loop(ITEM, print_x, print_y, print_z, print_e); }while(0)
898+
void tmc_report_all(
899+
LOGICAL_AXIS_LIST(const bool print_e/*=true*/, const bool print_x/*=true*/, const bool print_y/*=true*/, const bool print_z/*=true*/)
900+
) {
901+
#define TMC_REPORT(LABEL, ITEM) do{ SERIAL_ECHOPGM(LABEL); tmc_debug_loop(ITEM, LOGICAL_AXIS_LIST(print_e, print_x, print_y, print_z)); }while(0)
902+
#define DRV_REPORT(LABEL, ITEM) do{ SERIAL_ECHOPGM(LABEL); drv_status_loop(ITEM, LOGICAL_AXIS_LIST(print_e, print_x, print_y, print_z)); }while(0)
903+
895904
TMC_REPORT("\t", TMC_CODES);
896905
#if HAS_DRIVER(TMC2209)
897906
TMC_REPORT("Address\t", TMC_UART_ADDR);
@@ -1015,7 +1024,10 @@
10151024
}
10161025
#endif
10171026

1018-
static void tmc_get_registers(TMC_get_registers_enum i, const bool print_x, const bool print_y, const bool print_z, const bool print_e) {
1027+
static void tmc_get_registers(
1028+
TMC_get_registers_enum i,
1029+
LOGICAL_AXIS_LIST(const bool print_e, const bool print_x, const bool print_y, const bool print_z)
1030+
) {
10191031
if (print_x) {
10201032
#if AXIS_IS_TMC(X)
10211033
tmc_get_registers(stepperX, i);
@@ -1079,8 +1091,10 @@
10791091
SERIAL_EOL();
10801092
}
10811093

1082-
void tmc_get_registers(bool print_x, bool print_y, bool print_z, bool print_e) {
1083-
#define _TMC_GET_REG(LABEL, ITEM) do{ SERIAL_ECHOPGM(LABEL); tmc_get_registers(ITEM, print_x, print_y, print_z, print_e); }while(0)
1094+
void tmc_get_registers(
1095+
LOGICAL_AXIS_LIST(bool print_e, bool print_x, bool print_y, bool print_z)
1096+
) {
1097+
#define _TMC_GET_REG(LABEL, ITEM) do{ SERIAL_ECHOPGM(LABEL); tmc_get_registers(ITEM, LOGICAL_AXIS_LIST(print_e, print_x, print_y, print_z)); }while(0)
10841098
#define TMC_GET_REG(NAME, TABS) _TMC_GET_REG(STRINGIFY(NAME) TABS, TMC_GET_##NAME)
10851099
_TMC_GET_REG("\t", TMC_AXIS_CODES);
10861100
TMC_GET_REG(GCONF, "\t\t");
@@ -1214,7 +1228,9 @@ static bool test_connection(TMC &st) {
12141228
return test_result;
12151229
}
12161230

1217-
void test_tmc_connection(const bool test_x/*=true*/, const bool test_y/*=true*/, const bool test_z/*=true*/, const bool test_e/*=true*/) {
1231+
void test_tmc_connection(
1232+
LOGICAL_AXIS_LIST(const bool test_e/*=true*/, const bool test_x/*=true*/, const bool test_y/*=true*/, const bool test_z/*=true*/)
1233+
) {
12181234
uint8_t axis_connection = 0;
12191235

12201236
if (test_x) {

Marlin/src/feature/tmc_util.h

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -335,14 +335,20 @@ void tmc_print_current(TMC &st) {
335335
#endif
336336

337337
void monitor_tmc_drivers();
338-
void test_tmc_connection(const bool test_x=true, const bool test_y=true, const bool test_z=true, const bool test_e=true);
338+
void test_tmc_connection(
339+
LOGICAL_AXIS_LIST(const bool test_e=true, const bool test_x=true, const bool test_y=true, const bool test_z=true)
340+
);
339341

340342
#if ENABLED(TMC_DEBUG)
341343
#if ENABLED(MONITOR_DRIVER_STATUS)
342344
void tmc_set_report_interval(const uint16_t update_interval);
343345
#endif
344-
void tmc_report_all(const bool print_x=true, const bool print_y=true, const bool print_z=true, const bool print_e=true);
345-
void tmc_get_registers(const bool print_x, const bool print_y, const bool print_z, const bool print_e);
346+
void tmc_report_all(
347+
LOGICAL_AXIS_LIST(const bool print_e=true, const bool print_x=true, const bool print_y=true, const bool print_z=true)
348+
);
349+
void tmc_get_registers(
350+
LOGICAL_AXIS_LIST(const bool print_e, const bool print_x, const bool print_y, const bool print_z)
351+
);
346352
#endif
347353

348354
/**
@@ -355,7 +361,7 @@ void test_tmc_connection(const bool test_x=true, const bool test_y=true, const b
355361
#if USE_SENSORLESS
356362

357363
// Track enabled status of stealthChop and only re-enable where applicable
358-
struct sensorless_t { bool x, y, z, x2, y2, z2, z3, z4; };
364+
struct sensorless_t { bool LINEAR_AXIS_LIST(x, y, z), x2, y2, z2, z3, z4; };
359365

360366
#if ENABLED(IMPROVE_HOMING_RELIABILITY)
361367
extern millis_t sg_guard_period;

Marlin/src/gcode/calibrate/G28.cpp

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -321,12 +321,23 @@ void GcodeSuite::G28() {
321321

322322
#else
323323

324+
#define _UNSAFE(A) (homeZ && TERN0(Z_SAFE_HOMING, axes_should_home(_BV(A##_AXIS))))
325+
324326
const bool homeZ = parser.seen_test('Z'),
325-
needX = homeZ && TERN0(Z_SAFE_HOMING, axes_should_home(_BV(X_AXIS))),
326-
needY = homeZ && TERN0(Z_SAFE_HOMING, axes_should_home(_BV(Y_AXIS))),
327-
homeX = needX || parser.seen_test('X'), homeY = needY || parser.seen_test('Y'),
328-
home_all = homeX == homeY && homeX == homeZ, // All or None
329-
doX = home_all || homeX, doY = home_all || homeY, doZ = home_all || homeZ;
327+
LINEAR_AXIS_LIST( // Other axes should be homed before Z safe-homing
328+
needX = _UNSAFE(X), needY = _UNSAFE(Y), needZ = false // UNUSED
329+
),
330+
LINEAR_AXIS_LIST( // Home each axis if needed or flagged
331+
homeX = needX || parser.seen_test('X'),
332+
homeY = needY || parser.seen_test('Y'),
333+
homeZZ = homeZ // UNUSED
334+
),
335+
// Home-all if all or none are flagged
336+
home_all = true LINEAR_AXIS_GANG(&& homeX == homeX, && homeX == homeY, && homeX == homeZ),
337+
LINEAR_AXIS_LIST(doX = home_all || homeX, doY = home_all || homeY, doZ = home_all || homeZ);
338+
339+
UNUSED(needZ);
340+
UNUSED(homeZZ);
330341

331342
#if ENABLED(HOME_Z_FIRST)
332343

@@ -336,7 +347,7 @@ void GcodeSuite::G28() {
336347

337348
const float z_homing_height = parser.seenval('R') ? parser.value_linear_units() : Z_HOMING_HEIGHT;
338349

339-
if (z_homing_height && (doX || doY || TERN0(Z_SAFE_HOMING, doZ))) {
350+
if (z_homing_height && (0 LINEAR_AXIS_GANG(|| doX, || doY, || TERN0(Z_SAFE_HOMING, doZ)))) {
340351
// Raise Z before homing any other axes and z is not already high enough (never lower z)
341352
if (DEBUGGING(LEVELING)) DEBUG_ECHOLNPAIR("Raise Z (before homing) by ", z_homing_height);
342353
do_z_clearance(z_homing_height);
@@ -469,7 +480,7 @@ void GcodeSuite::G28() {
469480
#if HAS_CURRENT_HOME(Y2)
470481
stepperY2.rms_current(tmc_save_current_Y2);
471482
#endif
472-
#endif
483+
#endif // HAS_HOMING_CURRENT
473484

474485
ui.refresh();
475486

@@ -490,7 +501,7 @@ void GcodeSuite::G28() {
490501
static constexpr AxisEnum L64XX_axis_xref[MAX_L64XX] = {
491502
X_AXIS, Y_AXIS, Z_AXIS,
492503
X_AXIS, Y_AXIS, Z_AXIS, Z_AXIS,
493-
E_AXIS, E_AXIS, E_AXIS, E_AXIS, E_AXIS, E_AXIS
504+
E_AXIS, E_AXIS, E_AXIS, E_AXIS, E_AXIS, E_AXIS, E_AXIS, E_AXIS
494505
};
495506
for (uint8_t j = 1; j <= L64XX::chain[0]; j++) {
496507
const uint8_t cv = L64XX::chain[j];

Marlin/src/gcode/calibrate/G425.cpp

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -307,9 +307,11 @@ inline void probe_sides(measurements_t &m, const float uncertainty) {
307307

308308
// The difference between the known and the measured location
309309
// of the calibration object is the positional error
310-
m.pos_error.x = TERN0(HAS_X_CENTER, true_center.x - m.obj_center.x);
311-
m.pos_error.y = TERN0(HAS_Y_CENTER, true_center.y - m.obj_center.y);
312-
m.pos_error.z = true_center.z - m.obj_center.z;
310+
LINEAR_AXIS_CODE(
311+
m.pos_error.x = TERN0(HAS_X_CENTER, true_center.x - m.obj_center.x),
312+
m.pos_error.y = TERN0(HAS_Y_CENTER, true_center.y - m.obj_center.y),
313+
m.pos_error.z = true_center.z - m.obj_center.z
314+
);
313315
}
314316

315317
#if ENABLED(CALIBRATION_REPORTING)
@@ -455,7 +457,9 @@ inline void calibrate_backlash(measurements_t &m, const float uncertainty) {
455457
// New scope for TEMPORARY_BACKLASH_CORRECTION
456458
TEMPORARY_BACKLASH_CORRECTION(all_on);
457459
TEMPORARY_BACKLASH_SMOOTHING(0.0f);
458-
const xyz_float_t move = { AXIS_CAN_CALIBRATE(X) * 3, AXIS_CAN_CALIBRATE(Y) * 3, AXIS_CAN_CALIBRATE(Z) * 3 };
460+
const xyz_float_t move = LINEAR_AXIS_ARRAY(
461+
AXIS_CAN_CALIBRATE(X) * 3, AXIS_CAN_CALIBRATE(Y) * 3, AXIS_CAN_CALIBRATE(Z) * 3
462+
);
459463
current_position += move; calibration_move();
460464
current_position -= move; calibration_move();
461465
}

Marlin/src/gcode/calibrate/M425.cpp

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -48,10 +48,12 @@ void GcodeSuite::M425() {
4848

4949
auto axis_can_calibrate = [](const uint8_t a) {
5050
switch (a) {
51-
default:
52-
case X_AXIS: return AXIS_CAN_CALIBRATE(X);
53-
case Y_AXIS: return AXIS_CAN_CALIBRATE(Y);
54-
case Z_AXIS: return AXIS_CAN_CALIBRATE(Z);
51+
default: return false;
52+
LINEAR_AXIS_CODE(
53+
case X_AXIS: return AXIS_CAN_CALIBRATE(X),
54+
case Y_AXIS: return AXIS_CAN_CALIBRATE(Y),
55+
case Z_AXIS: return AXIS_CAN_CALIBRATE(Z)
56+
);
5557
}
5658
};
5759

0 commit comments

Comments
 (0)