My Marlin configs for Fabrikator Mini and CTC i3 Pro B
您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

Marlin_main.cpp 66KB

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