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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391
  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 "planner.h"
  44. #include "speed_lookuptable.h"
  45. #include "stepper_indirection.h"
  46. #include "language.h"
  47. #include "types.h"
  48. class Stepper;
  49. extern Stepper stepper;
  50. #define ENABLE_STEPPER_DRIVER_INTERRUPT() SBI(TIMSK1, OCIE1A)
  51. #define DISABLE_STEPPER_DRIVER_INTERRUPT() CBI(TIMSK1, OCIE1A)
  52. // intRes = intIn1 * intIn2 >> 16
  53. // uses:
  54. // r26 to store 0
  55. // r27 to store the byte 1 of the 24 bit result
  56. #define MultiU16X8toH16(intRes, charIn1, intIn2) \
  57. asm volatile ( \
  58. "clr r26 \n\t" \
  59. "mul %A1, %B2 \n\t" \
  60. "movw %A0, r0 \n\t" \
  61. "mul %A1, %A2 \n\t" \
  62. "add %A0, r1 \n\t" \
  63. "adc %B0, r26 \n\t" \
  64. "lsr r0 \n\t" \
  65. "adc %A0, r26 \n\t" \
  66. "adc %B0, r26 \n\t" \
  67. "clr r1 \n\t" \
  68. : \
  69. "=&r" (intRes) \
  70. : \
  71. "d" (charIn1), \
  72. "d" (intIn2) \
  73. : \
  74. "r26" \
  75. )
  76. class Stepper {
  77. public:
  78. static block_t* current_block; // A pointer to the block currently being traced
  79. #if ENABLED(ABORT_ON_ENDSTOP_HIT_FEATURE_ENABLED)
  80. static bool abort_on_endstop_hit;
  81. #endif
  82. #if ENABLED(X_DUAL_ENDSTOPS) || ENABLED(Y_DUAL_ENDSTOPS) || ENABLED(Z_DUAL_ENDSTOPS)
  83. static bool performing_homing;
  84. #endif
  85. #if HAS_MOTOR_CURRENT_PWM
  86. #ifndef PWM_MOTOR_CURRENT
  87. #define PWM_MOTOR_CURRENT DEFAULT_PWM_MOTOR_CURRENT
  88. #endif
  89. static uint32_t motor_current_setting[3];
  90. #endif
  91. static int16_t cleaning_buffer_counter;
  92. private:
  93. static uint8_t last_direction_bits; // The next stepping-bits to be output
  94. #if ENABLED(X_DUAL_ENDSTOPS)
  95. static bool locked_x_motor, locked_x2_motor;
  96. #endif
  97. #if ENABLED(Y_DUAL_ENDSTOPS)
  98. static bool locked_y_motor, locked_y2_motor;
  99. #endif
  100. #if ENABLED(Z_DUAL_ENDSTOPS)
  101. static bool locked_z_motor, locked_z2_motor;
  102. #endif
  103. // Counter variables for the Bresenham line tracer
  104. static long counter_X, counter_Y, counter_Z, counter_E;
  105. static volatile uint32_t step_events_completed; // The number of step events executed in the current block
  106. #if ENABLED(LIN_ADVANCE)
  107. static uint16_t nextMainISR, nextAdvanceISR, eISR_Rate;
  108. #define _NEXT_ISR(T) nextMainISR = T
  109. static volatile int e_steps[E_STEPPERS];
  110. static int final_estep_rate;
  111. static int current_estep_rate[E_STEPPERS]; // Actual extruder speed [steps/s]
  112. static int current_adv_steps[E_STEPPERS]; // The amount of current added esteps due to advance.
  113. // i.e., the current amount of pressure applied
  114. // to the spring (=filament).
  115. #else // !LIN_ADVANCE
  116. #define _NEXT_ISR(T) OCR1A = T
  117. #endif // !LIN_ADVANCE
  118. static long acceleration_time, deceleration_time;
  119. static uint8_t step_loops, step_loops_nominal;
  120. static uint16_t OCR1A_nominal,
  121. acc_step_rate; // needed for deceleration start point
  122. static volatile long endstops_trigsteps[XYZ];
  123. static volatile long endstops_stepsTotal, endstops_stepsDone;
  124. //
  125. // Positions of stepper motors, in step units
  126. //
  127. static volatile long count_position[NUM_AXIS];
  128. //
  129. // Current direction of stepper motors (+1 or -1)
  130. //
  131. static volatile signed char count_direction[NUM_AXIS];
  132. //
  133. // Mixing extruder mix counters
  134. //
  135. #if ENABLED(MIXING_EXTRUDER)
  136. static long counter_m[MIXING_STEPPERS];
  137. #define MIXING_STEPPERS_LOOP(VAR) \
  138. for (uint8_t VAR = 0; VAR < MIXING_STEPPERS; VAR++) \
  139. if (current_block->mix_event_count[VAR])
  140. #endif
  141. public:
  142. //
  143. // Constructor / initializer
  144. //
  145. Stepper() { };
  146. //
  147. // Initialize stepper hardware
  148. //
  149. static void init();
  150. //
  151. // Interrupt Service Routines
  152. //
  153. static void isr();
  154. #if ENABLED(LIN_ADVANCE)
  155. static void advance_isr();
  156. static void advance_isr_scheduler();
  157. #endif
  158. //
  159. // Block until all buffered steps are executed
  160. //
  161. static void synchronize();
  162. //
  163. // Set the current position in steps
  164. //
  165. static void set_position(const long &a, const long &b, const long &c, const long &e);
  166. static void set_position(const AxisEnum &a, const long &v);
  167. static void set_e_position(const long &e);
  168. //
  169. // Set direction bits for all steppers
  170. //
  171. static void set_directions();
  172. //
  173. // Get the position of a stepper, in steps
  174. //
  175. static long position(const AxisEnum axis);
  176. //
  177. // Report the positions of the steppers, in steps
  178. //
  179. static void report_positions();
  180. //
  181. // Get the position (mm) of an axis based on stepper position(s)
  182. //
  183. static float get_axis_position_mm(const AxisEnum axis);
  184. //
  185. // SCARA AB axes are in degrees, not mm
  186. //
  187. #if IS_SCARA
  188. FORCE_INLINE static float get_axis_position_degrees(const AxisEnum axis) { return get_axis_position_mm(axis); }
  189. #endif
  190. //
  191. // The stepper subsystem goes to sleep when it runs out of things to execute. Call this
  192. // to notify the subsystem that it is time to go to work.
  193. //
  194. static void wake_up();
  195. //
  196. // Wait for moves to finish and disable all steppers
  197. //
  198. static void finish_and_disable();
  199. //
  200. // Quickly stop all steppers and clear the blocks queue
  201. //
  202. static void quick_stop();
  203. //
  204. // The direction of a single motor
  205. //
  206. FORCE_INLINE static bool motor_direction(const AxisEnum axis) { return TEST(last_direction_bits, axis); }
  207. #if HAS_DIGIPOTSS || HAS_MOTOR_CURRENT_PWM
  208. static void digitalPotWrite(const int16_t address, const int16_t value);
  209. static void digipot_current(const uint8_t driver, const int16_t current);
  210. #endif
  211. #if HAS_MICROSTEPS
  212. static void microstep_ms(const uint8_t driver, const int8_t ms1, const int8_t ms2);
  213. static void microstep_mode(const uint8_t driver, const uint8_t stepping);
  214. static void microstep_readings();
  215. #endif
  216. #if ENABLED(X_DUAL_ENDSTOPS)
  217. FORCE_INLINE static void set_homing_flag_x(const bool state) { performing_homing = state; }
  218. FORCE_INLINE static void set_x_lock(const bool state) { locked_x_motor = state; }
  219. FORCE_INLINE static void set_x2_lock(const bool state) { locked_x2_motor = state; }
  220. #endif
  221. #if ENABLED(Y_DUAL_ENDSTOPS)
  222. FORCE_INLINE static void set_homing_flag_y(const bool state) { performing_homing = state; }
  223. FORCE_INLINE static void set_y_lock(const bool state) { locked_y_motor = state; }
  224. FORCE_INLINE static void set_y2_lock(const bool state) { locked_y2_motor = state; }
  225. #endif
  226. #if ENABLED(Z_DUAL_ENDSTOPS)
  227. FORCE_INLINE static void set_homing_flag_z(const bool state) { performing_homing = state; }
  228. FORCE_INLINE static void set_z_lock(const bool state) { locked_z_motor = state; }
  229. FORCE_INLINE static void set_z2_lock(const bool state) { locked_z2_motor = state; }
  230. #endif
  231. #if ENABLED(BABYSTEPPING)
  232. static void babystep(const AxisEnum axis, const bool direction); // perform a short step with a single stepper motor, outside of any convention
  233. #endif
  234. static inline void kill_current_block() {
  235. step_events_completed = current_block->step_event_count;
  236. }
  237. //
  238. // Handle a triggered endstop
  239. //
  240. static void endstop_triggered(const AxisEnum axis);
  241. //
  242. // Triggered position of an axis in mm (not core-savvy)
  243. //
  244. FORCE_INLINE static float triggered_position_mm(const AxisEnum axis) {
  245. return endstops_trigsteps[axis] * planner.steps_to_mm[axis];
  246. }
  247. #if HAS_MOTOR_CURRENT_PWM
  248. static void refresh_motor_power();
  249. #endif
  250. private:
  251. FORCE_INLINE static unsigned short calc_timer_interval(unsigned short step_rate) {
  252. unsigned short timer;
  253. NOMORE(step_rate, MAX_STEP_FREQUENCY);
  254. if (step_rate > 20000) { // If steprate > 20kHz >> step 4 times
  255. step_rate >>= 2;
  256. step_loops = 4;
  257. }
  258. else if (step_rate > 10000) { // If steprate > 10kHz >> step 2 times
  259. step_rate >>= 1;
  260. step_loops = 2;
  261. }
  262. else {
  263. step_loops = 1;
  264. }
  265. NOLESS(step_rate, F_CPU / 500000);
  266. step_rate -= F_CPU / 500000; // Correct for minimal speed
  267. if (step_rate >= (8 * 256)) { // higher step rate
  268. unsigned short table_address = (unsigned short)&speed_lookuptable_fast[(unsigned char)(step_rate >> 8)][0];
  269. unsigned char tmp_step_rate = (step_rate & 0x00FF);
  270. unsigned short gain = (unsigned short)pgm_read_word_near(table_address + 2);
  271. MultiU16X8toH16(timer, tmp_step_rate, gain);
  272. timer = (unsigned short)pgm_read_word_near(table_address) - timer;
  273. }
  274. else { // lower step rates
  275. unsigned short table_address = (unsigned short)&speed_lookuptable_slow[0][0];
  276. table_address += ((step_rate) >> 1) & 0xFFFC;
  277. timer = (unsigned short)pgm_read_word_near(table_address);
  278. timer -= (((unsigned short)pgm_read_word_near(table_address + 2) * (unsigned char)(step_rate & 0x0007)) >> 3);
  279. }
  280. if (timer < 100) { // (20kHz - this should never happen)
  281. timer = 100;
  282. MYSERIAL.print(MSG_STEPPER_TOO_HIGH);
  283. MYSERIAL.println(step_rate);
  284. }
  285. return timer;
  286. }
  287. // Initialize the trapezoid generator from the current block.
  288. // Called whenever a new block begins.
  289. FORCE_INLINE static void trapezoid_generator_reset() {
  290. static int8_t last_extruder = -1;
  291. if (current_block->direction_bits != last_direction_bits || current_block->active_extruder != last_extruder) {
  292. last_direction_bits = current_block->direction_bits;
  293. last_extruder = current_block->active_extruder;
  294. set_directions();
  295. }
  296. deceleration_time = 0;
  297. // step_rate to timer interval
  298. OCR1A_nominal = calc_timer_interval(current_block->nominal_rate);
  299. // make a note of the number of step loops required at nominal speed
  300. step_loops_nominal = step_loops;
  301. #if ENABLED(LIN_ADVANCE)
  302. if (current_block->use_advance_lead) {
  303. current_estep_rate[current_block->active_extruder] = ((unsigned long)acc_step_rate * current_block->abs_adv_steps_multiplier8) >> 17;
  304. final_estep_rate = (current_block->nominal_rate * current_block->abs_adv_steps_multiplier8) >> 17;
  305. }
  306. #endif
  307. // SERIAL_ECHO_START();
  308. // SERIAL_ECHOPGM("advance :");
  309. // SERIAL_ECHO(current_block->advance/256.0);
  310. // SERIAL_ECHOPGM("advance rate :");
  311. // SERIAL_ECHO(current_block->advance_rate/256.0);
  312. // SERIAL_ECHOPGM("initial advance :");
  313. // SERIAL_ECHO(current_block->initial_advance/256.0);
  314. // SERIAL_ECHOPGM("final advance :");
  315. // SERIAL_ECHOLN(current_block->final_advance/256.0);
  316. }
  317. #if HAS_DIGIPOTSS || HAS_MOTOR_CURRENT_PWM
  318. static void digipot_init();
  319. #endif
  320. #if HAS_MICROSTEPS
  321. static void microstep_init();
  322. #endif
  323. };
  324. #endif // STEPPER_H