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 13KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370
  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. * stepper.h - stepper motor driver: executes motion plans of planner.c using the stepper motors
  24. * Derived from Grbl
  25. *
  26. * Copyright (c) 2009-2011 Simen Svale Skogsrud
  27. *
  28. * Grbl is free software: you can redistribute it and/or modify
  29. * it under the terms of the GNU General Public License as published by
  30. * the Free Software Foundation, either version 3 of the License, or
  31. * (at your option) any later version.
  32. *
  33. * Grbl is distributed in the hope that it will be useful,
  34. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  35. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  36. * GNU General Public License for more details.
  37. *
  38. * You should have received a copy of the GNU General Public License
  39. * along with Grbl. If not, see <http://www.gnu.org/licenses/>.
  40. */
  41. #ifndef STEPPER_H
  42. #define STEPPER_H
  43. #include "stepper_indirection.h"
  44. #ifdef __AVR__
  45. #include "speed_lookuptable.h"
  46. #endif
  47. #include "../inc/MarlinConfig.h"
  48. #include "../module/planner.h"
  49. #include "../core/language.h"
  50. class Stepper;
  51. extern Stepper stepper;
  52. class Stepper {
  53. public:
  54. static block_t* current_block; // A pointer to the block currently being traced
  55. #if ENABLED(X_DUAL_ENDSTOPS) || ENABLED(Y_DUAL_ENDSTOPS) || ENABLED(Z_DUAL_ENDSTOPS)
  56. static bool homing_dual_axis;
  57. #endif
  58. #if HAS_MOTOR_CURRENT_PWM
  59. #ifndef PWM_MOTOR_CURRENT
  60. #define PWM_MOTOR_CURRENT DEFAULT_PWM_MOTOR_CURRENT
  61. #endif
  62. static uint32_t motor_current_setting[3];
  63. #endif
  64. private:
  65. static uint8_t last_direction_bits, // The next stepping-bits to be output
  66. axis_did_move; // Last Movement in the given direction is not null, as computed when the last movement was fetched from planner
  67. static bool abort_current_block; // Signals to the stepper that current block should be aborted
  68. #if DISABLED(MIXING_EXTRUDER)
  69. static uint8_t last_moved_extruder; // Last-moved extruder, as set when the last movement was fetched from planner
  70. #endif
  71. #if ENABLED(X_DUAL_ENDSTOPS)
  72. static bool locked_X_motor, locked_X2_motor;
  73. #endif
  74. #if ENABLED(Y_DUAL_ENDSTOPS)
  75. static bool locked_Y_motor, locked_Y2_motor;
  76. #endif
  77. #if ENABLED(Z_DUAL_ENDSTOPS)
  78. static bool locked_Z_motor, locked_Z2_motor;
  79. #endif
  80. static uint32_t acceleration_time, deceleration_time; // time measured in Stepper Timer ticks
  81. static uint8_t steps_per_isr; // Count of steps to perform per Stepper ISR call
  82. #if ENABLED(ADAPTIVE_STEP_SMOOTHING)
  83. static uint8_t oversampling_factor; // Oversampling factor (log2(multiplier)) to increase temporal resolution of axis
  84. #else
  85. static constexpr uint8_t oversampling_factor = 0;
  86. #endif
  87. // Delta error variables for the Bresenham line tracer
  88. static int32_t delta_error[XYZE];
  89. static uint32_t advance_dividend[XYZE],
  90. advance_divisor,
  91. step_events_completed, // The number of step events executed in the current block
  92. accelerate_until, // The point from where we need to stop acceleration
  93. decelerate_after, // The point from where we need to start decelerating
  94. step_event_count; // The total event count for the current block
  95. // Mixing extruder mix delta_errors for bresenham tracing
  96. #if ENABLED(MIXING_EXTRUDER)
  97. static int32_t delta_error_m[MIXING_STEPPERS];
  98. static uint32_t advance_dividend_m[MIXING_STEPPERS],
  99. advance_divisor_m;
  100. #define MIXING_STEPPERS_LOOP(VAR) \
  101. for (uint8_t VAR = 0; VAR < MIXING_STEPPERS; VAR++)
  102. #else
  103. static int8_t active_extruder; // Active extruder
  104. #endif
  105. #if ENABLED(S_CURVE_ACCELERATION)
  106. static int32_t bezier_A, // A coefficient in Bézier speed curve
  107. bezier_B, // B coefficient in Bézier speed curve
  108. bezier_C; // C coefficient in Bézier speed curve
  109. static uint32_t bezier_F, // F coefficient in Bézier speed curve
  110. bezier_AV; // AV coefficient in Bézier speed curve
  111. #ifdef __AVR__
  112. static bool A_negative; // If A coefficient was negative
  113. #endif
  114. static bool bezier_2nd_half; // If Bézier curve has been initialized or not
  115. #endif
  116. static uint32_t nextMainISR; // time remaining for the next Step ISR
  117. #if ENABLED(LIN_ADVANCE)
  118. static uint32_t nextAdvanceISR, LA_isr_rate;
  119. static uint16_t LA_current_adv_steps, LA_final_adv_steps, LA_max_adv_steps; // Copy from current executed block. Needed because current_block is set to NULL "too early".
  120. static int8_t LA_steps;
  121. static bool LA_use_advance_lead;
  122. #endif // LIN_ADVANCE
  123. static int32_t ticks_nominal;
  124. #if DISABLED(S_CURVE_ACCELERATION)
  125. static uint32_t acc_step_rate; // needed for deceleration start point
  126. #endif
  127. static volatile int32_t endstops_trigsteps[XYZ];
  128. //
  129. // Positions of stepper motors, in step units
  130. //
  131. static volatile int32_t count_position[NUM_AXIS];
  132. //
  133. // Current direction of stepper motors (+1 or -1)
  134. //
  135. static int8_t count_direction[NUM_AXIS];
  136. public:
  137. //
  138. // Constructor / initializer
  139. //
  140. Stepper() { };
  141. // Initialize stepper hardware
  142. static void init();
  143. // Interrupt Service Routines
  144. // The ISR scheduler
  145. static void isr();
  146. // The stepper pulse phase ISR
  147. static void stepper_pulse_phase_isr();
  148. // The stepper block processing phase ISR
  149. static uint32_t stepper_block_phase_isr();
  150. #if ENABLED(LIN_ADVANCE)
  151. // The Linear advance stepper ISR
  152. static uint32_t advance_isr();
  153. #endif
  154. // Get the position of a stepper, in steps
  155. static int32_t position(const AxisEnum axis);
  156. // Report the positions of the steppers, in steps
  157. static void report_positions();
  158. // The stepper subsystem goes to sleep when it runs out of things to execute. Call this
  159. // to notify the subsystem that it is time to go to work.
  160. static void wake_up();
  161. // Quickly stop all steppers
  162. FORCE_INLINE static void quick_stop() { abort_current_block = true; }
  163. // The direction of a single motor
  164. FORCE_INLINE static bool motor_direction(const AxisEnum axis) { return TEST(last_direction_bits, axis); }
  165. // The last movement direction was not null on the specified axis. Note that motor direction is not necessarily the same.
  166. FORCE_INLINE static bool axis_is_moving(const AxisEnum axis) { return TEST(axis_did_move, axis); }
  167. // The extruder associated to the last movement
  168. FORCE_INLINE static uint8_t movement_extruder() {
  169. return
  170. #if ENABLED(MIXING_EXTRUDER)
  171. 0
  172. #else
  173. last_moved_extruder
  174. #endif
  175. ;
  176. }
  177. // Handle a triggered endstop
  178. static void endstop_triggered(const AxisEnum axis);
  179. // Triggered position of an axis in steps
  180. static int32_t triggered_position(const AxisEnum axis);
  181. #if HAS_DIGIPOTSS || HAS_MOTOR_CURRENT_PWM
  182. static void digitalPotWrite(const int16_t address, const int16_t value);
  183. static void digipot_current(const uint8_t driver, const int16_t current);
  184. #endif
  185. #if HAS_MICROSTEPS
  186. static void microstep_ms(const uint8_t driver, const int8_t ms1, const int8_t ms2);
  187. static void microstep_mode(const uint8_t driver, const uint8_t stepping);
  188. static void microstep_readings();
  189. #endif
  190. #if ENABLED(X_DUAL_ENDSTOPS) || ENABLED(Y_DUAL_ENDSTOPS) || ENABLED(Z_DUAL_ENDSTOPS)
  191. FORCE_INLINE static void set_homing_dual_axis(const bool state) { homing_dual_axis = state; }
  192. #endif
  193. #if ENABLED(X_DUAL_ENDSTOPS)
  194. FORCE_INLINE static void set_x_lock(const bool state) { locked_X_motor = state; }
  195. FORCE_INLINE static void set_x2_lock(const bool state) { locked_X2_motor = state; }
  196. #endif
  197. #if ENABLED(Y_DUAL_ENDSTOPS)
  198. FORCE_INLINE static void set_y_lock(const bool state) { locked_Y_motor = state; }
  199. FORCE_INLINE static void set_y2_lock(const bool state) { locked_Y2_motor = state; }
  200. #endif
  201. #if ENABLED(Z_DUAL_ENDSTOPS)
  202. FORCE_INLINE static void set_z_lock(const bool state) { locked_Z_motor = state; }
  203. FORCE_INLINE static void set_z2_lock(const bool state) { locked_Z2_motor = state; }
  204. #endif
  205. #if ENABLED(BABYSTEPPING)
  206. static void babystep(const AxisEnum axis, const bool direction); // perform a short step with a single stepper motor, outside of any convention
  207. #endif
  208. #if HAS_MOTOR_CURRENT_PWM
  209. static void refresh_motor_power();
  210. #endif
  211. // Set the current position in steps
  212. inline static void set_position(const int32_t &a, const int32_t &b, const int32_t &c, const int32_t &e) {
  213. planner.synchronize();
  214. const bool was_enabled = STEPPER_ISR_ENABLED();
  215. if (was_enabled) DISABLE_STEPPER_DRIVER_INTERRUPT();
  216. _set_position(a, b, c, e);
  217. if (was_enabled) ENABLE_STEPPER_DRIVER_INTERRUPT();
  218. }
  219. inline static void set_position(const AxisEnum a, const int32_t &v) {
  220. planner.synchronize();
  221. #ifdef __AVR__
  222. // Protect the access to the position. Only required for AVR, as
  223. // any 32bit CPU offers atomic access to 32bit variables
  224. const bool was_enabled = STEPPER_ISR_ENABLED();
  225. if (was_enabled) DISABLE_STEPPER_DRIVER_INTERRUPT();
  226. #endif
  227. count_position[a] = v;
  228. #ifdef __AVR__
  229. // Reenable Stepper ISR
  230. if (was_enabled) ENABLE_STEPPER_DRIVER_INTERRUPT();
  231. #endif
  232. }
  233. private:
  234. // Set the current position in steps
  235. static void _set_position(const int32_t &a, const int32_t &b, const int32_t &c, const int32_t &e);
  236. // Set direction bits for all steppers
  237. static void set_directions();
  238. FORCE_INLINE static uint32_t calc_timer_interval(uint32_t step_rate, uint8_t scale, uint8_t* loops) {
  239. uint32_t timer;
  240. // Scale the frequency, as requested by the caller
  241. step_rate <<= scale;
  242. uint8_t multistep = 1;
  243. #if DISABLED(DISABLE_MULTI_STEPPING)
  244. // The stepping frequency limits for each multistepping rate
  245. static const uint32_t limit[] PROGMEM = {
  246. ( MAX_1X_STEP_ISR_FREQUENCY ),
  247. ( MAX_2X_STEP_ISR_FREQUENCY >> 1),
  248. ( MAX_4X_STEP_ISR_FREQUENCY >> 2),
  249. ( MAX_8X_STEP_ISR_FREQUENCY >> 3),
  250. ( MAX_16X_STEP_ISR_FREQUENCY >> 4),
  251. ( MAX_32X_STEP_ISR_FREQUENCY >> 5),
  252. ( MAX_64X_STEP_ISR_FREQUENCY >> 6),
  253. (MAX_128X_STEP_ISR_FREQUENCY >> 7)
  254. };
  255. // Select the proper multistepping
  256. uint8_t idx = 0;
  257. while (idx < 7 && step_rate > (uint32_t)pgm_read_dword(&limit[idx])) {
  258. step_rate >>= 1;
  259. multistep <<= 1;
  260. ++idx;
  261. };
  262. #else
  263. NOMORE(step_rate, uint32_t(MAX_1X_STEP_ISR_FREQUENCY));
  264. #endif
  265. *loops = multistep;
  266. #ifdef CPU_32_BIT
  267. // In case of high-performance processor, it is able to calculate in real-time
  268. timer = uint32_t(HAL_STEPPER_TIMER_RATE) / step_rate;
  269. #else
  270. constexpr uint32_t min_step_rate = F_CPU / 500000U;
  271. NOLESS(step_rate, min_step_rate);
  272. step_rate -= min_step_rate; // Correct for minimal speed
  273. if (step_rate >= (8 * 256)) { // higher step rate
  274. const uint8_t tmp_step_rate = (step_rate & 0x00FF);
  275. const uint16_t table_address = (uint16_t)&speed_lookuptable_fast[(uint8_t)(step_rate >> 8)][0],
  276. gain = (uint16_t)pgm_read_word_near(table_address + 2);
  277. timer = MultiU16X8toH16(tmp_step_rate, gain);
  278. timer = (uint16_t)pgm_read_word_near(table_address) - timer;
  279. }
  280. else { // lower step rates
  281. uint16_t table_address = (uint16_t)&speed_lookuptable_slow[0][0];
  282. table_address += ((step_rate) >> 1) & 0xFFFC;
  283. timer = (uint16_t)pgm_read_word_near(table_address)
  284. - (((uint16_t)pgm_read_word_near(table_address + 2) * (uint8_t)(step_rate & 0x0007)) >> 3);
  285. }
  286. // (there is no need to limit the timer value here. All limits have been
  287. // applied above, and AVR is able to keep up at 30khz Stepping ISR rate)
  288. #endif
  289. return timer;
  290. }
  291. #if ENABLED(S_CURVE_ACCELERATION)
  292. static void _calc_bezier_curve_coeffs(const int32_t v0, const int32_t v1, const uint32_t av);
  293. static int32_t _eval_bezier_curve(const uint32_t curr_step);
  294. #endif
  295. #if HAS_DIGIPOTSS || HAS_MOTOR_CURRENT_PWM
  296. static void digipot_init();
  297. #endif
  298. #if HAS_MICROSTEPS
  299. static void microstep_init();
  300. #endif
  301. };
  302. #endif // STEPPER_H