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

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