Marlin_main.cpp 206 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000600160026003600460056006600760086009601060116012601360146015601660176018601960206021602260236024602560266027602860296030603160326033603460356036603760386039604060416042604360446045604660476048604960506051605260536054605560566057605860596060606160626063606460656066606760686069607060716072607360746075
  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. #ifdef MESH_BED_LEVELING
  31. #include "mesh_bed_leveling.h"
  32. #include "mesh_bed_calibration.h"
  33. #endif
  34. #include "ultralcd.h"
  35. #include "Configuration_prusa.h"
  36. #include "planner.h"
  37. #include "stepper.h"
  38. #include "temperature.h"
  39. #include "motion_control.h"
  40. #include "cardreader.h"
  41. #include "watchdog.h"
  42. #include "ConfigurationStore.h"
  43. #include "language.h"
  44. #include "pins_arduino.h"
  45. #include "math.h"
  46. #include "util.h"
  47. #ifdef BLINKM
  48. #include "BlinkM.h"
  49. #include "Wire.h"
  50. #endif
  51. #ifdef ULTRALCD
  52. #include "ultralcd.h"
  53. #endif
  54. #if NUM_SERVOS > 0
  55. #include "Servo.h"
  56. #endif
  57. #if defined(DIGIPOTSS_PIN) && DIGIPOTSS_PIN > -1
  58. #include <SPI.h>
  59. #endif
  60. #define VERSION_STRING "1.0.2"
  61. #include "ultralcd.h"
  62. // Macros for bit masks
  63. #define BIT(b) (1<<(b))
  64. #define TEST(n,b) (((n)&BIT(b))!=0)
  65. #define SET_BIT(n,b,value) (n) ^= ((-value)^(n)) & (BIT(b))
  66. // look here for descriptions of G-codes: http://linuxcnc.org/handbook/gcode/g-code.html
  67. // http://objects.reprap.org/wiki/Mendel_User_Manual:_RepRapGCodes
  68. //Implemented Codes
  69. //-------------------
  70. // PRUSA CODES
  71. // P F - Returns FW versions
  72. // P R - Returns revision of printer
  73. // P Y - Starts filament allignment process for multicolor
  74. // G0 -> G1
  75. // G1 - Coordinated Movement X Y Z E
  76. // G2 - CW ARC
  77. // G3 - CCW ARC
  78. // G4 - Dwell S<seconds> or P<milliseconds>
  79. // G10 - retract filament according to settings of M207
  80. // G11 - retract recover filament according to settings of M208
  81. // G28 - Home all Axis
  82. // G29 - Detailed Z-Probe, probes the bed at 3 or more points. Will fail if you haven't homed yet.
  83. // G30 - Single Z Probe, probes bed at current XY location.
  84. // G31 - Dock sled (Z_PROBE_SLED only)
  85. // G32 - Undock sled (Z_PROBE_SLED only)
  86. // G80 - Automatic mesh bed leveling
  87. // G81 - Print bed profile
  88. // G90 - Use Absolute Coordinates
  89. // G91 - Use Relative Coordinates
  90. // G92 - Set current position to coordinates given
  91. // M Codes
  92. // M0 - Unconditional stop - Wait for user to press a button on the LCD (Only if ULTRA_LCD is enabled)
  93. // M1 - Same as M0
  94. // M17 - Enable/Power all stepper motors
  95. // M18 - Disable all stepper motors; same as M84
  96. // M20 - List SD card
  97. // M21 - Init SD card
  98. // M22 - Release SD card
  99. // M23 - Select SD file (M23 filename.g)
  100. // M24 - Start/resume SD print
  101. // M25 - Pause SD print
  102. // M26 - Set SD position in bytes (M26 S12345)
  103. // M27 - Report SD print status
  104. // M28 - Start SD write (M28 filename.g)
  105. // M29 - Stop SD write
  106. // M30 - Delete file from SD (M30 filename.g)
  107. // M31 - Output time since last M109 or SD card start to serial
  108. // M32 - Select file and start SD print (Can be used _while_ printing from SD card files):
  109. // syntax "M32 /path/filename#", or "M32 S<startpos bytes> !filename#"
  110. // Call gcode file : "M32 P !filename#" and return to caller file after finishing (similar to #include).
  111. // The '#' is necessary when calling from within sd files, as it stops buffer prereading
  112. // 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.
  113. // M80 - Turn on Power Supply
  114. // M81 - Turn off Power Supply
  115. // M82 - Set E codes absolute (default)
  116. // M83 - Set E codes relative while in Absolute Coordinates (G90) mode
  117. // M84 - Disable steppers until next move,
  118. // or use S<seconds> to specify an inactivity timeout, after which the steppers will be disabled. S0 to disable the timeout.
  119. // M85 - Set inactivity shutdown timer with parameter S<seconds>. To disable set zero (default)
  120. // M92 - Set axis_steps_per_unit - same syntax as G92
  121. // M104 - Set extruder target temp
  122. // M105 - Read current temp
  123. // M106 - Fan on
  124. // M107 - Fan off
  125. // M109 - Sxxx Wait for extruder current temp to reach target temp. Waits only when heating
  126. // Rxxx Wait for extruder current temp to reach target temp. Waits when heating and cooling
  127. // IF AUTOTEMP is enabled, S<mintemp> B<maxtemp> F<factor>. Exit autotemp by any M109 without F
  128. // M112 - Emergency stop
  129. // M114 - Output current position to serial port
  130. // M115 - Capabilities string
  131. // M117 - display message
  132. // M119 - Output Endstop status to serial port
  133. // M126 - Solenoid Air Valve Open (BariCUDA support by jmil)
  134. // M127 - Solenoid Air Valve Closed (BariCUDA vent to atmospheric pressure by jmil)
  135. // M128 - EtoP Open (BariCUDA EtoP = electricity to air pressure transducer by jmil)
  136. // M129 - EtoP Closed (BariCUDA EtoP = electricity to air pressure transducer by jmil)
  137. // M140 - Set bed target temp
  138. // 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.
  139. // M190 - Sxxx Wait for bed current temp to reach target temp. Waits only when heating
  140. // Rxxx Wait for bed current temp to reach target temp. Waits when heating and cooling
  141. // M200 D<millimeters>- set filament diameter and set E axis units to cubic millimeters (use S0 to set back to millimeters).
  142. // M201 - Set max acceleration in units/s^2 for print moves (M201 X1000 Y1000)
  143. // M202 - Set max acceleration in units/s^2 for travel moves (M202 X1000 Y1000) Unused in Marlin!!
  144. // M203 - Set maximum feedrate that your machine can sustain (M203 X200 Y200 Z300 E10000) in mm/sec
  145. // 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
  146. // 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
  147. // M206 - set additional homing offset
  148. // M207 - set retract length S[positive mm] F[feedrate mm/min] Z[additional zlift/hop], stays in mm regardless of M200 setting
  149. // M208 - set recover=unretract length S[positive mm surplus to the M207 S*] F[feedrate mm/sec]
  150. // 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.
  151. // M218 - set hotend offset (in mm): T<extruder_number> X<offset_on_X> Y<offset_on_Y>
  152. // M220 S<factor in percent>- set speed factor override percentage
  153. // M221 S<factor in percent>- set extrude factor override percentage
  154. // M226 P<pin number> S<pin state>- Wait until the specified pin reaches the state required
  155. // M240 - Trigger a camera to take a photograph
  156. // M250 - Set LCD contrast C<contrast value> (value 0..63)
  157. // M280 - set servo position absolute. P: servo index, S: angle or microseconds
  158. // M300 - Play beep sound S<frequency Hz> P<duration ms>
  159. // M301 - Set PID parameters P I and D
  160. // M302 - Allow cold extrudes, or set the minimum extrude S<temperature>.
  161. // M303 - PID relay autotune S<temperature> sets the target temperature. (default target temperature = 150C)
  162. // M304 - Set bed PID parameters P I and D
  163. // M400 - Finish all moves
  164. // M401 - Lower z-probe if present
  165. // M402 - Raise z-probe if present
  166. // M404 - N<dia in mm> Enter the nominal filament width (3mm, 1.75mm ) or will display nominal filament width without parameters
  167. // M405 - Turn on Filament Sensor extrusion control. Optional D<delay in cm> to set delay in centimeters between sensor and extruder
  168. // M406 - Turn off Filament Sensor extrusion control
  169. // M407 - Displays measured filament diameter
  170. // M500 - stores parameters in EEPROM
  171. // M501 - reads parameters from EEPROM (if you need reset them after you changed them temporarily).
  172. // M502 - reverts to the default "factory settings". You still need to store them in EEPROM afterwards if you want to.
  173. // M503 - print the current settings (from memory not from EEPROM)
  174. // M509 - force language selection on next restart
  175. // M540 - Use S[0|1] to enable or disable the stop SD card print on endstop hit (requires ABORT_ON_ENDSTOP_HIT_FEATURE_ENABLED)
  176. // M600 - Pause for filament change X[pos] Y[pos] Z[relative lift] E[initial retract] L[later retract distance for removal]
  177. // M605 - Set dual x-carriage movement mode: S<mode> [ X<duplication x-offset> R<duplication temp offset> ]
  178. // M907 - Set digital trimpot motor current using axis codes.
  179. // M908 - Control digital trimpot directly.
  180. // M350 - Set microstepping mode.
  181. // M351 - Toggle MS1 MS2 pins directly.
  182. // M928 - Start SD logging (M928 filename.g) - ended by M29
  183. // M999 - Restart after being stopped by error
  184. //Stepper Movement Variables
  185. //===========================================================================
  186. //=============================imported variables============================
  187. //===========================================================================
  188. //===========================================================================
  189. //=============================public variables=============================
  190. //===========================================================================
  191. #ifdef SDSUPPORT
  192. CardReader card;
  193. #endif
  194. unsigned long TimeSent = millis();
  195. unsigned long TimeNow = millis();
  196. union Data
  197. {
  198. byte b[2];
  199. int value;
  200. };
  201. float homing_feedrate[] = HOMING_FEEDRATE;
  202. // Currently only the extruder axis may be switched to a relative mode.
  203. // Other axes are always absolute or relative based on the common relative_mode flag.
  204. bool axis_relative_modes[] = AXIS_RELATIVE_MODES;
  205. int feedmultiply=100; //100->1 200->2
  206. int saved_feedmultiply;
  207. int extrudemultiply=100; //100->1 200->2
  208. int extruder_multiply[EXTRUDERS] = {100
  209. #if EXTRUDERS > 1
  210. , 100
  211. #if EXTRUDERS > 2
  212. , 100
  213. #endif
  214. #endif
  215. };
  216. bool is_usb_printing = false;
  217. bool homing_flag = false;
  218. unsigned long kicktime = millis()+100000;
  219. unsigned int usb_printing_counter;
  220. int lcd_change_fil_state = 0;
  221. int feedmultiplyBckp = 100;
  222. unsigned char lang_selected = 0;
  223. bool prusa_sd_card_upload = false;
  224. unsigned int status_number = 0;
  225. unsigned long total_filament_used;
  226. unsigned int heating_status;
  227. unsigned int heating_status_counter;
  228. bool custom_message;
  229. bool loading_flag = false;
  230. unsigned int custom_message_type;
  231. unsigned int custom_message_state;
  232. bool volumetric_enabled = false;
  233. float filament_size[EXTRUDERS] = { DEFAULT_NOMINAL_FILAMENT_DIA
  234. #if EXTRUDERS > 1
  235. , DEFAULT_NOMINAL_FILAMENT_DIA
  236. #if EXTRUDERS > 2
  237. , DEFAULT_NOMINAL_FILAMENT_DIA
  238. #endif
  239. #endif
  240. };
  241. float volumetric_multiplier[EXTRUDERS] = {1.0
  242. #if EXTRUDERS > 1
  243. , 1.0
  244. #if EXTRUDERS > 2
  245. , 1.0
  246. #endif
  247. #endif
  248. };
  249. float current_position[NUM_AXIS] = { 0.0, 0.0, 0.0, 0.0 };
  250. float add_homing[3]={0,0,0};
  251. float min_pos[3] = { X_MIN_POS, Y_MIN_POS, Z_MIN_POS };
  252. float max_pos[3] = { X_MAX_POS, Y_MAX_POS, Z_MAX_POS };
  253. bool axis_known_position[3] = {false, false, false};
  254. float zprobe_zoffset;
  255. // Extruder offset
  256. #if EXTRUDERS > 1
  257. #define NUM_EXTRUDER_OFFSETS 2 // only in XY plane
  258. float extruder_offset[NUM_EXTRUDER_OFFSETS][EXTRUDERS] = {
  259. #if defined(EXTRUDER_OFFSET_X) && defined(EXTRUDER_OFFSET_Y)
  260. EXTRUDER_OFFSET_X, EXTRUDER_OFFSET_Y
  261. #endif
  262. };
  263. #endif
  264. uint8_t active_extruder = 0;
  265. int fanSpeed=0;
  266. #ifdef FWRETRACT
  267. bool autoretract_enabled=false;
  268. bool retracted[EXTRUDERS]={false
  269. #if EXTRUDERS > 1
  270. , false
  271. #if EXTRUDERS > 2
  272. , false
  273. #endif
  274. #endif
  275. };
  276. bool retracted_swap[EXTRUDERS]={false
  277. #if EXTRUDERS > 1
  278. , false
  279. #if EXTRUDERS > 2
  280. , false
  281. #endif
  282. #endif
  283. };
  284. float retract_length = RETRACT_LENGTH;
  285. float retract_length_swap = RETRACT_LENGTH_SWAP;
  286. float retract_feedrate = RETRACT_FEEDRATE;
  287. float retract_zlift = RETRACT_ZLIFT;
  288. float retract_recover_length = RETRACT_RECOVER_LENGTH;
  289. float retract_recover_length_swap = RETRACT_RECOVER_LENGTH_SWAP;
  290. float retract_recover_feedrate = RETRACT_RECOVER_FEEDRATE;
  291. #endif
  292. #ifdef ULTIPANEL
  293. #ifdef PS_DEFAULT_OFF
  294. bool powersupply = false;
  295. #else
  296. bool powersupply = true;
  297. #endif
  298. #endif
  299. bool cancel_heatup = false ;
  300. #ifdef FILAMENT_SENSOR
  301. //Variables for Filament Sensor input
  302. float filament_width_nominal=DEFAULT_NOMINAL_FILAMENT_DIA; //Set nominal filament width, can be changed with M404
  303. bool filament_sensor=false; //M405 turns on filament_sensor control, M406 turns it off
  304. float filament_width_meas=DEFAULT_MEASURED_FILAMENT_DIA; //Stores the measured filament diameter
  305. signed char measurement_delay[MAX_MEASUREMENT_DELAY+1]; //ring buffer to delay measurement store extruder factor after subtracting 100
  306. int delay_index1=0; //index into ring buffer
  307. int delay_index2=-1; //index into ring buffer - set to -1 on startup to indicate ring buffer needs to be initialized
  308. float delay_dist=0; //delay distance counter
  309. int meas_delay_cm = MEASUREMENT_DELAY_CM; //distance delay setting
  310. #endif
  311. const char errormagic[] PROGMEM = "Error:";
  312. const char echomagic[] PROGMEM = "echo:";
  313. //===========================================================================
  314. //=============================Private Variables=============================
  315. //===========================================================================
  316. const char axis_codes[NUM_AXIS] = {'X', 'Y', 'Z', 'E'};
  317. float destination[NUM_AXIS] = { 0.0, 0.0, 0.0, 0.0};
  318. static float delta[3] = {0.0, 0.0, 0.0};
  319. // For tracing an arc
  320. static float offset[3] = {0.0, 0.0, 0.0};
  321. static bool home_all_axis = true;
  322. static float feedrate = 1500.0, next_feedrate, saved_feedrate;
  323. static long gcode_N, gcode_LastN, Stopped_gcode_LastN = 0;
  324. // Determines Absolute or Relative Coordinates.
  325. // Also there is bool axis_relative_modes[] per axis flag.
  326. static bool relative_mode = false;
  327. // String circular buffer. Commands may be pushed to the buffer from both sides:
  328. // Chained commands will be pushed to the front, interactive (from LCD menu)
  329. // and printing commands (from serial line or from SD card) are pushed to the tail.
  330. // First character of each entry indicates the type of the entry:
  331. #define CMDBUFFER_CURRENT_TYPE_UNKNOWN 0
  332. // Command in cmdbuffer was sent over USB.
  333. #define CMDBUFFER_CURRENT_TYPE_USB 1
  334. // Command in cmdbuffer was read from SDCARD.
  335. #define CMDBUFFER_CURRENT_TYPE_SDCARD 2
  336. // Command in cmdbuffer was generated by the UI.
  337. #define CMDBUFFER_CURRENT_TYPE_UI 3
  338. // Command in cmdbuffer was generated by another G-code.
  339. #define CMDBUFFER_CURRENT_TYPE_CHAINED 4
  340. // How much space to reserve for the chained commands
  341. // of type CMDBUFFER_CURRENT_TYPE_CHAINED,
  342. // which are pushed to the front of the queue?
  343. // Maximum 5 commands of max length 20 + null terminator.
  344. #define CMDBUFFER_RESERVE_FRONT (5*21)
  345. // Reserve BUFSIZE lines of length MAX_CMD_SIZE plus CMDBUFFER_RESERVE_FRONT.
  346. static char cmdbuffer[BUFSIZE * (MAX_CMD_SIZE + 1) + CMDBUFFER_RESERVE_FRONT];
  347. // Head of the circular buffer, where to read.
  348. static int bufindr = 0;
  349. // Tail of the buffer, where to write.
  350. static int bufindw = 0;
  351. // Number of lines in cmdbuffer.
  352. static int buflen = 0;
  353. // Flag for processing the current command inside the main Arduino loop().
  354. // If a new command was pushed to the front of a command buffer while
  355. // processing another command, this replaces the command on the top.
  356. // Therefore don't remove the command from the queue in the loop() function.
  357. static bool cmdbuffer_front_already_processed = false;
  358. // Type of a command, which is to be executed right now.
  359. #define CMDBUFFER_CURRENT_TYPE (cmdbuffer[bufindr])
  360. // String of a command, which is to be executed right now.
  361. #define CMDBUFFER_CURRENT_STRING (cmdbuffer+bufindr+1)
  362. // Enable debugging of the command buffer.
  363. // Debugging information will be sent to serial line.
  364. // #define CMDBUFFER_DEBUG
  365. static int serial_count = 0;
  366. static boolean comment_mode = false;
  367. static char *strchr_pointer; // just a pointer to find chars in the command string like X, Y, Z, E, etc
  368. const int sensitive_pins[] = SENSITIVE_PINS; // Sensitive pin list for M42
  369. //static float tt = 0;
  370. //static float bt = 0;
  371. //Inactivity shutdown variables
  372. static unsigned long previous_millis_cmd = 0;
  373. unsigned long max_inactive_time = 0;
  374. static unsigned long stepper_inactive_time = DEFAULT_STEPPER_DEACTIVE_TIME*1000l;
  375. unsigned long starttime=0;
  376. unsigned long stoptime=0;
  377. unsigned long _usb_timer = 0;
  378. static uint8_t tmp_extruder;
  379. bool Stopped=false;
  380. #if NUM_SERVOS > 0
  381. Servo servos[NUM_SERVOS];
  382. #endif
  383. bool CooldownNoWait = true;
  384. bool target_direction;
  385. //Insert variables if CHDK is defined
  386. #ifdef CHDK
  387. unsigned long chdkHigh = 0;
  388. boolean chdkActive = false;
  389. #endif
  390. //===========================================================================
  391. //=============================Routines======================================
  392. //===========================================================================
  393. void get_arc_coordinates();
  394. bool setTargetedHotend(int code);
  395. void serial_echopair_P(const char *s_P, float v)
  396. { serialprintPGM(s_P); SERIAL_ECHO(v); }
  397. void serial_echopair_P(const char *s_P, double v)
  398. { serialprintPGM(s_P); SERIAL_ECHO(v); }
  399. void serial_echopair_P(const char *s_P, unsigned long v)
  400. { serialprintPGM(s_P); SERIAL_ECHO(v); }
  401. #ifdef SDSUPPORT
  402. #include "SdFatUtil.h"
  403. int freeMemory() { return SdFatUtil::FreeRam(); }
  404. #else
  405. extern "C" {
  406. extern unsigned int __bss_end;
  407. extern unsigned int __heap_start;
  408. extern void *__brkval;
  409. int freeMemory() {
  410. int free_memory;
  411. if ((int)__brkval == 0)
  412. free_memory = ((int)&free_memory) - ((int)&__bss_end);
  413. else
  414. free_memory = ((int)&free_memory) - ((int)__brkval);
  415. return free_memory;
  416. }
  417. }
  418. #endif //!SDSUPPORT
  419. // Pop the currently processed command from the queue.
  420. // It is expected, that there is at least one command in the queue.
  421. bool cmdqueue_pop_front()
  422. {
  423. if (buflen > 0) {
  424. #ifdef CMDBUFFER_DEBUG
  425. SERIAL_ECHOPGM("Dequeing ");
  426. SERIAL_ECHO(cmdbuffer+bufindr+1);
  427. SERIAL_ECHOLNPGM("");
  428. SERIAL_ECHOPGM("Old indices: buflen ");
  429. SERIAL_ECHO(buflen);
  430. SERIAL_ECHOPGM(", bufindr ");
  431. SERIAL_ECHO(bufindr);
  432. SERIAL_ECHOPGM(", bufindw ");
  433. SERIAL_ECHO(bufindw);
  434. SERIAL_ECHOPGM(", serial_count ");
  435. SERIAL_ECHO(serial_count);
  436. SERIAL_ECHOPGM(", bufsize ");
  437. SERIAL_ECHO(sizeof(cmdbuffer));
  438. SERIAL_ECHOLNPGM("");
  439. #endif /* CMDBUFFER_DEBUG */
  440. if (-- buflen == 0) {
  441. // Empty buffer.
  442. if (serial_count == 0)
  443. // No serial communication is pending. Reset both pointers to zero.
  444. bufindw = 0;
  445. bufindr = bufindw;
  446. } else {
  447. // There is at least one ready line in the buffer.
  448. // First skip the current command ID and iterate up to the end of the string.
  449. for (++ bufindr; cmdbuffer[bufindr] != 0; ++ bufindr) ;
  450. // Second, skip the end of string null character and iterate until a nonzero command ID is found.
  451. for (++ bufindr; bufindr < sizeof(cmdbuffer) && cmdbuffer[bufindr] == 0; ++ bufindr) ;
  452. // If the end of the buffer was empty,
  453. if (bufindr == sizeof(cmdbuffer)) {
  454. // skip to the start and find the nonzero command.
  455. for (bufindr = 0; cmdbuffer[bufindr] == 0; ++ bufindr) ;
  456. }
  457. #ifdef CMDBUFFER_DEBUG
  458. SERIAL_ECHOPGM("New indices: buflen ");
  459. SERIAL_ECHO(buflen);
  460. SERIAL_ECHOPGM(", bufindr ");
  461. SERIAL_ECHO(bufindr);
  462. SERIAL_ECHOPGM(", bufindw ");
  463. SERIAL_ECHO(bufindw);
  464. SERIAL_ECHOPGM(", serial_count ");
  465. SERIAL_ECHO(serial_count);
  466. SERIAL_ECHOPGM(" new command on the top: ");
  467. SERIAL_ECHO(cmdbuffer+bufindr+1);
  468. SERIAL_ECHOLNPGM("");
  469. #endif /* CMDBUFFER_DEBUG */
  470. }
  471. return true;
  472. }
  473. return false;
  474. }
  475. void cmdqueue_reset()
  476. {
  477. while (cmdqueue_pop_front()) ;
  478. }
  479. // How long a string could be pushed to the front of the command queue?
  480. // If yes, adjust bufindr to the new position, where the new command could be enqued.
  481. // len_asked does not contain the zero terminator size.
  482. bool cmdqueue_could_enqueue_front(int len_asked)
  483. {
  484. // MAX_CMD_SIZE has to accommodate the zero terminator.
  485. if (len_asked >= MAX_CMD_SIZE)
  486. return false;
  487. // Remove the currently processed command from the queue.
  488. if (! cmdbuffer_front_already_processed) {
  489. cmdqueue_pop_front();
  490. cmdbuffer_front_already_processed = true;
  491. }
  492. if (bufindr == bufindw && buflen > 0)
  493. // Full buffer.
  494. return false;
  495. // Adjust the end of the write buffer based on whether a partial line is in the receive buffer.
  496. int endw = (serial_count > 0) ? (bufindw + MAX_CMD_SIZE + 1) : bufindw;
  497. if (bufindw < bufindr) {
  498. int bufindr_new = bufindr - len_asked - 2;
  499. // Simple case. There is a contiguous space between the write buffer and the read buffer.
  500. if (endw <= bufindr_new) {
  501. bufindr = bufindr_new;
  502. return true;
  503. }
  504. } else {
  505. // Otherwise the free space is split between the start and end.
  506. if (len_asked + 2 <= bufindr) {
  507. // Could fit at the start.
  508. bufindr -= len_asked + 2;
  509. return true;
  510. }
  511. int bufindr_new = sizeof(cmdbuffer) - len_asked - 2;
  512. if (endw <= bufindr_new) {
  513. memset(cmdbuffer, 0, bufindr);
  514. bufindr = bufindr_new;
  515. return true;
  516. }
  517. }
  518. return false;
  519. }
  520. // Could one enqueue a command of lenthg len_asked into the buffer,
  521. // while leaving CMDBUFFER_RESERVE_FRONT at the start?
  522. // If yes, adjust bufindw to the new position, where the new command could be enqued.
  523. // len_asked does not contain the zero terminator size.
  524. bool cmdqueue_could_enqueue_back(int len_asked)
  525. {
  526. // MAX_CMD_SIZE has to accommodate the zero terminator.
  527. if (len_asked >= MAX_CMD_SIZE)
  528. return false;
  529. if (bufindr == bufindw && buflen > 0)
  530. // Full buffer.
  531. return false;
  532. if (serial_count > 0) {
  533. // If there is some data stored starting at bufindw, len_asked is certainly smaller than
  534. // the allocated data buffer. Try to reserve a new buffer and to move the already received
  535. // serial data.
  536. // How much memory to reserve for the commands pushed to the front?
  537. // End of the queue, when pushing to the end.
  538. int endw = bufindw + len_asked + 2;
  539. if (bufindw < bufindr)
  540. // Simple case. There is a contiguous space between the write buffer and the read buffer.
  541. return endw + CMDBUFFER_RESERVE_FRONT <= bufindr;
  542. // Otherwise the free space is split between the start and end.
  543. if (// Could one fit to the end, including the reserve?
  544. endw + CMDBUFFER_RESERVE_FRONT <= sizeof(cmdbuffer) ||
  545. // Could one fit to the end, and the reserve to the start?
  546. (endw <= sizeof(cmdbuffer) && CMDBUFFER_RESERVE_FRONT <= bufindr))
  547. return true;
  548. // Could one fit both to the start?
  549. if (len_asked + 2 + CMDBUFFER_RESERVE_FRONT <= bufindr) {
  550. // Mark the rest of the buffer as used.
  551. memset(cmdbuffer+bufindw, 0, sizeof(cmdbuffer)-bufindw);
  552. // and point to the start.
  553. bufindw = 0;
  554. return true;
  555. }
  556. } else {
  557. // How much memory to reserve for the commands pushed to the front?
  558. // End of the queue, when pushing to the end.
  559. int endw = bufindw + len_asked + 2;
  560. if (bufindw < bufindr)
  561. // Simple case. There is a contiguous space between the write buffer and the read buffer.
  562. return endw + CMDBUFFER_RESERVE_FRONT <= bufindr;
  563. // Otherwise the free space is split between the start and end.
  564. if (// Could one fit to the end, including the reserve?
  565. endw + CMDBUFFER_RESERVE_FRONT <= sizeof(cmdbuffer) ||
  566. // Could one fit to the end, and the reserve to the start?
  567. (endw <= sizeof(cmdbuffer) && CMDBUFFER_RESERVE_FRONT <= bufindr))
  568. return true;
  569. // Could one fit both to the start?
  570. if (len_asked + 2 + CMDBUFFER_RESERVE_FRONT <= bufindr) {
  571. // Mark the rest of the buffer as used.
  572. memset(cmdbuffer+bufindw, 0, sizeof(cmdbuffer)-bufindw);
  573. // and point to the start.
  574. bufindw = 0;
  575. return true;
  576. }
  577. }
  578. return false;
  579. }
  580. #ifdef CMDBUFFER_DEBUG
  581. static void cmdqueue_dump_to_serial_single_line(int nr, const char *p)
  582. {
  583. SERIAL_ECHOPGM("Entry nr: ");
  584. SERIAL_ECHO(nr);
  585. SERIAL_ECHOPGM(", type: ");
  586. SERIAL_ECHO(int(*p));
  587. SERIAL_ECHOPGM(", cmd: ");
  588. SERIAL_ECHO(p+1);
  589. SERIAL_ECHOLNPGM("");
  590. }
  591. static void cmdqueue_dump_to_serial()
  592. {
  593. if (buflen == 0) {
  594. SERIAL_ECHOLNPGM("The command buffer is empty.");
  595. } else {
  596. SERIAL_ECHOPGM("Content of the buffer: entries ");
  597. SERIAL_ECHO(buflen);
  598. SERIAL_ECHOPGM(", indr ");
  599. SERIAL_ECHO(bufindr);
  600. SERIAL_ECHOPGM(", indw ");
  601. SERIAL_ECHO(bufindw);
  602. SERIAL_ECHOLNPGM("");
  603. int nr = 0;
  604. if (bufindr < bufindw) {
  605. for (const char *p = cmdbuffer + bufindr; p < cmdbuffer + bufindw; ++ nr) {
  606. cmdqueue_dump_to_serial_single_line(nr, p);
  607. // Skip the command.
  608. for (++p; *p != 0; ++ p);
  609. // Skip the gaps.
  610. for (++p; p < cmdbuffer + bufindw && *p == 0; ++ p);
  611. }
  612. } else {
  613. for (const char *p = cmdbuffer + bufindr; p < cmdbuffer + sizeof(cmdbuffer); ++ nr) {
  614. cmdqueue_dump_to_serial_single_line(nr, p);
  615. // Skip the command.
  616. for (++p; *p != 0; ++ p);
  617. // Skip the gaps.
  618. for (++p; p < cmdbuffer + sizeof(cmdbuffer) && *p == 0; ++ p);
  619. }
  620. for (const char *p = cmdbuffer; p < cmdbuffer + bufindw; ++ nr) {
  621. cmdqueue_dump_to_serial_single_line(nr, p);
  622. // Skip the command.
  623. for (++p; *p != 0; ++ p);
  624. // Skip the gaps.
  625. for (++p; p < cmdbuffer + bufindw && *p == 0; ++ p);
  626. }
  627. }
  628. SERIAL_ECHOLNPGM("End of the buffer.");
  629. }
  630. }
  631. #endif /* CMDBUFFER_DEBUG */
  632. //adds an command to the main command buffer
  633. //thats really done in a non-safe way.
  634. //needs overworking someday
  635. // Currently the maximum length of a command piped through this function is around 20 characters
  636. void enquecommand(const char *cmd, bool from_progmem)
  637. {
  638. int len = from_progmem ? strlen_P(cmd) : strlen(cmd);
  639. // Does cmd fit the queue while leaving sufficient space at the front for the chained commands?
  640. // If it fits, it may move bufindw, so it points to a contiguous buffer, which fits cmd.
  641. if (cmdqueue_could_enqueue_back(len)) {
  642. // This is dangerous if a mixing of serial and this happens
  643. // This may easily be tested: If serial_count > 0, we have a problem.
  644. cmdbuffer[bufindw] = CMDBUFFER_CURRENT_TYPE_UI;
  645. if (from_progmem)
  646. strcpy_P(cmdbuffer + bufindw + 1, cmd);
  647. else
  648. strcpy(cmdbuffer + bufindw + 1, cmd);
  649. SERIAL_ECHO_START;
  650. SERIAL_ECHORPGM(MSG_Enqueing);
  651. SERIAL_ECHO(cmdbuffer + bufindw + 1);
  652. SERIAL_ECHOLNPGM("\"");
  653. bufindw += len + 2;
  654. if (bufindw == sizeof(cmdbuffer))
  655. bufindw = 0;
  656. ++ buflen;
  657. #ifdef CMDBUFFER_DEBUG
  658. cmdqueue_dump_to_serial();
  659. #endif /* CMDBUFFER_DEBUG */
  660. } else {
  661. SERIAL_ERROR_START;
  662. SERIAL_ECHORPGM(MSG_Enqueing);
  663. if (from_progmem)
  664. SERIAL_PROTOCOLRPGM(cmd);
  665. else
  666. SERIAL_ECHO(cmd);
  667. SERIAL_ECHOLNPGM("\" failed: Buffer full!");
  668. #ifdef CMDBUFFER_DEBUG
  669. cmdqueue_dump_to_serial();
  670. #endif /* CMDBUFFER_DEBUG */
  671. }
  672. }
  673. void enquecommand_front(const char *cmd, bool from_progmem)
  674. {
  675. int len = from_progmem ? strlen_P(cmd) : strlen(cmd);
  676. // Does cmd fit the queue? This call shall move bufindr, so the command may be copied.
  677. if (cmdqueue_could_enqueue_front(len)) {
  678. cmdbuffer[bufindr] = CMDBUFFER_CURRENT_TYPE_UI;
  679. if (from_progmem)
  680. strcpy_P(cmdbuffer + bufindr + 1, cmd);
  681. else
  682. strcpy(cmdbuffer + bufindr + 1, cmd);
  683. ++ buflen;
  684. SERIAL_ECHO_START;
  685. SERIAL_ECHOPGM("Enqueing to the front: \"");
  686. SERIAL_ECHO(cmdbuffer + bufindr + 1);
  687. SERIAL_ECHOLNPGM("\"");
  688. #ifdef CMDBUFFER_DEBUG
  689. cmdqueue_dump_to_serial();
  690. #endif /* CMDBUFFER_DEBUG */
  691. } else {
  692. SERIAL_ERROR_START;
  693. SERIAL_ECHOPGM("Enqueing to the front: \"");
  694. if (from_progmem)
  695. SERIAL_PROTOCOLRPGM(cmd);
  696. else
  697. SERIAL_ECHO(cmd);
  698. SERIAL_ECHOLNPGM("\" failed: Buffer full!");
  699. #ifdef CMDBUFFER_DEBUG
  700. cmdqueue_dump_to_serial();
  701. #endif /* CMDBUFFER_DEBUG */
  702. }
  703. }
  704. // Mark the command at the top of the command queue as new.
  705. // Therefore it will not be removed from the queue.
  706. void repeatcommand_front()
  707. {
  708. cmdbuffer_front_already_processed = true;
  709. }
  710. void setup_killpin()
  711. {
  712. #if defined(KILL_PIN) && KILL_PIN > -1
  713. SET_INPUT(KILL_PIN);
  714. WRITE(KILL_PIN,HIGH);
  715. #endif
  716. }
  717. // Set home pin
  718. void setup_homepin(void)
  719. {
  720. #if defined(HOME_PIN) && HOME_PIN > -1
  721. SET_INPUT(HOME_PIN);
  722. WRITE(HOME_PIN,HIGH);
  723. #endif
  724. }
  725. void setup_photpin()
  726. {
  727. #if defined(PHOTOGRAPH_PIN) && PHOTOGRAPH_PIN > -1
  728. SET_OUTPUT(PHOTOGRAPH_PIN);
  729. WRITE(PHOTOGRAPH_PIN, LOW);
  730. #endif
  731. }
  732. void setup_powerhold()
  733. {
  734. #if defined(SUICIDE_PIN) && SUICIDE_PIN > -1
  735. SET_OUTPUT(SUICIDE_PIN);
  736. WRITE(SUICIDE_PIN, HIGH);
  737. #endif
  738. #if defined(PS_ON_PIN) && PS_ON_PIN > -1
  739. SET_OUTPUT(PS_ON_PIN);
  740. #if defined(PS_DEFAULT_OFF)
  741. WRITE(PS_ON_PIN, PS_ON_ASLEEP);
  742. #else
  743. WRITE(PS_ON_PIN, PS_ON_AWAKE);
  744. #endif
  745. #endif
  746. }
  747. void suicide()
  748. {
  749. #if defined(SUICIDE_PIN) && SUICIDE_PIN > -1
  750. SET_OUTPUT(SUICIDE_PIN);
  751. WRITE(SUICIDE_PIN, LOW);
  752. #endif
  753. }
  754. void servo_init()
  755. {
  756. #if (NUM_SERVOS >= 1) && defined(SERVO0_PIN) && (SERVO0_PIN > -1)
  757. servos[0].attach(SERVO0_PIN);
  758. #endif
  759. #if (NUM_SERVOS >= 2) && defined(SERVO1_PIN) && (SERVO1_PIN > -1)
  760. servos[1].attach(SERVO1_PIN);
  761. #endif
  762. #if (NUM_SERVOS >= 3) && defined(SERVO2_PIN) && (SERVO2_PIN > -1)
  763. servos[2].attach(SERVO2_PIN);
  764. #endif
  765. #if (NUM_SERVOS >= 4) && defined(SERVO3_PIN) && (SERVO3_PIN > -1)
  766. servos[3].attach(SERVO3_PIN);
  767. #endif
  768. #if (NUM_SERVOS >= 5)
  769. #error "TODO: enter initalisation code for more servos"
  770. #endif
  771. }
  772. static void lcd_language_menu();
  773. #ifdef MESH_BED_LEVELING
  774. enum MeshLevelingState { MeshReport, MeshStart, MeshNext, MeshSet };
  775. #endif
  776. // Factory reset function
  777. // This function is used to erase parts or whole EEPROM memory which is used for storing calibration and and so on.
  778. // Level input parameter sets depth of reset
  779. // Quiet parameter masks all waitings for user interact.
  780. int er_progress = 0;
  781. void factory_reset(char level, bool quiet)
  782. {
  783. lcd_implementation_clear();
  784. switch (level) {
  785. // Level 0: Language reset
  786. case 0:
  787. WRITE(BEEPER, HIGH);
  788. _delay_ms(100);
  789. WRITE(BEEPER, LOW);
  790. lcd_force_language_selection();
  791. break;
  792. //Level 1: Reset statistics
  793. case 1:
  794. WRITE(BEEPER, HIGH);
  795. _delay_ms(100);
  796. WRITE(BEEPER, LOW);
  797. eeprom_update_dword((uint32_t *)EEPROM_TOTALTIME, 0);
  798. eeprom_update_dword((uint32_t *)EEPROM_FILAMENTUSED, 0);
  799. lcd_menu_statistics();
  800. break;
  801. // Level 2: Prepare for shipping
  802. case 2:
  803. //lcd_printPGM(PSTR("Factory RESET"));
  804. //lcd_print_at_PGM(1,2,PSTR("Shipping prep"));
  805. // Force language selection at the next boot up.
  806. lcd_force_language_selection();
  807. // Force the "Follow calibration flow" message at the next boot up.
  808. calibration_status_store(CALIBRATION_STATUS_Z_CALIBRATION);
  809. farm_no = 0;
  810. EEPROM_save_B(EEPROM_FARM_MODE, &farm_no);
  811. farm_mode = false;
  812. WRITE(BEEPER, HIGH);
  813. _delay_ms(100);
  814. WRITE(BEEPER, LOW);
  815. //_delay_ms(2000);
  816. break;
  817. // Level 3: erase everything, whole EEPROM will be set to 0xFF
  818. case 3:
  819. lcd_printPGM(PSTR("Factory RESET"));
  820. lcd_print_at_PGM(1, 2, PSTR("ERASING all data"));
  821. WRITE(BEEPER, HIGH);
  822. _delay_ms(100);
  823. WRITE(BEEPER, LOW);
  824. er_progress = 0;
  825. lcd_print_at_PGM(3, 3, PSTR(" "));
  826. lcd_implementation_print_at(3, 3, er_progress);
  827. // Erase EEPROM
  828. for (int i = 0; i < 4096; i++) {
  829. eeprom_write_byte((uint8_t*)i, 0xFF);
  830. if (i % 41 == 0) {
  831. er_progress++;
  832. lcd_print_at_PGM(3, 3, PSTR(" "));
  833. lcd_implementation_print_at(3, 3, er_progress);
  834. lcd_printPGM(PSTR("%"));
  835. }
  836. }
  837. break;
  838. default:
  839. break;
  840. }
  841. }
  842. // "Setup" function is called by the Arduino framework on startup.
  843. // Before startup, the Timers-functions (PWM)/Analog RW and HardwareSerial provided by the Arduino-code
  844. // are initialized by the main() routine provided by the Arduino framework.
  845. void setup()
  846. {
  847. setup_killpin();
  848. setup_powerhold();
  849. MYSERIAL.begin(BAUDRATE);
  850. SERIAL_PROTOCOLLNPGM("start");
  851. SERIAL_ECHO_START;
  852. #if 0
  853. SERIAL_ECHOLN("Reading eeprom from 0 to 100: start");
  854. for (int i = 0; i < 4096; ++ i) {
  855. int b = eeprom_read_byte((unsigned char*)i);
  856. if (b != 255) {
  857. SERIAL_ECHO(i);
  858. SERIAL_ECHO(":");
  859. SERIAL_ECHO(b);
  860. SERIAL_ECHOLN("");
  861. }
  862. }
  863. SERIAL_ECHOLN("Reading eeprom from 0 to 100: done");
  864. #endif
  865. // Check startup - does nothing if bootloader sets MCUSR to 0
  866. byte mcu = MCUSR;
  867. if(mcu & 1) SERIAL_ECHOLNRPGM(MSG_POWERUP);
  868. if(mcu & 2) SERIAL_ECHOLNRPGM(MSG_EXTERNAL_RESET);
  869. if(mcu & 4) SERIAL_ECHOLNRPGM(MSG_BROWNOUT_RESET);
  870. if(mcu & 8) SERIAL_ECHOLNRPGM(MSG_WATCHDOG_RESET);
  871. if(mcu & 32) SERIAL_ECHOLNRPGM(MSG_SOFTWARE_RESET);
  872. MCUSR=0;
  873. //SERIAL_ECHORPGM(MSG_MARLIN);
  874. //SERIAL_ECHOLNRPGM(VERSION_STRING);
  875. #ifdef STRING_VERSION_CONFIG_H
  876. #ifdef STRING_CONFIG_H_AUTHOR
  877. SERIAL_ECHO_START;
  878. SERIAL_ECHORPGM(MSG_CONFIGURATION_VER);
  879. SERIAL_ECHOPGM(STRING_VERSION_CONFIG_H);
  880. SERIAL_ECHORPGM(MSG_AUTHOR);
  881. SERIAL_ECHOLNPGM(STRING_CONFIG_H_AUTHOR);
  882. SERIAL_ECHOPGM("Compiled: ");
  883. SERIAL_ECHOLNPGM(__DATE__);
  884. #endif
  885. #endif
  886. SERIAL_ECHO_START;
  887. SERIAL_ECHORPGM(MSG_FREE_MEMORY);
  888. SERIAL_ECHO(freeMemory());
  889. SERIAL_ECHORPGM(MSG_PLANNER_BUFFER_BYTES);
  890. SERIAL_ECHOLN((int)sizeof(block_t)*BLOCK_BUFFER_SIZE);
  891. // loads data from EEPROM if available else uses defaults (and resets step acceleration rate)
  892. Config_RetrieveSettings();
  893. SdFatUtil::set_stack_guard(); //writes magic number at the end of static variables to protect against overwriting static memory by stack
  894. tp_init(); // Initialize temperature loop
  895. plan_init(); // Initialize planner;
  896. watchdog_init();
  897. st_init(); // Initialize stepper, this enables interrupts!
  898. setup_photpin();
  899. servo_init();
  900. // Reset the machine correction matrix.
  901. // It does not make sense to load the correction matrix until the machine is homed.
  902. world2machine_reset();
  903. lcd_init();
  904. if (!READ(BTN_ENC))
  905. {
  906. _delay_ms(1000);
  907. if (!READ(BTN_ENC))
  908. {
  909. lcd_implementation_clear();
  910. lcd_printPGM(PSTR("Factory RESET"));
  911. SET_OUTPUT(BEEPER);
  912. WRITE(BEEPER, HIGH);
  913. while (!READ(BTN_ENC));
  914. WRITE(BEEPER, LOW);
  915. _delay_ms(2000);
  916. char level = reset_menu();
  917. factory_reset(level, false);
  918. switch (level) {
  919. case 0: _delay_ms(0); break;
  920. case 1: _delay_ms(0); break;
  921. case 2: _delay_ms(0); break;
  922. case 3: _delay_ms(0); break;
  923. }
  924. // _delay_ms(100);
  925. /*
  926. #ifdef MESH_BED_LEVELING
  927. _delay_ms(2000);
  928. if (!READ(BTN_ENC))
  929. {
  930. WRITE(BEEPER, HIGH);
  931. _delay_ms(100);
  932. WRITE(BEEPER, LOW);
  933. _delay_ms(200);
  934. WRITE(BEEPER, HIGH);
  935. _delay_ms(100);
  936. WRITE(BEEPER, LOW);
  937. int _z = 0;
  938. calibration_status_store(CALIBRATION_STATUS_CALIBRATED);
  939. EEPROM_save_B(EEPROM_BABYSTEP_X, &_z);
  940. EEPROM_save_B(EEPROM_BABYSTEP_Y, &_z);
  941. EEPROM_save_B(EEPROM_BABYSTEP_Z, &_z);
  942. }
  943. else
  944. {
  945. WRITE(BEEPER, HIGH);
  946. _delay_ms(100);
  947. WRITE(BEEPER, LOW);
  948. }
  949. #endif // mesh */
  950. }
  951. }
  952. else
  953. {
  954. _delay_ms(1000); // wait 1sec to display the splash screen
  955. }
  956. #if defined(CONTROLLERFAN_PIN) && CONTROLLERFAN_PIN > -1
  957. SET_OUTPUT(CONTROLLERFAN_PIN); //Set pin used for driver cooling fan
  958. #endif
  959. #ifdef DIGIPOT_I2C
  960. digipot_i2c_init();
  961. #endif
  962. setup_homepin();
  963. #if defined(Z_AXIS_ALWAYS_ON)
  964. enable_z();
  965. #endif
  966. EEPROM_read_B(EEPROM_FARM_MODE, &farm_no);
  967. if (farm_no > 0)
  968. {
  969. farm_mode = true;
  970. farm_no = farm_no;
  971. prusa_statistics(8);
  972. }
  973. else
  974. {
  975. farm_mode = false;
  976. farm_no = 0;
  977. }
  978. // Enable Toshiba FlashAir SD card / WiFi enahanced card.
  979. card.ToshibaFlashAir_enable(eeprom_read_byte((unsigned char*)EEPROM_TOSHIBA_FLASH_AIR_COMPATIBLITY) == 1);
  980. // Force SD card update. Otherwise the SD card update is done from loop() on card.checkautostart(false),
  981. // but this times out if a blocking dialog is shown in setup().
  982. card.initsd();
  983. if (eeprom_read_dword((uint32_t*)(EEPROM_TOP-4)) == 0x0ffffffff &&
  984. eeprom_read_dword((uint32_t*)(EEPROM_TOP-8)) == 0x0ffffffff &&
  985. eeprom_read_dword((uint32_t*)(EEPROM_TOP-12)) == 0x0ffffffff) {
  986. // Maiden startup. The firmware has been loaded and first started on a virgin RAMBo board,
  987. // where all the EEPROM entries are set to 0x0ff.
  988. // Once a firmware boots up, it forces at least a language selection, which changes
  989. // EEPROM_LANG to number lower than 0x0ff.
  990. // 1) Set a high power mode.
  991. eeprom_write_byte((uint8_t*)EEPROM_SILENT, 0);
  992. }
  993. // In the future, somewhere here would one compare the current firmware version against the firmware version stored in the EEPROM.
  994. // If they differ, an update procedure may need to be performed. At the end of this block, the current firmware version
  995. // is being written into the EEPROM, so the update procedure will be triggered only once.
  996. lang_selected = eeprom_read_byte((uint8_t*)EEPROM_LANG);
  997. if (lang_selected >= LANG_NUM){
  998. lcd_mylang();
  999. }
  1000. check_babystep(); //checking if Z babystep is in allowed range
  1001. if (calibration_status() == CALIBRATION_STATUS_ASSEMBLED ||
  1002. calibration_status() == CALIBRATION_STATUS_UNKNOWN) {
  1003. // Reset the babystepping values, so the printer will not move the Z axis up when the babystepping is enabled.
  1004. eeprom_update_word((uint16_t*)EEPROM_BABYSTEP_Z, 0);
  1005. // Show the message.
  1006. lcd_show_fullscreen_message_and_wait_P(MSG_FOLLOW_CALIBRATION_FLOW);
  1007. lcd_update_enable(true);
  1008. } else if (calibration_status() == CALIBRATION_STATUS_LIVE_ADJUST) {
  1009. // Show the message.
  1010. lcd_show_fullscreen_message_and_wait_P(MSG_BABYSTEP_Z_NOT_SET);
  1011. lcd_update_enable(true);
  1012. } else if (calibration_status() == CALIBRATION_STATUS_Z_CALIBRATION) {
  1013. // Show the message.
  1014. lcd_show_fullscreen_message_and_wait_P(MSG_FOLLOW_CALIBRATION_FLOW);
  1015. lcd_update_enable(true);
  1016. }
  1017. // Store the currently running firmware into an eeprom,
  1018. // so the next time the firmware gets updated, it will know from which version it has been updated.
  1019. update_current_firmware_version_to_eeprom();
  1020. }
  1021. void trace();
  1022. #define CHUNK_SIZE 64 // bytes
  1023. #define SAFETY_MARGIN 1
  1024. char chunk[CHUNK_SIZE+SAFETY_MARGIN];
  1025. int chunkHead = 0;
  1026. int serial_read_stream() {
  1027. setTargetHotend(0, 0);
  1028. setTargetBed(0);
  1029. lcd_implementation_clear();
  1030. lcd_printPGM(PSTR(" Upload in progress"));
  1031. // first wait for how many bytes we will receive
  1032. uint32_t bytesToReceive;
  1033. // receive the four bytes
  1034. char bytesToReceiveBuffer[4];
  1035. for (int i=0; i<4; i++) {
  1036. int data;
  1037. while ((data = MYSERIAL.read()) == -1) {};
  1038. bytesToReceiveBuffer[i] = data;
  1039. }
  1040. // make it a uint32
  1041. memcpy(&bytesToReceive, &bytesToReceiveBuffer, 4);
  1042. // we're ready, notify the sender
  1043. MYSERIAL.write('+');
  1044. // lock in the routine
  1045. uint32_t receivedBytes = 0;
  1046. while (prusa_sd_card_upload) {
  1047. int i;
  1048. for (i=0; i<CHUNK_SIZE; i++) {
  1049. int data;
  1050. // check if we're not done
  1051. if (receivedBytes == bytesToReceive) {
  1052. break;
  1053. }
  1054. // read the next byte
  1055. while ((data = MYSERIAL.read()) == -1) {};
  1056. receivedBytes++;
  1057. // save it to the chunk
  1058. chunk[i] = data;
  1059. }
  1060. // write the chunk to SD
  1061. card.write_command_no_newline(&chunk[0]);
  1062. // notify the sender we're ready for more data
  1063. MYSERIAL.write('+');
  1064. // for safety
  1065. manage_heater();
  1066. // check if we're done
  1067. if(receivedBytes == bytesToReceive) {
  1068. trace(); // beep
  1069. card.closefile();
  1070. prusa_sd_card_upload = false;
  1071. SERIAL_PROTOCOLLNRPGM(MSG_FILE_SAVED);
  1072. return 0;
  1073. }
  1074. }
  1075. }
  1076. // The loop() function is called in an endless loop by the Arduino framework from the default main() routine.
  1077. // Before loop(), the setup() function is called by the main() routine.
  1078. void loop()
  1079. {
  1080. bool stack_integrity = true;
  1081. if (usb_printing_counter > 0 && millis()-_usb_timer > 1000)
  1082. {
  1083. is_usb_printing = true;
  1084. usb_printing_counter--;
  1085. _usb_timer = millis();
  1086. }
  1087. if (usb_printing_counter == 0)
  1088. {
  1089. is_usb_printing = false;
  1090. }
  1091. if (prusa_sd_card_upload)
  1092. {
  1093. //we read byte-by byte
  1094. serial_read_stream();
  1095. } else
  1096. {
  1097. get_command();
  1098. #ifdef SDSUPPORT
  1099. card.checkautostart(false);
  1100. #endif
  1101. if(buflen)
  1102. {
  1103. #ifdef SDSUPPORT
  1104. if(card.saving)
  1105. {
  1106. // Saving a G-code file onto an SD-card is in progress.
  1107. // Saving starts with M28, saving until M29 is seen.
  1108. if(strstr_P(CMDBUFFER_CURRENT_STRING, PSTR("M29")) == NULL) {
  1109. card.write_command(CMDBUFFER_CURRENT_STRING);
  1110. if(card.logging)
  1111. process_commands();
  1112. else
  1113. SERIAL_PROTOCOLLNRPGM(MSG_OK);
  1114. } else {
  1115. card.closefile();
  1116. SERIAL_PROTOCOLLNRPGM(MSG_FILE_SAVED);
  1117. }
  1118. } else {
  1119. process_commands();
  1120. }
  1121. #else
  1122. process_commands();
  1123. #endif //SDSUPPORT
  1124. if (! cmdbuffer_front_already_processed)
  1125. cmdqueue_pop_front();
  1126. cmdbuffer_front_already_processed = false;
  1127. }
  1128. }
  1129. //check heater every n milliseconds
  1130. manage_heater();
  1131. manage_inactivity();
  1132. checkHitEndstops();
  1133. lcd_update();
  1134. }
  1135. void get_command()
  1136. {
  1137. // Test and reserve space for the new command string.
  1138. if (! cmdqueue_could_enqueue_back(MAX_CMD_SIZE-1))
  1139. return;
  1140. while (MYSERIAL.available() > 0) {
  1141. char serial_char = MYSERIAL.read();
  1142. TimeSent = millis();
  1143. TimeNow = millis();
  1144. if (serial_char < 0)
  1145. // Ignore extended ASCII characters. These characters have no meaning in the G-code apart from the file names
  1146. // and Marlin does not support such file names anyway.
  1147. // Serial characters with a highest bit set to 1 are generated when the USB cable is unplugged, leading
  1148. // to a hang-up of the print process from an SD card.
  1149. continue;
  1150. if(serial_char == '\n' ||
  1151. serial_char == '\r' ||
  1152. (serial_char == ':' && comment_mode == false) ||
  1153. serial_count >= (MAX_CMD_SIZE - 1) )
  1154. {
  1155. if(!serial_count) { //if empty line
  1156. comment_mode = false; //for new command
  1157. return;
  1158. }
  1159. cmdbuffer[bufindw+serial_count+1] = 0; //terminate string
  1160. if(!comment_mode){
  1161. comment_mode = false; //for new command
  1162. if ((strchr_pointer = strstr(cmdbuffer+bufindw+1, "PRUSA")) == NULL && (strchr_pointer = strchr(cmdbuffer+bufindw+1, 'N')) != NULL) {
  1163. if ((strchr_pointer = strchr(cmdbuffer+bufindw+1, 'N')) != NULL)
  1164. {
  1165. // Line number met. When sending a G-code over a serial line, each line may be stamped with its index,
  1166. // and Marlin tests, whether the successive lines are stamped with an increasing line number ID.
  1167. gcode_N = (strtol(strchr_pointer+1, NULL, 10));
  1168. if(gcode_N != gcode_LastN+1 && (strstr_P(cmdbuffer+bufindw+1, PSTR("M110")) == NULL) ) {
  1169. // M110 - set current line number.
  1170. // Line numbers not sent in succession.
  1171. SERIAL_ERROR_START;
  1172. SERIAL_ERRORRPGM(MSG_ERR_LINE_NO);
  1173. SERIAL_ERRORLN(gcode_LastN);
  1174. //Serial.println(gcode_N);
  1175. FlushSerialRequestResend();
  1176. serial_count = 0;
  1177. return;
  1178. }
  1179. if((strchr_pointer = strchr(cmdbuffer+bufindw+1, '*')) != NULL)
  1180. {
  1181. byte checksum = 0;
  1182. char *p = cmdbuffer+bufindw+1;
  1183. while (p != strchr_pointer)
  1184. checksum = checksum^(*p++);
  1185. if (int(strtol(strchr_pointer+1, NULL, 10)) != int(checksum)) {
  1186. SERIAL_ERROR_START;
  1187. SERIAL_ERRORRPGM(MSG_ERR_CHECKSUM_MISMATCH);
  1188. SERIAL_ERRORLN(gcode_LastN);
  1189. FlushSerialRequestResend();
  1190. serial_count = 0;
  1191. return;
  1192. }
  1193. // If no errors, remove the checksum and continue parsing.
  1194. *strchr_pointer = 0;
  1195. }
  1196. else
  1197. {
  1198. SERIAL_ERROR_START;
  1199. SERIAL_ERRORRPGM(MSG_ERR_NO_CHECKSUM);
  1200. SERIAL_ERRORLN(gcode_LastN);
  1201. FlushSerialRequestResend();
  1202. serial_count = 0;
  1203. return;
  1204. }
  1205. gcode_LastN = gcode_N;
  1206. //if no errors, continue parsing
  1207. } // end of 'N' command
  1208. }
  1209. else // if we don't receive 'N' but still see '*'
  1210. {
  1211. if((strchr(cmdbuffer+bufindw+1, '*') != NULL))
  1212. {
  1213. SERIAL_ERROR_START;
  1214. SERIAL_ERRORRPGM(MSG_ERR_NO_LINENUMBER_WITH_CHECKSUM);
  1215. SERIAL_ERRORLN(gcode_LastN);
  1216. serial_count = 0;
  1217. return;
  1218. }
  1219. } // end of '*' command
  1220. if ((strchr_pointer = strchr(cmdbuffer+bufindw+1, 'G')) != NULL) {
  1221. if (! IS_SD_PRINTING) {
  1222. usb_printing_counter = 10;
  1223. is_usb_printing = true;
  1224. }
  1225. if (Stopped == true) {
  1226. int gcode = strtol(strchr_pointer+1, NULL, 10);
  1227. if (gcode >= 0 && gcode <= 3) {
  1228. SERIAL_ERRORLNRPGM(MSG_ERR_STOPPED);
  1229. LCD_MESSAGERPGM(MSG_STOPPED);
  1230. }
  1231. }
  1232. } // end of 'G' command
  1233. //If command was e-stop process now
  1234. if(strcmp(cmdbuffer+bufindw+1, "M112") == 0)
  1235. kill();
  1236. // Store the current line into buffer, move to the next line.
  1237. cmdbuffer[bufindw] = CMDBUFFER_CURRENT_TYPE_USB;
  1238. #ifdef CMDBUFFER_DEBUG
  1239. SERIAL_ECHO_START;
  1240. SERIAL_ECHOPGM("Storing a command line to buffer: ");
  1241. SERIAL_ECHO(cmdbuffer+bufindw+1);
  1242. SERIAL_ECHOLNPGM("");
  1243. #endif /* CMDBUFFER_DEBUG */
  1244. bufindw += strlen(cmdbuffer+bufindw+1) + 2;
  1245. if (bufindw == sizeof(cmdbuffer))
  1246. bufindw = 0;
  1247. ++ buflen;
  1248. #ifdef CMDBUFFER_DEBUG
  1249. SERIAL_ECHOPGM("Number of commands in the buffer: ");
  1250. SERIAL_ECHO(buflen);
  1251. SERIAL_ECHOLNPGM("");
  1252. #endif /* CMDBUFFER_DEBUG */
  1253. } // end of 'not comment mode'
  1254. serial_count = 0; //clear buffer
  1255. // Don't call cmdqueue_could_enqueue_back if there are no characters waiting
  1256. // in the queue, as this function will reserve the memory.
  1257. if (MYSERIAL.available() == 0 || ! cmdqueue_could_enqueue_back(MAX_CMD_SIZE-1))
  1258. return;
  1259. } // end of "end of line" processing
  1260. else {
  1261. // Not an "end of line" symbol. Store the new character into a buffer.
  1262. if(serial_char == ';') comment_mode = true;
  1263. if(!comment_mode) cmdbuffer[bufindw+1+serial_count++] = serial_char;
  1264. }
  1265. } // end of serial line processing loop
  1266. if(farm_mode){
  1267. TimeNow = millis();
  1268. if ( ((TimeNow - TimeSent) > 800) && (serial_count > 0) ) {
  1269. cmdbuffer[bufindw+serial_count+1] = 0;
  1270. bufindw += strlen(cmdbuffer+bufindw+1) + 2;
  1271. if (bufindw == sizeof(cmdbuffer))
  1272. bufindw = 0;
  1273. ++ buflen;
  1274. serial_count = 0;
  1275. SERIAL_ECHOPGM("TIMEOUT:");
  1276. //memset(cmdbuffer, 0 , sizeof(cmdbuffer));
  1277. return;
  1278. }
  1279. }
  1280. #ifdef SDSUPPORT
  1281. if(!card.sdprinting || serial_count!=0){
  1282. // If there is a half filled buffer from serial line, wait until return before
  1283. // continuing with the serial line.
  1284. return;
  1285. }
  1286. //'#' stops reading from SD to the buffer prematurely, so procedural macro calls are possible
  1287. // if it occurs, stop_buffering is triggered and the buffer is ran dry.
  1288. // this character _can_ occur in serial com, due to checksums. however, no checksums are used in SD printing
  1289. static bool stop_buffering=false;
  1290. if(buflen==0) stop_buffering=false;
  1291. // Reads whole lines from the SD card. Never leaves a half-filled line in the cmdbuffer.
  1292. while( !card.eof() && !stop_buffering) {
  1293. int16_t n=card.get();
  1294. char serial_char = (char)n;
  1295. if(serial_char == '\n' ||
  1296. serial_char == '\r' ||
  1297. (serial_char == '#' && comment_mode == false) ||
  1298. (serial_char == ':' && comment_mode == false) ||
  1299. serial_count >= (MAX_CMD_SIZE - 1)||n==-1)
  1300. {
  1301. if(card.eof()){
  1302. SERIAL_PROTOCOLLNRPGM(MSG_FILE_PRINTED);
  1303. stoptime=millis();
  1304. char time[30];
  1305. unsigned long t=(stoptime-starttime)/1000;
  1306. int hours, minutes;
  1307. minutes=(t/60)%60;
  1308. hours=t/60/60;
  1309. save_statistics(total_filament_used, t);
  1310. sprintf_P(time, PSTR("%i hours %i minutes"),hours, minutes);
  1311. SERIAL_ECHO_START;
  1312. SERIAL_ECHOLN(time);
  1313. lcd_setstatus(time);
  1314. card.printingHasFinished();
  1315. card.checkautostart(true);
  1316. if (farm_mode)
  1317. {
  1318. prusa_statistics(6);
  1319. lcd_commands_type = LCD_COMMAND_FARM_MODE_CONFIRM;
  1320. }
  1321. }
  1322. if(serial_char=='#')
  1323. stop_buffering=true;
  1324. if(!serial_count)
  1325. {
  1326. comment_mode = false; //for new command
  1327. return; //if empty line
  1328. }
  1329. cmdbuffer[bufindw+serial_count+1] = 0; //terminate string
  1330. cmdbuffer[bufindw] = CMDBUFFER_CURRENT_TYPE_SDCARD;
  1331. ++ buflen;
  1332. bufindw += strlen(cmdbuffer+bufindw+1) + 2;
  1333. if (bufindw == sizeof(cmdbuffer))
  1334. bufindw = 0;
  1335. comment_mode = false; //for new command
  1336. serial_count = 0; //clear buffer
  1337. // The following line will reserve buffer space if available.
  1338. if (! cmdqueue_could_enqueue_back(MAX_CMD_SIZE-1))
  1339. return;
  1340. }
  1341. else
  1342. {
  1343. if(serial_char == ';') comment_mode = true;
  1344. if(!comment_mode) cmdbuffer[bufindw+1+serial_count++] = serial_char;
  1345. }
  1346. }
  1347. #endif //SDSUPPORT
  1348. }
  1349. // Return True if a character was found
  1350. static inline bool code_seen(char code) { return (strchr_pointer = strchr(CMDBUFFER_CURRENT_STRING, code)) != NULL; }
  1351. static inline bool code_seen(const char *code) { return (strchr_pointer = strstr(CMDBUFFER_CURRENT_STRING, code)) != NULL; }
  1352. static inline float code_value() { return strtod(strchr_pointer+1, NULL);}
  1353. static inline long code_value_long() { return strtol(strchr_pointer+1, NULL, 10); }
  1354. static inline int16_t code_value_short() { return int16_t(strtol(strchr_pointer+1, NULL, 10)); };
  1355. static inline uint8_t code_value_uint8() { return uint8_t(strtol(strchr_pointer+1, NULL, 10)); };
  1356. #define DEFINE_PGM_READ_ANY(type, reader) \
  1357. static inline type pgm_read_any(const type *p) \
  1358. { return pgm_read_##reader##_near(p); }
  1359. DEFINE_PGM_READ_ANY(float, float);
  1360. DEFINE_PGM_READ_ANY(signed char, byte);
  1361. #define XYZ_CONSTS_FROM_CONFIG(type, array, CONFIG) \
  1362. static const PROGMEM type array##_P[3] = \
  1363. { X_##CONFIG, Y_##CONFIG, Z_##CONFIG }; \
  1364. static inline type array(int axis) \
  1365. { return pgm_read_any(&array##_P[axis]); } \
  1366. type array##_ext(int axis) \
  1367. { return pgm_read_any(&array##_P[axis]); }
  1368. XYZ_CONSTS_FROM_CONFIG(float, base_min_pos, MIN_POS);
  1369. XYZ_CONSTS_FROM_CONFIG(float, base_max_pos, MAX_POS);
  1370. XYZ_CONSTS_FROM_CONFIG(float, base_home_pos, HOME_POS);
  1371. XYZ_CONSTS_FROM_CONFIG(float, max_length, MAX_LENGTH);
  1372. XYZ_CONSTS_FROM_CONFIG(float, home_retract_mm, HOME_RETRACT_MM);
  1373. XYZ_CONSTS_FROM_CONFIG(signed char, home_dir, HOME_DIR);
  1374. static void axis_is_at_home(int axis) {
  1375. current_position[axis] = base_home_pos(axis) + add_homing[axis];
  1376. min_pos[axis] = base_min_pos(axis) + add_homing[axis];
  1377. max_pos[axis] = base_max_pos(axis) + add_homing[axis];
  1378. }
  1379. inline void set_current_to_destination() { memcpy(current_position, destination, sizeof(current_position)); }
  1380. inline void set_destination_to_current() { memcpy(destination, current_position, sizeof(destination)); }
  1381. static void setup_for_endstop_move(bool enable_endstops_now = true) {
  1382. saved_feedrate = feedrate;
  1383. saved_feedmultiply = feedmultiply;
  1384. feedmultiply = 100;
  1385. previous_millis_cmd = millis();
  1386. enable_endstops(enable_endstops_now);
  1387. }
  1388. static void clean_up_after_endstop_move() {
  1389. #ifdef ENDSTOPS_ONLY_FOR_HOMING
  1390. enable_endstops(false);
  1391. #endif
  1392. feedrate = saved_feedrate;
  1393. feedmultiply = saved_feedmultiply;
  1394. previous_millis_cmd = millis();
  1395. }
  1396. #ifdef ENABLE_AUTO_BED_LEVELING
  1397. #ifdef AUTO_BED_LEVELING_GRID
  1398. static void set_bed_level_equation_lsq(double *plane_equation_coefficients)
  1399. {
  1400. vector_3 planeNormal = vector_3(-plane_equation_coefficients[0], -plane_equation_coefficients[1], 1);
  1401. planeNormal.debug("planeNormal");
  1402. plan_bed_level_matrix = matrix_3x3::create_look_at(planeNormal);
  1403. //bedLevel.debug("bedLevel");
  1404. //plan_bed_level_matrix.debug("bed level before");
  1405. //vector_3 uncorrected_position = plan_get_position_mm();
  1406. //uncorrected_position.debug("position before");
  1407. vector_3 corrected_position = plan_get_position();
  1408. // corrected_position.debug("position after");
  1409. current_position[X_AXIS] = corrected_position.x;
  1410. current_position[Y_AXIS] = corrected_position.y;
  1411. current_position[Z_AXIS] = corrected_position.z;
  1412. // put the bed at 0 so we don't go below it.
  1413. current_position[Z_AXIS] = zprobe_zoffset; // in the lsq we reach here after raising the extruder due to the loop structure
  1414. plan_set_position(current_position[X_AXIS], current_position[Y_AXIS], current_position[Z_AXIS], current_position[E_AXIS]);
  1415. }
  1416. #else // not AUTO_BED_LEVELING_GRID
  1417. static void set_bed_level_equation_3pts(float z_at_pt_1, float z_at_pt_2, float z_at_pt_3) {
  1418. plan_bed_level_matrix.set_to_identity();
  1419. vector_3 pt1 = vector_3(ABL_PROBE_PT_1_X, ABL_PROBE_PT_1_Y, z_at_pt_1);
  1420. vector_3 pt2 = vector_3(ABL_PROBE_PT_2_X, ABL_PROBE_PT_2_Y, z_at_pt_2);
  1421. vector_3 pt3 = vector_3(ABL_PROBE_PT_3_X, ABL_PROBE_PT_3_Y, z_at_pt_3);
  1422. vector_3 from_2_to_1 = (pt1 - pt2).get_normal();
  1423. vector_3 from_2_to_3 = (pt3 - pt2).get_normal();
  1424. vector_3 planeNormal = vector_3::cross(from_2_to_1, from_2_to_3).get_normal();
  1425. planeNormal = vector_3(planeNormal.x, planeNormal.y, abs(planeNormal.z));
  1426. plan_bed_level_matrix = matrix_3x3::create_look_at(planeNormal);
  1427. vector_3 corrected_position = plan_get_position();
  1428. current_position[X_AXIS] = corrected_position.x;
  1429. current_position[Y_AXIS] = corrected_position.y;
  1430. current_position[Z_AXIS] = corrected_position.z;
  1431. // put the bed at 0 so we don't go below it.
  1432. current_position[Z_AXIS] = zprobe_zoffset;
  1433. plan_set_position(current_position[X_AXIS], current_position[Y_AXIS], current_position[Z_AXIS], current_position[E_AXIS]);
  1434. }
  1435. #endif // AUTO_BED_LEVELING_GRID
  1436. static void run_z_probe() {
  1437. plan_bed_level_matrix.set_to_identity();
  1438. feedrate = homing_feedrate[Z_AXIS];
  1439. // move down until you find the bed
  1440. float zPosition = -10;
  1441. plan_buffer_line(current_position[X_AXIS], current_position[Y_AXIS], zPosition, current_position[E_AXIS], feedrate/60, active_extruder);
  1442. st_synchronize();
  1443. // we have to let the planner know where we are right now as it is not where we said to go.
  1444. zPosition = st_get_position_mm(Z_AXIS);
  1445. plan_set_position(current_position[X_AXIS], current_position[Y_AXIS], zPosition, current_position[E_AXIS]);
  1446. // move up the retract distance
  1447. zPosition += home_retract_mm(Z_AXIS);
  1448. plan_buffer_line(current_position[X_AXIS], current_position[Y_AXIS], zPosition, current_position[E_AXIS], feedrate/60, active_extruder);
  1449. st_synchronize();
  1450. // move back down slowly to find bed
  1451. feedrate = homing_feedrate[Z_AXIS]/4;
  1452. zPosition -= home_retract_mm(Z_AXIS) * 2;
  1453. plan_buffer_line(current_position[X_AXIS], current_position[Y_AXIS], zPosition, current_position[E_AXIS], feedrate/60, active_extruder);
  1454. st_synchronize();
  1455. current_position[Z_AXIS] = st_get_position_mm(Z_AXIS);
  1456. // make sure the planner knows where we are as it may be a bit different than we last said to move to
  1457. plan_set_position(current_position[X_AXIS], current_position[Y_AXIS], current_position[Z_AXIS], current_position[E_AXIS]);
  1458. }
  1459. static void do_blocking_move_to(float x, float y, float z) {
  1460. float oldFeedRate = feedrate;
  1461. feedrate = homing_feedrate[Z_AXIS];
  1462. current_position[Z_AXIS] = z;
  1463. plan_buffer_line(current_position[X_AXIS], current_position[Y_AXIS], current_position[Z_AXIS], current_position[E_AXIS], feedrate/60, active_extruder);
  1464. st_synchronize();
  1465. feedrate = XY_TRAVEL_SPEED;
  1466. current_position[X_AXIS] = x;
  1467. current_position[Y_AXIS] = y;
  1468. plan_buffer_line(current_position[X_AXIS], current_position[Y_AXIS], current_position[Z_AXIS], current_position[E_AXIS], feedrate/60, active_extruder);
  1469. st_synchronize();
  1470. feedrate = oldFeedRate;
  1471. }
  1472. static void do_blocking_move_relative(float offset_x, float offset_y, float offset_z) {
  1473. do_blocking_move_to(current_position[X_AXIS] + offset_x, current_position[Y_AXIS] + offset_y, current_position[Z_AXIS] + offset_z);
  1474. }
  1475. /// Probe bed height at position (x,y), returns the measured z value
  1476. static float probe_pt(float x, float y, float z_before) {
  1477. // move to right place
  1478. do_blocking_move_to(current_position[X_AXIS], current_position[Y_AXIS], z_before);
  1479. do_blocking_move_to(x - X_PROBE_OFFSET_FROM_EXTRUDER, y - Y_PROBE_OFFSET_FROM_EXTRUDER, current_position[Z_AXIS]);
  1480. run_z_probe();
  1481. float measured_z = current_position[Z_AXIS];
  1482. SERIAL_PROTOCOLRPGM(MSG_BED);
  1483. SERIAL_PROTOCOLPGM(" x: ");
  1484. SERIAL_PROTOCOL(x);
  1485. SERIAL_PROTOCOLPGM(" y: ");
  1486. SERIAL_PROTOCOL(y);
  1487. SERIAL_PROTOCOLPGM(" z: ");
  1488. SERIAL_PROTOCOL(measured_z);
  1489. SERIAL_PROTOCOLPGM("\n");
  1490. return measured_z;
  1491. }
  1492. #endif // #ifdef ENABLE_AUTO_BED_LEVELING
  1493. void homeaxis(int axis) {
  1494. #define HOMEAXIS_DO(LETTER) \
  1495. ((LETTER##_MIN_PIN > -1 && LETTER##_HOME_DIR==-1) || (LETTER##_MAX_PIN > -1 && LETTER##_HOME_DIR==1))
  1496. if (axis==X_AXIS ? HOMEAXIS_DO(X) :
  1497. axis==Y_AXIS ? HOMEAXIS_DO(Y) :
  1498. axis==Z_AXIS ? HOMEAXIS_DO(Z) :
  1499. 0) {
  1500. int axis_home_dir = home_dir(axis);
  1501. current_position[axis] = 0;
  1502. plan_set_position(current_position[X_AXIS], current_position[Y_AXIS], current_position[Z_AXIS], current_position[E_AXIS]);
  1503. destination[axis] = 1.5 * max_length(axis) * axis_home_dir;
  1504. feedrate = homing_feedrate[axis];
  1505. plan_buffer_line(destination[X_AXIS], destination[Y_AXIS], destination[Z_AXIS], destination[E_AXIS], feedrate/60, active_extruder);
  1506. st_synchronize();
  1507. current_position[axis] = 0;
  1508. plan_set_position(current_position[X_AXIS], current_position[Y_AXIS], current_position[Z_AXIS], current_position[E_AXIS]);
  1509. destination[axis] = -home_retract_mm(axis) * axis_home_dir;
  1510. plan_buffer_line(destination[X_AXIS], destination[Y_AXIS], destination[Z_AXIS], destination[E_AXIS], feedrate/60, active_extruder);
  1511. st_synchronize();
  1512. destination[axis] = 2*home_retract_mm(axis) * axis_home_dir;
  1513. feedrate = homing_feedrate[axis]/2 ;
  1514. plan_buffer_line(destination[X_AXIS], destination[Y_AXIS], destination[Z_AXIS], destination[E_AXIS], feedrate/60, active_extruder);
  1515. st_synchronize();
  1516. axis_is_at_home(axis);
  1517. destination[axis] = current_position[axis];
  1518. feedrate = 0.0;
  1519. endstops_hit_on_purpose();
  1520. axis_known_position[axis] = true;
  1521. }
  1522. }
  1523. void home_xy()
  1524. {
  1525. set_destination_to_current();
  1526. homeaxis(X_AXIS);
  1527. homeaxis(Y_AXIS);
  1528. plan_set_position(current_position[X_AXIS], current_position[Y_AXIS], current_position[Z_AXIS], current_position[E_AXIS]);
  1529. endstops_hit_on_purpose();
  1530. }
  1531. void refresh_cmd_timeout(void)
  1532. {
  1533. previous_millis_cmd = millis();
  1534. }
  1535. #ifdef FWRETRACT
  1536. void retract(bool retracting, bool swapretract = false) {
  1537. if(retracting && !retracted[active_extruder]) {
  1538. destination[X_AXIS]=current_position[X_AXIS];
  1539. destination[Y_AXIS]=current_position[Y_AXIS];
  1540. destination[Z_AXIS]=current_position[Z_AXIS];
  1541. destination[E_AXIS]=current_position[E_AXIS];
  1542. if (swapretract) {
  1543. current_position[E_AXIS]+=retract_length_swap/volumetric_multiplier[active_extruder];
  1544. } else {
  1545. current_position[E_AXIS]+=retract_length/volumetric_multiplier[active_extruder];
  1546. }
  1547. plan_set_e_position(current_position[E_AXIS]);
  1548. float oldFeedrate = feedrate;
  1549. feedrate=retract_feedrate*60;
  1550. retracted[active_extruder]=true;
  1551. prepare_move();
  1552. current_position[Z_AXIS]-=retract_zlift;
  1553. plan_set_position(current_position[X_AXIS], current_position[Y_AXIS], current_position[Z_AXIS], current_position[E_AXIS]);
  1554. prepare_move();
  1555. feedrate = oldFeedrate;
  1556. } else if(!retracting && retracted[active_extruder]) {
  1557. destination[X_AXIS]=current_position[X_AXIS];
  1558. destination[Y_AXIS]=current_position[Y_AXIS];
  1559. destination[Z_AXIS]=current_position[Z_AXIS];
  1560. destination[E_AXIS]=current_position[E_AXIS];
  1561. current_position[Z_AXIS]+=retract_zlift;
  1562. plan_set_position(current_position[X_AXIS], current_position[Y_AXIS], current_position[Z_AXIS], current_position[E_AXIS]);
  1563. //prepare_move();
  1564. if (swapretract) {
  1565. current_position[E_AXIS]-=(retract_length_swap+retract_recover_length_swap)/volumetric_multiplier[active_extruder];
  1566. } else {
  1567. current_position[E_AXIS]-=(retract_length+retract_recover_length)/volumetric_multiplier[active_extruder];
  1568. }
  1569. plan_set_e_position(current_position[E_AXIS]);
  1570. float oldFeedrate = feedrate;
  1571. feedrate=retract_recover_feedrate*60;
  1572. retracted[active_extruder]=false;
  1573. prepare_move();
  1574. feedrate = oldFeedrate;
  1575. }
  1576. } //retract
  1577. #endif //FWRETRACT
  1578. void trace() {
  1579. tone(BEEPER, 440);
  1580. delay(25);
  1581. noTone(BEEPER);
  1582. delay(20);
  1583. }
  1584. void ramming() {
  1585. // float tmp[4] = DEFAULT_MAX_FEEDRATE;
  1586. if (current_temperature[0] < 230) {
  1587. //PLA
  1588. max_feedrate[E_AXIS] = 50;
  1589. //current_position[E_AXIS] -= 8;
  1590. //plan_buffer_line(current_position[X_AXIS], current_position[Y_AXIS], current_position[Z_AXIS], current_position[E_AXIS], 2100 / 60, active_extruder);
  1591. //current_position[E_AXIS] += 8;
  1592. //plan_buffer_line(current_position[X_AXIS], current_position[Y_AXIS], current_position[Z_AXIS], current_position[E_AXIS], 2100 / 60, active_extruder);
  1593. current_position[E_AXIS] += 5.4;
  1594. plan_buffer_line(current_position[X_AXIS], current_position[Y_AXIS], current_position[Z_AXIS], current_position[E_AXIS], 2800 / 60, active_extruder);
  1595. current_position[E_AXIS] += 3.2;
  1596. plan_buffer_line(current_position[X_AXIS], current_position[Y_AXIS], current_position[Z_AXIS], current_position[E_AXIS], 3000 / 60, active_extruder);
  1597. current_position[E_AXIS] += 3;
  1598. plan_buffer_line(current_position[X_AXIS], current_position[Y_AXIS], current_position[Z_AXIS], current_position[E_AXIS], 3400 / 60, active_extruder);
  1599. st_synchronize();
  1600. max_feedrate[E_AXIS] = 80;
  1601. current_position[E_AXIS] -= 82;
  1602. plan_buffer_line(current_position[X_AXIS], current_position[Y_AXIS], current_position[Z_AXIS], current_position[E_AXIS], 9500 / 60, active_extruder);
  1603. max_feedrate[E_AXIS] = 50;//tmp[E_AXIS];
  1604. current_position[E_AXIS] -= 20;
  1605. plan_buffer_line(current_position[X_AXIS], current_position[Y_AXIS], current_position[Z_AXIS], current_position[E_AXIS], 1200 / 60, active_extruder);
  1606. current_position[E_AXIS] += 5;
  1607. plan_buffer_line(current_position[X_AXIS], current_position[Y_AXIS], current_position[Z_AXIS], current_position[E_AXIS], 400 / 60, active_extruder);
  1608. current_position[E_AXIS] += 5;
  1609. plan_buffer_line(current_position[X_AXIS], current_position[Y_AXIS], current_position[Z_AXIS], current_position[E_AXIS], 600 / 60, active_extruder);
  1610. current_position[E_AXIS] -= 10;
  1611. st_synchronize();
  1612. plan_buffer_line(current_position[X_AXIS], current_position[Y_AXIS], current_position[Z_AXIS], current_position[E_AXIS], 600 / 60, active_extruder);
  1613. current_position[E_AXIS] += 10;
  1614. plan_buffer_line(current_position[X_AXIS], current_position[Y_AXIS], current_position[Z_AXIS], current_position[E_AXIS], 600 / 60, active_extruder);
  1615. current_position[E_AXIS] -= 10;
  1616. plan_buffer_line(current_position[X_AXIS], current_position[Y_AXIS], current_position[Z_AXIS], current_position[E_AXIS], 800 / 60, active_extruder);
  1617. current_position[E_AXIS] += 10;
  1618. plan_buffer_line(current_position[X_AXIS], current_position[Y_AXIS], current_position[Z_AXIS], current_position[E_AXIS], 800 / 60, active_extruder);
  1619. current_position[E_AXIS] -= 10;
  1620. plan_buffer_line(current_position[X_AXIS], current_position[Y_AXIS], current_position[Z_AXIS], current_position[E_AXIS], 800 / 60, active_extruder);
  1621. st_synchronize();
  1622. }
  1623. else {
  1624. //ABS
  1625. max_feedrate[E_AXIS] = 50;
  1626. //current_position[E_AXIS] -= 8;
  1627. //plan_buffer_line(current_position[X_AXIS], current_position[Y_AXIS], current_position[Z_AXIS], current_position[E_AXIS], 2100 / 60, active_extruder);
  1628. //current_position[E_AXIS] += 8;
  1629. //plan_buffer_line(current_position[X_AXIS], current_position[Y_AXIS], current_position[Z_AXIS], current_position[E_AXIS], 2100 / 60, active_extruder);
  1630. current_position[E_AXIS] += 3.1;
  1631. plan_buffer_line(current_position[X_AXIS], current_position[Y_AXIS], current_position[Z_AXIS], current_position[E_AXIS], 2000 / 60, active_extruder);
  1632. current_position[E_AXIS] += 3.1;
  1633. plan_buffer_line(current_position[X_AXIS], current_position[Y_AXIS], current_position[Z_AXIS], current_position[E_AXIS], 2500 / 60, active_extruder);
  1634. current_position[E_AXIS] += 4;
  1635. plan_buffer_line(current_position[X_AXIS], current_position[Y_AXIS], current_position[Z_AXIS], current_position[E_AXIS], 3000 / 60, active_extruder);
  1636. st_synchronize();
  1637. /*current_position[X_AXIS] += 23; //delay
  1638. plan_buffer_line(current_position[X_AXIS], current_position[Y_AXIS], current_position[Z_AXIS], current_position[E_AXIS], 600/60, active_extruder); //delay
  1639. current_position[X_AXIS] -= 23; //delay
  1640. plan_buffer_line(current_position[X_AXIS], current_position[Y_AXIS], current_position[Z_AXIS], current_position[E_AXIS], 600/60, active_extruder); //delay*/
  1641. delay(4700);
  1642. max_feedrate[E_AXIS] = 80;
  1643. current_position[E_AXIS] -= 92;
  1644. plan_buffer_line(current_position[X_AXIS], current_position[Y_AXIS], current_position[Z_AXIS], current_position[E_AXIS], 9900 / 60, active_extruder);
  1645. max_feedrate[E_AXIS] = 50;//tmp[E_AXIS];
  1646. current_position[E_AXIS] -= 5;
  1647. plan_buffer_line(current_position[X_AXIS], current_position[Y_AXIS], current_position[Z_AXIS], current_position[E_AXIS], 800 / 60, active_extruder);
  1648. current_position[E_AXIS] += 5;
  1649. plan_buffer_line(current_position[X_AXIS], current_position[Y_AXIS], current_position[Z_AXIS], current_position[E_AXIS], 400 / 60, active_extruder);
  1650. current_position[E_AXIS] -= 5;
  1651. plan_buffer_line(current_position[X_AXIS], current_position[Y_AXIS], current_position[Z_AXIS], current_position[E_AXIS], 600 / 60, active_extruder);
  1652. st_synchronize();
  1653. current_position[E_AXIS] += 5;
  1654. plan_buffer_line(current_position[X_AXIS], current_position[Y_AXIS], current_position[Z_AXIS], current_position[E_AXIS], 600 / 60, active_extruder);
  1655. current_position[E_AXIS] -= 5;
  1656. plan_buffer_line(current_position[X_AXIS], current_position[Y_AXIS], current_position[Z_AXIS], current_position[E_AXIS], 600 / 60, active_extruder);
  1657. current_position[E_AXIS] += 5;
  1658. plan_buffer_line(current_position[X_AXIS], current_position[Y_AXIS], current_position[Z_AXIS], current_position[E_AXIS], 600 / 60, active_extruder);
  1659. current_position[E_AXIS] -= 5;
  1660. plan_buffer_line(current_position[X_AXIS], current_position[Y_AXIS], current_position[Z_AXIS], current_position[E_AXIS], 600 / 60, active_extruder);
  1661. st_synchronize();
  1662. }
  1663. }
  1664. void process_commands()
  1665. {
  1666. #ifdef FILAMENT_RUNOUT_SUPPORT
  1667. SET_INPUT(FR_SENS);
  1668. #endif
  1669. #ifdef CMDBUFFER_DEBUG
  1670. SERIAL_ECHOPGM("Processing a GCODE command: ");
  1671. SERIAL_ECHO(cmdbuffer+bufindr+1);
  1672. SERIAL_ECHOLNPGM("");
  1673. SERIAL_ECHOPGM("In cmdqueue: ");
  1674. SERIAL_ECHO(buflen);
  1675. SERIAL_ECHOLNPGM("");
  1676. #endif /* CMDBUFFER_DEBUG */
  1677. unsigned long codenum; //throw away variable
  1678. char *starpos = NULL;
  1679. #ifdef ENABLE_AUTO_BED_LEVELING
  1680. float x_tmp, y_tmp, z_tmp, real_z;
  1681. #endif
  1682. // PRUSA GCODES
  1683. #ifdef SNMM
  1684. float tmp_motor[3] = DEFAULT_PWM_MOTOR_CURRENT;
  1685. float tmp_motor_loud[3] = DEFAULT_PWM_MOTOR_CURRENT_LOUD;
  1686. int8_t SilentMode;
  1687. #endif
  1688. if(code_seen("PRUSA")){
  1689. if (code_seen("PRN")) {
  1690. MYSERIAL.println(status_number);
  1691. }else if (code_seen("fn")) {
  1692. if (farm_mode) {
  1693. MYSERIAL.println(farm_no);
  1694. }
  1695. else {
  1696. MYSERIAL.println("Not in farm mode.");
  1697. }
  1698. }else if (code_seen("fv")) {
  1699. // get file version
  1700. #ifdef SDSUPPORT
  1701. card.openFile(strchr_pointer + 3,true);
  1702. while (true) {
  1703. uint16_t readByte = card.get();
  1704. MYSERIAL.write(readByte);
  1705. if (readByte=='\n') {
  1706. break;
  1707. }
  1708. }
  1709. card.closefile();
  1710. #endif // SDSUPPORT
  1711. } else if (code_seen("M28")) {
  1712. trace();
  1713. prusa_sd_card_upload = true;
  1714. card.openFile(strchr_pointer+4,false);
  1715. } else if(code_seen("Fir")){
  1716. SERIAL_PROTOCOLLN(FW_version);
  1717. } else if(code_seen("Rev")){
  1718. SERIAL_PROTOCOLLN(FILAMENT_SIZE "-" ELECTRONICS "-" NOZZLE_TYPE );
  1719. } else if(code_seen("Lang")) {
  1720. lcd_force_language_selection();
  1721. } else if(code_seen("Lz")) {
  1722. EEPROM_save_B(EEPROM_BABYSTEP_Z,0);
  1723. } else if (code_seen("SERIAL LOW")) {
  1724. MYSERIAL.println("SERIAL LOW");
  1725. MYSERIAL.begin(BAUDRATE);
  1726. return;
  1727. } else if (code_seen("SERIAL HIGH")) {
  1728. MYSERIAL.println("SERIAL HIGH");
  1729. MYSERIAL.begin(1152000);
  1730. return;
  1731. } else if(code_seen("Beat")) {
  1732. // Kick farm link timer
  1733. kicktime = millis();
  1734. } else if(code_seen("FR")) {
  1735. // Factory full reset
  1736. factory_reset(0,true);
  1737. }else if(code_seen("Y")) { //filaments adjustment at the beginning of print (for SNMM)
  1738. #ifdef SNMM
  1739. int extr;
  1740. SilentMode = eeprom_read_byte((uint8_t*)EEPROM_SILENT); //is silent mode or loud mode set
  1741. lcd_implementation_clear();
  1742. lcd_display_message_fullscreen_P(MSG_FIL_ADJUSTING);
  1743. current_position[Z_AXIS] = 100;
  1744. plan_buffer_line(current_position[X_AXIS], current_position[Y_AXIS], current_position[Z_AXIS], current_position[E_AXIS], 500, active_extruder);
  1745. digipot_current(2, E_MOTOR_HIGH_CURRENT);
  1746. for (extr = 1; extr < 4; extr++) { //we dont know which filament is in nozzle, but we want to load filament0, so all other filaments must unloaded
  1747. change_extr(extr);
  1748. ramming();
  1749. }
  1750. change_extr(0);
  1751. current_position[E_AXIS] += FIL_LOAD_LENGTH; //loading filament0 into the nozzle
  1752. plan_buffer_line(current_position[X_AXIS], current_position[Y_AXIS], current_position[Z_AXIS], current_position[E_AXIS], 500, active_extruder);
  1753. st_synchronize();
  1754. for (extr = 1; extr < 4; extr++) {
  1755. digipot_current(2, E_MOTOR_LOW_CURRENT); //set lower current for extruder motors
  1756. change_extr(extr);
  1757. current_position[E_AXIS] += (FIL_LOAD_LENGTH + 3 * FIL_RETURN_LENGTH); //adjusting filaments
  1758. plan_buffer_line(current_position[X_AXIS], current_position[Y_AXIS], current_position[Z_AXIS], current_position[E_AXIS], 5000, active_extruder);
  1759. st_synchronize();
  1760. digipot_current(2, tmp_motor_loud[2]); //set back to normal operation currents
  1761. current_position[E_AXIS] -= FIL_RETURN_LENGTH;
  1762. plan_buffer_line(current_position[X_AXIS], current_position[Y_AXIS], current_position[Z_AXIS], current_position[E_AXIS], 500, active_extruder);
  1763. st_synchronize();
  1764. }
  1765. change_extr(0);
  1766. current_position[E_AXIS] += 25;
  1767. plan_buffer_line(current_position[X_AXIS], current_position[Y_AXIS], current_position[Z_AXIS], current_position[E_AXIS], 10, active_extruder);
  1768. digipot_current(2, E_MOTOR_HIGH_CURRENT);
  1769. ramming();
  1770. if (SilentMode == 1) digipot_current(2, tmp_motor[2]); //set back to normal operation currents
  1771. else digipot_current(2, tmp_motor_loud[2]);
  1772. st_synchronize();
  1773. lcd_show_fullscreen_message_and_wait_P(MSG_CONFIRM_NOZZLE_CLEAN_FIL_ADJ);
  1774. lcd_implementation_clear();
  1775. lcd_printPGM(MSG_PLEASE_WAIT);
  1776. current_position[Z_AXIS] = 0;
  1777. plan_buffer_line(current_position[X_AXIS], current_position[Y_AXIS], current_position[Z_AXIS], current_position[E_AXIS], 500, active_extruder);
  1778. st_synchronize();
  1779. lcd_update_enable(true);
  1780. #endif
  1781. }
  1782. else if (code_seen("SetF")) {
  1783. #ifdef SNMM
  1784. bool not_finished = (eeprom_read_byte((unsigned char*)EEPROM_PRINT_FLAG) != PRINT_FINISHED);
  1785. eeprom_update_byte((unsigned char*)EEPROM_PRINT_FLAG, PRINT_STARTED);
  1786. if (not_finished) enquecommand_front_P(PSTR("PRUSA Y"));
  1787. #endif
  1788. }
  1789. else if (code_seen("ResF")) {
  1790. #ifdef SNMM
  1791. eeprom_update_byte((unsigned char*)EEPROM_PRINT_FLAG, PRINT_FINISHED);
  1792. #endif
  1793. }
  1794. //else if (code_seen('Cal')) {
  1795. // lcd_calibration();
  1796. // }
  1797. }
  1798. else if (code_seen('^')) {
  1799. // nothing, this is a version line
  1800. } else if(code_seen('G'))
  1801. {
  1802. switch((int)code_value())
  1803. {
  1804. case 0: // G0 -> G1
  1805. case 1: // G1
  1806. if(Stopped == false) {
  1807. #ifdef FILAMENT_RUNOUT_SUPPORT
  1808. if(READ(FR_SENS)){
  1809. feedmultiplyBckp=feedmultiply;
  1810. float target[4];
  1811. float lastpos[4];
  1812. target[X_AXIS]=current_position[X_AXIS];
  1813. target[Y_AXIS]=current_position[Y_AXIS];
  1814. target[Z_AXIS]=current_position[Z_AXIS];
  1815. target[E_AXIS]=current_position[E_AXIS];
  1816. lastpos[X_AXIS]=current_position[X_AXIS];
  1817. lastpos[Y_AXIS]=current_position[Y_AXIS];
  1818. lastpos[Z_AXIS]=current_position[Z_AXIS];
  1819. lastpos[E_AXIS]=current_position[E_AXIS];
  1820. //retract by E
  1821. target[E_AXIS]+= FILAMENTCHANGE_FIRSTRETRACT ;
  1822. plan_buffer_line(target[X_AXIS], target[Y_AXIS], target[Z_AXIS], target[E_AXIS], 400, active_extruder);
  1823. target[Z_AXIS]+= FILAMENTCHANGE_ZADD ;
  1824. plan_buffer_line(target[X_AXIS], target[Y_AXIS], target[Z_AXIS], target[E_AXIS], 300, active_extruder);
  1825. target[X_AXIS]= FILAMENTCHANGE_XPOS ;
  1826. target[Y_AXIS]= FILAMENTCHANGE_YPOS ;
  1827. plan_buffer_line(target[X_AXIS], target[Y_AXIS], target[Z_AXIS], target[E_AXIS], 70, active_extruder);
  1828. target[E_AXIS]+= FILAMENTCHANGE_FINALRETRACT ;
  1829. plan_buffer_line(target[X_AXIS], target[Y_AXIS], target[Z_AXIS], target[E_AXIS], 20, active_extruder);
  1830. //finish moves
  1831. st_synchronize();
  1832. //disable extruder steppers so filament can be removed
  1833. disable_e0();
  1834. disable_e1();
  1835. disable_e2();
  1836. delay(100);
  1837. //LCD_ALERTMESSAGEPGM(MSG_FILAMENTCHANGE);
  1838. uint8_t cnt=0;
  1839. int counterBeep = 0;
  1840. lcd_wait_interact();
  1841. while(!lcd_clicked()){
  1842. cnt++;
  1843. manage_heater();
  1844. manage_inactivity(true);
  1845. //lcd_update();
  1846. if(cnt==0)
  1847. {
  1848. #if BEEPER > 0
  1849. if (counterBeep== 500){
  1850. counterBeep = 0;
  1851. }
  1852. SET_OUTPUT(BEEPER);
  1853. if (counterBeep== 0){
  1854. WRITE(BEEPER,HIGH);
  1855. }
  1856. if (counterBeep== 20){
  1857. WRITE(BEEPER,LOW);
  1858. }
  1859. counterBeep++;
  1860. #else
  1861. #if !defined(LCD_FEEDBACK_FREQUENCY_HZ) || !defined(LCD_FEEDBACK_FREQUENCY_DURATION_MS)
  1862. lcd_buzz(1000/6,100);
  1863. #else
  1864. lcd_buzz(LCD_FEEDBACK_FREQUENCY_DURATION_MS,LCD_FEEDBACK_FREQUENCY_HZ);
  1865. #endif
  1866. #endif
  1867. }
  1868. }
  1869. WRITE(BEEPER,LOW);
  1870. target[E_AXIS]+= FILAMENTCHANGE_FIRSTFEED ;
  1871. plan_buffer_line(target[X_AXIS], target[Y_AXIS], target[Z_AXIS], target[E_AXIS], 20, active_extruder);
  1872. target[E_AXIS]+= FILAMENTCHANGE_FINALFEED ;
  1873. plan_buffer_line(target[X_AXIS], target[Y_AXIS], target[Z_AXIS], target[E_AXIS], 2, active_extruder);
  1874. lcd_change_fil_state = 0;
  1875. lcd_loading_filament();
  1876. while ((lcd_change_fil_state == 0)||(lcd_change_fil_state != 1)){
  1877. lcd_change_fil_state = 0;
  1878. lcd_alright();
  1879. switch(lcd_change_fil_state){
  1880. case 2:
  1881. target[E_AXIS]+= FILAMENTCHANGE_FIRSTFEED ;
  1882. plan_buffer_line(target[X_AXIS], target[Y_AXIS], target[Z_AXIS], target[E_AXIS], 20, active_extruder);
  1883. target[E_AXIS]+= FILAMENTCHANGE_FINALFEED ;
  1884. plan_buffer_line(target[X_AXIS], target[Y_AXIS], target[Z_AXIS], target[E_AXIS], 2, active_extruder);
  1885. lcd_loading_filament();
  1886. break;
  1887. case 3:
  1888. target[E_AXIS]+= FILAMENTCHANGE_FINALFEED ;
  1889. plan_buffer_line(target[X_AXIS], target[Y_AXIS], target[Z_AXIS], target[E_AXIS], 2, active_extruder);
  1890. lcd_loading_color();
  1891. break;
  1892. default:
  1893. lcd_change_success();
  1894. break;
  1895. }
  1896. }
  1897. target[E_AXIS]+= 5;
  1898. plan_buffer_line(target[X_AXIS], target[Y_AXIS], target[Z_AXIS], target[E_AXIS], 2, active_extruder);
  1899. target[E_AXIS]+= FILAMENTCHANGE_FIRSTRETRACT;
  1900. plan_buffer_line(target[X_AXIS], target[Y_AXIS], target[Z_AXIS], target[E_AXIS], 400, active_extruder);
  1901. //current_position[E_AXIS]=target[E_AXIS]; //the long retract of L is compensated by manual filament feeding
  1902. //plan_set_e_position(current_position[E_AXIS]);
  1903. plan_buffer_line(target[X_AXIS], target[Y_AXIS], target[Z_AXIS], target[E_AXIS], 70, active_extruder); //should do nothing
  1904. plan_buffer_line(lastpos[X_AXIS], lastpos[Y_AXIS], target[Z_AXIS], target[E_AXIS], 70, active_extruder); //move xy back
  1905. plan_buffer_line(lastpos[X_AXIS], lastpos[Y_AXIS], lastpos[Z_AXIS], target[E_AXIS], 200, active_extruder); //move z back
  1906. target[E_AXIS]= target[E_AXIS] - FILAMENTCHANGE_FIRSTRETRACT;
  1907. plan_buffer_line(lastpos[X_AXIS], lastpos[Y_AXIS], lastpos[Z_AXIS], target[E_AXIS], 5, active_extruder); //final untretract
  1908. plan_set_e_position(lastpos[E_AXIS]);
  1909. feedmultiply=feedmultiplyBckp;
  1910. char cmd[9];
  1911. sprintf_P(cmd, PSTR("M220 S%i"), feedmultiplyBckp);
  1912. enquecommand(cmd);
  1913. }
  1914. #endif
  1915. get_coordinates(); // For X Y Z E F
  1916. if (total_filament_used > ((current_position[E_AXIS] - destination[E_AXIS]) * 100)) { //protection against total_filament_used overflow
  1917. total_filament_used = total_filament_used + ((destination[E_AXIS] - current_position[E_AXIS]) * 100);
  1918. }
  1919. #ifdef FWRETRACT
  1920. if(autoretract_enabled)
  1921. if( !(code_seen('X') || code_seen('Y') || code_seen('Z')) && code_seen('E')) {
  1922. float echange=destination[E_AXIS]-current_position[E_AXIS];
  1923. if((echange<-MIN_RETRACT && !retracted) || (echange>MIN_RETRACT && retracted)) { //move appears to be an attempt to retract or recover
  1924. current_position[E_AXIS] = destination[E_AXIS]; //hide the slicer-generated retract/recover from calculations
  1925. plan_set_e_position(current_position[E_AXIS]); //AND from the planner
  1926. retract(!retracted);
  1927. return;
  1928. }
  1929. }
  1930. #endif //FWRETRACT
  1931. prepare_move();
  1932. //ClearToSend();
  1933. }
  1934. break;
  1935. case 2: // G2 - CW ARC
  1936. if(Stopped == false) {
  1937. get_arc_coordinates();
  1938. prepare_arc_move(true);
  1939. }
  1940. break;
  1941. case 3: // G3 - CCW ARC
  1942. if(Stopped == false) {
  1943. get_arc_coordinates();
  1944. prepare_arc_move(false);
  1945. }
  1946. break;
  1947. case 4: // G4 dwell
  1948. LCD_MESSAGERPGM(MSG_DWELL);
  1949. codenum = 0;
  1950. if(code_seen('P')) codenum = code_value(); // milliseconds to wait
  1951. if(code_seen('S')) codenum = code_value() * 1000; // seconds to wait
  1952. st_synchronize();
  1953. codenum += millis(); // keep track of when we started waiting
  1954. previous_millis_cmd = millis();
  1955. while(millis() < codenum) {
  1956. manage_heater();
  1957. manage_inactivity();
  1958. lcd_update();
  1959. }
  1960. break;
  1961. #ifdef FWRETRACT
  1962. case 10: // G10 retract
  1963. #if EXTRUDERS > 1
  1964. retracted_swap[active_extruder]=(code_seen('S') && code_value_long() == 1); // checks for swap retract argument
  1965. retract(true,retracted_swap[active_extruder]);
  1966. #else
  1967. retract(true);
  1968. #endif
  1969. break;
  1970. case 11: // G11 retract_recover
  1971. #if EXTRUDERS > 1
  1972. retract(false,retracted_swap[active_extruder]);
  1973. #else
  1974. retract(false);
  1975. #endif
  1976. break;
  1977. #endif //FWRETRACT
  1978. case 28: //G28 Home all Axis one at a time
  1979. homing_flag = true;
  1980. #ifdef ENABLE_AUTO_BED_LEVELING
  1981. plan_bed_level_matrix.set_to_identity(); //Reset the plane ("erase" all leveling data)
  1982. #endif //ENABLE_AUTO_BED_LEVELING
  1983. // For mesh bed leveling deactivate the matrix temporarily
  1984. #ifdef MESH_BED_LEVELING
  1985. mbl.active = 0;
  1986. #endif
  1987. // Reset world2machine_rotation_and_skew and world2machine_shift, therefore
  1988. // the planner will not perform any adjustments in the XY plane.
  1989. // Wait for the motors to stop and update the current position with the absolute values.
  1990. world2machine_revert_to_uncorrected();
  1991. // Reset baby stepping to zero, if the babystepping has already been loaded before. The babystepsTodo value will be
  1992. // consumed during the first movements following this statement.
  1993. babystep_undo();
  1994. saved_feedrate = feedrate;
  1995. saved_feedmultiply = feedmultiply;
  1996. feedmultiply = 100;
  1997. previous_millis_cmd = millis();
  1998. enable_endstops(true);
  1999. for(int8_t i=0; i < NUM_AXIS; i++)
  2000. destination[i] = current_position[i];
  2001. feedrate = 0.0;
  2002. home_all_axis = !((code_seen(axis_codes[X_AXIS])) || (code_seen(axis_codes[Y_AXIS])) || (code_seen(axis_codes[Z_AXIS])));
  2003. #if Z_HOME_DIR > 0 // If homing away from BED do Z first
  2004. if((home_all_axis) || (code_seen(axis_codes[Z_AXIS]))) {
  2005. homeaxis(Z_AXIS);
  2006. }
  2007. #endif
  2008. #ifdef QUICK_HOME
  2009. // In the quick mode, if both x and y are to be homed, a diagonal move will be performed initially.
  2010. if((home_all_axis)||( code_seen(axis_codes[X_AXIS]) && code_seen(axis_codes[Y_AXIS])) ) //first diagonal move
  2011. {
  2012. current_position[X_AXIS] = 0;current_position[Y_AXIS] = 0;
  2013. int x_axis_home_dir = home_dir(X_AXIS);
  2014. plan_set_position(current_position[X_AXIS], current_position[Y_AXIS], current_position[Z_AXIS], current_position[E_AXIS]);
  2015. 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);
  2016. feedrate = homing_feedrate[X_AXIS];
  2017. if(homing_feedrate[Y_AXIS]<feedrate)
  2018. feedrate = homing_feedrate[Y_AXIS];
  2019. if (max_length(X_AXIS) > max_length(Y_AXIS)) {
  2020. feedrate *= sqrt(pow(max_length(Y_AXIS) / max_length(X_AXIS), 2) + 1);
  2021. } else {
  2022. feedrate *= sqrt(pow(max_length(X_AXIS) / max_length(Y_AXIS), 2) + 1);
  2023. }
  2024. plan_buffer_line(destination[X_AXIS], destination[Y_AXIS], destination[Z_AXIS], destination[E_AXIS], feedrate/60, active_extruder);
  2025. st_synchronize();
  2026. axis_is_at_home(X_AXIS);
  2027. axis_is_at_home(Y_AXIS);
  2028. plan_set_position(current_position[X_AXIS], current_position[Y_AXIS], current_position[Z_AXIS], current_position[E_AXIS]);
  2029. destination[X_AXIS] = current_position[X_AXIS];
  2030. destination[Y_AXIS] = current_position[Y_AXIS];
  2031. plan_buffer_line(destination[X_AXIS], destination[Y_AXIS], destination[Z_AXIS], destination[E_AXIS], feedrate/60, active_extruder);
  2032. feedrate = 0.0;
  2033. st_synchronize();
  2034. endstops_hit_on_purpose();
  2035. current_position[X_AXIS] = destination[X_AXIS];
  2036. current_position[Y_AXIS] = destination[Y_AXIS];
  2037. current_position[Z_AXIS] = destination[Z_AXIS];
  2038. }
  2039. #endif /* QUICK_HOME */
  2040. if((home_all_axis) || (code_seen(axis_codes[X_AXIS])))
  2041. homeaxis(X_AXIS);
  2042. if((home_all_axis) || (code_seen(axis_codes[Y_AXIS])))
  2043. homeaxis(Y_AXIS);
  2044. if(code_seen(axis_codes[X_AXIS]) && code_value_long() != 0)
  2045. current_position[X_AXIS]=code_value()+add_homing[X_AXIS];
  2046. if(code_seen(axis_codes[Y_AXIS]) && code_value_long() != 0)
  2047. current_position[Y_AXIS]=code_value()+add_homing[Y_AXIS];
  2048. #if Z_HOME_DIR < 0 // If homing towards BED do Z last
  2049. #ifndef Z_SAFE_HOMING
  2050. if((home_all_axis) || (code_seen(axis_codes[Z_AXIS]))) {
  2051. #if defined (Z_RAISE_BEFORE_HOMING) && (Z_RAISE_BEFORE_HOMING > 0)
  2052. destination[Z_AXIS] = Z_RAISE_BEFORE_HOMING * home_dir(Z_AXIS) * (-1); // Set destination away from bed
  2053. feedrate = max_feedrate[Z_AXIS];
  2054. plan_buffer_line(destination[X_AXIS], destination[Y_AXIS], destination[Z_AXIS], destination[E_AXIS], feedrate, active_extruder);
  2055. st_synchronize();
  2056. #endif // defined (Z_RAISE_BEFORE_HOMING) && (Z_RAISE_BEFORE_HOMING > 0)
  2057. #ifdef MESH_BED_LEVELING // If Mesh bed leveling, moxve X&Y to safe position for home
  2058. if (!(axis_known_position[X_AXIS] && axis_known_position[Y_AXIS] ))
  2059. {
  2060. homeaxis(X_AXIS);
  2061. homeaxis(Y_AXIS);
  2062. }
  2063. // 1st mesh bed leveling measurement point, corrected.
  2064. world2machine_initialize();
  2065. world2machine(pgm_read_float(bed_ref_points), pgm_read_float(bed_ref_points+1), destination[X_AXIS], destination[Y_AXIS]);
  2066. world2machine_reset();
  2067. if (destination[Y_AXIS] < Y_MIN_POS)
  2068. destination[Y_AXIS] = Y_MIN_POS;
  2069. destination[Z_AXIS] = MESH_HOME_Z_SEARCH; // Set destination away from bed
  2070. feedrate = homing_feedrate[Z_AXIS]/10;
  2071. current_position[Z_AXIS] = 0;
  2072. enable_endstops(false);
  2073. plan_set_position(current_position[X_AXIS], current_position[Y_AXIS], current_position[Z_AXIS], current_position[E_AXIS]);
  2074. plan_buffer_line(destination[X_AXIS], destination[Y_AXIS], destination[Z_AXIS], destination[E_AXIS], feedrate, active_extruder);
  2075. st_synchronize();
  2076. current_position[X_AXIS] = destination[X_AXIS];
  2077. current_position[Y_AXIS] = destination[Y_AXIS];
  2078. enable_endstops(true);
  2079. endstops_hit_on_purpose();
  2080. homeaxis(Z_AXIS);
  2081. #else // MESH_BED_LEVELING
  2082. homeaxis(Z_AXIS);
  2083. #endif // MESH_BED_LEVELING
  2084. }
  2085. #else // defined(Z_SAFE_HOMING): Z Safe mode activated.
  2086. if(home_all_axis) {
  2087. destination[X_AXIS] = round(Z_SAFE_HOMING_X_POINT - X_PROBE_OFFSET_FROM_EXTRUDER);
  2088. destination[Y_AXIS] = round(Z_SAFE_HOMING_Y_POINT - Y_PROBE_OFFSET_FROM_EXTRUDER);
  2089. destination[Z_AXIS] = Z_RAISE_BEFORE_HOMING * home_dir(Z_AXIS) * (-1); // Set destination away from bed
  2090. feedrate = XY_TRAVEL_SPEED/60;
  2091. current_position[Z_AXIS] = 0;
  2092. plan_set_position(current_position[X_AXIS], current_position[Y_AXIS], current_position[Z_AXIS], current_position[E_AXIS]);
  2093. plan_buffer_line(destination[X_AXIS], destination[Y_AXIS], destination[Z_AXIS], destination[E_AXIS], feedrate, active_extruder);
  2094. st_synchronize();
  2095. current_position[X_AXIS] = destination[X_AXIS];
  2096. current_position[Y_AXIS] = destination[Y_AXIS];
  2097. homeaxis(Z_AXIS);
  2098. }
  2099. // Let's see if X and Y are homed and probe is inside bed area.
  2100. if(code_seen(axis_codes[Z_AXIS])) {
  2101. if ( (axis_known_position[X_AXIS]) && (axis_known_position[Y_AXIS]) \
  2102. && (current_position[X_AXIS]+X_PROBE_OFFSET_FROM_EXTRUDER >= X_MIN_POS) \
  2103. && (current_position[X_AXIS]+X_PROBE_OFFSET_FROM_EXTRUDER <= X_MAX_POS) \
  2104. && (current_position[Y_AXIS]+Y_PROBE_OFFSET_FROM_EXTRUDER >= Y_MIN_POS) \
  2105. && (current_position[Y_AXIS]+Y_PROBE_OFFSET_FROM_EXTRUDER <= Y_MAX_POS)) {
  2106. current_position[Z_AXIS] = 0;
  2107. plan_set_position(current_position[X_AXIS], current_position[Y_AXIS], current_position[Z_AXIS], current_position[E_AXIS]);
  2108. destination[Z_AXIS] = Z_RAISE_BEFORE_HOMING * home_dir(Z_AXIS) * (-1); // Set destination away from bed
  2109. feedrate = max_feedrate[Z_AXIS];
  2110. plan_buffer_line(destination[X_AXIS], destination[Y_AXIS], destination[Z_AXIS], destination[E_AXIS], feedrate, active_extruder);
  2111. st_synchronize();
  2112. homeaxis(Z_AXIS);
  2113. } else if (!((axis_known_position[X_AXIS]) && (axis_known_position[Y_AXIS]))) {
  2114. LCD_MESSAGERPGM(MSG_POSITION_UNKNOWN);
  2115. SERIAL_ECHO_START;
  2116. SERIAL_ECHOLNRPGM(MSG_POSITION_UNKNOWN);
  2117. } else {
  2118. LCD_MESSAGERPGM(MSG_ZPROBE_OUT);
  2119. SERIAL_ECHO_START;
  2120. SERIAL_ECHOLNRPGM(MSG_ZPROBE_OUT);
  2121. }
  2122. }
  2123. #endif // Z_SAFE_HOMING
  2124. #endif // Z_HOME_DIR < 0
  2125. if(code_seen(axis_codes[Z_AXIS])) {
  2126. if(code_value_long() != 0) {
  2127. current_position[Z_AXIS]=code_value()+add_homing[Z_AXIS];
  2128. }
  2129. }
  2130. #ifdef ENABLE_AUTO_BED_LEVELING
  2131. if((home_all_axis) || (code_seen(axis_codes[Z_AXIS]))) {
  2132. current_position[Z_AXIS] += zprobe_zoffset; //Add Z_Probe offset (the distance is negative)
  2133. }
  2134. #endif
  2135. plan_set_position(current_position[X_AXIS], current_position[Y_AXIS], current_position[Z_AXIS], current_position[E_AXIS]);
  2136. #ifdef ENDSTOPS_ONLY_FOR_HOMING
  2137. enable_endstops(false);
  2138. #endif
  2139. feedrate = saved_feedrate;
  2140. feedmultiply = saved_feedmultiply;
  2141. previous_millis_cmd = millis();
  2142. endstops_hit_on_purpose();
  2143. #ifndef MESH_BED_LEVELING
  2144. // If MESH_BED_LEVELING is not active, then it is the original Prusa i3.
  2145. // Offer the user to load the baby step value, which has been adjusted at the previous print session.
  2146. if(card.sdprinting && eeprom_read_word((uint16_t *)EEPROM_BABYSTEP_Z))
  2147. lcd_adjust_z();
  2148. #endif
  2149. // Load the machine correction matrix
  2150. world2machine_initialize();
  2151. // and correct the current_position to match the transformed coordinate system.
  2152. world2machine_update_current();
  2153. #ifdef MESH_BED_LEVELING
  2154. if (code_seen(axis_codes[X_AXIS]) || code_seen(axis_codes[Y_AXIS]) || code_seen('W') || code_seen(axis_codes[Z_AXIS]))
  2155. {
  2156. }
  2157. else
  2158. {
  2159. st_synchronize();
  2160. homing_flag = false;
  2161. // Push the commands to the front of the message queue in the reverse order!
  2162. // There shall be always enough space reserved for these commands.
  2163. // enquecommand_front_P((PSTR("G80")));
  2164. goto case_G80;
  2165. }
  2166. #endif
  2167. if (farm_mode) { prusa_statistics(20); };
  2168. homing_flag = false;
  2169. break;
  2170. #ifdef ENABLE_AUTO_BED_LEVELING
  2171. case 29: // G29 Detailed Z-Probe, probes the bed at 3 or more points.
  2172. {
  2173. #if Z_MIN_PIN == -1
  2174. #error "You must have a Z_MIN endstop in order to enable Auto Bed Leveling feature! Z_MIN_PIN must point to a valid hardware pin."
  2175. #endif
  2176. // Prevent user from running a G29 without first homing in X and Y
  2177. if (! (axis_known_position[X_AXIS] && axis_known_position[Y_AXIS]) )
  2178. {
  2179. LCD_MESSAGERPGM(MSG_POSITION_UNKNOWN);
  2180. SERIAL_ECHO_START;
  2181. SERIAL_ECHOLNRPGM(MSG_POSITION_UNKNOWN);
  2182. break; // abort G29, since we don't know where we are
  2183. }
  2184. st_synchronize();
  2185. // make sure the bed_level_rotation_matrix is identity or the planner will get it incorectly
  2186. //vector_3 corrected_position = plan_get_position_mm();
  2187. //corrected_position.debug("position before G29");
  2188. plan_bed_level_matrix.set_to_identity();
  2189. vector_3 uncorrected_position = plan_get_position();
  2190. //uncorrected_position.debug("position durring G29");
  2191. current_position[X_AXIS] = uncorrected_position.x;
  2192. current_position[Y_AXIS] = uncorrected_position.y;
  2193. current_position[Z_AXIS] = uncorrected_position.z;
  2194. plan_set_position(current_position[X_AXIS], current_position[Y_AXIS], current_position[Z_AXIS], current_position[E_AXIS]);
  2195. setup_for_endstop_move();
  2196. feedrate = homing_feedrate[Z_AXIS];
  2197. #ifdef AUTO_BED_LEVELING_GRID
  2198. // probe at the points of a lattice grid
  2199. int xGridSpacing = (RIGHT_PROBE_BED_POSITION - LEFT_PROBE_BED_POSITION) / (AUTO_BED_LEVELING_GRID_POINTS-1);
  2200. int yGridSpacing = (BACK_PROBE_BED_POSITION - FRONT_PROBE_BED_POSITION) / (AUTO_BED_LEVELING_GRID_POINTS-1);
  2201. // solve the plane equation ax + by + d = z
  2202. // A is the matrix with rows [x y 1] for all the probed points
  2203. // B is the vector of the Z positions
  2204. // 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
  2205. // so Vx = -a Vy = -b Vz = 1 (we want the vector facing towards positive Z
  2206. // "A" matrix of the linear system of equations
  2207. double eqnAMatrix[AUTO_BED_LEVELING_GRID_POINTS*AUTO_BED_LEVELING_GRID_POINTS*3];
  2208. // "B" vector of Z points
  2209. double eqnBVector[AUTO_BED_LEVELING_GRID_POINTS*AUTO_BED_LEVELING_GRID_POINTS];
  2210. int probePointCounter = 0;
  2211. bool zig = true;
  2212. for (int yProbe=FRONT_PROBE_BED_POSITION; yProbe <= BACK_PROBE_BED_POSITION; yProbe += yGridSpacing)
  2213. {
  2214. int xProbe, xInc;
  2215. if (zig)
  2216. {
  2217. xProbe = LEFT_PROBE_BED_POSITION;
  2218. //xEnd = RIGHT_PROBE_BED_POSITION;
  2219. xInc = xGridSpacing;
  2220. zig = false;
  2221. } else // zag
  2222. {
  2223. xProbe = RIGHT_PROBE_BED_POSITION;
  2224. //xEnd = LEFT_PROBE_BED_POSITION;
  2225. xInc = -xGridSpacing;
  2226. zig = true;
  2227. }
  2228. for (int xCount=0; xCount < AUTO_BED_LEVELING_GRID_POINTS; xCount++)
  2229. {
  2230. float z_before;
  2231. if (probePointCounter == 0)
  2232. {
  2233. // raise before probing
  2234. z_before = Z_RAISE_BEFORE_PROBING;
  2235. } else
  2236. {
  2237. // raise extruder
  2238. z_before = current_position[Z_AXIS] + Z_RAISE_BETWEEN_PROBINGS;
  2239. }
  2240. float measured_z = probe_pt(xProbe, yProbe, z_before);
  2241. eqnBVector[probePointCounter] = measured_z;
  2242. eqnAMatrix[probePointCounter + 0*AUTO_BED_LEVELING_GRID_POINTS*AUTO_BED_LEVELING_GRID_POINTS] = xProbe;
  2243. eqnAMatrix[probePointCounter + 1*AUTO_BED_LEVELING_GRID_POINTS*AUTO_BED_LEVELING_GRID_POINTS] = yProbe;
  2244. eqnAMatrix[probePointCounter + 2*AUTO_BED_LEVELING_GRID_POINTS*AUTO_BED_LEVELING_GRID_POINTS] = 1;
  2245. probePointCounter++;
  2246. xProbe += xInc;
  2247. }
  2248. }
  2249. clean_up_after_endstop_move();
  2250. // solve lsq problem
  2251. double *plane_equation_coefficients = qr_solve(AUTO_BED_LEVELING_GRID_POINTS*AUTO_BED_LEVELING_GRID_POINTS, 3, eqnAMatrix, eqnBVector);
  2252. SERIAL_PROTOCOLPGM("Eqn coefficients: a: ");
  2253. SERIAL_PROTOCOL(plane_equation_coefficients[0]);
  2254. SERIAL_PROTOCOLPGM(" b: ");
  2255. SERIAL_PROTOCOL(plane_equation_coefficients[1]);
  2256. SERIAL_PROTOCOLPGM(" d: ");
  2257. SERIAL_PROTOCOLLN(plane_equation_coefficients[2]);
  2258. set_bed_level_equation_lsq(plane_equation_coefficients);
  2259. free(plane_equation_coefficients);
  2260. #else // AUTO_BED_LEVELING_GRID not defined
  2261. // Probe at 3 arbitrary points
  2262. // probe 1
  2263. float z_at_pt_1 = probe_pt(ABL_PROBE_PT_1_X, ABL_PROBE_PT_1_Y, Z_RAISE_BEFORE_PROBING);
  2264. // probe 2
  2265. float z_at_pt_2 = probe_pt(ABL_PROBE_PT_2_X, ABL_PROBE_PT_2_Y, current_position[Z_AXIS] + Z_RAISE_BETWEEN_PROBINGS);
  2266. // probe 3
  2267. float z_at_pt_3 = probe_pt(ABL_PROBE_PT_3_X, ABL_PROBE_PT_3_Y, current_position[Z_AXIS] + Z_RAISE_BETWEEN_PROBINGS);
  2268. clean_up_after_endstop_move();
  2269. set_bed_level_equation_3pts(z_at_pt_1, z_at_pt_2, z_at_pt_3);
  2270. #endif // AUTO_BED_LEVELING_GRID
  2271. st_synchronize();
  2272. // The following code correct the Z height difference from z-probe position and hotend tip position.
  2273. // The Z height on homing is measured by Z-Probe, but the probe is quite far from the hotend.
  2274. // When the bed is uneven, this height must be corrected.
  2275. 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)
  2276. x_tmp = current_position[X_AXIS] + X_PROBE_OFFSET_FROM_EXTRUDER;
  2277. y_tmp = current_position[Y_AXIS] + Y_PROBE_OFFSET_FROM_EXTRUDER;
  2278. z_tmp = current_position[Z_AXIS];
  2279. apply_rotation_xyz(plan_bed_level_matrix, x_tmp, y_tmp, z_tmp); //Apply the correction sending the probe offset
  2280. current_position[Z_AXIS] = z_tmp - real_z + current_position[Z_AXIS]; //The difference is added to current position and sent to planner.
  2281. plan_set_position(current_position[X_AXIS], current_position[Y_AXIS], current_position[Z_AXIS], current_position[E_AXIS]);
  2282. }
  2283. break;
  2284. #ifndef Z_PROBE_SLED
  2285. case 30: // G30 Single Z Probe
  2286. {
  2287. st_synchronize();
  2288. // TODO: make sure the bed_level_rotation_matrix is identity or the planner will get set incorectly
  2289. setup_for_endstop_move();
  2290. feedrate = homing_feedrate[Z_AXIS];
  2291. run_z_probe();
  2292. SERIAL_PROTOCOLPGM(MSG_BED);
  2293. SERIAL_PROTOCOLPGM(" X: ");
  2294. SERIAL_PROTOCOL(current_position[X_AXIS]);
  2295. SERIAL_PROTOCOLPGM(" Y: ");
  2296. SERIAL_PROTOCOL(current_position[Y_AXIS]);
  2297. SERIAL_PROTOCOLPGM(" Z: ");
  2298. SERIAL_PROTOCOL(current_position[Z_AXIS]);
  2299. SERIAL_PROTOCOLPGM("\n");
  2300. clean_up_after_endstop_move();
  2301. }
  2302. break;
  2303. #else
  2304. case 31: // dock the sled
  2305. dock_sled(true);
  2306. break;
  2307. case 32: // undock the sled
  2308. dock_sled(false);
  2309. break;
  2310. #endif // Z_PROBE_SLED
  2311. #endif // ENABLE_AUTO_BED_LEVELING
  2312. #ifdef MESH_BED_LEVELING
  2313. case 30: // G30 Single Z Probe
  2314. {
  2315. st_synchronize();
  2316. // TODO: make sure the bed_level_rotation_matrix is identity or the planner will get set incorectly
  2317. setup_for_endstop_move();
  2318. feedrate = homing_feedrate[Z_AXIS];
  2319. find_bed_induction_sensor_point_z(-10.f, 3);
  2320. SERIAL_PROTOCOLRPGM(MSG_BED);
  2321. SERIAL_PROTOCOLPGM(" X: ");
  2322. MYSERIAL.print(current_position[X_AXIS], 5);
  2323. SERIAL_PROTOCOLPGM(" Y: ");
  2324. MYSERIAL.print(current_position[Y_AXIS], 5);
  2325. SERIAL_PROTOCOLPGM(" Z: ");
  2326. MYSERIAL.print(current_position[Z_AXIS], 5);
  2327. SERIAL_PROTOCOLPGM("\n");
  2328. clean_up_after_endstop_move();
  2329. }
  2330. break;
  2331. /**
  2332. * G80: Mesh-based Z probe, probes a grid and produces a
  2333. * mesh to compensate for variable bed height
  2334. *
  2335. * The S0 report the points as below
  2336. *
  2337. * +----> X-axis
  2338. * |
  2339. * |
  2340. * v Y-axis
  2341. *
  2342. */
  2343. #ifdef DIS
  2344. case 77:
  2345. {
  2346. //G77 X200 Y150 XP100 YP15 XO10 Y015
  2347. //for 9 point mesh bed leveling G77 X203 Y196 XP3 YP3 XO0 YO0
  2348. //G77 X232 Y218 XP116 YP109 XO-11 YO0
  2349. float dimension_x = 40;
  2350. float dimension_y = 40;
  2351. int points_x = 40;
  2352. int points_y = 40;
  2353. float offset_x = 74;
  2354. float offset_y = 33;
  2355. if (code_seen('X')) dimension_x = code_value();
  2356. if (code_seen('Y')) dimension_y = code_value();
  2357. if (code_seen('XP')) points_x = code_value();
  2358. if (code_seen('YP')) points_y = code_value();
  2359. if (code_seen('XO')) offset_x = code_value();
  2360. if (code_seen('YO')) offset_y = code_value();
  2361. bed_analysis(dimension_x,dimension_y,points_x,points_y,offset_x,offset_y);
  2362. } break;
  2363. #endif
  2364. case 80:
  2365. case_G80:
  2366. {
  2367. // Firstly check if we know where we are
  2368. if ( !( axis_known_position[X_AXIS] && axis_known_position[Y_AXIS] && axis_known_position[Z_AXIS] ) ){
  2369. // We don't know where we are! HOME!
  2370. // Push the commands to the front of the message queue in the reverse order!
  2371. // There shall be always enough space reserved for these commands.
  2372. repeatcommand_front(); // repeat G80 with all its parameters
  2373. enquecommand_front_P((PSTR("G28 W0")));
  2374. break;
  2375. }
  2376. // Save custom message state, set a new custom message state to display: Calibrating point 9.
  2377. bool custom_message_old = custom_message;
  2378. unsigned int custom_message_type_old = custom_message_type;
  2379. unsigned int custom_message_state_old = custom_message_state;
  2380. custom_message = true;
  2381. custom_message_type = 1;
  2382. custom_message_state = (MESH_MEAS_NUM_X_POINTS * MESH_MEAS_NUM_Y_POINTS) + 10;
  2383. lcd_update(1);
  2384. mbl.reset();
  2385. // Reset baby stepping to zero, if the babystepping has already been loaded before. The babystepsTodo value will be
  2386. // consumed during the first movements following this statement.
  2387. babystep_undo();
  2388. // Cycle through all points and probe them
  2389. // First move up. During this first movement, the babystepping will be reverted.
  2390. current_position[Z_AXIS] = MESH_HOME_Z_SEARCH;
  2391. plan_buffer_line(current_position[X_AXIS], current_position[Y_AXIS], current_position[Z_AXIS], current_position[E_AXIS], homing_feedrate[Z_AXIS]/60, active_extruder);
  2392. // The move to the first calibration point.
  2393. current_position[X_AXIS] = pgm_read_float(bed_ref_points);
  2394. current_position[Y_AXIS] = pgm_read_float(bed_ref_points+1);
  2395. world2machine_clamp(current_position[X_AXIS], current_position[Y_AXIS]);
  2396. // mbl.get_meas_xy(0, 0, current_position[X_AXIS], current_position[Y_AXIS], false);
  2397. plan_buffer_line(current_position[X_AXIS], current_position[Y_AXIS], current_position[Z_AXIS], current_position[E_AXIS], homing_feedrate[X_AXIS]/30, active_extruder);
  2398. // Wait until the move is finished.
  2399. st_synchronize();
  2400. int mesh_point = 0;
  2401. int ix = 0;
  2402. int iy = 0;
  2403. int XY_AXIS_FEEDRATE = homing_feedrate[X_AXIS]/20;
  2404. int Z_PROBE_FEEDRATE = homing_feedrate[Z_AXIS]/60;
  2405. int Z_LIFT_FEEDRATE = homing_feedrate[Z_AXIS]/40;
  2406. bool has_z = is_bed_z_jitter_data_valid();
  2407. setup_for_endstop_move(false);
  2408. const char *kill_message = NULL;
  2409. while (mesh_point != MESH_MEAS_NUM_X_POINTS * MESH_MEAS_NUM_Y_POINTS) {
  2410. // Get coords of a measuring point.
  2411. ix = mesh_point % MESH_MEAS_NUM_X_POINTS;
  2412. iy = mesh_point / MESH_MEAS_NUM_X_POINTS;
  2413. if (iy & 1) ix = (MESH_MEAS_NUM_X_POINTS - 1) - ix; // Zig zag
  2414. float z0 = 0.f;
  2415. if (has_z && mesh_point > 0) {
  2416. uint16_t z_offset_u = eeprom_read_word((uint16_t*)(EEPROM_BED_CALIBRATION_Z_JITTER + 2 * (ix + iy * 3 - 1)));
  2417. z0 = mbl.z_values[0][0] + *reinterpret_cast<int16_t*>(&z_offset_u) * 0.01;
  2418. #if 0
  2419. SERIAL_ECHOPGM("Bed leveling, point: ");
  2420. MYSERIAL.print(mesh_point);
  2421. SERIAL_ECHOPGM(", calibration z: ");
  2422. MYSERIAL.print(z0, 5);
  2423. SERIAL_ECHOLNPGM("");
  2424. #endif
  2425. }
  2426. // Move Z up to MESH_HOME_Z_SEARCH.
  2427. current_position[Z_AXIS] = MESH_HOME_Z_SEARCH;
  2428. plan_buffer_line(current_position[X_AXIS], current_position[Y_AXIS], current_position[Z_AXIS], current_position[E_AXIS], Z_LIFT_FEEDRATE, active_extruder);
  2429. st_synchronize();
  2430. // Move to XY position of the sensor point.
  2431. current_position[X_AXIS] = pgm_read_float(bed_ref_points+2*mesh_point);
  2432. current_position[Y_AXIS] = pgm_read_float(bed_ref_points+2*mesh_point+1);
  2433. world2machine_clamp(current_position[X_AXIS], current_position[Y_AXIS]);
  2434. plan_buffer_line(current_position[X_AXIS], current_position[Y_AXIS], current_position[Z_AXIS], current_position[E_AXIS], XY_AXIS_FEEDRATE, active_extruder);
  2435. st_synchronize();
  2436. // Go down until endstop is hit
  2437. const float Z_CALIBRATION_THRESHOLD = 1.f;
  2438. if (! find_bed_induction_sensor_point_z((has_z && mesh_point > 0) ? z0 - Z_CALIBRATION_THRESHOLD : -10.f)) {
  2439. kill_message = MSG_BED_LEVELING_FAILED_POINT_LOW;
  2440. break;
  2441. }
  2442. if (MESH_HOME_Z_SEARCH - current_position[Z_AXIS] < 0.1f) {
  2443. kill_message = MSG_BED_LEVELING_FAILED_PROBE_DISCONNECTED;
  2444. break;
  2445. }
  2446. if (has_z && fabs(z0 - current_position[Z_AXIS]) > Z_CALIBRATION_THRESHOLD) {
  2447. kill_message = MSG_BED_LEVELING_FAILED_POINT_HIGH;
  2448. break;
  2449. }
  2450. mbl.set_z(ix, iy, current_position[Z_AXIS]);
  2451. custom_message_state--;
  2452. mesh_point++;
  2453. lcd_update(1);
  2454. }
  2455. current_position[Z_AXIS] = MESH_HOME_Z_SEARCH;
  2456. plan_buffer_line(current_position[X_AXIS], current_position[Y_AXIS],current_position[Z_AXIS] , current_position[E_AXIS], Z_LIFT_FEEDRATE, active_extruder);
  2457. st_synchronize();
  2458. if (mesh_point != MESH_MEAS_NUM_X_POINTS * MESH_MEAS_NUM_Y_POINTS) {
  2459. kill(kill_message);
  2460. }
  2461. clean_up_after_endstop_move();
  2462. // Apply Z height correction aka baby stepping before mesh bed leveing gets activated.
  2463. babystep_apply();
  2464. bool eeprom_bed_correction_valid = eeprom_read_byte((unsigned char*)EEPROM_BED_CORRECTION_VALID) == 1;
  2465. for (uint8_t i = 0; i < 4; ++ i) {
  2466. unsigned char codes[4] = { 'L', 'R', 'F', 'B' };
  2467. long correction = 0;
  2468. if (code_seen(codes[i]))
  2469. correction = code_value_long();
  2470. else if (eeprom_bed_correction_valid) {
  2471. unsigned char *addr = (i < 2) ?
  2472. ((i == 0) ? (unsigned char*)EEPROM_BED_CORRECTION_LEFT : (unsigned char*)EEPROM_BED_CORRECTION_RIGHT) :
  2473. ((i == 2) ? (unsigned char*)EEPROM_BED_CORRECTION_FRONT : (unsigned char*)EEPROM_BED_CORRECTION_REAR);
  2474. correction = eeprom_read_int8(addr);
  2475. }
  2476. if (correction == 0)
  2477. continue;
  2478. float offset = float(correction) * 0.001f;
  2479. if (fabs(offset) > 0.101f) {
  2480. SERIAL_ERROR_START;
  2481. SERIAL_ECHOPGM("Excessive bed leveling correction: ");
  2482. SERIAL_ECHO(offset);
  2483. SERIAL_ECHOLNPGM(" microns");
  2484. } else {
  2485. switch (i) {
  2486. case 0:
  2487. for (uint8_t row = 0; row < 3; ++ row) {
  2488. mbl.z_values[row][1] += 0.5f * offset;
  2489. mbl.z_values[row][0] += offset;
  2490. }
  2491. break;
  2492. case 1:
  2493. for (uint8_t row = 0; row < 3; ++ row) {
  2494. mbl.z_values[row][1] += 0.5f * offset;
  2495. mbl.z_values[row][2] += offset;
  2496. }
  2497. break;
  2498. case 2:
  2499. for (uint8_t col = 0; col < 3; ++ col) {
  2500. mbl.z_values[1][col] += 0.5f * offset;
  2501. mbl.z_values[0][col] += offset;
  2502. }
  2503. break;
  2504. case 3:
  2505. for (uint8_t col = 0; col < 3; ++ col) {
  2506. mbl.z_values[1][col] += 0.5f * offset;
  2507. mbl.z_values[2][col] += offset;
  2508. }
  2509. break;
  2510. }
  2511. }
  2512. }
  2513. mbl.upsample_3x3();
  2514. mbl.active = 1;
  2515. go_home_with_z_lift();
  2516. // Restore custom message state
  2517. custom_message = custom_message_old;
  2518. custom_message_type = custom_message_type_old;
  2519. custom_message_state = custom_message_state_old;
  2520. lcd_update(1);
  2521. }
  2522. break;
  2523. /**
  2524. * G81: Print mesh bed leveling status and bed profile if activated
  2525. */
  2526. case 81:
  2527. if (mbl.active) {
  2528. SERIAL_PROTOCOLPGM("Num X,Y: ");
  2529. SERIAL_PROTOCOL(MESH_NUM_X_POINTS);
  2530. SERIAL_PROTOCOLPGM(",");
  2531. SERIAL_PROTOCOL(MESH_NUM_Y_POINTS);
  2532. SERIAL_PROTOCOLPGM("\nZ search height: ");
  2533. SERIAL_PROTOCOL(MESH_HOME_Z_SEARCH);
  2534. SERIAL_PROTOCOLLNPGM("\nMeasured points:");
  2535. for (int y = MESH_NUM_Y_POINTS-1; y >= 0; y--) {
  2536. for (int x = 0; x < MESH_NUM_X_POINTS; x++) {
  2537. SERIAL_PROTOCOLPGM(" ");
  2538. SERIAL_PROTOCOL_F(mbl.z_values[y][x], 5);
  2539. }
  2540. SERIAL_PROTOCOLPGM("\n");
  2541. }
  2542. }
  2543. else
  2544. SERIAL_PROTOCOLLNPGM("Mesh bed leveling not active.");
  2545. break;
  2546. #if 0
  2547. /**
  2548. * G82: Single Z probe at current location
  2549. *
  2550. * WARNING! USE WITH CAUTION! If you'll try to probe where is no leveling pad, nasty things can happen!
  2551. *
  2552. */
  2553. case 82:
  2554. SERIAL_PROTOCOLLNPGM("Finding bed ");
  2555. setup_for_endstop_move();
  2556. find_bed_induction_sensor_point_z();
  2557. clean_up_after_endstop_move();
  2558. SERIAL_PROTOCOLPGM("Bed found at: ");
  2559. SERIAL_PROTOCOL_F(current_position[Z_AXIS], 5);
  2560. SERIAL_PROTOCOLPGM("\n");
  2561. break;
  2562. /**
  2563. * G83: Prusa3D specific: Babystep in Z and store to EEPROM
  2564. */
  2565. case 83:
  2566. {
  2567. int babystepz = code_seen('S') ? code_value() : 0;
  2568. int BabyPosition = code_seen('P') ? code_value() : 0;
  2569. if (babystepz != 0) {
  2570. //FIXME Vojtech: What shall be the index of the axis Z: 3 or 4?
  2571. // Is the axis indexed starting with zero or one?
  2572. if (BabyPosition > 4) {
  2573. SERIAL_PROTOCOLLNPGM("Index out of bounds");
  2574. }else{
  2575. // Save it to the eeprom
  2576. babystepLoadZ = babystepz;
  2577. EEPROM_save_B(EEPROM_BABYSTEP_Z0+(BabyPosition*2),&babystepLoadZ);
  2578. // adjust the Z
  2579. babystepsTodoZadd(babystepLoadZ);
  2580. }
  2581. }
  2582. }
  2583. break;
  2584. /**
  2585. * G84: Prusa3D specific: UNDO Babystep Z (move Z axis back)
  2586. */
  2587. case 84:
  2588. babystepsTodoZsubtract(babystepLoadZ);
  2589. // babystepLoadZ = 0;
  2590. break;
  2591. /**
  2592. * G85: Prusa3D specific: Pick best babystep
  2593. */
  2594. case 85:
  2595. lcd_pick_babystep();
  2596. break;
  2597. #endif
  2598. /**
  2599. * G86: Prusa3D specific: Disable babystep correction after home.
  2600. * This G-code will be performed at the start of a calibration script.
  2601. */
  2602. case 86:
  2603. calibration_status_store(CALIBRATION_STATUS_LIVE_ADJUST);
  2604. break;
  2605. /**
  2606. * G87: Prusa3D specific: Enable babystep correction after home
  2607. * This G-code will be performed at the end of a calibration script.
  2608. */
  2609. case 87:
  2610. calibration_status_store(CALIBRATION_STATUS_CALIBRATED);
  2611. break;
  2612. /**
  2613. * G88: Prusa3D specific: Don't know what it is for, it is in V2Calibration.gcode
  2614. */
  2615. case 88:
  2616. break;
  2617. #endif // ENABLE_MESH_BED_LEVELING
  2618. case 90: // G90
  2619. relative_mode = false;
  2620. break;
  2621. case 91: // G91
  2622. relative_mode = true;
  2623. break;
  2624. case 92: // G92
  2625. if(!code_seen(axis_codes[E_AXIS]))
  2626. st_synchronize();
  2627. for(int8_t i=0; i < NUM_AXIS; i++) {
  2628. if(code_seen(axis_codes[i])) {
  2629. if(i == E_AXIS) {
  2630. current_position[i] = code_value();
  2631. plan_set_e_position(current_position[E_AXIS]);
  2632. }
  2633. else {
  2634. current_position[i] = code_value()+add_homing[i];
  2635. plan_set_position(current_position[X_AXIS], current_position[Y_AXIS], current_position[Z_AXIS], current_position[E_AXIS]);
  2636. }
  2637. }
  2638. }
  2639. break;
  2640. case 98:
  2641. farm_no = 21;
  2642. EEPROM_save_B(EEPROM_FARM_MODE, &farm_no);
  2643. farm_mode = true;
  2644. break;
  2645. case 99:
  2646. farm_no = 0;
  2647. EEPROM_save_B(EEPROM_FARM_MODE, &farm_no);
  2648. farm_mode = false;
  2649. break;
  2650. }
  2651. } // end if(code_seen('G'))
  2652. else if(code_seen('M'))
  2653. {
  2654. int index;
  2655. for (index = 1; *(strchr_pointer + index) == ' ' || *(strchr_pointer + index) == '\t'; index++);
  2656. /*for (++strchr_pointer; *strchr_pointer == ' ' || *strchr_pointer == '\t'; ++strchr_pointer);*/
  2657. if (*(strchr_pointer+index) < '0' || *(strchr_pointer+index) > '9') {
  2658. SERIAL_ECHOLNPGM("Invalid M code");
  2659. } else
  2660. switch((int)code_value())
  2661. {
  2662. #ifdef ULTIPANEL
  2663. case 0: // M0 - Unconditional stop - Wait for user button press on LCD
  2664. case 1: // M1 - Conditional stop - Wait for user button press on LCD
  2665. {
  2666. char *src = strchr_pointer + 2;
  2667. codenum = 0;
  2668. bool hasP = false, hasS = false;
  2669. if (code_seen('P')) {
  2670. codenum = code_value(); // milliseconds to wait
  2671. hasP = codenum > 0;
  2672. }
  2673. if (code_seen('S')) {
  2674. codenum = code_value() * 1000; // seconds to wait
  2675. hasS = codenum > 0;
  2676. }
  2677. starpos = strchr(src, '*');
  2678. if (starpos != NULL) *(starpos) = '\0';
  2679. while (*src == ' ') ++src;
  2680. if (!hasP && !hasS && *src != '\0') {
  2681. lcd_setstatus(src);
  2682. } else {
  2683. LCD_MESSAGERPGM(MSG_USERWAIT);
  2684. }
  2685. lcd_ignore_click(); //call lcd_ignore_click aslo for else ???
  2686. st_synchronize();
  2687. previous_millis_cmd = millis();
  2688. if (codenum > 0){
  2689. codenum += millis(); // keep track of when we started waiting
  2690. while(millis() < codenum && !lcd_clicked()){
  2691. manage_heater();
  2692. manage_inactivity(true);
  2693. lcd_update();
  2694. }
  2695. lcd_ignore_click(false);
  2696. }else{
  2697. if (!lcd_detected())
  2698. break;
  2699. while(!lcd_clicked()){
  2700. manage_heater();
  2701. manage_inactivity(true);
  2702. lcd_update();
  2703. }
  2704. }
  2705. if (IS_SD_PRINTING)
  2706. LCD_MESSAGERPGM(MSG_RESUMING);
  2707. else
  2708. LCD_MESSAGERPGM(WELCOME_MSG);
  2709. }
  2710. break;
  2711. #endif
  2712. case 17:
  2713. LCD_MESSAGERPGM(MSG_NO_MOVE);
  2714. enable_x();
  2715. enable_y();
  2716. enable_z();
  2717. enable_e0();
  2718. enable_e1();
  2719. enable_e2();
  2720. break;
  2721. #ifdef SDSUPPORT
  2722. case 20: // M20 - list SD card
  2723. SERIAL_PROTOCOLLNRPGM(MSG_BEGIN_FILE_LIST);
  2724. card.ls();
  2725. SERIAL_PROTOCOLLNRPGM(MSG_END_FILE_LIST);
  2726. break;
  2727. case 21: // M21 - init SD card
  2728. card.initsd();
  2729. break;
  2730. case 22: //M22 - release SD card
  2731. card.release();
  2732. break;
  2733. case 23: //M23 - Select file
  2734. starpos = (strchr(strchr_pointer + 4,'*'));
  2735. if(starpos!=NULL)
  2736. *(starpos)='\0';
  2737. card.openFile(strchr_pointer + 4,true);
  2738. break;
  2739. case 24: //M24 - Start SD print
  2740. card.startFileprint();
  2741. starttime=millis();
  2742. break;
  2743. case 25: //M25 - Pause SD print
  2744. card.pauseSDPrint();
  2745. break;
  2746. case 26: //M26 - Set SD index
  2747. if(card.cardOK && code_seen('S')) {
  2748. card.setIndex(code_value_long());
  2749. }
  2750. break;
  2751. case 27: //M27 - Get SD status
  2752. card.getStatus();
  2753. break;
  2754. case 28: //M28 - Start SD write
  2755. starpos = (strchr(strchr_pointer + 4,'*'));
  2756. if(starpos != NULL){
  2757. char* npos = strchr(CMDBUFFER_CURRENT_STRING, 'N');
  2758. strchr_pointer = strchr(npos,' ') + 1;
  2759. *(starpos) = '\0';
  2760. }
  2761. card.openFile(strchr_pointer+4,false);
  2762. break;
  2763. case 29: //M29 - Stop SD write
  2764. //processed in write to file routine above
  2765. //card,saving = false;
  2766. break;
  2767. case 30: //M30 <filename> Delete File
  2768. if (card.cardOK){
  2769. card.closefile();
  2770. starpos = (strchr(strchr_pointer + 4,'*'));
  2771. if(starpos != NULL){
  2772. char* npos = strchr(CMDBUFFER_CURRENT_STRING, 'N');
  2773. strchr_pointer = strchr(npos,' ') + 1;
  2774. *(starpos) = '\0';
  2775. }
  2776. card.removeFile(strchr_pointer + 4);
  2777. }
  2778. break;
  2779. case 32: //M32 - Select file and start SD print
  2780. {
  2781. if(card.sdprinting) {
  2782. st_synchronize();
  2783. }
  2784. starpos = (strchr(strchr_pointer + 4,'*'));
  2785. char* namestartpos = (strchr(strchr_pointer + 4,'!')); //find ! to indicate filename string start.
  2786. if(namestartpos==NULL)
  2787. {
  2788. namestartpos=strchr_pointer + 4; //default name position, 4 letters after the M
  2789. }
  2790. else
  2791. namestartpos++; //to skip the '!'
  2792. if(starpos!=NULL)
  2793. *(starpos)='\0';
  2794. bool call_procedure=(code_seen('P'));
  2795. if(strchr_pointer>namestartpos)
  2796. call_procedure=false; //false alert, 'P' found within filename
  2797. if( card.cardOK )
  2798. {
  2799. card.openFile(namestartpos,true,!call_procedure);
  2800. if(code_seen('S'))
  2801. if(strchr_pointer<namestartpos) //only if "S" is occuring _before_ the filename
  2802. card.setIndex(code_value_long());
  2803. card.startFileprint();
  2804. if(!call_procedure)
  2805. starttime=millis(); //procedure calls count as normal print time.
  2806. }
  2807. } break;
  2808. case 928: //M928 - Start SD write
  2809. starpos = (strchr(strchr_pointer + 5,'*'));
  2810. if(starpos != NULL){
  2811. char* npos = strchr(CMDBUFFER_CURRENT_STRING, 'N');
  2812. strchr_pointer = strchr(npos,' ') + 1;
  2813. *(starpos) = '\0';
  2814. }
  2815. card.openLogFile(strchr_pointer+5);
  2816. break;
  2817. #endif //SDSUPPORT
  2818. case 31: //M31 take time since the start of the SD print or an M109 command
  2819. {
  2820. stoptime=millis();
  2821. char time[30];
  2822. unsigned long t=(stoptime-starttime)/1000;
  2823. int sec,min;
  2824. min=t/60;
  2825. sec=t%60;
  2826. sprintf_P(time, PSTR("%i min, %i sec"), min, sec);
  2827. SERIAL_ECHO_START;
  2828. SERIAL_ECHOLN(time);
  2829. lcd_setstatus(time);
  2830. autotempShutdown();
  2831. }
  2832. break;
  2833. case 42: //M42 -Change pin status via gcode
  2834. if (code_seen('S'))
  2835. {
  2836. int pin_status = code_value();
  2837. int pin_number = LED_PIN;
  2838. if (code_seen('P') && pin_status >= 0 && pin_status <= 255)
  2839. pin_number = code_value();
  2840. for(int8_t i = 0; i < (int8_t)(sizeof(sensitive_pins)/sizeof(int)); i++)
  2841. {
  2842. if (sensitive_pins[i] == pin_number)
  2843. {
  2844. pin_number = -1;
  2845. break;
  2846. }
  2847. }
  2848. #if defined(FAN_PIN) && FAN_PIN > -1
  2849. if (pin_number == FAN_PIN)
  2850. fanSpeed = pin_status;
  2851. #endif
  2852. if (pin_number > -1)
  2853. {
  2854. pinMode(pin_number, OUTPUT);
  2855. digitalWrite(pin_number, pin_status);
  2856. analogWrite(pin_number, pin_status);
  2857. }
  2858. }
  2859. break;
  2860. case 44: // M44: Prusa3D: Reset the bed skew and offset calibration.
  2861. // Reset the baby step value and the baby step applied flag.
  2862. calibration_status_store(CALIBRATION_STATUS_ASSEMBLED);
  2863. eeprom_update_word((uint16_t*)EEPROM_BABYSTEP_Z, 0);
  2864. // Reset the skew and offset in both RAM and EEPROM.
  2865. reset_bed_offset_and_skew();
  2866. // Reset world2machine_rotation_and_skew and world2machine_shift, therefore
  2867. // the planner will not perform any adjustments in the XY plane.
  2868. // Wait for the motors to stop and update the current position with the absolute values.
  2869. world2machine_revert_to_uncorrected();
  2870. break;
  2871. case 45: // M45: Prusa3D: bed skew and offset with manual Z up
  2872. {
  2873. // Only Z calibration?
  2874. bool onlyZ = code_seen('Z');
  2875. if (!onlyZ) {
  2876. setTargetBed(0);
  2877. setTargetHotend(0, 0);
  2878. setTargetHotend(0, 1);
  2879. setTargetHotend(0, 2);
  2880. adjust_bed_reset(); //reset bed level correction
  2881. }
  2882. // Disable the default update procedure of the display. We will do a modal dialog.
  2883. lcd_update_enable(false);
  2884. // Let the planner use the uncorrected coordinates.
  2885. mbl.reset();
  2886. // Reset world2machine_rotation_and_skew and world2machine_shift, therefore
  2887. // the planner will not perform any adjustments in the XY plane.
  2888. // Wait for the motors to stop and update the current position with the absolute values.
  2889. world2machine_revert_to_uncorrected();
  2890. // Reset the baby step value applied without moving the axes.
  2891. babystep_reset();
  2892. // Mark all axes as in a need for homing.
  2893. memset(axis_known_position, 0, sizeof(axis_known_position));
  2894. // Let the user move the Z axes up to the end stoppers.
  2895. if (lcd_calibrate_z_end_stop_manual( onlyZ )) {
  2896. refresh_cmd_timeout();
  2897. if (((degHotend(0)>MAX_HOTEND_TEMP_CALIBRATION) || (degBed() > MAX_BED_TEMP_CALIBRATION))&& (!onlyZ)) lcd_wait_for_cool_down();
  2898. lcd_display_message_fullscreen_P(MSG_MEASURE_BED_REFERENCE_HEIGHT_LINE1);
  2899. lcd_implementation_print_at(0, 3, 1);
  2900. lcd_printPGM(MSG_MEASURE_BED_REFERENCE_HEIGHT_LINE2);
  2901. // Move the print head close to the bed.
  2902. current_position[Z_AXIS] = MESH_HOME_Z_SEARCH;
  2903. plan_buffer_line(current_position[X_AXIS], current_position[Y_AXIS],current_position[Z_AXIS] , current_position[E_AXIS], homing_feedrate[Z_AXIS]/40, active_extruder);
  2904. st_synchronize();
  2905. // Home in the XY plane.
  2906. set_destination_to_current();
  2907. setup_for_endstop_move();
  2908. home_xy();
  2909. int8_t verbosity_level = 0;
  2910. if (code_seen('V')) {
  2911. // Just 'V' without a number counts as V1.
  2912. char c = strchr_pointer[1];
  2913. verbosity_level = (c == ' ' || c == '\t' || c == 0) ? 1 : code_value_short();
  2914. }
  2915. if (onlyZ) {
  2916. clean_up_after_endstop_move();
  2917. // Z only calibration.
  2918. // Load the machine correction matrix
  2919. world2machine_initialize();
  2920. // and correct the current_position to match the transformed coordinate system.
  2921. world2machine_update_current();
  2922. //FIXME
  2923. bool result = sample_mesh_and_store_reference();
  2924. if (result) {
  2925. if (calibration_status() == CALIBRATION_STATUS_Z_CALIBRATION)
  2926. // Shipped, the nozzle height has been set already. The user can start printing now.
  2927. calibration_status_store(CALIBRATION_STATUS_CALIBRATED);
  2928. // babystep_apply();
  2929. }
  2930. } else {
  2931. // Reset the baby step value and the baby step applied flag.
  2932. calibration_status_store(CALIBRATION_STATUS_ASSEMBLED);
  2933. eeprom_update_word((uint16_t*)EEPROM_BABYSTEP_Z, 0);
  2934. // Complete XYZ calibration.
  2935. BedSkewOffsetDetectionResultType result = find_bed_offset_and_skew(verbosity_level);
  2936. uint8_t point_too_far_mask = 0;
  2937. clean_up_after_endstop_move();
  2938. // Print head up.
  2939. current_position[Z_AXIS] = MESH_HOME_Z_SEARCH;
  2940. plan_buffer_line(current_position[X_AXIS], current_position[Y_AXIS],current_position[Z_AXIS] , current_position[E_AXIS], homing_feedrate[Z_AXIS]/40, active_extruder);
  2941. st_synchronize();
  2942. if (result >= 0) {
  2943. // Second half: The fine adjustment.
  2944. // Let the planner use the uncorrected coordinates.
  2945. mbl.reset();
  2946. world2machine_reset();
  2947. // Home in the XY plane.
  2948. setup_for_endstop_move();
  2949. home_xy();
  2950. result = improve_bed_offset_and_skew(1, verbosity_level, point_too_far_mask);
  2951. clean_up_after_endstop_move();
  2952. // Print head up.
  2953. current_position[Z_AXIS] = MESH_HOME_Z_SEARCH;
  2954. plan_buffer_line(current_position[X_AXIS], current_position[Y_AXIS],current_position[Z_AXIS] , current_position[E_AXIS], homing_feedrate[Z_AXIS]/40, active_extruder);
  2955. st_synchronize();
  2956. // if (result >= 0) babystep_apply();
  2957. }
  2958. lcd_bed_calibration_show_result(result, point_too_far_mask);
  2959. if (result >= 0) {
  2960. // Calibration valid, the machine should be able to print. Advise the user to run the V2Calibration.gcode.
  2961. calibration_status_store(CALIBRATION_STATUS_LIVE_ADJUST);
  2962. lcd_show_fullscreen_message_and_wait_P(MSG_BABYSTEP_Z_NOT_SET);
  2963. }
  2964. }
  2965. } else {
  2966. // Timeouted.
  2967. }
  2968. lcd_update_enable(true);
  2969. break;
  2970. }
  2971. /*
  2972. case 46:
  2973. {
  2974. // M46: Prusa3D: Show the assigned IP address.
  2975. uint8_t ip[4];
  2976. bool hasIP = card.ToshibaFlashAir_GetIP(ip);
  2977. if (hasIP) {
  2978. SERIAL_ECHOPGM("Toshiba FlashAir current IP: ");
  2979. SERIAL_ECHO(int(ip[0]));
  2980. SERIAL_ECHOPGM(".");
  2981. SERIAL_ECHO(int(ip[1]));
  2982. SERIAL_ECHOPGM(".");
  2983. SERIAL_ECHO(int(ip[2]));
  2984. SERIAL_ECHOPGM(".");
  2985. SERIAL_ECHO(int(ip[3]));
  2986. SERIAL_ECHOLNPGM("");
  2987. } else {
  2988. SERIAL_ECHOLNPGM("Toshiba FlashAir GetIP failed");
  2989. }
  2990. break;
  2991. }
  2992. */
  2993. case 47:
  2994. // M47: Prusa3D: Show end stops dialog on the display.
  2995. lcd_diag_show_end_stops();
  2996. break;
  2997. #if 0
  2998. case 48: // M48: scan the bed induction sensor points, print the sensor trigger coordinates to the serial line for visualization on the PC.
  2999. {
  3000. // Disable the default update procedure of the display. We will do a modal dialog.
  3001. lcd_update_enable(false);
  3002. // Let the planner use the uncorrected coordinates.
  3003. mbl.reset();
  3004. // Reset world2machine_rotation_and_skew and world2machine_shift, therefore
  3005. // the planner will not perform any adjustments in the XY plane.
  3006. // Wait for the motors to stop and update the current position with the absolute values.
  3007. world2machine_revert_to_uncorrected();
  3008. // Move the print head close to the bed.
  3009. current_position[Z_AXIS] = MESH_HOME_Z_SEARCH;
  3010. plan_buffer_line(current_position[X_AXIS], current_position[Y_AXIS],current_position[Z_AXIS] , current_position[E_AXIS], homing_feedrate[Z_AXIS]/40, active_extruder);
  3011. st_synchronize();
  3012. // Home in the XY plane.
  3013. set_destination_to_current();
  3014. setup_for_endstop_move();
  3015. home_xy();
  3016. int8_t verbosity_level = 0;
  3017. if (code_seen('V')) {
  3018. // Just 'V' without a number counts as V1.
  3019. char c = strchr_pointer[1];
  3020. verbosity_level = (c == ' ' || c == '\t' || c == 0) ? 1 : code_value_short();
  3021. }
  3022. bool success = scan_bed_induction_points(verbosity_level);
  3023. clean_up_after_endstop_move();
  3024. // Print head up.
  3025. current_position[Z_AXIS] = MESH_HOME_Z_SEARCH;
  3026. plan_buffer_line(current_position[X_AXIS], current_position[Y_AXIS],current_position[Z_AXIS] , current_position[E_AXIS], homing_feedrate[Z_AXIS]/40, active_extruder);
  3027. st_synchronize();
  3028. lcd_update_enable(true);
  3029. break;
  3030. }
  3031. #endif
  3032. // M48 Z-Probe repeatability measurement function.
  3033. //
  3034. // Usage: M48 <n #_samples> <X X_position_for_samples> <Y Y_position_for_samples> <V Verbose_Level> <L legs_of_movement_prior_to_doing_probe>
  3035. //
  3036. // This function assumes the bed has been homed. Specificaly, that a G28 command
  3037. // as been issued prior to invoking the M48 Z-Probe repeatability measurement function.
  3038. // Any information generated by a prior G29 Bed leveling command will be lost and need to be
  3039. // regenerated.
  3040. //
  3041. // The number of samples will default to 10 if not specified. You can use upper or lower case
  3042. // letters for any of the options EXCEPT n. n must be in lower case because Marlin uses a capital
  3043. // N for its communication protocol and will get horribly confused if you send it a capital N.
  3044. //
  3045. #ifdef ENABLE_AUTO_BED_LEVELING
  3046. #ifdef Z_PROBE_REPEATABILITY_TEST
  3047. case 48: // M48 Z-Probe repeatability
  3048. {
  3049. #if Z_MIN_PIN == -1
  3050. #error "You must have a Z_MIN endstop in order to enable calculation of Z-Probe repeatability."
  3051. #endif
  3052. double sum=0.0;
  3053. double mean=0.0;
  3054. double sigma=0.0;
  3055. double sample_set[50];
  3056. int verbose_level=1, n=0, j, n_samples = 10, n_legs=0;
  3057. double X_current, Y_current, Z_current;
  3058. double X_probe_location, Y_probe_location, Z_start_location, ext_position;
  3059. if (code_seen('V') || code_seen('v')) {
  3060. verbose_level = code_value();
  3061. if (verbose_level<0 || verbose_level>4 ) {
  3062. SERIAL_PROTOCOLPGM("?Verbose Level not plausable.\n");
  3063. goto Sigma_Exit;
  3064. }
  3065. }
  3066. if (verbose_level > 0) {
  3067. SERIAL_PROTOCOLPGM("M48 Z-Probe Repeatability test. Version 2.00\n");
  3068. SERIAL_PROTOCOLPGM("Full support at: http://3dprintboard.com/forum.php\n");
  3069. }
  3070. if (code_seen('n')) {
  3071. n_samples = code_value();
  3072. if (n_samples<4 || n_samples>50 ) {
  3073. SERIAL_PROTOCOLPGM("?Specified sample size not plausable.\n");
  3074. goto Sigma_Exit;
  3075. }
  3076. }
  3077. X_current = X_probe_location = st_get_position_mm(X_AXIS);
  3078. Y_current = Y_probe_location = st_get_position_mm(Y_AXIS);
  3079. Z_current = st_get_position_mm(Z_AXIS);
  3080. Z_start_location = st_get_position_mm(Z_AXIS) + Z_RAISE_BEFORE_PROBING;
  3081. ext_position = st_get_position_mm(E_AXIS);
  3082. if (code_seen('X') || code_seen('x') ) {
  3083. X_probe_location = code_value() - X_PROBE_OFFSET_FROM_EXTRUDER;
  3084. if (X_probe_location<X_MIN_POS || X_probe_location>X_MAX_POS ) {
  3085. SERIAL_PROTOCOLPGM("?Specified X position out of range.\n");
  3086. goto Sigma_Exit;
  3087. }
  3088. }
  3089. if (code_seen('Y') || code_seen('y') ) {
  3090. Y_probe_location = code_value() - Y_PROBE_OFFSET_FROM_EXTRUDER;
  3091. if (Y_probe_location<Y_MIN_POS || Y_probe_location>Y_MAX_POS ) {
  3092. SERIAL_PROTOCOLPGM("?Specified Y position out of range.\n");
  3093. goto Sigma_Exit;
  3094. }
  3095. }
  3096. if (code_seen('L') || code_seen('l') ) {
  3097. n_legs = code_value();
  3098. if ( n_legs==1 )
  3099. n_legs = 2;
  3100. if ( n_legs<0 || n_legs>15 ) {
  3101. SERIAL_PROTOCOLPGM("?Specified number of legs in movement not plausable.\n");
  3102. goto Sigma_Exit;
  3103. }
  3104. }
  3105. //
  3106. // Do all the preliminary setup work. First raise the probe.
  3107. //
  3108. st_synchronize();
  3109. plan_bed_level_matrix.set_to_identity();
  3110. plan_buffer_line( X_current, Y_current, Z_start_location,
  3111. ext_position,
  3112. homing_feedrate[Z_AXIS]/60,
  3113. active_extruder);
  3114. st_synchronize();
  3115. //
  3116. // Now get everything to the specified probe point So we can safely do a probe to
  3117. // get us close to the bed. If the Z-Axis is far from the bed, we don't want to
  3118. // use that as a starting point for each probe.
  3119. //
  3120. if (verbose_level > 2)
  3121. SERIAL_PROTOCOL("Positioning probe for the test.\n");
  3122. plan_buffer_line( X_probe_location, Y_probe_location, Z_start_location,
  3123. ext_position,
  3124. homing_feedrate[X_AXIS]/60,
  3125. active_extruder);
  3126. st_synchronize();
  3127. current_position[X_AXIS] = X_current = st_get_position_mm(X_AXIS);
  3128. current_position[Y_AXIS] = Y_current = st_get_position_mm(Y_AXIS);
  3129. current_position[Z_AXIS] = Z_current = st_get_position_mm(Z_AXIS);
  3130. current_position[E_AXIS] = ext_position = st_get_position_mm(E_AXIS);
  3131. //
  3132. // OK, do the inital probe to get us close to the bed.
  3133. // Then retrace the right amount and use that in subsequent probes
  3134. //
  3135. setup_for_endstop_move();
  3136. run_z_probe();
  3137. current_position[Z_AXIS] = Z_current = st_get_position_mm(Z_AXIS);
  3138. Z_start_location = st_get_position_mm(Z_AXIS) + Z_RAISE_BEFORE_PROBING;
  3139. plan_buffer_line( X_probe_location, Y_probe_location, Z_start_location,
  3140. ext_position,
  3141. homing_feedrate[X_AXIS]/60,
  3142. active_extruder);
  3143. st_synchronize();
  3144. current_position[Z_AXIS] = Z_current = st_get_position_mm(Z_AXIS);
  3145. for( n=0; n<n_samples; n++) {
  3146. do_blocking_move_to( X_probe_location, Y_probe_location, Z_start_location); // Make sure we are at the probe location
  3147. if ( n_legs) {
  3148. double radius=0.0, theta=0.0, x_sweep, y_sweep;
  3149. int rotational_direction, l;
  3150. rotational_direction = (unsigned long) millis() & 0x0001; // clockwise or counter clockwise
  3151. radius = (unsigned long) millis() % (long) (X_MAX_LENGTH/4); // limit how far out to go
  3152. theta = (float) ((unsigned long) millis() % (long) 360) / (360./(2*3.1415926)); // turn into radians
  3153. //SERIAL_ECHOPAIR("starting radius: ",radius);
  3154. //SERIAL_ECHOPAIR(" theta: ",theta);
  3155. //SERIAL_ECHOPAIR(" direction: ",rotational_direction);
  3156. //SERIAL_PROTOCOLLNPGM("");
  3157. for( l=0; l<n_legs-1; l++) {
  3158. if (rotational_direction==1)
  3159. theta += (float) ((unsigned long) millis() % (long) 20) / (360.0/(2*3.1415926)); // turn into radians
  3160. else
  3161. theta -= (float) ((unsigned long) millis() % (long) 20) / (360.0/(2*3.1415926)); // turn into radians
  3162. radius += (float) ( ((long) ((unsigned long) millis() % (long) 10)) - 5);
  3163. if ( radius<0.0 )
  3164. radius = -radius;
  3165. X_current = X_probe_location + cos(theta) * radius;
  3166. Y_current = Y_probe_location + sin(theta) * radius;
  3167. if ( X_current<X_MIN_POS) // Make sure our X & Y are sane
  3168. X_current = X_MIN_POS;
  3169. if ( X_current>X_MAX_POS)
  3170. X_current = X_MAX_POS;
  3171. if ( Y_current<Y_MIN_POS) // Make sure our X & Y are sane
  3172. Y_current = Y_MIN_POS;
  3173. if ( Y_current>Y_MAX_POS)
  3174. Y_current = Y_MAX_POS;
  3175. if (verbose_level>3 ) {
  3176. SERIAL_ECHOPAIR("x: ", X_current);
  3177. SERIAL_ECHOPAIR("y: ", Y_current);
  3178. SERIAL_PROTOCOLLNPGM("");
  3179. }
  3180. do_blocking_move_to( X_current, Y_current, Z_current );
  3181. }
  3182. do_blocking_move_to( X_probe_location, Y_probe_location, Z_start_location); // Go back to the probe location
  3183. }
  3184. setup_for_endstop_move();
  3185. run_z_probe();
  3186. sample_set[n] = current_position[Z_AXIS];
  3187. //
  3188. // Get the current mean for the data points we have so far
  3189. //
  3190. sum=0.0;
  3191. for( j=0; j<=n; j++) {
  3192. sum = sum + sample_set[j];
  3193. }
  3194. mean = sum / (double (n+1));
  3195. //
  3196. // Now, use that mean to calculate the standard deviation for the
  3197. // data points we have so far
  3198. //
  3199. sum=0.0;
  3200. for( j=0; j<=n; j++) {
  3201. sum = sum + (sample_set[j]-mean) * (sample_set[j]-mean);
  3202. }
  3203. sigma = sqrt( sum / (double (n+1)) );
  3204. if (verbose_level > 1) {
  3205. SERIAL_PROTOCOL(n+1);
  3206. SERIAL_PROTOCOL(" of ");
  3207. SERIAL_PROTOCOL(n_samples);
  3208. SERIAL_PROTOCOLPGM(" z: ");
  3209. SERIAL_PROTOCOL_F(current_position[Z_AXIS], 6);
  3210. }
  3211. if (verbose_level > 2) {
  3212. SERIAL_PROTOCOL(" mean: ");
  3213. SERIAL_PROTOCOL_F(mean,6);
  3214. SERIAL_PROTOCOL(" sigma: ");
  3215. SERIAL_PROTOCOL_F(sigma,6);
  3216. }
  3217. if (verbose_level > 0)
  3218. SERIAL_PROTOCOLPGM("\n");
  3219. plan_buffer_line( X_probe_location, Y_probe_location, Z_start_location,
  3220. current_position[E_AXIS], homing_feedrate[Z_AXIS]/60, active_extruder);
  3221. st_synchronize();
  3222. }
  3223. delay(1000);
  3224. clean_up_after_endstop_move();
  3225. // enable_endstops(true);
  3226. if (verbose_level > 0) {
  3227. SERIAL_PROTOCOLPGM("Mean: ");
  3228. SERIAL_PROTOCOL_F(mean, 6);
  3229. SERIAL_PROTOCOLPGM("\n");
  3230. }
  3231. SERIAL_PROTOCOLPGM("Standard Deviation: ");
  3232. SERIAL_PROTOCOL_F(sigma, 6);
  3233. SERIAL_PROTOCOLPGM("\n\n");
  3234. Sigma_Exit:
  3235. break;
  3236. }
  3237. #endif // Z_PROBE_REPEATABILITY_TEST
  3238. #endif // ENABLE_AUTO_BED_LEVELING
  3239. case 104: // M104
  3240. if(setTargetedHotend(104)){
  3241. break;
  3242. }
  3243. if (code_seen('S')) setTargetHotend(code_value(), tmp_extruder);
  3244. setWatch();
  3245. break;
  3246. case 112: // M112 -Emergency Stop
  3247. kill();
  3248. break;
  3249. case 140: // M140 set bed temp
  3250. if (code_seen('S')) setTargetBed(code_value());
  3251. break;
  3252. case 105 : // M105
  3253. if(setTargetedHotend(105)){
  3254. break;
  3255. }
  3256. #if defined(TEMP_0_PIN) && TEMP_0_PIN > -1
  3257. SERIAL_PROTOCOLPGM("ok T:");
  3258. SERIAL_PROTOCOL_F(degHotend(tmp_extruder),1);
  3259. SERIAL_PROTOCOLPGM(" /");
  3260. SERIAL_PROTOCOL_F(degTargetHotend(tmp_extruder),1);
  3261. #if defined(TEMP_BED_PIN) && TEMP_BED_PIN > -1
  3262. SERIAL_PROTOCOLPGM(" B:");
  3263. SERIAL_PROTOCOL_F(degBed(),1);
  3264. SERIAL_PROTOCOLPGM(" /");
  3265. SERIAL_PROTOCOL_F(degTargetBed(),1);
  3266. #endif //TEMP_BED_PIN
  3267. for (int8_t cur_extruder = 0; cur_extruder < EXTRUDERS; ++cur_extruder) {
  3268. SERIAL_PROTOCOLPGM(" T");
  3269. SERIAL_PROTOCOL(cur_extruder);
  3270. SERIAL_PROTOCOLPGM(":");
  3271. SERIAL_PROTOCOL_F(degHotend(cur_extruder),1);
  3272. SERIAL_PROTOCOLPGM(" /");
  3273. SERIAL_PROTOCOL_F(degTargetHotend(cur_extruder),1);
  3274. }
  3275. #else
  3276. SERIAL_ERROR_START;
  3277. SERIAL_ERRORLNRPGM(MSG_ERR_NO_THERMISTORS);
  3278. #endif
  3279. SERIAL_PROTOCOLPGM(" @:");
  3280. #ifdef EXTRUDER_WATTS
  3281. SERIAL_PROTOCOL((EXTRUDER_WATTS * getHeaterPower(tmp_extruder))/127);
  3282. SERIAL_PROTOCOLPGM("W");
  3283. #else
  3284. SERIAL_PROTOCOL(getHeaterPower(tmp_extruder));
  3285. #endif
  3286. SERIAL_PROTOCOLPGM(" B@:");
  3287. #ifdef BED_WATTS
  3288. SERIAL_PROTOCOL((BED_WATTS * getHeaterPower(-1))/127);
  3289. SERIAL_PROTOCOLPGM("W");
  3290. #else
  3291. SERIAL_PROTOCOL(getHeaterPower(-1));
  3292. #endif
  3293. #ifdef SHOW_TEMP_ADC_VALUES
  3294. #if defined(TEMP_BED_PIN) && TEMP_BED_PIN > -1
  3295. SERIAL_PROTOCOLPGM(" ADC B:");
  3296. SERIAL_PROTOCOL_F(degBed(),1);
  3297. SERIAL_PROTOCOLPGM("C->");
  3298. SERIAL_PROTOCOL_F(rawBedTemp()/OVERSAMPLENR,0);
  3299. #endif
  3300. for (int8_t cur_extruder = 0; cur_extruder < EXTRUDERS; ++cur_extruder) {
  3301. SERIAL_PROTOCOLPGM(" T");
  3302. SERIAL_PROTOCOL(cur_extruder);
  3303. SERIAL_PROTOCOLPGM(":");
  3304. SERIAL_PROTOCOL_F(degHotend(cur_extruder),1);
  3305. SERIAL_PROTOCOLPGM("C->");
  3306. SERIAL_PROTOCOL_F(rawHotendTemp(cur_extruder)/OVERSAMPLENR,0);
  3307. }
  3308. #endif
  3309. SERIAL_PROTOCOLLN("");
  3310. return;
  3311. break;
  3312. case 109:
  3313. {// M109 - Wait for extruder heater to reach target.
  3314. if(setTargetedHotend(109)){
  3315. break;
  3316. }
  3317. LCD_MESSAGERPGM(MSG_HEATING);
  3318. heating_status = 1;
  3319. if (farm_mode) { prusa_statistics(1); };
  3320. #ifdef AUTOTEMP
  3321. autotemp_enabled=false;
  3322. #endif
  3323. if (code_seen('S')) {
  3324. setTargetHotend(code_value(), tmp_extruder);
  3325. CooldownNoWait = true;
  3326. } else if (code_seen('R')) {
  3327. setTargetHotend(code_value(), tmp_extruder);
  3328. CooldownNoWait = false;
  3329. }
  3330. #ifdef AUTOTEMP
  3331. if (code_seen('S')) autotemp_min=code_value();
  3332. if (code_seen('B')) autotemp_max=code_value();
  3333. if (code_seen('F'))
  3334. {
  3335. autotemp_factor=code_value();
  3336. autotemp_enabled=true;
  3337. }
  3338. #endif
  3339. setWatch();
  3340. codenum = millis();
  3341. /* See if we are heating up or cooling down */
  3342. target_direction = isHeatingHotend(tmp_extruder); // true if heating, false if cooling
  3343. cancel_heatup = false;
  3344. #ifdef TEMP_RESIDENCY_TIME
  3345. long residencyStart;
  3346. residencyStart = -1;
  3347. /* continue to loop until we have reached the target temp
  3348. _and_ until TEMP_RESIDENCY_TIME hasn't passed since we reached it */
  3349. while((!cancel_heatup)&&((residencyStart == -1) ||
  3350. (residencyStart >= 0 && (((unsigned int) (millis() - residencyStart)) < (TEMP_RESIDENCY_TIME * 1000UL)))) ) {
  3351. #else
  3352. while ( target_direction ? (isHeatingHotend(tmp_extruder)) : (isCoolingHotend(tmp_extruder)&&(CooldownNoWait==false)) ) {
  3353. #endif //TEMP_RESIDENCY_TIME
  3354. if( (millis() - codenum) > 1000UL )
  3355. { //Print Temp Reading and remaining time every 1 second while heating up/cooling down
  3356. if (!farm_mode) {
  3357. SERIAL_PROTOCOLPGM("T:");
  3358. SERIAL_PROTOCOL_F(degHotend(tmp_extruder), 1);
  3359. SERIAL_PROTOCOLPGM(" E:");
  3360. SERIAL_PROTOCOL((int)tmp_extruder);
  3361. #ifdef TEMP_RESIDENCY_TIME
  3362. SERIAL_PROTOCOLPGM(" W:");
  3363. if (residencyStart > -1)
  3364. {
  3365. codenum = ((TEMP_RESIDENCY_TIME * 1000UL) - (millis() - residencyStart)) / 1000UL;
  3366. SERIAL_PROTOCOLLN(codenum);
  3367. }
  3368. else
  3369. {
  3370. SERIAL_PROTOCOLLN("?");
  3371. }
  3372. }
  3373. #else
  3374. SERIAL_PROTOCOLLN("");
  3375. #endif
  3376. codenum = millis();
  3377. }
  3378. manage_heater();
  3379. manage_inactivity();
  3380. lcd_update();
  3381. #ifdef TEMP_RESIDENCY_TIME
  3382. /* start/restart the TEMP_RESIDENCY_TIME timer whenever we reach target temp for the first time
  3383. or when current temp falls outside the hysteresis after target temp was reached */
  3384. if ((residencyStart == -1 && target_direction && (degHotend(tmp_extruder) >= (degTargetHotend(tmp_extruder)-TEMP_WINDOW))) ||
  3385. (residencyStart == -1 && !target_direction && (degHotend(tmp_extruder) <= (degTargetHotend(tmp_extruder)+TEMP_WINDOW))) ||
  3386. (residencyStart > -1 && labs(degHotend(tmp_extruder) - degTargetHotend(tmp_extruder)) > TEMP_HYSTERESIS) )
  3387. {
  3388. residencyStart = millis();
  3389. }
  3390. #endif //TEMP_RESIDENCY_TIME
  3391. }
  3392. LCD_MESSAGERPGM(MSG_HEATING_COMPLETE);
  3393. heating_status = 2;
  3394. if (farm_mode) { prusa_statistics(2); };
  3395. starttime=millis();
  3396. previous_millis_cmd = millis();
  3397. }
  3398. break;
  3399. case 190: // M190 - Wait for bed heater to reach target.
  3400. #if defined(TEMP_BED_PIN) && TEMP_BED_PIN > -1
  3401. LCD_MESSAGERPGM(MSG_BED_HEATING);
  3402. heating_status = 3;
  3403. if (farm_mode) { prusa_statistics(1); };
  3404. if (code_seen('S'))
  3405. {
  3406. setTargetBed(code_value());
  3407. CooldownNoWait = true;
  3408. }
  3409. else if (code_seen('R'))
  3410. {
  3411. setTargetBed(code_value());
  3412. CooldownNoWait = false;
  3413. }
  3414. codenum = millis();
  3415. cancel_heatup = false;
  3416. target_direction = isHeatingBed(); // true if heating, false if cooling
  3417. while ( (target_direction)&&(!cancel_heatup) ? (isHeatingBed()) : (isCoolingBed()&&(CooldownNoWait==false)) )
  3418. {
  3419. if(( millis() - codenum) > 1000 ) //Print Temp Reading every 1 second while heating up.
  3420. {
  3421. if (!farm_mode) {
  3422. float tt = degHotend(active_extruder);
  3423. SERIAL_PROTOCOLPGM("T:");
  3424. SERIAL_PROTOCOL(tt);
  3425. SERIAL_PROTOCOLPGM(" E:");
  3426. SERIAL_PROTOCOL((int)active_extruder);
  3427. SERIAL_PROTOCOLPGM(" B:");
  3428. SERIAL_PROTOCOL_F(degBed(), 1);
  3429. SERIAL_PROTOCOLLN("");
  3430. }
  3431. codenum = millis();
  3432. }
  3433. manage_heater();
  3434. manage_inactivity();
  3435. lcd_update();
  3436. }
  3437. LCD_MESSAGERPGM(MSG_BED_DONE);
  3438. heating_status = 4;
  3439. previous_millis_cmd = millis();
  3440. #endif
  3441. break;
  3442. #if defined(FAN_PIN) && FAN_PIN > -1
  3443. case 106: //M106 Fan On
  3444. if (code_seen('S')){
  3445. fanSpeed=constrain(code_value(),0,255);
  3446. }
  3447. else {
  3448. fanSpeed=255;
  3449. }
  3450. break;
  3451. case 107: //M107 Fan Off
  3452. fanSpeed = 0;
  3453. break;
  3454. #endif //FAN_PIN
  3455. #if defined(PS_ON_PIN) && PS_ON_PIN > -1
  3456. case 80: // M80 - Turn on Power Supply
  3457. SET_OUTPUT(PS_ON_PIN); //GND
  3458. WRITE(PS_ON_PIN, PS_ON_AWAKE);
  3459. // If you have a switch on suicide pin, this is useful
  3460. // if you want to start another print with suicide feature after
  3461. // a print without suicide...
  3462. #if defined SUICIDE_PIN && SUICIDE_PIN > -1
  3463. SET_OUTPUT(SUICIDE_PIN);
  3464. WRITE(SUICIDE_PIN, HIGH);
  3465. #endif
  3466. #ifdef ULTIPANEL
  3467. powersupply = true;
  3468. LCD_MESSAGERPGM(WELCOME_MSG);
  3469. lcd_update();
  3470. #endif
  3471. break;
  3472. #endif
  3473. case 81: // M81 - Turn off Power Supply
  3474. disable_heater();
  3475. st_synchronize();
  3476. disable_e0();
  3477. disable_e1();
  3478. disable_e2();
  3479. finishAndDisableSteppers();
  3480. fanSpeed = 0;
  3481. delay(1000); // Wait a little before to switch off
  3482. #if defined(SUICIDE_PIN) && SUICIDE_PIN > -1
  3483. st_synchronize();
  3484. suicide();
  3485. #elif defined(PS_ON_PIN) && PS_ON_PIN > -1
  3486. SET_OUTPUT(PS_ON_PIN);
  3487. WRITE(PS_ON_PIN, PS_ON_ASLEEP);
  3488. #endif
  3489. #ifdef ULTIPANEL
  3490. powersupply = false;
  3491. LCD_MESSAGERPGM(CAT4(CUSTOM_MENDEL_NAME,PSTR(" "),MSG_OFF,PSTR("."))); //!!
  3492. /*
  3493. MACHNAME = "Prusa i3"
  3494. MSGOFF = "Vypnuto"
  3495. "Prusai3"" ""vypnuto""."
  3496. "Prusa i3"" "MSG_ALL[lang_selected][50]"."
  3497. */
  3498. lcd_update();
  3499. #endif
  3500. break;
  3501. case 82:
  3502. axis_relative_modes[3] = false;
  3503. break;
  3504. case 83:
  3505. axis_relative_modes[3] = true;
  3506. break;
  3507. case 18: //compatibility
  3508. case 84: // M84
  3509. if(code_seen('S')){
  3510. stepper_inactive_time = code_value() * 1000;
  3511. }
  3512. else
  3513. {
  3514. 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])));
  3515. if(all_axis)
  3516. {
  3517. st_synchronize();
  3518. disable_e0();
  3519. disable_e1();
  3520. disable_e2();
  3521. finishAndDisableSteppers();
  3522. }
  3523. else
  3524. {
  3525. st_synchronize();
  3526. if(code_seen('X')) disable_x();
  3527. if(code_seen('Y')) disable_y();
  3528. if(code_seen('Z')) disable_z();
  3529. #if ((E0_ENABLE_PIN != X_ENABLE_PIN) && (E1_ENABLE_PIN != Y_ENABLE_PIN)) // Only enable on boards that have seperate ENABLE_PINS
  3530. if(code_seen('E')) {
  3531. disable_e0();
  3532. disable_e1();
  3533. disable_e2();
  3534. }
  3535. #endif
  3536. }
  3537. }
  3538. break;
  3539. case 85: // M85
  3540. if(code_seen('S')) {
  3541. max_inactive_time = code_value() * 1000;
  3542. }
  3543. break;
  3544. case 92: // M92
  3545. for(int8_t i=0; i < NUM_AXIS; i++)
  3546. {
  3547. if(code_seen(axis_codes[i]))
  3548. {
  3549. if(i == 3) { // E
  3550. float value = code_value();
  3551. if(value < 20.0) {
  3552. float factor = axis_steps_per_unit[i] / value; // increase e constants if M92 E14 is given for netfab.
  3553. max_jerk[E_AXIS] *= factor;
  3554. max_feedrate[i] *= factor;
  3555. axis_steps_per_sqr_second[i] *= factor;
  3556. }
  3557. axis_steps_per_unit[i] = value;
  3558. }
  3559. else {
  3560. axis_steps_per_unit[i] = code_value();
  3561. }
  3562. }
  3563. }
  3564. break;
  3565. case 115: // M115
  3566. if (code_seen('V')) {
  3567. // Report the Prusa version number.
  3568. SERIAL_PROTOCOLLNRPGM(FW_VERSION_STR_P());
  3569. } else if (code_seen('U')) {
  3570. // Check the firmware version provided. If the firmware version provided by the U code is higher than the currently running firmware,
  3571. // pause the print and ask the user to upgrade the firmware.
  3572. show_upgrade_dialog_if_version_newer(++ strchr_pointer);
  3573. } else {
  3574. SERIAL_PROTOCOLRPGM(MSG_M115_REPORT);
  3575. }
  3576. break;
  3577. case 117: // M117 display message
  3578. starpos = (strchr(strchr_pointer + 5,'*'));
  3579. if(starpos!=NULL)
  3580. *(starpos)='\0';
  3581. lcd_setstatus(strchr_pointer + 5);
  3582. break;
  3583. case 114: // M114
  3584. SERIAL_PROTOCOLPGM("X:");
  3585. SERIAL_PROTOCOL(current_position[X_AXIS]);
  3586. SERIAL_PROTOCOLPGM(" Y:");
  3587. SERIAL_PROTOCOL(current_position[Y_AXIS]);
  3588. SERIAL_PROTOCOLPGM(" Z:");
  3589. SERIAL_PROTOCOL(current_position[Z_AXIS]);
  3590. SERIAL_PROTOCOLPGM(" E:");
  3591. SERIAL_PROTOCOL(current_position[E_AXIS]);
  3592. SERIAL_PROTOCOLRPGM(MSG_COUNT_X);
  3593. SERIAL_PROTOCOL(float(st_get_position(X_AXIS))/axis_steps_per_unit[X_AXIS]);
  3594. SERIAL_PROTOCOLPGM(" Y:");
  3595. SERIAL_PROTOCOL(float(st_get_position(Y_AXIS))/axis_steps_per_unit[Y_AXIS]);
  3596. SERIAL_PROTOCOLPGM(" Z:");
  3597. SERIAL_PROTOCOL(float(st_get_position(Z_AXIS))/axis_steps_per_unit[Z_AXIS]);
  3598. SERIAL_PROTOCOLLN("");
  3599. break;
  3600. case 120: // M120
  3601. enable_endstops(false) ;
  3602. break;
  3603. case 121: // M121
  3604. enable_endstops(true) ;
  3605. break;
  3606. case 119: // M119
  3607. SERIAL_PROTOCOLRPGM(MSG_M119_REPORT);
  3608. SERIAL_PROTOCOLLN("");
  3609. #if defined(X_MIN_PIN) && X_MIN_PIN > -1
  3610. SERIAL_PROTOCOLRPGM(MSG_X_MIN);
  3611. if(READ(X_MIN_PIN)^X_MIN_ENDSTOP_INVERTING){
  3612. SERIAL_PROTOCOLRPGM(MSG_ENDSTOP_HIT);
  3613. }else{
  3614. SERIAL_PROTOCOLRPGM(MSG_ENDSTOP_OPEN);
  3615. }
  3616. SERIAL_PROTOCOLLN("");
  3617. #endif
  3618. #if defined(X_MAX_PIN) && X_MAX_PIN > -1
  3619. SERIAL_PROTOCOLRPGM(MSG_X_MAX);
  3620. if(READ(X_MAX_PIN)^X_MAX_ENDSTOP_INVERTING){
  3621. SERIAL_PROTOCOLRPGM(MSG_ENDSTOP_HIT);
  3622. }else{
  3623. SERIAL_PROTOCOLRPGM(MSG_ENDSTOP_OPEN);
  3624. }
  3625. SERIAL_PROTOCOLLN("");
  3626. #endif
  3627. #if defined(Y_MIN_PIN) && Y_MIN_PIN > -1
  3628. SERIAL_PROTOCOLRPGM(MSG_Y_MIN);
  3629. if(READ(Y_MIN_PIN)^Y_MIN_ENDSTOP_INVERTING){
  3630. SERIAL_PROTOCOLRPGM(MSG_ENDSTOP_HIT);
  3631. }else{
  3632. SERIAL_PROTOCOLRPGM(MSG_ENDSTOP_OPEN);
  3633. }
  3634. SERIAL_PROTOCOLLN("");
  3635. #endif
  3636. #if defined(Y_MAX_PIN) && Y_MAX_PIN > -1
  3637. SERIAL_PROTOCOLRPGM(MSG_Y_MAX);
  3638. if(READ(Y_MAX_PIN)^Y_MAX_ENDSTOP_INVERTING){
  3639. SERIAL_PROTOCOLRPGM(MSG_ENDSTOP_HIT);
  3640. }else{
  3641. SERIAL_PROTOCOLRPGM(MSG_ENDSTOP_OPEN);
  3642. }
  3643. SERIAL_PROTOCOLLN("");
  3644. #endif
  3645. #if defined(Z_MIN_PIN) && Z_MIN_PIN > -1
  3646. SERIAL_PROTOCOLRPGM(MSG_Z_MIN);
  3647. if(READ(Z_MIN_PIN)^Z_MIN_ENDSTOP_INVERTING){
  3648. SERIAL_PROTOCOLRPGM(MSG_ENDSTOP_HIT);
  3649. }else{
  3650. SERIAL_PROTOCOLRPGM(MSG_ENDSTOP_OPEN);
  3651. }
  3652. SERIAL_PROTOCOLLN("");
  3653. #endif
  3654. #if defined(Z_MAX_PIN) && Z_MAX_PIN > -1
  3655. SERIAL_PROTOCOLRPGM(MSG_Z_MAX);
  3656. if(READ(Z_MAX_PIN)^Z_MAX_ENDSTOP_INVERTING){
  3657. SERIAL_PROTOCOLRPGM(MSG_ENDSTOP_HIT);
  3658. }else{
  3659. SERIAL_PROTOCOLRPGM(MSG_ENDSTOP_OPEN);
  3660. }
  3661. SERIAL_PROTOCOLLN("");
  3662. #endif
  3663. break;
  3664. //TODO: update for all axis, use for loop
  3665. #ifdef BLINKM
  3666. case 150: // M150
  3667. {
  3668. byte red;
  3669. byte grn;
  3670. byte blu;
  3671. if(code_seen('R')) red = code_value();
  3672. if(code_seen('U')) grn = code_value();
  3673. if(code_seen('B')) blu = code_value();
  3674. SendColors(red,grn,blu);
  3675. }
  3676. break;
  3677. #endif //BLINKM
  3678. case 200: // M200 D<millimeters> set filament diameter and set E axis units to cubic millimeters (use S0 to set back to millimeters).
  3679. {
  3680. tmp_extruder = active_extruder;
  3681. if(code_seen('T')) {
  3682. tmp_extruder = code_value();
  3683. if(tmp_extruder >= EXTRUDERS) {
  3684. SERIAL_ECHO_START;
  3685. SERIAL_ECHO(MSG_M200_INVALID_EXTRUDER);
  3686. break;
  3687. }
  3688. }
  3689. float area = .0;
  3690. if(code_seen('D')) {
  3691. float diameter = (float)code_value();
  3692. if (diameter == 0.0) {
  3693. // setting any extruder filament size disables volumetric on the assumption that
  3694. // slicers either generate in extruder values as cubic mm or as as filament feeds
  3695. // for all extruders
  3696. volumetric_enabled = false;
  3697. } else {
  3698. filament_size[tmp_extruder] = (float)code_value();
  3699. // make sure all extruders have some sane value for the filament size
  3700. filament_size[0] = (filament_size[0] == 0.0 ? DEFAULT_NOMINAL_FILAMENT_DIA : filament_size[0]);
  3701. #if EXTRUDERS > 1
  3702. filament_size[1] = (filament_size[1] == 0.0 ? DEFAULT_NOMINAL_FILAMENT_DIA : filament_size[1]);
  3703. #if EXTRUDERS > 2
  3704. filament_size[2] = (filament_size[2] == 0.0 ? DEFAULT_NOMINAL_FILAMENT_DIA : filament_size[2]);
  3705. #endif
  3706. #endif
  3707. volumetric_enabled = true;
  3708. }
  3709. } else {
  3710. //reserved for setting filament diameter via UFID or filament measuring device
  3711. break;
  3712. }
  3713. calculate_volumetric_multipliers();
  3714. }
  3715. break;
  3716. case 201: // M201
  3717. for(int8_t i=0; i < NUM_AXIS; i++)
  3718. {
  3719. if(code_seen(axis_codes[i]))
  3720. {
  3721. max_acceleration_units_per_sq_second[i] = code_value();
  3722. }
  3723. }
  3724. // 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)
  3725. reset_acceleration_rates();
  3726. break;
  3727. #if 0 // Not used for Sprinter/grbl gen6
  3728. case 202: // M202
  3729. for(int8_t i=0; i < NUM_AXIS; i++) {
  3730. if(code_seen(axis_codes[i])) axis_travel_steps_per_sqr_second[i] = code_value() * axis_steps_per_unit[i];
  3731. }
  3732. break;
  3733. #endif
  3734. case 203: // M203 max feedrate mm/sec
  3735. for(int8_t i=0; i < NUM_AXIS; i++) {
  3736. if(code_seen(axis_codes[i])) max_feedrate[i] = code_value();
  3737. }
  3738. break;
  3739. case 204: // M204 acclereration S normal moves T filmanent only moves
  3740. {
  3741. if(code_seen('S')) acceleration = code_value() ;
  3742. if(code_seen('T')) retract_acceleration = code_value() ;
  3743. }
  3744. break;
  3745. 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
  3746. {
  3747. if(code_seen('S')) minimumfeedrate = code_value();
  3748. if(code_seen('T')) mintravelfeedrate = code_value();
  3749. if(code_seen('B')) minsegmenttime = code_value() ;
  3750. if(code_seen('X')) max_jerk[X_AXIS] = max_jerk[Y_AXIS] = code_value();
  3751. if(code_seen('Y')) max_jerk[Y_AXIS] = code_value();
  3752. if(code_seen('Z')) max_jerk[Z_AXIS] = code_value();
  3753. if(code_seen('E')) max_jerk[E_AXIS] = code_value();
  3754. }
  3755. break;
  3756. case 206: // M206 additional homing offset
  3757. for(int8_t i=0; i < 3; i++)
  3758. {
  3759. if(code_seen(axis_codes[i])) add_homing[i] = code_value();
  3760. }
  3761. break;
  3762. #ifdef FWRETRACT
  3763. case 207: //M207 - set retract length S[positive mm] F[feedrate mm/min] Z[additional zlift/hop]
  3764. {
  3765. if(code_seen('S'))
  3766. {
  3767. retract_length = code_value() ;
  3768. }
  3769. if(code_seen('F'))
  3770. {
  3771. retract_feedrate = code_value()/60 ;
  3772. }
  3773. if(code_seen('Z'))
  3774. {
  3775. retract_zlift = code_value() ;
  3776. }
  3777. }break;
  3778. case 208: // M208 - set retract recover length S[positive mm surplus to the M207 S*] F[feedrate mm/min]
  3779. {
  3780. if(code_seen('S'))
  3781. {
  3782. retract_recover_length = code_value() ;
  3783. }
  3784. if(code_seen('F'))
  3785. {
  3786. retract_recover_feedrate = code_value()/60 ;
  3787. }
  3788. }break;
  3789. 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.
  3790. {
  3791. if(code_seen('S'))
  3792. {
  3793. int t= code_value() ;
  3794. switch(t)
  3795. {
  3796. case 0:
  3797. {
  3798. autoretract_enabled=false;
  3799. retracted[0]=false;
  3800. #if EXTRUDERS > 1
  3801. retracted[1]=false;
  3802. #endif
  3803. #if EXTRUDERS > 2
  3804. retracted[2]=false;
  3805. #endif
  3806. }break;
  3807. case 1:
  3808. {
  3809. autoretract_enabled=true;
  3810. retracted[0]=false;
  3811. #if EXTRUDERS > 1
  3812. retracted[1]=false;
  3813. #endif
  3814. #if EXTRUDERS > 2
  3815. retracted[2]=false;
  3816. #endif
  3817. }break;
  3818. default:
  3819. SERIAL_ECHO_START;
  3820. SERIAL_ECHORPGM(MSG_UNKNOWN_COMMAND);
  3821. SERIAL_ECHO(CMDBUFFER_CURRENT_STRING);
  3822. SERIAL_ECHOLNPGM("\"");
  3823. }
  3824. }
  3825. }break;
  3826. #endif // FWRETRACT
  3827. #if EXTRUDERS > 1
  3828. case 218: // M218 - set hotend offset (in mm), T<extruder_number> X<offset_on_X> Y<offset_on_Y>
  3829. {
  3830. if(setTargetedHotend(218)){
  3831. break;
  3832. }
  3833. if(code_seen('X'))
  3834. {
  3835. extruder_offset[X_AXIS][tmp_extruder] = code_value();
  3836. }
  3837. if(code_seen('Y'))
  3838. {
  3839. extruder_offset[Y_AXIS][tmp_extruder] = code_value();
  3840. }
  3841. SERIAL_ECHO_START;
  3842. SERIAL_ECHORPGM(MSG_HOTEND_OFFSET);
  3843. for(tmp_extruder = 0; tmp_extruder < EXTRUDERS; tmp_extruder++)
  3844. {
  3845. SERIAL_ECHO(" ");
  3846. SERIAL_ECHO(extruder_offset[X_AXIS][tmp_extruder]);
  3847. SERIAL_ECHO(",");
  3848. SERIAL_ECHO(extruder_offset[Y_AXIS][tmp_extruder]);
  3849. }
  3850. SERIAL_ECHOLN("");
  3851. }break;
  3852. #endif
  3853. case 220: // M220 S<factor in percent>- set speed factor override percentage
  3854. {
  3855. if(code_seen('S'))
  3856. {
  3857. feedmultiply = code_value() ;
  3858. }
  3859. }
  3860. break;
  3861. case 221: // M221 S<factor in percent>- set extrude factor override percentage
  3862. {
  3863. if(code_seen('S'))
  3864. {
  3865. int tmp_code = code_value();
  3866. if (code_seen('T'))
  3867. {
  3868. if(setTargetedHotend(221)){
  3869. break;
  3870. }
  3871. extruder_multiply[tmp_extruder] = tmp_code;
  3872. }
  3873. else
  3874. {
  3875. extrudemultiply = tmp_code ;
  3876. }
  3877. }
  3878. }
  3879. break;
  3880. case 226: // M226 P<pin number> S<pin state>- Wait until the specified pin reaches the state required
  3881. {
  3882. if(code_seen('P')){
  3883. int pin_number = code_value(); // pin number
  3884. int pin_state = -1; // required pin state - default is inverted
  3885. if(code_seen('S')) pin_state = code_value(); // required pin state
  3886. if(pin_state >= -1 && pin_state <= 1){
  3887. for(int8_t i = 0; i < (int8_t)(sizeof(sensitive_pins)/sizeof(int)); i++)
  3888. {
  3889. if (sensitive_pins[i] == pin_number)
  3890. {
  3891. pin_number = -1;
  3892. break;
  3893. }
  3894. }
  3895. if (pin_number > -1)
  3896. {
  3897. int target = LOW;
  3898. st_synchronize();
  3899. pinMode(pin_number, INPUT);
  3900. switch(pin_state){
  3901. case 1:
  3902. target = HIGH;
  3903. break;
  3904. case 0:
  3905. target = LOW;
  3906. break;
  3907. case -1:
  3908. target = !digitalRead(pin_number);
  3909. break;
  3910. }
  3911. while(digitalRead(pin_number) != target){
  3912. manage_heater();
  3913. manage_inactivity();
  3914. lcd_update();
  3915. }
  3916. }
  3917. }
  3918. }
  3919. }
  3920. break;
  3921. #if NUM_SERVOS > 0
  3922. case 280: // M280 - set servo position absolute. P: servo index, S: angle or microseconds
  3923. {
  3924. int servo_index = -1;
  3925. int servo_position = 0;
  3926. if (code_seen('P'))
  3927. servo_index = code_value();
  3928. if (code_seen('S')) {
  3929. servo_position = code_value();
  3930. if ((servo_index >= 0) && (servo_index < NUM_SERVOS)) {
  3931. #if defined (ENABLE_AUTO_BED_LEVELING) && (PROBE_SERVO_DEACTIVATION_DELAY > 0)
  3932. servos[servo_index].attach(0);
  3933. #endif
  3934. servos[servo_index].write(servo_position);
  3935. #if defined (ENABLE_AUTO_BED_LEVELING) && (PROBE_SERVO_DEACTIVATION_DELAY > 0)
  3936. delay(PROBE_SERVO_DEACTIVATION_DELAY);
  3937. servos[servo_index].detach();
  3938. #endif
  3939. }
  3940. else {
  3941. SERIAL_ECHO_START;
  3942. SERIAL_ECHO("Servo ");
  3943. SERIAL_ECHO(servo_index);
  3944. SERIAL_ECHOLN(" out of range");
  3945. }
  3946. }
  3947. else if (servo_index >= 0) {
  3948. SERIAL_PROTOCOL(MSG_OK);
  3949. SERIAL_PROTOCOL(" Servo ");
  3950. SERIAL_PROTOCOL(servo_index);
  3951. SERIAL_PROTOCOL(": ");
  3952. SERIAL_PROTOCOL(servos[servo_index].read());
  3953. SERIAL_PROTOCOLLN("");
  3954. }
  3955. }
  3956. break;
  3957. #endif // NUM_SERVOS > 0
  3958. #if (LARGE_FLASH == true && ( BEEPER > 0 || defined(ULTRALCD) || defined(LCD_USE_I2C_BUZZER)))
  3959. case 300: // M300
  3960. {
  3961. int beepS = code_seen('S') ? code_value() : 110;
  3962. int beepP = code_seen('P') ? code_value() : 1000;
  3963. if (beepS > 0)
  3964. {
  3965. #if BEEPER > 0
  3966. tone(BEEPER, beepS);
  3967. delay(beepP);
  3968. noTone(BEEPER);
  3969. #elif defined(ULTRALCD)
  3970. lcd_buzz(beepS, beepP);
  3971. #elif defined(LCD_USE_I2C_BUZZER)
  3972. lcd_buzz(beepP, beepS);
  3973. #endif
  3974. }
  3975. else
  3976. {
  3977. delay(beepP);
  3978. }
  3979. }
  3980. break;
  3981. #endif // M300
  3982. #ifdef PIDTEMP
  3983. case 301: // M301
  3984. {
  3985. if(code_seen('P')) Kp = code_value();
  3986. if(code_seen('I')) Ki = scalePID_i(code_value());
  3987. if(code_seen('D')) Kd = scalePID_d(code_value());
  3988. #ifdef PID_ADD_EXTRUSION_RATE
  3989. if(code_seen('C')) Kc = code_value();
  3990. #endif
  3991. updatePID();
  3992. SERIAL_PROTOCOLRPGM(MSG_OK);
  3993. SERIAL_PROTOCOL(" p:");
  3994. SERIAL_PROTOCOL(Kp);
  3995. SERIAL_PROTOCOL(" i:");
  3996. SERIAL_PROTOCOL(unscalePID_i(Ki));
  3997. SERIAL_PROTOCOL(" d:");
  3998. SERIAL_PROTOCOL(unscalePID_d(Kd));
  3999. #ifdef PID_ADD_EXTRUSION_RATE
  4000. SERIAL_PROTOCOL(" c:");
  4001. //Kc does not have scaling applied above, or in resetting defaults
  4002. SERIAL_PROTOCOL(Kc);
  4003. #endif
  4004. SERIAL_PROTOCOLLN("");
  4005. }
  4006. break;
  4007. #endif //PIDTEMP
  4008. #ifdef PIDTEMPBED
  4009. case 304: // M304
  4010. {
  4011. if(code_seen('P')) bedKp = code_value();
  4012. if(code_seen('I')) bedKi = scalePID_i(code_value());
  4013. if(code_seen('D')) bedKd = scalePID_d(code_value());
  4014. updatePID();
  4015. SERIAL_PROTOCOLRPGM(MSG_OK);
  4016. SERIAL_PROTOCOL(" p:");
  4017. SERIAL_PROTOCOL(bedKp);
  4018. SERIAL_PROTOCOL(" i:");
  4019. SERIAL_PROTOCOL(unscalePID_i(bedKi));
  4020. SERIAL_PROTOCOL(" d:");
  4021. SERIAL_PROTOCOL(unscalePID_d(bedKd));
  4022. SERIAL_PROTOCOLLN("");
  4023. }
  4024. break;
  4025. #endif //PIDTEMP
  4026. case 240: // M240 Triggers a camera by emulating a Canon RC-1 : http://www.doc-diy.net/photo/rc-1_hacked/
  4027. {
  4028. #ifdef CHDK
  4029. SET_OUTPUT(CHDK);
  4030. WRITE(CHDK, HIGH);
  4031. chdkHigh = millis();
  4032. chdkActive = true;
  4033. #else
  4034. #if defined(PHOTOGRAPH_PIN) && PHOTOGRAPH_PIN > -1
  4035. const uint8_t NUM_PULSES=16;
  4036. const float PULSE_LENGTH=0.01524;
  4037. for(int i=0; i < NUM_PULSES; i++) {
  4038. WRITE(PHOTOGRAPH_PIN, HIGH);
  4039. _delay_ms(PULSE_LENGTH);
  4040. WRITE(PHOTOGRAPH_PIN, LOW);
  4041. _delay_ms(PULSE_LENGTH);
  4042. }
  4043. delay(7.33);
  4044. for(int i=0; i < NUM_PULSES; i++) {
  4045. WRITE(PHOTOGRAPH_PIN, HIGH);
  4046. _delay_ms(PULSE_LENGTH);
  4047. WRITE(PHOTOGRAPH_PIN, LOW);
  4048. _delay_ms(PULSE_LENGTH);
  4049. }
  4050. #endif
  4051. #endif //chdk end if
  4052. }
  4053. break;
  4054. #ifdef DOGLCD
  4055. case 250: // M250 Set LCD contrast value: C<value> (value 0..63)
  4056. {
  4057. if (code_seen('C')) {
  4058. lcd_setcontrast( ((int)code_value())&63 );
  4059. }
  4060. SERIAL_PROTOCOLPGM("lcd contrast value: ");
  4061. SERIAL_PROTOCOL(lcd_contrast);
  4062. SERIAL_PROTOCOLLN("");
  4063. }
  4064. break;
  4065. #endif
  4066. #ifdef PREVENT_DANGEROUS_EXTRUDE
  4067. case 302: // allow cold extrudes, or set the minimum extrude temperature
  4068. {
  4069. float temp = .0;
  4070. if (code_seen('S')) temp=code_value();
  4071. set_extrude_min_temp(temp);
  4072. }
  4073. break;
  4074. #endif
  4075. case 303: // M303 PID autotune
  4076. {
  4077. float temp = 150.0;
  4078. int e=0;
  4079. int c=5;
  4080. if (code_seen('E')) e=code_value();
  4081. if (e<0)
  4082. temp=70;
  4083. if (code_seen('S')) temp=code_value();
  4084. if (code_seen('C')) c=code_value();
  4085. PID_autotune(temp, e, c);
  4086. }
  4087. break;
  4088. case 400: // M400 finish all moves
  4089. {
  4090. st_synchronize();
  4091. }
  4092. break;
  4093. #ifdef FILAMENT_SENSOR
  4094. case 404: //M404 Enter the nominal filament width (3mm, 1.75mm ) N<3.0> or display nominal filament width
  4095. {
  4096. #if (FILWIDTH_PIN > -1)
  4097. if(code_seen('N')) filament_width_nominal=code_value();
  4098. else{
  4099. SERIAL_PROTOCOLPGM("Filament dia (nominal mm):");
  4100. SERIAL_PROTOCOLLN(filament_width_nominal);
  4101. }
  4102. #endif
  4103. }
  4104. break;
  4105. case 405: //M405 Turn on filament sensor for control
  4106. {
  4107. if(code_seen('D')) meas_delay_cm=code_value();
  4108. if(meas_delay_cm> MAX_MEASUREMENT_DELAY)
  4109. meas_delay_cm = MAX_MEASUREMENT_DELAY;
  4110. if(delay_index2 == -1) //initialize the ring buffer if it has not been done since startup
  4111. {
  4112. int temp_ratio = widthFil_to_size_ratio();
  4113. for (delay_index1=0; delay_index1<(MAX_MEASUREMENT_DELAY+1); ++delay_index1 ){
  4114. measurement_delay[delay_index1]=temp_ratio-100; //subtract 100 to scale within a signed byte
  4115. }
  4116. delay_index1=0;
  4117. delay_index2=0;
  4118. }
  4119. filament_sensor = true ;
  4120. //SERIAL_PROTOCOLPGM("Filament dia (measured mm):");
  4121. //SERIAL_PROTOCOL(filament_width_meas);
  4122. //SERIAL_PROTOCOLPGM("Extrusion ratio(%):");
  4123. //SERIAL_PROTOCOL(extrudemultiply);
  4124. }
  4125. break;
  4126. case 406: //M406 Turn off filament sensor for control
  4127. {
  4128. filament_sensor = false ;
  4129. }
  4130. break;
  4131. case 407: //M407 Display measured filament diameter
  4132. {
  4133. SERIAL_PROTOCOLPGM("Filament dia (measured mm):");
  4134. SERIAL_PROTOCOLLN(filament_width_meas);
  4135. }
  4136. break;
  4137. #endif
  4138. case 500: // M500 Store settings in EEPROM
  4139. {
  4140. Config_StoreSettings();
  4141. }
  4142. break;
  4143. case 501: // M501 Read settings from EEPROM
  4144. {
  4145. Config_RetrieveSettings();
  4146. }
  4147. break;
  4148. case 502: // M502 Revert to default settings
  4149. {
  4150. Config_ResetDefault();
  4151. }
  4152. break;
  4153. case 503: // M503 print settings currently in memory
  4154. {
  4155. Config_PrintSettings();
  4156. }
  4157. break;
  4158. case 509: //M509 Force language selection
  4159. {
  4160. lcd_force_language_selection();
  4161. SERIAL_ECHO_START;
  4162. SERIAL_PROTOCOLPGM(("LANG SEL FORCED"));
  4163. }
  4164. break;
  4165. #ifdef ABORT_ON_ENDSTOP_HIT_FEATURE_ENABLED
  4166. case 540:
  4167. {
  4168. if(code_seen('S')) abort_on_endstop_hit = code_value() > 0;
  4169. }
  4170. break;
  4171. #endif
  4172. #ifdef CUSTOM_M_CODE_SET_Z_PROBE_OFFSET
  4173. case CUSTOM_M_CODE_SET_Z_PROBE_OFFSET:
  4174. {
  4175. float value;
  4176. if (code_seen('Z'))
  4177. {
  4178. value = code_value();
  4179. if ((Z_PROBE_OFFSET_RANGE_MIN <= value) && (value <= Z_PROBE_OFFSET_RANGE_MAX))
  4180. {
  4181. zprobe_zoffset = -value; // compare w/ line 278 of ConfigurationStore.cpp
  4182. SERIAL_ECHO_START;
  4183. SERIAL_ECHOLNRPGM(CAT4(MSG_ZPROBE_ZOFFSET, " ", MSG_OK,PSTR("")));
  4184. SERIAL_PROTOCOLLN("");
  4185. }
  4186. else
  4187. {
  4188. SERIAL_ECHO_START;
  4189. SERIAL_ECHORPGM(MSG_ZPROBE_ZOFFSET);
  4190. SERIAL_ECHORPGM(MSG_Z_MIN);
  4191. SERIAL_ECHO(Z_PROBE_OFFSET_RANGE_MIN);
  4192. SERIAL_ECHORPGM(MSG_Z_MAX);
  4193. SERIAL_ECHO(Z_PROBE_OFFSET_RANGE_MAX);
  4194. SERIAL_PROTOCOLLN("");
  4195. }
  4196. }
  4197. else
  4198. {
  4199. SERIAL_ECHO_START;
  4200. SERIAL_ECHOLNRPGM(CAT2(MSG_ZPROBE_ZOFFSET, PSTR(" : ")));
  4201. SERIAL_ECHO(-zprobe_zoffset);
  4202. SERIAL_PROTOCOLLN("");
  4203. }
  4204. break;
  4205. }
  4206. #endif // CUSTOM_M_CODE_SET_Z_PROBE_OFFSET
  4207. #ifdef FILAMENTCHANGEENABLE
  4208. case 600: //Pause for filament change X[pos] Y[pos] Z[relative lift] E[initial retract] L[later retract distance for removal]
  4209. {
  4210. st_synchronize();
  4211. if (farm_mode)
  4212. {
  4213. prusa_statistics(22);
  4214. }
  4215. feedmultiplyBckp=feedmultiply;
  4216. int8_t TooLowZ = 0;
  4217. float target[4];
  4218. float lastpos[4];
  4219. target[X_AXIS]=current_position[X_AXIS];
  4220. target[Y_AXIS]=current_position[Y_AXIS];
  4221. target[Z_AXIS]=current_position[Z_AXIS];
  4222. target[E_AXIS]=current_position[E_AXIS];
  4223. lastpos[X_AXIS]=current_position[X_AXIS];
  4224. lastpos[Y_AXIS]=current_position[Y_AXIS];
  4225. lastpos[Z_AXIS]=current_position[Z_AXIS];
  4226. lastpos[E_AXIS]=current_position[E_AXIS];
  4227. //Restract extruder
  4228. if(code_seen('E'))
  4229. {
  4230. target[E_AXIS]+= code_value();
  4231. }
  4232. else
  4233. {
  4234. #ifdef FILAMENTCHANGE_FIRSTRETRACT
  4235. target[E_AXIS]+= FILAMENTCHANGE_FIRSTRETRACT ;
  4236. #endif
  4237. }
  4238. plan_buffer_line(target[X_AXIS], target[Y_AXIS], target[Z_AXIS], target[E_AXIS], FILAMENTCHANGE_RFEED, active_extruder);
  4239. //Lift Z
  4240. if(code_seen('Z'))
  4241. {
  4242. target[Z_AXIS]+= code_value();
  4243. }
  4244. else
  4245. {
  4246. #ifdef FILAMENTCHANGE_ZADD
  4247. target[Z_AXIS]+= FILAMENTCHANGE_ZADD ;
  4248. if(target[Z_AXIS] < 10){
  4249. target[Z_AXIS]+= 10 ;
  4250. TooLowZ = 1;
  4251. }else{
  4252. TooLowZ = 0;
  4253. }
  4254. #endif
  4255. }
  4256. plan_buffer_line(target[X_AXIS], target[Y_AXIS], target[Z_AXIS], target[E_AXIS], FILAMENTCHANGE_ZFEED, active_extruder);
  4257. //Move XY to side
  4258. if(code_seen('X'))
  4259. {
  4260. target[X_AXIS]+= code_value();
  4261. }
  4262. else
  4263. {
  4264. #ifdef FILAMENTCHANGE_XPOS
  4265. target[X_AXIS]= FILAMENTCHANGE_XPOS ;
  4266. #endif
  4267. }
  4268. if(code_seen('Y'))
  4269. {
  4270. target[Y_AXIS]= code_value();
  4271. }
  4272. else
  4273. {
  4274. #ifdef FILAMENTCHANGE_YPOS
  4275. target[Y_AXIS]= FILAMENTCHANGE_YPOS ;
  4276. #endif
  4277. }
  4278. plan_buffer_line(target[X_AXIS], target[Y_AXIS], target[Z_AXIS], target[E_AXIS], FILAMENTCHANGE_XYFEED, active_extruder);
  4279. // Unload filament
  4280. if(code_seen('L'))
  4281. {
  4282. target[E_AXIS]+= code_value();
  4283. }
  4284. else
  4285. {
  4286. #ifdef FILAMENTCHANGE_FINALRETRACT
  4287. target[E_AXIS]+= FILAMENTCHANGE_FINALRETRACT ;
  4288. #endif
  4289. }
  4290. plan_buffer_line(target[X_AXIS], target[Y_AXIS], target[Z_AXIS], target[E_AXIS], FILAMENTCHANGE_RFEED, active_extruder);
  4291. //finish moves
  4292. st_synchronize();
  4293. //disable extruder steppers so filament can be removed
  4294. disable_e0();
  4295. disable_e1();
  4296. disable_e2();
  4297. delay(100);
  4298. //Wait for user to insert filament
  4299. uint8_t cnt=0;
  4300. int counterBeep = 0;
  4301. lcd_wait_interact();
  4302. while(!lcd_clicked()){
  4303. cnt++;
  4304. manage_heater();
  4305. manage_inactivity(true);
  4306. if(cnt==0)
  4307. {
  4308. #if BEEPER > 0
  4309. if (counterBeep== 500){
  4310. counterBeep = 0;
  4311. }
  4312. SET_OUTPUT(BEEPER);
  4313. if (counterBeep== 0){
  4314. WRITE(BEEPER,HIGH);
  4315. }
  4316. if (counterBeep== 20){
  4317. WRITE(BEEPER,LOW);
  4318. }
  4319. counterBeep++;
  4320. #else
  4321. #if !defined(LCD_FEEDBACK_FREQUENCY_HZ) || !defined(LCD_FEEDBACK_FREQUENCY_DURATION_MS)
  4322. lcd_buzz(1000/6,100);
  4323. #else
  4324. lcd_buzz(LCD_FEEDBACK_FREQUENCY_DURATION_MS,LCD_FEEDBACK_FREQUENCY_HZ);
  4325. #endif
  4326. #endif
  4327. }
  4328. }
  4329. //Filament inserted
  4330. WRITE(BEEPER,LOW);
  4331. //Feed the filament to the end of nozzle quickly
  4332. target[E_AXIS]+= FILAMENTCHANGE_FIRSTFEED ;
  4333. plan_buffer_line(target[X_AXIS], target[Y_AXIS], target[Z_AXIS], target[E_AXIS], FILAMENTCHANGE_EFEED, active_extruder);
  4334. //Extrude some filament
  4335. target[E_AXIS]+= FILAMENTCHANGE_FINALFEED ;
  4336. plan_buffer_line(target[X_AXIS], target[Y_AXIS], target[Z_AXIS], target[E_AXIS], FILAMENTCHANGE_EXFEED, active_extruder);
  4337. //Wait for user to check the state
  4338. lcd_change_fil_state = 0;
  4339. lcd_loading_filament();
  4340. while ((lcd_change_fil_state == 0)||(lcd_change_fil_state != 1)){
  4341. lcd_change_fil_state = 0;
  4342. lcd_alright();
  4343. switch(lcd_change_fil_state){
  4344. // Filament failed to load so load it again
  4345. case 2:
  4346. target[E_AXIS]+= FILAMENTCHANGE_FIRSTFEED ;
  4347. plan_buffer_line(target[X_AXIS], target[Y_AXIS], target[Z_AXIS], target[E_AXIS], FILAMENTCHANGE_EFEED, active_extruder);
  4348. target[E_AXIS]+= FILAMENTCHANGE_FINALFEED ;
  4349. plan_buffer_line(target[X_AXIS], target[Y_AXIS], target[Z_AXIS], target[E_AXIS], FILAMENTCHANGE_EXFEED, active_extruder);
  4350. lcd_loading_filament();
  4351. break;
  4352. // Filament loaded properly but color is not clear
  4353. case 3:
  4354. target[E_AXIS]+= FILAMENTCHANGE_FINALFEED ;
  4355. plan_buffer_line(target[X_AXIS], target[Y_AXIS], target[Z_AXIS], target[E_AXIS], 2, active_extruder);
  4356. lcd_loading_color();
  4357. break;
  4358. // Everything good
  4359. default:
  4360. lcd_change_success();
  4361. break;
  4362. }
  4363. }
  4364. //Not let's go back to print
  4365. //Feed a little of filament to stabilize pressure
  4366. target[E_AXIS]+= FILAMENTCHANGE_RECFEED;
  4367. plan_buffer_line(target[X_AXIS], target[Y_AXIS], target[Z_AXIS], target[E_AXIS], FILAMENTCHANGE_EXFEED, active_extruder);
  4368. //Retract
  4369. target[E_AXIS]+= FILAMENTCHANGE_FIRSTRETRACT;
  4370. plan_buffer_line(target[X_AXIS], target[Y_AXIS], target[Z_AXIS], target[E_AXIS], FILAMENTCHANGE_RFEED, active_extruder);
  4371. //plan_buffer_line(target[X_AXIS], target[Y_AXIS], target[Z_AXIS], target[E_AXIS], 70, active_extruder); //should do nothing
  4372. //Move XY back
  4373. plan_buffer_line(lastpos[X_AXIS], lastpos[Y_AXIS], target[Z_AXIS], target[E_AXIS], FILAMENTCHANGE_XYFEED, active_extruder);
  4374. //Move Z back
  4375. plan_buffer_line(lastpos[X_AXIS], lastpos[Y_AXIS], lastpos[Z_AXIS], target[E_AXIS], FILAMENTCHANGE_ZFEED, active_extruder);
  4376. target[E_AXIS]= target[E_AXIS] - FILAMENTCHANGE_FIRSTRETRACT;
  4377. //Unretract
  4378. plan_buffer_line(lastpos[X_AXIS], lastpos[Y_AXIS], lastpos[Z_AXIS], target[E_AXIS], FILAMENTCHANGE_RFEED, active_extruder);
  4379. //Set E position to original
  4380. plan_set_e_position(lastpos[E_AXIS]);
  4381. //Recover feed rate
  4382. feedmultiply=feedmultiplyBckp;
  4383. char cmd[9];
  4384. sprintf_P(cmd, PSTR("M220 S%i"), feedmultiplyBckp);
  4385. enquecommand(cmd);
  4386. }
  4387. break;
  4388. #endif //FILAMENTCHANGEENABLE
  4389. case 907: // M907 Set digital trimpot motor current using axis codes.
  4390. {
  4391. #if defined(DIGIPOTSS_PIN) && DIGIPOTSS_PIN > -1
  4392. for(int i=0;i<NUM_AXIS;i++) if(code_seen(axis_codes[i])) digipot_current(i,code_value());
  4393. if(code_seen('B')) digipot_current(4,code_value());
  4394. if(code_seen('S')) for(int i=0;i<=4;i++) digipot_current(i,code_value());
  4395. #endif
  4396. #ifdef MOTOR_CURRENT_PWM_XY_PIN
  4397. if(code_seen('X')) digipot_current(0, code_value());
  4398. #endif
  4399. #ifdef MOTOR_CURRENT_PWM_Z_PIN
  4400. if(code_seen('Z')) digipot_current(1, code_value());
  4401. #endif
  4402. #ifdef MOTOR_CURRENT_PWM_E_PIN
  4403. if(code_seen('E')) digipot_current(2, code_value());
  4404. #endif
  4405. #ifdef DIGIPOT_I2C
  4406. // this one uses actual amps in floating point
  4407. for(int i=0;i<NUM_AXIS;i++) if(code_seen(axis_codes[i])) digipot_i2c_set_current(i, code_value());
  4408. // for each additional extruder (named B,C,D,E..., channels 4,5,6,7...)
  4409. 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());
  4410. #endif
  4411. }
  4412. break;
  4413. case 908: // M908 Control digital trimpot directly.
  4414. {
  4415. #if defined(DIGIPOTSS_PIN) && DIGIPOTSS_PIN > -1
  4416. uint8_t channel,current;
  4417. if(code_seen('P')) channel=code_value();
  4418. if(code_seen('S')) current=code_value();
  4419. digitalPotWrite(channel, current);
  4420. #endif
  4421. }
  4422. break;
  4423. case 350: // M350 Set microstepping mode. Warning: Steps per unit remains unchanged. S code sets stepping mode for all drivers.
  4424. {
  4425. #if defined(X_MS1_PIN) && X_MS1_PIN > -1
  4426. if(code_seen('S')) for(int i=0;i<=4;i++) microstep_mode(i,code_value());
  4427. for(int i=0;i<NUM_AXIS;i++) if(code_seen(axis_codes[i])) microstep_mode(i,(uint8_t)code_value());
  4428. if(code_seen('B')) microstep_mode(4,code_value());
  4429. microstep_readings();
  4430. #endif
  4431. }
  4432. break;
  4433. case 351: // M351 Toggle MS1 MS2 pins directly, S# determines MS1 or MS2, X# sets the pin high/low.
  4434. {
  4435. #if defined(X_MS1_PIN) && X_MS1_PIN > -1
  4436. if(code_seen('S')) switch((int)code_value())
  4437. {
  4438. case 1:
  4439. for(int i=0;i<NUM_AXIS;i++) if(code_seen(axis_codes[i])) microstep_ms(i,code_value(),-1);
  4440. if(code_seen('B')) microstep_ms(4,code_value(),-1);
  4441. break;
  4442. case 2:
  4443. for(int i=0;i<NUM_AXIS;i++) if(code_seen(axis_codes[i])) microstep_ms(i,-1,code_value());
  4444. if(code_seen('B')) microstep_ms(4,-1,code_value());
  4445. break;
  4446. }
  4447. microstep_readings();
  4448. #endif
  4449. }
  4450. break;
  4451. case 701: //M701: load filament
  4452. {
  4453. enable_z();
  4454. custom_message = true;
  4455. custom_message_type = 2;
  4456. lcd_setstatuspgm(MSG_LOADING_FILAMENT);
  4457. current_position[E_AXIS] += 65;
  4458. plan_buffer_line(current_position[X_AXIS], current_position[Y_AXIS], current_position[Z_AXIS], current_position[E_AXIS], 400 / 60, active_extruder); //fast sequence
  4459. current_position[E_AXIS] += 40;
  4460. plan_buffer_line(current_position[X_AXIS], current_position[Y_AXIS], current_position[Z_AXIS], current_position[E_AXIS], 100 / 60, active_extruder); //slow sequence
  4461. st_synchronize();
  4462. if (!farm_mode && loading_flag) {
  4463. bool clean = lcd_show_fullscreen_message_yes_no_and_wait_P(MSG_FILAMENT_CLEAN, false, true);
  4464. while (!clean) {
  4465. lcd_update_enable(true);
  4466. lcd_update(2);
  4467. current_position[E_AXIS] += 40;
  4468. plan_buffer_line(current_position[X_AXIS], current_position[Y_AXIS], current_position[Z_AXIS], current_position[E_AXIS], 100 / 60, active_extruder); //slow sequence
  4469. st_synchronize();
  4470. clean = lcd_show_fullscreen_message_yes_no_and_wait_P(MSG_FILAMENT_CLEAN, false, true);
  4471. }
  4472. }
  4473. lcd_update_enable(true);
  4474. lcd_update(2);
  4475. lcd_setstatuspgm(WELCOME_MSG);
  4476. disable_z();
  4477. loading_flag = false;
  4478. custom_message = false;
  4479. custom_message_type = 0;
  4480. }
  4481. break;
  4482. case 702:
  4483. {
  4484. custom_message = true;
  4485. custom_message_type = 2;
  4486. lcd_setstatuspgm(MSG_UNLOADING_FILAMENT);
  4487. current_position[E_AXIS] -= 80;
  4488. plan_buffer_line(current_position[X_AXIS], current_position[Y_AXIS], current_position[Z_AXIS], current_position[E_AXIS], 7000 / 60, active_extruder);
  4489. st_synchronize();
  4490. lcd_setstatuspgm(WELCOME_MSG);
  4491. custom_message = false;
  4492. custom_message_type = 0;
  4493. }
  4494. break;
  4495. case 999: // M999: Restart after being stopped
  4496. Stopped = false;
  4497. lcd_reset_alert_level();
  4498. gcode_LastN = Stopped_gcode_LastN;
  4499. FlushSerialRequestResend();
  4500. break;
  4501. default: SERIAL_ECHOLNPGM("Invalid M code.");
  4502. }
  4503. } // end if(code_seen('M')) (end of M codes)
  4504. else if(code_seen('T'))
  4505. {
  4506. int index;
  4507. for (index = 1; *(strchr_pointer + index) == ' ' || *(strchr_pointer + index) == '\t'; index++);
  4508. if (*(strchr_pointer + index) < '0' || *(strchr_pointer + index) > '9') {
  4509. SERIAL_ECHOLNPGM("Invalid T code.");
  4510. }
  4511. else {
  4512. tmp_extruder = code_value();
  4513. #ifdef SNMM
  4514. st_synchronize();
  4515. delay(100);
  4516. disable_e0();
  4517. disable_e1();
  4518. disable_e2();
  4519. pinMode(E_MUX0_PIN, OUTPUT);
  4520. pinMode(E_MUX1_PIN, OUTPUT);
  4521. pinMode(E_MUX2_PIN, OUTPUT);
  4522. delay(100);
  4523. SERIAL_ECHO_START;
  4524. SERIAL_ECHO("T:");
  4525. SERIAL_ECHOLN((int)tmp_extruder);
  4526. switch (tmp_extruder) {
  4527. case 1:
  4528. WRITE(E_MUX0_PIN, HIGH);
  4529. WRITE(E_MUX1_PIN, LOW);
  4530. WRITE(E_MUX2_PIN, LOW);
  4531. break;
  4532. case 2:
  4533. WRITE(E_MUX0_PIN, LOW);
  4534. WRITE(E_MUX1_PIN, HIGH);
  4535. WRITE(E_MUX2_PIN, LOW);
  4536. break;
  4537. case 3:
  4538. WRITE(E_MUX0_PIN, HIGH);
  4539. WRITE(E_MUX1_PIN, HIGH);
  4540. WRITE(E_MUX2_PIN, LOW);
  4541. break;
  4542. default:
  4543. WRITE(E_MUX0_PIN, LOW);
  4544. WRITE(E_MUX1_PIN, LOW);
  4545. WRITE(E_MUX2_PIN, LOW);
  4546. break;
  4547. }
  4548. delay(100);
  4549. #else
  4550. if (tmp_extruder >= EXTRUDERS) {
  4551. SERIAL_ECHO_START;
  4552. SERIAL_ECHOPGM("T");
  4553. SERIAL_PROTOCOLLN((int)tmp_extruder);
  4554. SERIAL_ECHOLNRPGM(MSG_INVALID_EXTRUDER);
  4555. }
  4556. else {
  4557. boolean make_move = false;
  4558. if (code_seen('F')) {
  4559. make_move = true;
  4560. next_feedrate = code_value();
  4561. if (next_feedrate > 0.0) {
  4562. feedrate = next_feedrate;
  4563. }
  4564. }
  4565. #if EXTRUDERS > 1
  4566. if (tmp_extruder != active_extruder) {
  4567. // Save current position to return to after applying extruder offset
  4568. memcpy(destination, current_position, sizeof(destination));
  4569. // Offset extruder (only by XY)
  4570. int i;
  4571. for (i = 0; i < 2; i++) {
  4572. current_position[i] = current_position[i] -
  4573. extruder_offset[i][active_extruder] +
  4574. extruder_offset[i][tmp_extruder];
  4575. }
  4576. // Set the new active extruder and position
  4577. active_extruder = tmp_extruder;
  4578. plan_set_position(current_position[X_AXIS], current_position[Y_AXIS], current_position[Z_AXIS], current_position[E_AXIS]);
  4579. // Move to the old position if 'F' was in the parameters
  4580. if (make_move && Stopped == false) {
  4581. prepare_move();
  4582. }
  4583. }
  4584. #endif
  4585. SERIAL_ECHO_START;
  4586. SERIAL_ECHORPGM(MSG_ACTIVE_EXTRUDER);
  4587. SERIAL_PROTOCOLLN((int)active_extruder);
  4588. }
  4589. #endif
  4590. }
  4591. } // end if(code_seen('T')) (end of T codes)
  4592. else
  4593. {
  4594. SERIAL_ECHO_START;
  4595. SERIAL_ECHORPGM(MSG_UNKNOWN_COMMAND);
  4596. SERIAL_ECHO(CMDBUFFER_CURRENT_STRING);
  4597. SERIAL_ECHOLNPGM("\"");
  4598. }
  4599. ClearToSend();
  4600. }
  4601. void FlushSerialRequestResend()
  4602. {
  4603. //char cmdbuffer[bufindr][100]="Resend:";
  4604. MYSERIAL.flush();
  4605. SERIAL_PROTOCOLRPGM(MSG_RESEND);
  4606. SERIAL_PROTOCOLLN(gcode_LastN + 1);
  4607. ClearToSend();
  4608. }
  4609. // Confirm the execution of a command, if sent from a serial line.
  4610. // Execution of a command from a SD card will not be confirmed.
  4611. void ClearToSend()
  4612. {
  4613. previous_millis_cmd = millis();
  4614. if (CMDBUFFER_CURRENT_TYPE == CMDBUFFER_CURRENT_TYPE_USB)
  4615. SERIAL_PROTOCOLLNRPGM(MSG_OK);
  4616. }
  4617. void get_coordinates()
  4618. {
  4619. bool seen[4]={false,false,false,false};
  4620. for(int8_t i=0; i < NUM_AXIS; i++) {
  4621. if(code_seen(axis_codes[i]))
  4622. {
  4623. destination[i] = (float)code_value() + (axis_relative_modes[i] || relative_mode)*current_position[i];
  4624. seen[i]=true;
  4625. }
  4626. else destination[i] = current_position[i]; //Are these else lines really needed?
  4627. }
  4628. if(code_seen('F')) {
  4629. next_feedrate = code_value();
  4630. if(next_feedrate > 0.0) feedrate = next_feedrate;
  4631. }
  4632. }
  4633. void get_arc_coordinates()
  4634. {
  4635. #ifdef SF_ARC_FIX
  4636. bool relative_mode_backup = relative_mode;
  4637. relative_mode = true;
  4638. #endif
  4639. get_coordinates();
  4640. #ifdef SF_ARC_FIX
  4641. relative_mode=relative_mode_backup;
  4642. #endif
  4643. if(code_seen('I')) {
  4644. offset[0] = code_value();
  4645. }
  4646. else {
  4647. offset[0] = 0.0;
  4648. }
  4649. if(code_seen('J')) {
  4650. offset[1] = code_value();
  4651. }
  4652. else {
  4653. offset[1] = 0.0;
  4654. }
  4655. }
  4656. void clamp_to_software_endstops(float target[3])
  4657. {
  4658. world2machine_clamp(target[0], target[1]);
  4659. // Clamp the Z coordinate.
  4660. if (min_software_endstops) {
  4661. float negative_z_offset = 0;
  4662. #ifdef ENABLE_AUTO_BED_LEVELING
  4663. if (Z_PROBE_OFFSET_FROM_EXTRUDER < 0) negative_z_offset = negative_z_offset + Z_PROBE_OFFSET_FROM_EXTRUDER;
  4664. if (add_homing[Z_AXIS] < 0) negative_z_offset = negative_z_offset + add_homing[Z_AXIS];
  4665. #endif
  4666. if (target[Z_AXIS] < min_pos[Z_AXIS]+negative_z_offset) target[Z_AXIS] = min_pos[Z_AXIS]+negative_z_offset;
  4667. }
  4668. if (max_software_endstops) {
  4669. if (target[Z_AXIS] > max_pos[Z_AXIS]) target[Z_AXIS] = max_pos[Z_AXIS];
  4670. }
  4671. }
  4672. #ifdef MESH_BED_LEVELING
  4673. void mesh_plan_buffer_line(const float &x, const float &y, const float &z, const float &e, const float &feed_rate, const uint8_t extruder) {
  4674. float dx = x - current_position[X_AXIS];
  4675. float dy = y - current_position[Y_AXIS];
  4676. float dz = z - current_position[Z_AXIS];
  4677. int n_segments = 0;
  4678. if (mbl.active) {
  4679. float len = abs(dx) + abs(dy);
  4680. if (len > 0)
  4681. // Split to 3cm segments or shorter.
  4682. n_segments = int(ceil(len / 30.f));
  4683. }
  4684. if (n_segments > 1) {
  4685. float de = e - current_position[E_AXIS];
  4686. for (int i = 1; i < n_segments; ++ i) {
  4687. float t = float(i) / float(n_segments);
  4688. plan_buffer_line(
  4689. current_position[X_AXIS] + t * dx,
  4690. current_position[Y_AXIS] + t * dy,
  4691. current_position[Z_AXIS] + t * dz,
  4692. current_position[E_AXIS] + t * de,
  4693. feed_rate, extruder);
  4694. }
  4695. }
  4696. // The rest of the path.
  4697. plan_buffer_line(x, y, z, e, feed_rate, extruder);
  4698. current_position[X_AXIS] = x;
  4699. current_position[Y_AXIS] = y;
  4700. current_position[Z_AXIS] = z;
  4701. current_position[E_AXIS] = e;
  4702. }
  4703. #endif // MESH_BED_LEVELING
  4704. void prepare_move()
  4705. {
  4706. clamp_to_software_endstops(destination);
  4707. previous_millis_cmd = millis();
  4708. // Do not use feedmultiply for E or Z only moves
  4709. if( (current_position[X_AXIS] == destination [X_AXIS]) && (current_position[Y_AXIS] == destination [Y_AXIS])) {
  4710. plan_buffer_line(destination[X_AXIS], destination[Y_AXIS], destination[Z_AXIS], destination[E_AXIS], feedrate/60, active_extruder);
  4711. }
  4712. else {
  4713. #ifdef MESH_BED_LEVELING
  4714. mesh_plan_buffer_line(destination[X_AXIS], destination[Y_AXIS], destination[Z_AXIS], destination[E_AXIS], feedrate*feedmultiply*(1./(60.f*100.f)), active_extruder);
  4715. #else
  4716. plan_buffer_line(destination[X_AXIS], destination[Y_AXIS], destination[Z_AXIS], destination[E_AXIS], feedrate*feedmultiply*(1./(60.f*100.f)), active_extruder);
  4717. #endif
  4718. }
  4719. for(int8_t i=0; i < NUM_AXIS; i++) {
  4720. current_position[i] = destination[i];
  4721. }
  4722. }
  4723. void prepare_arc_move(char isclockwise) {
  4724. float r = hypot(offset[X_AXIS], offset[Y_AXIS]); // Compute arc radius for mc_arc
  4725. // Trace the arc
  4726. mc_arc(current_position, destination, offset, X_AXIS, Y_AXIS, Z_AXIS, feedrate*feedmultiply/60/100.0, r, isclockwise, active_extruder);
  4727. // As far as the parser is concerned, the position is now == target. In reality the
  4728. // motion control system might still be processing the action and the real tool position
  4729. // in any intermediate location.
  4730. for(int8_t i=0; i < NUM_AXIS; i++) {
  4731. current_position[i] = destination[i];
  4732. }
  4733. previous_millis_cmd = millis();
  4734. }
  4735. #if defined(CONTROLLERFAN_PIN) && CONTROLLERFAN_PIN > -1
  4736. #if defined(FAN_PIN)
  4737. #if CONTROLLERFAN_PIN == FAN_PIN
  4738. #error "You cannot set CONTROLLERFAN_PIN equal to FAN_PIN"
  4739. #endif
  4740. #endif
  4741. unsigned long lastMotor = 0; //Save the time for when a motor was turned on last
  4742. unsigned long lastMotorCheck = 0;
  4743. void controllerFan()
  4744. {
  4745. if ((millis() - lastMotorCheck) >= 2500) //Not a time critical function, so we only check every 2500ms
  4746. {
  4747. lastMotorCheck = millis();
  4748. if(!READ(X_ENABLE_PIN) || !READ(Y_ENABLE_PIN) || !READ(Z_ENABLE_PIN) || (soft_pwm_bed > 0)
  4749. #if EXTRUDERS > 2
  4750. || !READ(E2_ENABLE_PIN)
  4751. #endif
  4752. #if EXTRUDER > 1
  4753. #if defined(X2_ENABLE_PIN) && X2_ENABLE_PIN > -1
  4754. || !READ(X2_ENABLE_PIN)
  4755. #endif
  4756. || !READ(E1_ENABLE_PIN)
  4757. #endif
  4758. || !READ(E0_ENABLE_PIN)) //If any of the drivers are enabled...
  4759. {
  4760. lastMotor = millis(); //... set time to NOW so the fan will turn on
  4761. }
  4762. if ((millis() - lastMotor) >= (CONTROLLERFAN_SECS*1000UL) || lastMotor == 0) //If the last time any driver was enabled, is longer since than CONTROLLERSEC...
  4763. {
  4764. digitalWrite(CONTROLLERFAN_PIN, 0);
  4765. analogWrite(CONTROLLERFAN_PIN, 0);
  4766. }
  4767. else
  4768. {
  4769. // allows digital or PWM fan output to be used (see M42 handling)
  4770. digitalWrite(CONTROLLERFAN_PIN, CONTROLLERFAN_SPEED);
  4771. analogWrite(CONTROLLERFAN_PIN, CONTROLLERFAN_SPEED);
  4772. }
  4773. }
  4774. }
  4775. #endif
  4776. #ifdef TEMP_STAT_LEDS
  4777. static bool blue_led = false;
  4778. static bool red_led = false;
  4779. static uint32_t stat_update = 0;
  4780. void handle_status_leds(void) {
  4781. float max_temp = 0.0;
  4782. if(millis() > stat_update) {
  4783. stat_update += 500; // Update every 0.5s
  4784. for (int8_t cur_extruder = 0; cur_extruder < EXTRUDERS; ++cur_extruder) {
  4785. max_temp = max(max_temp, degHotend(cur_extruder));
  4786. max_temp = max(max_temp, degTargetHotend(cur_extruder));
  4787. }
  4788. #if defined(TEMP_BED_PIN) && TEMP_BED_PIN > -1
  4789. max_temp = max(max_temp, degTargetBed());
  4790. max_temp = max(max_temp, degBed());
  4791. #endif
  4792. if((max_temp > 55.0) && (red_led == false)) {
  4793. digitalWrite(STAT_LED_RED, 1);
  4794. digitalWrite(STAT_LED_BLUE, 0);
  4795. red_led = true;
  4796. blue_led = false;
  4797. }
  4798. if((max_temp < 54.0) && (blue_led == false)) {
  4799. digitalWrite(STAT_LED_RED, 0);
  4800. digitalWrite(STAT_LED_BLUE, 1);
  4801. red_led = false;
  4802. blue_led = true;
  4803. }
  4804. }
  4805. }
  4806. #endif
  4807. void manage_inactivity(bool ignore_stepper_queue/*=false*/) //default argument set in Marlin.h
  4808. {
  4809. #if defined(KILL_PIN) && KILL_PIN > -1
  4810. static int killCount = 0; // make the inactivity button a bit less responsive
  4811. const int KILL_DELAY = 10000;
  4812. #endif
  4813. if(buflen < (BUFSIZE-1)){
  4814. get_command();
  4815. }
  4816. if( (millis() - previous_millis_cmd) > max_inactive_time )
  4817. if(max_inactive_time)
  4818. kill();
  4819. if(stepper_inactive_time) {
  4820. if( (millis() - previous_millis_cmd) > stepper_inactive_time )
  4821. {
  4822. if(blocks_queued() == false && ignore_stepper_queue == false) {
  4823. disable_x();
  4824. // SERIAL_ECHOLNPGM("manage_inactivity - disable Y");
  4825. disable_y();
  4826. disable_z();
  4827. disable_e0();
  4828. disable_e1();
  4829. disable_e2();
  4830. }
  4831. }
  4832. }
  4833. #ifdef CHDK //Check if pin should be set to LOW after M240 set it to HIGH
  4834. if (chdkActive && (millis() - chdkHigh > CHDK_DELAY))
  4835. {
  4836. chdkActive = false;
  4837. WRITE(CHDK, LOW);
  4838. }
  4839. #endif
  4840. #if defined(KILL_PIN) && KILL_PIN > -1
  4841. // Check if the kill button was pressed and wait just in case it was an accidental
  4842. // key kill key press
  4843. // -------------------------------------------------------------------------------
  4844. if( 0 == READ(KILL_PIN) )
  4845. {
  4846. killCount++;
  4847. }
  4848. else if (killCount > 0)
  4849. {
  4850. killCount--;
  4851. }
  4852. // Exceeded threshold and we can confirm that it was not accidental
  4853. // KILL the machine
  4854. // ----------------------------------------------------------------
  4855. if ( killCount >= KILL_DELAY)
  4856. {
  4857. kill();
  4858. }
  4859. #endif
  4860. #if defined(CONTROLLERFAN_PIN) && CONTROLLERFAN_PIN > -1
  4861. controllerFan(); //Check if fan should be turned on to cool stepper drivers down
  4862. #endif
  4863. #ifdef EXTRUDER_RUNOUT_PREVENT
  4864. if( (millis() - previous_millis_cmd) > EXTRUDER_RUNOUT_SECONDS*1000 )
  4865. if(degHotend(active_extruder)>EXTRUDER_RUNOUT_MINTEMP)
  4866. {
  4867. bool oldstatus=READ(E0_ENABLE_PIN);
  4868. enable_e0();
  4869. float oldepos=current_position[E_AXIS];
  4870. float oldedes=destination[E_AXIS];
  4871. plan_buffer_line(destination[X_AXIS], destination[Y_AXIS], destination[Z_AXIS],
  4872. destination[E_AXIS]+EXTRUDER_RUNOUT_EXTRUDE*EXTRUDER_RUNOUT_ESTEPS/axis_steps_per_unit[E_AXIS],
  4873. EXTRUDER_RUNOUT_SPEED/60.*EXTRUDER_RUNOUT_ESTEPS/axis_steps_per_unit[E_AXIS], active_extruder);
  4874. current_position[E_AXIS]=oldepos;
  4875. destination[E_AXIS]=oldedes;
  4876. plan_set_e_position(oldepos);
  4877. previous_millis_cmd=millis();
  4878. st_synchronize();
  4879. WRITE(E0_ENABLE_PIN,oldstatus);
  4880. }
  4881. #endif
  4882. #ifdef TEMP_STAT_LEDS
  4883. handle_status_leds();
  4884. #endif
  4885. check_axes_activity();
  4886. }
  4887. void kill(const char *full_screen_message)
  4888. {
  4889. cli(); // Stop interrupts
  4890. disable_heater();
  4891. disable_x();
  4892. // SERIAL_ECHOLNPGM("kill - disable Y");
  4893. disable_y();
  4894. disable_z();
  4895. disable_e0();
  4896. disable_e1();
  4897. disable_e2();
  4898. #if defined(PS_ON_PIN) && PS_ON_PIN > -1
  4899. pinMode(PS_ON_PIN,INPUT);
  4900. #endif
  4901. SERIAL_ERROR_START;
  4902. SERIAL_ERRORLNRPGM(MSG_ERR_KILLED);
  4903. if (full_screen_message != NULL) {
  4904. SERIAL_ERRORLNRPGM(full_screen_message);
  4905. lcd_display_message_fullscreen_P(full_screen_message);
  4906. } else {
  4907. LCD_ALERTMESSAGERPGM(MSG_KILLED);
  4908. }
  4909. // FMC small patch to update the LCD before ending
  4910. sei(); // enable interrupts
  4911. for ( int i=5; i--; lcd_update())
  4912. {
  4913. delay(200);
  4914. }
  4915. cli(); // disable interrupts
  4916. suicide();
  4917. while(1) { /* Intentionally left empty */ } // Wait for reset
  4918. }
  4919. void Stop()
  4920. {
  4921. disable_heater();
  4922. if(Stopped == false) {
  4923. Stopped = true;
  4924. Stopped_gcode_LastN = gcode_LastN; // Save last g_code for restart
  4925. SERIAL_ERROR_START;
  4926. SERIAL_ERRORLNRPGM(MSG_ERR_STOPPED);
  4927. LCD_MESSAGERPGM(MSG_STOPPED);
  4928. }
  4929. }
  4930. bool IsStopped() { return Stopped; };
  4931. #ifdef FAST_PWM_FAN
  4932. void setPwmFrequency(uint8_t pin, int val)
  4933. {
  4934. val &= 0x07;
  4935. switch(digitalPinToTimer(pin))
  4936. {
  4937. #if defined(TCCR0A)
  4938. case TIMER0A:
  4939. case TIMER0B:
  4940. // TCCR0B &= ~(_BV(CS00) | _BV(CS01) | _BV(CS02));
  4941. // TCCR0B |= val;
  4942. break;
  4943. #endif
  4944. #if defined(TCCR1A)
  4945. case TIMER1A:
  4946. case TIMER1B:
  4947. // TCCR1B &= ~(_BV(CS10) | _BV(CS11) | _BV(CS12));
  4948. // TCCR1B |= val;
  4949. break;
  4950. #endif
  4951. #if defined(TCCR2)
  4952. case TIMER2:
  4953. case TIMER2:
  4954. TCCR2 &= ~(_BV(CS10) | _BV(CS11) | _BV(CS12));
  4955. TCCR2 |= val;
  4956. break;
  4957. #endif
  4958. #if defined(TCCR2A)
  4959. case TIMER2A:
  4960. case TIMER2B:
  4961. TCCR2B &= ~(_BV(CS20) | _BV(CS21) | _BV(CS22));
  4962. TCCR2B |= val;
  4963. break;
  4964. #endif
  4965. #if defined(TCCR3A)
  4966. case TIMER3A:
  4967. case TIMER3B:
  4968. case TIMER3C:
  4969. TCCR3B &= ~(_BV(CS30) | _BV(CS31) | _BV(CS32));
  4970. TCCR3B |= val;
  4971. break;
  4972. #endif
  4973. #if defined(TCCR4A)
  4974. case TIMER4A:
  4975. case TIMER4B:
  4976. case TIMER4C:
  4977. TCCR4B &= ~(_BV(CS40) | _BV(CS41) | _BV(CS42));
  4978. TCCR4B |= val;
  4979. break;
  4980. #endif
  4981. #if defined(TCCR5A)
  4982. case TIMER5A:
  4983. case TIMER5B:
  4984. case TIMER5C:
  4985. TCCR5B &= ~(_BV(CS50) | _BV(CS51) | _BV(CS52));
  4986. TCCR5B |= val;
  4987. break;
  4988. #endif
  4989. }
  4990. }
  4991. #endif //FAST_PWM_FAN
  4992. bool setTargetedHotend(int code){
  4993. tmp_extruder = active_extruder;
  4994. if(code_seen('T')) {
  4995. tmp_extruder = code_value();
  4996. if(tmp_extruder >= EXTRUDERS) {
  4997. SERIAL_ECHO_START;
  4998. switch(code){
  4999. case 104:
  5000. SERIAL_ECHORPGM(MSG_M104_INVALID_EXTRUDER);
  5001. break;
  5002. case 105:
  5003. SERIAL_ECHO(MSG_M105_INVALID_EXTRUDER);
  5004. break;
  5005. case 109:
  5006. SERIAL_ECHO(MSG_M109_INVALID_EXTRUDER);
  5007. break;
  5008. case 218:
  5009. SERIAL_ECHO(MSG_M218_INVALID_EXTRUDER);
  5010. break;
  5011. case 221:
  5012. SERIAL_ECHO(MSG_M221_INVALID_EXTRUDER);
  5013. break;
  5014. }
  5015. SERIAL_PROTOCOLLN((int)tmp_extruder);
  5016. return true;
  5017. }
  5018. }
  5019. return false;
  5020. }
  5021. void save_statistics(unsigned long _total_filament_used, unsigned long _total_print_time) //_total_filament_used unit: mm/100; print time in s
  5022. {
  5023. if (eeprom_read_byte((uint8_t *)EEPROM_TOTALTIME) == 255 && eeprom_read_byte((uint8_t *)EEPROM_TOTALTIME + 1) == 255 && eeprom_read_byte((uint8_t *)EEPROM_TOTALTIME + 2) == 255 && eeprom_read_byte((uint8_t *)EEPROM_TOTALTIME + 3) == 255)
  5024. {
  5025. eeprom_update_dword((uint32_t *)EEPROM_TOTALTIME, 0);
  5026. eeprom_update_dword((uint32_t *)EEPROM_FILAMENTUSED, 0);
  5027. }
  5028. unsigned long _previous_filament = eeprom_read_dword((uint32_t *)EEPROM_FILAMENTUSED); //_previous_filament unit: cm
  5029. unsigned long _previous_time = eeprom_read_dword((uint32_t *)EEPROM_TOTALTIME); //_previous_time unit: min
  5030. eeprom_update_dword((uint32_t *)EEPROM_TOTALTIME, _previous_time + (_total_print_time/60)); //EEPROM_TOTALTIME unit: min
  5031. eeprom_update_dword((uint32_t *)EEPROM_FILAMENTUSED, _previous_filament + (_total_filament_used / 1000));
  5032. total_filament_used = 0;
  5033. }
  5034. float calculate_volumetric_multiplier(float diameter) {
  5035. float area = .0;
  5036. float radius = .0;
  5037. radius = diameter * .5;
  5038. if (! volumetric_enabled || radius == 0) {
  5039. area = 1;
  5040. }
  5041. else {
  5042. area = M_PI * pow(radius, 2);
  5043. }
  5044. return 1.0 / area;
  5045. }
  5046. void calculate_volumetric_multipliers() {
  5047. volumetric_multiplier[0] = calculate_volumetric_multiplier(filament_size[0]);
  5048. #if EXTRUDERS > 1
  5049. volumetric_multiplier[1] = calculate_volumetric_multiplier(filament_size[1]);
  5050. #if EXTRUDERS > 2
  5051. volumetric_multiplier[2] = calculate_volumetric_multiplier(filament_size[2]);
  5052. #endif
  5053. #endif
  5054. }
  5055. void delay_keep_alive(int ms)
  5056. {
  5057. for (;;) {
  5058. manage_heater();
  5059. // Manage inactivity, but don't disable steppers on timeout.
  5060. manage_inactivity(true);
  5061. lcd_update();
  5062. if (ms == 0)
  5063. break;
  5064. else if (ms >= 50) {
  5065. delay(50);
  5066. ms -= 50;
  5067. } else {
  5068. delay(ms);
  5069. ms = 0;
  5070. }
  5071. }
  5072. }
  5073. void check_babystep() {
  5074. int babystep_z;
  5075. EEPROM_read_B(EEPROM_BABYSTEP_Z, &babystep_z);
  5076. if ((babystep_z < Z_BABYSTEP_MIN) || (babystep_z > Z_BABYSTEP_MAX)) {
  5077. babystep_z = 0; //if babystep value is out of min max range, set it to 0
  5078. SERIAL_ECHOLNPGM("Z live adjust out of range. Setting to 0");
  5079. EEPROM_save_B(EEPROM_BABYSTEP_Z, &babystep_z);
  5080. lcd_show_fullscreen_message_and_wait_P(PSTR("Z live adjust out of range. Setting to 0. Click to continue."));
  5081. lcd_update_enable(true);
  5082. }
  5083. }
  5084. #ifdef DIS
  5085. void d_setup()
  5086. {
  5087. pinMode(D_DATACLOCK, INPUT_PULLUP);
  5088. pinMode(D_DATA, INPUT_PULLUP);
  5089. pinMode(D_REQUIRE, OUTPUT);
  5090. digitalWrite(D_REQUIRE, HIGH);
  5091. }
  5092. float d_ReadData()
  5093. {
  5094. int digit[13];
  5095. String mergeOutput;
  5096. float output;
  5097. digitalWrite(D_REQUIRE, HIGH);
  5098. for (int i = 0; i<13; i++)
  5099. {
  5100. for (int j = 0; j < 4; j++)
  5101. {
  5102. while (digitalRead(D_DATACLOCK) == LOW) {}
  5103. while (digitalRead(D_DATACLOCK) == HIGH) {}
  5104. bitWrite(digit[i], j, digitalRead(D_DATA));
  5105. }
  5106. }
  5107. digitalWrite(D_REQUIRE, LOW);
  5108. mergeOutput = "";
  5109. output = 0;
  5110. for (int r = 5; r <= 10; r++) //Merge digits
  5111. {
  5112. mergeOutput += digit[r];
  5113. }
  5114. output = mergeOutput.toFloat();
  5115. if (digit[4] == 8) //Handle sign
  5116. {
  5117. output *= -1;
  5118. }
  5119. for (int i = digit[11]; i > 0; i--) //Handle floating point
  5120. {
  5121. output /= 10;
  5122. }
  5123. return output;
  5124. }
  5125. void bed_analysis(float x_dimension, float y_dimension, int x_points_num, int y_points_num, float shift_x, float shift_y) {
  5126. int t1 = 0;
  5127. int t_delay = 0;
  5128. int digit[13];
  5129. int m;
  5130. char str[3];
  5131. //String mergeOutput;
  5132. char mergeOutput[15];
  5133. float output;
  5134. int mesh_point = 0; //index number of calibration point
  5135. float bed_zero_ref_x = (-22.f + X_PROBE_OFFSET_FROM_EXTRUDER); //shift between zero point on bed and target and between probe and nozzle
  5136. float bed_zero_ref_y = (-0.6f + Y_PROBE_OFFSET_FROM_EXTRUDER);
  5137. float mesh_home_z_search = 4;
  5138. float row[x_points_num];
  5139. int ix = 0;
  5140. int iy = 0;
  5141. char* filename_wldsd = "wldsd.txt";
  5142. char data_wldsd[70];
  5143. char numb_wldsd[10];
  5144. d_setup();
  5145. if (!(axis_known_position[X_AXIS] && axis_known_position[Y_AXIS] && axis_known_position[Z_AXIS])) {
  5146. // We don't know where we are! HOME!
  5147. // Push the commands to the front of the message queue in the reverse order!
  5148. // There shall be always enough space reserved for these commands.
  5149. repeatcommand_front(); // repeat G80 with all its parameters
  5150. enquecommand_front_P((PSTR("G28 W0")));
  5151. enquecommand_front_P((PSTR("G1 Z5")));
  5152. return;
  5153. }
  5154. bool custom_message_old = custom_message;
  5155. unsigned int custom_message_type_old = custom_message_type;
  5156. unsigned int custom_message_state_old = custom_message_state;
  5157. custom_message = true;
  5158. custom_message_type = 1;
  5159. custom_message_state = (x_points_num * y_points_num) + 10;
  5160. lcd_update(1);
  5161. mbl.reset();
  5162. babystep_undo();
  5163. card.openFile(filename_wldsd, false);
  5164. current_position[Z_AXIS] = mesh_home_z_search;
  5165. plan_buffer_line(current_position[X_AXIS], current_position[Y_AXIS], current_position[Z_AXIS], current_position[E_AXIS], homing_feedrate[Z_AXIS] / 60, active_extruder);
  5166. int XY_AXIS_FEEDRATE = homing_feedrate[X_AXIS] / 20;
  5167. int Z_PROBE_FEEDRATE = homing_feedrate[Z_AXIS] / 60;
  5168. int Z_LIFT_FEEDRATE = homing_feedrate[Z_AXIS] / 40;
  5169. setup_for_endstop_move(false);
  5170. SERIAL_PROTOCOLPGM("Num X,Y: ");
  5171. SERIAL_PROTOCOL(x_points_num);
  5172. SERIAL_PROTOCOLPGM(",");
  5173. SERIAL_PROTOCOL(y_points_num);
  5174. SERIAL_PROTOCOLPGM("\nZ search height: ");
  5175. SERIAL_PROTOCOL(mesh_home_z_search);
  5176. SERIAL_PROTOCOLPGM("\nDimension X,Y: ");
  5177. SERIAL_PROTOCOL(x_dimension);
  5178. SERIAL_PROTOCOLPGM(",");
  5179. SERIAL_PROTOCOL(y_dimension);
  5180. SERIAL_PROTOCOLLNPGM("\nMeasured points:");
  5181. while (mesh_point != x_points_num * y_points_num) {
  5182. ix = mesh_point % x_points_num; // from 0 to MESH_NUM_X_POINTS - 1
  5183. iy = mesh_point / x_points_num;
  5184. if (iy & 1) ix = (x_points_num - 1) - ix; // Zig zag
  5185. float z0 = 0.f;
  5186. current_position[Z_AXIS] = mesh_home_z_search;
  5187. plan_buffer_line(current_position[X_AXIS], current_position[Y_AXIS], current_position[Z_AXIS], current_position[E_AXIS], Z_LIFT_FEEDRATE, active_extruder);
  5188. st_synchronize();
  5189. current_position[X_AXIS] = 13.f + ix * (x_dimension / (x_points_num - 1)) - bed_zero_ref_x + shift_x;
  5190. current_position[Y_AXIS] = 6.4f + iy * (y_dimension / (y_points_num - 1)) - bed_zero_ref_y + shift_y;
  5191. plan_buffer_line(current_position[X_AXIS], current_position[Y_AXIS], current_position[Z_AXIS], current_position[E_AXIS], XY_AXIS_FEEDRATE, active_extruder);
  5192. st_synchronize();
  5193. if (!find_bed_induction_sensor_point_z(-10.f)) { //if we have data from z calibration max allowed difference is 1mm for each point, if we dont have data max difference is 10mm from initial point
  5194. break;
  5195. card.closefile();
  5196. }
  5197. //memset(numb_wldsd, 0, sizeof(numb_wldsd));
  5198. //dtostrf(d_ReadData(), 8, 5, numb_wldsd);
  5199. //strcat(data_wldsd, numb_wldsd);
  5200. //MYSERIAL.println(data_wldsd);
  5201. //delay(1000);
  5202. //delay(3000);
  5203. //t1 = millis();
  5204. //while (digitalRead(D_DATACLOCK) == LOW) {}
  5205. //while (digitalRead(D_DATACLOCK) == HIGH) {}
  5206. memset(digit, 0, sizeof(digit));
  5207. //cli();
  5208. digitalWrite(D_REQUIRE, LOW);
  5209. for (int i = 0; i<13; i++)
  5210. {
  5211. //t1 = millis();
  5212. for (int j = 0; j < 4; j++)
  5213. {
  5214. while (digitalRead(D_DATACLOCK) == LOW) {}
  5215. while (digitalRead(D_DATACLOCK) == HIGH) {}
  5216. bitWrite(digit[i], j, digitalRead(D_DATA));
  5217. }
  5218. //t_delay = (millis() - t1);
  5219. //SERIAL_PROTOCOLPGM(" ");
  5220. //SERIAL_PROTOCOL_F(t_delay, 5);
  5221. //SERIAL_PROTOCOLPGM(" ");
  5222. }
  5223. //sei();
  5224. digitalWrite(D_REQUIRE, HIGH);
  5225. mergeOutput[0] = '\0';
  5226. output = 0;
  5227. for (int r = 5; r <= 10; r++) //Merge digits
  5228. {
  5229. sprintf(str, "%d", digit[r]);
  5230. strcat(mergeOutput, str);
  5231. }
  5232. output = atof(mergeOutput);
  5233. if (digit[4] == 8) //Handle sign
  5234. {
  5235. output *= -1;
  5236. }
  5237. for (int i = digit[11]; i > 0; i--) //Handle floating point
  5238. {
  5239. output *= 0.1;
  5240. }
  5241. //output = d_ReadData();
  5242. //row[ix] = current_position[Z_AXIS];
  5243. memset(data_wldsd, 0, sizeof(data_wldsd));
  5244. for (int i = 0; i <3; i++) {
  5245. memset(numb_wldsd, 0, sizeof(numb_wldsd));
  5246. dtostrf(current_position[i], 8, 5, numb_wldsd);
  5247. strcat(data_wldsd, numb_wldsd);
  5248. strcat(data_wldsd, ";");
  5249. }
  5250. memset(numb_wldsd, 0, sizeof(numb_wldsd));
  5251. dtostrf(output, 8, 5, numb_wldsd);
  5252. strcat(data_wldsd, numb_wldsd);
  5253. //strcat(data_wldsd, ";");
  5254. card.write_command(data_wldsd);
  5255. //row[ix] = d_ReadData();
  5256. row[ix] = output; // current_position[Z_AXIS];
  5257. if (iy % 2 == 1 ? ix == 0 : ix == x_points_num - 1) {
  5258. for (int i = 0; i < x_points_num; i++) {
  5259. SERIAL_PROTOCOLPGM(" ");
  5260. SERIAL_PROTOCOL_F(row[i], 5);
  5261. }
  5262. SERIAL_PROTOCOLPGM("\n");
  5263. }
  5264. custom_message_state--;
  5265. mesh_point++;
  5266. lcd_update(1);
  5267. }
  5268. card.closefile();
  5269. }
  5270. #endif