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

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