My Marlin configs for Fabrikator Mini and CTC i3 Pro B
您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

stepper.cpp 29KB

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