My Marlin configs for Fabrikator Mini and CTC i3 Pro B
Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

Marlin_main.cpp 59KB

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