瀏覽代碼

Stepper and Endstops as singleton objects

Scott Lahteine 9 年之前
父節點
當前提交
5e4e535ce8
共有 10 個文件被更改,包括 948 次插入760 次删除
  1. 1
    1
      Marlin/Marlin.h
  2. 96
    127
      Marlin/Marlin_main.cpp
  3. 1
    1
      Marlin/cardreader.cpp
  4. 317
    0
      Marlin/endstops.cpp
  5. 94
    0
      Marlin/endstops.h
  6. 4
    4
      Marlin/planner.cpp
  7. 146
    561
      Marlin/stepper.cpp
  8. 285
    62
      Marlin/stepper.h
  9. 1
    1
      Marlin/temperature.cpp
  10. 3
    3
      Marlin/ultralcd.cpp

+ 1
- 1
Marlin/Marlin.h 查看文件

216
  */
216
  */
217
 enum AxisEnum {X_AXIS = 0, A_AXIS = 0, Y_AXIS = 1, B_AXIS = 1, Z_AXIS = 2, C_AXIS = 2, E_AXIS = 3, X_HEAD = 4, Y_HEAD = 5, Z_HEAD = 5};
217
 enum AxisEnum {X_AXIS = 0, A_AXIS = 0, Y_AXIS = 1, B_AXIS = 1, Z_AXIS = 2, C_AXIS = 2, E_AXIS = 3, X_HEAD = 4, Y_HEAD = 5, Z_HEAD = 5};
218
 
218
 
219
-enum EndstopEnum {X_MIN = 0, Y_MIN = 1, Z_MIN = 2, Z_MIN_PROBE = 3, X_MAX = 4, Y_MAX = 5, Z_MAX = 6, Z2_MIN = 7, Z2_MAX = 8};
219
+#define _AXIS(AXIS) AXIS ##_AXIS
220
 
220
 
221
 void enable_all_steppers();
221
 void enable_all_steppers();
222
 void disable_all_steppers();
222
 void disable_all_steppers();

+ 96
- 127
Marlin/Marlin_main.cpp 查看文件

48
 #include "ultralcd.h"
48
 #include "ultralcd.h"
49
 #include "planner.h"
49
 #include "planner.h"
50
 #include "stepper.h"
50
 #include "stepper.h"
51
+#include "endstops.h"
51
 #include "temperature.h"
52
 #include "temperature.h"
52
 #include "cardreader.h"
53
 #include "cardreader.h"
53
 #include "configuration_store.h"
54
 #include "configuration_store.h"
547
   float extrude_min_temp = EXTRUDE_MINTEMP;
548
   float extrude_min_temp = EXTRUDE_MINTEMP;
548
 #endif
549
 #endif
549
 
550
 
550
-#if ENABLED(HAS_Z_MIN_PROBE)
551
-  extern volatile bool z_probe_is_active;
552
-#endif
553
-
554
 #if ENABLED(SDSUPPORT)
551
 #if ENABLED(SDSUPPORT)
555
   #include "SdFatUtil.h"
552
   #include "SdFatUtil.h"
556
   int freeMemory() { return SdFatUtil::FreeRam(); }
553
   int freeMemory() { return SdFatUtil::FreeRam(); }
711
 
708
 
712
    #if HAS_SERVO_ENDSTOPS
709
    #if HAS_SERVO_ENDSTOPS
713
 
710
 
714
-    z_probe_is_active = false;
711
+    endstops.enable_z_probe(false);
715
 
712
 
716
     /**
713
     /**
717
      * Set position of all defined Servo Endstops
714
      * Set position of all defined Servo Endstops
831
     watchdog_init();
828
     watchdog_init();
832
   #endif
829
   #endif
833
 
830
 
834
-  st_init();    // Initialize stepper, this enables interrupts!
831
+  stepper.init();    // Initialize stepper, this enables interrupts!
835
   setup_photpin();
832
   setup_photpin();
836
   servo_init();
833
   servo_init();
837
 
834
 
915
     commands_in_queue--;
912
     commands_in_queue--;
916
     cmd_queue_index_r = (cmd_queue_index_r + 1) % BUFSIZE;
913
     cmd_queue_index_r = (cmd_queue_index_r + 1) % BUFSIZE;
917
   }
914
   }
918
-  checkHitEndstops();
915
+  endstops.report_state();
919
   idle();
916
   idle();
920
 }
917
 }
921
 
918
 
1445
   feedrate_multiplier = 100;
1442
   feedrate_multiplier = 100;
1446
   refresh_cmd_timeout();
1443
   refresh_cmd_timeout();
1447
   #if ENABLED(DEBUG_LEVELING_FEATURE)
1444
   #if ENABLED(DEBUG_LEVELING_FEATURE)
1448
-    if (DEBUGGING(LEVELING)) SERIAL_ECHOLNPGM("setup_for_endstop_move > enable_endstops(true)");
1445
+    if (DEBUGGING(LEVELING)) SERIAL_ECHOLNPGM("setup_for_endstop_move > endstops.enable()");
1449
   #endif
1446
   #endif
1450
-  enable_endstops(true);
1447
+  endstops.enable();
1451
 }
1448
 }
1452
 
1449
 
1453
 #if ENABLED(AUTO_BED_LEVELING_FEATURE)
1450
 #if ENABLED(AUTO_BED_LEVELING_FEATURE)
1553
     #if ENABLED(DELTA)
1550
     #if ENABLED(DELTA)
1554
 
1551
 
1555
       float start_z = current_position[Z_AXIS];
1552
       float start_z = current_position[Z_AXIS];
1556
-      long start_steps = st_get_position(Z_AXIS);
1553
+      long start_steps = stepper.position(Z_AXIS);
1557
 
1554
 
1558
       #if ENABLED(DEBUG_LEVELING_FEATURE)
1555
       #if ENABLED(DEBUG_LEVELING_FEATURE)
1559
         if (DEBUGGING(LEVELING)) SERIAL_ECHOLNPGM("run_z_probe (DELTA) 1");
1556
         if (DEBUGGING(LEVELING)) SERIAL_ECHOLNPGM("run_z_probe (DELTA) 1");
1563
       feedrate = homing_feedrate[Z_AXIS] / 4;
1560
       feedrate = homing_feedrate[Z_AXIS] / 4;
1564
       destination[Z_AXIS] = -10;
1561
       destination[Z_AXIS] = -10;
1565
       prepare_move_raw(); // this will also set_current_to_destination
1562
       prepare_move_raw(); // this will also set_current_to_destination
1566
-      st_synchronize();
1567
-      endstops_hit_on_purpose(); // clear endstop hit flags
1563
+      stepper.synchronize();
1564
+      endstops.hit_on_purpose(); // clear endstop hit flags
1568
 
1565
 
1569
       /**
1566
       /**
1570
        * We have to let the planner know where we are right now as it
1567
        * We have to let the planner know where we are right now as it
1571
        * is not where we said to go.
1568
        * is not where we said to go.
1572
        */
1569
        */
1573
-      long stop_steps = st_get_position(Z_AXIS);
1570
+      long stop_steps = stepper.position(Z_AXIS);
1574
       float mm = start_z - float(start_steps - stop_steps) / axis_steps_per_unit[Z_AXIS];
1571
       float mm = start_z - float(start_steps - stop_steps) / axis_steps_per_unit[Z_AXIS];
1575
       current_position[Z_AXIS] = mm;
1572
       current_position[Z_AXIS] = mm;
1576
 
1573
 
1588
       // Move down until the Z probe (or endstop?) is triggered
1585
       // Move down until the Z probe (or endstop?) is triggered
1589
       float zPosition = -(Z_MAX_LENGTH + 10);
1586
       float zPosition = -(Z_MAX_LENGTH + 10);
1590
       line_to_z(zPosition);
1587
       line_to_z(zPosition);
1591
-      st_synchronize();
1588
+      stepper.synchronize();
1592
 
1589
 
1593
       // Tell the planner where we ended up - Get this from the stepper handler
1590
       // Tell the planner where we ended up - Get this from the stepper handler
1594
-      zPosition = st_get_axis_position_mm(Z_AXIS);
1591
+      zPosition = stepper.get_axis_position_mm(Z_AXIS);
1595
       plan_set_position(
1592
       plan_set_position(
1596
         current_position[X_AXIS], current_position[Y_AXIS], zPosition,
1593
         current_position[X_AXIS], current_position[Y_AXIS], zPosition,
1597
         current_position[E_AXIS]
1594
         current_position[E_AXIS]
1600
       // move up the retract distance
1597
       // move up the retract distance
1601
       zPosition += home_bump_mm(Z_AXIS);
1598
       zPosition += home_bump_mm(Z_AXIS);
1602
       line_to_z(zPosition);
1599
       line_to_z(zPosition);
1603
-      st_synchronize();
1604
-      endstops_hit_on_purpose(); // clear endstop hit flags
1600
+      stepper.synchronize();
1601
+      endstops.hit_on_purpose(); // clear endstop hit flags
1605
 
1602
 
1606
       // move back down slowly to find bed
1603
       // move back down slowly to find bed
1607
       set_homing_bump_feedrate(Z_AXIS);
1604
       set_homing_bump_feedrate(Z_AXIS);
1608
 
1605
 
1609
       zPosition -= home_bump_mm(Z_AXIS) * 2;
1606
       zPosition -= home_bump_mm(Z_AXIS) * 2;
1610
       line_to_z(zPosition);
1607
       line_to_z(zPosition);
1611
-      st_synchronize();
1612
-      endstops_hit_on_purpose(); // clear endstop hit flags
1608
+      stepper.synchronize();
1609
+      endstops.hit_on_purpose(); // clear endstop hit flags
1613
 
1610
 
1614
       // Get the current stepper position after bumping an endstop
1611
       // Get the current stepper position after bumping an endstop
1615
-      current_position[Z_AXIS] = st_get_axis_position_mm(Z_AXIS);
1612
+      current_position[Z_AXIS] = stepper.get_axis_position_mm(Z_AXIS);
1616
       sync_plan_position();
1613
       sync_plan_position();
1617
 
1614
 
1618
       #if ENABLED(DEBUG_LEVELING_FEATURE)
1615
       #if ENABLED(DEBUG_LEVELING_FEATURE)
1641
       destination[Y_AXIS] = y;
1638
       destination[Y_AXIS] = y;
1642
       destination[Z_AXIS] = z;
1639
       destination[Z_AXIS] = z;
1643
       prepare_move_raw(); // this will also set_current_to_destination
1640
       prepare_move_raw(); // this will also set_current_to_destination
1644
-      st_synchronize();
1641
+      stepper.synchronize();
1645
 
1642
 
1646
     #else
1643
     #else
1647
 
1644
 
1649
 
1646
 
1650
       current_position[Z_AXIS] = z;
1647
       current_position[Z_AXIS] = z;
1651
       line_to_current_position();
1648
       line_to_current_position();
1652
-      st_synchronize();
1649
+      stepper.synchronize();
1653
 
1650
 
1654
       feedrate = xy_travel_speed;
1651
       feedrate = xy_travel_speed;
1655
 
1652
 
1656
       current_position[X_AXIS] = x;
1653
       current_position[X_AXIS] = x;
1657
       current_position[Y_AXIS] = y;
1654
       current_position[Y_AXIS] = y;
1658
       line_to_current_position();
1655
       line_to_current_position();
1659
-      st_synchronize();
1656
+      stepper.synchronize();
1660
 
1657
 
1661
     #endif
1658
     #endif
1662
 
1659
 
1681
 
1678
 
1682
   static void clean_up_after_endstop_move() {
1679
   static void clean_up_after_endstop_move() {
1683
     #if ENABLED(DEBUG_LEVELING_FEATURE)
1680
     #if ENABLED(DEBUG_LEVELING_FEATURE)
1684
-      if (DEBUGGING(LEVELING)) SERIAL_ECHOLNPGM("clean_up_after_endstop_move > ENDSTOPS_ONLY_FOR_HOMING > endstops_not_homing()");
1681
+      if (DEBUGGING(LEVELING)) SERIAL_ECHOLNPGM("clean_up_after_endstop_move > ENDSTOPS_ONLY_FOR_HOMING > endstops.not_homing()");
1685
     #endif
1682
     #endif
1686
-    endstops_not_homing();
1683
+    endstops.not_homing();
1687
     feedrate = saved_feedrate;
1684
     feedrate = saved_feedrate;
1688
     feedrate_multiplier = saved_feedrate_multiplier;
1685
     feedrate_multiplier = saved_feedrate_multiplier;
1689
     refresh_cmd_timeout();
1686
     refresh_cmd_timeout();
1697
       if (DEBUGGING(LEVELING)) DEBUG_POS("deploy_z_probe", current_position);
1694
       if (DEBUGGING(LEVELING)) DEBUG_POS("deploy_z_probe", current_position);
1698
     #endif
1695
     #endif
1699
 
1696
 
1700
-    if (z_probe_is_active) return;
1697
+    if (endstops.z_probe_enabled) return;
1701
 
1698
 
1702
     #if HAS_SERVO_ENDSTOPS
1699
     #if HAS_SERVO_ENDSTOPS
1703
 
1700
 
1757
       destination[Y_AXIS] = destination[Y_AXIS] * 0.75;
1754
       destination[Y_AXIS] = destination[Y_AXIS] * 0.75;
1758
       prepare_move_raw(); // this will also set_current_to_destination
1755
       prepare_move_raw(); // this will also set_current_to_destination
1759
 
1756
 
1760
-      st_synchronize();
1757
+      stepper.synchronize();
1761
 
1758
 
1762
       #if ENABLED(Z_MIN_PROBE_ENDSTOP)
1759
       #if ENABLED(Z_MIN_PROBE_ENDSTOP)
1763
         z_probe_endstop = (READ(Z_MIN_PROBE_PIN) != Z_MIN_PROBE_ENDSTOP_INVERTING);
1760
         z_probe_endstop = (READ(Z_MIN_PROBE_PIN) != Z_MIN_PROBE_ENDSTOP_INVERTING);
1778
     #endif // Z_PROBE_ALLEN_KEY
1775
     #endif // Z_PROBE_ALLEN_KEY
1779
 
1776
 
1780
     #if ENABLED(FIX_MOUNTED_PROBE)
1777
     #if ENABLED(FIX_MOUNTED_PROBE)
1781
-      // Noting to be done. Just set z_probe_is_active
1778
+      // Noting to be done. Just set endstops.z_probe_enabled
1782
     #endif
1779
     #endif
1783
 
1780
 
1784
-    z_probe_is_active = true;
1781
+    endstops.enable_z_probe();
1785
 
1782
 
1786
   }
1783
   }
1787
 
1784
 
1793
       if (DEBUGGING(LEVELING)) DEBUG_POS("stow_z_probe", current_position);
1790
       if (DEBUGGING(LEVELING)) DEBUG_POS("stow_z_probe", current_position);
1794
     #endif
1791
     #endif
1795
 
1792
 
1796
-    if (!z_probe_is_active) return;
1793
+    if (!endstops.z_probe_enabled) return;
1797
 
1794
 
1798
     #if HAS_SERVO_ENDSTOPS
1795
     #if HAS_SERVO_ENDSTOPS
1799
 
1796
 
1811
               }
1808
               }
1812
             #endif
1809
             #endif
1813
             raise_z_after_probing(); // this also updates current_position
1810
             raise_z_after_probing(); // this also updates current_position
1814
-            st_synchronize();
1811
+            stepper.synchronize();
1815
           }
1812
           }
1816
         #endif
1813
         #endif
1817
 
1814
 
1861
       destination[Y_AXIS] = 0;
1858
       destination[Y_AXIS] = 0;
1862
       prepare_move_raw(); // this will also set_current_to_destination
1859
       prepare_move_raw(); // this will also set_current_to_destination
1863
 
1860
 
1864
-      st_synchronize();
1861
+      stepper.synchronize();
1865
 
1862
 
1866
       #if ENABLED(Z_MIN_PROBE_ENDSTOP)
1863
       #if ENABLED(Z_MIN_PROBE_ENDSTOP)
1867
         bool z_probe_endstop = (READ(Z_MIN_PROBE_PIN) != Z_MIN_PROBE_ENDSTOP_INVERTING);
1864
         bool z_probe_endstop = (READ(Z_MIN_PROBE_PIN) != Z_MIN_PROBE_ENDSTOP_INVERTING);
1881
     #endif // Z_PROBE_ALLEN_KEY
1878
     #endif // Z_PROBE_ALLEN_KEY
1882
 
1879
 
1883
     #if ENABLED(FIX_MOUNTED_PROBE)
1880
     #if ENABLED(FIX_MOUNTED_PROBE)
1884
-      // Nothing to do here. Just clear z_probe_is_active
1881
+      // Nothing to do here. Just clear endstops.z_probe_enabled
1885
     #endif
1882
     #endif
1886
 
1883
 
1887
-    z_probe_is_active = false;
1884
+    endstops.enable_z_probe(false);
1888
   }
1885
   }
1889
   #endif // HAS_Z_MIN_PROBE
1886
   #endif // HAS_Z_MIN_PROBE
1890
 
1887
 
2081
       }
2078
       }
2082
     #endif
2079
     #endif
2083
 
2080
 
2084
-    if (z_probe_is_active == dock) return;
2085
-
2086
     if (!axis_homed[X_AXIS] || !axis_homed[Y_AXIS]) {
2081
     if (!axis_homed[X_AXIS] || !axis_homed[Y_AXIS]) {
2087
       axis_unhomed_error();
2082
       axis_unhomed_error();
2088
       return;
2083
       return;
2089
     }
2084
     }
2090
 
2085
 
2086
+    if (endstops.z_probe_enabled == !dock) return; // already docked/undocked?
2087
+
2091
     float oldXpos = current_position[X_AXIS]; // save x position
2088
     float oldXpos = current_position[X_AXIS]; // save x position
2092
     if (dock) {
2089
     if (dock) {
2093
       #if Z_RAISE_AFTER_PROBING > 0
2090
       #if Z_RAISE_AFTER_PROBING > 0
2105
     }
2102
     }
2106
     do_blocking_move_to_x(oldXpos); // return to position before docking
2103
     do_blocking_move_to_x(oldXpos); // return to position before docking
2107
 
2104
 
2108
-    z_probe_is_active = dock;
2105
+    endstops.enable_z_probe(!dock); // logically disable docked probe
2109
   }
2106
   }
2110
 
2107
 
2111
 #endif // Z_PROBE_SLED
2108
 #endif // Z_PROBE_SLED
2167
       // Engage an X or Y Servo endstop if enabled
2164
       // Engage an X or Y Servo endstop if enabled
