My Marlin configs for Fabrikator Mini and CTC i3 Pro B
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

Marlin_main.cpp 63KB

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