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

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