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

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586
  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. }
  261. void loop()
  262. {
  263. if(buflen < (BUFSIZE-1))
  264. get_command();
  265. #ifdef SDSUPPORT
  266. card.checkautostart(false);
  267. #endif
  268. if(buflen)
  269. {
  270. #ifdef SDSUPPORT
  271. if(card.saving)
  272. {
  273. if(strstr(cmdbuffer[bufindr],"M29") == NULL)
  274. {
  275. card.write_command(cmdbuffer[bufindr]);
  276. SERIAL_PROTOCOLLNPGM(MSG_OK);
  277. }
  278. else
  279. {
  280. card.closefile();
  281. SERIAL_PROTOCOLLNPGM(MSG_FILE_SAVED);
  282. }
  283. }
  284. else
  285. {
  286. process_commands();
  287. }
  288. #else
  289. process_commands();
  290. #endif //SDSUPPORT
  291. buflen = (buflen-1);
  292. bufindr = (bufindr + 1)%BUFSIZE;
  293. }
  294. //check heater every n milliseconds
  295. manage_heater();
  296. manage_inactivity(1);
  297. checkHitEndstops();
  298. LCD_STATUS;
  299. }
  300. void get_command()
  301. {
  302. while( MYSERIAL.available() > 0 && buflen < BUFSIZE) {
  303. serial_char = MYSERIAL.read();
  304. if(serial_char == '\n' ||
  305. serial_char == '\r' ||
  306. (serial_char == ':' && comment_mode == false) ||
  307. serial_count >= (MAX_CMD_SIZE - 1) )
  308. {
  309. if(!serial_count) { //if empty line
  310. comment_mode = false; //for new command
  311. return;
  312. }
  313. cmdbuffer[bufindw][serial_count] = 0; //terminate string
  314. if(!comment_mode){
  315. comment_mode = false; //for new command
  316. fromsd[bufindw] = false;
  317. if(strstr(cmdbuffer[bufindw], "N") != NULL)
  318. {
  319. strchr_pointer = strchr(cmdbuffer[bufindw], 'N');
  320. gcode_N = (strtol(&cmdbuffer[bufindw][strchr_pointer - cmdbuffer[bufindw] + 1], NULL, 10));
  321. if(gcode_N != gcode_LastN+1 && (strstr(cmdbuffer[bufindw], "M110") == NULL) ) {
  322. SERIAL_ERROR_START;
  323. SERIAL_ERRORPGM(MSG_ERR_LINE_NO);
  324. SERIAL_ERRORLN(gcode_LastN);
  325. //Serial.println(gcode_N);
  326. FlushSerialRequestResend();
  327. serial_count = 0;
  328. return;
  329. }
  330. if(strstr(cmdbuffer[bufindw], "*") != NULL)
  331. {
  332. byte checksum = 0;
  333. byte count = 0;
  334. while(cmdbuffer[bufindw][count] != '*') checksum = checksum^cmdbuffer[bufindw][count++];
  335. strchr_pointer = strchr(cmdbuffer[bufindw], '*');
  336. if( (int)(strtod(&cmdbuffer[bufindw][strchr_pointer - cmdbuffer[bufindw] + 1], NULL)) != checksum) {
  337. SERIAL_ERROR_START;
  338. SERIAL_ERRORPGM(MSG_ERR_CHECKSUM_MISMATCH);
  339. SERIAL_ERRORLN(gcode_LastN);
  340. FlushSerialRequestResend();
  341. serial_count = 0;
  342. return;
  343. }
  344. //if no errors, continue parsing
  345. }
  346. else
  347. {
  348. SERIAL_ERROR_START;
  349. SERIAL_ERRORPGM(MSG_ERR_NO_CHECKSUM);
  350. SERIAL_ERRORLN(gcode_LastN);
  351. FlushSerialRequestResend();
  352. serial_count = 0;
  353. return;
  354. }
  355. gcode_LastN = gcode_N;
  356. //if no errors, continue parsing
  357. }
  358. else // if we don't receive 'N' but still see '*'
  359. {
  360. if((strstr(cmdbuffer[bufindw], "*") != NULL))
  361. {
  362. SERIAL_ERROR_START;
  363. SERIAL_ERRORPGM(MSG_ERR_NO_LINENUMBER_WITH_CHECKSUM);
  364. SERIAL_ERRORLN(gcode_LastN);
  365. serial_count = 0;
  366. return;
  367. }
  368. }
  369. if((strstr(cmdbuffer[bufindw], "G") != NULL)){
  370. strchr_pointer = strchr(cmdbuffer[bufindw], 'G');
  371. switch((int)((strtod(&cmdbuffer[bufindw][strchr_pointer - cmdbuffer[bufindw] + 1], NULL)))){
  372. case 0:
  373. case 1:
  374. case 2:
  375. case 3:
  376. if(Stopped == false) { // If printer is stopped by an error the G[0-3] codes are ignored.
  377. #ifdef SDSUPPORT
  378. if(card.saving)
  379. break;
  380. #endif //SDSUPPORT
  381. SERIAL_PROTOCOLLNPGM(MSG_OK);
  382. }
  383. else {
  384. SERIAL_ERRORLNPGM(MSG_ERR_STOPPED);
  385. LCD_MESSAGEPGM(MSG_STOPPED);
  386. }
  387. break;
  388. default:
  389. break;
  390. }
  391. }
  392. bufindw = (bufindw + 1)%BUFSIZE;
  393. buflen += 1;
  394. }
  395. serial_count = 0; //clear buffer
  396. }
  397. else
  398. {
  399. if(serial_char == ';') comment_mode = true;
  400. if(!comment_mode) cmdbuffer[bufindw][serial_count++] = serial_char;
  401. }
  402. }
  403. #ifdef SDSUPPORT
  404. if(!card.sdprinting || serial_count!=0){
  405. return;
  406. }
  407. while( !card.eof() && buflen < BUFSIZE) {
  408. int16_t n=card.get();
  409. serial_char = (char)n;
  410. if(serial_char == '\n' ||
  411. serial_char == '\r' ||
  412. (serial_char == ':' && comment_mode == false) ||
  413. serial_count >= (MAX_CMD_SIZE - 1)||n==-1)
  414. {
  415. if(card.eof()){
  416. SERIAL_PROTOCOLLNPGM(MSG_FILE_PRINTED);
  417. stoptime=millis();
  418. char time[30];
  419. unsigned long t=(stoptime-starttime)/1000;
  420. int sec,min;
  421. min=t/60;
  422. sec=t%60;
  423. sprintf(time,"%i min, %i sec",min,sec);
  424. SERIAL_ECHO_START;
  425. SERIAL_ECHOLN(time);
  426. LCD_MESSAGE(time);
  427. card.printingHasFinished();
  428. card.checkautostart(true);
  429. }
  430. if(!serial_count)
  431. {
  432. comment_mode = false; //for new command
  433. return; //if empty line
  434. }
  435. cmdbuffer[bufindw][serial_count] = 0; //terminate string
  436. // if(!comment_mode){
  437. fromsd[bufindw] = true;
  438. buflen += 1;
  439. bufindw = (bufindw + 1)%BUFSIZE;
  440. // }
  441. comment_mode = false; //for new command
  442. serial_count = 0; //clear buffer
  443. }
  444. else
  445. {
  446. if(serial_char == ';') comment_mode = true;
  447. if(!comment_mode) cmdbuffer[bufindw][serial_count++] = serial_char;
  448. }
  449. }
  450. #endif //SDSUPPORT
  451. }
  452. float code_value()
  453. {
  454. return (strtod(&cmdbuffer[bufindr][strchr_pointer - cmdbuffer[bufindr] + 1], NULL));
  455. }
  456. long code_value_long()
  457. {
  458. return (strtol(&cmdbuffer[bufindr][strchr_pointer - cmdbuffer[bufindr] + 1], NULL, 10));
  459. }
  460. bool code_seen(char code_string[]) //Return True if the string was found
  461. {
  462. return (strstr(cmdbuffer[bufindr], code_string) != NULL);
  463. }
  464. bool code_seen(char code)
  465. {
  466. strchr_pointer = strchr(cmdbuffer[bufindr], code);
  467. return (strchr_pointer != NULL); //Return True if a character was found
  468. }
  469. #define HOMEAXIS(LETTER) \
  470. if ((LETTER##_MIN_PIN > -1 && LETTER##_HOME_DIR==-1) || (LETTER##_MAX_PIN > -1 && LETTER##_HOME_DIR==1))\
  471. { \
  472. current_position[LETTER##_AXIS] = 0; \
  473. plan_set_position(current_position[X_AXIS], current_position[Y_AXIS], current_position[Z_AXIS], current_position[E_AXIS]); \
  474. destination[LETTER##_AXIS] = 1.5 * LETTER##_MAX_LENGTH * LETTER##_HOME_DIR; \
  475. feedrate = homing_feedrate[LETTER##_AXIS]; \
  476. plan_buffer_line(destination[X_AXIS], destination[Y_AXIS], destination[Z_AXIS], destination[E_AXIS], feedrate/60, active_extruder); \
  477. st_synchronize();\
  478. \
  479. current_position[LETTER##_AXIS] = 0;\
  480. plan_set_position(current_position[X_AXIS], current_position[Y_AXIS], current_position[Z_AXIS], current_position[E_AXIS]);\
  481. destination[LETTER##_AXIS] = -LETTER##_HOME_RETRACT_MM * LETTER##_HOME_DIR;\
  482. plan_buffer_line(destination[X_AXIS], destination[Y_AXIS], destination[Z_AXIS], destination[E_AXIS], feedrate/60, active_extruder); \
  483. st_synchronize();\
  484. \
  485. destination[LETTER##_AXIS] = 2*LETTER##_HOME_RETRACT_MM * LETTER##_HOME_DIR;\
  486. feedrate = homing_feedrate[LETTER##_AXIS]/2 ; \
  487. plan_buffer_line(destination[X_AXIS], destination[Y_AXIS], destination[Z_AXIS], destination[E_AXIS], feedrate/60, active_extruder); \
  488. st_synchronize();\
  489. \
  490. current_position[LETTER##_AXIS] = (LETTER##_HOME_DIR == -1) ? LETTER##_HOME_POS : LETTER##_MAX_LENGTH;\
  491. destination[LETTER##_AXIS] = current_position[LETTER##_AXIS];\
  492. feedrate = 0.0;\
  493. endstops_hit_on_purpose();\
  494. }
  495. void process_commands()
  496. {
  497. unsigned long codenum; //throw away variable
  498. char *starpos = NULL;
  499. if(code_seen('G'))
  500. {
  501. switch((int)code_value())
  502. {
  503. case 0: // G0 -> G1
  504. case 1: // G1
  505. if(Stopped == false) {
  506. get_coordinates(); // For X Y Z E F
  507. prepare_move();
  508. //ClearToSend();
  509. return;
  510. }
  511. //break;
  512. case 2: // G2 - CW ARC
  513. if(Stopped == false) {
  514. get_arc_coordinates();
  515. prepare_arc_move(true);
  516. return;
  517. }
  518. case 3: // G3 - CCW ARC
  519. if(Stopped == false) {
  520. get_arc_coordinates();
  521. prepare_arc_move(false);
  522. return;
  523. }
  524. case 4: // G4 dwell
  525. LCD_MESSAGEPGM(MSG_DWELL);
  526. codenum = 0;
  527. if(code_seen('P')) codenum = code_value(); // milliseconds to wait
  528. if(code_seen('S')) codenum = code_value() * 1000; // seconds to wait
  529. st_synchronize();
  530. codenum += millis(); // keep track of when we started waiting
  531. previous_millis_cmd = millis();
  532. while(millis() < codenum ){
  533. manage_heater();
  534. manage_inactivity(1);
  535. }
  536. break;
  537. case 28: //G28 Home all Axis one at a time
  538. saved_feedrate = feedrate;
  539. saved_feedmultiply = feedmultiply;
  540. feedmultiply = 100;
  541. previous_millis_cmd = millis();
  542. enable_endstops(true);
  543. for(int8_t i=0; i < NUM_AXIS; i++) {
  544. destination[i] = current_position[i];
  545. }
  546. feedrate = 0.0;
  547. home_all_axis = !((code_seen(axis_codes[0])) || (code_seen(axis_codes[1])) || (code_seen(axis_codes[2])));
  548. #ifdef QUICK_HOME
  549. if((home_all_axis)||( code_seen(axis_codes[X_AXIS]) && code_seen(axis_codes[Y_AXIS])) ) //first diagonal move
  550. {
  551. current_position[X_AXIS] = 0;current_position[Y_AXIS] = 0;
  552. plan_set_position(current_position[X_AXIS], current_position[Y_AXIS], current_position[Z_AXIS], current_position[E_AXIS]);
  553. destination[X_AXIS] = 1.5 * X_MAX_LENGTH * X_HOME_DIR;destination[Y_AXIS] = 1.5 * Y_MAX_LENGTH * Y_HOME_DIR;
  554. feedrate = homing_feedrate[X_AXIS];
  555. if(homing_feedrate[Y_AXIS]<feedrate)
  556. feedrate =homing_feedrate[Y_AXIS];
  557. plan_buffer_line(destination[X_AXIS], destination[Y_AXIS], destination[Z_AXIS], destination[E_AXIS], feedrate/60, active_extruder);
  558. st_synchronize();
  559. current_position[X_AXIS] = (X_HOME_DIR == -1) ? X_HOME_POS : X_MAX_LENGTH;
  560. current_position[Y_AXIS] = (Y_HOME_DIR == -1) ? Y_HOME_POS : Y_MAX_LENGTH;
  561. plan_set_position(current_position[X_AXIS], current_position[Y_AXIS], current_position[Z_AXIS], current_position[E_AXIS]);
  562. destination[X_AXIS] = current_position[X_AXIS];
  563. destination[Y_AXIS] = current_position[Y_AXIS];
  564. plan_buffer_line(destination[X_AXIS], destination[Y_AXIS], destination[Z_AXIS], destination[E_AXIS], feedrate/60, active_extruder);
  565. feedrate = 0.0;
  566. st_synchronize();
  567. endstops_hit_on_purpose();
  568. }
  569. #endif
  570. if((home_all_axis) || (code_seen(axis_codes[X_AXIS])))
  571. {
  572. HOMEAXIS(X);
  573. }
  574. if((home_all_axis) || (code_seen(axis_codes[Y_AXIS]))) {
  575. HOMEAXIS(Y);
  576. }
  577. if((home_all_axis) || (code_seen(axis_codes[Z_AXIS]))) {
  578. HOMEAXIS(Z);
  579. }
  580. if(code_seen(axis_codes[X_AXIS]))
  581. {
  582. if(code_value_long() != 0) {
  583. current_position[X_AXIS]=code_value()+add_homeing[0];
  584. }
  585. }
  586. if(code_seen(axis_codes[Y_AXIS])) {
  587. if(code_value_long() != 0) {
  588. current_position[Y_AXIS]=code_value()+add_homeing[1];
  589. }
  590. }
  591. if(code_seen(axis_codes[Z_AXIS])) {
  592. if(code_value_long() != 0) {
  593. current_position[Z_AXIS]=code_value()+add_homeing[2];
  594. }
  595. }
  596. plan_set_position(current_position[X_AXIS], current_position[Y_AXIS], current_position[Z_AXIS], current_position[E_AXIS]);
  597. #ifdef ENDSTOPS_ONLY_FOR_HOMING
  598. enable_endstops(false);
  599. #endif
  600. feedrate = saved_feedrate;
  601. feedmultiply = saved_feedmultiply;
  602. previous_millis_cmd = millis();
  603. endstops_hit_on_purpose();
  604. break;
  605. case 90: // G90
  606. relative_mode = false;
  607. break;
  608. case 91: // G91
  609. relative_mode = true;
  610. break;
  611. case 92: // G92
  612. if(!code_seen(axis_codes[E_AXIS]))
  613. st_synchronize();
  614. for(int8_t i=0; i < NUM_AXIS; i++) {
  615. if(code_seen(axis_codes[i])) {
  616. current_position[i] = code_value()+add_homeing[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: // finish all moves
  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