Marlin_main.cpp 236 KB

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