Skip to content

Commit 26a2443

Browse files
committed
♻️ Refactor axis counts and loops
1 parent f7d28ce commit 26a2443

45 files changed

Lines changed: 178 additions & 165 deletions

Some content is hidden

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

Marlin/src/core/types.h

Lines changed: 18 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -47,25 +47,23 @@ struct IF<true, L, R> { typedef L type; };
4747
// - X_HEAD, Y_HEAD, and Z_HEAD should be used for Steppers on Core kinematics
4848
//
4949
enum AxisEnum : uint8_t {
50-
X_AXIS = 0, A_AXIS = 0,
51-
Y_AXIS = 1, B_AXIS = 1,
52-
Z_AXIS = 2, C_AXIS = 2,
53-
E_AXIS = 3,
54-
X_HEAD = 4, Y_HEAD = 5, Z_HEAD = 6,
55-
E0_AXIS = 3,
50+
X_AXIS = 0, A_AXIS = X_AXIS,
51+
Y_AXIS = 1, B_AXIS = Y_AXIS,
52+
Z_AXIS = 2, C_AXIS = Z_AXIS,
53+
E_AXIS,
54+
X_HEAD, Y_HEAD, Z_HEAD,
55+
E0_AXIS = E_AXIS,
5656
E1_AXIS, E2_AXIS, E3_AXIS, E4_AXIS, E5_AXIS, E6_AXIS, E7_AXIS,
57-
ALL_AXES = 0xFE, NO_AXIS = 0xFF
57+
ALL_AXES_MASK = 0xFE, NO_AXIS_MASK = 0xFF
5858
};
5959

6060
//
61-
// Loop over XYZE axes
61+
// Loop over axes
6262
//
63-
#define LOOP_XYZ(VAR) LOOP_S_LE_N(VAR, X_AXIS, Z_AXIS)
64-
#define LOOP_XYZE(VAR) LOOP_S_LE_N(VAR, X_AXIS, E_AXIS)
65-
#define LOOP_XYZE_N(VAR) LOOP_S_L_N(VAR, X_AXIS, XYZE_N)
6663
#define LOOP_ABC(VAR) LOOP_S_LE_N(VAR, A_AXIS, C_AXIS)
67-
#define LOOP_ABCE(VAR) LOOP_S_LE_N(VAR, A_AXIS, E_AXIS)
68-
#define LOOP_ABCE_N(VAR) LOOP_S_L_N(VAR, A_AXIS, XYZE_N)
64+
#define LOOP_LINEAR_AXES(VAR) LOOP_S_L_N(VAR, X_AXIS, LINEAR_AXES)
65+
#define LOOP_LOGICAL_AXES(VAR) LOOP_S_L_N(VAR, X_AXIS, LOGICAL_AXES)
66+
#define LOOP_DISTINCT_AXES(VAR) LOOP_S_L_N(VAR, X_AXIS, DISTINCT_AXES)
6967

