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

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070
  1. /*
  2. stepper.c - stepper motor driver: executes motion plans using stepper motors
  3. Part of Grbl
  4. Copyright (c) 2009-2011 Simen Svale Skogsrud
  5. Grbl is free software: you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation, either version 3 of the License, or
  8. (at your option) any later version.
  9. Grbl is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with Grbl. If not, see <http://www.gnu.org/licenses/>.
  15. */
  16. /* The timer calculations of this module informed by the 'RepRap cartesian firmware' by Zack Smith
  17. and Philipp Tiefenbacher. */
  18. #include "Marlin.h"
  19. #include "stepper.h"
  20. #include "planner.h"
  21. #include "temperature.h"
  22. #include "ultralcd.h"
  23. #include "language.h"
  24. #include "cardreader.h"
  25. #include "speed_lookuptable.h"
  26. #if DIGIPOTSS_PIN > -1
  27. #include <SPI.h>
  28. #endif
  29. //===========================================================================
  30. //=============================public variables ============================
  31. //===========================================================================
  32. block_t *current_block; // A pointer to the block currently being traced
  33. //===========================================================================
  34. //=============================private variables ============================
  35. //===========================================================================
  36. //static makes it inpossible to be called from outside of this file by extern.!
  37. // Variables used by The Stepper Driver Interrupt
  38. static unsigned char out_bits; // The next stepping-bits to be output
  39. static long counter_x, // Counter variables for the bresenham line tracer
  40. counter_y,
  41. counter_z,
  42. counter_e;
  43. volatile static unsigned long step_events_completed; // The number of step events executed in the current block
  44. #ifdef ADVANCE
  45. static long advance_rate, advance, final_advance = 0;
  46. static long old_advance = 0;
  47. static long e_steps[3];
  48. #endif
  49. static long acceleration_time, deceleration_time;
  50. //static unsigned long accelerate_until, decelerate_after, acceleration_rate, initial_rate, final_rate, nominal_rate;
  51. static unsigned short acc_step_rate; // needed for deccelaration start point
  52. static char step_loops;
  53. static unsigned short OCR1A_nominal;
  54. volatile long endstops_trigsteps[3]={0,0,0};
  55. volatile long endstops_stepsTotal,endstops_stepsDone;
  56. static volatile bool endstop_x_hit=false;
  57. static volatile bool endstop_y_hit=false;
  58. static volatile bool endstop_z_hit=false;
  59. #ifdef ABORT_ON_ENDSTOP_HIT_FEATURE_ENABLED
  60. bool abort_on_endstop_hit = false;
  61. #endif
  62. static bool old_x_min_endstop=false;
  63. static bool old_x_max_endstop=false;
  64. static bool old_y_min_endstop=false;
  65. static bool old_y_max_endstop=false;
  66. static bool old_z_min_endstop=false;
  67. static bool old_z_max_endstop=false;
  68. static bool check_endstops = true;
  69. volatile long count_position[NUM_AXIS] = { 0, 0, 0, 0};
  70. volatile signed char count_direction[NUM_AXIS] = { 1, 1, 1, 1};
  71. //===========================================================================
  72. //=============================functions ============================
  73. //===========================================================================
  74. #define CHECK_ENDSTOPS if(check_endstops)
  75. // intRes = intIn1 * intIn2 >> 16
  76. // uses:
  77. // r26 to store 0
  78. // r27 to store the byte 1 of the 24 bit result
  79. #define MultiU16X8toH16(intRes, charIn1, intIn2) \
  80. asm volatile ( \
  81. "clr r26 \n\t" \
  82. "mul %A1, %B2 \n\t" \
  83. "movw %A0, r0 \n\t" \
  84. "mul %A1, %A2 \n\t" \
  85. "add %A0, r1 \n\t" \
  86. "adc %B0, r26 \n\t" \
  87. "lsr r0 \n\t" \
  88. "adc %A0, r26 \n\t" \
  89. "adc %B0, r26 \n\t" \
  90. "clr r1 \n\t" \
  91. : \
  92. "=&r" (intRes) \
  93. : \
  94. "d" (charIn1), \
  95. "d" (intIn2) \
  96. : \
  97. "r26" \
  98. )
  99. // intRes = longIn1 * longIn2 >> 24
  100. // uses:
  101. // r26 to store 0
  102. // r27 to store the byte 1 of the 48bit result
  103. #define MultiU24X24toH16(intRes, longIn1, longIn2) \
  104. asm volatile ( \
  105. "clr r26 \n\t" \
  106. "mul %A1, %B2 \n\t" \
  107. "mov r27, r1 \n\t" \
  108. "mul %B1, %C2 \n\t" \
  109. "movw %A0, r0 \n\t" \
  110. "mul %C1, %C2 \n\t" \
  111. "add %B0, r0 \n\t" \
  112. "mul %C1, %B2 \n\t" \
  113. "add %A0, r0 \n\t" \
  114. "adc %B0, r1 \n\t" \
  115. "mul %A1, %C2 \n\t" \
  116. "add r27, r0 \n\t" \
  117. "adc %A0, r1 \n\t" \
  118. "adc %B0, r26 \n\t" \
  119. "mul %B1, %B2 \n\t" \
  120. "add r27, r0 \n\t" \
  121. "adc %A0, r1 \n\t" \
  122. "adc %B0, r26 \n\t" \
  123. "mul %C1, %A2 \n\t" \
  124. "add r27, r0 \n\t" \
  125. "adc %A0, r1 \n\t" \
  126. "adc %B0, r26 \n\t" \
  127. "mul %B1, %A2 \n\t" \
  128. "add r27, r1 \n\t" \
  129. "adc %A0, r26 \n\t" \
  130. "adc %B0, r26 \n\t" \
  131. "lsr r27 \n\t" \
  132. "adc %A0, r26 \n\t" \
  133. "adc %B0, r26 \n\t" \
  134. "clr r1 \n\t" \
  135. : \
  136. "=&r" (intRes) \
  137. : \
  138. "d" (longIn1), \
  139. "d" (longIn2) \
  140. : \
  141. "r26" , "r27" \
  142. )
  143. // Some useful constants
  144. #define ENABLE_STEPPER_DRIVER_INTERRUPT() TIMSK1 |= (1<<OCIE1A)
  145. #define DISABLE_STEPPER_DRIVER_INTERRUPT() TIMSK1 &= ~(1<<OCIE1A)
  146. void checkHitEndstops()
  147. {
  148. if( endstop_x_hit || endstop_y_hit || endstop_z_hit) {
  149. SERIAL_ECHO_START;
  150. SERIAL_ECHOPGM(MSG_ENDSTOPS_HIT);
  151. if(endstop_x_hit) {
  152. SERIAL_ECHOPAIR(" X:",(float)endstops_trigsteps[X_AXIS]/axis_steps_per_unit[X_AXIS]);
  153. LCD_MESSAGEPGM(MSG_ENDSTOPS_HIT "X");
  154. }
  155. if(endstop_y_hit) {
  156. SERIAL_ECHOPAIR(" Y:",(float)endstops_trigsteps[Y_AXIS]/axis_steps_per_unit[Y_AXIS]);
  157. LCD_MESSAGEPGM(MSG_ENDSTOPS_HIT "Y");
  158. }
  159. if(endstop_z_hit) {
  160. SERIAL_ECHOPAIR(" Z:",(float)endstops_trigsteps[Z_AXIS]/axis_steps_per_unit[Z_AXIS]);
  161. LCD_MESSAGEPGM(MSG_ENDSTOPS_HIT "Z");
  162. }
  163. SERIAL_ECHOLN("");
  164. endstop_x_hit=false;
  165. endstop_y_hit=false;
  166. endstop_z_hit=false;
  167. #ifdef ABORT_ON_ENDSTOP_HIT_FEATURE_ENABLED
  168. if (abort_on_endstop_hit)
  169. {
  170. card.sdprinting = false;
  171. card.closefile();
  172. quickStop();
  173. setTargetHotend0(0);
  174. setTargetHotend1(0);
  175. setTargetHotend2(0);
  176. }
  177. #endif
  178. }
  179. }
  180. void endstops_hit_on_purpose()
  181. {
  182. endstop_x_hit=false;
  183. endstop_y_hit=false;
  184. endstop_z_hit=false;
  185. }
  186. void enable_endstops(bool check)
  187. {
  188. check_endstops = check;
  189. }
  190. // __________________________
  191. // /| |\ _________________ ^
  192. // / | | \ /| |\ |
  193. // / | | \ / | | \ s
  194. // / | | | | | \ p
  195. // / | | | | | \ e
  196. // +-----+------------------------+---+--+---------------+----+ e
  197. // | BLOCK 1 | BLOCK 2 | d
  198. //
  199. // time ----->
  200. //
  201. // The trapezoid is the shape the speed curve over time. It starts at block->initial_rate, accelerates
  202. // first block->accelerate_until step_events_completed, then keeps going at constant speed until
  203. // step_events_completed reaches block->decelerate_after after which it decelerates until the trapezoid generator is reset.
  204. // The slope of acceleration is calculated with the leib ramp alghorithm.
  205. void st_wake_up() {
  206. // TCNT1 = 0;
  207. ENABLE_STEPPER_DRIVER_INTERRUPT();
  208. }
  209. void step_wait(){
  210. for(int8_t i=0; i < 6; i++){
  211. }
  212. }
  213. FORCE_INLINE unsigned short calc_timer(unsigned short step_rate) {
  214. unsigned short timer;
  215. if(step_rate > MAX_STEP_FREQUENCY) step_rate = MAX_STEP_FREQUENCY;
  216. if(step_rate > 20000) { // If steprate > 20kHz >> step 4 times
  217. step_rate = (step_rate >> 2)&0x3fff;
  218. step_loops = 4;
  219. }
  220. else if(step_rate > 10000) { // If steprate > 10kHz >> step 2 times
  221. step_rate = (step_rate >> 1)&0x7fff;
  222. step_loops = 2;
  223. }
  224. else {
  225. step_loops = 1;
  226. }
  227. if(step_rate < (F_CPU/500000)) step_rate = (F_CPU/500000);
  228. step_rate -= (F_CPU/500000); // Correct for minimal speed
  229. if(step_rate >= (8*256)){ // higher step rate
  230. unsigned short table_address = (unsigned short)&speed_lookuptable_fast[(unsigned char)(step_rate>>8)][0];
  231. unsigned char tmp_step_rate = (step_rate & 0x00ff);
  232. unsigned short gain = (unsigned short)pgm_read_word_near(table_address+2);
  233. MultiU16X8toH16(timer, tmp_step_rate, gain);
  234. timer = (unsigned short)pgm_read_word_near(table_address) - timer;
  235. }
  236. else { // lower step rates
  237. unsigned short table_address = (unsigned short)&speed_lookuptable_slow[0][0];
  238. table_address += ((step_rate)>>1) & 0xfffc;
  239. timer = (unsigned short)pgm_read_word_near(table_address);
  240. timer -= (((unsigned short)pgm_read_word_near(table_address+2) * (unsigned char)(step_rate & 0x0007))>>3);
  241. }
  242. if(timer < 100) { timer = 100; MYSERIAL.print(MSG_STEPPER_TO_HIGH); MYSERIAL.println(step_rate); }//(20kHz this should never happen)
  243. return timer;
  244. }
  245. // Initializes the trapezoid generator from the current block. Called whenever a new
  246. // block begins.
  247. FORCE_INLINE void trapezoid_generator_reset() {
  248. #ifdef ADVANCE
  249. advance = current_block->initial_advance;
  250. final_advance = current_block->final_advance;
  251. // Do E steps + advance steps
  252. e_steps[current_block->active_extruder] += ((advance >>8) - old_advance);
  253. old_advance = advance >>8;
  254. #endif
  255. deceleration_time = 0;
  256. // step_rate to timer interval
  257. OCR1A_nominal = calc_timer(current_block->nominal_rate);
  258. acc_step_rate = current_block->initial_rate;
  259. acceleration_time = calc_timer(acc_step_rate);
  260. OCR1A = acceleration_time;
  261. // SERIAL_ECHO_START;
  262. // SERIAL_ECHOPGM("advance :");
  263. // SERIAL_ECHO(current_block->advance/256.0);
  264. // SERIAL_ECHOPGM("advance rate :");
  265. // SERIAL_ECHO(current_block->advance_rate/256.0);
  266. // SERIAL_ECHOPGM("initial advance :");
  267. // SERIAL_ECHO(current_block->initial_advance/256.0);
  268. // SERIAL_ECHOPGM("final advance :");
  269. // SERIAL_ECHOLN(current_block->final_advance/256.0);
  270. }
  271. // "The Stepper Driver Interrupt" - This timer interrupt is the workhorse.
  272. // It pops blocks from the block_buffer and executes them by pulsing the stepper pins appropriately.
  273. ISR(TIMER1_COMPA_vect)
  274. {
  275. // If there is no current block, attempt to pop one from the buffer
  276. if (current_block == NULL) {
  277. // Anything in the buffer?
  278. current_block = plan_get_current_block();
  279. if (current_block != NULL) {
  280. current_block->busy = true;
  281. trapezoid_generator_reset();
  282. counter_x = -(current_block->step_event_count >> 1);
  283. counter_y = counter_x;
  284. counter_z = counter_x;
  285. counter_e = counter_x;
  286. step_events_completed = 0;
  287. #ifdef Z_LATE_ENABLE
  288. if(current_block->steps_z > 0) {
  289. enable_z();
  290. OCR1A = 2000; //1ms wait
  291. return;
  292. }
  293. #endif
  294. // #ifdef ADVANCE
  295. // e_steps[current_block->active_extruder] = 0;
  296. // #endif
  297. }
  298. else {
  299. OCR1A=2000; // 1kHz.
  300. }
  301. }
  302. if (current_block != NULL) {
  303. // Set directions TO DO This should be done once during init of trapezoid. Endstops -> interrupt
  304. out_bits = current_block->direction_bits;
  305. // Set direction en check limit switches
  306. if ((out_bits & (1<<X_AXIS)) != 0) { // stepping along -X axis
  307. #if !defined COREXY //NOT COREXY
  308. WRITE(X_DIR_PIN, INVERT_X_DIR);
  309. #endif
  310. count_direction[X_AXIS]=-1;
  311. CHECK_ENDSTOPS
  312. {
  313. #if X_MIN_PIN > -1
  314. bool x_min_endstop=(READ(X_MIN_PIN) != X_ENDSTOPS_INVERTING);
  315. if(x_min_endstop && old_x_min_endstop && (current_block->steps_x > 0)) {
  316. endstops_trigsteps[X_AXIS] = count_position[X_AXIS];
  317. endstop_x_hit=true;
  318. step_events_completed = current_block->step_event_count;
  319. }
  320. old_x_min_endstop = x_min_endstop;
  321. #endif
  322. }
  323. }
  324. else { // +direction
  325. #if !defined COREXY //NOT COREXY
  326. WRITE(X_DIR_PIN,!INVERT_X_DIR);
  327. #endif
  328. count_direction[X_AXIS]=1;
  329. CHECK_ENDSTOPS
  330. {
  331. #if X_MAX_PIN > -1
  332. bool x_max_endstop=(READ(X_MAX_PIN) != X_ENDSTOPS_INVERTING);
  333. if(x_max_endstop && old_x_max_endstop && (current_block->steps_x > 0)){
  334. endstops_trigsteps[X_AXIS] = count_position[X_AXIS];
  335. endstop_x_hit=true;
  336. step_events_completed = current_block->step_event_count;
  337. }
  338. old_x_max_endstop = x_max_endstop;
  339. #endif
  340. }
  341. }
  342. if ((out_bits & (1<<Y_AXIS)) != 0) { // -direction
  343. #if !defined COREXY //NOT COREXY
  344. WRITE(Y_DIR_PIN,INVERT_Y_DIR);
  345. #endif
  346. count_direction[Y_AXIS]=-1;
  347. CHECK_ENDSTOPS
  348. {
  349. #if Y_MIN_PIN > -1
  350. bool y_min_endstop=(READ(Y_MIN_PIN) != Y_ENDSTOPS_INVERTING);
  351. if(y_min_endstop && old_y_min_endstop && (current_block->steps_y > 0)) {
  352. endstops_trigsteps[Y_AXIS] = count_position[Y_AXIS];
  353. endstop_y_hit=true;
  354. step_events_completed = current_block->step_event_count;
  355. }
  356. old_y_min_endstop = y_min_endstop;
  357. #endif
  358. }
  359. }
  360. else { // +direction
  361. #if !defined COREXY //NOT COREXY
  362. WRITE(Y_DIR_PIN,!INVERT_Y_DIR);
  363. #endif
  364. count_direction[Y_AXIS]=1;
  365. CHECK_ENDSTOPS
  366. {
  367. #if Y_MAX_PIN > -1
  368. bool y_max_endstop=(READ(Y_MAX_PIN) != Y_ENDSTOPS_INVERTING);
  369. if(y_max_endstop && old_y_max_endstop && (current_block->steps_y > 0)){
  370. endstops_trigsteps[Y_AXIS] = count_position[Y_AXIS];
  371. endstop_y_hit=true;
  372. step_events_completed = current_block->step_event_count;
  373. }
  374. old_y_max_endstop = y_max_endstop;
  375. #endif
  376. }
  377. }
  378. #ifdef COREXY //coreXY kinematics defined
  379. if((current_block->steps_x >= current_block->steps_y)&&((out_bits & (1<<X_AXIS)) == 0)){ //+X is major axis
  380. WRITE(X_DIR_PIN, !INVERT_X_DIR);
  381. WRITE(Y_DIR_PIN, !INVERT_Y_DIR);
  382. }
  383. if((current_block->steps_x >= current_block->steps_y)&&((out_bits & (1<<X_AXIS)) != 0)){ //-X is major axis
  384. WRITE(X_DIR_PIN, INVERT_X_DIR);
  385. WRITE(Y_DIR_PIN, INVERT_Y_DIR);
  386. }
  387. if((current_block->steps_y > current_block->steps_x)&&((out_bits & (1<<Y_AXIS)) == 0)){ //+Y is major axis
  388. WRITE(X_DIR_PIN, !INVERT_X_DIR);
  389. WRITE(Y_DIR_PIN, INVERT_Y_DIR);
  390. }
  391. if((current_block->steps_y > current_block->steps_x)&&((out_bits & (1<<Y_AXIS)) != 0)){ //-Y is major axis
  392. WRITE(X_DIR_PIN, INVERT_X_DIR);
  393. WRITE(Y_DIR_PIN, !INVERT_Y_DIR);
  394. }
  395. #endif //coreXY
  396. if ((out_bits & (1<<Z_AXIS)) != 0) { // -direction
  397. WRITE(Z_DIR_PIN,INVERT_Z_DIR);
  398. #ifdef Z_DUAL_STEPPER_DRIVERS
  399. WRITE(Z2_DIR_PIN,INVERT_Z_DIR);
  400. #endif
  401. count_direction[Z_AXIS]=-1;
  402. CHECK_ENDSTOPS
  403. {
  404. #if Z_MIN_PIN > -1
  405. bool z_min_endstop=(READ(Z_MIN_PIN) != Z_ENDSTOPS_INVERTING);
  406. if(z_min_endstop && old_z_min_endstop && (current_block->steps_z > 0)) {
  407. endstops_trigsteps[Z_AXIS] = count_position[Z_AXIS];
  408. endstop_z_hit=true;
  409. step_events_completed = current_block->step_event_count;
  410. }
  411. old_z_min_endstop = z_min_endstop;
  412. #endif
  413. }
  414. }
  415. else { // +direction
  416. WRITE(Z_DIR_PIN,!INVERT_Z_DIR);
  417. #ifdef Z_DUAL_STEPPER_DRIVERS
  418. WRITE(Z2_DIR_PIN,!INVERT_Z_DIR);
  419. #endif
  420. count_direction[Z_AXIS]=1;
  421. CHECK_ENDSTOPS
  422. {
  423. #if Z_MAX_PIN > -1
  424. bool z_max_endstop=(READ(Z_MAX_PIN) != Z_ENDSTOPS_INVERTING);
  425. if(z_max_endstop && old_z_max_endstop && (current_block->steps_z > 0)) {
  426. endstops_trigsteps[Z_AXIS] = count_position[Z_AXIS];
  427. endstop_z_hit=true;
  428. step_events_completed = current_block->step_event_count;
  429. }
  430. old_z_max_endstop = z_max_endstop;
  431. #endif
  432. }
  433. }
  434. #ifndef ADVANCE
  435. if ((out_bits & (1<<E_AXIS)) != 0) { // -direction
  436. REV_E_DIR();
  437. count_direction[E_AXIS]=-1;
  438. }
  439. else { // +direction
  440. NORM_E_DIR();
  441. count_direction[E_AXIS]=1;
  442. }
  443. #endif //!ADVANCE
  444. for(int8_t i=0; i < step_loops; i++) { // Take multiple steps per interrupt (For high speed moves)
  445. #ifndef AT90USB
  446. MSerial.checkRx(); // Check for serial chars.
  447. #endif
  448. #ifdef ADVANCE
  449. counter_e += current_block->steps_e;
  450. if (counter_e > 0) {
  451. counter_e -= current_block->step_event_count;
  452. if ((out_bits & (1<<E_AXIS)) != 0) { // - direction
  453. e_steps[current_block->active_extruder]--;
  454. }
  455. else {
  456. e_steps[current_block->active_extruder]++;
  457. }
  458. }
  459. #endif //ADVANCE
  460. #if !defined COREXY
  461. counter_x += current_block->steps_x;
  462. if (counter_x > 0) {
  463. WRITE(X_STEP_PIN, !INVERT_X_STEP_PIN);
  464. counter_x -= current_block->step_event_count;
  465. count_position[X_AXIS]+=count_direction[X_AXIS];
  466. WRITE(X_STEP_PIN, INVERT_X_STEP_PIN);
  467. }
  468. counter_y += current_block->steps_y;
  469. if (counter_y > 0) {
  470. WRITE(Y_STEP_PIN, !INVERT_Y_STEP_PIN);
  471. counter_y -= current_block->step_event_count;
  472. count_position[Y_AXIS]+=count_direction[Y_AXIS];
  473. WRITE(Y_STEP_PIN, INVERT_Y_STEP_PIN);
  474. }
  475. #endif
  476. #ifdef COREXY
  477. counter_x += current_block->steps_x;
  478. counter_y += current_block->steps_y;
  479. if ((counter_x > 0)&&!(counter_y>0)){ //X step only
  480. WRITE(X_STEP_PIN, !INVERT_X_STEP_PIN);
  481. WRITE(Y_STEP_PIN, !INVERT_Y_STEP_PIN);
  482. counter_x -= current_block->step_event_count;
  483. count_position[X_AXIS]+=count_direction[X_AXIS];
  484. WRITE(X_STEP_PIN, INVERT_X_STEP_PIN);
  485. WRITE(Y_STEP_PIN, INVERT_Y_STEP_PIN);
  486. }
  487. if (!(counter_x > 0)&&(counter_y>0)){ //Y step only
  488. WRITE(X_STEP_PIN, !INVERT_X_STEP_PIN);
  489. WRITE(Y_STEP_PIN, !INVERT_Y_STEP_PIN);
  490. counter_y -= current_block->step_event_count;
  491. count_position[Y_AXIS]+=count_direction[Y_AXIS];
  492. WRITE(X_STEP_PIN, INVERT_X_STEP_PIN);
  493. WRITE(Y_STEP_PIN, INVERT_Y_STEP_PIN);
  494. }
  495. if ((counter_x > 0)&&(counter_y>0)){ //step in both axes
  496. if (((out_bits & (1<<X_AXIS)) == 0)^((out_bits & (1<<Y_AXIS)) == 0)){ //X and Y in different directions
  497. WRITE(Y_STEP_PIN, !INVERT_Y_STEP_PIN);
  498. counter_x -= current_block->step_event_count;
  499. WRITE(Y_STEP_PIN, INVERT_Y_STEP_PIN);
  500. step_wait();
  501. count_position[X_AXIS]+=count_direction[X_AXIS];
  502. count_position[Y_AXIS]+=count_direction[Y_AXIS];
  503. WRITE(Y_STEP_PIN, !INVERT_Y_STEP_PIN);
  504. counter_y -= current_block->step_event_count;
  505. WRITE(Y_STEP_PIN, INVERT_Y_STEP_PIN);
  506. }
  507. else{ //X and Y in same direction
  508. WRITE(X_STEP_PIN, !INVERT_X_STEP_PIN);
  509. counter_x -= current_block->step_event_count;
  510. WRITE(X_STEP_PIN, INVERT_X_STEP_PIN) ;
  511. step_wait();
  512. count_position[X_AXIS]+=count_direction[X_AXIS];
  513. count_position[Y_AXIS]+=count_direction[Y_AXIS];
  514. WRITE(X_STEP_PIN, !INVERT_X_STEP_PIN);
  515. counter_y -= current_block->step_event_count;
  516. WRITE(X_STEP_PIN, INVERT_X_STEP_PIN);
  517. }
  518. }
  519. #endif //corexy
  520. counter_z += current_block->steps_z;
  521. if (counter_z > 0) {
  522. WRITE(Z_STEP_PIN, !INVERT_Z_STEP_PIN);
  523. #ifdef Z_DUAL_STEPPER_DRIVERS
  524. WRITE(Z2_STEP_PIN, !INVERT_Z_STEP_PIN);
  525. #endif
  526. counter_z -= current_block->step_event_count;
  527. count_position[Z_AXIS]+=count_direction[Z_AXIS];
  528. WRITE(Z_STEP_PIN, INVERT_Z_STEP_PIN);
  529. #ifdef Z_DUAL_STEPPER_DRIVERS
  530. WRITE(Z2_STEP_PIN, INVERT_Z_STEP_PIN);
  531. #endif
  532. }
  533. #ifndef ADVANCE
  534. counter_e += current_block->steps_e;
  535. if (counter_e > 0) {
  536. WRITE_E_STEP(!INVERT_E_STEP_PIN);
  537. counter_e -= current_block->step_event_count;
  538. count_position[E_AXIS]+=count_direction[E_AXIS];
  539. WRITE_E_STEP(INVERT_E_STEP_PIN);
  540. }
  541. #endif //!ADVANCE
  542. step_events_completed += 1;
  543. if(step_events_completed >= current_block->step_event_count) break;
  544. }
  545. // Calculare new timer value
  546. unsigned short timer;
  547. unsigned short step_rate;
  548. if (step_events_completed <= (unsigned long int)current_block->accelerate_until) {
  549. MultiU24X24toH16(acc_step_rate, acceleration_time, current_block->acceleration_rate);
  550. acc_step_rate += current_block->initial_rate;
  551. // upper limit
  552. if(acc_step_rate > current_block->nominal_rate)
  553. acc_step_rate = current_block->nominal_rate;
  554. // step_rate to timer interval
  555. timer = calc_timer(acc_step_rate);
  556. OCR1A = timer;
  557. acceleration_time += timer;
  558. #ifdef ADVANCE
  559. for(int8_t i=0; i < step_loops; i++) {
  560. advance += advance_rate;
  561. }
  562. //if(advance > current_block->advance) advance = current_block->advance;
  563. // Do E steps + advance steps
  564. e_steps[current_block->active_extruder] += ((advance >>8) - old_advance);
  565. old_advance = advance >>8;
  566. #endif
  567. }
  568. else if (step_events_completed > (unsigned long int)current_block->decelerate_after) {
  569. MultiU24X24toH16(step_rate, deceleration_time, current_block->acceleration_rate);
  570. if(step_rate > acc_step_rate) { // Check step_rate stays positive
  571. step_rate = current_block->final_rate;
  572. }
  573. else {
  574. step_rate = acc_step_rate - step_rate; // Decelerate from aceleration end point.
  575. }
  576. // lower limit
  577. if(step_rate < current_block->final_rate)
  578. step_rate = current_block->final_rate;
  579. // step_rate to timer interval
  580. timer = calc_timer(step_rate);
  581. OCR1A = timer;
  582. deceleration_time += timer;
  583. #ifdef ADVANCE
  584. for(int8_t i=0; i < step_loops; i++) {
  585. advance -= advance_rate;
  586. }
  587. if(advance < final_advance) advance = final_advance;
  588. // Do E steps + advance steps
  589. e_steps[current_block->active_extruder] += ((advance >>8) - old_advance);
  590. old_advance = advance >>8;
  591. #endif //ADVANCE
  592. }
  593. else {
  594. OCR1A = OCR1A_nominal;
  595. }
  596. // If current block is finished, reset pointer
  597. if (step_events_completed >= current_block->step_event_count) {
  598. current_block = NULL;
  599. plan_discard_current_block();
  600. }
  601. }
  602. }
  603. #ifdef ADVANCE
  604. unsigned char old_OCR0A;
  605. // Timer interrupt for E. e_steps is set in the main routine;
  606. // Timer 0 is shared with millies
  607. ISR(TIMER0_COMPA_vect)
  608. {
  609. old_OCR0A += 52; // ~10kHz interrupt (250000 / 26 = 9615kHz)
  610. OCR0A = old_OCR0A;
  611. // Set E direction (Depends on E direction + advance)
  612. for(unsigned char i=0; i<4;i++) {
  613. if (e_steps[0] != 0) {
  614. WRITE(E0_STEP_PIN, INVERT_E_STEP_PIN);
  615. if (e_steps[0] < 0) {
  616. WRITE(E0_DIR_PIN, INVERT_E0_DIR);
  617. e_steps[0]++;
  618. WRITE(E0_STEP_PIN, !INVERT_E_STEP_PIN);
  619. }
  620. else if (e_steps[0] > 0) {
  621. WRITE(E0_DIR_PIN, !INVERT_E0_DIR);
  622. e_steps[0]--;
  623. WRITE(E0_STEP_PIN, !INVERT_E_STEP_PIN);
  624. }
  625. }
  626. #if EXTRUDERS > 1
  627. if (e_steps[1] != 0) {
  628. WRITE(E1_STEP_PIN, INVERT_E_STEP_PIN);
  629. if (e_steps[1] < 0) {
  630. WRITE(E1_DIR_PIN, INVERT_E1_DIR);
  631. e_steps[1]++;
  632. WRITE(E1_STEP_PIN, !INVERT_E_STEP_PIN);
  633. }
  634. else if (e_steps[1] > 0) {
  635. WRITE(E1_DIR_PIN, !INVERT_E1_DIR);
  636. e_steps[1]--;
  637. WRITE(E1_STEP_PIN, !INVERT_E_STEP_PIN);
  638. }
  639. }
  640. #endif
  641. #if EXTRUDERS > 2
  642. if (e_steps[2] != 0) {
  643. WRITE(E2_STEP_PIN, INVERT_E_STEP_PIN);
  644. if (e_steps[2] < 0) {
  645. WRITE(E2_DIR_PIN, INVERT_E2_DIR);
  646. e_steps[2]++;
  647. WRITE(E2_STEP_PIN, !INVERT_E_STEP_PIN);
  648. }
  649. else if (e_steps[2] > 0) {
  650. WRITE(E2_DIR_PIN, !INVERT_E2_DIR);
  651. e_steps[2]--;
  652. WRITE(E2_STEP_PIN, !INVERT_E_STEP_PIN);
  653. }
  654. }
  655. #endif
  656. }
  657. }
  658. #endif // ADVANCE
  659. void st_init()
  660. {
  661. digipot_init(); //Initialize Digipot Motor Current
  662. microstep_init(); //Initialize Microstepping Pins
  663. //Initialize Dir Pins
  664. #if X_DIR_PIN > -1
  665. SET_OUTPUT(X_DIR_PIN);
  666. #endif
  667. #if Y_DIR_PIN > -1
  668. SET_OUTPUT(Y_DIR_PIN);
  669. #endif
  670. #if Z_DIR_PIN > -1
  671. SET_OUTPUT(Z_DIR_PIN);
  672. #if defined(Z_DUAL_STEPPER_DRIVERS) && (Z2_DIR_PIN > -1)
  673. SET_OUTPUT(Z2_DIR_PIN);
  674. #endif
  675. #endif
  676. #if E0_DIR_PIN > -1
  677. SET_OUTPUT(E0_DIR_PIN);
  678. #endif
  679. #if defined(E1_DIR_PIN) && (E1_DIR_PIN > -1)
  680. SET_OUTPUT(E1_DIR_PIN);
  681. #endif
  682. #if defined(E2_DIR_PIN) && (E2_DIR_PIN > -1)
  683. SET_OUTPUT(E2_DIR_PIN);
  684. #endif
  685. //Initialize Enable Pins - steppers default to disabled.
  686. #if (X_ENABLE_PIN > -1)
  687. SET_OUTPUT(X_ENABLE_PIN);
  688. if(!X_ENABLE_ON) WRITE(X_ENABLE_PIN,HIGH);
  689. #endif
  690. #if (Y_ENABLE_PIN > -1)
  691. SET_OUTPUT(Y_ENABLE_PIN);
  692. if(!Y_ENABLE_ON) WRITE(Y_ENABLE_PIN,HIGH);
  693. #endif
  694. #if (Z_ENABLE_PIN > -1)
  695. SET_OUTPUT(Z_ENABLE_PIN);
  696. if(!Z_ENABLE_ON) WRITE(Z_ENABLE_PIN,HIGH);
  697. #if defined(Z_DUAL_STEPPER_DRIVERS) && (Z2_ENABLE_PIN > -1)
  698. SET_OUTPUT(Z2_ENABLE_PIN);
  699. if(!Z_ENABLE_ON) WRITE(Z2_ENABLE_PIN,HIGH);
  700. #endif
  701. #endif
  702. #if (E0_ENABLE_PIN > -1)
  703. SET_OUTPUT(E0_ENABLE_PIN);
  704. if(!E_ENABLE_ON) WRITE(E0_ENABLE_PIN,HIGH);
  705. #endif
  706. #if defined(E1_ENABLE_PIN) && (E1_ENABLE_PIN > -1)
  707. SET_OUTPUT(E1_ENABLE_PIN);
  708. if(!E_ENABLE_ON) WRITE(E1_ENABLE_PIN,HIGH);
  709. #endif
  710. #if defined(E2_ENABLE_PIN) && (E2_ENABLE_PIN > -1)
  711. SET_OUTPUT(E2_ENABLE_PIN);
  712. if(!E_ENABLE_ON) WRITE(E2_ENABLE_PIN,HIGH);
  713. #endif
  714. //endstops and pullups
  715. #if X_MIN_PIN > -1
  716. SET_INPUT(X_MIN_PIN);
  717. #ifdef ENDSTOPPULLUP_XMIN
  718. WRITE(X_MIN_PIN,HIGH);
  719. #endif
  720. #endif
  721. #if Y_MIN_PIN > -1
  722. SET_INPUT(Y_MIN_PIN);
  723. #ifdef ENDSTOPPULLUP_YMIN
  724. WRITE(Y_MIN_PIN,HIGH);
  725. #endif
  726. #endif
  727. #if Z_MIN_PIN > -1
  728. SET_INPUT(Z_MIN_PIN);
  729. #ifdef ENDSTOPPULLUP_ZMIN
  730. WRITE(Z_MIN_PIN,HIGH);
  731. #endif
  732. #endif
  733. #if X_MAX_PIN > -1
  734. SET_INPUT(X_MAX_PIN);
  735. #ifdef ENDSTOPPULLUP_XMAX
  736. WRITE(X_MAX_PIN,HIGH);
  737. #endif
  738. #endif
  739. #if Y_MAX_PIN > -1
  740. SET_INPUT(Y_MAX_PIN);
  741. #ifdef ENDSTOPPULLUP_YMAX
  742. WRITE(Y_MAX_PIN,HIGH);
  743. #endif
  744. #endif
  745. #if Z_MAX_PIN > -1
  746. SET_INPUT(Z_MAX_PIN);
  747. #ifdef ENDSTOPPULLUP_ZMAX
  748. WRITE(Z_MAX_PIN,HIGH);
  749. #endif
  750. #endif
  751. //Initialize Step Pins
  752. #if (X_STEP_PIN > -1)
  753. SET_OUTPUT(X_STEP_PIN);
  754. WRITE(X_STEP_PIN,INVERT_X_STEP_PIN);
  755. disable_x();
  756. #endif
  757. #if (Y_STEP_PIN > -1)
  758. SET_OUTPUT(Y_STEP_PIN);
  759. WRITE(Y_STEP_PIN,INVERT_Y_STEP_PIN);
  760. disable_y();
  761. #endif
  762. #if (Z_STEP_PIN > -1)
  763. SET_OUTPUT(Z_STEP_PIN);
  764. WRITE(Z_STEP_PIN,INVERT_Z_STEP_PIN);
  765. #if defined(Z_DUAL_STEPPER_DRIVERS) && (Z2_STEP_PIN > -1)
  766. SET_OUTPUT(Z2_STEP_PIN);
  767. WRITE(Z2_STEP_PIN,INVERT_Z_STEP_PIN);
  768. #endif
  769. disable_z();
  770. #endif
  771. #if (E0_STEP_PIN > -1)
  772. SET_OUTPUT(E0_STEP_PIN);
  773. WRITE(E0_STEP_PIN,INVERT_E_STEP_PIN);
  774. disable_e0();
  775. #endif
  776. #if defined(E1_STEP_PIN) && (E1_STEP_PIN > -1)
  777. SET_OUTPUT(E1_STEP_PIN);
  778. WRITE(E1_STEP_PIN,INVERT_E_STEP_PIN);
  779. disable_e1();
  780. #endif
  781. #if defined(E2_STEP_PIN) && (E2_STEP_PIN > -1)
  782. SET_OUTPUT(E2_STEP_PIN);
  783. WRITE(E2_STEP_PIN,INVERT_E_STEP_PIN);
  784. disable_e2();
  785. #endif
  786. #ifdef CONTROLLERFAN_PIN
  787. SET_OUTPUT(CONTROLLERFAN_PIN); //Set pin used for driver cooling fan
  788. #endif
  789. // waveform generation = 0100 = CTC
  790. TCCR1B &= ~(1<<WGM13);
  791. TCCR1B |= (1<<WGM12);
  792. TCCR1A &= ~(1<<WGM11);
  793. TCCR1A &= ~(1<<WGM10);
  794. // output mode = 00 (disconnected)
  795. TCCR1A &= ~(3<<COM1A0);
  796. TCCR1A &= ~(3<<COM1B0);
  797. // Set the timer pre-scaler
  798. // Generally we use a divider of 8, resulting in a 2MHz timer
  799. // frequency on a 16MHz MCU. If you are going to change this, be
  800. // sure to regenerate speed_lookuptable.h with
  801. // create_speed_lookuptable.py
  802. TCCR1B = (TCCR1B & ~(0x07<<CS10)) | (2<<CS10);
  803. OCR1A = 0x4000;
  804. TCNT1 = 0;
  805. ENABLE_STEPPER_DRIVER_INTERRUPT();
  806. #ifdef ADVANCE
  807. #if defined(TCCR0A) && defined(WGM01)
  808. TCCR0A &= ~(1<<WGM01);
  809. TCCR0A &= ~(1<<WGM00);
  810. #endif
  811. e_steps[0] = 0;
  812. e_steps[1] = 0;
  813. e_steps[2] = 0;
  814. TIMSK0 |= (1<<OCIE0A);
  815. #endif //ADVANCE
  816. enable_endstops(true); // Start with endstops active. After homing they can be disabled
  817. sei();
  818. }
  819. // Block until all buffered steps are executed
  820. void st_synchronize()
  821. {
  822. while( blocks_queued()) {
  823. manage_heater();
  824. manage_inactivity();
  825. lcd_update();
  826. }
  827. }
  828. void st_set_position(const long &x, const long &y, const long &z, const long &e)
  829. {
  830. CRITICAL_SECTION_START;
  831. count_position[X_AXIS] = x;
  832. count_position[Y_AXIS] = y;
  833. count_position[Z_AXIS] = z;
  834. count_position[E_AXIS] = e;
  835. CRITICAL_SECTION_END;
  836. }
  837. void st_set_e_position(const long &e)
  838. {
  839. CRITICAL_SECTION_START;
  840. count_position[E_AXIS] = e;
  841. CRITICAL_SECTION_END;
  842. }
  843. long st_get_position(uint8_t axis)
  844. {
  845. long count_pos;
  846. CRITICAL_SECTION_START;
  847. count_pos = count_position[axis];
  848. CRITICAL_SECTION_END;
  849. return count_pos;
  850. }
  851. void finishAndDisableSteppers()
  852. {
  853. st_synchronize();
  854. disable_x();
  855. disable_y();
  856. disable_z();
  857. disable_e0();
  858. disable_e1();
  859. disable_e2();
  860. }
  861. void quickStop()
  862. {
  863. DISABLE_STEPPER_DRIVER_INTERRUPT();
  864. while(blocks_queued())
  865. plan_discard_current_block();
  866. current_block = NULL;
  867. ENABLE_STEPPER_DRIVER_INTERRUPT();
  868. }
  869. void digitalPotWrite(int address, int value) // From Arduino DigitalPotControl example
  870. {
  871. #if DIGIPOTSS_PIN > -1
  872. digitalWrite(DIGIPOTSS_PIN,LOW); // take the SS pin low to select the chip
  873. SPI.transfer(address); // send in the address and value via SPI:
  874. SPI.transfer(value);
  875. digitalWrite(DIGIPOTSS_PIN,HIGH); // take the SS pin high to de-select the chip:
  876. //delay(10);
  877. #endif
  878. }
  879. void digipot_init() //Initialize Digipot Motor Current
  880. {
  881. #if DIGIPOTSS_PIN > -1
  882. const uint8_t digipot_motor_current[] = DIGIPOT_MOTOR_CURRENT;
  883. SPI.begin();
  884. pinMode(DIGIPOTSS_PIN, OUTPUT);
  885. for(int i=0;i<=4;i++)
  886. //digitalPotWrite(digipot_ch[i], digipot_motor_current[i]);
  887. digipot_current(i,digipot_motor_current[i]);
  888. #endif
  889. }
  890. void digipot_current(uint8_t driver, int current)
  891. {
  892. #if DIGIPOTSS_PIN > -1
  893. const uint8_t digipot_ch[] = DIGIPOT_CHANNELS;
  894. digitalPotWrite(digipot_ch[driver], current);
  895. #endif
  896. }
  897. void microstep_init()
  898. {
  899. #if X_MS1_PIN > -1
  900. const uint8_t microstep_modes[] = MICROSTEP_MODES;
  901. pinMode(X_MS2_PIN,OUTPUT);
  902. pinMode(Y_MS2_PIN,OUTPUT);
  903. pinMode(Z_MS2_PIN,OUTPUT);
  904. pinMode(E0_MS2_PIN,OUTPUT);
  905. pinMode(E1_MS2_PIN,OUTPUT);
  906. for(int i=0;i<=4;i++) microstep_mode(i,microstep_modes[i]);
  907. #endif
  908. }
  909. void microstep_ms(uint8_t driver, int8_t ms1, int8_t ms2)
  910. {
  911. if(ms1 > -1) switch(driver)
  912. {
  913. case 0: digitalWrite( X_MS1_PIN,ms1); break;
  914. case 1: digitalWrite( Y_MS1_PIN,ms1); break;
  915. case 2: digitalWrite( Z_MS1_PIN,ms1); break;
  916. case 3: digitalWrite(E0_MS1_PIN,ms1); break;
  917. case 4: digitalWrite(E1_MS1_PIN,ms1); break;
  918. }
  919. if(ms2 > -1) switch(driver)
  920. {
  921. case 0: digitalWrite( X_MS2_PIN,ms2); break;
  922. case 1: digitalWrite( Y_MS2_PIN,ms2); break;
  923. case 2: digitalWrite( Z_MS2_PIN,ms2); break;
  924. case 3: digitalWrite(E0_MS2_PIN,ms2); break;
  925. case 4: digitalWrite(E1_MS2_PIN,ms2); break;
  926. }
  927. }
  928. void microstep_mode(uint8_t driver, uint8_t stepping_mode)
  929. {
  930. switch(stepping_mode)
  931. {
  932. case 1: microstep_ms(driver,MICROSTEP1); break;
  933. case 2: microstep_ms(driver,MICROSTEP2); break;
  934. case 4: microstep_ms(driver,MICROSTEP4); break;
  935. case 8: microstep_ms(driver,MICROSTEP8); break;
  936. case 16: microstep_ms(driver,MICROSTEP16); break;
  937. }
  938. }
  939. void microstep_readings()
  940. {
  941. SERIAL_PROTOCOLPGM("MS1,MS2 Pins\n");
  942. SERIAL_PROTOCOLPGM("X: ");
  943. SERIAL_PROTOCOL( digitalRead(X_MS1_PIN));
  944. SERIAL_PROTOCOLLN( digitalRead(X_MS2_PIN));
  945. SERIAL_PROTOCOLPGM("Y: ");
  946. SERIAL_PROTOCOL( digitalRead(Y_MS1_PIN));
  947. SERIAL_PROTOCOLLN( digitalRead(Y_MS2_PIN));
  948. SERIAL_PROTOCOLPGM("Z: ");
  949. SERIAL_PROTOCOL( digitalRead(Z_MS1_PIN));
  950. SERIAL_PROTOCOLLN( digitalRead(Z_MS2_PIN));
  951. SERIAL_PROTOCOLPGM("E0: ");
  952. SERIAL_PROTOCOL( digitalRead(E0_MS1_PIN));
  953. SERIAL_PROTOCOLLN( digitalRead(E0_MS2_PIN));
  954. SERIAL_PROTOCOLPGM("E1: ");
  955. SERIAL_PROTOCOL( digitalRead(E1_MS1_PIN));
  956. SERIAL_PROTOCOLLN( digitalRead(E1_MS2_PIN));
  957. }