My Marlin configs for Fabrikator Mini and CTC i3 Pro B
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

stepper.h 29KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788
  1. /**
  2. * Marlin 3D Printer Firmware
  3. * Copyright (c) 2020 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 <https://www.gnu.org/licenses/>.
  20. *
  21. */
  22. #pragma once
  23. /**
  24. * stepper.h - stepper motor driver: executes motion plans of planner.c using the stepper motors
  25. * Derived from 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 <https://www.gnu.org/licenses/>.
  41. */
  42. #include "../inc/MarlinConfig.h"
  43. #include "planner.h"
  44. #include "stepper/indirection.h"
  45. #ifdef __AVR__
  46. #include "stepper/speed_lookuptable.h"
  47. #endif
  48. // Disable multiple steps per ISR
  49. //#define DISABLE_MULTI_STEPPING
  50. //
  51. // Estimate the amount of time the Stepper ISR will take to execute
  52. //
  53. /**
  54. * The method of calculating these cycle-constants is unclear.
  55. * Most of them are no longer used directly for pulse timing, and exist
  56. * only to estimate a maximum step rate based on the user's configuration.
  57. * As 32-bit processors continue to diverge, maintaining cycle counts
  58. * will become increasingly difficult and error-prone.
  59. */
  60. #ifdef CPU_32_BIT
  61. /**
  62. * Duration of START_TIMED_PULSE
  63. *
  64. * ...as measured on an LPC1768 with a scope and converted to cycles.
  65. * Not applicable to other 32-bit processors, but as long as others
  66. * take longer, pulses will be longer. For example the SKR Pro
  67. * (stm32f407zgt6) requires ~60 cyles.
  68. */
  69. #define TIMER_READ_ADD_AND_STORE_CYCLES 34UL
  70. // The base ISR takes 792 cycles
  71. #define ISR_BASE_CYCLES 792UL
  72. // Linear advance base time is 64 cycles
  73. #if ENABLED(LIN_ADVANCE)
  74. #define ISR_LA_BASE_CYCLES 64UL
  75. #else
  76. #define ISR_LA_BASE_CYCLES 0UL
  77. #endif
  78. // S curve interpolation adds 40 cycles
  79. #if ENABLED(S_CURVE_ACCELERATION)
  80. #define ISR_S_CURVE_CYCLES 40UL
  81. #else
  82. #define ISR_S_CURVE_CYCLES 0UL
  83. #endif
  84. // Stepper Loop base cycles
  85. #define ISR_LOOP_BASE_CYCLES 4UL
  86. // To start the step pulse, in the worst case takes
  87. #define ISR_START_STEPPER_CYCLES 13UL
  88. // And each stepper (start + stop pulse) takes in worst case
  89. #define ISR_STEPPER_CYCLES 16UL
  90. #else
  91. // Cycles to perform actions in START_TIMED_PULSE
  92. #define TIMER_READ_ADD_AND_STORE_CYCLES 13UL
  93. // The base ISR takes 752 cycles
  94. #define ISR_BASE_CYCLES 752UL
  95. // Linear advance base time is 32 cycles
  96. #if ENABLED(LIN_ADVANCE)
  97. #define ISR_LA_BASE_CYCLES 32UL
  98. #else
  99. #define ISR_LA_BASE_CYCLES 0UL
  100. #endif
  101. // S curve interpolation adds 160 cycles
  102. #if ENABLED(S_CURVE_ACCELERATION)
  103. #define ISR_S_CURVE_CYCLES 160UL
  104. #else
  105. #define ISR_S_CURVE_CYCLES 0UL
  106. #endif
  107. // Stepper Loop base cycles
  108. #define ISR_LOOP_BASE_CYCLES 32UL
  109. // To start the step pulse, in the worst case takes
  110. #define ISR_START_STEPPER_CYCLES 57UL
  111. // And each stepper (start + stop pulse) takes in worst case
  112. #define ISR_STEPPER_CYCLES 88UL
  113. #endif
  114. // If linear advance is disabled, the loop also handles them
  115. #if DISABLED(LIN_ADVANCE) && ENABLED(MIXING_EXTRUDER)
  116. #define ISR_MIXING_STEPPER_CYCLES ((MIXING_STEPPERS) * (ISR_STEPPER_CYCLES))
  117. #else
  118. #define ISR_MIXING_STEPPER_CYCLES 0UL
  119. #endif
  120. // Add time for each stepper
  121. #if HAS_X_STEP
  122. #define ISR_X_STEPPER_CYCLES ISR_STEPPER_CYCLES
  123. #endif
  124. #if HAS_Y_STEP
  125. #define ISR_Y_STEPPER_CYCLES ISR_STEPPER_CYCLES
  126. #endif
  127. #if HAS_Z_STEP
  128. #define ISR_Z_STEPPER_CYCLES ISR_STEPPER_CYCLES
  129. #endif
  130. #if HAS_I_STEP
  131. #define ISR_I_STEPPER_CYCLES ISR_STEPPER_CYCLES
  132. #endif
  133. #if HAS_J_STEP
  134. #define ISR_J_STEPPER_CYCLES ISR_STEPPER_CYCLES
  135. #endif
  136. #if HAS_K_STEP
  137. #define ISR_K_STEPPER_CYCLES ISR_STEPPER_CYCLES
  138. #endif
  139. #if HAS_U_STEP
  140. #define ISR_U_STEPPER_CYCLES ISR_STEPPER_CYCLES
  141. #endif
  142. #if HAS_V_STEP
  143. #define ISR_V_STEPPER_CYCLES ISR_STEPPER_CYCLES
  144. #endif
  145. #if HAS_W_STEP
  146. #define ISR_W_STEPPER_CYCLES ISR_STEPPER_CYCLES
  147. #endif
  148. #if HAS_EXTRUDERS
  149. #define ISR_E_STEPPER_CYCLES ISR_STEPPER_CYCLES // E is always interpolated, even for mixing extruders
  150. #endif
  151. // And the total minimum loop time, not including the base
  152. #define MIN_ISR_LOOP_CYCLES (ISR_MIXING_STEPPER_CYCLES LOGICAL_AXIS_GANG(+ ISR_E_STEPPER_CYCLES, + ISR_X_STEPPER_CYCLES, + ISR_Y_STEPPER_CYCLES, + ISR_Z_STEPPER_CYCLES, + ISR_I_STEPPER_CYCLES, + ISR_J_STEPPER_CYCLES, + ISR_K_STEPPER_CYCLES, + ISR_U_STEPPER_CYCLES, + ISR_V_STEPPER_CYCLES, + ISR_W_STEPPER_CYCLES))
  153. // Calculate the minimum MPU cycles needed per pulse to enforce, limited to the max stepper rate
  154. #define _MIN_STEPPER_PULSE_CYCLES(N) _MAX(uint32_t((F_CPU) / (MAXIMUM_STEPPER_RATE)), ((F_CPU) / 500000UL) * (N))
  155. #if MINIMUM_STEPPER_PULSE
  156. #define MIN_STEPPER_PULSE_CYCLES _MIN_STEPPER_PULSE_CYCLES(uint32_t(MINIMUM_STEPPER_PULSE))
  157. #elif HAS_DRIVER(LV8729)
  158. #define MIN_STEPPER_PULSE_CYCLES uint32_t((((F_CPU) - 1) / 2000000) + 1) // 0.5µs, aka 500ns
  159. #else
  160. #define MIN_STEPPER_PULSE_CYCLES _MIN_STEPPER_PULSE_CYCLES(1UL)
  161. #endif
  162. // Calculate the minimum pulse times (high and low)
  163. #if MINIMUM_STEPPER_PULSE && MAXIMUM_STEPPER_RATE
  164. constexpr uint32_t _MIN_STEP_PERIOD_NS = 1000000000UL / MAXIMUM_STEPPER_RATE;
  165. constexpr uint32_t _MIN_PULSE_HIGH_NS = 1000UL * MINIMUM_STEPPER_PULSE;
  166. constexpr uint32_t _MIN_PULSE_LOW_NS = _MAX((_MIN_STEP_PERIOD_NS - _MIN(_MIN_STEP_PERIOD_NS, _MIN_PULSE_HIGH_NS)), _MIN_PULSE_HIGH_NS);
  167. #elif MINIMUM_STEPPER_PULSE
  168. // Assume 50% duty cycle
  169. constexpr uint32_t _MIN_PULSE_HIGH_NS = 1000UL * MINIMUM_STEPPER_PULSE;
  170. constexpr uint32_t _MIN_PULSE_LOW_NS = _MIN_PULSE_HIGH_NS;
  171. #elif MAXIMUM_STEPPER_RATE
  172. // Assume 50% duty cycle
  173. constexpr uint32_t _MIN_PULSE_HIGH_NS = 500000000UL / MAXIMUM_STEPPER_RATE;
  174. constexpr uint32_t _MIN_PULSE_LOW_NS = _MIN_PULSE_HIGH_NS;
  175. #else
  176. #error "Expected at least one of MINIMUM_STEPPER_PULSE or MAXIMUM_STEPPER_RATE to be defined"
  177. #endif
  178. // But the user could be enforcing a minimum time, so the loop time is
  179. #define ISR_LOOP_CYCLES (ISR_LOOP_BASE_CYCLES + _MAX(MIN_STEPPER_PULSE_CYCLES, MIN_ISR_LOOP_CYCLES))
  180. // If linear advance is enabled, then it is handled separately
  181. #if ENABLED(LIN_ADVANCE)
  182. // Estimate the minimum LA loop time
  183. #if ENABLED(MIXING_EXTRUDER) // ToDo: ???
  184. // HELP ME: What is what?
  185. // Directions are set up for MIXING_STEPPERS - like before.
  186. // Finding the right stepper may last up to MIXING_STEPPERS loops in get_next_stepper().
  187. // These loops are a bit faster than advancing a bresenham counter.
  188. // Always only one E stepper is stepped.
  189. #define MIN_ISR_LA_LOOP_CYCLES ((MIXING_STEPPERS) * (ISR_STEPPER_CYCLES))
  190. #else
  191. #define MIN_ISR_LA_LOOP_CYCLES ISR_STEPPER_CYCLES
  192. #endif
  193. // And the real loop time
  194. #define ISR_LA_LOOP_CYCLES _MAX(MIN_STEPPER_PULSE_CYCLES, MIN_ISR_LA_LOOP_CYCLES)
  195. #else
  196. #define ISR_LA_LOOP_CYCLES 0UL
  197. #endif
  198. // Now estimate the total ISR execution time in cycles given a step per ISR multiplier
  199. #define ISR_EXECUTION_CYCLES(R) (((ISR_BASE_CYCLES + ISR_S_CURVE_CYCLES + (ISR_LOOP_CYCLES) * (R) + ISR_LA_BASE_CYCLES + ISR_LA_LOOP_CYCLES)) / (R))
  200. // The maximum allowable stepping frequency when doing x128-x1 stepping (in Hz)
  201. #define MAX_STEP_ISR_FREQUENCY_128X ((F_CPU) / ISR_EXECUTION_CYCLES(128))
  202. #define MAX_STEP_ISR_FREQUENCY_64X ((F_CPU) / ISR_EXECUTION_CYCLES(64))
  203. #define MAX_STEP_ISR_FREQUENCY_32X ((F_CPU) / ISR_EXECUTION_CYCLES(32))
  204. #define MAX_STEP_ISR_FREQUENCY_16X ((F_CPU) / ISR_EXECUTION_CYCLES(16))
  205. #define MAX_STEP_ISR_FREQUENCY_8X ((F_CPU) / ISR_EXECUTION_CYCLES(8))
  206. #define MAX_STEP_ISR_FREQUENCY_4X ((F_CPU) / ISR_EXECUTION_CYCLES(4))
  207. #define MAX_STEP_ISR_FREQUENCY_2X ((F_CPU) / ISR_EXECUTION_CYCLES(2))
  208. #define MAX_STEP_ISR_FREQUENCY_1X ((F_CPU) / ISR_EXECUTION_CYCLES(1))
  209. // The minimum step ISR rate used by ADAPTIVE_STEP_SMOOTHING to target 50% CPU usage
  210. // This does not account for the possibility of multi-stepping.
  211. // Perhaps DISABLE_MULTI_STEPPING should be required with ADAPTIVE_STEP_SMOOTHING.
  212. #define MIN_STEP_ISR_FREQUENCY (MAX_STEP_ISR_FREQUENCY_1X / 2)
  213. #define ENABLE_COUNT (NUM_AXES + E_STEPPERS)
  214. #if ENABLE_COUNT > 16
  215. typedef uint32_t ena_mask_t;
  216. #else
  217. typedef IF<(ENABLE_COUNT > 8), uint16_t, uint8_t>::type ena_mask_t;
  218. #endif
  219. // Axis flags type, for enabled state or other simple state
  220. typedef struct {
  221. union {
  222. ena_mask_t bits;
  223. struct {
  224. bool NUM_AXIS_LIST(X:1, Y:1, Z:1, I:1, J:1, K:1, U:1, V:1, W:1);
  225. #if HAS_EXTRUDERS
  226. bool LIST_N(EXTRUDERS, E0:1, E1:1, E2:1, E3:1, E4:1, E5:1, E6:1, E7:1);
  227. #endif
  228. };
  229. };
  230. } stepper_flags_t;
  231. // All the stepper enable pins
  232. constexpr pin_t ena_pins[] = {
  233. NUM_AXIS_LIST(X_ENABLE_PIN, Y_ENABLE_PIN, Z_ENABLE_PIN, I_ENABLE_PIN, J_ENABLE_PIN, K_ENABLE_PIN, U_ENABLE_PIN, V_ENABLE_PIN, W_ENABLE_PIN),
  234. LIST_N(E_STEPPERS, E0_ENABLE_PIN, E1_ENABLE_PIN, E2_ENABLE_PIN, E3_ENABLE_PIN, E4_ENABLE_PIN, E5_ENABLE_PIN, E6_ENABLE_PIN, E7_ENABLE_PIN)
  235. };
  236. // Index of the axis or extruder element in a combined array
  237. constexpr uint8_t index_of_axis(const AxisEnum axis E_OPTARG(const uint8_t eindex=0)) {
  238. return uint8_t(axis) + (E_TERN0(axis < NUM_AXES ? 0 : eindex));
  239. }
  240. //#define __IAX_N(N,V...) _IAX_##N(V)
  241. //#define _IAX_N(N,V...) __IAX_N(N,V)
  242. //#define _IAX_1(A) index_of_axis(A)
  243. //#define _IAX_2(A,B) index_of_axis(A E_OPTARG(B))
  244. //#define INDEX_OF_AXIS(V...) _IAX_N(TWO_ARGS(V),V)
  245. #define INDEX_OF_AXIS(A,V...) index_of_axis(A E_OPTARG(V+0))
  246. // Bit mask for a matching enable pin, or 0
  247. constexpr ena_mask_t ena_same(const uint8_t a, const uint8_t b) {
  248. return ena_pins[a] == ena_pins[b] ? _BV(b) : 0;
  249. }
  250. // Recursively get the enable overlaps mask for a given linear axis or extruder
  251. constexpr ena_mask_t ena_overlap(const uint8_t a=0, const uint8_t b=0) {
  252. return b >= ENABLE_COUNT ? 0 : (a == b ? 0 : ena_same(a, b)) | ena_overlap(a, b + 1);
  253. }
  254. // Recursively get whether there's any overlap at all
  255. constexpr bool any_enable_overlap(const uint8_t a=0) {
  256. return a >= ENABLE_COUNT ? false : ena_overlap(a) || any_enable_overlap(a + 1);
  257. }
  258. // Array of axes that overlap with each
  259. // TODO: Consider cases where >=2 steppers are used by a linear axis or extruder
  260. // (e.g., CoreXY, Dual XYZ, or E with multiple steppers, etc.).
  261. constexpr ena_mask_t enable_overlap[] = {
  262. #define _OVERLAP(N) ena_overlap(INDEX_OF_AXIS(AxisEnum(N))),
  263. REPEAT(NUM_AXES, _OVERLAP)
  264. #if HAS_EXTRUDERS
  265. #define _E_OVERLAP(N) ena_overlap(INDEX_OF_AXIS(E_AXIS, N)),
  266. REPEAT(E_STEPPERS, _E_OVERLAP)
  267. #endif
  268. };
  269. //static_assert(!any_enable_overlap(), "There is some overlap.");
  270. #if ENABLED(INPUT_SHAPING)
  271. typedef IF<ENABLED(__AVR__), uint16_t, uint32_t>::type shaping_time_t;
  272. // These constexpr are used to calculate the shaping queue buffer sizes
  273. constexpr xyze_float_t max_feedrate = DEFAULT_MAX_FEEDRATE;
  274. constexpr xyze_float_t steps_per_unit = DEFAULT_AXIS_STEPS_PER_UNIT;
  275. constexpr float max_steprate = _MAX(LOGICAL_AXIS_LIST(
  276. max_feedrate.e * steps_per_unit.e,
  277. max_feedrate.x * steps_per_unit.x,
  278. max_feedrate.y * steps_per_unit.y,
  279. max_feedrate.z * steps_per_unit.z,
  280. max_feedrate.i * steps_per_unit.i,
  281. max_feedrate.j * steps_per_unit.j,
  282. max_feedrate.k * steps_per_unit.k,
  283. max_feedrate.u * steps_per_unit.u,
  284. max_feedrate.v * steps_per_unit.v,
  285. max_feedrate.w * steps_per_unit.w
  286. ));
  287. constexpr uint16_t shaping_dividends = max_steprate / _MIN(0x7FFFFFFFL OPTARG(HAS_SHAPING_X, SHAPING_FREQ_X) OPTARG(HAS_SHAPING_Y, SHAPING_FREQ_Y)) / 2 + 3;
  288. constexpr uint16_t shaping_segments = max_steprate / (MIN_STEPS_PER_SEGMENT) / _MIN(0x7FFFFFFFL OPTARG(HAS_SHAPING_X, SHAPING_FREQ_X) OPTARG(HAS_SHAPING_Y, SHAPING_FREQ_Y)) / 2 + 3;
  289. class DelayTimeManager {
  290. private:
  291. static shaping_time_t now;
  292. #ifdef HAS_SHAPING_X
  293. static shaping_time_t delay_x;
  294. #endif
  295. #ifdef HAS_SHAPING_Y
  296. static shaping_time_t delay_y;
  297. #endif
  298. public:
  299. static void decrement_delays(const shaping_time_t interval) { now += interval; }
  300. static void set_delay(const AxisEnum axis, const shaping_time_t delay) {
  301. TERN_(HAS_SHAPING_X, if (axis == X_AXIS) delay_x = delay);
  302. TERN_(HAS_SHAPING_Y, if (axis == Y_AXIS) delay_y = delay);
  303. }
  304. };
  305. template<int SIZE>
  306. class DelayQueue : public DelayTimeManager {
  307. protected:
  308. shaping_time_t times[SIZE];
  309. uint16_t tail = 0 OPTARG(HAS_SHAPING_X, head_x = 0) OPTARG(HAS_SHAPING_Y, head_y = 0);
  310. public:
  311. void enqueue() {
  312. times[tail] = now;
  313. if (++tail == SIZE) tail = 0;
  314. }
  315. #ifdef HAS_SHAPING_X
  316. shaping_time_t peek_x() {
  317. if (head_x != tail) return times[head_x] + delay_x - now;
  318. else return shaping_time_t(-1);
  319. }
  320. void dequeue_x() { if (++head_x == SIZE) head_x = 0; }
  321. bool empty_x() { return head_x == tail; }
  322. uint16_t free_count_x() { return head_x > tail ? head_x - tail - 1 : head_x + SIZE - tail - 1; }
  323. #endif
  324. #ifdef HAS_SHAPING_Y
  325. shaping_time_t peek_y() {
  326. if (head_y != tail) return times[head_y] + delay_y - now;
  327. else return shaping_time_t(-1);
  328. }
  329. void dequeue_y() { if (++head_y == SIZE) head_y = 0; }
  330. bool empty_y() { return head_y == tail; }
  331. uint16_t free_count_y() { return head_y > tail ? head_y - tail - 1 : head_y + SIZE - tail - 1; }
  332. #endif
  333. void purge() { auto temp = TERN_(HAS_SHAPING_X, head_x) = TERN_(HAS_SHAPING_Y, head_y) = tail; UNUSED(temp);}
  334. };
  335. class ParamDelayQueue : public DelayQueue<shaping_segments> {
  336. private:
  337. #ifdef HAS_SHAPING_X
  338. int32_t params_x[shaping_segments];
  339. #endif
  340. #ifdef HAS_SHAPING_Y
  341. int32_t params_y[shaping_segments];
  342. #endif
  343. public:
  344. void enqueue(const int32_t param_x, const int32_t param_y) {
  345. TERN(HAS_SHAPING_X, params_x[DelayQueue<shaping_segments>::tail] = param_x, UNUSED(param_x));
  346. TERN(HAS_SHAPING_Y, params_y[DelayQueue<shaping_segments>::tail] = param_y, UNUSED(param_y));
  347. DelayQueue<shaping_segments>::enqueue();
  348. }
  349. #ifdef HAS_SHAPING_X
  350. const int32_t dequeue_x() {
  351. const int32_t result = params_x[DelayQueue<shaping_segments>::head_x];
  352. DelayQueue<shaping_segments>::dequeue_x();
  353. return result;
  354. }
  355. #endif
  356. #ifdef HAS_SHAPING_Y
  357. const int32_t dequeue_y() {
  358. const int32_t result = params_y[DelayQueue<shaping_segments>::head_y];
  359. DelayQueue<shaping_segments>::dequeue_y();
  360. return result;
  361. }
  362. #endif
  363. };
  364. struct ShapeParams {
  365. float frequency;
  366. float zeta;
  367. uint8_t factor;
  368. int32_t dividend;
  369. };
  370. #endif // INPUT_SHAPING
  371. //
  372. // Stepper class definition
  373. //
  374. class Stepper {
  375. friend class KinematicSystem;
  376. friend class DeltaKinematicSystem;
  377. friend void stepperTask(void *);
  378. public:
  379. #if EITHER(HAS_EXTRA_ENDSTOPS, Z_STEPPER_AUTO_ALIGN)
  380. static bool separate_multi_axis;
  381. #endif
  382. #if HAS_MOTOR_CURRENT_SPI || HAS_MOTOR_CURRENT_PWM
  383. #if HAS_MOTOR_CURRENT_PWM
  384. #ifndef PWM_MOTOR_CURRENT
  385. #define PWM_MOTOR_CURRENT DEFAULT_PWM_MOTOR_CURRENT
  386. #endif
  387. #ifndef MOTOR_CURRENT_PWM_FREQUENCY
  388. #define MOTOR_CURRENT_PWM_FREQUENCY 31400
  389. #endif
  390. #define MOTOR_CURRENT_COUNT 3
  391. #elif HAS_MOTOR_CURRENT_SPI
  392. static constexpr uint32_t digipot_count[] = DIGIPOT_MOTOR_CURRENT;
  393. #define MOTOR_CURRENT_COUNT COUNT(Stepper::digipot_count)
  394. #endif
  395. static bool initialized;
  396. static uint32_t motor_current_setting[MOTOR_CURRENT_COUNT]; // Initialized by settings.load()
  397. #endif
  398. // Last-moved extruder, as set when the last movement was fetched from planner
  399. #if HAS_MULTI_EXTRUDER
  400. static uint8_t last_moved_extruder;
  401. #else
  402. static constexpr uint8_t last_moved_extruder = 0;
  403. #endif
  404. #if ENABLED(FREEZE_FEATURE)
  405. static bool frozen; // Set this flag to instantly freeze motion
  406. #endif
  407. private:
  408. static block_t* current_block; // A pointer to the block currently being traced
  409. static axis_bits_t last_direction_bits, // The next stepping-bits to be output
  410. axis_did_move; // Last Movement in the given direction is not null, as computed when the last movement was fetched from planner
  411. static bool abort_current_block; // Signals to the stepper that current block should be aborted
  412. #if ENABLED(X_DUAL_ENDSTOPS)
  413. static bool locked_X_motor, locked_X2_motor;
  414. #endif
  415. #if ENABLED(Y_DUAL_ENDSTOPS)
  416. static bool locked_Y_motor, locked_Y2_motor;
  417. #endif
  418. #if EITHER(Z_MULTI_ENDSTOPS, Z_STEPPER_AUTO_ALIGN)
  419. static bool locked_Z_motor, locked_Z2_motor
  420. #if NUM_Z_STEPPERS >= 3
  421. , locked_Z3_motor
  422. #if NUM_Z_STEPPERS >= 4
  423. , locked_Z4_motor
  424. #endif
  425. #endif
  426. ;
  427. #endif
  428. static uint32_t acceleration_time, deceleration_time; // time measured in Stepper Timer ticks
  429. static uint8_t steps_per_isr; // Count of steps to perform per Stepper ISR call
  430. #if ENABLED(ADAPTIVE_STEP_SMOOTHING)
  431. static uint8_t oversampling_factor; // Oversampling factor (log2(multiplier)) to increase temporal resolution of axis
  432. #else
  433. static constexpr uint8_t oversampling_factor = 0;
  434. #endif
  435. // Delta error variables for the Bresenham line tracer
  436. static xyze_long_t delta_error;
  437. static xyze_long_t advance_dividend;
  438. static uint32_t advance_divisor,
  439. step_events_completed, // The number of step events executed in the current block
  440. accelerate_until, // The point from where we need to stop acceleration
  441. decelerate_after, // The point from where we need to start decelerating
  442. step_event_count; // The total event count for the current block
  443. #if EITHER(HAS_MULTI_EXTRUDER, MIXING_EXTRUDER)
  444. static uint8_t stepper_extruder;
  445. #else
  446. static constexpr uint8_t stepper_extruder = 0;
  447. #endif
  448. #if ENABLED(S_CURVE_ACCELERATION)
  449. static int32_t bezier_A, // A coefficient in Bézier speed curve
  450. bezier_B, // B coefficient in Bézier speed curve
  451. bezier_C; // C coefficient in Bézier speed curve
  452. static uint32_t bezier_F, // F coefficient in Bézier speed curve
  453. bezier_AV; // AV coefficient in Bézier speed curve
  454. #ifdef __AVR__
  455. static bool A_negative; // If A coefficient was negative
  456. #endif
  457. static bool bezier_2nd_half; // If Bézier curve has been initialized or not
  458. #endif
  459. #if ENABLED(INPUT_SHAPING)
  460. static ParamDelayQueue shaping_dividend_queue;
  461. static DelayQueue<shaping_dividends> shaping_queue;
  462. #if HAS_SHAPING_X
  463. static ShapeParams shaping_x;
  464. #endif
  465. #if HAS_SHAPING_Y
  466. static ShapeParams shaping_y;
  467. #endif
  468. #endif
  469. #if ENABLED(LIN_ADVANCE)
  470. static constexpr uint32_t LA_ADV_NEVER = 0xFFFFFFFF;
  471. static uint32_t nextAdvanceISR,
  472. la_interval; // Interval between ISR calls for LA
  473. static int32_t la_delta_error, // Analogue of delta_error.e for E steps in LA ISR
  474. la_dividend, // Analogue of advance_dividend.e for E steps in LA ISR
  475. la_advance_steps; // Count of steps added to increase nozzle pressure
  476. #endif
  477. #if ENABLED(INTEGRATED_BABYSTEPPING)
  478. static constexpr uint32_t BABYSTEP_NEVER = 0xFFFFFFFF;
  479. static uint32_t nextBabystepISR;
  480. #endif
  481. #if ENABLED(DIRECT_STEPPING)
  482. static page_step_state_t page_step_state;
  483. #endif
  484. static int32_t ticks_nominal;
  485. #if DISABLED(S_CURVE_ACCELERATION)
  486. static uint32_t acc_step_rate; // needed for deceleration start point
  487. #endif
  488. // Exact steps at which an endstop was triggered
  489. static xyz_long_t endstops_trigsteps;
  490. // Positions of stepper motors, in step units
  491. static xyze_long_t count_position;
  492. // Current stepper motor directions (+1 or -1)
  493. static xyze_int8_t count_direction;
  494. public:
  495. // Initialize stepper hardware
  496. static void init();
  497. // Interrupt Service Routine and phases
  498. // The stepper subsystem goes to sleep when it runs out of things to execute.
  499. // Call this to notify the subsystem that it is time to go to work.
  500. static void wake_up() { ENABLE_STEPPER_DRIVER_INTERRUPT(); }
  501. static bool is_awake() { return STEPPER_ISR_ENABLED(); }
  502. static bool suspend() {
  503. const bool awake = is_awake();
  504. if (awake) DISABLE_STEPPER_DRIVER_INTERRUPT();
  505. return awake;
  506. }
  507. // The ISR scheduler
  508. static void isr();
  509. // The stepper pulse ISR phase
  510. static void pulse_phase_isr();
  511. // The stepper block processing ISR phase
  512. static uint32_t block_phase_isr();
  513. #if ENABLED(INPUT_SHAPING)
  514. static void shaping_isr();
  515. #endif
  516. #if ENABLED(LIN_ADVANCE)
  517. // The Linear advance ISR phase
  518. static void advance_isr();
  519. #endif
  520. #if ENABLED(INTEGRATED_BABYSTEPPING)
  521. // The Babystepping ISR phase
  522. static uint32_t babystepping_isr();
  523. FORCE_INLINE static void initiateBabystepping() {
  524. if (nextBabystepISR == BABYSTEP_NEVER) {
  525. nextBabystepISR = 0;
  526. wake_up();
  527. }
  528. }
  529. #endif
  530. // Check if the given block is busy or not - Must not be called from ISR contexts
  531. static bool is_block_busy(const block_t * const block);
  532. // Get the position of a stepper, in steps
  533. static int32_t position(const AxisEnum axis);
  534. // Set the current position in steps
  535. static void set_position(const xyze_long_t &spos);
  536. static void set_axis_position(const AxisEnum a, const int32_t &v);
  537. // Report the positions of the steppers, in steps
  538. static void report_a_position(const xyz_long_t &pos);
  539. static void report_positions();
  540. // Discard current block and free any resources
  541. FORCE_INLINE static void discard_current_block() {
  542. #if ENABLED(DIRECT_STEPPING)
  543. if (current_block->is_page()) page_manager.free_page(current_block->page_idx);
  544. #endif
  545. current_block = nullptr;
  546. axis_did_move = 0;
  547. planner.release_current_block();
  548. TERN_(LIN_ADVANCE, la_interval = nextAdvanceISR = LA_ADV_NEVER);
  549. }
  550. // Quickly stop all steppers
  551. FORCE_INLINE static void quick_stop() { abort_current_block = true; }
  552. // The direction of a single motor
  553. FORCE_INLINE static bool motor_direction(const AxisEnum axis) { return TEST(last_direction_bits, axis); }
  554. // The last movement direction was not null on the specified axis. Note that motor direction is not necessarily the same.
  555. FORCE_INLINE static bool axis_is_moving(const AxisEnum axis) { return TEST(axis_did_move, axis); }
  556. // Handle a triggered endstop
  557. static void endstop_triggered(const AxisEnum axis);
  558. // Triggered position of an axis in steps
  559. static int32_t triggered_position(const AxisEnum axis);
  560. #if HAS_MOTOR_CURRENT_SPI || HAS_MOTOR_CURRENT_PWM
  561. static void set_digipot_value_spi(const int16_t address, const int16_t value);
  562. static void set_digipot_current(const uint8_t driver, const int16_t current);
  563. #endif
  564. #if HAS_MICROSTEPS
  565. static void microstep_ms(const uint8_t driver, const int8_t ms1, const int8_t ms2, const int8_t ms3);
  566. static void microstep_mode(const uint8_t driver, const uint8_t stepping);
  567. static void microstep_readings();
  568. #endif
  569. #if EITHER(HAS_EXTRA_ENDSTOPS, Z_STEPPER_AUTO_ALIGN)
  570. FORCE_INLINE static void set_separate_multi_axis(const bool state) { separate_multi_axis = state; }
  571. #endif
  572. #if ENABLED(X_DUAL_ENDSTOPS)
  573. FORCE_INLINE static void set_x_lock(const bool state) { locked_X_motor = state; }
  574. FORCE_INLINE static void set_x2_lock(const bool state) { locked_X2_motor = state; }
  575. #endif
  576. #if ENABLED(Y_DUAL_ENDSTOPS)
  577. FORCE_INLINE static void set_y_lock(const bool state) { locked_Y_motor = state; }
  578. FORCE_INLINE static void set_y2_lock(const bool state) { locked_Y2_motor = state; }
  579. #endif
  580. #if EITHER(Z_MULTI_ENDSTOPS, Z_STEPPER_AUTO_ALIGN)
  581. FORCE_INLINE static void set_z1_lock(const bool state) { locked_Z_motor = state; }
  582. FORCE_INLINE static void set_z2_lock(const bool state) { locked_Z2_motor = state; }
  583. #if NUM_Z_STEPPERS >= 3
  584. FORCE_INLINE static void set_z3_lock(const bool state) { locked_Z3_motor = state; }
  585. #if NUM_Z_STEPPERS >= 4
  586. FORCE_INLINE static void set_z4_lock(const bool state) { locked_Z4_motor = state; }
  587. #endif
  588. #endif
  589. static void set_all_z_lock(const bool lock, const int8_t except=-1) {
  590. set_z1_lock(lock ^ (except == 0));
  591. set_z2_lock(lock ^ (except == 1));
  592. #if NUM_Z_STEPPERS >= 3
  593. set_z3_lock(lock ^ (except == 2));
  594. #if NUM_Z_STEPPERS >= 4
  595. set_z4_lock(lock ^ (except == 3));
  596. #endif
  597. #endif
  598. }
  599. #endif
  600. #if ENABLED(BABYSTEPPING)
  601. static void do_babystep(const AxisEnum axis, const bool direction); // perform a short step with a single stepper motor, outside of any convention
  602. #endif
  603. #if HAS_MOTOR_CURRENT_PWM
  604. static void refresh_motor_power();
  605. #endif
  606. static stepper_flags_t axis_enabled; // Axis stepper(s) ENABLED states
  607. static bool axis_is_enabled(const AxisEnum axis E_OPTARG(const uint8_t eindex=0)) {
  608. return TEST(axis_enabled.bits, INDEX_OF_AXIS(axis, eindex));
  609. }
  610. static void mark_axis_enabled(const AxisEnum axis E_OPTARG(const uint8_t eindex=0)) {
  611. SBI(axis_enabled.bits, INDEX_OF_AXIS(axis, eindex));
  612. }
  613. static void mark_axis_disabled(const AxisEnum axis E_OPTARG(const uint8_t eindex=0)) {
  614. CBI(axis_enabled.bits, INDEX_OF_AXIS(axis, eindex));
  615. }
  616. static bool can_axis_disable(const AxisEnum axis E_OPTARG(const uint8_t eindex=0)) {
  617. return !any_enable_overlap() || !(axis_enabled.bits & enable_overlap[INDEX_OF_AXIS(axis, eindex)]);
  618. }
  619. static void enable_axis(const AxisEnum axis);
  620. static bool disable_axis(const AxisEnum axis);
  621. #if HAS_EXTRUDERS
  622. static void enable_extruder(E_TERN_(const uint8_t eindex=0));
  623. static bool disable_extruder(E_TERN_(const uint8_t eindex=0));
  624. static void enable_e_steppers();
  625. static void disable_e_steppers();
  626. #else
  627. static void enable_extruder() {}
  628. static bool disable_extruder() { return true; }
  629. static void enable_e_steppers() {}
  630. static void disable_e_steppers() {}
  631. #endif
  632. #define ENABLE_EXTRUDER(N) enable_extruder(E_TERN_(N))
  633. #define DISABLE_EXTRUDER(N) disable_extruder(E_TERN_(N))
  634. #define AXIS_IS_ENABLED(N,V...) axis_is_enabled(N E_OPTARG(#V))
  635. static void enable_all_steppers();
  636. static void disable_all_steppers();
  637. // Update direction states for all steppers
  638. static void set_directions();
  639. // Set direction bits and update all stepper DIR states
  640. static void set_directions(const axis_bits_t bits) {
  641. last_direction_bits = bits;
  642. set_directions();
  643. }
  644. #if ENABLED(INPUT_SHAPING)
  645. static void set_shaping_damping_ratio(const AxisEnum axis, const float zeta);
  646. static float get_shaping_damping_ratio(const AxisEnum axis);
  647. static void set_shaping_frequency(const AxisEnum axis, const float freq);
  648. static float get_shaping_frequency(const AxisEnum axis);
  649. #endif
  650. private:
  651. // Set the current position in steps
  652. static void _set_position(const abce_long_t &spos);
  653. // Calculate timing interval for the given step rate
  654. static uint32_t calc_timer_interval(uint32_t step_rate);
  655. static uint32_t calc_timer_interval(uint32_t step_rate, uint8_t &loops);
  656. #if ENABLED(S_CURVE_ACCELERATION)
  657. static void _calc_bezier_curve_coeffs(const int32_t v0, const int32_t v1, const uint32_t av);
  658. static int32_t _eval_bezier_curve(const uint32_t curr_step);
  659. #endif
  660. #if HAS_MOTOR_CURRENT_SPI || HAS_MOTOR_CURRENT_PWM
  661. static void digipot_init();
  662. #endif
  663. #if HAS_MICROSTEPS
  664. static void microstep_init();
  665. #endif
  666. };
  667. extern Stepper stepper;