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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378
  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(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. private:
  68. static uint8_t last_direction_bits; // The next stepping-bits to be output
  69. static uint16_t cleaning_buffer_counter;
  70. #if ENABLED(Z_DUAL_ENDSTOPS)
  71. static bool locked_z_motor, locked_z2_motor;
  72. #endif
  73. // Counter variables for the Bresenham line tracer
  74. static long counter_X, counter_Y, counter_Z, counter_E;
  75. static volatile uint32_t step_events_completed; // The number of step events executed in the current block
  76. #if ENABLED(LIN_ADVANCE)
  77. static timer_t nextMainISR, nextAdvanceISR, eISR_Rate;
  78. #define _NEXT_ISR(T) nextMainISR = T
  79. static volatile int e_steps[E_STEPPERS];
  80. static int final_estep_rate;
  81. static int current_estep_rate[E_STEPPERS]; // Actual extruder speed [steps/s]
  82. static int current_adv_steps[E_STEPPERS]; // The amount of current added esteps due to advance.
  83. // i.e., the current amount of pressure applied
  84. // to the spring (=filament).
  85. #else
  86. #define _NEXT_ISR(T) HAL_timer_set_count(STEP_TIMER_NUM, T);
  87. #endif // LIN_ADVANCE
  88. static long acceleration_time, deceleration_time;
  89. //unsigned long accelerate_until, decelerate_after, acceleration_rate, initial_rate, final_rate, nominal_rate;
  90. static timer_t acc_step_rate; // needed for deceleration start point
  91. static uint8_t step_loops, step_loops_nominal;
  92. static timer_t OCR1A_nominal;
  93. static volatile long endstops_trigsteps[XYZ];
  94. static volatile long endstops_stepsTotal, endstops_stepsDone;
  95. //
  96. // Positions of stepper motors, in step units
  97. //
  98. static volatile long count_position[NUM_AXIS];
  99. //
  100. // Current direction of stepper motors (+1 or -1)
  101. //
  102. static volatile signed char count_direction[NUM_AXIS];
  103. //
  104. // Mixing extruder mix counters
  105. //
  106. #if ENABLED(MIXING_EXTRUDER)
  107. static long counter_m[MIXING_STEPPERS];
  108. #define MIXING_STEPPERS_LOOP(VAR) \
  109. for (uint8_t VAR = 0; VAR < MIXING_STEPPERS; VAR++) \
  110. if (current_block->mix_event_count[VAR])
  111. #endif
  112. public:
  113. //
  114. // Constructor / initializer
  115. //
  116. Stepper() { };
  117. //
  118. // Initialize stepper hardware
  119. //
  120. static void init();
  121. //
  122. // Interrupt Service Routines
  123. //
  124. static void isr();
  125. #if ENABLED(LIN_ADVANCE)
  126. static void advance_isr();
  127. static void advance_isr_scheduler();
  128. #endif
  129. //
  130. // Block until all buffered steps are executed
  131. //
  132. static void synchronize();
  133. //
  134. // Set the current position in steps
  135. //
  136. static void set_position(const long &a, const long &b, const long &c, const long &e);
  137. static void set_position(const AxisEnum &a, const long &v);
  138. static void set_e_position(const long &e);
  139. //
  140. // Set direction bits for all steppers
  141. //
  142. static void set_directions();
  143. //
  144. // Get the position of a stepper, in steps
  145. //
  146. static long position(AxisEnum axis);
  147. //
  148. // Report the positions of the steppers, in steps
  149. //
  150. static void report_positions();
  151. //
  152. // Get the position (mm) of an axis based on stepper position(s)
  153. //
  154. static float get_axis_position_mm(AxisEnum axis);
  155. //
  156. // SCARA AB axes are in degrees, not mm
  157. //
  158. #if IS_SCARA
  159. static FORCE_INLINE float get_axis_position_degrees(AxisEnum axis) { return get_axis_position_mm(axis); }
  160. #endif
  161. //
  162. // The stepper subsystem goes to sleep when it runs out of things to execute. Call this
  163. // to notify the subsystem that it is time to go to work.
  164. //
  165. static void wake_up();
  166. //
  167. // Wait for moves to finish and disable all steppers
  168. //
  169. static void finish_and_disable();
  170. //
  171. // Quickly stop all steppers and clear the blocks queue
  172. //
  173. static void quick_stop();
  174. //
  175. // The direction of a single motor
  176. //
  177. static FORCE_INLINE bool motor_direction(AxisEnum axis) { return TEST(last_direction_bits, axis); }
  178. #if HAS_DIGIPOTSS || HAS_MOTOR_CURRENT_PWM
  179. static void digitalPotWrite(const int16_t address, const int16_t value);
  180. static void digipot_current(const uint8_t driver, const int16_t current);
  181. #endif
  182. #if HAS_MICROSTEPS
  183. static void microstep_ms(const uint8_t driver, const int8_t ms1, const int8_t ms2);
  184. static void microstep_mode(const uint8_t driver, const uint8_t stepping);
  185. static void microstep_readings();
  186. #endif
  187. #if ENABLED(Z_DUAL_ENDSTOPS)
  188. static FORCE_INLINE void set_homing_flag(const bool state) { performing_homing = state; }
  189. static FORCE_INLINE void set_z_lock(const bool state) { locked_z_motor = state; }
  190. static FORCE_INLINE 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. static inline void kill_current_block() {
  196. step_events_completed = current_block->step_event_count;
  197. }
  198. //
  199. // Handle a triggered endstop
  200. //
  201. static void endstop_triggered(AxisEnum axis);
  202. //
  203. // Triggered position of an axis in mm (not core-savvy)
  204. //
  205. static FORCE_INLINE float triggered_position_mm(AxisEnum axis) {
  206. return endstops_trigsteps[axis] * planner.steps_to_mm[axis];
  207. }
  208. #if HAS_MOTOR_CURRENT_PWM
  209. static void refresh_motor_power();
  210. #endif
  211. private:
  212. static FORCE_INLINE timer_t calc_timer(timer_t step_rate) {
  213. timer_t timer;
  214. NOMORE(step_rate, MAX_STEP_FREQUENCY);
  215. // TODO: HAL: tidy this up, use condtionals_post.h
  216. #ifdef CPU_32_BIT
  217. #if ENABLED(DISABLE_MULTI_STEPPING)
  218. step_loops = 1;
  219. #else
  220. if (step_rate > STEP_DOUBLER_FREQUENCY * 2) { // If steprate > (STEP_DOUBLER_FREQUENCY * 2) kHz >> step 4 times
  221. step_rate >>= 2;
  222. step_loops = 4;
  223. }
  224. else if (step_rate > STEP_DOUBLER_FREQUENCY) { // If steprate > STEP_DOUBLER_FREQUENCY kHz >> step 2 times
  225. step_rate >>= 1;
  226. step_loops = 2;
  227. }
  228. else {
  229. step_loops = 1;
  230. }
  231. #endif
  232. #else
  233. if (step_rate > 20000) { // If steprate > 20kHz >> step 4 times
  234. step_rate >>= 2;
  235. step_loops = 4;
  236. }
  237. else if (step_rate > 10000) { // If steprate > 10kHz >> step 2 times
  238. step_rate >>= 1;
  239. step_loops = 2;
  240. }
  241. else {
  242. step_loops = 1;
  243. }
  244. #endif
  245. #ifdef CPU_32_BIT
  246. // In case of high-performance processor, it is able to calculate in real-time
  247. const uint32_t MIN_TIME_PER_STEP = (HAL_STEPPER_TIMER_RATE) / ((STEP_DOUBLER_FREQUENCY) * 2);
  248. timer = uint32_t(HAL_STEPPER_TIMER_RATE) / step_rate;
  249. NOLESS(timer, MIN_TIME_PER_STEP); // (STEP_DOUBLER_FREQUENCY * 2 kHz - this should never happen)
  250. #else
  251. NOLESS(step_rate, F_CPU / 500000);
  252. step_rate -= F_CPU / 500000; // Correct for minimal speed
  253. if (step_rate >= (8 * 256)) { // higher step rate
  254. unsigned short table_address = (unsigned short)&speed_lookuptable_fast[(unsigned char)(step_rate >> 8)][0];
  255. unsigned char tmp_step_rate = (step_rate & 0x00ff);
  256. unsigned short gain = (unsigned short)pgm_read_word_near(table_address + 2);
  257. MultiU16X8toH16(timer, tmp_step_rate, gain);
  258. timer = (unsigned short)pgm_read_word_near(table_address) - timer;
  259. }
  260. else { // lower step rates
  261. unsigned short table_address = (unsigned short)&speed_lookuptable_slow[0][0];
  262. table_address += ((step_rate) >> 1) & 0xfffc;
  263. timer = (unsigned short)pgm_read_word_near(table_address);
  264. timer -= (((unsigned short)pgm_read_word_near(table_address + 2) * (unsigned char)(step_rate & 0x0007)) >> 3);
  265. }
  266. if (timer < 100) { // (20kHz - this should never happen)
  267. timer = 100;
  268. MYSERIAL.print(MSG_STEPPER_TOO_HIGH);
  269. MYSERIAL.println(step_rate);
  270. }
  271. #endif
  272. return timer;
  273. }
  274. // Initialize the trapezoid generator from the current block.
  275. // Called whenever a new block begins.
  276. static FORCE_INLINE void trapezoid_generator_reset() {
  277. static int8_t last_extruder = -1;
  278. if (current_block->direction_bits != last_direction_bits || current_block->active_extruder != last_extruder) {
  279. last_direction_bits = current_block->direction_bits;
  280. last_extruder = current_block->active_extruder;
  281. set_directions();
  282. }
  283. deceleration_time = 0;
  284. // step_rate to timer interval
  285. OCR1A_nominal = calc_timer(current_block->nominal_rate);
  286. // make a note of the number of step loops required at nominal speed
  287. step_loops_nominal = step_loops;
  288. acc_step_rate = current_block->initial_rate;
  289. acceleration_time = calc_timer(acc_step_rate);
  290. _NEXT_ISR(acceleration_time);
  291. #if ENABLED(LIN_ADVANCE)
  292. if (current_block->use_advance_lead) {
  293. current_estep_rate[current_block->active_extruder] = ((unsigned long)acc_step_rate * current_block->abs_adv_steps_multiplier8) >> 17;
  294. final_estep_rate = (current_block->nominal_rate * current_block->abs_adv_steps_multiplier8) >> 17;
  295. }
  296. #endif
  297. // SERIAL_ECHO_START();
  298. // SERIAL_ECHOPGM("advance :");
  299. // SERIAL_ECHO(current_block->advance/256.0);
  300. // SERIAL_ECHOPGM("advance rate :");
  301. // SERIAL_ECHO(current_block->advance_rate/256.0);
  302. // SERIAL_ECHOPGM("initial advance :");
  303. // SERIAL_ECHO(current_block->initial_advance/256.0);
  304. // SERIAL_ECHOPGM("final advance :");
  305. // SERIAL_ECHOLN(current_block->final_advance/256.0);
  306. }
  307. #if HAS_DIGIPOTSS || HAS_MOTOR_CURRENT_PWM
  308. static void digipot_init();
  309. #endif
  310. #if HAS_MICROSTEPS
  311. static void microstep_init();
  312. #endif
  313. };
  314. #endif // STEPPER_H