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

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