My Marlin configs for Fabrikator Mini and CTC i3 Pro B
Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

stepper.h 10KB

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