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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399
  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(ABORT_ON_ENDSTOP_HIT_FEATURE_ENABLED)
  56. static bool abort_on_endstop_hit;
  57. #endif
  58. #if ENABLED(X_DUAL_ENDSTOPS) || ENABLED(Y_DUAL_ENDSTOPS) || ENABLED(Z_DUAL_ENDSTOPS)
  59. static bool performing_homing;
  60. #endif
  61. #if HAS_MOTOR_CURRENT_PWM
  62. #ifndef PWM_MOTOR_CURRENT
  63. #define PWM_MOTOR_CURRENT DEFAULT_PWM_MOTOR_CURRENT
  64. #endif
  65. static uint32_t motor_current_setting[3];
  66. #endif
  67. static int16_t cleaning_buffer_counter;
  68. private:
  69. static uint8_t last_direction_bits; // The next stepping-bits to be output
  70. #if ENABLED(X_DUAL_ENDSTOPS)
  71. static bool locked_x_motor, locked_x2_motor;
  72. #endif
  73. #if ENABLED(Y_DUAL_ENDSTOPS)
  74. static bool locked_y_motor, locked_y2_motor;
  75. #endif
  76. #if ENABLED(Z_DUAL_ENDSTOPS)
  77. static bool locked_z_motor, locked_z2_motor;
  78. #endif
  79. // Counter variables for the Bresenham line tracer
  80. static int32_t counter_X, counter_Y, counter_Z, counter_E;
  81. static volatile uint32_t step_events_completed; // The number of step events executed in the current block
  82. #if ENABLED(BEZIER_JERK_CONTROL)
  83. static int32_t bezier_A, // A coefficient in Bézier speed curve
  84. bezier_B, // B coefficient in Bézier speed curve
  85. bezier_C; // C coefficient in Bézier speed curve
  86. static uint32_t bezier_F; // F coefficient in Bézier speed curve
  87. static uint32_t bezier_AV; // AV coefficient in Bézier speed curve
  88. #ifdef __AVR__
  89. static bool A_negative; // If A coefficient was negative
  90. #endif
  91. static bool bezier_2nd_half; // If Bézier curve has been initialized or not
  92. #endif
  93. #if ENABLED(LIN_ADVANCE)
  94. static uint32_t LA_decelerate_after; // Copy from current executed block. Needed because current_block is set to NULL "too early".
  95. static hal_timer_t nextMainISR, nextAdvanceISR, eISR_Rate;
  96. 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".
  97. #define _NEXT_ISR(T) nextMainISR = T
  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. #else // !LIN_ADVANCE
  106. #define _NEXT_ISR(T) HAL_timer_set_compare(STEP_TIMER_NUM, T);
  107. #endif // !LIN_ADVANCE
  108. static int32_t acceleration_time, deceleration_time;
  109. static uint8_t step_loops, step_loops_nominal;
  110. static hal_timer_t OCR1A_nominal;
  111. #if DISABLED(BEZIER_JERK_CONTROL)
  112. static hal_timer_t acc_step_rate; // needed for deceleration start point
  113. #endif
  114. static volatile int32_t endstops_trigsteps[XYZ];
  115. static volatile int32_t endstops_stepsTotal, endstops_stepsDone;
  116. //
  117. // Positions of stepper motors, in step units
  118. //
  119. static volatile int32_t count_position[NUM_AXIS];
  120. //
  121. // Current direction of stepper motors (+1 or -1)
  122. //
  123. static volatile signed char count_direction[NUM_AXIS];
  124. //
  125. // Mixing extruder mix counters
  126. //
  127. #if ENABLED(MIXING_EXTRUDER)
  128. static int32_t counter_m[MIXING_STEPPERS];
  129. #define MIXING_STEPPERS_LOOP(VAR) \
  130. for (uint8_t VAR = 0; VAR < MIXING_STEPPERS; VAR++) \
  131. if (current_block->mix_event_count[VAR])
  132. #endif
  133. public:
  134. //
  135. // Constructor / initializer
  136. //
  137. Stepper() { };
  138. //
  139. // Initialize stepper hardware
  140. //
  141. static void init();
  142. //
  143. // Interrupt Service Routines
  144. //
  145. static void isr();
  146. #if ENABLED(LIN_ADVANCE)
  147. static void advance_isr();
  148. static void advance_isr_scheduler();
  149. #endif
  150. //
  151. // Set the current position in steps
  152. //
  153. static void _set_position(const int32_t &a, const int32_t &b, const int32_t &c, const int32_t &e);
  154. FORCE_INLINE static void _set_position(const AxisEnum a, const int32_t &v) { count_position[a] = v; }
  155. FORCE_INLINE static void set_position(const int32_t &a, const int32_t &b, const int32_t &c, const int32_t &e) {
  156. planner.synchronize();
  157. CRITICAL_SECTION_START;
  158. _set_position(a, b, c, e);
  159. CRITICAL_SECTION_END;
  160. }
  161. static void set_position(const AxisEnum a, const int32_t &v) {
  162. planner.synchronize();
  163. CRITICAL_SECTION_START;
  164. count_position[a] = v;
  165. CRITICAL_SECTION_END;
  166. }
  167. FORCE_INLINE static void _set_e_position(const int32_t &e) { count_position[E_AXIS] = e; }
  168. static void set_e_position(const int32_t &e) {
  169. planner.synchronize();
  170. CRITICAL_SECTION_START;
  171. count_position[E_AXIS] = e;
  172. CRITICAL_SECTION_END;
  173. }
  174. //
  175. // Set direction bits for all steppers
  176. //
  177. static void set_directions();
  178. //
  179. // Get the position of a stepper, in steps
  180. //
  181. static int32_t position(const AxisEnum axis);
  182. //
  183. // Report the positions of the steppers, in steps
  184. //
  185. static void report_positions();
  186. //
  187. // Get the position (mm) of an axis based on stepper position(s)
  188. //
  189. static float get_axis_position_mm(const AxisEnum axis);
  190. //
  191. // SCARA AB axes are in degrees, not mm
  192. //
  193. #if IS_SCARA
  194. FORCE_INLINE static float get_axis_position_degrees(const AxisEnum axis) { return get_axis_position_mm(axis); }
  195. #endif
  196. //
  197. // The stepper subsystem goes to sleep when it runs out of things to execute. Call this
  198. // to notify the subsystem that it is time to go to work.
  199. //
  200. static void wake_up();
  201. //
  202. // Wait for moves to finish and disable all steppers
  203. //
  204. static void finish_and_disable();
  205. //
  206. // Quickly stop all steppers and clear the blocks queue
  207. //
  208. static void quick_stop();
  209. //
  210. // The direction of a single motor
  211. //
  212. FORCE_INLINE static bool motor_direction(const AxisEnum axis) { return TEST(last_direction_bits, axis); }
  213. #if HAS_DIGIPOTSS || HAS_MOTOR_CURRENT_PWM
  214. static void digitalPotWrite(const int16_t address, const int16_t value);
  215. static void digipot_current(const uint8_t driver, const int16_t current);
  216. #endif
  217. #if HAS_MICROSTEPS
  218. static void microstep_ms(const uint8_t driver, const int8_t ms1, const int8_t ms2);
  219. static void microstep_mode(const uint8_t driver, const uint8_t stepping);
  220. static void microstep_readings();
  221. #endif
  222. #if ENABLED(X_DUAL_ENDSTOPS)
  223. FORCE_INLINE static void set_homing_flag_x(const bool state) { performing_homing = state; }
  224. FORCE_INLINE static void set_x_lock(const bool state) { locked_x_motor = state; }
  225. FORCE_INLINE static void set_x2_lock(const bool state) { locked_x2_motor = state; }
  226. #endif
  227. #if ENABLED(Y_DUAL_ENDSTOPS)
  228. FORCE_INLINE static void set_homing_flag_y(const bool state) { performing_homing = state; }
  229. FORCE_INLINE static void set_y_lock(const bool state) { locked_y_motor = state; }
  230. FORCE_INLINE static void set_y2_lock(const bool state) { locked_y2_motor = state; }
  231. #endif
  232. #if ENABLED(Z_DUAL_ENDSTOPS)
  233. FORCE_INLINE static void set_homing_flag_z(const bool state) { performing_homing = state; }
  234. FORCE_INLINE static void set_z_lock(const bool state) { locked_z_motor = state; }
  235. FORCE_INLINE static void set_z2_lock(const bool state) { locked_z2_motor = state; }
  236. #endif
  237. #if ENABLED(BABYSTEPPING)
  238. static void babystep(const AxisEnum axis, const bool direction); // perform a short step with a single stepper motor, outside of any convention
  239. #endif
  240. static inline void kill_current_block() {
  241. step_events_completed = current_block->step_event_count;
  242. }
  243. //
  244. // Handle a triggered endstop
  245. //
  246. static void endstop_triggered(const AxisEnum axis);
  247. //
  248. // Triggered position of an axis in mm (not core-savvy)
  249. //
  250. FORCE_INLINE static float triggered_position_mm(const AxisEnum axis) {
  251. return endstops_trigsteps[axis] * planner.steps_to_mm[axis];
  252. }
  253. #if HAS_MOTOR_CURRENT_PWM
  254. static void refresh_motor_power();
  255. #endif
  256. private:
  257. FORCE_INLINE static hal_timer_t calc_timer_interval(hal_timer_t step_rate) {
  258. hal_timer_t timer;
  259. NOMORE(step_rate, MAX_STEP_FREQUENCY);
  260. // TODO: HAL: tidy this up, use condtionals_post.h
  261. #ifdef CPU_32_BIT
  262. #if ENABLED(DISABLE_MULTI_STEPPING)
  263. step_loops = 1;
  264. #else
  265. if (step_rate > STEP_DOUBLER_FREQUENCY * 2) { // If steprate > (STEP_DOUBLER_FREQUENCY * 2) kHz >> step 4 times
  266. step_rate >>= 2;
  267. step_loops = 4;
  268. }
  269. else if (step_rate > STEP_DOUBLER_FREQUENCY) { // If steprate > STEP_DOUBLER_FREQUENCY kHz >> step 2 times
  270. step_rate >>= 1;
  271. step_loops = 2;
  272. }
  273. else {
  274. step_loops = 1;
  275. }
  276. #endif
  277. #else
  278. if (step_rate > 20000) { // If steprate > 20kHz >> step 4 times
  279. step_rate >>= 2;
  280. step_loops = 4;
  281. }
  282. else if (step_rate > 10000) { // If steprate > 10kHz >> step 2 times
  283. step_rate >>= 1;
  284. step_loops = 2;
  285. }
  286. else {
  287. step_loops = 1;
  288. }
  289. #endif
  290. #ifdef CPU_32_BIT
  291. // In case of high-performance processor, it is able to calculate in real-time
  292. const uint32_t MIN_TIME_PER_STEP = (HAL_STEPPER_TIMER_RATE) / ((STEP_DOUBLER_FREQUENCY) * 2);
  293. timer = uint32_t(HAL_STEPPER_TIMER_RATE) / step_rate;
  294. NOLESS(timer, MIN_TIME_PER_STEP); // (STEP_DOUBLER_FREQUENCY * 2 kHz - this should never happen)
  295. #else
  296. NOLESS(step_rate, F_CPU / 500000);
  297. step_rate -= F_CPU / 500000; // Correct for minimal speed
  298. if (step_rate >= (8 * 256)) { // higher step rate
  299. unsigned short table_address = (unsigned short)&speed_lookuptable_fast[(unsigned char)(step_rate >> 8)][0];
  300. unsigned char tmp_step_rate = (step_rate & 0x00FF);
  301. unsigned short gain = (unsigned short)pgm_read_word_near(table_address + 2);
  302. MultiU16X8toH16(timer, tmp_step_rate, gain);
  303. timer = (unsigned short)pgm_read_word_near(table_address) - timer;
  304. }
  305. else { // lower step rates
  306. unsigned short table_address = (unsigned short)&speed_lookuptable_slow[0][0];
  307. table_address += ((step_rate) >> 1) & 0xFFFC;
  308. timer = (unsigned short)pgm_read_word_near(table_address);
  309. timer -= (((unsigned short)pgm_read_word_near(table_address + 2) * (unsigned char)(step_rate & 0x0007)) >> 3);
  310. }
  311. if (timer < 100) { // (20kHz - this should never happen)
  312. timer = 100;
  313. SERIAL_ECHOPGM(MSG_STEPPER_TOO_HIGH);
  314. SERIAL_ECHOLN(step_rate);
  315. }
  316. #endif
  317. return timer;
  318. }
  319. #if ENABLED(BEZIER_JERK_CONTROL)
  320. static void _calc_bezier_curve_coeffs(const int32_t v0, const int32_t v1, const uint32_t av);
  321. static int32_t _eval_bezier_curve(const uint32_t curr_step);
  322. #endif
  323. #if HAS_DIGIPOTSS || HAS_MOTOR_CURRENT_PWM
  324. static void digipot_init();
  325. #endif
  326. #if HAS_MICROSTEPS
  327. static void microstep_init();
  328. #endif
  329. };
  330. #endif // STEPPER_H