My Marlin configs for Fabrikator Mini and CTC i3 Pro B
Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

Marlin_main.cpp 58KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917
  1. /* -*- c++ -*- */
  2. /*
  3. Reprap firmware based on Sprinter and grbl.
  4. Copyright (C) 2011 Camiel Gubbels / Erik van der Zalm
  5. This program is free software: you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation, either version 3 of the License, or
  8. (at your option) any later version.
  9. This program is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with this program. If not, see <http://www.gnu.org/licenses/>.
  15. */
  16. /*
  17. This firmware is a mashup between Sprinter and grbl.
  18. (https://github.com/kliment/Sprinter)
  19. (https://github.com/simen/grbl/tree)
  20. It has preliminary support for Matthew Roberts advance algorithm
  21. http://reprap.org/pipermail/reprap-dev/2011-May/003323.html
  22. */
  23. #include <SPI.h>
  24. #include "Marlin.h"
  25. #include "ultralcd.h"
  26. #include "planner.h"
  27. #include "stepper.h"
  28. #include "temperature.h"
  29. #include "motion_control.h"
  30. #include "cardreader.h"
  31. #include "watchdog.h"
  32. #include "ConfigurationStore.h"
  33. #include "language.h"
  34. #include "pins_arduino.h"
  35. #define VERSION_STRING "1.0.0"
  36. // look here for descriptions of gcodes: http://linuxcnc.org/handbook/gcode/g-code.html
  37. // http://objects.reprap.org/wiki/Mendel_User_Manual:_RepRapGCodes
  38. //Implemented Codes
  39. //-------------------
  40. // G0 -> G1
  41. // G1 - Coordinated Movement X Y Z E
  42. // G2 - CW ARC
  43. // G3 - CCW ARC
  44. // G4 - Dwell S<seconds> or P<milliseconds>
  45. // G10 - retract filament according to settings of M207
  46. // G11 - retract recover filament according to settings of M208
  47. // G28 - Home all Axis
  48. // G90 - Use Absolute Coordinates
  49. // G91 - Use Relative Coordinates
  50. // G92 - Set current position to cordinates given
  51. //RepRap M Codes
  52. // M0 - Unconditional stop - Wait for user to press a button on the LCD (Only if ULTRA_LCD is enabled)
  53. // M1 - Same as M0
  54. // M104 - Set extruder target temp
  55. // M105 - Read current temp
  56. // M106 - Fan on
  57. // M107 - Fan off
  58. // M109 - Wait for extruder current temp to reach target temp.
  59. // M114 - Display current position
  60. //Custom M Codes
  61. // M17 - Enable/Power all stepper motors
  62. // M18 - Disable all stepper motors; same as M84
  63. // M20 - List SD card
  64. // M21 - Init SD card
  65. // M22 - Release SD card
  66. // M23 - Select SD file (M23 filename.g)
  67. // M24 - Start/resume SD print
  68. // M25 - Pause SD print
  69. // M26 - Set SD position in bytes (M26 S12345)
  70. // M27 - Report SD print status
  71. // M28 - Start SD write (M28 filename.g)
  72. // M29 - Stop SD write
  73. // M30 - Delete file from SD (M30 filename.g)
  74. // M31 - Output time since last M109 or SD card start to serial
  75. // M42 - Change pin status via gcode
  76. // M80 - Turn on Power Supply
  77. // M81 - Turn off Power Supply
  78. // M82 - Set E codes absolute (default)
  79. // M83 - Set E codes relative while in Absolute Coordinates (G90) mode
  80. // M84 - Disable steppers until next move,
  81. // or use S<seconds> to specify an inactivity timeout, after which the steppers will be disabled. S0 to disable the timeout.
  82. // M85 - Set inactivity shutdown timer with parameter S<seconds>. To disable set zero (default)
  83. // M92 - Set axis_steps_per_unit - same syntax as G92
  84. // M114 - Output current position to serial port
  85. // M115 - Capabilities string
  86. // M117 - display message
  87. // M119 - Output Endstop status to serial port
  88. // M140 - Set bed target temp
  89. // M190 - Wait for bed current temp to reach target temp.
  90. // M200 - Set filament diameter
  91. // M201 - Set max acceleration in units/s^2 for print moves (M201 X1000 Y1000)
  92. // M202 - Set max acceleration in units/s^2 for travel moves (M202 X1000 Y1000) Unused in Marlin!!
  93. // M203 - Set maximum feedrate that your machine can sustain (M203 X200 Y200 Z300 E10000) in mm/sec
  94. // 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
  95. // 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
  96. // M206 - set additional homeing offset
  97. // M207 - set retract length S[positive mm] F[feedrate mm/sec] Z[additional zlift/hop]
  98. // M208 - set recover=unretract length S[positive mm surplus to the M207 S*] F[feedrate mm/sec]
  99. // M209 - S<1=true/0=false> enable automatic retract detect if the slicer did not support G10/11: every normal extrude-only move will be classified as retract depending on the direction.
  100. // M220 S<factor in percent>- set speed factor override percentage
  101. // M221 S<factor in percent>- set extrude factor override percentage
  102. // M240 - Trigger a camera to take a photograph
  103. // M301 - Set PID parameters P I and D
  104. // M302 - Allow cold extrudes
  105. // M303 - PID relay autotune S<temperature> sets the target temperature. (default target temperature = 150C)
  106. // M304 - Set bed PID parameters P I and D
  107. // M400 - Finish all moves
  108. // M500 - stores paramters in EEPROM
  109. // M501 - reads parameters from EEPROM (if you need reset them after you changed them temporarily).
  110. // M502 - reverts to the default "factory settings". You still need to store them in EEPROM afterwards if you want to.
  111. // M503 - print the current settings (from memory not from eeprom)
  112. // M907 - Set digital trimpot motor current using axis codes.
  113. // M908 - Control digital trimpot directly.
  114. // M350 - Set microstepping mode.
  115. // M351 - Toggle MS1 MS2 pins directly.
  116. // M999 - Restart after being stopped by error
  117. //Stepper Movement Variables
  118. //===========================================================================
  119. //=============================imported variables============================
  120. //===========================================================================
  121. //===========================================================================
  122. //=============================public variables=============================
  123. //===========================================================================
  124. #ifdef SDSUPPORT
  125. CardReader card;
  126. #endif
  127. float homing_feedrate[] = HOMING_FEEDRATE;
  128. bool axis_relative_modes[] = AXIS_RELATIVE_MODES;
  129. volatile int feedmultiply=100; //100->1 200->2
  130. int saved_feedmultiply;
  131. volatile bool feedmultiplychanged=false;
  132. volatile int extrudemultiply=100; //100->1 200->2
  133. float current_position[NUM_AXIS] = { 0.0, 0.0, 0.0, 0.0 };
  134. float add_homeing[3]={0,0,0};
  135. float min_pos[3] = { X_MIN_POS, Y_MIN_POS, Z_MIN_POS };
  136. float max_pos[3] = { X_MAX_POS, Y_MAX_POS, Z_MAX_POS };
  137. uint8_t active_extruder = 0;
  138. unsigned char FanSpeed=0;
  139. #ifdef FWRETRACT
  140. bool autoretract_enabled=true;
  141. bool retracted=false;
  142. float retract_length=3, retract_feedrate=17*60, retract_zlift=0.8;
  143. float retract_recover_length=0, retract_recover_feedrate=8*60;
  144. #endif
  145. //===========================================================================
  146. //=============================private variables=============================
  147. //===========================================================================
  148. const char axis_codes[NUM_AXIS] = {'X', 'Y', 'Z', 'E'};
  149. static float destination[NUM_AXIS] = { 0.0, 0.0, 0.0, 0.0};
  150. static float offset[3] = {0.0, 0.0, 0.0};
  151. static bool home_all_axis = true;
  152. static float feedrate = 1500.0, next_feedrate, saved_feedrate;
  153. static long gcode_N, gcode_LastN, Stopped_gcode_LastN = 0;
  154. static bool relative_mode = false; //Determines Absolute or Relative Coordinates
  155. static char cmdbuffer[BUFSIZE][MAX_CMD_SIZE];
  156. static bool fromsd[BUFSIZE];
  157. static int bufindr = 0;
  158. static int bufindw = 0;
  159. static int buflen = 0;
  160. //static int i = 0;
  161. static char serial_char;
  162. static int serial_count = 0;
  163. static boolean comment_mode = false;
  164. static char *strchr_pointer; // just a pointer to find chars in the cmd string like X, Y, Z, E, etc
  165. const int sensitive_pins[] = SENSITIVE_PINS; // Sensitive pin list for M42
  166. //static float tt = 0;
  167. //static float bt = 0;
  168. //Inactivity shutdown variables
  169. static unsigned long previous_millis_cmd = 0;
  170. static unsigned long max_inactive_time = 0;
  171. static unsigned long stepper_inactive_time = DEFAULT_STEPPER_DEACTIVE_TIME*1000l;
  172. unsigned long starttime=0;
  173. unsigned long stoptime=0;
  174. static uint8_t tmp_extruder;
  175. bool Stopped=false;
  176. //===========================================================================
  177. //=============================ROUTINES=============================
  178. //===========================================================================
  179. void get_arc_coordinates();
  180. bool setTargetedHotend(int code);
  181. void serial_echopair_P(const char *s_P, float v)
  182. { serialprintPGM(s_P); SERIAL_ECHO(v); }
  183. void serial_echopair_P(const char *s_P, double v)
  184. { serialprintPGM(s_P); SERIAL_ECHO(v); }
  185. void serial_echopair_P(const char *s_P, unsigned long v)
  186. { serialprintPGM(s_P); SERIAL_ECHO(v); }
  187. extern "C"{
  188. extern unsigned int __bss_end;
  189. extern unsigned int __heap_start;
  190. extern void *__brkval;
  191. int freeMemory() {
  192. int free_memory;
  193. if((int)__brkval == 0)
  194. free_memory = ((int)&free_memory) - ((int)&__bss_end);
  195. else
  196. free_memory = ((int)&free_memory) - ((int)__brkval);
  197. return free_memory;
  198. }
  199. }
  200. //adds an command to the main command buffer
  201. //thats really done in a non-safe way.
  202. //needs overworking someday
  203. void enquecommand(const char *cmd)
  204. {
  205. if(buflen < BUFSIZE)
  206. {
  207. //this is dangerous if a mixing of serial and this happsens
  208. strcpy(&(cmdbuffer[bufindw][0]),cmd);
  209. SERIAL_ECHO_START;
  210. SERIAL_ECHOPGM("enqueing \"");
  211. SERIAL_ECHO(cmdbuffer[bufindw]);
  212. SERIAL_ECHOLNPGM("\"");
  213. bufindw= (bufindw + 1)%BUFSIZE;
  214. buflen += 1;
  215. }
  216. }
  217. void setup_killpin()
  218. {
  219. #if( KILL_PIN>-1 )
  220. pinMode(KILL_PIN,INPUT);
  221. WRITE(KILL_PIN,HIGH);
  222. #endif
  223. }
  224. void setup_photpin()
  225. {
  226. #ifdef PHOTOGRAPH_PIN
  227. #if (PHOTOGRAPH_PIN > -1)
  228. SET_OUTPUT(PHOTOGRAPH_PIN);
  229. WRITE(PHOTOGRAPH_PIN, LOW);
  230. #endif
  231. #endif
  232. }
  233. void setup_powerhold()
  234. {
  235. #ifdef SUICIDE_PIN
  236. #if (SUICIDE_PIN> -1)
  237. SET_OUTPUT(SUICIDE_PIN);
  238. WRITE(SUICIDE_PIN, HIGH);
  239. #endif
  240. #endif
  241. }
  242. void suicide()
  243. {
  244. #ifdef SUICIDE_PIN
  245. #if (SUICIDE_PIN> -1)
  246. SET_OUTPUT(SUICIDE_PIN);
  247. WRITE(SUICIDE_PIN, LOW);
  248. #endif
  249. #endif
  250. }
  251. void setup()
  252. {
  253. setup_killpin();
  254. setup_powerhold();
  255. MYSERIAL.begin(BAUDRATE);
  256. SERIAL_PROTOCOLLNPGM("start");
  257. SERIAL_ECHO_START;
  258. // Check startup - does nothing if bootloader sets MCUSR to 0
  259. byte mcu = MCUSR;
  260. if(mcu & 1) SERIAL_ECHOLNPGM(MSG_POWERUP);
  261. if(mcu & 2) SERIAL_ECHOLNPGM(MSG_EXTERNAL_RESET);
  262. if(mcu & 4) SERIAL_ECHOLNPGM(MSG_BROWNOUT_RESET);
  263. if(mcu & 8) SERIAL_ECHOLNPGM(MSG_WATCHDOG_RESET);
  264. if(mcu & 32) SERIAL_ECHOLNPGM(MSG_SOFTWARE_RESET);
  265. MCUSR=0;
  266. SERIAL_ECHOPGM(MSG_MARLIN);
  267. SERIAL_ECHOLNPGM(VERSION_STRING);
  268. #ifdef STRING_VERSION_CONFIG_H
  269. #ifdef STRING_CONFIG_H_AUTHOR
  270. SERIAL_ECHO_START;
  271. SERIAL_ECHOPGM(MSG_CONFIGURATION_VER);
  272. SERIAL_ECHOPGM(STRING_VERSION_CONFIG_H);
  273. SERIAL_ECHOPGM(MSG_AUTHOR);
  274. SERIAL_ECHOLNPGM(STRING_CONFIG_H_AUTHOR);
  275. SERIAL_ECHOPGM("Compiled: ");
  276. SERIAL_ECHOLNPGM(__DATE__);
  277. #endif
  278. #endif
  279. SERIAL_ECHO_START;
  280. SERIAL_ECHOPGM(MSG_FREE_MEMORY);
  281. SERIAL_ECHO(freeMemory());
  282. SERIAL_ECHOPGM(MSG_PLANNER_BUFFER_BYTES);
  283. SERIAL_ECHOLN((int)sizeof(block_t)*BLOCK_BUFFER_SIZE);
  284. for(int8_t i = 0; i < BUFSIZE; i++)
  285. {
  286. fromsd[i] = false;
  287. }
  288. Config_RetrieveSettings(); // loads data from EEPROM if available
  289. for(int8_t i=0; i < NUM_AXIS; i++)
  290. {
  291. axis_steps_per_sqr_second[i] = max_acceleration_units_per_sq_second[i] * axis_steps_per_unit[i];
  292. }
  293. tp_init(); // Initialize temperature loop
  294. plan_init(); // Initialize planner;
  295. watchdog_init();
  296. st_init(); // Initialize stepper, this enables interrupts!
  297. setup_photpin();
  298. LCD_INIT;
  299. }
  300. void loop()
  301. {
  302. if(buflen < (BUFSIZE-1))
  303. get_command();
  304. #ifdef SDSUPPORT
  305. card.checkautostart(false);
  306. #endif
  307. if(buflen)
  308. {
  309. #ifdef SDSUPPORT
  310. if(card.saving)
  311. {
  312. if(strstr(cmdbuffer[bufindr],"M29") == NULL)
  313. {
  314. card.write_command(cmdbuffer[bufindr]);
  315. SERIAL_PROTOCOLLNPGM(MSG_OK);
  316. }
  317. else
  318. {
  319. card.closefile();
  320. SERIAL_PROTOCOLLNPGM(MSG_FILE_SAVED);
  321. }
  322. }
  323. else
  324. {
  325. process_commands();
  326. }
  327. #else
  328. process_commands();
  329. #endif //SDSUPPORT
  330. buflen = (buflen-1);
  331. bufindr = (bufindr + 1)%BUFSIZE;
  332. }
  333. //check heater every n milliseconds
  334. manage_heater();
  335. manage_inactivity();
  336. checkHitEndstops();
  337. LCD_STATUS;
  338. }
  339. void get_command()
  340. {
  341. while( MYSERIAL.available() > 0 && buflen < BUFSIZE) {
  342. serial_char = MYSERIAL.read();
  343. if(serial_char == '\n' ||
  344. serial_char == '\r' ||
  345. (serial_char == ':' && comment_mode == false) ||
  346. serial_count >= (MAX_CMD_SIZE - 1) )
  347. {
  348. if(!serial_count) { //if empty line
  349. comment_mode = false; //for new command
  350. return;
  351. }
  352. cmdbuffer[bufindw][serial_count] = 0; //terminate string
  353. if(!comment_mode){
  354. comment_mode = false; //for new command
  355. fromsd[bufindw] = false;
  356. if(strstr(cmdbuffer[bufindw], "N") != NULL)
  357. {
  358. strchr_pointer = strchr(cmdbuffer[bufindw], 'N');
  359. gcode_N = (strtol(&cmdbuffer[bufindw][strchr_pointer - cmdbuffer[bufindw] + 1], NULL, 10));
  360. if(gcode_N != gcode_LastN+1 && (strstr(cmdbuffer[bufindw], "M110") == NULL) ) {
  361. SERIAL_ERROR_START;
  362. SERIAL_ERRORPGM(MSG_ERR_LINE_NO);
  363. SERIAL_ERRORLN(gcode_LastN);
  364. //Serial.println(gcode_N);
  365. FlushSerialRequestResend();
  366. serial_count = 0;
  367. return;
  368. }
  369. if(strstr(cmdbuffer[bufindw], "*") != NULL)
  370. {
  371. byte checksum = 0;
  372. byte count = 0;
  373. while(cmdbuffer[bufindw][count] != '*') checksum = checksum^cmdbuffer[bufindw][count++];
  374. strchr_pointer = strchr(cmdbuffer[bufindw], '*');
  375. if( (int)(strtod(&cmdbuffer[bufindw][strchr_pointer - cmdbuffer[bufindw] + 1], NULL)) != checksum) {
  376. SERIAL_ERROR_START;
  377. SERIAL_ERRORPGM(MSG_ERR_CHECKSUM_MISMATCH);
  378. SERIAL_ERRORLN(gcode_LastN);
  379. FlushSerialRequestResend();
  380. serial_count = 0;
  381. return;
  382. }
  383. //if no errors, continue parsing
  384. }
  385. else
  386. {
  387. SERIAL_ERROR_START;
  388. SERIAL_ERRORPGM(MSG_ERR_NO_CHECKSUM);
  389. SERIAL_ERRORLN(gcode_LastN);
  390. FlushSerialRequestResend();
  391. serial_count = 0;
  392. return;
  393. }
  394. gcode_LastN = gcode_N;
  395. //if no errors, continue parsing
  396. }
  397. else // if we don't receive 'N' but still see '*'
  398. {
  399. if((strstr(cmdbuffer[bufindw], "*") != NULL))
  400. {
  401. SERIAL_ERROR_START;
  402. SERIAL_ERRORPGM(MSG_ERR_NO_LINENUMBER_WITH_CHECKSUM);
  403. SERIAL_ERRORLN(gcode_LastN);
  404. serial_count = 0;
  405. return;
  406. }
  407. }
  408. if((strstr(cmdbuffer[bufindw], "G") != NULL)){
  409. strchr_pointer = strchr(cmdbuffer[bufindw], 'G');
  410. switch((int)((strtod(&cmdbuffer[bufindw][strchr_pointer - cmdbuffer[bufindw] + 1], NULL)))){
  411. case 0:
  412. case 1:
  413. case 2:
  414. case 3:
  415. if(Stopped == false) { // If printer is stopped by an error the G[0-3] codes are ignored.
  416. #ifdef SDSUPPORT
  417. if(card.saving)
  418. break;
  419. #endif //SDSUPPORT
  420. SERIAL_PROTOCOLLNPGM(MSG_OK);
  421. }
  422. else {
  423. SERIAL_ERRORLNPGM(MSG_ERR_STOPPED);
  424. LCD_MESSAGEPGM(MSG_STOPPED);
  425. }
  426. break;
  427. default:
  428. break;
  429. }
  430. }
  431. bufindw = (bufindw + 1)%BUFSIZE;
  432. buflen += 1;
  433. }
  434. serial_count = 0; //clear buffer
  435. }
  436. else
  437. {
  438. if(serial_char == ';') comment_mode = true;
  439. if(!comment_mode) cmdbuffer[bufindw][serial_count++] = serial_char;
  440. }
  441. }
  442. #ifdef SDSUPPORT
  443. if(!card.sdprinting || serial_count!=0){
  444. return;
  445. }
  446. while( !card.eof() && buflen < BUFSIZE) {
  447. int16_t n=card.get();
  448. serial_char = (char)n;
  449. if(serial_char == '\n' ||
  450. serial_char == '\r' ||
  451. (serial_char == ':' && comment_mode == false) ||
  452. serial_count >= (MAX_CMD_SIZE - 1)||n==-1)
  453. {
  454. if(card.eof()){
  455. SERIAL_PROTOCOLLNPGM(MSG_FILE_PRINTED);
  456. stoptime=millis();
  457. char time[30];
  458. unsigned long t=(stoptime-starttime)/1000;
  459. int sec,min;
  460. min=t/60;
  461. sec=t%60;
  462. sprintf(time,"%i min, %i sec",min,sec);
  463. SERIAL_ECHO_START;
  464. SERIAL_ECHOLN(time);
  465. LCD_MESSAGE(time);
  466. card.printingHasFinished();
  467. card.checkautostart(true);
  468. }
  469. if(!serial_count)
  470. {
  471. comment_mode = false; //for new command
  472. return; //if empty line
  473. }
  474. cmdbuffer[bufindw][serial_count] = 0; //terminate string
  475. // if(!comment_mode){
  476. fromsd[bufindw] = true;
  477. buflen += 1;
  478. bufindw = (bufindw + 1)%BUFSIZE;
  479. // }
  480. comment_mode = false; //for new command
  481. serial_count = 0; //clear buffer
  482. }
  483. else
  484. {
  485. if(serial_char == ';') comment_mode = true;
  486. if(!comment_mode) cmdbuffer[bufindw][serial_count++] = serial_char;
  487. }
  488. }
  489. #endif //SDSUPPORT
  490. }
  491. float code_value()
  492. {
  493. return (strtod(&cmdbuffer[bufindr][strchr_pointer - cmdbuffer[bufindr] + 1], NULL));
  494. }
  495. long code_value_long()
  496. {
  497. return (strtol(&cmdbuffer[bufindr][strchr_pointer - cmdbuffer[bufindr] + 1], NULL, 10));
  498. }
  499. bool code_seen(char code_string[]) //Return True if the string was found
  500. {
  501. return (strstr(cmdbuffer[bufindr], code_string) != NULL);
  502. }
  503. bool code_seen(char code)
  504. {
  505. strchr_pointer = strchr(cmdbuffer[bufindr], code);
  506. return (strchr_pointer != NULL); //Return True if a character was found
  507. }
  508. #define DEFINE_PGM_READ_ANY(type, reader) \
  509. static inline type pgm_read_any(const type *p) \
  510. { return pgm_read_##reader##_near(p); }
  511. DEFINE_PGM_READ_ANY(float, float);
  512. DEFINE_PGM_READ_ANY(signed char, byte);
  513. #define XYZ_CONSTS_FROM_CONFIG(type, array, CONFIG) \
  514. static const PROGMEM type array##_P[3] = \
  515. { X_##CONFIG, Y_##CONFIG, Z_##CONFIG }; \
  516. static inline type array(int axis) \
  517. { return pgm_read_any(&array##_P[axis]); }
  518. XYZ_CONSTS_FROM_CONFIG(float, base_min_pos, MIN_POS);
  519. XYZ_CONSTS_FROM_CONFIG(float, base_max_pos, MAX_POS);
  520. XYZ_CONSTS_FROM_CONFIG(float, base_home_pos, HOME_POS);
  521. XYZ_CONSTS_FROM_CONFIG(float, max_length, MAX_LENGTH);
  522. XYZ_CONSTS_FROM_CONFIG(float, home_retract_mm, HOME_RETRACT_MM);
  523. XYZ_CONSTS_FROM_CONFIG(signed char, home_dir, HOME_DIR);
  524. static void axis_is_at_home(int axis) {
  525. current_position[axis] = base_home_pos(axis) + add_homeing[axis];
  526. min_pos[axis] = base_min_pos(axis) + add_homeing[axis];
  527. max_pos[axis] = base_max_pos(axis) + add_homeing[axis];
  528. }
  529. static void homeaxis(int axis) {
  530. #define HOMEAXIS_DO(LETTER) \
  531. ((LETTER##_MIN_PIN > -1 && LETTER##_HOME_DIR==-1) || (LETTER##_MAX_PIN > -1 && LETTER##_HOME_DIR==1))
  532. if (axis==X_AXIS ? HOMEAXIS_DO(X) :
  533. axis==Y_AXIS ? HOMEAXIS_DO(Y) :
  534. axis==Z_AXIS ? HOMEAXIS_DO(Z) :
  535. 0) {
  536. current_position[axis] = 0;
  537. plan_set_position(current_position[X_AXIS], current_position[Y_AXIS], current_position[Z_AXIS], current_position[E_AXIS]);
  538. destination[axis] = 1.5 * max_length(axis) * home_dir(axis);
  539. feedrate = homing_feedrate[axis];
  540. plan_buffer_line(destination[X_AXIS], destination[Y_AXIS], destination[Z_AXIS], destination[E_AXIS], feedrate/60, active_extruder);
  541. st_synchronize();
  542. current_position[axis] = 0;
  543. plan_set_position(current_position[X_AXIS], current_position[Y_AXIS], current_position[Z_AXIS], current_position[E_AXIS]);
  544. destination[axis] = -home_retract_mm(axis) * home_dir(axis);
  545. plan_buffer_line(destination[X_AXIS], destination[Y_AXIS], destination[Z_AXIS], destination[E_AXIS], feedrate/60, active_extruder);
  546. st_synchronize();
  547. destination[axis] = 2*home_retract_mm(axis) * home_dir(axis);
  548. feedrate = homing_feedrate[axis]/2 ;
  549. plan_buffer_line(destination[X_AXIS], destination[Y_AXIS], destination[Z_AXIS], destination[E_AXIS], feedrate/60, active_extruder);
  550. st_synchronize();
  551. axis_is_at_home(axis);
  552. destination[axis] = current_position[axis];
  553. feedrate = 0.0;
  554. endstops_hit_on_purpose();
  555. }
  556. }
  557. #define HOMEAXIS(LETTER) homeaxis(LETTER##_AXIS)
  558. void process_commands()
  559. {
  560. unsigned long codenum; //throw away variable
  561. char *starpos = NULL;
  562. if(code_seen('G'))
  563. {
  564. switch((int)code_value())
  565. {
  566. case 0: // G0 -> G1
  567. case 1: // G1
  568. if(Stopped == false) {
  569. get_coordinates(); // For X Y Z E F
  570. prepare_move();
  571. //ClearToSend();
  572. return;
  573. }
  574. //break;
  575. case 2: // G2 - CW ARC
  576. if(Stopped == false) {
  577. get_arc_coordinates();
  578. prepare_arc_move(true);
  579. return;
  580. }
  581. case 3: // G3 - CCW ARC
  582. if(Stopped == false) {
  583. get_arc_coordinates();
  584. prepare_arc_move(false);
  585. return;
  586. }
  587. case 4: // G4 dwell
  588. LCD_MESSAGEPGM(MSG_DWELL);
  589. codenum = 0;
  590. if(code_seen('P')) codenum = code_value(); // milliseconds to wait
  591. if(code_seen('S')) codenum = code_value() * 1000; // seconds to wait
  592. st_synchronize();
  593. codenum += millis(); // keep track of when we started waiting
  594. previous_millis_cmd = millis();
  595. while(millis() < codenum ){
  596. manage_heater();
  597. manage_inactivity();
  598. LCD_STATUS;
  599. }
  600. break;
  601. #ifdef FWRETRACT
  602. case 10: // G10 retract
  603. if(!retracted)
  604. {
  605. destination[X_AXIS]=current_position[X_AXIS];
  606. destination[Y_AXIS]=current_position[Y_AXIS];
  607. destination[Z_AXIS]=current_position[Z_AXIS];
  608. current_position[Z_AXIS]+=-retract_zlift;
  609. destination[E_AXIS]=current_position[E_AXIS]-retract_length;
  610. feedrate=retract_feedrate;
  611. retracted=true;
  612. prepare_move();
  613. }
  614. break;
  615. case 11: // G10 retract_recover
  616. if(!retracted)
  617. {
  618. destination[X_AXIS]=current_position[X_AXIS];
  619. destination[Y_AXIS]=current_position[Y_AXIS];
  620. destination[Z_AXIS]=current_position[Z_AXIS];
  621. current_position[Z_AXIS]+=retract_zlift;
  622. current_position[E_AXIS]+=-retract_recover_length;
  623. feedrate=retract_recover_feedrate;
  624. retracted=false;
  625. prepare_move();
  626. }
  627. break;
  628. #endif //FWRETRACT
  629. case 28: //G28 Home all Axis one at a time
  630. saved_feedrate = feedrate;
  631. saved_feedmultiply = feedmultiply;
  632. feedmultiply = 100;
  633. previous_millis_cmd = millis();
  634. enable_endstops(true);
  635. for(int8_t i=0; i < NUM_AXIS; i++) {
  636. destination[i] = current_position[i];
  637. }
  638. feedrate = 0.0;
  639. home_all_axis = !((code_seen(axis_codes[0])) || (code_seen(axis_codes[1])) || (code_seen(axis_codes[2])));
  640. #if Z_HOME_DIR > 0 // If homing away from BED do Z first
  641. if((home_all_axis) || (code_seen(axis_codes[Z_AXIS]))) {
  642. HOMEAXIS(Z);
  643. }
  644. #endif
  645. #ifdef QUICK_HOME
  646. if((home_all_axis)||( code_seen(axis_codes[X_AXIS]) && code_seen(axis_codes[Y_AXIS])) ) //first diagonal move
  647. {
  648. current_position[X_AXIS] = 0;current_position[Y_AXIS] = 0;
  649. plan_set_position(current_position[X_AXIS], current_position[Y_AXIS], current_position[Z_AXIS], current_position[E_AXIS]);
  650. destination[X_AXIS] = 1.5 * X_MAX_LENGTH * X_HOME_DIR;destination[Y_AXIS] = 1.5 * Y_MAX_LENGTH * Y_HOME_DIR;
  651. feedrate = homing_feedrate[X_AXIS];
  652. if(homing_feedrate[Y_AXIS]<feedrate)
  653. feedrate =homing_feedrate[Y_AXIS];
  654. plan_buffer_line(destination[X_AXIS], destination[Y_AXIS], destination[Z_AXIS], destination[E_AXIS], feedrate/60, active_extruder);
  655. st_synchronize();
  656. axis_is_at_home(X_AXIS);
  657. axis_is_at_home(Y_AXIS);
  658. plan_set_position(current_position[X_AXIS], current_position[Y_AXIS], current_position[Z_AXIS], current_position[E_AXIS]);
  659. destination[X_AXIS] = current_position[X_AXIS];
  660. destination[Y_AXIS] = current_position[Y_AXIS];
  661. plan_buffer_line(destination[X_AXIS], destination[Y_AXIS], destination[Z_AXIS], destination[E_AXIS], feedrate/60, active_extruder);
  662. feedrate = 0.0;
  663. st_synchronize();
  664. endstops_hit_on_purpose();
  665. }
  666. #endif
  667. if((home_all_axis) || (code_seen(axis_codes[X_AXIS])))
  668. {
  669. HOMEAXIS(X);
  670. }
  671. if((home_all_axis) || (code_seen(axis_codes[Y_AXIS]))) {
  672. HOMEAXIS(Y);
  673. }
  674. #if Z_HOME_DIR < 0 // If homing towards BED do Z last
  675. if((home_all_axis) || (code_seen(axis_codes[Z_AXIS]))) {
  676. HOMEAXIS(Z);
  677. }
  678. #endif
  679. if(code_seen(axis_codes[X_AXIS]))
  680. {
  681. if(code_value_long() != 0) {
  682. current_position[X_AXIS]=code_value()+add_homeing[0];
  683. }
  684. }
  685. if(code_seen(axis_codes[Y_AXIS])) {
  686. if(code_value_long() != 0) {
  687. current_position[Y_AXIS]=code_value()+add_homeing[1];
  688. }
  689. }
  690. if(code_seen(axis_codes[Z_AXIS])) {
  691. if(code_value_long() != 0) {
  692. current_position[Z_AXIS]=code_value()+add_homeing[2];
  693. }
  694. }
  695. plan_set_position(current_position[X_AXIS], current_position[Y_AXIS], current_position[Z_AXIS], current_position[E_AXIS]);
  696. #ifdef ENDSTOPS_ONLY_FOR_HOMING
  697. enable_endstops(false);
  698. #endif
  699. feedrate = saved_feedrate;
  700. feedmultiply = saved_feedmultiply;
  701. previous_millis_cmd = millis();
  702. endstops_hit_on_purpose();
  703. break;
  704. case 90: // G90
  705. relative_mode = false;
  706. break;
  707. case 91: // G91
  708. relative_mode = true;
  709. break;
  710. case 92: // G92
  711. if(!code_seen(axis_codes[E_AXIS]))
  712. st_synchronize();
  713. for(int8_t i=0; i < NUM_AXIS; i++) {
  714. if(code_seen(axis_codes[i])) {
  715. if(i == E_AXIS) {
  716. current_position[i] = code_value();
  717. plan_set_e_position(current_position[E_AXIS]);
  718. }
  719. else {
  720. current_position[i] = code_value()+add_homeing[i];
  721. plan_set_position(current_position[X_AXIS], current_position[Y_AXIS], current_position[Z_AXIS], current_position[E_AXIS]);
  722. }
  723. }
  724. }
  725. break;
  726. }
  727. }
  728. else if(code_seen('M'))
  729. {
  730. switch( (int)code_value() )
  731. {
  732. #ifdef ULTRA_LCD
  733. case 0: // M0 - Unconditional stop - Wait for user button press on LCD
  734. case 1: // M1 - Conditional stop - Wait for user button press on LCD
  735. {
  736. LCD_MESSAGEPGM(MSG_USERWAIT);
  737. codenum = 0;
  738. if(code_seen('P')) codenum = code_value(); // milliseconds to wait
  739. if(code_seen('S')) codenum = code_value() * 1000; // seconds to wait
  740. st_synchronize();
  741. previous_millis_cmd = millis();
  742. if (codenum > 0){
  743. codenum += millis(); // keep track of when we started waiting
  744. while(millis() < codenum && !CLICKED){
  745. manage_heater();
  746. manage_inactivity();
  747. LCD_STATUS;
  748. }
  749. }else{
  750. while(!CLICKED){
  751. manage_heater();
  752. manage_inactivity();
  753. LCD_STATUS;
  754. }
  755. }
  756. }
  757. break;
  758. #endif
  759. case 17:
  760. LCD_MESSAGEPGM(MSG_NO_MOVE);
  761. enable_x();
  762. enable_y();
  763. enable_z();
  764. enable_e0();
  765. enable_e1();
  766. enable_e2();
  767. break;
  768. #ifdef SDSUPPORT
  769. case 20: // M20 - list SD card
  770. SERIAL_PROTOCOLLNPGM(MSG_BEGIN_FILE_LIST);
  771. card.ls();
  772. SERIAL_PROTOCOLLNPGM(MSG_END_FILE_LIST);
  773. break;
  774. case 21: // M21 - init SD card
  775. card.initsd();
  776. break;
  777. case 22: //M22 - release SD card
  778. card.release();
  779. break;
  780. case 23: //M23 - Select file
  781. starpos = (strchr(strchr_pointer + 4,'*'));
  782. if(starpos!=NULL)
  783. *(starpos-1)='\0';
  784. card.openFile(strchr_pointer + 4,true);
  785. break;
  786. case 24: //M24 - Start SD print
  787. card.startFileprint();
  788. starttime=millis();
  789. break;
  790. case 25: //M25 - Pause SD print
  791. card.pauseSDPrint();
  792. break;
  793. case 26: //M26 - Set SD index
  794. if(card.cardOK && code_seen('S')) {
  795. card.setIndex(code_value_long());
  796. }
  797. break;
  798. case 27: //M27 - Get SD status
  799. card.getStatus();
  800. break;
  801. case 28: //M28 - Start SD write
  802. starpos = (strchr(strchr_pointer + 4,'*'));
  803. if(starpos != NULL){
  804. char* npos = strchr(cmdbuffer[bufindr], 'N');
  805. strchr_pointer = strchr(npos,' ') + 1;
  806. *(starpos-1) = '\0';
  807. }
  808. card.openFile(strchr_pointer+4,false);
  809. break;
  810. case 29: //M29 - Stop SD write
  811. //processed in write to file routine above
  812. //card,saving = false;
  813. break;
  814. case 30: //M30 <filename> Delete File
  815. if (card.cardOK){
  816. card.closefile();
  817. starpos = (strchr(strchr_pointer + 4,'*'));
  818. if(starpos != NULL){
  819. char* npos = strchr(cmdbuffer[bufindr], 'N');
  820. strchr_pointer = strchr(npos,' ') + 1;
  821. *(starpos-1) = '\0';
  822. }
  823. card.removeFile(strchr_pointer + 4);
  824. }
  825. break;
  826. #endif //SDSUPPORT
  827. case 31: //M31 take time since the start of the SD print or an M109 command
  828. {
  829. stoptime=millis();
  830. char time[30];
  831. unsigned long t=(stoptime-starttime)/1000;
  832. int sec,min;
  833. min=t/60;
  834. sec=t%60;
  835. sprintf(time,"%i min, %i sec",min,sec);
  836. SERIAL_ECHO_START;
  837. SERIAL_ECHOLN(time);
  838. LCD_MESSAGE(time);
  839. autotempShutdown();
  840. }
  841. break;
  842. case 42: //M42 -Change pin status via gcode
  843. if (code_seen('S'))
  844. {
  845. int pin_status = code_value();
  846. if (code_seen('P') && pin_status >= 0 && pin_status <= 255)
  847. {
  848. int pin_number = code_value();
  849. for(int8_t i = 0; i < (int8_t)sizeof(sensitive_pins); i++)
  850. {
  851. if (sensitive_pins[i] == pin_number)
  852. {
  853. pin_number = -1;
  854. break;
  855. }
  856. }
  857. if (pin_number > -1)
  858. {
  859. pinMode(pin_number, OUTPUT);
  860. digitalWrite(pin_number, pin_status);
  861. analogWrite(pin_number, pin_status);
  862. }
  863. }
  864. }
  865. break;
  866. case 104: // M104
  867. if(setTargetedHotend(104)){
  868. break;
  869. }
  870. if (code_seen('S')) setTargetHotend(code_value(), tmp_extruder);
  871. setWatch();
  872. break;
  873. case 140: // M140 set bed temp
  874. if (code_seen('S')) setTargetBed(code_value());
  875. break;
  876. case 105 : // M105
  877. if(setTargetedHotend(105)){
  878. break;
  879. }
  880. #if (TEMP_0_PIN > -1)
  881. SERIAL_PROTOCOLPGM("ok T:");
  882. SERIAL_PROTOCOL_F(degHotend(tmp_extruder),1);
  883. SERIAL_PROTOCOLPGM(" /");
  884. SERIAL_PROTOCOL_F(degTargetHotend(tmp_extruder),1);
  885. #if TEMP_BED_PIN > -1
  886. SERIAL_PROTOCOLPGM(" B:");
  887. SERIAL_PROTOCOL_F(degBed(),1);
  888. SERIAL_PROTOCOLPGM(" /");
  889. SERIAL_PROTOCOL_F(degTargetBed(),1);
  890. #endif //TEMP_BED_PIN
  891. #else
  892. SERIAL_ERROR_START;
  893. SERIAL_ERRORLNPGM(MSG_ERR_NO_THERMISTORS);
  894. #endif
  895. SERIAL_PROTOCOLPGM(" @:");
  896. SERIAL_PROTOCOL(getHeaterPower(tmp_extruder));
  897. SERIAL_PROTOCOLPGM(" B@:");
  898. SERIAL_PROTOCOL(getHeaterPower(-1));
  899. SERIAL_PROTOCOLLN("");
  900. return;
  901. break;
  902. case 109:
  903. {// M109 - Wait for extruder heater to reach target.
  904. if(setTargetedHotend(109)){
  905. break;
  906. }
  907. LCD_MESSAGEPGM(MSG_HEATING);
  908. #ifdef AUTOTEMP
  909. autotemp_enabled=false;
  910. #endif
  911. if (code_seen('S')) setTargetHotend(code_value(), tmp_extruder);
  912. #ifdef AUTOTEMP
  913. if (code_seen('S')) autotemp_min=code_value();
  914. if (code_seen('B')) autotemp_max=code_value();
  915. if (code_seen('F'))
  916. {
  917. autotemp_factor=code_value();
  918. autotemp_enabled=true;
  919. }
  920. #endif
  921. setWatch();
  922. codenum = millis();
  923. /* See if we are heating up or cooling down */
  924. bool target_direction = isHeatingHotend(tmp_extruder); // true if heating, false if cooling
  925. #ifdef TEMP_RESIDENCY_TIME
  926. long residencyStart;
  927. residencyStart = -1;
  928. /* continue to loop until we have reached the target temp
  929. _and_ until TEMP_RESIDENCY_TIME hasn't passed since we reached it */
  930. while((residencyStart == -1) ||
  931. (residencyStart >= 0 && (((unsigned int) (millis() - residencyStart)) < (TEMP_RESIDENCY_TIME * 1000UL))) ) {
  932. #else
  933. while ( target_direction ? (isHeatingHotend(tmp_extruder)) : (isCoolingHotend(tmp_extruder)&&(CooldownNoWait==false)) ) {
  934. #endif //TEMP_RESIDENCY_TIME
  935. if( (millis() - codenum) > 1000UL )
  936. { //Print Temp Reading and remaining time every 1 second while heating up/cooling down
  937. SERIAL_PROTOCOLPGM("T:");
  938. SERIAL_PROTOCOL_F(degHotend(tmp_extruder),1);
  939. SERIAL_PROTOCOLPGM(" E:");
  940. SERIAL_PROTOCOL((int)tmp_extruder);
  941. #ifdef TEMP_RESIDENCY_TIME
  942. SERIAL_PROTOCOLPGM(" W:");
  943. if(residencyStart > -1)
  944. {
  945. codenum = ((TEMP_RESIDENCY_TIME * 1000UL) - (millis() - residencyStart)) / 1000UL;
  946. SERIAL_PROTOCOLLN( codenum );
  947. }
  948. else
  949. {
  950. SERIAL_PROTOCOLLN( "?" );
  951. }
  952. #else
  953. SERIAL_PROTOCOLLN("");
  954. #endif
  955. codenum = millis();
  956. }
  957. manage_heater();
  958. manage_inactivity();
  959. LCD_STATUS;
  960. #ifdef TEMP_RESIDENCY_TIME
  961. /* start/restart the TEMP_RESIDENCY_TIME timer whenever we reach target temp for the first time
  962. or when current temp falls outside the hysteresis after target temp was reached */
  963. if ((residencyStart == -1 && target_direction && (degHotend(tmp_extruder) >= (degTargetHotend(tmp_extruder)-TEMP_WINDOW))) ||
  964. (residencyStart == -1 && !target_direction && (degHotend(tmp_extruder) <= (degTargetHotend(tmp_extruder)+TEMP_WINDOW))) ||
  965. (residencyStart > -1 && labs(degHotend(tmp_extruder) - degTargetHotend(tmp_extruder)) > TEMP_HYSTERESIS) )
  966. {
  967. residencyStart = millis();
  968. }
  969. #endif //TEMP_RESIDENCY_TIME
  970. }
  971. LCD_MESSAGEPGM(MSG_HEATING_COMPLETE);
  972. starttime=millis();
  973. previous_millis_cmd = millis();
  974. }
  975. break;
  976. case 190: // M190 - Wait for bed heater to reach target.
  977. #if TEMP_BED_PIN > -1
  978. LCD_MESSAGEPGM(MSG_BED_HEATING);
  979. if (code_seen('S')) setTargetBed(code_value());
  980. codenum = millis();
  981. while(isHeatingBed())
  982. {
  983. if(( millis() - codenum) > 1000 ) //Print Temp Reading every 1 second while heating up.
  984. {
  985. float tt=degHotend(active_extruder);
  986. SERIAL_PROTOCOLPGM("T:");
  987. SERIAL_PROTOCOL(tt);
  988. SERIAL_PROTOCOLPGM(" E:");
  989. SERIAL_PROTOCOL((int)active_extruder);
  990. SERIAL_PROTOCOLPGM(" B:");
  991. SERIAL_PROTOCOL_F(degBed(),1);
  992. SERIAL_PROTOCOLLN("");
  993. codenum = millis();
  994. }
  995. manage_heater();
  996. manage_inactivity();
  997. LCD_STATUS;
  998. }
  999. LCD_MESSAGEPGM(MSG_BED_DONE);
  1000. previous_millis_cmd = millis();
  1001. #endif
  1002. break;
  1003. #if FAN_PIN > -1
  1004. case 106: //M106 Fan On
  1005. if (code_seen('S')){
  1006. FanSpeed=constrain(code_value(),0,255);
  1007. }
  1008. else {
  1009. FanSpeed=255;
  1010. }
  1011. break;
  1012. case 107: //M107 Fan Off
  1013. FanSpeed = 0;
  1014. break;
  1015. #endif //FAN_PIN
  1016. #if (PS_ON_PIN > -1)
  1017. case 80: // M80 - ATX Power On
  1018. SET_OUTPUT(PS_ON_PIN); //GND
  1019. WRITE(PS_ON_PIN, LOW);
  1020. break;
  1021. #endif
  1022. case 81: // M81 - ATX Power Off
  1023. #if defined SUICIDE_PIN && SUICIDE_PIN > -1
  1024. st_synchronize();
  1025. suicide();
  1026. #elif (PS_ON_PIN > -1)
  1027. SET_OUTPUT(PS_ON_PIN);
  1028. WRITE(PS_ON_PIN, HIGH);
  1029. #endif
  1030. break;
  1031. case 82:
  1032. axis_relative_modes[3] = false;
  1033. break;
  1034. case 83:
  1035. axis_relative_modes[3] = true;
  1036. break;
  1037. case 18: //compatibility
  1038. case 84: // M84
  1039. if(code_seen('S')){
  1040. stepper_inactive_time = code_value() * 1000;
  1041. }
  1042. else
  1043. {
  1044. bool all_axis = !((code_seen(axis_codes[0])) || (code_seen(axis_codes[1])) || (code_seen(axis_codes[2]))|| (code_seen(axis_codes[3])));
  1045. if(all_axis)
  1046. {
  1047. st_synchronize();
  1048. disable_e0();
  1049. disable_e1();
  1050. disable_e2();
  1051. finishAndDisableSteppers();
  1052. }
  1053. else
  1054. {
  1055. st_synchronize();
  1056. if(code_seen('X')) disable_x();
  1057. if(code_seen('Y')) disable_y();
  1058. if(code_seen('Z')) disable_z();
  1059. #if ((E0_ENABLE_PIN != X_ENABLE_PIN) && (E1_ENABLE_PIN != Y_ENABLE_PIN)) // Only enable on boards that have seperate ENABLE_PINS
  1060. if(code_seen('E')) {
  1061. disable_e0();
  1062. disable_e1();
  1063. disable_e2();
  1064. }
  1065. #endif
  1066. LCD_MESSAGEPGM(MSG_PART_RELEASE);
  1067. }
  1068. }
  1069. break;
  1070. case 85: // M85
  1071. code_seen('S');
  1072. max_inactive_time = code_value() * 1000;
  1073. break;
  1074. case 92: // M92
  1075. for(int8_t i=0; i < NUM_AXIS; i++)
  1076. {
  1077. if(code_seen(axis_codes[i]))
  1078. {
  1079. if(i == 3) { // E
  1080. float value = code_value();
  1081. if(value < 20.0) {
  1082. float factor = axis_steps_per_unit[i] / value; // increase e constants if M92 E14 is given for netfab.
  1083. max_e_jerk *= factor;
  1084. max_feedrate[i] *= factor;
  1085. axis_steps_per_sqr_second[i] *= factor;
  1086. }
  1087. axis_steps_per_unit[i] = value;
  1088. }
  1089. else {
  1090. axis_steps_per_unit[i] = code_value();
  1091. }
  1092. }
  1093. }
  1094. break;
  1095. case 115: // M115
  1096. SERIAL_PROTOCOLPGM(MSG_M115_REPORT);
  1097. break;
  1098. case 117: // M117 display message
  1099. LCD_MESSAGE(cmdbuffer[bufindr]+5);
  1100. break;
  1101. case 114: // M114
  1102. SERIAL_PROTOCOLPGM("X:");
  1103. SERIAL_PROTOCOL(current_position[X_AXIS]);
  1104. SERIAL_PROTOCOLPGM("Y:");
  1105. SERIAL_PROTOCOL(current_position[Y_AXIS]);
  1106. SERIAL_PROTOCOLPGM("Z:");
  1107. SERIAL_PROTOCOL(current_position[Z_AXIS]);
  1108. SERIAL_PROTOCOLPGM("E:");
  1109. SERIAL_PROTOCOL(current_position[E_AXIS]);
  1110. SERIAL_PROTOCOLPGM(MSG_COUNT_X);
  1111. SERIAL_PROTOCOL(float(st_get_position(X_AXIS))/axis_steps_per_unit[X_AXIS]);
  1112. SERIAL_PROTOCOLPGM("Y:");
  1113. SERIAL_PROTOCOL(float(st_get_position(Y_AXIS))/axis_steps_per_unit[Y_AXIS]);
  1114. SERIAL_PROTOCOLPGM("Z:");
  1115. SERIAL_PROTOCOL(float(st_get_position(Z_AXIS))/axis_steps_per_unit[Z_AXIS]);
  1116. SERIAL_PROTOCOLLN("");
  1117. break;
  1118. case 120: // M120
  1119. enable_endstops(false) ;
  1120. break;
  1121. case 121: // M121
  1122. enable_endstops(true) ;
  1123. break;
  1124. case 119: // M119
  1125. SERIAL_PROTOCOLLN(MSG_M119_REPORT);
  1126. #if (X_MIN_PIN > -1)
  1127. SERIAL_PROTOCOLPGM(MSG_X_MIN);
  1128. SERIAL_PROTOCOLLN(((READ(X_MIN_PIN)^X_ENDSTOPS_INVERTING)?MSG_ENDSTOP_HIT:MSG_ENDSTOP_OPEN));
  1129. #endif
  1130. #if (X_MAX_PIN > -1)
  1131. SERIAL_PROTOCOLPGM(MSG_X_MAX);
  1132. SERIAL_PROTOCOLLN(((READ(X_MAX_PIN)^X_ENDSTOPS_INVERTING)?MSG_ENDSTOP_HIT:MSG_ENDSTOP_OPEN));
  1133. #endif
  1134. #if (Y_MIN_PIN > -1)
  1135. SERIAL_PROTOCOLPGM(MSG_Y_MIN);
  1136. SERIAL_PROTOCOLLN(((READ(Y_MIN_PIN)^Y_ENDSTOPS_INVERTING)?MSG_ENDSTOP_HIT:MSG_ENDSTOP_OPEN));
  1137. #endif
  1138. #if (Y_MAX_PIN > -1)
  1139. SERIAL_PROTOCOLPGM(MSG_Y_MAX);
  1140. SERIAL_PROTOCOLLN(((READ(Y_MAX_PIN)^Y_ENDSTOPS_INVERTING)?MSG_ENDSTOP_HIT:MSG_ENDSTOP_OPEN));
  1141. #endif
  1142. #if (Z_MIN_PIN > -1)
  1143. SERIAL_PROTOCOLPGM(MSG_Z_MIN);
  1144. SERIAL_PROTOCOLLN(((READ(Z_MIN_PIN)^Z_ENDSTOPS_INVERTING)?MSG_ENDSTOP_HIT:MSG_ENDSTOP_OPEN));
  1145. #endif
  1146. #if (Z_MAX_PIN > -1)
  1147. SERIAL_PROTOCOLPGM(MSG_Z_MAX);
  1148. SERIAL_PROTOCOLLN(((READ(Z_MAX_PIN)^Z_ENDSTOPS_INVERTING)?MSG_ENDSTOP_HIT:MSG_ENDSTOP_OPEN));
  1149. #endif
  1150. break;
  1151. //TODO: update for all axis, use for loop
  1152. case 201: // M201
  1153. for(int8_t i=0; i < NUM_AXIS; i++)
  1154. {
  1155. if(code_seen(axis_codes[i]))
  1156. {
  1157. max_acceleration_units_per_sq_second[i] = code_value();
  1158. axis_steps_per_sqr_second[i] = code_value() * axis_steps_per_unit[i];
  1159. }
  1160. }
  1161. break;
  1162. #if 0 // Not used for Sprinter/grbl gen6
  1163. case 202: // M202
  1164. for(int8_t i=0; i < NUM_AXIS; i++) {
  1165. if(code_seen(axis_codes[i])) axis_travel_steps_per_sqr_second[i] = code_value() * axis_steps_per_unit[i];
  1166. }
  1167. break;
  1168. #endif
  1169. case 203: // M203 max feedrate mm/sec
  1170. for(int8_t i=0; i < NUM_AXIS; i++) {
  1171. if(code_seen(axis_codes[i])) max_feedrate[i] = code_value();
  1172. }
  1173. break;
  1174. case 204: // M204 acclereration S normal moves T filmanent only moves
  1175. {
  1176. if(code_seen('S')) acceleration = code_value() ;
  1177. if(code_seen('T')) retract_acceleration = code_value() ;
  1178. }
  1179. break;
  1180. 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
  1181. {
  1182. if(code_seen('S')) minimumfeedrate = code_value();
  1183. if(code_seen('T')) mintravelfeedrate = code_value();
  1184. if(code_seen('B')) minsegmenttime = code_value() ;
  1185. if(code_seen('X')) max_xy_jerk = code_value() ;
  1186. if(code_seen('Z')) max_z_jerk = code_value() ;
  1187. if(code_seen('E')) max_e_jerk = code_value() ;
  1188. }
  1189. break;
  1190. case 206: // M206 additional homeing offset
  1191. for(int8_t i=0; i < 3; i++)
  1192. {
  1193. if(code_seen(axis_codes[i])) add_homeing[i] = code_value();
  1194. }
  1195. break;
  1196. #ifdef FWRETRACT
  1197. case 207: //M207 - set retract length S[positive mm] F[feedrate mm/sec] Z[additional zlift/hop]
  1198. {
  1199. if(code_seen('S'))
  1200. {
  1201. retract_length = code_value() ;
  1202. }
  1203. if(code_seen('F'))
  1204. {
  1205. retract_feedrate = code_value() ;
  1206. }
  1207. if(code_seen('Z'))
  1208. {
  1209. retract_zlift = code_value() ;
  1210. }
  1211. }break;
  1212. case 208: // M208 - set retract recover length S[positive mm surplus to the M207 S*] F[feedrate mm/sec]
  1213. {
  1214. if(code_seen('S'))
  1215. {
  1216. retract_recover_length = code_value() ;
  1217. }
  1218. if(code_seen('F'))
  1219. {
  1220. retract_recover_feedrate = code_value() ;
  1221. }
  1222. }break;
  1223. case 209: // M209 - S<1=true/0=false> enable automatic retract detect if the slicer did not support G10/11: every normal extrude-only move will be classified as retract depending on the direction.
  1224. {
  1225. if(code_seen('S'))
  1226. {
  1227. int t= code_value() ;
  1228. switch(t)
  1229. {
  1230. case 0: autoretract_enabled=false;retracted=false;break;
  1231. case 1: autoretract_enabled=true;retracted=false;break;
  1232. default:
  1233. SERIAL_ECHO_START;
  1234. SERIAL_ECHOPGM(MSG_UNKNOWN_COMMAND);
  1235. SERIAL_ECHO(cmdbuffer[bufindr]);
  1236. SERIAL_ECHOLNPGM("\"");
  1237. }
  1238. }
  1239. }break;
  1240. #endif
  1241. case 220: // M220 S<factor in percent>- set speed factor override percentage
  1242. {
  1243. if(code_seen('S'))
  1244. {
  1245. feedmultiply = code_value() ;
  1246. feedmultiplychanged=true;
  1247. }
  1248. }
  1249. break;
  1250. case 221: // M221 S<factor in percent>- set extrude factor override percentage
  1251. {
  1252. if(code_seen('S'))
  1253. {
  1254. extrudemultiply = code_value() ;
  1255. }
  1256. }
  1257. break;
  1258. #ifdef PIDTEMP
  1259. case 301: // M301
  1260. {
  1261. if(code_seen('P')) Kp = code_value();
  1262. if(code_seen('I')) Ki = code_value()*PID_dT;
  1263. if(code_seen('D')) Kd = code_value()/PID_dT;
  1264. #ifdef PID_ADD_EXTRUSION_RATE
  1265. if(code_seen('C')) Kc = code_value();
  1266. #endif
  1267. updatePID();
  1268. SERIAL_PROTOCOL(MSG_OK);
  1269. SERIAL_PROTOCOL(" p:");
  1270. SERIAL_PROTOCOL(Kp);
  1271. SERIAL_PROTOCOL(" i:");
  1272. SERIAL_PROTOCOL(Ki/PID_dT);
  1273. SERIAL_PROTOCOL(" d:");
  1274. SERIAL_PROTOCOL(Kd*PID_dT);
  1275. #ifdef PID_ADD_EXTRUSION_RATE
  1276. SERIAL_PROTOCOL(" c:");
  1277. SERIAL_PROTOCOL(Kc*PID_dT);
  1278. #endif
  1279. SERIAL_PROTOCOLLN("");
  1280. }
  1281. break;
  1282. #endif //PIDTEMP
  1283. #ifdef PIDTEMPBED
  1284. case 304: // M304
  1285. {
  1286. if(code_seen('P')) bedKp = code_value();
  1287. if(code_seen('I')) bedKi = code_value()*PID_dT;
  1288. if(code_seen('D')) bedKd = code_value()/PID_dT;
  1289. updatePID();
  1290. SERIAL_PROTOCOL(MSG_OK);
  1291. SERIAL_PROTOCOL(" p:");
  1292. SERIAL_PROTOCOL(bedKp);
  1293. SERIAL_PROTOCOL(" i:");
  1294. SERIAL_PROTOCOL(bedKi/PID_dT);
  1295. SERIAL_PROTOCOL(" d:");
  1296. SERIAL_PROTOCOL(bedKd*PID_dT);
  1297. SERIAL_PROTOCOLLN("");
  1298. }
  1299. break;
  1300. #endif //PIDTEMP
  1301. case 240: // M240 Triggers a camera by emulating a Canon RC-1 : http://www.doc-diy.net/photo/rc-1_hacked/
  1302. {
  1303. #ifdef PHOTOGRAPH_PIN
  1304. #if (PHOTOGRAPH_PIN > -1)
  1305. const uint8_t NUM_PULSES=16;
  1306. const float PULSE_LENGTH=0.01524;
  1307. for(int i=0; i < NUM_PULSES; i++) {
  1308. WRITE(PHOTOGRAPH_PIN, HIGH);
  1309. _delay_ms(PULSE_LENGTH);
  1310. WRITE(PHOTOGRAPH_PIN, LOW);
  1311. _delay_ms(PULSE_LENGTH);
  1312. }
  1313. delay(7.33);
  1314. for(int i=0; i < NUM_PULSES; i++) {
  1315. WRITE(PHOTOGRAPH_PIN, HIGH);
  1316. _delay_ms(PULSE_LENGTH);
  1317. WRITE(PHOTOGRAPH_PIN, LOW);
  1318. _delay_ms(PULSE_LENGTH);
  1319. }
  1320. #endif
  1321. #endif
  1322. }
  1323. break;
  1324. case 302: // allow cold extrudes
  1325. {
  1326. allow_cold_extrudes(true);
  1327. }
  1328. break;
  1329. case 303: // M303 PID autotune
  1330. {
  1331. float temp = 150.0;
  1332. int e=0;
  1333. int c=5;
  1334. if (code_seen('E')) e=code_value();
  1335. if (e<0)
  1336. temp=70;
  1337. if (code_seen('S')) temp=code_value();
  1338. if (code_seen('C')) c=code_value();
  1339. PID_autotune(temp, e, c);
  1340. }
  1341. break;
  1342. case 400: // M400 finish all moves
  1343. {
  1344. st_synchronize();
  1345. }
  1346. break;
  1347. case 500: // Store settings in EEPROM
  1348. {
  1349. Config_StoreSettings();
  1350. }
  1351. break;
  1352. case 501: // Read settings from EEPROM
  1353. {
  1354. Config_RetrieveSettings();
  1355. }
  1356. break;
  1357. case 502: // Revert to default settings
  1358. {
  1359. Config_ResetDefault();
  1360. }
  1361. break;
  1362. case 503: // print settings currently in memory
  1363. {
  1364. Config_PrintSettings();
  1365. }
  1366. break;
  1367. case 907: // Set digital trimpot motor current using axis codes.
  1368. {
  1369. #if DIGIPOTSS_PIN > -1
  1370. for(int i=0;i<=NUM_AXIS;i++) if(code_seen(axis_codes[i])) digipot_current(i,code_value());
  1371. if(code_seen('B')) digipot_current(4,code_value());
  1372. if(code_seen('S')) for(int i=0;i<=4;i++) digipot_current(i,code_value());
  1373. #endif
  1374. }
  1375. case 908: // Control digital trimpot directly.
  1376. {
  1377. #if DIGIPOTSS_PIN > -1
  1378. uint8_t channel,current;
  1379. if(code_seen('P')) channel=code_value();
  1380. if(code_seen('S')) current=code_value();
  1381. digitalPotWrite(channel, current);
  1382. #endif
  1383. }
  1384. break;
  1385. case 350: // Set microstepping mode. Warning: Steps per unit remains unchanged. S code sets stepping mode for all drivers.
  1386. {
  1387. #if X_MS1_PIN > -1
  1388. if(code_seen('S')) for(int i=0;i<=4;i++) microstep_mode(i,code_value());
  1389. for(int i=0;i<=NUM_AXIS;i++) if(code_seen(axis_codes[i])) microstep_mode(i,(uint8_t)code_value());
  1390. if(code_seen('B')) microstep_mode(4,code_value());
  1391. microstep_readings();
  1392. #endif
  1393. }
  1394. break;
  1395. case 351: // Toggle MS1 MS2 pins directly, S# determines MS1 or MS2, X# sets the pin high/low.
  1396. {
  1397. #if X_MS1_PIN > -1
  1398. if(code_seen('S')) switch((int)code_value())
  1399. {
  1400. case 1:
  1401. for(int i=0;i<=NUM_AXIS;i++) if(code_seen(axis_codes[i])) microstep_ms(i,code_value(),-1);
  1402. if(code_seen('B')) microstep_ms(4,code_value(),-1);
  1403. break;
  1404. case 2:
  1405. for(int i=0;i<=NUM_AXIS;i++) if(code_seen(axis_codes[i])) microstep_ms(i,-1,code_value());
  1406. if(code_seen('B')) microstep_ms(4,-1,code_value());
  1407. break;
  1408. }
  1409. microstep_readings();
  1410. #endif
  1411. }
  1412. break;
  1413. case 999: // Restart after being stopped
  1414. Stopped = false;
  1415. gcode_LastN = Stopped_gcode_LastN;
  1416. FlushSerialRequestResend();
  1417. break;
  1418. }
  1419. }
  1420. else if(code_seen('T'))
  1421. {
  1422. tmp_extruder = code_value();
  1423. if(tmp_extruder >= EXTRUDERS) {
  1424. SERIAL_ECHO_START;
  1425. SERIAL_ECHO("T");
  1426. SERIAL_ECHO(tmp_extruder);
  1427. SERIAL_ECHOLN(MSG_INVALID_EXTRUDER);
  1428. }
  1429. else {
  1430. active_extruder = tmp_extruder;
  1431. SERIAL_ECHO_START;
  1432. SERIAL_ECHO(MSG_ACTIVE_EXTRUDER);
  1433. SERIAL_PROTOCOLLN((int)active_extruder);
  1434. }
  1435. }
  1436. else
  1437. {
  1438. SERIAL_ECHO_START;
  1439. SERIAL_ECHOPGM(MSG_UNKNOWN_COMMAND);
  1440. SERIAL_ECHO(cmdbuffer[bufindr]);
  1441. SERIAL_ECHOLNPGM("\"");
  1442. }
  1443. ClearToSend();
  1444. }
  1445. void FlushSerialRequestResend()
  1446. {
  1447. //char cmdbuffer[bufindr][100]="Resend:";
  1448. MYSERIAL.flush();
  1449. SERIAL_PROTOCOLPGM(MSG_RESEND);
  1450. SERIAL_PROTOCOLLN(gcode_LastN + 1);
  1451. ClearToSend();
  1452. }
  1453. void ClearToSend()
  1454. {
  1455. previous_millis_cmd = millis();
  1456. #ifdef SDSUPPORT
  1457. if(fromsd[bufindr])
  1458. return;
  1459. #endif //SDSUPPORT
  1460. SERIAL_PROTOCOLLNPGM(MSG_OK);
  1461. }
  1462. void get_coordinates()
  1463. {
  1464. bool seen[4]={false,false,false,false};
  1465. for(int8_t i=0; i < NUM_AXIS; i++) {
  1466. if(code_seen(axis_codes[i]))
  1467. {
  1468. destination[i] = (float)code_value() + (axis_relative_modes[i] || relative_mode)*current_position[i];
  1469. seen[i]=true;
  1470. }
  1471. else destination[i] = current_position[i]; //Are these else lines really needed?
  1472. }
  1473. if(code_seen('F')) {
  1474. next_feedrate = code_value();
  1475. if(next_feedrate > 0.0) feedrate = next_feedrate;
  1476. }
  1477. #ifdef FWRETRACT
  1478. if(autoretract_enabled)
  1479. if( !(seen[X_AXIS] || seen[Y_AXIS] || seen[Z_AXIS]) && seen[E_AXIS])
  1480. {
  1481. float echange=destination[E_AXIS]-current_position[E_AXIS];
  1482. if(echange<-MIN_RETRACT) //retract
  1483. {
  1484. if(!retracted)
  1485. {
  1486. destination[Z_AXIS]+=retract_zlift; //not sure why chaninging current_position negatively does not work.
  1487. //if slicer retracted by echange=-1mm and you want to retract 3mm, corrrectede=-2mm additionally
  1488. float correctede=-echange-retract_length;
  1489. //to generate the additional steps, not the destination is changed, but inversely the current position
  1490. current_position[E_AXIS]+=-correctede;
  1491. feedrate=retract_feedrate;
  1492. retracted=true;
  1493. }
  1494. }
  1495. else
  1496. if(echange>MIN_RETRACT) //retract_recover
  1497. {
  1498. if(retracted)
  1499. {
  1500. //current_position[Z_AXIS]+=-retract_zlift;
  1501. //if slicer retracted_recovered by echange=+1mm and you want to retract_recover 3mm, corrrectede=2mm additionally
  1502. float correctede=-echange+1*retract_length+retract_recover_length; //total unretract=retract_length+retract_recover_length[surplus]
  1503. current_position[E_AXIS]+=correctede; //to generate the additional steps, not the destination is changed, but inversely the current position
  1504. feedrate=retract_recover_feedrate;
  1505. retracted=false;
  1506. }
  1507. }
  1508. }
  1509. #endif //FWRETRACT
  1510. }
  1511. void get_arc_coordinates()
  1512. {
  1513. #ifdef SF_ARC_FIX
  1514. bool relative_mode_backup = relative_mode;
  1515. relative_mode = true;
  1516. #endif
  1517. get_coordinates();
  1518. #ifdef SF_ARC_FIX
  1519. relative_mode=relative_mode_backup;
  1520. #endif
  1521. if(code_seen('I')) {
  1522. offset[0] = code_value();
  1523. }
  1524. else {
  1525. offset[0] = 0.0;
  1526. }
  1527. if(code_seen('J')) {
  1528. offset[1] = code_value();
  1529. }
  1530. else {
  1531. offset[1] = 0.0;
  1532. }
  1533. }
  1534. void clamp_to_software_endstops(float target[3])
  1535. {
  1536. if (min_software_endstops) {
  1537. if (target[X_AXIS] < min_pos[X_AXIS]) target[X_AXIS] = min_pos[X_AXIS];
  1538. if (target[Y_AXIS] < min_pos[Y_AXIS]) target[Y_AXIS] = min_pos[Y_AXIS];
  1539. if (target[Z_AXIS] < min_pos[Z_AXIS]) target[Z_AXIS] = min_pos[Z_AXIS];
  1540. }
  1541. if (max_software_endstops) {
  1542. if (target[X_AXIS] > max_pos[X_AXIS]) target[X_AXIS] = max_pos[X_AXIS];
  1543. if (target[Y_AXIS] > max_pos[Y_AXIS]) target[Y_AXIS] = max_pos[Y_AXIS];
  1544. if (target[Z_AXIS] > max_pos[Z_AXIS]) target[Z_AXIS] = max_pos[Z_AXIS];
  1545. }
  1546. }
  1547. void prepare_move()
  1548. {
  1549. clamp_to_software_endstops(destination);
  1550. previous_millis_cmd = millis();
  1551. // Do not use feedmultiply for E or Z only moves
  1552. if( (current_position[X_AXIS] == destination [X_AXIS]) && (current_position[Y_AXIS] == destination [Y_AXIS])) {
  1553. plan_buffer_line(destination[X_AXIS], destination[Y_AXIS], destination[Z_AXIS], destination[E_AXIS], feedrate/60, active_extruder);
  1554. }
  1555. else {
  1556. plan_buffer_line(destination[X_AXIS], destination[Y_AXIS], destination[Z_AXIS], destination[E_AXIS], feedrate*feedmultiply/60/100.0, active_extruder);
  1557. }
  1558. for(int8_t i=0; i < NUM_AXIS; i++) {
  1559. current_position[i] = destination[i];
  1560. }
  1561. }
  1562. void prepare_arc_move(char isclockwise) {
  1563. float r = hypot(offset[X_AXIS], offset[Y_AXIS]); // Compute arc radius for mc_arc
  1564. // Trace the arc
  1565. mc_arc(current_position, destination, offset, X_AXIS, Y_AXIS, Z_AXIS, feedrate*feedmultiply/60/100.0, r, isclockwise, active_extruder);
  1566. // As far as the parser is concerned, the position is now == target. In reality the
  1567. // motion control system might still be processing the action and the real tool position
  1568. // in any intermediate location.
  1569. for(int8_t i=0; i < NUM_AXIS; i++) {
  1570. current_position[i] = destination[i];
  1571. }
  1572. previous_millis_cmd = millis();
  1573. }
  1574. #ifdef CONTROLLERFAN_PIN
  1575. unsigned long lastMotor = 0; //Save the time for when a motor was turned on last
  1576. unsigned long lastMotorCheck = 0;
  1577. void controllerFan()
  1578. {
  1579. if ((millis() - lastMotorCheck) >= 2500) //Not a time critical function, so we only check every 2500ms
  1580. {
  1581. lastMotorCheck = millis();
  1582. if(!READ(X_ENABLE_PIN) || !READ(Y_ENABLE_PIN) || !READ(Z_ENABLE_PIN)
  1583. #if EXTRUDERS > 2
  1584. || !READ(E2_ENABLE_PIN)
  1585. #endif
  1586. #if EXTRUDER > 1
  1587. || !READ(E2_ENABLE_PIN)
  1588. #endif
  1589. || !READ(E0_ENABLE_PIN)) //If any of the drivers are enabled...
  1590. {
  1591. lastMotor = millis(); //... set time to NOW so the fan will turn on
  1592. }
  1593. if ((millis() - lastMotor) >= (CONTROLLERFAN_SEC*1000UL) || lastMotor == 0) //If the last time any driver was enabled, is longer since than CONTROLLERSEC...
  1594. {
  1595. WRITE(CONTROLLERFAN_PIN, LOW); //... turn the fan off
  1596. }
  1597. else
  1598. {
  1599. WRITE(CONTROLLERFAN_PIN, HIGH); //... turn the fan on
  1600. }
  1601. }
  1602. }
  1603. #endif
  1604. void manage_inactivity()
  1605. {
  1606. if( (millis() - previous_millis_cmd) > max_inactive_time )
  1607. if(max_inactive_time)
  1608. kill();
  1609. if(stepper_inactive_time) {
  1610. if( (millis() - previous_millis_cmd) > stepper_inactive_time )
  1611. {
  1612. if(blocks_queued() == false) {
  1613. disable_x();
  1614. disable_y();
  1615. disable_z();
  1616. disable_e0();
  1617. disable_e1();
  1618. disable_e2();
  1619. }
  1620. }
  1621. }
  1622. #if( KILL_PIN>-1 )
  1623. if( 0 == READ(KILL_PIN) )
  1624. kill();
  1625. #endif
  1626. #ifdef CONTROLLERFAN_PIN
  1627. controllerFan(); //Check if fan should be turned on to cool stepper drivers down
  1628. #endif
  1629. #ifdef EXTRUDER_RUNOUT_PREVENT
  1630. if( (millis() - previous_millis_cmd) > EXTRUDER_RUNOUT_SECONDS*1000 )
  1631. if(degHotend(active_extruder)>EXTRUDER_RUNOUT_MINTEMP)
  1632. {
  1633. bool oldstatus=READ(E0_ENABLE_PIN);
  1634. enable_e0();
  1635. float oldepos=current_position[E_AXIS];
  1636. float oldedes=destination[E_AXIS];
  1637. plan_buffer_line(current_position[X_AXIS], current_position[Y_AXIS], current_position[Z_AXIS],
  1638. current_position[E_AXIS]+EXTRUDER_RUNOUT_EXTRUDE*EXTRUDER_RUNOUT_ESTEPS/axis_steps_per_unit[E_AXIS],
  1639. EXTRUDER_RUNOUT_SPEED/60.*EXTRUDER_RUNOUT_ESTEPS/axis_steps_per_unit[E_AXIS], active_extruder);
  1640. current_position[E_AXIS]=oldepos;
  1641. destination[E_AXIS]=oldedes;
  1642. plan_set_e_position(oldepos);
  1643. previous_millis_cmd=millis();
  1644. st_synchronize();
  1645. WRITE(E0_ENABLE_PIN,oldstatus);
  1646. }
  1647. #endif
  1648. check_axes_activity();
  1649. }
  1650. void kill()
  1651. {
  1652. cli(); // Stop interrupts
  1653. disable_heater();
  1654. disable_x();
  1655. disable_y();
  1656. disable_z();
  1657. disable_e0();
  1658. disable_e1();
  1659. disable_e2();
  1660. if(PS_ON_PIN > -1) pinMode(PS_ON_PIN,INPUT);
  1661. SERIAL_ERROR_START;
  1662. SERIAL_ERRORLNPGM(MSG_ERR_KILLED);
  1663. LCD_ALERTMESSAGEPGM(MSG_KILLED);
  1664. suicide();
  1665. while(1) { /* Intentionally left empty */ } // Wait for reset
  1666. }
  1667. void Stop()
  1668. {
  1669. disable_heater();
  1670. if(Stopped == false) {
  1671. Stopped = true;
  1672. Stopped_gcode_LastN = gcode_LastN; // Save last g_code for restart
  1673. SERIAL_ERROR_START;
  1674. SERIAL_ERRORLNPGM(MSG_ERR_STOPPED);
  1675. LCD_MESSAGEPGM(MSG_STOPPED);
  1676. }
  1677. }
  1678. bool IsStopped() { return Stopped; };
  1679. #ifdef FAST_PWM_FAN
  1680. void setPwmFrequency(uint8_t pin, int val)
  1681. {
  1682. val &= 0x07;
  1683. switch(digitalPinToTimer(pin))
  1684. {
  1685. #if defined(TCCR0A)
  1686. case TIMER0A:
  1687. case TIMER0B:
  1688. // TCCR0B &= ~(CS00 | CS01 | CS02);
  1689. // TCCR0B |= val;
  1690. break;
  1691. #endif
  1692. #if defined(TCCR1A)
  1693. case TIMER1A:
  1694. case TIMER1B:
  1695. // TCCR1B &= ~(CS10 | CS11 | CS12);
  1696. // TCCR1B |= val;
  1697. break;
  1698. #endif
  1699. #if defined(TCCR2)
  1700. case TIMER2:
  1701. case TIMER2:
  1702. TCCR2 &= ~(CS10 | CS11 | CS12);
  1703. TCCR2 |= val;
  1704. break;
  1705. #endif
  1706. #if defined(TCCR2A)
  1707. case TIMER2A:
  1708. case TIMER2B:
  1709. TCCR2B &= ~(CS20 | CS21 | CS22);
  1710. TCCR2B |= val;
  1711. break;
  1712. #endif
  1713. #if defined(TCCR3A)
  1714. case TIMER3A:
  1715. case TIMER3B:
  1716. case TIMER3C:
  1717. TCCR3B &= ~(CS30 | CS31 | CS32);
  1718. TCCR3B |= val;
  1719. break;
  1720. #endif
  1721. #if defined(TCCR4A)
  1722. case TIMER4A:
  1723. case TIMER4B:
  1724. case TIMER4C:
  1725. TCCR4B &= ~(CS40 | CS41 | CS42);
  1726. TCCR4B |= val;
  1727. break;
  1728. #endif
  1729. #if defined(TCCR5A)
  1730. case TIMER5A:
  1731. case TIMER5B:
  1732. case TIMER5C:
  1733. TCCR5B &= ~(CS50 | CS51 | CS52);
  1734. TCCR5B |= val;
  1735. break;
  1736. #endif
  1737. }
  1738. }
  1739. #endif //FAST_PWM_FAN
  1740. bool setTargetedHotend(int code){
  1741. tmp_extruder = active_extruder;
  1742. if(code_seen('T')) {
  1743. tmp_extruder = code_value();
  1744. if(tmp_extruder >= EXTRUDERS) {
  1745. SERIAL_ECHO_START;
  1746. switch(code){
  1747. case 104:
  1748. SERIAL_ECHO(MSG_M104_INVALID_EXTRUDER);
  1749. break;
  1750. case 105:
  1751. SERIAL_ECHO(MSG_M105_INVALID_EXTRUDER);
  1752. break;
  1753. case 109:
  1754. SERIAL_ECHO(MSG_M109_INVALID_EXTRUDER);
  1755. break;
  1756. }
  1757. SERIAL_ECHOLN(tmp_extruder);
  1758. return true;
  1759. }
  1760. }
  1761. return false;
  1762. }