2168
       if (_Z_SERVO_TEST && servo_endstop_id[axis] >= 0) {
2165
       if (_Z_SERVO_TEST && servo_endstop_id[axis] >= 0) {
2169
         servo[servo_endstop_id[axis]].move(servo_endstop_angle[axis][0]);
2166
         servo[servo_endstop_id[axis]].move(servo_endstop_angle[axis][0]);
2170
-        if (_Z_PROBE_SUBTEST) z_probe_is_active = true;
2167
+        if (_Z_PROBE_SUBTEST) endstops.z_probe_enabled = true;
2171
       }
2168
       }
2172
     #endif
2169
     #endif
2173
 
2170
 
2174
     // Set a flag for Z motor locking
2171
     // Set a flag for Z motor locking
2175
     #if ENABLED(Z_DUAL_ENDSTOPS)
2172
     #if ENABLED(Z_DUAL_ENDSTOPS)
2176
-      if (axis == Z_AXIS) In_Homing_Process(true);
2173
+      if (axis == Z_AXIS) stepper.set_homing_flag(true);
2177
     #endif
2174
     #endif
2178
 
2175
 
2179
     // Move towards the endstop until an endstop is triggered
2176
     // Move towards the endstop until an endstop is triggered
2180
     destination[axis] = 1.5 * max_length(axis) * axis_home_dir;
2177
     destination[axis] = 1.5 * max_length(axis) * axis_home_dir;
2181
     feedrate = homing_feedrate[axis];
2178
     feedrate = homing_feedrate[axis];
2182
     line_to_destination();
2179
     line_to_destination();
2183
-    st_synchronize();
2180
+    stepper.synchronize();
2184
 
2181
 
2185
     // Set the axis position as setup for the move
2182
     // Set the axis position as setup for the move
2186
     current_position[axis] = 0;
2183
     current_position[axis] = 0;
2187
     sync_plan_position();
2184
     sync_plan_position();
2188
 
2185
 
2189
     #if ENABLED(DEBUG_LEVELING_FEATURE)
2186
     #if ENABLED(DEBUG_LEVELING_FEATURE)
2190
-      if (DEBUGGING(LEVELING)) SERIAL_ECHOLNPGM("> enable_endstops(false)");
2187
+      if (DEBUGGING(LEVELING)) SERIAL_ECHOLNPGM("> endstops.enable(false)");
2191
     #endif
2188
     #endif
2192
-    enable_endstops(false); // Disable endstops while moving away
2189
+    endstops.enable(false); // Disable endstops while moving away
2193
 
2190
 
2194
     // Move away from the endstop by the axis HOME_BUMP_MM
2191
     // Move away from the endstop by the axis HOME_BUMP_MM
2195
     destination[axis] = -home_bump_mm(axis) * axis_home_dir;
2192
     destination[axis] = -home_bump_mm(axis) * axis_home_dir;
2196
     line_to_destination();
2193
     line_to_destination();
2197
-    st_synchronize();
2194
+    stepper.synchronize();
2198
 
2195
 
2199
     #if ENABLED(DEBUG_LEVELING_FEATURE)
2196
     #if ENABLED(DEBUG_LEVELING_FEATURE)
2200
-      if (DEBUGGING(LEVELING)) SERIAL_ECHOLNPGM("> enable_endstops(true)");
2197
+      if (DEBUGGING(LEVELING)) SERIAL_ECHOLNPGM("> endstops.enable(true)");
2201
     #endif
2198
     #endif
2202
-    enable_endstops(true); // Enable endstops for next homing move
2199
+    endstops.enable(true); // Enable endstops for next homing move
2203
 
2200
 
2204
     // Slow down the feedrate for the next move
2201
     // Slow down the feedrate for the next move
2205
     set_homing_bump_feedrate(axis);
2202
     set_homing_bump_feedrate(axis);
2207
     // Move slowly towards the endstop until triggered
2204
     // Move slowly towards the endstop until triggered
2208
     destination[axis] = 2 * home_bump_mm(axis) * axis_home_dir;
2205
     destination[axis] = 2 * home_bump_mm(axis) * axis_home_dir;
2209
     line_to_destination();
2206
     line_to_destination();
2210
-    st_synchronize();
2207
+    stepper.synchronize();
2211
 
2208
 
2212
     #if ENABLED(DEBUG_LEVELING_FEATURE)
2209
     #if ENABLED(DEBUG_LEVELING_FEATURE)
2213
       if (DEBUGGING(LEVELING)) DEBUG_POS("> TRIGGER ENDSTOP", current_position);
2210
       if (DEBUGGING(LEVELING)) DEBUG_POS("> TRIGGER ENDSTOP", current_position);
2224
         else
2221
         else
2225
           lockZ1 = (z_endstop_adj < 0);
2222
           lockZ1 = (z_endstop_adj < 0);
2226
 
2223
 
2227
-        if (lockZ1) Lock_z_motor(true); else Lock_z2_motor(true);
2224
+        if (lockZ1) stepper.set_z_lock(true); else stepper.set_z2_lock(true);
2228
         sync_plan_position();
2225
         sync_plan_position();
2229
 
2226
 
2230
         // Move to the adjusted endstop height
2227
         // Move to the adjusted endstop height
2231
         feedrate = homing_feedrate[axis];
2228
         feedrate = homing_feedrate[axis];
2232
         destination[Z_AXIS] = adj;
2229
         destination[Z_AXIS] = adj;
2233
         line_to_destination();
2230
         line_to_destination();
2234
-        st_synchronize();
2231
+        stepper.synchronize();
2235
 
2232
 
2236
-        if (lockZ1) Lock_z_motor(false); else Lock_z2_motor(false);
2237
-        In_Homing_Process(false);
2233
+        if (lockZ1) stepper.set_z_lock(false); else stepper.set_z2_lock(false);
2234
+        stepper.set_homing_flag(false);
2238
       } // Z_AXIS
2235
       } // Z_AXIS
2239
     #endif
2236
     #endif
2240
 
2237
 
2242
       // retrace by the amount specified in endstop_adj
2239
       // retrace by the amount specified in endstop_adj
2243
       if (endstop_adj[axis] * axis_home_dir < 0) {
2240
       if (endstop_adj[axis] * axis_home_dir < 0) {
2244
         #if ENABLED(DEBUG_LEVELING_FEATURE)
2241
         #if ENABLED(DEBUG_LEVELING_FEATURE)
2245
-          if (DEBUGGING(LEVELING)) SERIAL_ECHOLNPGM("> enable_endstops(false)");
2242
+          if (DEBUGGING(LEVELING)) SERIAL_ECHOLNPGM("> endstops.enable(false)");
2246
         #endif
2243
         #endif
2247
-        enable_endstops(false); // Disable endstops while moving away
2244
+        endstops.enable(false); // Disable endstops while moving away
2248
         sync_plan_position();
2245
         sync_plan_position();
2249
         destination[axis] = endstop_adj[axis];
2246
         destination[axis] = endstop_adj[axis];
2250
         #if ENABLED(DEBUG_LEVELING_FEATURE)
2247
         #if ENABLED(DEBUG_LEVELING_FEATURE)
2254
           }
2251
           }
2255
         #endif
2252
         #endif
2256
         line_to_destination();
2253
         line_to_destination();
2257
-        st_synchronize();
2254
+        stepper.synchronize();
2258
         #if ENABLED(DEBUG_LEVELING_FEATURE)
2255
         #if ENABLED(DEBUG_LEVELING_FEATURE)
2259
-          if (DEBUGGING(LEVELING)) SERIAL_ECHOLNPGM("> enable_endstops(true)");
2256
+          if (DEBUGGING(LEVELING)) SERIAL_ECHOLNPGM("> endstops.enable(true)");
2260
         #endif
2257
         #endif
2261
-        enable_endstops(true); // Enable endstops for next homing move
2258
+        endstops.enable(true); // Enable endstops for next homing move
2262
       }
2259
       }
2263
       #if ENABLED(DEBUG_LEVELING_FEATURE)
2260
       #if ENABLED(DEBUG_LEVELING_FEATURE)
2264
         else {
2261
         else {
2280
 
2277
 
2281
     destination[axis] = current_position[axis];
2278
     destination[axis] = current_position[axis];
2282
     feedrate = 0.0;
2279
     feedrate = 0.0;
2283
-    endstops_hit_on_purpose(); // clear endstop hit flags
2280
+    endstops.hit_on_purpose(); // clear endstop hit flags
2284
     axis_known_position[axis] = true;
2281
     axis_known_position[axis] = true;
2285
     axis_homed[axis] = true;
2282
     axis_homed[axis] = true;
2286
 
2283
 
2301
           if (DEBUGGING(LEVELING)) SERIAL_ECHOLNPGM("> SERVO_ENDSTOPS > Stow with servo.move()");
2298
           if (DEBUGGING(LEVELING)) SERIAL_ECHOLNPGM("> SERVO_ENDSTOPS > Stow with servo.move()");
2302
         #endif
2299
         #endif
2303
         servo[servo_endstop_id[axis]].move(servo_endstop_angle[axis][1]);
2300
         servo[servo_endstop_id[axis]].move(servo_endstop_angle[axis][1]);
2304
-        if (_Z_PROBE_SUBTEST) z_probe_is_active = false;
2301
+        if (_Z_PROBE_SUBTEST) endstops.enable_z_probe(false);
2305
       }
2302
       }
2306
     #endif
2303
     #endif
2307
 
2304
 
2499
   if (code_seen('P')) codenum = code_value_long(); // milliseconds to wait
2496
   if (code_seen('P')) codenum = code_value_long(); // milliseconds to wait
2500
   if (code_seen('S')) codenum = code_value() * 1000UL; // seconds to wait
2497
   if (code_seen('S')) codenum = code_value() * 1000UL; // seconds to wait
2501
 
2498
 
2502
-  st_synchronize();
2499
+  stepper.synchronize();
2503
   refresh_cmd_timeout();
2500
   refresh_cmd_timeout();
2504
   codenum += previous_cmd_ms;  // keep track of when we started waiting
2501
   codenum += previous_cmd_ms;  // keep track of when we started waiting
2505
 
2502
 
2551
   #endif
2548
   #endif
2552
 
2549
 
2553
   // Wait for planner moves to finish!
2550
   // Wait for planner moves to finish!
2554
-  st_synchronize();
2551
+  stepper.synchronize();
2555
 
2552
 
2556
   // For auto bed leveling, clear the level matrix
2553
   // For auto bed leveling, clear the level matrix
2557
   #if ENABLED(AUTO_BED_LEVELING_FEATURE)
2554
   #if ENABLED(AUTO_BED_LEVELING_FEATURE)
2594
     for (int i = X_AXIS; i <= Z_AXIS; i++) destination[i] = 3 * (Z_MAX_LENGTH);
2591
     for (int i = X_AXIS; i <= Z_AXIS; i++) destination[i] = 3 * (Z_MAX_LENGTH);
2595
     feedrate = 1.732 * homing_feedrate[X_AXIS];
2592
     feedrate = 1.732 * homing_feedrate[X_AXIS];
2596
     line_to_destination();
2593
     line_to_destination();
2597
-    st_synchronize();
2598
-    endstops_hit_on_purpose(); // clear endstop hit flags
2594
+    stepper.synchronize();
2595
+    endstops.hit_on_purpose(); // clear endstop hit flags
2599
 
2596
 
2600
     // Destination reached
2597
     // Destination reached
2601
     for (int i = X_AXIS; i <= Z_AXIS; i++) current_position[i] = destination[i];
2598
     for (int i = X_AXIS; i <= Z_AXIS; i++) current_position[i] = destination[i];
2643
           }
2640
           }
2644
         #endif
2641
         #endif
2645
         line_to_destination();
2642
         line_to_destination();
2646
-        st_synchronize();
2643
+        stepper.synchronize();
2647
 
2644
 
2648
         /**
2645
         /**
2649
          * Update the current Z position even if it currently not real from
2646
          * Update the current Z position even if it currently not real from
2676
         destination[Y_AXIS] = 1.5 * mly * home_dir(Y_AXIS);
2673
         destination[Y_AXIS] = 1.5 * mly * home_dir(Y_AXIS);
2677
         feedrate = min(homing_feedrate[X_AXIS], homing_feedrate[Y_AXIS]) * sqrt(mlratio * mlratio + 1);
2674
         feedrate = min(homing_feedrate[X_AXIS], homing_feedrate[Y_AXIS]) * sqrt(mlratio * mlratio + 1);
2678
         line_to_destination();
2675
         line_to_destination();
2679
-        st_synchronize();
2676
+        stepper.synchronize();
2680
 
2677
 
2681
         set_axis_is_at_home(X_AXIS);
2678
         set_axis_is_at_home(X_AXIS);
2682
         set_axis_is_at_home(Y_AXIS);
2679
         set_axis_is_at_home(Y_AXIS);
2690
         destination[Y_AXIS] = current_position[Y_AXIS];
2687
         destination[Y_AXIS] = current_position[Y_AXIS];
2691
         line_to_destination();
2688
         line_to_destination();
2692
         feedrate = 0.0;
2689
         feedrate = 0.0;
2693
-        st_synchronize();
2694
-        endstops_hit_on_purpose(); // clear endstop hit flags
2690
+        stepper.synchronize();
2691
+        endstops.hit_on_purpose(); // clear endstop hit flags
2695
 
2692
 
2696
         current_position[X_AXIS] = destination[X_AXIS];
2693
         current_position[X_AXIS] = destination[X_AXIS];
2697
         current_position[Y_AXIS] = destination[Y_AXIS];
2694
         current_position[Y_AXIS] = destination[Y_AXIS];
2784
 
2781
 
2785
             // Move in the XY plane
2782
             // Move in the XY plane
2786
             line_to_destination();
2783
             line_to_destination();
2787
-            st_synchronize();
2784
+            stepper.synchronize();
2788
 
2785
 
2789
             /**
2786
             /**
2790
              * Update the current positions for XY, Z is still at least at
2787
              * Update the current positions for XY, Z is still at least at
2857
   #endif
2854
   #endif
2858
 
2855
 
2859
   #if ENABLED(ENDSTOPS_ONLY_FOR_HOMING)
2856
   #if ENABLED(ENDSTOPS_ONLY_FOR_HOMING)
2860
-    enable_endstops(false);
2857
+    endstops.enable(false);
2861
     #if ENABLED(DEBUG_LEVELING_FEATURE)
2858
     #if ENABLED(DEBUG_LEVELING_FEATURE)
2862
       if (DEBUGGING(LEVELING)) {
2859
       if (DEBUGGING(LEVELING)) {
2863
-        SERIAL_ECHOLNPGM("ENDSTOPS_ONLY_FOR_HOMING enable_endstops(false)");
2860
+        SERIAL_ECHOLNPGM("ENDSTOPS_ONLY_FOR_HOMING endstops.enable(false)");
2864
       }
2861
       }
2865
     #endif
2862
     #endif
2866
   #endif
2863
   #endif
2875
       set_destination_to_current();
2872
       set_destination_to_current();
2876
       feedrate = homing_feedrate[Z_AXIS];
2873
       feedrate = homing_feedrate[Z_AXIS];
2877
       line_to_destination();
2874
       line_to_destination();
2878
-      st_synchronize();
2875
+      stepper.synchronize();
2879
       #if ENABLED(DEBUG_LEVELING_FEATURE)
2876
       #if ENABLED(DEBUG_LEVELING_FEATURE)
2880
         if (DEBUGGING(LEVELING)) DEBUG_POS("mbl_was_active", current_position);
2877
         if (DEBUGGING(LEVELING)) DEBUG_POS("mbl_was_active", current_position);
2881
       #endif
2878
       #endif
2885
   feedrate = saved_feedrate;
2882
   feedrate = saved_feedrate;
2886
   feedrate_multiplier = saved_feedrate_multiplier;
2883
   feedrate_multiplier = saved_feedrate_multiplier;
2887
   refresh_cmd_timeout();
2884
   refresh_cmd_timeout();
2888
-  endstops_hit_on_purpose(); // clear endstop hit flags
2885
+  endstops.hit_on_purpose(); // clear endstop hit flags
2889
 
2886
 
2890
   #if ENABLED(DEBUG_LEVELING_FEATURE)
2887
   #if ENABLED(DEBUG_LEVELING_FEATURE)
2891
     if (DEBUGGING(LEVELING)) {
2888
     if (DEBUGGING(LEVELING)) {
2921
     #endif
2918
     #endif
2922
 
2919
 
2923
     feedrate = saved_feedrate;
2920
     feedrate = saved_feedrate;
2924
-    st_synchronize();
2921
+    stepper.synchronize();
2925
   }
2922
   }
2926
 
2923
 
2927
   /**
2924
   /**
3015
             #endif
3012
             #endif
3016
           ;
3013
           ;
3017
           line_to_current_position();
3014
           line_to_current_position();
3018
-          st_synchronize();
3015
+          stepper.synchronize();
3019
 
3016
 
3020
           // After recording the last point, activate the mbl and home
3017
           // After recording the last point, activate the mbl and home
3021
           SERIAL_PROTOCOLLNPGM("Mesh probing done.");
3018
           SERIAL_PROTOCOLLNPGM("Mesh probing done.");
3240
       deploy_z_probe();
3237
       deploy_z_probe();
3241
     #endif
3238
     #endif
3242
 
3239
 
3243
-    st_synchronize();
3240
+    stepper.synchronize();
3244
 
3241
 
3245
     setup_for_endstop_move();
3242
     setup_for_endstop_move();
3246
 
3243
 
3511
         float x_tmp = current_position[X_AXIS] + X_PROBE_OFFSET_FROM_EXTRUDER,
3508
         float x_tmp = current_position[X_AXIS] + X_PROBE_OFFSET_FROM_EXTRUDER,
3512
               y_tmp = current_position[Y_AXIS] + Y_PROBE_OFFSET_FROM_EXTRUDER,
3509
               y_tmp = current_position[Y_AXIS] + Y_PROBE_OFFSET_FROM_EXTRUDER,
3513
               z_tmp = current_position[Z_AXIS],
3510
               z_tmp = current_position[Z_AXIS],
3514
-              real_z = st_get_axis_position_mm(Z_AXIS);  //get the real Z (since plan_get_position is now correcting the plane)
3511
+              real_z = stepper.get_axis_position_mm(Z_AXIS);  //get the real Z (since plan_get_position is now correcting the plane)
3515
 
3512
 
3516
         #if ENABLED(DEBUG_LEVELING_FEATURE)
3513
         #if ENABLED(DEBUG_LEVELING_FEATURE)
3517
           if (DEBUGGING(LEVELING)) {
3514
           if (DEBUGGING(LEVELING)) {
3588
       #endif
3585
       #endif
3589
       enqueue_and_echo_commands_P(PSTR(Z_PROBE_END_SCRIPT));
3586
       enqueue_and_echo_commands_P(PSTR(Z_PROBE_END_SCRIPT));
3590
       #if ENABLED(HAS_Z_MIN_PROBE)
3587
       #if ENABLED(HAS_Z_MIN_PROBE)
3591
-        z_probe_is_active = false;
3588
+        endstops.enable_z_probe(false);
3592
       #endif
3589
       #endif
3593
-      st_synchronize();
3590
+      stepper.synchronize();
3594
     #endif
3591
     #endif
3595
 
3592
 
3596
     KEEPALIVE_STATE(IN_HANDLER);
3593
     KEEPALIVE_STATE(IN_HANDLER);
3615
       #endif
3612
       #endif
3616
       deploy_z_probe(); // Engage Z Servo endstop if available. Z_PROBE_SLED is missed here.
3613
       deploy_z_probe(); // Engage Z Servo endstop if available. Z_PROBE_SLED is missed here.
3617
 
3614
 
3618
-      st_synchronize();
3615
+      stepper.synchronize();
3619
       // TODO: clear the leveling matrix or the planner will be set incorrectly
3616
       // TODO: clear the leveling matrix or the planner will be set incorrectly
3620
       setup_for_endstop_move(); // Too late. Must be done before deploying.
3617
       setup_for_endstop_move(); // Too late. Must be done before deploying.
3621
 
3618
 
3650
 inline void gcode_G92() {
3647
 inline void gcode_G92() {
3651
   bool didE = code_seen(axis_codes[E_AXIS]);
3648
   bool didE = code_seen(axis_codes[E_AXIS]);
3652
 
3649
 
3653
-  if (!didE) st_synchronize();
3650
+  if (!didE) stepper.synchronize();
3654
 
3651
 
3655
   bool didXYZ = false;
3652
   bool didXYZ = false;
3656
   for (int i = 0; i < NUM_AXIS; i++) {
3653
   for (int i = 0; i < NUM_AXIS; i++) {
3712
     }
3709
     }
3713
 
3710
 
3714
     lcd_ignore_click();
3711
     lcd_ignore_click();
3715
-    st_synchronize();
3712
+    stepper.synchronize();
3716
     refresh_cmd_timeout();
3713
     refresh_cmd_timeout();
3717
     if (codenum > 0) {
3714
     if (codenum > 0) {
3718
       codenum += previous_cmd_ms;  // wait until this time for a click
3715
       codenum += previous_cmd_ms;  // wait until this time for a click
3853
    */
