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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351
  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 performing_homing;
  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. last_movement_extruder, // Last movement extruder, as computed when the last movement was fetched from planner
  67. axis_did_move; // Last Movement in the given direction is not null, as computed when the last movement was fetched from planner
  68. static bool abort_current_block; // Signals to the stepper that current block should be aborted
  69. #if ENABLED(X_DUAL_ENDSTOPS)
  70. static bool locked_x_motor, locked_x2_motor;
  71. #endif
  72. #if ENABLED(Y_DUAL_ENDSTOPS)
  73. static bool locked_y_motor, locked_y2_motor;
  74. #endif
  75. #if ENABLED(Z_DUAL_ENDSTOPS)
  76. static bool locked_z_motor, locked_z2_motor;
  77. #endif
  78. // Counter variables for the Bresenham line tracer
  79. static int32_t counter_X, counter_Y, counter_Z, counter_E;
  80. static uint32_t step_events_completed; // The number of step events executed in the current block
  81. #if ENABLED(BEZIER_JERK_CONTROL)
  82. static int32_t bezier_A, // A coefficient in Bézier speed curve
  83. bezier_B, // B coefficient in Bézier speed curve
  84. bezier_C; // C coefficient in Bézier speed curve
  85. static uint32_t bezier_F, // F coefficient in Bézier speed curve
  86. bezier_AV; // AV coefficient in Bézier speed curve
  87. #ifdef __AVR__
  88. static bool A_negative; // If A coefficient was negative
  89. #endif
  90. static bool bezier_2nd_half; // If Bézier curve has been initialized or not
  91. #endif
  92. static uint32_t nextMainISR; // time remaining for the next Step ISR
  93. static bool all_steps_done; // all steps done
  94. #if ENABLED(LIN_ADVANCE)
  95. static uint32_t LA_decelerate_after; // Copy from current executed block. Needed because current_block is set to NULL "too early".
  96. static uint32_t nextAdvanceISR, eISR_Rate;
  97. static uint16_t current_adv_steps, final_adv_steps, max_adv_steps; // Copy from current executed block. Needed because current_block is set to NULL "too early".
  98. static int8_t e_steps;
  99. static bool use_advance_lead;
  100. #if E_STEPPERS > 1
  101. static int8_t LA_active_extruder; // Copy from current executed block. Needed because current_block is set to NULL "too early".
  102. #else
  103. static constexpr int8_t LA_active_extruder = 0;
  104. #endif
  105. #endif // LIN_ADVANCE
  106. static uint32_t acceleration_time, deceleration_time;
  107. static uint8_t step_loops, step_loops_nominal;
  108. static uint32_t ticks_nominal;
  109. #if DISABLED(BEZIER_JERK_CONTROL)
  110. static uint32_t acc_step_rate; // needed for deceleration start point
  111. #endif
  112. static volatile int32_t endstops_trigsteps[XYZ];
  113. static volatile int32_t endstops_stepsTotal, endstops_stepsDone;
  114. //
  115. // Positions of stepper motors, in step units
  116. //
  117. static volatile int32_t count_position[NUM_AXIS];
  118. //
  119. // Current direction of stepper motors (+1 or -1)
  120. //
  121. static volatile signed char count_direction[NUM_AXIS];
  122. //
  123. // Mixing extruder mix counters
  124. //
  125. #if ENABLED(MIXING_EXTRUDER)
  126. static int32_t counter_m[MIXING_STEPPERS];
  127. #define MIXING_STEPPERS_LOOP(VAR) \
  128. for (uint8_t VAR = 0; VAR < MIXING_STEPPERS; VAR++) \
  129. if (current_block->mix_event_count[VAR])
  130. #endif
  131. public:
  132. //
  133. // Constructor / initializer
  134. //
  135. Stepper() { };
  136. // Initialize stepper hardware
  137. static void init();
  138. // Interrupt Service Routines
  139. // The ISR scheduler
  140. static hal_timer_t isr_scheduler();
  141. // The stepper pulse phase ISR
  142. static void stepper_pulse_phase_isr();
  143. // The stepper block processing phase ISR
  144. static uint32_t stepper_block_phase_isr();
  145. #if ENABLED(LIN_ADVANCE)
  146. // The Linear advance stepper ISR
  147. static uint32_t advance_isr();
  148. #endif
  149. // Get the position of a stepper, in steps
  150. static int32_t position(const AxisEnum axis);
  151. // Report the positions of the steppers, in steps
  152. static void report_positions();
  153. // The stepper subsystem goes to sleep when it runs out of things to execute. Call this
  154. // to notify the subsystem that it is time to go to work.
  155. static void wake_up();
  156. // Quickly stop all steppers
  157. FORCE_INLINE static void quick_stop() { abort_current_block = true; }
  158. // The direction of a single motor
  159. FORCE_INLINE static bool motor_direction(const AxisEnum axis) { return TEST(last_direction_bits, axis); }
  160. // The last movement direction was not null on the specified axis. Note that motor direction is not necessarily the same.
  161. FORCE_INLINE static bool axis_is_moving(const AxisEnum axis) { return TEST(axis_did_move, axis); }
  162. // The extruder associated to the last movement
  163. FORCE_INLINE static uint8_t movement_extruder() { return last_movement_extruder; }
  164. // Handle a triggered endstop
  165. static void endstop_triggered(const AxisEnum axis);
  166. // Triggered position of an axis in steps
  167. static int32_t triggered_position(const AxisEnum axis);
  168. #if HAS_DIGIPOTSS || HAS_MOTOR_CURRENT_PWM
  169. static void digitalPotWrite(const int16_t address, const int16_t value);
  170. static void digipot_current(const uint8_t driver, const int16_t current);
  171. #endif
  172. #if HAS_MICROSTEPS
  173. static void microstep_ms(const uint8_t driver, const int8_t ms1, const int8_t ms2);
  174. static void microstep_mode(const uint8_t driver, const uint8_t stepping);
  175. static void microstep_readings();
  176. #endif
  177. #if ENABLED(X_DUAL_ENDSTOPS)
  178. FORCE_INLINE static void set_homing_flag_x(const bool state) { performing_homing = state; }
  179. FORCE_INLINE static void set_x_lock(const bool state) { locked_x_motor = state; }
  180. FORCE_INLINE static void set_x2_lock(const bool state) { locked_x2_motor = state; }
  181. #endif
  182. #if ENABLED(Y_DUAL_ENDSTOPS)
  183. FORCE_INLINE static void set_homing_flag_y(const bool state) { performing_homing = state; }
  184. FORCE_INLINE static void set_y_lock(const bool state) { locked_y_motor = state; }
  185. FORCE_INLINE static void set_y2_lock(const bool state) { locked_y2_motor = state; }
  186. #endif
  187. #if ENABLED(Z_DUAL_ENDSTOPS)
  188. FORCE_INLINE static void set_homing_flag_z(const bool state) { performing_homing = state; }
  189. FORCE_INLINE static void set_z_lock(const bool state) { locked_z_motor = state; }
  190. FORCE_INLINE static void set_z2_lock(const bool state) { locked_z2_motor = state; }
  191. #endif
  192. #if ENABLED(BABYSTEPPING)
  193. static void babystep(const AxisEnum axis, const bool direction); // perform a short step with a single stepper motor, outside of any convention
  194. #endif
  195. #if HAS_MOTOR_CURRENT_PWM
  196. static void refresh_motor_power();
  197. #endif
  198. // Set the current position in steps
  199. inline static void set_position(const int32_t &a, const int32_t &b, const int32_t &c, const int32_t &e) {
  200. planner.synchronize();
  201. CRITICAL_SECTION_START;
  202. _set_position(a, b, c, e);
  203. CRITICAL_SECTION_END;
  204. }
  205. inline static void set_position(const AxisEnum a, const int32_t &v) {
  206. planner.synchronize();
  207. CRITICAL_SECTION_START;
  208. count_position[a] = v;
  209. CRITICAL_SECTION_END;
  210. }
  211. private:
  212. // Set the current position in steps
  213. static void _set_position(const int32_t &a, const int32_t &b, const int32_t &c, const int32_t &e);
  214. // Set direction bits for all steppers
  215. static void set_directions();
  216. FORCE_INLINE static uint32_t calc_timer_interval(uint32_t step_rate) {
  217. uint32_t timer;
  218. NOMORE(step_rate, uint32_t(MAX_STEP_FREQUENCY));
  219. // TODO: HAL: tidy this up, use Conditionals_post.h
  220. #ifdef CPU_32_BIT
  221. #if ENABLED(DISABLE_MULTI_STEPPING)
  222. step_loops = 1;
  223. #else
  224. if (step_rate > STEP_DOUBLER_FREQUENCY * 2) { // If steprate > (STEP_DOUBLER_FREQUENCY * 2) kHz >> step 4 times
  225. step_rate >>= 2;
  226. step_loops = 4;
  227. }
  228. else if (step_rate > STEP_DOUBLER_FREQUENCY) { // If steprate > STEP_DOUBLER_FREQUENCY kHz >> step 2 times
  229. step_rate >>= 1;
  230. step_loops = 2;
  231. }
  232. else {
  233. step_loops = 1;
  234. }
  235. #endif
  236. #else
  237. if (step_rate > 20000) { // If steprate > 20kHz >> step 4 times
  238. step_rate >>= 2;
  239. step_loops = 4;
  240. }
  241. else if (step_rate > 10000) { // If steprate > 10kHz >> step 2 times
  242. step_rate >>= 1;
  243. step_loops = 2;
  244. }
  245. else {
  246. step_loops = 1;
  247. }
  248. #endif
  249. #ifdef CPU_32_BIT
  250. // In case of high-performance processor, it is able to calculate in real-time
  251. const uint32_t min_time_per_step = (HAL_STEPPER_TIMER_RATE) / ((STEP_DOUBLER_FREQUENCY) * 2);
  252. timer = uint32_t(HAL_STEPPER_TIMER_RATE) / step_rate;
  253. NOLESS(timer, min_time_per_step); // (STEP_DOUBLER_FREQUENCY * 2 kHz - this should never happen)
  254. #else
  255. NOLESS(step_rate, uint32_t(F_CPU / 500000U));
  256. step_rate -= F_CPU / 500000; // Correct for minimal speed
  257. if (step_rate >= (8 * 256)) { // higher step rate
  258. const uint8_t tmp_step_rate = (step_rate & 0x00FF);
  259. const uint16_t table_address = (uint16_t)&speed_lookuptable_fast[(uint8_t)(step_rate >> 8)][0],
  260. gain = (uint16_t)pgm_read_word_near(table_address + 2);
  261. timer = MultiU16X8toH16(tmp_step_rate, gain);
  262. timer = (uint16_t)pgm_read_word_near(table_address) - timer;
  263. }
  264. else { // lower step rates
  265. uint16_t table_address = (uint16_t)&speed_lookuptable_slow[0][0];
  266. table_address += ((step_rate) >> 1) & 0xFFFC;
  267. timer = (uint16_t)pgm_read_word_near(table_address)
  268. - (((uint16_t)pgm_read_word_near(table_address + 2) * (uint8_t)(step_rate & 0x0007)) >> 3);
  269. }
  270. if (timer < 100) { // (20kHz - this should never happen)
  271. timer = 100;
  272. SERIAL_ECHOLNPAIR(MSG_STEPPER_TOO_HIGH, step_rate);
  273. }
  274. #endif
  275. return timer;
  276. }
  277. #if ENABLED(BEZIER_JERK_CONTROL)
  278. static void _calc_bezier_curve_coeffs(const int32_t v0, const int32_t v1, const uint32_t av);
  279. static int32_t _eval_bezier_curve(const uint32_t curr_step);
  280. #endif
  281. #if HAS_DIGIPOTSS || HAS_MOTOR_CURRENT_PWM
  282. static void digipot_init();
  283. #endif
  284. #if HAS_MICROSTEPS
  285. static void microstep_init();
  286. #endif
  287. };
  288. #endif // STEPPER_H