My Marlin configs for Fabrikator Mini and CTC i3 Pro B
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

Marlin.pde 39KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250
  1. /*
  2. Reprap firmware based on Sprinter and grbl.
  3. Copyright (C) 2011 Camiel Gubbels / Erik van der Zalm
  4. This program is free software: you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation, either version 3 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program. If not, see <http://www.gnu.org/licenses/>.
  14. */
  15. /*
  16. This firmware is a mashup between Sprinter and grbl.
  17. (https://github.com/kliment/Sprinter)
  18. (https://github.com/simen/grbl/tree)
  19. It has preliminary support for Matthew Roberts advance algorithm
  20. http://reprap.org/pipermail/reprap-dev/2011-May/003323.html
  21. */
  22. #include <math.h>
  23. #include <EEPROM.h>
  24. #include <stdio.h>
  25. #include "EEPROMwrite.h"
  26. #include "fastio.h"
  27. #include "Configuration.h"
  28. #include "pins.h"
  29. #include "Marlin.h"
  30. #include "ultralcd.h"
  31. #include "planner.h"
  32. #include "stepper.h"
  33. #include "temperature.h"
  34. #include "motion_control.h"
  35. #include "cardreader.h"
  36. #include "watchdog.h"
  37. #define VERSION_STRING "1.0.0 Beta 1"
  38. // look here for descriptions of gcodes: http://linuxcnc.org/handbook/gcode/g-code.html
  39. // http://objects.reprap.org/wiki/Mendel_User_Manual:_RepRapGCodes
  40. //Implemented Codes
  41. //-------------------
  42. // G0 -> G1
  43. // G1 - Coordinated Movement X Y Z E
  44. // G2 - CW ARC
  45. // G3 - CCW ARC
  46. // G4 - Dwell S<seconds> or P<milliseconds>
  47. // G28 - Home all Axis
  48. // G90 - Use Absolute Coordinates
  49. // G91 - Use Relative Coordinates
  50. // G92 - Set current position to cordinates given
  51. //RepRap M Codes
  52. // M104 - Set extruder target temp
  53. // M105 - Read current temp
  54. // M106 - Fan on
  55. // M107 - Fan off
  56. // M109 - Wait for extruder current temp to reach target temp.
  57. // M114 - Display current position
  58. //Custom M Codes
  59. // M17 - Enable/Power all stepper motors
  60. // M18 - Disable all stepper motors; same as M84
  61. // M20 - List SD card
  62. // M21 - Init SD card
  63. // M22 - Release SD card
  64. // M23 - Select SD file (M23 filename.g)
  65. // M24 - Start/resume SD print
  66. // M25 - Pause SD print
  67. // M26 - Set SD position in bytes (M26 S12345)
  68. // M27 - Report SD print status
  69. // M28 - Start SD write (M28 filename.g)
  70. // M29 - Stop SD write
  71. // M30 - Output time since last M109 or SD card start to serial
  72. // M42 - Change pin status via gcode
  73. // M80 - Turn on Power Supply
  74. // M81 - Turn off Power Supply
  75. // M82 - Set E codes absolute (default)
  76. // M83 - Set E codes relative while in Absolute Coordinates (G90) mode
  77. // M84 - Disable steppers until next move,
  78. // or use S<seconds> to specify an inactivity timeout, after which the steppers will be disabled. S0 to disable the timeout.
  79. // M85 - Set inactivity shutdown timer with parameter S<seconds>. To disable set zero (default)
  80. // M92 - Set axis_steps_per_unit - same syntax as G92
  81. // M114 - Output current position to serial port
  82. // M115 - Capabilities string
  83. // M117 - display message
  84. // M119 - Output Endstop status to serial port
  85. // M140 - Set bed target temp
  86. // M190 - Wait for bed current temp to reach target temp.
  87. // M200 - Set filament diameter
  88. // M201 - Set max acceleration in units/s^2 for print moves (M201 X1000 Y1000)
  89. // M202 - Set max acceleration in units/s^2 for travel moves (M202 X1000 Y1000) Unused in Marlin!!
  90. // M203 - Set maximum feedrate that your machine can sustain (M203 X200 Y200 Z300 E10000) in mm/sec
  91. // M204 - Set default acceleration: S normal moves T filament only moves (M204 S3000 T7000) im mm/sec^2 also sets minimum segment time in ms (B20000) to prevent buffer underruns and M20 minimum feedrate
  92. // M205 - advanced settings: minimum travel speed S=while printing T=travel only, B=minimum segment time X= maximum xy jerk, Z=maximum Z jerk
  93. // M206 - set additional homeing offset
  94. // M220 - set speed factor override percentage S:factor in percent
  95. // M301 - Set PID parameters P I and D
  96. // M302 - Allow cold extrudes
  97. // M400 - Finish all moves
  98. // M500 - stores paramters in EEPROM
  99. // M501 - reads parameters from EEPROM (if you need reset them after you changed them temporarily).
  100. // M502 - reverts to the default "factory settings". You still need to store them in EEPROM afterwards if you want to.
  101. // M503 - print the current settings (from memory not from eeprom)
  102. //Stepper Movement Variables
  103. //===========================================================================
  104. //=============================imported variables============================
  105. //===========================================================================
  106. extern float HeaterPower;
  107. //===========================================================================
  108. //=============================public variables=============================
  109. //===========================================================================
  110. #ifdef SDSUPPORT
  111. CardReader card;
  112. #endif
  113. float homing_feedrate[] = HOMING_FEEDRATE;
  114. bool axis_relative_modes[] = AXIS_RELATIVE_MODES;
  115. volatile int feedmultiply=100; //100->1 200->2
  116. int saved_feedmultiply;
  117. volatile bool feedmultiplychanged=false;
  118. float current_position[NUM_AXIS] = { 0.0, 0.0, 0.0, 0.0};
  119. float add_homeing[3]={0,0,0};
  120. //===========================================================================
  121. //=============================private variables=============================
  122. //===========================================================================
  123. const char axis_codes[NUM_AXIS] = {'X', 'Y', 'Z', 'E'};
  124. static float destination[NUM_AXIS] = { 0.0, 0.0, 0.0, 0.0};
  125. static float offset[3] = {0.0, 0.0, 0.0};
  126. static bool home_all_axis = true;
  127. static float feedrate = 1500.0, next_feedrate, saved_feedrate;
  128. static long gcode_N, gcode_LastN;
  129. static bool relative_mode = false; //Determines Absolute or Relative Coordinates
  130. static bool relative_mode_e = false; //Determines Absolute or Relative E Codes while in Absolute Coordinates mode. E is always relative in Relative Coordinates mode.
  131. static uint8_t fanpwm=0;
  132. static char cmdbuffer[BUFSIZE][MAX_CMD_SIZE];
  133. static bool fromsd[BUFSIZE];
  134. static int bufindr = 0;
  135. static int bufindw = 0;
  136. static int buflen = 0;
  137. static int i = 0;
  138. static char serial_char;
  139. static int serial_count = 0;
  140. static boolean comment_mode = false;
  141. static char *strchr_pointer; // just a pointer to find chars in the cmd string like X, Y, Z, E, etc
  142. const int sensitive_pins[] = SENSITIVE_PINS; // Sensitive pin list for M42
  143. //static float tt = 0;
  144. //static float bt = 0;
  145. //Inactivity shutdown variables
  146. static unsigned long previous_millis_cmd = 0;
  147. static unsigned long max_inactive_time = 0;
  148. static unsigned long stepper_inactive_time = DEFAULT_STEPPER_DEACTIVE_TIME*1000;
  149. static unsigned long last_stepperdisabled_time=30*1000; //first release check after 30 seconds
  150. static unsigned long starttime=0;
  151. static unsigned long stoptime=0;
  152. static uint8_t tmp_extruder;
  153. //===========================================================================
  154. //=============================ROUTINES=============================
  155. //===========================================================================
  156. void get_arc_coordinates();
  157. extern "C"{
  158. extern unsigned int __bss_end;
  159. extern unsigned int __heap_start;
  160. extern void *__brkval;
  161. int freeMemory() {
  162. int free_memory;
  163. if((int)__brkval == 0)
  164. free_memory = ((int)&free_memory) - ((int)&__bss_end);
  165. else
  166. free_memory = ((int)&free_memory) - ((int)__brkval);
  167. return free_memory;
  168. }
  169. }
  170. //adds an command to the main command buffer
  171. //thats really done in a non-safe way.
  172. //needs overworking someday
  173. void enquecommand(const char *cmd)
  174. {
  175. if(buflen < BUFSIZE)
  176. {
  177. //this is dangerous if a mixing of serial and this happsens
  178. strcpy(&(cmdbuffer[bufindw][0]),cmd);
  179. SERIAL_ECHO_START;
  180. SERIAL_ECHOPGM("enqueing \"");
  181. SERIAL_ECHO(cmdbuffer[bufindw]);
  182. SERIAL_ECHOLNPGM("\"");
  183. bufindw= (bufindw + 1)%BUFSIZE;
  184. buflen += 1;
  185. }
  186. }
  187. void setup()
  188. {
  189. MSerial.begin(BAUDRATE);
  190. SERIAL_ECHO_START;
  191. SERIAL_ECHOLNPGM(VERSION_STRING);
  192. SERIAL_PROTOCOLLNPGM("start");
  193. SERIAL_ECHO_START;
  194. SERIAL_ECHOPGM("Free Memory:");
  195. SERIAL_ECHOLN(freeMemory());
  196. for(int8_t i = 0; i < BUFSIZE; i++)
  197. {
  198. fromsd[i] = false;
  199. }
  200. EEPROM_RetrieveSettings(); // loads data from EEPROM if available
  201. for(int8_t i=0; i < NUM_AXIS; i++)
  202. {
  203. axis_steps_per_sqr_second[i] = max_acceleration_units_per_sq_second[i] * axis_steps_per_unit[i];
  204. }
  205. tp_init(); // Initialize temperature loop
  206. plan_init(); // Initialize planner;
  207. st_init(); // Initialize stepper;
  208. wd_init();
  209. }
  210. void loop()
  211. {
  212. if(buflen<3)
  213. get_command();
  214. #ifdef SDSUPPORT
  215. card.checkautostart(false);
  216. #endif
  217. if(buflen)
  218. {
  219. #ifdef SDSUPPORT
  220. if(card.saving)
  221. {
  222. if(strstr(cmdbuffer[bufindr],"M29") == NULL)
  223. {
  224. card.write_command(cmdbuffer[bufindr]);
  225. SERIAL_PROTOCOLLNPGM("ok");
  226. }
  227. else
  228. {
  229. card.closefile();
  230. SERIAL_PROTOCOLLNPGM("Done saving file.");
  231. }
  232. }
  233. else
  234. {
  235. process_commands();
  236. }
  237. #else
  238. process_commands();
  239. #endif //SDSUPPORT
  240. buflen = (buflen-1);
  241. bufindr = (bufindr + 1)%BUFSIZE;
  242. }
  243. //check heater every n milliseconds
  244. manage_heater();
  245. manage_inactivity(1);
  246. checkHitEndstops();
  247. LCD_STATUS;
  248. }
  249. FORCE_INLINE void get_command()
  250. {
  251. while( MSerial.available() > 0 && buflen < BUFSIZE) {
  252. serial_char = MSerial.read();
  253. if(serial_char == '\n' || serial_char == '\r' || serial_char == ':' || serial_count >= (MAX_CMD_SIZE - 1) )
  254. {
  255. if(!serial_count) return; //if empty line
  256. cmdbuffer[bufindw][serial_count] = 0; //terminate string
  257. if(!comment_mode){
  258. fromsd[bufindw] = false;
  259. if(strstr(cmdbuffer[bufindw], "N") != NULL)
  260. {
  261. strchr_pointer = strchr(cmdbuffer[bufindw], 'N');
  262. gcode_N = (strtol(&cmdbuffer[bufindw][strchr_pointer - cmdbuffer[bufindw] + 1], NULL, 10));
  263. if(gcode_N != gcode_LastN+1 && (strstr(cmdbuffer[bufindw], "M110") == NULL) ) {
  264. SERIAL_ERROR_START;
  265. SERIAL_ERRORPGM("Line Number is not Last Line Number+1, Last Line:");
  266. SERIAL_ERRORLN(gcode_LastN);
  267. //Serial.println(gcode_N);
  268. FlushSerialRequestResend();
  269. serial_count = 0;
  270. return;
  271. }
  272. if(strstr(cmdbuffer[bufindw], "*") != NULL)
  273. {
  274. byte checksum = 0;
  275. byte count = 0;
  276. while(cmdbuffer[bufindw][count] != '*') checksum = checksum^cmdbuffer[bufindw][count++];
  277. strchr_pointer = strchr(cmdbuffer[bufindw], '*');
  278. if( (int)(strtod(&cmdbuffer[bufindw][strchr_pointer - cmdbuffer[bufindw] + 1], NULL)) != checksum) {
  279. SERIAL_ERROR_START;
  280. SERIAL_ERRORPGM("checksum mismatch, Last Line:");
  281. SERIAL_ERRORLN(gcode_LastN);
  282. FlushSerialRequestResend();
  283. serial_count = 0;
  284. return;
  285. }
  286. //if no errors, continue parsing
  287. }
  288. else
  289. {
  290. SERIAL_ERROR_START;
  291. SERIAL_ERRORPGM("No Checksum with line number, Last Line:");
  292. SERIAL_ERRORLN(gcode_LastN);
  293. FlushSerialRequestResend();
  294. serial_count = 0;
  295. return;
  296. }
  297. gcode_LastN = gcode_N;
  298. //if no errors, continue parsing
  299. }
  300. else // if we don't receive 'N' but still see '*'
  301. {
  302. if((strstr(cmdbuffer[bufindw], "*") != NULL))
  303. {
  304. SERIAL_ERROR_START;
  305. SERIAL_ERRORPGM("No Line Number with checksum, Last Line:");
  306. SERIAL_ERRORLN(gcode_LastN);
  307. serial_count = 0;
  308. return;
  309. }
  310. }
  311. if((strstr(cmdbuffer[bufindw], "G") != NULL)){
  312. strchr_pointer = strchr(cmdbuffer[bufindw], 'G');
  313. switch((int)((strtod(&cmdbuffer[bufindw][strchr_pointer - cmdbuffer[bufindw] + 1], NULL)))){
  314. case 0:
  315. case 1:
  316. case 2:
  317. case 3:
  318. #ifdef SDSUPPORT
  319. if(card.saving)
  320. break;
  321. #endif //SDSUPPORT
  322. SERIAL_PROTOCOLLNPGM("ok");
  323. break;
  324. default:
  325. break;
  326. }
  327. }
  328. bufindw = (bufindw + 1)%BUFSIZE;
  329. buflen += 1;
  330. }
  331. comment_mode = false; //for new command
  332. serial_count = 0; //clear buffer
  333. }
  334. else
  335. {
  336. if(serial_char == ';') comment_mode = true;
  337. if(!comment_mode) cmdbuffer[bufindw][serial_count++] = serial_char;
  338. }
  339. }
  340. #ifdef SDSUPPORT
  341. if(!card.sdprinting || serial_count!=0){
  342. return;
  343. }
  344. while( !card.eof() && buflen < BUFSIZE) {
  345. int16_t n=card.get();
  346. serial_char = (char)n;
  347. if(serial_char == '\n' || serial_char == '\r' || serial_char == ':' || serial_count >= (MAX_CMD_SIZE - 1)||n==-1)
  348. {
  349. if(card.eof()){
  350. SERIAL_PROTOCOLLNPGM("Done printing file");
  351. stoptime=millis();
  352. char time[30];
  353. unsigned long t=(stoptime-starttime)/1000;
  354. int sec,min;
  355. min=t/60;
  356. sec=t%60;
  357. sprintf(time,"%i min, %i sec",min,sec);
  358. SERIAL_ECHO_START;
  359. SERIAL_ECHOLN(time);
  360. LCD_MESSAGE(time);
  361. card.printingHasFinished();
  362. card.checkautostart(true);
  363. }
  364. if(serial_char=='\n')
  365. comment_mode = false; //for new command
  366. if(!serial_count)
  367. {
  368. return; //if empty line
  369. }
  370. cmdbuffer[bufindw][serial_count] = 0; //terminate string
  371. if(!comment_mode){
  372. fromsd[bufindw] = true;
  373. buflen += 1;
  374. bufindw = (bufindw + 1)%BUFSIZE;
  375. }
  376. serial_count = 0; //clear buffer
  377. }
  378. else
  379. {
  380. if(serial_char == ';') comment_mode = true;
  381. if(!comment_mode) cmdbuffer[bufindw][serial_count++] = serial_char;
  382. }
  383. }
  384. #endif //SDSUPPORT
  385. }
  386. FORCE_INLINE float code_value()
  387. {
  388. return (strtod(&cmdbuffer[bufindr][strchr_pointer - cmdbuffer[bufindr] + 1], NULL));
  389. }
  390. FORCE_INLINE long code_value_long()
  391. {
  392. return (strtol(&cmdbuffer[bufindr][strchr_pointer - cmdbuffer[bufindr] + 1], NULL, 10));
  393. }
  394. FORCE_INLINE bool code_seen(char code_string[]) //Return True if the string was found
  395. {
  396. return (strstr(cmdbuffer[bufindr], code_string) != NULL);
  397. }
  398. FORCE_INLINE bool code_seen(char code)
  399. {
  400. strchr_pointer = strchr(cmdbuffer[bufindr], code);
  401. return (strchr_pointer != NULL); //Return True if a character was found
  402. }
  403. #define HOMEAXIS(LETTER) \
  404. if ((LETTER##_MIN_PIN > -1 && LETTER##_HOME_DIR==-1) || (LETTER##_MAX_PIN > -1 && LETTER##_HOME_DIR==1))\
  405. { \
  406. current_position[LETTER##_AXIS] = 0; \
  407. plan_set_position(current_position[X_AXIS], current_position[Y_AXIS], current_position[Z_AXIS], current_position[E_AXIS]); \
  408. destination[LETTER##_AXIS] = 1.5 * LETTER##_MAX_LENGTH * LETTER##_HOME_DIR; \
  409. feedrate = homing_feedrate[LETTER##_AXIS]; \
  410. prepare_move(); \
  411. \
  412. current_position[LETTER##_AXIS] = 0;\
  413. plan_set_position(current_position[X_AXIS], current_position[Y_AXIS], current_position[Z_AXIS], current_position[E_AXIS]);\
  414. destination[LETTER##_AXIS] = -LETTER##_HOME_RETRACT_MM * LETTER##_HOME_DIR;\
  415. prepare_move(); \
  416. \
  417. destination[LETTER##_AXIS] = 2*LETTER##_HOME_RETRACT_MM * LETTER##_HOME_DIR;\
  418. feedrate = homing_feedrate[LETTER##_AXIS]/2 ; \
  419. prepare_move(); \
  420. \
  421. current_position[LETTER##_AXIS] = (LETTER##_HOME_DIR == -1) ? 0 : LETTER##_MAX_LENGTH;\
  422. plan_set_position(current_position[X_AXIS], current_position[Y_AXIS], current_position[Z_AXIS], current_position[E_AXIS]);\
  423. destination[LETTER##_AXIS] = current_position[LETTER##_AXIS];\
  424. feedrate = 0.0;\
  425. st_synchronize();\
  426. plan_set_position(current_position[X_AXIS], current_position[Y_AXIS], current_position[Z_AXIS], current_position[E_AXIS]);\
  427. endstops_hit_on_purpose();\
  428. }
  429. FORCE_INLINE void process_commands()
  430. {
  431. unsigned long codenum; //throw away variable
  432. char *starpos = NULL;
  433. if(code_seen('G'))
  434. {
  435. switch((int)code_value())
  436. {
  437. case 0: // G0 -> G1
  438. case 1: // G1
  439. get_coordinates(); // For X Y Z E F
  440. prepare_move();
  441. //ClearToSend();
  442. return;
  443. //break;
  444. case 2: // G2 - CW ARC
  445. get_arc_coordinates();
  446. prepare_arc_move(true);
  447. return;
  448. case 3: // G3 - CCW ARC
  449. get_arc_coordinates();
  450. prepare_arc_move(false);
  451. return;
  452. case 4: // G4 dwell
  453. LCD_MESSAGEPGM("DWELL...");
  454. codenum = 0;
  455. if(code_seen('P')) codenum = code_value(); // milliseconds to wait
  456. if(code_seen('S')) codenum = code_value() * 1000; // seconds to wait
  457. st_synchronize();
  458. codenum += millis(); // keep track of when we started waiting
  459. previous_millis_cmd = millis();
  460. while(millis() < codenum ){
  461. manage_heater();
  462. }
  463. break;
  464. case 28: //G28 Home all Axis one at a time
  465. saved_feedrate = feedrate;
  466. saved_feedmultiply = feedmultiply;
  467. feedmultiply = 100;
  468. enable_endstops(true);
  469. for(int8_t i=0; i < NUM_AXIS; i++) {
  470. destination[i] = current_position[i];
  471. }
  472. feedrate = 0.0;
  473. home_all_axis = !((code_seen(axis_codes[0])) || (code_seen(axis_codes[1])) || (code_seen(axis_codes[2])));
  474. #ifdef QUICK_HOME
  475. if( code_seen(axis_codes[0]) && code_seen(axis_codes[1]) ) //first diagonal move
  476. {
  477. current_position[X_AXIS] = 0;current_position[Y_AXIS] = 0;
  478. plan_set_position(current_position[X_AXIS], current_position[Y_AXIS], current_position[Z_AXIS], current_position[E_AXIS]);
  479. destination[X_AXIS] = 1.5 * X_MAX_LENGTH * X_HOME_DIR;destination[Y_AXIS] = 1.5 * Y_MAX_LENGTH * Y_HOME_DIR;
  480. feedrate = homing_feedrate[X_AXIS];
  481. if(homing_feedrate[Y_AXIS]<feedrate)
  482. feedrate =homing_feedrate[Y_AXIS];
  483. prepare_move();
  484. current_position[X_AXIS] = (X_HOME_DIR == -1) ? 0 : X_MAX_LENGTH;
  485. current_position[Y_AXIS] = (Y_HOME_DIR == -1) ? 0 : Y_MAX_LENGTH;
  486. plan_set_position(current_position[X_AXIS], current_position[Y_AXIS], current_position[Z_AXIS], current_position[E_AXIS]);
  487. destination[X_AXIS] = current_position[X_AXIS];
  488. destination[Y_AXIS] = current_position[Y_AXIS];
  489. feedrate = 0.0;
  490. st_synchronize();
  491. plan_set_position(0, 0, current_position[Z_AXIS], current_position[E_AXIS]);
  492. current_position[X_AXIS] = 0;current_position[Y_AXIS] = 0;
  493. endstops_hit_on_purpose();
  494. }
  495. #endif
  496. if((home_all_axis) || (code_seen(axis_codes[X_AXIS])))
  497. {
  498. HOMEAXIS(X);
  499. }
  500. if((home_all_axis) || (code_seen(axis_codes[Y_AXIS]))) {
  501. HOMEAXIS(Y);
  502. }
  503. if((home_all_axis) || (code_seen(axis_codes[Z_AXIS]))) {
  504. HOMEAXIS(Z);
  505. }
  506. if(code_seen(axis_codes[X_AXIS]))
  507. {
  508. current_position[0]=code_value()+add_homeing[0];
  509. }
  510. if(code_seen(axis_codes[Y_AXIS])) {
  511. current_position[1]=code_value()+add_homeing[1];
  512. }
  513. if(code_seen(axis_codes[Z_AXIS])) {
  514. current_position[2]=code_value()+add_homeing[2];
  515. }
  516. #ifdef ENDSTOPS_ONLY_FOR_HOMING
  517. enable_endstops(false);
  518. #endif
  519. feedrate = saved_feedrate;
  520. feedmultiply = saved_feedmultiply;
  521. previous_millis_cmd = millis();
  522. endstops_hit_on_purpose();
  523. break;
  524. case 90: // G90
  525. relative_mode = false;
  526. break;
  527. case 91: // G91
  528. relative_mode = true;
  529. break;
  530. case 92: // G92
  531. if(!code_seen(axis_codes[E_AXIS]))
  532. st_synchronize();
  533. for(int8_t i=0; i < NUM_AXIS; i++) {
  534. if(code_seen(axis_codes[i])) {
  535. current_position[i] = code_value()+add_homeing[i];
  536. if(i == E_AXIS) {
  537. plan_set_e_position(current_position[E_AXIS]);
  538. }
  539. else {
  540. plan_set_position(current_position[X_AXIS], current_position[Y_AXIS], current_position[Z_AXIS], current_position[E_AXIS]);
  541. }
  542. }
  543. }
  544. break;
  545. }
  546. }
  547. else if(code_seen('M'))
  548. {
  549. switch( (int)code_value() )
  550. {
  551. case 17:
  552. LCD_MESSAGEPGM("No move.");
  553. enable_x();
  554. enable_y();
  555. enable_z();
  556. enable_e();
  557. break;
  558. #ifdef SDSUPPORT
  559. case 20: // M20 - list SD card
  560. SERIAL_PROTOCOLLNPGM("Begin file list");
  561. card.ls();
  562. SERIAL_PROTOCOLLNPGM("End file list");
  563. break;
  564. case 21: // M21 - init SD card
  565. card.initsd();
  566. break;
  567. case 22: //M22 - release SD card
  568. card.release();
  569. break;
  570. case 23: //M23 - Select file
  571. starpos = (strchr(strchr_pointer + 4,'*'));
  572. if(starpos!=NULL)
  573. *(starpos-1)='\0';
  574. card.openFile(strchr_pointer + 4,true);
  575. break;
  576. case 24: //M24 - Start SD print
  577. card.startFileprint();
  578. starttime=millis();
  579. break;
  580. case 25: //M25 - Pause SD print
  581. card.pauseSDPrint();
  582. break;
  583. case 26: //M26 - Set SD index
  584. if(card.cardOK && code_seen('S')){
  585. card.setIndex(code_value_long());
  586. }
  587. break;
  588. case 27: //M27 - Get SD status
  589. card.getStatus();
  590. break;
  591. case 28: //M28 - Start SD write
  592. starpos = (strchr(strchr_pointer + 4,'*'));
  593. if(starpos != NULL){
  594. char* npos = strchr(cmdbuffer[bufindr], 'N');
  595. strchr_pointer = strchr(npos,' ') + 1;
  596. *(starpos-1) = '\0';
  597. }
  598. card.openFile(strchr_pointer+4,false);
  599. break;
  600. case 29: //M29 - Stop SD write
  601. //processed in write to file routine above
  602. //card,saving = false;
  603. break;
  604. #endif //SDSUPPORT
  605. case 30: //M30 take time since the start of the SD print or an M109 command
  606. {
  607. stoptime=millis();
  608. char time[30];
  609. unsigned long t=(stoptime-starttime)/1000;
  610. int sec,min;
  611. min=t/60;
  612. sec=t%60;
  613. sprintf(time,"%i min, %i sec",min,sec);
  614. SERIAL_ECHO_START;
  615. SERIAL_ECHOLN(time);
  616. LCD_MESSAGE(time);
  617. autotempShutdown();
  618. }
  619. break;
  620. case 42: //M42 -Change pin status via gcode
  621. if (code_seen('S'))
  622. {
  623. int pin_status = code_value();
  624. if (code_seen('P') && pin_status >= 0 && pin_status <= 255)
  625. {
  626. int pin_number = code_value();
  627. for(int8_t i = 0; i < (int8_t)sizeof(sensitive_pins); i++)
  628. {
  629. if (sensitive_pins[i] == pin_number)
  630. {
  631. pin_number = -1;
  632. break;
  633. }
  634. }
  635. if (pin_number > -1)
  636. {
  637. pinMode(pin_number, OUTPUT);
  638. digitalWrite(pin_number, pin_status);
  639. analogWrite(pin_number, pin_status);
  640. }
  641. }
  642. }
  643. break;
  644. case 104: // M104
  645. tmp_extruder = active_extruder;
  646. if(code_seen('T')) {
  647. tmp_extruder = code_value();
  648. if(tmp_extruder >= EXTRUDERS) {
  649. SERIAL_ECHO_START;
  650. SERIAL_ECHO("M104 Invalid extruder ");
  651. SERIAL_ECHOLN(tmp_extruder);
  652. break;
  653. }
  654. }
  655. if (code_seen('S')) setTargetHotend(code_value(), tmp_extruder);
  656. setWatch();
  657. break;
  658. case 140: // M140 set bed temp
  659. if (code_seen('S')) setTargetBed(code_value());
  660. break;
  661. case 105 : // M105
  662. tmp_extruder = active_extruder;
  663. if(code_seen('T')) {
  664. tmp_extruder = code_value();
  665. if(tmp_extruder >= EXTRUDERS) {
  666. SERIAL_ECHO_START;
  667. SERIAL_ECHO("M105 Invalid extruder ");
  668. SERIAL_ECHOLN(tmp_extruder);
  669. break;
  670. }
  671. }
  672. #if (TEMP_0_PIN > -1) || (TEMP_2_PIN > -1)
  673. SERIAL_PROTOCOLPGM("ok T:");
  674. SERIAL_PROTOCOL( degHotend(tmp_extruder));
  675. #if TEMP_1_PIN > -1
  676. SERIAL_PROTOCOLPGM(" B:");
  677. SERIAL_PROTOCOL(degBed());
  678. #endif //TEMP_1_PIN
  679. #else
  680. SERIAL_ERROR_START;
  681. SERIAL_ERRORLNPGM("No thermistors - no temp");
  682. #endif
  683. #ifdef PIDTEMP
  684. SERIAL_PROTOCOLPGM(" @:");
  685. SERIAL_PROTOCOL( HeaterPower);
  686. #endif
  687. SERIAL_PROTOCOLLN("");
  688. return;
  689. break;
  690. case 109:
  691. {// M109 - Wait for extruder heater to reach target.
  692. tmp_extruder = active_extruder;
  693. if(code_seen('T')) {
  694. tmp_extruder = code_value();
  695. if(tmp_extruder >= EXTRUDERS) {
  696. SERIAL_ECHO_START;
  697. SERIAL_ECHO("M109 Invalid extruder ");
  698. SERIAL_ECHOLN(tmp_extruder);
  699. break;
  700. }
  701. }
  702. LCD_MESSAGEPGM("Heating...");
  703. #ifdef AUTOTEMP
  704. autotemp_enabled=false;
  705. #endif
  706. if (code_seen('S')) setTargetHotend(code_value(), tmp_extruder);
  707. #ifdef AUTOTEMP
  708. if (code_seen('S')) autotemp_min=code_value();
  709. if (code_seen('G')) autotemp_max=code_value();
  710. if (code_seen('F'))
  711. {
  712. autotemp_factor=code_value();
  713. autotemp_enabled=true;
  714. }
  715. #endif
  716. setWatch();
  717. codenum = millis();
  718. /* See if we are heating up or cooling down */
  719. bool target_direction = isHeatingHotend(tmp_extruder); // true if heating, false if cooling
  720. #ifdef TEMP_RESIDENCY_TIME
  721. long residencyStart;
  722. residencyStart = -1;
  723. /* continue to loop until we have reached the target temp
  724. _and_ until TEMP_RESIDENCY_TIME hasn't passed since we reached it */
  725. while((target_direction ? (isHeatingHotend(tmp_extruder)) : (isCoolingHotend(tmp_extruder))) ||
  726. (residencyStart > -1 && (millis() - residencyStart) < TEMP_RESIDENCY_TIME*1000) ) {
  727. #else
  728. while ( target_direction ? (isHeatingHotend(tmp_extruder)) : (isCoolingHotend(tmp_extruder)&&(CooldownNoWait==false)) ) {
  729. #endif //TEMP_RESIDENCY_TIME
  730. if( (millis() - codenum) > 1000 )
  731. { //Print Temp Reading every 1 second while heating up/cooling down
  732. SERIAL_PROTOCOLPGM("T:");
  733. SERIAL_PROTOCOLLN( degHotend(tmp_extruder) );
  734. codenum = millis();
  735. }
  736. manage_heater();
  737. LCD_STATUS;
  738. #ifdef TEMP_RESIDENCY_TIME
  739. /* start/restart the TEMP_RESIDENCY_TIME timer whenever we reach target temp for the first time
  740. or when current temp falls outside the hysteresis after target temp was reached */
  741. if ((residencyStart == -1 && target_direction && !isHeatingHotend(tmp_extruder)) ||
  742. (residencyStart == -1 && !target_direction && !isCoolingHotend(tmp_extruder)) ||
  743. (residencyStart > -1 && labs(degHotend(tmp_extruder) - degTargetHotend(tmp_extruder)) > TEMP_HYSTERESIS) )
  744. {
  745. residencyStart = millis();
  746. }
  747. #endif //TEMP_RESIDENCY_TIME
  748. }
  749. LCD_MESSAGEPGM("Heating done.");
  750. starttime=millis();
  751. previous_millis_cmd = millis();
  752. }
  753. break;
  754. case 190: // M190 - Wait bed for heater to reach target.
  755. #if TEMP_1_PIN > -1
  756. LCD_MESSAGEPGM("Bed Heating.");
  757. if (code_seen('S')) setTargetBed(code_value());
  758. codenum = millis();
  759. while(isHeatingBed())
  760. {
  761. if( (millis()-codenum) > 1000 ) //Print Temp Reading every 1 second while heating up.
  762. {
  763. float tt=degHotend0();
  764. SERIAL_PROTOCOLPGM("T:");
  765. SERIAL_PROTOCOLLN(tt );
  766. SERIAL_PROTOCOLPGM("ok T:");
  767. SERIAL_PROTOCOL(tt );
  768. SERIAL_PROTOCOLPGM(" B:");
  769. SERIAL_PROTOCOLLN(degBed() );
  770. codenum = millis();
  771. }
  772. manage_heater();
  773. }
  774. LCD_MESSAGEPGM("Bed done.");
  775. previous_millis_cmd = millis();
  776. #endif
  777. break;
  778. #if FAN_PIN > -1
  779. case 106: //M106 Fan On
  780. if (code_seen('S')){
  781. WRITE(FAN_PIN,HIGH);
  782. fanpwm=constrain(code_value(),0,255);
  783. analogWrite(FAN_PIN, fanpwm);
  784. }
  785. else {
  786. WRITE(FAN_PIN,HIGH);
  787. fanpwm=255;
  788. analogWrite(FAN_PIN, fanpwm);
  789. }
  790. break;
  791. case 107: //M107 Fan Off
  792. WRITE(FAN_PIN,LOW);
  793. analogWrite(FAN_PIN, 0);
  794. break;
  795. #endif //FAN_PIN
  796. #if (PS_ON_PIN > -1)
  797. case 80: // M80 - ATX Power On
  798. SET_OUTPUT(PS_ON_PIN); //GND
  799. break;
  800. case 81: // M81 - ATX Power Off
  801. SET_INPUT(PS_ON_PIN); //Floating
  802. break;
  803. #endif
  804. case 82:
  805. axis_relative_modes[3] = false;
  806. break;
  807. case 83:
  808. axis_relative_modes[3] = true;
  809. break;
  810. case 18: //compatibility
  811. case 84: // M84
  812. if(code_seen('S')){
  813. stepper_inactive_time = code_value() * 1000;
  814. }
  815. else
  816. {
  817. bool all_axis = !((code_seen(axis_codes[0])) || (code_seen(axis_codes[1])) || (code_seen(axis_codes[2]))|| (code_seen(axis_codes[3])));
  818. if(all_axis)
  819. {
  820. finishAndDisableSteppers();
  821. }
  822. else
  823. {
  824. st_synchronize();
  825. if(code_seen('X')) disable_x();
  826. if(code_seen('Y')) disable_y();
  827. if(code_seen('Z')) disable_z();
  828. #if ((E_ENABLE_PIN != X_ENABLE_PIN) && (E_ENABLE_PIN != Y_ENABLE_PIN)) // Only enable on boards that have seperate ENABLE_PINS
  829. if(code_seen('E')) disable_e();
  830. #endif
  831. LCD_MESSAGEPGM("Partial Release");
  832. }
  833. }
  834. break;
  835. case 85: // M85
  836. code_seen('S');
  837. max_inactive_time = code_value() * 1000;
  838. break;
  839. case 92: // M92
  840. for(int8_t i=0; i < NUM_AXIS; i++)
  841. {
  842. if(code_seen(axis_codes[i]))
  843. axis_steps_per_unit[i] = code_value();
  844. }
  845. break;
  846. case 115: // M115
  847. SerialprintPGM("FIRMWARE_NAME:Marlin; Sprinter/grbl mashup for gen6 FIRMWARE_URL:http://www.mendel-parts.com PROTOCOL_VERSION:1.0 MACHINE_TYPE:Mendel EXTRUDER_COUNT:1");
  848. break;
  849. case 117: // M117 display message
  850. LCD_MESSAGE(cmdbuffer[bufindr]+5);
  851. break;
  852. case 114: // M114
  853. SERIAL_PROTOCOLPGM("X:");
  854. SERIAL_PROTOCOL(current_position[X_AXIS]);
  855. SERIAL_PROTOCOLPGM("Y:");
  856. SERIAL_PROTOCOL(current_position[Y_AXIS]);
  857. SERIAL_PROTOCOLPGM("Z:");
  858. SERIAL_PROTOCOL(current_position[Z_AXIS]);
  859. SERIAL_PROTOCOLPGM("E:");
  860. SERIAL_PROTOCOL(current_position[E_AXIS]);
  861. SERIAL_PROTOCOLPGM(" Count X:");
  862. SERIAL_PROTOCOL(float(st_get_position(X_AXIS))/axis_steps_per_unit[X_AXIS]);
  863. SERIAL_PROTOCOLPGM("Y:");
  864. SERIAL_PROTOCOL(float(st_get_position(Y_AXIS))/axis_steps_per_unit[Y_AXIS]);
  865. SERIAL_PROTOCOLPGM("Z:");
  866. SERIAL_PROTOCOL(float(st_get_position(Z_AXIS))/axis_steps_per_unit[Z_AXIS]);
  867. SERIAL_PROTOCOLLN("");
  868. break;
  869. case 119: // M119
  870. #if (X_MIN_PIN > -1)
  871. SERIAL_PROTOCOLPGM("x_min:");
  872. SERIAL_PROTOCOL(((READ(X_MIN_PIN)^X_ENDSTOPS_INVERTING)?"H ":"L "));
  873. #endif
  874. #if (X_MAX_PIN > -1)
  875. SERIAL_PROTOCOLPGM("x_max:");
  876. SERIAL_PROTOCOL(((READ(X_MAX_PIN)^X_ENDSTOPS_INVERTING)?"H ":"L "));
  877. #endif
  878. #if (Y_MIN_PIN > -1)
  879. SERIAL_PROTOCOLPGM("y_min:");
  880. SERIAL_PROTOCOL(((READ(Y_MIN_PIN)^Y_ENDSTOPS_INVERTING)?"H ":"L "));
  881. #endif
  882. #if (Y_MAX_PIN > -1)
  883. SERIAL_PROTOCOLPGM("y_max:");
  884. SERIAL_PROTOCOL(((READ(Y_MAX_PIN)^Y_ENDSTOPS_INVERTING)?"H ":"L "));
  885. #endif
  886. #if (Z_MIN_PIN > -1)
  887. SERIAL_PROTOCOLPGM("z_min:");
  888. SERIAL_PROTOCOL(((READ(Z_MIN_PIN)^Z_ENDSTOPS_INVERTING)?"H ":"L "));
  889. #endif
  890. #if (Z_MAX_PIN > -1)
  891. SERIAL_PROTOCOLPGM("z_max:");
  892. SERIAL_PROTOCOL(((READ(Z_MAX_PIN)^Z_ENDSTOPS_INVERTING)?"H ":"L "));
  893. #endif
  894. SERIAL_PROTOCOLLN("");
  895. break;
  896. //TODO: update for all axis, use for loop
  897. case 201: // M201
  898. for(int8_t i=0; i < NUM_AXIS; i++)
  899. {
  900. if(code_seen(axis_codes[i]))
  901. {
  902. max_acceleration_units_per_sq_second[i] = code_value();
  903. axis_steps_per_sqr_second[i] = code_value() * axis_steps_per_unit[i];
  904. }
  905. }
  906. break;
  907. #if 0 // Not used for Sprinter/grbl gen6
  908. case 202: // M202
  909. for(int8_t i=0; i < NUM_AXIS; i++) {
  910. if(code_seen(axis_codes[i])) axis_travel_steps_per_sqr_second[i] = code_value() * axis_steps_per_unit[i];
  911. }
  912. break;
  913. #endif
  914. case 203: // M203 max feedrate mm/sec
  915. for(int8_t i=0; i < NUM_AXIS; i++) {
  916. if(code_seen(axis_codes[i])) max_feedrate[i] = code_value();
  917. }
  918. break;
  919. case 204: // M204 acclereration S normal moves T filmanent only moves
  920. {
  921. if(code_seen('S')) acceleration = code_value() ;
  922. if(code_seen('T')) retract_acceleration = code_value() ;
  923. }
  924. break;
  925. case 205: //M205 advanced settings: minimum travel speed S=while printing T=travel only, B=minimum segment time X= maximum xy jerk, Z=maximum Z jerk
  926. {
  927. if(code_seen('S')) minimumfeedrate = code_value();
  928. if(code_seen('T')) mintravelfeedrate = code_value();
  929. if(code_seen('B')) minsegmenttime = code_value() ;
  930. if(code_seen('X')) max_xy_jerk = code_value() ;
  931. if(code_seen('Z')) max_z_jerk = code_value() ;
  932. }
  933. break;
  934. case 206: // M206 additional homeing offset
  935. for(int8_t i=0; i < 3; i++)
  936. {
  937. if(code_seen(axis_codes[i])) add_homeing[i] = code_value();
  938. }
  939. break;
  940. case 220: // M220 S<factor in percent>- set speed factor override percentage
  941. {
  942. if(code_seen('S'))
  943. {
  944. feedmultiply = code_value() ;
  945. feedmultiplychanged=true;
  946. }
  947. }
  948. break;
  949. #ifdef PIDTEMP
  950. case 301: // M301
  951. {
  952. if(code_seen('P')) Kp = code_value();
  953. if(code_seen('I')) Ki = code_value()*PID_dT;
  954. if(code_seen('D')) Kd = code_value()/PID_dT;
  955. #ifdef PID_ADD_EXTRUSION_RATE
  956. if(code_seen('C')) Kc = code_value();
  957. #endif
  958. updatePID();
  959. SERIAL_PROTOCOL("ok p:");
  960. SERIAL_PROTOCOL(Kp);
  961. SERIAL_PROTOCOL(" i:");
  962. SERIAL_PROTOCOL(Ki/PID_dT);
  963. SERIAL_PROTOCOL(" d:");
  964. SERIAL_PROTOCOL(Kd*PID_dT);
  965. #ifdef PID_ADD_EXTRUSION_RATE
  966. SERIAL_PROTOCOL(" c:");
  967. SERIAL_PROTOCOL(Kc*PID_dT);
  968. #endif
  969. SERIAL_PROTOCOLLN("");
  970. }
  971. break;
  972. #endif //PIDTEMP
  973. case 302: // finish all moves
  974. {
  975. allow_cold_extrudes(true);
  976. }
  977. break;
  978. case 400: // finish all moves
  979. {
  980. st_synchronize();
  981. }
  982. break;
  983. case 500: // Store settings in EEPROM
  984. {
  985. EEPROM_StoreSettings();
  986. }
  987. break;
  988. case 501: // Read settings from EEPROM
  989. {
  990. EEPROM_RetrieveSettings();
  991. }
  992. break;
  993. case 502: // Revert to default settings
  994. {
  995. EEPROM_RetrieveSettings(true);
  996. }
  997. break;
  998. case 503: // print settings currently in memory
  999. {
  1000. EEPROM_printSettings();
  1001. }
  1002. break;
  1003. }
  1004. }
  1005. else if(code_seen('T')) {
  1006. tmp_extruder = code_value();
  1007. if(tmp_extruder >= EXTRUDERS) {
  1008. SERIAL_ECHO_START;
  1009. SERIAL_ECHO("T");
  1010. SERIAL_ECHO(tmp_extruder);
  1011. SERIAL_ECHOLN("Invalid extruder");
  1012. }
  1013. else {
  1014. active_extruder = tmp_extruder;
  1015. }
  1016. }
  1017. else
  1018. {
  1019. SERIAL_ECHO_START;
  1020. SERIAL_ECHOPGM("Unknown command:\"");
  1021. SERIAL_ECHO(cmdbuffer[bufindr]);
  1022. SERIAL_ECHOLNPGM("\"");
  1023. }
  1024. ClearToSend();
  1025. }
  1026. void FlushSerialRequestResend()
  1027. {
  1028. //char cmdbuffer[bufindr][100]="Resend:";
  1029. MSerial.flush();
  1030. SERIAL_PROTOCOLPGM("Resend:");
  1031. SERIAL_PROTOCOLLN(gcode_LastN + 1);
  1032. ClearToSend();
  1033. }
  1034. void ClearToSend()
  1035. {
  1036. previous_millis_cmd = millis();
  1037. #ifdef SDSUPPORT
  1038. if(fromsd[bufindr])
  1039. return;
  1040. #endif //SDSUPPORT
  1041. SERIAL_PROTOCOLLNPGM("ok");
  1042. }
  1043. FORCE_INLINE void get_coordinates()
  1044. {
  1045. for(int8_t i=0; i < NUM_AXIS; i++) {
  1046. if(code_seen(axis_codes[i])) destination[i] = (float)code_value() + (axis_relative_modes[i] || relative_mode)*current_position[i];
  1047. else destination[i] = current_position[i]; //Are these else lines really needed?
  1048. }
  1049. if(code_seen('F')) {
  1050. next_feedrate = code_value();
  1051. if(next_feedrate > 0.0) feedrate = next_feedrate;
  1052. }
  1053. }
  1054. FORCE_INLINE void get_arc_coordinates()
  1055. {
  1056. get_coordinates();
  1057. if(code_seen('I')) offset[0] = code_value();
  1058. if(code_seen('J')) offset[1] = code_value();
  1059. }
  1060. void prepare_move()
  1061. {
  1062. if (min_software_endstops) {
  1063. if (destination[X_AXIS] < 0) destination[X_AXIS] = 0.0;
  1064. if (destination[Y_AXIS] < 0) destination[Y_AXIS] = 0.0;
  1065. if (destination[Z_AXIS] < 0) destination[Z_AXIS] = 0.0;
  1066. }
  1067. if (max_software_endstops) {
  1068. if (destination[X_AXIS] > X_MAX_LENGTH) destination[X_AXIS] = X_MAX_LENGTH;
  1069. if (destination[Y_AXIS] > Y_MAX_LENGTH) destination[Y_AXIS] = Y_MAX_LENGTH;
  1070. if (destination[Z_AXIS] > Z_MAX_LENGTH) destination[Z_AXIS] = Z_MAX_LENGTH;
  1071. }
  1072. plan_buffer_line(destination[X_AXIS], destination[Y_AXIS], destination[Z_AXIS], destination[E_AXIS], feedrate*feedmultiply/60/100.0, active_extruder);
  1073. for(int8_t i=0; i < NUM_AXIS; i++) {
  1074. current_position[i] = destination[i];
  1075. }
  1076. previous_millis_cmd = millis();
  1077. }
  1078. void prepare_arc_move(char isclockwise) {
  1079. float r = hypot(offset[X_AXIS], offset[Y_AXIS]); // Compute arc radius for mc_arc
  1080. // Trace the arc
  1081. mc_arc(current_position, destination, offset, X_AXIS, Y_AXIS, Z_AXIS, feedrate*feedmultiply/60/100.0, r, isclockwise, active_extruder);
  1082. // As far as the parser is concerned, the position is now == target. In reality the
  1083. // motion control system might still be processing the action and the real tool position
  1084. // in any intermediate location.
  1085. for(int8_t i=0; i < NUM_AXIS; i++) {
  1086. current_position[i] = destination[i];
  1087. }
  1088. previous_millis_cmd = millis();
  1089. }
  1090. void manage_inactivity(byte debug)
  1091. {
  1092. if( (millis()-previous_millis_cmd) > max_inactive_time )
  1093. if(max_inactive_time)
  1094. kill();
  1095. if(stepper_inactive_time)
  1096. if( (millis()-last_stepperdisabled_time) > stepper_inactive_time )
  1097. {
  1098. if(previous_millis_cmd>last_stepperdisabled_time)
  1099. last_stepperdisabled_time=previous_millis_cmd;
  1100. else
  1101. {
  1102. enquecommand(DEFAULT_STEPPER_DEACTIVE_COMMAND);
  1103. last_stepperdisabled_time=millis();
  1104. }
  1105. }
  1106. #ifdef EXTRUDER_RUNOUT_PREVENT
  1107. if( (millis()-previous_millis_cmd) > EXTRUDER_RUNOUT_SECONDS*1000 )
  1108. if(degHotend(active_extruder)>EXTRUDER_RUNOUT_MINTEMP)
  1109. {
  1110. enable_e();
  1111. float oldepos=current_position[E_AXIS];
  1112. float oldedes=destination[E_AXIS];
  1113. plan_buffer_line(current_position[X_AXIS], current_position[Y_AXIS], current_position[Z_AXIS],
  1114. current_position[E_AXIS]+EXTRUDER_RUNOUT_EXTRUDE*EXTRUDER_RUNOUT_ESTEPS/axis_steps_per_unit[E_AXIS],
  1115. EXTRUDER_RUNOUT_SPEED/60.*EXTRUDER_RUNOUT_ESTEPS/axis_steps_per_unit[E_AXIS], active_extruder);
  1116. current_position[E_AXIS]=oldepos;
  1117. destination[E_AXIS]=oldedes;
  1118. plan_set_e_position(oldepos);
  1119. previous_millis_cmd=millis();
  1120. enquecommand(DEFAULT_STEPPER_DEACTIVE_COMMAND);
  1121. }
  1122. #endif
  1123. check_axes_activity();
  1124. }
  1125. void kill()
  1126. {
  1127. disable_heater();
  1128. disable_x();
  1129. disable_y();
  1130. disable_z();
  1131. disable_e();
  1132. if(PS_ON_PIN > -1) pinMode(PS_ON_PIN,INPUT);
  1133. SERIAL_ERROR_START;
  1134. SERIAL_ERRORLNPGM("Printer halted. kill() called !!");
  1135. LCD_MESSAGEPGM("KILLED. ");
  1136. while(1); // Wait for reset
  1137. }