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.cpp 28KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981
  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.cpp - A singleton object to execute motion plans using stepper motors
  24. * Marlin Firmware
  25. *
  26. * Derived from Grbl
  27. * Copyright (c) 2009-2011 Simen Svale Skogsrud
  28. *
  29. * Grbl is free software: you can redistribute it and/or modify
  30. * it under the terms of the GNU General Public License as published by
  31. * the Free Software Foundation, either version 3 of the License, or
  32. * (at your option) any later version.
  33. *
  34. * Grbl is distributed in the hope that it will be useful,
  35. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  36. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  37. * GNU General Public License for more details.
  38. *
  39. * You should have received a copy of the GNU General Public License
  40. * along with Grbl. If not, see <http://www.gnu.org/licenses/>.
  41. */
  42. /* The timer calculations of this module informed by the 'RepRap cartesian firmware' by Zack Smith
  43. and Philipp Tiefenbacher. */
  44. #include "Marlin.h"
  45. #include "stepper.h"
  46. #include "endstops.h"
  47. #include "planner.h"
  48. #include "temperature.h"
  49. #include "ultralcd.h"
  50. #include "language.h"
  51. #include "cardreader.h"
  52. #include "speed_lookuptable.h"
  53. #if HAS_DIGIPOTSS
  54. #include <SPI.h>
  55. #endif
  56. Stepper stepper; // Singleton
  57. #if ENABLED(DUAL_X_CARRIAGE)
  58. #define X_APPLY_DIR(v,ALWAYS) \
  59. if (extruder_duplication_enabled || ALWAYS) { \
  60. X_DIR_WRITE(v); \
  61. X2_DIR_WRITE(v); \
  62. } \
  63. else { \
  64. if (current_block->active_extruder) X2_DIR_WRITE(v); else X_DIR_WRITE(v); \
  65. }
  66. #define X_APPLY_STEP(v,ALWAYS) \
  67. if (extruder_duplication_enabled || ALWAYS) { \
  68. X_STEP_WRITE(v); \
  69. X2_STEP_WRITE(v); \
  70. } \
  71. else { \
  72. if (current_block->active_extruder != 0) X2_STEP_WRITE(v); else X_STEP_WRITE(v); \
  73. }
  74. #else
  75. #define X_APPLY_DIR(v,Q) X_DIR_WRITE(v)
  76. #define X_APPLY_STEP(v,Q) X_STEP_WRITE(v)
  77. #endif
  78. #if ENABLED(Y_DUAL_STEPPER_DRIVERS)
  79. #define Y_APPLY_DIR(v,Q) { Y_DIR_WRITE(v); Y2_DIR_WRITE((v) != INVERT_Y2_VS_Y_DIR); }
  80. #define Y_APPLY_STEP(v,Q) { Y_STEP_WRITE(v); Y2_STEP_WRITE(v); }
  81. #else
  82. #define Y_APPLY_DIR(v,Q) Y_DIR_WRITE(v)
  83. #define Y_APPLY_STEP(v,Q) Y_STEP_WRITE(v)
  84. #endif
  85. #if ENABLED(Z_DUAL_STEPPER_DRIVERS)
  86. #define Z_APPLY_DIR(v,Q) { Z_DIR_WRITE(v); Z2_DIR_WRITE(v); }
  87. #if ENABLED(Z_DUAL_ENDSTOPS)
  88. #define Z_APPLY_STEP(v,Q) \
  89. if (performing_homing) { \
  90. if (Z_HOME_DIR > 0) {\
  91. if (!(TEST(endstops.old_endstop_bits, Z_MAX) && (count_direction[Z_AXIS] > 0)) && !locked_z_motor) Z_STEP_WRITE(v); \
  92. if (!(TEST(endstops.old_endstop_bits, Z2_MAX) && (count_direction[Z_AXIS] > 0)) && !locked_z2_motor) Z2_STEP_WRITE(v); \
  93. } \
  94. else { \
  95. if (!(TEST(endstops.old_endstop_bits, Z_MIN) && (count_direction[Z_AXIS] < 0)) && !locked_z_motor) Z_STEP_WRITE(v); \
  96. if (!(TEST(endstops.old_endstop_bits, Z2_MIN) && (count_direction[Z_AXIS] < 0)) && !locked_z2_motor) Z2_STEP_WRITE(v); \
  97. } \
  98. } \
  99. else { \
  100. Z_STEP_WRITE(v); \
  101. Z2_STEP_WRITE(v); \
  102. }
  103. #else
  104. #define Z_APPLY_STEP(v,Q) { Z_STEP_WRITE(v); Z2_STEP_WRITE(v); }
  105. #endif
  106. #else
  107. #define Z_APPLY_DIR(v,Q) Z_DIR_WRITE(v)
  108. #define Z_APPLY_STEP(v,Q) Z_STEP_WRITE(v)
  109. #endif
  110. #define E_APPLY_STEP(v,Q) E_STEP_WRITE(v)
  111. // intRes = longIn1 * longIn2 >> 24
  112. // uses:
  113. // r26 to store 0
  114. // r27 to store bits 16-23 of the 48bit result. The top bit is used to round the two byte result.
  115. // note that the lower two bytes and the upper byte of the 48bit result are not calculated.
  116. // this can cause the result to be out by one as the lower bytes may cause carries into the upper ones.
  117. // B0 A0 are bits 24-39 and are the returned value
  118. // C1 B1 A1 is longIn1
  119. // D2 C2 B2 A2 is longIn2
  120. //
  121. #define MultiU24X32toH16(intRes, longIn1, longIn2) \
  122. asm volatile ( \
  123. "clr r26 \n\t" \
  124. "mul %A1, %B2 \n\t" \
  125. "mov r27, r1 \n\t" \
  126. "mul %B1, %C2 \n\t" \
  127. "movw %A0, r0 \n\t" \
  128. "mul %C1, %C2 \n\t" \
  129. "add %B0, r0 \n\t" \
  130. "mul %C1, %B2 \n\t" \
  131. "add %A0, r0 \n\t" \
  132. "adc %B0, r1 \n\t" \
  133. "mul %A1, %C2 \n\t" \
  134. "add r27, r0 \n\t" \
  135. "adc %A0, r1 \n\t" \
  136. "adc %B0, r26 \n\t" \
  137. "mul %B1, %B2 \n\t" \
  138. "add r27, r0 \n\t" \
  139. "adc %A0, r1 \n\t" \
  140. "adc %B0, r26 \n\t" \
  141. "mul %C1, %A2 \n\t" \
  142. "add r27, r0 \n\t" \
  143. "adc %A0, r1 \n\t" \
  144. "adc %B0, r26 \n\t" \
  145. "mul %B1, %A2 \n\t" \
  146. "add r27, r1 \n\t" \
  147. "adc %A0, r26 \n\t" \
  148. "adc %B0, r26 \n\t" \
  149. "lsr r27 \n\t" \
  150. "adc %A0, r26 \n\t" \
  151. "adc %B0, r26 \n\t" \
  152. "mul %D2, %A1 \n\t" \
  153. "add %A0, r0 \n\t" \
  154. "adc %B0, r1 \n\t" \
  155. "mul %D2, %B1 \n\t" \
  156. "add %B0, r0 \n\t" \
  157. "clr r1 \n\t" \
  158. : \
  159. "=&r" (intRes) \
  160. : \
  161. "d" (longIn1), \
  162. "d" (longIn2) \
  163. : \
  164. "r26" , "r27" \
  165. )
  166. // Some useful constants
  167. #define ENABLE_STEPPER_DRIVER_INTERRUPT() SBI(TIMSK1, OCIE1A)
  168. #define DISABLE_STEPPER_DRIVER_INTERRUPT() CBI(TIMSK1, OCIE1A)
  169. /**
  170. * __________________________
  171. * /| |\ _________________ ^
  172. * / | | \ /| |\ |
  173. * / | | \ / | | \ s
  174. * / | | | | | \ p
  175. * / | | | | | \ e
  176. * +-----+------------------------+---+--+---------------+----+ e
  177. * | BLOCK 1 | BLOCK 2 | d
  178. *
  179. * time ----->
  180. *
  181. * The trapezoid is the shape the speed curve over time. It starts at block->initial_rate, accelerates
  182. * first block->accelerate_until step_events_completed, then keeps going at constant speed until
  183. * step_events_completed reaches block->decelerate_after after which it decelerates until the trapezoid generator is reset.
  184. * The slope of acceleration is calculated using v = u + at where t is the accumulated timer values of the steps so far.
  185. */
  186. void Stepper::wake_up() {
  187. // TCNT1 = 0;
  188. ENABLE_STEPPER_DRIVER_INTERRUPT();
  189. }
  190. /**
  191. * Set the stepper direction of each axis
  192. *
  193. * X_AXIS=A_AXIS and Y_AXIS=B_AXIS for COREXY
  194. * X_AXIS=A_AXIS and Z_AXIS=C_AXIS for COREXZ
  195. */
  196. void Stepper::set_directions() {
  197. #define SET_STEP_DIR(AXIS) \
  198. if (motor_direction(AXIS ##_AXIS)) { \
  199. AXIS ##_APPLY_DIR(INVERT_## AXIS ##_DIR, false); \
  200. count_direction[AXIS ##_AXIS] = -1; \
  201. } \
  202. else { \
  203. AXIS ##_APPLY_DIR(!INVERT_## AXIS ##_DIR, false); \
  204. count_direction[AXIS ##_AXIS] = 1; \
  205. }
  206. SET_STEP_DIR(X); // A
  207. SET_STEP_DIR(Y); // B
  208. SET_STEP_DIR(Z); // C
  209. #if DISABLED(ADVANCE)
  210. if (motor_direction(E_AXIS)) {
  211. REV_E_DIR();
  212. count_direction[E_AXIS] = -1;
  213. }
  214. else {
  215. NORM_E_DIR();
  216. count_direction[E_AXIS] = 1;
  217. }
  218. #endif //!ADVANCE
  219. }
  220. // "The Stepper Driver Interrupt" - This timer interrupt is the workhorse.
  221. // It pops blocks from the block_buffer and executes them by pulsing the stepper pins appropriately.
  222. ISR(TIMER1_COMPA_vect) { stepper.isr(); }
  223. void Stepper::isr() {
  224. if (cleaning_buffer_counter) {
  225. current_block = NULL;
  226. planner.discard_current_block();
  227. #ifdef SD_FINISHED_RELEASECOMMAND
  228. if ((cleaning_buffer_counter == 1) && (SD_FINISHED_STEPPERRELEASE)) enqueue_and_echo_commands_P(PSTR(SD_FINISHED_RELEASECOMMAND));
  229. #endif
  230. cleaning_buffer_counter--;
  231. OCR1A = 200;
  232. return;
  233. }
  234. // If there is no current block, attempt to pop one from the buffer
  235. if (!current_block) {
  236. // Anything in the buffer?
  237. current_block = planner.get_current_block();
  238. if (current_block) {
  239. current_block->busy = true;
  240. trapezoid_generator_reset();
  241. counter_X = -(current_block->step_event_count >> 1);
  242. counter_Y = counter_Z = counter_E = counter_X;
  243. step_events_completed = 0;
  244. #if ENABLED(Z_LATE_ENABLE)
  245. if (current_block->steps[Z_AXIS] > 0) {
  246. enable_z();
  247. OCR1A = 2000; //1ms wait
  248. return;
  249. }
  250. #endif
  251. // #if ENABLED(ADVANCE)
  252. // e_steps[current_block->active_extruder] = 0;
  253. // #endif
  254. }
  255. else {
  256. OCR1A = 2000; // 1kHz.
  257. }
  258. }
  259. if (current_block != NULL) {
  260. // Update endstops state, if enabled
  261. #if HAS_BED_PROBE
  262. if (endstops.enabled || endstops.z_probe_enabled) endstops.update();
  263. #else
  264. if (endstops.enabled) endstops.update();
  265. #endif
  266. // Take multiple steps per interrupt (For high speed moves)
  267. for (int8_t i = 0; i < step_loops; i++) {
  268. #ifndef USBCON
  269. customizedSerial.checkRx(); // Check for serial chars.
  270. #endif
  271. #if ENABLED(ADVANCE)
  272. counter_E += current_block->steps[E_AXIS];
  273. if (counter_E > 0) {
  274. counter_E -= current_block->step_event_count;
  275. e_steps[current_block->active_extruder] += motor_direction(E_AXIS) ? -1 : 1;
  276. }
  277. #endif //ADVANCE
  278. #define _COUNTER(AXIS) counter_## AXIS
  279. #define _APPLY_STEP(AXIS) AXIS ##_APPLY_STEP
  280. #define _INVERT_STEP_PIN(AXIS) INVERT_## AXIS ##_STEP_PIN
  281. #define STEP_ADD(AXIS) \
  282. _COUNTER(AXIS) += current_block->steps[_AXIS(AXIS)]; \
  283. if (_COUNTER(AXIS) > 0) { _APPLY_STEP(AXIS)(!_INVERT_STEP_PIN(AXIS),0); }
  284. STEP_ADD(X);
  285. STEP_ADD(Y);
  286. STEP_ADD(Z);
  287. #if DISABLED(ADVANCE)
  288. STEP_ADD(E);
  289. #endif
  290. #define STEP_IF_COUNTER(AXIS) \
  291. if (_COUNTER(AXIS) > 0) { \
  292. _COUNTER(AXIS) -= current_block->step_event_count; \
  293. count_position[_AXIS(AXIS)] += count_direction[_AXIS(AXIS)]; \
  294. _APPLY_STEP(AXIS)(_INVERT_STEP_PIN(AXIS),0); \
  295. }
  296. STEP_IF_COUNTER(X);
  297. STEP_IF_COUNTER(Y);
  298. STEP_IF_COUNTER(Z);
  299. #if DISABLED(ADVANCE)
  300. STEP_IF_COUNTER(E);
  301. #endif
  302. step_events_completed++;
  303. if (step_events_completed >= current_block->step_event_count) break;
  304. }
  305. // Calculate new timer value
  306. unsigned short timer, step_rate;
  307. if (step_events_completed <= (unsigned long)current_block->accelerate_until) {
  308. MultiU24X32toH16(acc_step_rate, acceleration_time, current_block->acceleration_rate);
  309. acc_step_rate += current_block->initial_rate;
  310. // upper limit
  311. NOMORE(acc_step_rate, current_block->nominal_rate);
  312. // step_rate to timer interval
  313. timer = calc_timer(acc_step_rate);
  314. OCR1A = timer;
  315. acceleration_time += timer;
  316. #if ENABLED(ADVANCE)
  317. advance += advance_rate * step_loops;
  318. //NOLESS(advance, current_block->advance);
  319. // Do E steps + advance steps
  320. e_steps[current_block->active_extruder] += ((advance >> 8) - old_advance);
  321. old_advance = advance >> 8;
  322. #endif //ADVANCE
  323. }
  324. else if (step_events_completed > (unsigned long)current_block->decelerate_after) {
  325. MultiU24X32toH16(step_rate, deceleration_time, current_block->acceleration_rate);
  326. if (step_rate <= acc_step_rate) { // Still decelerating?
  327. step_rate = acc_step_rate - step_rate;
  328. NOLESS(step_rate, current_block->final_rate);
  329. }
  330. else
  331. step_rate = current_block->final_rate;
  332. // step_rate to timer interval
  333. timer = calc_timer(step_rate);
  334. OCR1A = timer;
  335. deceleration_time += timer;
  336. #if ENABLED(ADVANCE)
  337. advance -= advance_rate * step_loops;
  338. NOLESS(advance, final_advance);
  339. // Do E steps + advance steps
  340. uint32_t advance_whole = advance >> 8;
  341. e_steps[current_block->active_extruder] += advance_whole - old_advance;
  342. old_advance = advance_whole;
  343. #endif //ADVANCE
  344. }
  345. else {
  346. OCR1A = OCR1A_nominal;
  347. // ensure we're running at the correct step rate, even if we just came off an acceleration
  348. step_loops = step_loops_nominal;
  349. }
  350. OCR1A = (OCR1A < (TCNT1 + 16)) ? (TCNT1 + 16) : OCR1A;
  351. // If current block is finished, reset pointer
  352. if (step_events_completed >= current_block->step_event_count) {
  353. current_block = NULL;
  354. planner.discard_current_block();
  355. }
  356. }
  357. }
  358. #if ENABLED(ADVANCE)
  359. // Timer interrupt for E. e_steps is set in the main routine;
  360. // Timer 0 is shared with millies
  361. ISR(TIMER0_COMPA_vect) { stepper.advance_isr(); }
  362. void Stepper::advance_isr() {
  363. old_OCR0A += 52; // ~10kHz interrupt (250000 / 26 = 9615kHz)
  364. OCR0A = old_OCR0A;
  365. #define STEP_E_ONCE(INDEX) \
  366. if (e_steps[INDEX] != 0) { \
  367. E## INDEX ##_STEP_WRITE(INVERT_E_STEP_PIN); \
  368. if (e_steps[INDEX] < 0) { \
  369. E## INDEX ##_DIR_WRITE(INVERT_E## INDEX ##_DIR); \
  370. e_steps[INDEX]++; \
  371. } \
  372. else if (e_steps[INDEX] > 0) { \
  373. E## INDEX ##_DIR_WRITE(!INVERT_E## INDEX ##_DIR); \
  374. e_steps[INDEX]--; \
  375. } \
  376. E## INDEX ##_STEP_WRITE(!INVERT_E_STEP_PIN); \
  377. }
  378. // Step all E steppers that have steps, up to 4 steps per interrupt
  379. for (unsigned char i = 0; i < 4; i++) {
  380. STEP_E_ONCE(0);
  381. #if EXTRUDERS > 1
  382. STEP_E_ONCE(1);
  383. #if EXTRUDERS > 2
  384. STEP_E_ONCE(2);
  385. #if EXTRUDERS > 3
  386. STEP_E_ONCE(3);
  387. #endif
  388. #endif
  389. #endif
  390. }
  391. }
  392. #endif // ADVANCE
  393. void Stepper::init() {
  394. digipot_init(); //Initialize Digipot Motor Current
  395. microstep_init(); //Initialize Microstepping Pins
  396. // initialise TMC Steppers
  397. #if ENABLED(HAVE_TMCDRIVER)
  398. tmc_init();
  399. #endif
  400. // initialise L6470 Steppers
  401. #if ENABLED(HAVE_L6470DRIVER)
  402. L6470_init();
  403. #endif
  404. // Initialize Dir Pins
  405. #if HAS_X_DIR
  406. X_DIR_INIT;
  407. #endif
  408. #if HAS_X2_DIR
  409. X2_DIR_INIT;
  410. #endif
  411. #if HAS_Y_DIR
  412. Y_DIR_INIT;
  413. #if ENABLED(Y_DUAL_STEPPER_DRIVERS) && HAS_Y2_DIR
  414. Y2_DIR_INIT;
  415. #endif
  416. #endif
  417. #if HAS_Z_DIR
  418. Z_DIR_INIT;
  419. #if ENABLED(Z_DUAL_STEPPER_DRIVERS) && HAS_Z2_DIR
  420. Z2_DIR_INIT;
  421. #endif
  422. #endif
  423. #if HAS_E0_DIR
  424. E0_DIR_INIT;
  425. #endif
  426. #if HAS_E1_DIR
  427. E1_DIR_INIT;
  428. #endif
  429. #if HAS_E2_DIR
  430. E2_DIR_INIT;
  431. #endif
  432. #if HAS_E3_DIR
  433. E3_DIR_INIT;
  434. #endif
  435. //Initialize Enable Pins - steppers default to disabled.
  436. #if HAS_X_ENABLE
  437. X_ENABLE_INIT;
  438. if (!X_ENABLE_ON) X_ENABLE_WRITE(HIGH);
  439. #endif
  440. #if HAS_X2_ENABLE
  441. X2_ENABLE_INIT;
  442. if (!X_ENABLE_ON) X2_ENABLE_WRITE(HIGH);
  443. #endif
  444. #if HAS_Y_ENABLE
  445. Y_ENABLE_INIT;
  446. if (!Y_ENABLE_ON) Y_ENABLE_WRITE(HIGH);
  447. #if ENABLED(Y_DUAL_STEPPER_DRIVERS) && HAS_Y2_ENABLE
  448. Y2_ENABLE_INIT;
  449. if (!Y_ENABLE_ON) Y2_ENABLE_WRITE(HIGH);
  450. #endif
  451. #endif
  452. #if HAS_Z_ENABLE
  453. Z_ENABLE_INIT;
  454. if (!Z_ENABLE_ON) Z_ENABLE_WRITE(HIGH);
  455. #if ENABLED(Z_DUAL_STEPPER_DRIVERS) && HAS_Z2_ENABLE
  456. Z2_ENABLE_INIT;
  457. if (!Z_ENABLE_ON) Z2_ENABLE_WRITE(HIGH);
  458. #endif
  459. #endif
  460. #if HAS_E0_ENABLE
  461. E0_ENABLE_INIT;
  462. if (!E_ENABLE_ON) E0_ENABLE_WRITE(HIGH);
  463. #endif
  464. #if HAS_E1_ENABLE
  465. E1_ENABLE_INIT;
  466. if (!E_ENABLE_ON) E1_ENABLE_WRITE(HIGH);
  467. #endif
  468. #if HAS_E2_ENABLE
  469. E2_ENABLE_INIT;
  470. if (!E_ENABLE_ON) E2_ENABLE_WRITE(HIGH);
  471. #endif
  472. #if HAS_E3_ENABLE
  473. E3_ENABLE_INIT;
  474. if (!E_ENABLE_ON) E3_ENABLE_WRITE(HIGH);
  475. #endif
  476. //
  477. // Init endstops and pullups here
  478. //
  479. endstops.init();
  480. #define _STEP_INIT(AXIS) AXIS ##_STEP_INIT
  481. #define _WRITE_STEP(AXIS, HIGHLOW) AXIS ##_STEP_WRITE(HIGHLOW)
  482. #define _DISABLE(axis) disable_## axis()
  483. #define AXIS_INIT(axis, AXIS, PIN) \
  484. _STEP_INIT(AXIS); \
  485. _WRITE_STEP(AXIS, _INVERT_STEP_PIN(PIN)); \
  486. _DISABLE(axis)
  487. #define E_AXIS_INIT(NUM) AXIS_INIT(e## NUM, E## NUM, E)
  488. // Initialize Step Pins
  489. #if HAS_X_STEP
  490. AXIS_INIT(x, X, X);
  491. #endif
  492. #if HAS_X2_STEP
  493. AXIS_INIT(x, X2, X);
  494. #endif
  495. #if HAS_Y_STEP
  496. #if ENABLED(Y_DUAL_STEPPER_DRIVERS) && HAS_Y2_STEP
  497. Y2_STEP_INIT;
  498. Y2_STEP_WRITE(INVERT_Y_STEP_PIN);
  499. #endif
  500. AXIS_INIT(y, Y, Y);
  501. #endif
  502. #if HAS_Z_STEP
  503. #if ENABLED(Z_DUAL_STEPPER_DRIVERS) && HAS_Z2_STEP
  504. Z2_STEP_INIT;
  505. Z2_STEP_WRITE(INVERT_Z_STEP_PIN);
  506. #endif
  507. AXIS_INIT(z, Z, Z);
  508. #endif
  509. #if HAS_E0_STEP
  510. E_AXIS_INIT(0);
  511. #endif
  512. #if HAS_E1_STEP
  513. E_AXIS_INIT(1);
  514. #endif
  515. #if HAS_E2_STEP
  516. E_AXIS_INIT(2);
  517. #endif
  518. #if HAS_E3_STEP
  519. E_AXIS_INIT(3);
  520. #endif
  521. // waveform generation = 0100 = CTC
  522. CBI(TCCR1B, WGM13);
  523. SBI(TCCR1B, WGM12);
  524. CBI(TCCR1A, WGM11);
  525. CBI(TCCR1A, WGM10);
  526. // output mode = 00 (disconnected)
  527. TCCR1A &= ~(3 << COM1A0);
  528. TCCR1A &= ~(3 << COM1B0);
  529. // Set the timer pre-scaler
  530. // Generally we use a divider of 8, resulting in a 2MHz timer
  531. // frequency on a 16MHz MCU. If you are going to change this, be
  532. // sure to regenerate speed_lookuptable.h with
  533. // create_speed_lookuptable.py
  534. TCCR1B = (TCCR1B & ~(0x07 << CS10)) | (2 << CS10);
  535. OCR1A = 0x4000;
  536. TCNT1 = 0;
  537. ENABLE_STEPPER_DRIVER_INTERRUPT();
  538. #if ENABLED(ADVANCE)
  539. #if defined(TCCR0A) && defined(WGM01)
  540. CBI(TCCR0A, WGM01);
  541. CBI(TCCR0A, WGM00);
  542. #endif
  543. e_steps[0] = e_steps[1] = e_steps[2] = e_steps[3] = 0;
  544. SBI(TIMSK0, OCIE0A);
  545. #endif //ADVANCE
  546. endstops.enable(true); // Start with endstops active. After homing they can be disabled
  547. sei();
  548. set_directions(); // Init directions to last_direction_bits = 0
  549. }
  550. /**
  551. * Block until all buffered steps are executed
  552. */
  553. void Stepper::synchronize() { while (planner.blocks_queued()) idle(); }
  554. /**
  555. * Set the stepper positions directly in steps
  556. *
  557. * The input is based on the typical per-axis XYZ steps.
  558. * For CORE machines XYZ needs to be translated to ABC.
  559. *
  560. * This allows get_axis_position_mm to correctly
  561. * derive the current XYZ position later on.
  562. */
  563. void Stepper::set_position(const long& x, const long& y, const long& z, const long& e) {
  564. CRITICAL_SECTION_START;
  565. #if ENABLED(COREXY)
  566. // corexy positioning
  567. // these equations follow the form of the dA and dB equations on http://www.corexy.com/theory.html
  568. count_position[A_AXIS] = x + y;
  569. count_position[B_AXIS] = x - y;
  570. count_position[Z_AXIS] = z;
  571. #elif ENABLED(COREXZ)
  572. // corexz planning
  573. count_position[A_AXIS] = x + z;
  574. count_position[Y_AXIS] = y;
  575. count_position[C_AXIS] = x - z;
  576. #else
  577. // default non-h-bot planning
  578. count_position[X_AXIS] = x;
  579. count_position[Y_AXIS] = y;
  580. count_position[Z_AXIS] = z;
  581. #endif
  582. count_position[E_AXIS] = e;
  583. CRITICAL_SECTION_END;
  584. }
  585. void Stepper::set_e_position(const long& e) {
  586. CRITICAL_SECTION_START;
  587. count_position[E_AXIS] = e;
  588. CRITICAL_SECTION_END;
  589. }
  590. /**
  591. * Get a stepper's position in steps.
  592. */
  593. long Stepper::position(AxisEnum axis) {
  594. CRITICAL_SECTION_START;
  595. long count_pos = count_position[axis];
  596. CRITICAL_SECTION_END;
  597. return count_pos;
  598. }
  599. /**
  600. * Get an axis position according to stepper position(s)
  601. * For CORE machines apply translation from ABC to XYZ.
  602. */
  603. float Stepper::get_axis_position_mm(AxisEnum axis) {
  604. float axis_steps;
  605. #if ENABLED(COREXY) | ENABLED(COREXZ)
  606. if (axis == X_AXIS || axis == CORE_AXIS_2) {
  607. CRITICAL_SECTION_START;
  608. long pos1 = count_position[A_AXIS],
  609. pos2 = count_position[CORE_AXIS_2];
  610. CRITICAL_SECTION_END;
  611. // ((a1+a2)+(a1-a2))/2 -> (a1+a2+a1-a2)/2 -> (a1+a1)/2 -> a1
  612. // ((a1+a2)-(a1-a2))/2 -> (a1+a2-a1+a2)/2 -> (a2+a2)/2 -> a2
  613. axis_steps = (pos1 + ((axis == X_AXIS) ? pos2 : -pos2)) / 2.0f;
  614. }
  615. else
  616. axis_steps = position(axis);
  617. #else
  618. axis_steps = position(axis);
  619. #endif
  620. return axis_steps / planner.axis_steps_per_unit[axis];
  621. }
  622. void Stepper::finish_and_disable() {
  623. synchronize();
  624. disable_all_steppers();
  625. }
  626. void Stepper::quick_stop() {
  627. cleaning_buffer_counter = 5000;
  628. DISABLE_STEPPER_DRIVER_INTERRUPT();
  629. while (planner.blocks_queued()) planner.discard_current_block();
  630. current_block = NULL;
  631. ENABLE_STEPPER_DRIVER_INTERRUPT();
  632. }
  633. void Stepper::endstop_triggered(AxisEnum axis) {
  634. #if ENABLED(COREXY) || ENABLED(COREXZ)
  635. float axis_pos = count_position[axis];
  636. if (axis == A_AXIS)
  637. axis_pos = (axis_pos + count_position[CORE_AXIS_2]) / 2;
  638. else if (axis == CORE_AXIS_2)
  639. axis_pos = (count_position[A_AXIS] - axis_pos) / 2;
  640. endstops_trigsteps[axis] = axis_pos;
  641. #else // !COREXY && !COREXZ
  642. endstops_trigsteps[axis] = count_position[axis];
  643. #endif // !COREXY && !COREXZ
  644. kill_current_block();
  645. }
  646. void Stepper::report_positions() {
  647. CRITICAL_SECTION_START;
  648. long xpos = count_position[X_AXIS],
  649. ypos = count_position[Y_AXIS],
  650. zpos = count_position[Z_AXIS];
  651. CRITICAL_SECTION_END;
  652. #if ENABLED(COREXY) || ENABLED(COREXZ)
  653. SERIAL_PROTOCOLPGM(MSG_COUNT_A);
  654. #else
  655. SERIAL_PROTOCOLPGM(MSG_COUNT_X);
  656. #endif
  657. SERIAL_PROTOCOL(xpos);
  658. #if ENABLED(COREXY) || ENABLED(COREXZ)
  659. SERIAL_PROTOCOLPGM(" B:");
  660. #else
  661. SERIAL_PROTOCOLPGM(" Y:");
  662. #endif
  663. SERIAL_PROTOCOL(ypos);
  664. #if ENABLED(COREXZ) || ENABLED(COREXZ)
  665. SERIAL_PROTOCOLPGM(" C:");
  666. #else
  667. SERIAL_PROTOCOLPGM(" Z:");
  668. #endif
  669. SERIAL_PROTOCOL(zpos);
  670. SERIAL_EOL;
  671. }
  672. #if ENABLED(BABYSTEPPING)
  673. // MUST ONLY BE CALLED BY AN ISR,
  674. // No other ISR should ever interrupt this!
  675. void Stepper::babystep(const uint8_t axis, const bool direction) {
  676. #define _ENABLE(axis) enable_## axis()
  677. #define _READ_DIR(AXIS) AXIS ##_DIR_READ
  678. #define _INVERT_DIR(AXIS) INVERT_## AXIS ##_DIR
  679. #define _APPLY_DIR(AXIS, INVERT) AXIS ##_APPLY_DIR(INVERT, true)
  680. #define BABYSTEP_AXIS(axis, AXIS, INVERT) { \
  681. _ENABLE(axis); \
  682. uint8_t old_pin = _READ_DIR(AXIS); \
  683. _APPLY_DIR(AXIS, _INVERT_DIR(AXIS)^direction^INVERT); \
  684. _APPLY_STEP(AXIS)(!_INVERT_STEP_PIN(AXIS), true); \
  685. delayMicroseconds(2); \
  686. _APPLY_STEP(AXIS)(_INVERT_STEP_PIN(AXIS), true); \
  687. _APPLY_DIR(AXIS, old_pin); \
  688. }
  689. switch (axis) {
  690. case X_AXIS:
  691. BABYSTEP_AXIS(x, X, false);
  692. break;
  693. case Y_AXIS:
  694. BABYSTEP_AXIS(y, Y, false);
  695. break;
  696. case Z_AXIS: {
  697. #if DISABLED(DELTA)
  698. BABYSTEP_AXIS(z, Z, BABYSTEP_INVERT_Z);
  699. #else // DELTA
  700. bool z_direction = direction ^ BABYSTEP_INVERT_Z;
  701. enable_x();
  702. enable_y();
  703. enable_z();
  704. uint8_t old_x_dir_pin = X_DIR_READ,
  705. old_y_dir_pin = Y_DIR_READ,
  706. old_z_dir_pin = Z_DIR_READ;
  707. //setup new step
  708. X_DIR_WRITE(INVERT_X_DIR ^ z_direction);
  709. Y_DIR_WRITE(INVERT_Y_DIR ^ z_direction);
  710. Z_DIR_WRITE(INVERT_Z_DIR ^ z_direction);
  711. //perform step
  712. X_STEP_WRITE(!INVERT_X_STEP_PIN);
  713. Y_STEP_WRITE(!INVERT_Y_STEP_PIN);
  714. Z_STEP_WRITE(!INVERT_Z_STEP_PIN);
  715. delayMicroseconds(2);
  716. X_STEP_WRITE(INVERT_X_STEP_PIN);
  717. Y_STEP_WRITE(INVERT_Y_STEP_PIN);
  718. Z_STEP_WRITE(INVERT_Z_STEP_PIN);
  719. //get old pin state back.
  720. X_DIR_WRITE(old_x_dir_pin);
  721. Y_DIR_WRITE(old_y_dir_pin);
  722. Z_DIR_WRITE(old_z_dir_pin);
  723. #endif
  724. } break;
  725. default: break;
  726. }
  727. }
  728. #endif //BABYSTEPPING
  729. /**
  730. * Software-controlled Stepper Motor Current
  731. */
  732. #if HAS_DIGIPOTSS
  733. // From Arduino DigitalPotControl example
  734. void Stepper::digitalPotWrite(int address, int value) {
  735. digitalWrite(DIGIPOTSS_PIN, LOW); // take the SS pin low to select the chip
  736. SPI.transfer(address); // send in the address and value via SPI:
  737. SPI.transfer(value);
  738. digitalWrite(DIGIPOTSS_PIN, HIGH); // take the SS pin high to de-select the chip:
  739. //delay(10);
  740. }
  741. #endif //HAS_DIGIPOTSS
  742. void Stepper::digipot_init() {
  743. #if HAS_DIGIPOTSS
  744. const uint8_t digipot_motor_current[] = DIGIPOT_MOTOR_CURRENT;
  745. SPI.begin();
  746. pinMode(DIGIPOTSS_PIN, OUTPUT);
  747. for (int i = 0; i < COUNT(digipot_motor_current); i++) {
  748. //digitalPotWrite(digipot_ch[i], digipot_motor_current[i]);
  749. digipot_current(i, digipot_motor_current[i]);
  750. }
  751. #endif
  752. #if HAS_MOTOR_CURRENT_PWM
  753. #if PIN_EXISTS(MOTOR_CURRENT_PWM_XY)
  754. pinMode(MOTOR_CURRENT_PWM_XY_PIN, OUTPUT);
  755. digipot_current(0, motor_current_setting[0]);
  756. #endif
  757. #if PIN_EXISTS(MOTOR_CURRENT_PWM_Z)
  758. pinMode(MOTOR_CURRENT_PWM_Z_PIN, OUTPUT);
  759. digipot_current(1, motor_current_setting[1]);
  760. #endif
  761. #if PIN_EXISTS(MOTOR_CURRENT_PWM_E)
  762. pinMode(MOTOR_CURRENT_PWM_E_PIN, OUTPUT);
  763. digipot_current(2, motor_current_setting[2]);
  764. #endif
  765. //Set timer5 to 31khz so the PWM of the motor power is as constant as possible. (removes a buzzing noise)
  766. TCCR5B = (TCCR5B & ~(_BV(CS50) | _BV(CS51) | _BV(CS52))) | _BV(CS50);
  767. #endif
  768. }
  769. void Stepper::digipot_current(uint8_t driver, int current) {
  770. #if HAS_DIGIPOTSS
  771. const uint8_t digipot_ch[] = DIGIPOT_CHANNELS;
  772. digitalPotWrite(digipot_ch[driver], current);
  773. #elif HAS_MOTOR_CURRENT_PWM
  774. #define _WRITE_CURRENT_PWM(P) analogWrite(P, 255L * current / (MOTOR_CURRENT_PWM_RANGE))
  775. switch (driver) {
  776. #if PIN_EXISTS(MOTOR_CURRENT_PWM_XY)
  777. case 0: _WRITE_CURRENT_PWM(MOTOR_CURRENT_PWM_XY_PIN); break;
  778. #endif
  779. #if PIN_EXISTS(MOTOR_CURRENT_PWM_Z)
  780. case 1: _WRITE_CURRENT_PWM(MOTOR_CURRENT_PWM_Z_PIN); break;
  781. #endif
  782. #if PIN_EXISTS(MOTOR_CURRENT_PWM_E)
  783. case 2: _WRITE_CURRENT_PWM(MOTOR_CURRENT_PWM_E_PIN); break;
  784. #endif
  785. }
  786. #else
  787. UNUSED(driver);
  788. UNUSED(current);
  789. #endif
  790. }
  791. void Stepper::microstep_init() {
  792. #if HAS_MICROSTEPS_E1
  793. pinMode(E1_MS1_PIN, OUTPUT);
  794. pinMode(E1_MS2_PIN, OUTPUT);
  795. #endif
  796. #if HAS_MICROSTEPS
  797. pinMode(X_MS1_PIN, OUTPUT);
  798. pinMode(X_MS2_PIN, OUTPUT);
  799. pinMode(Y_MS1_PIN, OUTPUT);
  800. pinMode(Y_MS2_PIN, OUTPUT);
  801. pinMode(Z_MS1_PIN, OUTPUT);
  802. pinMode(Z_MS2_PIN, OUTPUT);
  803. pinMode(E0_MS1_PIN, OUTPUT);
  804. pinMode(E0_MS2_PIN, OUTPUT);
  805. const uint8_t microstep_modes[] = MICROSTEP_MODES;
  806. for (uint16_t i = 0; i < COUNT(microstep_modes); i++)
  807. microstep_mode(i, microstep_modes[i]);
  808. #endif
  809. }
  810. /**
  811. * Software-controlled Microstepping
  812. */
  813. void Stepper::microstep_ms(uint8_t driver, int8_t ms1, int8_t ms2) {
  814. if (ms1 >= 0) switch (driver) {
  815. case 0: digitalWrite(X_MS1_PIN, ms1); break;
  816. case 1: digitalWrite(Y_MS1_PIN, ms1); break;
  817. case 2: digitalWrite(Z_MS1_PIN, ms1); break;
  818. case 3: digitalWrite(E0_MS1_PIN, ms1); break;
  819. #if HAS_MICROSTEPS_E1
  820. case 4: digitalWrite(E1_MS1_PIN, ms1); break;
  821. #endif
  822. }
  823. if (ms2 >= 0) switch (driver) {
  824. case 0: digitalWrite(X_MS2_PIN, ms2); break;
  825. case 1: digitalWrite(Y_MS2_PIN, ms2); break;
  826. case 2: digitalWrite(Z_MS2_PIN, ms2); break;
  827. case 3: digitalWrite(E0_MS2_PIN, ms2); break;
  828. #if PIN_EXISTS(E1_MS2)
  829. case 4: digitalWrite(E1_MS2_PIN, ms2); break;
  830. #endif
  831. }
  832. }
  833. void Stepper::microstep_mode(uint8_t driver, uint8_t stepping_mode) {
  834. switch (stepping_mode) {
  835. case 1: microstep_ms(driver, MICROSTEP1); break;
  836. case 2: microstep_ms(driver, MICROSTEP2); break;
  837. case 4: microstep_ms(driver, MICROSTEP4); break;
  838. case 8: microstep_ms(driver, MICROSTEP8); break;
  839. case 16: microstep_ms(driver, MICROSTEP16); break;
  840. }
  841. }
  842. void Stepper::microstep_readings() {
  843. SERIAL_PROTOCOLPGM("MS1,MS2 Pins\n");
  844. SERIAL_PROTOCOLPGM("X: ");
  845. SERIAL_PROTOCOL(digitalRead(X_MS1_PIN));
  846. SERIAL_PROTOCOLLN(digitalRead(X_MS2_PIN));
  847. SERIAL_PROTOCOLPGM("Y: ");
  848. SERIAL_PROTOCOL(digitalRead(Y_MS1_PIN));
  849. SERIAL_PROTOCOLLN(digitalRead(Y_MS2_PIN));
  850. SERIAL_PROTOCOLPGM("Z: ");
  851. SERIAL_PROTOCOL(digitalRead(Z_MS1_PIN));
  852. SERIAL_PROTOCOLLN(digitalRead(Z_MS2_PIN));
  853. SERIAL_PROTOCOLPGM("E0: ");
  854. SERIAL_PROTOCOL(digitalRead(E0_MS1_PIN));
  855. SERIAL_PROTOCOLLN(digitalRead(E0_MS2_PIN));
  856. #if HAS_MICROSTEPS_E1
  857. SERIAL_PROTOCOLPGM("E1: ");
  858. SERIAL_PROTOCOL(digitalRead(E1_MS1_PIN));
  859. SERIAL_PROTOCOLLN(digitalRead(E1_MS2_PIN));
  860. #endif
  861. }