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 46KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515
  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 "Marlin.h"
  23. #include "ultralcd.h"
  24. #include "planner.h"
  25. #include "stepper.h"
  26. #include "temperature.h"
  27. #include "motion_control.h"
  28. #include "cardreader.h"
  29. #include "watchdog.h"
  30. #include "EEPROMwrite.h"
  31. #include "language.h"
  32. #define VERSION_STRING "1.0.0 RC2"
  33. // look here for descriptions of gcodes: http://linuxcnc.org/handbook/gcode/g-code.html
  34. // http://objects.reprap.org/wiki/Mendel_User_Manual:_RepRapGCodes
  35. //Implemented Codes
  36. //-------------------
  37. // G0 -> G1
  38. // G1 - Coordinated Movement X Y Z E
  39. // G2 - CW ARC
  40. // G3 - CCW ARC
  41. // G4 - Dwell S<seconds> or P<milliseconds>
  42. // G28 - Home all Axis
  43. // G90 - Use Absolute Coordinates
  44. // G91 - Use Relative Coordinates
  45. // G92 - Set current position to cordinates given
  46. //RepRap M Codes
  47. // M104 - Set extruder target temp
  48. // M105 - Read current temp
  49. // M106 - Fan on
  50. // M107 - Fan off
  51. // M109 - Wait for extruder current temp to reach target temp.
  52. // M114 - Display current position
  53. //Custom M Codes
  54. // M17 - Enable/Power all stepper motors
  55. // M18 - Disable all stepper motors; same as M84
  56. // M20 - List SD card
  57. // M21 - Init SD card
  58. // M22 - Release SD card
  59. // M23 - Select SD file (M23 filename.g)
  60. // M24 - Start/resume SD print
  61. // M25 - Pause SD print
  62. // M26 - Set SD position in bytes (M26 S12345)
  63. // M27 - Report SD print status
  64. // M28 - Start SD write (M28 filename.g)
  65. // M29 - Stop SD write
  66. // M30 - Delete file from SD (M30 filename.g)
  67. // M31 - Output time since last M109 or SD card start to serial
  68. // M42 - Change pin status via gcode
  69. // M80 - Turn on Power Supply
  70. // M81 - Turn off Power Supply
  71. // M82 - Set E codes absolute (default)
  72. // M83 - Set E codes relative while in Absolute Coordinates (G90) mode
  73. // M84 - Disable steppers until next move,
  74. // or use S<seconds> to specify an inactivity timeout, after which the steppers will be disabled. S0 to disable the timeout.
  75. // M85 - Set inactivity shutdown timer with parameter S<seconds>. To disable set zero (default)
  76. // M92 - Set axis_steps_per_unit - same syntax as G92
  77. // M114 - Output current position to serial port
  78. // M115 - Capabilities string
  79. // M117 - display message
  80. // M119 - Output Endstop status to serial port
  81. // M140 - Set bed target temp
  82. // M190 - Wait for bed current temp to reach target temp.
  83. // M200 - Set filament diameter
  84. // M201 - Set max acceleration in units/s^2 for print moves (M201 X1000 Y1000)
  85. // M202 - Set max acceleration in units/s^2 for travel moves (M202 X1000 Y1000) Unused in Marlin!!
  86. // M203 - Set maximum feedrate that your machine can sustain (M203 X200 Y200 Z300 E10000) in mm/sec
  87. // 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
  88. // M205 - advanced settings: minimum travel speed S=while printing T=travel only, B=minimum segment time X= maximum xy jerk, Z=maximum Z jerk, E=maximum E jerk
  89. // M206 - set additional homeing offset
  90. // M220 S<factor in percent>- set speed factor override percentage
  91. // M221 S<factor in percent>- set extrude factor override percentage
  92. // M240 - Trigger a camera to take a photograph
  93. // M301 - Set PID parameters P I and D
  94. // M302 - Allow cold extrudes
  95. // M400 - Finish all moves
  96. // M500 - stores paramters in EEPROM
  97. // M501 - reads parameters from EEPROM (if you need reset them after you changed them temporarily).
  98. // M502 - reverts to the default "factory settings". You still need to store them in EEPROM afterwards if you want to.
  99. // M503 - print the current settings (from memory not from eeprom)
  100. // M303 - PID relay autotune S<temperature> sets the target temperature. (default target temperature = 150C)
  101. // M999 - Restart after being stopped by error
  102. //Stepper Movement Variables
  103. //===========================================================================
  104. //=============================imported variables============================
  105. //===========================================================================
  106. //===========================================================================
  107. //=============================public variables=============================
  108. //===========================================================================
  109. #ifdef SDSUPPORT
  110. CardReader card;
  111. #endif
  112. float homing_feedrate[] = HOMING_FEEDRATE;
  113. bool axis_relative_modes[] = AXIS_RELATIVE_MODES;
  114. volatile int feedmultiply=100; //100->1 200->2
  115. int saved_feedmultiply;
  116. volatile bool feedmultiplychanged=false;
  117. volatile int extrudemultiply=100; //100->1 200->2
  118. float current_position[NUM_AXIS] = { 0.0, 0.0, 0.0, 0.0 };
  119. float add_homeing[3]={0,0,0};
  120. uint8_t active_extruder = 0;
  121. unsigned char FanSpeed=0;
  122. //===========================================================================
  123. //=============================private variables=============================
  124. //===========================================================================
  125. const char axis_codes[NUM_AXIS] = {'X', 'Y', 'Z', 'E'};
  126. static float destination[NUM_AXIS] = { 0.0, 0.0, 0.0, 0.0};
  127. static float offset[3] = {0.0, 0.0, 0.0};
  128. static bool home_all_axis = true;
  129. static float feedrate = 1500.0, next_feedrate, saved_feedrate;
  130. static long gcode_N, gcode_LastN, Stopped_gcode_LastN = 0;
  131. static bool relative_mode = false; //Determines Absolute or Relative Coordinates
  132. 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.
  133. static char cmdbuffer[BUFSIZE][MAX_CMD_SIZE];
  134. static bool fromsd[BUFSIZE];
  135. static int bufindr = 0;
  136. static int bufindw = 0;
  137. static int buflen = 0;
  138. //static int i = 0;
  139. static char serial_char;
  140. static int serial_count = 0;
  141. static boolean comment_mode = false;
  142. static char *strchr_pointer; // just a pointer to find chars in the cmd string like X, Y, Z, E, etc
  143. const int sensitive_pins[] = SENSITIVE_PINS; // Sensitive pin list for M42
  144. //static float tt = 0;
  145. //static float bt = 0;
  146. //Inactivity shutdown variables
  147. static unsigned long previous_millis_cmd = 0;
  148. static unsigned long max_inactive_time = 0;
  149. static unsigned long stepper_inactive_time = DEFAULT_STEPPER_DEACTIVE_TIME*1000l;
  150. static unsigned long starttime=0;
  151. static unsigned long stoptime=0;
  152. static uint8_t tmp_extruder;
  153. bool Stopped=false;
  154. //===========================================================================
  155. //=============================ROUTINES=============================
  156. //===========================================================================
  157. void get_arc_coordinates();
  158. extern "C"{
  159. extern unsigned int __bss_end;
  160. extern unsigned int __heap_start;
  161. extern void *__brkval;
  162. int freeMemory() {
  163. int free_memory;
  164. if((int)__brkval == 0)
  165. free_memory = ((int)&free_memory) - ((int)&__bss_end);
  166. else
  167. free_memory = ((int)&free_memory) - ((int)__brkval);
  168. return free_memory;
  169. }
  170. }
  171. //adds an command to the main command buffer
  172. //thats really done in a non-safe way.
  173. //needs overworking someday
  174. void enquecommand(const char *cmd)
  175. {
  176. if(buflen < BUFSIZE)
  177. {
  178. //this is dangerous if a mixing of serial and this happsens
  179. strcpy(&(cmdbuffer[bufindw][0]),cmd);
  180. SERIAL_ECHO_START;
  181. SERIAL_ECHOPGM("enqueing \"");
  182. SERIAL_ECHO(cmdbuffer[bufindw]);
  183. SERIAL_ECHOLNPGM("\"");
  184. bufindw= (bufindw + 1)%BUFSIZE;
  185. buflen += 1;
  186. }
  187. }
  188. void setup_photpin()
  189. {
  190. #ifdef PHOTOGRAPH_PIN
  191. #if (PHOTOGRAPH_PIN > -1)
  192. SET_OUTPUT(PHOTOGRAPH_PIN);
  193. WRITE(PHOTOGRAPH_PIN, LOW);
  194. #endif
  195. #endif
  196. }
  197. void setup_powerhold()
  198. {
  199. #ifdef SUICIDE_PIN
  200. #if (SUICIDE_PIN> -1)
  201. SET_OUTPUT(SUICIDE_PIN);
  202. WRITE(SUICIDE_PIN, HIGH);
  203. #endif
  204. #endif
  205. }
  206. void suicide()
  207. {
  208. #ifdef SUICIDE_PIN
  209. #if (SUICIDE_PIN> -1)
  210. SET_OUTPUT(SUICIDE_PIN);
  211. WRITE(SUICIDE_PIN, LOW);
  212. #endif
  213. #endif
  214. }
  215. void setup()
  216. {
  217. setup_powerhold();
  218. MYSERIAL.begin(BAUDRATE);
  219. SERIAL_PROTOCOLLNPGM("start");
  220. SERIAL_ECHO_START;
  221. // Check startup - does nothing if bootloader sets MCUSR to 0
  222. byte mcu = MCUSR;
  223. if(mcu & 1) SERIAL_ECHOLNPGM(MSG_POWERUP);
  224. if(mcu & 2) SERIAL_ECHOLNPGM(MSG_EXTERNAL_RESET);
  225. if(mcu & 4) SERIAL_ECHOLNPGM(MSG_BROWNOUT_RESET);
  226. if(mcu & 8) SERIAL_ECHOLNPGM(MSG_WATCHDOG_RESET);
  227. if(mcu & 32) SERIAL_ECHOLNPGM(MSG_SOFTWARE_RESET);
  228. MCUSR=0;
  229. SERIAL_ECHOPGM(MSG_MARLIN);
  230. SERIAL_ECHOLNPGM(VERSION_STRING);
  231. #ifdef STRING_VERSION_CONFIG_H
  232. #ifdef STRING_CONFIG_H_AUTHOR
  233. SERIAL_ECHO_START;
  234. SERIAL_ECHOPGM(MSG_CONFIGURATION_VER);
  235. SERIAL_ECHOPGM(STRING_VERSION_CONFIG_H);
  236. SERIAL_ECHOPGM(MSG_AUTHOR);
  237. SERIAL_ECHOLNPGM(STRING_CONFIG_H_AUTHOR);
  238. #endif
  239. #endif
  240. SERIAL_ECHO_START;
  241. SERIAL_ECHOPGM(MSG_FREE_MEMORY);
  242. SERIAL_ECHO(freeMemory());
  243. SERIAL_ECHOPGM(MSG_PLANNER_BUFFER_BYTES);
  244. SERIAL_ECHOLN((int)sizeof(block_t)*BLOCK_BUFFER_SIZE);
  245. for(int8_t i = 0; i < BUFSIZE; i++)
  246. {
  247. fromsd[i] = false;
  248. }
  249. EEPROM_RetrieveSettings(); // loads data from EEPROM if available
  250. for(int8_t i=0; i < NUM_AXIS; i++)
  251. {
  252. axis_steps_per_sqr_second[i] = max_acceleration_units_per_sq_second[i] * axis_steps_per_unit[i];
  253. }
  254. tp_init(); // Initialize temperature loop
  255. plan_init(); // Initialize planner;
  256. st_init(); // Initialize stepper;
  257. wd_init();
  258. setup_photpin();
  259. }
  260. void loop()
  261. {
  262. if(buflen < (BUFSIZE-1))
  263. get_command();
  264. #ifdef SDSUPPORT
  265. card.checkautostart(false);
  266. #endif
  267. if(buflen)
  268. {
  269. #ifdef SDSUPPORT
  270. if(card.saving)
  271. {
  272. if(strstr(cmdbuffer[bufindr],"M29") == NULL)
  273. {
  274. card.write_command(cmdbuffer[bufindr]);
  275. SERIAL_PROTOCOLLNPGM(MSG_OK);
  276. }
  277. else
  278. {
  279. card.closefile();
  280. SERIAL_PROTOCOLLNPGM(MSG_FILE_SAVED);
  281. }
  282. }
  283. else
  284. {
  285. process_commands();
  286. }
  287. #else
  288. process_commands();
  289. #endif //SDSUPPORT
  290. buflen = (buflen-1);
  291. bufindr = (bufindr + 1)%BUFSIZE;
  292. }
  293. //check heater every n milliseconds
  294. manage_heater();
  295. manage_inactivity(1);
  296. checkHitEndstops();
  297. LCD_STATUS;
  298. }
  299. void get_command()
  300. {
  301. while( MYSERIAL.available() > 0 && buflen < BUFSIZE) {
  302. serial_char = MYSERIAL.read();
  303. if(serial_char == '\n' ||
  304. serial_char == '\r' ||
  305. (serial_char == ':' && comment_mode == false) ||
  306. serial_count >= (MAX_CMD_SIZE - 1) )
  307. {
  308. if(!serial_count) { //if empty line
  309. comment_mode = false; //for new command
  310. return;
  311. }
  312. cmdbuffer[bufindw][serial_count] = 0; //terminate string
  313. if(!comment_mode){
  314. comment_mode = false; //for new command
  315. fromsd[bufindw] = false;
  316. if(strstr(cmdbuffer[bufindw], "N") != NULL)
  317. {
  318. strchr_pointer = strchr(cmdbuffer[bufindw], 'N');
  319. gcode_N = (strtol(&cmdbuffer[bufindw][strchr_pointer - cmdbuffer[bufindw] + 1], NULL, 10));
  320. if(gcode_N != gcode_LastN+1 && (strstr(cmdbuffer[bufindw], "M110") == NULL) ) {
  321. SERIAL_ERROR_START;
  322. SERIAL_ERRORPGM(MSG_ERR_LINE_NO);
  323. SERIAL_ERRORLN(gcode_LastN);
  324. //Serial.println(gcode_N);
  325. FlushSerialRequestResend();
  326. serial_count = 0;
  327. return;
  328. }
  329. if(strstr(cmdbuffer[bufindw], "*") != NULL)
  330. {
  331. byte checksum = 0;
  332. byte count = 0;
  333. while(cmdbuffer[bufindw][count] != '*') checksum = checksum^cmdbuffer[bufindw][count++];
  334. strchr_pointer = strchr(cmdbuffer[bufindw], '*');
  335. if( (int)(strtod(&cmdbuffer[bufindw][strchr_pointer - cmdbuffer[bufindw] + 1], NULL)) != checksum) {
  336. SERIAL_ERROR_START;
  337. SERIAL_ERRORPGM(MSG_ERR_CHECKSUM_MISMATCH);
  338. SERIAL_ERRORLN(gcode_LastN);
  339. FlushSerialRequestResend();
  340. serial_count = 0;
  341. return;
  342. }
  343. //if no errors, continue parsing
  344. }
  345. else
  346. {
  347. SERIAL_ERROR_START;
  348. SERIAL_ERRORPGM(MSG_ERR_NO_CHECKSUM);
  349. SERIAL_ERRORLN(gcode_LastN);
  350. FlushSerialRequestResend();
  351. serial_count = 0;
  352. return;
  353. }
  354. gcode_LastN = gcode_N;
  355. //if no errors, continue parsing
  356. }
  357. else // if we don't receive 'N' but still see '*'
  358. {
  359. if((strstr(cmdbuffer[bufindw], "*") != NULL))
  360. {
  361. SERIAL_ERROR_START;
  362. SERIAL_ERRORPGM(MSG_ERR_NO_LINENUMBER_WITH_CHECKSUM);
  363. SERIAL_ERRORLN(gcode_LastN);
  364. serial_count = 0;
  365. return;
  366. }
  367. }
  368. if((strstr(cmdbuffer[bufindw], "G") != NULL)){
  369. strchr_pointer = strchr(cmdbuffer[bufindw], 'G');
  370. switch((int)((strtod(&cmdbuffer[bufindw][strchr_pointer - cmdbuffer[bufindw] + 1], NULL)))){
  371. case 0:
  372. case 1:
  373. case 2:
  374. case 3:
  375. if(Stopped == false) { // If printer is stopped by an error the G[0-3] codes are ignored.
  376. #ifdef SDSUPPORT
  377. if(card.saving)
  378. break;
  379. #endif //SDSUPPORT
  380. SERIAL_PROTOCOLLNPGM(MSG_OK);
  381. }
  382. else {
  383. SERIAL_ERRORLNPGM(MSG_ERR_STOPPED);
  384. LCD_MESSAGEPGM(MSG_STOPPED);
  385. }
  386. break;
  387. default:
  388. break;
  389. }
  390. }
  391. bufindw = (bufindw + 1)%BUFSIZE;
  392. buflen += 1;
  393. }
  394. serial_count = 0; //clear buffer
  395. }
  396. else
  397. {
  398. if(serial_char == ';') comment_mode = true;
  399. if(!comment_mode) cmdbuffer[bufindw][serial_count++] = serial_char;
  400. }
  401. }
  402. #ifdef SDSUPPORT
  403. if(!card.sdprinting || serial_count!=0){
  404. return;
  405. }
  406. while( !card.eof() && buflen < BUFSIZE) {
  407. int16_t n=card.get();
  408. serial_char = (char)n;
  409. if(serial_char == '\n' ||
  410. serial_char == '\r' ||
  411. (serial_char == ':' && comment_mode == false) ||
  412. serial_count >= (MAX_CMD_SIZE - 1)||n==-1)
  413. {
  414. if(card.eof()){
  415. SERIAL_PROTOCOLLNPGM(MSG_FILE_PRINTED);
  416. stoptime=millis();
  417. char time[30];
  418. unsigned long t=(stoptime-starttime)/1000;
  419. int sec,min;
  420. min=t/60;
  421. sec=t%60;
  422. sprintf(time,"%i min, %i sec",min,sec);
  423. SERIAL_ECHO_START;
  424. SERIAL_ECHOLN(time);
  425. LCD_MESSAGE(time);
  426. card.printingHasFinished();
  427. card.checkautostart(true);
  428. }
  429. if(!serial_count)
  430. {
  431. comment_mode = false; //for new command
  432. return; //if empty line
  433. }
  434. cmdbuffer[bufindw][serial_count] = 0; //terminate string
  435. // if(!comment_mode){
  436. fromsd[bufindw] = true;
  437. buflen += 1;
  438. bufindw = (bufindw + 1)%BUFSIZE;
  439. // }
  440. comment_mode = false; //for new command
  441. serial_count = 0; //clear buffer
  442. }
  443. else
  444. {
  445. if(serial_char == ';') comment_mode = true;
  446. if(!comment_mode) cmdbuffer[bufindw][serial_count++] = serial_char;
  447. }
  448. }
  449. #endif //SDSUPPORT
  450. }
  451. float code_value()
  452. {
  453. return (strtod(&cmdbuffer[bufindr][strchr_pointer - cmdbuffer[bufindr] + 1], NULL));
  454. }
  455. long code_value_long()
  456. {
  457. return (strtol(&cmdbuffer[bufindr][strchr_pointer - cmdbuffer[bufindr] + 1], NULL, 10));
  458. }
  459. bool code_seen(char code_string[]) //Return True if the string was found
  460. {
  461. return (strstr(cmdbuffer[bufindr], code_string) != NULL);
  462. }
  463. bool code_seen(char code)
  464. {
  465. strchr_pointer = strchr(cmdbuffer[bufindr], code);
  466. return (strchr_pointer != NULL); //Return True if a character was found
  467. }
  468. #define HOMEAXIS(LETTER) \
  469. if ((LETTER##_MIN_PIN > -1 && LETTER##_HOME_DIR==-1) || (LETTER##_MAX_PIN > -1 && LETTER##_HOME_DIR==1))\
  470. { \
  471. current_position[LETTER##_AXIS] = 0; \
  472. plan_set_position(current_position[X_AXIS], current_position[Y_AXIS], current_position[Z_AXIS], current_position[E_AXIS]); \
  473. destination[LETTER##_AXIS] = 1.5 * LETTER##_MAX_LENGTH * LETTER##_HOME_DIR; \
  474. feedrate = homing_feedrate[LETTER##_AXIS]; \
  475. plan_buffer_line(destination[X_AXIS], destination[Y_AXIS], destination[Z_AXIS], destination[E_AXIS], feedrate/60, active_extruder); \
  476. st_synchronize();\
  477. \
  478. current_position[LETTER##_AXIS] = 0;\
  479. plan_set_position(current_position[X_AXIS], current_position[Y_AXIS], current_position[Z_AXIS], current_position[E_AXIS]);\
  480. destination[LETTER##_AXIS] = -LETTER##_HOME_RETRACT_MM * LETTER##_HOME_DIR;\
  481. plan_buffer_line(destination[X_AXIS], destination[Y_AXIS], destination[Z_AXIS], destination[E_AXIS], feedrate/60, active_extruder); \
  482. st_synchronize();\
  483. \
  484. destination[LETTER##_AXIS] = 2*LETTER##_HOME_RETRACT_MM * LETTER##_HOME_DIR;\
  485. feedrate = homing_feedrate[LETTER##_AXIS]/2 ; \
  486. plan_buffer_line(destination[X_AXIS], destination[Y_AXIS], destination[Z_AXIS], destination[E_AXIS], feedrate/60, active_extruder); \
  487. st_synchronize();\
  488. \
  489. current_position[LETTER##_AXIS] = (LETTER##_HOME_DIR == -1) ? LETTER##_HOME_POS : LETTER##_MAX_LENGTH;\
  490. destination[LETTER##_AXIS] = current_position[LETTER##_AXIS];\
  491. feedrate = 0.0;\
  492. endstops_hit_on_purpose();\
  493. }
  494. void process_commands()
  495. {
  496. unsigned long codenum; //throw away variable
  497. char *starpos = NULL;
  498. if(code_seen('G'))
  499. {
  500. switch((int)code_value())
  501. {
  502. case 0: // G0 -> G1
  503. case 1: // G1
  504. if(Stopped == false) {
  505. get_coordinates(); // For X Y Z E F
  506. prepare_move();
  507. //ClearToSend();
  508. return;
  509. }
  510. //break;
  511. case 2: // G2 - CW ARC
  512. if(Stopped == false) {
  513. get_arc_coordinates();
  514. prepare_arc_move(true);
  515. return;
  516. }
  517. case 3: // G3 - CCW ARC
  518. if(Stopped == false) {
  519. get_arc_coordinates();
  520. prepare_arc_move(false);
  521. return;
  522. }
  523. case 4: // G4 dwell
  524. LCD_MESSAGEPGM(MSG_DWELL);
  525. codenum = 0;
  526. if(code_seen('P')) codenum = code_value(); // milliseconds to wait
  527. if(code_seen('S')) codenum = code_value() * 1000; // seconds to wait
  528. st_synchronize();
  529. codenum += millis(); // keep track of when we started waiting
  530. previous_millis_cmd = millis();
  531. while(millis() < codenum ){
  532. manage_heater();
  533. manage_inactivity(1);
  534. }
  535. break;
  536. case 28: //G28 Home all Axis one at a time
  537. saved_feedrate = feedrate;
  538. saved_feedmultiply = feedmultiply;
  539. feedmultiply = 100;
  540. previous_millis_cmd = millis();
  541. enable_endstops(true);
  542. for(int8_t i=0; i < NUM_AXIS; i++) {
  543. destination[i] = current_position[i];
  544. }
  545. feedrate = 0.0;
  546. home_all_axis = !((code_seen(axis_codes[0])) || (code_seen(axis_codes[1])) || (code_seen(axis_codes[2])));
  547. #ifdef QUICK_HOME
  548. if((home_all_axis)||( code_seen(axis_codes[X_AXIS]) && code_seen(axis_codes[Y_AXIS])) ) //first diagonal move
  549. {
  550. current_position[X_AXIS] = 0;current_position[Y_AXIS] = 0;
  551. plan_set_position(current_position[X_AXIS], current_position[Y_AXIS], current_position[Z_AXIS], current_position[E_AXIS]);
  552. destination[X_AXIS] = 1.5 * X_MAX_LENGTH * X_HOME_DIR;destination[Y_AXIS] = 1.5 * Y_MAX_LENGTH * Y_HOME_DIR;
  553. feedrate = homing_feedrate[X_AXIS];
  554. if(homing_feedrate[Y_AXIS]<feedrate)
  555. feedrate =homing_feedrate[Y_AXIS];
  556. plan_buffer_line(destination[X_AXIS], destination[Y_AXIS], destination[Z_AXIS], destination[E_AXIS], feedrate/60, active_extruder);
  557. st_synchronize();
  558. current_position[X_AXIS] = (X_HOME_DIR == -1) ? X_HOME_POS : X_MAX_LENGTH;
  559. current_position[Y_AXIS] = (Y_HOME_DIR == -1) ? Y_HOME_POS : Y_MAX_LENGTH;
  560. plan_set_position(current_position[X_AXIS], current_position[Y_AXIS], current_position[Z_AXIS], current_position[E_AXIS]);
  561. destination[X_AXIS] = current_position[X_AXIS];
  562. destination[Y_AXIS] = current_position[Y_AXIS];
  563. plan_buffer_line(destination[X_AXIS], destination[Y_AXIS], destination[Z_AXIS], destination[E_AXIS], feedrate/60, active_extruder);
  564. feedrate = 0.0;
  565. st_synchronize();
  566. endstops_hit_on_purpose();
  567. }
  568. #endif
  569. if((home_all_axis) || (code_seen(axis_codes[X_AXIS])))
  570. {
  571. HOMEAXIS(X);
  572. }
  573. if((home_all_axis) || (code_seen(axis_codes[Y_AXIS]))) {
  574. HOMEAXIS(Y);
  575. }
  576. if((home_all_axis) || (code_seen(axis_codes[Z_AXIS]))) {
  577. HOMEAXIS(Z);
  578. }
  579. if(code_seen(axis_codes[X_AXIS]))
  580. {
  581. if(code_value_long() != 0) {
  582. current_position[X_AXIS]=code_value()+add_homeing[0];
  583. }
  584. }
  585. if(code_seen(axis_codes[Y_AXIS])) {
  586. if(code_value_long() != 0) {
  587. current_position[Y_AXIS]=code_value()+add_homeing[1];
  588. }
  589. }
  590. if(code_seen(axis_codes[Z_AXIS])) {
  591. if(code_value_long() != 0) {
  592. current_position[Z_AXIS]=code_value()+add_homeing[2];
  593. }
  594. }
  595. plan_set_position(current_position[X_AXIS], current_position[Y_AXIS], current_position[Z_AXIS], current_position[E_AXIS]);
  596. #ifdef ENDSTOPS_ONLY_FOR_HOMING
  597. enable_endstops(false);
  598. #endif
  599. feedrate = saved_feedrate;
  600. feedmultiply = saved_feedmultiply;
  601. previous_millis_cmd = millis();
  602. endstops_hit_on_purpose();
  603. break;
  604. case 90: // G90
  605. relative_mode = false;
  606. break;
  607. case 91: // G91
  608. relative_mode = true;
  609. break;
  610. case 92: // G92
  611. if(!code_seen(axis_codes[E_AXIS]))
  612. st_synchronize();
  613. for(int8_t i=0; i < NUM_AXIS; i++) {
  614. if(code_seen(axis_codes[i])) {
  615. current_position[i] = code_value()+add_homeing[i];
  616. if(i == E_AXIS) {
  617. current_position[i] = code_value();
  618. plan_set_e_position(current_position[E_AXIS]);
  619. }
  620. else {
  621. current_position[i] = code_value()+add_homeing[i];
  622. plan_set_position(current_position[X_AXIS], current_position[Y_AXIS], current_position[Z_AXIS], current_position[E_AXIS]);
  623. }
  624. }
  625. }
  626. break;
  627. }
  628. }
  629. else if(code_seen('M'))
  630. {
  631. switch( (int)code_value() )
  632. {
  633. case 17:
  634. LCD_MESSAGEPGM(MSG_NO_MOVE);
  635. enable_x();
  636. enable_y();
  637. enable_z();
  638. enable_e0();
  639. enable_e1();
  640. enable_e2();
  641. break;
  642. #ifdef SDSUPPORT
  643. case 20: // M20 - list SD card
  644. SERIAL_PROTOCOLLNPGM(MSG_BEGIN_FILE_LIST);
  645. card.ls();
  646. SERIAL_PROTOCOLLNPGM(MSG_END_FILE_LIST);
  647. break;
  648. case 21: // M21 - init SD card
  649. card.initsd();
  650. break;
  651. case 22: //M22 - release SD card
  652. card.release();
  653. break;
  654. case 23: //M23 - Select file
  655. starpos = (strchr(strchr_pointer + 4,'*'));
  656. if(starpos!=NULL)
  657. *(starpos-1)='\0';
  658. card.openFile(strchr_pointer + 4,true);
  659. break;
  660. case 24: //M24 - Start SD print
  661. card.startFileprint();
  662. starttime=millis();
  663. break;
  664. case 25: //M25 - Pause SD print
  665. card.pauseSDPrint();
  666. break;
  667. case 26: //M26 - Set SD index
  668. if(card.cardOK && code_seen('S')) {
  669. card.setIndex(code_value_long());
  670. }
  671. break;
  672. case 27: //M27 - Get SD status
  673. card.getStatus();
  674. break;
  675. case 28: //M28 - Start SD write
  676. starpos = (strchr(strchr_pointer + 4,'*'));
  677. if(starpos != NULL){
  678. char* npos = strchr(cmdbuffer[bufindr], 'N');
  679. strchr_pointer = strchr(npos,' ') + 1;
  680. *(starpos-1) = '\0';
  681. }
  682. card.openFile(strchr_pointer+4,false);
  683. break;
  684. case 29: //M29 - Stop SD write
  685. //processed in write to file routine above
  686. //card,saving = false;
  687. break;
  688. case 30: //M30 <filename> Delete File
  689. if (card.cardOK){
  690. card.closefile();
  691. starpos = (strchr(strchr_pointer + 4,'*'));
  692. if(starpos != NULL){
  693. char* npos = strchr(cmdbuffer[bufindr], 'N');
  694. strchr_pointer = strchr(npos,' ') + 1;
  695. *(starpos-1) = '\0';
  696. }
  697. card.removeFile(strchr_pointer + 4);
  698. }
  699. break;
  700. #endif //SDSUPPORT
  701. case 31: //M31 take time since the start of the SD print or an M109 command
  702. {
  703. stoptime=millis();
  704. char time[30];
  705. unsigned long t=(stoptime-starttime)/1000;
  706. int sec,min;
  707. min=t/60;
  708. sec=t%60;
  709. sprintf(time,"%i min, %i sec",min,sec);
  710. SERIAL_ECHO_START;
  711. SERIAL_ECHOLN(time);
  712. LCD_MESSAGE(time);
  713. autotempShutdown();
  714. }
  715. break;
  716. case 42: //M42 -Change pin status via gcode
  717. if (code_seen('S'))
  718. {
  719. int pin_status = code_value();
  720. if (code_seen('P') && pin_status >= 0 && pin_status <= 255)
  721. {
  722. int pin_number = code_value();
  723. for(int8_t i = 0; i < (int8_t)sizeof(sensitive_pins); i++)
  724. {
  725. if (sensitive_pins[i] == pin_number)
  726. {
  727. pin_number = -1;
  728. break;
  729. }
  730. }
  731. if (pin_number > -1)
  732. {
  733. pinMode(pin_number, OUTPUT);
  734. digitalWrite(pin_number, pin_status);
  735. analogWrite(pin_number, pin_status);
  736. }
  737. }
  738. }
  739. break;
  740. case 104: // M104
  741. tmp_extruder = active_extruder;
  742. if(code_seen('T')) {
  743. tmp_extruder = code_value();
  744. if(tmp_extruder >= EXTRUDERS) {
  745. SERIAL_ECHO_START;
  746. SERIAL_ECHO(MSG_M104_INVALID_EXTRUDER);
  747. SERIAL_ECHOLN(tmp_extruder);
  748. break;
  749. }
  750. }
  751. if (code_seen('S')) setTargetHotend(code_value(), tmp_extruder);
  752. setWatch();
  753. break;
  754. case 140: // M140 set bed temp
  755. if (code_seen('S')) setTargetBed(code_value());
  756. break;
  757. case 105 : // M105
  758. tmp_extruder = active_extruder;
  759. if(code_seen('T')) {
  760. tmp_extruder = code_value();
  761. if(tmp_extruder >= EXTRUDERS) {
  762. SERIAL_ECHO_START;
  763. SERIAL_ECHO(MSG_M105_INVALID_EXTRUDER);
  764. SERIAL_ECHOLN(tmp_extruder);
  765. break;
  766. }
  767. }
  768. #if (TEMP_0_PIN > -1)
  769. SERIAL_PROTOCOLPGM("ok T:");
  770. SERIAL_PROTOCOL_F(degHotend(tmp_extruder),1);
  771. SERIAL_PROTOCOLPGM(" /");
  772. SERIAL_PROTOCOL_F(degTargetHotend(tmp_extruder),1);
  773. #if TEMP_BED_PIN > -1
  774. SERIAL_PROTOCOLPGM(" B:");
  775. SERIAL_PROTOCOL_F(degBed(),1);
  776. SERIAL_PROTOCOLPGM(" /");
  777. SERIAL_PROTOCOL_F(degTargetBed(),1);
  778. #endif //TEMP_BED_PIN
  779. #else
  780. SERIAL_ERROR_START;
  781. SERIAL_ERRORLNPGM(MSG_ERR_NO_THERMISTORS);
  782. #endif
  783. #ifdef PIDTEMP
  784. SERIAL_PROTOCOLPGM(" @:");
  785. SERIAL_PROTOCOL(getHeaterPower(tmp_extruder));
  786. #endif
  787. SERIAL_PROTOCOLLN("");
  788. return;
  789. break;
  790. case 109:
  791. {// M109 - Wait for extruder heater to reach target.
  792. tmp_extruder = active_extruder;
  793. if(code_seen('T')) {
  794. tmp_extruder = code_value();
  795. if(tmp_extruder >= EXTRUDERS) {
  796. SERIAL_ECHO_START;
  797. SERIAL_ECHO(MSG_M109_INVALID_EXTRUDER);
  798. SERIAL_ECHOLN(tmp_extruder);
  799. break;
  800. }
  801. }
  802. LCD_MESSAGEPGM(MSG_HEATING);
  803. #ifdef AUTOTEMP
  804. autotemp_enabled=false;
  805. #endif
  806. if (code_seen('S')) setTargetHotend(code_value(), tmp_extruder);
  807. #ifdef AUTOTEMP
  808. if (code_seen('S')) autotemp_min=code_value();
  809. if (code_seen('B')) autotemp_max=code_value();
  810. if (code_seen('F'))
  811. {
  812. autotemp_factor=code_value();
  813. autotemp_enabled=true;
  814. }
  815. #endif
  816. setWatch();
  817. codenum = millis();
  818. /* See if we are heating up or cooling down */
  819. bool target_direction = isHeatingHotend(tmp_extruder); // true if heating, false if cooling
  820. #ifdef TEMP_RESIDENCY_TIME
  821. long residencyStart;
  822. residencyStart = -1;
  823. /* continue to loop until we have reached the target temp
  824. _and_ until TEMP_RESIDENCY_TIME hasn't passed since we reached it */
  825. while((residencyStart == -1) ||
  826. (residencyStart >= 0 && (((unsigned int) (millis() - residencyStart)) < (TEMP_RESIDENCY_TIME * 1000UL))) ) {
  827. #else
  828. while ( target_direction ? (isHeatingHotend(tmp_extruder)) : (isCoolingHotend(tmp_extruder)&&(CooldownNoWait==false)) ) {
  829. #endif //TEMP_RESIDENCY_TIME
  830. if( (millis() - codenum) > 1000UL )
  831. { //Print Temp Reading and remaining time every 1 second while heating up/cooling down
  832. SERIAL_PROTOCOLPGM("T:");
  833. SERIAL_PROTOCOL_F(degHotend(tmp_extruder),1);
  834. SERIAL_PROTOCOLPGM(" E:");
  835. SERIAL_PROTOCOL((int)tmp_extruder);
  836. #ifdef TEMP_RESIDENCY_TIME
  837. SERIAL_PROTOCOLPGM(" W:");
  838. if(residencyStart > -1)
  839. {
  840. codenum = ((TEMP_RESIDENCY_TIME * 1000UL) - (millis() - residencyStart)) / 1000UL;
  841. SERIAL_PROTOCOLLN( codenum );
  842. }
  843. else
  844. {
  845. SERIAL_PROTOCOLLN( "?" );
  846. }
  847. #else
  848. SERIAL_PROTOCOLLN("");
  849. #endif
  850. codenum = millis();
  851. }
  852. manage_heater();
  853. manage_inactivity(1);
  854. LCD_STATUS;
  855. #ifdef TEMP_RESIDENCY_TIME
  856. /* start/restart the TEMP_RESIDENCY_TIME timer whenever we reach target temp for the first time
  857. or when current temp falls outside the hysteresis after target temp was reached */
  858. if ((residencyStart == -1 && target_direction && !isHeatingHotend(tmp_extruder)) ||
  859. (residencyStart == -1 && !target_direction && !isCoolingHotend(tmp_extruder)) ||
  860. (residencyStart > -1 && labs(degHotend(tmp_extruder) - degTargetHotend(tmp_extruder)) > TEMP_HYSTERESIS) )
  861. {
  862. residencyStart = millis();
  863. }
  864. #endif //TEMP_RESIDENCY_TIME
  865. }
  866. LCD_MESSAGEPGM(MSG_HEATING_COMPLETE);
  867. starttime=millis();
  868. previous_millis_cmd = millis();
  869. }
  870. break;
  871. case 190: // M190 - Wait for bed heater to reach target.
  872. #if TEMP_BED_PIN > -1
  873. LCD_MESSAGEPGM(MSG_BED_HEATING);
  874. if (code_seen('S')) setTargetBed(code_value());
  875. codenum = millis();
  876. while(isHeatingBed())
  877. {
  878. if(( millis() - codenum) > 1000 ) //Print Temp Reading every 1 second while heating up.
  879. {
  880. float tt=degHotend(active_extruder);
  881. SERIAL_PROTOCOLPGM("T:");
  882. SERIAL_PROTOCOL(tt);
  883. SERIAL_PROTOCOLPGM(" E:");
  884. SERIAL_PROTOCOL((int)active_extruder);
  885. SERIAL_PROTOCOLPGM(" B:");
  886. SERIAL_PROTOCOL_F(degBed(),1);
  887. SERIAL_PROTOCOLLN("");
  888. codenum = millis();
  889. }
  890. manage_heater();
  891. manage_inactivity(1);
  892. LCD_STATUS;
  893. }
  894. LCD_MESSAGEPGM(MSG_BED_DONE);
  895. previous_millis_cmd = millis();
  896. #endif
  897. break;
  898. #if FAN_PIN > -1
  899. case 106: //M106 Fan On
  900. if (code_seen('S')){
  901. FanSpeed=constrain(code_value(),0,255);
  902. }
  903. else {
  904. FanSpeed=255;
  905. }
  906. break;
  907. case 107: //M107 Fan Off
  908. FanSpeed = 0;
  909. break;
  910. #endif //FAN_PIN
  911. #if (PS_ON_PIN > -1)
  912. case 80: // M80 - ATX Power On
  913. SET_OUTPUT(PS_ON_PIN); //GND
  914. WRITE(PS_ON_PIN, LOW);
  915. break;
  916. #endif
  917. case 81: // M81 - ATX Power Off
  918. #if defined SUICIDE_PIN && SUICIDE_PIN > -1
  919. st_synchronize();
  920. suicide();
  921. #elif (PS_ON_PIN > -1)
  922. SET_INPUT(PS_ON_PIN); //Floating
  923. #endif
  924. break;
  925. case 82:
  926. axis_relative_modes[3] = false;
  927. break;
  928. case 83:
  929. axis_relative_modes[3] = true;
  930. break;
  931. case 18: //compatibility
  932. case 84: // M84
  933. if(code_seen('S')){
  934. stepper_inactive_time = code_value() * 1000;
  935. }
  936. else
  937. {
  938. bool all_axis = !((code_seen(axis_codes[0])) || (code_seen(axis_codes[1])) || (code_seen(axis_codes[2]))|| (code_seen(axis_codes[3])));
  939. if(all_axis)
  940. {
  941. st_synchronize();
  942. disable_e0();
  943. disable_e1();
  944. disable_e2();
  945. finishAndDisableSteppers();
  946. }
  947. else
  948. {
  949. st_synchronize();
  950. if(code_seen('X')) disable_x();
  951. if(code_seen('Y')) disable_y();
  952. if(code_seen('Z')) disable_z();
  953. #if ((E0_ENABLE_PIN != X_ENABLE_PIN) && (E1_ENABLE_PIN != Y_ENABLE_PIN)) // Only enable on boards that have seperate ENABLE_PINS
  954. if(code_seen('E')) {
  955. disable_e0();
  956. disable_e1();
  957. disable_e2();
  958. }
  959. #endif
  960. LCD_MESSAGEPGM(MSG_PART_RELEASE);
  961. }
  962. }
  963. break;
  964. case 85: // M85
  965. code_seen('S');
  966. max_inactive_time = code_value() * 1000;
  967. break;
  968. case 92: // M92
  969. for(int8_t i=0; i < NUM_AXIS; i++)
  970. {
  971. if(code_seen(axis_codes[i]))
  972. if(i == 3) { // E
  973. float value = code_value();
  974. if(value < 20.0) {
  975. float factor = axis_steps_per_unit[i] / value; // increase e constants if M92 E14 is given for netfab.
  976. max_e_jerk *= factor;
  977. max_feedrate[i] *= factor;
  978. axis_steps_per_sqr_second[i] *= factor;
  979. }
  980. axis_steps_per_unit[i] = value;
  981. }
  982. else {
  983. axis_steps_per_unit[i] = code_value();
  984. }
  985. }
  986. break;
  987. case 115: // M115
  988. SerialprintPGM(MSG_M115_REPORT);
  989. break;
  990. case 117: // M117 display message
  991. LCD_MESSAGE(cmdbuffer[bufindr]+5);
  992. break;
  993. case 114: // M114
  994. SERIAL_PROTOCOLPGM("X:");
  995. SERIAL_PROTOCOL(current_position[X_AXIS]);
  996. SERIAL_PROTOCOLPGM("Y:");
  997. SERIAL_PROTOCOL(current_position[Y_AXIS]);
  998. SERIAL_PROTOCOLPGM("Z:");
  999. SERIAL_PROTOCOL(current_position[Z_AXIS]);
  1000. SERIAL_PROTOCOLPGM("E:");
  1001. SERIAL_PROTOCOL(current_position[E_AXIS]);
  1002. SERIAL_PROTOCOLPGM(MSG_COUNT_X);
  1003. SERIAL_PROTOCOL(float(st_get_position(X_AXIS))/axis_steps_per_unit[X_AXIS]);
  1004. SERIAL_PROTOCOLPGM("Y:");
  1005. SERIAL_PROTOCOL(float(st_get_position(Y_AXIS))/axis_steps_per_unit[Y_AXIS]);
  1006. SERIAL_PROTOCOLPGM("Z:");
  1007. SERIAL_PROTOCOL(float(st_get_position(Z_AXIS))/axis_steps_per_unit[Z_AXIS]);
  1008. SERIAL_PROTOCOLLN("");
  1009. break;
  1010. case 120: // M120
  1011. enable_endstops(false) ;
  1012. break;
  1013. case 121: // M121
  1014. enable_endstops(true) ;
  1015. break;
  1016. case 119: // M119
  1017. #if (X_MIN_PIN > -1)
  1018. SERIAL_PROTOCOLPGM(MSG_X_MIN);
  1019. SERIAL_PROTOCOL(((READ(X_MIN_PIN)^X_ENDSTOPS_INVERTING)?"H ":"L "));
  1020. #endif
  1021. #if (X_MAX_PIN > -1)
  1022. SERIAL_PROTOCOLPGM(MSG_X_MAX);
  1023. SERIAL_PROTOCOL(((READ(X_MAX_PIN)^X_ENDSTOPS_INVERTING)?"H ":"L "));
  1024. #endif
  1025. #if (Y_MIN_PIN > -1)
  1026. SERIAL_PROTOCOLPGM(MSG_Y_MIN);
  1027. SERIAL_PROTOCOL(((READ(Y_MIN_PIN)^Y_ENDSTOPS_INVERTING)?"H ":"L "));
  1028. #endif
  1029. #if (Y_MAX_PIN > -1)
  1030. SERIAL_PROTOCOLPGM(MSG_Y_MAX);
  1031. SERIAL_PROTOCOL(((READ(Y_MAX_PIN)^Y_ENDSTOPS_INVERTING)?"H ":"L "));
  1032. #endif
  1033. #if (Z_MIN_PIN > -1)
  1034. SERIAL_PROTOCOLPGM(MSG_Z_MIN);
  1035. SERIAL_PROTOCOL(((READ(Z_MIN_PIN)^Z_ENDSTOPS_INVERTING)?"H ":"L "));
  1036. #endif
  1037. #if (Z_MAX_PIN > -1)
  1038. SERIAL_PROTOCOLPGM(MSG_Z_MAX);
  1039. SERIAL_PROTOCOL(((READ(Z_MAX_PIN)^Z_ENDSTOPS_INVERTING)?"H ":"L "));
  1040. #endif
  1041. SERIAL_PROTOCOLLN("");
  1042. break;
  1043. //TODO: update for all axis, use for loop
  1044. case 201: // M201
  1045. for(int8_t i=0; i < NUM_AXIS; i++)
  1046. {
  1047. if(code_seen(axis_codes[i]))
  1048. {
  1049. max_acceleration_units_per_sq_second[i] = code_value();
  1050. axis_steps_per_sqr_second[i] = code_value() * axis_steps_per_unit[i];
  1051. }
  1052. }
  1053. break;
  1054. #if 0 // Not used for Sprinter/grbl gen6
  1055. case 202: // M202
  1056. for(int8_t i=0; i < NUM_AXIS; i++) {
  1057. if(code_seen(axis_codes[i])) axis_travel_steps_per_sqr_second[i] = code_value() * axis_steps_per_unit[i];
  1058. }
  1059. break;
  1060. #endif
  1061. case 203: // M203 max feedrate mm/sec
  1062. for(int8_t i=0; i < NUM_AXIS; i++) {
  1063. if(code_seen(axis_codes[i])) max_feedrate[i] = code_value();
  1064. }
  1065. break;
  1066. case 204: // M204 acclereration S normal moves T filmanent only moves
  1067. {
  1068. if(code_seen('S')) acceleration = code_value() ;
  1069. if(code_seen('T')) retract_acceleration = code_value() ;
  1070. }
  1071. break;
  1072. 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
  1073. {
  1074. if(code_seen('S')) minimumfeedrate = code_value();
  1075. if(code_seen('T')) mintravelfeedrate = code_value();
  1076. if(code_seen('B')) minsegmenttime = code_value() ;
  1077. if(code_seen('X')) max_xy_jerk = code_value() ;
  1078. if(code_seen('Z')) max_z_jerk = code_value() ;
  1079. if(code_seen('E')) max_e_jerk = code_value() ;
  1080. }
  1081. break;
  1082. case 206: // M206 additional homeing offset
  1083. for(int8_t i=0; i < 3; i++)
  1084. {
  1085. if(code_seen(axis_codes[i])) add_homeing[i] = code_value();
  1086. }
  1087. break;
  1088. case 220: // M220 S<factor in percent>- set speed factor override percentage
  1089. {
  1090. if(code_seen('S'))
  1091. {
  1092. feedmultiply = code_value() ;
  1093. feedmultiplychanged=true;
  1094. }
  1095. }
  1096. break;
  1097. case 221: // M221 S<factor in percent>- set extrude factor override percentage
  1098. {
  1099. if(code_seen('S'))
  1100. {
  1101. extrudemultiply = code_value() ;
  1102. }
  1103. }
  1104. break;
  1105. #ifdef PIDTEMP
  1106. case 301: // M301
  1107. {
  1108. if(code_seen('P')) Kp = code_value();
  1109. if(code_seen('I')) Ki = code_value()*PID_dT;
  1110. if(code_seen('D')) Kd = code_value()/PID_dT;
  1111. #ifdef PID_ADD_EXTRUSION_RATE
  1112. if(code_seen('C')) Kc = code_value();
  1113. #endif
  1114. updatePID();
  1115. SERIAL_PROTOCOL(MSG_OK);
  1116. SERIAL_PROTOCOL(" p:");
  1117. SERIAL_PROTOCOL(Kp);
  1118. SERIAL_PROTOCOL(" i:");
  1119. SERIAL_PROTOCOL(Ki/PID_dT);
  1120. SERIAL_PROTOCOL(" d:");
  1121. SERIAL_PROTOCOL(Kd*PID_dT);
  1122. #ifdef PID_ADD_EXTRUSION_RATE
  1123. SERIAL_PROTOCOL(" c:");
  1124. SERIAL_PROTOCOL(Kc*PID_dT);
  1125. #endif
  1126. SERIAL_PROTOCOLLN("");
  1127. }
  1128. break;
  1129. #endif //PIDTEMP
  1130. case 240: // M240 Triggers a camera by emulating a Canon RC-1 : http://www.doc-diy.net/photo/rc-1_hacked/
  1131. {
  1132. #ifdef PHOTOGRAPH_PIN
  1133. #if (PHOTOGRAPH_PIN > -1)
  1134. const uint8_t NUM_PULSES=16;
  1135. const float PULSE_LENGTH=0.01524;
  1136. for(int i=0; i < NUM_PULSES; i++) {
  1137. WRITE(PHOTOGRAPH_PIN, HIGH);
  1138. _delay_ms(PULSE_LENGTH);
  1139. WRITE(PHOTOGRAPH_PIN, LOW);
  1140. _delay_ms(PULSE_LENGTH);
  1141. }
  1142. delay(7.33);
  1143. for(int i=0; i < NUM_PULSES; i++) {
  1144. WRITE(PHOTOGRAPH_PIN, HIGH);
  1145. _delay_ms(PULSE_LENGTH);
  1146. WRITE(PHOTOGRAPH_PIN, LOW);
  1147. _delay_ms(PULSE_LENGTH);
  1148. }
  1149. #endif
  1150. #endif
  1151. }
  1152. break;
  1153. case 302: // finish all moves
  1154. {
  1155. allow_cold_extrudes(true);
  1156. }
  1157. break;
  1158. case 303: // M303 PID autotune
  1159. {
  1160. float temp = 150.0;
  1161. if (code_seen('S')) temp=code_value();
  1162. PID_autotune(temp);
  1163. }
  1164. break;
  1165. case 400: // finish all moves
  1166. {
  1167. st_synchronize();
  1168. }
  1169. break;
  1170. case 500: // Store settings in EEPROM
  1171. {
  1172. EEPROM_StoreSettings();
  1173. }
  1174. break;
  1175. case 501: // Read settings from EEPROM
  1176. {
  1177. EEPROM_RetrieveSettings();
  1178. }
  1179. break;
  1180. case 502: // Revert to default settings
  1181. {
  1182. EEPROM_RetrieveSettings(true);
  1183. }
  1184. break;
  1185. case 503: // print settings currently in memory
  1186. {
  1187. EEPROM_printSettings();
  1188. }
  1189. break;
  1190. case 999: // Restart after being stopped
  1191. Stopped = false;
  1192. gcode_LastN = Stopped_gcode_LastN;
  1193. FlushSerialRequestResend();
  1194. break;
  1195. }
  1196. }
  1197. else if(code_seen('T'))
  1198. {
  1199. tmp_extruder = code_value();
  1200. if(tmp_extruder >= EXTRUDERS) {
  1201. SERIAL_ECHO_START;
  1202. SERIAL_ECHO("T");
  1203. SERIAL_ECHO(tmp_extruder);
  1204. SERIAL_ECHOLN(MSG_INVALID_EXTRUDER);
  1205. }
  1206. else {
  1207. active_extruder = tmp_extruder;
  1208. SERIAL_ECHO_START;
  1209. SERIAL_ECHO(MSG_ACTIVE_EXTRUDER);
  1210. SERIAL_PROTOCOLLN((int)active_extruder);
  1211. }
  1212. }
  1213. else
  1214. {
  1215. SERIAL_ECHO_START;
  1216. SERIAL_ECHOPGM(MSG_UNKNOWN_COMMAND);
  1217. SERIAL_ECHO(cmdbuffer[bufindr]);
  1218. SERIAL_ECHOLNPGM("\"");
  1219. }
  1220. ClearToSend();
  1221. }
  1222. void FlushSerialRequestResend()
  1223. {
  1224. //char cmdbuffer[bufindr][100]="Resend:";
  1225. MYSERIAL.flush();
  1226. SERIAL_PROTOCOLPGM(MSG_RESEND);
  1227. SERIAL_PROTOCOLLN(gcode_LastN + 1);
  1228. ClearToSend();
  1229. }
  1230. void ClearToSend()
  1231. {
  1232. previous_millis_cmd = millis();
  1233. #ifdef SDSUPPORT
  1234. if(fromsd[bufindr])
  1235. return;
  1236. #endif //SDSUPPORT
  1237. SERIAL_PROTOCOLLNPGM(MSG_OK);
  1238. }
  1239. void get_coordinates()
  1240. {
  1241. for(int8_t i=0; i < NUM_AXIS; i++) {
  1242. if(code_seen(axis_codes[i])) destination[i] = (float)code_value() + (axis_relative_modes[i] || relative_mode)*current_position[i];
  1243. else destination[i] = current_position[i]; //Are these else lines really needed?
  1244. }
  1245. if(code_seen('F')) {
  1246. next_feedrate = code_value();
  1247. if(next_feedrate > 0.0) feedrate = next_feedrate;
  1248. }
  1249. }
  1250. void get_arc_coordinates()
  1251. {
  1252. get_coordinates();
  1253. if(code_seen('I')) {
  1254. offset[0] = code_value();
  1255. }
  1256. else {
  1257. offset[0] = 0.0;
  1258. }
  1259. if(code_seen('J')) {
  1260. offset[1] = code_value();
  1261. }
  1262. else {
  1263. offset[1] = 0.0;
  1264. }
  1265. }
  1266. void prepare_move()
  1267. {
  1268. if (min_software_endstops) {
  1269. if (destination[X_AXIS] < X_HOME_POS) destination[X_AXIS] = X_HOME_POS;
  1270. if (destination[Y_AXIS] < Y_HOME_POS) destination[Y_AXIS] = Y_HOME_POS;
  1271. if (destination[Z_AXIS] < Z_HOME_POS) destination[Z_AXIS] = Z_HOME_POS;
  1272. }
  1273. if (max_software_endstops) {
  1274. if (destination[X_AXIS] > X_MAX_LENGTH) destination[X_AXIS] = X_MAX_LENGTH;
  1275. if (destination[Y_AXIS] > Y_MAX_LENGTH) destination[Y_AXIS] = Y_MAX_LENGTH;
  1276. if (destination[Z_AXIS] > Z_MAX_LENGTH) destination[Z_AXIS] = Z_MAX_LENGTH;
  1277. }
  1278. previous_millis_cmd = millis();
  1279. plan_buffer_line(destination[X_AXIS], destination[Y_AXIS], destination[Z_AXIS], destination[E_AXIS], feedrate*feedmultiply/60/100.0, active_extruder);
  1280. for(int8_t i=0; i < NUM_AXIS; i++) {
  1281. current_position[i] = destination[i];
  1282. }
  1283. }
  1284. void prepare_arc_move(char isclockwise) {
  1285. float r = hypot(offset[X_AXIS], offset[Y_AXIS]); // Compute arc radius for mc_arc
  1286. // Trace the arc
  1287. mc_arc(current_position, destination, offset, X_AXIS, Y_AXIS, Z_AXIS, feedrate*feedmultiply/60/100.0, r, isclockwise, active_extruder);
  1288. // As far as the parser is concerned, the position is now == target. In reality the
  1289. // motion control system might still be processing the action and the real tool position
  1290. // in any intermediate location.
  1291. for(int8_t i=0; i < NUM_AXIS; i++) {
  1292. current_position[i] = destination[i];
  1293. }
  1294. previous_millis_cmd = millis();
  1295. }
  1296. #ifdef CONTROLLERFAN_PIN
  1297. unsigned long lastMotor = 0; //Save the time for when a motor was turned on last
  1298. unsigned long lastMotorCheck = 0;
  1299. void controllerFan()
  1300. {
  1301. if ((millis() - lastMotorCheck) >= 2500) //Not a time critical function, so we only check every 2500ms
  1302. {
  1303. lastMotorCheck = millis();
  1304. if(!READ(X_ENABLE_PIN) || !READ(Y_ENABLE_PIN) || !READ(Z_ENABLE_PIN)
  1305. #if EXTRUDERS > 2
  1306. || !READ(E2_ENABLE_PIN)
  1307. #endif
  1308. #if EXTRUDER > 1
  1309. || !READ(E2_ENABLE_PIN)
  1310. #endif
  1311. || !READ(E0_ENABLE_PIN)) //If any of the drivers are enabled...
  1312. {
  1313. lastMotor = millis(); //... set time to NOW so the fan will turn on
  1314. }
  1315. if ((millis() - lastMotor) >= (CONTROLLERFAN_SEC*1000UL) || lastMotor == 0) //If the last time any driver was enabled, is longer since than CONTROLLERSEC...
  1316. {
  1317. WRITE(CONTROLLERFAN_PIN, LOW); //... turn the fan off
  1318. }
  1319. else
  1320. {
  1321. WRITE(CONTROLLERFAN_PIN, HIGH); //... turn the fan on
  1322. }
  1323. }
  1324. }
  1325. #endif
  1326. void manage_inactivity(byte debug)
  1327. {
  1328. if( (millis() - previous_millis_cmd) > max_inactive_time )
  1329. if(max_inactive_time)
  1330. kill();
  1331. if(stepper_inactive_time) {
  1332. if( (millis() - previous_millis_cmd) > stepper_inactive_time )
  1333. {
  1334. if(blocks_queued() == false) {
  1335. disable_x();
  1336. disable_y();
  1337. disable_z();
  1338. disable_e0();
  1339. disable_e1();
  1340. disable_e2();
  1341. }
  1342. }
  1343. }
  1344. #ifdef CONTROLLERFAN_PIN
  1345. controllerFan(); //Check if fan should be turned on to cool stepper drivers down
  1346. #endif
  1347. #ifdef EXTRUDER_RUNOUT_PREVENT
  1348. if( (millis() - previous_millis_cmd) > EXTRUDER_RUNOUT_SECONDS*1000 )
  1349. if(degHotend(active_extruder)>EXTRUDER_RUNOUT_MINTEMP)
  1350. {
  1351. bool oldstatus=READ(E0_ENABLE_PIN);
  1352. enable_e0();
  1353. float oldepos=current_position[E_AXIS];
  1354. float oldedes=destination[E_AXIS];
  1355. plan_buffer_line(current_position[X_AXIS], current_position[Y_AXIS], current_position[Z_AXIS],
  1356. current_position[E_AXIS]+EXTRUDER_RUNOUT_EXTRUDE*EXTRUDER_RUNOUT_ESTEPS/axis_steps_per_unit[E_AXIS],
  1357. EXTRUDER_RUNOUT_SPEED/60.*EXTRUDER_RUNOUT_ESTEPS/axis_steps_per_unit[E_AXIS], active_extruder);
  1358. current_position[E_AXIS]=oldepos;
  1359. destination[E_AXIS]=oldedes;
  1360. plan_set_e_position(oldepos);
  1361. previous_millis_cmd=millis();
  1362. st_synchronize();
  1363. WRITE(E0_ENABLE_PIN,oldstatus);
  1364. }
  1365. #endif
  1366. check_axes_activity();
  1367. }
  1368. void kill()
  1369. {
  1370. cli(); // Stop interrupts
  1371. disable_heater();
  1372. disable_x();
  1373. disable_y();
  1374. disable_z();
  1375. disable_e0();
  1376. disable_e1();
  1377. disable_e2();
  1378. if(PS_ON_PIN > -1) pinMode(PS_ON_PIN,INPUT);
  1379. SERIAL_ERROR_START;
  1380. SERIAL_ERRORLNPGM(MSG_ERR_KILLED);
  1381. LCD_MESSAGEPGM(MSG_KILLED);
  1382. suicide();
  1383. while(1); // Wait for reset
  1384. }
  1385. void Stop()
  1386. {
  1387. disable_heater();
  1388. if(Stopped == false) {
  1389. Stopped = true;
  1390. Stopped_gcode_LastN = gcode_LastN; // Save last g_code for restart
  1391. SERIAL_ERROR_START;
  1392. SERIAL_ERRORLNPGM(MSG_ERR_STOPPED);
  1393. LCD_MESSAGEPGM(MSG_STOPPED);
  1394. }
  1395. }
  1396. bool IsStopped() { return Stopped; };