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

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