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

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