3850
    */
3854
   inline void gcode_M32() {
3851
   inline void gcode_M32() {
3855
     if (card.sdprinting)
3852
     if (card.sdprinting)
3856
-      st_synchronize();
3853
+      stepper.synchronize();
3857
 
3854
 
3858
     char* namestartpos = strchr(current_command_args, '!');  // Find ! to indicate filename string start.
3855
     char* namestartpos = strchr(current_command_args, '!');  // Find ! to indicate filename string start.
3859
     if (!namestartpos)
3856
     if (!namestartpos)
4819
  */
4816
  */
4820
 inline void gcode_M81() {
4817
 inline void gcode_M81() {
4821
   disable_all_heaters();
4818
   disable_all_heaters();
4822
-  finishAndDisableSteppers();
4819
+  stepper.finish_and_disable();
4823
   #if FAN_COUNT > 0
4820
   #if FAN_COUNT > 0
4824
     #if FAN_COUNT > 1
4821
     #if FAN_COUNT > 1
4825
       for (uint8_t i = 0; i < FAN_COUNT; i++) fanSpeeds[i] = 0;
4822
       for (uint8_t i = 0; i < FAN_COUNT; i++) fanSpeeds[i] = 0;
4829
   #endif
4826
   #endif
4830
   delay(1000); // Wait 1 second before switching off
4827
   delay(1000); // Wait 1 second before switching off
4831
   #if HAS_SUICIDE
4828
   #if HAS_SUICIDE
4832
-    st_synchronize();
4829
+    stepper.synchronize();
4833
     suicide();
4830
     suicide();
4834
   #elif HAS_POWER_SWITCH
4831
   #elif HAS_POWER_SWITCH
4835
     OUT_WRITE(PS_ON_PIN, PS_ON_ASLEEP);
4832
     OUT_WRITE(PS_ON_PIN, PS_ON_ASLEEP);
4864
   else {
4861
   else {
4865
     bool all_axis = !((code_seen(axis_codes[X_AXIS])) || (code_seen(axis_codes[Y_AXIS])) || (code_seen(axis_codes[Z_AXIS])) || (code_seen(axis_codes[E_AXIS])));
4862
     bool all_axis = !((code_seen(axis_codes[X_AXIS])) || (code_seen(axis_codes[Y_AXIS])) || (code_seen(axis_codes[Z_AXIS])) || (code_seen(axis_codes[E_AXIS])));
4866
     if (all_axis) {
4863
     if (all_axis) {
4867
-      finishAndDisableSteppers();
4864
+      stepper.finish_and_disable();
4868
     }
4865
     }
4869
     else {
4866
     else {
4870
-      st_synchronize();
4867
+      stepper.synchronize();
4871
       if (code_seen('X')) disable_x();
4868
       if (code_seen('X')) disable_x();
4872
       if (code_seen('Y')) disable_y();
4869
       if (code_seen('Y')) disable_y();
4873
       if (code_seen('Z')) disable_z();
4870
       if (code_seen('Z')) disable_z();
4927
   SERIAL_PROTOCOLPGM(" E:");
4924
   SERIAL_PROTOCOLPGM(" E:");
4928
   SERIAL_PROTOCOL(current_position[E_AXIS]);
4925
   SERIAL_PROTOCOL(current_position[E_AXIS]);
4929
 
4926
 
4930
-  CRITICAL_SECTION_START;
4931
-  extern volatile long count_position[NUM_AXIS];
4932
-  long xpos = count_position[X_AXIS],
4933
-       ypos = count_position[Y_AXIS],
4934
-       zpos = count_position[Z_AXIS];
4935
-  CRITICAL_SECTION_END;
4936
-
4937
-  #if ENABLED(COREXY) || ENABLED(COREXZ)
4938
-    SERIAL_PROTOCOLPGM(MSG_COUNT_A);
4939
-  #else
4940
-    SERIAL_PROTOCOLPGM(MSG_COUNT_X);
4941
-  #endif
4942
-  SERIAL_PROTOCOL(xpos);
4943
-
4944
-  #if ENABLED(COREXY)
4945
-    SERIAL_PROTOCOLPGM(" B:");
4946
-  #else
4947
-    SERIAL_PROTOCOLPGM(" Y:");
4948
-  #endif
4949
-  SERIAL_PROTOCOL(ypos);
4950
-
4951
-  #if ENABLED(COREXZ)
4952
-    SERIAL_PROTOCOLPGM(" C:");
4953
-  #else
4954
-    SERIAL_PROTOCOLPGM(" Z:");
4955
-  #endif
4956
-  SERIAL_PROTOCOL(zpos);
4957
-
4958
-  SERIAL_EOL;
4927
+  stepper.report_positions();
4959
 
4928
 
4960
   #if ENABLED(SCARA)
4929
   #if ENABLED(SCARA)
4961
     SERIAL_PROTOCOLPGM("SCARA Theta:");
4930
     SERIAL_PROTOCOLPGM("SCARA Theta:");
5039
 /**
5008
 /**
5040
  * M120: Enable endstops and set non-homing endstop state to "enabled"
5009
  * M120: Enable endstops and set non-homing endstop state to "enabled"
5041
  */
5010
  */
5042
-inline void gcode_M120() { enable_endstops_globally(true); }
5011
+inline void gcode_M120() { endstops.enable_globally(true); }
5043
 
5012
 
5044
 /**
5013
 /**
5045
  * M121: Disable endstops and set non-homing endstop state to "disabled"
5014
  * M121: Disable endstops and set non-homing endstop state to "disabled"
5046
  */
5015
  */
5047
-inline void gcode_M121() { enable_endstops_globally(false); }
5016
+inline void gcode_M121() { endstops.enable_globally(false); }
5048
 
5017
 
5049
 #if ENABLED(BLINKM)
5018
 #if ENABLED(BLINKM)
5050
 
5019
 
5439
       if (pin_number > -1) {
5408
       if (pin_number > -1) {
5440
         int target = LOW;
5409
         int target = LOW;
5441
 
5410
 
5442
-        st_synchronize();
5411
+        stepper.synchronize();
5443
 
5412
 
5444
         pinMode(pin_number, INPUT);
5413
         pinMode(pin_number, INPUT);
5445
 
5414
 
5801
 /**
5770
 /**
5802
  * M400: Finish all moves
5771
  * M400: Finish all moves
5803
  */
5772
  */
5804
-inline void gcode_M400() { st_synchronize(); }
5773
+inline void gcode_M400() { stepper.synchronize(); }
5805
 
5774
 
5806
 #if ENABLED(AUTO_BED_LEVELING_FEATURE) && DISABLED(Z_PROBE_SLED) && (HAS_SERVO_ENDSTOPS || ENABLED(Z_PROBE_ALLEN_KEY))
5775
 #if ENABLED(AUTO_BED_LEVELING_FEATURE) && DISABLED(Z_PROBE_SLED) && (HAS_SERVO_ENDSTOPS || ENABLED(Z_PROBE_ALLEN_KEY))
5807
 
5776
 
5887
  * This will stop the carriages mid-move, so most likely they
5856
  * This will stop the carriages mid-move, so most likely they
5888
  * will be out of sync with the stepper position after this.
5857
  * will be out of sync with the stepper position after this.
5889
  */
5858
  */
5890
-inline void gcode_M410() { quickStop(); }
5859
+inline void gcode_M410() { stepper.quick_stop(); }
5891
 
5860
 
5892
 
5861
 
5893
 #if ENABLED(MESH_BED_LEVELING)
5862
 #if ENABLED(MESH_BED_LEVELING)
6111
     RUNPLAN;
6080
     RUNPLAN;
6112
 
6081
 
6113
     //finish moves
6082
     //finish moves
6114
-    st_synchronize();
6083
+    stepper.synchronize();
6115
     //disable extruder steppers so filament can be removed
6084
     //disable extruder steppers so filament can be removed
6116
     disable_e0();
6085
     disable_e0();
6117
     disable_e1();
6086
     disable_e1();
6135
         current_position[E_AXIS] += AUTO_FILAMENT_CHANGE_LENGTH;
6104
         current_position[E_AXIS] += AUTO_FILAMENT_CHANGE_LENGTH;
6136
         destination[E_AXIS] = current_position[E_AXIS];
6105
         destination[E_AXIS] = current_position[E_AXIS];
6137
         line_to_destination(AUTO_FILAMENT_CHANGE_FEEDRATE);
6106
         line_to_destination(AUTO_FILAMENT_CHANGE_FEEDRATE);
6138
-        st_synchronize();
6107
+        stepper.synchronize();
6139
       #endif
6108
       #endif
6140
     } // while(!lcd_clicked)
6109
     } // while(!lcd_clicked)
6141
     KEEPALIVE_STATE(IN_HANDLER);
6110
     KEEPALIVE_STATE(IN_HANDLER);
6143
 
6112
 
6144
     #if ENABLED(AUTO_FILAMENT_CHANGE)
6113
     #if ENABLED(AUTO_FILAMENT_CHANGE)
6145
       current_position[E_AXIS] = 0;
6114
       current_position[E_AXIS] = 0;
6146
-      st_synchronize();
6115
+      stepper.synchronize();
6147
     #endif
6116
     #endif
6148
 
6117
 
6149
     //return to normal
6118
     //return to normal
6198
    *    Note: the X axis should be homed after changing dual x-carriage mode.
6167
    *    Note: the X axis should be homed after changing dual x-carriage mode.
6199
    */
6168
    */
6200
   inline void gcode_M605() {
6169
   inline void gcode_M605() {
6201
-    st_synchronize();
6170
+    stepper.synchronize();
6202
     if (code_seen('S')) dual_x_carriage_mode = code_value();
6171
     if (code_seen('S')) dual_x_carriage_mode = code_value();
6203
     switch (dual_x_carriage_mode) {
6172
     switch (dual_x_carriage_mode) {
6204
       case DXC_DUPLICATION_MODE:
6173
       case DXC_DUPLICATION_MODE:
6375
                            current_position[E_AXIS], max_feedrate[X_AXIS], active_extruder);
6344
                            current_position[E_AXIS], max_feedrate[X_AXIS], active_extruder);
6376
           plan_buffer_line(x_home_pos(active_extruder), current_position[Y_AXIS], current_position[Z_AXIS],
6345
           plan_buffer_line(x_home_pos(active_extruder), current_position[Y_AXIS], current_position[Z_AXIS],
6377
                            current_position[E_AXIS], max_feedrate[Z_AXIS], active_extruder);
6346
                            current_position[E_AXIS], max_feedrate[Z_AXIS], active_extruder);
6378
-          st_synchronize();
6347
+          stepper.synchronize();
6379
         }
6348
         }
6380
 
6349
 
6381
         // apply Y & Z extruder offset (x offset is already used in determining home pos)
6350
         // apply Y & Z extruder offset (x offset is already used in determining home pos)
6460
     } // (tmp_extruder != active_extruder)
6429
     } // (tmp_extruder != active_extruder)
6461
 
6430
 
6462
     #if ENABLED(EXT_SOLENOID)
6431
     #if ENABLED(EXT_SOLENOID)
6463
-      st_synchronize();
6432
+      stepper.synchronize();
6464
       disable_all_solenoids();
6433
       disable_all_solenoids();
6465
       enable_solenoid_on_active_extruder();
6434
       enable_solenoid_on_active_extruder();
6466
     #endif // EXT_SOLENOID
6435
     #endif // EXT_SOLENOID
7400
         plan_buffer_line(current_position[X_AXIS] + duplicate_extruder_x_offset,
7369
         plan_buffer_line(current_position[X_AXIS] + duplicate_extruder_x_offset,
7401
                          current_position[Y_AXIS], current_position[Z_AXIS], current_position[E_AXIS], max_feedrate[X_AXIS], 1);
7370
                          current_position[Y_AXIS], current_position[Z_AXIS], current_position[E_AXIS], max_feedrate[X_AXIS], 1);
7402
         sync_plan_position();
7371
         sync_plan_position();
7403
-        st_synchronize();
7372
+        stepper.synchronize();
7404
         extruder_duplication_enabled = true;
7373
         extruder_duplication_enabled = true;
7405
         active_extruder_parked = false;
7374
         active_extruder_parked = false;
7406
       }
7375
       }
7927
       destination[E_AXIS] = oldedes;
7896
       destination[E_AXIS] = oldedes;
7928
       plan_set_e_position(oldepos);
7897
       plan_set_e_position(oldepos);
7929
       previous_cmd_ms = ms; // refresh_cmd_timeout()
7898
       previous_cmd_ms = ms; // refresh_cmd_timeout()
7930
-      st_synchronize();
7899
+      stepper.synchronize();
7931
       switch (active_extruder) {
7900
       switch (active_extruder) {
7932
         case 0:
7901
         case 0:
7933
           E0_ENABLE_WRITE(oldstatus);
7902
           E0_ENABLE_WRITE(oldstatus);
8004
     if (!filament_ran_out) {
7973
     if (!filament_ran_out) {
8005
       filament_ran_out = true;
7974
       filament_ran_out = true;
8006
       enqueue_and_echo_commands_P(PSTR(FILAMENT_RUNOUT_SCRIPT));
7975
       enqueue_and_echo_commands_P(PSTR(FILAMENT_RUNOUT_SCRIPT));
8007
-      st_synchronize();
7976
+      stepper.synchronize();
8008
     }
7977
     }
8009
   }
7978
   }
8010
 
7979
 

+ 1
- 1
Marlin/cardreader.cpp 查看文件

596
 }
596
 }
597
 
597
 
598
 void CardReader::printingHasFinished() {
598
 void CardReader::printingHasFinished() {
599
-  st_synchronize();
599
+  stepper.synchronize();
600
   if (file_subcall_ctr > 0) { // Heading up to a parent file that called current as a procedure.
600
   if (file_subcall_ctr > 0) { // Heading up to a parent file that called current as a procedure.
601
     file.close();
601
     file.close();
602
     file_subcall_ctr--;
602
     file_subcall_ctr--;

+ 317
- 0
Marlin/endstops.cpp 查看文件

1
+/**
2
+ * Marlin 3D Printer Firmware
3
+ * Copyright (C) 2016 MarlinFirmware [https://github.com/MarlinFirmware/Marlin]
4
+ *
5
+ * Based on Sprinter and grbl.
6
+ * Copyright (C) 2011 Camiel Gubbels / Erik van der Zalm
7
+ *
8
+ * This program is free software: you can redistribute it and/or modify
9
+ * it under the terms of the GNU General Public License as published by
10
+ * the Free Software Foundation, either version 3 of the License, or
11
+ * (at your option) any later version.
12
+ *
13
+ * This program is distributed in the hope that it will be useful,
14
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
15
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16
+ * GNU General Public License for more details.
17
+ *
18
+ * You should have received a copy of the GNU General Public License
19
+ * along with this program.  If not, see <http://www.gnu.org/licenses/>.
20
+ *
21
+ */
22
+
23
+/**
24
+ * endstops.cpp - A singleton object to manage endstops
25
+ */
26
+
27
+#include "Marlin.h"
28
+#include "endstops.h"
29
+#include "stepper.h"
30
+#include "ultralcd.h"
31
+
32
+// TEST_ENDSTOP: test the old and the current status of an endstop
33
+#define TEST_ENDSTOP(ENDSTOP) (TEST(current_endstop_bits & old_endstop_bits, ENDSTOP))
34
+
35
+Endstops endstops;
36
+
37
+Endstops::Endstops() {
38
+  enable_globally(ENABLED(ENDSTOPS_ONLY_FOR_HOMING));
39
+  enable(true);
40
+  #if ENABLED(HAS_Z_MIN_PROBE)
41
+    enable_z_probe(false);
42
+  #endif
43
+} // Endstops::Endstops
44
+
45
+void Endstops::init() {
46
+
47
+  #if HAS_X_MIN
48
+    SET_INPUT(X_MIN_PIN);
49
+    #if ENABLED(ENDSTOPPULLUP_XMIN)
50
+      WRITE(X_MIN_PIN,HIGH);
51
+    #endif
52
+  #endif
53
+
54
+  #if HAS_Y_MIN
55
+    SET_INPUT(Y_MIN_PIN);
56
+    #if ENABLED(ENDSTOPPULLUP_YMIN)
57
+      WRITE(Y_MIN_PIN,HIGH);
58
+    #endif
59
+  #endif
60
+
61
+  #if HAS_Z_MIN
62
+    SET_INPUT(Z_MIN_PIN);
63
+    #if ENABLED(ENDSTOPPULLUP_ZMIN)
64
+      WRITE(Z_MIN_PIN,HIGH);
65
+    #endif
66
+  #endif
67
+
68
+  #if HAS_Z2_MIN
69
+    SET_INPUT(Z2_MIN_PIN);
70
+    #if ENABLED(ENDSTOPPULLUP_ZMIN)
71
+      WRITE(Z2_MIN_PIN,HIGH);
72
+    #endif
73
+  #endif
74
+
75
+  #if HAS_X_MAX
76
+    SET_INPUT(X_MAX_PIN);
77
+    #if ENABLED(ENDSTOPPULLUP_XMAX)
78
+      WRITE(X_MAX_PIN,HIGH);
79
+    #endif
80
+  #endif
81
+
82
+  #if HAS_Y_MAX
83
+    SET_INPUT(Y_MAX_PIN);
84
+    #if ENABLED(ENDSTOPPULLUP_YMAX)
85
+      WRITE(Y_MAX_PIN,HIGH);
86
+    #endif
87
+  #endif
88
+
89
+  #if HAS_Z_MAX
90
+    SET_INPUT(Z_MAX_PIN);
91
+    #if ENABLED(ENDSTOPPULLUP_ZMAX)
92
+      WRITE(Z_MAX_PIN,HIGH);
93
+    #endif
94
+  #endif
95
+
96
+  #if HAS_Z2_MAX
97
+    SET_INPUT(Z2_MAX_PIN);
98
+    #if ENABLED(ENDSTOPPULLUP_ZMAX)
99
+      WRITE(Z2_MAX_PIN,HIGH);
100
+    #endif
101
+  #endif
102
+
103
+  #if HAS_Z_PROBE && ENABLED(Z_MIN_PROBE_ENDSTOP) // Check for Z_MIN_PROBE_ENDSTOP so we don't pull a pin high unless it's to be used.
104
+    SET_INPUT(Z_MIN_PROBE_PIN);
105
+    #if ENABLED(ENDSTOPPULLUP_ZMIN_PROBE)
106
+      WRITE(Z_MIN_PROBE_PIN,HIGH);
107
+    #endif
108
+  #endif
109
+
110
+} // Endstops::init
111
+
112
+void Endstops::report_state() {
113
+  if (endstop_hit_bits) {
114
+    #if ENABLED(ULTRA_LCD)
115
+      char chrX = ' ', chrY = ' ', chrZ = ' ', chrP = ' ';
116
+      #define _SET_STOP_CHAR(A,C) (chr## A = C)
117
+    #else
118
+      #define _SET_STOP_CHAR(A,C) ;
119
+    #endif
120
+
121
+    #define _ENDSTOP_HIT_ECHO(A,C) do{ \
122
+      SERIAL_ECHOPAIR(" " STRINGIFY(A) ":", stepper.triggered_position_mm(A ##_AXIS)); \
123
+      _SET_STOP_CHAR(A,C); }while(0)
124
+
125
+    #define _ENDSTOP_HIT_TEST(A,C) \
126
+      if (TEST(endstop_hit_bits, A ##_MIN) || TEST(endstop_hit_bits, A ##_MAX)) \
127
+        _ENDSTOP_HIT_ECHO(A,C)
128
+
129
+    SERIAL_ECHO_START;
130
+    SERIAL_ECHOPGM(MSG_ENDSTOPS_HIT);
131
+    _ENDSTOP_HIT_TEST(X, 'X');
132
+    _ENDSTOP_HIT_TEST(Y, 'Y');
133
+    _ENDSTOP_HIT_TEST(Z, 'Z');
134
+
135
+    #if ENABLED(Z_MIN_PROBE_ENDSTOP)
136
+      #define P_AXIS Z_AXIS
137
+      if (TEST(endstop_hit_bits, Z_MIN_PROBE)) _ENDSTOP_HIT_ECHO(P, 'P');
138
+    #endif
139
+    SERIAL_EOL;
140
+
141
+    #if ENABLED(ULTRA_LCD)
142
+      char msg[3 * strlen(MSG_LCD_ENDSTOPS) + 8 + 1]; // Room for a UTF 8 string
143
+      sprintf_P(msg, PSTR(MSG_LCD_ENDSTOPS " %c %c %c %c"), chrX, chrY, chrZ, chrP);
144
+      lcd_setstatus(msg);
145
+    #endif
146
+
147
+    hit_on_purpose();
148
+
149
+    #if ENABLED(ABORT_ON_ENDSTOP_HIT_FEATURE_ENABLED) && ENABLED(SDSUPPORT)
150
+      if (abort_on_endstop_hit) {
151
+        card.sdprinting = false;
152
+        card.closefile();
153
+        stepper.quick_stop();
154
+        disable_all_heaters(); // switch off all heaters.
155
+      }
156
+    #endif
157
+  }
158
+} // Endstops::report_state
159
+
160
+// Check endstops - Called from ISR!
161
+void Endstops::update() {
162
+
163
+  #define _ENDSTOP_PIN(AXIS, MINMAX) AXIS ##_## MINMAX ##_PIN
164
+  #define _ENDSTOP_INVERTING(AXIS, MINMAX) AXIS ##_## MINMAX ##_ENDSTOP_INVERTING
165
+  #define _ENDSTOP_HIT(AXIS) SBI(endstop_hit_bits, _ENDSTOP(AXIS, MIN))
166
+  #define _ENDSTOP(AXIS, MINMAX) AXIS ##_## MINMAX
167
+
168
+  // UPDATE_ENDSTOP_BIT: set the current endstop bits for an endstop to its status
169
+  #define UPDATE_ENDSTOP_BIT(AXIS, MINMAX) SET_BIT(current_endstop_bits, _ENDSTOP(AXIS, MINMAX), (READ(_ENDSTOP_PIN(AXIS, MINMAX)) != _ENDSTOP_INVERTING(AXIS, MINMAX)))
170
+  // COPY_BIT: copy the value of COPY_BIT to BIT in bits
171
+  #define COPY_BIT(bits, COPY_BIT, BIT) SET_BIT(bits, BIT, TEST(bits, COPY_BIT))
172
+
173
+  #define UPDATE_ENDSTOP(AXIS,MINMAX) do { \
174
+      UPDATE_ENDSTOP_BIT(AXIS, MINMAX); \
175
+      if (TEST_ENDSTOP(_ENDSTOP(AXIS, MINMAX)) && stepper.current_block->steps[_AXIS(AXIS)] > 0) { \
176
+        _ENDSTOP_HIT(AXIS); \
177
+        stepper.endstop_triggered(_AXIS(AXIS)); \
178
+      } \
179
+    } while(0)
180
+
181
+  #if ENABLED(COREXY) || ENABLED(COREXZ)
182
+    // Head direction in -X axis for CoreXY and CoreXZ bots.
183
+    // If Delta1 == -Delta2, the movement is only in Y or Z axis
184
+    if ((stepper.current_block->steps[A_AXIS] != stepper.current_block->steps[CORE_AXIS_2]) || (stepper.motor_direction(A_AXIS) == stepper.motor_direction(CORE_AXIS_2))) {
185
+      if (stepper.motor_direction(X_HEAD))
186
+  #else
187
+    if (stepper.motor_direction(X_AXIS))   // stepping along -X axis (regular Cartesian bot)
188
+  #endif
189
+      { // -direction
190
+        #if ENABLED(DUAL_X_CARRIAGE)
191
+          // with 2 x-carriages, endstops are only checked in the homing direction for the active extruder
192
+          if ((stepper.current_block->active_extruder == 0 && X_HOME_DIR == -1) || (stepper.current_block->active_extruder != 0 && X2_HOME_DIR == -1))
193
+        #endif
194
+          {
195
+            #if HAS_X_MIN
196
+              UPDATE_ENDSTOP(X, MIN);
197
+            #endif
198
+          }
199
+      }
200
+      else { // +direction
201
+        #if ENABLED(DUAL_X_CARRIAGE)
202
+          // with 2 x-carriages, endstops are only checked in the homing direction for the active extruder
203
+          if ((stepper.current_block->active_extruder == 0 && X_HOME_DIR == 1) || (stepper.current_block->active_extruder != 0 && X2_HOME_DIR == 1))
204
+        #endif
205
+          {
206
+            #if HAS_X_MAX
207
+              UPDATE_ENDSTOP(X, MAX);
208
+            #endif
209
+          }
210
+      }
211
+  #if ENABLED(COREXY) || ENABLED(COREXZ)
212
+    }
213
+  #endif
214
+
215
+  #if ENABLED(COREXY)
216
+    // Head direction in -Y axis for CoreXY bots.
217
+    // If DeltaX == DeltaY, the movement is only in X axis
218
+    if ((stepper.current_block->steps[A_AXIS] != stepper.current_block->steps[B_AXIS]) || (stepper.motor_direction(A_AXIS) != stepper.motor_direction(B_AXIS))) {
219
+      if (stepper.motor_direction(Y_HEAD))
220
+  #else
221
+      if (stepper.motor_direction(Y_AXIS))   // -direction
222
+  #endif
223
+      { // -direction
224
+        #if HAS_Y_MIN
225
+          UPDATE_ENDSTOP(Y, MIN);
226
+        #endif
227
+      }
228
+      else { // +direction
229
+        #if HAS_Y_MAX
230
+          UPDATE_ENDSTOP(Y, MAX);
231
+        #endif
232
+      }
233
+  #if ENABLED(COREXY)
234
+    }
235
+  #endif
236
+
237
+  #if ENABLED(COREXZ)
238
+    // Head direction in -Z axis for CoreXZ bots.
239
+    // If DeltaX == DeltaZ, the movement is only in X axis
240
+    if ((stepper.current_block->steps[A_AXIS] != stepper.current_block->steps[C_AXIS]) || (stepper.motor_direction(A_AXIS) != stepper.motor_direction(C_AXIS))) {
241
+      if (stepper.motor_direction(Z_HEAD))
242
+  #else
243
+      if (stepper.motor_direction(Z_AXIS))
244
+  #endif
245
+      { // z -direction
246
+        #if HAS_Z_MIN
247
+
248
+          #if ENABLED(Z_DUAL_ENDSTOPS)
249
+
250
+            UPDATE_ENDSTOP_BIT(Z, MIN);
251
+            #if HAS_Z2_MIN
252
+              UPDATE_ENDSTOP_BIT(Z2, MIN);
253
+            #else
254
+              COPY_BIT(current_endstop_bits, Z_MIN, Z2_MIN);
255
+            #endif
256
+
257
+            byte z_test = TEST_ENDSTOP(Z_MIN) | (TEST_ENDSTOP(Z2_MIN) << 1); // bit 0 for Z, bit 1 for Z2
258
+
259
+            if (z_test && stepper.current_block->steps[Z_AXIS] > 0) { // z_test = Z_MIN || Z2_MIN
260
+              stepper.endstop_triggered(Z_AXIS);
261
+              SBI(endstop_hit_bits, Z_MIN);
262
+              if (!performing_homing || (z_test == 0x3))  //if not performing home or if both endstops were trigged during homing...
263
+                stepper.kill_current_block();
264
+            }
265
+
266
+          #else // !Z_DUAL_ENDSTOPS
267
+
268
+            #if ENABLED(Z_MIN_PROBE_USES_Z_MIN_ENDSTOP_PIN) && ENABLED(HAS_Z_MIN_PROBE)
269
+              if (z_probe_enabled) UPDATE_ENDSTOP(Z, MIN);
270
+            #else
271
+              UPDATE_ENDSTOP(Z, MIN);
272
+            #endif
273
+
274
+          #endif // !Z_DUAL_ENDSTOPS
275
+
276
+        #endif // HAS_Z_MIN
277
+
278
+        #if ENABLED(Z_MIN_PROBE_ENDSTOP) && DISABLED(Z_MIN_PROBE_USES_Z_MIN_ENDSTOP_PIN) && ENABLED(HAS_Z_MIN_PROBE)
279
+          if (z_probe_enabled) {
280
+            UPDATE_ENDSTOP(Z, MIN_PROBE);
281
+            if (TEST_ENDSTOP(Z_MIN_PROBE)) SBI(endstop_hit_bits, Z_MIN_PROBE);
282
+          }
283
+        #endif
284
+      }
285
+      else { // z +direction
286
+        #if HAS_Z_MAX
287
+
288
+          #if ENABLED(Z_DUAL_ENDSTOPS)
289
+
290
+            UPDATE_ENDSTOP_BIT(Z, MAX);
291
+            #if HAS_Z2_MAX
292
+              UPDATE_ENDSTOP_BIT(Z2, MAX);
293
+            #else
294
+              COPY_BIT(current_endstop_bits, Z_MAX, Z2_MAX);
295
+            #endif
296
+
297
+            byte z_test = TEST_ENDSTOP(Z_MAX) | (TEST_ENDSTOP(Z2_MAX) << 1); // bit 0 for Z, bit 1 for Z2
298
+
299
+            if (z_test && stepper.current_block->steps[Z_AXIS] > 0) {  // t_test = Z_MAX || Z2_MAX
300
+              stepper.endstop_triggered(Z_AXIS);
301
+              SBI(endstop_hit_bits, Z_MIN);
302
+              if (!performing_homing || (z_test == 0x3))  //if not performing home or if both endstops were trigged during homing...
303
+                stepper.kill_current_block();
304
+            }
305
+
306
+          #else // !Z_DUAL_ENDSTOPS
307
+
308
+            UPDATE_ENDSTOP(Z, MAX);
309
+
310
+          #endif // !Z_DUAL_ENDSTOPS
311
+        #endif // Z_MAX_PIN
312
+      }
313
+  #if ENABLED(COREXZ)
314
+    }
315
+  #endif
316
+  old_endstop_bits = current_endstop_bits;
317
+} // Endstops::update()

+ 94
- 0
Marlin/endstops.h 查看文件

1
+/**
2
+ * Marlin 3D Printer Firmware
3
+ * Copyright (C) 2016 MarlinFirmware [https://github.com/MarlinFirmware/Marlin]
4
+ *
5
+ * Based on Sprinter and grbl.
6
+ * Copyright (C) 2011 Camiel Gubbels / Erik van der Zalm
7
+ *
8
+ * This program is free software: you can redistribute it and/or modify
9
+ * it under the terms of the GNU General Public License as published by
10
+ * the Free Software Foundation, either version 3 of the License, or
11
+ * (at your option) any later version.
12
+ *
13
+ * This program is distributed in the hope that it will be useful,
14
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
15
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16
+ * GNU General Public License for more details.
17
+ *
18
+ * You should have received a copy of the GNU General Public License
19
+ * along with this program.  If not, see <http://www.gnu.org/licenses/>.
20
+ *
21
+ */
22
+
23
+/**
24
+ *  endstops.h - manages endstops
25
+ */
26
+
27
+#ifndef ENDSTOPS_H
28
+#define ENDSTOPS_H
29
+
30
+enum EndstopEnum {X_MIN = 0, Y_MIN = 1, Z_MIN = 2, Z_MIN_PROBE = 3, X_MAX = 4, Y_MAX = 5, Z_MAX = 6, Z2_MIN = 7, Z2_MAX = 8};
31
+
32
+class Endstops {
33
+
34
+  public:
35
+
36
+    volatile char endstop_hit_bits; // use X_MIN, Y_MIN, Z_MIN and Z_MIN_PROBE as BIT value
37
+
38
+    #if ENABLED(Z_DUAL_ENDSTOPS)
39
+      uint16_t current_endstop_bits = 0,
40
+                   old_endstop_bits = 0;
41
+    #else
42
+      byte current_endstop_bits = 0,
43
+               old_endstop_bits = 0;
44
+    #endif
45
+        
46
+
47
+    bool enabled = true;
48
+    bool enabled_globally =
49
+      #if ENABLED(ENDSTOPS_ONLY_FOR_HOMING)
50
+        false
51
+      #else
52
+        true
53
+      #endif
54
+    ;
55
+
56
+    Endstops();
57
+
58
+    /**
59
+     * Initialize the endstop pins
60
+     */
61
+    void init();
62
+
63
+    /**
64
+     * Update the endstops bits from the pins
65
+     */
66
+    void update();
67
+
68
+    /**
69
+     * Print an error message reporting the position when the endstops were last hit.
70
+     */
71
+    void report_state(); //call from somewhere to create an serial error message with the locations the endstops where hit, in case they were triggered
72
+
73
+    // Enable / disable endstop checking globally
74
+    FORCE_INLINE void enable_globally(bool onoff=true) { enabled_globally = enabled = onoff; }
75
+
76
+    // Enable / disable endstop checking
77
+    FORCE_INLINE void enable(bool onoff=true) { enabled = onoff; }
78
+
79
+    // Disable / Enable endstops based on ENSTOPS_ONLY_FOR_HOMING and global enable
80
+    FORCE_INLINE void not_homing() { enabled = enabled_globally; }
81
+
82
+    // Clear endstops (i.e., they were hit intentionally) to suppress the report
83
+    FORCE_INLINE void hit_on_purpose() { endstop_hit_bits = 0; }
84
+
85
+    // Enable / disable endstop z-probe checking
86
+    #if ENABLED(HAS_Z_MIN_PROBE)
87
+      volatile bool z_probe_enabled = false;
88
+      FORCE_INLINE void enable_z_probe(bool onoff=true) { z_probe_enabled = onoff; }
89
+    #endif
90
+};
91
+
92
+extern Endstops endstops;
93
+
94
+#endif // ENDSTOPS_H

+ 4
- 4
Marlin/planner.cpp 查看文件

1085
 
1085
 
1086
   planner_recalculate();
1086
   planner_recalculate();
1087
 
1087
 
1088
-  st_wake_up();
1088
+  stepper.wake_up();
1089
 
1089
 
1090
 } // plan_buffer_line()
1090
 } // plan_buffer_line()
1091
 
1091
 
1097
    * On CORE machines XYZ is derived from ABC.
1097
    * On CORE machines XYZ is derived from ABC.
1098
    */
1098
    */
1099
   vector_3 plan_get_position() {
1099
   vector_3 plan_get_position() {
1100
-    vector_3 position = vector_3(st_get_axis_position_mm(X_AXIS), st_get_axis_position_mm(Y_AXIS), st_get_axis_position_mm(Z_AXIS));
1100
+    vector_3 position = vector_3(stepper.get_axis_position_mm(X_AXIS), stepper.get_axis_position_mm(Y_AXIS), stepper.get_axis_position_mm(Z_AXIS));
1101
 
1101
 
1102
     //position.debug("in plan_get position");
1102
     //position.debug("in plan_get position");
1103
     //plan_bed_level_matrix.debug("in plan_get_position");
1103
     //plan_bed_level_matrix.debug("in plan_get_position");
1132
          ny = position[Y_AXIS] = lround(y * axis_steps_per_unit[Y_AXIS]),
1132
          ny = position[Y_AXIS] = lround(y * axis_steps_per_unit[Y_AXIS]),
1133
          nz = position[Z_AXIS] = lround(z * axis_steps_per_unit[Z_AXIS]),
1133
          nz = position[Z_AXIS] = lround(z * axis_steps_per_unit[Z_AXIS]),
1134
          ne = position[E_AXIS] = lround(e * axis_steps_per_unit[E_AXIS]);
1134
          ne = position[E_AXIS] = lround(e * axis_steps_per_unit[E_AXIS]);
1135
-    st_set_position(nx, ny, nz, ne);
1135
+    stepper.set_position(nx, ny, nz, ne);
1136
     previous_nominal_speed = 0.0; // Resets planner junction speeds. Assumes start from rest.
1136
     previous_nominal_speed = 0.0; // Resets planner junction speeds. Assumes start from rest.
1137
 
1137
 
1138
     for (int i = 0; i < NUM_AXIS; i++) previous_speed[i] = 0.0;
1138
     for (int i = 0; i < NUM_AXIS; i++) previous_speed[i] = 0.0;
1140
 
1140
 
1141
 void plan_set_e_position(const float& e) {
1141
 void plan_set_e_position(const float& e) {
1142
   position[E_AXIS] = lround(e * axis_steps_per_unit[E_AXIS]);
1142
   position[E_AXIS] = lround(e * axis_steps_per_unit[E_AXIS]);
1143
-  st_set_e_position(position[E_AXIS]);
1143
+  stepper.set_e_position(position[E_AXIS]);
1144
 }
1144
 }
1145
 
1145
 
1146
 // Calculate the steps/s^2 acceleration rates, based on the mm/s^s
1146
 // Calculate the steps/s^2 acceleration rates, based on the mm/s^s

+ 146
- 561
Marlin/stepper.cpp 查看文件

21
  */
21
  */
22
 
22
 
23
 /**
23
 /**
24
- * stepper.cpp - stepper motor driver: executes motion plans using stepper motors
24
+ * stepper.cpp - A singleton object to execute motion plans using stepper motors
25
  * Marlin Firmware
25
  * Marlin Firmware
26
  *
26
  *
27
  * Derived from Grbl
27
  * Derived from Grbl
46
 
46
 
47
 #include "Marlin.h"
47
 #include "Marlin.h"
48
 #include "stepper.h"
48
 #include "stepper.h"
49
+#include "endstops.h"
49
 #include "planner.h"
50
 #include "planner.h"
50
 #include "temperature.h"
51
 #include "temperature.h"
51
 #include "ultralcd.h"
52
 #include "ultralcd.h"
57
   #include <SPI.h>
58
   #include <SPI.h>
58
 #endif
59
 #endif
59
 
60
 
60
-//===========================================================================
61
-//============================= public variables ============================
62
-//===========================================================================
63
-block_t* current_block;  // A pointer to the block currently being traced
64
-
65
-#if ENABLED(HAS_Z_MIN_PROBE)
66
-  volatile bool z_probe_is_active = false;
67
-#endif
68
-
69
-//===========================================================================
70
-//============================= private variables ===========================
71
-//===========================================================================
72
-//static makes it impossible to be called from outside of this file by extern.!
73
-
74
-// Variables used by The Stepper Driver Interrupt
75
-static unsigned char out_bits = 0;        // The next stepping-bits to be output
76
-static unsigned int cleaning_buffer_counter;
77
-
78
-#if ENABLED(Z_DUAL_ENDSTOPS)
79
-  static bool performing_homing = false,
80
-              locked_z_motor = false,
81
-              locked_z2_motor = false;
82
-#endif
83
-
84
-// Counter variables for the Bresenham line tracer
85
-static long counter_x, counter_y, counter_z, counter_e;
86
-volatile static unsigned long step_events_completed; // The number of step events executed in the current block
87
-
88
-#if ENABLED(ADVANCE)
89
-  static long advance_rate, advance, final_advance = 0;
90
-  static long old_advance = 0;
91
-  static long e_steps[4];
92
-#endif
93
-
94
-static long acceleration_time, deceleration_time;
95
-//static unsigned long accelerate_until, decelerate_after, acceleration_rate, initial_rate, final_rate, nominal_rate;
96
-static unsigned short acc_step_rate; // needed for deceleration start point
97
-static uint8_t step_loops;
98
-static uint8_t step_loops_nominal;
99
-static unsigned short OCR1A_nominal;
100
-
101
-volatile long endstops_trigsteps[3] = { 0 };
102
-volatile long endstops_stepsTotal, endstops_stepsDone;
103
-static volatile char endstop_hit_bits = 0; // use X_MIN, Y_MIN, Z_MIN and Z_MIN_PROBE as BIT value
104
-
105
-#if DISABLED(Z_DUAL_ENDSTOPS)
106
-  static byte
107
-#else
108
-  static uint16_t
109
-#endif
110
-    old_endstop_bits = 0; // use X_MIN, X_MAX... Z_MAX, Z_MIN_PROBE, Z2_MIN, Z2_MAX
111
-
112
-#if ENABLED(ABORT_ON_ENDSTOP_HIT_FEATURE_ENABLED)
113
-  bool abort_on_endstop_hit = false;
114
-#endif
115
-
116
-#if HAS_MOTOR_CURRENT_PWM
117
-  #ifndef PWM_MOTOR_CURRENT
118
-    #define PWM_MOTOR_CURRENT DEFAULT_PWM_MOTOR_CURRENT
119
-  #endif
120
-  const int motor_current_setting[3] = PWM_MOTOR_CURRENT;
121
-#endif
122
-
123
-static bool check_endstops = true;
124
-static bool check_endstops_global =
125
-  #if ENABLED(ENDSTOPS_ONLY_FOR_HOMING)
126
-    false
127
-  #else
128
-    true
129
-  #endif
130
-;
131
-
132
-volatile long count_position[NUM_AXIS] = { 0 }; // Positions of stepper motors, in step units
133
-volatile signed char count_direction[NUM_AXIS] = { 1 };
134
-
135
-
136
-//===========================================================================
137
-//================================ functions ================================
138
-//===========================================================================
61
+Stepper stepper; // Singleton
139
 
62
 
140
 #if ENABLED(DUAL_X_CARRIAGE)
63
 #if ENABLED(DUAL_X_CARRIAGE)
141
   #define X_APPLY_DIR(v,ALWAYS) \
64
   #define X_APPLY_DIR(v,ALWAYS) \
173
     #define Z_APPLY_STEP(v,Q) \
96
     #define Z_APPLY_STEP(v,Q) \
174
     if (performing_homing) { \
97
     if (performing_homing) { \
175
       if (Z_HOME_DIR > 0) {\
98
       if (Z_HOME_DIR > 0) {\
176
-        if (!(TEST(old_endstop_bits, Z_MAX) && (count_direction[Z_AXIS] > 0)) && !locked_z_motor) Z_STEP_WRITE(v); \
177
-        if (!(TEST(old_endstop_bits, Z2_MAX) && (count_direction[Z_AXIS] > 0)) && !locked_z2_motor) Z2_STEP_WRITE(v); \
99
+        if (!(TEST(endstops.old_endstop_bits, Z_MAX) && (count_direction[Z_AXIS] > 0)) && !locked_z_motor) Z_STEP_WRITE(v); \
100
+        if (!(TEST(endstops.old_endstop_bits, Z2_MAX) && (count_direction[Z_AXIS] > 0)) && !locked_z2_motor) Z2_STEP_WRITE(v); \
178
       } \
101
       } \
179
       else { \
102
       else { \
180
-        if (!(TEST(old_endstop_bits, Z_MIN) && (count_direction[Z_AXIS] < 0)) && !locked_z_motor) Z_STEP_WRITE(v); \
181
-        if (!(TEST(old_endstop_bits, Z2_MIN) && (count_direction[Z_AXIS] < 0)) && !locked_z2_motor) Z2_STEP_WRITE(v); \
103
+        if (!(TEST(endstops.old_endstop_bits, Z_MIN) && (count_direction[Z_AXIS] < 0)) && !locked_z_motor) Z_STEP_WRITE(v); \
104
+        if (!(TEST(endstops.old_endstop_bits, Z2_MIN) && (count_direction[Z_AXIS] < 0)) && !locked_z2_motor) Z2_STEP_WRITE(v); \
182
       } \
105
       } \
183
     } \
106
     } \
184
     else { \
107
     else { \
195
 
118
 
196
 #define E_APPLY_STEP(v,Q) E_STEP_WRITE(v)
119
 #define E_APPLY_STEP(v,Q) E_STEP_WRITE(v)
197
 
120
 
198
-// intRes = intIn1 * intIn2 >> 16
199
-// uses:
200
-// r26 to store 0
201
-// r27 to store the byte 1 of the 24 bit result
202
-#define MultiU16X8toH16(intRes, charIn1, intIn2) \
203
-  asm volatile ( \
204
-                 "clr r26 \n\t" \
205
-                 "mul %A1, %B2 \n\t" \
206
-                 "movw %A0, r0 \n\t" \
207
-                 "mul %A1, %A2 \n\t" \
208
-                 "add %A0, r1 \n\t" \
209
-                 "adc %B0, r26 \n\t" \
210
-                 "lsr r0 \n\t" \
211
-                 "adc %A0, r26 \n\t" \
212
-                 "adc %B0, r26 \n\t" \
213
-                 "clr r1 \n\t" \
214
-                 : \
215
-                 "=&r" (intRes) \
216
-                 : \
217
-                 "d" (charIn1), \
218
-                 "d" (intIn2) \
219
-                 : \
220
-                 "r26" \
221
-               )
222
-
223
 // intRes = longIn1 * longIn2 >> 24
121
 // intRes = longIn1 * longIn2 >> 24
224
 // uses:
122
 // uses:
225
 // r26 to store 0
123
 // r26 to store 0
281
 #define ENABLE_STEPPER_DRIVER_INTERRUPT()  SBI(TIMSK1, OCIE1A)
179
 #define ENABLE_STEPPER_DRIVER_INTERRUPT()  SBI(TIMSK1, OCIE1A)
282
 #define DISABLE_STEPPER_DRIVER_INTERRUPT() CBI(TIMSK1, OCIE1A)
180
 #define DISABLE_STEPPER_DRIVER_INTERRUPT() CBI(TIMSK1, OCIE1A)
283
 
181
 
284
-void enable_endstops(bool check) { check_endstops = check; }
285
-
286
-void enable_endstops_globally(bool check) { check_endstops_global = check_endstops = check; }
287
-
288
-void endstops_not_homing() { check_endstops = check_endstops_global; }
289
-
290
-void endstops_hit_on_purpose() { endstop_hit_bits = 0; }
291
-
292
-void checkHitEndstops() {
293
-  if (endstop_hit_bits) {
294
-    #if ENABLED(ULTRA_LCD)
295
-      char chrX = ' ', chrY = ' ', chrZ = ' ', chrP = ' ';
296
-      #define _SET_STOP_CHAR(A,C) (chr## A = C)
297
-    #else
298
-      #define _SET_STOP_CHAR(A,C) ;
299
-    #endif
300
-
301
-    #define _ENDSTOP_HIT_ECHO(A,C) do{ \
302
-      SERIAL_ECHOPAIR(" " STRINGIFY(A) ":", endstops_trigsteps[A ##_AXIS] / axis_steps_per_unit[A ##_AXIS]); \
303
-      _SET_STOP_CHAR(A,C); }while(0)
304
-
305
-    #define _ENDSTOP_HIT_TEST(A,C) \
306
-      if (TEST(endstop_hit_bits, A ##_MIN) || TEST(endstop_hit_bits, A ##_MAX)) \
307
-        _ENDSTOP_HIT_ECHO(A,C)
308
-
309
-    SERIAL_ECHO_START;
310
-    SERIAL_ECHOPGM(MSG_ENDSTOPS_HIT);
311
-    _ENDSTOP_HIT_TEST(X, 'X');
312
-    _ENDSTOP_HIT_TEST(Y, 'Y');
313
-    _ENDSTOP_HIT_TEST(Z, 'Z');
314
-
315
-    #if ENABLED(Z_MIN_PROBE_ENDSTOP)
316
-      #define P_AXIS Z_AXIS
317
-      if (TEST(endstop_hit_bits, Z_MIN_PROBE)) _ENDSTOP_HIT_ECHO(P, 'P');
318
-    #endif
319
-    SERIAL_EOL;
320
-
321
-    #if ENABLED(ULTRA_LCD)
322
-      char msg[3 * strlen(MSG_LCD_ENDSTOPS) + 8 + 1]; // Room for a UTF 8 string
323
-      sprintf_P(msg, PSTR(MSG_LCD_ENDSTOPS " %c %c %c %c"), chrX, chrY, chrZ, chrP);
324
-      lcd_setstatus(msg);
325
-    #endif
326
-
327
-    endstops_hit_on_purpose();
328
-
329
-    #if ENABLED(ABORT_ON_ENDSTOP_HIT_FEATURE_ENABLED) && ENABLED(SDSUPPORT)
330
-      if (abort_on_endstop_hit) {
331
-        card.sdprinting = false;
332
-        card.closefile();
333
-        quickStop();
334
-        disable_all_heaters(); // switch off all heaters.
335
-      }
336
-    #endif
337
-  }
338
-}
339
-
340
-// Check endstops - Called from ISR!
341
-inline void update_endstops() {
342
-
343
-  #if ENABLED(Z_DUAL_ENDSTOPS)
344
-    uint16_t
345
-  #else
346
-    byte
347
-  #endif
348
-      current_endstop_bits = 0;
349
-
350
-  #define _ENDSTOP_PIN(AXIS, MINMAX) AXIS ##_## MINMAX ##_PIN
351
-  #define _ENDSTOP_INVERTING(AXIS, MINMAX) AXIS ##_## MINMAX ##_ENDSTOP_INVERTING
352
-  #define _AXIS(AXIS) AXIS ##_AXIS
353
-  #define _ENDSTOP_HIT(AXIS) SBI(endstop_hit_bits, _ENDSTOP(AXIS, MIN))
354
-  #define _ENDSTOP(AXIS, MINMAX) AXIS ##_## MINMAX
355
-
356
-  // SET_ENDSTOP_BIT: set the current endstop bits for an endstop to its status
357
-  #define SET_ENDSTOP_BIT(AXIS, MINMAX) SET_BIT(current_endstop_bits, _ENDSTOP(AXIS, MINMAX), (READ(_ENDSTOP_PIN(AXIS, MINMAX)) != _ENDSTOP_INVERTING(AXIS, MINMAX)))
358
-  // COPY_BIT: copy the value of COPY_BIT to BIT in bits
359
-  #define COPY_BIT(bits, COPY_BIT, BIT) SET_BIT(bits, BIT, TEST(bits, COPY_BIT))
360
-  // TEST_ENDSTOP: test the old and the current status of an endstop
361
-  #define TEST_ENDSTOP(ENDSTOP) (TEST(current_endstop_bits, ENDSTOP) && TEST(old_endstop_bits, ENDSTOP))
362
-
363
-  #if ENABLED(COREXY) || ENABLED(COREXZ)
364
-
365
-    #define _SET_TRIGSTEPS(AXIS) do { \
366
-        float axis_pos = count_position[_AXIS(AXIS)]; \
367
-        if (_AXIS(AXIS) == A_AXIS) \
368
-          axis_pos = (axis_pos + count_position[CORE_AXIS_2]) / 2; \
369
-        else if (_AXIS(AXIS) == CORE_AXIS_2) \
370
-          axis_pos = (count_position[A_AXIS] - axis_pos) / 2; \
371
-        endstops_trigsteps[_AXIS(AXIS)] = axis_pos; \
372
-      } while(0)
373
-
374
-  #else
375
-
376
-    #define _SET_TRIGSTEPS(AXIS) endstops_trigsteps[_AXIS(AXIS)] = count_position[_AXIS(AXIS)]
377
-
378
-  #endif // COREXY || COREXZ
379
-
380
-  #define UPDATE_ENDSTOP(AXIS,MINMAX) do { \
381
-      SET_ENDSTOP_BIT(AXIS, MINMAX); \
382
-      if (TEST_ENDSTOP(_ENDSTOP(AXIS, MINMAX)) && current_block->steps[_AXIS(AXIS)] > 0) { \
383
-        _SET_TRIGSTEPS(AXIS); \
384
-        _ENDSTOP_HIT(AXIS); \
385
-        step_events_completed = current_block->step_event_count; \
386
-      } \
387
-    } while(0)
388
-
389
-  #if ENABLED(COREXY) || ENABLED(COREXZ)
390
-    // Head direction in -X axis for CoreXY and CoreXZ bots.
391
-    // If Delta1 == -Delta2, the movement is only in Y or Z axis
392
-    if ((current_block->steps[A_AXIS] != current_block->steps[CORE_AXIS_2]) || (TEST(out_bits, A_AXIS) == TEST(out_bits, CORE_AXIS_2))) {
393
-      if (TEST(out_bits, X_HEAD))
394
-  #else
395
-    if (TEST(out_bits, X_AXIS))   // stepping along -X axis (regular Cartesian bot)
396
-  #endif
397
-      { // -direction
398
-        #if ENABLED(DUAL_X_CARRIAGE)
399
-          // with 2 x-carriages, endstops are only checked in the homing direction for the active extruder
400
-          if ((current_block->active_extruder == 0 && X_HOME_DIR == -1) || (current_block->active_extruder != 0 && X2_HOME_DIR == -1))
401
-        #endif
402
-          {
403
-            #if HAS_X_MIN
404
-              UPDATE_ENDSTOP(X, MIN);
405
-            #endif
406
-          }
407
-      }
408
-      else { // +direction
409
-        #if ENABLED(DUAL_X_CARRIAGE)
410
-          // with 2 x-carriages, endstops are only checked in the homing direction for the active extruder
411
-          if ((current_block->active_extruder == 0 && X_HOME_DIR == 1) || (current_block->active_extruder != 0 && X2_HOME_DIR == 1))
412
-        #endif
413
-          {
414
-            #if HAS_X_MAX
415
-              UPDATE_ENDSTOP(X, MAX);
416
-            #endif
417
-          }
418
-      }
419
-  #if ENABLED(COREXY) || ENABLED(COREXZ)
420
-    }
421
-  #endif
422
-
423
-  #if ENABLED(COREXY)
424
-    // Head direction in -Y axis for CoreXY bots.
425
-    // If DeltaX == DeltaY, the movement is only in X axis
426
-    if ((current_block->steps[A_AXIS] != current_block->steps[B_AXIS]) || (TEST(out_bits, A_AXIS) != TEST(out_bits, B_AXIS))) {
427
-      if (TEST(out_bits, Y_HEAD))
428
-  #else
429
-      if (TEST(out_bits, Y_AXIS))   // -direction
430
-  #endif
431
-      { // -direction
432
-        #if HAS_Y_MIN
433
-          UPDATE_ENDSTOP(Y, MIN);
434
-        #endif
435
-      }
436
-      else { // +direction
437
-        #if HAS_Y_MAX
438
-          UPDATE_ENDSTOP(Y, MAX);
439
-        #endif
440
-      }
441
-  #if ENABLED(COREXY)
442
-    }
443
-  #endif
444
-
445
-  #if ENABLED(COREXZ)
446
-    // Head direction in -Z axis for CoreXZ bots.
447
-    // If DeltaX == DeltaZ, the movement is only in X axis
448
-    if ((current_block->steps[A_AXIS] != current_block->steps[C_AXIS]) || (TEST(out_bits, A_AXIS) != TEST(out_bits, C_AXIS))) {
449
-      if (TEST(out_bits, Z_HEAD))
450
-  #else
451
-      if (TEST(out_bits, Z_AXIS))
452
-  #endif
453
-      { // z -direction
454
-        #if HAS_Z_MIN
455
-
456
-          #if ENABLED(Z_DUAL_ENDSTOPS)
457
-            SET_ENDSTOP_BIT(Z, MIN);
458
-            #if HAS_Z2_MIN
459
-              SET_ENDSTOP_BIT(Z2, MIN);
460
-            #else
461
-              COPY_BIT(current_endstop_bits, Z_MIN, Z2_MIN);
462
-            #endif
463
-
464
-            byte z_test = TEST_ENDSTOP(Z_MIN) | (TEST_ENDSTOP(Z2_MIN) << 1); // bit 0 for Z, bit 1 for Z2
465
-
466
-            if (z_test && current_block->steps[Z_AXIS] > 0) { // z_test = Z_MIN || Z2_MIN
467
-              endstops_trigsteps[Z_AXIS] = count_position[Z_AXIS];
468
-              SBI(endstop_hit_bits, Z_MIN);
469
-              if (!performing_homing || (z_test == 0x3))  //if not performing home or if both endstops were trigged during homing...
470
-                step_events_completed = current_block->step_event_count;
471
-            }
472
-          #else // !Z_DUAL_ENDSTOPS
473
-
474
-            #if ENABLED(Z_MIN_PROBE_USES_Z_MIN_ENDSTOP_PIN) && ENABLED(HAS_Z_MIN_PROBE)
475
-              if (z_probe_is_active) UPDATE_ENDSTOP(Z, MIN);
476
-            #else
477
-              UPDATE_ENDSTOP(Z, MIN);
478
-            #endif
479
-          #endif // !Z_DUAL_ENDSTOPS
480
-        #endif
481
-
482
-        #if ENABLED(Z_MIN_PROBE_ENDSTOP) && DISABLED(Z_MIN_PROBE_USES_Z_MIN_ENDSTOP_PIN) && ENABLED(HAS_Z_MIN_PROBE)
483
-          if (z_probe_is_active) {
484
-            UPDATE_ENDSTOP(Z, MIN_PROBE);
485
-            if (TEST_ENDSTOP(Z_MIN_PROBE)) SBI(endstop_hit_bits, Z_MIN_PROBE);
486
-          }
487
-        #endif
488
-      }
489
-      else { // z +direction
490
-        #if HAS_Z_MAX
491
-
492
-          #if ENABLED(Z_DUAL_ENDSTOPS)
493
-
494
-            SET_ENDSTOP_BIT(Z, MAX);
495
-            #if HAS_Z2_MAX
496
-              SET_ENDSTOP_BIT(Z2, MAX);
497
-            #else
498
-              COPY_BIT(current_endstop_bits, Z_MAX, Z2_MAX);
499
-            #endif
500
-
501
-            byte z_test = TEST_ENDSTOP(Z_MAX) | (TEST_ENDSTOP(Z2_MAX) << 1); // bit 0 for Z, bit 1 for Z2
502
-
503
-            if (z_test && current_block->steps[Z_AXIS] > 0) {  // t_test = Z_MAX || Z2_MAX
504
-              endstops_trigsteps[Z_AXIS] = count_position[Z_AXIS];
505
-              SBI(endstop_hit_bits, Z_MIN);
506
-              if (!performing_homing || (z_test == 0x3))  //if not performing home or if both endstops were trigged during homing...
507
-                step_events_completed = current_block->step_event_count;
508
-            }
509
-
510
-          #else // !Z_DUAL_ENDSTOPS
511
-
512
-            UPDATE_ENDSTOP(Z, MAX);
513
-
514
-          #endif // !Z_DUAL_ENDSTOPS
515
-        #endif // Z_MAX_PIN
516
-      }
517
-  #if ENABLED(COREXZ)
518
-    }
519
-  #endif
520
-  old_endstop_bits = current_endstop_bits;
521
-}
522
-
523
-//         __________________________
524
-//        /|                        |\     _________________         ^
525
-//       / |                        | \   /|               |\        |
526
-//      /  |                        |  \ / |               | \       s
527
-//     /   |                        |   |  |               |  \      p
528
-//    /    |                        |   |  |               |   \     e
529
-//   +-----+------------------------+---+--+---------------+----+    e
530
-//   |               BLOCK 1            |      BLOCK 2          |    d
531
-//
532
-//                           time ----->
533
-//
534
-//  The trapezoid is the shape the speed curve over time. It starts at block->initial_rate, accelerates
535
-//  first block->accelerate_until step_events_completed, then keeps going at constant speed until
536
-//  step_events_completed reaches block->decelerate_after after which it decelerates until the trapezoid generator is reset.
537
-//  The slope of acceleration is calculated using v = u + at where t is the accumulated timer values of the steps so far.
538
-
539
-void st_wake_up() {
182
+/**
183
+ *         __________________________
184
+ *        /|                        |\     _________________         ^
185
+ *       / |                        | \   /|               |\        |
186
+ *      /  |                        |  \ / |               | \       s
187
+ *     /   |                        |   |  |               |  \      p
188
+ *    /    |                        |   |  |               |   \     e
189
+ *   +-----+------------------------+---+--+---------------+----+    e
190
+ *   |               BLOCK 1            |      BLOCK 2          |    d
191
+ *
192
+ *                           time ----->
193
+ *
194
+ *  The trapezoid is the shape the speed curve over time. It starts at block->initial_rate, accelerates
195
+ *  first block->accelerate_until step_events_completed, then keeps going at constant speed until
196
+ *  step_events_completed reaches block->decelerate_after after which it decelerates until the trapezoid generator is reset.
197
+ *  The slope of acceleration is calculated using v = u + at where t is the accumulated timer values of the steps so far.
198
+ */
199
+void Stepper::wake_up() {
540
   //  TCNT1 = 0;
200
   //  TCNT1 = 0;
541
   ENABLE_STEPPER_DRIVER_INTERRUPT();
201
   ENABLE_STEPPER_DRIVER_INTERRUPT();
542
 }
202
 }
543
 
203
 
544
-FORCE_INLINE unsigned short calc_timer(unsigned short step_rate) {
545
-  unsigned short timer;
546
-
547
-  NOMORE(step_rate, MAX_STEP_FREQUENCY);
548
-
549
-  if (step_rate > 20000) { // If steprate > 20kHz >> step 4 times
550
-    step_rate = (step_rate >> 2) & 0x3fff;
551
-    step_loops = 4;
552
-  }
553
-  else if (step_rate > 10000) { // If steprate > 10kHz >> step 2 times
554
-    step_rate = (step_rate >> 1) & 0x7fff;
555
-    step_loops = 2;
556
-  }
557
-  else {
558
-    step_loops = 1;
559
-  }
560
-
561
-  NOLESS(step_rate, F_CPU / 500000);
562
-  step_rate -= F_CPU / 500000; // Correct for minimal speed
563
-  if (step_rate >= (8 * 256)) { // higher step rate
564
-    unsigned short table_address = (unsigned short)&speed_lookuptable_fast[(unsigned char)(step_rate >> 8)][0];
565
-    unsigned char tmp_step_rate = (step_rate & 0x00ff);
566
-    unsigned short gain = (unsigned short)pgm_read_word_near(table_address + 2);
567
-    MultiU16X8toH16(timer, tmp_step_rate, gain);
568
-    timer = (unsigned short)pgm_read_word_near(table_address) - timer;
569
-  }
570
-  else { // lower step rates
571
-    unsigned short table_address = (unsigned short)&speed_lookuptable_slow[0][0];
572
-    table_address += ((step_rate) >> 1) & 0xfffc;
573
-    timer = (unsigned short)pgm_read_word_near(table_address);
574
-    timer -= (((unsigned short)pgm_read_word_near(table_address + 2) * (unsigned char)(step_rate & 0x0007)) >> 3);
575
-  }
576
-  if (timer < 100) { timer = 100; MYSERIAL.print(MSG_STEPPER_TOO_HIGH); MYSERIAL.println(step_rate); }//(20kHz this should never happen)
577
-  return timer;
578
-}
579
-
580
 /**
204
 /**
581
  * Set the stepper direction of each axis
205
  * Set the stepper direction of each axis
582
  *
206
  *
583
  *   X_AXIS=A_AXIS and Y_AXIS=B_AXIS for COREXY
207
  *   X_AXIS=A_AXIS and Y_AXIS=B_AXIS for COREXY
584
  *   X_AXIS=A_AXIS and Z_AXIS=C_AXIS for COREXZ
208
  *   X_AXIS=A_AXIS and Z_AXIS=C_AXIS for COREXZ
585
  */
209
  */
586
-void set_stepper_direction() {
210
+void Stepper::set_directions() {
587
 
211
 
588
   #define SET_STEP_DIR(AXIS) \
212
   #define SET_STEP_DIR(AXIS) \
589
-    if (TEST(out_bits, AXIS ##_AXIS)) { \
213
+    if (motor_direction(AXIS ##_AXIS)) { \
590
       AXIS ##_APPLY_DIR(INVERT_## AXIS ##_DIR, false); \
214
       AXIS ##_APPLY_DIR(INVERT_## AXIS ##_DIR, false); \
591
       count_direction[AXIS ##_AXIS] = -1; \
215
       count_direction[AXIS ##_AXIS] = -1; \
592
     } \
216
     } \
600
   SET_STEP_DIR(Z); // C
224
   SET_STEP_DIR(Z); // C
601
 
225
 
602
   #if DISABLED(ADVANCE)
226
   #if DISABLED(ADVANCE)
603
-    if (TEST(out_bits, E_AXIS)) {
227
+    if (motor_direction(E_AXIS)) {
604
       REV_E_DIR();
228
       REV_E_DIR();
605
       count_direction[E_AXIS] = -1;
229
       count_direction[E_AXIS] = -1;
606
     }
230
     }
611
   #endif //!ADVANCE
235
   #endif //!ADVANCE
612
 }
236
 }
613
 
237
 
614
-// Initializes the trapezoid generator from the current block. Called whenever a new
615
-// block begins.
616
-FORCE_INLINE void trapezoid_generator_reset() {
617
-
618
-  static int8_t last_extruder = -1;
619
-
620
-  if (current_block->direction_bits != out_bits || current_block->active_extruder != last_extruder) {
621
-    out_bits = current_block->direction_bits;
622
-    last_extruder = current_block->active_extruder;
623
-    set_stepper_direction();
624
-  }
625
-
626
-  #if ENABLED(ADVANCE)
627
-    advance = current_block->initial_advance;
628
-    final_advance = current_block->final_advance;
629
-    // Do E steps + advance steps
630
-    e_steps[current_block->active_extruder] += ((advance >>8) - old_advance);
631
-    old_advance = advance >>8;
632
-  #endif
633
-  deceleration_time = 0;
634
-  // step_rate to timer interval
635
-  OCR1A_nominal = calc_timer(current_block->nominal_rate);
636
-  // make a note of the number of step loops required at nominal speed
637
-  step_loops_nominal = step_loops;
638
-  acc_step_rate = current_block->initial_rate;
639
-  acceleration_time = calc_timer(acc_step_rate);
640
-  OCR1A = acceleration_time;
641
-
642
-  // SERIAL_ECHO_START;
643
-  // SERIAL_ECHOPGM("advance :");
644
-  // SERIAL_ECHO(current_block->advance/256.0);
645
-  // SERIAL_ECHOPGM("advance rate :");
646
-  // SERIAL_ECHO(current_block->advance_rate/256.0);
647
-  // SERIAL_ECHOPGM("initial advance :");
648
-  // SERIAL_ECHO(current_block->initial_advance/256.0);
649
-  // SERIAL_ECHOPGM("final advance :");
650
-  // SERIAL_ECHOLN(current_block->final_advance/256.0);
651
-}
652
-
653
 // "The Stepper Driver Interrupt" - This timer interrupt is the workhorse.
238
 // "The Stepper Driver Interrupt" - This timer interrupt is the workhorse.
654
 // It pops blocks from the block_buffer and executes them by pulsing the stepper pins appropriately.
239
 // It pops blocks from the block_buffer and executes them by pulsing the stepper pins appropriately.
655
-ISR(TIMER1_COMPA_vect) {
240
+ISR(TIMER1_COMPA_vect) { stepper.isr(); }
656
 
241
 
242
+void Stepper::isr() {
657
   if (cleaning_buffer_counter) {
243
   if (cleaning_buffer_counter) {
658
     current_block = NULL;
244
     current_block = NULL;
659
     plan_discard_current_block();
245
     plan_discard_current_block();
672
     if (current_block) {
258
     if (current_block) {
673
       current_block->busy = true;
259
       current_block->busy = true;
674
       trapezoid_generator_reset();
260
       trapezoid_generator_reset();
675
-      counter_x = -(current_block->step_event_count >> 1);
676
-      counter_y = counter_z = counter_e = counter_x;
261
+      counter_X = -(current_block->step_event_count >> 1);
262
+      counter_Y = counter_Z = counter_E = counter_X;
677
       step_events_completed = 0;
263
       step_events_completed = 0;
678
 
264
 
679
       #if ENABLED(Z_LATE_ENABLE)
265
       #if ENABLED(Z_LATE_ENABLE)
697
 
283
 
698
     // Update endstops state, if enabled
284
     // Update endstops state, if enabled
699
     #if ENABLED(HAS_Z_MIN_PROBE)
285
     #if ENABLED(HAS_Z_MIN_PROBE)
700
-      if (check_endstops || z_probe_is_active) update_endstops();
286
+      if (endstops.enabled || endstops.z_probe_enabled) endstops.update();
701
     #else
287
     #else
702
-      if (check_endstops) update_endstops();
288
+      if (endstops.enabled) endstops.update();
703
     #endif
289
     #endif
704
 
290
 
705
     // Take multiple steps per interrupt (For high speed moves)
291
     // Take multiple steps per interrupt (For high speed moves)
709
       #endif
295
       #endif
710
 
296
 
711
       #if ENABLED(ADVANCE)
297
       #if ENABLED(ADVANCE)
712
-        counter_e += current_block->steps[E_AXIS];
713
-        if (counter_e > 0) {
714
-          counter_e -= current_block->step_event_count;
715
-          e_steps[current_block->active_extruder] += TEST(out_bits, E_AXIS) ? -1 : 1;
298
+        counter_E += current_block->steps[E_AXIS];
299
+        if (counter_E > 0) {
300
+          counter_E -= current_block->step_event_count;
301
+          e_steps[current_block->active_extruder] += motor_direction(E_AXIS) ? -1 : 1;
716
         }
302
         }
717
       #endif //ADVANCE
303
       #endif //ADVANCE
718
 
304
 
719
-      #define _COUNTER(axis) counter_## axis
305
+      #define _COUNTER(AXIS) counter_## AXIS
720
       #define _APPLY_STEP(AXIS) AXIS ##_APPLY_STEP
306
       #define _APPLY_STEP(AXIS) AXIS ##_APPLY_STEP
721
       #define _INVERT_STEP_PIN(AXIS) INVERT_## AXIS ##_STEP_PIN
307
       #define _INVERT_STEP_PIN(AXIS) INVERT_## AXIS ##_STEP_PIN
722
 
308
 
723
-      #define STEP_ADD(axis, AXIS) \
724
-        _COUNTER(axis) += current_block->steps[_AXIS(AXIS)]; \
725
-        if (_COUNTER(axis) > 0) { _APPLY_STEP(AXIS)(!_INVERT_STEP_PIN(AXIS),0); }
309
+      #define STEP_ADD(AXIS) \
310
+        _COUNTER(AXIS) += current_block->steps[_AXIS(AXIS)]; \
311
+        if (_COUNTER(AXIS) > 0) { _APPLY_STEP(AXIS)(!_INVERT_STEP_PIN(AXIS),0); }
726
 
312
 
727
-      STEP_ADD(x,X);
728
-      STEP_ADD(y,Y);
729
-      STEP_ADD(z,Z);
313
+      STEP_ADD(X);
314
+      STEP_ADD(Y);
315
+      STEP_ADD(Z);
730
       #if DISABLED(ADVANCE)
316
       #if DISABLED(ADVANCE)
731
-        STEP_ADD(e,E);
317
+        STEP_ADD(E);
732
       #endif
318
       #endif
733
 
319
 
734
-      #define STEP_IF_COUNTER(axis, AXIS) \
735
-        if (_COUNTER(axis) > 0) { \
736
-          _COUNTER(axis) -= current_block->step_event_count; \
320
+      #define STEP_IF_COUNTER(AXIS) \
321
+        if (_COUNTER(AXIS) > 0) { \
322
+          _COUNTER(AXIS) -= current_block->step_event_count; \
737
           count_position[_AXIS(AXIS)] += count_direction[_AXIS(AXIS)]; \
323
           count_position[_AXIS(AXIS)] += count_direction[_AXIS(AXIS)]; \
738
           _APPLY_STEP(AXIS)(_INVERT_STEP_PIN(AXIS),0); \
324
           _APPLY_STEP(AXIS)(_INVERT_STEP_PIN(AXIS),0); \
739
         }
325
         }
740
 
326
 
741
-      STEP_IF_COUNTER(x, X);
742
-      STEP_IF_COUNTER(y, Y);
743
-      STEP_IF_COUNTER(z, Z);
327
+      STEP_IF_COUNTER(X);
328
+      STEP_IF_COUNTER(Y);
329
+      STEP_IF_COUNTER(Z);
744
       #if DISABLED(ADVANCE)
330
       #if DISABLED(ADVANCE)
745
-        STEP_IF_COUNTER(e, E);
331
+        STEP_IF_COUNTER(E);
746
       #endif
332
       #endif
747
 
333
 
748
       step_events_completed++;
334
       step_events_completed++;
749
       if (step_events_completed >= current_block->step_event_count) break;
335
       if (step_events_completed >= current_block->step_event_count) break;
750
     }
336
     }
751
     // Calculate new timer value
337
     // Calculate new timer value
752
-    unsigned short timer;
753
-    unsigned short step_rate;
338
+    unsigned short timer, step_rate;
754
     if (step_events_completed <= (unsigned long)current_block->accelerate_until) {
339
     if (step_events_completed <= (unsigned long)current_block->accelerate_until) {
755
 
340
 
756
       MultiU24X32toH16(acc_step_rate, acceleration_time, current_block->acceleration_rate);
341
       MultiU24X32toH16(acc_step_rate, acceleration_time, current_block->acceleration_rate);
817
 }
402
 }
818
 
403
 
819
 #if ENABLED(ADVANCE)
404
 #if ENABLED(ADVANCE)
820
-  unsigned char old_OCR0A;
821
   // Timer interrupt for E. e_steps is set in the main routine;
405
   // Timer interrupt for E. e_steps is set in the main routine;
822
   // Timer 0 is shared with millies
406
   // Timer 0 is shared with millies
823
-  ISR(TIMER0_COMPA_vect) {
407
+  ISR(TIMER0_COMPA_vect) { stepper.advance_isr(); }
408
+
409
+  void Stepper::advance_isr() {
824
     old_OCR0A += 52; // ~10kHz interrupt (250000 / 26 = 9615kHz)
410
     old_OCR0A += 52; // ~10kHz interrupt (250000 / 26 = 9615kHz)
825
     OCR0A = old_OCR0A;
411
     OCR0A = old_OCR0A;
826
 
412
 
852
       #endif
438
       #endif
853
     }
439
     }
854
   }
440
   }
441
+
855
 #endif // ADVANCE
442
 #endif // ADVANCE
856
 
443
 
857
-void st_init() {
444
+void Stepper::init() {
858
   digipot_init(); //Initialize Digipot Motor Current
445
   digipot_init(); //Initialize Digipot Motor Current
859
   microstep_init(); //Initialize Microstepping Pins
446
   microstep_init(); //Initialize Microstepping Pins
860
 
447
 
944
     if (!E_ENABLE_ON) E3_ENABLE_WRITE(HIGH);
531
     if (!E_ENABLE_ON) E3_ENABLE_WRITE(HIGH);
945
   #endif
532
   #endif
946
 
533
 
947
-  //endstops and pullups
948
-
949
-  #if HAS_X_MIN
950
-    SET_INPUT(X_MIN_PIN);
951
-    #if ENABLED(ENDSTOPPULLUP_XMIN)
952
-      WRITE(X_MIN_PIN,HIGH);
953
-    #endif
954
-  #endif
955
-
956
-  #if HAS_Y_MIN
957
-    SET_INPUT(Y_MIN_PIN);
958
-    #if ENABLED(ENDSTOPPULLUP_YMIN)
959
-      WRITE(Y_MIN_PIN,HIGH);
960
-    #endif
961
-  #endif
962
-
963
-  #if HAS_Z_MIN
964
-    SET_INPUT(Z_MIN_PIN);
965
-    #if ENABLED(ENDSTOPPULLUP_ZMIN)
966
-      WRITE(Z_MIN_PIN,HIGH);
967
-    #endif
968
-  #endif
969
-
970
-  #if HAS_Z2_MIN
971
-    SET_INPUT(Z2_MIN_PIN);
972
-    #if ENABLED(ENDSTOPPULLUP_ZMIN)
973
-      WRITE(Z2_MIN_PIN,HIGH);
974
-    #endif
975
-  #endif
976
-
977
-  #if HAS_X_MAX
978
-    SET_INPUT(X_MAX_PIN);
979
-    #if ENABLED(ENDSTOPPULLUP_XMAX)
980
-      WRITE(X_MAX_PIN,HIGH);
981
-    #endif
982
-  #endif
983
-
984
-  #if HAS_Y_MAX
985
-    SET_INPUT(Y_MAX_PIN);
986
-    #if ENABLED(ENDSTOPPULLUP_YMAX)
987
-      WRITE(Y_MAX_PIN,HIGH);
988
-    #endif
989
-  #endif
990
-
991
-  #if HAS_Z_MAX
992
-    SET_INPUT(Z_MAX_PIN);
993
-    #if ENABLED(ENDSTOPPULLUP_ZMAX)
994
-      WRITE(Z_MAX_PIN,HIGH);
995
-    #endif
996
-  #endif
997
-
998
-  #if HAS_Z2_MAX
999
-    SET_INPUT(Z2_MAX_PIN);
1000
-    #if ENABLED(ENDSTOPPULLUP_ZMAX)
1001
-      WRITE(Z2_MAX_PIN,HIGH);
1002
-    #endif
1003
-  #endif
1004
-
1005
-  #if HAS_Z_PROBE && ENABLED(Z_MIN_PROBE_ENDSTOP) // Check for Z_MIN_PROBE_ENDSTOP so we don't pull a pin high unless it's to be used.
1006
-    SET_INPUT(Z_MIN_PROBE_PIN);
1007
-    #if ENABLED(ENDSTOPPULLUP_ZMIN_PROBE)
1008
-      WRITE(Z_MIN_PROBE_PIN,HIGH);
1009
-    #endif
1010
-  #endif
534
+  //
535
+  // Init endstops and pullups here
536
+  //
537
+  endstops.init();
1011
 
538
 
1012
   #define _STEP_INIT(AXIS) AXIS ##_STEP_INIT
539
   #define _STEP_INIT(AXIS) AXIS ##_STEP_INIT
1013
   #define _WRITE_STEP(AXIS, HIGHLOW) AXIS ##_STEP_WRITE(HIGHLOW)
540
   #define _WRITE_STEP(AXIS, HIGHLOW) AXIS ##_STEP_WRITE(HIGHLOW)
1083
     SBI(TIMSK0, OCIE0A);
610
     SBI(TIMSK0, OCIE0A);
1084
   #endif //ADVANCE
611
   #endif //ADVANCE
1085
 
612
 
1086
-  enable_endstops(true); // Start with endstops active. After homing they can be disabled
613
+  endstops.enable(true); // Start with endstops active. After homing they can be disabled
1087
   sei();
614
   sei();
1088
 
615
 
1089
-  set_stepper_direction(); // Init directions to out_bits = 0
616
+  set_directions(); // Init directions to last_direction_bits = 0
1090
 }
617
 }
1091
 
618
 
1092
 
619
 
1093
 /**
620
 /**
1094
  * Block until all buffered steps are executed
621
  * Block until all buffered steps are executed
1095
  */
622
  */
1096
-void st_synchronize() { while (blocks_queued()) idle(); }
623
+void Stepper::synchronize() { while (blocks_queued()) idle(); }
1097
 
624
 
1098
 /**
625
 /**
1099
  * Set the stepper positions directly in steps
626
  * Set the stepper positions directly in steps
1101
  * The input is based on the typical per-axis XYZ steps.
628
  * The input is based on the typical per-axis XYZ steps.
1102
  * For CORE machines XYZ needs to be translated to ABC.
629
  * For CORE machines XYZ needs to be translated to ABC.
1103
  *
630
  *
1104
- * This allows st_get_axis_position_mm to correctly
631
+ * This allows get_axis_position_mm to correctly
1105
  * derive the current XYZ position later on.
632
  * derive the current XYZ position later on.
1106
  */
633
  */
1107
-void st_set_position(const long& x, const long& y, const long& z, const long& e) {
634
+void Stepper::set_position(const long& x, const long& y, const long& z, const long& e) {
1108
   CRITICAL_SECTION_START;
635
   CRITICAL_SECTION_START;
1109
 
636
 
1110
   #if ENABLED(COREXY)
637
   #if ENABLED(COREXY)
1129
   CRITICAL_SECTION_END;
656
   CRITICAL_SECTION_END;
1130
 }
657
 }
1131
 
658
 
1132
-void st_set_e_position(const long& e) {
659
+void Stepper::set_e_position(const long& e) {
1133
   CRITICAL_SECTION_START;
660
   CRITICAL_SECTION_START;
1134
   count_position[E_AXIS] = e;
661
   count_position[E_AXIS] = e;
1135
   CRITICAL_SECTION_END;
662
   CRITICAL_SECTION_END;
1138
 /**
665
 /**
1139
  * Get a stepper's position in steps.
666
  * Get a stepper's position in steps.
1140
  */
667
  */
1141
-long st_get_position(AxisEnum axis) {
668
+long Stepper::position(AxisEnum axis) {
1142
   CRITICAL_SECTION_START;
669
   CRITICAL_SECTION_START;
1143
   long count_pos = count_position[axis];
670
   long count_pos = count_position[axis];
1144
   CRITICAL_SECTION_END;
671
   CRITICAL_SECTION_END;
1149
  * Get an axis position according to stepper position(s)
676
  * Get an axis position according to stepper position(s)
1150
  * For CORE machines apply translation from ABC to XYZ.
677
  * For CORE machines apply translation from ABC to XYZ.
1151
  */
678
  */
1152
-float st_get_axis_position_mm(AxisEnum axis) {
679
+float Stepper::get_axis_position_mm(AxisEnum axis) {
1153
   float axis_steps;
680
   float axis_steps;
1154
   #if ENABLED(COREXY) | ENABLED(COREXZ)
681
   #if ENABLED(COREXY) | ENABLED(COREXZ)
1155
     if (axis == X_AXIS || axis == CORE_AXIS_2) {
682
     if (axis == X_AXIS || axis == CORE_AXIS_2) {
1162
       axis_steps = (pos1 + ((axis == X_AXIS) ? pos2 : -pos2)) / 2.0f;
689
       axis_steps = (pos1 + ((axis == X_AXIS) ? pos2 : -pos2)) / 2.0f;
1163
     }
690
     }
1164
     else
691
     else
1165
-      axis_steps = st_get_position(axis);
692
+      axis_steps = position(axis);
1166
   #else
693
   #else
1167
-    axis_steps = st_get_position(axis);
694
+    axis_steps = position(axis);
1168
   #endif
695
   #endif
1169
   return axis_steps / axis_steps_per_unit[axis];
696
   return axis_steps / axis_steps_per_unit[axis];
1170
 }
697
 }
1171
 
698
 
1172
-void finishAndDisableSteppers() {
1173
-  st_synchronize();
699
+void Stepper::finish_and_disable() {
700
+  synchronize();
1174
   disable_all_steppers();
701
   disable_all_steppers();
1175
 }
702
 }
1176
 
703
 
1177
-void quickStop() {
704
+void Stepper::quick_stop() {
1178
   cleaning_buffer_counter = 5000;
705
   cleaning_buffer_counter = 5000;
1179
   DISABLE_STEPPER_DRIVER_INTERRUPT();
706
   DISABLE_STEPPER_DRIVER_INTERRUPT();
1180
   while (blocks_queued()) plan_discard_current_block();
707
   while (blocks_queued()) plan_discard_current_block();
1182
   ENABLE_STEPPER_DRIVER_INTERRUPT();
709
   ENABLE_STEPPER_DRIVER_INTERRUPT();
1183
 }
710
 }
1184
 
711
 
712
+void Stepper::endstop_triggered(AxisEnum axis) {
713
+
714
+  #if ENABLED(COREXY) || ENABLED(COREXZ)
715
+
716
+    float axis_pos = count_position[axis];
717
+    if (axis == A_AXIS)
718
+      axis_pos = (axis_pos + count_position[CORE_AXIS_2]) / 2;
719
+    else if (axis == CORE_AXIS_2)
720
+      axis_pos = (count_position[A_AXIS] - axis_pos) / 2;
721
+    endstops_trigsteps[axis] = axis_pos;
722
+
723
+  #else // !COREXY && !COREXZ
724
+
725
+    endstops_trigsteps[axis] = count_position[axis];
726
+
727
+  #endif // !COREXY && !COREXZ
728
+
729
+  kill_current_block();
730
+}
731
+
732
+void Stepper::report_positions() {
733
+  CRITICAL_SECTION_START;
734
+  long xpos = count_position[X_AXIS],
735
+       ypos = count_position[Y_AXIS],
736
+       zpos = count_position[Z_AXIS];
737
+  CRITICAL_SECTION_END;
738
+
739
+  #if ENABLED(COREXY) || ENABLED(COREXZ)
740
+    SERIAL_PROTOCOLPGM(MSG_COUNT_A);
741
+  #else
742
+    SERIAL_PROTOCOLPGM(MSG_COUNT_X);
743
+  #endif
744
+  SERIAL_PROTOCOL(xpos);
745
+
746
+  #if ENABLED(COREXY) || ENABLED(COREXZ)
747
+    SERIAL_PROTOCOLPGM(" B:");
748
+  #else
749
+    SERIAL_PROTOCOLPGM(" Y:");
750
+  #endif
751
+  SERIAL_PROTOCOL(ypos);
752
+
753
+  #if ENABLED(COREXZ) || ENABLED(COREXZ)
754
+    SERIAL_PROTOCOLPGM(" C:");
755
+  #else
756
+    SERIAL_PROTOCOLPGM(" Z:");
757
+  #endif
758
+  SERIAL_PROTOCOL(zpos);
759
+
760
+  SERIAL_EOL;
761
+}
762
+
1185
 #if ENABLED(BABYSTEPPING)
763
 #if ENABLED(BABYSTEPPING)
1186
 
764
 
1187
   // MUST ONLY BE CALLED BY AN ISR,
765
   // MUST ONLY BE CALLED BY AN ISR,
1188
   // No other ISR should ever interrupt this!
766
   // No other ISR should ever interrupt this!
1189
-  void babystep(const uint8_t axis, const bool direction) {
767
+  void Stepper::babystep(const uint8_t axis, const bool direction) {
1190
 
768
 
1191
     #define _ENABLE(axis) enable_## axis()
769
     #define _ENABLE(axis) enable_## axis()
1192
     #define _READ_DIR(AXIS) AXIS ##_DIR_READ
770
     #define _READ_DIR(AXIS) AXIS ##_DIR_READ
1256
 
834
 
1257
 #endif //BABYSTEPPING
835
 #endif //BABYSTEPPING
1258
 
836
 
837
+/**
838
+ * Software-controlled Stepper Motor Current
839
+ */
840
+
1259
 #if HAS_DIGIPOTSS
841
 #if HAS_DIGIPOTSS
1260
 
842
 
1261
   // From Arduino DigitalPotControl example
843
   // From Arduino DigitalPotControl example
1262
-  void digitalPotWrite(int address, int value) {
844
+  void Stepper::digitalPotWrite(int address, int value) {
1263
     digitalWrite(DIGIPOTSS_PIN, LOW); // take the SS pin low to select the chip
845
     digitalWrite(DIGIPOTSS_PIN, LOW); // take the SS pin low to select the chip
1264
     SPI.transfer(address); //  send in the address and value via SPI:
846
     SPI.transfer(address); //  send in the address and value via SPI:
1265
     SPI.transfer(value);
847
     SPI.transfer(value);
1269
 
851
 
1270
 #endif //HAS_DIGIPOTSS
852
 #endif //HAS_DIGIPOTSS
1271
 
853
 
1272
-// Initialize Digipot Motor Current
1273
-void digipot_init() {
854
+void Stepper::digipot_init() {
1274
   #if HAS_DIGIPOTSS
855
   #if HAS_DIGIPOTSS
1275
     const uint8_t digipot_motor_current[] = DIGIPOT_MOTOR_CURRENT;
856
     const uint8_t digipot_motor_current[] = DIGIPOT_MOTOR_CURRENT;
1276
 
857
 
1299
   #endif
880
   #endif
1300
 }
881
 }
1301
 
882
 
1302
-void digipot_current(uint8_t driver, int current) {
883
+void Stepper::digipot_current(uint8_t driver, int current) {
1303
   #if HAS_DIGIPOTSS
884
   #if HAS_DIGIPOTSS
1304
     const uint8_t digipot_ch[] = DIGIPOT_CHANNELS;
885
     const uint8_t digipot_ch[] = DIGIPOT_CHANNELS;
1305
     digitalPotWrite(digipot_ch[driver], current);
886
     digitalPotWrite(digipot_ch[driver], current);
1322
   #endif
903
   #endif
1323
 }
904
 }
1324
 
905
 
1325
-void microstep_init() {
906
+void Stepper::microstep_init() {
1326
   #if HAS_MICROSTEPS_E1
907
   #if HAS_MICROSTEPS_E1
1327
     pinMode(E1_MS1_PIN, OUTPUT);
908
     pinMode(E1_MS1_PIN, OUTPUT);
1328
     pinMode(E1_MS2_PIN, OUTPUT);
909
     pinMode(E1_MS2_PIN, OUTPUT);
1343
   #endif
924
   #endif
1344
 }
925
 }
1345
 
926
 
1346
-void microstep_ms(uint8_t driver, int8_t ms1, int8_t ms2) {
927
+/**
928
+ * Software-controlled Microstepping
929
+ */
930
+
931
+void Stepper::microstep_ms(uint8_t driver, int8_t ms1, int8_t ms2) {
1347
   if (ms1 >= 0) switch (driver) {
932
   if (ms1 >= 0) switch (driver) {
1348
     case 0: digitalWrite(X_MS1_PIN, ms1); break;
933
     case 0: digitalWrite(X_MS1_PIN, ms1); break;
1349
     case 1: digitalWrite(Y_MS1_PIN, ms1); break;
934
     case 1: digitalWrite(Y_MS1_PIN, ms1); break;
1364
   }
949
   }
1365
 }
950
 }
1366
 
951
 
1367
-void microstep_mode(uint8_t driver, uint8_t stepping_mode) {
952
+void Stepper::microstep_mode(uint8_t driver, uint8_t stepping_mode) {
1368
   switch (stepping_mode) {
953
   switch (stepping_mode) {
1369
     case 1: microstep_ms(driver, MICROSTEP1); break;
954
     case 1: microstep_ms(driver, MICROSTEP1); break;
1370
     case 2: microstep_ms(driver, MICROSTEP2); break;
955
     case 2: microstep_ms(driver, MICROSTEP2); break;
1374
   }
959
   }
1375
 }
960
 }
1376
 
961
 
1377
-void microstep_readings() {
962
+void Stepper::microstep_readings() {
1378
   SERIAL_PROTOCOLPGM("MS1,MS2 Pins\n");
963
   SERIAL_PROTOCOLPGM("MS1,MS2 Pins\n");
1379
   SERIAL_PROTOCOLPGM("X: ");
964
   SERIAL_PROTOCOLPGM("X: ");
1380
   SERIAL_PROTOCOL(digitalRead(X_MS1_PIN));
965
   SERIAL_PROTOCOL(digitalRead(X_MS1_PIN));
1396
 }
981
 }
1397
 
982
 
1398
 #if ENABLED(Z_DUAL_ENDSTOPS)
983
 #if ENABLED(Z_DUAL_ENDSTOPS)
1399
-  void In_Homing_Process(bool state) { performing_homing = state; }
1400
-  void Lock_z_motor(bool state) { locked_z_motor = state; }
1401
-  void Lock_z2_motor(bool state) { locked_z2_motor = state; }
984
+  void Stepper::set_homing_flag(bool state) { performing_homing = state; }
985
+  void Stepper::set_z_lock(bool state) { locked_z_motor = state; }
986
+  void Stepper::set_z2_lock(bool state) { locked_z2_motor = state; }
1402
 #endif
987
 #endif

+ 285
- 62
Marlin/stepper.h 查看文件

21
  */
21
  */
22
 
22
 
23
 /**
23
 /**
24
-  stepper.h - stepper motor driver: executes motion plans of planner.c using the stepper motors
25
-  Part of Grbl
24
+ * stepper.h - stepper motor driver: executes motion plans of planner.c using the stepper motors
25
+ * Part of Grbl
26
+ *
27
+ * Copyright (c) 2009-2011 Simen Svale Skogsrud
28
+ *
29
+ * Grbl is free software: you can redistribute it and/or modify
30
+ * it under the terms of the GNU General Public License as published by
31
+ * the Free Software Foundation, either version 3 of the License, or
32
+ * (at your option) any later version.
33
+ *
34
+ * Grbl is distributed in the hope that it will be useful,
35
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
36
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
37
+ * GNU General Public License for more details.
38
+ *
39
+ * You should have received a copy of the GNU General Public License
40
+ * along with Grbl.  If not, see <http://www.gnu.org/licenses/>.
41
+ */
26
 
42
 
27
-  Copyright (c) 2009-2011 Simen Svale Skogsrud
43
+#ifndef STEPPER_H
44
+#define STEPPER_H
28
 
45
 
29
-  Grbl is free software: you can redistribute it and/or modify
30
-  it under the terms of the GNU General Public License as published by
31
-  the Free Software Foundation, either version 3 of the License, or
32
-  (at your option) any later version.
46
+#include "planner.h"
47
+#include "speed_lookuptable.h"
48
+#include "stepper_indirection.h"
49
+#include "language.h"
33
 
50
 
34
-  Grbl is distributed in the hope that it will be useful,
35
-  but WITHOUT ANY WARRANTY; without even the implied warranty of
36
-  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
37
-  GNU General Public License for more details.
51
+class Stepper;
52
+extern Stepper stepper;
38
 
53
 
39
-  You should have received a copy of the GNU General Public License
40
-  along with Grbl.  If not, see <http://www.gnu.org/licenses/>.
41
-*/
54
+// intRes = intIn1 * intIn2 >> 16
55
+// uses:
56
+// r26 to store 0
57
+// r27 to store the byte 1 of the 24 bit result
58
+#define MultiU16X8toH16(intRes, charIn1, intIn2) \
59
+  asm volatile ( \
60
+                 "clr r26 \n\t" \
61
+                 "mul %A1, %B2 \n\t" \
62
+                 "movw %A0, r0 \n\t" \
63
+                 "mul %A1, %A2 \n\t" \
64
+                 "add %A0, r1 \n\t" \
65
+                 "adc %B0, r26 \n\t" \
66
+                 "lsr r0 \n\t" \
67
+                 "adc %A0, r26 \n\t" \
68
+                 "adc %B0, r26 \n\t" \
69
+                 "clr r1 \n\t" \
70
+                 : \
71
+                 "=&r" (intRes) \
72
+                 : \
73
+                 "d" (charIn1), \
74
+                 "d" (intIn2) \
75
+                 : \
76
+                 "r26" \
77
+               )
42
 
78
 
43
-#ifndef stepper_h
44
-#define stepper_h
79
+class Stepper {
45
 
80
 
46
-#include "planner.h"
47
-#include "stepper_indirection.h"
81
+  public:
82
+
83
+    block_t* current_block = NULL;  // A pointer to the block currently being traced
84
+
85
+    #if ENABLED(ABORT_ON_ENDSTOP_HIT_FEATURE_ENABLED)
86
+      bool abort_on_endstop_hit = false;
87
+    #endif
88
+
89
+    #if ENABLED(Z_DUAL_ENDSTOPS)
90
+      bool performing_homing = false;
91
+    #endif
92
+
93
+    #if ENABLED(ADVANCE)
94
+      long e_steps[4];
95
+    #endif
96
+
97
+  private:
98
+
99
+    unsigned char last_direction_bits = 0;        // The next stepping-bits to be output
100
+    unsigned int cleaning_buffer_counter = 0;
101
+
102
+    #if ENABLED(Z_DUAL_ENDSTOPS)
103
+      bool locked_z_motor = false,
104
+           locked_z2_motor = false;
105
+    #endif
106
+
107
+    // Counter variables for the Bresenham line tracer
108
+    long counter_X = 0, counter_Y = 0, counter_Z = 0, counter_E = 0;
109
+    volatile unsigned long step_events_completed = 0; // The number of step events executed in the current block
110
+
111
+    #if ENABLED(ADVANCE)
112
+      unsigned char old_OCR0A;
113
+      long advance_rate, advance, final_advance = 0;
114
+      long old_advance = 0;
115
+    #endif
116
+
117
+    long acceleration_time, deceleration_time;
118
+    //unsigned long accelerate_until, decelerate_after, acceleration_rate, initial_rate, final_rate, nominal_rate;
119
+    unsigned short acc_step_rate; // needed for deceleration start point
120
+    uint8_t step_loops;
121
+    uint8_t step_loops_nominal;
122
+    unsigned short OCR1A_nominal;
123
+
124
+    volatile long endstops_trigsteps[3];
125
+    volatile long endstops_stepsTotal, endstops_stepsDone;
126
+
127
+    #if HAS_MOTOR_CURRENT_PWM
128
+      #ifndef PWM_MOTOR_CURRENT
129
+        #define PWM_MOTOR_CURRENT DEFAULT_PWM_MOTOR_CURRENT
130
+      #endif
131
+      const int motor_current_setting[3] = PWM_MOTOR_CURRENT;
132
+    #endif
133
+
134
+    //
135
+    // Positions of stepper motors, in step units
136
+    //
137
+    volatile long count_position[NUM_AXIS] = { 0 };
138
+
139
+    //
140
+    // Current direction of stepper motors (+1 or -1)
141
+    //
142
+    volatile signed char count_direction[NUM_AXIS] = { 1 };
143
+
144
+  public:
145
+
146
+    //
147
+    // Constructor / initializer
148
+    //
149
+    Stepper() {};
150
+
151
+    //
152
+    // Initialize stepper hardware
153
+    //
154
+    void init();
155
+
156
+    //
157
+    // Interrupt Service Routines
158
+    //
159
+
160
+    void isr();
161
+
162
+    #if ENABLED(ADVANCE)
163
+      void advance_isr();
164
+    #endif
165
+
166
+    //
167
+    // Block until all buffered steps are executed
168
+    //
169
+    void synchronize();
170
+
171
+    //
172
+    // Set the current position in steps
173
+    //
174
+    void set_position(const long& x, const long& y, const long& z, const long& e);
175
+    void set_e_position(const long& e);
176
+
177
+    //
178
+    // Set direction bits for all steppers
179
+    //
180
+    void set_directions();
181
+
182
+    //
183
+    // Get the position of a stepper, in steps
184
+    //
185
+    long position(AxisEnum axis);
186
+
187
+    //
188
+    // Report the positions of the steppers, in steps
189
+    //
190
+    void report_positions();
191
+
192
+    //
193
+    // Get the position (mm) of an axis based on stepper position(s)
194
+    //
195
+    float get_axis_position_mm(AxisEnum axis);
196
+
197
+    //
198
+    // The stepper subsystem goes to sleep when it runs out of things to execute. Call this
199
+    // to notify the subsystem that it is time to go to work.
200
+    //
201
+    void wake_up();
202
+
203
+    //
204
+    // Wait for moves to finish and disable all steppers
205
+    //
206
+    void finish_and_disable();
207
+
208
+    //
209
+    // Quickly stop all steppers and clear the blocks queue
210
+    //
211
+    void quick_stop();
48
 
212
 
49
-#if ENABLED(ABORT_ON_ENDSTOP_HIT_FEATURE_ENABLED)
50
-  extern bool abort_on_endstop_hit;
51
-#endif
213
+    //
214
+    // The direction of a single motor
215
+    //
216
+    FORCE_INLINE bool motor_direction(AxisEnum axis) { return TEST(last_direction_bits, axis); }
52
 
217
 
53
-// Initialize and start the stepper motor subsystem
54
-void st_init();
218
+    #if HAS_DIGIPOTSS
219
+      void digitalPotWrite(int address, int value);
220
+    #endif
221
+    void microstep_ms(uint8_t driver, int8_t ms1, int8_t ms2);
222
+    void digipot_current(uint8_t driver, int current);
223
+    void microstep_readings();
55
 
224
 
56
-// Block until all buffered steps are executed
57
-void st_synchronize();
225
+    #if ENABLED(Z_DUAL_ENDSTOPS)
226
+      void set_homing_flag(bool state);
227
+      void set_z_lock(bool state);
228
+      void set_z2_lock(bool state);
229
+    #endif
58
 
230
 
59
-// Set current position in steps
60
-void st_set_position(const long& x, const long& y, const long& z, const long& e);
61
-void st_set_e_position(const long& e);
231
+    #if ENABLED(BABYSTEPPING)
232
+      void babystep(const uint8_t axis, const bool direction); // perform a short step with a single stepper motor, outside of any convention
233
+    #endif
62
 
234
 
63
-// Get current position in steps
64
-long st_get_position(AxisEnum axis);
235
+    inline void kill_current_block() {
236
+      step_events_completed = current_block->step_event_count;
237
+    }
65
 
238
 
66
-// Get current axis position in mm
67
-float st_get_axis_position_mm(AxisEnum axis);
239
+    //
240
+    // Handle a triggered endstop
241
+    //
242
+    void endstop_triggered(AxisEnum axis);
68
 
243
 
69
-// The stepper subsystem goes to sleep when it runs out of things to execute. Call this
70
-// to notify the subsystem that it is time to go to work.
71
-void st_wake_up();
244
+    //
245
+    // Triggered position of an axis in mm (not core-savvy)
246
+    //
247
+    FORCE_INLINE float triggered_position_mm(AxisEnum axis) {
248
+      return endstops_trigsteps[axis] / axis_steps_per_unit[axis];
249
+    }
72
 
250
 
251
+    FORCE_INLINE unsigned short calc_timer(unsigned short step_rate) {
252
+      unsigned short timer;
73
 
253
 
74
-void checkHitEndstops(); //call from somewhere to create an serial error message with the locations the endstops where hit, in case they were triggered
75
-void endstops_hit_on_purpose(); //avoid creation of the message, i.e. after homing and before a routine call of checkHitEndstops();
254
+      NOMORE(step_rate, MAX_STEP_FREQUENCY);
76
 
255
 
77
-void enable_endstops(bool check); // Enable/disable endstop checking
256
+      if (step_rate > 20000) { // If steprate > 20kHz >> step 4 times
257
+        step_rate = (step_rate >> 2) & 0x3fff;
258
+        step_loops = 4;
259
+      }
260
+      else if (step_rate > 10000) { // If steprate > 10kHz >> step 2 times
261
+        step_rate = (step_rate >> 1) & 0x7fff;
262
+        step_loops = 2;
263
+      }
264
+      else {
265
+        step_loops = 1;
266
+      }
78
 
267
 
79
-void enable_endstops_globally(bool check);
80
-void endstops_not_homing();
268
+      NOLESS(step_rate, F_CPU / 500000);
269
+      step_rate -= F_CPU / 500000; // Correct for minimal speed
270
+      if (step_rate >= (8 * 256)) { // higher step rate
271
+        unsigned short table_address = (unsigned short)&speed_lookuptable_fast[(unsigned char)(step_rate >> 8)][0];
272
+        unsigned char tmp_step_rate = (step_rate & 0x00ff);
273
+        unsigned short gain = (unsigned short)pgm_read_word_near(table_address + 2);
274
+        MultiU16X8toH16(timer, tmp_step_rate, gain);
275
+        timer = (unsigned short)pgm_read_word_near(table_address) - timer;
276
+      }
277
+      else { // lower step rates
278
+        unsigned short table_address = (unsigned short)&speed_lookuptable_slow[0][0];
279
+        table_address += ((step_rate) >> 1) & 0xfffc;
280
+        timer = (unsigned short)pgm_read_word_near(table_address);
281
+        timer -= (((unsigned short)pgm_read_word_near(table_address + 2) * (unsigned char)(step_rate & 0x0007)) >> 3);
282
+      }
283
+      if (timer < 100) { timer = 100; MYSERIAL.print(MSG_STEPPER_TOO_HIGH); MYSERIAL.println(step_rate); }//(20kHz this should never happen)
284
+      return timer;
285
+    }
81
 
286
 
82
-void checkStepperErrors(); //Print errors detected by the stepper
287
+    // Initializes the trapezoid generator from the current block. Called whenever a new
288
+    // block begins.
289
+    FORCE_INLINE void trapezoid_generator_reset() {
83
 
290
 
84
-void finishAndDisableSteppers();
291
+      static int8_t last_extruder = -1;
85
 
292
 
86
-extern block_t* current_block;  // A pointer to the block currently being traced
293
+      if (current_block->direction_bits != last_direction_bits || current_block->active_extruder != last_extruder) {
294
+        last_direction_bits = current_block->direction_bits;
295
+        last_extruder = current_block->active_extruder;
296
+        set_directions();
297
+      }
87
 
298
 
88
-void quickStop();
299
+      #if ENABLED(ADVANCE)
300
+        advance = current_block->initial_advance;
301
+        final_advance = current_block->final_advance;
302
+        // Do E steps + advance steps
303
+        e_steps[current_block->active_extruder] += ((advance >>8) - old_advance);
304
+        old_advance = advance >>8;
305
+      #endif
306
+      deceleration_time = 0;
307
+      // step_rate to timer interval
308
+      OCR1A_nominal = calc_timer(current_block->nominal_rate);
309
+      // make a note of the number of step loops required at nominal speed
310
+      step_loops_nominal = step_loops;
311
+      acc_step_rate = current_block->initial_rate;
312
+      acceleration_time = calc_timer(acc_step_rate);
313
+      OCR1A = acceleration_time;
89
 
314
 
90
-#if HAS_DIGIPOTSS
91
-  void digitalPotWrite(int address, int value);
92
-#endif
93
-void microstep_ms(uint8_t driver, int8_t ms1, int8_t ms2);
94
-void microstep_mode(uint8_t driver, uint8_t stepping);
95
-void digipot_init();
96
-void digipot_current(uint8_t driver, int current);
97
-void microstep_init();
98
-void microstep_readings();
315
+      // SERIAL_ECHO_START;
316
+      // SERIAL_ECHOPGM("advance :");
317
+      // SERIAL_ECHO(current_block->advance/256.0);
318
+      // SERIAL_ECHOPGM("advance rate :");
319
+      // SERIAL_ECHO(current_block->advance_rate/256.0);
320
+      // SERIAL_ECHOPGM("initial advance :");
321
+      // SERIAL_ECHO(current_block->initial_advance/256.0);
322
+      // SERIAL_ECHOPGM("final advance :");
323
+      // SERIAL_ECHOLN(current_block->final_advance/256.0);
324
+    }
99
 
325
 
100
-#if ENABLED(Z_DUAL_ENDSTOPS)
101
-  void In_Homing_Process(bool state);
102
-  void Lock_z_motor(bool state);
103
-  void Lock_z2_motor(bool state);
104
-#endif
326
+  private:
327
+    void microstep_mode(uint8_t driver, uint8_t stepping);
328
+    void digipot_init();
329
+    void microstep_init();
105
 
330
 
106
-#if ENABLED(BABYSTEPPING)
107
-  void babystep(const uint8_t axis, const bool direction); // perform a short step with a single stepper motor, outside of any convention
108
-#endif
331
+};
109
 
332
 
110
-#endif
333
+#endif // STEPPER_H

+ 1
- 1
Marlin/temperature.cpp 查看文件

604
         #if ENABLED(PID_ADD_EXTRUSION_RATE)
604
         #if ENABLED(PID_ADD_EXTRUSION_RATE)
605
           cTerm[e] = 0;
605
           cTerm[e] = 0;
606
           if (e == active_extruder) {
606
           if (e == active_extruder) {
607
-            long e_position = st_get_position(E_AXIS);
607
+            long e_position = stepper.position(E_AXIS);
608
             if (e_position > last_position[e]) {
608
             if (e_position > last_position[e]) {
609
               lpq[lpq_ptr++] = e_position - last_position[e];
609
               lpq[lpq_ptr++] = e_position - last_position[e];
610
               last_position[e] = e_position;
610
               last_position[e] = e_position;

+ 3
- 3
Marlin/ultralcd.cpp 查看文件

476
   static void lcd_sdcard_resume() { card.startFileprint(); }
476
   static void lcd_sdcard_resume() { card.startFileprint(); }
477
 
477
 
478
   static void lcd_sdcard_stop() {
478
   static void lcd_sdcard_stop() {
479
-    quickStop();
479
+    stepper.quick_stop();
480
     card.sdprinting = false;
480
     card.sdprinting = false;
481
     card.closefile();
481
     card.closefile();
482
     autotempShutdown();
482
     autotempShutdown();
911
       current_position[Z_AXIS] = MESH_HOME_SEARCH_Z;
911
       current_position[Z_AXIS] = MESH_HOME_SEARCH_Z;
912
       line_to_current(Z_AXIS);
912
       line_to_current(Z_AXIS);
913
     #endif
913
     #endif
914
-    st_synchronize();
914
+    stepper.synchronize();
915
   }
915
   }
916
 
916
 
917
   static void _lcd_level_goto_next_point();
917
   static void _lcd_level_goto_next_point();
964
             #endif
964
             #endif
965
           ;
965
           ;
966
           line_to_current(Z_AXIS);
966
           line_to_current(Z_AXIS);
967
-          st_synchronize();
967
+          stepper.synchronize();
968
 
968
 
969
           mbl.active = true;
969
           mbl.active = true;
970
           enqueue_and_echo_commands_P(PSTR("G28"));
970
           enqueue_and_echo_commands_P(PSTR("G28"));

Loading…
取消
儲存