Procházet zdrojové kódy

Macros to loop over axes

Scott Lahteine před 8 roky
rodič
revize
4e67a85a5d
4 změnil soubory, kde provedl 32 přidání a 30 odebrání
  1. 20
    21
      Marlin/Marlin_main.cpp
  2. 1
    1
      Marlin/configuration_store.cpp
  3. 3
    0
      Marlin/enum.h
  4. 8
    8
      Marlin/planner.cpp

+ 20
- 21
Marlin/Marlin_main.cpp Zobrazit soubor

@@ -1524,8 +1524,7 @@ static void set_axis_is_at_home(AxisEnum axis) {
1524 1524
     if (axis == X_AXIS || axis == Y_AXIS) {
1525 1525
 
1526 1526
       float homeposition[3];
1527
-      for (uint8_t i = X_AXIS; i <= Z_AXIS; i++)
1528
-        homeposition[i] = LOGICAL_POSITION(base_home_pos(i), i);
1527
+      LOOP_XYZ(i) homeposition[i] = LOGICAL_POSITION(base_home_pos(i), i);
1529 1528
 
1530 1529
       // SERIAL_ECHOPGM("homeposition[x]= "); SERIAL_ECHO(homeposition[0]);
1531 1530
       // SERIAL_ECHOPGM("homeposition[y]= "); SERIAL_ECHOLN(homeposition[1]);
@@ -2597,7 +2596,7 @@ static void homeaxis(AxisEnum axis) {
2597 2596
  *  - Set the feedrate, if included
2598 2597
  */
2599 2598
 void gcode_get_destination() {
2600
-  for (int i = 0; i < NUM_AXIS; i++) {
2599
+  LOOP_XYZE(i) {
2601 2600
     if (code_seen(axis_codes[i]))
2602 2601
       destination[i] = code_value_axis_units(i) + (axis_relative_modes[i] || relative_mode ? current_position[i] : 0);
2603 2602
     else
@@ -3900,7 +3899,7 @@ inline void gcode_G92() {
3900 3899
   if (!didE) stepper.synchronize();
3901 3900
 
3902 3901
   bool didXYZ = false;
3903
-  for (int i = 0; i < NUM_AXIS; i++) {
3902
+  LOOP_XYZE(i) {
3904 3903
     if (code_seen(axis_codes[i])) {
3905 3904
       float p = current_position[i],
3906 3905
             v = code_value_axis_units(i);
@@ -5147,7 +5146,7 @@ inline void gcode_M85() {
5147 5146
  *      (Follows the same syntax as G92)
5148 5147
  */
5149 5148
 inline void gcode_M92() {
5150
-  for (int8_t i = 0; i < NUM_AXIS; i++) {
5149
+  LOOP_XYZE(i) {
5151 5150
     if (code_seen(axis_codes[i])) {
5152 5151
       if (i == E_AXIS) {
5153 5152
         float value = code_value_per_axis_unit(i);
@@ -5339,7 +5338,7 @@ inline void gcode_M200() {
5339 5338
  * M201: Set max acceleration in units/s^2 for print moves (M201 X1000 Y1000)
5340 5339
  */
5341 5340
 inline void gcode_M201() {
5342
-  for (int8_t i = 0; i < NUM_AXIS; i++) {
5341
+  LOOP_XYZE(i) {
5343 5342
     if (code_seen(axis_codes[i])) {
5344 5343
       planner.max_acceleration_mm_per_s2[i] = code_value_axis_units(i);
5345 5344
     }
@@ -5350,7 +5349,7 @@ inline void gcode_M201() {
5350 5349
 
5351 5350
 #if 0 // Not used for Sprinter/grbl gen6
5352 5351
   inline void gcode_M202() {
5353
-    for (int8_t i = 0; i < NUM_AXIS; i++) {
5352
+    LOOP_XYZE(i) {
5354 5353
       if (code_seen(axis_codes[i])) axis_travel_steps_per_sqr_second[i] = code_value_axis_units(i) * planner.axis_steps_per_mm[i];
5355 5354
     }
5356 5355
   }
@@ -5361,7 +5360,7 @@ inline void gcode_M201() {
5361 5360
  * M203: Set maximum feedrate that your machine can sustain (M203 X200 Y200 Z300 E10000) in units/sec
5362 5361
  */
5363 5362
 inline void gcode_M203() {
5364
-  for (int8_t i = 0; i < NUM_AXIS; i++)
5363
+  LOOP_XYZE(i)
5365 5364
     if (code_seen(axis_codes[i]))
5366 5365
       planner.max_feedrate_mm_s[i] = code_value_axis_units(i);
5367 5366
 }
@@ -5421,7 +5420,7 @@ inline void gcode_M205() {
5421 5420
  * M206: Set Additional Homing Offset (X Y Z). SCARA aliases T=X, P=Y
5422 5421
  */
5423 5422
 inline void gcode_M206() {
5424
-  for (int8_t i = X_AXIS; i <= Z_AXIS; i++)
5423
+  LOOP_XYZ(i)
5425 5424
     if (code_seen(axis_codes[i]))
5426 5425
       set_home_offset((AxisEnum)i, code_value_axis_units(i));
5427 5426
 
@@ -5463,7 +5462,7 @@ inline void gcode_M206() {
5463 5462
         SERIAL_ECHOLNPGM(">>> gcode_M666");
5464 5463
       }
5465 5464
     #endif
5466
-    for (int8_t i = X_AXIS; i <= Z_AXIS; i++) {
5465
+    LOOP_XYZ(i) {
5467 5466
       if (code_seen(axis_codes[i])) {
5468 5467
         endstop_adj[i] = code_value_axis_units(i);
5469 5468
         #if ENABLED(DEBUG_LEVELING_FEATURE)
@@ -5955,7 +5954,7 @@ inline void gcode_M303() {
5955 5954
    * M365: SCARA calibration: Scaling factor, X, Y, Z axis
5956 5955
    */
5957 5956
   inline void gcode_M365() {
5958
-    for (int8_t i = X_AXIS; i <= Z_AXIS; i++)
5957
+    LOOP_XYZ(i)
5959 5958
       if (code_seen(axis_codes[i]))
5960 5959
         axis_scaling[i] = code_value_float();
5961 5960
   }
@@ -6155,7 +6154,7 @@ void quickstop_stepper() {
6155 6154
  */
6156 6155
 inline void gcode_M428() {
6157 6156
   bool err = false;
6158
-  for (int8_t i = X_AXIS; i <= Z_AXIS; i++) {
6157
+  LOOP_XYZ(i) {
6159 6158
     if (axis_homed[i]) {
6160 6159
       float base = (current_position[i] > (sw_endstop_min[i] + sw_endstop_max[i]) / 2) ? base_home_pos(i) : 0,
6161 6160
             diff = current_position[i] - LOGICAL_POSITION(base, i);
@@ -6285,7 +6284,7 @@ inline void gcode_M503() {
6285 6284
     float lastpos[NUM_AXIS];
6286 6285
 
6287 6286
     // Save current position of all axes
6288
-    for (uint8_t i = 0; i < NUM_AXIS; i++)
6287
+    LOOP_XYZE(i)
6289 6288
       lastpos[i] = destination[i] = current_position[i];
6290 6289
 
6291 6290
     // Define runplan for move axes
@@ -6506,7 +6505,7 @@ inline void gcode_M503() {
6506 6505
  */
6507 6506
 inline void gcode_M907() {
6508 6507
   #if HAS_DIGIPOTSS
6509
-    for (int i = 0; i < NUM_AXIS; i++)
6508
+    LOOP_XYZE(i)
6510 6509
       if (code_seen(axis_codes[i])) stepper.digipot_current(i, code_value_int());
6511 6510
     if (code_seen('B')) stepper.digipot_current(4, code_value_int());
6512 6511
     if (code_seen('S')) for (int i = 0; i <= 4; i++) stepper.digipot_current(i, code_value_int());
@@ -6522,7 +6521,7 @@ inline void gcode_M907() {
6522 6521
   #endif
6523 6522
   #if ENABLED(DIGIPOT_I2C)
6524 6523
     // this one uses actual amps in floating point
6525
-    for (int i = 0; i < NUM_AXIS; i++) if (code_seen(axis_codes[i])) digipot_i2c_set_current(i, code_value_float());
6524
+    LOOP_XYZE(i) if (code_seen(axis_codes[i])) digipot_i2c_set_current(i, code_value_float());
6526 6525
     // for each additional extruder (named B,C,D,E..., channels 4,5,6,7...)
6527 6526
     for (int i = NUM_AXIS; i < DIGIPOT_I2C_NUM_CHANNELS; i++) if (code_seen('B' + i - (NUM_AXIS))) digipot_i2c_set_current(i, code_value_float());
6528 6527
   #endif
@@ -6531,7 +6530,7 @@ inline void gcode_M907() {
6531 6530
       float dac_percent = code_value_float();
6532 6531
       for (uint8_t i = 0; i <= 4; i++) dac_current_percent(i, dac_percent);
6533 6532
     }
6534
-    for (uint8_t i = 0; i < NUM_AXIS; i++) if (code_seen(axis_codes[i])) dac_current_percent(i, code_value_float());
6533
+    LOOP_XYZE(i) if (code_seen(axis_codes[i])) dac_current_percent(i, code_value_float());
6535 6534
   #endif
6536 6535
 }
6537 6536
 
@@ -6570,7 +6569,7 @@ inline void gcode_M907() {
6570 6569
   // M350 Set microstepping mode. Warning: Steps per unit remains unchanged. S code sets stepping mode for all drivers.
6571 6570
   inline void gcode_M350() {
6572 6571
     if (code_seen('S')) for (int i = 0; i <= 4; i++) stepper.microstep_mode(i, code_value_byte());
6573
-    for (int i = 0; i < NUM_AXIS; i++) if (code_seen(axis_codes[i])) stepper.microstep_mode(i, code_value_byte());
6572
+    LOOP_XYZE(i) if (code_seen(axis_codes[i])) stepper.microstep_mode(i, code_value_byte());
6574 6573
     if (code_seen('B')) stepper.microstep_mode(4, code_value_byte());
6575 6574
     stepper.microstep_readings();
6576 6575
   }
@@ -6582,11 +6581,11 @@ inline void gcode_M907() {
6582 6581
   inline void gcode_M351() {
6583 6582
     if (code_seen('S')) switch (code_value_byte()) {
6584 6583
       case 1:
6585
-        for (int i = 0; i < NUM_AXIS; i++) if (code_seen(axis_codes[i])) stepper.microstep_ms(i, code_value_byte(), -1);
6584
+        LOOP_XYZE(i) if (code_seen(axis_codes[i])) stepper.microstep_ms(i, code_value_byte(), -1);
6586 6585
         if (code_seen('B')) stepper.microstep_ms(4, code_value_byte(), -1);
6587 6586
         break;
6588 6587
       case 2:
6589
-        for (int i = 0; i < NUM_AXIS; i++) if (code_seen(axis_codes[i])) stepper.microstep_ms(i, -1, code_value_byte());
6588
+        LOOP_XYZE(i) if (code_seen(axis_codes[i])) stepper.microstep_ms(i, -1, code_value_byte());
6590 6589
         if (code_seen('B')) stepper.microstep_ms(4, -1, code_value_byte());
6591 6590
         break;
6592 6591
     }
@@ -8013,7 +8012,7 @@ void mesh_line_to_destination(float fr_mm_m, uint8_t x_splits = 0xff, uint8_t y_
8013 8012
 
8014 8013
   inline bool prepare_kinematic_move_to(float target[NUM_AXIS]) {
8015 8014
     float difference[NUM_AXIS];
8016
-    for (int8_t i = 0; i < NUM_AXIS; i++) difference[i] = target[i] - current_position[i];
8015
+    LOOP_XYZE(i) difference[i] = target[i] - current_position[i];
8017 8016
 
8018 8017
     float cartesian_mm = sqrt(sq(difference[X_AXIS]) + sq(difference[Y_AXIS]) + sq(difference[Z_AXIS]));
8019 8018
     if (cartesian_mm < 0.000001) cartesian_mm = abs(difference[E_AXIS]);
@@ -8031,7 +8030,7 @@ void mesh_line_to_destination(float fr_mm_m, uint8_t x_splits = 0xff, uint8_t y_
8031 8030
 
8032 8031
       float fraction = float(s) * inv_steps;
8033 8032
 
8034
-      for (int8_t i = 0; i < NUM_AXIS; i++)
8033
+      LOOP_XYZE(i)
8035 8034
         target[i] = current_position[i] + difference[i] * fraction;
8036 8035
 
8037 8036
       inverse_kinematics(target);

+ 1
- 1
Marlin/configuration_store.cpp Zobrazit soubor

@@ -563,7 +563,7 @@ void Config_ResetDefault() {
563 563
   float tmp1[] = DEFAULT_AXIS_STEPS_PER_UNIT;
564 564
   float tmp2[] = DEFAULT_MAX_FEEDRATE;
565 565
   long tmp3[] = DEFAULT_MAX_ACCELERATION;
566
-  for (uint8_t i = 0; i < NUM_AXIS; i++) {
566
+  LOOP_XYZE(i) {
567 567
     planner.axis_steps_per_mm[i] = tmp1[i];
568 568
     planner.max_feedrate_mm_s[i] = tmp2[i];
569 569
     planner.max_acceleration_mm_per_s2[i] = tmp3[i];

+ 3
- 0
Marlin/enum.h Zobrazit soubor

@@ -45,6 +45,9 @@ enum AxisEnum {
45 45
   Z_HEAD  = 5
46 46
 };
47 47
 
48
+#define LOOP_XYZ(VAR)  for (uint8_t VAR=X_AXIS; VAR<=Z_AXIS; VAR++)
49
+#define LOOP_XYZE(VAR) for (uint8_t VAR=X_AXIS; VAR<=E_AXIS; VAR++)
50
+
48 51
 typedef enum {
49 52
   LINEARUNIT_MM,
50 53
   LINEARUNIT_INCH

+ 8
- 8
Marlin/planner.cpp Zobrazit soubor

@@ -134,7 +134,7 @@ Planner::Planner() { init(); }
134 134
 void Planner::init() {
135 135
   block_buffer_head = block_buffer_tail = 0;
136 136
   memset(position, 0, sizeof(position)); // clear position
137
-  for (int i = 0; i < NUM_AXIS; i++) previous_speed[i] = 0.0;
137
+  LOOP_XYZE(i) previous_speed[i] = 0.0;
138 138
   previous_nominal_speed = 0.0;
139 139
   #if ENABLED(AUTO_BED_LEVELING_FEATURE)
140 140
     bed_level_matrix.set_to_identity();
@@ -423,7 +423,7 @@ void Planner::check_axes_activity() {
423 423
 
424 424
     for (uint8_t b = block_buffer_tail; b != block_buffer_head; b = next_block_index(b)) {
425 425
       block = &block_buffer[b];
426
-      for (int i = 0; i < NUM_AXIS; i++) if (block->steps[i]) axis_active[i]++;
426
+      LOOP_XYZE(i) if (block->steps[i]) axis_active[i]++;
427 427
     }
428 428
   }
429 429
   #if ENABLED(DISABLE_X)
@@ -893,7 +893,7 @@ void Planner::check_axes_activity() {
893 893
   // Calculate and limit speed in mm/sec for each axis
894 894
   float current_speed[NUM_AXIS];
895 895
   float speed_factor = 1.0; //factor <=1 do decrease speed
896
-  for (int i = 0; i < NUM_AXIS; i++) {
896
+  LOOP_XYZE(i) {
897 897
     current_speed[i] = delta_mm[i] * inverse_second;
898 898
     float cs = fabs(current_speed[i]), mf = max_feedrate_mm_s[i];
899 899
     if (cs > mf) speed_factor = min(speed_factor, mf / cs);
@@ -939,7 +939,7 @@ void Planner::check_axes_activity() {
939 939
 
940 940
   // Correct the speed
941 941
   if (speed_factor < 1.0) {
942
-    for (unsigned char i = 0; i < NUM_AXIS; i++) current_speed[i] *= speed_factor;
942
+    LOOP_XYZE(i) current_speed[i] *= speed_factor;
943 943
     block->nominal_speed *= speed_factor;
944 944
     block->nominal_rate *= speed_factor;
945 945
   }
@@ -1051,7 +1051,7 @@ void Planner::check_axes_activity() {
1051 1051
   block->recalculate_flag = true; // Always calculate trapezoid for new block
1052 1052
 
1053 1053
   // Update previous path unit_vector and nominal speed
1054
-  for (int i = 0; i < NUM_AXIS; i++) previous_speed[i] = current_speed[i];
1054
+  LOOP_XYZE(i) previous_speed[i] = current_speed[i];
1055 1055
   previous_nominal_speed = block->nominal_speed;
1056 1056
 
1057 1057
   #if ENABLED(LIN_ADVANCE)
@@ -1098,7 +1098,7 @@ void Planner::check_axes_activity() {
1098 1098
   block_buffer_head = next_buffer_head;
1099 1099
 
1100 1100
   // Update position
1101
-  for (int i = 0; i < NUM_AXIS; i++) position[i] = target[i];
1101
+  LOOP_XYZE(i) position[i] = target[i];
1102 1102
 
1103 1103
   recalculate();
1104 1104
 
@@ -1155,7 +1155,7 @@ void Planner::check_axes_activity() {
1155 1155
     stepper.set_position(nx, ny, nz, ne);
1156 1156
     previous_nominal_speed = 0.0; // Resets planner junction speeds. Assumes start from rest.
1157 1157
 
1158
-    for (int i = 0; i < NUM_AXIS; i++) previous_speed[i] = 0.0;
1158
+    LOOP_XYZE(i) previous_speed[i] = 0.0;
1159 1159
   }
1160 1160
 
1161 1161
 /**
@@ -1168,7 +1168,7 @@ void Planner::set_e_position_mm(const float& e) {
1168 1168
 
1169 1169
 // Recalculate the steps/s^2 acceleration rates, based on the mm/s^2
1170 1170
 void Planner::reset_acceleration_rates() {
1171
-  for (int i = 0; i < NUM_AXIS; i++)
1171
+  LOOP_XYZE(i)
1172 1172
     max_acceleration_steps_per_s2[i] = max_acceleration_mm_per_s2[i] * axis_steps_per_mm[i];
1173 1173
 }
1174 1174
 

Loading…
Zrušit
Uložit