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

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