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

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