My Marlin configs for Fabrikator Mini and CTC i3 Pro B
Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055
  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 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_STATUS;
  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. LCD_MESSAGEPGM(MSG_STEPPER_RELEASED);
  838. disable_x();
  839. disable_y();
  840. disable_z();
  841. disable_e0();
  842. disable_e1();
  843. disable_e2();
  844. }
  845. void quickStop()
  846. {
  847. DISABLE_STEPPER_DRIVER_INTERRUPT();
  848. while(blocks_queued())
  849. plan_discard_current_block();
  850. current_block = NULL;
  851. ENABLE_STEPPER_DRIVER_INTERRUPT();
  852. }
  853. int digitalPotWrite(int address, int value) // From Arduino DigitalPotControl example
  854. {
  855. #if DIGIPOTSS_PIN > -1
  856. digitalWrite(DIGIPOTSS_PIN,LOW); // take the SS pin low to select the chip
  857. SPI.transfer(address); // send in the address and value via SPI:
  858. SPI.transfer(value);
  859. digitalWrite(DIGIPOTSS_PIN,HIGH); // take the SS pin high to de-select the chip:
  860. //delay(10);
  861. #endif
  862. }
  863. void digipot_init() //Initialize Digipot Motor Current
  864. {
  865. #if DIGIPOTSS_PIN > -1
  866. const uint8_t digipot_motor_current[] = DIGIPOT_MOTOR_CURRENT;
  867. SPI.begin();
  868. pinMode(DIGIPOTSS_PIN, OUTPUT);
  869. for(int i=0;i<=4;i++)
  870. //digitalPotWrite(digipot_ch[i], digipot_motor_current[i]);
  871. digipot_current(i,digipot_motor_current[i]);
  872. #endif
  873. }
  874. void digipot_current(uint8_t driver, int current)
  875. {
  876. #if DIGIPOTSS_PIN > -1
  877. const uint8_t digipot_ch[] = DIGIPOT_CHANNELS;
  878. digitalPotWrite(digipot_ch[driver], current);
  879. #endif
  880. }
  881. void microstep_init()
  882. {
  883. #if X_MS1_PIN > -1
  884. const uint8_t microstep_modes[] = MICROSTEP_MODES;
  885. pinMode(X_MS2_PIN,OUTPUT);
  886. pinMode(Y_MS2_PIN,OUTPUT);
  887. pinMode(Z_MS2_PIN,OUTPUT);
  888. pinMode(E0_MS2_PIN,OUTPUT);
  889. pinMode(E1_MS2_PIN,OUTPUT);
  890. for(int i=0;i<=4;i++) microstep_mode(i,microstep_modes[i]);
  891. #endif
  892. }
  893. void microstep_ms(uint8_t driver, int8_t ms1, int8_t ms2)
  894. {
  895. if(ms1 > -1) switch(driver)
  896. {
  897. case 0: digitalWrite( X_MS1_PIN,ms1); break;
  898. case 1: digitalWrite( Y_MS1_PIN,ms1); break;
  899. case 2: digitalWrite( Z_MS1_PIN,ms1); break;
  900. case 3: digitalWrite(E0_MS1_PIN,ms1); break;
  901. case 4: digitalWrite(E1_MS1_PIN,ms1); break;
  902. }
  903. if(ms2 > -1) switch(driver)
  904. {
  905. case 0: digitalWrite( X_MS2_PIN,ms2); break;
  906. case 1: digitalWrite( Y_MS2_PIN,ms2); break;
  907. case 2: digitalWrite( Z_MS2_PIN,ms2); break;
  908. case 3: digitalWrite(E0_MS2_PIN,ms2); break;
  909. case 4: digitalWrite(E1_MS2_PIN,ms2); break;
  910. }
  911. }
  912. void microstep_mode(uint8_t driver, uint8_t stepping_mode)
  913. {
  914. switch(stepping_mode)
  915. {
  916. case 1: microstep_ms(driver,MICROSTEP1); break;
  917. case 2: microstep_ms(driver,MICROSTEP2); break;
  918. case 4: microstep_ms(driver,MICROSTEP4); break;
  919. case 8: microstep_ms(driver,MICROSTEP8); break;
  920. case 16: microstep_ms(driver,MICROSTEP16); break;
  921. }
  922. }
  923. void microstep_readings()
  924. {
  925. SERIAL_PROTOCOLPGM("MS1,MS2 Pins\n");
  926. SERIAL_PROTOCOLPGM("X: ");
  927. SERIAL_PROTOCOL( digitalRead(X_MS1_PIN));
  928. SERIAL_PROTOCOLLN( digitalRead(X_MS2_PIN));
  929. SERIAL_PROTOCOLPGM("Y: ");
  930. SERIAL_PROTOCOL( digitalRead(Y_MS1_PIN));
  931. SERIAL_PROTOCOLLN( digitalRead(Y_MS2_PIN));
  932. SERIAL_PROTOCOLPGM("Z: ");
  933. SERIAL_PROTOCOL( digitalRead(Z_MS1_PIN));
  934. SERIAL_PROTOCOLLN( digitalRead(Z_MS2_PIN));
  935. SERIAL_PROTOCOLPGM("E0: ");
  936. SERIAL_PROTOCOL( digitalRead(E0_MS1_PIN));
  937. SERIAL_PROTOCOLLN( digitalRead(E0_MS2_PIN));
  938. SERIAL_PROTOCOLPGM("E1: ");
  939. SERIAL_PROTOCOL( digitalRead(E1_MS1_PIN));
  940. SERIAL_PROTOCOLLN( digitalRead(E1_MS2_PIN));
  941. }