瀏覽代碼

Add LOGICAL_POSITION macro, apply to kinematics

Scott Lahteine 9 年之前
父節點
當前提交
b3eb0c8569
共有 1 個文件被更改,包括 48 次插入44 次删除
  1. 48
    44
      Marlin/Marlin_main.cpp

+ 48
- 44
Marlin/Marlin_main.cpp 查看文件

@@ -330,6 +330,7 @@ float position_shift[3] = { 0 };
330 330
 // Set by M206, M428, or menu item. Saved to EEPROM.
331 331
 float home_offset[3] = { 0 };
332 332
 
333
+#define LOGICAL_POSITION(POS, AXIS) (POS + home_offset[AXIS] + position_shift[AXIS])
333 334
 #define RAW_POSITION(POS, AXIS) (POS - home_offset[AXIS] - position_shift[AXIS])
334 335
 #define RAW_CURRENT_POSITION(AXIS) (RAW_POSITION(current_position[AXIS], AXIS))
335 336
 
@@ -1402,7 +1403,7 @@ XYZ_CONSTS_FROM_CONFIG(signed char, home_dir, HOME_DIR);
1402 1403
 
1403 1404
   static float x_home_pos(int extruder) {
1404 1405
     if (extruder == 0)
1405
-      return base_home_pos(X_AXIS) + home_offset[X_AXIS];
1406
+      return LOGICAL_POSITION(base_home_pos(X_AXIS), X_AXIS);
1406 1407
     else
1407 1408
       /**
1408 1409
        * In dual carriage mode the extruder offset provides an override of the
@@ -1437,7 +1438,7 @@ XYZ_CONSTS_FROM_CONFIG(signed char, home_dir, HOME_DIR);
1437 1438
  * at the same positions relative to the machine.
1438 1439
  */
1439 1440
 static void update_software_endstops(AxisEnum axis) {
1440
-  float offs = home_offset[axis] + position_shift[axis];
1441
+  float offs = LOGICAL_POSITION(0, axis);
1441 1442
 
1442 1443
   #if ENABLED(DUAL_X_CARRIAGE)
1443 1444
     if (axis == X_AXIS) {
@@ -1508,7 +1509,7 @@ static void set_axis_is_at_home(AxisEnum axis) {
1508 1509
       if (active_extruder != 0)
1509 1510
         current_position[X_AXIS] = x_home_pos(active_extruder);
1510 1511
       else
1511
-        current_position[X_AXIS] = base_home_pos(X_AXIS) + home_offset[X_AXIS];
1512
+        current_position[X_AXIS] = LOGICAL_POSITION(base_home_pos(X_AXIS), X_AXIS);
1512 1513
       update_software_endstops(X_AXIS);
1513 1514
       return;
1514 1515
     }
@@ -1519,7 +1520,8 @@ static void set_axis_is_at_home(AxisEnum axis) {
1519 1520
     if (axis == X_AXIS || axis == Y_AXIS) {
1520 1521
 
1521 1522
       float homeposition[3];
1522
-      for (int i = 0; i < 3; i++) homeposition[i] = base_home_pos(i);
1523
+      for (uint8_t i = X_AXIS; i <= Z_AXIS; i++)
1524
+        homeposition[i] = LOGICAL_POSITION(base_home_pos(i), i);
1523 1525
 
1524 1526
       // SERIAL_ECHOPGM("homeposition[x]= "); SERIAL_ECHO(homeposition[0]);
1525 1527
       // SERIAL_ECHOPGM("homeposition[y]= "); SERIAL_ECHOLN(homeposition[1]);
@@ -1529,23 +1531,12 @@ static void set_axis_is_at_home(AxisEnum axis) {
1529 1531
        * and calculates homing offset using forward kinematics
1530 1532
        */
1531 1533
       inverse_kinematics(homeposition);
1532
-
1533
-      // SERIAL_ECHOPGM("base Theta= "); SERIAL_ECHO(delta[X_AXIS]);
1534
-      // SERIAL_ECHOPGM(" base Psi+Theta="); SERIAL_ECHOLN(delta[Y_AXIS]);
1535
-
1536
-      for (int i = 0; i < 2; i++) delta[i] -= home_offset[i];
1537
-
1538
-      // SERIAL_ECHOPGM("addhome X="); SERIAL_ECHO(home_offset[X_AXIS]);
1539
-      // SERIAL_ECHOPGM(" addhome Y="); SERIAL_ECHO(home_offset[Y_AXIS]);
1540
-      // SERIAL_ECHOPGM(" addhome Theta="); SERIAL_ECHO(delta[X_AXIS]);
1541
-      // SERIAL_ECHOPGM(" addhome Psi+Theta="); SERIAL_ECHOLN(delta[Y_AXIS]);
1542
-
1543 1534
       forward_kinematics_SCARA(delta);
1544 1535
 
1545
-      // SERIAL_ECHOPGM("Delta X="); SERIAL_ECHO(delta[X_AXIS]);
1536
+      // SERIAL_ECHOPAIR("Delta X=", delta[X_AXIS]);
1546 1537
       // SERIAL_ECHOPGM(" Delta Y="); SERIAL_ECHOLN(delta[Y_AXIS]);
1547 1538
 
1548
-      current_position[axis] = delta[axis];
1539
+      current_position[axis] = LOGICAL_POSITION(delta[axis], axis);
1549 1540
 
1550 1541
       /**
1551 1542
        * SCARA home positions are based on configuration since the actual
@@ -1557,7 +1548,7 @@ static void set_axis_is_at_home(AxisEnum axis) {
1557 1548
     else
1558 1549
   #endif
1559 1550
   {
1560
-    current_position[axis] = base_home_pos(axis) + home_offset[axis];
1551
+    current_position[axis] = LOGICAL_POSITION(base_home_pos(axis), axis);
1561 1552
     update_software_endstops(axis);
1562 1553
 
1563 1554
     #if HAS_BED_PROBE && Z_HOME_DIR < 0 && DISABLED(Z_MIN_PROBE_ENDSTOP)
@@ -1786,7 +1777,7 @@ static void clean_up_after_endstop_or_probe_move() {
1786 1777
         SERIAL_ECHOLNPGM(")");
1787 1778
       }
1788 1779
     #endif
1789
-    float z_dest = home_offset[Z_AXIS] + z_raise;
1780
+    float z_dest = LOGICAL_POSITION(z_raise, Z_AXIS);
1790 1781
 
1791 1782
     if (zprobe_zoffset < 0)
1792 1783
       z_dest -= zprobe_zoffset;
@@ -2089,7 +2080,7 @@ static void clean_up_after_endstop_or_probe_move() {
2089 2080
   #if ENABLED(DELTA)
2090 2081
     #define SET_Z_FROM_STEPPERS() set_current_from_steppers()
2091 2082
   #else
2092
-    #define SET_Z_FROM_STEPPERS() current_position[Z_AXIS] = stepper.get_axis_position_mm(Z_AXIS)
2083
+    #define SET_Z_FROM_STEPPERS() current_position[Z_AXIS] = LOGICAL_POSITION(stepper.get_axis_position_mm(Z_AXIS), Z_AXIS)
2093 2084
   #endif
2094 2085
 
2095 2086
   // Do a single Z probe and return with current_position[Z_AXIS]
@@ -2958,7 +2949,7 @@ inline void gcode_G28() {
2958 2949
 
2959 2950
       if (home_all_axis || homeX || homeY) {
2960 2951
         // Raise Z before homing any other axes and z is not already high enough (never lower z)
2961
-        destination[Z_AXIS] = home_offset[Z_AXIS] + MIN_Z_HEIGHT_FOR_HOMING;
2952
+        destination[Z_AXIS] = LOGICAL_POSITION(MIN_Z_HEIGHT_FOR_HOMING, Z_AXIS);
2962 2953
         if (destination[Z_AXIS] > current_position[Z_AXIS]) {
2963 2954
 
2964 2955
           #if ENABLED(DEBUG_LEVELING_FEATURE)
@@ -3213,12 +3204,12 @@ inline void gcode_G28() {
3213 3204
     ;
3214 3205
     line_to_current_position();
3215 3206
 
3216
-    current_position[X_AXIS] = x + home_offset[X_AXIS];
3217
-    current_position[Y_AXIS] = y + home_offset[Y_AXIS];
3207
+    current_position[X_AXIS] = LOGICAL_POSITION(x, X_AXIS);
3208
+    current_position[Y_AXIS] = LOGICAL_POSITION(y, Y_AXIS);
3218 3209
     line_to_current_position();
3219 3210
 
3220 3211
     #if Z_RAISE_BETWEEN_PROBINGS > 0 || MIN_Z_HEIGHT_FOR_HOMING > 0
3221
-      current_position[Z_AXIS] = MESH_HOME_SEARCH_Z;
3212
+      current_position[Z_AXIS] = LOGICAL_POSITION(MESH_HOME_SEARCH_Z, Z_AXIS);
3222 3213
       line_to_current_position();
3223 3214
     #endif
3224 3215
 
@@ -3636,14 +3627,14 @@ inline void gcode_G28() {
3636 3627
       #endif
3637 3628
 
3638 3629
       // Probe at 3 arbitrary points
3639
-      float z_at_pt_1 = probe_pt( ABL_PROBE_PT_1_X + home_offset[X_AXIS],
3640
-                                  ABL_PROBE_PT_1_Y + home_offset[Y_AXIS],
3630
+      float z_at_pt_1 = probe_pt( LOGICAL_POSITION(ABL_PROBE_PT_1_X, X_AXIS),
3631
+                                  LOGICAL_POSITION(ABL_PROBE_PT_1_Y, Y_AXIS),
3641 3632
                                   stow_probe_after_each, verbose_level),
3642
-            z_at_pt_2 = probe_pt( ABL_PROBE_PT_2_X + home_offset[X_AXIS],
3643
-                                  ABL_PROBE_PT_2_Y + home_offset[Y_AXIS],
3633
+            z_at_pt_2 = probe_pt( LOGICAL_POSITION(ABL_PROBE_PT_2_X, X_AXIS),
3634
+                                  LOGICAL_POSITION(ABL_PROBE_PT_2_Y, Y_AXIS),
3644 3635
                                   stow_probe_after_each, verbose_level),
3645
-            z_at_pt_3 = probe_pt( ABL_PROBE_PT_3_X + home_offset[X_AXIS],
3646
-                                  ABL_PROBE_PT_3_Y + home_offset[Y_AXIS],
3636
+            z_at_pt_3 = probe_pt( LOGICAL_POSITION(ABL_PROBE_PT_3_X, X_AXIS),
3637
+                                  LOGICAL_POSITION(ABL_PROBE_PT_3_Y, Y_AXIS),
3647 3638
                                   stow_probe_after_each, verbose_level);
3648 3639
 
3649 3640
       if (!dryrun) set_bed_level_equation_3pts(z_at_pt_1, z_at_pt_2, z_at_pt_3);
@@ -5174,9 +5165,9 @@ static void report_current_position() {
5174 5165
     SERIAL_EOL;
5175 5166
 
5176 5167
     SERIAL_PROTOCOLPGM("SCARA Cal - Theta:");
5177
-    SERIAL_PROTOCOL(delta[X_AXIS] + home_offset[X_AXIS]);
5168
+    SERIAL_PROTOCOL(delta[X_AXIS]);
5178 5169
     SERIAL_PROTOCOLPGM("   Psi+Theta (90):");
5179
-    SERIAL_PROTOCOL(delta[Y_AXIS] - delta[X_AXIS] - 90 + home_offset[Y_AXIS]);
5170
+    SERIAL_PROTOCOL(delta[Y_AXIS] - delta[X_AXIS] - 90);
5180 5171
     SERIAL_EOL;
5181 5172
 
5182 5173
     SERIAL_PROTOCOLPGM("SCARA step Cal - Theta:");
@@ -6143,7 +6134,7 @@ inline void gcode_M428() {
6143 6134
   for (int8_t i = X_AXIS; i <= Z_AXIS; i++) {
6144 6135
     if (axis_homed[i]) {
6145 6136
       float base = (current_position[i] > (sw_endstop_min[i] + sw_endstop_max[i]) / 2) ? base_home_pos(i) : 0,
6146
-            diff = current_position[i] - base;
6137
+            diff = current_position[i] - LOGICAL_POSITION(base, i);
6147 6138
       if (diff > -20 && diff < 20) {
6148 6139
         set_home_offset((AxisEnum)i, home_offset[i] - diff);
6149 6140
       }
@@ -7739,6 +7730,12 @@ void clamp_to_software_endstops(float target[3]) {
7739 7730
 
7740 7731
   void inverse_kinematics(const float in_cartesian[3]) {
7741 7732
 
7733
+    const float cartesian[3] = {
7734
+      RAW_POSITION(in_cartesian[X_AXIS], X_AXIS),
7735
+      RAW_POSITION(in_cartesian[Y_AXIS], Y_AXIS),
7736
+      RAW_POSITION(in_cartesian[Z_AXIS], Z_AXIS)
7737
+    };
7738
+
7742 7739
     delta[TOWER_1] = sqrt(delta_diagonal_rod_2_tower_1
7743 7740
                           - sq(delta_tower1_x - cartesian[X_AXIS])
7744 7741
                           - sq(delta_tower1_y - cartesian[Y_AXIS])
@@ -7763,10 +7760,14 @@ void clamp_to_software_endstops(float target[3]) {
7763 7760
   }
7764 7761
 
7765 7762
   float delta_safe_distance_from_top() {
7766
-    float cartesian[3] = { 0 };
7763
+    float cartesian[3] = {
7764
+      LOGICAL_POSITION(0, X_AXIS),
7765
+      LOGICAL_POSITION(0, Y_AXIS),
7766
+      LOGICAL_POSITION(0, Z_AXIS)
7767
+    };
7767 7768
     inverse_kinematics(cartesian);
7768 7769
     float distance = delta[TOWER_3];
7769
-    cartesian[Y_AXIS] = DELTA_PRINTABLE_RADIUS;
7770
+    cartesian[Y_AXIS] = LOGICAL_POSITION(DELTA_PRINTABLE_RADIUS, Y_AXIS);
7770 7771
     inverse_kinematics(cartesian);
7771 7772
     return abs(distance - delta[TOWER_3]);
7772 7773
   }
@@ -7846,8 +7847,8 @@ void clamp_to_software_endstops(float target[3]) {
7846 7847
 
7847 7848
   void set_cartesian_from_steppers() {
7848 7849
     forward_kinematics_DELTA(stepper.get_axis_position_mm(X_AXIS),
7849
-                      stepper.get_axis_position_mm(Y_AXIS),
7850
-                      stepper.get_axis_position_mm(Z_AXIS));
7850
+                             stepper.get_axis_position_mm(Y_AXIS),
7851
+                             stepper.get_axis_position_mm(Z_AXIS));
7851 7852
   }
7852 7853
 
7853 7854
   #if ENABLED(AUTO_BED_LEVELING_FEATURE)
@@ -7858,8 +7859,8 @@ void clamp_to_software_endstops(float target[3]) {
7858 7859
 
7859 7860
       int half = (AUTO_BED_LEVELING_GRID_POINTS - 1) / 2;
7860 7861
       float h1 = 0.001 - half, h2 = half - 0.001,
7861
-            grid_x = max(h1, min(h2, cartesian[X_AXIS] / delta_grid_spacing[0])),
7862
-            grid_y = max(h1, min(h2, cartesian[Y_AXIS] / delta_grid_spacing[1]));
7862
+            grid_x = max(h1, min(h2, RAW_POSITION(cartesian[X_AXIS], X_AXIS) / delta_grid_spacing[0])),
7863
+            grid_y = max(h1, min(h2, RAW_POSITION(cartesian[Y_AXIS], Y_AXIS) / delta_grid_spacing[1]));
7863 7864
       int floor_x = floor(grid_x), floor_y = floor(grid_y);
7864 7865
       float ratio_x = grid_x - floor_x, ratio_y = grid_y - floor_y,
7865 7866
             z1 = bed_level[floor_x + half][floor_y + half],
@@ -7910,6 +7911,9 @@ void set_current_from_steppers() {
7910 7911
     current_position[Y_AXIS] = stepper.get_axis_position_mm(Y_AXIS);
7911 7912
     current_position[Z_AXIS] = stepper.get_axis_position_mm(Z_AXIS);
7912 7913
   #endif
7914
+
7915
+  for (uint8_t i = X_AXIS; i <= Z_AXIS; i++)
7916
+    current_position[i] += LOGICAL_POSITION(0, i);
7913 7917
 }
7914 7918
 
7915 7919
 #if ENABLED(MESH_BED_LEVELING)
@@ -7940,14 +7944,14 @@ void mesh_line_to_destination(float fr_mm_m, uint8_t x_splits = 0xff, uint8_t y_
7940 7944
   int8_t gcx = max(cx1, cx2), gcy = max(cy1, cy2);
7941 7945
   if (cx2 != cx1 && TEST(x_splits, gcx)) {
7942 7946
     memcpy(end, destination, sizeof(end));
7943
-    destination[X_AXIS] = mbl.get_probe_x(gcx) + home_offset[X_AXIS] + position_shift[X_AXIS];
7947
+    destination[X_AXIS] = LOGICAL_POSITION(mbl.get_probe_x(gcx), X_AXIS);
7944 7948
     normalized_dist = (destination[X_AXIS] - current_position[X_AXIS]) / (end[X_AXIS] - current_position[X_AXIS]);
7945 7949
     destination[Y_AXIS] = MBL_SEGMENT_END(Y);
7946 7950
     CBI(x_splits, gcx);
7947 7951
   }
7948 7952
   else if (cy2 != cy1 && TEST(y_splits, gcy)) {
7949 7953
     memcpy(end, destination, sizeof(end));
7950
-    destination[Y_AXIS] = mbl.get_probe_y(gcy) + home_offset[Y_AXIS] + position_shift[Y_AXIS];
7954
+    destination[Y_AXIS] = LOGICAL_POSITION(mbl.get_probe_y(gcy), Y_AXIS);
7951 7955
     normalized_dist = (destination[Y_AXIS] - current_position[Y_AXIS]) / (end[Y_AXIS] - current_position[Y_AXIS]);
7952 7956
     destination[X_AXIS] = MBL_SEGMENT_END(X);
7953 7957
     CBI(y_splits, gcy);
@@ -8362,8 +8366,8 @@ void prepare_move_to_destination() {
8362 8366
     float SCARA_pos[2];
8363 8367
     static float SCARA_C2, SCARA_S2, SCARA_K1, SCARA_K2, SCARA_theta, SCARA_psi;
8364 8368
 
8365
-    SCARA_pos[X_AXIS] = cartesian[X_AXIS] * axis_scaling[X_AXIS] - SCARA_offset_x;  //Translate SCARA to standard X Y
8366
-    SCARA_pos[Y_AXIS] = cartesian[Y_AXIS] * axis_scaling[Y_AXIS] - SCARA_offset_y;  // With scaling factor.
8369
+    SCARA_pos[X_AXIS] = RAW_POSITION(cartesian[X_AXIS], X_AXIS) * axis_scaling[X_AXIS] - SCARA_offset_x;  //Translate SCARA to standard X Y
8370
+    SCARA_pos[Y_AXIS] = RAW_POSITION(cartesian[Y_AXIS], Y_AXIS) * axis_scaling[Y_AXIS] - SCARA_offset_y;  // With scaling factor.
8367 8371
 
8368 8372
     #if (Linkage_1 == Linkage_2)
8369 8373
       SCARA_C2 = ((sq(SCARA_pos[X_AXIS]) + sq(SCARA_pos[Y_AXIS])) / (2 * (float)L1_2)) - 1;
@@ -8381,7 +8385,7 @@ void prepare_move_to_destination() {
8381 8385
 
8382 8386
     delta[X_AXIS] = SCARA_theta * SCARA_RAD2DEG;  // Multiply by 180/Pi  -  theta is support arm angle
8383 8387
     delta[Y_AXIS] = (SCARA_theta + SCARA_psi) * SCARA_RAD2DEG;  //       -  equal to sub arm angle (inverted motor)
8384
-    delta[Z_AXIS] = cartesian[Z_AXIS];
8388
+    delta[Z_AXIS] = RAW_POSITION(cartesian[Z_AXIS], Z_AXIS);
8385 8389
 
8386 8390
     /**
8387 8391
     SERIAL_ECHOPGM("cartesian x="); SERIAL_ECHO(cartesian[X_AXIS]);

Loading…
取消
儲存