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

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472
  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. // public:
  58. block_t* Stepper::current_block = NULL; // A pointer to the block currently being traced
  59. #if ENABLED(ABORT_ON_ENDSTOP_HIT_FEATURE_ENABLED)
  60. bool Stepper::abort_on_endstop_hit = false;
  61. #endif
  62. #if ENABLED(Z_DUAL_ENDSTOPS)
  63. bool Stepper::performing_homing = false;
  64. #endif
  65. // private:
  66. unsigned char Stepper::last_direction_bits = 0; // The next stepping-bits to be output
  67. unsigned int Stepper::cleaning_buffer_counter = 0;
  68. #if ENABLED(Z_DUAL_ENDSTOPS)
  69. bool Stepper::locked_z_motor = false;
  70. bool Stepper::locked_z2_motor = false;
  71. #endif
  72. long Stepper::counter_X = 0,
  73. Stepper::counter_Y = 0,
  74. Stepper::counter_Z = 0,
  75. Stepper::counter_E = 0;
  76. volatile uint32_t Stepper::step_events_completed = 0; // The number of step events executed in the current block
  77. #if ENABLED(ADVANCE) || ENABLED(LIN_ADVANCE)
  78. constexpr uint16_t ADV_NEVER = 65535;
  79. uint16_t Stepper::nextMainISR = 0,
  80. Stepper::nextAdvanceISR = ADV_NEVER,
  81. Stepper::eISR_Rate = ADV_NEVER;
  82. #if ENABLED(LIN_ADVANCE)
  83. volatile int Stepper::e_steps[E_STEPPERS];
  84. int Stepper::final_estep_rate,
  85. Stepper::current_estep_rate[E_STEPPERS],
  86. Stepper::current_adv_steps[E_STEPPERS];
  87. #else
  88. long Stepper::e_steps[E_STEPPERS],
  89. Stepper::final_advance = 0,
  90. Stepper::old_advance = 0,
  91. Stepper::advance_rate,
  92. Stepper::advance;
  93. #endif
  94. #define ADV_RATE(T, L) (e_steps[TOOL_E_INDEX] ? (T) * (L) / abs(e_steps[TOOL_E_INDEX]) : ADV_NEVER)
  95. #endif
  96. long Stepper::acceleration_time, Stepper::deceleration_time;
  97. volatile long Stepper::count_position[NUM_AXIS] = { 0 };
  98. volatile signed char Stepper::count_direction[NUM_AXIS] = { 1, 1, 1, 1 };
  99. #if ENABLED(MIXING_EXTRUDER)
  100. long Stepper::counter_m[MIXING_STEPPERS];
  101. #endif
  102. unsigned short Stepper::acc_step_rate; // needed for deceleration start point
  103. uint8_t Stepper::step_loops, Stepper::step_loops_nominal;
  104. unsigned short Stepper::OCR1A_nominal;
  105. volatile long Stepper::endstops_trigsteps[XYZ];
  106. #if ENABLED(X_DUAL_STEPPER_DRIVERS)
  107. #define X_APPLY_DIR(v,Q) do{ X_DIR_WRITE(v); X2_DIR_WRITE((v) != INVERT_X2_VS_X_DIR); }while(0)
  108. #define X_APPLY_STEP(v,Q) do{ X_STEP_WRITE(v); X2_STEP_WRITE(v); }while(0)
  109. #elif ENABLED(DUAL_X_CARRIAGE)
  110. #define X_APPLY_DIR(v,ALWAYS) \
  111. if (extruder_duplication_enabled || ALWAYS) { \
  112. X_DIR_WRITE(v); \
  113. X2_DIR_WRITE(v); \
  114. } \
  115. else { \
  116. if (current_block->active_extruder) X2_DIR_WRITE(v); else X_DIR_WRITE(v); \
  117. }
  118. #define X_APPLY_STEP(v,ALWAYS) \
  119. if (extruder_duplication_enabled || ALWAYS) { \
  120. X_STEP_WRITE(v); \
  121. X2_STEP_WRITE(v); \
  122. } \
  123. else { \
  124. if (current_block->active_extruder != 0) X2_STEP_WRITE(v); else X_STEP_WRITE(v); \
  125. }
  126. #else
  127. #define X_APPLY_DIR(v,Q) X_DIR_WRITE(v)
  128. #define X_APPLY_STEP(v,Q) X_STEP_WRITE(v)
  129. #endif
  130. #if ENABLED(Y_DUAL_STEPPER_DRIVERS)
  131. #define Y_APPLY_DIR(v,Q) do{ Y_DIR_WRITE(v); Y2_DIR_WRITE((v) != INVERT_Y2_VS_Y_DIR); }while(0)
  132. #define Y_APPLY_STEP(v,Q) do{ Y_STEP_WRITE(v); Y2_STEP_WRITE(v); }while(0)
  133. #else
  134. #define Y_APPLY_DIR(v,Q) Y_DIR_WRITE(v)
  135. #define Y_APPLY_STEP(v,Q) Y_STEP_WRITE(v)
  136. #endif
  137. #if ENABLED(Z_DUAL_STEPPER_DRIVERS)
  138. #define Z_APPLY_DIR(v,Q) do{ Z_DIR_WRITE(v); Z2_DIR_WRITE(v); }while(0)
  139. #if ENABLED(Z_DUAL_ENDSTOPS)
  140. #define Z_APPLY_STEP(v,Q) \
  141. if (performing_homing) { \
  142. if (Z_HOME_DIR < 0) { \
  143. if (!(TEST(endstops.old_endstop_bits, Z_MIN) && (count_direction[Z_AXIS] < 0)) && !locked_z_motor) Z_STEP_WRITE(v); \
  144. if (!(TEST(endstops.old_endstop_bits, Z2_MIN) && (count_direction[Z_AXIS] < 0)) && !locked_z2_motor) Z2_STEP_WRITE(v); \
  145. } \
  146. else { \
  147. if (!(TEST(endstops.old_endstop_bits, Z_MAX) && (count_direction[Z_AXIS] > 0)) && !locked_z_motor) Z_STEP_WRITE(v); \
  148. if (!(TEST(endstops.old_endstop_bits, Z2_MAX) && (count_direction[Z_AXIS] > 0)) && !locked_z2_motor) Z2_STEP_WRITE(v); \
  149. } \
  150. } \
  151. else { \
  152. Z_STEP_WRITE(v); \
  153. Z2_STEP_WRITE(v); \
  154. }
  155. #else
  156. #define Z_APPLY_STEP(v,Q) do{ Z_STEP_WRITE(v); Z2_STEP_WRITE(v); }while(0)
  157. #endif
  158. #else
  159. #define Z_APPLY_DIR(v,Q) Z_DIR_WRITE(v)
  160. #define Z_APPLY_STEP(v,Q) Z_STEP_WRITE(v)
  161. #endif
  162. #if DISABLED(MIXING_EXTRUDER)
  163. #define E_APPLY_STEP(v,Q) E_STEP_WRITE(v)
  164. #endif
  165. // intRes = longIn1 * longIn2 >> 24
  166. // uses:
  167. // r26 to store 0
  168. // r27 to store bits 16-23 of the 48bit result. The top bit is used to round the two byte result.
  169. // note that the lower two bytes and the upper byte of the 48bit result are not calculated.
  170. // this can cause the result to be out by one as the lower bytes may cause carries into the upper ones.
  171. // B0 A0 are bits 24-39 and are the returned value
  172. // C1 B1 A1 is longIn1
  173. // D2 C2 B2 A2 is longIn2
  174. //
  175. #define MultiU24X32toH16(intRes, longIn1, longIn2) \
  176. asm volatile ( \
  177. "clr r26 \n\t" \
  178. "mul %A1, %B2 \n\t" \
  179. "mov r27, r1 \n\t" \
  180. "mul %B1, %C2 \n\t" \
  181. "movw %A0, r0 \n\t" \
  182. "mul %C1, %C2 \n\t" \
  183. "add %B0, r0 \n\t" \
  184. "mul %C1, %B2 \n\t" \
  185. "add %A0, r0 \n\t" \
  186. "adc %B0, r1 \n\t" \
  187. "mul %A1, %C2 \n\t" \
  188. "add r27, r0 \n\t" \
  189. "adc %A0, r1 \n\t" \
  190. "adc %B0, r26 \n\t" \
  191. "mul %B1, %B2 \n\t" \
  192. "add r27, r0 \n\t" \
  193. "adc %A0, r1 \n\t" \
  194. "adc %B0, r26 \n\t" \
  195. "mul %C1, %A2 \n\t" \
  196. "add r27, r0 \n\t" \
  197. "adc %A0, r1 \n\t" \
  198. "adc %B0, r26 \n\t" \
  199. "mul %B1, %A2 \n\t" \
  200. "add r27, r1 \n\t" \
  201. "adc %A0, r26 \n\t" \
  202. "adc %B0, r26 \n\t" \
  203. "lsr r27 \n\t" \
  204. "adc %A0, r26 \n\t" \
  205. "adc %B0, r26 \n\t" \
  206. "mul %D2, %A1 \n\t" \
  207. "add %A0, r0 \n\t" \
  208. "adc %B0, r1 \n\t" \
  209. "mul %D2, %B1 \n\t" \
  210. "add %B0, r0 \n\t" \
  211. "clr r1 \n\t" \
  212. : \
  213. "=&r" (intRes) \
  214. : \
  215. "d" (longIn1), \
  216. "d" (longIn2) \
  217. : \
  218. "r26" , "r27" \
  219. )
  220. // Some useful constants
  221. #define ENABLE_STEPPER_DRIVER_INTERRUPT() SBI(TIMSK1, OCIE1A)
  222. #define DISABLE_STEPPER_DRIVER_INTERRUPT() CBI(TIMSK1, OCIE1A)
  223. /**
  224. * __________________________
  225. * /| |\ _________________ ^
  226. * / | | \ /| |\ |
  227. * / | | \ / | | \ s
  228. * / | | | | | \ p
  229. * / | | | | | \ e
  230. * +-----+------------------------+---+--+---------------+----+ e
  231. * | BLOCK 1 | BLOCK 2 | d
  232. *
  233. * time ----->
  234. *
  235. * The trapezoid is the shape the speed curve over time. It starts at block->initial_rate, accelerates
  236. * first block->accelerate_until step_events_completed, then keeps going at constant speed until
  237. * step_events_completed reaches block->decelerate_after after which it decelerates until the trapezoid generator is reset.
  238. * The slope of acceleration is calculated using v = u + at where t is the accumulated timer values of the steps so far.
  239. */
  240. void Stepper::wake_up() {
  241. // TCNT1 = 0;
  242. ENABLE_STEPPER_DRIVER_INTERRUPT();
  243. }
  244. /**
  245. * Set the stepper direction of each axis
  246. *
  247. * COREXY: X_AXIS=A_AXIS and Y_AXIS=B_AXIS
  248. * COREXZ: X_AXIS=A_AXIS and Z_AXIS=C_AXIS
  249. * COREYZ: Y_AXIS=B_AXIS and Z_AXIS=C_AXIS
  250. */
  251. void Stepper::set_directions() {
  252. #define SET_STEP_DIR(AXIS) \
  253. if (motor_direction(AXIS ##_AXIS)) { \
  254. AXIS ##_APPLY_DIR(INVERT_## AXIS ##_DIR, false); \
  255. count_direction[AXIS ##_AXIS] = -1; \
  256. } \
  257. else { \
  258. AXIS ##_APPLY_DIR(!INVERT_## AXIS ##_DIR, false); \
  259. count_direction[AXIS ##_AXIS] = 1; \
  260. }
  261. #if HAS_X_DIR
  262. SET_STEP_DIR(X); // A
  263. #endif
  264. #if HAS_Y_DIR
  265. SET_STEP_DIR(Y); // B
  266. #endif
  267. #if HAS_Z_DIR
  268. SET_STEP_DIR(Z); // C
  269. #endif
  270. #if DISABLED(ADVANCE) && DISABLED(LIN_ADVANCE)
  271. if (motor_direction(E_AXIS)) {
  272. REV_E_DIR();
  273. count_direction[E_AXIS] = -1;
  274. }
  275. else {
  276. NORM_E_DIR();
  277. count_direction[E_AXIS] = 1;
  278. }
  279. #endif // !ADVANCE && !LIN_ADVANCE
  280. }
  281. #if ENABLED(ENDSTOP_INTERRUPTS_FEATURE)
  282. extern volatile uint8_t e_hit;
  283. #endif
  284. /**
  285. * Stepper Driver Interrupt
  286. *
  287. * Directly pulses the stepper motors at high frequency.
  288. * Timer 1 runs at a base frequency of 2MHz, with this ISR using OCR1A compare mode.
  289. *
  290. * OCR1A Frequency
  291. * 1 2 MHz
  292. * 50 40 KHz
  293. * 100 20 KHz - capped max rate
  294. * 200 10 KHz - nominal max rate
  295. * 2000 1 KHz - sleep rate
  296. * 4000 500 Hz - init rate
  297. */
  298. ISR(TIMER1_COMPA_vect) {
  299. #if ENABLED(ADVANCE) || ENABLED(LIN_ADVANCE)
  300. Stepper::advance_isr_scheduler();
  301. #else
  302. Stepper::isr();
  303. #endif
  304. }
  305. void Stepper::isr() {
  306. #define _ENABLE_ISRs() cli(); SBI(TIMSK0, OCIE0B); ENABLE_STEPPER_DRIVER_INTERRUPT()
  307. uint16_t timer, remainder, ocr_val;
  308. static uint32_t step_remaining = 0;
  309. #define ENDSTOP_NOMINAL_OCR_VAL 3000 // check endstops every 1.5ms to guarantee two stepper ISRs within 5ms for BLTouch
  310. #define OCR_VAL_TOLERANCE 1000 // First max delay is 2.0ms, last min delay is 0.5ms, all others 1.5ms
  311. #if DISABLED(ADVANCE) && DISABLED(LIN_ADVANCE)
  312. // Disable Timer0 ISRs and enable global ISR again to capture UART events (incoming chars)
  313. CBI(TIMSK0, OCIE0B); // Temperature ISR
  314. DISABLE_STEPPER_DRIVER_INTERRUPT();
  315. sei();
  316. #endif
  317. #define _SPLIT(L) (ocr_val = (uint16_t)L)
  318. #if ENABLED(ENDSTOP_INTERRUPTS_FEATURE)
  319. #define SPLIT(L) _SPLIT(L)
  320. #else // sample endstops in between step pulses
  321. #define SPLIT(L) do { \
  322. _SPLIT(L); \
  323. if (ENDSTOPS_ENABLED && L > ENDSTOP_NOMINAL_OCR_VAL) { \
  324. remainder = (uint16_t)L % (ENDSTOP_NOMINAL_OCR_VAL); \
  325. ocr_val = (remainder < OCR_VAL_TOLERANCE) ? ENDSTOP_NOMINAL_OCR_VAL + remainder : ENDSTOP_NOMINAL_OCR_VAL; \
  326. step_remaining = (uint16_t)L - ocr_val; \
  327. } \
  328. } while(0)
  329. if (step_remaining && ENDSTOPS_ENABLED) { // Just check endstops - not yet time for a step
  330. endstops.update();
  331. ocr_val = step_remaining;
  332. if (step_remaining > ENDSTOP_NOMINAL_OCR_VAL) {
  333. step_remaining = step_remaining - ENDSTOP_NOMINAL_OCR_VAL;
  334. ocr_val = ENDSTOP_NOMINAL_OCR_VAL;
  335. }
  336. else step_remaining = 0; // last one before the ISR that does the step
  337. _NEXT_ISR(ocr_val); //
  338. NOLESS(OCR1A, TCNT1 + 16);
  339. _ENABLE_ISRs(); // re-enable ISRs
  340. return;
  341. }
  342. # endif
  343. if (cleaning_buffer_counter) {
  344. --cleaning_buffer_counter;
  345. current_block = NULL;
  346. planner.discard_current_block();
  347. #ifdef SD_FINISHED_RELEASECOMMAND
  348. if (!cleaning_buffer_counter && (SD_FINISHED_STEPPERRELEASE)) enqueue_and_echo_commands_P(PSTR(SD_FINISHED_RELEASECOMMAND));
  349. #endif
  350. _NEXT_ISR(200); // Run at max speed - 10 KHz
  351. _ENABLE_ISRs(); // re-enable ISRs
  352. return;
  353. }
  354. // If there is no current block, attempt to pop one from the buffer
  355. if (!current_block) {
  356. // Anything in the buffer?
  357. current_block = planner.get_current_block();
  358. if (current_block) {
  359. trapezoid_generator_reset();
  360. // Initialize Bresenham counters to 1/2 the ceiling
  361. counter_X = counter_Y = counter_Z = counter_E = -(current_block->step_event_count >> 1);
  362. #if ENABLED(MIXING_EXTRUDER)
  363. MIXING_STEPPERS_LOOP(i)
  364. counter_m[i] = -(current_block->mix_event_count[i] >> 1);
  365. #endif
  366. step_events_completed = 0;
  367. #if ENABLED(ENDSTOP_INTERRUPTS_FEATURE)
  368. e_hit = 2; // Needed for the case an endstop is already triggered before the new move begins.
  369. // No 'change' can be detected.
  370. #endif
  371. #if ENABLED(Z_LATE_ENABLE)
  372. if (current_block->steps[Z_AXIS] > 0) {
  373. enable_z();
  374. _NEXT_ISR(2000); // Run at slow speed - 1 KHz
  375. _ENABLE_ISRs(); // re-enable ISRs
  376. return;
  377. }
  378. #endif
  379. // #if ENABLED(ADVANCE)
  380. // e_steps[TOOL_E_INDEX] = 0;
  381. // #endif
  382. }
  383. else {
  384. _NEXT_ISR(2000); // Run at slow speed - 1 KHz
  385. _ENABLE_ISRs(); // re-enable ISRs
  386. return;
  387. }
  388. }
  389. // Update endstops state, if enabled
  390. #if ENABLED(ENDSTOP_INTERRUPTS_FEATURE)
  391. if (ENDSTOPS_ENABLED && e_hit) {
  392. endstops.update();
  393. e_hit--;
  394. }
  395. #else
  396. if (ENDSTOPS_ENABLED) endstops.update();
  397. #endif
  398. // Take multiple steps per interrupt (For high speed moves)
  399. bool all_steps_done = false;
  400. for (int8_t i = 0; i < step_loops; i++) {
  401. #if ENABLED(LIN_ADVANCE)
  402. counter_E += current_block->steps[E_AXIS];
  403. if (counter_E > 0) {
  404. counter_E -= current_block->step_event_count;
  405. #if DISABLED(MIXING_EXTRUDER)
  406. // Don't step E here for mixing extruder
  407. count_position[E_AXIS] += count_direction[E_AXIS];
  408. motor_direction(E_AXIS) ? --e_steps[TOOL_E_INDEX] : ++e_steps[TOOL_E_INDEX];
  409. #endif
  410. }
  411. #if ENABLED(MIXING_EXTRUDER)
  412. // Step mixing steppers proportionally
  413. const bool dir = motor_direction(E_AXIS);
  414. MIXING_STEPPERS_LOOP(j) {
  415. counter_m[j] += current_block->steps[E_AXIS];
  416. if (counter_m[j] > 0) {
  417. counter_m[j] -= current_block->mix_event_count[j];
  418. dir ? --e_steps[j] : ++e_steps[j];
  419. }
  420. }
  421. #endif
  422. #elif ENABLED(ADVANCE)
  423. // Always count the unified E axis
  424. counter_E += current_block->steps[E_AXIS];
  425. if (counter_E > 0) {
  426. counter_E -= current_block->step_event_count;
  427. #if DISABLED(MIXING_EXTRUDER)
  428. // Don't step E here for mixing extruder
  429. motor_direction(E_AXIS) ? --e_steps[TOOL_E_INDEX] : ++e_steps[TOOL_E_INDEX];
  430. #endif
  431. }
  432. #if ENABLED(MIXING_EXTRUDER)
  433. // Step mixing steppers proportionally
  434. const bool dir = motor_direction(E_AXIS);
  435. MIXING_STEPPERS_LOOP(j) {
  436. counter_m[j] += current_block->steps[E_AXIS];
  437. if (counter_m[j] > 0) {
  438. counter_m[j] -= current_block->mix_event_count[j];
  439. dir ? --e_steps[j] : ++e_steps[j];
  440. }
  441. }
  442. #endif // MIXING_EXTRUDER
  443. #endif // ADVANCE or LIN_ADVANCE
  444. #define _COUNTER(AXIS) counter_## AXIS
  445. #define _APPLY_STEP(AXIS) AXIS ##_APPLY_STEP
  446. #define _INVERT_STEP_PIN(AXIS) INVERT_## AXIS ##_STEP_PIN
  447. // Advance the Bresenham counter; start a pulse if the axis needs a step
  448. #define PULSE_START(AXIS) \
  449. _COUNTER(AXIS) += current_block->steps[_AXIS(AXIS)]; \
  450. if (_COUNTER(AXIS) > 0) { _APPLY_STEP(AXIS)(!_INVERT_STEP_PIN(AXIS),0); }
  451. // Stop an active pulse, reset the Bresenham counter, update the position
  452. #define PULSE_STOP(AXIS) \
  453. if (_COUNTER(AXIS) > 0) { \
  454. _COUNTER(AXIS) -= current_block->step_event_count; \
  455. count_position[_AXIS(AXIS)] += count_direction[_AXIS(AXIS)]; \
  456. _APPLY_STEP(AXIS)(_INVERT_STEP_PIN(AXIS),0); \
  457. }
  458. #define CYCLES_EATEN_BY_CODE 240
  459. // If a minimum pulse time was specified get the CPU clock
  460. #if STEP_PULSE_CYCLES > CYCLES_EATEN_BY_CODE
  461. static uint32_t pulse_start;
  462. pulse_start = TCNT0;
  463. #endif
  464. #if HAS_X_STEP
  465. PULSE_START(X);
  466. #endif
  467. #if HAS_Y_STEP
  468. PULSE_START(Y);
  469. #endif
  470. #if HAS_Z_STEP
  471. PULSE_START(Z);
  472. #endif
  473. // For non-advance use linear interpolation for E also
  474. #if DISABLED(ADVANCE) && DISABLED(LIN_ADVANCE)
  475. #if ENABLED(MIXING_EXTRUDER)
  476. // Keep updating the single E axis
  477. counter_E += current_block->steps[E_AXIS];
  478. // Tick the counters used for this mix
  479. MIXING_STEPPERS_LOOP(j) {
  480. // Step mixing steppers (proportionally)
  481. counter_m[j] += current_block->steps[E_AXIS];
  482. // Step when the counter goes over zero
  483. if (counter_m[j] > 0) En_STEP_WRITE(j, !INVERT_E_STEP_PIN);
  484. }
  485. #else // !MIXING_EXTRUDER
  486. PULSE_START(E);
  487. #endif
  488. #endif // !ADVANCE && !LIN_ADVANCE
  489. // For a minimum pulse time wait before stopping pulses
  490. #if STEP_PULSE_CYCLES > CYCLES_EATEN_BY_CODE
  491. while ((uint32_t)(TCNT0 - pulse_start) < STEP_PULSE_CYCLES - CYCLES_EATEN_BY_CODE) { /* nada */ }
  492. #endif
  493. #if HAS_X_STEP
  494. PULSE_STOP(X);
  495. #endif
  496. #if HAS_Y_STEP
  497. PULSE_STOP(Y);
  498. #endif
  499. #if HAS_Z_STEP
  500. PULSE_STOP(Z);
  501. #endif
  502. #if DISABLED(ADVANCE) && DISABLED(LIN_ADVANCE)
  503. #if ENABLED(MIXING_EXTRUDER)
  504. // Always step the single E axis
  505. if (counter_E > 0) {
  506. counter_E -= current_block->step_event_count;
  507. count_position[E_AXIS] += count_direction[E_AXIS];
  508. }
  509. MIXING_STEPPERS_LOOP(j) {
  510. if (counter_m[j] > 0) {
  511. counter_m[j] -= current_block->mix_event_count[j];
  512. En_STEP_WRITE(j, INVERT_E_STEP_PIN);
  513. }
  514. }
  515. #else // !MIXING_EXTRUDER
  516. PULSE_STOP(E);
  517. #endif
  518. #endif // !ADVANCE && !LIN_ADVANCE
  519. if (++step_events_completed >= current_block->step_event_count) {
  520. all_steps_done = true;
  521. break;
  522. }
  523. }
  524. #if ENABLED(LIN_ADVANCE)
  525. if (current_block->use_advance_lead) {
  526. int delta_adv_steps = current_estep_rate[TOOL_E_INDEX] - current_adv_steps[TOOL_E_INDEX];
  527. current_adv_steps[TOOL_E_INDEX] += delta_adv_steps;
  528. #if ENABLED(MIXING_EXTRUDER)
  529. // Mixing extruders apply advance lead proportionally
  530. MIXING_STEPPERS_LOOP(j)
  531. e_steps[j] += delta_adv_steps * current_block->step_event_count / current_block->mix_event_count[j];
  532. #else
  533. // For most extruders, advance the single E stepper
  534. e_steps[TOOL_E_INDEX] += delta_adv_steps;
  535. #endif
  536. }
  537. #endif
  538. #if ENABLED(ADVANCE) || ENABLED(LIN_ADVANCE)
  539. // If we have esteps to execute, fire the next advance_isr "now"
  540. if (e_steps[TOOL_E_INDEX]) nextAdvanceISR = 0;
  541. #endif
  542. // Calculate new timer value
  543. if (step_events_completed <= (uint32_t)current_block->accelerate_until) {
  544. MultiU24X32toH16(acc_step_rate, acceleration_time, current_block->acceleration_rate);
  545. acc_step_rate += current_block->initial_rate;
  546. // upper limit
  547. NOMORE(acc_step_rate, current_block->nominal_rate);
  548. // step_rate to timer interval
  549. uint16_t timer = calc_timer(acc_step_rate);
  550. SPLIT(timer); // split step into multiple ISRs if larger than ENDSTOP_NOMINAL_OCR_VAL
  551. _NEXT_ISR(ocr_val);
  552. acceleration_time += timer;
  553. #if ENABLED(LIN_ADVANCE)
  554. if (current_block->use_advance_lead) {
  555. #if ENABLED(MIXING_EXTRUDER)
  556. MIXING_STEPPERS_LOOP(j)
  557. current_estep_rate[j] = ((uint32_t)acc_step_rate * current_block->abs_adv_steps_multiplier8 * current_block->step_event_count / current_block->mix_event_count[j]) >> 17;
  558. #else
  559. current_estep_rate[TOOL_E_INDEX] = ((uint32_t)acc_step_rate * current_block->abs_adv_steps_multiplier8) >> 17;
  560. #endif
  561. }
  562. #elif ENABLED(ADVANCE)
  563. advance += advance_rate * step_loops;
  564. //NOLESS(advance, current_block->advance);
  565. long advance_whole = advance >> 8,
  566. advance_factor = advance_whole - old_advance;
  567. // Do E steps + advance steps
  568. #if ENABLED(MIXING_EXTRUDER)
  569. // ...for mixing steppers proportionally
  570. MIXING_STEPPERS_LOOP(j)
  571. e_steps[j] += advance_factor * current_block->step_event_count / current_block->mix_event_count[j];
  572. #else
  573. // ...for the active extruder
  574. e_steps[TOOL_E_INDEX] += advance_factor;
  575. #endif
  576. old_advance = advance_whole;
  577. #endif // ADVANCE or LIN_ADVANCE
  578. #if ENABLED(ADVANCE) || ENABLED(LIN_ADVANCE)
  579. eISR_Rate = ADV_RATE(timer, step_loops);
  580. #endif
  581. }
  582. else if (step_events_completed > (uint32_t)current_block->decelerate_after) {
  583. uint16_t step_rate;
  584. MultiU24X32toH16(step_rate, deceleration_time, current_block->acceleration_rate);
  585. if (step_rate < acc_step_rate) { // Still decelerating?
  586. step_rate = acc_step_rate - step_rate;
  587. NOLESS(step_rate, current_block->final_rate);
  588. }
  589. else
  590. step_rate = current_block->final_rate;
  591. // step_rate to timer interval
  592. uint16_t timer = calc_timer(step_rate);
  593. SPLIT(timer); // split step into multiple ISRs if larger than ENDSTOP_NOMINAL_OCR_VAL
  594. _NEXT_ISR(ocr_val);
  595. deceleration_time += timer;
  596. #if ENABLED(LIN_ADVANCE)
  597. if (current_block->use_advance_lead) {
  598. #if ENABLED(MIXING_EXTRUDER)
  599. MIXING_STEPPERS_LOOP(j)
  600. current_estep_rate[j] = ((uint32_t)step_rate * current_block->abs_adv_steps_multiplier8 * current_block->step_event_count / current_block->mix_event_count[j]) >> 17;
  601. #else
  602. current_estep_rate[TOOL_E_INDEX] = ((uint32_t)step_rate * current_block->abs_adv_steps_multiplier8) >> 17;
  603. #endif
  604. }
  605. #elif ENABLED(ADVANCE)
  606. advance -= advance_rate * step_loops;
  607. NOLESS(advance, final_advance);
  608. // Do E steps + advance steps
  609. long advance_whole = advance >> 8,
  610. advance_factor = advance_whole - old_advance;
  611. #if ENABLED(MIXING_EXTRUDER)
  612. MIXING_STEPPERS_LOOP(j)
  613. e_steps[j] += advance_factor * current_block->step_event_count / current_block->mix_event_count[j];
  614. #else
  615. e_steps[TOOL_E_INDEX] += advance_factor;
  616. #endif
  617. old_advance = advance_whole;
  618. #endif // ADVANCE or LIN_ADVANCE
  619. #if ENABLED(ADVANCE) || ENABLED(LIN_ADVANCE)
  620. eISR_Rate = ADV_RATE(timer, step_loops);
  621. #endif
  622. }
  623. else {
  624. #if ENABLED(LIN_ADVANCE)
  625. if (current_block->use_advance_lead)
  626. current_estep_rate[TOOL_E_INDEX] = final_estep_rate;
  627. eISR_Rate = ADV_RATE(OCR1A_nominal, step_loops_nominal);
  628. #endif
  629. SPLIT(OCR1A_nominal); // split step into multiple ISRs if larger than ENDSTOP_NOMINAL_OCR_VAL
  630. _NEXT_ISR(ocr_val);
  631. // ensure we're running at the correct step rate, even if we just came off an acceleration
  632. step_loops = step_loops_nominal;
  633. }
  634. #if DISABLED(ADVANCE) && DISABLED(LIN_ADVANCE)
  635. NOLESS(OCR1A, TCNT1 + 16);
  636. #endif
  637. // If current block is finished, reset pointer
  638. if (all_steps_done) {
  639. current_block = NULL;
  640. planner.discard_current_block();
  641. }
  642. #if DISABLED(ADVANCE) && DISABLED(LIN_ADVANCE)
  643. _ENABLE_ISRs(); // re-enable ISRs
  644. #endif
  645. }
  646. #if ENABLED(ADVANCE) || ENABLED(LIN_ADVANCE)
  647. // Timer interrupt for E. e_steps is set in the main routine;
  648. void Stepper::advance_isr() {
  649. nextAdvanceISR = eISR_Rate;
  650. #define SET_E_STEP_DIR(INDEX) \
  651. if (e_steps[INDEX]) E## INDEX ##_DIR_WRITE(e_steps[INDEX] < 0 ? INVERT_E## INDEX ##_DIR : !INVERT_E## INDEX ##_DIR)
  652. #define START_E_PULSE(INDEX) \
  653. if (e_steps[INDEX]) E## INDEX ##_STEP_WRITE(!INVERT_E_STEP_PIN)
  654. #define STOP_E_PULSE(INDEX) \
  655. if (e_steps[INDEX]) { \
  656. e_steps[INDEX] < 0 ? ++e_steps[INDEX] : --e_steps[INDEX]; \
  657. E## INDEX ##_STEP_WRITE(INVERT_E_STEP_PIN); \
  658. }
  659. SET_E_STEP_DIR(0);
  660. #if E_STEPPERS > 1
  661. SET_E_STEP_DIR(1);
  662. #if E_STEPPERS > 2
  663. SET_E_STEP_DIR(2);
  664. #if E_STEPPERS > 3
  665. SET_E_STEP_DIR(3);
  666. #endif
  667. #endif
  668. #endif
  669. #define CYCLES_EATEN_BY_E 60
  670. // Step all E steppers that have steps
  671. for (uint8_t i = 0; i < step_loops; i++) {
  672. #if STEP_PULSE_CYCLES > CYCLES_EATEN_BY_E
  673. static uint32_t pulse_start;
  674. pulse_start = TCNT0;
  675. #endif
  676. START_E_PULSE(0);
  677. #if E_STEPPERS > 1
  678. START_E_PULSE(1);
  679. #if E_STEPPERS > 2
  680. START_E_PULSE(2);
  681. #if E_STEPPERS > 3
  682. START_E_PULSE(3);
  683. #endif
  684. #endif
  685. #endif
  686. // For a minimum pulse time wait before stopping pulses
  687. #if STEP_PULSE_CYCLES > CYCLES_EATEN_BY_E
  688. while ((uint32_t)(TCNT0 - pulse_start) < STEP_PULSE_CYCLES - CYCLES_EATEN_BY_E) { /* nada */ }
  689. #endif
  690. STOP_E_PULSE(0);
  691. #if E_STEPPERS > 1
  692. STOP_E_PULSE(1);
  693. #if E_STEPPERS > 2
  694. STOP_E_PULSE(2);
  695. #if E_STEPPERS > 3
  696. STOP_E_PULSE(3);
  697. #endif
  698. #endif
  699. #endif
  700. }
  701. }
  702. void Stepper::advance_isr_scheduler() {
  703. // Disable Timer0 ISRs and enable global ISR again to capture UART events (incoming chars)
  704. CBI(TIMSK0, OCIE0B); // Temperature ISR
  705. DISABLE_STEPPER_DRIVER_INTERRUPT();
  706. sei();
  707. // Run main stepping ISR if flagged
  708. if (!nextMainISR) isr();
  709. // Run Advance stepping ISR if flagged
  710. if (!nextAdvanceISR) advance_isr();
  711. // Is the next advance ISR scheduled before the next main ISR?
  712. if (nextAdvanceISR <= nextMainISR) {
  713. // Set up the next interrupt
  714. OCR1A = nextAdvanceISR;
  715. // New interval for the next main ISR
  716. if (nextMainISR) nextMainISR -= nextAdvanceISR;
  717. // Will call Stepper::advance_isr on the next interrupt
  718. nextAdvanceISR = 0;
  719. }
  720. else {
  721. // The next main ISR comes first
  722. OCR1A = nextMainISR;
  723. // New interval for the next advance ISR, if any
  724. if (nextAdvanceISR && nextAdvanceISR != ADV_NEVER)
  725. nextAdvanceISR -= nextMainISR;
  726. // Will call Stepper::isr on the next interrupt
  727. nextMainISR = 0;
  728. }
  729. // Don't run the ISR faster than possible
  730. NOLESS(OCR1A, TCNT1 + 16);
  731. // Restore original ISR settings
  732. cli();
  733. SBI(TIMSK0, OCIE0B);
  734. ENABLE_STEPPER_DRIVER_INTERRUPT();
  735. }
  736. #endif // ADVANCE or LIN_ADVANCE
  737. void Stepper::init() {
  738. // Init Digipot Motor Current
  739. #if HAS_DIGIPOTSS || HAS_MOTOR_CURRENT_PWM
  740. digipot_init();
  741. #endif
  742. // Init Microstepping Pins
  743. #if HAS_MICROSTEPS
  744. microstep_init();
  745. #endif
  746. // Init TMC Steppers
  747. #if ENABLED(HAVE_TMCDRIVER)
  748. tmc_init();
  749. #endif
  750. // Init TMC2130 Steppers
  751. #if ENABLED(HAVE_TMC2130DRIVER)
  752. tmc2130_init();
  753. #endif
  754. // Init L6470 Steppers
  755. #if ENABLED(HAVE_L6470DRIVER)
  756. L6470_init();
  757. #endif
  758. // Init Dir Pins
  759. #if HAS_X_DIR
  760. X_DIR_INIT;
  761. #endif
  762. #if HAS_X2_DIR
  763. X2_DIR_INIT;
  764. #endif
  765. #if HAS_Y_DIR
  766. Y_DIR_INIT;
  767. #if ENABLED(Y_DUAL_STEPPER_DRIVERS) && HAS_Y2_DIR
  768. Y2_DIR_INIT;
  769. #endif
  770. #endif
  771. #if HAS_Z_DIR
  772. Z_DIR_INIT;
  773. #if ENABLED(Z_DUAL_STEPPER_DRIVERS) && HAS_Z2_DIR
  774. Z2_DIR_INIT;
  775. #endif
  776. #endif
  777. #if HAS_E0_DIR
  778. E0_DIR_INIT;
  779. #endif
  780. #if HAS_E1_DIR
  781. E1_DIR_INIT;
  782. #endif
  783. #if HAS_E2_DIR
  784. E2_DIR_INIT;
  785. #endif
  786. #if HAS_E3_DIR
  787. E3_DIR_INIT;
  788. #endif
  789. // Init Enable Pins - steppers default to disabled.
  790. #if HAS_X_ENABLE
  791. X_ENABLE_INIT;
  792. if (!X_ENABLE_ON) X_ENABLE_WRITE(HIGH);
  793. #if ENABLED(DUAL_X_CARRIAGE) && HAS_X2_ENABLE
  794. X2_ENABLE_INIT;
  795. if (!X_ENABLE_ON) X2_ENABLE_WRITE(HIGH);
  796. #endif
  797. #endif
  798. #if HAS_Y_ENABLE
  799. Y_ENABLE_INIT;
  800. if (!Y_ENABLE_ON) Y_ENABLE_WRITE(HIGH);
  801. #if ENABLED(Y_DUAL_STEPPER_DRIVERS) && HAS_Y2_ENABLE
  802. Y2_ENABLE_INIT;
  803. if (!Y_ENABLE_ON) Y2_ENABLE_WRITE(HIGH);
  804. #endif
  805. #endif
  806. #if HAS_Z_ENABLE
  807. Z_ENABLE_INIT;
  808. if (!Z_ENABLE_ON) Z_ENABLE_WRITE(HIGH);
  809. #if ENABLED(Z_DUAL_STEPPER_DRIVERS) && HAS_Z2_ENABLE
  810. Z2_ENABLE_INIT;
  811. if (!Z_ENABLE_ON) Z2_ENABLE_WRITE(HIGH);
  812. #endif
  813. #endif
  814. #if HAS_E0_ENABLE
  815. E0_ENABLE_INIT;
  816. if (!E_ENABLE_ON) E0_ENABLE_WRITE(HIGH);
  817. #endif
  818. #if HAS_E1_ENABLE
  819. E1_ENABLE_INIT;
  820. if (!E_ENABLE_ON) E1_ENABLE_WRITE(HIGH);
  821. #endif
  822. #if HAS_E2_ENABLE
  823. E2_ENABLE_INIT;
  824. if (!E_ENABLE_ON) E2_ENABLE_WRITE(HIGH);
  825. #endif
  826. #if HAS_E3_ENABLE
  827. E3_ENABLE_INIT;
  828. if (!E_ENABLE_ON) E3_ENABLE_WRITE(HIGH);
  829. #endif
  830. // Init endstops and pullups
  831. endstops.init();
  832. #define _STEP_INIT(AXIS) AXIS ##_STEP_INIT
  833. #define _WRITE_STEP(AXIS, HIGHLOW) AXIS ##_STEP_WRITE(HIGHLOW)
  834. #define _DISABLE(axis) disable_## axis()
  835. #define AXIS_INIT(axis, AXIS, PIN) \
  836. _STEP_INIT(AXIS); \
  837. _WRITE_STEP(AXIS, _INVERT_STEP_PIN(PIN)); \
  838. _DISABLE(axis)
  839. #define E_AXIS_INIT(NUM) AXIS_INIT(e## NUM, E## NUM, E)
  840. // Init Step Pins
  841. #if HAS_X_STEP
  842. #if ENABLED(X_DUAL_STEPPER_DRIVERS) || ENABLED(DUAL_X_CARRIAGE)
  843. X2_STEP_INIT;
  844. X2_STEP_WRITE(INVERT_X_STEP_PIN);
  845. #endif
  846. AXIS_INIT(x, X, X);
  847. #endif
  848. #if HAS_Y_STEP
  849. #if ENABLED(Y_DUAL_STEPPER_DRIVERS)
  850. Y2_STEP_INIT;
  851. Y2_STEP_WRITE(INVERT_Y_STEP_PIN);
  852. #endif
  853. AXIS_INIT(y, Y, Y);
  854. #endif
  855. #if HAS_Z_STEP
  856. #if ENABLED(Z_DUAL_STEPPER_DRIVERS)
  857. Z2_STEP_INIT;
  858. Z2_STEP_WRITE(INVERT_Z_STEP_PIN);
  859. #endif
  860. AXIS_INIT(z, Z, Z);
  861. #endif
  862. #if HAS_E0_STEP
  863. E_AXIS_INIT(0);
  864. #endif
  865. #if HAS_E1_STEP
  866. E_AXIS_INIT(1);
  867. #endif
  868. #if HAS_E2_STEP
  869. E_AXIS_INIT(2);
  870. #endif
  871. #if HAS_E3_STEP
  872. E_AXIS_INIT(3);
  873. #endif
  874. // waveform generation = 0100 = CTC
  875. CBI(TCCR1B, WGM13);
  876. SBI(TCCR1B, WGM12);
  877. CBI(TCCR1A, WGM11);
  878. CBI(TCCR1A, WGM10);
  879. // output mode = 00 (disconnected)
  880. TCCR1A &= ~(3 << COM1A0);
  881. TCCR1A &= ~(3 << COM1B0);
  882. // Set the timer pre-scaler
  883. // Generally we use a divider of 8, resulting in a 2MHz timer
  884. // frequency on a 16MHz MCU. If you are going to change this, be
  885. // sure to regenerate speed_lookuptable.h with
  886. // create_speed_lookuptable.py
  887. TCCR1B = (TCCR1B & ~(0x07 << CS10)) | (2 << CS10);
  888. // Init Stepper ISR to 122 Hz for quick starting
  889. OCR1A = 0x4000;
  890. TCNT1 = 0;
  891. ENABLE_STEPPER_DRIVER_INTERRUPT();
  892. #if ENABLED(ADVANCE) || ENABLED(LIN_ADVANCE)
  893. for (int i = 0; i < E_STEPPERS; i++) {
  894. e_steps[i] = 0;
  895. #if ENABLED(LIN_ADVANCE)
  896. current_adv_steps[i] = 0;
  897. #endif
  898. }
  899. #endif // ADVANCE or LIN_ADVANCE
  900. endstops.enable(true); // Start with endstops active. After homing they can be disabled
  901. sei();
  902. set_directions(); // Init directions to last_direction_bits = 0
  903. }
  904. /**
  905. * Block until all buffered steps are executed
  906. */
  907. void Stepper::synchronize() { while (planner.blocks_queued()) idle(); }
  908. /**
  909. * Set the stepper positions directly in steps
  910. *
  911. * The input is based on the typical per-axis XYZ steps.
  912. * For CORE machines XYZ needs to be translated to ABC.
  913. *
  914. * This allows get_axis_position_mm to correctly
  915. * derive the current XYZ position later on.
  916. */
  917. void Stepper::set_position(const long &a, const long &b, const long &c, const long &e) {
  918. synchronize(); // Bad to set stepper counts in the middle of a move
  919. CRITICAL_SECTION_START;
  920. #if CORE_IS_XY
  921. // corexy positioning
  922. // these equations follow the form of the dA and dB equations on http://www.corexy.com/theory.html
  923. count_position[A_AXIS] = a + b;
  924. count_position[B_AXIS] = CORESIGN(a - b);
  925. count_position[Z_AXIS] = c;
  926. #elif CORE_IS_XZ
  927. // corexz planning
  928. count_position[A_AXIS] = a + c;
  929. count_position[Y_AXIS] = b;
  930. count_position[C_AXIS] = CORESIGN(a - c);
  931. #elif CORE_IS_YZ
  932. // coreyz planning
  933. count_position[X_AXIS] = a;
  934. count_position[B_AXIS] = b + c;
  935. count_position[C_AXIS] = CORESIGN(b - c);
  936. #else
  937. // default non-h-bot planning
  938. count_position[X_AXIS] = a;
  939. count_position[Y_AXIS] = b;
  940. count_position[Z_AXIS] = c;
  941. #endif
  942. count_position[E_AXIS] = e;
  943. CRITICAL_SECTION_END;
  944. }
  945. void Stepper::set_position(const AxisEnum &axis, const long &v) {
  946. CRITICAL_SECTION_START;
  947. count_position[axis] = v;
  948. CRITICAL_SECTION_END;
  949. }
  950. void Stepper::set_e_position(const long &e) {
  951. CRITICAL_SECTION_START;
  952. count_position[E_AXIS] = e;
  953. CRITICAL_SECTION_END;
  954. }
  955. /**
  956. * Get a stepper's position in steps.
  957. */
  958. long Stepper::position(AxisEnum axis) {
  959. CRITICAL_SECTION_START;
  960. long count_pos = count_position[axis];
  961. CRITICAL_SECTION_END;
  962. return count_pos;
  963. }
  964. /**
  965. * Get an axis position according to stepper position(s)
  966. * For CORE machines apply translation from ABC to XYZ.
  967. */
  968. float Stepper::get_axis_position_mm(AxisEnum axis) {
  969. float axis_steps;
  970. #if IS_CORE
  971. // Requesting one of the "core" axes?
  972. if (axis == CORE_AXIS_1 || axis == CORE_AXIS_2) {
  973. CRITICAL_SECTION_START;
  974. // ((a1+a2)+(a1-a2))/2 -> (a1+a2+a1-a2)/2 -> (a1+a1)/2 -> a1
  975. // ((a1+a2)-(a1-a2))/2 -> (a1+a2-a1+a2)/2 -> (a2+a2)/2 -> a2
  976. axis_steps = 0.5f * (
  977. axis == CORE_AXIS_2 ? CORESIGN(count_position[CORE_AXIS_1] - count_position[CORE_AXIS_2])
  978. : count_position[CORE_AXIS_1] + count_position[CORE_AXIS_2]
  979. );
  980. CRITICAL_SECTION_END;
  981. }
  982. else
  983. axis_steps = position(axis);
  984. #else
  985. axis_steps = position(axis);
  986. #endif
  987. return axis_steps * planner.steps_to_mm[axis];
  988. }
  989. void Stepper::finish_and_disable() {
  990. synchronize();
  991. disable_all_steppers();
  992. }
  993. void Stepper::quick_stop() {
  994. cleaning_buffer_counter = 5000;
  995. DISABLE_STEPPER_DRIVER_INTERRUPT();
  996. while (planner.blocks_queued()) planner.discard_current_block();
  997. current_block = NULL;
  998. ENABLE_STEPPER_DRIVER_INTERRUPT();
  999. #if ENABLED(ULTRA_LCD)
  1000. planner.clear_block_buffer_runtime();
  1001. #endif
  1002. }
  1003. void Stepper::endstop_triggered(AxisEnum axis) {
  1004. #if IS_CORE
  1005. endstops_trigsteps[axis] = 0.5f * (
  1006. axis == CORE_AXIS_2 ? CORESIGN(count_position[CORE_AXIS_1] - count_position[CORE_AXIS_2])
  1007. : count_position[CORE_AXIS_1] + count_position[CORE_AXIS_2]
  1008. );
  1009. #else // !COREXY && !COREXZ && !COREYZ
  1010. endstops_trigsteps[axis] = count_position[axis];
  1011. #endif // !COREXY && !COREXZ && !COREYZ
  1012. kill_current_block();
  1013. }
  1014. void Stepper::report_positions() {
  1015. CRITICAL_SECTION_START;
  1016. long xpos = count_position[X_AXIS],
  1017. ypos = count_position[Y_AXIS],
  1018. zpos = count_position[Z_AXIS];
  1019. CRITICAL_SECTION_END;
  1020. #if CORE_IS_XY || CORE_IS_XZ || IS_SCARA
  1021. SERIAL_PROTOCOLPGM(MSG_COUNT_A);
  1022. #else
  1023. SERIAL_PROTOCOLPGM(MSG_COUNT_X);
  1024. #endif
  1025. SERIAL_PROTOCOL(xpos);
  1026. #if CORE_IS_XY || CORE_IS_YZ || IS_SCARA
  1027. SERIAL_PROTOCOLPGM(" B:");
  1028. #else
  1029. SERIAL_PROTOCOLPGM(" Y:");
  1030. #endif
  1031. SERIAL_PROTOCOL(ypos);
  1032. #if CORE_IS_XZ || CORE_IS_YZ
  1033. SERIAL_PROTOCOLPGM(" C:");
  1034. #else
  1035. SERIAL_PROTOCOLPGM(" Z:");
  1036. #endif
  1037. SERIAL_PROTOCOL(zpos);
  1038. SERIAL_EOL;
  1039. }
  1040. #if ENABLED(BABYSTEPPING)
  1041. #define _ENABLE(axis) enable_## axis()
  1042. #define _READ_DIR(AXIS) AXIS ##_DIR_READ
  1043. #define _INVERT_DIR(AXIS) INVERT_## AXIS ##_DIR
  1044. #define _APPLY_DIR(AXIS, INVERT) AXIS ##_APPLY_DIR(INVERT, true)
  1045. #define BABYSTEP_AXIS(axis, AXIS, INVERT) { \
  1046. _ENABLE(axis); \
  1047. uint8_t old_pin = _READ_DIR(AXIS); \
  1048. _APPLY_DIR(AXIS, _INVERT_DIR(AXIS)^direction^INVERT); \
  1049. _APPLY_STEP(AXIS)(!_INVERT_STEP_PIN(AXIS), true); \
  1050. delayMicroseconds(2); \
  1051. _APPLY_STEP(AXIS)(_INVERT_STEP_PIN(AXIS), true); \
  1052. _APPLY_DIR(AXIS, old_pin); \
  1053. }
  1054. // MUST ONLY BE CALLED BY AN ISR,
  1055. // No other ISR should ever interrupt this!
  1056. void Stepper::babystep(const AxisEnum axis, const bool direction) {
  1057. switch (axis) {
  1058. case X_AXIS:
  1059. BABYSTEP_AXIS(x, X, false);
  1060. break;
  1061. case Y_AXIS:
  1062. BABYSTEP_AXIS(y, Y, false);
  1063. break;
  1064. case Z_AXIS: {
  1065. #if DISABLED(DELTA)
  1066. BABYSTEP_AXIS(z, Z, BABYSTEP_INVERT_Z);
  1067. #else // DELTA
  1068. bool z_direction = direction ^ BABYSTEP_INVERT_Z;
  1069. enable_x();
  1070. enable_y();
  1071. enable_z();
  1072. uint8_t old_x_dir_pin = X_DIR_READ,
  1073. old_y_dir_pin = Y_DIR_READ,
  1074. old_z_dir_pin = Z_DIR_READ;
  1075. //setup new step
  1076. X_DIR_WRITE(INVERT_X_DIR ^ z_direction);
  1077. Y_DIR_WRITE(INVERT_Y_DIR ^ z_direction);
  1078. Z_DIR_WRITE(INVERT_Z_DIR ^ z_direction);
  1079. //perform step
  1080. X_STEP_WRITE(!INVERT_X_STEP_PIN);
  1081. Y_STEP_WRITE(!INVERT_Y_STEP_PIN);
  1082. Z_STEP_WRITE(!INVERT_Z_STEP_PIN);
  1083. delayMicroseconds(2);
  1084. X_STEP_WRITE(INVERT_X_STEP_PIN);
  1085. Y_STEP_WRITE(INVERT_Y_STEP_PIN);
  1086. Z_STEP_WRITE(INVERT_Z_STEP_PIN);
  1087. //get old pin state back.
  1088. X_DIR_WRITE(old_x_dir_pin);
  1089. Y_DIR_WRITE(old_y_dir_pin);
  1090. Z_DIR_WRITE(old_z_dir_pin);
  1091. #endif
  1092. } break;
  1093. default: break;
  1094. }
  1095. }
  1096. #endif //BABYSTEPPING
  1097. /**
  1098. * Software-controlled Stepper Motor Current
  1099. */
  1100. #if HAS_DIGIPOTSS
  1101. // From Arduino DigitalPotControl example
  1102. void Stepper::digitalPotWrite(int address, int value) {
  1103. WRITE(DIGIPOTSS_PIN, LOW); // take the SS pin low to select the chip
  1104. SPI.transfer(address); // send in the address and value via SPI:
  1105. SPI.transfer(value);
  1106. WRITE(DIGIPOTSS_PIN, HIGH); // take the SS pin high to de-select the chip:
  1107. //delay(10);
  1108. }
  1109. #endif //HAS_DIGIPOTSS
  1110. #if HAS_DIGIPOTSS || HAS_MOTOR_CURRENT_PWM
  1111. void Stepper::digipot_init() {
  1112. #if HAS_DIGIPOTSS
  1113. static const uint8_t digipot_motor_current[] = DIGIPOT_MOTOR_CURRENT;
  1114. SPI.begin();
  1115. SET_OUTPUT(DIGIPOTSS_PIN);
  1116. for (uint8_t i = 0; i < COUNT(digipot_motor_current); i++) {
  1117. //digitalPotWrite(digipot_ch[i], digipot_motor_current[i]);
  1118. digipot_current(i, digipot_motor_current[i]);
  1119. }
  1120. #elif HAS_MOTOR_CURRENT_PWM
  1121. #if PIN_EXISTS(MOTOR_CURRENT_PWM_XY)
  1122. SET_OUTPUT(MOTOR_CURRENT_PWM_XY_PIN);
  1123. digipot_current(0, motor_current_setting[0]);
  1124. #endif
  1125. #if PIN_EXISTS(MOTOR_CURRENT_PWM_Z)
  1126. SET_OUTPUT(MOTOR_CURRENT_PWM_Z_PIN);
  1127. digipot_current(1, motor_current_setting[1]);
  1128. #endif
  1129. #if PIN_EXISTS(MOTOR_CURRENT_PWM_E)
  1130. SET_OUTPUT(MOTOR_CURRENT_PWM_E_PIN);
  1131. digipot_current(2, motor_current_setting[2]);
  1132. #endif
  1133. //Set timer5 to 31khz so the PWM of the motor power is as constant as possible. (removes a buzzing noise)
  1134. TCCR5B = (TCCR5B & ~(_BV(CS50) | _BV(CS51) | _BV(CS52))) | _BV(CS50);
  1135. #endif
  1136. }
  1137. void Stepper::digipot_current(uint8_t driver, int current) {
  1138. #if HAS_DIGIPOTSS
  1139. const uint8_t digipot_ch[] = DIGIPOT_CHANNELS;
  1140. digitalPotWrite(digipot_ch[driver], current);
  1141. #elif HAS_MOTOR_CURRENT_PWM
  1142. #define _WRITE_CURRENT_PWM(P) analogWrite(P, 255L * current / (MOTOR_CURRENT_PWM_RANGE))
  1143. switch (driver) {
  1144. #if PIN_EXISTS(MOTOR_CURRENT_PWM_XY)
  1145. case 0: _WRITE_CURRENT_PWM(MOTOR_CURRENT_PWM_XY_PIN); break;
  1146. #endif
  1147. #if PIN_EXISTS(MOTOR_CURRENT_PWM_Z)
  1148. case 1: _WRITE_CURRENT_PWM(MOTOR_CURRENT_PWM_Z_PIN); break;
  1149. #endif
  1150. #if PIN_EXISTS(MOTOR_CURRENT_PWM_E)
  1151. case 2: _WRITE_CURRENT_PWM(MOTOR_CURRENT_PWM_E_PIN); break;
  1152. #endif
  1153. }
  1154. #endif
  1155. }
  1156. #endif
  1157. #if HAS_MICROSTEPS
  1158. /**
  1159. * Software-controlled Microstepping
  1160. */
  1161. void Stepper::microstep_init() {
  1162. SET_OUTPUT(X_MS1_PIN);
  1163. SET_OUTPUT(X_MS2_PIN);
  1164. #if HAS_MICROSTEPS_Y
  1165. SET_OUTPUT(Y_MS1_PIN);
  1166. SET_OUTPUT(Y_MS2_PIN);
  1167. #endif
  1168. #if HAS_MICROSTEPS_Z
  1169. SET_OUTPUT(Z_MS1_PIN);
  1170. SET_OUTPUT(Z_MS2_PIN);
  1171. #endif
  1172. #if HAS_MICROSTEPS_E0
  1173. SET_OUTPUT(E0_MS1_PIN);
  1174. SET_OUTPUT(E0_MS2_PIN);
  1175. #endif
  1176. #if HAS_MICROSTEPS_E1
  1177. SET_OUTPUT(E1_MS1_PIN);
  1178. SET_OUTPUT(E1_MS2_PIN);
  1179. #endif
  1180. static const uint8_t microstep_modes[] = MICROSTEP_MODES;
  1181. for (uint16_t i = 0; i < COUNT(microstep_modes); i++)
  1182. microstep_mode(i, microstep_modes[i]);
  1183. }
  1184. void Stepper::microstep_ms(uint8_t driver, int8_t ms1, int8_t ms2) {
  1185. if (ms1 >= 0) switch (driver) {
  1186. case 0: digitalWrite(X_MS1_PIN, ms1); break;
  1187. #if HAS_MICROSTEPS_Y
  1188. case 1: digitalWrite(Y_MS1_PIN, ms1); break;
  1189. #endif
  1190. #if HAS_MICROSTEPS_Z
  1191. case 2: digitalWrite(Z_MS1_PIN, ms1); break;
  1192. #endif
  1193. #if HAS_MICROSTEPS_E0
  1194. case 3: digitalWrite(E0_MS1_PIN, ms1); break;
  1195. #endif
  1196. #if HAS_MICROSTEPS_E1
  1197. case 4: digitalWrite(E1_MS1_PIN, ms1); break;
  1198. #endif
  1199. }
  1200. if (ms2 >= 0) switch (driver) {
  1201. case 0: digitalWrite(X_MS2_PIN, ms2); break;
  1202. #if HAS_MICROSTEPS_Y
  1203. case 1: digitalWrite(Y_MS2_PIN, ms2); break;
  1204. #endif
  1205. #if HAS_MICROSTEPS_Z
  1206. case 2: digitalWrite(Z_MS2_PIN, ms2); break;
  1207. #endif
  1208. #if HAS_MICROSTEPS_E0
  1209. case 3: digitalWrite(E0_MS2_PIN, ms2); break;
  1210. #endif
  1211. #if HAS_MICROSTEPS_E1
  1212. case 4: digitalWrite(E1_MS2_PIN, ms2); break;
  1213. #endif
  1214. }
  1215. }
  1216. void Stepper::microstep_mode(uint8_t driver, uint8_t stepping_mode) {
  1217. switch (stepping_mode) {
  1218. case 1: microstep_ms(driver, MICROSTEP1); break;
  1219. case 2: microstep_ms(driver, MICROSTEP2); break;
  1220. case 4: microstep_ms(driver, MICROSTEP4); break;
  1221. case 8: microstep_ms(driver, MICROSTEP8); break;
  1222. case 16: microstep_ms(driver, MICROSTEP16); break;
  1223. }
  1224. }
  1225. void Stepper::microstep_readings() {
  1226. SERIAL_PROTOCOLLNPGM("MS1,MS2 Pins");
  1227. SERIAL_PROTOCOLPGM("X: ");
  1228. SERIAL_PROTOCOL(READ(X_MS1_PIN));
  1229. SERIAL_PROTOCOLLN(READ(X_MS2_PIN));
  1230. #if HAS_MICROSTEPS_Y
  1231. SERIAL_PROTOCOLPGM("Y: ");
  1232. SERIAL_PROTOCOL(READ(Y_MS1_PIN));
  1233. SERIAL_PROTOCOLLN(READ(Y_MS2_PIN));
  1234. #endif
  1235. #if HAS_MICROSTEPS_Z
  1236. SERIAL_PROTOCOLPGM("Z: ");
  1237. SERIAL_PROTOCOL(READ(Z_MS1_PIN));
  1238. SERIAL_PROTOCOLLN(READ(Z_MS2_PIN));
  1239. #endif
  1240. #if HAS_MICROSTEPS_E0
  1241. SERIAL_PROTOCOLPGM("E0: ");
  1242. SERIAL_PROTOCOL(READ(E0_MS1_PIN));
  1243. SERIAL_PROTOCOLLN(READ(E0_MS2_PIN));
  1244. #endif
  1245. #if HAS_MICROSTEPS_E1
  1246. SERIAL_PROTOCOLPGM("E1: ");
  1247. SERIAL_PROTOCOL(READ(E1_MS1_PIN));
  1248. SERIAL_PROTOCOLLN(READ(E1_MS2_PIN));
  1249. #endif
  1250. }
  1251. #endif // HAS_MICROSTEPS