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

stepper.cpp 32KB

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