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

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