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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917
  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 AUTO_BED_LEVELING_GRID
  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. // look here for descriptions of G-codes: http://linuxcnc.org/handbook/gcode/g-code.html
  52. // http://objects.reprap.org/wiki/Mendel_User_Manual:_RepRapGCodes
  53. //Implemented Codes
  54. //-------------------
  55. // G0 -> G1
  56. // G1 - Coordinated Movement X Y Z E
  57. // G2 - CW ARC
  58. // G3 - CCW ARC
  59. // G4 - Dwell S<seconds> or P<milliseconds>
  60. // G10 - retract filament according to settings of M207
  61. // G11 - retract recover filament according to settings of M208
  62. // G28 - Home all Axis
  63. // G29 - Detailed Z-Probe, probes the bed at 3 or more points. Will fail if you haven't homed yet.
  64. // G30 - Single Z Probe, probes bed at current XY location.
  65. // G31 - Dock sled (Z_PROBE_SLED only)
  66. // G32 - Undock sled (Z_PROBE_SLED only)
  67. // G90 - Use Absolute Coordinates
  68. // G91 - Use Relative Coordinates
  69. // G92 - Set current position to coordinates given
  70. // M Codes
  71. // M0 - Unconditional stop - Wait for user to press a button on the LCD (Only if ULTRA_LCD is enabled)
  72. // M1 - Same as M0
  73. // M17 - Enable/Power all stepper motors
  74. // M18 - Disable all stepper motors; same as M84
  75. // M20 - List SD card
  76. // M21 - Init SD card
  77. // M22 - Release SD card
  78. // M23 - Select SD file (M23 filename.g)
  79. // M24 - Start/resume SD print
  80. // M25 - Pause SD print
  81. // M26 - Set SD position in bytes (M26 S12345)
  82. // M27 - Report SD print status
  83. // M28 - Start SD write (M28 filename.g)
  84. // M29 - Stop SD write
  85. // M30 - Delete file from SD (M30 filename.g)
  86. // M31 - Output time since last M109 or SD card start to serial
  87. // M32 - Select file and start SD print (Can be used _while_ printing from SD card files):
  88. // syntax "M32 /path/filename#", or "M32 S<startpos bytes> !filename#"
  89. // Call gcode file : "M32 P !filename#" and return to caller file after finishing (similar to #include).
  90. // The '#' is necessary when calling from within sd files, as it stops buffer prereading
  91. // 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.
  92. // M80 - Turn on Power Supply
  93. // M81 - Turn off Power Supply
  94. // M82 - Set E codes absolute (default)
  95. // M83 - Set E codes relative while in Absolute Coordinates (G90) mode
  96. // M84 - Disable steppers until next move,
  97. // or use S<seconds> to specify an inactivity timeout, after which the steppers will be disabled. S0 to disable the timeout.
  98. // M85 - Set inactivity shutdown timer with parameter S<seconds>. To disable set zero (default)
  99. // M92 - Set axis_steps_per_unit - same syntax as G92
  100. // M104 - Set extruder target temp
  101. // M105 - Read current temp
  102. // M106 - Fan on
  103. // M107 - Fan off
  104. // M109 - Sxxx Wait for extruder current temp to reach target temp. Waits only when heating
  105. // Rxxx Wait for extruder current temp to reach target temp. Waits when heating and cooling
  106. // IF AUTOTEMP is enabled, S<mintemp> B<maxtemp> F<factor>. Exit autotemp by any M109 without F
  107. // M112 - Emergency stop
  108. // M114 - Output current position to serial port
  109. // M115 - Capabilities string
  110. // M117 - display message
  111. // M119 - Output Endstop status to serial port
  112. // M126 - Solenoid Air Valve Open (BariCUDA support by jmil)
  113. // M127 - Solenoid Air Valve Closed (BariCUDA vent to atmospheric pressure by jmil)
  114. // M128 - EtoP Open (BariCUDA EtoP = electricity to air pressure transducer by jmil)
  115. // M129 - EtoP Closed (BariCUDA EtoP = electricity to air pressure transducer by jmil)
  116. // M140 - Set bed target temp
  117. // M150 - Set BlinkM Color Output R: Red<0-255> U(!): Green<0-255> B: Blue<0-255> over i2c, G for green does not work.
  118. // M190 - Sxxx Wait for bed current temp to reach target temp. Waits only when heating
  119. // Rxxx Wait for bed current temp to reach target temp. Waits when heating and cooling
  120. // M200 D<millimeters>- set filament diameter and set E axis units to cubic millimeters (use S0 to set back to millimeters).
  121. // M201 - Set max acceleration in units/s^2 for print moves (M201 X1000 Y1000)
  122. // M202 - Set max acceleration in units/s^2 for travel moves (M202 X1000 Y1000) Unused in Marlin!!
  123. // M203 - Set maximum feedrate that your machine can sustain (M203 X200 Y200 Z300 E10000) in mm/sec
  124. // M204 - Set default acceleration: S normal moves T filament only moves (M204 S3000 T7000) in mm/sec^2 also sets minimum segment time in ms (B20000) to prevent buffer under-runs and M20 minimum feedrate
  125. // 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
  126. // M206 - Set additional homing offset
  127. // M207 - Set retract length S[positive mm] F[feedrate mm/min] Z[additional zlift/hop], stays in mm regardless of M200 setting
  128. // M208 - Set recover=unretract length S[positive mm surplus to the M207 S*] F[feedrate mm/sec]
  129. // 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.
  130. // M218 - Set hotend offset (in mm): T<extruder_number> X<offset_on_X> Y<offset_on_Y>
  131. // M220 S<factor in percent>- set speed factor override percentage
  132. // M221 S<factor in percent>- set extrude factor override percentage
  133. // M226 P<pin number> S<pin state>- Wait until the specified pin reaches the state required
  134. // M240 - Trigger a camera to take a photograph
  135. // M250 - Set LCD contrast C<contrast value> (value 0..63)
  136. // M280 - Set servo position absolute. P: servo index, S: angle or microseconds
  137. // M300 - Play beep sound S<frequency Hz> P<duration ms>
  138. // M301 - Set PID parameters P I and D
  139. // M302 - Allow cold extrudes, or set the minimum extrude S<temperature>.
  140. // M303 - PID relay autotune S<temperature> sets the target temperature. (default target temperature = 150C)
  141. // M304 - Set bed PID parameters P I and D
  142. // M380 - Activate solenoid on active extruder
  143. // M381 - Disable all solenoids
  144. // M400 - Finish all moves
  145. // M401 - Lower z-probe if present
  146. // M402 - Raise z-probe if present
  147. // M404 - N<dia in mm> Enter the nominal filament width (3mm, 1.75mm ) or will display nominal filament width without parameters
  148. // M405 - Turn on Filament Sensor extrusion control. Optional D<delay in cm> to set delay in centimeters between sensor and extruder
  149. // M406 - Turn off Filament Sensor extrusion control
  150. // M407 - Displays measured filament diameter
  151. // M500 - Store parameters in EEPROM
  152. // M501 - Read parameters from EEPROM (if you need reset them after you changed them temporarily).
  153. // M502 - Revert to the default "factory settings". You still need to store them in EEPROM afterwards if you want to.
  154. // M503 - Print the current settings (from memory not from EEPROM). Use S0 to leave off headings.
  155. // M540 - Use S[0|1] to enable or disable the stop SD card print on endstop hit (requires ABORT_ON_ENDSTOP_HIT_FEATURE_ENABLED)
  156. // M600 - Pause for filament change X[pos] Y[pos] Z[relative lift] E[initial retract] L[later retract distance for removal]
  157. // M665 - Set delta configurations
  158. // M666 - Set delta endstop adjustment
  159. // M605 - Set dual x-carriage movement mode: S<mode> [ X<duplication x-offset> R<duplication temp offset> ]
  160. // M907 - Set digital trimpot motor current using axis codes.
  161. // M908 - Control digital trimpot directly.
  162. // M350 - Set microstepping mode.
  163. // M351 - Toggle MS1 MS2 pins directly.
  164. // ************ SCARA Specific - This can change to suit future G-code regulations
  165. // M360 - SCARA calibration: Move to cal-position ThetaA (0 deg calibration)
  166. // M361 - SCARA calibration: Move to cal-position ThetaB (90 deg calibration - steps per degree)
  167. // M362 - SCARA calibration: Move to cal-position PsiA (0 deg calibration)
  168. // M363 - SCARA calibration: Move to cal-position PsiB (90 deg calibration - steps per degree)
  169. // M364 - SCARA calibration: Move to cal-position PSIC (90 deg to Theta calibration position)
  170. // M365 - SCARA calibration: Scaling factor, X, Y, Z axis
  171. //************* SCARA End ***************
  172. // M928 - Start SD logging (M928 filename.g) - ended by M29
  173. // M999 - Restart after being stopped by error
  174. #ifdef SDSUPPORT
  175. CardReader card;
  176. #endif
  177. float homing_feedrate[] = HOMING_FEEDRATE;
  178. bool axis_relative_modes[] = AXIS_RELATIVE_MODES;
  179. int feedmultiply = 100; //100->1 200->2
  180. int saved_feedmultiply;
  181. int extrudemultiply = 100; //100->1 200->2
  182. int extruder_multiply[EXTRUDERS] = { 100
  183. #if EXTRUDERS > 1
  184. , 100
  185. #if EXTRUDERS > 2
  186. , 100
  187. #if EXTRUDERS > 3
  188. , 100
  189. #endif
  190. #endif
  191. #endif
  192. };
  193. bool volumetric_enabled = false;
  194. float filament_size[EXTRUDERS] = { DEFAULT_NOMINAL_FILAMENT_DIA
  195. #if EXTRUDERS > 1
  196. , DEFAULT_NOMINAL_FILAMENT_DIA
  197. #if EXTRUDERS > 2
  198. , DEFAULT_NOMINAL_FILAMENT_DIA
  199. #if EXTRUDERS > 3
  200. , DEFAULT_NOMINAL_FILAMENT_DIA
  201. #endif
  202. #endif
  203. #endif
  204. };
  205. float volumetric_multiplier[EXTRUDERS] = {1.0
  206. #if EXTRUDERS > 1
  207. , 1.0
  208. #if EXTRUDERS > 2
  209. , 1.0
  210. #if EXTRUDERS > 3
  211. , 1.0
  212. #endif
  213. #endif
  214. #endif
  215. };
  216. float current_position[NUM_AXIS] = { 0.0, 0.0, 0.0, 0.0 };
  217. float add_homing[3] = { 0, 0, 0 };
  218. #ifdef DELTA
  219. float endstop_adj[3] = { 0, 0, 0 };
  220. #endif
  221. float min_pos[3] = { X_MIN_POS, Y_MIN_POS, Z_MIN_POS };
  222. float max_pos[3] = { X_MAX_POS, Y_MAX_POS, Z_MAX_POS };
  223. bool axis_known_position[3] = { false, false, false };
  224. float zprobe_zoffset;
  225. // Extruder offset
  226. #if EXTRUDERS > 1
  227. #ifndef DUAL_X_CARRIAGE
  228. #define NUM_EXTRUDER_OFFSETS 2 // only in XY plane
  229. #else
  230. #define NUM_EXTRUDER_OFFSETS 3 // supports offsets in XYZ plane
  231. #endif
  232. float extruder_offset[NUM_EXTRUDER_OFFSETS][EXTRUDERS] = {
  233. #if defined(EXTRUDER_OFFSET_X)
  234. EXTRUDER_OFFSET_X
  235. #else
  236. 0
  237. #endif
  238. ,
  239. #if defined(EXTRUDER_OFFSET_Y)
  240. EXTRUDER_OFFSET_Y
  241. #else
  242. 0
  243. #endif
  244. };
  245. #endif
  246. uint8_t active_extruder = 0;
  247. int fanSpeed = 0;
  248. #ifdef SERVO_ENDSTOPS
  249. int servo_endstops[] = SERVO_ENDSTOPS;
  250. int servo_endstop_angles[] = SERVO_ENDSTOP_ANGLES;
  251. #endif
  252. #ifdef BARICUDA
  253. int ValvePressure = 0;
  254. int EtoPPressure = 0;
  255. #endif
  256. #ifdef FWRETRACT
  257. bool autoretract_enabled = false;
  258. bool retracted[EXTRUDERS] = { false
  259. #if EXTRUDERS > 1
  260. , false
  261. #if EXTRUDERS > 2
  262. , false
  263. #if EXTRUDERS > 3
  264. , false
  265. #endif
  266. #endif
  267. #endif
  268. };
  269. bool retracted_swap[EXTRUDERS] = { false
  270. #if EXTRUDERS > 1
  271. , false
  272. #if EXTRUDERS > 2
  273. , false
  274. #if EXTRUDERS > 3
  275. , false
  276. #endif
  277. #endif
  278. #endif
  279. };
  280. float retract_length = RETRACT_LENGTH;
  281. float retract_length_swap = RETRACT_LENGTH_SWAP;
  282. float retract_feedrate = RETRACT_FEEDRATE;
  283. float retract_zlift = RETRACT_ZLIFT;
  284. float retract_recover_length = RETRACT_RECOVER_LENGTH;
  285. float retract_recover_length_swap = RETRACT_RECOVER_LENGTH_SWAP;
  286. float retract_recover_feedrate = RETRACT_RECOVER_FEEDRATE;
  287. #endif // FWRETRACT
  288. #ifdef ULTIPANEL
  289. bool powersupply =
  290. #ifdef PS_DEFAULT_OFF
  291. false
  292. #else
  293. true
  294. #endif
  295. ;
  296. #endif
  297. #ifdef DELTA
  298. float delta[3] = { 0, 0, 0 };
  299. #define SIN_60 0.8660254037844386
  300. #define COS_60 0.5
  301. // these are the default values, can be overriden with M665
  302. float delta_radius = DELTA_RADIUS;
  303. float delta_tower1_x = -SIN_60 * delta_radius; // front left tower
  304. float delta_tower1_y = -COS_60 * delta_radius;
  305. float delta_tower2_x = SIN_60 * delta_radius; // front right tower
  306. float delta_tower2_y = -COS_60 * delta_radius;
  307. float delta_tower3_x = 0; // back middle tower
  308. float delta_tower3_y = delta_radius;
  309. float delta_diagonal_rod = DELTA_DIAGONAL_ROD;
  310. float delta_diagonal_rod_2 = sq(delta_diagonal_rod);
  311. float delta_segments_per_second = DELTA_SEGMENTS_PER_SECOND;
  312. #endif
  313. #ifdef SCARA
  314. float axis_scaling[3] = { 1, 1, 1 }; // Build size scaling, default to 1
  315. #endif
  316. bool cancel_heatup = false;
  317. #ifdef FILAMENT_SENSOR
  318. //Variables for Filament Sensor input
  319. float filament_width_nominal=DEFAULT_NOMINAL_FILAMENT_DIA; //Set nominal filament width, can be changed with M404
  320. bool filament_sensor=false; //M405 turns on filament_sensor control, M406 turns it off
  321. float filament_width_meas=DEFAULT_MEASURED_FILAMENT_DIA; //Stores the measured filament diameter
  322. signed char measurement_delay[MAX_MEASUREMENT_DELAY+1]; //ring buffer to delay measurement store extruder factor after subtracting 100
  323. int delay_index1=0; //index into ring buffer
  324. int delay_index2=-1; //index into ring buffer - set to -1 on startup to indicate ring buffer needs to be initialized
  325. float delay_dist=0; //delay distance counter
  326. int meas_delay_cm = MEASUREMENT_DELAY_CM; //distance delay setting
  327. #endif
  328. const char errormagic[] PROGMEM = "Error:";
  329. const char echomagic[] PROGMEM = "echo:";
  330. const char axis_codes[NUM_AXIS] = {'X', 'Y', 'Z', 'E'};
  331. static float destination[NUM_AXIS] = { 0, 0, 0, 0 };
  332. #ifndef DELTA
  333. static float delta[3] = { 0, 0, 0 };
  334. #endif
  335. static float offset[3] = { 0, 0, 0 };
  336. static bool home_all_axis = true;
  337. static float feedrate = 1500.0, next_feedrate, saved_feedrate;
  338. static long gcode_N, gcode_LastN, Stopped_gcode_LastN = 0;
  339. static bool relative_mode = false; //Determines Absolute or Relative Coordinates
  340. static char cmdbuffer[BUFSIZE][MAX_CMD_SIZE];
  341. static bool fromsd[BUFSIZE];
  342. static int bufindr = 0;
  343. static int bufindw = 0;
  344. static int buflen = 0;
  345. static char serial_char;
  346. static int serial_count = 0;
  347. static boolean comment_mode = false;
  348. static char *strchr_pointer; ///< A pointer to find chars in the command string (X, Y, Z, E, etc.)
  349. const char* queued_commands_P= NULL; /* pointer to the current line in the active sequence of commands, or NULL when none */
  350. const int sensitive_pins[] = SENSITIVE_PINS; ///< Sensitive pin list for M42
  351. // Inactivity shutdown
  352. static unsigned long previous_millis_cmd = 0;
  353. static unsigned long max_inactive_time = 0;
  354. static unsigned long stepper_inactive_time = DEFAULT_STEPPER_DEACTIVE_TIME*1000l;
  355. unsigned long starttime = 0; ///< Print job start time
  356. unsigned long stoptime = 0; ///< Print job stop time
  357. static uint8_t tmp_extruder;
  358. bool Stopped = false;
  359. #if NUM_SERVOS > 0
  360. Servo servos[NUM_SERVOS];
  361. #endif
  362. bool CooldownNoWait = true;
  363. bool target_direction;
  364. #ifdef CHDK
  365. unsigned long chdkHigh = 0;
  366. boolean chdkActive = false;
  367. #endif
  368. //===========================================================================
  369. //=============================Routines======================================
  370. //===========================================================================
  371. void get_arc_coordinates();
  372. bool setTargetedHotend(int code);
  373. void serial_echopair_P(const char *s_P, float v)
  374. { serialprintPGM(s_P); SERIAL_ECHO(v); }
  375. void serial_echopair_P(const char *s_P, double v)
  376. { serialprintPGM(s_P); SERIAL_ECHO(v); }
  377. void serial_echopair_P(const char *s_P, unsigned long v)
  378. { serialprintPGM(s_P); SERIAL_ECHO(v); }
  379. #ifdef SDSUPPORT
  380. #include "SdFatUtil.h"
  381. int freeMemory() { return SdFatUtil::FreeRam(); }
  382. #else
  383. extern "C" {
  384. extern unsigned int __bss_end;
  385. extern unsigned int __heap_start;
  386. extern void *__brkval;
  387. int freeMemory() {
  388. int free_memory;
  389. if ((int)__brkval == 0)
  390. free_memory = ((int)&free_memory) - ((int)&__bss_end);
  391. else
  392. free_memory = ((int)&free_memory) - ((int)__brkval);
  393. return free_memory;
  394. }
  395. }
  396. #endif //!SDSUPPORT
  397. //Injects the next command from the pending sequence of commands, when possible
  398. //Return false if and only if no command was pending
  399. static bool drain_queued_commands_P()
  400. {
  401. char cmd[30];
  402. if(!queued_commands_P)
  403. return false;
  404. // Get the next 30 chars from the sequence of gcodes to run
  405. strncpy_P(cmd, queued_commands_P, sizeof(cmd)-1);
  406. cmd[sizeof(cmd)-1]= 0;
  407. // Look for the end of line, or the end of sequence
  408. size_t i= 0;
  409. char c;
  410. while( (c= cmd[i]) && c!='\n' )
  411. ++i; // look for the end of this gcode command
  412. cmd[i]= 0;
  413. if(enquecommand(cmd)) // buffer was not full (else we will retry later)
  414. {
  415. if(c)
  416. queued_commands_P+= i+1; // move to next command
  417. else
  418. queued_commands_P= NULL; // will have no more commands in the sequence
  419. }
  420. return true;
  421. }
  422. //Record one or many commands to run from program memory.
  423. //Aborts the current queue, if any.
  424. //Note: drain_queued_commands_P() must be called repeatedly to drain the commands afterwards
  425. void enquecommands_P(const char* pgcode)
  426. {
  427. queued_commands_P= pgcode;
  428. drain_queued_commands_P(); // first command exectuted asap (when possible)
  429. }
  430. //adds a single command to the main command buffer, from RAM
  431. //that is really done in a non-safe way.
  432. //needs overworking someday
  433. //Returns false if it failed to do so
  434. bool enquecommand(const char *cmd)
  435. {
  436. if(*cmd==';')
  437. return false;
  438. if(buflen >= BUFSIZE)
  439. return false;
  440. //this is dangerous if a mixing of serial and this happens
  441. strcpy(&(cmdbuffer[bufindw][0]),cmd);
  442. SERIAL_ECHO_START;
  443. SERIAL_ECHOPGM(MSG_Enqueing);
  444. SERIAL_ECHO(cmdbuffer[bufindw]);
  445. SERIAL_ECHOLNPGM("\"");
  446. bufindw= (bufindw + 1)%BUFSIZE;
  447. buflen += 1;
  448. return true;
  449. }
  450. void setup_killpin()
  451. {
  452. #if defined(KILL_PIN) && KILL_PIN > -1
  453. SET_INPUT(KILL_PIN);
  454. WRITE(KILL_PIN,HIGH);
  455. #endif
  456. }
  457. // Set home pin
  458. void setup_homepin(void)
  459. {
  460. #if defined(HOME_PIN) && HOME_PIN > -1
  461. SET_INPUT(HOME_PIN);
  462. WRITE(HOME_PIN,HIGH);
  463. #endif
  464. }
  465. void setup_photpin()
  466. {
  467. #if defined(PHOTOGRAPH_PIN) && PHOTOGRAPH_PIN > -1
  468. OUT_WRITE(PHOTOGRAPH_PIN, LOW);
  469. #endif
  470. }
  471. void setup_powerhold()
  472. {
  473. #if defined(SUICIDE_PIN) && SUICIDE_PIN > -1
  474. OUT_WRITE(SUICIDE_PIN, HIGH);
  475. #endif
  476. #if defined(PS_ON_PIN) && PS_ON_PIN > -1
  477. #if defined(PS_DEFAULT_OFF)
  478. OUT_WRITE(PS_ON_PIN, PS_ON_ASLEEP);
  479. #else
  480. OUT_WRITE(PS_ON_PIN, PS_ON_AWAKE);
  481. #endif
  482. #endif
  483. }
  484. void suicide()
  485. {
  486. #if defined(SUICIDE_PIN) && SUICIDE_PIN > -1
  487. OUT_WRITE(SUICIDE_PIN, LOW);
  488. #endif
  489. }
  490. void servo_init()
  491. {
  492. #if (NUM_SERVOS >= 1) && defined(SERVO0_PIN) && (SERVO0_PIN > -1)
  493. servos[0].attach(SERVO0_PIN);
  494. #endif
  495. #if (NUM_SERVOS >= 2) && defined(SERVO1_PIN) && (SERVO1_PIN > -1)
  496. servos[1].attach(SERVO1_PIN);
  497. #endif
  498. #if (NUM_SERVOS >= 3) && defined(SERVO2_PIN) && (SERVO2_PIN > -1)
  499. servos[2].attach(SERVO2_PIN);
  500. #endif
  501. #if (NUM_SERVOS >= 4) && defined(SERVO3_PIN) && (SERVO3_PIN > -1)
  502. servos[3].attach(SERVO3_PIN);
  503. #endif
  504. #if (NUM_SERVOS >= 5)
  505. #error "TODO: enter initalisation code for more servos"
  506. #endif
  507. // Set position of Servo Endstops that are defined
  508. #ifdef SERVO_ENDSTOPS
  509. for(int8_t i = 0; i < 3; i++)
  510. {
  511. if(servo_endstops[i] > -1) {
  512. servos[servo_endstops[i]].write(servo_endstop_angles[i * 2 + 1]);
  513. }
  514. }
  515. #endif
  516. #if defined (ENABLE_AUTO_BED_LEVELING) && (PROBE_SERVO_DEACTIVATION_DELAY > 0)
  517. delay(PROBE_SERVO_DEACTIVATION_DELAY);
  518. servos[servo_endstops[Z_AXIS]].detach();
  519. #endif
  520. }
  521. void setup()
  522. {
  523. setup_killpin();
  524. setup_powerhold();
  525. MYSERIAL.begin(BAUDRATE);
  526. SERIAL_PROTOCOLLNPGM("start");
  527. SERIAL_ECHO_START;
  528. // Check startup - does nothing if bootloader sets MCUSR to 0
  529. byte mcu = MCUSR;
  530. if(mcu & 1) SERIAL_ECHOLNPGM(MSG_POWERUP);
  531. if(mcu & 2) SERIAL_ECHOLNPGM(MSG_EXTERNAL_RESET);
  532. if(mcu & 4) SERIAL_ECHOLNPGM(MSG_BROWNOUT_RESET);
  533. if(mcu & 8) SERIAL_ECHOLNPGM(MSG_WATCHDOG_RESET);
  534. if(mcu & 32) SERIAL_ECHOLNPGM(MSG_SOFTWARE_RESET);
  535. MCUSR=0;
  536. SERIAL_ECHOPGM(MSG_MARLIN);
  537. SERIAL_ECHOLNPGM(STRING_VERSION);
  538. #ifdef STRING_VERSION_CONFIG_H
  539. #ifdef STRING_CONFIG_H_AUTHOR
  540. SERIAL_ECHO_START;
  541. SERIAL_ECHOPGM(MSG_CONFIGURATION_VER);
  542. SERIAL_ECHOPGM(STRING_VERSION_CONFIG_H);
  543. SERIAL_ECHOPGM(MSG_AUTHOR);
  544. SERIAL_ECHOLNPGM(STRING_CONFIG_H_AUTHOR);
  545. SERIAL_ECHOPGM("Compiled: ");
  546. SERIAL_ECHOLNPGM(__DATE__);
  547. #endif // STRING_CONFIG_H_AUTHOR
  548. #endif // STRING_VERSION_CONFIG_H
  549. SERIAL_ECHO_START;
  550. SERIAL_ECHOPGM(MSG_FREE_MEMORY);
  551. SERIAL_ECHO(freeMemory());
  552. SERIAL_ECHOPGM(MSG_PLANNER_BUFFER_BYTES);
  553. SERIAL_ECHOLN((int)sizeof(block_t)*BLOCK_BUFFER_SIZE);
  554. for(int8_t i = 0; i < BUFSIZE; i++)
  555. {
  556. fromsd[i] = false;
  557. }
  558. // loads data from EEPROM if available else uses defaults (and resets step acceleration rate)
  559. Config_RetrieveSettings();
  560. tp_init(); // Initialize temperature loop
  561. plan_init(); // Initialize planner;
  562. watchdog_init();
  563. st_init(); // Initialize stepper, this enables interrupts!
  564. setup_photpin();
  565. servo_init();
  566. lcd_init();
  567. _delay_ms(1000); // wait 1sec to display the splash screen
  568. #if defined(CONTROLLERFAN_PIN) && CONTROLLERFAN_PIN > -1
  569. SET_OUTPUT(CONTROLLERFAN_PIN); //Set pin used for driver cooling fan
  570. #endif
  571. #ifdef DIGIPOT_I2C
  572. digipot_i2c_init();
  573. #endif
  574. #ifdef Z_PROBE_SLED
  575. pinMode(SERVO0_PIN, OUTPUT);
  576. digitalWrite(SERVO0_PIN, LOW); // turn it off
  577. #endif // Z_PROBE_SLED
  578. setup_homepin();
  579. #ifdef STAT_LED_RED
  580. pinMode(STAT_LED_RED, OUTPUT);
  581. digitalWrite(STAT_LED_RED, LOW); // turn it off
  582. #endif
  583. #ifdef STAT_LED_BLUE
  584. pinMode(STAT_LED_BLUE, OUTPUT);
  585. digitalWrite(STAT_LED_BLUE, LOW); // turn it off
  586. #endif
  587. }
  588. void loop()
  589. {
  590. if(buflen < (BUFSIZE-1))
  591. get_command();
  592. #ifdef SDSUPPORT
  593. card.checkautostart(false);
  594. #endif
  595. if(buflen)
  596. {
  597. #ifdef SDSUPPORT
  598. if(card.saving)
  599. {
  600. if(strstr_P(cmdbuffer[bufindr], PSTR("M29")) == NULL)
  601. {
  602. card.write_command(cmdbuffer[bufindr]);
  603. if(card.logging)
  604. {
  605. process_commands();
  606. }
  607. else
  608. {
  609. SERIAL_PROTOCOLLNPGM(MSG_OK);
  610. }
  611. }
  612. else
  613. {
  614. card.closefile();
  615. SERIAL_PROTOCOLLNPGM(MSG_FILE_SAVED);
  616. }
  617. }
  618. else
  619. {
  620. process_commands();
  621. }
  622. #else
  623. process_commands();
  624. #endif //SDSUPPORT
  625. buflen = (buflen-1);
  626. bufindr = (bufindr + 1)%BUFSIZE;
  627. }
  628. //check heater every n milliseconds
  629. manage_heater();
  630. manage_inactivity();
  631. checkHitEndstops();
  632. lcd_update();
  633. }
  634. void get_command()
  635. {
  636. if(drain_queued_commands_P()) // priority is given to non-serial commands
  637. return;
  638. while( MYSERIAL.available() > 0 && buflen < BUFSIZE) {
  639. serial_char = MYSERIAL.read();
  640. if(serial_char == '\n' ||
  641. serial_char == '\r' ||
  642. (serial_char == ':' && comment_mode == false) ||
  643. serial_count >= (MAX_CMD_SIZE - 1) )
  644. {
  645. if(!serial_count) { //if empty line
  646. comment_mode = false; //for new command
  647. return;
  648. }
  649. cmdbuffer[bufindw][serial_count] = 0; //terminate string
  650. if(!comment_mode){
  651. comment_mode = false; //for new command
  652. fromsd[bufindw] = false;
  653. if(strchr(cmdbuffer[bufindw], 'N') != NULL)
  654. {
  655. strchr_pointer = strchr(cmdbuffer[bufindw], 'N');
  656. gcode_N = (strtol(&cmdbuffer[bufindw][strchr_pointer - cmdbuffer[bufindw] + 1], NULL, 10));
  657. if(gcode_N != gcode_LastN+1 && (strstr_P(cmdbuffer[bufindw], PSTR("M110")) == NULL) ) {
  658. SERIAL_ERROR_START;
  659. SERIAL_ERRORPGM(MSG_ERR_LINE_NO);
  660. SERIAL_ERRORLN(gcode_LastN);
  661. //Serial.println(gcode_N);
  662. FlushSerialRequestResend();
  663. serial_count = 0;
  664. return;
  665. }
  666. if(strchr(cmdbuffer[bufindw], '*') != NULL)
  667. {
  668. byte checksum = 0;
  669. byte count = 0;
  670. while(cmdbuffer[bufindw][count] != '*') checksum = checksum^cmdbuffer[bufindw][count++];
  671. strchr_pointer = strchr(cmdbuffer[bufindw], '*');
  672. if( (int)(strtod(&cmdbuffer[bufindw][strchr_pointer - cmdbuffer[bufindw] + 1], NULL)) != checksum) {
  673. SERIAL_ERROR_START;
  674. SERIAL_ERRORPGM(MSG_ERR_CHECKSUM_MISMATCH);
  675. SERIAL_ERRORLN(gcode_LastN);
  676. FlushSerialRequestResend();
  677. serial_count = 0;
  678. return;
  679. }
  680. //if no errors, continue parsing
  681. }
  682. else
  683. {
  684. SERIAL_ERROR_START;
  685. SERIAL_ERRORPGM(MSG_ERR_NO_CHECKSUM);
  686. SERIAL_ERRORLN(gcode_LastN);
  687. FlushSerialRequestResend();
  688. serial_count = 0;
  689. return;
  690. }
  691. gcode_LastN = gcode_N;
  692. //if no errors, continue parsing
  693. }
  694. else // if we don't receive 'N' but still see '*'
  695. {
  696. if((strchr(cmdbuffer[bufindw], '*') != NULL))
  697. {
  698. SERIAL_ERROR_START;
  699. SERIAL_ERRORPGM(MSG_ERR_NO_LINENUMBER_WITH_CHECKSUM);
  700. SERIAL_ERRORLN(gcode_LastN);
  701. serial_count = 0;
  702. return;
  703. }
  704. }
  705. if((strchr(cmdbuffer[bufindw], 'G') != NULL)){
  706. strchr_pointer = strchr(cmdbuffer[bufindw], 'G');
  707. switch((int)((strtod(&cmdbuffer[bufindw][strchr_pointer - cmdbuffer[bufindw] + 1], NULL)))){
  708. case 0:
  709. case 1:
  710. case 2:
  711. case 3:
  712. if (Stopped == true) {
  713. SERIAL_ERRORLNPGM(MSG_ERR_STOPPED);
  714. LCD_MESSAGEPGM(MSG_STOPPED);
  715. }
  716. break;
  717. default:
  718. break;
  719. }
  720. }
  721. //If command was e-stop process now
  722. if(strcmp(cmdbuffer[bufindw], "M112") == 0)
  723. kill();
  724. bufindw = (bufindw + 1)%BUFSIZE;
  725. buflen += 1;
  726. }
  727. serial_count = 0; //clear buffer
  728. }
  729. else
  730. {
  731. if(serial_char == ';') comment_mode = true;
  732. if(!comment_mode) cmdbuffer[bufindw][serial_count++] = serial_char;
  733. }
  734. }
  735. #ifdef SDSUPPORT
  736. if(!card.sdprinting || serial_count!=0){
  737. return;
  738. }
  739. //'#' stops reading from SD to the buffer prematurely, so procedural macro calls are possible
  740. // if it occurs, stop_buffering is triggered and the buffer is ran dry.
  741. // this character _can_ occur in serial com, due to checksums. however, no checksums are used in SD printing
  742. static bool stop_buffering=false;
  743. if(buflen==0) stop_buffering=false;
  744. while( !card.eof() && buflen < BUFSIZE && !stop_buffering) {
  745. int16_t n=card.get();
  746. serial_char = (char)n;
  747. if(serial_char == '\n' ||
  748. serial_char == '\r' ||
  749. (serial_char == '#' && comment_mode == false) ||
  750. (serial_char == ':' && comment_mode == false) ||
  751. serial_count >= (MAX_CMD_SIZE - 1)||n==-1)
  752. {
  753. if(card.eof()){
  754. SERIAL_PROTOCOLLNPGM(MSG_FILE_PRINTED);
  755. stoptime=millis();
  756. char time[30];
  757. unsigned long t=(stoptime-starttime)/1000;
  758. int hours, minutes;
  759. minutes=(t/60)%60;
  760. hours=t/60/60;
  761. sprintf_P(time, PSTR("%i hours %i minutes"),hours, minutes);
  762. SERIAL_ECHO_START;
  763. SERIAL_ECHOLN(time);
  764. lcd_setstatus(time);
  765. card.printingHasFinished();
  766. card.checkautostart(true);
  767. }
  768. if(serial_char=='#')
  769. stop_buffering=true;
  770. if(!serial_count)
  771. {
  772. comment_mode = false; //for new command
  773. return; //if empty line
  774. }
  775. cmdbuffer[bufindw][serial_count] = 0; //terminate string
  776. // if(!comment_mode){
  777. fromsd[bufindw] = true;
  778. buflen += 1;
  779. bufindw = (bufindw + 1)%BUFSIZE;
  780. // }
  781. comment_mode = false; //for new command
  782. serial_count = 0; //clear buffer
  783. }
  784. else
  785. {
  786. if(serial_char == ';') comment_mode = true;
  787. if(!comment_mode) cmdbuffer[bufindw][serial_count++] = serial_char;
  788. }
  789. }
  790. #endif //SDSUPPORT
  791. }
  792. float code_value()
  793. {
  794. return (strtod(&cmdbuffer[bufindr][strchr_pointer - cmdbuffer[bufindr] + 1], NULL));
  795. }
  796. long code_value_long()
  797. {
  798. return (strtol(&cmdbuffer[bufindr][strchr_pointer - cmdbuffer[bufindr] + 1], NULL, 10));
  799. }
  800. bool code_seen(char code)
  801. {
  802. strchr_pointer = strchr(cmdbuffer[bufindr], code);
  803. return (strchr_pointer != NULL); //Return True if a character was found
  804. }
  805. #define DEFINE_PGM_READ_ANY(type, reader) \
  806. static inline type pgm_read_any(const type *p) \
  807. { return pgm_read_##reader##_near(p); }
  808. DEFINE_PGM_READ_ANY(float, float);
  809. DEFINE_PGM_READ_ANY(signed char, byte);
  810. #define XYZ_CONSTS_FROM_CONFIG(type, array, CONFIG) \
  811. static const PROGMEM type array##_P[3] = \
  812. { X_##CONFIG, Y_##CONFIG, Z_##CONFIG }; \
  813. static inline type array(int axis) \
  814. { return pgm_read_any(&array##_P[axis]); }
  815. XYZ_CONSTS_FROM_CONFIG(float, base_min_pos, MIN_POS);
  816. XYZ_CONSTS_FROM_CONFIG(float, base_max_pos, MAX_POS);
  817. XYZ_CONSTS_FROM_CONFIG(float, base_home_pos, HOME_POS);
  818. XYZ_CONSTS_FROM_CONFIG(float, max_length, MAX_LENGTH);
  819. XYZ_CONSTS_FROM_CONFIG(float, home_retract_mm, HOME_RETRACT_MM);
  820. XYZ_CONSTS_FROM_CONFIG(signed char, home_dir, HOME_DIR);
  821. #ifdef DUAL_X_CARRIAGE
  822. #if EXTRUDERS == 1 || defined(COREXY) \
  823. || !defined(X2_ENABLE_PIN) || !defined(X2_STEP_PIN) || !defined(X2_DIR_PIN) \
  824. || !defined(X2_HOME_POS) || !defined(X2_MIN_POS) || !defined(X2_MAX_POS) \
  825. || !defined(X_MAX_PIN) || X_MAX_PIN < 0
  826. #error "Missing or invalid definitions for DUAL_X_CARRIAGE mode."
  827. #endif
  828. #if X_HOME_DIR != -1 || X2_HOME_DIR != 1
  829. #error "Please use canonical x-carriage assignment" // the x-carriages are defined by their homing directions
  830. #endif
  831. #define DXC_FULL_CONTROL_MODE 0
  832. #define DXC_AUTO_PARK_MODE 1
  833. #define DXC_DUPLICATION_MODE 2
  834. static int dual_x_carriage_mode = DEFAULT_DUAL_X_CARRIAGE_MODE;
  835. static float x_home_pos(int extruder) {
  836. if (extruder == 0)
  837. return base_home_pos(X_AXIS) + add_homing[X_AXIS];
  838. else
  839. // In dual carriage mode the extruder offset provides an override of the
  840. // second X-carriage offset when homed - otherwise X2_HOME_POS is used.
  841. // This allow soft recalibration of the second extruder offset position without firmware reflash
  842. // (through the M218 command).
  843. return (extruder_offset[X_AXIS][1] > 0) ? extruder_offset[X_AXIS][1] : X2_HOME_POS;
  844. }
  845. static int x_home_dir(int extruder) {
  846. return (extruder == 0) ? X_HOME_DIR : X2_HOME_DIR;
  847. }
  848. static float inactive_extruder_x_pos = X2_MAX_POS; // used in mode 0 & 1
  849. static bool active_extruder_parked = false; // used in mode 1 & 2
  850. static float raised_parked_position[NUM_AXIS]; // used in mode 1
  851. static unsigned long delayed_move_time = 0; // used in mode 1
  852. static float duplicate_extruder_x_offset = DEFAULT_DUPLICATION_X_OFFSET; // used in mode 2
  853. static float duplicate_extruder_temp_offset = 0; // used in mode 2
  854. bool extruder_duplication_enabled = false; // used in mode 2
  855. #endif //DUAL_X_CARRIAGE
  856. static void axis_is_at_home(int axis) {
  857. #ifdef DUAL_X_CARRIAGE
  858. if (axis == X_AXIS) {
  859. if (active_extruder != 0) {
  860. current_position[X_AXIS] = x_home_pos(active_extruder);
  861. min_pos[X_AXIS] = X2_MIN_POS;
  862. max_pos[X_AXIS] = max(extruder_offset[X_AXIS][1], X2_MAX_POS);
  863. return;
  864. }
  865. else if (dual_x_carriage_mode == DXC_DUPLICATION_MODE && active_extruder == 0) {
  866. current_position[X_AXIS] = base_home_pos(X_AXIS) + add_homing[X_AXIS];
  867. min_pos[X_AXIS] = base_min_pos(X_AXIS) + add_homing[X_AXIS];
  868. max_pos[X_AXIS] = min(base_max_pos(X_AXIS) + add_homing[X_AXIS],
  869. max(extruder_offset[X_AXIS][1], X2_MAX_POS) - duplicate_extruder_x_offset);
  870. return;
  871. }
  872. }
  873. #endif
  874. #ifdef SCARA
  875. float homeposition[3];
  876. char i;
  877. if (axis < 2)
  878. {
  879. for (i=0; i<3; i++)
  880. {
  881. homeposition[i] = base_home_pos(i);
  882. }
  883. // SERIAL_ECHOPGM("homeposition[x]= "); SERIAL_ECHO(homeposition[0]);
  884. // SERIAL_ECHOPGM("homeposition[y]= "); SERIAL_ECHOLN(homeposition[1]);
  885. // Works out real Homeposition angles using inverse kinematics,
  886. // and calculates homing offset using forward kinematics
  887. calculate_delta(homeposition);
  888. // SERIAL_ECHOPGM("base Theta= "); SERIAL_ECHO(delta[X_AXIS]);
  889. // SERIAL_ECHOPGM(" base Psi+Theta="); SERIAL_ECHOLN(delta[Y_AXIS]);
  890. for (i=0; i<2; i++)
  891. {
  892. delta[i] -= add_homing[i];
  893. }
  894. // SERIAL_ECHOPGM("addhome X="); SERIAL_ECHO(add_homing[X_AXIS]);
  895. // SERIAL_ECHOPGM(" addhome Y="); SERIAL_ECHO(add_homing[Y_AXIS]);
  896. // SERIAL_ECHOPGM(" addhome Theta="); SERIAL_ECHO(delta[X_AXIS]);
  897. // SERIAL_ECHOPGM(" addhome Psi+Theta="); SERIAL_ECHOLN(delta[Y_AXIS]);
  898. calculate_SCARA_forward_Transform(delta);
  899. // SERIAL_ECHOPGM("Delta X="); SERIAL_ECHO(delta[X_AXIS]);
  900. // SERIAL_ECHOPGM(" Delta Y="); SERIAL_ECHOLN(delta[Y_AXIS]);
  901. current_position[axis] = delta[axis];
  902. // SCARA home positions are based on configuration since the actual limits are determined by the
  903. // inverse kinematic transform.
  904. min_pos[axis] = base_min_pos(axis); // + (delta[axis] - base_home_pos(axis));
  905. max_pos[axis] = base_max_pos(axis); // + (delta[axis] - base_home_pos(axis));
  906. }
  907. else
  908. {
  909. current_position[axis] = base_home_pos(axis) + add_homing[axis];
  910. min_pos[axis] = base_min_pos(axis) + add_homing[axis];
  911. max_pos[axis] = base_max_pos(axis) + add_homing[axis];
  912. }
  913. #else
  914. current_position[axis] = base_home_pos(axis) + add_homing[axis];
  915. min_pos[axis] = base_min_pos(axis) + add_homing[axis];
  916. max_pos[axis] = base_max_pos(axis) + add_homing[axis];
  917. #endif
  918. }
  919. #ifdef ENABLE_AUTO_BED_LEVELING
  920. #ifdef AUTO_BED_LEVELING_GRID
  921. static void set_bed_level_equation_lsq(double *plane_equation_coefficients)
  922. {
  923. vector_3 planeNormal = vector_3(-plane_equation_coefficients[0], -plane_equation_coefficients[1], 1);
  924. planeNormal.debug("planeNormal");
  925. plan_bed_level_matrix = matrix_3x3::create_look_at(planeNormal);
  926. //bedLevel.debug("bedLevel");
  927. //plan_bed_level_matrix.debug("bed level before");
  928. //vector_3 uncorrected_position = plan_get_position_mm();
  929. //uncorrected_position.debug("position before");
  930. vector_3 corrected_position = plan_get_position();
  931. // corrected_position.debug("position after");
  932. current_position[X_AXIS] = corrected_position.x;
  933. current_position[Y_AXIS] = corrected_position.y;
  934. current_position[Z_AXIS] = corrected_position.z;
  935. // put the bed at 0 so we don't go below it.
  936. current_position[Z_AXIS] = zprobe_zoffset; // in the lsq we reach here after raising the extruder due to the loop structure
  937. plan_set_position(current_position[X_AXIS], current_position[Y_AXIS], current_position[Z_AXIS], current_position[E_AXIS]);
  938. }
  939. #else // not AUTO_BED_LEVELING_GRID
  940. static void set_bed_level_equation_3pts(float z_at_pt_1, float z_at_pt_2, float z_at_pt_3) {
  941. plan_bed_level_matrix.set_to_identity();
  942. vector_3 pt1 = vector_3(ABL_PROBE_PT_1_X, ABL_PROBE_PT_1_Y, z_at_pt_1);
  943. vector_3 pt2 = vector_3(ABL_PROBE_PT_2_X, ABL_PROBE_PT_2_Y, z_at_pt_2);
  944. vector_3 pt3 = vector_3(ABL_PROBE_PT_3_X, ABL_PROBE_PT_3_Y, z_at_pt_3);
  945. vector_3 from_2_to_1 = (pt1 - pt2).get_normal();
  946. vector_3 from_2_to_3 = (pt3 - pt2).get_normal();
  947. vector_3 planeNormal = vector_3::cross(from_2_to_1, from_2_to_3).get_normal();
  948. planeNormal = vector_3(planeNormal.x, planeNormal.y, abs(planeNormal.z));
  949. plan_bed_level_matrix = matrix_3x3::create_look_at(planeNormal);
  950. vector_3 corrected_position = plan_get_position();
  951. current_position[X_AXIS] = corrected_position.x;
  952. current_position[Y_AXIS] = corrected_position.y;
  953. current_position[Z_AXIS] = corrected_position.z;
  954. // put the bed at 0 so we don't go below it.
  955. current_position[Z_AXIS] = zprobe_zoffset;
  956. plan_set_position(current_position[X_AXIS], current_position[Y_AXIS], current_position[Z_AXIS], current_position[E_AXIS]);
  957. }
  958. #endif // AUTO_BED_LEVELING_GRID
  959. static void run_z_probe() {
  960. plan_bed_level_matrix.set_to_identity();
  961. feedrate = homing_feedrate[Z_AXIS];
  962. // move down until you find the bed
  963. float zPosition = -10;
  964. plan_buffer_line(current_position[X_AXIS], current_position[Y_AXIS], zPosition, current_position[E_AXIS], feedrate/60, active_extruder);
  965. st_synchronize();
  966. // we have to let the planner know where we are right now as it is not where we said to go.
  967. zPosition = st_get_position_mm(Z_AXIS);
  968. plan_set_position(current_position[X_AXIS], current_position[Y_AXIS], zPosition, current_position[E_AXIS]);
  969. // move up the retract distance
  970. zPosition += home_retract_mm(Z_AXIS);
  971. plan_buffer_line(current_position[X_AXIS], current_position[Y_AXIS], zPosition, current_position[E_AXIS], feedrate/60, active_extruder);
  972. st_synchronize();
  973. // move back down slowly to find bed
  974. feedrate = homing_feedrate[Z_AXIS]/4;
  975. zPosition -= home_retract_mm(Z_AXIS) * 2;
  976. plan_buffer_line(current_position[X_AXIS], current_position[Y_AXIS], zPosition, current_position[E_AXIS], feedrate/60, active_extruder);
  977. st_synchronize();
  978. current_position[Z_AXIS] = st_get_position_mm(Z_AXIS);
  979. // make sure the planner knows where we are as it may be a bit different than we last said to move to
  980. plan_set_position(current_position[X_AXIS], current_position[Y_AXIS], current_position[Z_AXIS], current_position[E_AXIS]);
  981. }
  982. static void do_blocking_move_to(float x, float y, float z) {
  983. float oldFeedRate = feedrate;
  984. feedrate = homing_feedrate[Z_AXIS];
  985. current_position[Z_AXIS] = z;
  986. plan_buffer_line(current_position[X_AXIS], current_position[Y_AXIS], current_position[Z_AXIS], current_position[E_AXIS], feedrate/60, active_extruder);
  987. st_synchronize();
  988. feedrate = XY_TRAVEL_SPEED;
  989. current_position[X_AXIS] = x;
  990. current_position[Y_AXIS] = y;
  991. plan_buffer_line(current_position[X_AXIS], current_position[Y_AXIS], current_position[Z_AXIS], current_position[E_AXIS], feedrate/60, active_extruder);
  992. st_synchronize();
  993. feedrate = oldFeedRate;
  994. }
  995. static void do_blocking_move_relative(float offset_x, float offset_y, float offset_z) {
  996. do_blocking_move_to(current_position[X_AXIS] + offset_x, current_position[Y_AXIS] + offset_y, current_position[Z_AXIS] + offset_z);
  997. }
  998. static void setup_for_endstop_move() {
  999. saved_feedrate = feedrate;
  1000. saved_feedmultiply = feedmultiply;
  1001. feedmultiply = 100;
  1002. previous_millis_cmd = millis();
  1003. enable_endstops(true);
  1004. }
  1005. static void clean_up_after_endstop_move() {
  1006. #ifdef ENDSTOPS_ONLY_FOR_HOMING
  1007. enable_endstops(false);
  1008. #endif
  1009. feedrate = saved_feedrate;
  1010. feedmultiply = saved_feedmultiply;
  1011. previous_millis_cmd = millis();
  1012. }
  1013. static void engage_z_probe() {
  1014. // Engage Z Servo endstop if enabled
  1015. #ifdef SERVO_ENDSTOPS
  1016. if (servo_endstops[Z_AXIS] > -1) {
  1017. #if defined (ENABLE_AUTO_BED_LEVELING) && (PROBE_SERVO_DEACTIVATION_DELAY > 0)
  1018. servos[servo_endstops[Z_AXIS]].attach(0);
  1019. #endif
  1020. servos[servo_endstops[Z_AXIS]].write(servo_endstop_angles[Z_AXIS * 2]);
  1021. #if defined (ENABLE_AUTO_BED_LEVELING) && (PROBE_SERVO_DEACTIVATION_DELAY > 0)
  1022. delay(PROBE_SERVO_DEACTIVATION_DELAY);
  1023. servos[servo_endstops[Z_AXIS]].detach();
  1024. #endif
  1025. }
  1026. #endif
  1027. }
  1028. static void retract_z_probe() {
  1029. // Retract Z Servo endstop if enabled
  1030. #ifdef SERVO_ENDSTOPS
  1031. if (servo_endstops[Z_AXIS] > -1) {
  1032. #if defined (ENABLE_AUTO_BED_LEVELING) && (PROBE_SERVO_DEACTIVATION_DELAY > 0)
  1033. servos[servo_endstops[Z_AXIS]].attach(0);
  1034. #endif
  1035. servos[servo_endstops[Z_AXIS]].write(servo_endstop_angles[Z_AXIS * 2 + 1]);
  1036. #if defined (ENABLE_AUTO_BED_LEVELING) && (PROBE_SERVO_DEACTIVATION_DELAY > 0)
  1037. delay(PROBE_SERVO_DEACTIVATION_DELAY);
  1038. servos[servo_endstops[Z_AXIS]].detach();
  1039. #endif
  1040. }
  1041. #endif
  1042. }
  1043. enum ProbeAction { ProbeStay, ProbeEngage, ProbeRetract, ProbeEngageRetract };
  1044. /// Probe bed height at position (x,y), returns the measured z value
  1045. static float probe_pt(float x, float y, float z_before, ProbeAction retract_action=ProbeEngageRetract) {
  1046. // move to right place
  1047. do_blocking_move_to(current_position[X_AXIS], current_position[Y_AXIS], z_before);
  1048. do_blocking_move_to(x - X_PROBE_OFFSET_FROM_EXTRUDER, y - Y_PROBE_OFFSET_FROM_EXTRUDER, current_position[Z_AXIS]);
  1049. #ifndef Z_PROBE_SLED
  1050. if (retract_action & ProbeEngage) engage_z_probe();
  1051. #endif
  1052. run_z_probe();
  1053. float measured_z = current_position[Z_AXIS];
  1054. #ifndef Z_PROBE_SLED
  1055. if (retract_action & ProbeRetract) retract_z_probe();
  1056. #endif
  1057. SERIAL_PROTOCOLPGM(MSG_BED);
  1058. SERIAL_PROTOCOLPGM(" x: ");
  1059. SERIAL_PROTOCOL(x);
  1060. SERIAL_PROTOCOLPGM(" y: ");
  1061. SERIAL_PROTOCOL(y);
  1062. SERIAL_PROTOCOLPGM(" z: ");
  1063. SERIAL_PROTOCOL(measured_z);
  1064. SERIAL_PROTOCOLPGM("\n");
  1065. return measured_z;
  1066. }
  1067. #endif // #ifdef ENABLE_AUTO_BED_LEVELING
  1068. static void homeaxis(int axis) {
  1069. #define HOMEAXIS_DO(LETTER) \
  1070. ((LETTER##_MIN_PIN > -1 && LETTER##_HOME_DIR==-1) || (LETTER##_MAX_PIN > -1 && LETTER##_HOME_DIR==1))
  1071. if (axis==X_AXIS ? HOMEAXIS_DO(X) :
  1072. axis==Y_AXIS ? HOMEAXIS_DO(Y) :
  1073. axis==Z_AXIS ? HOMEAXIS_DO(Z) :
  1074. 0) {
  1075. int axis_home_dir = home_dir(axis);
  1076. #ifdef DUAL_X_CARRIAGE
  1077. if (axis == X_AXIS)
  1078. axis_home_dir = x_home_dir(active_extruder);
  1079. #endif
  1080. current_position[axis] = 0;
  1081. plan_set_position(current_position[X_AXIS], current_position[Y_AXIS], current_position[Z_AXIS], current_position[E_AXIS]);
  1082. #ifndef Z_PROBE_SLED
  1083. // Engage Servo endstop if enabled
  1084. #ifdef SERVO_ENDSTOPS
  1085. #if defined (ENABLE_AUTO_BED_LEVELING) && (PROBE_SERVO_DEACTIVATION_DELAY > 0)
  1086. if (axis==Z_AXIS) {
  1087. engage_z_probe();
  1088. }
  1089. else
  1090. #endif
  1091. if (servo_endstops[axis] > -1) {
  1092. servos[servo_endstops[axis]].write(servo_endstop_angles[axis * 2]);
  1093. }
  1094. #endif
  1095. #endif // Z_PROBE_SLED
  1096. destination[axis] = 1.5 * max_length(axis) * axis_home_dir;
  1097. feedrate = homing_feedrate[axis];
  1098. plan_buffer_line(destination[X_AXIS], destination[Y_AXIS], destination[Z_AXIS], destination[E_AXIS], feedrate/60, active_extruder);
  1099. st_synchronize();
  1100. current_position[axis] = 0;
  1101. plan_set_position(current_position[X_AXIS], current_position[Y_AXIS], current_position[Z_AXIS], current_position[E_AXIS]);
  1102. destination[axis] = -home_retract_mm(axis) * axis_home_dir;
  1103. plan_buffer_line(destination[X_AXIS], destination[Y_AXIS], destination[Z_AXIS], destination[E_AXIS], feedrate/60, active_extruder);
  1104. st_synchronize();
  1105. destination[axis] = 2*home_retract_mm(axis) * axis_home_dir;
  1106. #ifdef DELTA
  1107. feedrate = homing_feedrate[axis]/10;
  1108. #else
  1109. feedrate = homing_feedrate[axis]/2 ;
  1110. #endif
  1111. plan_buffer_line(destination[X_AXIS], destination[Y_AXIS], destination[Z_AXIS], destination[E_AXIS], feedrate/60, active_extruder);
  1112. st_synchronize();
  1113. #ifdef DELTA
  1114. // retrace by the amount specified in endstop_adj
  1115. if (endstop_adj[axis] * axis_home_dir < 0) {
  1116. plan_set_position(current_position[X_AXIS], current_position[Y_AXIS], current_position[Z_AXIS], current_position[E_AXIS]);
  1117. destination[axis] = endstop_adj[axis];
  1118. plan_buffer_line(destination[X_AXIS], destination[Y_AXIS], destination[Z_AXIS], destination[E_AXIS], feedrate/60, active_extruder);
  1119. st_synchronize();
  1120. }
  1121. #endif
  1122. axis_is_at_home(axis);
  1123. destination[axis] = current_position[axis];
  1124. feedrate = 0.0;
  1125. endstops_hit_on_purpose();
  1126. axis_known_position[axis] = true;
  1127. // Retract Servo endstop if enabled
  1128. #ifdef SERVO_ENDSTOPS
  1129. if (servo_endstops[axis] > -1) {
  1130. servos[servo_endstops[axis]].write(servo_endstop_angles[axis * 2 + 1]);
  1131. }
  1132. #endif
  1133. #if defined (ENABLE_AUTO_BED_LEVELING) && (PROBE_SERVO_DEACTIVATION_DELAY > 0)
  1134. #ifndef Z_PROBE_SLED
  1135. if (axis==Z_AXIS) retract_z_probe();
  1136. #endif
  1137. #endif
  1138. }
  1139. }
  1140. #define HOMEAXIS(LETTER) homeaxis(LETTER##_AXIS)
  1141. void refresh_cmd_timeout(void)
  1142. {
  1143. previous_millis_cmd = millis();
  1144. }
  1145. #ifdef FWRETRACT
  1146. void retract(bool retracting, bool swapretract = false) {
  1147. if(retracting && !retracted[active_extruder]) {
  1148. destination[X_AXIS]=current_position[X_AXIS];
  1149. destination[Y_AXIS]=current_position[Y_AXIS];
  1150. destination[Z_AXIS]=current_position[Z_AXIS];
  1151. destination[E_AXIS]=current_position[E_AXIS];
  1152. if (swapretract) {
  1153. current_position[E_AXIS]+=retract_length_swap/volumetric_multiplier[active_extruder];
  1154. } else {
  1155. current_position[E_AXIS]+=retract_length/volumetric_multiplier[active_extruder];
  1156. }
  1157. plan_set_e_position(current_position[E_AXIS]);
  1158. float oldFeedrate = feedrate;
  1159. feedrate=retract_feedrate*60;
  1160. retracted[active_extruder]=true;
  1161. prepare_move();
  1162. if(retract_zlift > 0.01) {
  1163. current_position[Z_AXIS]-=retract_zlift;
  1164. #ifdef DELTA
  1165. calculate_delta(current_position); // change cartesian kinematic to delta kinematic;
  1166. plan_set_position(delta[X_AXIS], delta[Y_AXIS], delta[Z_AXIS], current_position[E_AXIS]);
  1167. #else
  1168. plan_set_position(current_position[X_AXIS], current_position[Y_AXIS], current_position[Z_AXIS], current_position[E_AXIS]);
  1169. #endif
  1170. prepare_move();
  1171. }
  1172. feedrate = oldFeedrate;
  1173. } else if(!retracting && retracted[active_extruder]) {
  1174. destination[X_AXIS]=current_position[X_AXIS];
  1175. destination[Y_AXIS]=current_position[Y_AXIS];
  1176. destination[Z_AXIS]=current_position[Z_AXIS];
  1177. destination[E_AXIS]=current_position[E_AXIS];
  1178. if(retract_zlift > 0.01) {
  1179. current_position[Z_AXIS]+=retract_zlift;
  1180. #ifdef DELTA
  1181. calculate_delta(current_position); // change cartesian kinematic to delta kinematic;
  1182. plan_set_position(delta[X_AXIS], delta[Y_AXIS], delta[Z_AXIS], current_position[E_AXIS]);
  1183. #else
  1184. plan_set_position(current_position[X_AXIS], current_position[Y_AXIS], current_position[Z_AXIS], current_position[E_AXIS]);
  1185. #endif
  1186. //prepare_move();
  1187. }
  1188. if (swapretract) {
  1189. current_position[E_AXIS]-=(retract_length_swap+retract_recover_length_swap)/volumetric_multiplier[active_extruder];
  1190. } else {
  1191. current_position[E_AXIS]-=(retract_length+retract_recover_length)/volumetric_multiplier[active_extruder];
  1192. }
  1193. plan_set_e_position(current_position[E_AXIS]);
  1194. float oldFeedrate = feedrate;
  1195. feedrate=retract_recover_feedrate*60;
  1196. retracted[active_extruder]=false;
  1197. prepare_move();
  1198. feedrate = oldFeedrate;
  1199. }
  1200. } //retract
  1201. #endif //FWRETRACT
  1202. #ifdef Z_PROBE_SLED
  1203. #ifndef SLED_DOCKING_OFFSET
  1204. #define SLED_DOCKING_OFFSET 0
  1205. #endif
  1206. //
  1207. // Method to dock/undock a sled designed by Charles Bell.
  1208. //
  1209. // dock[in] If true, move to MAX_X and engage the electromagnet
  1210. // offset[in] The additional distance to move to adjust docking location
  1211. //
  1212. static void dock_sled(bool dock, int offset=0) {
  1213. int z_loc;
  1214. if (!((axis_known_position[X_AXIS]) && (axis_known_position[Y_AXIS]))) {
  1215. LCD_MESSAGEPGM(MSG_POSITION_UNKNOWN);
  1216. SERIAL_ECHO_START;
  1217. SERIAL_ECHOLNPGM(MSG_POSITION_UNKNOWN);
  1218. return;
  1219. }
  1220. if (dock) {
  1221. do_blocking_move_to(X_MAX_POS + SLED_DOCKING_OFFSET + offset,
  1222. current_position[Y_AXIS],
  1223. current_position[Z_AXIS]);
  1224. // turn off magnet
  1225. digitalWrite(SERVO0_PIN, LOW);
  1226. } else {
  1227. if (current_position[Z_AXIS] < (Z_RAISE_BEFORE_PROBING + 5))
  1228. z_loc = Z_RAISE_BEFORE_PROBING;
  1229. else
  1230. z_loc = current_position[Z_AXIS];
  1231. do_blocking_move_to(X_MAX_POS + SLED_DOCKING_OFFSET + offset,
  1232. Y_PROBE_OFFSET_FROM_EXTRUDER, z_loc);
  1233. // turn on magnet
  1234. digitalWrite(SERVO0_PIN, HIGH);
  1235. }
  1236. }
  1237. #endif
  1238. void process_commands()
  1239. {
  1240. unsigned long codenum; //throw away variable
  1241. char *starpos = NULL;
  1242. #ifdef ENABLE_AUTO_BED_LEVELING
  1243. float x_tmp, y_tmp, z_tmp, real_z;
  1244. #endif
  1245. if(code_seen('G'))
  1246. {
  1247. switch((int)code_value())
  1248. {
  1249. case 0: // G0 -> G1
  1250. case 1: // G1
  1251. if(Stopped == false) {
  1252. get_coordinates(); // For X Y Z E F
  1253. #ifdef FWRETRACT
  1254. if(autoretract_enabled)
  1255. if( !(code_seen('X') || code_seen('Y') || code_seen('Z')) && code_seen('E')) {
  1256. float echange=destination[E_AXIS]-current_position[E_AXIS];
  1257. if((echange<-MIN_RETRACT && !retracted[active_extruder]) || (echange>MIN_RETRACT && retracted[active_extruder])) { //move appears to be an attempt to retract or recover
  1258. current_position[E_AXIS] = destination[E_AXIS]; //hide the slicer-generated retract/recover from calculations
  1259. plan_set_e_position(current_position[E_AXIS]); //AND from the planner
  1260. retract(!retracted[active_extruder]);
  1261. return;
  1262. }
  1263. }
  1264. #endif //FWRETRACT
  1265. prepare_move();
  1266. //ClearToSend();
  1267. }
  1268. break;
  1269. #ifndef SCARA //disable arc support
  1270. case 2: // G2 - CW ARC
  1271. if(Stopped == false) {
  1272. get_arc_coordinates();
  1273. prepare_arc_move(true);
  1274. }
  1275. break;
  1276. case 3: // G3 - CCW ARC
  1277. if(Stopped == false) {
  1278. get_arc_coordinates();
  1279. prepare_arc_move(false);
  1280. }
  1281. break;
  1282. #endif
  1283. case 4: // G4 dwell
  1284. LCD_MESSAGEPGM(MSG_DWELL);
  1285. codenum = 0;
  1286. if(code_seen('P')) codenum = code_value(); // milliseconds to wait
  1287. if(code_seen('S')) codenum = code_value() * 1000; // seconds to wait
  1288. st_synchronize();
  1289. codenum += millis(); // keep track of when we started waiting
  1290. previous_millis_cmd = millis();
  1291. while(millis() < codenum) {
  1292. manage_heater();
  1293. manage_inactivity();
  1294. lcd_update();
  1295. }
  1296. break;
  1297. #ifdef FWRETRACT
  1298. case 10: // G10 retract
  1299. #if EXTRUDERS > 1
  1300. retracted_swap[active_extruder]=(code_seen('S') && code_value_long() == 1); // checks for swap retract argument
  1301. retract(true,retracted_swap[active_extruder]);
  1302. #else
  1303. retract(true);
  1304. #endif
  1305. break;
  1306. case 11: // G11 retract_recover
  1307. #if EXTRUDERS > 1
  1308. retract(false,retracted_swap[active_extruder]);
  1309. #else
  1310. retract(false);
  1311. #endif
  1312. break;
  1313. #endif //FWRETRACT
  1314. case 28: //G28 Home all Axis one at a time
  1315. #ifdef ENABLE_AUTO_BED_LEVELING
  1316. plan_bed_level_matrix.set_to_identity(); //Reset the plane ("erase" all leveling data)
  1317. #endif //ENABLE_AUTO_BED_LEVELING
  1318. saved_feedrate = feedrate;
  1319. saved_feedmultiply = feedmultiply;
  1320. feedmultiply = 100;
  1321. previous_millis_cmd = millis();
  1322. enable_endstops(true);
  1323. for(int8_t i=0; i < NUM_AXIS; i++) {
  1324. destination[i] = current_position[i];
  1325. }
  1326. feedrate = 0.0;
  1327. #ifdef DELTA
  1328. // A delta can only safely home all axis at the same time
  1329. // all axis have to home at the same time
  1330. // Move all carriages up together until the first endstop is hit.
  1331. current_position[X_AXIS] = 0;
  1332. current_position[Y_AXIS] = 0;
  1333. current_position[Z_AXIS] = 0;
  1334. plan_set_position(current_position[X_AXIS], current_position[Y_AXIS], current_position[Z_AXIS], current_position[E_AXIS]);
  1335. destination[X_AXIS] = 3 * Z_MAX_LENGTH;
  1336. destination[Y_AXIS] = 3 * Z_MAX_LENGTH;
  1337. destination[Z_AXIS] = 3 * Z_MAX_LENGTH;
  1338. feedrate = 1.732 * homing_feedrate[X_AXIS];
  1339. plan_buffer_line(destination[X_AXIS], destination[Y_AXIS], destination[Z_AXIS], destination[E_AXIS], feedrate/60, active_extruder);
  1340. st_synchronize();
  1341. endstops_hit_on_purpose();
  1342. current_position[X_AXIS] = destination[X_AXIS];
  1343. current_position[Y_AXIS] = destination[Y_AXIS];
  1344. current_position[Z_AXIS] = destination[Z_AXIS];
  1345. // take care of back off and rehome now we are all at the top
  1346. HOMEAXIS(X);
  1347. HOMEAXIS(Y);
  1348. HOMEAXIS(Z);
  1349. calculate_delta(current_position);
  1350. plan_set_position(delta[X_AXIS], delta[Y_AXIS], delta[Z_AXIS], current_position[E_AXIS]);
  1351. #else // NOT DELTA
  1352. home_all_axis = !((code_seen(axis_codes[X_AXIS])) || (code_seen(axis_codes[Y_AXIS])) || (code_seen(axis_codes[Z_AXIS])));
  1353. #if Z_HOME_DIR > 0 // If homing away from BED do Z first
  1354. if((home_all_axis) || (code_seen(axis_codes[Z_AXIS]))) {
  1355. HOMEAXIS(Z);
  1356. }
  1357. #endif
  1358. #ifdef QUICK_HOME
  1359. if((home_all_axis)||( code_seen(axis_codes[X_AXIS]) && code_seen(axis_codes[Y_AXIS])) ) //first diagonal move
  1360. {
  1361. current_position[X_AXIS] = 0;current_position[Y_AXIS] = 0;
  1362. #ifndef DUAL_X_CARRIAGE
  1363. int x_axis_home_dir = home_dir(X_AXIS);
  1364. #else
  1365. int x_axis_home_dir = x_home_dir(active_extruder);
  1366. extruder_duplication_enabled = false;
  1367. #endif
  1368. plan_set_position(current_position[X_AXIS], current_position[Y_AXIS], current_position[Z_AXIS], current_position[E_AXIS]);
  1369. 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);
  1370. feedrate = homing_feedrate[X_AXIS];
  1371. if(homing_feedrate[Y_AXIS]<feedrate)
  1372. feedrate = homing_feedrate[Y_AXIS];
  1373. if (max_length(X_AXIS) > max_length(Y_AXIS)) {
  1374. feedrate *= sqrt(pow(max_length(Y_AXIS) / max_length(X_AXIS), 2) + 1);
  1375. } else {
  1376. feedrate *= sqrt(pow(max_length(X_AXIS) / max_length(Y_AXIS), 2) + 1);
  1377. }
  1378. plan_buffer_line(destination[X_AXIS], destination[Y_AXIS], destination[Z_AXIS], destination[E_AXIS], feedrate/60, active_extruder);
  1379. st_synchronize();
  1380. axis_is_at_home(X_AXIS);
  1381. axis_is_at_home(Y_AXIS);
  1382. plan_set_position(current_position[X_AXIS], current_position[Y_AXIS], current_position[Z_AXIS], current_position[E_AXIS]);
  1383. destination[X_AXIS] = current_position[X_AXIS];
  1384. destination[Y_AXIS] = current_position[Y_AXIS];
  1385. plan_buffer_line(destination[X_AXIS], destination[Y_AXIS], destination[Z_AXIS], destination[E_AXIS], feedrate/60, active_extruder);
  1386. feedrate = 0.0;
  1387. st_synchronize();
  1388. endstops_hit_on_purpose();
  1389. current_position[X_AXIS] = destination[X_AXIS];
  1390. current_position[Y_AXIS] = destination[Y_AXIS];
  1391. #ifndef SCARA
  1392. current_position[Z_AXIS] = destination[Z_AXIS];
  1393. #endif
  1394. }
  1395. #endif
  1396. if((home_all_axis) || (code_seen(axis_codes[X_AXIS])))
  1397. {
  1398. #ifdef DUAL_X_CARRIAGE
  1399. int tmp_extruder = active_extruder;
  1400. extruder_duplication_enabled = false;
  1401. active_extruder = !active_extruder;
  1402. HOMEAXIS(X);
  1403. inactive_extruder_x_pos = current_position[X_AXIS];
  1404. active_extruder = tmp_extruder;
  1405. HOMEAXIS(X);
  1406. // reset state used by the different modes
  1407. memcpy(raised_parked_position, current_position, sizeof(raised_parked_position));
  1408. delayed_move_time = 0;
  1409. active_extruder_parked = true;
  1410. #else
  1411. HOMEAXIS(X);
  1412. #endif
  1413. }
  1414. if((home_all_axis) || (code_seen(axis_codes[Y_AXIS]))) {
  1415. HOMEAXIS(Y);
  1416. }
  1417. if(code_seen(axis_codes[X_AXIS]))
  1418. {
  1419. if(code_value_long() != 0) {
  1420. #ifdef SCARA
  1421. current_position[X_AXIS]=code_value();
  1422. #else
  1423. current_position[X_AXIS]=code_value()+add_homing[X_AXIS];
  1424. #endif
  1425. }
  1426. }
  1427. if(code_seen(axis_codes[Y_AXIS])) {
  1428. if(code_value_long() != 0) {
  1429. #ifdef SCARA
  1430. current_position[Y_AXIS]=code_value();
  1431. #else
  1432. current_position[Y_AXIS]=code_value()+add_homing[Y_AXIS];
  1433. #endif
  1434. }
  1435. }
  1436. #if Z_HOME_DIR < 0 // If homing towards BED do Z last
  1437. #ifndef Z_SAFE_HOMING
  1438. if((home_all_axis) || (code_seen(axis_codes[Z_AXIS]))) {
  1439. #if defined (Z_RAISE_BEFORE_HOMING) && (Z_RAISE_BEFORE_HOMING > 0)
  1440. destination[Z_AXIS] = Z_RAISE_BEFORE_HOMING * home_dir(Z_AXIS) * (-1); // Set destination away from bed
  1441. feedrate = max_feedrate[Z_AXIS];
  1442. plan_buffer_line(destination[X_AXIS], destination[Y_AXIS], destination[Z_AXIS], destination[E_AXIS], feedrate, active_extruder);
  1443. st_synchronize();
  1444. #endif
  1445. HOMEAXIS(Z);
  1446. }
  1447. #else // Z Safe mode activated.
  1448. if(home_all_axis) {
  1449. destination[X_AXIS] = round(Z_SAFE_HOMING_X_POINT - X_PROBE_OFFSET_FROM_EXTRUDER);
  1450. destination[Y_AXIS] = round(Z_SAFE_HOMING_Y_POINT - Y_PROBE_OFFSET_FROM_EXTRUDER);
  1451. destination[Z_AXIS] = Z_RAISE_BEFORE_HOMING * home_dir(Z_AXIS) * (-1); // Set destination away from bed
  1452. feedrate = XY_TRAVEL_SPEED/60;
  1453. current_position[Z_AXIS] = 0;
  1454. plan_set_position(current_position[X_AXIS], current_position[Y_AXIS], current_position[Z_AXIS], current_position[E_AXIS]);
  1455. plan_buffer_line(destination[X_AXIS], destination[Y_AXIS], destination[Z_AXIS], destination[E_AXIS], feedrate, active_extruder);
  1456. st_synchronize();
  1457. current_position[X_AXIS] = destination[X_AXIS];
  1458. current_position[Y_AXIS] = destination[Y_AXIS];
  1459. HOMEAXIS(Z);
  1460. }
  1461. // Let's see if X and Y are homed and probe is inside bed area.
  1462. if(code_seen(axis_codes[Z_AXIS])) {
  1463. if ( (axis_known_position[X_AXIS]) && (axis_known_position[Y_AXIS]) \
  1464. && (current_position[X_AXIS] >= X_MIN_POS - X_PROBE_OFFSET_FROM_EXTRUDER) \
  1465. && (current_position[X_AXIS] <= X_MAX_POS - X_PROBE_OFFSET_FROM_EXTRUDER) \
  1466. && (current_position[Y_AXIS] >= Y_MIN_POS - Y_PROBE_OFFSET_FROM_EXTRUDER) \
  1467. && (current_position[Y_AXIS] <= Y_MAX_POS - Y_PROBE_OFFSET_FROM_EXTRUDER)) {
  1468. current_position[Z_AXIS] = 0;
  1469. plan_set_position(current_position[X_AXIS], current_position[Y_AXIS], current_position[Z_AXIS], current_position[E_AXIS]);
  1470. destination[Z_AXIS] = Z_RAISE_BEFORE_HOMING * home_dir(Z_AXIS) * (-1); // Set destination away from bed
  1471. feedrate = max_feedrate[Z_AXIS];
  1472. plan_buffer_line(destination[X_AXIS], destination[Y_AXIS], destination[Z_AXIS], destination[E_AXIS], feedrate, active_extruder);
  1473. st_synchronize();
  1474. HOMEAXIS(Z);
  1475. } else if (!((axis_known_position[X_AXIS]) && (axis_known_position[Y_AXIS]))) {
  1476. LCD_MESSAGEPGM(MSG_POSITION_UNKNOWN);
  1477. SERIAL_ECHO_START;
  1478. SERIAL_ECHOLNPGM(MSG_POSITION_UNKNOWN);
  1479. } else {
  1480. LCD_MESSAGEPGM(MSG_ZPROBE_OUT);
  1481. SERIAL_ECHO_START;
  1482. SERIAL_ECHOLNPGM(MSG_ZPROBE_OUT);
  1483. }
  1484. }
  1485. #endif
  1486. #endif
  1487. if(code_seen(axis_codes[Z_AXIS])) {
  1488. if(code_value_long() != 0) {
  1489. current_position[Z_AXIS]=code_value()+add_homing[Z_AXIS];
  1490. }
  1491. }
  1492. #ifdef ENABLE_AUTO_BED_LEVELING
  1493. if((home_all_axis) || (code_seen(axis_codes[Z_AXIS]))) {
  1494. current_position[Z_AXIS] += zprobe_zoffset; //Add Z_Probe offset (the distance is negative)
  1495. }
  1496. #endif
  1497. plan_set_position(current_position[X_AXIS], current_position[Y_AXIS], current_position[Z_AXIS], current_position[E_AXIS]);
  1498. #endif // else DELTA
  1499. #ifdef SCARA
  1500. calculate_delta(current_position);
  1501. plan_set_position(delta[X_AXIS], delta[Y_AXIS], delta[Z_AXIS], current_position[E_AXIS]);
  1502. #endif // SCARA
  1503. #ifdef ENDSTOPS_ONLY_FOR_HOMING
  1504. enable_endstops(false);
  1505. #endif
  1506. feedrate = saved_feedrate;
  1507. feedmultiply = saved_feedmultiply;
  1508. previous_millis_cmd = millis();
  1509. endstops_hit_on_purpose();
  1510. break;
  1511. #ifdef ENABLE_AUTO_BED_LEVELING
  1512. #if Z_MIN_PIN == -1
  1513. #error "You must have a Z_MIN endstop in order to enable Auto Bed Leveling!!! Z_MIN_PIN must point to a valid hardware pin."
  1514. #endif
  1515. /**
  1516. * Enhanced G29 Auto Bed Leveling Probe Routine
  1517. *
  1518. * Parameters With AUTO_BED_LEVELING_GRID:
  1519. *
  1520. * P Set the size of the grid that will be probed (P x P points).
  1521. * Example: "G29 P4"
  1522. *
  1523. * V Set the verbose level (0-4). Example: "G29 V3"
  1524. *
  1525. * T Generate a Bed Topology Report. Example: "G29 P5 T" for a detailed report.
  1526. * This is useful for manual bed leveling and finding flaws in the bed (to
  1527. * assist with part placement).
  1528. *
  1529. * F Set the Front limit of the probing grid
  1530. * B Set the Back limit of the probing grid
  1531. * L Set the Left limit of the probing grid
  1532. * R Set the Right limit of the probing grid
  1533. *
  1534. * Global Parameters:
  1535. *
  1536. * E/e By default G29 engages / disengages the probe for each point.
  1537. * Include "E" to engage and disengage the probe just once.
  1538. * There's no extra effect if you have a fixed probe.
  1539. * Usage: "G29 E" or "G29 e"
  1540. *
  1541. */
  1542. case 29: // G29 Detailed Z-Probe, probes the bed at 3 or more points.
  1543. {
  1544. // Use one of these defines to specify the origin
  1545. // for a topographical map to be printed for your bed.
  1546. #define ORIGIN_BACK_LEFT 1
  1547. #define ORIGIN_FRONT_RIGHT 2
  1548. #define ORIGIN_BACK_RIGHT 3
  1549. #define ORIGIN_FRONT_LEFT 4
  1550. #define TOPO_ORIGIN ORIGIN_FRONT_LEFT
  1551. // Prevent user from running a G29 without first homing in X and Y
  1552. if (!(axis_known_position[X_AXIS] && axis_known_position[Y_AXIS])) {
  1553. LCD_MESSAGEPGM(MSG_POSITION_UNKNOWN);
  1554. SERIAL_ECHO_START;
  1555. SERIAL_ECHOLNPGM(MSG_POSITION_UNKNOWN);
  1556. break; // abort G29, since we don't know where we are
  1557. }
  1558. bool enhanced_g29 = code_seen('E') || code_seen('e');
  1559. #ifdef AUTO_BED_LEVELING_GRID
  1560. // Example Syntax: G29 N4 V2 E T
  1561. int verbose_level = 1;
  1562. bool topo_flag = code_seen('T') || code_seen('t');
  1563. if (code_seen('V') || code_seen('v')) {
  1564. verbose_level = code_value();
  1565. if (verbose_level < 0 || verbose_level > 4) {
  1566. SERIAL_PROTOCOLPGM("?(V)erbose Level is implausible (0-4).\n");
  1567. break;
  1568. }
  1569. if (verbose_level > 0) {
  1570. SERIAL_PROTOCOLPGM("G29 Enhanced Auto Bed Leveling Code V1.25:\n");
  1571. SERIAL_PROTOCOLPGM("Full support at: http://3dprintboard.com/forum.php\n");
  1572. if (verbose_level > 2) topo_flag = true;
  1573. }
  1574. }
  1575. int auto_bed_leveling_grid_points = code_seen('P') ? code_value_long() : AUTO_BED_LEVELING_GRID_POINTS;
  1576. if (auto_bed_leveling_grid_points < 2 || auto_bed_leveling_grid_points > AUTO_BED_LEVELING_GRID_POINTS) {
  1577. SERIAL_PROTOCOLPGM("?Number of probed (P)oints is implausible (2 minimum).\n");
  1578. break;
  1579. }
  1580. // Define the possible boundaries for probing based on the set limits.
  1581. // Code above (in G28) might have these limits wrong, or I am wrong here.
  1582. #define MIN_PROBE_EDGE 10 // Edges of the probe square can be no less
  1583. const int min_probe_x = max(X_MIN_POS, X_MIN_POS + X_PROBE_OFFSET_FROM_EXTRUDER),
  1584. max_probe_x = min(X_MAX_POS, X_MAX_POS + X_PROBE_OFFSET_FROM_EXTRUDER),
  1585. min_probe_y = max(Y_MIN_POS, Y_MIN_POS + Y_PROBE_OFFSET_FROM_EXTRUDER),
  1586. max_probe_y = min(Y_MAX_POS, Y_MAX_POS + Y_PROBE_OFFSET_FROM_EXTRUDER);
  1587. int left_probe_bed_position = code_seen('L') ? code_value_long() : LEFT_PROBE_BED_POSITION,
  1588. right_probe_bed_position = code_seen('R') ? code_value_long() : RIGHT_PROBE_BED_POSITION,
  1589. front_probe_bed_position = code_seen('F') ? code_value_long() : FRONT_PROBE_BED_POSITION,
  1590. back_probe_bed_position = code_seen('B') ? code_value_long() : BACK_PROBE_BED_POSITION;
  1591. bool left_out_l = left_probe_bed_position < min_probe_x,
  1592. left_out_r = left_probe_bed_position > right_probe_bed_position - MIN_PROBE_EDGE,
  1593. left_out = left_out_l || left_out_r,
  1594. right_out_r = right_probe_bed_position > max_probe_x,
  1595. right_out_l =right_probe_bed_position < left_probe_bed_position + MIN_PROBE_EDGE,
  1596. right_out = right_out_l || right_out_r,
  1597. front_out_f = front_probe_bed_position < min_probe_y,
  1598. front_out_b = front_probe_bed_position > back_probe_bed_position - MIN_PROBE_EDGE,
  1599. front_out = front_out_f || front_out_b,
  1600. back_out_b = back_probe_bed_position > max_probe_y,
  1601. back_out_f = back_probe_bed_position < front_probe_bed_position + MIN_PROBE_EDGE,
  1602. back_out = back_out_f || back_out_b;
  1603. if (left_out || right_out || front_out || back_out) {
  1604. if (left_out) {
  1605. SERIAL_PROTOCOLPGM("?Probe (L)eft position out of range.\n");
  1606. left_probe_bed_position = left_out_l ? min_probe_x : right_probe_bed_position - MIN_PROBE_EDGE;
  1607. }
  1608. if (right_out) {
  1609. SERIAL_PROTOCOLPGM("?Probe (R)ight position out of range.\n");
  1610. right_probe_bed_position = right_out_r ? max_probe_x : left_probe_bed_position + MIN_PROBE_EDGE;
  1611. }
  1612. if (front_out) {
  1613. SERIAL_PROTOCOLPGM("?Probe (F)ront position out of range.\n");
  1614. front_probe_bed_position = front_out_f ? min_probe_y : back_probe_bed_position - MIN_PROBE_EDGE;
  1615. }
  1616. if (back_out) {
  1617. SERIAL_PROTOCOLPGM("?Probe (B)ack position out of range.\n");
  1618. back_probe_bed_position = back_out_b ? max_probe_y : front_probe_bed_position + MIN_PROBE_EDGE;
  1619. }
  1620. break;
  1621. }
  1622. #endif
  1623. #ifdef Z_PROBE_SLED
  1624. dock_sled(false); // engage (un-dock) the probe
  1625. #endif
  1626. st_synchronize();
  1627. // make sure the bed_level_rotation_matrix is identity or the planner will get it incorectly
  1628. //vector_3 corrected_position = plan_get_position_mm();
  1629. //corrected_position.debug("position before G29");
  1630. plan_bed_level_matrix.set_to_identity();
  1631. vector_3 uncorrected_position = plan_get_position();
  1632. //uncorrected_position.debug("position durring G29");
  1633. current_position[X_AXIS] = uncorrected_position.x;
  1634. current_position[Y_AXIS] = uncorrected_position.y;
  1635. current_position[Z_AXIS] = uncorrected_position.z;
  1636. plan_set_position(current_position[X_AXIS], current_position[Y_AXIS], current_position[Z_AXIS], current_position[E_AXIS]);
  1637. setup_for_endstop_move();
  1638. feedrate = homing_feedrate[Z_AXIS];
  1639. #ifdef AUTO_BED_LEVELING_GRID
  1640. // probe at the points of a lattice grid
  1641. int xGridSpacing = (right_probe_bed_position - left_probe_bed_position) / (auto_bed_leveling_grid_points - 1);
  1642. int yGridSpacing = (back_probe_bed_position - front_probe_bed_position) / (auto_bed_leveling_grid_points - 1);
  1643. // solve the plane equation ax + by + d = z
  1644. // A is the matrix with rows [x y 1] for all the probed points
  1645. // B is the vector of the Z positions
  1646. // 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
  1647. // so Vx = -a Vy = -b Vz = 1 (we want the vector facing towards positive Z
  1648. int abl2 = auto_bed_leveling_grid_points * auto_bed_leveling_grid_points;
  1649. double eqnAMatrix[abl2 * 3], // "A" matrix of the linear system of equations
  1650. eqnBVector[abl2], // "B" vector of Z points
  1651. mean = 0.0;
  1652. int probePointCounter = 0;
  1653. bool zig = true;
  1654. for (int yProbe = front_probe_bed_position; yProbe <= back_probe_bed_position; yProbe += yGridSpacing) {
  1655. int xProbe, xInc;
  1656. if (zig)
  1657. xProbe = left_probe_bed_position, xInc = xGridSpacing;
  1658. else
  1659. xProbe = right_probe_bed_position, xInc = -xGridSpacing;
  1660. // If topo_flag is set then don't zig-zag. Just scan in one direction.
  1661. // This gets the probe points in more readable order.
  1662. if (!topo_flag) zig = !zig;
  1663. for (int xCount = 0; xCount < auto_bed_leveling_grid_points; xCount++) {
  1664. // raise extruder
  1665. float z_before = probePointCounter == 0 ? Z_RAISE_BEFORE_PROBING : current_position[Z_AXIS] + Z_RAISE_BETWEEN_PROBINGS,
  1666. measured_z;
  1667. // Enhanced G29 - Do not retract servo between probes
  1668. ProbeAction act;
  1669. if (enhanced_g29) {
  1670. if (yProbe == front_probe_bed_position && xCount == 0)
  1671. act = ProbeEngage;
  1672. else if (yProbe == front_probe_bed_position + (yGridSpacing * (auto_bed_leveling_grid_points - 1)) && xCount == auto_bed_leveling_grid_points - 1)
  1673. act = ProbeRetract;
  1674. else
  1675. act = ProbeStay;
  1676. }
  1677. else
  1678. act = ProbeEngageRetract;
  1679. measured_z = probe_pt(xProbe, yProbe, z_before, act);
  1680. mean += measured_z;
  1681. eqnBVector[probePointCounter] = measured_z;
  1682. eqnAMatrix[probePointCounter + 0 * abl2] = xProbe;
  1683. eqnAMatrix[probePointCounter + 1 * abl2] = yProbe;
  1684. eqnAMatrix[probePointCounter + 2 * abl2] = 1;
  1685. probePointCounter++;
  1686. xProbe += xInc;
  1687. } //xProbe
  1688. } //yProbe
  1689. clean_up_after_endstop_move();
  1690. // solve lsq problem
  1691. double *plane_equation_coefficients = qr_solve(abl2, 3, eqnAMatrix, eqnBVector);
  1692. mean /= abl2;
  1693. if (verbose_level) {
  1694. SERIAL_PROTOCOLPGM("Eqn coefficients: a: ");
  1695. SERIAL_PROTOCOL(plane_equation_coefficients[0]);
  1696. SERIAL_PROTOCOLPGM(" b: ");
  1697. SERIAL_PROTOCOL(plane_equation_coefficients[1]);
  1698. SERIAL_PROTOCOLPGM(" d: ");
  1699. SERIAL_PROTOCOLLN(plane_equation_coefficients[2]);
  1700. if (verbose_level > 2) {
  1701. SERIAL_PROTOCOLPGM("Mean of sampled points: ");
  1702. SERIAL_PROTOCOL_F(mean, 6);
  1703. SERIAL_PROTOCOLPGM(" \n");
  1704. }
  1705. }
  1706. if (topo_flag) {
  1707. int xx, yy;
  1708. SERIAL_PROTOCOLPGM(" \nBed Height Topography: \n");
  1709. #if TOPO_ORIGIN == ORIGIN_FRONT_LEFT
  1710. for (yy = auto_bed_leveling_grid_points - 1; yy >= 0; yy--)
  1711. #else
  1712. for (yy = 0; yy < auto_bed_leveling_grid_points; yy++)
  1713. #endif
  1714. {
  1715. #if TOPO_ORIGIN == ORIGIN_BACK_RIGHT
  1716. for (xx = auto_bed_leveling_grid_points - 1; xx >= 0; xx--)
  1717. #else
  1718. for (xx = 0; xx < auto_bed_leveling_grid_points; xx++)
  1719. #endif
  1720. {
  1721. int ind =
  1722. #if TOPO_ORIGIN == ORIGIN_BACK_RIGHT || TOPO_ORIGIN == ORIGIN_FRONT_LEFT
  1723. yy * auto_bed_leveling_grid_points + xx
  1724. #elif TOPO_ORIGIN == ORIGIN_BACK_LEFT
  1725. xx * auto_bed_leveling_grid_points + yy
  1726. #elif TOPO_ORIGIN == ORIGIN_FRONT_RIGHT
  1727. abl2 - xx * auto_bed_leveling_grid_points - yy - 1
  1728. #endif
  1729. ;
  1730. float diff = eqnBVector[ind] - mean;
  1731. if (diff >= 0.0)
  1732. SERIAL_PROTOCOLPGM(" +"); // Watch column alignment in Pronterface
  1733. else
  1734. SERIAL_PROTOCOLPGM(" -");
  1735. SERIAL_PROTOCOL_F(diff, 5);
  1736. } // xx
  1737. SERIAL_PROTOCOLPGM("\n");
  1738. } // yy
  1739. SERIAL_PROTOCOLPGM("\n");
  1740. } //topo_flag
  1741. set_bed_level_equation_lsq(plane_equation_coefficients);
  1742. free(plane_equation_coefficients);
  1743. #else // !AUTO_BED_LEVELING_GRID
  1744. // Probe at 3 arbitrary points
  1745. float z_at_pt_1, z_at_pt_2, z_at_pt_3;
  1746. if (enhanced_g29) {
  1747. // Basic Enhanced G29
  1748. z_at_pt_1 = probe_pt(ABL_PROBE_PT_1_X, ABL_PROBE_PT_1_Y, Z_RAISE_BEFORE_PROBING, ProbeEngage);
  1749. z_at_pt_2 = probe_pt(ABL_PROBE_PT_2_X, ABL_PROBE_PT_2_Y, current_position[Z_AXIS] + Z_RAISE_BETWEEN_PROBINGS, ProbeStay);
  1750. z_at_pt_3 = probe_pt(ABL_PROBE_PT_3_X, ABL_PROBE_PT_3_Y, current_position[Z_AXIS] + Z_RAISE_BETWEEN_PROBINGS, ProbeRetract);
  1751. }
  1752. else {
  1753. z_at_pt_1 = probe_pt(ABL_PROBE_PT_1_X, ABL_PROBE_PT_1_Y, Z_RAISE_BEFORE_PROBING);
  1754. z_at_pt_2 = probe_pt(ABL_PROBE_PT_2_X, ABL_PROBE_PT_2_Y, current_position[Z_AXIS] + Z_RAISE_BETWEEN_PROBINGS);
  1755. z_at_pt_3 = probe_pt(ABL_PROBE_PT_3_X, ABL_PROBE_PT_3_Y, current_position[Z_AXIS] + Z_RAISE_BETWEEN_PROBINGS);
  1756. }
  1757. clean_up_after_endstop_move();
  1758. set_bed_level_equation_3pts(z_at_pt_1, z_at_pt_2, z_at_pt_3);
  1759. #endif // !AUTO_BED_LEVELING_GRID
  1760. st_synchronize();
  1761. if (verbose_level > 0)
  1762. plan_bed_level_matrix.debug(" \n\nBed Level Correction Matrix:");
  1763. // The following code correct the Z height difference from z-probe position and hotend tip position.
  1764. // The Z height on homing is measured by Z-Probe, but the probe is quite far from the hotend.
  1765. // When the bed is uneven, this height must be corrected.
  1766. 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)
  1767. x_tmp = current_position[X_AXIS] + X_PROBE_OFFSET_FROM_EXTRUDER;
  1768. y_tmp = current_position[Y_AXIS] + Y_PROBE_OFFSET_FROM_EXTRUDER;
  1769. z_tmp = current_position[Z_AXIS];
  1770. apply_rotation_xyz(plan_bed_level_matrix, x_tmp, y_tmp, z_tmp); //Apply the correction sending the probe offset
  1771. current_position[Z_AXIS] = z_tmp - real_z + current_position[Z_AXIS]; //The difference is added to current position and sent to planner.
  1772. plan_set_position(current_position[X_AXIS], current_position[Y_AXIS], current_position[Z_AXIS], current_position[E_AXIS]);
  1773. #ifdef Z_PROBE_SLED
  1774. dock_sled(true, -SLED_DOCKING_OFFSET); // dock the probe, correcting for over-travel
  1775. #endif
  1776. }
  1777. break;
  1778. #ifndef Z_PROBE_SLED
  1779. case 30: // G30 Single Z Probe
  1780. {
  1781. engage_z_probe(); // Engage Z Servo endstop if available
  1782. st_synchronize();
  1783. // TODO: make sure the bed_level_rotation_matrix is identity or the planner will get set incorectly
  1784. setup_for_endstop_move();
  1785. feedrate = homing_feedrate[Z_AXIS];
  1786. run_z_probe();
  1787. SERIAL_PROTOCOLPGM(MSG_BED);
  1788. SERIAL_PROTOCOLPGM(" X: ");
  1789. SERIAL_PROTOCOL(current_position[X_AXIS]);
  1790. SERIAL_PROTOCOLPGM(" Y: ");
  1791. SERIAL_PROTOCOL(current_position[Y_AXIS]);
  1792. SERIAL_PROTOCOLPGM(" Z: ");
  1793. SERIAL_PROTOCOL(current_position[Z_AXIS]);
  1794. SERIAL_PROTOCOLPGM("\n");
  1795. clean_up_after_endstop_move();
  1796. retract_z_probe(); // Retract Z Servo endstop if available
  1797. }
  1798. break;
  1799. #else
  1800. case 31: // dock the sled
  1801. dock_sled(true);
  1802. break;
  1803. case 32: // undock the sled
  1804. dock_sled(false);
  1805. break;
  1806. #endif // Z_PROBE_SLED
  1807. #endif // ENABLE_AUTO_BED_LEVELING
  1808. case 90: // G90
  1809. relative_mode = false;
  1810. break;
  1811. case 91: // G91
  1812. relative_mode = true;
  1813. break;
  1814. case 92: // G92
  1815. if(!code_seen(axis_codes[E_AXIS]))
  1816. st_synchronize();
  1817. for(int8_t i=0; i < NUM_AXIS; i++) {
  1818. if(code_seen(axis_codes[i])) {
  1819. if(i == E_AXIS) {
  1820. current_position[i] = code_value();
  1821. plan_set_e_position(current_position[E_AXIS]);
  1822. }
  1823. else {
  1824. #ifdef SCARA
  1825. if (i == X_AXIS || i == Y_AXIS) {
  1826. current_position[i] = code_value();
  1827. }
  1828. else {
  1829. current_position[i] = code_value()+add_homing[i];
  1830. }
  1831. #else
  1832. current_position[i] = code_value()+add_homing[i];
  1833. #endif
  1834. plan_set_position(current_position[X_AXIS], current_position[Y_AXIS], current_position[Z_AXIS], current_position[E_AXIS]);
  1835. }
  1836. }
  1837. }
  1838. break;
  1839. }
  1840. }
  1841. else if(code_seen('M'))
  1842. {
  1843. switch( (int)code_value() )
  1844. {
  1845. #ifdef ULTIPANEL
  1846. case 0: // M0 - Unconditional stop - Wait for user button press on LCD
  1847. case 1: // M1 - Conditional stop - Wait for user button press on LCD
  1848. {
  1849. char *src = strchr_pointer + 2;
  1850. codenum = 0;
  1851. bool hasP = false, hasS = false;
  1852. if (code_seen('P')) {
  1853. codenum = code_value(); // milliseconds to wait
  1854. hasP = codenum > 0;
  1855. }
  1856. if (code_seen('S')) {
  1857. codenum = code_value() * 1000; // seconds to wait
  1858. hasS = codenum > 0;
  1859. }
  1860. starpos = strchr(src, '*');
  1861. if (starpos != NULL) *(starpos) = '\0';
  1862. while (*src == ' ') ++src;
  1863. if (!hasP && !hasS && *src != '\0') {
  1864. lcd_setstatus(src);
  1865. } else {
  1866. LCD_MESSAGEPGM(MSG_USERWAIT);
  1867. }
  1868. lcd_ignore_click();
  1869. st_synchronize();
  1870. previous_millis_cmd = millis();
  1871. if (codenum > 0){
  1872. codenum += millis(); // keep track of when we started waiting
  1873. while(millis() < codenum && !lcd_clicked()){
  1874. manage_heater();
  1875. manage_inactivity();
  1876. lcd_update();
  1877. }
  1878. lcd_ignore_click(false);
  1879. }else{
  1880. if (!lcd_detected())
  1881. break;
  1882. while(!lcd_clicked()){
  1883. manage_heater();
  1884. manage_inactivity();
  1885. lcd_update();
  1886. }
  1887. }
  1888. if (IS_SD_PRINTING)
  1889. LCD_MESSAGEPGM(MSG_RESUMING);
  1890. else
  1891. LCD_MESSAGEPGM(WELCOME_MSG);
  1892. }
  1893. break;
  1894. #endif
  1895. case 17:
  1896. LCD_MESSAGEPGM(MSG_NO_MOVE);
  1897. enable_x();
  1898. enable_y();
  1899. enable_z();
  1900. enable_e0();
  1901. enable_e1();
  1902. enable_e2();
  1903. enable_e3();
  1904. break;
  1905. #ifdef SDSUPPORT
  1906. case 20: // M20 - list SD card
  1907. SERIAL_PROTOCOLLNPGM(MSG_BEGIN_FILE_LIST);
  1908. card.ls();
  1909. SERIAL_PROTOCOLLNPGM(MSG_END_FILE_LIST);
  1910. break;
  1911. case 21: // M21 - init SD card
  1912. card.initsd();
  1913. break;
  1914. case 22: //M22 - release SD card
  1915. card.release();
  1916. break;
  1917. case 23: //M23 - Select file
  1918. starpos = (strchr(strchr_pointer + 4,'*'));
  1919. if(starpos!=NULL)
  1920. *(starpos)='\0';
  1921. card.openFile(strchr_pointer + 4,true);
  1922. break;
  1923. case 24: //M24 - Start SD print
  1924. card.startFileprint();
  1925. starttime=millis();
  1926. break;
  1927. case 25: //M25 - Pause SD print
  1928. card.pauseSDPrint();
  1929. break;
  1930. case 26: //M26 - Set SD index
  1931. if(card.cardOK && code_seen('S')) {
  1932. card.setIndex(code_value_long());
  1933. }
  1934. break;
  1935. case 27: //M27 - Get SD status
  1936. card.getStatus();
  1937. break;
  1938. case 28: //M28 - Start SD write
  1939. starpos = (strchr(strchr_pointer + 4,'*'));
  1940. if(starpos != NULL){
  1941. char* npos = strchr(cmdbuffer[bufindr], 'N');
  1942. strchr_pointer = strchr(npos,' ') + 1;
  1943. *(starpos) = '\0';
  1944. }
  1945. card.openFile(strchr_pointer+4,false);
  1946. break;
  1947. case 29: //M29 - Stop SD write
  1948. //processed in write to file routine above
  1949. //card,saving = false;
  1950. break;
  1951. case 30: //M30 <filename> Delete File
  1952. if (card.cardOK){
  1953. card.closefile();
  1954. starpos = (strchr(strchr_pointer + 4,'*'));
  1955. if(starpos != NULL){
  1956. char* npos = strchr(cmdbuffer[bufindr], 'N');
  1957. strchr_pointer = strchr(npos,' ') + 1;
  1958. *(starpos) = '\0';
  1959. }
  1960. card.removeFile(strchr_pointer + 4);
  1961. }
  1962. break;
  1963. case 32: //M32 - Select file and start SD print
  1964. {
  1965. if(card.sdprinting) {
  1966. st_synchronize();
  1967. }
  1968. starpos = (strchr(strchr_pointer + 4,'*'));
  1969. char* namestartpos = (strchr(strchr_pointer + 4,'!')); //find ! to indicate filename string start.
  1970. if(namestartpos==NULL)
  1971. {
  1972. namestartpos=strchr_pointer + 4; //default name position, 4 letters after the M
  1973. }
  1974. else
  1975. namestartpos++; //to skip the '!'
  1976. if(starpos!=NULL)
  1977. *(starpos)='\0';
  1978. bool call_procedure=(code_seen('P'));
  1979. if(strchr_pointer>namestartpos)
  1980. call_procedure=false; //false alert, 'P' found within filename
  1981. if( card.cardOK )
  1982. {
  1983. card.openFile(namestartpos,true,!call_procedure);
  1984. if(code_seen('S'))
  1985. if(strchr_pointer<namestartpos) //only if "S" is occuring _before_ the filename
  1986. card.setIndex(code_value_long());
  1987. card.startFileprint();
  1988. if(!call_procedure)
  1989. starttime=millis(); //procedure calls count as normal print time.
  1990. }
  1991. } break;
  1992. case 928: //M928 - Start SD write
  1993. starpos = (strchr(strchr_pointer + 5,'*'));
  1994. if(starpos != NULL){
  1995. char* npos = strchr(cmdbuffer[bufindr], 'N');
  1996. strchr_pointer = strchr(npos,' ') + 1;
  1997. *(starpos) = '\0';
  1998. }
  1999. card.openLogFile(strchr_pointer+5);
  2000. break;
  2001. #endif //SDSUPPORT
  2002. case 31: //M31 take time since the start of the SD print or an M109 command
  2003. {
  2004. stoptime=millis();
  2005. char time[30];
  2006. unsigned long t=(stoptime-starttime)/1000;
  2007. int sec,min;
  2008. min=t/60;
  2009. sec=t%60;
  2010. sprintf_P(time, PSTR("%i min, %i sec"), min, sec);
  2011. SERIAL_ECHO_START;
  2012. SERIAL_ECHOLN(time);
  2013. lcd_setstatus(time);
  2014. autotempShutdown();
  2015. }
  2016. break;
  2017. case 42: //M42 -Change pin status via gcode
  2018. if (code_seen('S'))
  2019. {
  2020. int pin_status = code_value();
  2021. int pin_number = LED_PIN;
  2022. if (code_seen('P') && pin_status >= 0 && pin_status <= 255)
  2023. pin_number = code_value();
  2024. for(int8_t i = 0; i < (int8_t)(sizeof(sensitive_pins)/sizeof(int)); i++)
  2025. {
  2026. if (sensitive_pins[i] == pin_number)
  2027. {
  2028. pin_number = -1;
  2029. break;
  2030. }
  2031. }
  2032. #if defined(FAN_PIN) && FAN_PIN > -1
  2033. if (pin_number == FAN_PIN)
  2034. fanSpeed = pin_status;
  2035. #endif
  2036. if (pin_number > -1)
  2037. {
  2038. pinMode(pin_number, OUTPUT);
  2039. digitalWrite(pin_number, pin_status);
  2040. analogWrite(pin_number, pin_status);
  2041. }
  2042. }
  2043. break;
  2044. // M48 Z-Probe repeatability measurement function.
  2045. //
  2046. // Usage: M48 <n #_samples> <X X_position_for_samples> <Y Y_position_for_samples> <V Verbose_Level> <Engage_probe_for_each_reading> <L legs_of_movement_prior_to_doing_probe>
  2047. //
  2048. // This function assumes the bed has been homed. Specificaly, that a G28 command
  2049. // as been issued prior to invoking the M48 Z-Probe repeatability measurement function.
  2050. // Any information generated by a prior G29 Bed leveling command will be lost and need to be
  2051. // regenerated.
  2052. //
  2053. // The number of samples will default to 10 if not specified. You can use upper or lower case
  2054. // letters for any of the options EXCEPT n. n must be in lower case because Marlin uses a capital
  2055. // N for its communication protocol and will get horribly confused if you send it a capital N.
  2056. //
  2057. #ifdef ENABLE_AUTO_BED_LEVELING
  2058. #ifdef Z_PROBE_REPEATABILITY_TEST
  2059. case 48: // M48 Z-Probe repeatability
  2060. {
  2061. #if Z_MIN_PIN == -1
  2062. #error "You must have a Z_MIN endstop in order to enable calculation of Z-Probe repeatability."
  2063. #endif
  2064. double sum=0.0;
  2065. double mean=0.0;
  2066. double sigma=0.0;
  2067. double sample_set[50];
  2068. int verbose_level=1, n=0, j, n_samples = 10, n_legs=0, engage_probe_for_each_reading=0 ;
  2069. double X_current, Y_current, Z_current;
  2070. double X_probe_location, Y_probe_location, Z_start_location, ext_position;
  2071. if (code_seen('V') || code_seen('v')) {
  2072. verbose_level = code_value();
  2073. if (verbose_level<0 || verbose_level>4 ) {
  2074. SERIAL_PROTOCOLPGM("?Verbose Level not plausable.\n");
  2075. goto Sigma_Exit;
  2076. }
  2077. }
  2078. if (verbose_level > 0) {
  2079. SERIAL_PROTOCOLPGM("M48 Z-Probe Repeatability test. Version 2.00\n");
  2080. SERIAL_PROTOCOLPGM("Full support at: http://3dprintboard.com/forum.php\n");
  2081. }
  2082. if (code_seen('n')) {
  2083. n_samples = code_value();
  2084. if (n_samples<4 || n_samples>50 ) {
  2085. SERIAL_PROTOCOLPGM("?Specified sample size not plausable.\n");
  2086. goto Sigma_Exit;
  2087. }
  2088. }
  2089. X_current = X_probe_location = st_get_position_mm(X_AXIS);
  2090. Y_current = Y_probe_location = st_get_position_mm(Y_AXIS);
  2091. Z_current = st_get_position_mm(Z_AXIS);
  2092. Z_start_location = st_get_position_mm(Z_AXIS) + Z_RAISE_BEFORE_PROBING;
  2093. ext_position = st_get_position_mm(E_AXIS);
  2094. if (code_seen('E') || code_seen('e') )
  2095. engage_probe_for_each_reading++;
  2096. if (code_seen('X') || code_seen('x') ) {
  2097. X_probe_location = code_value() - X_PROBE_OFFSET_FROM_EXTRUDER;
  2098. if (X_probe_location<X_MIN_POS || X_probe_location>X_MAX_POS ) {
  2099. SERIAL_PROTOCOLPGM("?Specified X position out of range.\n");
  2100. goto Sigma_Exit;
  2101. }
  2102. }
  2103. if (code_seen('Y') || code_seen('y') ) {
  2104. Y_probe_location = code_value() - Y_PROBE_OFFSET_FROM_EXTRUDER;
  2105. if (Y_probe_location<Y_MIN_POS || Y_probe_location>Y_MAX_POS ) {
  2106. SERIAL_PROTOCOLPGM("?Specified Y position out of range.\n");
  2107. goto Sigma_Exit;
  2108. }
  2109. }
  2110. if (code_seen('L') || code_seen('l') ) {
  2111. n_legs = code_value();
  2112. if ( n_legs==1 )
  2113. n_legs = 2;
  2114. if ( n_legs<0 || n_legs>15 ) {
  2115. SERIAL_PROTOCOLPGM("?Specified number of legs in movement not plausable.\n");
  2116. goto Sigma_Exit;
  2117. }
  2118. }
  2119. //
  2120. // Do all the preliminary setup work. First raise the probe.
  2121. //
  2122. st_synchronize();
  2123. plan_bed_level_matrix.set_to_identity();
  2124. plan_buffer_line( X_current, Y_current, Z_start_location,
  2125. ext_position,
  2126. homing_feedrate[Z_AXIS]/60,
  2127. active_extruder);
  2128. st_synchronize();
  2129. //
  2130. // Now get everything to the specified probe point So we can safely do a probe to
  2131. // get us close to the bed. If the Z-Axis is far from the bed, we don't want to
  2132. // use that as a starting point for each probe.
  2133. //
  2134. if (verbose_level > 2)
  2135. SERIAL_PROTOCOL("Positioning probe for the test.\n");
  2136. plan_buffer_line( X_probe_location, Y_probe_location, Z_start_location,
  2137. ext_position,
  2138. homing_feedrate[X_AXIS]/60,
  2139. active_extruder);
  2140. st_synchronize();
  2141. current_position[X_AXIS] = X_current = st_get_position_mm(X_AXIS);
  2142. current_position[Y_AXIS] = Y_current = st_get_position_mm(Y_AXIS);
  2143. current_position[Z_AXIS] = Z_current = st_get_position_mm(Z_AXIS);
  2144. current_position[E_AXIS] = ext_position = st_get_position_mm(E_AXIS);
  2145. //
  2146. // OK, do the inital probe to get us close to the bed.
  2147. // Then retrace the right amount and use that in subsequent probes
  2148. //
  2149. engage_z_probe();
  2150. setup_for_endstop_move();
  2151. run_z_probe();
  2152. current_position[Z_AXIS] = Z_current = st_get_position_mm(Z_AXIS);
  2153. Z_start_location = st_get_position_mm(Z_AXIS) + Z_RAISE_BEFORE_PROBING;
  2154. plan_buffer_line( X_probe_location, Y_probe_location, Z_start_location,
  2155. ext_position,
  2156. homing_feedrate[X_AXIS]/60,
  2157. active_extruder);
  2158. st_synchronize();
  2159. current_position[Z_AXIS] = Z_current = st_get_position_mm(Z_AXIS);
  2160. if (engage_probe_for_each_reading)
  2161. retract_z_probe();
  2162. for( n=0; n<n_samples; n++) {
  2163. do_blocking_move_to( X_probe_location, Y_probe_location, Z_start_location); // Make sure we are at the probe location
  2164. if ( n_legs) {
  2165. double radius=0.0, theta=0.0, x_sweep, y_sweep;
  2166. int rotational_direction, l;
  2167. rotational_direction = (unsigned long) millis() & 0x0001; // clockwise or counter clockwise
  2168. radius = (unsigned long) millis() % (long) (X_MAX_LENGTH/4); // limit how far out to go
  2169. theta = (float) ((unsigned long) millis() % (long) 360) / (360./(2*3.1415926)); // turn into radians
  2170. //SERIAL_ECHOPAIR("starting radius: ",radius);
  2171. //SERIAL_ECHOPAIR(" theta: ",theta);
  2172. //SERIAL_ECHOPAIR(" direction: ",rotational_direction);
  2173. //SERIAL_PROTOCOLLNPGM("");
  2174. for( l=0; l<n_legs-1; l++) {
  2175. if (rotational_direction==1)
  2176. theta += (float) ((unsigned long) millis() % (long) 20) / (360.0/(2*3.1415926)); // turn into radians
  2177. else
  2178. theta -= (float) ((unsigned long) millis() % (long) 20) / (360.0/(2*3.1415926)); // turn into radians
  2179. radius += (float) ( ((long) ((unsigned long) millis() % (long) 10)) - 5);
  2180. if ( radius<0.0 )
  2181. radius = -radius;
  2182. X_current = X_probe_location + cos(theta) * radius;
  2183. Y_current = Y_probe_location + sin(theta) * radius;
  2184. if ( X_current<X_MIN_POS) // Make sure our X & Y are sane
  2185. X_current = X_MIN_POS;
  2186. if ( X_current>X_MAX_POS)
  2187. X_current = X_MAX_POS;
  2188. if ( Y_current<Y_MIN_POS) // Make sure our X & Y are sane
  2189. Y_current = Y_MIN_POS;
  2190. if ( Y_current>Y_MAX_POS)
  2191. Y_current = Y_MAX_POS;
  2192. if (verbose_level>3 ) {
  2193. SERIAL_ECHOPAIR("x: ", X_current);
  2194. SERIAL_ECHOPAIR("y: ", Y_current);
  2195. SERIAL_PROTOCOLLNPGM("");
  2196. }
  2197. do_blocking_move_to( X_current, Y_current, Z_current );
  2198. }
  2199. do_blocking_move_to( X_probe_location, Y_probe_location, Z_start_location); // Go back to the probe location
  2200. }
  2201. if (engage_probe_for_each_reading) {
  2202. engage_z_probe();
  2203. delay(1000);
  2204. }
  2205. setup_for_endstop_move();
  2206. run_z_probe();
  2207. sample_set[n] = current_position[Z_AXIS];
  2208. //
  2209. // Get the current mean for the data points we have so far
  2210. //
  2211. sum=0.0;
  2212. for( j=0; j<=n; j++) {
  2213. sum = sum + sample_set[j];
  2214. }
  2215. mean = sum / (double (n+1));
  2216. //
  2217. // Now, use that mean to calculate the standard deviation for the
  2218. // data points we have so far
  2219. //
  2220. sum=0.0;
  2221. for( j=0; j<=n; j++) {
  2222. sum = sum + (sample_set[j]-mean) * (sample_set[j]-mean);
  2223. }
  2224. sigma = sqrt( sum / (double (n+1)) );
  2225. if (verbose_level > 1) {
  2226. SERIAL_PROTOCOL(n+1);
  2227. SERIAL_PROTOCOL(" of ");
  2228. SERIAL_PROTOCOL(n_samples);
  2229. SERIAL_PROTOCOLPGM(" z: ");
  2230. SERIAL_PROTOCOL_F(current_position[Z_AXIS], 6);
  2231. }
  2232. if (verbose_level > 2) {
  2233. SERIAL_PROTOCOL(" mean: ");
  2234. SERIAL_PROTOCOL_F(mean,6);
  2235. SERIAL_PROTOCOL(" sigma: ");
  2236. SERIAL_PROTOCOL_F(sigma,6);
  2237. }
  2238. if (verbose_level > 0)
  2239. SERIAL_PROTOCOLPGM("\n");
  2240. plan_buffer_line( X_probe_location, Y_probe_location, Z_start_location,
  2241. current_position[E_AXIS], homing_feedrate[Z_AXIS]/60, active_extruder);
  2242. st_synchronize();
  2243. if (engage_probe_for_each_reading) {
  2244. retract_z_probe();
  2245. delay(1000);
  2246. }
  2247. }
  2248. retract_z_probe();
  2249. delay(1000);
  2250. clean_up_after_endstop_move();
  2251. // enable_endstops(true);
  2252. if (verbose_level > 0) {
  2253. SERIAL_PROTOCOLPGM("Mean: ");
  2254. SERIAL_PROTOCOL_F(mean, 6);
  2255. SERIAL_PROTOCOLPGM("\n");
  2256. }
  2257. SERIAL_PROTOCOLPGM("Standard Deviation: ");
  2258. SERIAL_PROTOCOL_F(sigma, 6);
  2259. SERIAL_PROTOCOLPGM("\n\n");
  2260. Sigma_Exit:
  2261. break;
  2262. }
  2263. #endif // Z_PROBE_REPEATABILITY_TEST
  2264. #endif // ENABLE_AUTO_BED_LEVELING
  2265. case 104: // M104
  2266. if(setTargetedHotend(104)){
  2267. break;
  2268. }
  2269. if (code_seen('S')) setTargetHotend(code_value(), tmp_extruder);
  2270. #ifdef DUAL_X_CARRIAGE
  2271. if (dual_x_carriage_mode == DXC_DUPLICATION_MODE && tmp_extruder == 0)
  2272. setTargetHotend1(code_value() == 0.0 ? 0.0 : code_value() + duplicate_extruder_temp_offset);
  2273. #endif
  2274. setWatch();
  2275. break;
  2276. case 112: // M112 -Emergency Stop
  2277. kill();
  2278. break;
  2279. case 140: // M140 set bed temp
  2280. if (code_seen('S')) setTargetBed(code_value());
  2281. break;
  2282. case 105 : // M105
  2283. if(setTargetedHotend(105)){
  2284. break;
  2285. }
  2286. #if defined(TEMP_0_PIN) && TEMP_0_PIN > -1
  2287. SERIAL_PROTOCOLPGM("ok T:");
  2288. SERIAL_PROTOCOL_F(degHotend(tmp_extruder),1);
  2289. SERIAL_PROTOCOLPGM(" /");
  2290. SERIAL_PROTOCOL_F(degTargetHotend(tmp_extruder),1);
  2291. #if defined(TEMP_BED_PIN) && TEMP_BED_PIN > -1
  2292. SERIAL_PROTOCOLPGM(" B:");
  2293. SERIAL_PROTOCOL_F(degBed(),1);
  2294. SERIAL_PROTOCOLPGM(" /");
  2295. SERIAL_PROTOCOL_F(degTargetBed(),1);
  2296. #endif //TEMP_BED_PIN
  2297. for (int8_t cur_extruder = 0; cur_extruder < EXTRUDERS; ++cur_extruder) {
  2298. SERIAL_PROTOCOLPGM(" T");
  2299. SERIAL_PROTOCOL(cur_extruder);
  2300. SERIAL_PROTOCOLPGM(":");
  2301. SERIAL_PROTOCOL_F(degHotend(cur_extruder),1);
  2302. SERIAL_PROTOCOLPGM(" /");
  2303. SERIAL_PROTOCOL_F(degTargetHotend(cur_extruder),1);
  2304. }
  2305. #else
  2306. SERIAL_ERROR_START;
  2307. SERIAL_ERRORLNPGM(MSG_ERR_NO_THERMISTORS);
  2308. #endif
  2309. SERIAL_PROTOCOLPGM(" @:");
  2310. #ifdef EXTRUDER_WATTS
  2311. SERIAL_PROTOCOL((EXTRUDER_WATTS * getHeaterPower(tmp_extruder))/127);
  2312. SERIAL_PROTOCOLPGM("W");
  2313. #else
  2314. SERIAL_PROTOCOL(getHeaterPower(tmp_extruder));
  2315. #endif
  2316. SERIAL_PROTOCOLPGM(" B@:");
  2317. #ifdef BED_WATTS
  2318. SERIAL_PROTOCOL((BED_WATTS * getHeaterPower(-1))/127);
  2319. SERIAL_PROTOCOLPGM("W");
  2320. #else
  2321. SERIAL_PROTOCOL(getHeaterPower(-1));
  2322. #endif
  2323. #ifdef SHOW_TEMP_ADC_VALUES
  2324. #if defined(TEMP_BED_PIN) && TEMP_BED_PIN > -1
  2325. SERIAL_PROTOCOLPGM(" ADC B:");
  2326. SERIAL_PROTOCOL_F(degBed(),1);
  2327. SERIAL_PROTOCOLPGM("C->");
  2328. SERIAL_PROTOCOL_F(rawBedTemp()/OVERSAMPLENR,0);
  2329. #endif
  2330. for (int8_t cur_extruder = 0; cur_extruder < EXTRUDERS; ++cur_extruder) {
  2331. SERIAL_PROTOCOLPGM(" T");
  2332. SERIAL_PROTOCOL(cur_extruder);
  2333. SERIAL_PROTOCOLPGM(":");
  2334. SERIAL_PROTOCOL_F(degHotend(cur_extruder),1);
  2335. SERIAL_PROTOCOLPGM("C->");
  2336. SERIAL_PROTOCOL_F(rawHotendTemp(cur_extruder)/OVERSAMPLENR,0);
  2337. }
  2338. #endif
  2339. SERIAL_PROTOCOLLN("");
  2340. return;
  2341. break;
  2342. case 109:
  2343. {// M109 - Wait for extruder heater to reach target.
  2344. if(setTargetedHotend(109)){
  2345. break;
  2346. }
  2347. LCD_MESSAGEPGM(MSG_HEATING);
  2348. #ifdef AUTOTEMP
  2349. autotemp_enabled=false;
  2350. #endif
  2351. if (code_seen('S')) {
  2352. setTargetHotend(code_value(), tmp_extruder);
  2353. #ifdef DUAL_X_CARRIAGE
  2354. if (dual_x_carriage_mode == DXC_DUPLICATION_MODE && tmp_extruder == 0)
  2355. setTargetHotend1(code_value() == 0.0 ? 0.0 : code_value() + duplicate_extruder_temp_offset);
  2356. #endif
  2357. CooldownNoWait = true;
  2358. } else if (code_seen('R')) {
  2359. setTargetHotend(code_value(), tmp_extruder);
  2360. #ifdef DUAL_X_CARRIAGE
  2361. if (dual_x_carriage_mode == DXC_DUPLICATION_MODE && tmp_extruder == 0)
  2362. setTargetHotend1(code_value() == 0.0 ? 0.0 : code_value() + duplicate_extruder_temp_offset);
  2363. #endif
  2364. CooldownNoWait = false;
  2365. }
  2366. #ifdef AUTOTEMP
  2367. if (code_seen('S')) autotemp_min=code_value();
  2368. if (code_seen('B')) autotemp_max=code_value();
  2369. if (code_seen('F'))
  2370. {
  2371. autotemp_factor=code_value();
  2372. autotemp_enabled=true;
  2373. }
  2374. #endif
  2375. setWatch();
  2376. codenum = millis();
  2377. /* See if we are heating up or cooling down */
  2378. target_direction = isHeatingHotend(tmp_extruder); // true if heating, false if cooling
  2379. cancel_heatup = false;
  2380. #ifdef TEMP_RESIDENCY_TIME
  2381. long residencyStart;
  2382. residencyStart = -1;
  2383. /* continue to loop until we have reached the target temp
  2384. _and_ until TEMP_RESIDENCY_TIME hasn't passed since we reached it */
  2385. while((!cancel_heatup)&&((residencyStart == -1) ||
  2386. (residencyStart >= 0 && (((unsigned int) (millis() - residencyStart)) < (TEMP_RESIDENCY_TIME * 1000UL)))) ) {
  2387. #else
  2388. while ( target_direction ? (isHeatingHotend(tmp_extruder)) : (isCoolingHotend(tmp_extruder)&&(CooldownNoWait==false)) ) {
  2389. #endif //TEMP_RESIDENCY_TIME
  2390. if( (millis() - codenum) > 1000UL )
  2391. { //Print Temp Reading and remaining time every 1 second while heating up/cooling down
  2392. SERIAL_PROTOCOLPGM("T:");
  2393. SERIAL_PROTOCOL_F(degHotend(tmp_extruder),1);
  2394. SERIAL_PROTOCOLPGM(" E:");
  2395. SERIAL_PROTOCOL((int)tmp_extruder);
  2396. #ifdef TEMP_RESIDENCY_TIME
  2397. SERIAL_PROTOCOLPGM(" W:");
  2398. if(residencyStart > -1)
  2399. {
  2400. codenum = ((TEMP_RESIDENCY_TIME * 1000UL) - (millis() - residencyStart)) / 1000UL;
  2401. SERIAL_PROTOCOLLN( codenum );
  2402. }
  2403. else
  2404. {
  2405. SERIAL_PROTOCOLLN( "?" );
  2406. }
  2407. #else
  2408. SERIAL_PROTOCOLLN("");
  2409. #endif
  2410. codenum = millis();
  2411. }
  2412. manage_heater();
  2413. manage_inactivity();
  2414. lcd_update();
  2415. #ifdef TEMP_RESIDENCY_TIME
  2416. /* start/restart the TEMP_RESIDENCY_TIME timer whenever we reach target temp for the first time
  2417. or when current temp falls outside the hysteresis after target temp was reached */
  2418. if ((residencyStart == -1 && target_direction && (degHotend(tmp_extruder) >= (degTargetHotend(tmp_extruder)-TEMP_WINDOW))) ||
  2419. (residencyStart == -1 && !target_direction && (degHotend(tmp_extruder) <= (degTargetHotend(tmp_extruder)+TEMP_WINDOW))) ||
  2420. (residencyStart > -1 && labs(degHotend(tmp_extruder) - degTargetHotend(tmp_extruder)) > TEMP_HYSTERESIS) )
  2421. {
  2422. residencyStart = millis();
  2423. }
  2424. #endif //TEMP_RESIDENCY_TIME
  2425. }
  2426. LCD_MESSAGEPGM(MSG_HEATING_COMPLETE);
  2427. starttime=millis();
  2428. previous_millis_cmd = millis();
  2429. }
  2430. break;
  2431. case 190: // M190 - Wait for bed heater to reach target.
  2432. #if defined(TEMP_BED_PIN) && TEMP_BED_PIN > -1
  2433. LCD_MESSAGEPGM(MSG_BED_HEATING);
  2434. if (code_seen('S')) {
  2435. setTargetBed(code_value());
  2436. CooldownNoWait = true;
  2437. } else if (code_seen('R')) {
  2438. setTargetBed(code_value());
  2439. CooldownNoWait = false;
  2440. }
  2441. codenum = millis();
  2442. cancel_heatup = false;
  2443. target_direction = isHeatingBed(); // true if heating, false if cooling
  2444. while ( (target_direction)&&(!cancel_heatup) ? (isHeatingBed()) : (isCoolingBed()&&(CooldownNoWait==false)) )
  2445. {
  2446. if(( millis() - codenum) > 1000 ) //Print Temp Reading every 1 second while heating up.
  2447. {
  2448. float tt=degHotend(active_extruder);
  2449. SERIAL_PROTOCOLPGM("T:");
  2450. SERIAL_PROTOCOL(tt);
  2451. SERIAL_PROTOCOLPGM(" E:");
  2452. SERIAL_PROTOCOL((int)active_extruder);
  2453. SERIAL_PROTOCOLPGM(" B:");
  2454. SERIAL_PROTOCOL_F(degBed(),1);
  2455. SERIAL_PROTOCOLLN("");
  2456. codenum = millis();
  2457. }
  2458. manage_heater();
  2459. manage_inactivity();
  2460. lcd_update();
  2461. }
  2462. LCD_MESSAGEPGM(MSG_BED_DONE);
  2463. previous_millis_cmd = millis();
  2464. #endif
  2465. break;
  2466. #if defined(FAN_PIN) && FAN_PIN > -1
  2467. case 106: //M106 Fan On
  2468. if (code_seen('S')){
  2469. fanSpeed=constrain(code_value(),0,255);
  2470. }
  2471. else {
  2472. fanSpeed=255;
  2473. }
  2474. break;
  2475. case 107: //M107 Fan Off
  2476. fanSpeed = 0;
  2477. break;
  2478. #endif //FAN_PIN
  2479. #ifdef BARICUDA
  2480. // PWM for HEATER_1_PIN
  2481. #if defined(HEATER_1_PIN) && HEATER_1_PIN > -1
  2482. case 126: //M126 valve open
  2483. if (code_seen('S')){
  2484. ValvePressure=constrain(code_value(),0,255);
  2485. }
  2486. else {
  2487. ValvePressure=255;
  2488. }
  2489. break;
  2490. case 127: //M127 valve closed
  2491. ValvePressure = 0;
  2492. break;
  2493. #endif //HEATER_1_PIN
  2494. // PWM for HEATER_2_PIN
  2495. #if defined(HEATER_2_PIN) && HEATER_2_PIN > -1
  2496. case 128: //M128 valve open
  2497. if (code_seen('S')){
  2498. EtoPPressure=constrain(code_value(),0,255);
  2499. }
  2500. else {
  2501. EtoPPressure=255;
  2502. }
  2503. break;
  2504. case 129: //M129 valve closed
  2505. EtoPPressure = 0;
  2506. break;
  2507. #endif //HEATER_2_PIN
  2508. #endif
  2509. #if defined(PS_ON_PIN) && PS_ON_PIN > -1
  2510. case 80: // M80 - Turn on Power Supply
  2511. OUT_WRITE(PS_ON_PIN, PS_ON_AWAKE); // GND
  2512. // If you have a switch on suicide pin, this is useful
  2513. // if you want to start another print with suicide feature after
  2514. // a print without suicide...
  2515. #if defined SUICIDE_PIN && SUICIDE_PIN > -1
  2516. OUT_WRITE(SUICIDE_PIN, HIGH);
  2517. #endif
  2518. #ifdef ULTIPANEL
  2519. powersupply = true;
  2520. LCD_MESSAGEPGM(WELCOME_MSG);
  2521. lcd_update();
  2522. #endif
  2523. break;
  2524. #endif
  2525. case 81: // M81 - Turn off Power Supply
  2526. disable_heater();
  2527. st_synchronize();
  2528. disable_e0();
  2529. disable_e1();
  2530. disable_e2();
  2531. disable_e3();
  2532. finishAndDisableSteppers();
  2533. fanSpeed = 0;
  2534. delay(1000); // Wait a little before to switch off
  2535. #if defined(SUICIDE_PIN) && SUICIDE_PIN > -1
  2536. st_synchronize();
  2537. suicide();
  2538. #elif defined(PS_ON_PIN) && PS_ON_PIN > -1
  2539. OUT_WRITE(PS_ON_PIN, PS_ON_ASLEEP);
  2540. #endif
  2541. #ifdef ULTIPANEL
  2542. powersupply = false;
  2543. LCD_MESSAGEPGM(MACHINE_NAME" "MSG_OFF".");
  2544. lcd_update();
  2545. #endif
  2546. break;
  2547. case 82:
  2548. axis_relative_modes[3] = false;
  2549. break;
  2550. case 83:
  2551. axis_relative_modes[3] = true;
  2552. break;
  2553. case 18: //compatibility
  2554. case 84: // M84
  2555. if(code_seen('S')){
  2556. stepper_inactive_time = code_value() * 1000;
  2557. }
  2558. else
  2559. {
  2560. bool all_axis = !((code_seen(axis_codes[X_AXIS])) || (code_seen(axis_codes[Y_AXIS])) || (code_seen(axis_codes[Z_AXIS]))|| (code_seen(axis_codes[E_AXIS])));
  2561. if(all_axis)
  2562. {
  2563. st_synchronize();
  2564. disable_e0();
  2565. disable_e1();
  2566. disable_e2();
  2567. disable_e3();
  2568. finishAndDisableSteppers();
  2569. }
  2570. else
  2571. {
  2572. st_synchronize();
  2573. if(code_seen('X')) disable_x();
  2574. if(code_seen('Y')) disable_y();
  2575. if(code_seen('Z')) disable_z();
  2576. #if ((E0_ENABLE_PIN != X_ENABLE_PIN) && (E1_ENABLE_PIN != Y_ENABLE_PIN)) // Only enable on boards that have seperate ENABLE_PINS
  2577. if(code_seen('E')) {
  2578. disable_e0();
  2579. disable_e1();
  2580. disable_e2();
  2581. disable_e3();
  2582. }
  2583. #endif
  2584. }
  2585. }
  2586. break;
  2587. case 85: // M85
  2588. if(code_seen('S')) {
  2589. max_inactive_time = code_value() * 1000;
  2590. }
  2591. break;
  2592. case 92: // M92
  2593. for(int8_t i=0; i < NUM_AXIS; i++)
  2594. {
  2595. if(code_seen(axis_codes[i]))
  2596. {
  2597. if(i == 3) { // E
  2598. float value = code_value();
  2599. if(value < 20.0) {
  2600. float factor = axis_steps_per_unit[i] / value; // increase e constants if M92 E14 is given for netfab.
  2601. max_e_jerk *= factor;
  2602. max_feedrate[i] *= factor;
  2603. axis_steps_per_sqr_second[i] *= factor;
  2604. }
  2605. axis_steps_per_unit[i] = value;
  2606. }
  2607. else {
  2608. axis_steps_per_unit[i] = code_value();
  2609. }
  2610. }
  2611. }
  2612. break;
  2613. case 115: // M115
  2614. SERIAL_PROTOCOLPGM(MSG_M115_REPORT);
  2615. break;
  2616. case 117: // M117 display message
  2617. starpos = (strchr(strchr_pointer + 5,'*'));
  2618. if(starpos!=NULL)
  2619. *(starpos)='\0';
  2620. lcd_setstatus(strchr_pointer + 5);
  2621. break;
  2622. case 114: // M114
  2623. SERIAL_PROTOCOLPGM("X:");
  2624. SERIAL_PROTOCOL(current_position[X_AXIS]);
  2625. SERIAL_PROTOCOLPGM(" Y:");
  2626. SERIAL_PROTOCOL(current_position[Y_AXIS]);
  2627. SERIAL_PROTOCOLPGM(" Z:");
  2628. SERIAL_PROTOCOL(current_position[Z_AXIS]);
  2629. SERIAL_PROTOCOLPGM(" E:");
  2630. SERIAL_PROTOCOL(current_position[E_AXIS]);
  2631. SERIAL_PROTOCOLPGM(MSG_COUNT_X);
  2632. SERIAL_PROTOCOL(float(st_get_position(X_AXIS))/axis_steps_per_unit[X_AXIS]);
  2633. SERIAL_PROTOCOLPGM(" Y:");
  2634. SERIAL_PROTOCOL(float(st_get_position(Y_AXIS))/axis_steps_per_unit[Y_AXIS]);
  2635. SERIAL_PROTOCOLPGM(" Z:");
  2636. SERIAL_PROTOCOL(float(st_get_position(Z_AXIS))/axis_steps_per_unit[Z_AXIS]);
  2637. SERIAL_PROTOCOLLN("");
  2638. #ifdef SCARA
  2639. SERIAL_PROTOCOLPGM("SCARA Theta:");
  2640. SERIAL_PROTOCOL(delta[X_AXIS]);
  2641. SERIAL_PROTOCOLPGM(" Psi+Theta:");
  2642. SERIAL_PROTOCOL(delta[Y_AXIS]);
  2643. SERIAL_PROTOCOLLN("");
  2644. SERIAL_PROTOCOLPGM("SCARA Cal - Theta:");
  2645. SERIAL_PROTOCOL(delta[X_AXIS]+add_homing[X_AXIS]);
  2646. SERIAL_PROTOCOLPGM(" Psi+Theta (90):");
  2647. SERIAL_PROTOCOL(delta[Y_AXIS]-delta[X_AXIS]-90+add_homing[Y_AXIS]);
  2648. SERIAL_PROTOCOLLN("");
  2649. SERIAL_PROTOCOLPGM("SCARA step Cal - Theta:");
  2650. SERIAL_PROTOCOL(delta[X_AXIS]/90*axis_steps_per_unit[X_AXIS]);
  2651. SERIAL_PROTOCOLPGM(" Psi+Theta:");
  2652. SERIAL_PROTOCOL((delta[Y_AXIS]-delta[X_AXIS])/90*axis_steps_per_unit[Y_AXIS]);
  2653. SERIAL_PROTOCOLLN("");
  2654. SERIAL_PROTOCOLLN("");
  2655. #endif
  2656. break;
  2657. case 120: // M120
  2658. enable_endstops(false) ;
  2659. break;
  2660. case 121: // M121
  2661. enable_endstops(true) ;
  2662. break;
  2663. case 119: // M119
  2664. SERIAL_PROTOCOLLN(MSG_M119_REPORT);
  2665. #if defined(X_MIN_PIN) && X_MIN_PIN > -1
  2666. SERIAL_PROTOCOLPGM(MSG_X_MIN);
  2667. SERIAL_PROTOCOLLN(((READ(X_MIN_PIN)^X_MIN_ENDSTOP_INVERTING)?MSG_ENDSTOP_HIT:MSG_ENDSTOP_OPEN));
  2668. #endif
  2669. #if defined(X_MAX_PIN) && X_MAX_PIN > -1
  2670. SERIAL_PROTOCOLPGM(MSG_X_MAX);
  2671. SERIAL_PROTOCOLLN(((READ(X_MAX_PIN)^X_MAX_ENDSTOP_INVERTING)?MSG_ENDSTOP_HIT:MSG_ENDSTOP_OPEN));
  2672. #endif
  2673. #if defined(Y_MIN_PIN) && Y_MIN_PIN > -1
  2674. SERIAL_PROTOCOLPGM(MSG_Y_MIN);
  2675. SERIAL_PROTOCOLLN(((READ(Y_MIN_PIN)^Y_MIN_ENDSTOP_INVERTING)?MSG_ENDSTOP_HIT:MSG_ENDSTOP_OPEN));
  2676. #endif
  2677. #if defined(Y_MAX_PIN) && Y_MAX_PIN > -1
  2678. SERIAL_PROTOCOLPGM(MSG_Y_MAX);
  2679. SERIAL_PROTOCOLLN(((READ(Y_MAX_PIN)^Y_MAX_ENDSTOP_INVERTING)?MSG_ENDSTOP_HIT:MSG_ENDSTOP_OPEN));
  2680. #endif
  2681. #if defined(Z_MIN_PIN) && Z_MIN_PIN > -1
  2682. SERIAL_PROTOCOLPGM(MSG_Z_MIN);
  2683. SERIAL_PROTOCOLLN(((READ(Z_MIN_PIN)^Z_MIN_ENDSTOP_INVERTING)?MSG_ENDSTOP_HIT:MSG_ENDSTOP_OPEN));
  2684. #endif
  2685. #if defined(Z_MAX_PIN) && Z_MAX_PIN > -1
  2686. SERIAL_PROTOCOLPGM(MSG_Z_MAX);
  2687. SERIAL_PROTOCOLLN(((READ(Z_MAX_PIN)^Z_MAX_ENDSTOP_INVERTING)?MSG_ENDSTOP_HIT:MSG_ENDSTOP_OPEN));
  2688. #endif
  2689. break;
  2690. //TODO: update for all axis, use for loop
  2691. #ifdef BLINKM
  2692. case 150: // M150
  2693. {
  2694. byte red;
  2695. byte grn;
  2696. byte blu;
  2697. if(code_seen('R')) red = code_value();
  2698. if(code_seen('U')) grn = code_value();
  2699. if(code_seen('B')) blu = code_value();
  2700. SendColors(red,grn,blu);
  2701. }
  2702. break;
  2703. #endif //BLINKM
  2704. case 200: // M200 D<millimeters> set filament diameter and set E axis units to cubic millimeters (use S0 to set back to millimeters).
  2705. {
  2706. tmp_extruder = active_extruder;
  2707. if(code_seen('T')) {
  2708. tmp_extruder = code_value();
  2709. if(tmp_extruder >= EXTRUDERS) {
  2710. SERIAL_ECHO_START;
  2711. SERIAL_ECHO(MSG_M200_INVALID_EXTRUDER);
  2712. break;
  2713. }
  2714. }
  2715. float area = .0;
  2716. if(code_seen('D')) {
  2717. float diameter = code_value();
  2718. // setting any extruder filament size disables volumetric on the assumption that
  2719. // slicers either generate in extruder values as cubic mm or as as filament feeds
  2720. // for all extruders
  2721. volumetric_enabled = (diameter != 0.0);
  2722. if (volumetric_enabled) {
  2723. filament_size[tmp_extruder] = diameter;
  2724. // make sure all extruders have some sane value for the filament size
  2725. for (int i=0; i<EXTRUDERS; i++)
  2726. if (! filament_size[i]) filament_size[i] = DEFAULT_NOMINAL_FILAMENT_DIA;
  2727. }
  2728. } else {
  2729. //reserved for setting filament diameter via UFID or filament measuring device
  2730. break;
  2731. }
  2732. calculate_volumetric_multipliers();
  2733. }
  2734. break;
  2735. case 201: // M201
  2736. for(int8_t i=0; i < NUM_AXIS; i++)
  2737. {
  2738. if(code_seen(axis_codes[i]))
  2739. {
  2740. max_acceleration_units_per_sq_second[i] = code_value();
  2741. }
  2742. }
  2743. // 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)
  2744. reset_acceleration_rates();
  2745. break;
  2746. #if 0 // Not used for Sprinter/grbl gen6
  2747. case 202: // M202
  2748. for(int8_t i=0; i < NUM_AXIS; i++) {
  2749. if(code_seen(axis_codes[i])) axis_travel_steps_per_sqr_second[i] = code_value() * axis_steps_per_unit[i];
  2750. }
  2751. break;
  2752. #endif
  2753. case 203: // M203 max feedrate mm/sec
  2754. for(int8_t i=0; i < NUM_AXIS; i++) {
  2755. if(code_seen(axis_codes[i])) max_feedrate[i] = code_value();
  2756. }
  2757. break;
  2758. case 204: // M204 acclereration S normal moves T filmanent only moves
  2759. {
  2760. if(code_seen('S')) acceleration = code_value() ;
  2761. if(code_seen('T')) retract_acceleration = code_value() ;
  2762. }
  2763. break;
  2764. 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
  2765. {
  2766. if(code_seen('S')) minimumfeedrate = code_value();
  2767. if(code_seen('T')) mintravelfeedrate = code_value();
  2768. if(code_seen('B')) minsegmenttime = code_value() ;
  2769. if(code_seen('X')) max_xy_jerk = code_value() ;
  2770. if(code_seen('Z')) max_z_jerk = code_value() ;
  2771. if(code_seen('E')) max_e_jerk = code_value() ;
  2772. }
  2773. break;
  2774. case 206: // M206 additional homing offset
  2775. for(int8_t i=0; i < 3; i++)
  2776. {
  2777. if(code_seen(axis_codes[i])) add_homing[i] = code_value();
  2778. }
  2779. #ifdef SCARA
  2780. if(code_seen('T')) // Theta
  2781. {
  2782. add_homing[X_AXIS] = code_value() ;
  2783. }
  2784. if(code_seen('P')) // Psi
  2785. {
  2786. add_homing[Y_AXIS] = code_value() ;
  2787. }
  2788. #endif
  2789. break;
  2790. #ifdef DELTA
  2791. case 665: // M665 set delta configurations L<diagonal_rod> R<delta_radius> S<segments_per_sec>
  2792. if(code_seen('L')) {
  2793. delta_diagonal_rod= code_value();
  2794. }
  2795. if(code_seen('R')) {
  2796. delta_radius= code_value();
  2797. }
  2798. if(code_seen('S')) {
  2799. delta_segments_per_second= code_value();
  2800. }
  2801. recalc_delta_settings(delta_radius, delta_diagonal_rod);
  2802. break;
  2803. case 666: // M666 set delta endstop adjustemnt
  2804. for(int8_t i=0; i < 3; i++)
  2805. {
  2806. if(code_seen(axis_codes[i])) endstop_adj[i] = code_value();
  2807. }
  2808. break;
  2809. #endif
  2810. #ifdef FWRETRACT
  2811. case 207: //M207 - set retract length S[positive mm] F[feedrate mm/min] Z[additional zlift/hop]
  2812. {
  2813. if(code_seen('S'))
  2814. {
  2815. retract_length = code_value() ;
  2816. }
  2817. if(code_seen('F'))
  2818. {
  2819. retract_feedrate = code_value()/60 ;
  2820. }
  2821. if(code_seen('Z'))
  2822. {
  2823. retract_zlift = code_value() ;
  2824. }
  2825. }break;
  2826. case 208: // M208 - set retract recover length S[positive mm surplus to the M207 S*] F[feedrate mm/min]
  2827. {
  2828. if(code_seen('S'))
  2829. {
  2830. retract_recover_length = code_value() ;
  2831. }
  2832. if(code_seen('F'))
  2833. {
  2834. retract_recover_feedrate = code_value()/60 ;
  2835. }
  2836. }break;
  2837. 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.
  2838. {
  2839. if(code_seen('S'))
  2840. {
  2841. int t= code_value() ;
  2842. switch(t)
  2843. {
  2844. case 0:
  2845. case 1:
  2846. {
  2847. autoretract_enabled = (t == 1);
  2848. for (int i=0; i<EXTRUDERS; i++) retracted[i] = false;
  2849. }break;
  2850. default:
  2851. SERIAL_ECHO_START;
  2852. SERIAL_ECHOPGM(MSG_UNKNOWN_COMMAND);
  2853. SERIAL_ECHO(cmdbuffer[bufindr]);
  2854. SERIAL_ECHOLNPGM("\"");
  2855. }
  2856. }
  2857. }break;
  2858. #endif // FWRETRACT
  2859. #if EXTRUDERS > 1
  2860. case 218: // M218 - set hotend offset (in mm), T<extruder_number> X<offset_on_X> Y<offset_on_Y>
  2861. {
  2862. if(setTargetedHotend(218)){
  2863. break;
  2864. }
  2865. if(code_seen('X'))
  2866. {
  2867. extruder_offset[X_AXIS][tmp_extruder] = code_value();
  2868. }
  2869. if(code_seen('Y'))
  2870. {
  2871. extruder_offset[Y_AXIS][tmp_extruder] = code_value();
  2872. }
  2873. #ifdef DUAL_X_CARRIAGE
  2874. if(code_seen('Z'))
  2875. {
  2876. extruder_offset[Z_AXIS][tmp_extruder] = code_value();
  2877. }
  2878. #endif
  2879. SERIAL_ECHO_START;
  2880. SERIAL_ECHOPGM(MSG_HOTEND_OFFSET);
  2881. for(tmp_extruder = 0; tmp_extruder < EXTRUDERS; tmp_extruder++)
  2882. {
  2883. SERIAL_ECHO(" ");
  2884. SERIAL_ECHO(extruder_offset[X_AXIS][tmp_extruder]);
  2885. SERIAL_ECHO(",");
  2886. SERIAL_ECHO(extruder_offset[Y_AXIS][tmp_extruder]);
  2887. #ifdef DUAL_X_CARRIAGE
  2888. SERIAL_ECHO(",");
  2889. SERIAL_ECHO(extruder_offset[Z_AXIS][tmp_extruder]);
  2890. #endif
  2891. }
  2892. SERIAL_EOL;
  2893. }break;
  2894. #endif
  2895. case 220: // M220 S<factor in percent>- set speed factor override percentage
  2896. {
  2897. if(code_seen('S'))
  2898. {
  2899. feedmultiply = code_value() ;
  2900. }
  2901. }
  2902. break;
  2903. case 221: // M221 S<factor in percent>- set extrude factor override percentage
  2904. {
  2905. if(code_seen('S'))
  2906. {
  2907. int tmp_code = code_value();
  2908. if (code_seen('T'))
  2909. {
  2910. if(setTargetedHotend(221)){
  2911. break;
  2912. }
  2913. extruder_multiply[tmp_extruder] = tmp_code;
  2914. }
  2915. else
  2916. {
  2917. extrudemultiply = tmp_code ;
  2918. }
  2919. }
  2920. }
  2921. break;
  2922. case 226: // M226 P<pin number> S<pin state>- Wait until the specified pin reaches the state required
  2923. {
  2924. if(code_seen('P')){
  2925. int pin_number = code_value(); // pin number
  2926. int pin_state = -1; // required pin state - default is inverted
  2927. if(code_seen('S')) pin_state = code_value(); // required pin state
  2928. if(pin_state >= -1 && pin_state <= 1){
  2929. for(int8_t i = 0; i < (int8_t)(sizeof(sensitive_pins)/sizeof(int)); i++)
  2930. {
  2931. if (sensitive_pins[i] == pin_number)
  2932. {
  2933. pin_number = -1;
  2934. break;
  2935. }
  2936. }
  2937. if (pin_number > -1)
  2938. {
  2939. int target = LOW;
  2940. st_synchronize();
  2941. pinMode(pin_number, INPUT);
  2942. switch(pin_state){
  2943. case 1:
  2944. target = HIGH;
  2945. break;
  2946. case 0:
  2947. target = LOW;
  2948. break;
  2949. case -1:
  2950. target = !digitalRead(pin_number);
  2951. break;
  2952. }
  2953. while(digitalRead(pin_number) != target){
  2954. manage_heater();
  2955. manage_inactivity();
  2956. lcd_update();
  2957. }
  2958. }
  2959. }
  2960. }
  2961. }
  2962. break;
  2963. #if NUM_SERVOS > 0
  2964. case 280: // M280 - set servo position absolute. P: servo index, S: angle or microseconds
  2965. {
  2966. int servo_index = -1;
  2967. int servo_position = 0;
  2968. if (code_seen('P'))
  2969. servo_index = code_value();
  2970. if (code_seen('S')) {
  2971. servo_position = code_value();
  2972. if ((servo_index >= 0) && (servo_index < NUM_SERVOS)) {
  2973. #if defined (ENABLE_AUTO_BED_LEVELING) && (PROBE_SERVO_DEACTIVATION_DELAY > 0)
  2974. servos[servo_index].attach(0);
  2975. #endif
  2976. servos[servo_index].write(servo_position);
  2977. #if defined (ENABLE_AUTO_BED_LEVELING) && (PROBE_SERVO_DEACTIVATION_DELAY > 0)
  2978. delay(PROBE_SERVO_DEACTIVATION_DELAY);
  2979. servos[servo_index].detach();
  2980. #endif
  2981. }
  2982. else {
  2983. SERIAL_ECHO_START;
  2984. SERIAL_ECHO("Servo ");
  2985. SERIAL_ECHO(servo_index);
  2986. SERIAL_ECHOLN(" out of range");
  2987. }
  2988. }
  2989. else if (servo_index >= 0) {
  2990. SERIAL_PROTOCOL(MSG_OK);
  2991. SERIAL_PROTOCOL(" Servo ");
  2992. SERIAL_PROTOCOL(servo_index);
  2993. SERIAL_PROTOCOL(": ");
  2994. SERIAL_PROTOCOL(servos[servo_index].read());
  2995. SERIAL_PROTOCOLLN("");
  2996. }
  2997. }
  2998. break;
  2999. #endif // NUM_SERVOS > 0
  3000. #if (LARGE_FLASH == true && ( BEEPER > 0 || defined(ULTRALCD) || defined(LCD_USE_I2C_BUZZER)))
  3001. case 300: // M300
  3002. {
  3003. int beepS = code_seen('S') ? code_value() : 110;
  3004. int beepP = code_seen('P') ? code_value() : 1000;
  3005. if (beepS > 0)
  3006. {
  3007. #if BEEPER > 0
  3008. tone(BEEPER, beepS);
  3009. delay(beepP);
  3010. noTone(BEEPER);
  3011. #elif defined(ULTRALCD)
  3012. lcd_buzz(beepS, beepP);
  3013. #elif defined(LCD_USE_I2C_BUZZER)
  3014. lcd_buzz(beepP, beepS);
  3015. #endif
  3016. }
  3017. else
  3018. {
  3019. delay(beepP);
  3020. }
  3021. }
  3022. break;
  3023. #endif // M300
  3024. #ifdef PIDTEMP
  3025. case 301: // M301
  3026. {
  3027. // multi-extruder PID patch: M301 updates or prints a single extruder's PID values
  3028. // default behaviour (omitting E parameter) is to update for extruder 0 only
  3029. int e = 0; // extruder being updated
  3030. if (code_seen('E'))
  3031. {
  3032. e = (int)code_value();
  3033. }
  3034. if (e < EXTRUDERS) // catch bad input value
  3035. {
  3036. if (code_seen('P')) PID_PARAM(Kp,e) = code_value();
  3037. if (code_seen('I')) PID_PARAM(Ki,e) = scalePID_i(code_value());
  3038. if (code_seen('D')) PID_PARAM(Kd,e) = scalePID_d(code_value());
  3039. #ifdef PID_ADD_EXTRUSION_RATE
  3040. if (code_seen('C')) PID_PARAM(Kc,e) = code_value();
  3041. #endif
  3042. updatePID();
  3043. SERIAL_PROTOCOL(MSG_OK);
  3044. #ifdef PID_PARAMS_PER_EXTRUDER
  3045. SERIAL_PROTOCOL(" e:"); // specify extruder in serial output
  3046. SERIAL_PROTOCOL(e);
  3047. #endif // PID_PARAMS_PER_EXTRUDER
  3048. SERIAL_PROTOCOL(" p:");
  3049. SERIAL_PROTOCOL(PID_PARAM(Kp,e));
  3050. SERIAL_PROTOCOL(" i:");
  3051. SERIAL_PROTOCOL(unscalePID_i(PID_PARAM(Ki,e)));
  3052. SERIAL_PROTOCOL(" d:");
  3053. SERIAL_PROTOCOL(unscalePID_d(PID_PARAM(Kd,e)));
  3054. #ifdef PID_ADD_EXTRUSION_RATE
  3055. SERIAL_PROTOCOL(" c:");
  3056. //Kc does not have scaling applied above, or in resetting defaults
  3057. SERIAL_PROTOCOL(PID_PARAM(Kc,e));
  3058. #endif
  3059. SERIAL_PROTOCOLLN("");
  3060. }
  3061. else
  3062. {
  3063. SERIAL_ECHO_START;
  3064. SERIAL_ECHOLN(MSG_INVALID_EXTRUDER);
  3065. }
  3066. }
  3067. break;
  3068. #endif //PIDTEMP
  3069. #ifdef PIDTEMPBED
  3070. case 304: // M304
  3071. {
  3072. if(code_seen('P')) bedKp = code_value();
  3073. if(code_seen('I')) bedKi = scalePID_i(code_value());
  3074. if(code_seen('D')) bedKd = scalePID_d(code_value());
  3075. updatePID();
  3076. SERIAL_PROTOCOL(MSG_OK);
  3077. SERIAL_PROTOCOL(" p:");
  3078. SERIAL_PROTOCOL(bedKp);
  3079. SERIAL_PROTOCOL(" i:");
  3080. SERIAL_PROTOCOL(unscalePID_i(bedKi));
  3081. SERIAL_PROTOCOL(" d:");
  3082. SERIAL_PROTOCOL(unscalePID_d(bedKd));
  3083. SERIAL_PROTOCOLLN("");
  3084. }
  3085. break;
  3086. #endif //PIDTEMP
  3087. case 240: // M240 Triggers a camera by emulating a Canon RC-1 : http://www.doc-diy.net/photo/rc-1_hacked/
  3088. {
  3089. #ifdef CHDK
  3090. OUT_WRITE(CHDK, HIGH);
  3091. chdkHigh = millis();
  3092. chdkActive = true;
  3093. #else
  3094. #if defined(PHOTOGRAPH_PIN) && PHOTOGRAPH_PIN > -1
  3095. const uint8_t NUM_PULSES=16;
  3096. const float PULSE_LENGTH=0.01524;
  3097. for(int i=0; i < NUM_PULSES; i++) {
  3098. WRITE(PHOTOGRAPH_PIN, HIGH);
  3099. _delay_ms(PULSE_LENGTH);
  3100. WRITE(PHOTOGRAPH_PIN, LOW);
  3101. _delay_ms(PULSE_LENGTH);
  3102. }
  3103. delay(7.33);
  3104. for(int i=0; i < NUM_PULSES; i++) {
  3105. WRITE(PHOTOGRAPH_PIN, HIGH);
  3106. _delay_ms(PULSE_LENGTH);
  3107. WRITE(PHOTOGRAPH_PIN, LOW);
  3108. _delay_ms(PULSE_LENGTH);
  3109. }
  3110. #endif
  3111. #endif //chdk end if
  3112. }
  3113. break;
  3114. #ifdef DOGLCD
  3115. case 250: // M250 Set LCD contrast value: C<value> (value 0..63)
  3116. {
  3117. if (code_seen('C')) {
  3118. lcd_setcontrast( ((int)code_value())&63 );
  3119. }
  3120. SERIAL_PROTOCOLPGM("lcd contrast value: ");
  3121. SERIAL_PROTOCOL(lcd_contrast);
  3122. SERIAL_PROTOCOLLN("");
  3123. }
  3124. break;
  3125. #endif
  3126. #ifdef PREVENT_DANGEROUS_EXTRUDE
  3127. case 302: // allow cold extrudes, or set the minimum extrude temperature
  3128. {
  3129. float temp = .0;
  3130. if (code_seen('S')) temp=code_value();
  3131. set_extrude_min_temp(temp);
  3132. }
  3133. break;
  3134. #endif
  3135. case 303: // M303 PID autotune
  3136. {
  3137. float temp = 150.0;
  3138. int e=0;
  3139. int c=5;
  3140. if (code_seen('E')) e=code_value();
  3141. if (e<0)
  3142. temp=70;
  3143. if (code_seen('S')) temp=code_value();
  3144. if (code_seen('C')) c=code_value();
  3145. PID_autotune(temp, e, c);
  3146. }
  3147. break;
  3148. #ifdef SCARA
  3149. case 360: // M360 SCARA Theta pos1
  3150. SERIAL_ECHOLN(" Cal: Theta 0 ");
  3151. //SoftEndsEnabled = false; // Ignore soft endstops during calibration
  3152. //SERIAL_ECHOLN(" Soft endstops disabled ");
  3153. if(Stopped == false) {
  3154. //get_coordinates(); // For X Y Z E F
  3155. delta[X_AXIS] = 0;
  3156. delta[Y_AXIS] = 120;
  3157. calculate_SCARA_forward_Transform(delta);
  3158. destination[X_AXIS] = delta[X_AXIS]/axis_scaling[X_AXIS];
  3159. destination[Y_AXIS] = delta[Y_AXIS]/axis_scaling[Y_AXIS];
  3160. prepare_move();
  3161. //ClearToSend();
  3162. return;
  3163. }
  3164. break;
  3165. case 361: // SCARA Theta pos2
  3166. SERIAL_ECHOLN(" Cal: Theta 90 ");
  3167. //SoftEndsEnabled = false; // Ignore soft endstops during calibration
  3168. //SERIAL_ECHOLN(" Soft endstops disabled ");
  3169. if(Stopped == false) {
  3170. //get_coordinates(); // For X Y Z E F
  3171. delta[X_AXIS] = 90;
  3172. delta[Y_AXIS] = 130;
  3173. calculate_SCARA_forward_Transform(delta);
  3174. destination[X_AXIS] = delta[X_AXIS]/axis_scaling[X_AXIS];
  3175. destination[Y_AXIS] = delta[Y_AXIS]/axis_scaling[Y_AXIS];
  3176. prepare_move();
  3177. //ClearToSend();
  3178. return;
  3179. }
  3180. break;
  3181. case 362: // SCARA Psi pos1
  3182. SERIAL_ECHOLN(" Cal: Psi 0 ");
  3183. //SoftEndsEnabled = false; // Ignore soft endstops during calibration
  3184. //SERIAL_ECHOLN(" Soft endstops disabled ");
  3185. if(Stopped == false) {
  3186. //get_coordinates(); // For X Y Z E F
  3187. delta[X_AXIS] = 60;
  3188. delta[Y_AXIS] = 180;
  3189. calculate_SCARA_forward_Transform(delta);
  3190. destination[X_AXIS] = delta[X_AXIS]/axis_scaling[X_AXIS];
  3191. destination[Y_AXIS] = delta[Y_AXIS]/axis_scaling[Y_AXIS];
  3192. prepare_move();
  3193. //ClearToSend();
  3194. return;
  3195. }
  3196. break;
  3197. case 363: // SCARA Psi pos2
  3198. SERIAL_ECHOLN(" Cal: Psi 90 ");
  3199. //SoftEndsEnabled = false; // Ignore soft endstops during calibration
  3200. //SERIAL_ECHOLN(" Soft endstops disabled ");
  3201. if(Stopped == false) {
  3202. //get_coordinates(); // For X Y Z E F
  3203. delta[X_AXIS] = 50;
  3204. delta[Y_AXIS] = 90;
  3205. calculate_SCARA_forward_Transform(delta);
  3206. destination[X_AXIS] = delta[X_AXIS]/axis_scaling[X_AXIS];
  3207. destination[Y_AXIS] = delta[Y_AXIS]/axis_scaling[Y_AXIS];
  3208. prepare_move();
  3209. //ClearToSend();
  3210. return;
  3211. }
  3212. break;
  3213. case 364: // SCARA Psi pos3 (90 deg to Theta)
  3214. SERIAL_ECHOLN(" Cal: Theta-Psi 90 ");
  3215. // SoftEndsEnabled = false; // Ignore soft endstops during calibration
  3216. //SERIAL_ECHOLN(" Soft endstops disabled ");
  3217. if(Stopped == false) {
  3218. //get_coordinates(); // For X Y Z E F
  3219. delta[X_AXIS] = 45;
  3220. delta[Y_AXIS] = 135;
  3221. calculate_SCARA_forward_Transform(delta);
  3222. destination[X_AXIS] = delta[X_AXIS]/axis_scaling[X_AXIS];
  3223. destination[Y_AXIS] = delta[Y_AXIS]/axis_scaling[Y_AXIS];
  3224. prepare_move();
  3225. //ClearToSend();
  3226. return;
  3227. }
  3228. break;
  3229. case 365: // M364 Set SCARA scaling for X Y Z
  3230. for(int8_t i=0; i < 3; i++)
  3231. {
  3232. if(code_seen(axis_codes[i]))
  3233. {
  3234. axis_scaling[i] = code_value();
  3235. }
  3236. }
  3237. break;
  3238. #endif
  3239. #ifdef EXT_SOLENOID
  3240. case 380:
  3241. enable_solenoid_on_active_extruder();
  3242. break;
  3243. case 381:
  3244. disable_all_solenoids();
  3245. break;
  3246. #endif //EXT_SOLENOID
  3247. case 400: // M400 finish all moves
  3248. {
  3249. st_synchronize();
  3250. }
  3251. break;
  3252. #if defined(ENABLE_AUTO_BED_LEVELING) && defined(SERVO_ENDSTOPS) && not defined(Z_PROBE_SLED)
  3253. case 401:
  3254. {
  3255. engage_z_probe(); // Engage Z Servo endstop if available
  3256. }
  3257. break;
  3258. case 402:
  3259. {
  3260. retract_z_probe(); // Retract Z Servo endstop if enabled
  3261. }
  3262. break;
  3263. #endif
  3264. #ifdef FILAMENT_SENSOR
  3265. case 404: //M404 Enter the nominal filament width (3mm, 1.75mm ) N<3.0> or display nominal filament width
  3266. {
  3267. #if (FILWIDTH_PIN > -1)
  3268. if(code_seen('N')) filament_width_nominal=code_value();
  3269. else{
  3270. SERIAL_PROTOCOLPGM("Filament dia (nominal mm):");
  3271. SERIAL_PROTOCOLLN(filament_width_nominal);
  3272. }
  3273. #endif
  3274. }
  3275. break;
  3276. case 405: //M405 Turn on filament sensor for control
  3277. {
  3278. if(code_seen('D')) meas_delay_cm=code_value();
  3279. if(meas_delay_cm> MAX_MEASUREMENT_DELAY)
  3280. meas_delay_cm = MAX_MEASUREMENT_DELAY;
  3281. if(delay_index2 == -1) //initialize the ring buffer if it has not been done since startup
  3282. {
  3283. int temp_ratio = widthFil_to_size_ratio();
  3284. for (delay_index1=0; delay_index1<(MAX_MEASUREMENT_DELAY+1); ++delay_index1 ){
  3285. measurement_delay[delay_index1]=temp_ratio-100; //subtract 100 to scale within a signed byte
  3286. }
  3287. delay_index1=0;
  3288. delay_index2=0;
  3289. }
  3290. filament_sensor = true ;
  3291. //SERIAL_PROTOCOLPGM("Filament dia (measured mm):");
  3292. //SERIAL_PROTOCOL(filament_width_meas);
  3293. //SERIAL_PROTOCOLPGM("Extrusion ratio(%):");
  3294. //SERIAL_PROTOCOL(extrudemultiply);
  3295. }
  3296. break;
  3297. case 406: //M406 Turn off filament sensor for control
  3298. {
  3299. filament_sensor = false ;
  3300. }
  3301. break;
  3302. case 407: //M407 Display measured filament diameter
  3303. {
  3304. SERIAL_PROTOCOLPGM("Filament dia (measured mm):");
  3305. SERIAL_PROTOCOLLN(filament_width_meas);
  3306. }
  3307. break;
  3308. #endif
  3309. case 500: // M500 Store settings in EEPROM
  3310. {
  3311. Config_StoreSettings();
  3312. }
  3313. break;
  3314. case 501: // M501 Read settings from EEPROM
  3315. {
  3316. Config_RetrieveSettings();
  3317. }
  3318. break;
  3319. case 502: // M502 Revert to default settings
  3320. {
  3321. Config_ResetDefault();
  3322. }
  3323. break;
  3324. case 503: // M503 print settings currently in memory
  3325. {
  3326. Config_PrintSettings(code_seen('S') && code_value == 0);
  3327. }
  3328. break;
  3329. #ifdef ABORT_ON_ENDSTOP_HIT_FEATURE_ENABLED
  3330. case 540:
  3331. {
  3332. if(code_seen('S')) abort_on_endstop_hit = code_value() > 0;
  3333. }
  3334. break;
  3335. #endif
  3336. #ifdef CUSTOM_M_CODE_SET_Z_PROBE_OFFSET
  3337. case CUSTOM_M_CODE_SET_Z_PROBE_OFFSET:
  3338. {
  3339. float value;
  3340. if (code_seen('Z'))
  3341. {
  3342. value = code_value();
  3343. if ((Z_PROBE_OFFSET_RANGE_MIN <= value) && (value <= Z_PROBE_OFFSET_RANGE_MAX))
  3344. {
  3345. zprobe_zoffset = -value; // compare w/ line 278 of ConfigurationStore.cpp
  3346. SERIAL_ECHO_START;
  3347. SERIAL_ECHOLNPGM(MSG_ZPROBE_ZOFFSET " " MSG_OK);
  3348. SERIAL_PROTOCOLLN("");
  3349. }
  3350. else
  3351. {
  3352. SERIAL_ECHO_START;
  3353. SERIAL_ECHOPGM(MSG_ZPROBE_ZOFFSET);
  3354. SERIAL_ECHOPGM(MSG_Z_MIN);
  3355. SERIAL_ECHO(Z_PROBE_OFFSET_RANGE_MIN);
  3356. SERIAL_ECHOPGM(MSG_Z_MAX);
  3357. SERIAL_ECHO(Z_PROBE_OFFSET_RANGE_MAX);
  3358. SERIAL_PROTOCOLLN("");
  3359. }
  3360. }
  3361. else
  3362. {
  3363. SERIAL_ECHO_START;
  3364. SERIAL_ECHOLNPGM(MSG_ZPROBE_ZOFFSET " : ");
  3365. SERIAL_ECHO(-zprobe_zoffset);
  3366. SERIAL_PROTOCOLLN("");
  3367. }
  3368. break;
  3369. }
  3370. #endif // CUSTOM_M_CODE_SET_Z_PROBE_OFFSET
  3371. #ifdef FILAMENTCHANGEENABLE
  3372. case 600: //Pause for filament change X[pos] Y[pos] Z[relative lift] E[initial retract] L[later retract distance for removal]
  3373. {
  3374. float target[NUM_AXIS], lastpos[NUM_AXIS], fr60 = feedrate/60;
  3375. for (int i=0; i<NUM_AXIS; i++)
  3376. target[i] = lastpos[i] = current_position[i];
  3377. #define BASICPLAN plan_buffer_line(target[X_AXIS], target[Y_AXIS], target[Z_AXIS], target[E_AXIS], fr60, active_extruder);
  3378. #ifdef DELTA
  3379. #define RUNPLAN calculate_delta(target); BASICPLAN
  3380. #else
  3381. #define RUNPLAN BASICPLAN
  3382. #endif
  3383. //retract by E
  3384. if(code_seen('E'))
  3385. {
  3386. target[E_AXIS]+= code_value();
  3387. }
  3388. else
  3389. {
  3390. #ifdef FILAMENTCHANGE_FIRSTRETRACT
  3391. target[E_AXIS]+= FILAMENTCHANGE_FIRSTRETRACT ;
  3392. #endif
  3393. }
  3394. RUNPLAN;
  3395. //lift Z
  3396. if(code_seen('Z'))
  3397. {
  3398. target[Z_AXIS]+= code_value();
  3399. }
  3400. else
  3401. {
  3402. #ifdef FILAMENTCHANGE_ZADD
  3403. target[Z_AXIS]+= FILAMENTCHANGE_ZADD ;
  3404. #endif
  3405. }
  3406. RUNPLAN;
  3407. //move xy
  3408. if(code_seen('X'))
  3409. {
  3410. target[X_AXIS]= code_value();
  3411. }
  3412. else
  3413. {
  3414. #ifdef FILAMENTCHANGE_XPOS
  3415. target[X_AXIS]= FILAMENTCHANGE_XPOS ;
  3416. #endif
  3417. }
  3418. if(code_seen('Y'))
  3419. {
  3420. target[Y_AXIS]= code_value();
  3421. }
  3422. else
  3423. {
  3424. #ifdef FILAMENTCHANGE_YPOS
  3425. target[Y_AXIS]= FILAMENTCHANGE_YPOS ;
  3426. #endif
  3427. }
  3428. RUNPLAN;
  3429. if(code_seen('L'))
  3430. {
  3431. target[E_AXIS]+= code_value();
  3432. }
  3433. else
  3434. {
  3435. #ifdef FILAMENTCHANGE_FINALRETRACT
  3436. target[E_AXIS]+= FILAMENTCHANGE_FINALRETRACT ;
  3437. #endif
  3438. }
  3439. RUNPLAN;
  3440. //finish moves
  3441. st_synchronize();
  3442. //disable extruder steppers so filament can be removed
  3443. disable_e0();
  3444. disable_e1();
  3445. disable_e2();
  3446. disable_e3();
  3447. delay(100);
  3448. LCD_ALERTMESSAGEPGM(MSG_FILAMENTCHANGE);
  3449. uint8_t cnt=0;
  3450. while(!lcd_clicked()){
  3451. cnt++;
  3452. manage_heater();
  3453. manage_inactivity(true);
  3454. lcd_update();
  3455. if(cnt==0)
  3456. {
  3457. #if BEEPER > 0
  3458. OUT_WRITE(BEEPER,HIGH);
  3459. delay(3);
  3460. WRITE(BEEPER,LOW);
  3461. delay(3);
  3462. #else
  3463. #if !defined(LCD_FEEDBACK_FREQUENCY_HZ) || !defined(LCD_FEEDBACK_FREQUENCY_DURATION_MS)
  3464. lcd_buzz(1000/6,100);
  3465. #else
  3466. lcd_buzz(LCD_FEEDBACK_FREQUENCY_DURATION_MS,LCD_FEEDBACK_FREQUENCY_HZ);
  3467. #endif
  3468. #endif
  3469. }
  3470. }
  3471. //return to normal
  3472. if(code_seen('L'))
  3473. {
  3474. target[E_AXIS]+= -code_value();
  3475. }
  3476. else
  3477. {
  3478. #ifdef FILAMENTCHANGE_FINALRETRACT
  3479. target[E_AXIS]+=(-1)*FILAMENTCHANGE_FINALRETRACT ;
  3480. #endif
  3481. }
  3482. current_position[E_AXIS]=target[E_AXIS]; //the long retract of L is compensated by manual filament feeding
  3483. plan_set_e_position(current_position[E_AXIS]);
  3484. RUNPLAN; //should do nothing
  3485. //reset LCD alert message
  3486. lcd_reset_alert_level();
  3487. #ifdef DELTA
  3488. calculate_delta(lastpos);
  3489. plan_buffer_line(delta[X_AXIS], delta[Y_AXIS], delta[Z_AXIS], target[E_AXIS], fr60, active_extruder); //move xyz back
  3490. plan_buffer_line(delta[X_AXIS], delta[Y_AXIS], delta[Z_AXIS], lastpos[E_AXIS], fr60, active_extruder); //final untretract
  3491. #else
  3492. plan_buffer_line(lastpos[X_AXIS], lastpos[Y_AXIS], target[Z_AXIS], target[E_AXIS], fr60, active_extruder); //move xy back
  3493. plan_buffer_line(lastpos[X_AXIS], lastpos[Y_AXIS], lastpos[Z_AXIS], target[E_AXIS], fr60, active_extruder); //move z back
  3494. plan_buffer_line(lastpos[X_AXIS], lastpos[Y_AXIS], lastpos[Z_AXIS], lastpos[E_AXIS], fr60, active_extruder); //final untretract
  3495. #endif
  3496. }
  3497. break;
  3498. #endif //FILAMENTCHANGEENABLE
  3499. #ifdef DUAL_X_CARRIAGE
  3500. case 605: // Set dual x-carriage movement mode:
  3501. // M605 S0: Full control mode. The slicer has full control over x-carriage movement
  3502. // M605 S1: Auto-park mode. The inactive head will auto park/unpark without slicer involvement
  3503. // M605 S2 [Xnnn] [Rmmm]: Duplication mode. The second extruder will duplicate the first with nnn
  3504. // millimeters x-offset and an optional differential hotend temperature of
  3505. // mmm degrees. E.g., with "M605 S2 X100 R2" the second extruder will duplicate
  3506. // the first with a spacing of 100mm in the x direction and 2 degrees hotter.
  3507. //
  3508. // Note: the X axis should be homed after changing dual x-carriage mode.
  3509. {
  3510. st_synchronize();
  3511. if (code_seen('S'))
  3512. dual_x_carriage_mode = code_value();
  3513. if (dual_x_carriage_mode == DXC_DUPLICATION_MODE)
  3514. {
  3515. if (code_seen('X'))
  3516. duplicate_extruder_x_offset = max(code_value(),X2_MIN_POS - x_home_pos(0));
  3517. if (code_seen('R'))
  3518. duplicate_extruder_temp_offset = code_value();
  3519. SERIAL_ECHO_START;
  3520. SERIAL_ECHOPGM(MSG_HOTEND_OFFSET);
  3521. SERIAL_ECHO(" ");
  3522. SERIAL_ECHO(extruder_offset[X_AXIS][0]);
  3523. SERIAL_ECHO(",");
  3524. SERIAL_ECHO(extruder_offset[Y_AXIS][0]);
  3525. SERIAL_ECHO(" ");
  3526. SERIAL_ECHO(duplicate_extruder_x_offset);
  3527. SERIAL_ECHO(",");
  3528. SERIAL_ECHOLN(extruder_offset[Y_AXIS][1]);
  3529. }
  3530. else if (dual_x_carriage_mode != DXC_FULL_CONTROL_MODE && dual_x_carriage_mode != DXC_AUTO_PARK_MODE)
  3531. {
  3532. dual_x_carriage_mode = DEFAULT_DUAL_X_CARRIAGE_MODE;
  3533. }
  3534. active_extruder_parked = false;
  3535. extruder_duplication_enabled = false;
  3536. delayed_move_time = 0;
  3537. }
  3538. break;
  3539. #endif //DUAL_X_CARRIAGE
  3540. case 907: // M907 Set digital trimpot motor current using axis codes.
  3541. {
  3542. #if defined(DIGIPOTSS_PIN) && DIGIPOTSS_PIN > -1
  3543. for(int i=0;i<NUM_AXIS;i++) if(code_seen(axis_codes[i])) digipot_current(i,code_value());
  3544. if(code_seen('B')) digipot_current(4,code_value());
  3545. if(code_seen('S')) for(int i=0;i<=4;i++) digipot_current(i,code_value());
  3546. #endif
  3547. #ifdef MOTOR_CURRENT_PWM_XY_PIN
  3548. if(code_seen('X')) digipot_current(0, code_value());
  3549. #endif
  3550. #ifdef MOTOR_CURRENT_PWM_Z_PIN
  3551. if(code_seen('Z')) digipot_current(1, code_value());
  3552. #endif
  3553. #ifdef MOTOR_CURRENT_PWM_E_PIN
  3554. if(code_seen('E')) digipot_current(2, code_value());
  3555. #endif
  3556. #ifdef DIGIPOT_I2C
  3557. // this one uses actual amps in floating point
  3558. for(int i=0;i<NUM_AXIS;i++) if(code_seen(axis_codes[i])) digipot_i2c_set_current(i, code_value());
  3559. // for each additional extruder (named B,C,D,E..., channels 4,5,6,7...)
  3560. for(int i=NUM_AXIS;i<DIGIPOT_I2C_NUM_CHANNELS;i++) if(code_seen('B'+i-NUM_AXIS)) digipot_i2c_set_current(i, code_value());
  3561. #endif
  3562. }
  3563. break;
  3564. case 908: // M908 Control digital trimpot directly.
  3565. {
  3566. #if defined(DIGIPOTSS_PIN) && DIGIPOTSS_PIN > -1
  3567. uint8_t channel,current;
  3568. if(code_seen('P')) channel=code_value();
  3569. if(code_seen('S')) current=code_value();
  3570. digitalPotWrite(channel, current);
  3571. #endif
  3572. }
  3573. break;
  3574. case 350: // M350 Set microstepping mode. Warning: Steps per unit remains unchanged. S code sets stepping mode for all drivers.
  3575. {
  3576. #if defined(X_MS1_PIN) && X_MS1_PIN > -1
  3577. if(code_seen('S')) for(int i=0;i<=4;i++) microstep_mode(i,code_value());
  3578. for(int i=0;i<NUM_AXIS;i++) if(code_seen(axis_codes[i])) microstep_mode(i,(uint8_t)code_value());
  3579. if(code_seen('B')) microstep_mode(4,code_value());
  3580. microstep_readings();
  3581. #endif
  3582. }
  3583. break;
  3584. case 351: // M351 Toggle MS1 MS2 pins directly, S# determines MS1 or MS2, X# sets the pin high/low.
  3585. {
  3586. #if defined(X_MS1_PIN) && X_MS1_PIN > -1
  3587. if(code_seen('S')) switch((int)code_value())
  3588. {
  3589. case 1:
  3590. for(int i=0;i<NUM_AXIS;i++) if(code_seen(axis_codes[i])) microstep_ms(i,code_value(),-1);
  3591. if(code_seen('B')) microstep_ms(4,code_value(),-1);
  3592. break;
  3593. case 2:
  3594. for(int i=0;i<NUM_AXIS;i++) if(code_seen(axis_codes[i])) microstep_ms(i,-1,code_value());
  3595. if(code_seen('B')) microstep_ms(4,-1,code_value());
  3596. break;
  3597. }
  3598. microstep_readings();
  3599. #endif
  3600. }
  3601. break;
  3602. case 999: // M999: Restart after being stopped
  3603. Stopped = false;
  3604. lcd_reset_alert_level();
  3605. gcode_LastN = Stopped_gcode_LastN;
  3606. FlushSerialRequestResend();
  3607. break;
  3608. }
  3609. }
  3610. else if(code_seen('T'))
  3611. {
  3612. tmp_extruder = code_value();
  3613. if(tmp_extruder >= EXTRUDERS) {
  3614. SERIAL_ECHO_START;
  3615. SERIAL_ECHO("T");
  3616. SERIAL_ECHO(tmp_extruder);
  3617. SERIAL_ECHOLN(MSG_INVALID_EXTRUDER);
  3618. }
  3619. else {
  3620. boolean make_move = false;
  3621. if(code_seen('F')) {
  3622. make_move = true;
  3623. next_feedrate = code_value();
  3624. if(next_feedrate > 0.0) {
  3625. feedrate = next_feedrate;
  3626. }
  3627. }
  3628. #if EXTRUDERS > 1
  3629. if(tmp_extruder != active_extruder) {
  3630. // Save current position to return to after applying extruder offset
  3631. memcpy(destination, current_position, sizeof(destination));
  3632. #ifdef DUAL_X_CARRIAGE
  3633. if (dual_x_carriage_mode == DXC_AUTO_PARK_MODE && Stopped == false &&
  3634. (delayed_move_time != 0 || current_position[X_AXIS] != x_home_pos(active_extruder)))
  3635. {
  3636. // Park old head: 1) raise 2) move to park position 3) lower
  3637. plan_buffer_line(current_position[X_AXIS], current_position[Y_AXIS], current_position[Z_AXIS] + TOOLCHANGE_PARK_ZLIFT,
  3638. current_position[E_AXIS], max_feedrate[Z_AXIS], active_extruder);
  3639. plan_buffer_line(x_home_pos(active_extruder), current_position[Y_AXIS], current_position[Z_AXIS] + TOOLCHANGE_PARK_ZLIFT,
  3640. current_position[E_AXIS], max_feedrate[X_AXIS], active_extruder);
  3641. plan_buffer_line(x_home_pos(active_extruder), current_position[Y_AXIS], current_position[Z_AXIS],
  3642. current_position[E_AXIS], max_feedrate[Z_AXIS], active_extruder);
  3643. st_synchronize();
  3644. }
  3645. // apply Y & Z extruder offset (x offset is already used in determining home pos)
  3646. current_position[Y_AXIS] = current_position[Y_AXIS] -
  3647. extruder_offset[Y_AXIS][active_extruder] +
  3648. extruder_offset[Y_AXIS][tmp_extruder];
  3649. current_position[Z_AXIS] = current_position[Z_AXIS] -
  3650. extruder_offset[Z_AXIS][active_extruder] +
  3651. extruder_offset[Z_AXIS][tmp_extruder];
  3652. active_extruder = tmp_extruder;
  3653. // This function resets the max/min values - the current position may be overwritten below.
  3654. axis_is_at_home(X_AXIS);
  3655. if (dual_x_carriage_mode == DXC_FULL_CONTROL_MODE)
  3656. {
  3657. current_position[X_AXIS] = inactive_extruder_x_pos;
  3658. inactive_extruder_x_pos = destination[X_AXIS];
  3659. }
  3660. else if (dual_x_carriage_mode == DXC_DUPLICATION_MODE)
  3661. {
  3662. active_extruder_parked = (active_extruder == 0); // this triggers the second extruder to move into the duplication position
  3663. if (active_extruder == 0 || active_extruder_parked)
  3664. current_position[X_AXIS] = inactive_extruder_x_pos;
  3665. else
  3666. current_position[X_AXIS] = destination[X_AXIS] + duplicate_extruder_x_offset;
  3667. inactive_extruder_x_pos = destination[X_AXIS];
  3668. extruder_duplication_enabled = false;
  3669. }
  3670. else
  3671. {
  3672. // record raised toolhead position for use by unpark
  3673. memcpy(raised_parked_position, current_position, sizeof(raised_parked_position));
  3674. raised_parked_position[Z_AXIS] += TOOLCHANGE_UNPARK_ZLIFT;
  3675. active_extruder_parked = true;
  3676. delayed_move_time = 0;
  3677. }
  3678. #else
  3679. // Offset extruder (only by XY)
  3680. int i;
  3681. for(i = 0; i < 2; i++) {
  3682. current_position[i] = current_position[i] -
  3683. extruder_offset[i][active_extruder] +
  3684. extruder_offset[i][tmp_extruder];
  3685. }
  3686. // Set the new active extruder and position
  3687. active_extruder = tmp_extruder;
  3688. #endif //else DUAL_X_CARRIAGE
  3689. #ifdef DELTA
  3690. calculate_delta(current_position); // change cartesian kinematic to delta kinematic;
  3691. //sent position to plan_set_position();
  3692. plan_set_position(delta[X_AXIS], delta[Y_AXIS], delta[Z_AXIS],current_position[E_AXIS]);
  3693. #else
  3694. plan_set_position(current_position[X_AXIS], current_position[Y_AXIS], current_position[Z_AXIS], current_position[E_AXIS]);
  3695. #endif
  3696. // Move to the old position if 'F' was in the parameters
  3697. if(make_move && Stopped == false) {
  3698. prepare_move();
  3699. }
  3700. }
  3701. #ifdef EXT_SOLENOID
  3702. st_synchronize();
  3703. disable_all_solenoids();
  3704. enable_solenoid_on_active_extruder();
  3705. #endif //EXT_SOLENOID
  3706. #endif
  3707. SERIAL_ECHO_START;
  3708. SERIAL_ECHO(MSG_ACTIVE_EXTRUDER);
  3709. SERIAL_PROTOCOLLN((int)active_extruder);
  3710. }
  3711. }
  3712. else
  3713. {
  3714. SERIAL_ECHO_START;
  3715. SERIAL_ECHOPGM(MSG_UNKNOWN_COMMAND);
  3716. SERIAL_ECHO(cmdbuffer[bufindr]);
  3717. SERIAL_ECHOLNPGM("\"");
  3718. }
  3719. ClearToSend();
  3720. }
  3721. void FlushSerialRequestResend()
  3722. {
  3723. //char cmdbuffer[bufindr][100]="Resend:";
  3724. MYSERIAL.flush();
  3725. SERIAL_PROTOCOLPGM(MSG_RESEND);
  3726. SERIAL_PROTOCOLLN(gcode_LastN + 1);
  3727. ClearToSend();
  3728. }
  3729. void ClearToSend()
  3730. {
  3731. previous_millis_cmd = millis();
  3732. #ifdef SDSUPPORT
  3733. if(fromsd[bufindr])
  3734. return;
  3735. #endif //SDSUPPORT
  3736. SERIAL_PROTOCOLLNPGM(MSG_OK);
  3737. }
  3738. void get_coordinates()
  3739. {
  3740. bool seen[4]={false,false,false,false};
  3741. for(int8_t i=0; i < NUM_AXIS; i++) {
  3742. if(code_seen(axis_codes[i]))
  3743. {
  3744. destination[i] = (float)code_value() + (axis_relative_modes[i] || relative_mode)*current_position[i];
  3745. seen[i]=true;
  3746. }
  3747. else destination[i] = current_position[i]; //Are these else lines really needed?
  3748. }
  3749. if(code_seen('F')) {
  3750. next_feedrate = code_value();
  3751. if(next_feedrate > 0.0) feedrate = next_feedrate;
  3752. }
  3753. }
  3754. void get_arc_coordinates()
  3755. {
  3756. #ifdef SF_ARC_FIX
  3757. bool relative_mode_backup = relative_mode;
  3758. relative_mode = true;
  3759. #endif
  3760. get_coordinates();
  3761. #ifdef SF_ARC_FIX
  3762. relative_mode=relative_mode_backup;
  3763. #endif
  3764. if(code_seen('I')) {
  3765. offset[0] = code_value();
  3766. }
  3767. else {
  3768. offset[0] = 0.0;
  3769. }
  3770. if(code_seen('J')) {
  3771. offset[1] = code_value();
  3772. }
  3773. else {
  3774. offset[1] = 0.0;
  3775. }
  3776. }
  3777. void clamp_to_software_endstops(float target[3])
  3778. {
  3779. if (min_software_endstops) {
  3780. if (target[X_AXIS] < min_pos[X_AXIS]) target[X_AXIS] = min_pos[X_AXIS];
  3781. if (target[Y_AXIS] < min_pos[Y_AXIS]) target[Y_AXIS] = min_pos[Y_AXIS];
  3782. float negative_z_offset = 0;
  3783. #ifdef ENABLE_AUTO_BED_LEVELING
  3784. if (Z_PROBE_OFFSET_FROM_EXTRUDER < 0) negative_z_offset = negative_z_offset + Z_PROBE_OFFSET_FROM_EXTRUDER;
  3785. if (add_homing[Z_AXIS] < 0) negative_z_offset = negative_z_offset + add_homing[Z_AXIS];
  3786. #endif
  3787. if (target[Z_AXIS] < min_pos[Z_AXIS]+negative_z_offset) target[Z_AXIS] = min_pos[Z_AXIS]+negative_z_offset;
  3788. }
  3789. if (max_software_endstops) {
  3790. if (target[X_AXIS] > max_pos[X_AXIS]) target[X_AXIS] = max_pos[X_AXIS];
  3791. if (target[Y_AXIS] > max_pos[Y_AXIS]) target[Y_AXIS] = max_pos[Y_AXIS];
  3792. if (target[Z_AXIS] > max_pos[Z_AXIS]) target[Z_AXIS] = max_pos[Z_AXIS];
  3793. }
  3794. }
  3795. #ifdef DELTA
  3796. void recalc_delta_settings(float radius, float diagonal_rod)
  3797. {
  3798. delta_tower1_x= -SIN_60*radius; // front left tower
  3799. delta_tower1_y= -COS_60*radius;
  3800. delta_tower2_x= SIN_60*radius; // front right tower
  3801. delta_tower2_y= -COS_60*radius;
  3802. delta_tower3_x= 0.0; // back middle tower
  3803. delta_tower3_y= radius;
  3804. delta_diagonal_rod_2= sq(diagonal_rod);
  3805. }
  3806. void calculate_delta(float cartesian[3])
  3807. {
  3808. delta[X_AXIS] = sqrt(delta_diagonal_rod_2
  3809. - sq(delta_tower1_x-cartesian[X_AXIS])
  3810. - sq(delta_tower1_y-cartesian[Y_AXIS])
  3811. ) + cartesian[Z_AXIS];
  3812. delta[Y_AXIS] = sqrt(delta_diagonal_rod_2
  3813. - sq(delta_tower2_x-cartesian[X_AXIS])
  3814. - sq(delta_tower2_y-cartesian[Y_AXIS])
  3815. ) + cartesian[Z_AXIS];
  3816. delta[Z_AXIS] = sqrt(delta_diagonal_rod_2
  3817. - sq(delta_tower3_x-cartesian[X_AXIS])
  3818. - sq(delta_tower3_y-cartesian[Y_AXIS])
  3819. ) + cartesian[Z_AXIS];
  3820. /*
  3821. SERIAL_ECHOPGM("cartesian x="); SERIAL_ECHO(cartesian[X_AXIS]);
  3822. SERIAL_ECHOPGM(" y="); SERIAL_ECHO(cartesian[Y_AXIS]);
  3823. SERIAL_ECHOPGM(" z="); SERIAL_ECHOLN(cartesian[Z_AXIS]);
  3824. SERIAL_ECHOPGM("delta x="); SERIAL_ECHO(delta[X_AXIS]);
  3825. SERIAL_ECHOPGM(" y="); SERIAL_ECHO(delta[Y_AXIS]);
  3826. SERIAL_ECHOPGM(" z="); SERIAL_ECHOLN(delta[Z_AXIS]);
  3827. */
  3828. }
  3829. #endif
  3830. void prepare_move()
  3831. {
  3832. clamp_to_software_endstops(destination);
  3833. previous_millis_cmd = millis();
  3834. #ifdef SCARA //for now same as delta-code
  3835. float difference[NUM_AXIS];
  3836. for (int8_t i=0; i < NUM_AXIS; i++) {
  3837. difference[i] = destination[i] - current_position[i];
  3838. }
  3839. float cartesian_mm = sqrt( sq(difference[X_AXIS]) +
  3840. sq(difference[Y_AXIS]) +
  3841. sq(difference[Z_AXIS]));
  3842. if (cartesian_mm < 0.000001) { cartesian_mm = abs(difference[E_AXIS]); }
  3843. if (cartesian_mm < 0.000001) { return; }
  3844. float seconds = 6000 * cartesian_mm / feedrate / feedmultiply;
  3845. int steps = max(1, int(scara_segments_per_second * seconds));
  3846. //SERIAL_ECHOPGM("mm="); SERIAL_ECHO(cartesian_mm);
  3847. //SERIAL_ECHOPGM(" seconds="); SERIAL_ECHO(seconds);
  3848. //SERIAL_ECHOPGM(" steps="); SERIAL_ECHOLN(steps);
  3849. for (int s = 1; s <= steps; s++) {
  3850. float fraction = float(s) / float(steps);
  3851. for(int8_t i=0; i < NUM_AXIS; i++) {
  3852. destination[i] = current_position[i] + difference[i] * fraction;
  3853. }
  3854. calculate_delta(destination);
  3855. //SERIAL_ECHOPGM("destination[X_AXIS]="); SERIAL_ECHOLN(destination[X_AXIS]);
  3856. //SERIAL_ECHOPGM("destination[Y_AXIS]="); SERIAL_ECHOLN(destination[Y_AXIS]);
  3857. //SERIAL_ECHOPGM("destination[Z_AXIS]="); SERIAL_ECHOLN(destination[Z_AXIS]);
  3858. //SERIAL_ECHOPGM("delta[X_AXIS]="); SERIAL_ECHOLN(delta[X_AXIS]);
  3859. //SERIAL_ECHOPGM("delta[Y_AXIS]="); SERIAL_ECHOLN(delta[Y_AXIS]);
  3860. //SERIAL_ECHOPGM("delta[Z_AXIS]="); SERIAL_ECHOLN(delta[Z_AXIS]);
  3861. plan_buffer_line(delta[X_AXIS], delta[Y_AXIS], delta[Z_AXIS],
  3862. destination[E_AXIS], feedrate*feedmultiply/60/100.0,
  3863. active_extruder);
  3864. }
  3865. #endif // SCARA
  3866. #ifdef DELTA
  3867. float difference[NUM_AXIS];
  3868. for (int8_t i=0; i < NUM_AXIS; i++) {
  3869. difference[i] = destination[i] - current_position[i];
  3870. }
  3871. float cartesian_mm = sqrt(sq(difference[X_AXIS]) +
  3872. sq(difference[Y_AXIS]) +
  3873. sq(difference[Z_AXIS]));
  3874. if (cartesian_mm < 0.000001) { cartesian_mm = abs(difference[E_AXIS]); }
  3875. if (cartesian_mm < 0.000001) { return; }
  3876. float seconds = 6000 * cartesian_mm / feedrate / feedmultiply;
  3877. int steps = max(1, int(delta_segments_per_second * seconds));
  3878. // SERIAL_ECHOPGM("mm="); SERIAL_ECHO(cartesian_mm);
  3879. // SERIAL_ECHOPGM(" seconds="); SERIAL_ECHO(seconds);
  3880. // SERIAL_ECHOPGM(" steps="); SERIAL_ECHOLN(steps);
  3881. for (int s = 1; s <= steps; s++) {
  3882. float fraction = float(s) / float(steps);
  3883. for(int8_t i=0; i < NUM_AXIS; i++) {
  3884. destination[i] = current_position[i] + difference[i] * fraction;
  3885. }
  3886. calculate_delta(destination);
  3887. plan_buffer_line(delta[X_AXIS], delta[Y_AXIS], delta[Z_AXIS],
  3888. destination[E_AXIS], feedrate*feedmultiply/60/100.0,
  3889. active_extruder);
  3890. }
  3891. #endif // DELTA
  3892. #ifdef DUAL_X_CARRIAGE
  3893. if (active_extruder_parked)
  3894. {
  3895. if (dual_x_carriage_mode == DXC_DUPLICATION_MODE && active_extruder == 0)
  3896. {
  3897. // move duplicate extruder into correct duplication position.
  3898. plan_set_position(inactive_extruder_x_pos, current_position[Y_AXIS], current_position[Z_AXIS], current_position[E_AXIS]);
  3899. plan_buffer_line(current_position[X_AXIS] + duplicate_extruder_x_offset, current_position[Y_AXIS], current_position[Z_AXIS],
  3900. current_position[E_AXIS], max_feedrate[X_AXIS], 1);
  3901. plan_set_position(current_position[X_AXIS], current_position[Y_AXIS], current_position[Z_AXIS], current_position[E_AXIS]);
  3902. st_synchronize();
  3903. extruder_duplication_enabled = true;
  3904. active_extruder_parked = false;
  3905. }
  3906. else if (dual_x_carriage_mode == DXC_AUTO_PARK_MODE) // handle unparking of head
  3907. {
  3908. if (current_position[E_AXIS] == destination[E_AXIS])
  3909. {
  3910. // this is a travel move - skit it but keep track of current position (so that it can later
  3911. // be used as start of first non-travel move)
  3912. if (delayed_move_time != 0xFFFFFFFFUL)
  3913. {
  3914. memcpy(current_position, destination, sizeof(current_position));
  3915. if (destination[Z_AXIS] > raised_parked_position[Z_AXIS])
  3916. raised_parked_position[Z_AXIS] = destination[Z_AXIS];
  3917. delayed_move_time = millis();
  3918. return;
  3919. }
  3920. }
  3921. delayed_move_time = 0;
  3922. // unpark extruder: 1) raise, 2) move into starting XY position, 3) lower
  3923. 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);
  3924. plan_buffer_line(current_position[X_AXIS], current_position[Y_AXIS], raised_parked_position[Z_AXIS],
  3925. current_position[E_AXIS], min(max_feedrate[X_AXIS],max_feedrate[Y_AXIS]), active_extruder);
  3926. plan_buffer_line(current_position[X_AXIS], current_position[Y_AXIS], current_position[Z_AXIS],
  3927. current_position[E_AXIS], max_feedrate[Z_AXIS], active_extruder);
  3928. active_extruder_parked = false;
  3929. }
  3930. }
  3931. #endif //DUAL_X_CARRIAGE
  3932. #if ! (defined DELTA || defined SCARA)
  3933. // Do not use feedmultiply for E or Z only moves
  3934. if( (current_position[X_AXIS] == destination [X_AXIS]) && (current_position[Y_AXIS] == destination [Y_AXIS])) {
  3935. plan_buffer_line(destination[X_AXIS], destination[Y_AXIS], destination[Z_AXIS], destination[E_AXIS], feedrate/60, active_extruder);
  3936. }
  3937. else {
  3938. plan_buffer_line(destination[X_AXIS], destination[Y_AXIS], destination[Z_AXIS], destination[E_AXIS], feedrate*feedmultiply/60/100.0, active_extruder);
  3939. }
  3940. #endif // !(DELTA || SCARA)
  3941. for(int8_t i=0; i < NUM_AXIS; i++) {
  3942. current_position[i] = destination[i];
  3943. }
  3944. }
  3945. void prepare_arc_move(char isclockwise) {
  3946. float r = hypot(offset[X_AXIS], offset[Y_AXIS]); // Compute arc radius for mc_arc
  3947. // Trace the arc
  3948. mc_arc(current_position, destination, offset, X_AXIS, Y_AXIS, Z_AXIS, feedrate*feedmultiply/60/100.0, r, isclockwise, active_extruder);
  3949. // As far as the parser is concerned, the position is now == target. In reality the
  3950. // motion control system might still be processing the action and the real tool position
  3951. // in any intermediate location.
  3952. for(int8_t i=0; i < NUM_AXIS; i++) {
  3953. current_position[i] = destination[i];
  3954. }
  3955. previous_millis_cmd = millis();
  3956. }
  3957. #if defined(CONTROLLERFAN_PIN) && CONTROLLERFAN_PIN > -1
  3958. #if defined(FAN_PIN)
  3959. #if CONTROLLERFAN_PIN == FAN_PIN
  3960. #error "You cannot set CONTROLLERFAN_PIN equal to FAN_PIN"
  3961. #endif
  3962. #endif
  3963. unsigned long lastMotor = 0; //Save the time for when a motor was turned on last
  3964. unsigned long lastMotorCheck = 0;
  3965. void controllerFan()
  3966. {
  3967. if ((millis() - lastMotorCheck) >= 2500) //Not a time critical function, so we only check every 2500ms
  3968. {
  3969. lastMotorCheck = millis();
  3970. if(!READ(X_ENABLE_PIN) || !READ(Y_ENABLE_PIN) || !READ(Z_ENABLE_PIN) || (soft_pwm_bed > 0)
  3971. #if EXTRUDERS > 2
  3972. || !READ(E2_ENABLE_PIN)
  3973. #endif
  3974. #if EXTRUDER > 1
  3975. #if defined(X2_ENABLE_PIN) && X2_ENABLE_PIN > -1
  3976. || !READ(X2_ENABLE_PIN)
  3977. #endif
  3978. || !READ(E1_ENABLE_PIN)
  3979. #endif
  3980. || !READ(E0_ENABLE_PIN)) //If any of the drivers are enabled...
  3981. {
  3982. lastMotor = millis(); //... set time to NOW so the fan will turn on
  3983. }
  3984. if ((millis() - lastMotor) >= (CONTROLLERFAN_SECS*1000UL) || lastMotor == 0) //If the last time any driver was enabled, is longer since than CONTROLLERSEC...
  3985. {
  3986. digitalWrite(CONTROLLERFAN_PIN, 0);
  3987. analogWrite(CONTROLLERFAN_PIN, 0);
  3988. }
  3989. else
  3990. {
  3991. // allows digital or PWM fan output to be used (see M42 handling)
  3992. digitalWrite(CONTROLLERFAN_PIN, CONTROLLERFAN_SPEED);
  3993. analogWrite(CONTROLLERFAN_PIN, CONTROLLERFAN_SPEED);
  3994. }
  3995. }
  3996. }
  3997. #endif
  3998. #ifdef SCARA
  3999. void calculate_SCARA_forward_Transform(float f_scara[3])
  4000. {
  4001. // Perform forward kinematics, and place results in delta[3]
  4002. // The maths and first version has been done by QHARLEY . Integrated into masterbranch 06/2014 and slightly restructured by Joachim Cerny in June 2014
  4003. float x_sin, x_cos, y_sin, y_cos;
  4004. //SERIAL_ECHOPGM("f_delta x="); SERIAL_ECHO(f_scara[X_AXIS]);
  4005. //SERIAL_ECHOPGM(" y="); SERIAL_ECHO(f_scara[Y_AXIS]);
  4006. x_sin = sin(f_scara[X_AXIS]/SCARA_RAD2DEG) * Linkage_1;
  4007. x_cos = cos(f_scara[X_AXIS]/SCARA_RAD2DEG) * Linkage_1;
  4008. y_sin = sin(f_scara[Y_AXIS]/SCARA_RAD2DEG) * Linkage_2;
  4009. y_cos = cos(f_scara[Y_AXIS]/SCARA_RAD2DEG) * Linkage_2;
  4010. // SERIAL_ECHOPGM(" x_sin="); SERIAL_ECHO(x_sin);
  4011. // SERIAL_ECHOPGM(" x_cos="); SERIAL_ECHO(x_cos);
  4012. // SERIAL_ECHOPGM(" y_sin="); SERIAL_ECHO(y_sin);
  4013. // SERIAL_ECHOPGM(" y_cos="); SERIAL_ECHOLN(y_cos);
  4014. delta[X_AXIS] = x_cos + y_cos + SCARA_offset_x; //theta
  4015. delta[Y_AXIS] = x_sin + y_sin + SCARA_offset_y; //theta+phi
  4016. //SERIAL_ECHOPGM(" delta[X_AXIS]="); SERIAL_ECHO(delta[X_AXIS]);
  4017. //SERIAL_ECHOPGM(" delta[Y_AXIS]="); SERIAL_ECHOLN(delta[Y_AXIS]);
  4018. }
  4019. void calculate_delta(float cartesian[3]){
  4020. //reverse kinematics.
  4021. // Perform reversed kinematics, and place results in delta[3]
  4022. // The maths and first version has been done by QHARLEY . Integrated into masterbranch 06/2014 and slightly restructured by Joachim Cerny in June 2014
  4023. float SCARA_pos[2];
  4024. static float SCARA_C2, SCARA_S2, SCARA_K1, SCARA_K2, SCARA_theta, SCARA_psi;
  4025. SCARA_pos[X_AXIS] = cartesian[X_AXIS] * axis_scaling[X_AXIS] - SCARA_offset_x; //Translate SCARA to standard X Y
  4026. SCARA_pos[Y_AXIS] = cartesian[Y_AXIS] * axis_scaling[Y_AXIS] - SCARA_offset_y; // With scaling factor.
  4027. #if (Linkage_1 == Linkage_2)
  4028. SCARA_C2 = ( ( sq(SCARA_pos[X_AXIS]) + sq(SCARA_pos[Y_AXIS]) ) / (2 * (float)L1_2) ) - 1;
  4029. #else
  4030. SCARA_C2 = ( sq(SCARA_pos[X_AXIS]) + sq(SCARA_pos[Y_AXIS]) - (float)L1_2 - (float)L2_2 ) / 45000;
  4031. #endif
  4032. SCARA_S2 = sqrt( 1 - sq(SCARA_C2) );
  4033. SCARA_K1 = Linkage_1 + Linkage_2 * SCARA_C2;
  4034. SCARA_K2 = Linkage_2 * SCARA_S2;
  4035. SCARA_theta = ( atan2(SCARA_pos[X_AXIS],SCARA_pos[Y_AXIS])-atan2(SCARA_K1, SCARA_K2) ) * -1;
  4036. SCARA_psi = atan2(SCARA_S2,SCARA_C2);
  4037. delta[X_AXIS] = SCARA_theta * SCARA_RAD2DEG; // Multiply by 180/Pi - theta is support arm angle
  4038. delta[Y_AXIS] = (SCARA_theta + SCARA_psi) * SCARA_RAD2DEG; // - equal to sub arm angle (inverted motor)
  4039. delta[Z_AXIS] = cartesian[Z_AXIS];
  4040. /*
  4041. SERIAL_ECHOPGM("cartesian x="); SERIAL_ECHO(cartesian[X_AXIS]);
  4042. SERIAL_ECHOPGM(" y="); SERIAL_ECHO(cartesian[Y_AXIS]);
  4043. SERIAL_ECHOPGM(" z="); SERIAL_ECHOLN(cartesian[Z_AXIS]);
  4044. SERIAL_ECHOPGM("scara x="); SERIAL_ECHO(SCARA_pos[X_AXIS]);
  4045. SERIAL_ECHOPGM(" y="); SERIAL_ECHOLN(SCARA_pos[Y_AXIS]);
  4046. SERIAL_ECHOPGM("delta x="); SERIAL_ECHO(delta[X_AXIS]);
  4047. SERIAL_ECHOPGM(" y="); SERIAL_ECHO(delta[Y_AXIS]);
  4048. SERIAL_ECHOPGM(" z="); SERIAL_ECHOLN(delta[Z_AXIS]);
  4049. SERIAL_ECHOPGM("C2="); SERIAL_ECHO(SCARA_C2);
  4050. SERIAL_ECHOPGM(" S2="); SERIAL_ECHO(SCARA_S2);
  4051. SERIAL_ECHOPGM(" Theta="); SERIAL_ECHO(SCARA_theta);
  4052. SERIAL_ECHOPGM(" Psi="); SERIAL_ECHOLN(SCARA_psi);
  4053. SERIAL_ECHOLN(" ");*/
  4054. }
  4055. #endif
  4056. #ifdef TEMP_STAT_LEDS
  4057. static bool blue_led = false;
  4058. static bool red_led = false;
  4059. static uint32_t stat_update = 0;
  4060. void handle_status_leds(void) {
  4061. float max_temp = 0.0;
  4062. if(millis() > stat_update) {
  4063. stat_update += 500; // Update every 0.5s
  4064. for (int8_t cur_extruder = 0; cur_extruder < EXTRUDERS; ++cur_extruder) {
  4065. max_temp = max(max_temp, degHotend(cur_extruder));
  4066. max_temp = max(max_temp, degTargetHotend(cur_extruder));
  4067. }
  4068. #if defined(TEMP_BED_PIN) && TEMP_BED_PIN > -1
  4069. max_temp = max(max_temp, degTargetBed());
  4070. max_temp = max(max_temp, degBed());
  4071. #endif
  4072. if((max_temp > 55.0) && (red_led == false)) {
  4073. digitalWrite(STAT_LED_RED, 1);
  4074. digitalWrite(STAT_LED_BLUE, 0);
  4075. red_led = true;
  4076. blue_led = false;
  4077. }
  4078. if((max_temp < 54.0) && (blue_led == false)) {
  4079. digitalWrite(STAT_LED_RED, 0);
  4080. digitalWrite(STAT_LED_BLUE, 1);
  4081. red_led = false;
  4082. blue_led = true;
  4083. }
  4084. }
  4085. }
  4086. #endif
  4087. void manage_inactivity(bool ignore_stepper_queue/*=false*/) //default argument set in Marlin.h
  4088. {
  4089. #if defined(KILL_PIN) && KILL_PIN > -1
  4090. static int killCount = 0; // make the inactivity button a bit less responsive
  4091. const int KILL_DELAY = 10000;
  4092. #endif
  4093. #if defined(HOME_PIN) && HOME_PIN > -1
  4094. static int homeDebounceCount = 0; // poor man's debouncing count
  4095. const int HOME_DEBOUNCE_DELAY = 10000;
  4096. #endif
  4097. if(buflen < (BUFSIZE-1))
  4098. get_command();
  4099. if( (millis() - previous_millis_cmd) > max_inactive_time )
  4100. if(max_inactive_time)
  4101. kill();
  4102. if(stepper_inactive_time) {
  4103. if( (millis() - previous_millis_cmd) > stepper_inactive_time )
  4104. {
  4105. if(blocks_queued() == false && ignore_stepper_queue == false) {
  4106. disable_x();
  4107. disable_y();
  4108. disable_z();
  4109. disable_e0();
  4110. disable_e1();
  4111. disable_e2();
  4112. disable_e3();
  4113. }
  4114. }
  4115. }
  4116. #ifdef CHDK //Check if pin should be set to LOW after M240 set it to HIGH
  4117. if (chdkActive && (millis() - chdkHigh > CHDK_DELAY))
  4118. {
  4119. chdkActive = false;
  4120. WRITE(CHDK, LOW);
  4121. }
  4122. #endif
  4123. #if defined(KILL_PIN) && KILL_PIN > -1
  4124. // Check if the kill button was pressed and wait just in case it was an accidental
  4125. // key kill key press
  4126. // -------------------------------------------------------------------------------
  4127. if( 0 == READ(KILL_PIN) )
  4128. {
  4129. killCount++;
  4130. }
  4131. else if (killCount > 0)
  4132. {
  4133. killCount--;
  4134. }
  4135. // Exceeded threshold and we can confirm that it was not accidental
  4136. // KILL the machine
  4137. // ----------------------------------------------------------------
  4138. if ( killCount >= KILL_DELAY)
  4139. {
  4140. kill();
  4141. }
  4142. #endif
  4143. #if defined(HOME_PIN) && HOME_PIN > -1
  4144. // Check to see if we have to home, use poor man's debouncer
  4145. // ---------------------------------------------------------
  4146. if ( 0 == READ(HOME_PIN) )
  4147. {
  4148. if (homeDebounceCount == 0)
  4149. {
  4150. enquecommands_P((PSTR("G28")));
  4151. homeDebounceCount++;
  4152. LCD_ALERTMESSAGEPGM(MSG_AUTO_HOME);
  4153. }
  4154. else if (homeDebounceCount < HOME_DEBOUNCE_DELAY)
  4155. {
  4156. homeDebounceCount++;
  4157. }
  4158. else
  4159. {
  4160. homeDebounceCount = 0;
  4161. }
  4162. }
  4163. #endif
  4164. #if defined(CONTROLLERFAN_PIN) && CONTROLLERFAN_PIN > -1
  4165. controllerFan(); //Check if fan should be turned on to cool stepper drivers down
  4166. #endif
  4167. #ifdef EXTRUDER_RUNOUT_PREVENT
  4168. if( (millis() - previous_millis_cmd) > EXTRUDER_RUNOUT_SECONDS*1000 )
  4169. if(degHotend(active_extruder)>EXTRUDER_RUNOUT_MINTEMP)
  4170. {
  4171. bool oldstatus=READ(E0_ENABLE_PIN);
  4172. enable_e0();
  4173. float oldepos=current_position[E_AXIS];
  4174. float oldedes=destination[E_AXIS];
  4175. plan_buffer_line(destination[X_AXIS], destination[Y_AXIS], destination[Z_AXIS],
  4176. destination[E_AXIS]+EXTRUDER_RUNOUT_EXTRUDE*EXTRUDER_RUNOUT_ESTEPS/axis_steps_per_unit[E_AXIS],
  4177. EXTRUDER_RUNOUT_SPEED/60.*EXTRUDER_RUNOUT_ESTEPS/axis_steps_per_unit[E_AXIS], active_extruder);
  4178. current_position[E_AXIS]=oldepos;
  4179. destination[E_AXIS]=oldedes;
  4180. plan_set_e_position(oldepos);
  4181. previous_millis_cmd=millis();
  4182. st_synchronize();
  4183. WRITE(E0_ENABLE_PIN,oldstatus);
  4184. }
  4185. #endif
  4186. #if defined(DUAL_X_CARRIAGE)
  4187. // handle delayed move timeout
  4188. if (delayed_move_time != 0 && (millis() - delayed_move_time) > 1000 && Stopped == false)
  4189. {
  4190. // travel moves have been received so enact them
  4191. delayed_move_time = 0xFFFFFFFFUL; // force moves to be done
  4192. memcpy(destination,current_position,sizeof(destination));
  4193. prepare_move();
  4194. }
  4195. #endif
  4196. #ifdef TEMP_STAT_LEDS
  4197. handle_status_leds();
  4198. #endif
  4199. check_axes_activity();
  4200. }
  4201. void kill()
  4202. {
  4203. cli(); // Stop interrupts
  4204. disable_heater();
  4205. disable_x();
  4206. disable_y();
  4207. disable_z();
  4208. disable_e0();
  4209. disable_e1();
  4210. disable_e2();
  4211. disable_e3();
  4212. #if defined(PS_ON_PIN) && PS_ON_PIN > -1
  4213. pinMode(PS_ON_PIN,INPUT);
  4214. #endif
  4215. SERIAL_ERROR_START;
  4216. SERIAL_ERRORLNPGM(MSG_ERR_KILLED);
  4217. LCD_ALERTMESSAGEPGM(MSG_KILLED);
  4218. // FMC small patch to update the LCD before ending
  4219. sei(); // enable interrupts
  4220. for ( int i=5; i--; lcd_update())
  4221. {
  4222. delay(200);
  4223. }
  4224. cli(); // disable interrupts
  4225. suicide();
  4226. while(1) { /* Intentionally left empty */ } // Wait for reset
  4227. }
  4228. void Stop()
  4229. {
  4230. disable_heater();
  4231. if(Stopped == false) {
  4232. Stopped = true;
  4233. Stopped_gcode_LastN = gcode_LastN; // Save last g_code for restart
  4234. SERIAL_ERROR_START;
  4235. SERIAL_ERRORLNPGM(MSG_ERR_STOPPED);
  4236. LCD_MESSAGEPGM(MSG_STOPPED);
  4237. }
  4238. }
  4239. bool IsStopped() { return Stopped; };
  4240. #ifdef FAST_PWM_FAN
  4241. void setPwmFrequency(uint8_t pin, int val)
  4242. {
  4243. val &= 0x07;
  4244. switch(digitalPinToTimer(pin))
  4245. {
  4246. #if defined(TCCR0A)
  4247. case TIMER0A:
  4248. case TIMER0B:
  4249. // TCCR0B &= ~(_BV(CS00) | _BV(CS01) | _BV(CS02));
  4250. // TCCR0B |= val;
  4251. break;
  4252. #endif
  4253. #if defined(TCCR1A)
  4254. case TIMER1A:
  4255. case TIMER1B:
  4256. // TCCR1B &= ~(_BV(CS10) | _BV(CS11) | _BV(CS12));
  4257. // TCCR1B |= val;
  4258. break;
  4259. #endif
  4260. #if defined(TCCR2)
  4261. case TIMER2:
  4262. case TIMER2:
  4263. TCCR2 &= ~(_BV(CS10) | _BV(CS11) | _BV(CS12));
  4264. TCCR2 |= val;
  4265. break;
  4266. #endif
  4267. #if defined(TCCR2A)
  4268. case TIMER2A:
  4269. case TIMER2B:
  4270. TCCR2B &= ~(_BV(CS20) | _BV(CS21) | _BV(CS22));
  4271. TCCR2B |= val;
  4272. break;
  4273. #endif
  4274. #if defined(TCCR3A)
  4275. case TIMER3A:
  4276. case TIMER3B:
  4277. case TIMER3C:
  4278. TCCR3B &= ~(_BV(CS30) | _BV(CS31) | _BV(CS32));
  4279. TCCR3B |= val;
  4280. break;
  4281. #endif
  4282. #if defined(TCCR4A)
  4283. case TIMER4A:
  4284. case TIMER4B:
  4285. case TIMER4C:
  4286. TCCR4B &= ~(_BV(CS40) | _BV(CS41) | _BV(CS42));
  4287. TCCR4B |= val;
  4288. break;
  4289. #endif
  4290. #if defined(TCCR5A)
  4291. case TIMER5A:
  4292. case TIMER5B:
  4293. case TIMER5C:
  4294. TCCR5B &= ~(_BV(CS50) | _BV(CS51) | _BV(CS52));
  4295. TCCR5B |= val;
  4296. break;
  4297. #endif
  4298. }
  4299. }
  4300. #endif //FAST_PWM_FAN
  4301. bool setTargetedHotend(int code){
  4302. tmp_extruder = active_extruder;
  4303. if(code_seen('T')) {
  4304. tmp_extruder = code_value();
  4305. if(tmp_extruder >= EXTRUDERS) {
  4306. SERIAL_ECHO_START;
  4307. switch(code){
  4308. case 104:
  4309. SERIAL_ECHO(MSG_M104_INVALID_EXTRUDER);
  4310. break;
  4311. case 105:
  4312. SERIAL_ECHO(MSG_M105_INVALID_EXTRUDER);
  4313. break;
  4314. case 109:
  4315. SERIAL_ECHO(MSG_M109_INVALID_EXTRUDER);
  4316. break;
  4317. case 218:
  4318. SERIAL_ECHO(MSG_M218_INVALID_EXTRUDER);
  4319. break;
  4320. case 221:
  4321. SERIAL_ECHO(MSG_M221_INVALID_EXTRUDER);
  4322. break;
  4323. }
  4324. SERIAL_ECHOLN(tmp_extruder);
  4325. return true;
  4326. }
  4327. }
  4328. return false;
  4329. }
  4330. float calculate_volumetric_multiplier(float diameter) {
  4331. if (!volumetric_enabled || diameter == 0) return 1.0;
  4332. float d2 = diameter * 0.5;
  4333. return 1.0 / (M_PI * d2 * d2);
  4334. }
  4335. void calculate_volumetric_multipliers() {
  4336. for (int i=0; i<EXTRUDERS; i++)
  4337. volumetric_multiplier[i] = calculate_volumetric_multiplier(filament_size[i]);
  4338. }
  4339. #ifdef EXT_SOLENOID
  4340. void enable_solenoid(uint8_t num) {
  4341. switch(num) {
  4342. case 0:
  4343. OUT_WRITE(SOL0_PIN, HIGH);
  4344. break;
  4345. #if defined(SOL1_PIN) && SOL1_PIN > -1
  4346. case 1:
  4347. OUT_WRITE(SOL1_PIN, HIGH);
  4348. break;
  4349. #endif
  4350. #if defined(SOL2_PIN) && SOL2_PIN > -1
  4351. case 2:
  4352. OUT_WRITE(SOL2_PIN, HIGH);
  4353. break;
  4354. #endif
  4355. #if defined(SOL3_PIN) && SOL3_PIN > -1
  4356. case 3:
  4357. OUT_WRITE(SOL3_PIN, HIGH);
  4358. break;
  4359. #endif
  4360. default:
  4361. SERIAL_ECHO_START;
  4362. SERIAL_ECHOLNPGM(MSG_INVALID_SOLENOID);
  4363. break;
  4364. }
  4365. }
  4366. void enable_solenoid_on_active_extruder() { enable_solenoid(active_extruder); }
  4367. void disable_all_solenoids() {
  4368. OUT_WRITE(SOL0_PIN, LOW);
  4369. OUT_WRITE(SOL1_PIN, LOW);
  4370. OUT_WRITE(SOL2_PIN, LOW);
  4371. OUT_WRITE(SOL3_PIN, LOW);
  4372. }
  4373. #endif //EXT_SOLENOID