7068
//
7169
// feedRate_t is just a humble float
@@ -201,8 +199,8 @@ struct XYval {
201199
FI void set(const T (&arr)[XY]) { x = arr[0]; y = arr[1]; }
202200
FI void set(const T (&arr)[XYZ]) { x = arr[0]; y = arr[1]; }
203201
FI void set(const T (&arr)[XYZE]) { x = arr[0]; y = arr[1]; }
204-
#if XYZE_N > XYZE
205-
FI void set(const T (&arr)[XYZE_N]) { x = arr[0]; y = arr[1]; }
202+
#if DISTINCT_AXES > LOGICAL_AXES
203+
FI void set(const T (&arr)[DISTINCT_AXES]) { x = arr[0]; y = arr[1]; }
206204
#endif
207205
FI void reset() { x = y = 0; }
208206
FI T magnitude() const { return (T)sqrtf(x*x + y*y); }
@@ -312,8 +310,8 @@ struct XYZval {
312310
FI void set(const T (&arr)[XY]) { x = arr[0]; y = arr[1]; }
313311
FI void set(const T (&arr)[XYZ]) { x = arr[0]; y = arr[1]; z = arr[2]; }
314312
FI void set(const T (&arr)[XYZE]) { x = arr[0]; y = arr[1]; z = arr[2]; }
315-
#if XYZE_N > XYZE
316-
FI void set(const T (&arr)[XYZE_N]) { x = arr[0]; y = arr[1]; z = arr[2]; }
313+
#if DISTINCT_AXES > XYZE
314+
FI void set(const T (&arr)[DISTINCT_AXES]) { x = arr[0]; y = arr[1]; z = arr[2]; }
317315
#endif
318316
FI void reset() { x = y = z = 0; }
319317
FI T magnitude() const { return (T)sqrtf(x*x + y*y + z*z); }
@@ -427,8 +425,8 @@ struct XYZEval {
427425
FI void set(const T (&arr)[XY]) { x = arr[0]; y = arr[1]; }
428426
FI void set(const T (&arr)[XYZ]) { x = arr[0]; y = arr[1]; z = arr[2]; }
429427
FI void set(const T (&arr)[XYZE]) { x = arr[0]; y = arr[1]; z = arr[2]; e = arr[3]; }
430-
#if XYZE_N > XYZE
431-
FI void set(const T (&arr)[XYZE_N]) { x = arr[0]; y = arr[1]; z = arr[2]; e = arr[3]; }
428+
#if DISTINCT_AXES > XYZE
429+
FI void set(const T (&arr)[DISTINCT_AXES]) { x = arr[0]; y = arr[1]; z = arr[2]; e = arr[3]; }
432430
#endif
433431
FI XYZEval<T> copy() const { return *this; }
434432
FI XYZEval<T> ABS() const { return { T(_ABS(x)), T(_ABS(y)), T(_ABS(z)), T(_ABS(e)) }; }
@@ -518,4 +516,4 @@ struct XYZEval {
518516
#undef FI
519517

520518
const xyze_char_t axis_codes { 'X', 'Y', 'Z', 'E' };
521-
#define XYZ_CHAR(A) ((char)('X' + A))
519+
#define AXIS_CHAR(A) ((char)('X' + A))

Marlin/src/core/utility.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -123,9 +123,9 @@ void safe_delay(millis_t ms) {
123123
#endif
124124
#if ABL_PLANAR
125125
SERIAL_ECHOPGM("ABL Adjustment X");
126-
LOOP_XYZ(a) {
126+
LOOP_LINEAR_AXES(a) {
127127
const float v = planner.get_axis_position_mm(AxisEnum(a)) - current_position[a];
128-
SERIAL_CHAR(' ', XYZ_CHAR(a));
128+
SERIAL_CHAR(' ', AXIS_CHAR(a));
129129
if (v > 0) SERIAL_CHAR('+');
130130
SERIAL_DECIMAL(v);
131131
}

Marlin/src/feature/backlash.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@ void Backlash::add_correction_steps(const int32_t &da, const int32_t &db, const
104104

105105
const float f_corr = float(correction) / 255.0f;
106106

107-
LOOP_XYZ(axis) {
107+
LOOP_LINEAR_AXES(axis) {
108108
if (distance_mm[axis]) {
109109
const bool reversing = TEST(dm,axis);
110110

Marlin/src/feature/dac/dac_mcp4728.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ uint8_t MCP4728::analogWrite(const uint8_t channel, const uint16_t value) {
7373
uint8_t MCP4728::eepromWrite() {
7474
Wire.beginTransmission(I2C_ADDRESS(DAC_DEV_ADDRESS));
7575
Wire.write(SEQWRITE);
76-
LOOP_XYZE(i) {
76+
LOOP_LOGICAL_AXES(i) {
7777
Wire.write(DAC_STEPPER_VREF << 7 | DAC_STEPPER_GAIN << 4 | highByte(dac_values[i]));
7878
Wire.write(lowByte(dac_values[i]));
7979
}
@@ -135,7 +135,7 @@ void MCP4728::setDrvPct(xyze_uint_t &pct) {
135135
*/
136136
uint8_t MCP4728::fastWrite() {
137137
Wire.beginTransmission(I2C_ADDRESS(DAC_DEV_ADDRESS));
138-
LOOP_XYZE(i) {
138+
LOOP_LOGICAL_AXES(i) {
139139
Wire.write(highByte(dac_values[i]));
140140
Wire.write(lowByte(dac_values[i]));
141141
}

Marlin/src/feature/dac/stepper_dac.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ static float dac_amps(int8_t n) { return mcp4728.getValue(dac_order[n]) * 0.125
7777

7878
uint8_t StepperDAC::get_current_percent(const AxisEnum axis) { return mcp4728.getDrvPct(dac_order[axis]); }
7979
void StepperDAC::set_current_percents(xyze_uint8_t &pct) {
80-
LOOP_XYZE(i) dac_channel_pct[i] = pct[dac_order[i]];
80+
LOOP_LOGICAL_AXES(i) dac_channel_pct[i] = pct[dac_order[i]];
8181
mcp4728.setDrvPct(dac_channel_pct);
8282
}
8383

Marlin/src/feature/encoder_i2c.cpp

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -337,7 +337,7 @@ bool I2CPositionEncoder::test_axis() {
337337
ec = false;
338338

339339
xyze_pos_t startCoord, endCoord;
340-
LOOP_XYZ(a) {
340+
LOOP_LINEAR_AXES(a) {
341341
startCoord[a] = planner.get_axis_position_mm((AxisEnum)a);
342342
endCoord[a] = planner.get_axis_position_mm((AxisEnum)a);
343343
}
@@ -392,7 +392,7 @@ void I2CPositionEncoder::calibrate_steps_mm(const uint8_t iter) {
392392
travelDistance = endDistance - startDistance;
393393

394394
xyze_pos_t startCoord, endCoord;
395-
LOOP_XYZ(a) {
395+
LOOP_LINEAR_AXES(a) {
396396
startCoord[a] = planner.get_axis_position_mm((AxisEnum)a);
397397
endCoord[a] = planner.get_axis_position_mm((AxisEnum)a);
398398
}
@@ -822,7 +822,7 @@ void I2CPositionEncodersMgr::M860() {
822822
const bool hasU = parser.seen_test('U'), hasO = parser.seen_test('O');
823823

824824
if (I2CPE_idx == 0xFF) {
825-
LOOP_XYZE(i) {
825+
LOOP_LOGICAL_AXES(i) {
826826
if (!I2CPE_anyaxis || parser.seen_test(axis_codes[i])) {
827827
const uint8_t idx = idx_from_axis(AxisEnum(i));
828828
if ((int8_t)idx >= 0) report_position(idx, hasU, hasO);
@@ -849,7 +849,7 @@ void I2CPositionEncodersMgr::M861() {
849849
if (parse()) return;
850850

851851
if (I2CPE_idx == 0xFF) {
852-
LOOP_XYZE(i) {
852+
LOOP_LOGICAL_AXES(i) {
853853
if (!I2CPE_anyaxis || parser.seen(axis_codes[i])) {
854854
const uint8_t idx = idx_from_axis(AxisEnum(i));
855855
if ((int8_t)idx >= 0) report_status(idx);
@@ -877,7 +877,7 @@ void I2CPositionEncodersMgr::M862() {
877877
if (parse()) return;
878878

879879
if (I2CPE_idx == 0xFF) {
880-
LOOP_XYZE(i) {
880+
LOOP_LOGICAL_AXES(i) {
881881
if (!I2CPE_anyaxis || parser.seen(axis_codes[i])) {
882882
const uint8_t idx = idx_from_axis(AxisEnum(i));
883883
if ((int8_t)idx >= 0) test_axis(idx);
@@ -908,7 +908,7 @@ void I2CPositionEncodersMgr::M863() {
908908
const uint8_t iterations = constrain(parser.byteval('P', 1), 1, 10);
909909

910910
if (I2CPE_idx == 0xFF) {
911-
LOOP_XYZE(i) {
911+
LOOP_LOGICAL_AXES(i) {
912912
if (!I2CPE_anyaxis || parser.seen(axis_codes[i])) {
913913
const uint8_t idx = idx_from_axis(AxisEnum(i));
914914
if ((int8_t)idx >= 0) calibrate_steps_mm(idx, iterations);
@@ -984,7 +984,7 @@ void I2CPositionEncodersMgr::M865() {
984984
if (parse()) return;
985985

986986
if (!I2CPE_addr) {
987-
LOOP_XYZE(i) {
987+
LOOP_LOGICAL_AXES(i) {
988988
if (!I2CPE_anyaxis || parser.seen(axis_codes[i])) {
989989
const uint8_t idx = idx_from_axis(AxisEnum(i));
990990
if ((int8_t)idx >= 0) report_module_firmware(encoders[idx].get_address());
@@ -1015,7 +1015,7 @@ void I2CPositionEncodersMgr::M866() {
10151015
const bool hasR = parser.seen_test('R');
10161016

10171017
if (I2CPE_idx == 0xFF) {
1018-
LOOP_XYZE(i) {
1018+
LOOP_LOGICAL_AXES(i) {
10191019
if (!I2CPE_anyaxis || parser.seen(axis_codes[i])) {
10201020
const uint8_t idx = idx_from_axis(AxisEnum(i));
10211021
if ((int8_t)idx >= 0) {
@@ -1053,7 +1053,7 @@ void I2CPositionEncodersMgr::M867() {
10531053
const int8_t onoff = parser.seenval('S') ? parser.value_int() : -1;
10541054

10551055
if (I2CPE_idx == 0xFF) {
1056-
LOOP_XYZE(i) {
1056+
LOOP_LOGICAL_AXES(i) {
10571057
if (!I2CPE_anyaxis || parser.seen(axis_codes[i])) {
10581058
const uint8_t idx = idx_from_axis(AxisEnum(i));
10591059
if ((int8_t)idx >= 0) {
@@ -1089,7 +1089,7 @@ void I2CPositionEncodersMgr::M868() {
10891089
const float newThreshold = parser.seenval('T') ? parser.value_float() : -9999;
10901090

10911091
if (I2CPE_idx == 0xFF) {
1092-
LOOP_XYZE(i) {
1092+
LOOP_LOGICAL_AXES(i) {
10931093
if (!I2CPE_anyaxis || parser.seen(axis_codes[i])) {
10941094
const uint8_t idx = idx_from_axis(AxisEnum(i));
10951095
if ((int8_t)idx >= 0) {
@@ -1123,7 +1123,7 @@ void I2CPositionEncodersMgr::M869() {
11231123
if (parse()) return;
11241124

11251125
if (I2CPE_idx == 0xFF) {
1126-
LOOP_XYZE(i) {
1126+
LOOP_LOGICAL_AXES(i) {
11271127
if (!I2CPE_anyaxis || parser.seen(axis_codes[i])) {
11281128
const uint8_t idx = idx_from_axis(AxisEnum(i));
11291129
if ((int8_t)idx >= 0) report_error(idx);

Marlin/src/feature/joystick.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -163,7 +163,7 @@ Joystick joystick;
163163
// norm_jog values of [-1 .. 1] maps linearly to [-feedrate .. feedrate]
164164
xyz_float_t move_dist{0};
165165
float hypot2 = 0;
166-
LOOP_XYZ(i) if (norm_jog[i]) {
166+
LOOP_LINEAR_AXES(i) if (norm_jog[i]) {
167167
move_dist[i] = seg_time * norm_jog[i] * TERN(EXTENSIBLE_UI, manual_feedrate_mm_s, planner.settings.max_feedrate_mm_s)[i];
168168
hypot2 += sq(move_dist[i]);
169169
}

Marlin/src/feature/powerloss.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -549,7 +549,7 @@ void PrintJobRecovery::resume() {
549549
TERN_(HAS_HOME_OFFSET, home_offset = info.home_offset);
550550
TERN_(HAS_POSITION_SHIFT, position_shift = info.position_shift);
551551
#if HAS_HOME_OFFSET || HAS_POSITION_SHIFT
552-
LOOP_XYZ(i) update_workspace_offset((AxisEnum)i);
552+
LOOP_LINEAR_AXES(i) update_workspace_offset((AxisEnum)i);
553553
#endif
554554

555555
// Relative axis modes
@@ -581,7 +581,7 @@ void PrintJobRecovery::resume() {
581581
if (info.valid_head) {
582582
if (info.valid_head == info.valid_foot) {
583583
DEBUG_ECHOPGM("current_position: ");
584-
LOOP_XYZE(i) {
584+
LOOP_LOGICAL_AXES(i) {
585585
if (i) DEBUG_CHAR(',');
586586
DEBUG_DECIMAL(info.current_position[i]);
587587
}
@@ -599,7 +599,7 @@ void PrintJobRecovery::resume() {
599599

600600
#if HAS_HOME_OFFSET
601601
DEBUG_ECHOPGM("home_offset: ");
602-
LOOP_XYZ(i) {
602+
LOOP_LINEAR_AXES(i) {
603603
if (i) DEBUG_CHAR(',');
604604
DEBUG_DECIMAL(info.home_offset[i]);
605605
}
@@ -608,7 +608,7 @@ void PrintJobRecovery::resume() {
608608

609609
#if HAS_POSITION_SHIFT
610610
DEBUG_ECHOPGM("position_shift: ");
611-
LOOP_XYZ(i) {
611+
LOOP_LINEAR_AXES(i) {
612612
if (i) DEBUG_CHAR(',');
613613
DEBUG_DECIMAL(info.position_shift[i]);
614614
}

Marlin/src/gcode/calibrate/G28.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -220,7 +220,7 @@ void GcodeSuite::G28() {
220220

221221
#if ENABLED(MARLIN_DEV_MODE)
222222
if (parser.seen_test('S')) {
223-
LOOP_XYZ(a) set_axis_is_at_home((AxisEnum)a);
223+
LOOP_LINEAR_AXES(a) set_axis_is_at_home((AxisEnum)a);
224224
sync_plan_position();
225225
SERIAL_ECHOLNPGM("Simulated Homing");
226226
report_current_position();

Marlin/src/gcode/calibrate/G33.cpp

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -347,7 +347,7 @@ static float auto_tune_a() {
347347
abc_float_t delta_e = { 0.0f }, delta_t = { 0.0f };
348348

349349
delta_t.reset();
350-
LOOP_XYZ(axis) {
350+
LOOP_LINEAR_AXES(axis) {
351351
delta_t[axis] = diff;
352352
calc_kinematics_diff_probe_points(z_pt, delta_e, delta_r, delta_t);
353353
delta_t[axis] = 0;
@@ -525,7 +525,7 @@ void GcodeSuite::G33() {
525525

526526
case 1:
527527
test_precision = 0.0f; // forced end
528-
LOOP_XYZ(axis) e_delta[axis] = +Z4(CEN);
528+
LOOP_LINEAR_AXES(axis) e_delta[axis] = +Z4(CEN);
529529
break;
530530

531531
case 2:
@@ -573,14 +573,14 @@ void GcodeSuite::G33() {
573573
// Normalize angles to least-squares
574574
if (_angle_results) {
575575
float a_sum = 0.0f;
576-
LOOP_XYZ(axis) a_sum += delta_tower_angle_trim[axis];
577-
LOOP_XYZ(axis) delta_tower_angle_trim[axis] -= a_sum / 3.0f;
576+
LOOP_LINEAR_AXES(axis) a_sum += delta_tower_angle_trim[axis];
577+
LOOP_LINEAR_AXES(axis) delta_tower_angle_trim[axis] -= a_sum / 3.0f;
578578
}
579579

580580
// adjust delta_height and endstops by the max amount
581581
const float z_temp = _MAX(delta_endstop_adj.a, delta_endstop_adj.b, delta_endstop_adj.c);
582582
delta_height -= z_temp;
583-
LOOP_XYZ(axis) delta_endstop_adj[axis] -= z_temp;
583+
LOOP_LINEAR_AXES(axis) delta_endstop_adj[axis] -= z_temp;
584584
}
585585
recalc_delta_settings();
586586
NOMORE(zero_std_dev_min, zero_std_dev);

0 commit comments

Comments
 (0)