Marlin_main.cpp 296 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000600160026003600460056006600760086009601060116012601360146015601660176018601960206021602260236024602560266027602860296030603160326033603460356036603760386039604060416042604360446045604660476048604960506051605260536054605560566057605860596060606160626063606460656066606760686069607060716072607360746075607660776078607960806081608260836084608560866087608860896090609160926093609460956096609760986099610061016102610361046105610661076108610961106111611261136114611561166117611861196120612161226123612461256126612761286129613061316132613361346135613661376138613961406141614261436144614561466147614861496150615161526153615461556156615761586159616061616162616361646165616661676168616961706171617261736174617561766177617861796180618161826183618461856186618761886189619061916192619361946195619661976198619962006201620262036204620562066207620862096210621162126213621462156216621762186219622062216222622362246225622662276228622962306231623262336234623562366237623862396240624162426243624462456246624762486249625062516252625362546255625662576258625962606261626262636264626562666267626862696270627162726273627462756276627762786279628062816282628362846285628662876288628962906291629262936294629562966297629862996300630163026303630463056306630763086309631063116312631363146315631663176318631963206321632263236324632563266327632863296330633163326333633463356336633763386339634063416342634363446345634663476348634963506351635263536354635563566357635863596360636163626363636463656366636763686369637063716372637363746375637663776378637963806381638263836384638563866387638863896390639163926393639463956396639763986399640064016402640364046405640664076408640964106411641264136414641564166417641864196420642164226423642464256426642764286429643064316432643364346435643664376438643964406441644264436444644564466447644864496450645164526453645464556456645764586459646064616462646364646465646664676468646964706471647264736474647564766477647864796480648164826483648464856486648764886489649064916492649364946495649664976498649965006501650265036504650565066507650865096510651165126513651465156516651765186519652065216522652365246525652665276528652965306531653265336534653565366537653865396540654165426543654465456546654765486549655065516552655365546555655665576558655965606561656265636564656565666567656865696570657165726573657465756576657765786579658065816582658365846585658665876588658965906591659265936594659565966597659865996600660166026603660466056606660766086609661066116612661366146615661666176618661966206621662266236624662566266627662866296630663166326633663466356636663766386639664066416642664366446645664666476648664966506651665266536654665566566657665866596660666166626663666466656666666766686669667066716672667366746675667666776678667966806681668266836684668566866687668866896690669166926693669466956696669766986699670067016702670367046705670667076708670967106711671267136714671567166717671867196720672167226723672467256726672767286729673067316732673367346735673667376738673967406741674267436744674567466747674867496750675167526753675467556756675767586759676067616762676367646765676667676768676967706771677267736774677567766777677867796780678167826783678467856786678767886789679067916792679367946795679667976798679968006801680268036804680568066807680868096810681168126813681468156816681768186819682068216822682368246825682668276828682968306831683268336834683568366837683868396840684168426843684468456846684768486849685068516852685368546855685668576858685968606861686268636864686568666867686868696870687168726873687468756876687768786879688068816882688368846885688668876888688968906891689268936894689568966897689868996900690169026903690469056906690769086909691069116912691369146915691669176918691969206921692269236924692569266927692869296930693169326933693469356936693769386939694069416942694369446945694669476948694969506951695269536954695569566957695869596960696169626963696469656966696769686969697069716972697369746975697669776978697969806981698269836984698569866987698869896990699169926993699469956996699769986999700070017002700370047005700670077008700970107011701270137014701570167017701870197020702170227023702470257026702770287029703070317032703370347035703670377038703970407041704270437044704570467047704870497050705170527053705470557056705770587059706070617062706370647065706670677068706970707071707270737074707570767077707870797080708170827083708470857086708770887089709070917092709370947095709670977098709971007101710271037104710571067107710871097110711171127113711471157116711771187119712071217122712371247125712671277128712971307131713271337134713571367137713871397140714171427143714471457146714771487149715071517152715371547155715671577158715971607161716271637164716571667167716871697170717171727173717471757176717771787179718071817182718371847185718671877188718971907191719271937194719571967197719871997200720172027203720472057206720772087209721072117212721372147215721672177218721972207221722272237224722572267227722872297230723172327233723472357236723772387239724072417242724372447245724672477248724972507251725272537254725572567257725872597260726172627263726472657266726772687269727072717272727372747275727672777278727972807281728272837284728572867287728872897290729172927293729472957296729772987299730073017302730373047305730673077308730973107311731273137314731573167317731873197320732173227323732473257326732773287329733073317332733373347335733673377338733973407341734273437344734573467347734873497350735173527353735473557356735773587359736073617362736373647365736673677368736973707371737273737374737573767377737873797380738173827383738473857386738773887389739073917392739373947395739673977398739974007401740274037404740574067407740874097410741174127413741474157416741774187419742074217422742374247425742674277428742974307431743274337434743574367437743874397440744174427443744474457446744774487449745074517452745374547455745674577458745974607461746274637464746574667467746874697470747174727473747474757476747774787479748074817482748374847485748674877488748974907491749274937494749574967497749874997500750175027503750475057506750775087509751075117512751375147515751675177518751975207521752275237524752575267527752875297530753175327533753475357536753775387539754075417542754375447545754675477548754975507551755275537554755575567557755875597560756175627563756475657566756775687569757075717572757375747575757675777578757975807581758275837584758575867587758875897590759175927593759475957596759775987599760076017602760376047605760676077608760976107611761276137614761576167617761876197620762176227623762476257626762776287629763076317632763376347635763676377638763976407641764276437644764576467647764876497650765176527653765476557656765776587659766076617662766376647665766676677668766976707671767276737674767576767677767876797680768176827683768476857686768776887689769076917692769376947695769676977698769977007701770277037704770577067707770877097710771177127713771477157716771777187719772077217722772377247725772677277728772977307731773277337734773577367737773877397740774177427743774477457746774777487749775077517752775377547755775677577758775977607761776277637764776577667767776877697770777177727773777477757776777777787779778077817782778377847785778677877788778977907791779277937794779577967797779877997800780178027803780478057806780778087809781078117812781378147815781678177818781978207821782278237824782578267827782878297830783178327833783478357836783778387839784078417842784378447845784678477848784978507851785278537854785578567857785878597860786178627863786478657866786778687869787078717872787378747875787678777878787978807881788278837884788578867887788878897890789178927893789478957896789778987899790079017902790379047905790679077908790979107911791279137914791579167917791879197920792179227923792479257926792779287929793079317932793379347935793679377938793979407941794279437944794579467947794879497950795179527953795479557956795779587959796079617962796379647965796679677968796979707971797279737974797579767977797879797980798179827983798479857986798779887989799079917992799379947995799679977998799980008001800280038004800580068007800880098010801180128013801480158016801780188019802080218022802380248025802680278028802980308031803280338034803580368037803880398040804180428043804480458046804780488049805080518052805380548055805680578058805980608061806280638064806580668067806880698070807180728073807480758076807780788079808080818082808380848085808680878088808980908091809280938094809580968097809880998100810181028103810481058106810781088109811081118112811381148115811681178118811981208121812281238124812581268127812881298130813181328133813481358136813781388139814081418142814381448145814681478148814981508151815281538154815581568157815881598160816181628163816481658166816781688169817081718172817381748175817681778178817981808181818281838184818581868187818881898190819181928193819481958196819781988199820082018202820382048205820682078208820982108211821282138214821582168217821882198220822182228223822482258226822782288229823082318232823382348235823682378238823982408241824282438244824582468247824882498250825182528253825482558256825782588259826082618262826382648265826682678268826982708271827282738274827582768277827882798280828182828283828482858286828782888289829082918292829382948295829682978298829983008301830283038304830583068307830883098310831183128313831483158316831783188319832083218322832383248325832683278328832983308331833283338334833583368337833883398340834183428343834483458346834783488349835083518352835383548355835683578358835983608361836283638364836583668367836883698370837183728373837483758376837783788379838083818382838383848385838683878388838983908391839283938394839583968397839883998400840184028403840484058406840784088409841084118412841384148415841684178418841984208421842284238424842584268427842884298430843184328433843484358436843784388439844084418442844384448445844684478448844984508451845284538454845584568457845884598460846184628463846484658466846784688469847084718472847384748475847684778478847984808481848284838484848584868487848884898490849184928493849484958496849784988499850085018502850385048505850685078508850985108511851285138514851585168517851885198520852185228523852485258526852785288529853085318532853385348535853685378538853985408541854285438544854585468547854885498550855185528553855485558556855785588559856085618562856385648565856685678568856985708571857285738574857585768577857885798580858185828583858485858586858785888589859085918592859385948595859685978598859986008601860286038604860586068607860886098610861186128613861486158616861786188619862086218622862386248625862686278628862986308631863286338634863586368637863886398640864186428643864486458646864786488649865086518652865386548655865686578658865986608661866286638664866586668667866886698670867186728673867486758676867786788679868086818682868386848685868686878688868986908691869286938694869586968697869886998700870187028703870487058706870787088709871087118712871387148715871687178718871987208721872287238724872587268727872887298730873187328733873487358736873787388739874087418742874387448745874687478748874987508751875287538754875587568757875887598760876187628763876487658766876787688769877087718772877387748775877687778778877987808781878287838784878587868787878887898790879187928793879487958796879787988799880088018802880388048805880688078808880988108811881288138814881588168817881888198820882188228823882488258826882788288829
  1. /* -*- c++ -*- */
  2. /**
  3. * @file
  4. */
  5. /**
  6. * @mainpage Reprap 3D printer firmware based on Sprinter and grbl.
  7. *
  8. * @section intro_sec Introduction
  9. *
  10. * This firmware is a mashup between Sprinter and grbl.
  11. * https://github.com/kliment/Sprinter
  12. * https://github.com/simen/grbl/tree
  13. *
  14. * It has preliminary support for Matthew Roberts advance algorithm
  15. * http://reprap.org/pipermail/reprap-dev/2011-May/003323.html
  16. *
  17. * Prusa Research s.r.o. https://www.prusa3d.cz
  18. *
  19. * @section copyright_sec Copyright
  20. *
  21. * Copyright (C) 2011 Camiel Gubbels / Erik van der Zalm
  22. *
  23. * This program is free software: you can redistribute it and/or modify
  24. * it under the terms of the GNU General Public License as published by
  25. * the Free Software Foundation, either version 3 of the License, or
  26. * (at your option) any later version.
  27. *
  28. * This program is distributed in the hope that it will be useful,
  29. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  30. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  31. * GNU General Public License for more details.
  32. *
  33. * You should have received a copy of the GNU General Public License
  34. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  35. *
  36. * @section notes_sec Notes
  37. *
  38. * * Do not create static objects in global functions.
  39. * Otherwise constructor guard against concurrent calls is generated costing
  40. * about 8B RAM and 14B flash.
  41. *
  42. *
  43. */
  44. #include "Marlin.h"
  45. #ifdef ENABLE_AUTO_BED_LEVELING
  46. #include "vector_3.h"
  47. #ifdef AUTO_BED_LEVELING_GRID
  48. #include "qr_solve.h"
  49. #endif
  50. #endif // ENABLE_AUTO_BED_LEVELING
  51. #ifdef MESH_BED_LEVELING
  52. #include "mesh_bed_leveling.h"
  53. #include "mesh_bed_calibration.h"
  54. #endif
  55. #include "printers.h"
  56. #include "ultralcd.h"
  57. #include "Configuration_prusa.h"
  58. #include "planner.h"
  59. #include "stepper.h"
  60. #include "temperature.h"
  61. #include "motion_control.h"
  62. #include "cardreader.h"
  63. #include "ConfigurationStore.h"
  64. #include "language.h"
  65. #include "pins_arduino.h"
  66. #include "math.h"
  67. #include "util.h"
  68. #include "Timer.h"
  69. #include <avr/wdt.h>
  70. #include <avr/pgmspace.h>
  71. #include "Dcodes.h"
  72. #ifdef SWSPI
  73. #include "swspi.h"
  74. #endif //SWSPI
  75. #ifdef NEW_SPI
  76. #include "spi.h"
  77. #endif //NEW_SPI
  78. #ifdef SWI2C
  79. #include "swi2c.h"
  80. #endif //SWI2C
  81. #ifdef PAT9125
  82. #include "pat9125.h"
  83. #include "fsensor.h"
  84. #endif //PAT9125
  85. #ifdef TMC2130
  86. #include "tmc2130.h"
  87. #endif //TMC2130
  88. #ifdef BLINKM
  89. #include "BlinkM.h"
  90. #include "Wire.h"
  91. #endif
  92. #ifdef ULTRALCD
  93. #include "ultralcd.h"
  94. #endif
  95. #if NUM_SERVOS > 0
  96. #include "Servo.h"
  97. #endif
  98. #if defined(DIGIPOTSS_PIN) && DIGIPOTSS_PIN > -1
  99. #include <SPI.h>
  100. #endif
  101. #define VERSION_STRING "1.0.2"
  102. #include "ultralcd.h"
  103. #include "cmdqueue.h"
  104. // Macros for bit masks
  105. #define BIT(b) (1<<(b))
  106. #define TEST(n,b) (((n)&BIT(b))!=0)
  107. #define SET_BIT(n,b,value) (n) ^= ((-value)^(n)) & (BIT(b))
  108. //Macro for print fan speed
  109. #define FAN_PULSE_WIDTH_LIMIT ((fanSpeed > 100) ? 3 : 4) //time in ms
  110. #define PRINTING_TYPE_SD 0
  111. #define PRINTING_TYPE_USB 1
  112. // look here for descriptions of G-codes: http://linuxcnc.org/handbook/gcode/g-code.html
  113. // http://objects.reprap.org/wiki/Mendel_User_Manual:_RepRapGCodes
  114. //Implemented Codes
  115. //-------------------
  116. // PRUSA CODES
  117. // P F - Returns FW versions
  118. // P R - Returns revision of printer
  119. // G0 -> G1
  120. // G1 - Coordinated Movement X Y Z E
  121. // G2 - CW ARC
  122. // G3 - CCW ARC
  123. // G4 - Dwell S<seconds> or P<milliseconds>
  124. // G10 - retract filament according to settings of M207
  125. // G11 - retract recover filament according to settings of M208
  126. // G28 - Home all Axis
  127. // G29 - Detailed Z-Probe, probes the bed at 3 or more points. Will fail if you haven't homed yet.
  128. // G30 - Single Z Probe, probes bed at current XY location.
  129. // G31 - Dock sled (Z_PROBE_SLED only)
  130. // G32 - Undock sled (Z_PROBE_SLED only)
  131. // G80 - Automatic mesh bed leveling
  132. // G81 - Print bed profile
  133. // G90 - Use Absolute Coordinates
  134. // G91 - Use Relative Coordinates
  135. // G92 - Set current position to coordinates given
  136. // M Codes
  137. // M0 - Unconditional stop - Wait for user to press a button on the LCD (Only if ULTRA_LCD is enabled)
  138. // M1 - Same as M0
  139. // M17 - Enable/Power all stepper motors
  140. // M18 - Disable all stepper motors; same as M84
  141. // M20 - List SD card
  142. // M21 - Init SD card
  143. // M22 - Release SD card
  144. // M23 - Select SD file (M23 filename.g)
  145. // M24 - Start/resume SD print
  146. // M25 - Pause SD print
  147. // M26 - Set SD position in bytes (M26 S12345)
  148. // M27 - Report SD print status
  149. // M28 - Start SD write (M28 filename.g)
  150. // M29 - Stop SD write
  151. // M30 - Delete file from SD (M30 filename.g)
  152. // M31 - Output time since last M109 or SD card start to serial
  153. // M32 - Select file and start SD print (Can be used _while_ printing from SD card files):
  154. // syntax "M32 /path/filename#", or "M32 S<startpos bytes> !filename#"
  155. // Call gcode file : "M32 P !filename#" and return to caller file after finishing (similar to #include).
  156. // The '#' is necessary when calling from within sd files, as it stops buffer prereading
  157. // 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.
  158. // M73 - Show percent done and print time remaining
  159. // M80 - Turn on Power Supply
  160. // M81 - Turn off Power Supply
  161. // M82 - Set E codes absolute (default)
  162. // M83 - Set E codes relative while in Absolute Coordinates (G90) mode
  163. // M84 - Disable steppers until next move,
  164. // or use S<seconds> to specify an inactivity timeout, after which the steppers will be disabled. S0 to disable the timeout.
  165. // M85 - Set inactivity shutdown timer with parameter S<seconds>. To disable set zero (default)
  166. // M92 - Set axis_steps_per_unit - same syntax as G92
  167. // M104 - Set extruder target temp
  168. // M105 - Read current temp
  169. // M106 - Fan on
  170. // M107 - Fan off
  171. // M109 - Sxxx Wait for extruder current temp to reach target temp. Waits only when heating
  172. // Rxxx Wait for extruder current temp to reach target temp. Waits when heating and cooling
  173. // IF AUTOTEMP is enabled, S<mintemp> B<maxtemp> F<factor>. Exit autotemp by any M109 without F
  174. // M112 - Emergency stop
  175. // M113 - Get or set the timeout interval for Host Keepalive "busy" messages
  176. // M114 - Output current position to serial port
  177. // M115 - Capabilities string
  178. // M117 - display message
  179. // M119 - Output Endstop status to serial port
  180. // M126 - Solenoid Air Valve Open (BariCUDA support by jmil)
  181. // M127 - Solenoid Air Valve Closed (BariCUDA vent to atmospheric pressure by jmil)
  182. // M128 - EtoP Open (BariCUDA EtoP = electricity to air pressure transducer by jmil)
  183. // M129 - EtoP Closed (BariCUDA EtoP = electricity to air pressure transducer by jmil)
  184. // M140 - Set bed target temp
  185. // 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.
  186. // M190 - Sxxx Wait for bed current temp to reach target temp. Waits only when heating
  187. // Rxxx Wait for bed current temp to reach target temp. Waits when heating and cooling
  188. // M200 D<millimeters>- set filament diameter and set E axis units to cubic millimeters (use S0 to set back to millimeters).
  189. // M201 - Set max acceleration in units/s^2 for print moves (M201 X1000 Y1000)
  190. // M202 - Set max acceleration in units/s^2 for travel moves (M202 X1000 Y1000) Unused in Marlin!!
  191. // M203 - Set maximum feedrate that your machine can sustain (M203 X200 Y200 Z300 E10000) in mm/sec
  192. // 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
  193. // 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
  194. // M206 - set additional homing offset
  195. // M207 - set retract length S[positive mm] F[feedrate mm/min] Z[additional zlift/hop], stays in mm regardless of M200 setting
  196. // M208 - set recover=unretract length S[positive mm surplus to the M207 S*] F[feedrate mm/sec]
  197. // 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.
  198. // M218 - set hotend offset (in mm): T<extruder_number> X<offset_on_X> Y<offset_on_Y>
  199. // M220 S<factor in percent>- set speed factor override percentage
  200. // M221 S<factor in percent>- set extrude factor override percentage
  201. // M226 P<pin number> S<pin state>- Wait until the specified pin reaches the state required
  202. // M240 - Trigger a camera to take a photograph
  203. // M250 - Set LCD contrast C<contrast value> (value 0..63)
  204. // M280 - set servo position absolute. P: servo index, S: angle or microseconds
  205. // M300 - Play beep sound S<frequency Hz> P<duration ms>
  206. // M301 - Set PID parameters P I and D
  207. // M302 - Allow cold extrudes, or set the minimum extrude S<temperature>.
  208. // M303 - PID relay autotune S<temperature> sets the target temperature. (default target temperature = 150C)
  209. // M304 - Set bed PID parameters P I and D
  210. // M400 - Finish all moves
  211. // M401 - Lower z-probe if present
  212. // M402 - Raise z-probe if present
  213. // M404 - N<dia in mm> Enter the nominal filament width (3mm, 1.75mm ) or will display nominal filament width without parameters
  214. // M405 - Turn on Filament Sensor extrusion control. Optional D<delay in cm> to set delay in centimeters between sensor and extruder
  215. // M406 - Turn off Filament Sensor extrusion control
  216. // M407 - Displays measured filament diameter
  217. // M500 - stores parameters in EEPROM
  218. // M501 - reads parameters from EEPROM (if you need reset them after you changed them temporarily).
  219. // M502 - reverts to the default "factory settings". You still need to store them in EEPROM afterwards if you want to.
  220. // M503 - print the current settings (from memory not from EEPROM)
  221. // M509 - force language selection on next restart
  222. // M540 - Use S[0|1] to enable or disable the stop SD card print on endstop hit (requires ABORT_ON_ENDSTOP_HIT_FEATURE_ENABLED)
  223. // M600 - Pause for filament change X[pos] Y[pos] Z[relative lift] E[initial retract] L[later retract distance for removal]
  224. // M605 - Set dual x-carriage movement mode: S<mode> [ X<duplication x-offset> R<duplication temp offset> ]
  225. // M860 - Wait for PINDA thermistor to reach target temperature.
  226. // M861 - Set / Read PINDA temperature compensation offsets
  227. // M900 - Set LIN_ADVANCE options, if enabled. See Configuration_adv.h for details.
  228. // M907 - Set digital trimpot motor current using axis codes.
  229. // M908 - Control digital trimpot directly.
  230. // M350 - Set microstepping mode.
  231. // M351 - Toggle MS1 MS2 pins directly.
  232. // M928 - Start SD logging (M928 filename.g) - ended by M29
  233. // M999 - Restart after being stopped by error
  234. //Stepper Movement Variables
  235. //===========================================================================
  236. //=============================imported variables============================
  237. //===========================================================================
  238. //===========================================================================
  239. //=============================public variables=============================
  240. //===========================================================================
  241. #ifdef SDSUPPORT
  242. CardReader card;
  243. #endif
  244. unsigned long PingTime = millis();
  245. unsigned long NcTime;
  246. union Data
  247. {
  248. byte b[2];
  249. int value;
  250. };
  251. float homing_feedrate[] = HOMING_FEEDRATE;
  252. // Currently only the extruder axis may be switched to a relative mode.
  253. // Other axes are always absolute or relative based on the common relative_mode flag.
  254. bool axis_relative_modes[] = AXIS_RELATIVE_MODES;
  255. int feedmultiply=100; //100->1 200->2
  256. int saved_feedmultiply;
  257. int extrudemultiply=100; //100->1 200->2
  258. int extruder_multiply[EXTRUDERS] = {100
  259. #if EXTRUDERS > 1
  260. , 100
  261. #if EXTRUDERS > 2
  262. , 100
  263. #endif
  264. #endif
  265. };
  266. int bowden_length[4] = {385, 385, 385, 385};
  267. bool is_usb_printing = false;
  268. bool homing_flag = false;
  269. bool temp_cal_active = false;
  270. unsigned long kicktime = millis()+100000;
  271. unsigned int usb_printing_counter;
  272. int lcd_change_fil_state = 0;
  273. int feedmultiplyBckp = 100;
  274. float HotendTempBckp = 0;
  275. int fanSpeedBckp = 0;
  276. float pause_lastpos[4];
  277. unsigned long pause_time = 0;
  278. unsigned long start_pause_print = millis();
  279. unsigned long t_fan_rising_edge = millis();
  280. static LongTimer safetyTimer;
  281. //unsigned long load_filament_time;
  282. bool mesh_bed_leveling_flag = false;
  283. bool mesh_bed_run_from_menu = false;
  284. //unsigned char lang_selected = 0;
  285. int8_t FarmMode = 0;
  286. bool prusa_sd_card_upload = false;
  287. unsigned int status_number = 0;
  288. unsigned long total_filament_used;
  289. unsigned int heating_status;
  290. unsigned int heating_status_counter;
  291. bool custom_message;
  292. bool loading_flag = false;
  293. unsigned int custom_message_type;
  294. unsigned int custom_message_state;
  295. char snmm_filaments_used = 0;
  296. bool fan_state[2];
  297. int fan_edge_counter[2];
  298. int fan_speed[2];
  299. char dir_names[3][9];
  300. bool sortAlpha = false;
  301. bool volumetric_enabled = false;
  302. float filament_size[EXTRUDERS] = { DEFAULT_NOMINAL_FILAMENT_DIA
  303. #if EXTRUDERS > 1
  304. , DEFAULT_NOMINAL_FILAMENT_DIA
  305. #if EXTRUDERS > 2
  306. , DEFAULT_NOMINAL_FILAMENT_DIA
  307. #endif
  308. #endif
  309. };
  310. float extruder_multiplier[EXTRUDERS] = {1.0
  311. #if EXTRUDERS > 1
  312. , 1.0
  313. #if EXTRUDERS > 2
  314. , 1.0
  315. #endif
  316. #endif
  317. };
  318. float current_position[NUM_AXIS] = { 0.0, 0.0, 0.0, 0.0 };
  319. float add_homing[3]={0,0,0};
  320. float min_pos[3] = { X_MIN_POS, Y_MIN_POS, Z_MIN_POS };
  321. float max_pos[3] = { X_MAX_POS, Y_MAX_POS, Z_MAX_POS };
  322. bool axis_known_position[3] = {false, false, false};
  323. float zprobe_zoffset;
  324. // Extruder offset
  325. #if EXTRUDERS > 1
  326. #define NUM_EXTRUDER_OFFSETS 2 // only in XY plane
  327. float extruder_offset[NUM_EXTRUDER_OFFSETS][EXTRUDERS] = {
  328. #if defined(EXTRUDER_OFFSET_X) && defined(EXTRUDER_OFFSET_Y)
  329. EXTRUDER_OFFSET_X, EXTRUDER_OFFSET_Y
  330. #endif
  331. };
  332. #endif
  333. uint8_t active_extruder = 0;
  334. int fanSpeed=0;
  335. #ifdef FWRETRACT
  336. bool autoretract_enabled=false;
  337. bool retracted[EXTRUDERS]={false
  338. #if EXTRUDERS > 1
  339. , false
  340. #if EXTRUDERS > 2
  341. , false
  342. #endif
  343. #endif
  344. };
  345. bool retracted_swap[EXTRUDERS]={false
  346. #if EXTRUDERS > 1
  347. , false
  348. #if EXTRUDERS > 2
  349. , false
  350. #endif
  351. #endif
  352. };
  353. float retract_length = RETRACT_LENGTH;
  354. float retract_length_swap = RETRACT_LENGTH_SWAP;
  355. float retract_feedrate = RETRACT_FEEDRATE;
  356. float retract_zlift = RETRACT_ZLIFT;
  357. float retract_recover_length = RETRACT_RECOVER_LENGTH;
  358. float retract_recover_length_swap = RETRACT_RECOVER_LENGTH_SWAP;
  359. float retract_recover_feedrate = RETRACT_RECOVER_FEEDRATE;
  360. #endif
  361. #ifdef ULTIPANEL
  362. #ifdef PS_DEFAULT_OFF
  363. bool powersupply = false;
  364. #else
  365. bool powersupply = true;
  366. #endif
  367. #endif
  368. bool cancel_heatup = false ;
  369. #ifdef HOST_KEEPALIVE_FEATURE
  370. int busy_state = NOT_BUSY;
  371. static long prev_busy_signal_ms = -1;
  372. uint8_t host_keepalive_interval = HOST_KEEPALIVE_INTERVAL;
  373. #else
  374. #define host_keepalive();
  375. #define KEEPALIVE_STATE(n);
  376. #endif
  377. const char errormagic[] PROGMEM = "Error:";
  378. const char echomagic[] PROGMEM = "echo:";
  379. bool no_response = false;
  380. uint8_t important_status;
  381. uint8_t saved_filament_type;
  382. // save/restore printing
  383. bool saved_printing = false;
  384. // storing estimated time to end of print counted by slicer
  385. uint8_t print_percent_done_normal = PRINT_PERCENT_DONE_INIT;
  386. uint16_t print_time_remaining_normal = PRINT_TIME_REMAINING_INIT; //estimated remaining print time in minutes
  387. uint8_t print_percent_done_silent = PRINT_PERCENT_DONE_INIT;
  388. uint16_t print_time_remaining_silent = PRINT_TIME_REMAINING_INIT; //estimated remaining print time in minutes
  389. //===========================================================================
  390. //=============================Private Variables=============================
  391. //===========================================================================
  392. const char axis_codes[NUM_AXIS] = {'X', 'Y', 'Z', 'E'};
  393. float destination[NUM_AXIS] = { 0.0, 0.0, 0.0, 0.0};
  394. static float delta[3] = {0.0, 0.0, 0.0};
  395. // For tracing an arc
  396. static float offset[3] = {0.0, 0.0, 0.0};
  397. static float feedrate = 1500.0, next_feedrate, saved_feedrate;
  398. // Determines Absolute or Relative Coordinates.
  399. // Also there is bool axis_relative_modes[] per axis flag.
  400. static bool relative_mode = false;
  401. #ifndef _DISABLE_M42_M226
  402. const int sensitive_pins[] = SENSITIVE_PINS; // Sensitive pin list for M42
  403. #endif //_DISABLE_M42_M226
  404. //static float tt = 0;
  405. //static float bt = 0;
  406. //Inactivity shutdown variables
  407. static unsigned long previous_millis_cmd = 0;
  408. unsigned long max_inactive_time = 0;
  409. static unsigned long stepper_inactive_time = DEFAULT_STEPPER_DEACTIVE_TIME*1000l;
  410. unsigned long starttime=0;
  411. unsigned long stoptime=0;
  412. unsigned long _usb_timer = 0;
  413. static uint8_t tmp_extruder;
  414. bool extruder_under_pressure = true;
  415. bool Stopped=false;
  416. #if NUM_SERVOS > 0
  417. Servo servos[NUM_SERVOS];
  418. #endif
  419. bool CooldownNoWait = true;
  420. bool target_direction;
  421. //Insert variables if CHDK is defined
  422. #ifdef CHDK
  423. unsigned long chdkHigh = 0;
  424. boolean chdkActive = false;
  425. #endif
  426. // save/restore printing
  427. static uint32_t saved_sdpos = 0;
  428. static uint8_t saved_printing_type = PRINTING_TYPE_SD;
  429. static float saved_pos[4] = { 0, 0, 0, 0 };
  430. // Feedrate hopefully derived from an active block of the planner at the time the print has been canceled, in mm/min.
  431. static float saved_feedrate2 = 0;
  432. static uint8_t saved_active_extruder = 0;
  433. static bool saved_extruder_under_pressure = false;
  434. //===========================================================================
  435. //=============================Routines======================================
  436. //===========================================================================
  437. void get_arc_coordinates();
  438. bool setTargetedHotend(int code);
  439. void serial_echopair_P(const char *s_P, float v)
  440. { serialprintPGM(s_P); SERIAL_ECHO(v); }
  441. void serial_echopair_P(const char *s_P, double v)
  442. { serialprintPGM(s_P); SERIAL_ECHO(v); }
  443. void serial_echopair_P(const char *s_P, unsigned long v)
  444. { serialprintPGM(s_P); SERIAL_ECHO(v); }
  445. #ifdef SDSUPPORT
  446. #include "SdFatUtil.h"
  447. int freeMemory() { return SdFatUtil::FreeRam(); }
  448. #else
  449. extern "C" {
  450. extern unsigned int __bss_end;
  451. extern unsigned int __heap_start;
  452. extern void *__brkval;
  453. int freeMemory() {
  454. int free_memory;
  455. if ((int)__brkval == 0)
  456. free_memory = ((int)&free_memory) - ((int)&__bss_end);
  457. else
  458. free_memory = ((int)&free_memory) - ((int)__brkval);
  459. return free_memory;
  460. }
  461. }
  462. #endif //!SDSUPPORT
  463. void setup_killpin()
  464. {
  465. #if defined(KILL_PIN) && KILL_PIN > -1
  466. SET_INPUT(KILL_PIN);
  467. WRITE(KILL_PIN,HIGH);
  468. #endif
  469. }
  470. // Set home pin
  471. void setup_homepin(void)
  472. {
  473. #if defined(HOME_PIN) && HOME_PIN > -1
  474. SET_INPUT(HOME_PIN);
  475. WRITE(HOME_PIN,HIGH);
  476. #endif
  477. }
  478. void setup_photpin()
  479. {
  480. #if defined(PHOTOGRAPH_PIN) && PHOTOGRAPH_PIN > -1
  481. SET_OUTPUT(PHOTOGRAPH_PIN);
  482. WRITE(PHOTOGRAPH_PIN, LOW);
  483. #endif
  484. }
  485. void setup_powerhold()
  486. {
  487. #if defined(SUICIDE_PIN) && SUICIDE_PIN > -1
  488. SET_OUTPUT(SUICIDE_PIN);
  489. WRITE(SUICIDE_PIN, HIGH);
  490. #endif
  491. #if defined(PS_ON_PIN) && PS_ON_PIN > -1
  492. SET_OUTPUT(PS_ON_PIN);
  493. #if defined(PS_DEFAULT_OFF)
  494. WRITE(PS_ON_PIN, PS_ON_ASLEEP);
  495. #else
  496. WRITE(PS_ON_PIN, PS_ON_AWAKE);
  497. #endif
  498. #endif
  499. }
  500. void suicide()
  501. {
  502. #if defined(SUICIDE_PIN) && SUICIDE_PIN > -1
  503. SET_OUTPUT(SUICIDE_PIN);
  504. WRITE(SUICIDE_PIN, LOW);
  505. #endif
  506. }
  507. void servo_init()
  508. {
  509. #if (NUM_SERVOS >= 1) && defined(SERVO0_PIN) && (SERVO0_PIN > -1)
  510. servos[0].attach(SERVO0_PIN);
  511. #endif
  512. #if (NUM_SERVOS >= 2) && defined(SERVO1_PIN) && (SERVO1_PIN > -1)
  513. servos[1].attach(SERVO1_PIN);
  514. #endif
  515. #if (NUM_SERVOS >= 3) && defined(SERVO2_PIN) && (SERVO2_PIN > -1)
  516. servos[2].attach(SERVO2_PIN);
  517. #endif
  518. #if (NUM_SERVOS >= 4) && defined(SERVO3_PIN) && (SERVO3_PIN > -1)
  519. servos[3].attach(SERVO3_PIN);
  520. #endif
  521. #if (NUM_SERVOS >= 5)
  522. #error "TODO: enter initalisation code for more servos"
  523. #endif
  524. }
  525. static void lcd_language_menu();
  526. void stop_and_save_print_to_ram(float z_move, float e_move);
  527. void restore_print_from_ram_and_continue(float e_move);
  528. bool fans_check_enabled = true;
  529. bool filament_autoload_enabled = true;
  530. #ifdef TMC2130
  531. extern int8_t CrashDetectMenu;
  532. void crashdet_enable()
  533. {
  534. // MYSERIAL.println("crashdet_enable");
  535. tmc2130_sg_stop_on_crash = true;
  536. eeprom_update_byte((uint8_t*)EEPROM_CRASH_DET, 0xFF);
  537. CrashDetectMenu = 1;
  538. }
  539. void crashdet_disable()
  540. {
  541. // MYSERIAL.println("crashdet_disable");
  542. tmc2130_sg_stop_on_crash = false;
  543. tmc2130_sg_crash = 0;
  544. eeprom_update_byte((uint8_t*)EEPROM_CRASH_DET, 0x00);
  545. CrashDetectMenu = 0;
  546. }
  547. void crashdet_stop_and_save_print()
  548. {
  549. stop_and_save_print_to_ram(10, -2); //XY - no change, Z 10mm up, E -2mm retract
  550. }
  551. void crashdet_restore_print_and_continue()
  552. {
  553. restore_print_from_ram_and_continue(2); //XYZ = orig, E +2mm unretract
  554. // babystep_apply();
  555. }
  556. void crashdet_stop_and_save_print2()
  557. {
  558. cli();
  559. planner_abort_hard(); //abort printing
  560. cmdqueue_reset(); //empty cmdqueue
  561. card.sdprinting = false;
  562. card.closefile();
  563. // Reset and re-enable the stepper timer just before the global interrupts are enabled.
  564. st_reset_timer();
  565. sei();
  566. }
  567. void crashdet_detected(uint8_t mask)
  568. {
  569. // printf("CRASH_DETECTED");
  570. /* while (!is_buffer_empty())
  571. {
  572. process_commands();
  573. cmdqueue_pop_front();
  574. }*/
  575. st_synchronize();
  576. lcd_update_enable(true);
  577. lcd_implementation_clear();
  578. lcd_update(2);
  579. if (mask & X_AXIS_MASK)
  580. {
  581. eeprom_update_byte((uint8_t*)EEPROM_CRASH_COUNT_X, eeprom_read_byte((uint8_t*)EEPROM_CRASH_COUNT_X) + 1);
  582. eeprom_update_word((uint16_t*)EEPROM_CRASH_COUNT_X_TOT, eeprom_read_word((uint16_t*)EEPROM_CRASH_COUNT_X_TOT) + 1);
  583. }
  584. if (mask & Y_AXIS_MASK)
  585. {
  586. eeprom_update_byte((uint8_t*)EEPROM_CRASH_COUNT_Y, eeprom_read_byte((uint8_t*)EEPROM_CRASH_COUNT_Y) + 1);
  587. eeprom_update_word((uint16_t*)EEPROM_CRASH_COUNT_Y_TOT, eeprom_read_word((uint16_t*)EEPROM_CRASH_COUNT_Y_TOT) + 1);
  588. }
  589. #ifdef AUTOMATIC_RECOVERY_AFTER_CRASH
  590. bool yesno = true;
  591. #else
  592. bool yesno = lcd_show_fullscreen_message_yes_no_and_wait_P(_T(MSG_CRASH_DETECTED), false);
  593. #endif
  594. lcd_update_enable(true);
  595. lcd_update(2);
  596. lcd_setstatuspgm(_T(MSG_CRASH_DETECTED));
  597. if (yesno)
  598. {
  599. enquecommand_P(PSTR("G28 X Y"));
  600. enquecommand_P(PSTR("CRASH_RECOVER"));
  601. }
  602. else
  603. {
  604. enquecommand_P(PSTR("CRASH_CANCEL"));
  605. }
  606. }
  607. void crashdet_recover()
  608. {
  609. crashdet_restore_print_and_continue();
  610. tmc2130_sg_stop_on_crash = true;
  611. }
  612. void crashdet_cancel()
  613. {
  614. card.sdprinting = false;
  615. card.closefile();
  616. tmc2130_sg_stop_on_crash = true;
  617. }
  618. #endif //TMC2130
  619. void failstats_reset_print()
  620. {
  621. eeprom_update_byte((uint8_t *)EEPROM_CRASH_COUNT_X, 0);
  622. eeprom_update_byte((uint8_t *)EEPROM_CRASH_COUNT_Y, 0);
  623. eeprom_update_byte((uint8_t *)EEPROM_FERROR_COUNT, 0);
  624. eeprom_update_byte((uint8_t *)EEPROM_POWER_COUNT, 0);
  625. }
  626. #ifdef MESH_BED_LEVELING
  627. enum MeshLevelingState { MeshReport, MeshStart, MeshNext, MeshSet };
  628. #endif
  629. // Factory reset function
  630. // This function is used to erase parts or whole EEPROM memory which is used for storing calibration and and so on.
  631. // Level input parameter sets depth of reset
  632. // Quiet parameter masks all waitings for user interact.
  633. int er_progress = 0;
  634. void factory_reset(char level, bool quiet)
  635. {
  636. lcd_implementation_clear();
  637. int cursor_pos = 0;
  638. switch (level) {
  639. // Level 0: Language reset
  640. case 0:
  641. WRITE(BEEPER, HIGH);
  642. _delay_ms(100);
  643. WRITE(BEEPER, LOW);
  644. lcd_force_language_selection();
  645. break;
  646. //Level 1: Reset statistics
  647. case 1:
  648. WRITE(BEEPER, HIGH);
  649. _delay_ms(100);
  650. WRITE(BEEPER, LOW);
  651. eeprom_update_dword((uint32_t *)EEPROM_TOTALTIME, 0);
  652. eeprom_update_dword((uint32_t *)EEPROM_FILAMENTUSED, 0);
  653. eeprom_update_byte((uint8_t *)EEPROM_CRASH_COUNT_X, 0);
  654. eeprom_update_byte((uint8_t *)EEPROM_CRASH_COUNT_Y, 0);
  655. eeprom_update_byte((uint8_t *)EEPROM_FERROR_COUNT, 0);
  656. eeprom_update_byte((uint8_t *)EEPROM_POWER_COUNT, 0);
  657. eeprom_update_word((uint16_t *)EEPROM_CRASH_COUNT_X_TOT, 0);
  658. eeprom_update_word((uint16_t *)EEPROM_CRASH_COUNT_Y_TOT, 0);
  659. eeprom_update_word((uint16_t *)EEPROM_FERROR_COUNT_TOT, 0);
  660. eeprom_update_word((uint16_t *)EEPROM_POWER_COUNT_TOT, 0);
  661. lcd_menu_statistics();
  662. break;
  663. // Level 2: Prepare for shipping
  664. case 2:
  665. //lcd_printPGM(PSTR("Factory RESET"));
  666. //lcd_print_at_PGM(1,2,PSTR("Shipping prep"));
  667. // Force language selection at the next boot up.
  668. lcd_force_language_selection();
  669. // Force the "Follow calibration flow" message at the next boot up.
  670. calibration_status_store(CALIBRATION_STATUS_Z_CALIBRATION);
  671. eeprom_write_byte((uint8_t*)EEPROM_WIZARD_ACTIVE, 1); //run wizard
  672. farm_no = 0;
  673. //*** MaR::180501_01
  674. farm_mode = false;
  675. eeprom_update_byte((uint8_t*)EEPROM_FARM_MODE, farm_mode);
  676. EEPROM_save_B(EEPROM_FARM_NUMBER, &farm_no);
  677. WRITE(BEEPER, HIGH);
  678. _delay_ms(100);
  679. WRITE(BEEPER, LOW);
  680. //_delay_ms(2000);
  681. break;
  682. // Level 3: erase everything, whole EEPROM will be set to 0xFF
  683. case 3:
  684. lcd_printPGM(PSTR("Factory RESET"));
  685. lcd_print_at_PGM(1, 2, PSTR("ERASING all data"));
  686. WRITE(BEEPER, HIGH);
  687. _delay_ms(100);
  688. WRITE(BEEPER, LOW);
  689. er_progress = 0;
  690. lcd_print_at_PGM(3, 3, PSTR(" "));
  691. lcd_implementation_print_at(3, 3, er_progress);
  692. // Erase EEPROM
  693. for (int i = 0; i < 4096; i++) {
  694. eeprom_write_byte((uint8_t*)i, 0xFF);
  695. if (i % 41 == 0) {
  696. er_progress++;
  697. lcd_print_at_PGM(3, 3, PSTR(" "));
  698. lcd_implementation_print_at(3, 3, er_progress);
  699. lcd_printPGM(PSTR("%"));
  700. }
  701. }
  702. break;
  703. case 4:
  704. bowden_menu();
  705. break;
  706. default:
  707. break;
  708. }
  709. }
  710. #include "LiquidCrystal_Prusa.h"
  711. extern LiquidCrystal_Prusa lcd;
  712. FILE _lcdout = {0};
  713. int lcd_putchar(char c, FILE *stream)
  714. {
  715. lcd.write(c);
  716. return 0;
  717. }
  718. FILE _uartout = {0};
  719. int uart_putchar(char c, FILE *stream)
  720. {
  721. MYSERIAL.write(c);
  722. return 0;
  723. }
  724. void lcd_splash()
  725. {
  726. // lcd_print_at_PGM(0, 1, PSTR(" Original Prusa "));
  727. // lcd_print_at_PGM(0, 2, PSTR(" 3D Printers "));
  728. // lcd.print_P(PSTR("\x1b[1;3HOriginal Prusa\x1b[2;4H3D Printers"));
  729. fputs_P(PSTR(ESC_2J ESC_H(1,1) "Original Prusa i3" ESC_H(3,2) "Prusa Research"), lcdout);
  730. }
  731. void factory_reset()
  732. {
  733. KEEPALIVE_STATE(PAUSED_FOR_USER);
  734. if (!READ(BTN_ENC))
  735. {
  736. _delay_ms(1000);
  737. if (!READ(BTN_ENC))
  738. {
  739. lcd_implementation_clear();
  740. lcd_printPGM(PSTR("Factory RESET"));
  741. SET_OUTPUT(BEEPER);
  742. WRITE(BEEPER, HIGH);
  743. while (!READ(BTN_ENC));
  744. WRITE(BEEPER, LOW);
  745. _delay_ms(2000);
  746. char level = reset_menu();
  747. factory_reset(level, false);
  748. switch (level) {
  749. case 0: _delay_ms(0); break;
  750. case 1: _delay_ms(0); break;
  751. case 2: _delay_ms(0); break;
  752. case 3: _delay_ms(0); break;
  753. }
  754. // _delay_ms(100);
  755. /*
  756. #ifdef MESH_BED_LEVELING
  757. _delay_ms(2000);
  758. if (!READ(BTN_ENC))
  759. {
  760. WRITE(BEEPER, HIGH);
  761. _delay_ms(100);
  762. WRITE(BEEPER, LOW);
  763. _delay_ms(200);
  764. WRITE(BEEPER, HIGH);
  765. _delay_ms(100);
  766. WRITE(BEEPER, LOW);
  767. int _z = 0;
  768. calibration_status_store(CALIBRATION_STATUS_CALIBRATED);
  769. EEPROM_save_B(EEPROM_BABYSTEP_X, &_z);
  770. EEPROM_save_B(EEPROM_BABYSTEP_Y, &_z);
  771. EEPROM_save_B(EEPROM_BABYSTEP_Z, &_z);
  772. }
  773. else
  774. {
  775. WRITE(BEEPER, HIGH);
  776. _delay_ms(100);
  777. WRITE(BEEPER, LOW);
  778. }
  779. #endif // mesh */
  780. }
  781. }
  782. else
  783. {
  784. //_delay_ms(1000); // wait 1sec to display the splash screen // what's this and why do we need it?? - andre
  785. }
  786. KEEPALIVE_STATE(IN_HANDLER);
  787. }
  788. void show_fw_version_warnings() {
  789. if (FW_DEV_VERSION == FW_VERSION_GOLD || FW_DEV_VERSION == FW_VERSION_RC) return;
  790. switch (FW_DEV_VERSION) {
  791. case(FW_VERSION_ALPHA): lcd_show_fullscreen_message_and_wait_P(_i("You are using firmware alpha version. This is development version. Using this version is not recommended and may cause printer damage.")); break;////MSG_FW_VERSION_ALPHA c=20 r=8
  792. case(FW_VERSION_BETA): lcd_show_fullscreen_message_and_wait_P(_i("You are using firmware beta version. This is development version. Using this version is not recommended and may cause printer damage.")); break;////MSG_FW_VERSION_BETA c=20 r=8
  793. case(FW_VERSION_DEVEL):
  794. case(FW_VERSION_DEBUG):
  795. lcd_update_enable(false);
  796. lcd_implementation_clear();
  797. #if FW_DEV_VERSION == FW_VERSION_DEVEL
  798. lcd_print_at_PGM(0, 0, PSTR("Development build !!"));
  799. #else
  800. lcd_print_at_PGM(0, 0, PSTR("Debbugging build !!!"));
  801. #endif
  802. lcd_print_at_PGM(0, 1, PSTR("May destroy printer!"));
  803. lcd_print_at_PGM(0, 2, PSTR("ver ")); lcd_printPGM(PSTR(FW_VERSION_FULL));
  804. lcd_print_at_PGM(0, 3, PSTR(FW_REPOSITORY));
  805. lcd_wait_for_click();
  806. break;
  807. default: lcd_show_fullscreen_message_and_wait_P(_i("WARNING: This is an unofficial, unsupported build. Use at your own risk!")); break;////MSG_FW_VERSION_UNKNOWN c=20 r=8
  808. }
  809. lcd_update_enable(true);
  810. }
  811. uint8_t check_printer_version()
  812. {
  813. uint8_t version_changed = 0;
  814. uint16_t printer_type = eeprom_read_word((uint16_t*)EEPROM_PRINTER_TYPE);
  815. uint16_t motherboard = eeprom_read_word((uint16_t*)EEPROM_BOARD_TYPE);
  816. if (printer_type != PRINTER_TYPE) {
  817. if (printer_type == 0xffff) eeprom_write_word((uint16_t*)EEPROM_PRINTER_TYPE, PRINTER_TYPE);
  818. else version_changed |= 0b10;
  819. }
  820. if (motherboard != MOTHERBOARD) {
  821. if(motherboard == 0xffff) eeprom_write_word((uint16_t*)EEPROM_BOARD_TYPE, MOTHERBOARD);
  822. else version_changed |= 0b01;
  823. }
  824. return version_changed;
  825. }
  826. void erase_eeprom_section(uint16_t offset, uint16_t bytes)
  827. {
  828. for (int i = offset; i < (offset+bytes); i++) eeprom_write_byte((uint8_t*)i, 0xFF);
  829. }
  830. #include "bootapp.h"
  831. void __test()
  832. {
  833. cli();
  834. boot_app_magic = 0x55aa55aa;
  835. boot_app_flags = BOOT_APP_FLG_USER0;
  836. boot_reserved = 0x00;
  837. wdt_enable(WDTO_15MS);
  838. while(1);
  839. }
  840. void upgrade_sec_lang_from_external_flash()
  841. {
  842. if ((boot_app_magic == 0x55aa55aa) && (boot_app_flags & BOOT_APP_FLG_USER0))
  843. {
  844. fprintf_P(lcdout, PSTR(ESC_2J ESC_H(1,1) "TEST %d"), boot_reserved);
  845. boot_reserved++;
  846. if (boot_reserved < 4)
  847. {
  848. _delay_ms(1000);
  849. cli();
  850. wdt_enable(WDTO_15MS);
  851. while(1);
  852. }
  853. }
  854. boot_app_flags &= ~BOOT_APP_FLG_USER0;
  855. }
  856. // "Setup" function is called by the Arduino framework on startup.
  857. // Before startup, the Timers-functions (PWM)/Analog RW and HardwareSerial provided by the Arduino-code
  858. // are initialized by the main() routine provided by the Arduino framework.
  859. void setup()
  860. {
  861. lcd_init();
  862. fdev_setup_stream(lcdout, lcd_putchar, NULL, _FDEV_SETUP_WRITE); //setup lcdout stream
  863. // upgrade_sec_lang_from_external_flash();
  864. lcd_splash();
  865. setup_killpin();
  866. setup_powerhold();
  867. //*** MaR::180501_02b
  868. farm_mode = eeprom_read_byte((uint8_t*)EEPROM_FARM_MODE);
  869. EEPROM_read_B(EEPROM_FARM_NUMBER, &farm_no);
  870. if ((farm_mode == 0xFF && farm_no == 0) || ((uint16_t)farm_no == 0xFFFF))
  871. 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
  872. if ((uint16_t)farm_no == 0xFFFF) farm_no = 0;
  873. selectedSerialPort = eeprom_read_byte((uint8_t*)EEPROM_SECOND_SERIAL_ACTIVE);
  874. if (selectedSerialPort == 0xFF) selectedSerialPort = 0;
  875. if (farm_mode)
  876. {
  877. no_response = true; //we need confirmation by recieving PRUSA thx
  878. important_status = 8;
  879. prusa_statistics(8);
  880. selectedSerialPort = 1;
  881. }
  882. MYSERIAL.begin(BAUDRATE);
  883. fdev_setup_stream(uartout, uart_putchar, NULL, _FDEV_SETUP_WRITE); //setup uart out stream
  884. stdout = uartout;
  885. SERIAL_PROTOCOLLNPGM("start");
  886. SERIAL_ECHO_START;
  887. printf_P(PSTR(" " FW_VERSION_FULL "\n"));
  888. #if 0
  889. SERIAL_ECHOLN("Reading eeprom from 0 to 100: start");
  890. for (int i = 0; i < 4096; ++i) {
  891. int b = eeprom_read_byte((unsigned char*)i);
  892. if (b != 255) {
  893. SERIAL_ECHO(i);
  894. SERIAL_ECHO(":");
  895. SERIAL_ECHO(b);
  896. SERIAL_ECHOLN("");
  897. }
  898. }
  899. SERIAL_ECHOLN("Reading eeprom from 0 to 100: done");
  900. #endif
  901. // Check startup - does nothing if bootloader sets MCUSR to 0
  902. byte mcu = MCUSR;
  903. /* if (mcu & 1) SERIAL_ECHOLNRPGM(_T(MSG_POWERUP));
  904. if (mcu & 2) SERIAL_ECHOLNRPGM(MSG_EXTERNAL_RESET);
  905. if (mcu & 4) SERIAL_ECHOLNRPGM(MSG_BROWNOUT_RESET);
  906. if (mcu & 8) SERIAL_ECHOLNRPGM(MSG_WATCHDOG_RESET);
  907. if (mcu & 32) SERIAL_ECHOLNRPGM(MSG_SOFTWARE_RESET);*/
  908. if (mcu & 1) puts_P(_T(MSG_POWERUP));
  909. if (mcu & 2) puts_P(MSG_EXTERNAL_RESET);
  910. if (mcu & 4) puts_P(MSG_BROWNOUT_RESET);
  911. if (mcu & 8) puts_P(MSG_WATCHDOG_RESET);
  912. if (mcu & 32) puts_P(MSG_SOFTWARE_RESET);
  913. MCUSR = 0;
  914. //SERIAL_ECHORPGM(MSG_MARLIN);
  915. //SERIAL_ECHOLNRPGM(VERSION_STRING);
  916. #ifdef STRING_VERSION_CONFIG_H
  917. #ifdef STRING_CONFIG_H_AUTHOR
  918. SERIAL_ECHO_START;
  919. SERIAL_ECHORPGM(_i(" Last Updated: "));////MSG_CONFIGURATION_VER c=0 r=0
  920. SERIAL_ECHOPGM(STRING_VERSION_CONFIG_H);
  921. SERIAL_ECHORPGM(_n(" | Author: "));////MSG_AUTHOR c=0 r=0
  922. SERIAL_ECHOLNPGM(STRING_CONFIG_H_AUTHOR);
  923. SERIAL_ECHOPGM("Compiled: ");
  924. SERIAL_ECHOLNPGM(__DATE__);
  925. #endif
  926. #endif
  927. SERIAL_ECHO_START;
  928. SERIAL_ECHORPGM(_i(" Free Memory: "));////MSG_FREE_MEMORY c=0 r=0
  929. SERIAL_ECHO(freeMemory());
  930. SERIAL_ECHORPGM(_i(" PlannerBufferBytes: "));////MSG_PLANNER_BUFFER_BYTES c=0 r=0
  931. SERIAL_ECHOLN((int)sizeof(block_t)*BLOCK_BUFFER_SIZE);
  932. //lcd_update_enable(false); // why do we need this?? - andre
  933. // loads data from EEPROM if available else uses defaults (and resets step acceleration rate)
  934. bool previous_settings_retrieved = false;
  935. uint8_t hw_changed = check_printer_version();
  936. if (!(hw_changed & 0b10)) { //if printer version wasn't changed, check for eeprom version and retrieve settings from eeprom in case that version wasn't changed
  937. previous_settings_retrieved = Config_RetrieveSettings(EEPROM_OFFSET);
  938. }
  939. else { //printer version was changed so use default settings
  940. Config_ResetDefault();
  941. }
  942. SdFatUtil::set_stack_guard(); //writes magic number at the end of static variables to protect against overwriting static memory by stack
  943. tp_init(); // Initialize temperature loop
  944. lcd_splash(); // we need to do this again, because tp_init() kills lcd
  945. plan_init(); // Initialize planner;
  946. factory_reset();
  947. #ifdef TMC2130
  948. uint8_t silentMode = eeprom_read_byte((uint8_t*)EEPROM_SILENT);
  949. if (silentMode == 0xff) silentMode = 0;
  950. // tmc2130_mode = silentMode?TMC2130_MODE_SILENT:TMC2130_MODE_NORMAL;
  951. tmc2130_mode = TMC2130_MODE_NORMAL;
  952. uint8_t crashdet = eeprom_read_byte((uint8_t*)EEPROM_CRASH_DET);
  953. if (crashdet && !farm_mode)
  954. {
  955. crashdet_enable();
  956. MYSERIAL.println("CrashDetect ENABLED!");
  957. }
  958. else
  959. {
  960. crashdet_disable();
  961. MYSERIAL.println("CrashDetect DISABLED");
  962. }
  963. #ifdef TMC2130_LINEARITY_CORRECTION
  964. #ifdef TMC2130_LINEARITY_CORRECTION_XYZ
  965. tmc2130_wave_fac[X_AXIS] = eeprom_read_byte((uint8_t*)EEPROM_TMC2130_WAVE_X_FAC);
  966. tmc2130_wave_fac[Y_AXIS] = eeprom_read_byte((uint8_t*)EEPROM_TMC2130_WAVE_Y_FAC);
  967. tmc2130_wave_fac[Z_AXIS] = eeprom_read_byte((uint8_t*)EEPROM_TMC2130_WAVE_Z_FAC);
  968. #endif //TMC2130_LINEARITY_CORRECTION_XYZ
  969. tmc2130_wave_fac[E_AXIS] = eeprom_read_byte((uint8_t*)EEPROM_TMC2130_WAVE_E_FAC);
  970. if (tmc2130_wave_fac[X_AXIS] == 0xff) tmc2130_wave_fac[X_AXIS] = 0;
  971. if (tmc2130_wave_fac[Y_AXIS] == 0xff) tmc2130_wave_fac[Y_AXIS] = 0;
  972. if (tmc2130_wave_fac[Z_AXIS] == 0xff) tmc2130_wave_fac[Z_AXIS] = 0;
  973. if (tmc2130_wave_fac[E_AXIS] == 0xff) tmc2130_wave_fac[E_AXIS] = 0;
  974. #endif //TMC2130_LINEARITY_CORRECTION
  975. #ifdef TMC2130_VARIABLE_RESOLUTION
  976. tmc2130_mres[X_AXIS] = eeprom_read_byte((uint8_t*)EEPROM_TMC2130_X_MRES);
  977. tmc2130_mres[Y_AXIS] = eeprom_read_byte((uint8_t*)EEPROM_TMC2130_Y_MRES);
  978. tmc2130_mres[Z_AXIS] = eeprom_read_byte((uint8_t*)EEPROM_TMC2130_Z_MRES);
  979. tmc2130_mres[E_AXIS] = eeprom_read_byte((uint8_t*)EEPROM_TMC2130_E_MRES);
  980. if (tmc2130_mres[X_AXIS] == 0xff) tmc2130_mres[X_AXIS] = tmc2130_usteps2mres(TMC2130_USTEPS_XY);
  981. if (tmc2130_mres[Y_AXIS] == 0xff) tmc2130_mres[Y_AXIS] = tmc2130_usteps2mres(TMC2130_USTEPS_XY);
  982. if (tmc2130_mres[Z_AXIS] == 0xff) tmc2130_mres[Z_AXIS] = tmc2130_usteps2mres(TMC2130_USTEPS_Z);
  983. if (tmc2130_mres[E_AXIS] == 0xff) tmc2130_mres[E_AXIS] = tmc2130_usteps2mres(TMC2130_USTEPS_E);
  984. eeprom_update_byte((uint8_t*)EEPROM_TMC2130_X_MRES, tmc2130_mres[X_AXIS]);
  985. eeprom_update_byte((uint8_t*)EEPROM_TMC2130_Y_MRES, tmc2130_mres[Y_AXIS]);
  986. eeprom_update_byte((uint8_t*)EEPROM_TMC2130_Z_MRES, tmc2130_mres[Z_AXIS]);
  987. eeprom_update_byte((uint8_t*)EEPROM_TMC2130_E_MRES, tmc2130_mres[E_AXIS]);
  988. #else //TMC2130_VARIABLE_RESOLUTION
  989. tmc2130_mres[X_AXIS] = tmc2130_usteps2mres(TMC2130_USTEPS_XY);
  990. tmc2130_mres[Y_AXIS] = tmc2130_usteps2mres(TMC2130_USTEPS_XY);
  991. tmc2130_mres[Z_AXIS] = tmc2130_usteps2mres(TMC2130_USTEPS_Z);
  992. tmc2130_mres[E_AXIS] = tmc2130_usteps2mres(TMC2130_USTEPS_E);
  993. #endif //TMC2130_VARIABLE_RESOLUTION
  994. #endif //TMC2130
  995. #ifdef NEW_SPI
  996. spi_init();
  997. #endif //NEW_SPI
  998. st_init(); // Initialize stepper, this enables interrupts!
  999. #ifdef TMC2130
  1000. tmc2130_mode = silentMode?TMC2130_MODE_SILENT:TMC2130_MODE_NORMAL;
  1001. tmc2130_init();
  1002. #endif //TMC2130
  1003. setup_photpin();
  1004. servo_init();
  1005. // Reset the machine correction matrix.
  1006. // It does not make sense to load the correction matrix until the machine is homed.
  1007. world2machine_reset();
  1008. #ifdef PAT9125
  1009. fsensor_init();
  1010. #endif //PAT9125
  1011. #if defined(CONTROLLERFAN_PIN) && (CONTROLLERFAN_PIN > -1)
  1012. SET_OUTPUT(CONTROLLERFAN_PIN); //Set pin used for driver cooling fan
  1013. #endif
  1014. setup_homepin();
  1015. #ifdef TMC2130
  1016. if (1) {
  1017. /// SERIAL_ECHOPGM("initial zsteps on power up: "); MYSERIAL.println(tmc2130_rd_MSCNT(Z_AXIS));
  1018. // try to run to zero phase before powering the Z motor.
  1019. // Move in negative direction
  1020. WRITE(Z_DIR_PIN,INVERT_Z_DIR);
  1021. // Round the current micro-micro steps to micro steps.
  1022. for (uint16_t phase = (tmc2130_rd_MSCNT(Z_AXIS) + 8) >> 4; phase > 0; -- phase) {
  1023. // Until the phase counter is reset to zero.
  1024. WRITE(Z_STEP_PIN, !INVERT_Z_STEP_PIN);
  1025. delay(2);
  1026. WRITE(Z_STEP_PIN, INVERT_Z_STEP_PIN);
  1027. delay(2);
  1028. }
  1029. // SERIAL_ECHOPGM("initial zsteps after reset: "); MYSERIAL.println(tmc2130_rd_MSCNT(Z_AXIS));
  1030. }
  1031. #endif //TMC2130
  1032. #if defined(Z_AXIS_ALWAYS_ON)
  1033. enable_z();
  1034. #endif
  1035. //*** MaR::180501_02
  1036. farm_mode = eeprom_read_byte((uint8_t*)EEPROM_FARM_MODE);
  1037. EEPROM_read_B(EEPROM_FARM_NUMBER, &farm_no);
  1038. 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
  1039. if (farm_no == 0xFFFF) farm_no = 0;
  1040. if (farm_mode)
  1041. {
  1042. prusa_statistics(8);
  1043. }
  1044. // Enable Toshiba FlashAir SD card / WiFi enahanced card.
  1045. card.ToshibaFlashAir_enable(eeprom_read_byte((unsigned char*)EEPROM_TOSHIBA_FLASH_AIR_COMPATIBLITY) == 1);
  1046. if (eeprom_read_dword((uint32_t*)(EEPROM_TOP - 4)) == 0x0ffffffff &&
  1047. eeprom_read_dword((uint32_t*)(EEPROM_TOP - 8)) == 0x0ffffffff) {
  1048. // Maiden startup. The firmware has been loaded and first started on a virgin RAMBo board,
  1049. // where all the EEPROM entries are set to 0x0ff.
  1050. // Once a firmware boots up, it forces at least a language selection, which changes
  1051. // EEPROM_LANG to number lower than 0x0ff.
  1052. // 1) Set a high power mode.
  1053. #ifdef TMC2130
  1054. eeprom_write_byte((uint8_t*)EEPROM_SILENT, 0);
  1055. tmc2130_mode = TMC2130_MODE_NORMAL;
  1056. #endif //TMC2130
  1057. eeprom_write_byte((uint8_t*)EEPROM_WIZARD_ACTIVE, 1); //run wizard
  1058. }
  1059. // Force SD card update. Otherwise the SD card update is done from loop() on card.checkautostart(false),
  1060. // but this times out if a blocking dialog is shown in setup().
  1061. card.initsd();
  1062. #ifdef DEBUG_SD_SPEED_TEST
  1063. if (card.cardOK)
  1064. {
  1065. uint8_t* buff = (uint8_t*)block_buffer;
  1066. uint32_t block = 0;
  1067. uint32_t sumr = 0;
  1068. uint32_t sumw = 0;
  1069. for (int i = 0; i < 1024; i++)
  1070. {
  1071. uint32_t u = micros();
  1072. bool res = card.card.readBlock(i, buff);
  1073. u = micros() - u;
  1074. if (res)
  1075. {
  1076. printf_P(PSTR("readBlock %4d 512 bytes %lu us\n"), i, u);
  1077. sumr += u;
  1078. u = micros();
  1079. res = card.card.writeBlock(i, buff);
  1080. u = micros() - u;
  1081. if (res)
  1082. {
  1083. printf_P(PSTR("writeBlock %4d 512 bytes %lu us\n"), i, u);
  1084. sumw += u;
  1085. }
  1086. else
  1087. {
  1088. printf_P(PSTR("writeBlock %4d error\n"), i);
  1089. break;
  1090. }
  1091. }
  1092. else
  1093. {
  1094. printf_P(PSTR("readBlock %4d error\n"), i);
  1095. break;
  1096. }
  1097. }
  1098. uint32_t avg_rspeed = (1024 * 1000000) / (sumr / 512);
  1099. uint32_t avg_wspeed = (1024 * 1000000) / (sumw / 512);
  1100. printf_P(PSTR("avg read speed %lu bytes/s\n"), avg_rspeed);
  1101. printf_P(PSTR("avg write speed %lu bytes/s\n"), avg_wspeed);
  1102. }
  1103. else
  1104. printf_P(PSTR("Card NG!\n"));
  1105. #endif //DEBUG_SD_SPEED_TEST
  1106. if (eeprom_read_byte((uint8_t*)EEPROM_POWER_COUNT) == 0xff) eeprom_write_byte((uint8_t*)EEPROM_POWER_COUNT, 0);
  1107. if (eeprom_read_byte((uint8_t*)EEPROM_CRASH_COUNT_X) == 0xff) eeprom_write_byte((uint8_t*)EEPROM_CRASH_COUNT_X, 0);
  1108. if (eeprom_read_byte((uint8_t*)EEPROM_CRASH_COUNT_Y) == 0xff) eeprom_write_byte((uint8_t*)EEPROM_CRASH_COUNT_Y, 0);
  1109. if (eeprom_read_byte((uint8_t*)EEPROM_FERROR_COUNT) == 0xff) eeprom_write_byte((uint8_t*)EEPROM_FERROR_COUNT, 0);
  1110. if (eeprom_read_word((uint16_t*)EEPROM_POWER_COUNT_TOT) == 0xffff) eeprom_write_word((uint16_t*)EEPROM_POWER_COUNT_TOT, 0);
  1111. if (eeprom_read_word((uint16_t*)EEPROM_CRASH_COUNT_X_TOT) == 0xffff) eeprom_write_word((uint16_t*)EEPROM_CRASH_COUNT_X_TOT, 0);
  1112. if (eeprom_read_word((uint16_t*)EEPROM_CRASH_COUNT_Y_TOT) == 0xffff) eeprom_write_word((uint16_t*)EEPROM_CRASH_COUNT_Y_TOT, 0);
  1113. if (eeprom_read_word((uint16_t*)EEPROM_FERROR_COUNT_TOT) == 0xffff) eeprom_write_word((uint16_t*)EEPROM_FERROR_COUNT_TOT, 0);
  1114. #ifdef SNMM
  1115. if (eeprom_read_dword((uint32_t*)EEPROM_BOWDEN_LENGTH) == 0x0ffffffff) { //bowden length used for SNMM
  1116. int _z = BOWDEN_LENGTH;
  1117. for(int i = 0; i<4; i++) EEPROM_save_B(EEPROM_BOWDEN_LENGTH + i * 2, &_z);
  1118. }
  1119. #endif
  1120. // In the future, somewhere here would one compare the current firmware version against the firmware version stored in the EEPROM.
  1121. // If they differ, an update procedure may need to be performed. At the end of this block, the current firmware version
  1122. // is being written into the EEPROM, so the update procedure will be triggered only once.
  1123. lang_selected = eeprom_read_byte((uint8_t*)EEPROM_LANG);
  1124. if (lang_selected >= LANG_NUM)
  1125. {
  1126. lcd_mylang();
  1127. }
  1128. lang_select(lang_selected);
  1129. puts_P(_n("\nNew ML support"));
  1130. printf_P(_n(" lang_selected = %d\n"), lang_selected);
  1131. printf_P(_n(" &_SEC_LANG = 0x%04x\n"), &_SEC_LANG);
  1132. printf_P(_n(" sizeof(_SEC_LANG) = 0x%04x\n"), sizeof(_SEC_LANG));
  1133. uint16_t ptr_lang_table0 = ((uint16_t)(&_SEC_LANG) + 0xff) & 0xff00;
  1134. printf_P(_n(" &_lang_table0 = 0x%04x\n"), ptr_lang_table0);
  1135. uint32_t _lt_magic = pgm_read_dword(((uint32_t*)(ptr_lang_table0 + 0)));
  1136. uint16_t _lt_size = pgm_read_word(((uint16_t*)(ptr_lang_table0 + 4)));
  1137. uint16_t _lt_count = pgm_read_word(((uint16_t*)(ptr_lang_table0 + 6)));
  1138. uint16_t _lt_chsum = pgm_read_word(((uint16_t*)(ptr_lang_table0 + 8)));
  1139. uint16_t _lt_resv0 = pgm_read_word(((uint16_t*)(ptr_lang_table0 + 10)));
  1140. uint32_t _lt_resv1 = pgm_read_dword(((uint32_t*)(ptr_lang_table0 + 12)));
  1141. printf_P(_n(" _lt_magic = 0x%08lx %S\n"), _lt_magic, (_lt_magic==0x4bb45aa5)?_n("OK"):_n("NA"));
  1142. printf_P(_n(" _lt_size = 0x%04x (%d)\n"), _lt_size, _lt_size);
  1143. printf_P(_n(" _lt_count = 0x%04x (%d)\n"), _lt_count, _lt_count);
  1144. printf_P(_n(" _lt_chsum = 0x%04x\n"), _lt_chsum);
  1145. printf_P(_n(" _lt_resv0 = 0x%04x\n"), _lt_resv0);
  1146. printf_P(_n(" _lt_resv1 = 0x%08lx\n"), _lt_resv1);
  1147. puts_P(_n("\n"));
  1148. if (eeprom_read_byte((uint8_t*)EEPROM_TEMP_CAL_ACTIVE) == 255) {
  1149. eeprom_write_byte((uint8_t*)EEPROM_TEMP_CAL_ACTIVE, 0);
  1150. temp_cal_active = false;
  1151. } else temp_cal_active = eeprom_read_byte((uint8_t*)EEPROM_TEMP_CAL_ACTIVE);
  1152. if (eeprom_read_byte((uint8_t*)EEPROM_CALIBRATION_STATUS_PINDA) == 255) {
  1153. //eeprom_write_byte((uint8_t*)EEPROM_CALIBRATION_STATUS_PINDA, 0);
  1154. eeprom_write_byte((uint8_t*)EEPROM_CALIBRATION_STATUS_PINDA, 1);
  1155. int16_t z_shift = 0;
  1156. for (uint8_t i = 0; i < 5; i++) EEPROM_save_B(EEPROM_PROBE_TEMP_SHIFT + i * 2, &z_shift);
  1157. eeprom_write_byte((uint8_t*)EEPROM_TEMP_CAL_ACTIVE, 0);
  1158. temp_cal_active = false;
  1159. }
  1160. if (eeprom_read_byte((uint8_t*)EEPROM_UVLO) == 255) {
  1161. eeprom_write_byte((uint8_t*)EEPROM_UVLO, 0);
  1162. }
  1163. if (eeprom_read_byte((uint8_t*)EEPROM_SD_SORT) == 255) {
  1164. eeprom_write_byte((uint8_t*)EEPROM_SD_SORT, 0);
  1165. }
  1166. check_babystep(); //checking if Z babystep is in allowed range
  1167. #ifdef UVLO_SUPPORT
  1168. setup_uvlo_interrupt();
  1169. #endif //UVLO_SUPPORT
  1170. #if !defined(DEBUG_DISABLE_FANCHECK) && defined(FANCHECK) && defined(TACH_1) && TACH_1 >-1
  1171. setup_fan_interrupt();
  1172. #endif //DEBUG_DISABLE_FANCHECK
  1173. #ifdef PAT9125
  1174. #ifndef DEBUG_DISABLE_FSENSORCHECK
  1175. fsensor_setup_interrupt();
  1176. #endif //DEBUG_DISABLE_FSENSORCHECK
  1177. #endif //PAT9125
  1178. for (int i = 0; i<4; i++) EEPROM_read_B(EEPROM_BOWDEN_LENGTH + i * 2, &bowden_length[i]);
  1179. #ifndef DEBUG_DISABLE_STARTMSGS
  1180. KEEPALIVE_STATE(PAUSED_FOR_USER);
  1181. show_fw_version_warnings();
  1182. switch (hw_changed) {
  1183. //if motherboard or printer type was changed inform user as it can indicate flashing wrong firmware version
  1184. //if user confirms with knob, new hw version (printer and/or motherboard) is written to eeprom and message will be not shown next time
  1185. case(0b01):
  1186. lcd_show_fullscreen_message_and_wait_P(_i("Warning: motherboard type changed.")); ////MSG_CHANGED_MOTHERBOARD c=20 r=4
  1187. eeprom_write_word((uint16_t*)EEPROM_BOARD_TYPE, MOTHERBOARD);
  1188. break;
  1189. case(0b10):
  1190. lcd_show_fullscreen_message_and_wait_P(_i("Warning: printer type changed.")); ////MSG_CHANGED_PRINTER c=20 r=4
  1191. eeprom_write_word((uint16_t*)EEPROM_PRINTER_TYPE, PRINTER_TYPE);
  1192. break;
  1193. case(0b11):
  1194. lcd_show_fullscreen_message_and_wait_P(_i("Warning: both printer type and motherboard type changed.")); ////MSG_CHANGED_BOTH c=20 r=4
  1195. eeprom_write_word((uint16_t*)EEPROM_PRINTER_TYPE, PRINTER_TYPE);
  1196. eeprom_write_word((uint16_t*)EEPROM_BOARD_TYPE, MOTHERBOARD);
  1197. break;
  1198. default: break; //no change, show no message
  1199. }
  1200. if (!previous_settings_retrieved) {
  1201. lcd_show_fullscreen_message_and_wait_P(_i("Old settings found. Default PID, Esteps etc. will be set.")); //if EEPROM version or printer type was changed, inform user that default setting were loaded////MSG_DEFAULT_SETTINGS_LOADED c=20 r=4
  1202. erase_eeprom_section(EEPROM_OFFSET, 156); //erase M500 part of eeprom
  1203. }
  1204. if (eeprom_read_byte((uint8_t*)EEPROM_WIZARD_ACTIVE) == 1) {
  1205. lcd_wizard(0);
  1206. }
  1207. if (eeprom_read_byte((uint8_t*)EEPROM_WIZARD_ACTIVE) == 0) { //dont show calibration status messages if wizard is currently active
  1208. if (calibration_status() == CALIBRATION_STATUS_ASSEMBLED ||
  1209. calibration_status() == CALIBRATION_STATUS_UNKNOWN ||
  1210. calibration_status() == CALIBRATION_STATUS_XYZ_CALIBRATION) {
  1211. // Reset the babystepping values, so the printer will not move the Z axis up when the babystepping is enabled.
  1212. eeprom_update_word((uint16_t*)EEPROM_BABYSTEP_Z, 0);
  1213. // Show the message.
  1214. lcd_show_fullscreen_message_and_wait_P(_T(MSG_FOLLOW_CALIBRATION_FLOW));
  1215. }
  1216. else if (calibration_status() == CALIBRATION_STATUS_LIVE_ADJUST) {
  1217. // Show the message.
  1218. lcd_show_fullscreen_message_and_wait_P(_T(MSG_BABYSTEP_Z_NOT_SET));
  1219. lcd_update_enable(true);
  1220. }
  1221. else if (calibration_status() == CALIBRATION_STATUS_CALIBRATED && temp_cal_active == true && calibration_status_pinda() == false) {
  1222. //lcd_show_fullscreen_message_and_wait_P(_i("Temperature calibration has not been run yet"));////MSG_PINDA_NOT_CALIBRATED c=20 r=4
  1223. lcd_update_enable(true);
  1224. }
  1225. else if (calibration_status() == CALIBRATION_STATUS_Z_CALIBRATION) {
  1226. // Show the message.
  1227. lcd_show_fullscreen_message_and_wait_P(_T(MSG_FOLLOW_CALIBRATION_FLOW));
  1228. }
  1229. }
  1230. #if !defined (DEBUG_DISABLE_FORCE_SELFTEST) && defined (TMC2130)
  1231. if (force_selftest_if_fw_version() && calibration_status() < CALIBRATION_STATUS_ASSEMBLED) {
  1232. lcd_show_fullscreen_message_and_wait_P(_i("Selftest will be run to calibrate accurate sensorless rehoming."));////MSG_FORCE_SELFTEST c=20 r=8
  1233. update_current_firmware_version_to_eeprom();
  1234. lcd_selftest();
  1235. }
  1236. #endif //TMC2130 && !DEBUG_DISABLE_FORCE_SELFTEST
  1237. KEEPALIVE_STATE(IN_PROCESS);
  1238. #endif //DEBUG_DISABLE_STARTMSGS
  1239. lcd_update_enable(true);
  1240. lcd_implementation_clear();
  1241. lcd_update(2);
  1242. // Store the currently running firmware into an eeprom,
  1243. // so the next time the firmware gets updated, it will know from which version it has been updated.
  1244. update_current_firmware_version_to_eeprom();
  1245. #ifdef TMC2130
  1246. tmc2130_home_origin[X_AXIS] = eeprom_read_byte((uint8_t*)EEPROM_TMC2130_HOME_X_ORIGIN);
  1247. tmc2130_home_bsteps[X_AXIS] = eeprom_read_byte((uint8_t*)EEPROM_TMC2130_HOME_X_BSTEPS);
  1248. tmc2130_home_fsteps[X_AXIS] = eeprom_read_byte((uint8_t*)EEPROM_TMC2130_HOME_X_FSTEPS);
  1249. if (tmc2130_home_origin[X_AXIS] == 0xff) tmc2130_home_origin[X_AXIS] = 0;
  1250. if (tmc2130_home_bsteps[X_AXIS] == 0xff) tmc2130_home_bsteps[X_AXIS] = 48;
  1251. if (tmc2130_home_fsteps[X_AXIS] == 0xff) tmc2130_home_fsteps[X_AXIS] = 48;
  1252. tmc2130_home_origin[Y_AXIS] = eeprom_read_byte((uint8_t*)EEPROM_TMC2130_HOME_Y_ORIGIN);
  1253. tmc2130_home_bsteps[Y_AXIS] = eeprom_read_byte((uint8_t*)EEPROM_TMC2130_HOME_Y_BSTEPS);
  1254. tmc2130_home_fsteps[Y_AXIS] = eeprom_read_byte((uint8_t*)EEPROM_TMC2130_HOME_Y_FSTEPS);
  1255. if (tmc2130_home_origin[Y_AXIS] == 0xff) tmc2130_home_origin[Y_AXIS] = 0;
  1256. if (tmc2130_home_bsteps[Y_AXIS] == 0xff) tmc2130_home_bsteps[Y_AXIS] = 48;
  1257. if (tmc2130_home_fsteps[Y_AXIS] == 0xff) tmc2130_home_fsteps[Y_AXIS] = 48;
  1258. tmc2130_home_enabled = eeprom_read_byte((uint8_t*)EEPROM_TMC2130_HOME_ENABLED);
  1259. if (tmc2130_home_enabled == 0xff) tmc2130_home_enabled = 0;
  1260. #endif //TMC2130
  1261. #ifdef UVLO_SUPPORT
  1262. if (eeprom_read_byte((uint8_t*)EEPROM_UVLO) == 1) { //previous print was terminated by UVLO
  1263. /*
  1264. if (lcd_show_fullscreen_message_yes_no_and_wait_P(_T(MSG_RECOVER_PRINT), false)) recover_print();
  1265. else {
  1266. eeprom_update_byte((uint8_t*)EEPROM_UVLO, 0);
  1267. lcd_update_enable(true);
  1268. lcd_update(2);
  1269. lcd_setstatuspgm(_T(WELCOME_MSG));
  1270. }
  1271. */
  1272. manage_heater(); // Update temperatures
  1273. #ifdef DEBUG_UVLO_AUTOMATIC_RECOVER
  1274. MYSERIAL.println("Power panic detected!");
  1275. MYSERIAL.print("Current bed temp:");
  1276. MYSERIAL.println(degBed());
  1277. MYSERIAL.print("Saved bed temp:");
  1278. MYSERIAL.println((float)eeprom_read_byte((uint8_t*)EEPROM_UVLO_TARGET_BED));
  1279. #endif
  1280. if ( degBed() > ( (float)eeprom_read_byte((uint8_t*)EEPROM_UVLO_TARGET_BED) - AUTOMATIC_UVLO_BED_TEMP_OFFSET) ){
  1281. #ifdef DEBUG_UVLO_AUTOMATIC_RECOVER
  1282. MYSERIAL.println("Automatic recovery!");
  1283. #endif
  1284. recover_print(1);
  1285. }
  1286. else{
  1287. #ifdef DEBUG_UVLO_AUTOMATIC_RECOVER
  1288. MYSERIAL.println("Normal recovery!");
  1289. #endif
  1290. if ( lcd_show_fullscreen_message_yes_no_and_wait_P(_T(MSG_RECOVER_PRINT), false) ) recover_print(0);
  1291. else {
  1292. eeprom_update_byte((uint8_t*)EEPROM_UVLO, 0);
  1293. lcd_update_enable(true);
  1294. lcd_update(2);
  1295. lcd_setstatuspgm(_T(WELCOME_MSG));
  1296. }
  1297. }
  1298. }
  1299. #endif //UVLO_SUPPORT
  1300. KEEPALIVE_STATE(NOT_BUSY);
  1301. #ifdef WATCHDOG
  1302. wdt_enable(WDTO_4S);
  1303. #endif //WATCHDOG
  1304. }
  1305. #ifdef PAT9125
  1306. void fsensor_init() {
  1307. int pat9125 = pat9125_init();
  1308. printf_P(PSTR("PAT9125_init:%d\n"), pat9125);
  1309. uint8_t fsensor = eeprom_read_byte((uint8_t*)EEPROM_FSENSOR);
  1310. filament_autoload_enabled=eeprom_read_byte((uint8_t*)EEPROM_FSENS_AUTOLOAD_ENABLED);
  1311. if (!pat9125)
  1312. {
  1313. fsensor = 0; //disable sensor
  1314. fsensor_not_responding = true;
  1315. }
  1316. else {
  1317. fsensor_not_responding = false;
  1318. }
  1319. puts_P(PSTR("FSensor "));
  1320. if (fsensor)
  1321. {
  1322. puts_P(PSTR("ENABLED\n"));
  1323. fsensor_enable();
  1324. }
  1325. else
  1326. {
  1327. puts_P(PSTR("DISABLED\n"));
  1328. fsensor_disable();
  1329. }
  1330. #ifdef DEBUG_DISABLE_FSENSORCHECK
  1331. filament_autoload_enabled = false;
  1332. fsensor_disable();
  1333. #endif //DEBUG_DISABLE_FSENSORCHECK
  1334. }
  1335. #endif //PAT9125
  1336. void trace();
  1337. #define CHUNK_SIZE 64 // bytes
  1338. #define SAFETY_MARGIN 1
  1339. char chunk[CHUNK_SIZE+SAFETY_MARGIN];
  1340. int chunkHead = 0;
  1341. int serial_read_stream() {
  1342. setTargetHotend(0, 0);
  1343. setTargetBed(0);
  1344. lcd_implementation_clear();
  1345. lcd_printPGM(PSTR(" Upload in progress"));
  1346. // first wait for how many bytes we will receive
  1347. uint32_t bytesToReceive;
  1348. // receive the four bytes
  1349. char bytesToReceiveBuffer[4];
  1350. for (int i=0; i<4; i++) {
  1351. int data;
  1352. while ((data = MYSERIAL.read()) == -1) {};
  1353. bytesToReceiveBuffer[i] = data;
  1354. }
  1355. // make it a uint32
  1356. memcpy(&bytesToReceive, &bytesToReceiveBuffer, 4);
  1357. // we're ready, notify the sender
  1358. MYSERIAL.write('+');
  1359. // lock in the routine
  1360. uint32_t receivedBytes = 0;
  1361. while (prusa_sd_card_upload) {
  1362. int i;
  1363. for (i=0; i<CHUNK_SIZE; i++) {
  1364. int data;
  1365. // check if we're not done
  1366. if (receivedBytes == bytesToReceive) {
  1367. break;
  1368. }
  1369. // read the next byte
  1370. while ((data = MYSERIAL.read()) == -1) {};
  1371. receivedBytes++;
  1372. // save it to the chunk
  1373. chunk[i] = data;
  1374. }
  1375. // write the chunk to SD
  1376. card.write_command_no_newline(&chunk[0]);
  1377. // notify the sender we're ready for more data
  1378. MYSERIAL.write('+');
  1379. // for safety
  1380. manage_heater();
  1381. // check if we're done
  1382. if(receivedBytes == bytesToReceive) {
  1383. trace(); // beep
  1384. card.closefile();
  1385. prusa_sd_card_upload = false;
  1386. SERIAL_PROTOCOLLNRPGM(MSG_FILE_SAVED);
  1387. return 0;
  1388. }
  1389. }
  1390. }
  1391. #ifdef HOST_KEEPALIVE_FEATURE
  1392. /**
  1393. * Output a "busy" message at regular intervals
  1394. * while the machine is not accepting commands.
  1395. */
  1396. void host_keepalive() {
  1397. if (farm_mode) return;
  1398. long ms = millis();
  1399. if (host_keepalive_interval && busy_state != NOT_BUSY) {
  1400. if ((ms - prev_busy_signal_ms) < (long)(1000L * host_keepalive_interval)) return;
  1401. switch (busy_state) {
  1402. case IN_HANDLER:
  1403. case IN_PROCESS:
  1404. SERIAL_ECHO_START;
  1405. SERIAL_ECHOLNPGM("busy: processing");
  1406. break;
  1407. case PAUSED_FOR_USER:
  1408. SERIAL_ECHO_START;
  1409. SERIAL_ECHOLNPGM("busy: paused for user");
  1410. break;
  1411. case PAUSED_FOR_INPUT:
  1412. SERIAL_ECHO_START;
  1413. SERIAL_ECHOLNPGM("busy: paused for input");
  1414. break;
  1415. default:
  1416. break;
  1417. }
  1418. }
  1419. prev_busy_signal_ms = ms;
  1420. }
  1421. #endif
  1422. // The loop() function is called in an endless loop by the Arduino framework from the default main() routine.
  1423. // Before loop(), the setup() function is called by the main() routine.
  1424. void loop()
  1425. {
  1426. KEEPALIVE_STATE(NOT_BUSY);
  1427. bool stack_integrity = true;
  1428. if ((usb_printing_counter > 0) && ((millis()-_usb_timer) > 1000))
  1429. {
  1430. is_usb_printing = true;
  1431. usb_printing_counter--;
  1432. _usb_timer = millis();
  1433. }
  1434. if (usb_printing_counter == 0)
  1435. {
  1436. is_usb_printing = false;
  1437. }
  1438. if (prusa_sd_card_upload)
  1439. {
  1440. //we read byte-by byte
  1441. serial_read_stream();
  1442. } else
  1443. {
  1444. get_command();
  1445. #ifdef SDSUPPORT
  1446. card.checkautostart(false);
  1447. #endif
  1448. if(buflen)
  1449. {
  1450. cmdbuffer_front_already_processed = false;
  1451. #ifdef SDSUPPORT
  1452. if(card.saving)
  1453. {
  1454. // Saving a G-code file onto an SD-card is in progress.
  1455. // Saving starts with M28, saving until M29 is seen.
  1456. if(strstr_P(CMDBUFFER_CURRENT_STRING, PSTR("M29")) == NULL) {
  1457. card.write_command(CMDBUFFER_CURRENT_STRING);
  1458. if(card.logging)
  1459. process_commands();
  1460. else
  1461. SERIAL_PROTOCOLLNRPGM(_T(MSG_OK));
  1462. } else {
  1463. card.closefile();
  1464. SERIAL_PROTOCOLLNRPGM(MSG_FILE_SAVED);
  1465. }
  1466. } else {
  1467. process_commands();
  1468. }
  1469. #else
  1470. process_commands();
  1471. #endif //SDSUPPORT
  1472. if (! cmdbuffer_front_already_processed && buflen)
  1473. {
  1474. // ptr points to the start of the block currently being processed.
  1475. // The first character in the block is the block type.
  1476. char *ptr = cmdbuffer + bufindr;
  1477. if (*ptr == CMDBUFFER_CURRENT_TYPE_SDCARD) {
  1478. // To support power panic, move the lenght of the command on the SD card to a planner buffer.
  1479. union {
  1480. struct {
  1481. char lo;
  1482. char hi;
  1483. } lohi;
  1484. uint16_t value;
  1485. } sdlen;
  1486. sdlen.value = 0;
  1487. {
  1488. // This block locks the interrupts globally for 3.25 us,
  1489. // which corresponds to a maximum repeat frequency of 307.69 kHz.
  1490. // This blocking is safe in the context of a 10kHz stepper driver interrupt
  1491. // or a 115200 Bd serial line receive interrupt, which will not trigger faster than 12kHz.
  1492. cli();
  1493. // Reset the command to something, which will be ignored by the power panic routine,
  1494. // so this buffer length will not be counted twice.
  1495. *ptr ++ = CMDBUFFER_CURRENT_TYPE_TO_BE_REMOVED;
  1496. // Extract the current buffer length.
  1497. sdlen.lohi.lo = *ptr ++;
  1498. sdlen.lohi.hi = *ptr;
  1499. // and pass it to the planner queue.
  1500. planner_add_sd_length(sdlen.value);
  1501. sei();
  1502. }
  1503. }
  1504. else if((*ptr == CMDBUFFER_CURRENT_TYPE_USB_WITH_LINENR) && !IS_SD_PRINTING){
  1505. cli();
  1506. *ptr ++ = CMDBUFFER_CURRENT_TYPE_TO_BE_REMOVED;
  1507. // and one for each command to previous block in the planner queue.
  1508. planner_add_sd_length(1);
  1509. sei();
  1510. }
  1511. // Now it is safe to release the already processed command block. If interrupted by the power panic now,
  1512. // this block's SD card length will not be counted twice as its command type has been replaced
  1513. // by CMDBUFFER_CURRENT_TYPE_TO_BE_REMOVED.
  1514. cmdqueue_pop_front();
  1515. }
  1516. host_keepalive();
  1517. }
  1518. }
  1519. //check heater every n milliseconds
  1520. manage_heater();
  1521. isPrintPaused ? manage_inactivity(true) : manage_inactivity(false);
  1522. checkHitEndstops();
  1523. lcd_update();
  1524. #ifdef PAT9125
  1525. fsensor_update();
  1526. #endif //PAT9125
  1527. #ifdef TMC2130
  1528. tmc2130_check_overtemp();
  1529. if (tmc2130_sg_crash)
  1530. {
  1531. uint8_t crash = tmc2130_sg_crash;
  1532. tmc2130_sg_crash = 0;
  1533. // crashdet_stop_and_save_print();
  1534. switch (crash)
  1535. {
  1536. case 1: enquecommand_P((PSTR("CRASH_DETECTEDX"))); break;
  1537. case 2: enquecommand_P((PSTR("CRASH_DETECTEDY"))); break;
  1538. case 3: enquecommand_P((PSTR("CRASH_DETECTEDXY"))); break;
  1539. }
  1540. }
  1541. #endif //TMC2130
  1542. }
  1543. #define DEFINE_PGM_READ_ANY(type, reader) \
  1544. static inline type pgm_read_any(const type *p) \
  1545. { return pgm_read_##reader##_near(p); }
  1546. DEFINE_PGM_READ_ANY(float, float);
  1547. DEFINE_PGM_READ_ANY(signed char, byte);
  1548. #define XYZ_CONSTS_FROM_CONFIG(type, array, CONFIG) \
  1549. static const PROGMEM type array##_P[3] = \
  1550. { X_##CONFIG, Y_##CONFIG, Z_##CONFIG }; \
  1551. static inline type array(int axis) \
  1552. { return pgm_read_any(&array##_P[axis]); } \
  1553. type array##_ext(int axis) \
  1554. { return pgm_read_any(&array##_P[axis]); }
  1555. XYZ_CONSTS_FROM_CONFIG(float, base_min_pos, MIN_POS);
  1556. XYZ_CONSTS_FROM_CONFIG(float, base_max_pos, MAX_POS);
  1557. XYZ_CONSTS_FROM_CONFIG(float, base_home_pos, HOME_POS);
  1558. XYZ_CONSTS_FROM_CONFIG(float, max_length, MAX_LENGTH);
  1559. XYZ_CONSTS_FROM_CONFIG(float, home_retract_mm, HOME_RETRACT_MM);
  1560. XYZ_CONSTS_FROM_CONFIG(signed char, home_dir, HOME_DIR);
  1561. static void axis_is_at_home(int axis) {
  1562. current_position[axis] = base_home_pos(axis) + add_homing[axis];
  1563. min_pos[axis] = base_min_pos(axis) + add_homing[axis];
  1564. max_pos[axis] = base_max_pos(axis) + add_homing[axis];
  1565. }
  1566. inline void set_current_to_destination() { memcpy(current_position, destination, sizeof(current_position)); }
  1567. inline void set_destination_to_current() { memcpy(destination, current_position, sizeof(destination)); }
  1568. static void setup_for_endstop_move(bool enable_endstops_now = true) {
  1569. saved_feedrate = feedrate;
  1570. saved_feedmultiply = feedmultiply;
  1571. feedmultiply = 100;
  1572. previous_millis_cmd = millis();
  1573. enable_endstops(enable_endstops_now);
  1574. }
  1575. static void clean_up_after_endstop_move() {
  1576. #ifdef ENDSTOPS_ONLY_FOR_HOMING
  1577. enable_endstops(false);
  1578. #endif
  1579. feedrate = saved_feedrate;
  1580. feedmultiply = saved_feedmultiply;
  1581. previous_millis_cmd = millis();
  1582. }
  1583. #ifdef ENABLE_AUTO_BED_LEVELING
  1584. #ifdef AUTO_BED_LEVELING_GRID
  1585. static void set_bed_level_equation_lsq(double *plane_equation_coefficients)
  1586. {
  1587. vector_3 planeNormal = vector_3(-plane_equation_coefficients[0], -plane_equation_coefficients[1], 1);
  1588. planeNormal.debug("planeNormal");
  1589. plan_bed_level_matrix = matrix_3x3::create_look_at(planeNormal);
  1590. //bedLevel.debug("bedLevel");
  1591. //plan_bed_level_matrix.debug("bed level before");
  1592. //vector_3 uncorrected_position = plan_get_position_mm();
  1593. //uncorrected_position.debug("position before");
  1594. vector_3 corrected_position = plan_get_position();
  1595. // corrected_position.debug("position after");
  1596. current_position[X_AXIS] = corrected_position.x;
  1597. current_position[Y_AXIS] = corrected_position.y;
  1598. current_position[Z_AXIS] = corrected_position.z;
  1599. // put the bed at 0 so we don't go below it.
  1600. current_position[Z_AXIS] = zprobe_zoffset; // in the lsq we reach here after raising the extruder due to the loop structure
  1601. plan_set_position(current_position[X_AXIS], current_position[Y_AXIS], current_position[Z_AXIS], current_position[E_AXIS]);
  1602. }
  1603. #else // not AUTO_BED_LEVELING_GRID
  1604. static void set_bed_level_equation_3pts(float z_at_pt_1, float z_at_pt_2, float z_at_pt_3) {
  1605. plan_bed_level_matrix.set_to_identity();
  1606. vector_3 pt1 = vector_3(ABL_PROBE_PT_1_X, ABL_PROBE_PT_1_Y, z_at_pt_1);
  1607. vector_3 pt2 = vector_3(ABL_PROBE_PT_2_X, ABL_PROBE_PT_2_Y, z_at_pt_2);
  1608. vector_3 pt3 = vector_3(ABL_PROBE_PT_3_X, ABL_PROBE_PT_3_Y, z_at_pt_3);
  1609. vector_3 from_2_to_1 = (pt1 - pt2).get_normal();
  1610. vector_3 from_2_to_3 = (pt3 - pt2).get_normal();
  1611. vector_3 planeNormal = vector_3::cross(from_2_to_1, from_2_to_3).get_normal();
  1612. planeNormal = vector_3(planeNormal.x, planeNormal.y, abs(planeNormal.z));
  1613. plan_bed_level_matrix = matrix_3x3::create_look_at(planeNormal);
  1614. vector_3 corrected_position = plan_get_position();
  1615. current_position[X_AXIS] = corrected_position.x;
  1616. current_position[Y_AXIS] = corrected_position.y;
  1617. current_position[Z_AXIS] = corrected_position.z;
  1618. // put the bed at 0 so we don't go below it.
  1619. current_position[Z_AXIS] = zprobe_zoffset;
  1620. plan_set_position(current_position[X_AXIS], current_position[Y_AXIS], current_position[Z_AXIS], current_position[E_AXIS]);
  1621. }
  1622. #endif // AUTO_BED_LEVELING_GRID
  1623. static void run_z_probe() {
  1624. plan_bed_level_matrix.set_to_identity();
  1625. feedrate = homing_feedrate[Z_AXIS];
  1626. // move down until you find the bed
  1627. float zPosition = -10;
  1628. plan_buffer_line(current_position[X_AXIS], current_position[Y_AXIS], zPosition, current_position[E_AXIS], feedrate/60, active_extruder);
  1629. st_synchronize();
  1630. // we have to let the planner know where we are right now as it is not where we said to go.
  1631. zPosition = st_get_position_mm(Z_AXIS);
  1632. plan_set_position(current_position[X_AXIS], current_position[Y_AXIS], zPosition, current_position[E_AXIS]);
  1633. // move up the retract distance
  1634. zPosition += home_retract_mm(Z_AXIS);
  1635. plan_buffer_line(current_position[X_AXIS], current_position[Y_AXIS], zPosition, current_position[E_AXIS], feedrate/60, active_extruder);
  1636. st_synchronize();
  1637. // move back down slowly to find bed
  1638. feedrate = homing_feedrate[Z_AXIS]/4;
  1639. zPosition -= home_retract_mm(Z_AXIS) * 2;
  1640. plan_buffer_line(current_position[X_AXIS], current_position[Y_AXIS], zPosition, current_position[E_AXIS], feedrate/60, active_extruder);
  1641. st_synchronize();
  1642. current_position[Z_AXIS] = st_get_position_mm(Z_AXIS);
  1643. // make sure the planner knows where we are as it may be a bit different than we last said to move to
  1644. plan_set_position(current_position[X_AXIS], current_position[Y_AXIS], current_position[Z_AXIS], current_position[E_AXIS]);
  1645. }
  1646. static void do_blocking_move_to(float x, float y, float z) {
  1647. float oldFeedRate = feedrate;
  1648. feedrate = homing_feedrate[Z_AXIS];
  1649. current_position[Z_AXIS] = z;
  1650. plan_buffer_line(current_position[X_AXIS], current_position[Y_AXIS], current_position[Z_AXIS], current_position[E_AXIS], feedrate/60, active_extruder);
  1651. st_synchronize();
  1652. feedrate = XY_TRAVEL_SPEED;
  1653. current_position[X_AXIS] = x;
  1654. current_position[Y_AXIS] = y;
  1655. plan_buffer_line(current_position[X_AXIS], current_position[Y_AXIS], current_position[Z_AXIS], current_position[E_AXIS], feedrate/60, active_extruder);
  1656. st_synchronize();
  1657. feedrate = oldFeedRate;
  1658. }
  1659. static void do_blocking_move_relative(float offset_x, float offset_y, float offset_z) {
  1660. do_blocking_move_to(current_position[X_AXIS] + offset_x, current_position[Y_AXIS] + offset_y, current_position[Z_AXIS] + offset_z);
  1661. }
  1662. /// Probe bed height at position (x,y), returns the measured z value
  1663. static float probe_pt(float x, float y, float z_before) {
  1664. // move to right place
  1665. do_blocking_move_to(current_position[X_AXIS], current_position[Y_AXIS], z_before);
  1666. do_blocking_move_to(x - X_PROBE_OFFSET_FROM_EXTRUDER, y - Y_PROBE_OFFSET_FROM_EXTRUDER, current_position[Z_AXIS]);
  1667. run_z_probe();
  1668. float measured_z = current_position[Z_AXIS];
  1669. SERIAL_PROTOCOLRPGM(_T(MSG_BED));
  1670. SERIAL_PROTOCOLPGM(" x: ");
  1671. SERIAL_PROTOCOL(x);
  1672. SERIAL_PROTOCOLPGM(" y: ");
  1673. SERIAL_PROTOCOL(y);
  1674. SERIAL_PROTOCOLPGM(" z: ");
  1675. SERIAL_PROTOCOL(measured_z);
  1676. SERIAL_PROTOCOLPGM("\n");
  1677. return measured_z;
  1678. }
  1679. #endif // #ifdef ENABLE_AUTO_BED_LEVELING
  1680. #ifdef LIN_ADVANCE
  1681. /**
  1682. * M900: Set and/or Get advance K factor and WH/D ratio
  1683. *
  1684. * K<factor> Set advance K factor
  1685. * R<ratio> Set ratio directly (overrides WH/D)
  1686. * W<width> H<height> D<diam> Set ratio from WH/D
  1687. */
  1688. inline void gcode_M900() {
  1689. st_synchronize();
  1690. const float newK = code_seen('K') ? code_value_float() : -1;
  1691. if (newK >= 0) extruder_advance_k = newK;
  1692. float newR = code_seen('R') ? code_value_float() : -1;
  1693. if (newR < 0) {
  1694. const float newD = code_seen('D') ? code_value_float() : -1,
  1695. newW = code_seen('W') ? code_value_float() : -1,
  1696. newH = code_seen('H') ? code_value_float() : -1;
  1697. if (newD >= 0 && newW >= 0 && newH >= 0)
  1698. newR = newD ? (newW * newH) / (sq(newD * 0.5) * M_PI) : 0;
  1699. }
  1700. if (newR >= 0) advance_ed_ratio = newR;
  1701. SERIAL_ECHO_START;
  1702. SERIAL_ECHOPGM("Advance K=");
  1703. SERIAL_ECHOLN(extruder_advance_k);
  1704. SERIAL_ECHOPGM(" E/D=");
  1705. const float ratio = advance_ed_ratio;
  1706. if (ratio) SERIAL_ECHOLN(ratio); else SERIAL_ECHOLNPGM("Auto");
  1707. }
  1708. #endif // LIN_ADVANCE
  1709. bool check_commands() {
  1710. bool end_command_found = false;
  1711. while (buflen)
  1712. {
  1713. if ((code_seen("M84")) || (code_seen("M 84"))) end_command_found = true;
  1714. if (!cmdbuffer_front_already_processed)
  1715. cmdqueue_pop_front();
  1716. cmdbuffer_front_already_processed = false;
  1717. }
  1718. return end_command_found;
  1719. }
  1720. #ifdef TMC2130
  1721. bool calibrate_z_auto()
  1722. {
  1723. //lcd_display_message_fullscreen_P(_T(MSG_CALIBRATE_Z_AUTO));
  1724. lcd_implementation_clear();
  1725. lcd_print_at_PGM(0,1, _T(MSG_CALIBRATE_Z_AUTO));
  1726. bool endstops_enabled = enable_endstops(true);
  1727. int axis_up_dir = -home_dir(Z_AXIS);
  1728. tmc2130_home_enter(Z_AXIS_MASK);
  1729. current_position[Z_AXIS] = 0;
  1730. plan_set_position(current_position[X_AXIS], current_position[Y_AXIS], current_position[Z_AXIS], current_position[E_AXIS]);
  1731. set_destination_to_current();
  1732. destination[Z_AXIS] += (1.1 * max_length(Z_AXIS) * axis_up_dir);
  1733. feedrate = homing_feedrate[Z_AXIS];
  1734. plan_buffer_line(destination[X_AXIS], destination[Y_AXIS], destination[Z_AXIS], destination[E_AXIS], feedrate/60, active_extruder);
  1735. st_synchronize();
  1736. // current_position[axis] = 0;
  1737. // plan_set_position(current_position[X_AXIS], current_position[Y_AXIS], current_position[Z_AXIS], current_position[E_AXIS]);
  1738. tmc2130_home_exit();
  1739. enable_endstops(false);
  1740. current_position[Z_AXIS] = 0;
  1741. plan_set_position(current_position[X_AXIS], current_position[Y_AXIS], current_position[Z_AXIS], current_position[E_AXIS]);
  1742. set_destination_to_current();
  1743. destination[Z_AXIS] += 10 * axis_up_dir; //10mm up
  1744. feedrate = homing_feedrate[Z_AXIS] / 2;
  1745. plan_buffer_line(destination[X_AXIS], destination[Y_AXIS], destination[Z_AXIS], destination[E_AXIS], feedrate/60, active_extruder);
  1746. st_synchronize();
  1747. enable_endstops(endstops_enabled);
  1748. current_position[Z_AXIS] = Z_MAX_POS+2.0;
  1749. plan_set_position(current_position[X_AXIS], current_position[Y_AXIS], current_position[Z_AXIS], current_position[E_AXIS]);
  1750. return true;
  1751. }
  1752. #endif //TMC2130
  1753. void homeaxis(int axis, uint8_t cnt, uint8_t* pstep)
  1754. {
  1755. bool endstops_enabled = enable_endstops(true); //RP: endstops should be allways enabled durring homing
  1756. #define HOMEAXIS_DO(LETTER) \
  1757. ((LETTER##_MIN_PIN > -1 && LETTER##_HOME_DIR==-1) || (LETTER##_MAX_PIN > -1 && LETTER##_HOME_DIR==1))
  1758. if ((axis==X_AXIS)?HOMEAXIS_DO(X):(axis==Y_AXIS)?HOMEAXIS_DO(Y):0)
  1759. {
  1760. int axis_home_dir = home_dir(axis);
  1761. feedrate = homing_feedrate[axis];
  1762. #ifdef TMC2130
  1763. tmc2130_home_enter(X_AXIS_MASK << axis);
  1764. #endif //TMC2130
  1765. // Move right a bit, so that the print head does not touch the left end position,
  1766. // and the following left movement has a chance to achieve the required velocity
  1767. // for the stall guard to work.
  1768. current_position[axis] = 0;
  1769. plan_set_position(current_position[X_AXIS], current_position[Y_AXIS], current_position[Z_AXIS], current_position[E_AXIS]);
  1770. set_destination_to_current();
  1771. // destination[axis] = 11.f;
  1772. destination[axis] = 3.f;
  1773. plan_buffer_line(destination[X_AXIS], destination[Y_AXIS], destination[Z_AXIS], destination[E_AXIS], feedrate/60, active_extruder);
  1774. st_synchronize();
  1775. // Move left away from the possible collision with the collision detection disabled.
  1776. endstops_hit_on_purpose();
  1777. enable_endstops(false);
  1778. current_position[axis] = 0;
  1779. plan_set_position(current_position[X_AXIS], current_position[Y_AXIS], current_position[Z_AXIS], current_position[E_AXIS]);
  1780. destination[axis] = - 1.;
  1781. plan_buffer_line(destination[X_AXIS], destination[Y_AXIS], destination[Z_AXIS], destination[E_AXIS], feedrate/60, active_extruder);
  1782. st_synchronize();
  1783. // Now continue to move up to the left end stop with the collision detection enabled.
  1784. enable_endstops(true);
  1785. destination[axis] = - 1.1 * max_length(axis);
  1786. plan_buffer_line(destination[X_AXIS], destination[Y_AXIS], destination[Z_AXIS], destination[E_AXIS], feedrate/60, active_extruder);
  1787. st_synchronize();
  1788. for (uint8_t i = 0; i < cnt; i++)
  1789. {
  1790. // Move right from the collision to a known distance from the left end stop with the collision detection disabled.
  1791. endstops_hit_on_purpose();
  1792. enable_endstops(false);
  1793. current_position[axis] = 0;
  1794. plan_set_position(current_position[X_AXIS], current_position[Y_AXIS], current_position[Z_AXIS], current_position[E_AXIS]);
  1795. destination[axis] = 10.f;
  1796. plan_buffer_line(destination[X_AXIS], destination[Y_AXIS], destination[Z_AXIS], destination[E_AXIS], feedrate/60, active_extruder);
  1797. st_synchronize();
  1798. endstops_hit_on_purpose();
  1799. // Now move left up to the collision, this time with a repeatable velocity.
  1800. enable_endstops(true);
  1801. destination[axis] = - 11.f;
  1802. #ifdef TMC2130
  1803. feedrate = homing_feedrate[axis];
  1804. #else //TMC2130
  1805. feedrate = homing_feedrate[axis] / 2;
  1806. #endif //TMC2130
  1807. plan_buffer_line(destination[X_AXIS], destination[Y_AXIS], destination[Z_AXIS], destination[E_AXIS], feedrate/60, active_extruder);
  1808. st_synchronize();
  1809. #ifdef TMC2130
  1810. uint16_t mscnt = tmc2130_rd_MSCNT(axis);
  1811. if (pstep) pstep[i] = mscnt >> 4;
  1812. printf_P(PSTR("%3d step=%2d mscnt=%4d\n"), i, mscnt >> 4, mscnt);
  1813. #endif //TMC2130
  1814. }
  1815. endstops_hit_on_purpose();
  1816. enable_endstops(false);
  1817. #ifdef TMC2130
  1818. uint8_t orig = tmc2130_home_origin[axis];
  1819. uint8_t back = tmc2130_home_bsteps[axis];
  1820. if (tmc2130_home_enabled && (orig <= 63))
  1821. {
  1822. tmc2130_goto_step(axis, orig, 2, 1000, tmc2130_get_res(axis));
  1823. if (back > 0)
  1824. tmc2130_do_steps(axis, back, 1, 1000);
  1825. }
  1826. else
  1827. tmc2130_do_steps(axis, 8, 2, 1000);
  1828. tmc2130_home_exit();
  1829. #endif //TMC2130
  1830. axis_is_at_home(axis);
  1831. axis_known_position[axis] = true;
  1832. // Move from minimum
  1833. #ifdef TMC2130
  1834. float dist = 0.01f * tmc2130_home_fsteps[axis];
  1835. #else //TMC2130
  1836. float dist = 0.01f * 64;
  1837. #endif //TMC2130
  1838. current_position[axis] -= dist;
  1839. plan_set_position(current_position[X_AXIS], current_position[Y_AXIS], current_position[Z_AXIS], current_position[E_AXIS]);
  1840. current_position[axis] += dist;
  1841. destination[axis] = current_position[axis];
  1842. plan_buffer_line(destination[X_AXIS], destination[Y_AXIS], destination[Z_AXIS], destination[E_AXIS], 0.5f*feedrate/60, active_extruder);
  1843. st_synchronize();
  1844. feedrate = 0.0;
  1845. }
  1846. else if ((axis==Z_AXIS)?HOMEAXIS_DO(Z):0)
  1847. {
  1848. #ifdef TMC2130
  1849. FORCE_HIGH_POWER_START;
  1850. #endif
  1851. int axis_home_dir = home_dir(axis);
  1852. current_position[axis] = 0;
  1853. plan_set_position(current_position[X_AXIS], current_position[Y_AXIS], current_position[Z_AXIS], current_position[E_AXIS]);
  1854. destination[axis] = 1.5 * max_length(axis) * axis_home_dir;
  1855. feedrate = homing_feedrate[axis];
  1856. plan_buffer_line(destination[X_AXIS], destination[Y_AXIS], destination[Z_AXIS], destination[E_AXIS], feedrate/60, active_extruder);
  1857. st_synchronize();
  1858. #ifdef TMC2130
  1859. if (READ(Z_TMC2130_DIAG) != 0) { //Z crash
  1860. FORCE_HIGH_POWER_END;
  1861. kill(MSG_BED_LEVELING_FAILED_POINT_LOW);
  1862. return;
  1863. }
  1864. #endif //TMC2130
  1865. current_position[axis] = 0;
  1866. plan_set_position(current_position[X_AXIS], current_position[Y_AXIS], current_position[Z_AXIS], current_position[E_AXIS]);
  1867. destination[axis] = -home_retract_mm(axis) * axis_home_dir;
  1868. plan_buffer_line(destination[X_AXIS], destination[Y_AXIS], destination[Z_AXIS], destination[E_AXIS], feedrate/60, active_extruder);
  1869. st_synchronize();
  1870. destination[axis] = 2*home_retract_mm(axis) * axis_home_dir;
  1871. feedrate = homing_feedrate[axis]/2 ;
  1872. plan_buffer_line(destination[X_AXIS], destination[Y_AXIS], destination[Z_AXIS], destination[E_AXIS], feedrate/60, active_extruder);
  1873. st_synchronize();
  1874. #ifdef TMC2130
  1875. if (READ(Z_TMC2130_DIAG) != 0) { //Z crash
  1876. FORCE_HIGH_POWER_END;
  1877. kill(MSG_BED_LEVELING_FAILED_POINT_LOW);
  1878. return;
  1879. }
  1880. #endif //TMC2130
  1881. axis_is_at_home(axis);
  1882. destination[axis] = current_position[axis];
  1883. feedrate = 0.0;
  1884. endstops_hit_on_purpose();
  1885. axis_known_position[axis] = true;
  1886. #ifdef TMC2130
  1887. FORCE_HIGH_POWER_END;
  1888. #endif
  1889. }
  1890. enable_endstops(endstops_enabled);
  1891. }
  1892. /**/
  1893. void home_xy()
  1894. {
  1895. set_destination_to_current();
  1896. homeaxis(X_AXIS);
  1897. homeaxis(Y_AXIS);
  1898. plan_set_position(current_position[X_AXIS], current_position[Y_AXIS], current_position[Z_AXIS], current_position[E_AXIS]);
  1899. endstops_hit_on_purpose();
  1900. }
  1901. void refresh_cmd_timeout(void)
  1902. {
  1903. previous_millis_cmd = millis();
  1904. }
  1905. #ifdef FWRETRACT
  1906. void retract(bool retracting, bool swapretract = false) {
  1907. if(retracting && !retracted[active_extruder]) {
  1908. destination[X_AXIS]=current_position[X_AXIS];
  1909. destination[Y_AXIS]=current_position[Y_AXIS];
  1910. destination[Z_AXIS]=current_position[Z_AXIS];
  1911. destination[E_AXIS]=current_position[E_AXIS];
  1912. current_position[E_AXIS]+=(swapretract?retract_length_swap:retract_length)*float(extrudemultiply)*0.01f;
  1913. plan_set_e_position(current_position[E_AXIS]);
  1914. float oldFeedrate = feedrate;
  1915. feedrate=retract_feedrate*60;
  1916. retracted[active_extruder]=true;
  1917. prepare_move();
  1918. current_position[Z_AXIS]-=retract_zlift;
  1919. plan_set_position(current_position[X_AXIS], current_position[Y_AXIS], current_position[Z_AXIS], current_position[E_AXIS]);
  1920. prepare_move();
  1921. feedrate = oldFeedrate;
  1922. } else if(!retracting && retracted[active_extruder]) {
  1923. destination[X_AXIS]=current_position[X_AXIS];
  1924. destination[Y_AXIS]=current_position[Y_AXIS];
  1925. destination[Z_AXIS]=current_position[Z_AXIS];
  1926. destination[E_AXIS]=current_position[E_AXIS];
  1927. current_position[Z_AXIS]+=retract_zlift;
  1928. plan_set_position(current_position[X_AXIS], current_position[Y_AXIS], current_position[Z_AXIS], current_position[E_AXIS]);
  1929. current_position[E_AXIS]-=(swapretract?(retract_length_swap+retract_recover_length_swap):(retract_length+retract_recover_length))*float(extrudemultiply)*0.01f;
  1930. plan_set_e_position(current_position[E_AXIS]);
  1931. float oldFeedrate = feedrate;
  1932. feedrate=retract_recover_feedrate*60;
  1933. retracted[active_extruder]=false;
  1934. prepare_move();
  1935. feedrate = oldFeedrate;
  1936. }
  1937. } //retract
  1938. #endif //FWRETRACT
  1939. void trace() {
  1940. tone(BEEPER, 440);
  1941. delay(25);
  1942. noTone(BEEPER);
  1943. delay(20);
  1944. }
  1945. /*
  1946. void ramming() {
  1947. // float tmp[4] = DEFAULT_MAX_FEEDRATE;
  1948. if (current_temperature[0] < 230) {
  1949. //PLA
  1950. max_feedrate[E_AXIS] = 50;
  1951. //current_position[E_AXIS] -= 8;
  1952. //plan_buffer_line(current_position[X_AXIS], current_position[Y_AXIS], current_position[Z_AXIS], current_position[E_AXIS], 2100 / 60, active_extruder);
  1953. //current_position[E_AXIS] += 8;
  1954. //plan_buffer_line(current_position[X_AXIS], current_position[Y_AXIS], current_position[Z_AXIS], current_position[E_AXIS], 2100 / 60, active_extruder);
  1955. current_position[E_AXIS] += 5.4;
  1956. plan_buffer_line(current_position[X_AXIS], current_position[Y_AXIS], current_position[Z_AXIS], current_position[E_AXIS], 2800 / 60, active_extruder);
  1957. current_position[E_AXIS] += 3.2;
  1958. plan_buffer_line(current_position[X_AXIS], current_position[Y_AXIS], current_position[Z_AXIS], current_position[E_AXIS], 3000 / 60, active_extruder);
  1959. current_position[E_AXIS] += 3;
  1960. plan_buffer_line(current_position[X_AXIS], current_position[Y_AXIS], current_position[Z_AXIS], current_position[E_AXIS], 3400 / 60, active_extruder);
  1961. st_synchronize();
  1962. max_feedrate[E_AXIS] = 80;
  1963. current_position[E_AXIS] -= 82;
  1964. plan_buffer_line(current_position[X_AXIS], current_position[Y_AXIS], current_position[Z_AXIS], current_position[E_AXIS], 9500 / 60, active_extruder);
  1965. max_feedrate[E_AXIS] = 50;//tmp[E_AXIS];
  1966. current_position[E_AXIS] -= 20;
  1967. plan_buffer_line(current_position[X_AXIS], current_position[Y_AXIS], current_position[Z_AXIS], current_position[E_AXIS], 1200 / 60, active_extruder);
  1968. current_position[E_AXIS] += 5;
  1969. plan_buffer_line(current_position[X_AXIS], current_position[Y_AXIS], current_position[Z_AXIS], current_position[E_AXIS], 400 / 60, active_extruder);
  1970. current_position[E_AXIS] += 5;
  1971. plan_buffer_line(current_position[X_AXIS], current_position[Y_AXIS], current_position[Z_AXIS], current_position[E_AXIS], 600 / 60, active_extruder);
  1972. current_position[E_AXIS] -= 10;
  1973. st_synchronize();
  1974. plan_buffer_line(current_position[X_AXIS], current_position[Y_AXIS], current_position[Z_AXIS], current_position[E_AXIS], 600 / 60, active_extruder);
  1975. current_position[E_AXIS] += 10;
  1976. plan_buffer_line(current_position[X_AXIS], current_position[Y_AXIS], current_position[Z_AXIS], current_position[E_AXIS], 600 / 60, active_extruder);
  1977. current_position[E_AXIS] -= 10;
  1978. plan_buffer_line(current_position[X_AXIS], current_position[Y_AXIS], current_position[Z_AXIS], current_position[E_AXIS], 800 / 60, active_extruder);
  1979. current_position[E_AXIS] += 10;
  1980. plan_buffer_line(current_position[X_AXIS], current_position[Y_AXIS], current_position[Z_AXIS], current_position[E_AXIS], 800 / 60, active_extruder);
  1981. current_position[E_AXIS] -= 10;
  1982. plan_buffer_line(current_position[X_AXIS], current_position[Y_AXIS], current_position[Z_AXIS], current_position[E_AXIS], 800 / 60, active_extruder);
  1983. st_synchronize();
  1984. }
  1985. else {
  1986. //ABS
  1987. max_feedrate[E_AXIS] = 50;
  1988. //current_position[E_AXIS] -= 8;
  1989. //plan_buffer_line(current_position[X_AXIS], current_position[Y_AXIS], current_position[Z_AXIS], current_position[E_AXIS], 2100 / 60, active_extruder);
  1990. //current_position[E_AXIS] += 8;
  1991. //plan_buffer_line(current_position[X_AXIS], current_position[Y_AXIS], current_position[Z_AXIS], current_position[E_AXIS], 2100 / 60, active_extruder);
  1992. current_position[E_AXIS] += 3.1;
  1993. plan_buffer_line(current_position[X_AXIS], current_position[Y_AXIS], current_position[Z_AXIS], current_position[E_AXIS], 2000 / 60, active_extruder);
  1994. current_position[E_AXIS] += 3.1;
  1995. plan_buffer_line(current_position[X_AXIS], current_position[Y_AXIS], current_position[Z_AXIS], current_position[E_AXIS], 2500 / 60, active_extruder);
  1996. current_position[E_AXIS] += 4;
  1997. plan_buffer_line(current_position[X_AXIS], current_position[Y_AXIS], current_position[Z_AXIS], current_position[E_AXIS], 3000 / 60, active_extruder);
  1998. st_synchronize();
  1999. //current_position[X_AXIS] += 23; //delay
  2000. //plan_buffer_line(current_position[X_AXIS], current_position[Y_AXIS], current_position[Z_AXIS], current_position[E_AXIS], 600/60, active_extruder); //delay
  2001. //current_position[X_AXIS] -= 23; //delay
  2002. //plan_buffer_line(current_position[X_AXIS], current_position[Y_AXIS], current_position[Z_AXIS], current_position[E_AXIS], 600/60, active_extruder); //delay
  2003. delay(4700);
  2004. max_feedrate[E_AXIS] = 80;
  2005. current_position[E_AXIS] -= 92;
  2006. plan_buffer_line(current_position[X_AXIS], current_position[Y_AXIS], current_position[Z_AXIS], current_position[E_AXIS], 9900 / 60, active_extruder);
  2007. max_feedrate[E_AXIS] = 50;//tmp[E_AXIS];
  2008. current_position[E_AXIS] -= 5;
  2009. plan_buffer_line(current_position[X_AXIS], current_position[Y_AXIS], current_position[Z_AXIS], current_position[E_AXIS], 800 / 60, active_extruder);
  2010. current_position[E_AXIS] += 5;
  2011. plan_buffer_line(current_position[X_AXIS], current_position[Y_AXIS], current_position[Z_AXIS], current_position[E_AXIS], 400 / 60, active_extruder);
  2012. current_position[E_AXIS] -= 5;
  2013. plan_buffer_line(current_position[X_AXIS], current_position[Y_AXIS], current_position[Z_AXIS], current_position[E_AXIS], 600 / 60, active_extruder);
  2014. st_synchronize();
  2015. current_position[E_AXIS] += 5;
  2016. plan_buffer_line(current_position[X_AXIS], current_position[Y_AXIS], current_position[Z_AXIS], current_position[E_AXIS], 600 / 60, active_extruder);
  2017. current_position[E_AXIS] -= 5;
  2018. plan_buffer_line(current_position[X_AXIS], current_position[Y_AXIS], current_position[Z_AXIS], current_position[E_AXIS], 600 / 60, active_extruder);
  2019. current_position[E_AXIS] += 5;
  2020. plan_buffer_line(current_position[X_AXIS], current_position[Y_AXIS], current_position[Z_AXIS], current_position[E_AXIS], 600 / 60, active_extruder);
  2021. current_position[E_AXIS] -= 5;
  2022. plan_buffer_line(current_position[X_AXIS], current_position[Y_AXIS], current_position[Z_AXIS], current_position[E_AXIS], 600 / 60, active_extruder);
  2023. st_synchronize();
  2024. }
  2025. }
  2026. */
  2027. #ifdef TMC2130
  2028. void force_high_power_mode(bool start_high_power_section) {
  2029. uint8_t silent;
  2030. silent = eeprom_read_byte((uint8_t*)EEPROM_SILENT);
  2031. if (silent == 1) {
  2032. //we are in silent mode, set to normal mode to enable crash detection
  2033. // Wait for the planner queue to drain and for the stepper timer routine to reach an idle state.
  2034. st_synchronize();
  2035. cli();
  2036. tmc2130_mode = (start_high_power_section == true) ? TMC2130_MODE_NORMAL : TMC2130_MODE_SILENT;
  2037. tmc2130_init();
  2038. // We may have missed a stepper timer interrupt due to the time spent in the tmc2130_init() routine.
  2039. // Be safe than sorry, reset the stepper timer before re-enabling interrupts.
  2040. st_reset_timer();
  2041. sei();
  2042. }
  2043. }
  2044. #endif //TMC2130
  2045. void gcode_G28(bool home_x, bool home_y, bool home_z, bool calib){
  2046. st_synchronize();
  2047. #if 0
  2048. SERIAL_ECHOPGM("G28, initial "); print_world_coordinates();
  2049. SERIAL_ECHOPGM("G28, initial "); print_physical_coordinates();
  2050. #endif
  2051. // Flag for the display update routine and to disable the print cancelation during homing.
  2052. homing_flag = true;
  2053. // Either all X,Y,Z codes are present, or none of them.
  2054. bool home_all_axes = home_x == home_y && home_x == home_z;
  2055. if (home_all_axes)
  2056. // No X/Y/Z code provided means to home all axes.
  2057. home_x = home_y = home_z = true;
  2058. #ifdef ENABLE_AUTO_BED_LEVELING
  2059. plan_bed_level_matrix.set_to_identity(); //Reset the plane ("erase" all leveling data)
  2060. #endif //ENABLE_AUTO_BED_LEVELING
  2061. // Reset world2machine_rotation_and_skew and world2machine_shift, therefore
  2062. // the planner will not perform any adjustments in the XY plane.
  2063. // Wait for the motors to stop and update the current position with the absolute values.
  2064. world2machine_revert_to_uncorrected();
  2065. // For mesh bed leveling deactivate the matrix temporarily.
  2066. // It is necessary to disable the bed leveling for the X and Y homing moves, so that the move is performed
  2067. // in a single axis only.
  2068. // In case of re-homing the X or Y axes only, the mesh bed leveling is restored after G28.
  2069. #ifdef MESH_BED_LEVELING
  2070. uint8_t mbl_was_active = mbl.active;
  2071. mbl.active = 0;
  2072. current_position[Z_AXIS] = st_get_position_mm(Z_AXIS);
  2073. #endif
  2074. // Reset baby stepping to zero, if the babystepping has already been loaded before. The babystepsTodo value will be
  2075. // consumed during the first movements following this statement.
  2076. if (home_z)
  2077. babystep_undo();
  2078. saved_feedrate = feedrate;
  2079. saved_feedmultiply = feedmultiply;
  2080. feedmultiply = 100;
  2081. previous_millis_cmd = millis();
  2082. enable_endstops(true);
  2083. memcpy(destination, current_position, sizeof(destination));
  2084. feedrate = 0.0;
  2085. #if Z_HOME_DIR > 0 // If homing away from BED do Z first
  2086. if(home_z)
  2087. homeaxis(Z_AXIS);
  2088. #endif
  2089. #ifdef QUICK_HOME
  2090. // In the quick mode, if both x and y are to be homed, a diagonal move will be performed initially.
  2091. if(home_x && home_y) //first diagonal move
  2092. {
  2093. current_position[X_AXIS] = 0;current_position[Y_AXIS] = 0;
  2094. int x_axis_home_dir = home_dir(X_AXIS);
  2095. plan_set_position(current_position[X_AXIS], current_position[Y_AXIS], current_position[Z_AXIS], current_position[E_AXIS]);
  2096. 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);
  2097. feedrate = homing_feedrate[X_AXIS];
  2098. if(homing_feedrate[Y_AXIS]<feedrate)
  2099. feedrate = homing_feedrate[Y_AXIS];
  2100. if (max_length(X_AXIS) > max_length(Y_AXIS)) {
  2101. feedrate *= sqrt(pow(max_length(Y_AXIS) / max_length(X_AXIS), 2) + 1);
  2102. } else {
  2103. feedrate *= sqrt(pow(max_length(X_AXIS) / max_length(Y_AXIS), 2) + 1);
  2104. }
  2105. plan_buffer_line(destination[X_AXIS], destination[Y_AXIS], destination[Z_AXIS], destination[E_AXIS], feedrate/60, active_extruder);
  2106. st_synchronize();
  2107. axis_is_at_home(X_AXIS);
  2108. axis_is_at_home(Y_AXIS);
  2109. plan_set_position(current_position[X_AXIS], current_position[Y_AXIS], current_position[Z_AXIS], current_position[E_AXIS]);
  2110. destination[X_AXIS] = current_position[X_AXIS];
  2111. destination[Y_AXIS] = current_position[Y_AXIS];
  2112. plan_buffer_line(destination[X_AXIS], destination[Y_AXIS], destination[Z_AXIS], destination[E_AXIS], feedrate/60, active_extruder);
  2113. feedrate = 0.0;
  2114. st_synchronize();
  2115. endstops_hit_on_purpose();
  2116. current_position[X_AXIS] = destination[X_AXIS];
  2117. current_position[Y_AXIS] = destination[Y_AXIS];
  2118. current_position[Z_AXIS] = destination[Z_AXIS];
  2119. }
  2120. #endif /* QUICK_HOME */
  2121. #ifdef TMC2130
  2122. if(home_x)
  2123. {
  2124. if (!calib)
  2125. homeaxis(X_AXIS);
  2126. else
  2127. tmc2130_home_calibrate(X_AXIS);
  2128. }
  2129. if(home_y)
  2130. {
  2131. if (!calib)
  2132. homeaxis(Y_AXIS);
  2133. else
  2134. tmc2130_home_calibrate(Y_AXIS);
  2135. }
  2136. #endif //TMC2130
  2137. if(code_seen(axis_codes[X_AXIS]) && code_value_long() != 0)
  2138. current_position[X_AXIS]=code_value()+add_homing[X_AXIS];
  2139. if(code_seen(axis_codes[Y_AXIS]) && code_value_long() != 0)
  2140. current_position[Y_AXIS]=code_value()+add_homing[Y_AXIS];
  2141. #if Z_HOME_DIR < 0 // If homing towards BED do Z last
  2142. #ifndef Z_SAFE_HOMING
  2143. if(home_z) {
  2144. #if defined (Z_RAISE_BEFORE_HOMING) && (Z_RAISE_BEFORE_HOMING > 0)
  2145. destination[Z_AXIS] = Z_RAISE_BEFORE_HOMING * home_dir(Z_AXIS) * (-1); // Set destination away from bed
  2146. feedrate = max_feedrate[Z_AXIS];
  2147. plan_buffer_line(destination[X_AXIS], destination[Y_AXIS], destination[Z_AXIS], destination[E_AXIS], feedrate, active_extruder);
  2148. st_synchronize();
  2149. #endif // defined (Z_RAISE_BEFORE_HOMING) && (Z_RAISE_BEFORE_HOMING > 0)
  2150. #if (defined(MESH_BED_LEVELING) && !defined(MK1BP)) // If Mesh bed leveling, move X&Y to safe position for home
  2151. if (!(axis_known_position[X_AXIS] && axis_known_position[Y_AXIS] ))
  2152. {
  2153. homeaxis(X_AXIS);
  2154. homeaxis(Y_AXIS);
  2155. }
  2156. // 1st mesh bed leveling measurement point, corrected.
  2157. world2machine_initialize();
  2158. world2machine(pgm_read_float(bed_ref_points_4), pgm_read_float(bed_ref_points_4+1), destination[X_AXIS], destination[Y_AXIS]);
  2159. world2machine_reset();
  2160. if (destination[Y_AXIS] < Y_MIN_POS)
  2161. destination[Y_AXIS] = Y_MIN_POS;
  2162. destination[Z_AXIS] = MESH_HOME_Z_SEARCH; // Set destination away from bed
  2163. feedrate = homing_feedrate[Z_AXIS]/10;
  2164. current_position[Z_AXIS] = 0;
  2165. enable_endstops(false);
  2166. #ifdef DEBUG_BUILD
  2167. SERIAL_ECHOLNPGM("plan_set_position()");
  2168. MYSERIAL.println(current_position[X_AXIS]);MYSERIAL.println(current_position[Y_AXIS]);
  2169. MYSERIAL.println(current_position[Z_AXIS]);MYSERIAL.println(current_position[E_AXIS]);
  2170. #endif
  2171. plan_set_position(current_position[X_AXIS], current_position[Y_AXIS], current_position[Z_AXIS], current_position[E_AXIS]);
  2172. #ifdef DEBUG_BUILD
  2173. SERIAL_ECHOLNPGM("plan_buffer_line()");
  2174. MYSERIAL.println(destination[X_AXIS]);MYSERIAL.println(destination[Y_AXIS]);
  2175. MYSERIAL.println(destination[Z_AXIS]);MYSERIAL.println(destination[E_AXIS]);
  2176. MYSERIAL.println(feedrate);MYSERIAL.println(active_extruder);
  2177. #endif
  2178. plan_buffer_line(destination[X_AXIS], destination[Y_AXIS], destination[Z_AXIS], destination[E_AXIS], feedrate, active_extruder);
  2179. st_synchronize();
  2180. current_position[X_AXIS] = destination[X_AXIS];
  2181. current_position[Y_AXIS] = destination[Y_AXIS];
  2182. enable_endstops(true);
  2183. endstops_hit_on_purpose();
  2184. homeaxis(Z_AXIS);
  2185. #else // MESH_BED_LEVELING
  2186. homeaxis(Z_AXIS);
  2187. #endif // MESH_BED_LEVELING
  2188. }
  2189. #else // defined(Z_SAFE_HOMING): Z Safe mode activated.
  2190. if(home_all_axes) {
  2191. destination[X_AXIS] = round(Z_SAFE_HOMING_X_POINT - X_PROBE_OFFSET_FROM_EXTRUDER);
  2192. destination[Y_AXIS] = round(Z_SAFE_HOMING_Y_POINT - Y_PROBE_OFFSET_FROM_EXTRUDER);
  2193. destination[Z_AXIS] = Z_RAISE_BEFORE_HOMING * home_dir(Z_AXIS) * (-1); // Set destination away from bed
  2194. feedrate = XY_TRAVEL_SPEED/60;
  2195. current_position[Z_AXIS] = 0;
  2196. plan_set_position(current_position[X_AXIS], current_position[Y_AXIS], current_position[Z_AXIS], current_position[E_AXIS]);
  2197. plan_buffer_line(destination[X_AXIS], destination[Y_AXIS], destination[Z_AXIS], destination[E_AXIS], feedrate, active_extruder);
  2198. st_synchronize();
  2199. current_position[X_AXIS] = destination[X_AXIS];
  2200. current_position[Y_AXIS] = destination[Y_AXIS];
  2201. homeaxis(Z_AXIS);
  2202. }
  2203. // Let's see if X and Y are homed and probe is inside bed area.
  2204. if(home_z) {
  2205. if ( (axis_known_position[X_AXIS]) && (axis_known_position[Y_AXIS]) \
  2206. && (current_position[X_AXIS]+X_PROBE_OFFSET_FROM_EXTRUDER >= X_MIN_POS) \
  2207. && (current_position[X_AXIS]+X_PROBE_OFFSET_FROM_EXTRUDER <= X_MAX_POS) \
  2208. && (current_position[Y_AXIS]+Y_PROBE_OFFSET_FROM_EXTRUDER >= Y_MIN_POS) \
  2209. && (current_position[Y_AXIS]+Y_PROBE_OFFSET_FROM_EXTRUDER <= Y_MAX_POS)) {
  2210. current_position[Z_AXIS] = 0;
  2211. plan_set_position(current_position[X_AXIS], current_position[Y_AXIS], current_position[Z_AXIS], current_position[E_AXIS]);
  2212. destination[Z_AXIS] = Z_RAISE_BEFORE_HOMING * home_dir(Z_AXIS) * (-1); // Set destination away from bed
  2213. feedrate = max_feedrate[Z_AXIS];
  2214. plan_buffer_line(destination[X_AXIS], destination[Y_AXIS], destination[Z_AXIS], destination[E_AXIS], feedrate, active_extruder);
  2215. st_synchronize();
  2216. homeaxis(Z_AXIS);
  2217. } else if (!((axis_known_position[X_AXIS]) && (axis_known_position[Y_AXIS]))) {
  2218. LCD_MESSAGERPGM(MSG_POSITION_UNKNOWN);
  2219. SERIAL_ECHO_START;
  2220. SERIAL_ECHOLNRPGM(MSG_POSITION_UNKNOWN);
  2221. } else {
  2222. LCD_MESSAGERPGM(MSG_ZPROBE_OUT);
  2223. SERIAL_ECHO_START;
  2224. SERIAL_ECHOLNRPGM(MSG_ZPROBE_OUT);
  2225. }
  2226. }
  2227. #endif // Z_SAFE_HOMING
  2228. #endif // Z_HOME_DIR < 0
  2229. if(code_seen(axis_codes[Z_AXIS]) && code_value_long() != 0)
  2230. current_position[Z_AXIS]=code_value()+add_homing[Z_AXIS];
  2231. #ifdef ENABLE_AUTO_BED_LEVELING
  2232. if(home_z)
  2233. current_position[Z_AXIS] += zprobe_zoffset; //Add Z_Probe offset (the distance is negative)
  2234. #endif
  2235. // Set the planner and stepper routine positions.
  2236. // At this point the mesh bed leveling and world2machine corrections are disabled and current_position
  2237. // contains the machine coordinates.
  2238. plan_set_position(current_position[X_AXIS], current_position[Y_AXIS], current_position[Z_AXIS], current_position[E_AXIS]);
  2239. #ifdef ENDSTOPS_ONLY_FOR_HOMING
  2240. enable_endstops(false);
  2241. #endif
  2242. feedrate = saved_feedrate;
  2243. feedmultiply = saved_feedmultiply;
  2244. previous_millis_cmd = millis();
  2245. endstops_hit_on_purpose();
  2246. #ifndef MESH_BED_LEVELING
  2247. // If MESH_BED_LEVELING is not active, then it is the original Prusa i3.
  2248. // Offer the user to load the baby step value, which has been adjusted at the previous print session.
  2249. if(card.sdprinting && eeprom_read_word((uint16_t *)EEPROM_BABYSTEP_Z))
  2250. lcd_adjust_z();
  2251. #endif
  2252. // Load the machine correction matrix
  2253. world2machine_initialize();
  2254. // and correct the current_position XY axes to match the transformed coordinate system.
  2255. world2machine_update_current();
  2256. #if (defined(MESH_BED_LEVELING) && !defined(MK1BP))
  2257. if (code_seen(axis_codes[X_AXIS]) || code_seen(axis_codes[Y_AXIS]) || code_seen('W') || code_seen(axis_codes[Z_AXIS]))
  2258. {
  2259. if (! home_z && mbl_was_active) {
  2260. // Re-enable the mesh bed leveling if only the X and Y axes were re-homed.
  2261. mbl.active = true;
  2262. // and re-adjust the current logical Z axis with the bed leveling offset applicable at the current XY position.
  2263. current_position[Z_AXIS] -= mbl.get_z(st_get_position_mm(X_AXIS), st_get_position_mm(Y_AXIS));
  2264. }
  2265. }
  2266. else
  2267. {
  2268. st_synchronize();
  2269. homing_flag = false;
  2270. // Push the commands to the front of the message queue in the reverse order!
  2271. // There shall be always enough space reserved for these commands.
  2272. enquecommand_front_P((PSTR("G80")));
  2273. //goto case_G80;
  2274. }
  2275. #endif
  2276. if (farm_mode) { prusa_statistics(20); };
  2277. homing_flag = false;
  2278. #if 0
  2279. SERIAL_ECHOPGM("G28, final "); print_world_coordinates();
  2280. SERIAL_ECHOPGM("G28, final "); print_physical_coordinates();
  2281. SERIAL_ECHOPGM("G28, final "); print_mesh_bed_leveling_table();
  2282. #endif
  2283. }
  2284. bool gcode_M45(bool onlyZ, int8_t verbosity_level)
  2285. {
  2286. bool final_result = false;
  2287. #ifdef TMC2130
  2288. FORCE_HIGH_POWER_START;
  2289. #endif // TMC2130
  2290. // Only Z calibration?
  2291. if (!onlyZ)
  2292. {
  2293. setTargetBed(0);
  2294. setTargetHotend(0, 0);
  2295. setTargetHotend(0, 1);
  2296. setTargetHotend(0, 2);
  2297. adjust_bed_reset(); //reset bed level correction
  2298. }
  2299. // Disable the default update procedure of the display. We will do a modal dialog.
  2300. lcd_update_enable(false);
  2301. // Let the planner use the uncorrected coordinates.
  2302. mbl.reset();
  2303. // Reset world2machine_rotation_and_skew and world2machine_shift, therefore
  2304. // the planner will not perform any adjustments in the XY plane.
  2305. // Wait for the motors to stop and update the current position with the absolute values.
  2306. world2machine_revert_to_uncorrected();
  2307. // Reset the baby step value applied without moving the axes.
  2308. babystep_reset();
  2309. // Mark all axes as in a need for homing.
  2310. memset(axis_known_position, 0, sizeof(axis_known_position));
  2311. // Home in the XY plane.
  2312. //set_destination_to_current();
  2313. setup_for_endstop_move();
  2314. lcd_display_message_fullscreen_P(_T(MSG_AUTO_HOME));
  2315. home_xy();
  2316. enable_endstops(false);
  2317. current_position[X_AXIS] += 5;
  2318. current_position[Y_AXIS] += 5;
  2319. 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);
  2320. st_synchronize();
  2321. // Let the user move the Z axes up to the end stoppers.
  2322. #ifdef TMC2130
  2323. if (calibrate_z_auto())
  2324. {
  2325. #else //TMC2130
  2326. if (lcd_calibrate_z_end_stop_manual(onlyZ))
  2327. {
  2328. #endif //TMC2130
  2329. refresh_cmd_timeout();
  2330. #ifndef STEEL_SHEET
  2331. if (((degHotend(0) > MAX_HOTEND_TEMP_CALIBRATION) || (degBed() > MAX_BED_TEMP_CALIBRATION)) && (!onlyZ))
  2332. {
  2333. lcd_wait_for_cool_down();
  2334. }
  2335. #endif //STEEL_SHEET
  2336. if(!onlyZ)
  2337. {
  2338. KEEPALIVE_STATE(PAUSED_FOR_USER);
  2339. #ifdef STEEL_SHEET
  2340. bool result = lcd_show_fullscreen_message_yes_no_and_wait_P(_T(MSG_STEEL_SHEET_CHECK), false, false);
  2341. if(result) lcd_show_fullscreen_message_and_wait_P(_T(MSG_REMOVE_STEEL_SHEET));
  2342. #endif //STEEL_SHEET
  2343. lcd_show_fullscreen_message_and_wait_P(_T(MSG_CONFIRM_NOZZLE_CLEAN));
  2344. lcd_show_fullscreen_message_and_wait_P(_T(MSG_PAPER));
  2345. KEEPALIVE_STATE(IN_HANDLER);
  2346. lcd_display_message_fullscreen_P(_T(MSG_FIND_BED_OFFSET_AND_SKEW_LINE1));
  2347. lcd_implementation_print_at(0, 2, 1);
  2348. lcd_printPGM(_T(MSG_FIND_BED_OFFSET_AND_SKEW_LINE2));
  2349. }
  2350. // Move the print head close to the bed.
  2351. current_position[Z_AXIS] = MESH_HOME_Z_SEARCH;
  2352. bool endstops_enabled = enable_endstops(true);
  2353. #ifdef TMC2130
  2354. tmc2130_home_enter(Z_AXIS_MASK);
  2355. #endif //TMC2130
  2356. 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);
  2357. st_synchronize();
  2358. #ifdef TMC2130
  2359. tmc2130_home_exit();
  2360. #endif //TMC2130
  2361. enable_endstops(endstops_enabled);
  2362. if (st_get_position_mm(Z_AXIS) == MESH_HOME_Z_SEARCH)
  2363. {
  2364. int8_t verbosity_level = 0;
  2365. if (code_seen('V'))
  2366. {
  2367. // Just 'V' without a number counts as V1.
  2368. char c = strchr_pointer[1];
  2369. verbosity_level = (c == ' ' || c == '\t' || c == 0) ? 1 : code_value_short();
  2370. }
  2371. if (onlyZ)
  2372. {
  2373. clean_up_after_endstop_move();
  2374. // Z only calibration.
  2375. // Load the machine correction matrix
  2376. world2machine_initialize();
  2377. // and correct the current_position to match the transformed coordinate system.
  2378. world2machine_update_current();
  2379. //FIXME
  2380. bool result = sample_mesh_and_store_reference();
  2381. if (result)
  2382. {
  2383. if (calibration_status() == CALIBRATION_STATUS_Z_CALIBRATION)
  2384. // Shipped, the nozzle height has been set already. The user can start printing now.
  2385. calibration_status_store(CALIBRATION_STATUS_CALIBRATED);
  2386. final_result = true;
  2387. // babystep_apply();
  2388. }
  2389. }
  2390. else
  2391. {
  2392. // Reset the baby step value and the baby step applied flag.
  2393. calibration_status_store(CALIBRATION_STATUS_XYZ_CALIBRATION);
  2394. eeprom_update_word((uint16_t*)EEPROM_BABYSTEP_Z, 0);
  2395. // Complete XYZ calibration.
  2396. uint8_t point_too_far_mask = 0;
  2397. BedSkewOffsetDetectionResultType result = find_bed_offset_and_skew(verbosity_level, point_too_far_mask);
  2398. clean_up_after_endstop_move();
  2399. // Print head up.
  2400. current_position[Z_AXIS] = MESH_HOME_Z_SEARCH;
  2401. 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);
  2402. st_synchronize();
  2403. //#ifndef NEW_XYZCAL
  2404. if (result >= 0)
  2405. {
  2406. #ifdef HEATBED_V2
  2407. sample_z();
  2408. #else //HEATBED_V2
  2409. point_too_far_mask = 0;
  2410. // Second half: The fine adjustment.
  2411. // Let the planner use the uncorrected coordinates.
  2412. mbl.reset();
  2413. world2machine_reset();
  2414. // Home in the XY plane.
  2415. setup_for_endstop_move();
  2416. home_xy();
  2417. result = improve_bed_offset_and_skew(1, verbosity_level, point_too_far_mask);
  2418. clean_up_after_endstop_move();
  2419. // Print head up.
  2420. current_position[Z_AXIS] = MESH_HOME_Z_SEARCH;
  2421. 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);
  2422. st_synchronize();
  2423. // if (result >= 0) babystep_apply();
  2424. #endif //HEATBED_V2
  2425. }
  2426. //#endif //NEW_XYZCAL
  2427. lcd_update_enable(true);
  2428. lcd_update(2);
  2429. lcd_bed_calibration_show_result(result, point_too_far_mask);
  2430. if (result >= 0)
  2431. {
  2432. // Calibration valid, the machine should be able to print. Advise the user to run the V2Calibration.gcode.
  2433. calibration_status_store(CALIBRATION_STATUS_LIVE_ADJUST);
  2434. if (eeprom_read_byte((uint8_t*)EEPROM_WIZARD_ACTIVE) != 1) lcd_show_fullscreen_message_and_wait_P(_T(MSG_BABYSTEP_Z_NOT_SET));
  2435. final_result = true;
  2436. }
  2437. }
  2438. #ifdef TMC2130
  2439. tmc2130_home_exit();
  2440. #endif
  2441. }
  2442. else
  2443. {
  2444. lcd_show_fullscreen_message_and_wait_P(PSTR("Calibration failed! Check the axes and run again."));
  2445. final_result = false;
  2446. }
  2447. }
  2448. else
  2449. {
  2450. // Timeouted.
  2451. }
  2452. lcd_update_enable(true);
  2453. #ifdef TMC2130
  2454. FORCE_HIGH_POWER_END;
  2455. #endif // TMC2130
  2456. return final_result;
  2457. }
  2458. void gcode_M114()
  2459. {
  2460. SERIAL_PROTOCOLPGM("X:");
  2461. SERIAL_PROTOCOL(current_position[X_AXIS]);
  2462. SERIAL_PROTOCOLPGM(" Y:");
  2463. SERIAL_PROTOCOL(current_position[Y_AXIS]);
  2464. SERIAL_PROTOCOLPGM(" Z:");
  2465. SERIAL_PROTOCOL(current_position[Z_AXIS]);
  2466. SERIAL_PROTOCOLPGM(" E:");
  2467. SERIAL_PROTOCOL(current_position[E_AXIS]);
  2468. SERIAL_PROTOCOLRPGM(_n(" Count X: "));////MSG_COUNT_X c=0 r=0
  2469. SERIAL_PROTOCOL(float(st_get_position(X_AXIS)) / axis_steps_per_unit[X_AXIS]);
  2470. SERIAL_PROTOCOLPGM(" Y:");
  2471. SERIAL_PROTOCOL(float(st_get_position(Y_AXIS)) / axis_steps_per_unit[Y_AXIS]);
  2472. SERIAL_PROTOCOLPGM(" Z:");
  2473. SERIAL_PROTOCOL(float(st_get_position(Z_AXIS)) / axis_steps_per_unit[Z_AXIS]);
  2474. SERIAL_PROTOCOLPGM(" E:");
  2475. SERIAL_PROTOCOL(float(st_get_position(E_AXIS)) / axis_steps_per_unit[E_AXIS]);
  2476. SERIAL_PROTOCOLLN("");
  2477. }
  2478. void gcode_M701()
  2479. {
  2480. #ifdef SNMM
  2481. extr_adj(snmm_extruder);//loads current extruder
  2482. #else
  2483. enable_z();
  2484. custom_message = true;
  2485. custom_message_type = 2;
  2486. lcd_setstatuspgm(_T(MSG_LOADING_FILAMENT));
  2487. current_position[E_AXIS] += 70;
  2488. 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
  2489. current_position[E_AXIS] += 25;
  2490. 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
  2491. st_synchronize();
  2492. tone(BEEPER, 500);
  2493. delay_keep_alive(50);
  2494. noTone(BEEPER);
  2495. if (!farm_mode && loading_flag) {
  2496. bool clean = lcd_show_fullscreen_message_yes_no_and_wait_P(_T(MSG_FILAMENT_CLEAN), false, true);
  2497. while (!clean) {
  2498. lcd_update_enable(true);
  2499. lcd_update(2);
  2500. current_position[E_AXIS] += 25;
  2501. 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
  2502. st_synchronize();
  2503. clean = lcd_show_fullscreen_message_yes_no_and_wait_P(_T(MSG_FILAMENT_CLEAN), false, true);
  2504. }
  2505. }
  2506. lcd_update_enable(true);
  2507. lcd_update(2);
  2508. lcd_setstatuspgm(_T(WELCOME_MSG));
  2509. disable_z();
  2510. loading_flag = false;
  2511. custom_message = false;
  2512. custom_message_type = 0;
  2513. #endif
  2514. }
  2515. /**
  2516. * @brief Get serial number from 32U2 processor
  2517. *
  2518. * Typical format of S/N is:CZPX0917X003XC13518
  2519. *
  2520. * Command operates only in farm mode, if not in farm mode, "Not in farm mode." is written to MYSERIAL.
  2521. *
  2522. * Send command ;S to serial port 0 to retrieve serial number stored in 32U2 processor,
  2523. * reply is transmitted to serial port 1 character by character.
  2524. * Operation takes typically 23 ms. If the retransmit is not finished until 100 ms,
  2525. * it is interrupted, so less, or no characters are retransmitted, only newline character is send
  2526. * in any case.
  2527. */
  2528. static void gcode_PRUSA_SN()
  2529. {
  2530. if (farm_mode) {
  2531. selectedSerialPort = 0;
  2532. MSerial.write(";S");
  2533. int numbersRead = 0;
  2534. ShortTimer timeout;
  2535. timeout.start();
  2536. while (numbersRead < 19) {
  2537. while (MSerial.available() > 0) {
  2538. uint8_t serial_char = MSerial.read();
  2539. selectedSerialPort = 1;
  2540. MSerial.write(serial_char);
  2541. numbersRead++;
  2542. selectedSerialPort = 0;
  2543. }
  2544. if (timeout.expired(100u)) break;
  2545. }
  2546. selectedSerialPort = 1;
  2547. MSerial.write('\n');
  2548. #if 0
  2549. for (int b = 0; b < 3; b++) {
  2550. tone(BEEPER, 110);
  2551. delay(50);
  2552. noTone(BEEPER);
  2553. delay(50);
  2554. }
  2555. #endif
  2556. } else {
  2557. MYSERIAL.println("Not in farm mode.");
  2558. }
  2559. }
  2560. void process_commands()
  2561. {
  2562. if (!buflen) return; //empty command
  2563. #ifdef FILAMENT_RUNOUT_SUPPORT
  2564. SET_INPUT(FR_SENS);
  2565. #endif
  2566. #ifdef CMDBUFFER_DEBUG
  2567. SERIAL_ECHOPGM("Processing a GCODE command: ");
  2568. SERIAL_ECHO(cmdbuffer+bufindr+CMDHDRSIZE);
  2569. SERIAL_ECHOLNPGM("");
  2570. SERIAL_ECHOPGM("In cmdqueue: ");
  2571. SERIAL_ECHO(buflen);
  2572. SERIAL_ECHOLNPGM("");
  2573. #endif /* CMDBUFFER_DEBUG */
  2574. unsigned long codenum; //throw away variable
  2575. char *starpos = NULL;
  2576. #ifdef ENABLE_AUTO_BED_LEVELING
  2577. float x_tmp, y_tmp, z_tmp, real_z;
  2578. #endif
  2579. // PRUSA GCODES
  2580. KEEPALIVE_STATE(IN_HANDLER);
  2581. #ifdef SNMM
  2582. float tmp_motor[3] = DEFAULT_PWM_MOTOR_CURRENT;
  2583. float tmp_motor_loud[3] = DEFAULT_PWM_MOTOR_CURRENT_LOUD;
  2584. int8_t SilentMode;
  2585. #endif
  2586. if (code_seen("M117")) { //moved to highest priority place to be able to to print strings which includes "G", "PRUSA" and "^"
  2587. starpos = (strchr(strchr_pointer + 5, '*'));
  2588. if (starpos != NULL)
  2589. *(starpos) = '\0';
  2590. lcd_setstatus(strchr_pointer + 5);
  2591. }
  2592. #ifdef TMC2130
  2593. else if (strncmp_P(CMDBUFFER_CURRENT_STRING, PSTR("CRASH_"), 6) == 0)
  2594. {
  2595. if(code_seen("CRASH_DETECTED"))
  2596. {
  2597. uint8_t mask = 0;
  2598. if (code_seen("X")) mask |= X_AXIS_MASK;
  2599. if (code_seen("Y")) mask |= Y_AXIS_MASK;
  2600. crashdet_detected(mask);
  2601. }
  2602. else if(code_seen("CRASH_RECOVER"))
  2603. crashdet_recover();
  2604. else if(code_seen("CRASH_CANCEL"))
  2605. crashdet_cancel();
  2606. }
  2607. else if (strncmp_P(CMDBUFFER_CURRENT_STRING, PSTR("TMC_"), 4) == 0)
  2608. {
  2609. if (strncmp_P(CMDBUFFER_CURRENT_STRING + 4, PSTR("SET_WAVE_E"), 10) == 0)
  2610. {
  2611. uint8_t fac = (uint8_t)strtol(CMDBUFFER_CURRENT_STRING + 14, NULL, 10);
  2612. tmc2130_set_wave(E_AXIS, 247, fac);
  2613. }
  2614. else if (strncmp_P(CMDBUFFER_CURRENT_STRING + 4, PSTR("SET_STEP_E"), 10) == 0)
  2615. {
  2616. uint8_t step = (uint8_t)strtol(CMDBUFFER_CURRENT_STRING + 14, NULL, 10);
  2617. uint16_t res = tmc2130_get_res(E_AXIS);
  2618. tmc2130_goto_step(E_AXIS, step & (4*res - 1), 2, 1000, res);
  2619. }
  2620. }
  2621. #endif //TMC2130
  2622. else if(code_seen("PRUSA")){
  2623. if (code_seen("Ping")) { //PRUSA Ping
  2624. if (farm_mode) {
  2625. PingTime = millis();
  2626. //MYSERIAL.print(farm_no); MYSERIAL.println(": OK");
  2627. }
  2628. }
  2629. else if (code_seen("PRN")) {
  2630. MYSERIAL.println(status_number);
  2631. }else if (code_seen("FAN")) {
  2632. MYSERIAL.print("E0:");
  2633. MYSERIAL.print(60*fan_speed[0]);
  2634. MYSERIAL.println(" RPM");
  2635. MYSERIAL.print("PRN0:");
  2636. MYSERIAL.print(60*fan_speed[1]);
  2637. MYSERIAL.println(" RPM");
  2638. }else if (code_seen("fn")) {
  2639. if (farm_mode) {
  2640. MYSERIAL.println(farm_no);
  2641. }
  2642. else {
  2643. MYSERIAL.println("Not in farm mode.");
  2644. }
  2645. }
  2646. else if (code_seen("thx")) {
  2647. no_response = false;
  2648. }else if (code_seen("fv")) {
  2649. // get file version
  2650. #ifdef SDSUPPORT
  2651. card.openFile(strchr_pointer + 3,true);
  2652. while (true) {
  2653. uint16_t readByte = card.get();
  2654. MYSERIAL.write(readByte);
  2655. if (readByte=='\n') {
  2656. break;
  2657. }
  2658. }
  2659. card.closefile();
  2660. #endif // SDSUPPORT
  2661. } else if (code_seen("M28")) {
  2662. trace();
  2663. prusa_sd_card_upload = true;
  2664. card.openFile(strchr_pointer+4,false);
  2665. } else if (code_seen("SN")) {
  2666. gcode_PRUSA_SN();
  2667. } else if(code_seen("Fir")){
  2668. SERIAL_PROTOCOLLN(FW_VERSION);
  2669. } else if(code_seen("Rev")){
  2670. SERIAL_PROTOCOLLN(FILAMENT_SIZE "-" ELECTRONICS "-" NOZZLE_TYPE );
  2671. } else if(code_seen("Lang")) {
  2672. lcd_force_language_selection();
  2673. } else if(code_seen("Lz")) {
  2674. EEPROM_save_B(EEPROM_BABYSTEP_Z,0);
  2675. } else if (code_seen("SERIAL LOW")) {
  2676. MYSERIAL.println("SERIAL LOW");
  2677. MYSERIAL.begin(BAUDRATE);
  2678. return;
  2679. } else if (code_seen("SERIAL HIGH")) {
  2680. MYSERIAL.println("SERIAL HIGH");
  2681. MYSERIAL.begin(1152000);
  2682. return;
  2683. } else if(code_seen("Beat")) {
  2684. // Kick farm link timer
  2685. kicktime = millis();
  2686. } else if(code_seen("FR")) {
  2687. // Factory full reset
  2688. factory_reset(0,true);
  2689. }
  2690. //else if (code_seen('Cal')) {
  2691. // lcd_calibration();
  2692. // }
  2693. }
  2694. else if (code_seen('^')) {
  2695. // nothing, this is a version line
  2696. } else if(code_seen('G'))
  2697. {
  2698. switch((int)code_value())
  2699. {
  2700. case 0: // G0 -> G1
  2701. case 1: // G1
  2702. if(Stopped == false) {
  2703. #ifdef FILAMENT_RUNOUT_SUPPORT
  2704. if(READ(FR_SENS)){
  2705. feedmultiplyBckp=feedmultiply;
  2706. float target[4];
  2707. float lastpos[4];
  2708. target[X_AXIS]=current_position[X_AXIS];
  2709. target[Y_AXIS]=current_position[Y_AXIS];
  2710. target[Z_AXIS]=current_position[Z_AXIS];
  2711. target[E_AXIS]=current_position[E_AXIS];
  2712. lastpos[X_AXIS]=current_position[X_AXIS];
  2713. lastpos[Y_AXIS]=current_position[Y_AXIS];
  2714. lastpos[Z_AXIS]=current_position[Z_AXIS];
  2715. lastpos[E_AXIS]=current_position[E_AXIS];
  2716. //retract by E
  2717. target[E_AXIS]+= FILAMENTCHANGE_FIRSTRETRACT ;
  2718. plan_buffer_line(target[X_AXIS], target[Y_AXIS], target[Z_AXIS], target[E_AXIS], 400, active_extruder);
  2719. target[Z_AXIS]+= FILAMENTCHANGE_ZADD ;
  2720. plan_buffer_line(target[X_AXIS], target[Y_AXIS], target[Z_AXIS], target[E_AXIS], 300, active_extruder);
  2721. target[X_AXIS]= FILAMENTCHANGE_XPOS ;
  2722. target[Y_AXIS]= FILAMENTCHANGE_YPOS ;
  2723. plan_buffer_line(target[X_AXIS], target[Y_AXIS], target[Z_AXIS], target[E_AXIS], 70, active_extruder);
  2724. target[E_AXIS]+= FILAMENTCHANGE_FINALRETRACT ;
  2725. plan_buffer_line(target[X_AXIS], target[Y_AXIS], target[Z_AXIS], target[E_AXIS], 20, active_extruder);
  2726. //finish moves
  2727. st_synchronize();
  2728. //disable extruder steppers so filament can be removed
  2729. disable_e0();
  2730. disable_e1();
  2731. disable_e2();
  2732. delay(100);
  2733. //LCD_ALERTMESSAGEPGM(_T(MSG_FILAMENTCHANGE));
  2734. uint8_t cnt=0;
  2735. int counterBeep = 0;
  2736. lcd_wait_interact();
  2737. while(!lcd_clicked()){
  2738. cnt++;
  2739. manage_heater();
  2740. manage_inactivity(true);
  2741. //lcd_update();
  2742. if(cnt==0)
  2743. {
  2744. #if BEEPER > 0
  2745. if (counterBeep== 500){
  2746. counterBeep = 0;
  2747. }
  2748. SET_OUTPUT(BEEPER);
  2749. if (counterBeep== 0){
  2750. WRITE(BEEPER,HIGH);
  2751. }
  2752. if (counterBeep== 20){
  2753. WRITE(BEEPER,LOW);
  2754. }
  2755. counterBeep++;
  2756. #else
  2757. #if !defined(LCD_FEEDBACK_FREQUENCY_HZ) || !defined(LCD_FEEDBACK_FREQUENCY_DURATION_MS)
  2758. lcd_buzz(1000/6,100);
  2759. #else
  2760. lcd_buzz(LCD_FEEDBACK_FREQUENCY_DURATION_MS,LCD_FEEDBACK_FREQUENCY_HZ);
  2761. #endif
  2762. #endif
  2763. }
  2764. }
  2765. WRITE(BEEPER,LOW);
  2766. target[E_AXIS]+= FILAMENTCHANGE_FIRSTFEED ;
  2767. plan_buffer_line(target[X_AXIS], target[Y_AXIS], target[Z_AXIS], target[E_AXIS], 20, active_extruder);
  2768. target[E_AXIS]+= FILAMENTCHANGE_FINALFEED ;
  2769. plan_buffer_line(target[X_AXIS], target[Y_AXIS], target[Z_AXIS], target[E_AXIS], 2, active_extruder);
  2770. lcd_change_fil_state = 0;
  2771. lcd_loading_filament();
  2772. while ((lcd_change_fil_state == 0)||(lcd_change_fil_state != 1)){
  2773. lcd_change_fil_state = 0;
  2774. lcd_alright();
  2775. switch(lcd_change_fil_state){
  2776. case 2:
  2777. target[E_AXIS]+= FILAMENTCHANGE_FIRSTFEED ;
  2778. plan_buffer_line(target[X_AXIS], target[Y_AXIS], target[Z_AXIS], target[E_AXIS], 20, active_extruder);
  2779. target[E_AXIS]+= FILAMENTCHANGE_FINALFEED ;
  2780. plan_buffer_line(target[X_AXIS], target[Y_AXIS], target[Z_AXIS], target[E_AXIS], 2, active_extruder);
  2781. lcd_loading_filament();
  2782. break;
  2783. case 3:
  2784. target[E_AXIS]+= FILAMENTCHANGE_FINALFEED ;
  2785. plan_buffer_line(target[X_AXIS], target[Y_AXIS], target[Z_AXIS], target[E_AXIS], 2, active_extruder);
  2786. lcd_loading_color();
  2787. break;
  2788. default:
  2789. lcd_change_success();
  2790. break;
  2791. }
  2792. }
  2793. target[E_AXIS]+= 5;
  2794. plan_buffer_line(target[X_AXIS], target[Y_AXIS], target[Z_AXIS], target[E_AXIS], 2, active_extruder);
  2795. target[E_AXIS]+= FILAMENTCHANGE_FIRSTRETRACT;
  2796. plan_buffer_line(target[X_AXIS], target[Y_AXIS], target[Z_AXIS], target[E_AXIS], 400, active_extruder);
  2797. //current_position[E_AXIS]=target[E_AXIS]; //the long retract of L is compensated by manual filament feeding
  2798. //plan_set_e_position(current_position[E_AXIS]);
  2799. plan_buffer_line(target[X_AXIS], target[Y_AXIS], target[Z_AXIS], target[E_AXIS], 70, active_extruder); //should do nothing
  2800. plan_buffer_line(lastpos[X_AXIS], lastpos[Y_AXIS], target[Z_AXIS], target[E_AXIS], 70, active_extruder); //move xy back
  2801. plan_buffer_line(lastpos[X_AXIS], lastpos[Y_AXIS], lastpos[Z_AXIS], target[E_AXIS], 200, active_extruder); //move z back
  2802. target[E_AXIS]= target[E_AXIS] - FILAMENTCHANGE_FIRSTRETRACT;
  2803. plan_buffer_line(lastpos[X_AXIS], lastpos[Y_AXIS], lastpos[Z_AXIS], target[E_AXIS], 5, active_extruder); //final untretract
  2804. plan_set_e_position(lastpos[E_AXIS]);
  2805. feedmultiply=feedmultiplyBckp;
  2806. char cmd[9];
  2807. sprintf_P(cmd, PSTR("M220 S%i"), feedmultiplyBckp);
  2808. enquecommand(cmd);
  2809. }
  2810. #endif
  2811. get_coordinates(); // For X Y Z E F
  2812. if (total_filament_used > ((current_position[E_AXIS] - destination[E_AXIS]) * 100)) { //protection against total_filament_used overflow
  2813. total_filament_used = total_filament_used + ((destination[E_AXIS] - current_position[E_AXIS]) * 100);
  2814. }
  2815. #ifdef FWRETRACT
  2816. if(autoretract_enabled)
  2817. if( !(code_seen('X') || code_seen('Y') || code_seen('Z')) && code_seen('E')) {
  2818. float echange=destination[E_AXIS]-current_position[E_AXIS];
  2819. if((echange<-MIN_RETRACT && !retracted) || (echange>MIN_RETRACT && retracted)) { //move appears to be an attempt to retract or recover
  2820. current_position[E_AXIS] = destination[E_AXIS]; //hide the slicer-generated retract/recover from calculations
  2821. plan_set_e_position(current_position[E_AXIS]); //AND from the planner
  2822. retract(!retracted);
  2823. return;
  2824. }
  2825. }
  2826. #endif //FWRETRACT
  2827. prepare_move();
  2828. //ClearToSend();
  2829. }
  2830. break;
  2831. case 2: // G2 - CW ARC
  2832. if(Stopped == false) {
  2833. get_arc_coordinates();
  2834. prepare_arc_move(true);
  2835. }
  2836. break;
  2837. case 3: // G3 - CCW ARC
  2838. if(Stopped == false) {
  2839. get_arc_coordinates();
  2840. prepare_arc_move(false);
  2841. }
  2842. break;
  2843. case 4: // G4 dwell
  2844. codenum = 0;
  2845. if(code_seen('P')) codenum = code_value(); // milliseconds to wait
  2846. if(code_seen('S')) codenum = code_value() * 1000; // seconds to wait
  2847. if(codenum != 0) LCD_MESSAGERPGM(_i("Sleep..."));////MSG_DWELL c=0 r=0
  2848. st_synchronize();
  2849. codenum += millis(); // keep track of when we started waiting
  2850. previous_millis_cmd = millis();
  2851. while(millis() < codenum) {
  2852. manage_heater();
  2853. manage_inactivity();
  2854. lcd_update();
  2855. }
  2856. break;
  2857. #ifdef FWRETRACT
  2858. case 10: // G10 retract
  2859. #if EXTRUDERS > 1
  2860. retracted_swap[active_extruder]=(code_seen('S') && code_value_long() == 1); // checks for swap retract argument
  2861. retract(true,retracted_swap[active_extruder]);
  2862. #else
  2863. retract(true);
  2864. #endif
  2865. break;
  2866. case 11: // G11 retract_recover
  2867. #if EXTRUDERS > 1
  2868. retract(false,retracted_swap[active_extruder]);
  2869. #else
  2870. retract(false);
  2871. #endif
  2872. break;
  2873. #endif //FWRETRACT
  2874. case 28: //G28 Home all Axis one at a time
  2875. {
  2876. // Which axes should be homed?
  2877. bool home_x = code_seen(axis_codes[X_AXIS]);
  2878. bool home_y = code_seen(axis_codes[Y_AXIS]);
  2879. bool home_z = code_seen(axis_codes[Z_AXIS]);
  2880. // calibrate?
  2881. bool calib = code_seen('C');
  2882. gcode_G28(home_x, home_y, home_z, calib);
  2883. break;
  2884. }
  2885. #ifdef ENABLE_AUTO_BED_LEVELING
  2886. case 29: // G29 Detailed Z-Probe, probes the bed at 3 or more points.
  2887. {
  2888. #if Z_MIN_PIN == -1
  2889. #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."
  2890. #endif
  2891. // Prevent user from running a G29 without first homing in X and Y
  2892. if (! (axis_known_position[X_AXIS] && axis_known_position[Y_AXIS]) )
  2893. {
  2894. LCD_MESSAGERPGM(MSG_POSITION_UNKNOWN);
  2895. SERIAL_ECHO_START;
  2896. SERIAL_ECHOLNRPGM(MSG_POSITION_UNKNOWN);
  2897. break; // abort G29, since we don't know where we are
  2898. }
  2899. st_synchronize();
  2900. // make sure the bed_level_rotation_matrix is identity or the planner will get it incorectly
  2901. //vector_3 corrected_position = plan_get_position_mm();
  2902. //corrected_position.debug("position before G29");
  2903. plan_bed_level_matrix.set_to_identity();
  2904. vector_3 uncorrected_position = plan_get_position();
  2905. //uncorrected_position.debug("position durring G29");
  2906. current_position[X_AXIS] = uncorrected_position.x;
  2907. current_position[Y_AXIS] = uncorrected_position.y;
  2908. current_position[Z_AXIS] = uncorrected_position.z;
  2909. plan_set_position(current_position[X_AXIS], current_position[Y_AXIS], current_position[Z_AXIS], current_position[E_AXIS]);
  2910. setup_for_endstop_move();
  2911. feedrate = homing_feedrate[Z_AXIS];
  2912. #ifdef AUTO_BED_LEVELING_GRID
  2913. // probe at the points of a lattice grid
  2914. int xGridSpacing = (RIGHT_PROBE_BED_POSITION - LEFT_PROBE_BED_POSITION) / (AUTO_BED_LEVELING_GRID_POINTS-1);
  2915. int yGridSpacing = (BACK_PROBE_BED_POSITION - FRONT_PROBE_BED_POSITION) / (AUTO_BED_LEVELING_GRID_POINTS-1);
  2916. // solve the plane equation ax + by + d = z
  2917. // A is the matrix with rows [x y 1] for all the probed points
  2918. // B is the vector of the Z positions
  2919. // 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
  2920. // so Vx = -a Vy = -b Vz = 1 (we want the vector facing towards positive Z
  2921. // "A" matrix of the linear system of equations
  2922. double eqnAMatrix[AUTO_BED_LEVELING_GRID_POINTS*AUTO_BED_LEVELING_GRID_POINTS*3];
  2923. // "B" vector of Z points
  2924. double eqnBVector[AUTO_BED_LEVELING_GRID_POINTS*AUTO_BED_LEVELING_GRID_POINTS];
  2925. int probePointCounter = 0;
  2926. bool zig = true;
  2927. for (int yProbe=FRONT_PROBE_BED_POSITION; yProbe <= BACK_PROBE_BED_POSITION; yProbe += yGridSpacing)
  2928. {
  2929. int xProbe, xInc;
  2930. if (zig)
  2931. {
  2932. xProbe = LEFT_PROBE_BED_POSITION;
  2933. //xEnd = RIGHT_PROBE_BED_POSITION;
  2934. xInc = xGridSpacing;
  2935. zig = false;
  2936. } else // zag
  2937. {
  2938. xProbe = RIGHT_PROBE_BED_POSITION;
  2939. //xEnd = LEFT_PROBE_BED_POSITION;
  2940. xInc = -xGridSpacing;
  2941. zig = true;
  2942. }
  2943. for (int xCount=0; xCount < AUTO_BED_LEVELING_GRID_POINTS; xCount++)
  2944. {
  2945. float z_before;
  2946. if (probePointCounter == 0)
  2947. {
  2948. // raise before probing
  2949. z_before = Z_RAISE_BEFORE_PROBING;
  2950. } else
  2951. {
  2952. // raise extruder
  2953. z_before = current_position[Z_AXIS] + Z_RAISE_BETWEEN_PROBINGS;
  2954. }
  2955. float measured_z = probe_pt(xProbe, yProbe, z_before);
  2956. eqnBVector[probePointCounter] = measured_z;
  2957. eqnAMatrix[probePointCounter + 0*AUTO_BED_LEVELING_GRID_POINTS*AUTO_BED_LEVELING_GRID_POINTS] = xProbe;
  2958. eqnAMatrix[probePointCounter + 1*AUTO_BED_LEVELING_GRID_POINTS*AUTO_BED_LEVELING_GRID_POINTS] = yProbe;
  2959. eqnAMatrix[probePointCounter + 2*AUTO_BED_LEVELING_GRID_POINTS*AUTO_BED_LEVELING_GRID_POINTS] = 1;
  2960. probePointCounter++;
  2961. xProbe += xInc;
  2962. }
  2963. }
  2964. clean_up_after_endstop_move();
  2965. // solve lsq problem
  2966. double *plane_equation_coefficients = qr_solve(AUTO_BED_LEVELING_GRID_POINTS*AUTO_BED_LEVELING_GRID_POINTS, 3, eqnAMatrix, eqnBVector);
  2967. SERIAL_PROTOCOLPGM("Eqn coefficients: a: ");
  2968. SERIAL_PROTOCOL(plane_equation_coefficients[0]);
  2969. SERIAL_PROTOCOLPGM(" b: ");
  2970. SERIAL_PROTOCOL(plane_equation_coefficients[1]);
  2971. SERIAL_PROTOCOLPGM(" d: ");
  2972. SERIAL_PROTOCOLLN(plane_equation_coefficients[2]);
  2973. set_bed_level_equation_lsq(plane_equation_coefficients);
  2974. free(plane_equation_coefficients);
  2975. #else // AUTO_BED_LEVELING_GRID not defined
  2976. // Probe at 3 arbitrary points
  2977. // probe 1
  2978. float z_at_pt_1 = probe_pt(ABL_PROBE_PT_1_X, ABL_PROBE_PT_1_Y, Z_RAISE_BEFORE_PROBING);
  2979. // probe 2
  2980. 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);
  2981. // probe 3
  2982. 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);
  2983. clean_up_after_endstop_move();
  2984. set_bed_level_equation_3pts(z_at_pt_1, z_at_pt_2, z_at_pt_3);
  2985. #endif // AUTO_BED_LEVELING_GRID
  2986. st_synchronize();
  2987. // The following code correct the Z height difference from z-probe position and hotend tip position.
  2988. // The Z height on homing is measured by Z-Probe, but the probe is quite far from the hotend.
  2989. // When the bed is uneven, this height must be corrected.
  2990. 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)
  2991. x_tmp = current_position[X_AXIS] + X_PROBE_OFFSET_FROM_EXTRUDER;
  2992. y_tmp = current_position[Y_AXIS] + Y_PROBE_OFFSET_FROM_EXTRUDER;
  2993. z_tmp = current_position[Z_AXIS];
  2994. apply_rotation_xyz(plan_bed_level_matrix, x_tmp, y_tmp, z_tmp); //Apply the correction sending the probe offset
  2995. current_position[Z_AXIS] = z_tmp - real_z + current_position[Z_AXIS]; //The difference is added to current position and sent to planner.
  2996. plan_set_position(current_position[X_AXIS], current_position[Y_AXIS], current_position[Z_AXIS], current_position[E_AXIS]);
  2997. }
  2998. break;
  2999. #ifndef Z_PROBE_SLED
  3000. case 30: // G30 Single Z Probe
  3001. {
  3002. st_synchronize();
  3003. // TODO: make sure the bed_level_rotation_matrix is identity or the planner will get set incorectly
  3004. setup_for_endstop_move();
  3005. feedrate = homing_feedrate[Z_AXIS];
  3006. run_z_probe();
  3007. SERIAL_PROTOCOLPGM(_T(MSG_BED));
  3008. SERIAL_PROTOCOLPGM(" X: ");
  3009. SERIAL_PROTOCOL(current_position[X_AXIS]);
  3010. SERIAL_PROTOCOLPGM(" Y: ");
  3011. SERIAL_PROTOCOL(current_position[Y_AXIS]);
  3012. SERIAL_PROTOCOLPGM(" Z: ");
  3013. SERIAL_PROTOCOL(current_position[Z_AXIS]);
  3014. SERIAL_PROTOCOLPGM("\n");
  3015. clean_up_after_endstop_move();
  3016. }
  3017. break;
  3018. #else
  3019. case 31: // dock the sled
  3020. dock_sled(true);
  3021. break;
  3022. case 32: // undock the sled
  3023. dock_sled(false);
  3024. break;
  3025. #endif // Z_PROBE_SLED
  3026. #endif // ENABLE_AUTO_BED_LEVELING
  3027. #ifdef MESH_BED_LEVELING
  3028. case 30: // G30 Single Z Probe
  3029. {
  3030. st_synchronize();
  3031. // TODO: make sure the bed_level_rotation_matrix is identity or the planner will get set incorectly
  3032. setup_for_endstop_move();
  3033. feedrate = homing_feedrate[Z_AXIS];
  3034. find_bed_induction_sensor_point_z(-10.f, 3);
  3035. SERIAL_PROTOCOLRPGM(_T(MSG_BED));
  3036. SERIAL_PROTOCOLPGM(" X: ");
  3037. MYSERIAL.print(current_position[X_AXIS], 5);
  3038. SERIAL_PROTOCOLPGM(" Y: ");
  3039. MYSERIAL.print(current_position[Y_AXIS], 5);
  3040. SERIAL_PROTOCOLPGM(" Z: ");
  3041. MYSERIAL.print(current_position[Z_AXIS], 5);
  3042. SERIAL_PROTOCOLPGM("\n");
  3043. clean_up_after_endstop_move();
  3044. }
  3045. break;
  3046. case 75:
  3047. {
  3048. for (int i = 40; i <= 110; i++) {
  3049. MYSERIAL.print(i);
  3050. MYSERIAL.print(" ");
  3051. MYSERIAL.println(temp_comp_interpolation(i));// / axis_steps_per_unit[Z_AXIS]);
  3052. }
  3053. }
  3054. break;
  3055. case 76: //PINDA probe temperature calibration
  3056. {
  3057. #ifdef PINDA_THERMISTOR
  3058. if (true)
  3059. {
  3060. if (calibration_status() >= CALIBRATION_STATUS_XYZ_CALIBRATION) {
  3061. //we need to know accurate position of first calibration point
  3062. //if xyz calibration was not performed yet, interrupt temperature calibration and inform user that xyz cal. is needed
  3063. lcd_show_fullscreen_message_and_wait_P(_i("Please run XYZ calibration first."));
  3064. break;
  3065. }
  3066. if (!(axis_known_position[X_AXIS] && axis_known_position[Y_AXIS] && axis_known_position[Z_AXIS]))
  3067. {
  3068. // We don't know where we are! HOME!
  3069. // Push the commands to the front of the message queue in the reverse order!
  3070. // There shall be always enough space reserved for these commands.
  3071. repeatcommand_front(); // repeat G76 with all its parameters
  3072. enquecommand_front_P((PSTR("G28 W0")));
  3073. break;
  3074. }
  3075. lcd_show_fullscreen_message_and_wait_P(_i("Stable ambient temperature 21-26C is needed a rigid stand is required."));////MSG_TEMP_CAL_WARNING c=20 r=4
  3076. bool result = lcd_show_fullscreen_message_yes_no_and_wait_P(_T(MSG_STEEL_SHEET_CHECK), false, false);
  3077. if (result)
  3078. {
  3079. current_position[Z_AXIS] = MESH_HOME_Z_SEARCH;
  3080. plan_buffer_line(current_position[X_AXIS], current_position[Y_AXIS], current_position[Z_AXIS], current_position[E_AXIS], 3000 / 60, active_extruder);
  3081. current_position[Z_AXIS] = 50;
  3082. current_position[Y_AXIS] = 180;
  3083. plan_buffer_line(current_position[X_AXIS], current_position[Y_AXIS], current_position[Z_AXIS], current_position[E_AXIS], 3000 / 60, active_extruder);
  3084. st_synchronize();
  3085. lcd_show_fullscreen_message_and_wait_P(_T(MSG_REMOVE_STEEL_SHEET));
  3086. current_position[Y_AXIS] = pgm_read_float(bed_ref_points_4 + 1);
  3087. current_position[X_AXIS] = pgm_read_float(bed_ref_points_4);
  3088. plan_buffer_line(current_position[X_AXIS], current_position[Y_AXIS], current_position[Z_AXIS], current_position[E_AXIS], 3000 / 60, active_extruder);
  3089. st_synchronize();
  3090. gcode_G28(false, false, true, false);
  3091. }
  3092. if ((current_temperature_pinda > 35) && (farm_mode == false)) {
  3093. //waiting for PIDNA probe to cool down in case that we are not in farm mode
  3094. current_position[Z_AXIS] = 100;
  3095. plan_buffer_line(current_position[X_AXIS], current_position[Y_AXIS], current_position[Z_AXIS], current_position[E_AXIS], 3000 / 60, active_extruder);
  3096. if (lcd_wait_for_pinda(35) == false) { //waiting for PINDA probe to cool, if this takes more then time expected, temp. cal. fails
  3097. lcd_temp_cal_show_result(false);
  3098. break;
  3099. }
  3100. }
  3101. lcd_update_enable(true);
  3102. KEEPALIVE_STATE(NOT_BUSY); //no need to print busy messages as we print current temperatures periodicaly
  3103. SERIAL_ECHOLNPGM("PINDA probe calibration start");
  3104. float zero_z;
  3105. int z_shift = 0; //unit: steps
  3106. float start_temp = 5 * (int)(current_temperature_pinda / 5);
  3107. if (start_temp < 35) start_temp = 35;
  3108. if (start_temp < current_temperature_pinda) start_temp += 5;
  3109. SERIAL_ECHOPGM("start temperature: ");
  3110. MYSERIAL.println(start_temp);
  3111. // setTargetHotend(200, 0);
  3112. setTargetBed(70 + (start_temp - 30));
  3113. custom_message = true;
  3114. custom_message_type = 4;
  3115. custom_message_state = 1;
  3116. custom_message = _T(MSG_TEMP_CALIBRATION);
  3117. current_position[Z_AXIS] = MESH_HOME_Z_SEARCH;
  3118. plan_buffer_line(current_position[X_AXIS], current_position[Y_AXIS], current_position[Z_AXIS], current_position[E_AXIS], 3000 / 60, active_extruder);
  3119. current_position[X_AXIS] = PINDA_PREHEAT_X;
  3120. current_position[Y_AXIS] = PINDA_PREHEAT_Y;
  3121. plan_buffer_line(current_position[X_AXIS], current_position[Y_AXIS], current_position[Z_AXIS], current_position[E_AXIS], 3000 / 60, active_extruder);
  3122. current_position[Z_AXIS] = PINDA_PREHEAT_Z;
  3123. plan_buffer_line(current_position[X_AXIS], current_position[Y_AXIS], current_position[Z_AXIS], current_position[E_AXIS], 3000 / 60, active_extruder);
  3124. st_synchronize();
  3125. while (current_temperature_pinda < start_temp)
  3126. {
  3127. delay_keep_alive(1000);
  3128. serialecho_temperatures();
  3129. }
  3130. eeprom_update_byte((uint8_t*)EEPROM_CALIBRATION_STATUS_PINDA, 0); //invalidate temp. calibration in case that in will be aborted during the calibration process
  3131. current_position[Z_AXIS] = MESH_HOME_Z_SEARCH;
  3132. plan_buffer_line(current_position[X_AXIS], current_position[Y_AXIS], current_position[Z_AXIS], current_position[E_AXIS], 3000 / 60, active_extruder);
  3133. current_position[X_AXIS] = pgm_read_float(bed_ref_points_4);
  3134. current_position[Y_AXIS] = pgm_read_float(bed_ref_points_4 + 1);
  3135. plan_buffer_line(current_position[X_AXIS], current_position[Y_AXIS], current_position[Z_AXIS], current_position[E_AXIS], 3000 / 60, active_extruder);
  3136. st_synchronize();
  3137. bool find_z_result = find_bed_induction_sensor_point_z(-1.f);
  3138. if (find_z_result == false) {
  3139. lcd_temp_cal_show_result(find_z_result);
  3140. break;
  3141. }
  3142. zero_z = current_position[Z_AXIS];
  3143. //current_position[Z_AXIS]
  3144. SERIAL_ECHOLNPGM("");
  3145. SERIAL_ECHOPGM("ZERO: ");
  3146. MYSERIAL.print(current_position[Z_AXIS]);
  3147. SERIAL_ECHOLNPGM("");
  3148. int i = -1; for (; i < 5; i++)
  3149. {
  3150. float temp = (40 + i * 5);
  3151. SERIAL_ECHOPGM("Step: ");
  3152. MYSERIAL.print(i + 2);
  3153. SERIAL_ECHOLNPGM("/6 (skipped)");
  3154. SERIAL_ECHOPGM("PINDA temperature: ");
  3155. MYSERIAL.print((40 + i*5));
  3156. SERIAL_ECHOPGM(" Z shift (mm):");
  3157. MYSERIAL.print(0);
  3158. SERIAL_ECHOLNPGM("");
  3159. if (i >= 0) EEPROM_save_B(EEPROM_PROBE_TEMP_SHIFT + i * 2, &z_shift);
  3160. if (start_temp <= temp) break;
  3161. }
  3162. for (i++; i < 5; i++)
  3163. {
  3164. float temp = (40 + i * 5);
  3165. SERIAL_ECHOPGM("Step: ");
  3166. MYSERIAL.print(i + 2);
  3167. SERIAL_ECHOLNPGM("/6");
  3168. custom_message_state = i + 2;
  3169. setTargetBed(50 + 10 * (temp - 30) / 5);
  3170. // setTargetHotend(255, 0);
  3171. current_position[Z_AXIS] = MESH_HOME_Z_SEARCH;
  3172. plan_buffer_line(current_position[X_AXIS], current_position[Y_AXIS], current_position[Z_AXIS], current_position[E_AXIS], 3000 / 60, active_extruder);
  3173. current_position[X_AXIS] = PINDA_PREHEAT_X;
  3174. current_position[Y_AXIS] = PINDA_PREHEAT_Y;
  3175. plan_buffer_line(current_position[X_AXIS], current_position[Y_AXIS], current_position[Z_AXIS], current_position[E_AXIS], 3000 / 60, active_extruder);
  3176. current_position[Z_AXIS] = PINDA_PREHEAT_Z;
  3177. plan_buffer_line(current_position[X_AXIS], current_position[Y_AXIS], current_position[Z_AXIS], current_position[E_AXIS], 3000 / 60, active_extruder);
  3178. st_synchronize();
  3179. while (current_temperature_pinda < temp)
  3180. {
  3181. delay_keep_alive(1000);
  3182. serialecho_temperatures();
  3183. }
  3184. current_position[Z_AXIS] = MESH_HOME_Z_SEARCH;
  3185. plan_buffer_line(current_position[X_AXIS], current_position[Y_AXIS], current_position[Z_AXIS], current_position[E_AXIS], 3000 / 60, active_extruder);
  3186. current_position[X_AXIS] = pgm_read_float(bed_ref_points_4);
  3187. current_position[Y_AXIS] = pgm_read_float(bed_ref_points_4 + 1);
  3188. plan_buffer_line(current_position[X_AXIS], current_position[Y_AXIS], current_position[Z_AXIS], current_position[E_AXIS], 3000 / 60, active_extruder);
  3189. st_synchronize();
  3190. find_z_result = find_bed_induction_sensor_point_z(-1.f);
  3191. if (find_z_result == false) {
  3192. lcd_temp_cal_show_result(find_z_result);
  3193. break;
  3194. }
  3195. z_shift = (int)((current_position[Z_AXIS] - zero_z)*axis_steps_per_unit[Z_AXIS]);
  3196. SERIAL_ECHOLNPGM("");
  3197. SERIAL_ECHOPGM("PINDA temperature: ");
  3198. MYSERIAL.print(current_temperature_pinda);
  3199. SERIAL_ECHOPGM(" Z shift (mm):");
  3200. MYSERIAL.print(current_position[Z_AXIS] - zero_z);
  3201. SERIAL_ECHOLNPGM("");
  3202. EEPROM_save_B(EEPROM_PROBE_TEMP_SHIFT + i * 2, &z_shift);
  3203. }
  3204. lcd_temp_cal_show_result(true);
  3205. break;
  3206. }
  3207. #endif //PINDA_THERMISTOR
  3208. setTargetBed(PINDA_MIN_T);
  3209. float zero_z;
  3210. int z_shift = 0; //unit: steps
  3211. int t_c; // temperature
  3212. if (!(axis_known_position[X_AXIS] && axis_known_position[Y_AXIS] && axis_known_position[Z_AXIS])) {
  3213. // We don't know where we are! HOME!
  3214. // Push the commands to the front of the message queue in the reverse order!
  3215. // There shall be always enough space reserved for these commands.
  3216. repeatcommand_front(); // repeat G76 with all its parameters
  3217. enquecommand_front_P((PSTR("G28 W0")));
  3218. break;
  3219. }
  3220. SERIAL_ECHOLNPGM("PINDA probe calibration start");
  3221. custom_message = true;
  3222. custom_message_type = 4;
  3223. custom_message_state = 1;
  3224. custom_message = _T(MSG_TEMP_CALIBRATION);
  3225. current_position[X_AXIS] = PINDA_PREHEAT_X;
  3226. current_position[Y_AXIS] = PINDA_PREHEAT_Y;
  3227. current_position[Z_AXIS] = PINDA_PREHEAT_Z;
  3228. plan_buffer_line(current_position[X_AXIS], current_position[Y_AXIS], current_position[Z_AXIS], current_position[E_AXIS], 3000 / 60, active_extruder);
  3229. st_synchronize();
  3230. while (abs(degBed() - PINDA_MIN_T) > 1) {
  3231. delay_keep_alive(1000);
  3232. serialecho_temperatures();
  3233. }
  3234. //enquecommand_P(PSTR("M190 S50"));
  3235. for (int i = 0; i < PINDA_HEAT_T; i++) {
  3236. delay_keep_alive(1000);
  3237. serialecho_temperatures();
  3238. }
  3239. eeprom_update_byte((uint8_t*)EEPROM_CALIBRATION_STATUS_PINDA, 0); //invalidate temp. calibration in case that in will be aborted during the calibration process
  3240. current_position[Z_AXIS] = 5;
  3241. plan_buffer_line(current_position[X_AXIS], current_position[Y_AXIS], current_position[Z_AXIS], current_position[E_AXIS], 3000 / 60, active_extruder);
  3242. current_position[X_AXIS] = pgm_read_float(bed_ref_points);
  3243. current_position[Y_AXIS] = pgm_read_float(bed_ref_points + 1);
  3244. plan_buffer_line(current_position[X_AXIS], current_position[Y_AXIS], current_position[Z_AXIS], current_position[E_AXIS], 3000 / 60, active_extruder);
  3245. st_synchronize();
  3246. find_bed_induction_sensor_point_z(-1.f);
  3247. zero_z = current_position[Z_AXIS];
  3248. //current_position[Z_AXIS]
  3249. SERIAL_ECHOLNPGM("");
  3250. SERIAL_ECHOPGM("ZERO: ");
  3251. MYSERIAL.print(current_position[Z_AXIS]);
  3252. SERIAL_ECHOLNPGM("");
  3253. for (int i = 0; i<5; i++) {
  3254. SERIAL_ECHOPGM("Step: ");
  3255. MYSERIAL.print(i+2);
  3256. SERIAL_ECHOLNPGM("/6");
  3257. custom_message_state = i + 2;
  3258. t_c = 60 + i * 10;
  3259. setTargetBed(t_c);
  3260. current_position[X_AXIS] = PINDA_PREHEAT_X;
  3261. current_position[Y_AXIS] = PINDA_PREHEAT_Y;
  3262. current_position[Z_AXIS] = PINDA_PREHEAT_Z;
  3263. plan_buffer_line(current_position[X_AXIS], current_position[Y_AXIS], current_position[Z_AXIS], current_position[E_AXIS], 3000 / 60, active_extruder);
  3264. st_synchronize();
  3265. while (degBed() < t_c) {
  3266. delay_keep_alive(1000);
  3267. serialecho_temperatures();
  3268. }
  3269. for (int i = 0; i < PINDA_HEAT_T; i++) {
  3270. delay_keep_alive(1000);
  3271. serialecho_temperatures();
  3272. }
  3273. current_position[Z_AXIS] = 5;
  3274. plan_buffer_line(current_position[X_AXIS], current_position[Y_AXIS], current_position[Z_AXIS], current_position[E_AXIS], 3000 / 60, active_extruder);
  3275. current_position[X_AXIS] = pgm_read_float(bed_ref_points);
  3276. current_position[Y_AXIS] = pgm_read_float(bed_ref_points + 1);
  3277. plan_buffer_line(current_position[X_AXIS], current_position[Y_AXIS], current_position[Z_AXIS], current_position[E_AXIS], 3000 / 60, active_extruder);
  3278. st_synchronize();
  3279. find_bed_induction_sensor_point_z(-1.f);
  3280. z_shift = (int)((current_position[Z_AXIS] - zero_z)*axis_steps_per_unit[Z_AXIS]);
  3281. SERIAL_ECHOLNPGM("");
  3282. SERIAL_ECHOPGM("Temperature: ");
  3283. MYSERIAL.print(t_c);
  3284. SERIAL_ECHOPGM(" Z shift (mm):");
  3285. MYSERIAL.print(current_position[Z_AXIS] - zero_z);
  3286. SERIAL_ECHOLNPGM("");
  3287. EEPROM_save_B(EEPROM_PROBE_TEMP_SHIFT + i*2, &z_shift);
  3288. }
  3289. custom_message_type = 0;
  3290. custom_message = false;
  3291. eeprom_update_byte((uint8_t*)EEPROM_CALIBRATION_STATUS_PINDA, 1);
  3292. SERIAL_ECHOLNPGM("Temperature calibration done. Continue with pressing the knob.");
  3293. disable_x();
  3294. disable_y();
  3295. disable_z();
  3296. disable_e0();
  3297. disable_e1();
  3298. disable_e2();
  3299. setTargetBed(0); //set bed target temperature back to 0
  3300. lcd_show_fullscreen_message_and_wait_P(_T(MSG_TEMP_CALIBRATION_DONE));
  3301. temp_cal_active = true;
  3302. eeprom_update_byte((unsigned char *)EEPROM_TEMP_CAL_ACTIVE, 1);
  3303. lcd_update_enable(true);
  3304. lcd_update(2);
  3305. }
  3306. break;
  3307. #ifdef DIS
  3308. case 77:
  3309. {
  3310. //G77 X200 Y150 XP100 YP15 XO10 Y015
  3311. //for 9 point mesh bed leveling G77 X203 Y196 XP3 YP3 XO0 YO0
  3312. //G77 X232 Y218 XP116 YP109 XO-11 YO0
  3313. float dimension_x = 40;
  3314. float dimension_y = 40;
  3315. int points_x = 40;
  3316. int points_y = 40;
  3317. float offset_x = 74;
  3318. float offset_y = 33;
  3319. if (code_seen('X')) dimension_x = code_value();
  3320. if (code_seen('Y')) dimension_y = code_value();
  3321. if (code_seen('XP')) points_x = code_value();
  3322. if (code_seen('YP')) points_y = code_value();
  3323. if (code_seen('XO')) offset_x = code_value();
  3324. if (code_seen('YO')) offset_y = code_value();
  3325. bed_analysis(dimension_x,dimension_y,points_x,points_y,offset_x,offset_y);
  3326. } break;
  3327. #endif
  3328. case 79: {
  3329. for (int i = 255; i > 0; i = i - 5) {
  3330. fanSpeed = i;
  3331. //delay_keep_alive(2000);
  3332. for (int j = 0; j < 100; j++) {
  3333. delay_keep_alive(100);
  3334. }
  3335. fan_speed[1];
  3336. MYSERIAL.print(i); SERIAL_ECHOPGM(": "); MYSERIAL.println(fan_speed[1]);
  3337. }
  3338. }break;
  3339. /**
  3340. * G80: Mesh-based Z probe, probes a grid and produces a
  3341. * mesh to compensate for variable bed height
  3342. *
  3343. * The S0 report the points as below
  3344. *
  3345. * +----> X-axis
  3346. * |
  3347. * |
  3348. * v Y-axis
  3349. *
  3350. */
  3351. case 80:
  3352. #ifdef MK1BP
  3353. break;
  3354. #endif //MK1BP
  3355. case_G80:
  3356. {
  3357. mesh_bed_leveling_flag = true;
  3358. int8_t verbosity_level = 0;
  3359. static bool run = false;
  3360. if (code_seen('V')) {
  3361. // Just 'V' without a number counts as V1.
  3362. char c = strchr_pointer[1];
  3363. verbosity_level = (c == ' ' || c == '\t' || c == 0) ? 1 : code_value_short();
  3364. }
  3365. // Firstly check if we know where we are
  3366. if (!(axis_known_position[X_AXIS] && axis_known_position[Y_AXIS] && axis_known_position[Z_AXIS])) {
  3367. // We don't know where we are! HOME!
  3368. // Push the commands to the front of the message queue in the reverse order!
  3369. // There shall be always enough space reserved for these commands.
  3370. if (lcd_commands_type != LCD_COMMAND_STOP_PRINT) {
  3371. repeatcommand_front(); // repeat G80 with all its parameters
  3372. enquecommand_front_P((PSTR("G28 W0")));
  3373. }
  3374. else {
  3375. mesh_bed_leveling_flag = false;
  3376. }
  3377. break;
  3378. }
  3379. bool temp_comp_start = true;
  3380. #ifdef PINDA_THERMISTOR
  3381. temp_comp_start = false;
  3382. #endif //PINDA_THERMISTOR
  3383. if (temp_comp_start)
  3384. if (run == false && temp_cal_active == true && calibration_status_pinda() == true && target_temperature_bed >= 50) {
  3385. if (lcd_commands_type != LCD_COMMAND_STOP_PRINT) {
  3386. temp_compensation_start();
  3387. run = true;
  3388. repeatcommand_front(); // repeat G80 with all its parameters
  3389. enquecommand_front_P((PSTR("G28 W0")));
  3390. }
  3391. else {
  3392. mesh_bed_leveling_flag = false;
  3393. }
  3394. break;
  3395. }
  3396. run = false;
  3397. if (lcd_commands_type == LCD_COMMAND_STOP_PRINT) {
  3398. mesh_bed_leveling_flag = false;
  3399. break;
  3400. }
  3401. // Save custom message state, set a new custom message state to display: Calibrating point 9.
  3402. bool custom_message_old = custom_message;
  3403. unsigned int custom_message_type_old = custom_message_type;
  3404. unsigned int custom_message_state_old = custom_message_state;
  3405. custom_message = true;
  3406. custom_message_type = 1;
  3407. custom_message_state = (MESH_MEAS_NUM_X_POINTS * MESH_MEAS_NUM_Y_POINTS) + 10;
  3408. lcd_update(1);
  3409. mbl.reset(); //reset mesh bed leveling
  3410. // Reset baby stepping to zero, if the babystepping has already been loaded before. The babystepsTodo value will be
  3411. // consumed during the first movements following this statement.
  3412. babystep_undo();
  3413. // Cycle through all points and probe them
  3414. // First move up. During this first movement, the babystepping will be reverted.
  3415. current_position[Z_AXIS] = MESH_HOME_Z_SEARCH;
  3416. 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);
  3417. // The move to the first calibration point.
  3418. current_position[X_AXIS] = pgm_read_float(bed_ref_points);
  3419. current_position[Y_AXIS] = pgm_read_float(bed_ref_points + 1);
  3420. bool clamped = world2machine_clamp(current_position[X_AXIS], current_position[Y_AXIS]);
  3421. #ifdef SUPPORT_VERBOSITY
  3422. if (verbosity_level >= 1) {
  3423. clamped ? SERIAL_PROTOCOLPGM("First calibration point clamped.\n") : SERIAL_PROTOCOLPGM("No clamping for first calibration point.\n");
  3424. }
  3425. #endif //SUPPORT_VERBOSITY
  3426. // mbl.get_meas_xy(0, 0, current_position[X_AXIS], current_position[Y_AXIS], false);
  3427. 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);
  3428. // Wait until the move is finished.
  3429. st_synchronize();
  3430. int mesh_point = 0; //index number of calibration point
  3431. int ix = 0;
  3432. int iy = 0;
  3433. int XY_AXIS_FEEDRATE = homing_feedrate[X_AXIS] / 20;
  3434. int Z_PROBE_FEEDRATE = homing_feedrate[Z_AXIS] / 60;
  3435. int Z_LIFT_FEEDRATE = homing_feedrate[Z_AXIS] / 40;
  3436. 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)
  3437. #ifdef SUPPORT_VERBOSITY
  3438. if (verbosity_level >= 1) {
  3439. has_z ? SERIAL_PROTOCOLPGM("Z jitter data from Z cal. valid.\n") : SERIAL_PROTOCOLPGM("Z jitter data from Z cal. not valid.\n");
  3440. }
  3441. #endif // SUPPORT_VERBOSITY
  3442. setup_for_endstop_move(false); //save feedrate and feedmultiply, sets feedmultiply to 100
  3443. const char *kill_message = NULL;
  3444. while (mesh_point != MESH_MEAS_NUM_X_POINTS * MESH_MEAS_NUM_Y_POINTS) {
  3445. // Get coords of a measuring point.
  3446. ix = mesh_point % MESH_MEAS_NUM_X_POINTS; // from 0 to MESH_NUM_X_POINTS - 1
  3447. iy = mesh_point / MESH_MEAS_NUM_X_POINTS;
  3448. if (iy & 1) ix = (MESH_MEAS_NUM_X_POINTS - 1) - ix; // Zig zag
  3449. float z0 = 0.f;
  3450. if (has_z && mesh_point > 0) {
  3451. uint16_t z_offset_u = eeprom_read_word((uint16_t*)(EEPROM_BED_CALIBRATION_Z_JITTER + 2 * (ix + iy * 3 - 1)));
  3452. z0 = mbl.z_values[0][0] + *reinterpret_cast<int16_t*>(&z_offset_u) * 0.01;
  3453. //#if 0
  3454. #ifdef SUPPORT_VERBOSITY
  3455. if (verbosity_level >= 1) {
  3456. SERIAL_ECHOLNPGM("");
  3457. SERIAL_ECHOPGM("Bed leveling, point: ");
  3458. MYSERIAL.print(mesh_point);
  3459. SERIAL_ECHOPGM(", calibration z: ");
  3460. MYSERIAL.print(z0, 5);
  3461. SERIAL_ECHOLNPGM("");
  3462. }
  3463. #endif // SUPPORT_VERBOSITY
  3464. //#endif
  3465. }
  3466. // Move Z up to MESH_HOME_Z_SEARCH.
  3467. current_position[Z_AXIS] = MESH_HOME_Z_SEARCH;
  3468. plan_buffer_line(current_position[X_AXIS], current_position[Y_AXIS], current_position[Z_AXIS], current_position[E_AXIS], Z_LIFT_FEEDRATE, active_extruder);
  3469. st_synchronize();
  3470. // Move to XY position of the sensor point.
  3471. current_position[X_AXIS] = pgm_read_float(bed_ref_points + 2 * mesh_point);
  3472. current_position[Y_AXIS] = pgm_read_float(bed_ref_points + 2 * mesh_point + 1);
  3473. world2machine_clamp(current_position[X_AXIS], current_position[Y_AXIS]);
  3474. #ifdef SUPPORT_VERBOSITY
  3475. if (verbosity_level >= 1) {
  3476. SERIAL_PROTOCOL(mesh_point);
  3477. clamped ? SERIAL_PROTOCOLPGM(": xy clamped.\n") : SERIAL_PROTOCOLPGM(": no xy clamping\n");
  3478. }
  3479. #endif // SUPPORT_VERBOSITY
  3480. plan_buffer_line(current_position[X_AXIS], current_position[Y_AXIS], current_position[Z_AXIS], current_position[E_AXIS], XY_AXIS_FEEDRATE, active_extruder);
  3481. st_synchronize();
  3482. // Go down until endstop is hit
  3483. const float Z_CALIBRATION_THRESHOLD = 1.f;
  3484. 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
  3485. kill_message = _T(MSG_BED_LEVELING_FAILED_POINT_LOW);
  3486. break;
  3487. }
  3488. if (MESH_HOME_Z_SEARCH - current_position[Z_AXIS] < 0.1f) {
  3489. kill_message = _i("Bed leveling failed. Sensor disconnected or cable broken. Waiting for reset.");////MSG_BED_LEVELING_FAILED_PROBE_DISCONNECTED c=20 r=4
  3490. break;
  3491. }
  3492. 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
  3493. kill_message = _i("Bed leveling failed. Sensor triggered too high. Waiting for reset.");////MSG_BED_LEVELING_FAILED_POINT_HIGH c=20 r=4
  3494. break;
  3495. }
  3496. #ifdef SUPPORT_VERBOSITY
  3497. if (verbosity_level >= 10) {
  3498. SERIAL_ECHOPGM("X: ");
  3499. MYSERIAL.print(current_position[X_AXIS], 5);
  3500. SERIAL_ECHOLNPGM("");
  3501. SERIAL_ECHOPGM("Y: ");
  3502. MYSERIAL.print(current_position[Y_AXIS], 5);
  3503. SERIAL_PROTOCOLPGM("\n");
  3504. }
  3505. #endif // SUPPORT_VERBOSITY
  3506. float offset_z = 0;
  3507. #ifdef PINDA_THERMISTOR
  3508. offset_z = temp_compensation_pinda_thermistor_offset(current_temperature_pinda);
  3509. #endif //PINDA_THERMISTOR
  3510. // #ifdef SUPPORT_VERBOSITY
  3511. /* if (verbosity_level >= 1)
  3512. {
  3513. SERIAL_ECHOPGM("mesh bed leveling: ");
  3514. MYSERIAL.print(current_position[Z_AXIS], 5);
  3515. SERIAL_ECHOPGM(" offset: ");
  3516. MYSERIAL.print(offset_z, 5);
  3517. SERIAL_ECHOLNPGM("");
  3518. }*/
  3519. // #endif // SUPPORT_VERBOSITY
  3520. mbl.set_z(ix, iy, current_position[Z_AXIS] - offset_z); //store measured z values z_values[iy][ix] = z - offset_z;
  3521. custom_message_state--;
  3522. mesh_point++;
  3523. lcd_update(1);
  3524. }
  3525. current_position[Z_AXIS] = MESH_HOME_Z_SEARCH;
  3526. #ifdef SUPPORT_VERBOSITY
  3527. if (verbosity_level >= 20) {
  3528. SERIAL_ECHOLNPGM("Mesh bed leveling while loop finished.");
  3529. SERIAL_ECHOLNPGM("MESH_HOME_Z_SEARCH: ");
  3530. MYSERIAL.print(current_position[Z_AXIS], 5);
  3531. }
  3532. #endif // SUPPORT_VERBOSITY
  3533. plan_buffer_line(current_position[X_AXIS], current_position[Y_AXIS], current_position[Z_AXIS], current_position[E_AXIS], Z_LIFT_FEEDRATE, active_extruder);
  3534. st_synchronize();
  3535. if (mesh_point != MESH_MEAS_NUM_X_POINTS * MESH_MEAS_NUM_Y_POINTS) {
  3536. kill(kill_message);
  3537. SERIAL_ECHOLNPGM("killed");
  3538. }
  3539. clean_up_after_endstop_move();
  3540. // SERIAL_ECHOLNPGM("clean up finished ");
  3541. bool apply_temp_comp = true;
  3542. #ifdef PINDA_THERMISTOR
  3543. apply_temp_comp = false;
  3544. #endif
  3545. if (apply_temp_comp)
  3546. if(temp_cal_active == true && calibration_status_pinda() == true) temp_compensation_apply(); //apply PINDA temperature compensation
  3547. babystep_apply(); // Apply Z height correction aka baby stepping before mesh bed leveing gets activated.
  3548. // SERIAL_ECHOLNPGM("babystep applied");
  3549. bool eeprom_bed_correction_valid = eeprom_read_byte((unsigned char*)EEPROM_BED_CORRECTION_VALID) == 1;
  3550. #ifdef SUPPORT_VERBOSITY
  3551. if (verbosity_level >= 1) {
  3552. eeprom_bed_correction_valid ? SERIAL_PROTOCOLPGM("Bed correction data valid\n") : SERIAL_PROTOCOLPGM("Bed correction data not valid\n");
  3553. }
  3554. #endif // SUPPORT_VERBOSITY
  3555. for (uint8_t i = 0; i < 4; ++i) {
  3556. unsigned char codes[4] = { 'L', 'R', 'F', 'B' };
  3557. long correction = 0;
  3558. if (code_seen(codes[i]))
  3559. correction = code_value_long();
  3560. else if (eeprom_bed_correction_valid) {
  3561. unsigned char *addr = (i < 2) ?
  3562. ((i == 0) ? (unsigned char*)EEPROM_BED_CORRECTION_LEFT : (unsigned char*)EEPROM_BED_CORRECTION_RIGHT) :
  3563. ((i == 2) ? (unsigned char*)EEPROM_BED_CORRECTION_FRONT : (unsigned char*)EEPROM_BED_CORRECTION_REAR);
  3564. correction = eeprom_read_int8(addr);
  3565. }
  3566. if (correction == 0)
  3567. continue;
  3568. float offset = float(correction) * 0.001f;
  3569. if (fabs(offset) > 0.101f) {
  3570. SERIAL_ERROR_START;
  3571. SERIAL_ECHOPGM("Excessive bed leveling correction: ");
  3572. SERIAL_ECHO(offset);
  3573. SERIAL_ECHOLNPGM(" microns");
  3574. }
  3575. else {
  3576. switch (i) {
  3577. case 0:
  3578. for (uint8_t row = 0; row < 3; ++row) {
  3579. mbl.z_values[row][1] += 0.5f * offset;
  3580. mbl.z_values[row][0] += offset;
  3581. }
  3582. break;
  3583. case 1:
  3584. for (uint8_t row = 0; row < 3; ++row) {
  3585. mbl.z_values[row][1] += 0.5f * offset;
  3586. mbl.z_values[row][2] += offset;
  3587. }
  3588. break;
  3589. case 2:
  3590. for (uint8_t col = 0; col < 3; ++col) {
  3591. mbl.z_values[1][col] += 0.5f * offset;
  3592. mbl.z_values[0][col] += offset;
  3593. }
  3594. break;
  3595. case 3:
  3596. for (uint8_t col = 0; col < 3; ++col) {
  3597. mbl.z_values[1][col] += 0.5f * offset;
  3598. mbl.z_values[2][col] += offset;
  3599. }
  3600. break;
  3601. }
  3602. }
  3603. }
  3604. // SERIAL_ECHOLNPGM("Bed leveling correction finished");
  3605. 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)
  3606. // SERIAL_ECHOLNPGM("Upsample finished");
  3607. mbl.active = 1; //activate mesh bed leveling
  3608. // SERIAL_ECHOLNPGM("Mesh bed leveling activated");
  3609. go_home_with_z_lift();
  3610. // SERIAL_ECHOLNPGM("Go home finished");
  3611. //unretract (after PINDA preheat retraction)
  3612. if (degHotend(active_extruder) > EXTRUDE_MINTEMP && temp_cal_active == true && calibration_status_pinda() == true && target_temperature_bed >= 50) {
  3613. current_position[E_AXIS] += DEFAULT_RETRACTION;
  3614. plan_buffer_line(current_position[X_AXIS], current_position[Y_AXIS], current_position[Z_AXIS], current_position[E_AXIS], 400, active_extruder);
  3615. }
  3616. KEEPALIVE_STATE(NOT_BUSY);
  3617. // Restore custom message state
  3618. custom_message = custom_message_old;
  3619. custom_message_type = custom_message_type_old;
  3620. custom_message_state = custom_message_state_old;
  3621. mesh_bed_leveling_flag = false;
  3622. mesh_bed_run_from_menu = false;
  3623. lcd_update(2);
  3624. }
  3625. break;
  3626. /**
  3627. * G81: Print mesh bed leveling status and bed profile if activated
  3628. */
  3629. case 81:
  3630. if (mbl.active) {
  3631. SERIAL_PROTOCOLPGM("Num X,Y: ");
  3632. SERIAL_PROTOCOL(MESH_NUM_X_POINTS);
  3633. SERIAL_PROTOCOLPGM(",");
  3634. SERIAL_PROTOCOL(MESH_NUM_Y_POINTS);
  3635. SERIAL_PROTOCOLPGM("\nZ search height: ");
  3636. SERIAL_PROTOCOL(MESH_HOME_Z_SEARCH);
  3637. SERIAL_PROTOCOLLNPGM("\nMeasured points:");
  3638. for (int y = MESH_NUM_Y_POINTS-1; y >= 0; y--) {
  3639. for (int x = 0; x < MESH_NUM_X_POINTS; x++) {
  3640. SERIAL_PROTOCOLPGM(" ");
  3641. SERIAL_PROTOCOL_F(mbl.z_values[y][x], 5);
  3642. }
  3643. SERIAL_PROTOCOLPGM("\n");
  3644. }
  3645. }
  3646. else
  3647. SERIAL_PROTOCOLLNPGM("Mesh bed leveling not active.");
  3648. break;
  3649. #if 0
  3650. /**
  3651. * G82: Single Z probe at current location
  3652. *
  3653. * WARNING! USE WITH CAUTION! If you'll try to probe where is no leveling pad, nasty things can happen!
  3654. *
  3655. */
  3656. case 82:
  3657. SERIAL_PROTOCOLLNPGM("Finding bed ");
  3658. setup_for_endstop_move();
  3659. find_bed_induction_sensor_point_z();
  3660. clean_up_after_endstop_move();
  3661. SERIAL_PROTOCOLPGM("Bed found at: ");
  3662. SERIAL_PROTOCOL_F(current_position[Z_AXIS], 5);
  3663. SERIAL_PROTOCOLPGM("\n");
  3664. break;
  3665. /**
  3666. * G83: Prusa3D specific: Babystep in Z and store to EEPROM
  3667. */
  3668. case 83:
  3669. {
  3670. int babystepz = code_seen('S') ? code_value() : 0;
  3671. int BabyPosition = code_seen('P') ? code_value() : 0;
  3672. if (babystepz != 0) {
  3673. //FIXME Vojtech: What shall be the index of the axis Z: 3 or 4?
  3674. // Is the axis indexed starting with zero or one?
  3675. if (BabyPosition > 4) {
  3676. SERIAL_PROTOCOLLNPGM("Index out of bounds");
  3677. }else{
  3678. // Save it to the eeprom
  3679. babystepLoadZ = babystepz;
  3680. EEPROM_save_B(EEPROM_BABYSTEP_Z0+(BabyPosition*2),&babystepLoadZ);
  3681. // adjust the Z
  3682. babystepsTodoZadd(babystepLoadZ);
  3683. }
  3684. }
  3685. }
  3686. break;
  3687. /**
  3688. * G84: Prusa3D specific: UNDO Babystep Z (move Z axis back)
  3689. */
  3690. case 84:
  3691. babystepsTodoZsubtract(babystepLoadZ);
  3692. // babystepLoadZ = 0;
  3693. break;
  3694. /**
  3695. * G85: Prusa3D specific: Pick best babystep
  3696. */
  3697. case 85:
  3698. lcd_pick_babystep();
  3699. break;
  3700. #endif
  3701. /**
  3702. * G86: Prusa3D specific: Disable babystep correction after home.
  3703. * This G-code will be performed at the start of a calibration script.
  3704. */
  3705. case 86:
  3706. calibration_status_store(CALIBRATION_STATUS_LIVE_ADJUST);
  3707. break;
  3708. /**
  3709. * G87: Prusa3D specific: Enable babystep correction after home
  3710. * This G-code will be performed at the end of a calibration script.
  3711. */
  3712. case 87:
  3713. calibration_status_store(CALIBRATION_STATUS_CALIBRATED);
  3714. break;
  3715. /**
  3716. * G88: Prusa3D specific: Don't know what it is for, it is in V2Calibration.gcode
  3717. */
  3718. case 88:
  3719. break;
  3720. #endif // ENABLE_MESH_BED_LEVELING
  3721. case 90: // G90
  3722. relative_mode = false;
  3723. break;
  3724. case 91: // G91
  3725. relative_mode = true;
  3726. break;
  3727. case 92: // G92
  3728. if(!code_seen(axis_codes[E_AXIS]))
  3729. st_synchronize();
  3730. for(int8_t i=0; i < NUM_AXIS; i++) {
  3731. if(code_seen(axis_codes[i])) {
  3732. if(i == E_AXIS) {
  3733. current_position[i] = code_value();
  3734. plan_set_e_position(current_position[E_AXIS]);
  3735. }
  3736. else {
  3737. current_position[i] = code_value()+add_homing[i];
  3738. plan_set_position(current_position[X_AXIS], current_position[Y_AXIS], current_position[Z_AXIS], current_position[E_AXIS]);
  3739. }
  3740. }
  3741. }
  3742. break;
  3743. case 98: // G98 (activate farm mode)
  3744. farm_mode = 1;
  3745. PingTime = millis();
  3746. eeprom_update_byte((unsigned char *)EEPROM_FARM_MODE, farm_mode);
  3747. SilentModeMenu = SILENT_MODE_OFF;
  3748. eeprom_update_byte((unsigned char *)EEPROM_SILENT, SilentModeMenu);
  3749. break;
  3750. case 99: // G99 (deactivate farm mode)
  3751. farm_mode = 0;
  3752. lcd_printer_connected();
  3753. eeprom_update_byte((unsigned char *)EEPROM_FARM_MODE, farm_mode);
  3754. lcd_update(2);
  3755. break;
  3756. default:
  3757. printf_P(PSTR("Unknown G code: %s \n"), cmdbuffer + bufindr + CMDHDRSIZE);
  3758. }
  3759. } // end if(code_seen('G'))
  3760. else if(code_seen('M'))
  3761. {
  3762. int index;
  3763. for (index = 1; *(strchr_pointer + index) == ' ' || *(strchr_pointer + index) == '\t'; index++);
  3764. /*for (++strchr_pointer; *strchr_pointer == ' ' || *strchr_pointer == '\t'; ++strchr_pointer);*/
  3765. if (*(strchr_pointer+index) < '0' || *(strchr_pointer+index) > '9') {
  3766. printf_P(PSTR("Invalid M code: %s \n"), cmdbuffer + bufindr + CMDHDRSIZE);
  3767. } else
  3768. switch((int)code_value())
  3769. {
  3770. #ifdef ULTIPANEL
  3771. case 0: // M0 - Unconditional stop - Wait for user button press on LCD
  3772. case 1: // M1 - Conditional stop - Wait for user button press on LCD
  3773. {
  3774. char *src = strchr_pointer + 2;
  3775. codenum = 0;
  3776. bool hasP = false, hasS = false;
  3777. if (code_seen('P')) {
  3778. codenum = code_value(); // milliseconds to wait
  3779. hasP = codenum > 0;
  3780. }
  3781. if (code_seen('S')) {
  3782. codenum = code_value() * 1000; // seconds to wait
  3783. hasS = codenum > 0;
  3784. }
  3785. starpos = strchr(src, '*');
  3786. if (starpos != NULL) *(starpos) = '\0';
  3787. while (*src == ' ') ++src;
  3788. if (!hasP && !hasS && *src != '\0') {
  3789. lcd_setstatus(src);
  3790. } else {
  3791. LCD_MESSAGERPGM(_i("Wait for user..."));////MSG_USERWAIT c=0 r=0
  3792. }
  3793. lcd_ignore_click(); //call lcd_ignore_click aslo for else ???
  3794. st_synchronize();
  3795. previous_millis_cmd = millis();
  3796. if (codenum > 0){
  3797. codenum += millis(); // keep track of when we started waiting
  3798. KEEPALIVE_STATE(PAUSED_FOR_USER);
  3799. while(millis() < codenum && !lcd_clicked()){
  3800. manage_heater();
  3801. manage_inactivity(true);
  3802. lcd_update();
  3803. }
  3804. KEEPALIVE_STATE(IN_HANDLER);
  3805. lcd_ignore_click(false);
  3806. }else{
  3807. if (!lcd_detected())
  3808. break;
  3809. KEEPALIVE_STATE(PAUSED_FOR_USER);
  3810. while(!lcd_clicked()){
  3811. manage_heater();
  3812. manage_inactivity(true);
  3813. lcd_update();
  3814. }
  3815. KEEPALIVE_STATE(IN_HANDLER);
  3816. }
  3817. if (IS_SD_PRINTING)
  3818. LCD_MESSAGERPGM(_i("Resuming print"));////MSG_RESUMING c=0 r=0
  3819. else
  3820. LCD_MESSAGERPGM(_T(WELCOME_MSG));
  3821. }
  3822. break;
  3823. #endif
  3824. case 17:
  3825. LCD_MESSAGERPGM(_i("No move."));////MSG_NO_MOVE c=0 r=0
  3826. enable_x();
  3827. enable_y();
  3828. enable_z();
  3829. enable_e0();
  3830. enable_e1();
  3831. enable_e2();
  3832. break;
  3833. #ifdef SDSUPPORT
  3834. case 20: // M20 - list SD card
  3835. SERIAL_PROTOCOLLNRPGM(_i("Begin file list"));////MSG_BEGIN_FILE_LIST c=0 r=0
  3836. card.ls();
  3837. SERIAL_PROTOCOLLNRPGM(_i("End file list"));////MSG_END_FILE_LIST c=0 r=0
  3838. break;
  3839. case 21: // M21 - init SD card
  3840. card.initsd();
  3841. break;
  3842. case 22: //M22 - release SD card
  3843. card.release();
  3844. break;
  3845. case 23: //M23 - Select file
  3846. starpos = (strchr(strchr_pointer + 4,'*'));
  3847. if(starpos!=NULL)
  3848. *(starpos)='\0';
  3849. card.openFile(strchr_pointer + 4,true);
  3850. break;
  3851. case 24: //M24 - Start SD print
  3852. if (!card.paused)
  3853. failstats_reset_print();
  3854. card.startFileprint();
  3855. starttime=millis();
  3856. break;
  3857. case 25: //M25 - Pause SD print
  3858. card.pauseSDPrint();
  3859. break;
  3860. case 26: //M26 - Set SD index
  3861. if(card.cardOK && code_seen('S')) {
  3862. card.setIndex(code_value_long());
  3863. }
  3864. break;
  3865. case 27: //M27 - Get SD status
  3866. card.getStatus();
  3867. break;
  3868. case 28: //M28 - Start SD write
  3869. starpos = (strchr(strchr_pointer + 4,'*'));
  3870. if(starpos != NULL){
  3871. char* npos = strchr(CMDBUFFER_CURRENT_STRING, 'N');
  3872. strchr_pointer = strchr(npos,' ') + 1;
  3873. *(starpos) = '\0';
  3874. }
  3875. card.openFile(strchr_pointer+4,false);
  3876. break;
  3877. case 29: //M29 - Stop SD write
  3878. //processed in write to file routine above
  3879. //card,saving = false;
  3880. break;
  3881. case 30: //M30 <filename> Delete File
  3882. if (card.cardOK){
  3883. card.closefile();
  3884. starpos = (strchr(strchr_pointer + 4,'*'));
  3885. if(starpos != NULL){
  3886. char* npos = strchr(CMDBUFFER_CURRENT_STRING, 'N');
  3887. strchr_pointer = strchr(npos,' ') + 1;
  3888. *(starpos) = '\0';
  3889. }
  3890. card.removeFile(strchr_pointer + 4);
  3891. }
  3892. break;
  3893. case 32: //M32 - Select file and start SD print
  3894. {
  3895. if(card.sdprinting) {
  3896. st_synchronize();
  3897. }
  3898. starpos = (strchr(strchr_pointer + 4,'*'));
  3899. char* namestartpos = (strchr(strchr_pointer + 4,'!')); //find ! to indicate filename string start.
  3900. if(namestartpos==NULL)
  3901. {
  3902. namestartpos=strchr_pointer + 4; //default name position, 4 letters after the M
  3903. }
  3904. else
  3905. namestartpos++; //to skip the '!'
  3906. if(starpos!=NULL)
  3907. *(starpos)='\0';
  3908. bool call_procedure=(code_seen('P'));
  3909. if(strchr_pointer>namestartpos)
  3910. call_procedure=false; //false alert, 'P' found within filename
  3911. if( card.cardOK )
  3912. {
  3913. card.openFile(namestartpos,true,!call_procedure);
  3914. if(code_seen('S'))
  3915. if(strchr_pointer<namestartpos) //only if "S" is occuring _before_ the filename
  3916. card.setIndex(code_value_long());
  3917. card.startFileprint();
  3918. if(!call_procedure)
  3919. starttime=millis(); //procedure calls count as normal print time.
  3920. }
  3921. } break;
  3922. case 928: //M928 - Start SD write
  3923. starpos = (strchr(strchr_pointer + 5,'*'));
  3924. if(starpos != NULL){
  3925. char* npos = strchr(CMDBUFFER_CURRENT_STRING, 'N');
  3926. strchr_pointer = strchr(npos,' ') + 1;
  3927. *(starpos) = '\0';
  3928. }
  3929. card.openLogFile(strchr_pointer+5);
  3930. break;
  3931. #endif //SDSUPPORT
  3932. case 31: //M31 take time since the start of the SD print or an M109 command
  3933. {
  3934. stoptime=millis();
  3935. char time[30];
  3936. unsigned long t=(stoptime-starttime)/1000;
  3937. int sec,min;
  3938. min=t/60;
  3939. sec=t%60;
  3940. sprintf_P(time, PSTR("%i min, %i sec"), min, sec);
  3941. SERIAL_ECHO_START;
  3942. SERIAL_ECHOLN(time);
  3943. lcd_setstatus(time);
  3944. autotempShutdown();
  3945. }
  3946. break;
  3947. #ifndef _DISABLE_M42_M226
  3948. case 42: //M42 -Change pin status via gcode
  3949. if (code_seen('S'))
  3950. {
  3951. int pin_status = code_value();
  3952. int pin_number = LED_PIN;
  3953. if (code_seen('P') && pin_status >= 0 && pin_status <= 255)
  3954. pin_number = code_value();
  3955. for(int8_t i = 0; i < (int8_t)(sizeof(sensitive_pins)/sizeof(int)); i++)
  3956. {
  3957. if (sensitive_pins[i] == pin_number)
  3958. {
  3959. pin_number = -1;
  3960. break;
  3961. }
  3962. }
  3963. #if defined(FAN_PIN) && FAN_PIN > -1
  3964. if (pin_number == FAN_PIN)
  3965. fanSpeed = pin_status;
  3966. #endif
  3967. if (pin_number > -1)
  3968. {
  3969. pinMode(pin_number, OUTPUT);
  3970. digitalWrite(pin_number, pin_status);
  3971. analogWrite(pin_number, pin_status);
  3972. }
  3973. }
  3974. break;
  3975. #endif //_DISABLE_M42_M226
  3976. case 44: // M44: Prusa3D: Reset the bed skew and offset calibration.
  3977. // Reset the baby step value and the baby step applied flag.
  3978. calibration_status_store(CALIBRATION_STATUS_ASSEMBLED);
  3979. eeprom_update_word((uint16_t*)EEPROM_BABYSTEP_Z, 0);
  3980. // Reset the skew and offset in both RAM and EEPROM.
  3981. reset_bed_offset_and_skew();
  3982. // Reset world2machine_rotation_and_skew and world2machine_shift, therefore
  3983. // the planner will not perform any adjustments in the XY plane.
  3984. // Wait for the motors to stop and update the current position with the absolute values.
  3985. world2machine_revert_to_uncorrected();
  3986. break;
  3987. case 45: // M45: Prusa3D: bed skew and offset with manual Z up
  3988. {
  3989. int8_t verbosity_level = 0;
  3990. bool only_Z = code_seen('Z');
  3991. #ifdef SUPPORT_VERBOSITY
  3992. if (code_seen('V'))
  3993. {
  3994. // Just 'V' without a number counts as V1.
  3995. char c = strchr_pointer[1];
  3996. verbosity_level = (c == ' ' || c == '\t' || c == 0) ? 1 : code_value_short();
  3997. }
  3998. #endif //SUPPORT_VERBOSITY
  3999. gcode_M45(only_Z, verbosity_level);
  4000. }
  4001. break;
  4002. /*
  4003. case 46:
  4004. {
  4005. // M46: Prusa3D: Show the assigned IP address.
  4006. uint8_t ip[4];
  4007. bool hasIP = card.ToshibaFlashAir_GetIP(ip);
  4008. if (hasIP) {
  4009. SERIAL_ECHOPGM("Toshiba FlashAir current IP: ");
  4010. SERIAL_ECHO(int(ip[0]));
  4011. SERIAL_ECHOPGM(".");
  4012. SERIAL_ECHO(int(ip[1]));
  4013. SERIAL_ECHOPGM(".");
  4014. SERIAL_ECHO(int(ip[2]));
  4015. SERIAL_ECHOPGM(".");
  4016. SERIAL_ECHO(int(ip[3]));
  4017. SERIAL_ECHOLNPGM("");
  4018. } else {
  4019. SERIAL_ECHOLNPGM("Toshiba FlashAir GetIP failed");
  4020. }
  4021. break;
  4022. }
  4023. */
  4024. case 47:
  4025. // M47: Prusa3D: Show end stops dialog on the display.
  4026. KEEPALIVE_STATE(PAUSED_FOR_USER);
  4027. lcd_diag_show_end_stops();
  4028. KEEPALIVE_STATE(IN_HANDLER);
  4029. break;
  4030. #if 0
  4031. case 48: // M48: scan the bed induction sensor points, print the sensor trigger coordinates to the serial line for visualization on the PC.
  4032. {
  4033. // Disable the default update procedure of the display. We will do a modal dialog.
  4034. lcd_update_enable(false);
  4035. // Let the planner use the uncorrected coordinates.
  4036. mbl.reset();
  4037. // Reset world2machine_rotation_and_skew and world2machine_shift, therefore
  4038. // the planner will not perform any adjustments in the XY plane.
  4039. // Wait for the motors to stop and update the current position with the absolute values.
  4040. world2machine_revert_to_uncorrected();
  4041. // Move the print head close to the bed.
  4042. current_position[Z_AXIS] = MESH_HOME_Z_SEARCH;
  4043. 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);
  4044. st_synchronize();
  4045. // Home in the XY plane.
  4046. set_destination_to_current();
  4047. setup_for_endstop_move();
  4048. home_xy();
  4049. int8_t verbosity_level = 0;
  4050. if (code_seen('V')) {
  4051. // Just 'V' without a number counts as V1.
  4052. char c = strchr_pointer[1];
  4053. verbosity_level = (c == ' ' || c == '\t' || c == 0) ? 1 : code_value_short();
  4054. }
  4055. bool success = scan_bed_induction_points(verbosity_level);
  4056. clean_up_after_endstop_move();
  4057. // Print head up.
  4058. current_position[Z_AXIS] = MESH_HOME_Z_SEARCH;
  4059. 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);
  4060. st_synchronize();
  4061. lcd_update_enable(true);
  4062. break;
  4063. }
  4064. #endif
  4065. // M48 Z-Probe repeatability measurement function.
  4066. //
  4067. // 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>
  4068. //
  4069. // This function assumes the bed has been homed. Specificaly, that a G28 command
  4070. // as been issued prior to invoking the M48 Z-Probe repeatability measurement function.
  4071. // Any information generated by a prior G29 Bed leveling command will be lost and need to be
  4072. // regenerated.
  4073. //
  4074. // The number of samples will default to 10 if not specified. You can use upper or lower case
  4075. // letters for any of the options EXCEPT n. n must be in lower case because Marlin uses a capital
  4076. // N for its communication protocol and will get horribly confused if you send it a capital N.
  4077. //
  4078. #ifdef ENABLE_AUTO_BED_LEVELING
  4079. #ifdef Z_PROBE_REPEATABILITY_TEST
  4080. case 48: // M48 Z-Probe repeatability
  4081. {
  4082. #if Z_MIN_PIN == -1
  4083. #error "You must have a Z_MIN endstop in order to enable calculation of Z-Probe repeatability."
  4084. #endif
  4085. double sum=0.0;
  4086. double mean=0.0;
  4087. double sigma=0.0;
  4088. double sample_set[50];
  4089. int verbose_level=1, n=0, j, n_samples = 10, n_legs=0;
  4090. double X_current, Y_current, Z_current;
  4091. double X_probe_location, Y_probe_location, Z_start_location, ext_position;
  4092. if (code_seen('V') || code_seen('v')) {
  4093. verbose_level = code_value();
  4094. if (verbose_level<0 || verbose_level>4 ) {
  4095. SERIAL_PROTOCOLPGM("?Verbose Level not plausable.\n");
  4096. goto Sigma_Exit;
  4097. }
  4098. }
  4099. if (verbose_level > 0) {
  4100. SERIAL_PROTOCOLPGM("M48 Z-Probe Repeatability test. Version 2.00\n");
  4101. SERIAL_PROTOCOLPGM("Full support at: http://3dprintboard.com/forum.php\n");
  4102. }
  4103. if (code_seen('n')) {
  4104. n_samples = code_value();
  4105. if (n_samples<4 || n_samples>50 ) {
  4106. SERIAL_PROTOCOLPGM("?Specified sample size not plausable.\n");
  4107. goto Sigma_Exit;
  4108. }
  4109. }
  4110. X_current = X_probe_location = st_get_position_mm(X_AXIS);
  4111. Y_current = Y_probe_location = st_get_position_mm(Y_AXIS);
  4112. Z_current = st_get_position_mm(Z_AXIS);
  4113. Z_start_location = st_get_position_mm(Z_AXIS) + Z_RAISE_BEFORE_PROBING;
  4114. ext_position = st_get_position_mm(E_AXIS);
  4115. if (code_seen('X') || code_seen('x') ) {
  4116. X_probe_location = code_value() - X_PROBE_OFFSET_FROM_EXTRUDER;
  4117. if (X_probe_location<X_MIN_POS || X_probe_location>X_MAX_POS ) {
  4118. SERIAL_PROTOCOLPGM("?Specified X position out of range.\n");
  4119. goto Sigma_Exit;
  4120. }
  4121. }
  4122. if (code_seen('Y') || code_seen('y') ) {
  4123. Y_probe_location = code_value() - Y_PROBE_OFFSET_FROM_EXTRUDER;
  4124. if (Y_probe_location<Y_MIN_POS || Y_probe_location>Y_MAX_POS ) {
  4125. SERIAL_PROTOCOLPGM("?Specified Y position out of range.\n");
  4126. goto Sigma_Exit;
  4127. }
  4128. }
  4129. if (code_seen('L') || code_seen('l') ) {
  4130. n_legs = code_value();
  4131. if ( n_legs==1 )
  4132. n_legs = 2;
  4133. if ( n_legs<0 || n_legs>15 ) {
  4134. SERIAL_PROTOCOLPGM("?Specified number of legs in movement not plausable.\n");
  4135. goto Sigma_Exit;
  4136. }
  4137. }
  4138. //
  4139. // Do all the preliminary setup work. First raise the probe.
  4140. //
  4141. st_synchronize();
  4142. plan_bed_level_matrix.set_to_identity();
  4143. plan_buffer_line( X_current, Y_current, Z_start_location,
  4144. ext_position,
  4145. homing_feedrate[Z_AXIS]/60,
  4146. active_extruder);
  4147. st_synchronize();
  4148. //
  4149. // Now get everything to the specified probe point So we can safely do a probe to
  4150. // get us close to the bed. If the Z-Axis is far from the bed, we don't want to
  4151. // use that as a starting point for each probe.
  4152. //
  4153. if (verbose_level > 2)
  4154. SERIAL_PROTOCOL("Positioning probe for the test.\n");
  4155. plan_buffer_line( X_probe_location, Y_probe_location, Z_start_location,
  4156. ext_position,
  4157. homing_feedrate[X_AXIS]/60,
  4158. active_extruder);
  4159. st_synchronize();
  4160. current_position[X_AXIS] = X_current = st_get_position_mm(X_AXIS);
  4161. current_position[Y_AXIS] = Y_current = st_get_position_mm(Y_AXIS);
  4162. current_position[Z_AXIS] = Z_current = st_get_position_mm(Z_AXIS);
  4163. current_position[E_AXIS] = ext_position = st_get_position_mm(E_AXIS);
  4164. //
  4165. // OK, do the inital probe to get us close to the bed.
  4166. // Then retrace the right amount and use that in subsequent probes
  4167. //
  4168. setup_for_endstop_move();
  4169. run_z_probe();
  4170. current_position[Z_AXIS] = Z_current = st_get_position_mm(Z_AXIS);
  4171. Z_start_location = st_get_position_mm(Z_AXIS) + Z_RAISE_BEFORE_PROBING;
  4172. plan_buffer_line( X_probe_location, Y_probe_location, Z_start_location,
  4173. ext_position,
  4174. homing_feedrate[X_AXIS]/60,
  4175. active_extruder);
  4176. st_synchronize();
  4177. current_position[Z_AXIS] = Z_current = st_get_position_mm(Z_AXIS);
  4178. for( n=0; n<n_samples; n++) {
  4179. do_blocking_move_to( X_probe_location, Y_probe_location, Z_start_location); // Make sure we are at the probe location
  4180. if ( n_legs) {
  4181. double radius=0.0, theta=0.0, x_sweep, y_sweep;
  4182. int rotational_direction, l;
  4183. rotational_direction = (unsigned long) millis() & 0x0001; // clockwise or counter clockwise
  4184. radius = (unsigned long) millis() % (long) (X_MAX_LENGTH/4); // limit how far out to go
  4185. theta = (float) ((unsigned long) millis() % (long) 360) / (360./(2*3.1415926)); // turn into radians
  4186. //SERIAL_ECHOPAIR("starting radius: ",radius);
  4187. //SERIAL_ECHOPAIR(" theta: ",theta);
  4188. //SERIAL_ECHOPAIR(" direction: ",rotational_direction);
  4189. //SERIAL_PROTOCOLLNPGM("");
  4190. for( l=0; l<n_legs-1; l++) {
  4191. if (rotational_direction==1)
  4192. theta += (float) ((unsigned long) millis() % (long) 20) / (360.0/(2*3.1415926)); // turn into radians
  4193. else
  4194. theta -= (float) ((unsigned long) millis() % (long) 20) / (360.0/(2*3.1415926)); // turn into radians
  4195. radius += (float) ( ((long) ((unsigned long) millis() % (long) 10)) - 5);
  4196. if ( radius<0.0 )
  4197. radius = -radius;
  4198. X_current = X_probe_location + cos(theta) * radius;
  4199. Y_current = Y_probe_location + sin(theta) * radius;
  4200. if ( X_current<X_MIN_POS) // Make sure our X & Y are sane
  4201. X_current = X_MIN_POS;
  4202. if ( X_current>X_MAX_POS)
  4203. X_current = X_MAX_POS;
  4204. if ( Y_current<Y_MIN_POS) // Make sure our X & Y are sane
  4205. Y_current = Y_MIN_POS;
  4206. if ( Y_current>Y_MAX_POS)
  4207. Y_current = Y_MAX_POS;
  4208. if (verbose_level>3 ) {
  4209. SERIAL_ECHOPAIR("x: ", X_current);
  4210. SERIAL_ECHOPAIR("y: ", Y_current);
  4211. SERIAL_PROTOCOLLNPGM("");
  4212. }
  4213. do_blocking_move_to( X_current, Y_current, Z_current );
  4214. }
  4215. do_blocking_move_to( X_probe_location, Y_probe_location, Z_start_location); // Go back to the probe location
  4216. }
  4217. setup_for_endstop_move();
  4218. run_z_probe();
  4219. sample_set[n] = current_position[Z_AXIS];
  4220. //
  4221. // Get the current mean for the data points we have so far
  4222. //
  4223. sum=0.0;
  4224. for( j=0; j<=n; j++) {
  4225. sum = sum + sample_set[j];
  4226. }
  4227. mean = sum / (double (n+1));
  4228. //
  4229. // Now, use that mean to calculate the standard deviation for the
  4230. // data points we have so far
  4231. //
  4232. sum=0.0;
  4233. for( j=0; j<=n; j++) {
  4234. sum = sum + (sample_set[j]-mean) * (sample_set[j]-mean);
  4235. }
  4236. sigma = sqrt( sum / (double (n+1)) );
  4237. if (verbose_level > 1) {
  4238. SERIAL_PROTOCOL(n+1);
  4239. SERIAL_PROTOCOL(" of ");
  4240. SERIAL_PROTOCOL(n_samples);
  4241. SERIAL_PROTOCOLPGM(" z: ");
  4242. SERIAL_PROTOCOL_F(current_position[Z_AXIS], 6);
  4243. }
  4244. if (verbose_level > 2) {
  4245. SERIAL_PROTOCOL(" mean: ");
  4246. SERIAL_PROTOCOL_F(mean,6);
  4247. SERIAL_PROTOCOL(" sigma: ");
  4248. SERIAL_PROTOCOL_F(sigma,6);
  4249. }
  4250. if (verbose_level > 0)
  4251. SERIAL_PROTOCOLPGM("\n");
  4252. plan_buffer_line( X_probe_location, Y_probe_location, Z_start_location,
  4253. current_position[E_AXIS], homing_feedrate[Z_AXIS]/60, active_extruder);
  4254. st_synchronize();
  4255. }
  4256. delay(1000);
  4257. clean_up_after_endstop_move();
  4258. // enable_endstops(true);
  4259. if (verbose_level > 0) {
  4260. SERIAL_PROTOCOLPGM("Mean: ");
  4261. SERIAL_PROTOCOL_F(mean, 6);
  4262. SERIAL_PROTOCOLPGM("\n");
  4263. }
  4264. SERIAL_PROTOCOLPGM("Standard Deviation: ");
  4265. SERIAL_PROTOCOL_F(sigma, 6);
  4266. SERIAL_PROTOCOLPGM("\n\n");
  4267. Sigma_Exit:
  4268. break;
  4269. }
  4270. #endif // Z_PROBE_REPEATABILITY_TEST
  4271. #endif // ENABLE_AUTO_BED_LEVELING
  4272. case 73: //M73 show percent done and time remaining
  4273. if(code_seen('P')) print_percent_done_normal = code_value();
  4274. if(code_seen('R')) print_time_remaining_normal = code_value();
  4275. if(code_seen('Q')) print_percent_done_silent = code_value();
  4276. if(code_seen('S')) print_time_remaining_silent = code_value();
  4277. SERIAL_ECHOPGM("NORMAL MODE: Percent done: ");
  4278. MYSERIAL.print(int(print_percent_done_normal));
  4279. SERIAL_ECHOPGM("; print time remaining in mins: ");
  4280. MYSERIAL.println(print_time_remaining_normal);
  4281. SERIAL_ECHOPGM("SILENT MODE: Percent done: ");
  4282. MYSERIAL.print(int(print_percent_done_silent));
  4283. SERIAL_ECHOPGM("; print time remaining in mins: ");
  4284. MYSERIAL.println(print_time_remaining_silent);
  4285. break;
  4286. case 104: // M104
  4287. if(setTargetedHotend(104)){
  4288. break;
  4289. }
  4290. if (code_seen('S')) setTargetHotend(code_value(), tmp_extruder);
  4291. setWatch();
  4292. break;
  4293. case 112: // M112 -Emergency Stop
  4294. kill("", 3);
  4295. break;
  4296. case 140: // M140 set bed temp
  4297. if (code_seen('S')) setTargetBed(code_value());
  4298. break;
  4299. case 105 : // M105
  4300. if(setTargetedHotend(105)){
  4301. break;
  4302. }
  4303. #if defined(TEMP_0_PIN) && TEMP_0_PIN > -1
  4304. SERIAL_PROTOCOLPGM("ok T:");
  4305. SERIAL_PROTOCOL_F(degHotend(tmp_extruder),1);
  4306. SERIAL_PROTOCOLPGM(" /");
  4307. SERIAL_PROTOCOL_F(degTargetHotend(tmp_extruder),1);
  4308. #if defined(TEMP_BED_PIN) && TEMP_BED_PIN > -1
  4309. SERIAL_PROTOCOLPGM(" B:");
  4310. SERIAL_PROTOCOL_F(degBed(),1);
  4311. SERIAL_PROTOCOLPGM(" /");
  4312. SERIAL_PROTOCOL_F(degTargetBed(),1);
  4313. #endif //TEMP_BED_PIN
  4314. for (int8_t cur_extruder = 0; cur_extruder < EXTRUDERS; ++cur_extruder) {
  4315. SERIAL_PROTOCOLPGM(" T");
  4316. SERIAL_PROTOCOL(cur_extruder);
  4317. SERIAL_PROTOCOLPGM(":");
  4318. SERIAL_PROTOCOL_F(degHotend(cur_extruder),1);
  4319. SERIAL_PROTOCOLPGM(" /");
  4320. SERIAL_PROTOCOL_F(degTargetHotend(cur_extruder),1);
  4321. }
  4322. #else
  4323. SERIAL_ERROR_START;
  4324. SERIAL_ERRORLNRPGM(_i("No thermistors - no temperature"));////MSG_ERR_NO_THERMISTORS c=0 r=0
  4325. #endif
  4326. SERIAL_PROTOCOLPGM(" @:");
  4327. #ifdef EXTRUDER_WATTS
  4328. SERIAL_PROTOCOL((EXTRUDER_WATTS * getHeaterPower(tmp_extruder))/127);
  4329. SERIAL_PROTOCOLPGM("W");
  4330. #else
  4331. SERIAL_PROTOCOL(getHeaterPower(tmp_extruder));
  4332. #endif
  4333. SERIAL_PROTOCOLPGM(" B@:");
  4334. #ifdef BED_WATTS
  4335. SERIAL_PROTOCOL((BED_WATTS * getHeaterPower(-1))/127);
  4336. SERIAL_PROTOCOLPGM("W");
  4337. #else
  4338. SERIAL_PROTOCOL(getHeaterPower(-1));
  4339. #endif
  4340. #ifdef PINDA_THERMISTOR
  4341. SERIAL_PROTOCOLPGM(" P:");
  4342. SERIAL_PROTOCOL_F(current_temperature_pinda,1);
  4343. #endif //PINDA_THERMISTOR
  4344. #ifdef AMBIENT_THERMISTOR
  4345. SERIAL_PROTOCOLPGM(" A:");
  4346. SERIAL_PROTOCOL_F(current_temperature_ambient,1);
  4347. #endif //AMBIENT_THERMISTOR
  4348. #ifdef SHOW_TEMP_ADC_VALUES
  4349. {float raw = 0.0;
  4350. #if defined(TEMP_BED_PIN) && TEMP_BED_PIN > -1
  4351. SERIAL_PROTOCOLPGM(" ADC B:");
  4352. SERIAL_PROTOCOL_F(degBed(),1);
  4353. SERIAL_PROTOCOLPGM("C->");
  4354. raw = rawBedTemp();
  4355. SERIAL_PROTOCOL_F(raw/OVERSAMPLENR,5);
  4356. SERIAL_PROTOCOLPGM(" Rb->");
  4357. SERIAL_PROTOCOL_F(100 * (1 + (PtA * (raw/OVERSAMPLENR)) + (PtB * sq((raw/OVERSAMPLENR)))), 5);
  4358. SERIAL_PROTOCOLPGM(" Rxb->");
  4359. SERIAL_PROTOCOL_F(raw, 5);
  4360. #endif
  4361. for (int8_t cur_extruder = 0; cur_extruder < EXTRUDERS; ++cur_extruder) {
  4362. SERIAL_PROTOCOLPGM(" T");
  4363. SERIAL_PROTOCOL(cur_extruder);
  4364. SERIAL_PROTOCOLPGM(":");
  4365. SERIAL_PROTOCOL_F(degHotend(cur_extruder),1);
  4366. SERIAL_PROTOCOLPGM("C->");
  4367. raw = rawHotendTemp(cur_extruder);
  4368. SERIAL_PROTOCOL_F(raw/OVERSAMPLENR,5);
  4369. SERIAL_PROTOCOLPGM(" Rt");
  4370. SERIAL_PROTOCOL(cur_extruder);
  4371. SERIAL_PROTOCOLPGM("->");
  4372. SERIAL_PROTOCOL_F(100 * (1 + (PtA * (raw/OVERSAMPLENR)) + (PtB * sq((raw/OVERSAMPLENR)))), 5);
  4373. SERIAL_PROTOCOLPGM(" Rx");
  4374. SERIAL_PROTOCOL(cur_extruder);
  4375. SERIAL_PROTOCOLPGM("->");
  4376. SERIAL_PROTOCOL_F(raw, 5);
  4377. }}
  4378. #endif
  4379. SERIAL_PROTOCOLLN("");
  4380. KEEPALIVE_STATE(NOT_BUSY);
  4381. return;
  4382. break;
  4383. case 109:
  4384. {// M109 - Wait for extruder heater to reach target.
  4385. if(setTargetedHotend(109)){
  4386. break;
  4387. }
  4388. LCD_MESSAGERPGM(_T(MSG_HEATING));
  4389. heating_status = 1;
  4390. if (farm_mode) { prusa_statistics(1); };
  4391. #ifdef AUTOTEMP
  4392. autotemp_enabled=false;
  4393. #endif
  4394. if (code_seen('S')) {
  4395. setTargetHotend(code_value(), tmp_extruder);
  4396. CooldownNoWait = true;
  4397. } else if (code_seen('R')) {
  4398. setTargetHotend(code_value(), tmp_extruder);
  4399. CooldownNoWait = false;
  4400. }
  4401. #ifdef AUTOTEMP
  4402. if (code_seen('S')) autotemp_min=code_value();
  4403. if (code_seen('B')) autotemp_max=code_value();
  4404. if (code_seen('F'))
  4405. {
  4406. autotemp_factor=code_value();
  4407. autotemp_enabled=true;
  4408. }
  4409. #endif
  4410. setWatch();
  4411. codenum = millis();
  4412. /* See if we are heating up or cooling down */
  4413. target_direction = isHeatingHotend(tmp_extruder); // true if heating, false if cooling
  4414. KEEPALIVE_STATE(NOT_BUSY);
  4415. cancel_heatup = false;
  4416. wait_for_heater(codenum); //loops until target temperature is reached
  4417. LCD_MESSAGERPGM(_T(MSG_HEATING_COMPLETE));
  4418. KEEPALIVE_STATE(IN_HANDLER);
  4419. heating_status = 2;
  4420. if (farm_mode) { prusa_statistics(2); };
  4421. //starttime=millis();
  4422. previous_millis_cmd = millis();
  4423. }
  4424. break;
  4425. case 190: // M190 - Wait for bed heater to reach target.
  4426. #if defined(TEMP_BED_PIN) && TEMP_BED_PIN > -1
  4427. LCD_MESSAGERPGM(_T(MSG_BED_HEATING));
  4428. heating_status = 3;
  4429. if (farm_mode) { prusa_statistics(1); };
  4430. if (code_seen('S'))
  4431. {
  4432. setTargetBed(code_value());
  4433. CooldownNoWait = true;
  4434. }
  4435. else if (code_seen('R'))
  4436. {
  4437. setTargetBed(code_value());
  4438. CooldownNoWait = false;
  4439. }
  4440. codenum = millis();
  4441. cancel_heatup = false;
  4442. target_direction = isHeatingBed(); // true if heating, false if cooling
  4443. KEEPALIVE_STATE(NOT_BUSY);
  4444. while ( (target_direction)&&(!cancel_heatup) ? (isHeatingBed()) : (isCoolingBed()&&(CooldownNoWait==false)) )
  4445. {
  4446. if(( millis() - codenum) > 1000 ) //Print Temp Reading every 1 second while heating up.
  4447. {
  4448. if (!farm_mode) {
  4449. float tt = degHotend(active_extruder);
  4450. SERIAL_PROTOCOLPGM("T:");
  4451. SERIAL_PROTOCOL(tt);
  4452. SERIAL_PROTOCOLPGM(" E:");
  4453. SERIAL_PROTOCOL((int)active_extruder);
  4454. SERIAL_PROTOCOLPGM(" B:");
  4455. SERIAL_PROTOCOL_F(degBed(), 1);
  4456. SERIAL_PROTOCOLLN("");
  4457. }
  4458. codenum = millis();
  4459. }
  4460. manage_heater();
  4461. manage_inactivity();
  4462. lcd_update();
  4463. }
  4464. LCD_MESSAGERPGM(_T(MSG_BED_DONE));
  4465. KEEPALIVE_STATE(IN_HANDLER);
  4466. heating_status = 4;
  4467. previous_millis_cmd = millis();
  4468. #endif
  4469. break;
  4470. #if defined(FAN_PIN) && FAN_PIN > -1
  4471. case 106: //M106 Fan On
  4472. if (code_seen('S')){
  4473. fanSpeed=constrain(code_value(),0,255);
  4474. }
  4475. else {
  4476. fanSpeed=255;
  4477. }
  4478. break;
  4479. case 107: //M107 Fan Off
  4480. fanSpeed = 0;
  4481. break;
  4482. #endif //FAN_PIN
  4483. #if defined(PS_ON_PIN) && PS_ON_PIN > -1
  4484. case 80: // M80 - Turn on Power Supply
  4485. SET_OUTPUT(PS_ON_PIN); //GND
  4486. WRITE(PS_ON_PIN, PS_ON_AWAKE);
  4487. // If you have a switch on suicide pin, this is useful
  4488. // if you want to start another print with suicide feature after
  4489. // a print without suicide...
  4490. #if defined SUICIDE_PIN && SUICIDE_PIN > -1
  4491. SET_OUTPUT(SUICIDE_PIN);
  4492. WRITE(SUICIDE_PIN, HIGH);
  4493. #endif
  4494. #ifdef ULTIPANEL
  4495. powersupply = true;
  4496. LCD_MESSAGERPGM(_T(WELCOME_MSG));
  4497. lcd_update();
  4498. #endif
  4499. break;
  4500. #endif
  4501. case 81: // M81 - Turn off Power Supply
  4502. disable_heater();
  4503. st_synchronize();
  4504. disable_e0();
  4505. disable_e1();
  4506. disable_e2();
  4507. finishAndDisableSteppers();
  4508. fanSpeed = 0;
  4509. delay(1000); // Wait a little before to switch off
  4510. #if defined(SUICIDE_PIN) && SUICIDE_PIN > -1
  4511. st_synchronize();
  4512. suicide();
  4513. #elif defined(PS_ON_PIN) && PS_ON_PIN > -1
  4514. SET_OUTPUT(PS_ON_PIN);
  4515. WRITE(PS_ON_PIN, PS_ON_ASLEEP);
  4516. #endif
  4517. #ifdef ULTIPANEL
  4518. powersupply = false;
  4519. LCD_MESSAGERPGM(CAT4(CUSTOM_MENDEL_NAME,PSTR(" "),MSG_OFF,PSTR("."))); //!!
  4520. /*
  4521. MACHNAME = "Prusa i3"
  4522. MSGOFF = "Vypnuto"
  4523. "Prusai3"" ""vypnuto""."
  4524. "Prusa i3"" "_T(MSG_ALL)[lang_selected][50]"."
  4525. */
  4526. lcd_update();
  4527. #endif
  4528. break;
  4529. case 82:
  4530. axis_relative_modes[3] = false;
  4531. break;
  4532. case 83:
  4533. axis_relative_modes[3] = true;
  4534. break;
  4535. case 18: //compatibility
  4536. case 84: // M84
  4537. if(code_seen('S')){
  4538. stepper_inactive_time = code_value() * 1000;
  4539. }
  4540. else
  4541. {
  4542. 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])));
  4543. if(all_axis)
  4544. {
  4545. st_synchronize();
  4546. disable_e0();
  4547. disable_e1();
  4548. disable_e2();
  4549. finishAndDisableSteppers();
  4550. }
  4551. else
  4552. {
  4553. st_synchronize();
  4554. if (code_seen('X')) disable_x();
  4555. if (code_seen('Y')) disable_y();
  4556. if (code_seen('Z')) disable_z();
  4557. #if ((E0_ENABLE_PIN != X_ENABLE_PIN) && (E1_ENABLE_PIN != Y_ENABLE_PIN)) // Only enable on boards that have seperate ENABLE_PINS
  4558. if (code_seen('E')) {
  4559. disable_e0();
  4560. disable_e1();
  4561. disable_e2();
  4562. }
  4563. #endif
  4564. }
  4565. }
  4566. //in the end of print set estimated time to end of print and extruders used during print to default values for next print
  4567. print_time_remaining_init();
  4568. snmm_filaments_used = 0;
  4569. break;
  4570. case 85: // M85
  4571. if(code_seen('S')) {
  4572. max_inactive_time = code_value() * 1000;
  4573. }
  4574. break;
  4575. case 92: // M92
  4576. for(int8_t i=0; i < NUM_AXIS; i++)
  4577. {
  4578. if(code_seen(axis_codes[i]))
  4579. {
  4580. if(i == 3) { // E
  4581. float value = code_value();
  4582. if(value < 20.0) {
  4583. float factor = axis_steps_per_unit[i] / value; // increase e constants if M92 E14 is given for netfab.
  4584. max_jerk[E_AXIS] *= factor;
  4585. max_feedrate[i] *= factor;
  4586. axis_steps_per_sqr_second[i] *= factor;
  4587. }
  4588. axis_steps_per_unit[i] = value;
  4589. }
  4590. else {
  4591. axis_steps_per_unit[i] = code_value();
  4592. }
  4593. }
  4594. }
  4595. break;
  4596. case 110: // M110 - reset line pos
  4597. if (code_seen('N'))
  4598. gcode_LastN = code_value_long();
  4599. break;
  4600. #ifdef HOST_KEEPALIVE_FEATURE
  4601. case 113: // M113 - Get or set Host Keepalive interval
  4602. if (code_seen('S')) {
  4603. host_keepalive_interval = (uint8_t)code_value_short();
  4604. // NOMORE(host_keepalive_interval, 60);
  4605. }
  4606. else {
  4607. SERIAL_ECHO_START;
  4608. SERIAL_ECHOPAIR("M113 S", (unsigned long)host_keepalive_interval);
  4609. SERIAL_PROTOCOLLN("");
  4610. }
  4611. break;
  4612. #endif
  4613. case 115: // M115
  4614. if (code_seen('V')) {
  4615. // Report the Prusa version number.
  4616. SERIAL_PROTOCOLLNRPGM(FW_VERSION_STR_P());
  4617. } else if (code_seen('U')) {
  4618. // Check the firmware version provided. If the firmware version provided by the U code is higher than the currently running firmware,
  4619. // pause the print and ask the user to upgrade the firmware.
  4620. show_upgrade_dialog_if_version_newer(++ strchr_pointer);
  4621. } else {
  4622. SERIAL_ECHOPGM("FIRMWARE_NAME:Prusa-Firmware ");
  4623. SERIAL_ECHORPGM(FW_VERSION_STR_P());
  4624. SERIAL_ECHOPGM(" based on Marlin FIRMWARE_URL:https://github.com/prusa3d/Prusa-Firmware PROTOCOL_VERSION:");
  4625. SERIAL_ECHOPGM(PROTOCOL_VERSION);
  4626. SERIAL_ECHOPGM(" MACHINE_TYPE:");
  4627. SERIAL_ECHOPGM(CUSTOM_MENDEL_NAME);
  4628. SERIAL_ECHOPGM(" EXTRUDER_COUNT:");
  4629. SERIAL_ECHOPGM(STRINGIFY(EXTRUDERS));
  4630. SERIAL_ECHOPGM(" UUID:");
  4631. SERIAL_ECHOLNPGM(MACHINE_UUID);
  4632. }
  4633. break;
  4634. /* case 117: // M117 display message
  4635. starpos = (strchr(strchr_pointer + 5,'*'));
  4636. if(starpos!=NULL)
  4637. *(starpos)='\0';
  4638. lcd_setstatus(strchr_pointer + 5);
  4639. break;*/
  4640. case 114: // M114
  4641. gcode_M114();
  4642. break;
  4643. case 120: // M120
  4644. enable_endstops(false) ;
  4645. break;
  4646. case 121: // M121
  4647. enable_endstops(true) ;
  4648. break;
  4649. case 119: // M119
  4650. SERIAL_PROTOCOLRPGM(_i("Reporting endstop status"));////MSG_M119_REPORT c=0 r=0
  4651. SERIAL_PROTOCOLLN("");
  4652. #if defined(X_MIN_PIN) && X_MIN_PIN > -1
  4653. SERIAL_PROTOCOLRPGM(_n("x_min: "));////MSG_X_MIN c=0 r=0
  4654. if(READ(X_MIN_PIN)^X_MIN_ENDSTOP_INVERTING){
  4655. SERIAL_PROTOCOLRPGM(_T(MSG_ENDSTOP_HIT));
  4656. }else{
  4657. SERIAL_PROTOCOLRPGM(_T(MSG_ENDSTOP_OPEN));
  4658. }
  4659. SERIAL_PROTOCOLLN("");
  4660. #endif
  4661. #if defined(X_MAX_PIN) && X_MAX_PIN > -1
  4662. SERIAL_PROTOCOLRPGM(_n("x_max: "));////MSG_X_MAX c=0 r=0
  4663. if(READ(X_MAX_PIN)^X_MAX_ENDSTOP_INVERTING){
  4664. SERIAL_PROTOCOLRPGM(_T(MSG_ENDSTOP_HIT));
  4665. }else{
  4666. SERIAL_PROTOCOLRPGM(_T(MSG_ENDSTOP_OPEN));
  4667. }
  4668. SERIAL_PROTOCOLLN("");
  4669. #endif
  4670. #if defined(Y_MIN_PIN) && Y_MIN_PIN > -1
  4671. SERIAL_PROTOCOLRPGM(_n("y_min: "));////MSG_Y_MIN c=0 r=0
  4672. if(READ(Y_MIN_PIN)^Y_MIN_ENDSTOP_INVERTING){
  4673. SERIAL_PROTOCOLRPGM(_T(MSG_ENDSTOP_HIT));
  4674. }else{
  4675. SERIAL_PROTOCOLRPGM(_T(MSG_ENDSTOP_OPEN));
  4676. }
  4677. SERIAL_PROTOCOLLN("");
  4678. #endif
  4679. #if defined(Y_MAX_PIN) && Y_MAX_PIN > -1
  4680. SERIAL_PROTOCOLRPGM(_n("y_max: "));////MSG_Y_MAX c=0 r=0
  4681. if(READ(Y_MAX_PIN)^Y_MAX_ENDSTOP_INVERTING){
  4682. SERIAL_PROTOCOLRPGM(_T(MSG_ENDSTOP_HIT));
  4683. }else{
  4684. SERIAL_PROTOCOLRPGM(_T(MSG_ENDSTOP_OPEN));
  4685. }
  4686. SERIAL_PROTOCOLLN("");
  4687. #endif
  4688. #if defined(Z_MIN_PIN) && Z_MIN_PIN > -1
  4689. SERIAL_PROTOCOLRPGM(MSG_Z_MIN);
  4690. if(READ(Z_MIN_PIN)^Z_MIN_ENDSTOP_INVERTING){
  4691. SERIAL_PROTOCOLRPGM(_T(MSG_ENDSTOP_HIT));
  4692. }else{
  4693. SERIAL_PROTOCOLRPGM(_T(MSG_ENDSTOP_OPEN));
  4694. }
  4695. SERIAL_PROTOCOLLN("");
  4696. #endif
  4697. #if defined(Z_MAX_PIN) && Z_MAX_PIN > -1
  4698. SERIAL_PROTOCOLRPGM(MSG_Z_MAX);
  4699. if(READ(Z_MAX_PIN)^Z_MAX_ENDSTOP_INVERTING){
  4700. SERIAL_PROTOCOLRPGM(_T(MSG_ENDSTOP_HIT));
  4701. }else{
  4702. SERIAL_PROTOCOLRPGM(_T(MSG_ENDSTOP_OPEN));
  4703. }
  4704. SERIAL_PROTOCOLLN("");
  4705. #endif
  4706. break;
  4707. //TODO: update for all axis, use for loop
  4708. #ifdef BLINKM
  4709. case 150: // M150
  4710. {
  4711. byte red;
  4712. byte grn;
  4713. byte blu;
  4714. if(code_seen('R')) red = code_value();
  4715. if(code_seen('U')) grn = code_value();
  4716. if(code_seen('B')) blu = code_value();
  4717. SendColors(red,grn,blu);
  4718. }
  4719. break;
  4720. #endif //BLINKM
  4721. case 200: // M200 D<millimeters> set filament diameter and set E axis units to cubic millimeters (use S0 to set back to millimeters).
  4722. {
  4723. tmp_extruder = active_extruder;
  4724. if(code_seen('T')) {
  4725. tmp_extruder = code_value();
  4726. if(tmp_extruder >= EXTRUDERS) {
  4727. SERIAL_ECHO_START;
  4728. SERIAL_ECHO(_i("M200 Invalid extruder "));////MSG_M200_INVALID_EXTRUDER c=0 r=0
  4729. break;
  4730. }
  4731. }
  4732. float area = .0;
  4733. if(code_seen('D')) {
  4734. float diameter = (float)code_value();
  4735. if (diameter == 0.0) {
  4736. // setting any extruder filament size disables volumetric on the assumption that
  4737. // slicers either generate in extruder values as cubic mm or as as filament feeds
  4738. // for all extruders
  4739. volumetric_enabled = false;
  4740. } else {
  4741. filament_size[tmp_extruder] = (float)code_value();
  4742. // make sure all extruders have some sane value for the filament size
  4743. filament_size[0] = (filament_size[0] == 0.0 ? DEFAULT_NOMINAL_FILAMENT_DIA : filament_size[0]);
  4744. #if EXTRUDERS > 1
  4745. filament_size[1] = (filament_size[1] == 0.0 ? DEFAULT_NOMINAL_FILAMENT_DIA : filament_size[1]);
  4746. #if EXTRUDERS > 2
  4747. filament_size[2] = (filament_size[2] == 0.0 ? DEFAULT_NOMINAL_FILAMENT_DIA : filament_size[2]);
  4748. #endif
  4749. #endif
  4750. volumetric_enabled = true;
  4751. }
  4752. } else {
  4753. //reserved for setting filament diameter via UFID or filament measuring device
  4754. break;
  4755. }
  4756. calculate_extruder_multipliers();
  4757. }
  4758. break;
  4759. case 201: // M201
  4760. for(int8_t i=0; i < NUM_AXIS; i++)
  4761. {
  4762. if(code_seen(axis_codes[i]))
  4763. {
  4764. max_acceleration_units_per_sq_second[i] = code_value();
  4765. }
  4766. }
  4767. // 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)
  4768. reset_acceleration_rates();
  4769. break;
  4770. #if 0 // Not used for Sprinter/grbl gen6
  4771. case 202: // M202
  4772. for(int8_t i=0; i < NUM_AXIS; i++) {
  4773. if(code_seen(axis_codes[i])) axis_travel_steps_per_sqr_second[i] = code_value() * axis_steps_per_unit[i];
  4774. }
  4775. break;
  4776. #endif
  4777. case 203: // M203 max feedrate mm/sec
  4778. for(int8_t i=0; i < NUM_AXIS; i++) {
  4779. if(code_seen(axis_codes[i])) max_feedrate[i] = code_value();
  4780. }
  4781. break;
  4782. case 204: // M204 acclereration S normal moves T filmanent only moves
  4783. {
  4784. if(code_seen('S')) acceleration = code_value() ;
  4785. if(code_seen('T')) retract_acceleration = code_value() ;
  4786. }
  4787. break;
  4788. 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
  4789. {
  4790. if(code_seen('S')) minimumfeedrate = code_value();
  4791. if(code_seen('T')) mintravelfeedrate = code_value();
  4792. if(code_seen('B')) minsegmenttime = code_value() ;
  4793. if(code_seen('X')) max_jerk[X_AXIS] = max_jerk[Y_AXIS] = code_value();
  4794. if(code_seen('Y')) max_jerk[Y_AXIS] = code_value();
  4795. if(code_seen('Z')) max_jerk[Z_AXIS] = code_value();
  4796. if(code_seen('E')) max_jerk[E_AXIS] = code_value();
  4797. if (max_jerk[X_AXIS] > DEFAULT_XJERK) max_jerk[X_AXIS] = DEFAULT_XJERK;
  4798. if (max_jerk[Y_AXIS] > DEFAULT_YJERK) max_jerk[Y_AXIS] = DEFAULT_YJERK;
  4799. }
  4800. break;
  4801. case 206: // M206 additional homing offset
  4802. for(int8_t i=0; i < 3; i++)
  4803. {
  4804. if(code_seen(axis_codes[i])) add_homing[i] = code_value();
  4805. }
  4806. break;
  4807. #ifdef FWRETRACT
  4808. case 207: //M207 - set retract length S[positive mm] F[feedrate mm/min] Z[additional zlift/hop]
  4809. {
  4810. if(code_seen('S'))
  4811. {
  4812. retract_length = code_value() ;
  4813. }
  4814. if(code_seen('F'))
  4815. {
  4816. retract_feedrate = code_value()/60 ;
  4817. }
  4818. if(code_seen('Z'))
  4819. {
  4820. retract_zlift = code_value() ;
  4821. }
  4822. }break;
  4823. case 208: // M208 - set retract recover length S[positive mm surplus to the M207 S*] F[feedrate mm/min]
  4824. {
  4825. if(code_seen('S'))
  4826. {
  4827. retract_recover_length = code_value() ;
  4828. }
  4829. if(code_seen('F'))
  4830. {
  4831. retract_recover_feedrate = code_value()/60 ;
  4832. }
  4833. }break;
  4834. 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.
  4835. {
  4836. if(code_seen('S'))
  4837. {
  4838. int t= code_value() ;
  4839. switch(t)
  4840. {
  4841. case 0:
  4842. {
  4843. autoretract_enabled=false;
  4844. retracted[0]=false;
  4845. #if EXTRUDERS > 1
  4846. retracted[1]=false;
  4847. #endif
  4848. #if EXTRUDERS > 2
  4849. retracted[2]=false;
  4850. #endif
  4851. }break;
  4852. case 1:
  4853. {
  4854. autoretract_enabled=true;
  4855. retracted[0]=false;
  4856. #if EXTRUDERS > 1
  4857. retracted[1]=false;
  4858. #endif
  4859. #if EXTRUDERS > 2
  4860. retracted[2]=false;
  4861. #endif
  4862. }break;
  4863. default:
  4864. SERIAL_ECHO_START;
  4865. SERIAL_ECHORPGM(MSG_UNKNOWN_COMMAND);
  4866. SERIAL_ECHO(CMDBUFFER_CURRENT_STRING);
  4867. SERIAL_ECHOLNPGM("\"(1)");
  4868. }
  4869. }
  4870. }break;
  4871. #endif // FWRETRACT
  4872. #if EXTRUDERS > 1
  4873. case 218: // M218 - set hotend offset (in mm), T<extruder_number> X<offset_on_X> Y<offset_on_Y>
  4874. {
  4875. if(setTargetedHotend(218)){
  4876. break;
  4877. }
  4878. if(code_seen('X'))
  4879. {
  4880. extruder_offset[X_AXIS][tmp_extruder] = code_value();
  4881. }
  4882. if(code_seen('Y'))
  4883. {
  4884. extruder_offset[Y_AXIS][tmp_extruder] = code_value();
  4885. }
  4886. SERIAL_ECHO_START;
  4887. SERIAL_ECHORPGM(MSG_HOTEND_OFFSET);
  4888. for(tmp_extruder = 0; tmp_extruder < EXTRUDERS; tmp_extruder++)
  4889. {
  4890. SERIAL_ECHO(" ");
  4891. SERIAL_ECHO(extruder_offset[X_AXIS][tmp_extruder]);
  4892. SERIAL_ECHO(",");
  4893. SERIAL_ECHO(extruder_offset[Y_AXIS][tmp_extruder]);
  4894. }
  4895. SERIAL_ECHOLN("");
  4896. }break;
  4897. #endif
  4898. case 220: // M220 S<factor in percent>- set speed factor override percentage
  4899. {
  4900. if(code_seen('S'))
  4901. {
  4902. feedmultiply = code_value() ;
  4903. }
  4904. }
  4905. break;
  4906. case 221: // M221 S<factor in percent>- set extrude factor override percentage
  4907. {
  4908. if(code_seen('S'))
  4909. {
  4910. int tmp_code = code_value();
  4911. if (code_seen('T'))
  4912. {
  4913. if(setTargetedHotend(221)){
  4914. break;
  4915. }
  4916. extruder_multiply[tmp_extruder] = tmp_code;
  4917. }
  4918. else
  4919. {
  4920. extrudemultiply = tmp_code ;
  4921. }
  4922. }
  4923. calculate_extruder_multipliers();
  4924. }
  4925. break;
  4926. #ifndef _DISABLE_M42_M226
  4927. case 226: // M226 P<pin number> S<pin state>- Wait until the specified pin reaches the state required
  4928. {
  4929. if(code_seen('P')){
  4930. int pin_number = code_value(); // pin number
  4931. int pin_state = -1; // required pin state - default is inverted
  4932. if(code_seen('S')) pin_state = code_value(); // required pin state
  4933. if(pin_state >= -1 && pin_state <= 1){
  4934. for(int8_t i = 0; i < (int8_t)(sizeof(sensitive_pins)/sizeof(int)); i++)
  4935. {
  4936. if (sensitive_pins[i] == pin_number)
  4937. {
  4938. pin_number = -1;
  4939. break;
  4940. }
  4941. }
  4942. if (pin_number > -1)
  4943. {
  4944. int target = LOW;
  4945. st_synchronize();
  4946. pinMode(pin_number, INPUT);
  4947. switch(pin_state){
  4948. case 1:
  4949. target = HIGH;
  4950. break;
  4951. case 0:
  4952. target = LOW;
  4953. break;
  4954. case -1:
  4955. target = !digitalRead(pin_number);
  4956. break;
  4957. }
  4958. while(digitalRead(pin_number) != target){
  4959. manage_heater();
  4960. manage_inactivity();
  4961. lcd_update();
  4962. }
  4963. }
  4964. }
  4965. }
  4966. }
  4967. break;
  4968. #endif //_DISABLE_M42_M226
  4969. #if NUM_SERVOS > 0
  4970. case 280: // M280 - set servo position absolute. P: servo index, S: angle or microseconds
  4971. {
  4972. int servo_index = -1;
  4973. int servo_position = 0;
  4974. if (code_seen('P'))
  4975. servo_index = code_value();
  4976. if (code_seen('S')) {
  4977. servo_position = code_value();
  4978. if ((servo_index >= 0) && (servo_index < NUM_SERVOS)) {
  4979. #if defined (ENABLE_AUTO_BED_LEVELING) && (PROBE_SERVO_DEACTIVATION_DELAY > 0)
  4980. servos[servo_index].attach(0);
  4981. #endif
  4982. servos[servo_index].write(servo_position);
  4983. #if defined (ENABLE_AUTO_BED_LEVELING) && (PROBE_SERVO_DEACTIVATION_DELAY > 0)
  4984. delay(PROBE_SERVO_DEACTIVATION_DELAY);
  4985. servos[servo_index].detach();
  4986. #endif
  4987. }
  4988. else {
  4989. SERIAL_ECHO_START;
  4990. SERIAL_ECHO("Servo ");
  4991. SERIAL_ECHO(servo_index);
  4992. SERIAL_ECHOLN(" out of range");
  4993. }
  4994. }
  4995. else if (servo_index >= 0) {
  4996. SERIAL_PROTOCOL(_T(MSG_OK));
  4997. SERIAL_PROTOCOL(" Servo ");
  4998. SERIAL_PROTOCOL(servo_index);
  4999. SERIAL_PROTOCOL(": ");
  5000. SERIAL_PROTOCOL(servos[servo_index].read());
  5001. SERIAL_PROTOCOLLN("");
  5002. }
  5003. }
  5004. break;
  5005. #endif // NUM_SERVOS > 0
  5006. #if (LARGE_FLASH == true && ( BEEPER > 0 || defined(ULTRALCD) || defined(LCD_USE_I2C_BUZZER)))
  5007. case 300: // M300
  5008. {
  5009. int beepS = code_seen('S') ? code_value() : 110;
  5010. int beepP = code_seen('P') ? code_value() : 1000;
  5011. if (beepS > 0)
  5012. {
  5013. #if BEEPER > 0
  5014. tone(BEEPER, beepS);
  5015. delay(beepP);
  5016. noTone(BEEPER);
  5017. #elif defined(ULTRALCD)
  5018. lcd_buzz(beepS, beepP);
  5019. #elif defined(LCD_USE_I2C_BUZZER)
  5020. lcd_buzz(beepP, beepS);
  5021. #endif
  5022. }
  5023. else
  5024. {
  5025. delay(beepP);
  5026. }
  5027. }
  5028. break;
  5029. #endif // M300
  5030. #ifdef PIDTEMP
  5031. case 301: // M301
  5032. {
  5033. if(code_seen('P')) Kp = code_value();
  5034. if(code_seen('I')) Ki = scalePID_i(code_value());
  5035. if(code_seen('D')) Kd = scalePID_d(code_value());
  5036. #ifdef PID_ADD_EXTRUSION_RATE
  5037. if(code_seen('C')) Kc = code_value();
  5038. #endif
  5039. updatePID();
  5040. SERIAL_PROTOCOLRPGM(_T(MSG_OK));
  5041. SERIAL_PROTOCOL(" p:");
  5042. SERIAL_PROTOCOL(Kp);
  5043. SERIAL_PROTOCOL(" i:");
  5044. SERIAL_PROTOCOL(unscalePID_i(Ki));
  5045. SERIAL_PROTOCOL(" d:");
  5046. SERIAL_PROTOCOL(unscalePID_d(Kd));
  5047. #ifdef PID_ADD_EXTRUSION_RATE
  5048. SERIAL_PROTOCOL(" c:");
  5049. //Kc does not have scaling applied above, or in resetting defaults
  5050. SERIAL_PROTOCOL(Kc);
  5051. #endif
  5052. SERIAL_PROTOCOLLN("");
  5053. }
  5054. break;
  5055. #endif //PIDTEMP
  5056. #ifdef PIDTEMPBED
  5057. case 304: // M304
  5058. {
  5059. if(code_seen('P')) bedKp = code_value();
  5060. if(code_seen('I')) bedKi = scalePID_i(code_value());
  5061. if(code_seen('D')) bedKd = scalePID_d(code_value());
  5062. updatePID();
  5063. SERIAL_PROTOCOLRPGM(_T(MSG_OK));
  5064. SERIAL_PROTOCOL(" p:");
  5065. SERIAL_PROTOCOL(bedKp);
  5066. SERIAL_PROTOCOL(" i:");
  5067. SERIAL_PROTOCOL(unscalePID_i(bedKi));
  5068. SERIAL_PROTOCOL(" d:");
  5069. SERIAL_PROTOCOL(unscalePID_d(bedKd));
  5070. SERIAL_PROTOCOLLN("");
  5071. }
  5072. break;
  5073. #endif //PIDTEMP
  5074. case 240: // M240 Triggers a camera by emulating a Canon RC-1 : http://www.doc-diy.net/photo/rc-1_hacked/
  5075. {
  5076. #ifdef CHDK
  5077. SET_OUTPUT(CHDK);
  5078. WRITE(CHDK, HIGH);
  5079. chdkHigh = millis();
  5080. chdkActive = true;
  5081. #else
  5082. #if defined(PHOTOGRAPH_PIN) && PHOTOGRAPH_PIN > -1
  5083. const uint8_t NUM_PULSES=16;
  5084. const float PULSE_LENGTH=0.01524;
  5085. for(int i=0; i < NUM_PULSES; i++) {
  5086. WRITE(PHOTOGRAPH_PIN, HIGH);
  5087. _delay_ms(PULSE_LENGTH);
  5088. WRITE(PHOTOGRAPH_PIN, LOW);
  5089. _delay_ms(PULSE_LENGTH);
  5090. }
  5091. delay(7.33);
  5092. for(int i=0; i < NUM_PULSES; i++) {
  5093. WRITE(PHOTOGRAPH_PIN, HIGH);
  5094. _delay_ms(PULSE_LENGTH);
  5095. WRITE(PHOTOGRAPH_PIN, LOW);
  5096. _delay_ms(PULSE_LENGTH);
  5097. }
  5098. #endif
  5099. #endif //chdk end if
  5100. }
  5101. break;
  5102. #ifdef DOGLCD
  5103. case 250: // M250 Set LCD contrast value: C<value> (value 0..63)
  5104. {
  5105. if (code_seen('C')) {
  5106. lcd_setcontrast( ((int)code_value())&63 );
  5107. }
  5108. SERIAL_PROTOCOLPGM("lcd contrast value: ");
  5109. SERIAL_PROTOCOL(lcd_contrast);
  5110. SERIAL_PROTOCOLLN("");
  5111. }
  5112. break;
  5113. #endif
  5114. #ifdef PREVENT_DANGEROUS_EXTRUDE
  5115. case 302: // allow cold extrudes, or set the minimum extrude temperature
  5116. {
  5117. float temp = .0;
  5118. if (code_seen('S')) temp=code_value();
  5119. set_extrude_min_temp(temp);
  5120. }
  5121. break;
  5122. #endif
  5123. case 303: // M303 PID autotune
  5124. {
  5125. float temp = 150.0;
  5126. int e=0;
  5127. int c=5;
  5128. if (code_seen('E')) e=code_value();
  5129. if (e<0)
  5130. temp=70;
  5131. if (code_seen('S')) temp=code_value();
  5132. if (code_seen('C')) c=code_value();
  5133. PID_autotune(temp, e, c);
  5134. }
  5135. break;
  5136. case 400: // M400 finish all moves
  5137. {
  5138. st_synchronize();
  5139. }
  5140. break;
  5141. case 500: // M500 Store settings in EEPROM
  5142. {
  5143. Config_StoreSettings(EEPROM_OFFSET);
  5144. }
  5145. break;
  5146. case 501: // M501 Read settings from EEPROM
  5147. {
  5148. Config_RetrieveSettings(EEPROM_OFFSET);
  5149. }
  5150. break;
  5151. case 502: // M502 Revert to default settings
  5152. {
  5153. Config_ResetDefault();
  5154. }
  5155. break;
  5156. case 503: // M503 print settings currently in memory
  5157. {
  5158. Config_PrintSettings();
  5159. }
  5160. break;
  5161. case 509: //M509 Force language selection
  5162. {
  5163. lcd_force_language_selection();
  5164. SERIAL_ECHO_START;
  5165. SERIAL_PROTOCOLPGM(("LANG SEL FORCED"));
  5166. }
  5167. break;
  5168. #ifdef ABORT_ON_ENDSTOP_HIT_FEATURE_ENABLED
  5169. case 540:
  5170. {
  5171. if(code_seen('S')) abort_on_endstop_hit = code_value() > 0;
  5172. }
  5173. break;
  5174. #endif
  5175. #ifdef CUSTOM_M_CODE_SET_Z_PROBE_OFFSET
  5176. case CUSTOM_M_CODE_SET_Z_PROBE_OFFSET:
  5177. {
  5178. float value;
  5179. if (code_seen('Z'))
  5180. {
  5181. value = code_value();
  5182. if ((Z_PROBE_OFFSET_RANGE_MIN <= value) && (value <= Z_PROBE_OFFSET_RANGE_MAX))
  5183. {
  5184. zprobe_zoffset = -value; // compare w/ line 278 of ConfigurationStore.cpp
  5185. SERIAL_ECHO_START;
  5186. SERIAL_ECHOLNRPGM(CAT4(MSG_ZPROBE_ZOFFSET, " ", _T(MSG_OK),PSTR("")));
  5187. SERIAL_PROTOCOLLN("");
  5188. }
  5189. else
  5190. {
  5191. SERIAL_ECHO_START;
  5192. SERIAL_ECHORPGM(MSG_ZPROBE_ZOFFSET);
  5193. SERIAL_ECHORPGM(MSG_Z_MIN);
  5194. SERIAL_ECHO(Z_PROBE_OFFSET_RANGE_MIN);
  5195. SERIAL_ECHORPGM(MSG_Z_MAX);
  5196. SERIAL_ECHO(Z_PROBE_OFFSET_RANGE_MAX);
  5197. SERIAL_PROTOCOLLN("");
  5198. }
  5199. }
  5200. else
  5201. {
  5202. SERIAL_ECHO_START;
  5203. SERIAL_ECHOLNRPGM(CAT2(MSG_ZPROBE_ZOFFSET, PSTR(" : ")));
  5204. SERIAL_ECHO(-zprobe_zoffset);
  5205. SERIAL_PROTOCOLLN("");
  5206. }
  5207. break;
  5208. }
  5209. #endif // CUSTOM_M_CODE_SET_Z_PROBE_OFFSET
  5210. #ifdef FILAMENTCHANGEENABLE
  5211. case 600: //Pause for filament change X[pos] Y[pos] Z[relative lift] E[initial retract] L[later retract distance for removal]
  5212. {
  5213. #ifdef PAT9125
  5214. bool old_fsensor_enabled = fsensor_enabled;
  5215. fsensor_enabled = false; //temporary solution for unexpected restarting
  5216. #endif //PAT9125
  5217. st_synchronize();
  5218. float target[4];
  5219. float lastpos[4];
  5220. if (farm_mode)
  5221. {
  5222. prusa_statistics(22);
  5223. }
  5224. feedmultiplyBckp=feedmultiply;
  5225. int8_t TooLowZ = 0;
  5226. float HotendTempBckp = degTargetHotend(active_extruder);
  5227. int fanSpeedBckp = fanSpeed;
  5228. target[X_AXIS]=current_position[X_AXIS];
  5229. target[Y_AXIS]=current_position[Y_AXIS];
  5230. target[Z_AXIS]=current_position[Z_AXIS];
  5231. target[E_AXIS]=current_position[E_AXIS];
  5232. lastpos[X_AXIS]=current_position[X_AXIS];
  5233. lastpos[Y_AXIS]=current_position[Y_AXIS];
  5234. lastpos[Z_AXIS]=current_position[Z_AXIS];
  5235. lastpos[E_AXIS]=current_position[E_AXIS];
  5236. //Restract extruder
  5237. if(code_seen('E'))
  5238. {
  5239. target[E_AXIS]+= code_value();
  5240. }
  5241. else
  5242. {
  5243. #ifdef FILAMENTCHANGE_FIRSTRETRACT
  5244. target[E_AXIS]+= FILAMENTCHANGE_FIRSTRETRACT ;
  5245. #endif
  5246. }
  5247. plan_buffer_line(target[X_AXIS], target[Y_AXIS], target[Z_AXIS], target[E_AXIS], FILAMENTCHANGE_RFEED, active_extruder);
  5248. //Lift Z
  5249. if(code_seen('Z'))
  5250. {
  5251. target[Z_AXIS]+= code_value();
  5252. }
  5253. else
  5254. {
  5255. #ifdef FILAMENTCHANGE_ZADD
  5256. target[Z_AXIS]+= FILAMENTCHANGE_ZADD ;
  5257. if(target[Z_AXIS] < 10){
  5258. target[Z_AXIS]+= 10 ;
  5259. TooLowZ = 1;
  5260. }else{
  5261. TooLowZ = 0;
  5262. }
  5263. #endif
  5264. }
  5265. plan_buffer_line(target[X_AXIS], target[Y_AXIS], target[Z_AXIS], target[E_AXIS], FILAMENTCHANGE_ZFEED, active_extruder);
  5266. //Move XY to side
  5267. if(code_seen('X'))
  5268. {
  5269. target[X_AXIS]+= code_value();
  5270. }
  5271. else
  5272. {
  5273. #ifdef FILAMENTCHANGE_XPOS
  5274. target[X_AXIS]= FILAMENTCHANGE_XPOS ;
  5275. #endif
  5276. }
  5277. if(code_seen('Y'))
  5278. {
  5279. target[Y_AXIS]= code_value();
  5280. }
  5281. else
  5282. {
  5283. #ifdef FILAMENTCHANGE_YPOS
  5284. target[Y_AXIS]= FILAMENTCHANGE_YPOS ;
  5285. #endif
  5286. }
  5287. plan_buffer_line(target[X_AXIS], target[Y_AXIS], target[Z_AXIS], target[E_AXIS], FILAMENTCHANGE_XYFEED, active_extruder);
  5288. st_synchronize();
  5289. KEEPALIVE_STATE(PAUSED_FOR_USER);
  5290. uint8_t cnt = 0;
  5291. int counterBeep = 0;
  5292. fanSpeed = 0;
  5293. unsigned long waiting_start_time = millis();
  5294. uint8_t wait_for_user_state = 0;
  5295. lcd_display_message_fullscreen_P(_T(MSG_PRESS_TO_UNLOAD));
  5296. while (!(wait_for_user_state == 0 && lcd_clicked())){
  5297. //cnt++;
  5298. manage_heater();
  5299. manage_inactivity(true);
  5300. /*#ifdef SNMM
  5301. target[E_AXIS] += 0.002;
  5302. plan_buffer_line(target[X_AXIS], target[Y_AXIS], target[Z_AXIS], target[E_AXIS], 500, active_extruder);
  5303. #endif // SNMM*/
  5304. //if (cnt == 0)
  5305. {
  5306. #if BEEPER > 0
  5307. if (counterBeep == 500) {
  5308. counterBeep = 0;
  5309. }
  5310. SET_OUTPUT(BEEPER);
  5311. if (counterBeep == 0) {
  5312. WRITE(BEEPER, HIGH);
  5313. }
  5314. if (counterBeep == 20) {
  5315. WRITE(BEEPER, LOW);
  5316. }
  5317. counterBeep++;
  5318. #else
  5319. #if !defined(LCD_FEEDBACK_FREQUENCY_HZ) || !defined(LCD_FEEDBACK_FREQUENCY_DURATION_MS)
  5320. lcd_buzz(1000 / 6, 100);
  5321. #else
  5322. lcd_buzz(LCD_FEEDBACK_FREQUENCY_DURATION_MS, LCD_FEEDBACK_FREQUENCY_HZ);
  5323. #endif
  5324. #endif
  5325. }
  5326. switch (wait_for_user_state) {
  5327. case 0:
  5328. delay_keep_alive(4);
  5329. if (millis() > waiting_start_time + (unsigned long)M600_TIMEOUT * 1000) {
  5330. lcd_display_message_fullscreen_P(_i("Press knob to preheat nozzle and continue."));////MSG_PRESS_TO_PREHEAT c=20 r=4
  5331. wait_for_user_state = 1;
  5332. setTargetHotend(0, 0);
  5333. setTargetHotend(0, 1);
  5334. setTargetHotend(0, 2);
  5335. st_synchronize();
  5336. disable_e0();
  5337. disable_e1();
  5338. disable_e2();
  5339. }
  5340. break;
  5341. case 1:
  5342. delay_keep_alive(4);
  5343. if (lcd_clicked()) {
  5344. setTargetHotend(HotendTempBckp, active_extruder);
  5345. lcd_wait_for_heater();
  5346. wait_for_user_state = 2;
  5347. }
  5348. break;
  5349. case 2:
  5350. if (abs(degTargetHotend(active_extruder) - degHotend(active_extruder)) < 1) {
  5351. lcd_display_message_fullscreen_P(_T(MSG_PRESS_TO_UNLOAD));
  5352. waiting_start_time = millis();
  5353. wait_for_user_state = 0;
  5354. }
  5355. else {
  5356. counterBeep = 20; //beeper will be inactive during waiting for nozzle preheat
  5357. lcd.setCursor(1, 4);
  5358. lcd.print(ftostr3(degHotend(active_extruder)));
  5359. }
  5360. break;
  5361. }
  5362. }
  5363. WRITE(BEEPER, LOW);
  5364. lcd_change_fil_state = 0;
  5365. // Unload filament
  5366. lcd_display_message_fullscreen_P(_T(MSG_UNLOADING_FILAMENT));
  5367. KEEPALIVE_STATE(IN_HANDLER);
  5368. custom_message = true;
  5369. lcd_setstatuspgm(_T(MSG_UNLOADING_FILAMENT));
  5370. if (code_seen('L'))
  5371. {
  5372. target[E_AXIS] += code_value();
  5373. }
  5374. else
  5375. {
  5376. #ifdef SNMM
  5377. #else
  5378. #ifdef FILAMENTCHANGE_FINALRETRACT
  5379. target[E_AXIS] += FILAMENTCHANGE_FINALRETRACT;
  5380. #endif
  5381. #endif // SNMM
  5382. }
  5383. #ifdef SNMM
  5384. target[E_AXIS] += 12;
  5385. plan_buffer_line(target[X_AXIS], target[Y_AXIS], target[Z_AXIS], target[E_AXIS], 3500, active_extruder);
  5386. target[E_AXIS] += 6;
  5387. plan_buffer_line(target[X_AXIS], target[Y_AXIS], target[Z_AXIS], target[E_AXIS], 5000, active_extruder);
  5388. target[E_AXIS] += (FIL_LOAD_LENGTH * -1);
  5389. plan_buffer_line(target[X_AXIS], target[Y_AXIS], target[Z_AXIS], target[E_AXIS], 5000, active_extruder);
  5390. st_synchronize();
  5391. target[E_AXIS] += (FIL_COOLING);
  5392. plan_buffer_line(target[X_AXIS], target[Y_AXIS], target[Z_AXIS], target[E_AXIS], 50, active_extruder);
  5393. target[E_AXIS] += (FIL_COOLING*-1);
  5394. plan_buffer_line(target[X_AXIS], target[Y_AXIS], target[Z_AXIS], target[E_AXIS], 50, active_extruder);
  5395. target[E_AXIS] += (bowden_length[snmm_extruder] * -1);
  5396. plan_buffer_line(target[X_AXIS], target[Y_AXIS], target[Z_AXIS], target[E_AXIS], 3000, active_extruder);
  5397. st_synchronize();
  5398. #else
  5399. // plan_buffer_line(target[X_AXIS], target[Y_AXIS], target[Z_AXIS], target[E_AXIS], FILAMENTCHANGE_RFEED, active_extruder);
  5400. //plan_buffer_line(target[X_AXIS], target[Y_AXIS], target[Z_AXIS], target[E_AXIS], 3500 / 60, active_extruder);
  5401. target[E_AXIS] -= FILAMENTCHANGE_FINALRETRACT;
  5402. st_synchronize();
  5403. #ifdef TMC2130
  5404. uint8_t tmc2130_current_r_bckp = tmc2130_current_r[E_AXIS];
  5405. tmc2130_set_current_r(E_AXIS, TMC2130_UNLOAD_CURRENT_R);
  5406. #else
  5407. st_current_set(2, 200); //set lower E motor current for unload to protect filament sensor and ptfe tube
  5408. float tmp_motor[3] = DEFAULT_PWM_MOTOR_CURRENT;
  5409. float tmp_motor_loud[3] = DEFAULT_PWM_MOTOR_CURRENT_LOUD;
  5410. #endif //TMC2130
  5411. target[E_AXIS] -= 45;
  5412. plan_buffer_line(target[X_AXIS], target[Y_AXIS], target[Z_AXIS], target[E_AXIS], 5200 / 60, active_extruder);
  5413. st_synchronize();
  5414. target[E_AXIS] -= 15;
  5415. plan_buffer_line(target[X_AXIS], target[Y_AXIS], target[Z_AXIS], target[E_AXIS], 1000 / 60, active_extruder);
  5416. st_synchronize();
  5417. target[E_AXIS] -= 20;
  5418. plan_buffer_line(target[X_AXIS], target[Y_AXIS], target[Z_AXIS], target[E_AXIS], 1000 / 60, active_extruder);
  5419. st_synchronize();
  5420. #ifdef TMC2130
  5421. tmc2130_set_current_r(E_AXIS, tmc2130_current_r_bckp);
  5422. #else
  5423. uint8_t silentMode = eeprom_read_byte((uint8_t*)EEPROM_SILENT);
  5424. if(silentMode != SILENT_MODE_POWER) st_current_set(2, tmp_motor[2]); //set E back to normal operation currents
  5425. else st_current_set(2, tmp_motor_loud[2]);
  5426. #endif //TMC2130
  5427. #endif // SNMM
  5428. //finish moves
  5429. st_synchronize();
  5430. lcd_display_message_fullscreen_P(_T(MSG_PULL_OUT_FILAMENT));
  5431. //disable extruder steppers so filament can be removed
  5432. disable_e0();
  5433. disable_e1();
  5434. disable_e2();
  5435. delay(100);
  5436. WRITE(BEEPER, HIGH);
  5437. counterBeep = 0;
  5438. while(!lcd_clicked() && (counterBeep < 50)) {
  5439. if(counterBeep > 5) WRITE(BEEPER, LOW);
  5440. delay_keep_alive(100);
  5441. counterBeep++;
  5442. }
  5443. WRITE(BEEPER, LOW);
  5444. KEEPALIVE_STATE(PAUSED_FOR_USER);
  5445. lcd_change_fil_state = lcd_show_fullscreen_message_yes_no_and_wait_P(_i("Was filament unload successful?"), false, true);////MSG_UNLOAD_SUCCESSFUL c=20 r=2
  5446. if (lcd_change_fil_state == 0) lcd_show_fullscreen_message_and_wait_P(_i("Please open idler and remove filament manually."));////MSG_CHECK_IDLER c=20 r=4
  5447. //lcd_return_to_status();
  5448. lcd_update_enable(true);
  5449. //Wait for user to insert filament
  5450. lcd_wait_interact();
  5451. //load_filament_time = millis();
  5452. KEEPALIVE_STATE(PAUSED_FOR_USER);
  5453. #ifdef PAT9125
  5454. if (filament_autoload_enabled && (old_fsensor_enabled || fsensor_M600)) fsensor_autoload_check_start();
  5455. #endif //PAT9125
  5456. // printf_P(PSTR("M600 PAT9125 filament_autoload_enabled=%d, old_fsensor_enabled=%d, fsensor_M600=%d"), filament_autoload_enabled, old_fsensor_enabled, fsensor_M600);
  5457. while(!lcd_clicked())
  5458. {
  5459. manage_heater();
  5460. manage_inactivity(true);
  5461. #ifdef PAT9125
  5462. if (filament_autoload_enabled && (old_fsensor_enabled || fsensor_M600) && fsensor_check_autoload())
  5463. {
  5464. tone(BEEPER, 1000);
  5465. delay_keep_alive(50);
  5466. noTone(BEEPER);
  5467. break;
  5468. }
  5469. #endif //PAT9125
  5470. /*#ifdef SNMM
  5471. target[E_AXIS] += 0.002;
  5472. plan_buffer_line(target[X_AXIS], target[Y_AXIS], target[Z_AXIS], target[E_AXIS], 500, active_extruder);
  5473. #endif // SNMM*/
  5474. }
  5475. #ifdef PAT9125
  5476. if (filament_autoload_enabled && (old_fsensor_enabled || fsensor_M600)) fsensor_autoload_check_stop();
  5477. #endif //PAT9125
  5478. //WRITE(BEEPER, LOW);
  5479. KEEPALIVE_STATE(IN_HANDLER);
  5480. #ifdef SNMM
  5481. display_loading();
  5482. KEEPALIVE_STATE(PAUSED_FOR_USER);
  5483. do {
  5484. target[E_AXIS] += 0.002;
  5485. plan_buffer_line(target[X_AXIS], target[Y_AXIS], target[Z_AXIS], target[E_AXIS], 500, active_extruder);
  5486. delay_keep_alive(2);
  5487. } while (!lcd_clicked());
  5488. KEEPALIVE_STATE(IN_HANDLER);
  5489. /*if (millis() - load_filament_time > 2) {
  5490. load_filament_time = millis();
  5491. target[E_AXIS] += 0.001;
  5492. plan_buffer_line(target[X_AXIS], target[Y_AXIS], target[Z_AXIS], target[E_AXIS], 1000, active_extruder);
  5493. }*/
  5494. //Filament inserted
  5495. //Feed the filament to the end of nozzle quickly
  5496. st_synchronize();
  5497. target[E_AXIS] += bowden_length[snmm_extruder];
  5498. plan_buffer_line(target[X_AXIS], target[Y_AXIS], target[Z_AXIS], target[E_AXIS], 3000, active_extruder);
  5499. target[E_AXIS] += FIL_LOAD_LENGTH - 60;
  5500. plan_buffer_line(target[X_AXIS], target[Y_AXIS], target[Z_AXIS], target[E_AXIS], 1400, active_extruder);
  5501. target[E_AXIS] += 40;
  5502. plan_buffer_line(target[X_AXIS], target[Y_AXIS], target[Z_AXIS], target[E_AXIS], 400, active_extruder);
  5503. target[E_AXIS] += 10;
  5504. plan_buffer_line(target[X_AXIS], target[Y_AXIS], target[Z_AXIS], target[E_AXIS], 50, active_extruder);
  5505. #else
  5506. target[E_AXIS] += FILAMENTCHANGE_FIRSTFEED;
  5507. plan_buffer_line(target[X_AXIS], target[Y_AXIS], target[Z_AXIS], target[E_AXIS], FILAMENTCHANGE_EFEED, active_extruder);
  5508. #endif // SNMM
  5509. //Extrude some filament
  5510. target[E_AXIS]+= FILAMENTCHANGE_FINALFEED ;
  5511. plan_buffer_line(target[X_AXIS], target[Y_AXIS], target[Z_AXIS], target[E_AXIS], FILAMENTCHANGE_EXFEED, active_extruder);
  5512. //Wait for user to check the state
  5513. lcd_change_fil_state = 0;
  5514. lcd_loading_filament();
  5515. tone(BEEPER, 500);
  5516. delay_keep_alive(50);
  5517. noTone(BEEPER);
  5518. while ((lcd_change_fil_state == 0)||(lcd_change_fil_state != 1)){
  5519. lcd_change_fil_state = 0;
  5520. KEEPALIVE_STATE(PAUSED_FOR_USER);
  5521. lcd_alright();
  5522. KEEPALIVE_STATE(IN_HANDLER);
  5523. switch(lcd_change_fil_state){
  5524. // Filament failed to load so load it again
  5525. case 2:
  5526. #ifdef SNMM
  5527. display_loading();
  5528. do {
  5529. target[E_AXIS] += 0.002;
  5530. plan_buffer_line(target[X_AXIS], target[Y_AXIS], target[Z_AXIS], target[E_AXIS], 500, active_extruder);
  5531. delay_keep_alive(2);
  5532. } while (!lcd_clicked());
  5533. st_synchronize();
  5534. target[E_AXIS] += bowden_length[snmm_extruder];
  5535. plan_buffer_line(target[X_AXIS], target[Y_AXIS], target[Z_AXIS], target[E_AXIS], 3000, active_extruder);
  5536. target[E_AXIS] += FIL_LOAD_LENGTH - 60;
  5537. plan_buffer_line(target[X_AXIS], target[Y_AXIS], target[Z_AXIS], target[E_AXIS], 1400, active_extruder);
  5538. target[E_AXIS] += 40;
  5539. plan_buffer_line(target[X_AXIS], target[Y_AXIS], target[Z_AXIS], target[E_AXIS], 400, active_extruder);
  5540. target[E_AXIS] += 10;
  5541. plan_buffer_line(target[X_AXIS], target[Y_AXIS], target[Z_AXIS], target[E_AXIS], 50, active_extruder);
  5542. #else
  5543. target[E_AXIS]+= FILAMENTCHANGE_FIRSTFEED ;
  5544. plan_buffer_line(target[X_AXIS], target[Y_AXIS], target[Z_AXIS], target[E_AXIS], FILAMENTCHANGE_EFEED, active_extruder);
  5545. #endif
  5546. target[E_AXIS]+= FILAMENTCHANGE_FINALFEED ;
  5547. plan_buffer_line(target[X_AXIS], target[Y_AXIS], target[Z_AXIS], target[E_AXIS], FILAMENTCHANGE_EXFEED, active_extruder);
  5548. lcd_loading_filament();
  5549. break;
  5550. // Filament loaded properly but color is not clear
  5551. case 3:
  5552. target[E_AXIS]+= FILAMENTCHANGE_FINALFEED ;
  5553. plan_buffer_line(target[X_AXIS], target[Y_AXIS], target[Z_AXIS], target[E_AXIS], 2, active_extruder);
  5554. lcd_loading_color();
  5555. break;
  5556. // Everything good
  5557. default:
  5558. lcd_change_success();
  5559. lcd_update_enable(true);
  5560. break;
  5561. }
  5562. }
  5563. //Not let's go back to print
  5564. fanSpeed = fanSpeedBckp;
  5565. //Feed a little of filament to stabilize pressure
  5566. target[E_AXIS]+= FILAMENTCHANGE_RECFEED;
  5567. plan_buffer_line(target[X_AXIS], target[Y_AXIS], target[Z_AXIS], target[E_AXIS], FILAMENTCHANGE_EXFEED, active_extruder);
  5568. //Retract
  5569. target[E_AXIS]+= FILAMENTCHANGE_FIRSTRETRACT;
  5570. plan_buffer_line(target[X_AXIS], target[Y_AXIS], target[Z_AXIS], target[E_AXIS], FILAMENTCHANGE_RFEED, active_extruder);
  5571. //plan_buffer_line(target[X_AXIS], target[Y_AXIS], target[Z_AXIS], target[E_AXIS], 70, active_extruder); //should do nothing
  5572. //Move XY back
  5573. plan_buffer_line(lastpos[X_AXIS], lastpos[Y_AXIS], target[Z_AXIS], target[E_AXIS], FILAMENTCHANGE_XYFEED, active_extruder);
  5574. //Move Z back
  5575. plan_buffer_line(lastpos[X_AXIS], lastpos[Y_AXIS], lastpos[Z_AXIS], target[E_AXIS], FILAMENTCHANGE_ZFEED, active_extruder);
  5576. target[E_AXIS]= target[E_AXIS] - FILAMENTCHANGE_FIRSTRETRACT;
  5577. //Unretract
  5578. plan_buffer_line(lastpos[X_AXIS], lastpos[Y_AXIS], lastpos[Z_AXIS], target[E_AXIS], FILAMENTCHANGE_RFEED, active_extruder);
  5579. //Set E position to original
  5580. plan_set_e_position(lastpos[E_AXIS]);
  5581. //Recover feed rate
  5582. feedmultiply=feedmultiplyBckp;
  5583. char cmd[9];
  5584. sprintf_P(cmd, PSTR("M220 S%i"), feedmultiplyBckp);
  5585. enquecommand(cmd);
  5586. lcd_setstatuspgm(_T(WELCOME_MSG));
  5587. custom_message = false;
  5588. custom_message_type = 0;
  5589. #ifdef PAT9125
  5590. fsensor_enabled = old_fsensor_enabled; //temporary solution for unexpected restarting
  5591. if (fsensor_M600)
  5592. {
  5593. cmdqueue_pop_front(); //hack because M600 repeated 2x when enqueued to front
  5594. st_synchronize();
  5595. while (!is_buffer_empty())
  5596. {
  5597. process_commands();
  5598. cmdqueue_pop_front();
  5599. }
  5600. fsensor_enable();
  5601. fsensor_restore_print_and_continue();
  5602. }
  5603. #endif //PAT9125
  5604. }
  5605. break;
  5606. #endif //FILAMENTCHANGEENABLE
  5607. case 601: {
  5608. if(lcd_commands_type == 0) lcd_commands_type = LCD_COMMAND_LONG_PAUSE;
  5609. }
  5610. break;
  5611. case 602: {
  5612. if(lcd_commands_type == 0) lcd_commands_type = LCD_COMMAND_LONG_PAUSE_RESUME;
  5613. }
  5614. break;
  5615. #ifdef PINDA_THERMISTOR
  5616. case 860: // M860 - Wait for PINDA thermistor to reach target temperature.
  5617. {
  5618. int set_target_pinda = 0;
  5619. if (code_seen('S')) {
  5620. set_target_pinda = code_value();
  5621. }
  5622. else {
  5623. break;
  5624. }
  5625. LCD_MESSAGERPGM(_T(MSG_PLEASE_WAIT));
  5626. SERIAL_PROTOCOLPGM("Wait for PINDA target temperature:");
  5627. SERIAL_PROTOCOL(set_target_pinda);
  5628. SERIAL_PROTOCOLLN("");
  5629. codenum = millis();
  5630. cancel_heatup = false;
  5631. bool is_pinda_cooling = false;
  5632. if ((degTargetBed() == 0) && (degTargetHotend(0) == 0)) {
  5633. is_pinda_cooling = true;
  5634. }
  5635. while ( ((!is_pinda_cooling) && (!cancel_heatup) && (current_temperature_pinda < set_target_pinda)) || (is_pinda_cooling && (current_temperature_pinda > set_target_pinda)) ) {
  5636. if ((millis() - codenum) > 1000) //Print Temp Reading every 1 second while waiting.
  5637. {
  5638. SERIAL_PROTOCOLPGM("P:");
  5639. SERIAL_PROTOCOL_F(current_temperature_pinda, 1);
  5640. SERIAL_PROTOCOLPGM("/");
  5641. SERIAL_PROTOCOL(set_target_pinda);
  5642. SERIAL_PROTOCOLLN("");
  5643. codenum = millis();
  5644. }
  5645. manage_heater();
  5646. manage_inactivity();
  5647. lcd_update();
  5648. }
  5649. LCD_MESSAGERPGM(_T(MSG_OK));
  5650. break;
  5651. }
  5652. case 861: // M861 - Set/Read PINDA temperature compensation offsets
  5653. if (code_seen('?')) { // ? - Print out current EEPROM offset values
  5654. uint8_t cal_status = calibration_status_pinda();
  5655. int16_t usteps = 0;
  5656. cal_status ? SERIAL_PROTOCOLLN("PINDA cal status: 1") : SERIAL_PROTOCOLLN("PINDA cal status: 0");
  5657. SERIAL_PROTOCOLLN("index, temp, ustep, um");
  5658. for (uint8_t i = 0; i < 6; i++)
  5659. {
  5660. if(i>0) EEPROM_read_B(EEPROM_PROBE_TEMP_SHIFT + (i-1) * 2, &usteps);
  5661. float mm = ((float)usteps) / axis_steps_per_unit[Z_AXIS];
  5662. i == 0 ? SERIAL_PROTOCOLPGM("n/a") : SERIAL_PROTOCOL(i - 1);
  5663. SERIAL_PROTOCOLPGM(", ");
  5664. SERIAL_PROTOCOL(35 + (i * 5));
  5665. SERIAL_PROTOCOLPGM(", ");
  5666. SERIAL_PROTOCOL(usteps);
  5667. SERIAL_PROTOCOLPGM(", ");
  5668. SERIAL_PROTOCOL(mm * 1000);
  5669. SERIAL_PROTOCOLLN("");
  5670. }
  5671. }
  5672. else if (code_seen('!')) { // ! - Set factory default values
  5673. eeprom_write_byte((uint8_t*)EEPROM_CALIBRATION_STATUS_PINDA, 1);
  5674. int16_t z_shift = 8; //40C - 20um - 8usteps
  5675. EEPROM_save_B(EEPROM_PROBE_TEMP_SHIFT, &z_shift);
  5676. z_shift = 24; //45C - 60um - 24usteps
  5677. EEPROM_save_B(EEPROM_PROBE_TEMP_SHIFT + 2, &z_shift);
  5678. z_shift = 48; //50C - 120um - 48usteps
  5679. EEPROM_save_B(EEPROM_PROBE_TEMP_SHIFT + 4, &z_shift);
  5680. z_shift = 80; //55C - 200um - 80usteps
  5681. EEPROM_save_B(EEPROM_PROBE_TEMP_SHIFT + 6, &z_shift);
  5682. z_shift = 120; //60C - 300um - 120usteps
  5683. EEPROM_save_B(EEPROM_PROBE_TEMP_SHIFT + 8, &z_shift);
  5684. SERIAL_PROTOCOLLN("factory restored");
  5685. }
  5686. else if (code_seen('Z')) { // Z - Set all values to 0 (effectively disabling PINDA temperature compensation)
  5687. eeprom_write_byte((uint8_t*)EEPROM_CALIBRATION_STATUS_PINDA, 1);
  5688. int16_t z_shift = 0;
  5689. for (uint8_t i = 0; i < 5; i++) EEPROM_save_B(EEPROM_PROBE_TEMP_SHIFT + i * 2, &z_shift);
  5690. SERIAL_PROTOCOLLN("zerorized");
  5691. }
  5692. else if (code_seen('S')) { // Sxxx Iyyy - Set compensation ustep value S for compensation table index I
  5693. int16_t usteps = code_value();
  5694. if (code_seen('I')) {
  5695. byte index = code_value();
  5696. if ((index >= 0) && (index < 5)) {
  5697. EEPROM_save_B(EEPROM_PROBE_TEMP_SHIFT + index * 2, &usteps);
  5698. SERIAL_PROTOCOLLN("OK");
  5699. SERIAL_PROTOCOLLN("index, temp, ustep, um");
  5700. for (uint8_t i = 0; i < 6; i++)
  5701. {
  5702. usteps = 0;
  5703. if (i>0) EEPROM_read_B(EEPROM_PROBE_TEMP_SHIFT + (i - 1) * 2, &usteps);
  5704. float mm = ((float)usteps) / axis_steps_per_unit[Z_AXIS];
  5705. i == 0 ? SERIAL_PROTOCOLPGM("n/a") : SERIAL_PROTOCOL(i - 1);
  5706. SERIAL_PROTOCOLPGM(", ");
  5707. SERIAL_PROTOCOL(35 + (i * 5));
  5708. SERIAL_PROTOCOLPGM(", ");
  5709. SERIAL_PROTOCOL(usteps);
  5710. SERIAL_PROTOCOLPGM(", ");
  5711. SERIAL_PROTOCOL(mm * 1000);
  5712. SERIAL_PROTOCOLLN("");
  5713. }
  5714. }
  5715. }
  5716. }
  5717. else {
  5718. SERIAL_PROTOCOLPGM("no valid command");
  5719. }
  5720. break;
  5721. #endif //PINDA_THERMISTOR
  5722. #ifdef LIN_ADVANCE
  5723. case 900: // M900: Set LIN_ADVANCE options.
  5724. gcode_M900();
  5725. break;
  5726. #endif
  5727. case 907: // M907 Set digital trimpot motor current using axis codes.
  5728. {
  5729. #if defined(DIGIPOTSS_PIN) && DIGIPOTSS_PIN > -1
  5730. for(int i=0;i<NUM_AXIS;i++) if(code_seen(axis_codes[i])) st_current_set(i,code_value());
  5731. if(code_seen('B')) st_current_set(4,code_value());
  5732. if(code_seen('S')) for(int i=0;i<=4;i++) st_current_set(i,code_value());
  5733. #endif
  5734. #ifdef MOTOR_CURRENT_PWM_XY_PIN
  5735. if(code_seen('X')) st_current_set(0, code_value());
  5736. #endif
  5737. #ifdef MOTOR_CURRENT_PWM_Z_PIN
  5738. if(code_seen('Z')) st_current_set(1, code_value());
  5739. #endif
  5740. #ifdef MOTOR_CURRENT_PWM_E_PIN
  5741. if(code_seen('E')) st_current_set(2, code_value());
  5742. #endif
  5743. }
  5744. break;
  5745. case 908: // M908 Control digital trimpot directly.
  5746. {
  5747. #if defined(DIGIPOTSS_PIN) && DIGIPOTSS_PIN > -1
  5748. uint8_t channel,current;
  5749. if(code_seen('P')) channel=code_value();
  5750. if(code_seen('S')) current=code_value();
  5751. digitalPotWrite(channel, current);
  5752. #endif
  5753. }
  5754. break;
  5755. #ifdef TMC2130
  5756. case 910: // M910 TMC2130 init
  5757. {
  5758. tmc2130_init();
  5759. }
  5760. break;
  5761. case 911: // M911 Set TMC2130 holding currents
  5762. {
  5763. if (code_seen('X')) tmc2130_set_current_h(0, code_value());
  5764. if (code_seen('Y')) tmc2130_set_current_h(1, code_value());
  5765. if (code_seen('Z')) tmc2130_set_current_h(2, code_value());
  5766. if (code_seen('E')) tmc2130_set_current_h(3, code_value());
  5767. }
  5768. break;
  5769. case 912: // M912 Set TMC2130 running currents
  5770. {
  5771. if (code_seen('X')) tmc2130_set_current_r(0, code_value());
  5772. if (code_seen('Y')) tmc2130_set_current_r(1, code_value());
  5773. if (code_seen('Z')) tmc2130_set_current_r(2, code_value());
  5774. if (code_seen('E')) tmc2130_set_current_r(3, code_value());
  5775. }
  5776. break;
  5777. case 913: // M913 Print TMC2130 currents
  5778. {
  5779. tmc2130_print_currents();
  5780. }
  5781. break;
  5782. case 914: // M914 Set normal mode
  5783. {
  5784. tmc2130_mode = TMC2130_MODE_NORMAL;
  5785. tmc2130_init();
  5786. }
  5787. break;
  5788. case 915: // M915 Set silent mode
  5789. {
  5790. tmc2130_mode = TMC2130_MODE_SILENT;
  5791. tmc2130_init();
  5792. }
  5793. break;
  5794. case 916: // M916 Set sg_thrs
  5795. {
  5796. if (code_seen('X')) tmc2130_sg_thr[X_AXIS] = code_value();
  5797. if (code_seen('Y')) tmc2130_sg_thr[Y_AXIS] = code_value();
  5798. if (code_seen('Z')) tmc2130_sg_thr[Z_AXIS] = code_value();
  5799. if (code_seen('E')) tmc2130_sg_thr[E_AXIS] = code_value();
  5800. MYSERIAL.print("tmc2130_sg_thr[X]=");
  5801. MYSERIAL.println(tmc2130_sg_thr[X_AXIS], DEC);
  5802. MYSERIAL.print("tmc2130_sg_thr[Y]=");
  5803. MYSERIAL.println(tmc2130_sg_thr[Y_AXIS], DEC);
  5804. MYSERIAL.print("tmc2130_sg_thr[Z]=");
  5805. MYSERIAL.println(tmc2130_sg_thr[Z_AXIS], DEC);
  5806. MYSERIAL.print("tmc2130_sg_thr[E]=");
  5807. MYSERIAL.println(tmc2130_sg_thr[E_AXIS], DEC);
  5808. }
  5809. break;
  5810. case 917: // M917 Set TMC2130 pwm_ampl
  5811. {
  5812. if (code_seen('X')) tmc2130_set_pwm_ampl(0, code_value());
  5813. if (code_seen('Y')) tmc2130_set_pwm_ampl(1, code_value());
  5814. if (code_seen('Z')) tmc2130_set_pwm_ampl(2, code_value());
  5815. if (code_seen('E')) tmc2130_set_pwm_ampl(3, code_value());
  5816. }
  5817. break;
  5818. case 918: // M918 Set TMC2130 pwm_grad
  5819. {
  5820. if (code_seen('X')) tmc2130_set_pwm_grad(0, code_value());
  5821. if (code_seen('Y')) tmc2130_set_pwm_grad(1, code_value());
  5822. if (code_seen('Z')) tmc2130_set_pwm_grad(2, code_value());
  5823. if (code_seen('E')) tmc2130_set_pwm_grad(3, code_value());
  5824. }
  5825. break;
  5826. #endif //TMC2130
  5827. case 350: // M350 Set microstepping mode. Warning: Steps per unit remains unchanged. S code sets stepping mode for all drivers.
  5828. {
  5829. #ifdef TMC2130
  5830. if(code_seen('E'))
  5831. {
  5832. uint16_t res_new = code_value();
  5833. if ((res_new == 8) || (res_new == 16) || (res_new == 32) || (res_new == 64) || (res_new == 128))
  5834. {
  5835. st_synchronize();
  5836. uint8_t axis = E_AXIS;
  5837. uint16_t res = tmc2130_get_res(axis);
  5838. tmc2130_set_res(axis, res_new);
  5839. if (res_new > res)
  5840. {
  5841. uint16_t fac = (res_new / res);
  5842. axis_steps_per_unit[axis] *= fac;
  5843. position[E_AXIS] *= fac;
  5844. }
  5845. else
  5846. {
  5847. uint16_t fac = (res / res_new);
  5848. axis_steps_per_unit[axis] /= fac;
  5849. position[E_AXIS] /= fac;
  5850. }
  5851. }
  5852. }
  5853. #else //TMC2130
  5854. #if defined(X_MS1_PIN) && X_MS1_PIN > -1
  5855. if(code_seen('S')) for(int i=0;i<=4;i++) microstep_mode(i,code_value());
  5856. for(int i=0;i<NUM_AXIS;i++) if(code_seen(axis_codes[i])) microstep_mode(i,(uint8_t)code_value());
  5857. if(code_seen('B')) microstep_mode(4,code_value());
  5858. microstep_readings();
  5859. #endif
  5860. #endif //TMC2130
  5861. }
  5862. break;
  5863. case 351: // M351 Toggle MS1 MS2 pins directly, S# determines MS1 or MS2, X# sets the pin high/low.
  5864. {
  5865. #if defined(X_MS1_PIN) && X_MS1_PIN > -1
  5866. if(code_seen('S')) switch((int)code_value())
  5867. {
  5868. case 1:
  5869. for(int i=0;i<NUM_AXIS;i++) if(code_seen(axis_codes[i])) microstep_ms(i,code_value(),-1);
  5870. if(code_seen('B')) microstep_ms(4,code_value(),-1);
  5871. break;
  5872. case 2:
  5873. for(int i=0;i<NUM_AXIS;i++) if(code_seen(axis_codes[i])) microstep_ms(i,-1,code_value());
  5874. if(code_seen('B')) microstep_ms(4,-1,code_value());
  5875. break;
  5876. }
  5877. microstep_readings();
  5878. #endif
  5879. }
  5880. break;
  5881. case 701: //M701: load filament
  5882. {
  5883. gcode_M701();
  5884. }
  5885. break;
  5886. case 702:
  5887. {
  5888. #ifdef SNMM
  5889. if (code_seen('U')) {
  5890. extr_unload_used(); //unload all filaments which were used in current print
  5891. }
  5892. else if (code_seen('C')) {
  5893. extr_unload(); //unload just current filament
  5894. }
  5895. else {
  5896. extr_unload_all(); //unload all filaments
  5897. }
  5898. #else
  5899. #ifdef PAT9125
  5900. bool old_fsensor_enabled = fsensor_enabled;
  5901. fsensor_enabled = false;
  5902. #endif //PAT9125
  5903. custom_message = true;
  5904. custom_message_type = 2;
  5905. lcd_setstatuspgm(_T(MSG_UNLOADING_FILAMENT));
  5906. // extr_unload2();
  5907. current_position[E_AXIS] -= 45;
  5908. plan_buffer_line(current_position[X_AXIS], current_position[Y_AXIS], current_position[Z_AXIS], current_position[E_AXIS], 5200 / 60, active_extruder);
  5909. st_synchronize();
  5910. current_position[E_AXIS] -= 15;
  5911. plan_buffer_line(current_position[X_AXIS], current_position[Y_AXIS], current_position[Z_AXIS], current_position[E_AXIS], 1000 / 60, active_extruder);
  5912. st_synchronize();
  5913. current_position[E_AXIS] -= 20;
  5914. plan_buffer_line(current_position[X_AXIS], current_position[Y_AXIS], current_position[Z_AXIS], current_position[E_AXIS], 1000 / 60, active_extruder);
  5915. st_synchronize();
  5916. lcd_display_message_fullscreen_P(_T(MSG_PULL_OUT_FILAMENT));
  5917. //disable extruder steppers so filament can be removed
  5918. disable_e0();
  5919. disable_e1();
  5920. disable_e2();
  5921. delay(100);
  5922. WRITE(BEEPER, HIGH);
  5923. uint8_t counterBeep = 0;
  5924. while (!lcd_clicked() && (counterBeep < 50)) {
  5925. if (counterBeep > 5) WRITE(BEEPER, LOW);
  5926. delay_keep_alive(100);
  5927. counterBeep++;
  5928. }
  5929. WRITE(BEEPER, LOW);
  5930. st_synchronize();
  5931. while (lcd_clicked()) delay_keep_alive(100);
  5932. lcd_update_enable(true);
  5933. lcd_setstatuspgm(_T(WELCOME_MSG));
  5934. custom_message = false;
  5935. custom_message_type = 0;
  5936. #ifdef PAT9125
  5937. fsensor_enabled = old_fsensor_enabled;
  5938. #endif //PAT9125
  5939. #endif
  5940. }
  5941. break;
  5942. case 999: // M999: Restart after being stopped
  5943. Stopped = false;
  5944. lcd_reset_alert_level();
  5945. gcode_LastN = Stopped_gcode_LastN;
  5946. FlushSerialRequestResend();
  5947. break;
  5948. default:
  5949. printf_P(PSTR("Unknown M code: %s \n"), cmdbuffer + bufindr + CMDHDRSIZE);
  5950. }
  5951. } // end if(code_seen('M')) (end of M codes)
  5952. else if(code_seen('T'))
  5953. {
  5954. int index;
  5955. st_synchronize();
  5956. for (index = 1; *(strchr_pointer + index) == ' ' || *(strchr_pointer + index) == '\t'; index++);
  5957. if ((*(strchr_pointer + index) < '0' || *(strchr_pointer + index) > '9') && *(strchr_pointer + index) != '?') {
  5958. SERIAL_ECHOLNPGM("Invalid T code.");
  5959. }
  5960. else {
  5961. if (*(strchr_pointer + index) == '?') {
  5962. tmp_extruder = choose_extruder_menu();
  5963. }
  5964. else {
  5965. tmp_extruder = code_value();
  5966. }
  5967. snmm_filaments_used |= (1 << tmp_extruder); //for stop print
  5968. #ifdef SNMM
  5969. #ifdef LIN_ADVANCE
  5970. if (snmm_extruder != tmp_extruder)
  5971. clear_current_adv_vars(); //Check if the selected extruder is not the active one and reset LIN_ADVANCE variables if so.
  5972. #endif
  5973. snmm_extruder = tmp_extruder;
  5974. delay(100);
  5975. disable_e0();
  5976. disable_e1();
  5977. disable_e2();
  5978. pinMode(E_MUX0_PIN, OUTPUT);
  5979. pinMode(E_MUX1_PIN, OUTPUT);
  5980. delay(100);
  5981. SERIAL_ECHO_START;
  5982. SERIAL_ECHO("T:");
  5983. SERIAL_ECHOLN((int)tmp_extruder);
  5984. switch (tmp_extruder) {
  5985. case 1:
  5986. WRITE(E_MUX0_PIN, HIGH);
  5987. WRITE(E_MUX1_PIN, LOW);
  5988. break;
  5989. case 2:
  5990. WRITE(E_MUX0_PIN, LOW);
  5991. WRITE(E_MUX1_PIN, HIGH);
  5992. break;
  5993. case 3:
  5994. WRITE(E_MUX0_PIN, HIGH);
  5995. WRITE(E_MUX1_PIN, HIGH);
  5996. break;
  5997. default:
  5998. WRITE(E_MUX0_PIN, LOW);
  5999. WRITE(E_MUX1_PIN, LOW);
  6000. break;
  6001. }
  6002. delay(100);
  6003. #else
  6004. if (tmp_extruder >= EXTRUDERS) {
  6005. SERIAL_ECHO_START;
  6006. SERIAL_ECHOPGM("T");
  6007. SERIAL_PROTOCOLLN((int)tmp_extruder);
  6008. SERIAL_ECHOLNRPGM(_n("Invalid extruder"));////MSG_INVALID_EXTRUDER c=0 r=0
  6009. }
  6010. else {
  6011. boolean make_move = false;
  6012. if (code_seen('F')) {
  6013. make_move = true;
  6014. next_feedrate = code_value();
  6015. if (next_feedrate > 0.0) {
  6016. feedrate = next_feedrate;
  6017. }
  6018. }
  6019. #if EXTRUDERS > 1
  6020. if (tmp_extruder != active_extruder) {
  6021. // Save current position to return to after applying extruder offset
  6022. memcpy(destination, current_position, sizeof(destination));
  6023. // Offset extruder (only by XY)
  6024. int i;
  6025. for (i = 0; i < 2; i++) {
  6026. current_position[i] = current_position[i] -
  6027. extruder_offset[i][active_extruder] +
  6028. extruder_offset[i][tmp_extruder];
  6029. }
  6030. // Set the new active extruder and position
  6031. active_extruder = tmp_extruder;
  6032. plan_set_position(current_position[X_AXIS], current_position[Y_AXIS], current_position[Z_AXIS], current_position[E_AXIS]);
  6033. // Move to the old position if 'F' was in the parameters
  6034. if (make_move && Stopped == false) {
  6035. prepare_move();
  6036. }
  6037. }
  6038. #endif
  6039. SERIAL_ECHO_START;
  6040. SERIAL_ECHORPGM(_n("Active Extruder: "));////MSG_ACTIVE_EXTRUDER c=0 r=0
  6041. SERIAL_PROTOCOLLN((int)active_extruder);
  6042. }
  6043. #endif
  6044. }
  6045. } // end if(code_seen('T')) (end of T codes)
  6046. #ifdef DEBUG_DCODES
  6047. else if (code_seen('D')) // D codes (debug)
  6048. {
  6049. switch((int)code_value())
  6050. {
  6051. case -1: // D-1 - Endless loop
  6052. dcode__1(); break;
  6053. case 0: // D0 - Reset
  6054. dcode_0(); break;
  6055. case 1: // D1 - Clear EEPROM
  6056. dcode_1(); break;
  6057. case 2: // D2 - Read/Write RAM
  6058. dcode_2(); break;
  6059. case 3: // D3 - Read/Write EEPROM
  6060. dcode_3(); break;
  6061. case 4: // D4 - Read/Write PIN
  6062. dcode_4(); break;
  6063. case 5: // D5 - Read/Write FLASH
  6064. // dcode_5(); break;
  6065. break;
  6066. case 6: // D6 - Read/Write external FLASH
  6067. dcode_6(); break;
  6068. case 7: // D7 - Read/Write Bootloader
  6069. dcode_7(); break;
  6070. case 8: // D8 - Read/Write PINDA
  6071. dcode_8(); break;
  6072. case 9: // D9 - Read/Write ADC
  6073. dcode_9(); break;
  6074. case 10: // D10 - XYZ calibration = OK
  6075. dcode_10(); break;
  6076. #ifdef TMC2130
  6077. case 2130: // D9125 - TMC2130
  6078. dcode_2130(); break;
  6079. #endif //TMC2130
  6080. #ifdef PAT9125
  6081. case 9125: // D9125 - PAT9125
  6082. dcode_9125(); break;
  6083. #endif //PAT9125
  6084. }
  6085. }
  6086. #endif //DEBUG_DCODES
  6087. else
  6088. {
  6089. SERIAL_ECHO_START;
  6090. SERIAL_ECHORPGM(MSG_UNKNOWN_COMMAND);
  6091. SERIAL_ECHO(CMDBUFFER_CURRENT_STRING);
  6092. SERIAL_ECHOLNPGM("\"(2)");
  6093. }
  6094. KEEPALIVE_STATE(NOT_BUSY);
  6095. ClearToSend();
  6096. }
  6097. void FlushSerialRequestResend()
  6098. {
  6099. //char cmdbuffer[bufindr][100]="Resend:";
  6100. MYSERIAL.flush();
  6101. SERIAL_PROTOCOLRPGM(_i("Resend: "));////MSG_RESEND c=0 r=0
  6102. SERIAL_PROTOCOLLN(gcode_LastN + 1);
  6103. previous_millis_cmd = millis();
  6104. SERIAL_PROTOCOLLNRPGM(_T(MSG_OK));
  6105. }
  6106. // Confirm the execution of a command, if sent from a serial line.
  6107. // Execution of a command from a SD card will not be confirmed.
  6108. void ClearToSend()
  6109. {
  6110. previous_millis_cmd = millis();
  6111. if ((CMDBUFFER_CURRENT_TYPE == CMDBUFFER_CURRENT_TYPE_USB) || (CMDBUFFER_CURRENT_TYPE == CMDBUFFER_CURRENT_TYPE_USB_WITH_LINENR))
  6112. SERIAL_PROTOCOLLNRPGM(_T(MSG_OK));
  6113. }
  6114. #if MOTHERBOARD == BOARD_RAMBO_MINI_1_0 || MOTHERBOARD == BOARD_RAMBO_MINI_1_3
  6115. void update_currents() {
  6116. float current_high[3] = DEFAULT_PWM_MOTOR_CURRENT_LOUD;
  6117. float current_low[3] = DEFAULT_PWM_MOTOR_CURRENT;
  6118. float tmp_motor[3];
  6119. //SERIAL_ECHOLNPGM("Currents updated: ");
  6120. if (destination[Z_AXIS] < Z_SILENT) {
  6121. //SERIAL_ECHOLNPGM("LOW");
  6122. for (uint8_t i = 0; i < 3; i++) {
  6123. st_current_set(i, current_low[i]);
  6124. /*MYSERIAL.print(int(i));
  6125. SERIAL_ECHOPGM(": ");
  6126. MYSERIAL.println(current_low[i]);*/
  6127. }
  6128. }
  6129. else if (destination[Z_AXIS] > Z_HIGH_POWER) {
  6130. //SERIAL_ECHOLNPGM("HIGH");
  6131. for (uint8_t i = 0; i < 3; i++) {
  6132. st_current_set(i, current_high[i]);
  6133. /*MYSERIAL.print(int(i));
  6134. SERIAL_ECHOPGM(": ");
  6135. MYSERIAL.println(current_high[i]);*/
  6136. }
  6137. }
  6138. else {
  6139. for (uint8_t i = 0; i < 3; i++) {
  6140. float q = current_low[i] - Z_SILENT*((current_high[i] - current_low[i]) / (Z_HIGH_POWER - Z_SILENT));
  6141. tmp_motor[i] = ((current_high[i] - current_low[i]) / (Z_HIGH_POWER - Z_SILENT))*destination[Z_AXIS] + q;
  6142. st_current_set(i, tmp_motor[i]);
  6143. /*MYSERIAL.print(int(i));
  6144. SERIAL_ECHOPGM(": ");
  6145. MYSERIAL.println(tmp_motor[i]);*/
  6146. }
  6147. }
  6148. }
  6149. #endif //MOTHERBOARD == BOARD_RAMBO_MINI_1_0 || MOTHERBOARD == BOARD_RAMBO_MINI_1_3
  6150. void get_coordinates()
  6151. {
  6152. bool seen[4]={false,false,false,false};
  6153. for(int8_t i=0; i < NUM_AXIS; i++) {
  6154. if(code_seen(axis_codes[i]))
  6155. {
  6156. bool relative = axis_relative_modes[i] || relative_mode;
  6157. destination[i] = (float)code_value();
  6158. if (i == E_AXIS) {
  6159. float emult = extruder_multiplier[active_extruder];
  6160. if (emult != 1.) {
  6161. if (! relative) {
  6162. destination[i] -= current_position[i];
  6163. relative = true;
  6164. }
  6165. destination[i] *= emult;
  6166. }
  6167. }
  6168. if (relative)
  6169. destination[i] += current_position[i];
  6170. seen[i]=true;
  6171. #if MOTHERBOARD == BOARD_RAMBO_MINI_1_0 || MOTHERBOARD == BOARD_RAMBO_MINI_1_3
  6172. if (i == Z_AXIS && SilentModeMenu == SILENT_MODE_AUTO) update_currents();
  6173. #endif //MOTHERBOARD == BOARD_RAMBO_MINI_1_0 || MOTHERBOARD == BOARD_RAMBO_MINI_1_3
  6174. }
  6175. else destination[i] = current_position[i]; //Are these else lines really needed?
  6176. }
  6177. if(code_seen('F')) {
  6178. next_feedrate = code_value();
  6179. #ifdef MAX_SILENT_FEEDRATE
  6180. if (tmc2130_mode == TMC2130_MODE_SILENT)
  6181. if (next_feedrate > MAX_SILENT_FEEDRATE) next_feedrate = MAX_SILENT_FEEDRATE;
  6182. #endif //MAX_SILENT_FEEDRATE
  6183. if(next_feedrate > 0.0) feedrate = next_feedrate;
  6184. if (!seen[0] && !seen[1] && !seen[2] && seen[3])
  6185. {
  6186. // float e_max_speed =
  6187. // printf_P(PSTR("E MOVE speed %7.3f\n"), feedrate / 60)
  6188. }
  6189. }
  6190. }
  6191. void get_arc_coordinates()
  6192. {
  6193. #ifdef SF_ARC_FIX
  6194. bool relative_mode_backup = relative_mode;
  6195. relative_mode = true;
  6196. #endif
  6197. get_coordinates();
  6198. #ifdef SF_ARC_FIX
  6199. relative_mode=relative_mode_backup;
  6200. #endif
  6201. if(code_seen('I')) {
  6202. offset[0] = code_value();
  6203. }
  6204. else {
  6205. offset[0] = 0.0;
  6206. }
  6207. if(code_seen('J')) {
  6208. offset[1] = code_value();
  6209. }
  6210. else {
  6211. offset[1] = 0.0;
  6212. }
  6213. }
  6214. void clamp_to_software_endstops(float target[3])
  6215. {
  6216. #ifdef DEBUG_DISABLE_SWLIMITS
  6217. return;
  6218. #endif //DEBUG_DISABLE_SWLIMITS
  6219. world2machine_clamp(target[0], target[1]);
  6220. // Clamp the Z coordinate.
  6221. if (min_software_endstops) {
  6222. float negative_z_offset = 0;
  6223. #ifdef ENABLE_AUTO_BED_LEVELING
  6224. if (Z_PROBE_OFFSET_FROM_EXTRUDER < 0) negative_z_offset = negative_z_offset + Z_PROBE_OFFSET_FROM_EXTRUDER;
  6225. if (add_homing[Z_AXIS] < 0) negative_z_offset = negative_z_offset + add_homing[Z_AXIS];
  6226. #endif
  6227. if (target[Z_AXIS] < min_pos[Z_AXIS]+negative_z_offset) target[Z_AXIS] = min_pos[Z_AXIS]+negative_z_offset;
  6228. }
  6229. if (max_software_endstops) {
  6230. if (target[Z_AXIS] > max_pos[Z_AXIS]) target[Z_AXIS] = max_pos[Z_AXIS];
  6231. }
  6232. }
  6233. #ifdef MESH_BED_LEVELING
  6234. 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) {
  6235. float dx = x - current_position[X_AXIS];
  6236. float dy = y - current_position[Y_AXIS];
  6237. float dz = z - current_position[Z_AXIS];
  6238. int n_segments = 0;
  6239. if (mbl.active) {
  6240. float len = abs(dx) + abs(dy);
  6241. if (len > 0)
  6242. // Split to 3cm segments or shorter.
  6243. n_segments = int(ceil(len / 30.f));
  6244. }
  6245. if (n_segments > 1) {
  6246. float de = e - current_position[E_AXIS];
  6247. for (int i = 1; i < n_segments; ++ i) {
  6248. float t = float(i) / float(n_segments);
  6249. plan_buffer_line(
  6250. current_position[X_AXIS] + t * dx,
  6251. current_position[Y_AXIS] + t * dy,
  6252. current_position[Z_AXIS] + t * dz,
  6253. current_position[E_AXIS] + t * de,
  6254. feed_rate, extruder);
  6255. }
  6256. }
  6257. // The rest of the path.
  6258. plan_buffer_line(x, y, z, e, feed_rate, extruder);
  6259. current_position[X_AXIS] = x;
  6260. current_position[Y_AXIS] = y;
  6261. current_position[Z_AXIS] = z;
  6262. current_position[E_AXIS] = e;
  6263. }
  6264. #endif // MESH_BED_LEVELING
  6265. void prepare_move()
  6266. {
  6267. clamp_to_software_endstops(destination);
  6268. previous_millis_cmd = millis();
  6269. // Do not use feedmultiply for E or Z only moves
  6270. if( (current_position[X_AXIS] == destination [X_AXIS]) && (current_position[Y_AXIS] == destination [Y_AXIS])) {
  6271. plan_buffer_line(destination[X_AXIS], destination[Y_AXIS], destination[Z_AXIS], destination[E_AXIS], feedrate/60, active_extruder);
  6272. }
  6273. else {
  6274. #ifdef MESH_BED_LEVELING
  6275. 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);
  6276. #else
  6277. plan_buffer_line(destination[X_AXIS], destination[Y_AXIS], destination[Z_AXIS], destination[E_AXIS], feedrate*feedmultiply*(1./(60.f*100.f)), active_extruder);
  6278. #endif
  6279. }
  6280. for(int8_t i=0; i < NUM_AXIS; i++) {
  6281. current_position[i] = destination[i];
  6282. }
  6283. }
  6284. void prepare_arc_move(char isclockwise) {
  6285. float r = hypot(offset[X_AXIS], offset[Y_AXIS]); // Compute arc radius for mc_arc
  6286. // Trace the arc
  6287. mc_arc(current_position, destination, offset, X_AXIS, Y_AXIS, Z_AXIS, feedrate*feedmultiply/60/100.0, r, isclockwise, active_extruder);
  6288. // As far as the parser is concerned, the position is now == target. In reality the
  6289. // motion control system might still be processing the action and the real tool position
  6290. // in any intermediate location.
  6291. for(int8_t i=0; i < NUM_AXIS; i++) {
  6292. current_position[i] = destination[i];
  6293. }
  6294. previous_millis_cmd = millis();
  6295. }
  6296. #if defined(CONTROLLERFAN_PIN) && CONTROLLERFAN_PIN > -1
  6297. #if defined(FAN_PIN)
  6298. #if CONTROLLERFAN_PIN == FAN_PIN
  6299. #error "You cannot set CONTROLLERFAN_PIN equal to FAN_PIN"
  6300. #endif
  6301. #endif
  6302. unsigned long lastMotor = 0; //Save the time for when a motor was turned on last
  6303. unsigned long lastMotorCheck = 0;
  6304. void controllerFan()
  6305. {
  6306. if ((millis() - lastMotorCheck) >= 2500) //Not a time critical function, so we only check every 2500ms
  6307. {
  6308. lastMotorCheck = millis();
  6309. if(!READ(X_ENABLE_PIN) || !READ(Y_ENABLE_PIN) || !READ(Z_ENABLE_PIN) || (soft_pwm_bed > 0)
  6310. #if EXTRUDERS > 2
  6311. || !READ(E2_ENABLE_PIN)
  6312. #endif
  6313. #if EXTRUDER > 1
  6314. #if defined(X2_ENABLE_PIN) && X2_ENABLE_PIN > -1
  6315. || !READ(X2_ENABLE_PIN)
  6316. #endif
  6317. || !READ(E1_ENABLE_PIN)
  6318. #endif
  6319. || !READ(E0_ENABLE_PIN)) //If any of the drivers are enabled...
  6320. {
  6321. lastMotor = millis(); //... set time to NOW so the fan will turn on
  6322. }
  6323. if ((millis() - lastMotor) >= (CONTROLLERFAN_SECS*1000UL) || lastMotor == 0) //If the last time any driver was enabled, is longer since than CONTROLLERSEC...
  6324. {
  6325. digitalWrite(CONTROLLERFAN_PIN, 0);
  6326. analogWrite(CONTROLLERFAN_PIN, 0);
  6327. }
  6328. else
  6329. {
  6330. // allows digital or PWM fan output to be used (see M42 handling)
  6331. digitalWrite(CONTROLLERFAN_PIN, CONTROLLERFAN_SPEED);
  6332. analogWrite(CONTROLLERFAN_PIN, CONTROLLERFAN_SPEED);
  6333. }
  6334. }
  6335. }
  6336. #endif
  6337. #ifdef TEMP_STAT_LEDS
  6338. static bool blue_led = false;
  6339. static bool red_led = false;
  6340. static uint32_t stat_update = 0;
  6341. void handle_status_leds(void) {
  6342. float max_temp = 0.0;
  6343. if(millis() > stat_update) {
  6344. stat_update += 500; // Update every 0.5s
  6345. for (int8_t cur_extruder = 0; cur_extruder < EXTRUDERS; ++cur_extruder) {
  6346. max_temp = max(max_temp, degHotend(cur_extruder));
  6347. max_temp = max(max_temp, degTargetHotend(cur_extruder));
  6348. }
  6349. #if defined(TEMP_BED_PIN) && TEMP_BED_PIN > -1
  6350. max_temp = max(max_temp, degTargetBed());
  6351. max_temp = max(max_temp, degBed());
  6352. #endif
  6353. if((max_temp > 55.0) && (red_led == false)) {
  6354. digitalWrite(STAT_LED_RED, 1);
  6355. digitalWrite(STAT_LED_BLUE, 0);
  6356. red_led = true;
  6357. blue_led = false;
  6358. }
  6359. if((max_temp < 54.0) && (blue_led == false)) {
  6360. digitalWrite(STAT_LED_RED, 0);
  6361. digitalWrite(STAT_LED_BLUE, 1);
  6362. red_led = false;
  6363. blue_led = true;
  6364. }
  6365. }
  6366. }
  6367. #endif
  6368. #ifdef SAFETYTIMER
  6369. /**
  6370. * @brief Turn off heating after 30 minutes of inactivity
  6371. *
  6372. * Full screen blocking notification message is shown after heater turning off.
  6373. * Paused print is not considered inactivity, as nozzle is cooled anyway and bed cooling would
  6374. * damage print.
  6375. */
  6376. static void handleSafetyTimer()
  6377. {
  6378. #if (EXTRUDERS > 1)
  6379. #error Implemented only for one extruder.
  6380. #endif //(EXTRUDERS > 1)
  6381. if ((PRINTER_ACTIVE) || (!degTargetBed() && !degTargetHotend(0)))
  6382. {
  6383. safetyTimer.stop();
  6384. }
  6385. else if ((degTargetBed() || degTargetHotend(0)) && (!safetyTimer.running()))
  6386. {
  6387. safetyTimer.start();
  6388. }
  6389. else if (safetyTimer.expired(1800000ul)) //30 min
  6390. {
  6391. setTargetBed(0);
  6392. setTargetHotend(0, 0);
  6393. lcd_show_fullscreen_message_and_wait_P(_i("Heating disabled by safety timer."));////MSG_BED_HEATING_SAFETY_DISABLED c=0 r=0
  6394. }
  6395. }
  6396. #endif //SAFETYTIMER
  6397. void manage_inactivity(bool ignore_stepper_queue/*=false*/) //default argument set in Marlin.h
  6398. {
  6399. #ifdef PAT9125
  6400. if (fsensor_enabled && filament_autoload_enabled && !fsensor_M600 && !moves_planned() && !IS_SD_PRINTING && !is_usb_printing && (lcd_commands_type != LCD_COMMAND_V2_CAL))
  6401. {
  6402. if (fsensor_autoload_enabled)
  6403. {
  6404. if (fsensor_check_autoload())
  6405. {
  6406. if (degHotend0() > EXTRUDE_MINTEMP)
  6407. {
  6408. fsensor_autoload_check_stop();
  6409. tone(BEEPER, 1000);
  6410. delay_keep_alive(50);
  6411. noTone(BEEPER);
  6412. loading_flag = true;
  6413. enquecommand_front_P((PSTR("M701")));
  6414. }
  6415. else
  6416. {
  6417. lcd_update_enable(false);
  6418. lcd_implementation_clear();
  6419. lcd.setCursor(0, 0);
  6420. lcd_printPGM(_T(MSG_ERROR));
  6421. lcd.setCursor(0, 2);
  6422. lcd_printPGM(_T(MSG_PREHEAT_NOZZLE));
  6423. delay(2000);
  6424. lcd_implementation_clear();
  6425. lcd_update_enable(true);
  6426. }
  6427. }
  6428. }
  6429. else
  6430. fsensor_autoload_check_start();
  6431. }
  6432. else
  6433. if (fsensor_autoload_enabled)
  6434. fsensor_autoload_check_stop();
  6435. #endif //PAT9125
  6436. #ifdef SAFETYTIMER
  6437. handleSafetyTimer();
  6438. #endif //SAFETYTIMER
  6439. #if defined(KILL_PIN) && KILL_PIN > -1
  6440. static int killCount = 0; // make the inactivity button a bit less responsive
  6441. const int KILL_DELAY = 10000;
  6442. #endif
  6443. if(buflen < (BUFSIZE-1)){
  6444. get_command();
  6445. }
  6446. if( (millis() - previous_millis_cmd) > max_inactive_time )
  6447. if(max_inactive_time)
  6448. kill("", 4);
  6449. if(stepper_inactive_time) {
  6450. if( (millis() - previous_millis_cmd) > stepper_inactive_time )
  6451. {
  6452. if(blocks_queued() == false && ignore_stepper_queue == false) {
  6453. disable_x();
  6454. // SERIAL_ECHOLNPGM("manage_inactivity - disable Y");
  6455. disable_y();
  6456. disable_z();
  6457. disable_e0();
  6458. disable_e1();
  6459. disable_e2();
  6460. }
  6461. }
  6462. }
  6463. #ifdef CHDK //Check if pin should be set to LOW after M240 set it to HIGH
  6464. if (chdkActive && (millis() - chdkHigh > CHDK_DELAY))
  6465. {
  6466. chdkActive = false;
  6467. WRITE(CHDK, LOW);
  6468. }
  6469. #endif
  6470. #if defined(KILL_PIN) && KILL_PIN > -1
  6471. // Check if the kill button was pressed and wait just in case it was an accidental
  6472. // key kill key press
  6473. // -------------------------------------------------------------------------------
  6474. if( 0 == READ(KILL_PIN) )
  6475. {
  6476. killCount++;
  6477. }
  6478. else if (killCount > 0)
  6479. {
  6480. killCount--;
  6481. }
  6482. // Exceeded threshold and we can confirm that it was not accidental
  6483. // KILL the machine
  6484. // ----------------------------------------------------------------
  6485. if ( killCount >= KILL_DELAY)
  6486. {
  6487. kill("", 5);
  6488. }
  6489. #endif
  6490. #if defined(CONTROLLERFAN_PIN) && CONTROLLERFAN_PIN > -1
  6491. controllerFan(); //Check if fan should be turned on to cool stepper drivers down
  6492. #endif
  6493. #ifdef EXTRUDER_RUNOUT_PREVENT
  6494. if( (millis() - previous_millis_cmd) > EXTRUDER_RUNOUT_SECONDS*1000 )
  6495. if(degHotend(active_extruder)>EXTRUDER_RUNOUT_MINTEMP)
  6496. {
  6497. bool oldstatus=READ(E0_ENABLE_PIN);
  6498. enable_e0();
  6499. float oldepos=current_position[E_AXIS];
  6500. float oldedes=destination[E_AXIS];
  6501. plan_buffer_line(destination[X_AXIS], destination[Y_AXIS], destination[Z_AXIS],
  6502. destination[E_AXIS]+EXTRUDER_RUNOUT_EXTRUDE*EXTRUDER_RUNOUT_ESTEPS/axis_steps_per_unit[E_AXIS],
  6503. EXTRUDER_RUNOUT_SPEED/60.*EXTRUDER_RUNOUT_ESTEPS/axis_steps_per_unit[E_AXIS], active_extruder);
  6504. current_position[E_AXIS]=oldepos;
  6505. destination[E_AXIS]=oldedes;
  6506. plan_set_e_position(oldepos);
  6507. previous_millis_cmd=millis();
  6508. st_synchronize();
  6509. WRITE(E0_ENABLE_PIN,oldstatus);
  6510. }
  6511. #endif
  6512. #ifdef TEMP_STAT_LEDS
  6513. handle_status_leds();
  6514. #endif
  6515. check_axes_activity();
  6516. }
  6517. void kill(const char *full_screen_message, unsigned char id)
  6518. {
  6519. SERIAL_ECHOPGM("KILL: ");
  6520. MYSERIAL.println(int(id));
  6521. //return;
  6522. cli(); // Stop interrupts
  6523. disable_heater();
  6524. disable_x();
  6525. // SERIAL_ECHOLNPGM("kill - disable Y");
  6526. disable_y();
  6527. disable_z();
  6528. disable_e0();
  6529. disable_e1();
  6530. disable_e2();
  6531. #if defined(PS_ON_PIN) && PS_ON_PIN > -1
  6532. pinMode(PS_ON_PIN,INPUT);
  6533. #endif
  6534. SERIAL_ERROR_START;
  6535. SERIAL_ERRORLNRPGM(_i("Printer halted. kill() called!"));////MSG_ERR_KILLED c=0 r=0
  6536. if (full_screen_message != NULL) {
  6537. SERIAL_ERRORLNRPGM(full_screen_message);
  6538. lcd_display_message_fullscreen_P(full_screen_message);
  6539. } else {
  6540. LCD_ALERTMESSAGERPGM(_i("KILLED. "));////MSG_KILLED c=0 r=0
  6541. }
  6542. // FMC small patch to update the LCD before ending
  6543. sei(); // enable interrupts
  6544. for ( int i=5; i--; lcd_update())
  6545. {
  6546. delay(200);
  6547. }
  6548. cli(); // disable interrupts
  6549. suicide();
  6550. while(1)
  6551. {
  6552. #ifdef WATCHDOG
  6553. wdt_reset();
  6554. #endif //WATCHDOG
  6555. /* Intentionally left empty */
  6556. } // Wait for reset
  6557. }
  6558. void Stop()
  6559. {
  6560. disable_heater();
  6561. if(Stopped == false) {
  6562. Stopped = true;
  6563. Stopped_gcode_LastN = gcode_LastN; // Save last g_code for restart
  6564. SERIAL_ERROR_START;
  6565. SERIAL_ERRORLNRPGM(_T(MSG_ERR_STOPPED));
  6566. LCD_MESSAGERPGM(_T(MSG_STOPPED));
  6567. }
  6568. }
  6569. bool IsStopped() { return Stopped; };
  6570. #ifdef FAST_PWM_FAN
  6571. void setPwmFrequency(uint8_t pin, int val)
  6572. {
  6573. val &= 0x07;
  6574. switch(digitalPinToTimer(pin))
  6575. {
  6576. #if defined(TCCR0A)
  6577. case TIMER0A:
  6578. case TIMER0B:
  6579. // TCCR0B &= ~(_BV(CS00) | _BV(CS01) | _BV(CS02));
  6580. // TCCR0B |= val;
  6581. break;
  6582. #endif
  6583. #if defined(TCCR1A)
  6584. case TIMER1A:
  6585. case TIMER1B:
  6586. // TCCR1B &= ~(_BV(CS10) | _BV(CS11) | _BV(CS12));
  6587. // TCCR1B |= val;
  6588. break;
  6589. #endif
  6590. #if defined(TCCR2)
  6591. case TIMER2:
  6592. case TIMER2:
  6593. TCCR2 &= ~(_BV(CS10) | _BV(CS11) | _BV(CS12));
  6594. TCCR2 |= val;
  6595. break;
  6596. #endif
  6597. #if defined(TCCR2A)
  6598. case TIMER2A:
  6599. case TIMER2B:
  6600. TCCR2B &= ~(_BV(CS20) | _BV(CS21) | _BV(CS22));
  6601. TCCR2B |= val;
  6602. break;
  6603. #endif
  6604. #if defined(TCCR3A)
  6605. case TIMER3A:
  6606. case TIMER3B:
  6607. case TIMER3C:
  6608. TCCR3B &= ~(_BV(CS30) | _BV(CS31) | _BV(CS32));
  6609. TCCR3B |= val;
  6610. break;
  6611. #endif
  6612. #if defined(TCCR4A)
  6613. case TIMER4A:
  6614. case TIMER4B:
  6615. case TIMER4C:
  6616. TCCR4B &= ~(_BV(CS40) | _BV(CS41) | _BV(CS42));
  6617. TCCR4B |= val;
  6618. break;
  6619. #endif
  6620. #if defined(TCCR5A)
  6621. case TIMER5A:
  6622. case TIMER5B:
  6623. case TIMER5C:
  6624. TCCR5B &= ~(_BV(CS50) | _BV(CS51) | _BV(CS52));
  6625. TCCR5B |= val;
  6626. break;
  6627. #endif
  6628. }
  6629. }
  6630. #endif //FAST_PWM_FAN
  6631. bool setTargetedHotend(int code){
  6632. tmp_extruder = active_extruder;
  6633. if(code_seen('T')) {
  6634. tmp_extruder = code_value();
  6635. if(tmp_extruder >= EXTRUDERS) {
  6636. SERIAL_ECHO_START;
  6637. switch(code){
  6638. case 104:
  6639. SERIAL_ECHORPGM(_i("M104 Invalid extruder "));////MSG_M104_INVALID_EXTRUDER c=0 r=0
  6640. break;
  6641. case 105:
  6642. SERIAL_ECHO(_i("M105 Invalid extruder "));////MSG_M105_INVALID_EXTRUDER c=0 r=0
  6643. break;
  6644. case 109:
  6645. SERIAL_ECHO(_i("M109 Invalid extruder "));////MSG_M109_INVALID_EXTRUDER c=0 r=0
  6646. break;
  6647. case 218:
  6648. SERIAL_ECHO(_i("M218 Invalid extruder "));////MSG_M218_INVALID_EXTRUDER c=0 r=0
  6649. break;
  6650. case 221:
  6651. SERIAL_ECHO(_i("M221 Invalid extruder "));////MSG_M221_INVALID_EXTRUDER c=0 r=0
  6652. break;
  6653. }
  6654. SERIAL_PROTOCOLLN((int)tmp_extruder);
  6655. return true;
  6656. }
  6657. }
  6658. return false;
  6659. }
  6660. void save_statistics(unsigned long _total_filament_used, unsigned long _total_print_time) //_total_filament_used unit: mm/100; print time in s
  6661. {
  6662. 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)
  6663. {
  6664. eeprom_update_dword((uint32_t *)EEPROM_TOTALTIME, 0);
  6665. eeprom_update_dword((uint32_t *)EEPROM_FILAMENTUSED, 0);
  6666. }
  6667. unsigned long _previous_filament = eeprom_read_dword((uint32_t *)EEPROM_FILAMENTUSED); //_previous_filament unit: cm
  6668. unsigned long _previous_time = eeprom_read_dword((uint32_t *)EEPROM_TOTALTIME); //_previous_time unit: min
  6669. eeprom_update_dword((uint32_t *)EEPROM_TOTALTIME, _previous_time + (_total_print_time/60)); //EEPROM_TOTALTIME unit: min
  6670. eeprom_update_dword((uint32_t *)EEPROM_FILAMENTUSED, _previous_filament + (_total_filament_used / 1000));
  6671. total_filament_used = 0;
  6672. }
  6673. float calculate_extruder_multiplier(float diameter) {
  6674. float out = 1.f;
  6675. if (volumetric_enabled && diameter > 0.f) {
  6676. float area = M_PI * diameter * diameter * 0.25;
  6677. out = 1.f / area;
  6678. }
  6679. if (extrudemultiply != 100)
  6680. out *= float(extrudemultiply) * 0.01f;
  6681. return out;
  6682. }
  6683. void calculate_extruder_multipliers() {
  6684. extruder_multiplier[0] = calculate_extruder_multiplier(filament_size[0]);
  6685. #if EXTRUDERS > 1
  6686. extruder_multiplier[1] = calculate_extruder_multiplier(filament_size[1]);
  6687. #if EXTRUDERS > 2
  6688. extruder_multiplier[2] = calculate_extruder_multiplier(filament_size[2]);
  6689. #endif
  6690. #endif
  6691. }
  6692. void delay_keep_alive(unsigned int ms)
  6693. {
  6694. for (;;) {
  6695. manage_heater();
  6696. // Manage inactivity, but don't disable steppers on timeout.
  6697. manage_inactivity(true);
  6698. lcd_update();
  6699. if (ms == 0)
  6700. break;
  6701. else if (ms >= 50) {
  6702. delay(50);
  6703. ms -= 50;
  6704. } else {
  6705. delay(ms);
  6706. ms = 0;
  6707. }
  6708. }
  6709. }
  6710. void wait_for_heater(long codenum) {
  6711. #ifdef TEMP_RESIDENCY_TIME
  6712. long residencyStart;
  6713. residencyStart = -1;
  6714. /* continue to loop until we have reached the target temp
  6715. _and_ until TEMP_RESIDENCY_TIME hasn't passed since we reached it */
  6716. while ((!cancel_heatup) && ((residencyStart == -1) ||
  6717. (residencyStart >= 0 && (((unsigned int)(millis() - residencyStart)) < (TEMP_RESIDENCY_TIME * 1000UL))))) {
  6718. #else
  6719. while (target_direction ? (isHeatingHotend(tmp_extruder)) : (isCoolingHotend(tmp_extruder) && (CooldownNoWait == false))) {
  6720. #endif //TEMP_RESIDENCY_TIME
  6721. if ((millis() - codenum) > 1000UL)
  6722. { //Print Temp Reading and remaining time every 1 second while heating up/cooling down
  6723. if (!farm_mode) {
  6724. SERIAL_PROTOCOLPGM("T:");
  6725. SERIAL_PROTOCOL_F(degHotend(tmp_extruder), 1);
  6726. SERIAL_PROTOCOLPGM(" E:");
  6727. SERIAL_PROTOCOL((int)tmp_extruder);
  6728. #ifdef TEMP_RESIDENCY_TIME
  6729. SERIAL_PROTOCOLPGM(" W:");
  6730. if (residencyStart > -1)
  6731. {
  6732. codenum = ((TEMP_RESIDENCY_TIME * 1000UL) - (millis() - residencyStart)) / 1000UL;
  6733. SERIAL_PROTOCOLLN(codenum);
  6734. }
  6735. else
  6736. {
  6737. SERIAL_PROTOCOLLN("?");
  6738. }
  6739. }
  6740. #else
  6741. SERIAL_PROTOCOLLN("");
  6742. #endif
  6743. codenum = millis();
  6744. }
  6745. manage_heater();
  6746. manage_inactivity();
  6747. lcd_update();
  6748. #ifdef TEMP_RESIDENCY_TIME
  6749. /* start/restart the TEMP_RESIDENCY_TIME timer whenever we reach target temp for the first time
  6750. or when current temp falls outside the hysteresis after target temp was reached */
  6751. if ((residencyStart == -1 && target_direction && (degHotend(tmp_extruder) >= (degTargetHotend(tmp_extruder) - TEMP_WINDOW))) ||
  6752. (residencyStart == -1 && !target_direction && (degHotend(tmp_extruder) <= (degTargetHotend(tmp_extruder) + TEMP_WINDOW))) ||
  6753. (residencyStart > -1 && labs(degHotend(tmp_extruder) - degTargetHotend(tmp_extruder)) > TEMP_HYSTERESIS))
  6754. {
  6755. residencyStart = millis();
  6756. }
  6757. #endif //TEMP_RESIDENCY_TIME
  6758. }
  6759. }
  6760. void check_babystep() {
  6761. int babystep_z;
  6762. EEPROM_read_B(EEPROM_BABYSTEP_Z, &babystep_z);
  6763. if ((babystep_z < Z_BABYSTEP_MIN) || (babystep_z > Z_BABYSTEP_MAX)) {
  6764. babystep_z = 0; //if babystep value is out of min max range, set it to 0
  6765. SERIAL_ECHOLNPGM("Z live adjust out of range. Setting to 0");
  6766. EEPROM_save_B(EEPROM_BABYSTEP_Z, &babystep_z);
  6767. lcd_show_fullscreen_message_and_wait_P(PSTR("Z live adjust out of range. Setting to 0. Click to continue."));
  6768. lcd_update_enable(true);
  6769. }
  6770. }
  6771. #ifdef DIS
  6772. void d_setup()
  6773. {
  6774. pinMode(D_DATACLOCK, INPUT_PULLUP);
  6775. pinMode(D_DATA, INPUT_PULLUP);
  6776. pinMode(D_REQUIRE, OUTPUT);
  6777. digitalWrite(D_REQUIRE, HIGH);
  6778. }
  6779. float d_ReadData()
  6780. {
  6781. int digit[13];
  6782. String mergeOutput;
  6783. float output;
  6784. digitalWrite(D_REQUIRE, HIGH);
  6785. for (int i = 0; i<13; i++)
  6786. {
  6787. for (int j = 0; j < 4; j++)
  6788. {
  6789. while (digitalRead(D_DATACLOCK) == LOW) {}
  6790. while (digitalRead(D_DATACLOCK) == HIGH) {}
  6791. bitWrite(digit[i], j, digitalRead(D_DATA));
  6792. }
  6793. }
  6794. digitalWrite(D_REQUIRE, LOW);
  6795. mergeOutput = "";
  6796. output = 0;
  6797. for (int r = 5; r <= 10; r++) //Merge digits
  6798. {
  6799. mergeOutput += digit[r];
  6800. }
  6801. output = mergeOutput.toFloat();
  6802. if (digit[4] == 8) //Handle sign
  6803. {
  6804. output *= -1;
  6805. }
  6806. for (int i = digit[11]; i > 0; i--) //Handle floating point
  6807. {
  6808. output /= 10;
  6809. }
  6810. return output;
  6811. }
  6812. void bed_analysis(float x_dimension, float y_dimension, int x_points_num, int y_points_num, float shift_x, float shift_y) {
  6813. int t1 = 0;
  6814. int t_delay = 0;
  6815. int digit[13];
  6816. int m;
  6817. char str[3];
  6818. //String mergeOutput;
  6819. char mergeOutput[15];
  6820. float output;
  6821. int mesh_point = 0; //index number of calibration point
  6822. 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
  6823. float bed_zero_ref_y = (-0.6f + Y_PROBE_OFFSET_FROM_EXTRUDER);
  6824. float mesh_home_z_search = 4;
  6825. float row[x_points_num];
  6826. int ix = 0;
  6827. int iy = 0;
  6828. char* filename_wldsd = "wldsd.txt";
  6829. char data_wldsd[70];
  6830. char numb_wldsd[10];
  6831. d_setup();
  6832. if (!(axis_known_position[X_AXIS] && axis_known_position[Y_AXIS] && axis_known_position[Z_AXIS])) {
  6833. // We don't know where we are! HOME!
  6834. // Push the commands to the front of the message queue in the reverse order!
  6835. // There shall be always enough space reserved for these commands.
  6836. repeatcommand_front(); // repeat G80 with all its parameters
  6837. enquecommand_front_P((PSTR("G28 W0")));
  6838. enquecommand_front_P((PSTR("G1 Z5")));
  6839. return;
  6840. }
  6841. bool custom_message_old = custom_message;
  6842. unsigned int custom_message_type_old = custom_message_type;
  6843. unsigned int custom_message_state_old = custom_message_state;
  6844. custom_message = true;
  6845. custom_message_type = 1;
  6846. custom_message_state = (x_points_num * y_points_num) + 10;
  6847. lcd_update(1);
  6848. mbl.reset();
  6849. babystep_undo();
  6850. card.openFile(filename_wldsd, false);
  6851. current_position[Z_AXIS] = mesh_home_z_search;
  6852. 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);
  6853. int XY_AXIS_FEEDRATE = homing_feedrate[X_AXIS] / 20;
  6854. int Z_PROBE_FEEDRATE = homing_feedrate[Z_AXIS] / 60;
  6855. int Z_LIFT_FEEDRATE = homing_feedrate[Z_AXIS] / 40;
  6856. setup_for_endstop_move(false);
  6857. SERIAL_PROTOCOLPGM("Num X,Y: ");
  6858. SERIAL_PROTOCOL(x_points_num);
  6859. SERIAL_PROTOCOLPGM(",");
  6860. SERIAL_PROTOCOL(y_points_num);
  6861. SERIAL_PROTOCOLPGM("\nZ search height: ");
  6862. SERIAL_PROTOCOL(mesh_home_z_search);
  6863. SERIAL_PROTOCOLPGM("\nDimension X,Y: ");
  6864. SERIAL_PROTOCOL(x_dimension);
  6865. SERIAL_PROTOCOLPGM(",");
  6866. SERIAL_PROTOCOL(y_dimension);
  6867. SERIAL_PROTOCOLLNPGM("\nMeasured points:");
  6868. while (mesh_point != x_points_num * y_points_num) {
  6869. ix = mesh_point % x_points_num; // from 0 to MESH_NUM_X_POINTS - 1
  6870. iy = mesh_point / x_points_num;
  6871. if (iy & 1) ix = (x_points_num - 1) - ix; // Zig zag
  6872. float z0 = 0.f;
  6873. current_position[Z_AXIS] = mesh_home_z_search;
  6874. plan_buffer_line(current_position[X_AXIS], current_position[Y_AXIS], current_position[Z_AXIS], current_position[E_AXIS], Z_LIFT_FEEDRATE, active_extruder);
  6875. st_synchronize();
  6876. current_position[X_AXIS] = 13.f + ix * (x_dimension / (x_points_num - 1)) - bed_zero_ref_x + shift_x;
  6877. current_position[Y_AXIS] = 6.4f + iy * (y_dimension / (y_points_num - 1)) - bed_zero_ref_y + shift_y;
  6878. plan_buffer_line(current_position[X_AXIS], current_position[Y_AXIS], current_position[Z_AXIS], current_position[E_AXIS], XY_AXIS_FEEDRATE, active_extruder);
  6879. st_synchronize();
  6880. 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
  6881. break;
  6882. card.closefile();
  6883. }
  6884. //memset(numb_wldsd, 0, sizeof(numb_wldsd));
  6885. //dtostrf(d_ReadData(), 8, 5, numb_wldsd);
  6886. //strcat(data_wldsd, numb_wldsd);
  6887. //MYSERIAL.println(data_wldsd);
  6888. //delay(1000);
  6889. //delay(3000);
  6890. //t1 = millis();
  6891. //while (digitalRead(D_DATACLOCK) == LOW) {}
  6892. //while (digitalRead(D_DATACLOCK) == HIGH) {}
  6893. memset(digit, 0, sizeof(digit));
  6894. //cli();
  6895. digitalWrite(D_REQUIRE, LOW);
  6896. for (int i = 0; i<13; i++)
  6897. {
  6898. //t1 = millis();
  6899. for (int j = 0; j < 4; j++)
  6900. {
  6901. while (digitalRead(D_DATACLOCK) == LOW) {}
  6902. while (digitalRead(D_DATACLOCK) == HIGH) {}
  6903. bitWrite(digit[i], j, digitalRead(D_DATA));
  6904. }
  6905. //t_delay = (millis() - t1);
  6906. //SERIAL_PROTOCOLPGM(" ");
  6907. //SERIAL_PROTOCOL_F(t_delay, 5);
  6908. //SERIAL_PROTOCOLPGM(" ");
  6909. }
  6910. //sei();
  6911. digitalWrite(D_REQUIRE, HIGH);
  6912. mergeOutput[0] = '\0';
  6913. output = 0;
  6914. for (int r = 5; r <= 10; r++) //Merge digits
  6915. {
  6916. sprintf(str, "%d", digit[r]);
  6917. strcat(mergeOutput, str);
  6918. }
  6919. output = atof(mergeOutput);
  6920. if (digit[4] == 8) //Handle sign
  6921. {
  6922. output *= -1;
  6923. }
  6924. for (int i = digit[11]; i > 0; i--) //Handle floating point
  6925. {
  6926. output *= 0.1;
  6927. }
  6928. //output = d_ReadData();
  6929. //row[ix] = current_position[Z_AXIS];
  6930. memset(data_wldsd, 0, sizeof(data_wldsd));
  6931. for (int i = 0; i <3; i++) {
  6932. memset(numb_wldsd, 0, sizeof(numb_wldsd));
  6933. dtostrf(current_position[i], 8, 5, numb_wldsd);
  6934. strcat(data_wldsd, numb_wldsd);
  6935. strcat(data_wldsd, ";");
  6936. }
  6937. memset(numb_wldsd, 0, sizeof(numb_wldsd));
  6938. dtostrf(output, 8, 5, numb_wldsd);
  6939. strcat(data_wldsd, numb_wldsd);
  6940. //strcat(data_wldsd, ";");
  6941. card.write_command(data_wldsd);
  6942. //row[ix] = d_ReadData();
  6943. row[ix] = output; // current_position[Z_AXIS];
  6944. if (iy % 2 == 1 ? ix == 0 : ix == x_points_num - 1) {
  6945. for (int i = 0; i < x_points_num; i++) {
  6946. SERIAL_PROTOCOLPGM(" ");
  6947. SERIAL_PROTOCOL_F(row[i], 5);
  6948. }
  6949. SERIAL_PROTOCOLPGM("\n");
  6950. }
  6951. custom_message_state--;
  6952. mesh_point++;
  6953. lcd_update(1);
  6954. }
  6955. card.closefile();
  6956. }
  6957. #endif
  6958. void temp_compensation_start() {
  6959. custom_message = true;
  6960. custom_message_type = 5;
  6961. custom_message_state = PINDA_HEAT_T + 1;
  6962. lcd_update(2);
  6963. if (degHotend(active_extruder) > EXTRUDE_MINTEMP) {
  6964. current_position[E_AXIS] -= DEFAULT_RETRACTION;
  6965. }
  6966. plan_buffer_line(current_position[X_AXIS], current_position[Y_AXIS], current_position[Z_AXIS], current_position[E_AXIS], 400, active_extruder);
  6967. current_position[X_AXIS] = PINDA_PREHEAT_X;
  6968. current_position[Y_AXIS] = PINDA_PREHEAT_Y;
  6969. current_position[Z_AXIS] = PINDA_PREHEAT_Z;
  6970. plan_buffer_line(current_position[X_AXIS], current_position[Y_AXIS], current_position[Z_AXIS], current_position[E_AXIS], 3000 / 60, active_extruder);
  6971. st_synchronize();
  6972. while (fabs(degBed() - target_temperature_bed) > 1) delay_keep_alive(1000);
  6973. for (int i = 0; i < PINDA_HEAT_T; i++) {
  6974. delay_keep_alive(1000);
  6975. custom_message_state = PINDA_HEAT_T - i;
  6976. if (custom_message_state == 99 || custom_message_state == 9) lcd_update(2); //force whole display redraw if number of digits changed
  6977. else lcd_update(1);
  6978. }
  6979. custom_message_type = 0;
  6980. custom_message_state = 0;
  6981. custom_message = false;
  6982. }
  6983. void temp_compensation_apply() {
  6984. int i_add;
  6985. int compensation_value;
  6986. int z_shift = 0;
  6987. float z_shift_mm;
  6988. if (calibration_status() == CALIBRATION_STATUS_CALIBRATED) {
  6989. if (target_temperature_bed % 10 == 0 && target_temperature_bed >= 60 && target_temperature_bed <= 100) {
  6990. i_add = (target_temperature_bed - 60) / 10;
  6991. EEPROM_read_B(EEPROM_PROBE_TEMP_SHIFT + i_add * 2, &z_shift);
  6992. z_shift_mm = z_shift / axis_steps_per_unit[Z_AXIS];
  6993. }else {
  6994. //interpolation
  6995. z_shift_mm = temp_comp_interpolation(target_temperature_bed) / axis_steps_per_unit[Z_AXIS];
  6996. }
  6997. SERIAL_PROTOCOLPGM("\n");
  6998. SERIAL_PROTOCOLPGM("Z shift applied:");
  6999. MYSERIAL.print(z_shift_mm);
  7000. 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);
  7001. st_synchronize();
  7002. plan_set_z_position(current_position[Z_AXIS]);
  7003. }
  7004. else {
  7005. //we have no temp compensation data
  7006. }
  7007. }
  7008. float temp_comp_interpolation(float inp_temperature) {
  7009. //cubic spline interpolation
  7010. int n, i, j, k;
  7011. float h[10], a, b, c, d, sum, s[10] = { 0 }, x[10], F[10], f[10], m[10][10] = { 0 }, temp;
  7012. int shift[10];
  7013. int temp_C[10];
  7014. n = 6; //number of measured points
  7015. shift[0] = 0;
  7016. for (i = 0; i < n; i++) {
  7017. if (i>0) EEPROM_read_B(EEPROM_PROBE_TEMP_SHIFT + (i-1) * 2, &shift[i]); //read shift in steps from EEPROM
  7018. temp_C[i] = 50 + i * 10; //temperature in C
  7019. #ifdef PINDA_THERMISTOR
  7020. temp_C[i] = 35 + i * 5; //temperature in C
  7021. #else
  7022. temp_C[i] = 50 + i * 10; //temperature in C
  7023. #endif
  7024. x[i] = (float)temp_C[i];
  7025. f[i] = (float)shift[i];
  7026. }
  7027. if (inp_temperature < x[0]) return 0;
  7028. for (i = n - 1; i>0; i--) {
  7029. F[i] = (f[i] - f[i - 1]) / (x[i] - x[i - 1]);
  7030. h[i - 1] = x[i] - x[i - 1];
  7031. }
  7032. //*********** formation of h, s , f matrix **************
  7033. for (i = 1; i<n - 1; i++) {
  7034. m[i][i] = 2 * (h[i - 1] + h[i]);
  7035. if (i != 1) {
  7036. m[i][i - 1] = h[i - 1];
  7037. m[i - 1][i] = h[i - 1];
  7038. }
  7039. m[i][n - 1] = 6 * (F[i + 1] - F[i]);
  7040. }
  7041. //*********** forward elimination **************
  7042. for (i = 1; i<n - 2; i++) {
  7043. temp = (m[i + 1][i] / m[i][i]);
  7044. for (j = 1; j <= n - 1; j++)
  7045. m[i + 1][j] -= temp*m[i][j];
  7046. }
  7047. //*********** backward substitution *********
  7048. for (i = n - 2; i>0; i--) {
  7049. sum = 0;
  7050. for (j = i; j <= n - 2; j++)
  7051. sum += m[i][j] * s[j];
  7052. s[i] = (m[i][n - 1] - sum) / m[i][i];
  7053. }
  7054. for (i = 0; i<n - 1; i++)
  7055. if ((x[i] <= inp_temperature && inp_temperature <= x[i + 1]) || (i == n-2 && inp_temperature > x[i + 1])) {
  7056. a = (s[i + 1] - s[i]) / (6 * h[i]);
  7057. b = s[i] / 2;
  7058. c = (f[i + 1] - f[i]) / h[i] - (2 * h[i] * s[i] + s[i + 1] * h[i]) / 6;
  7059. d = f[i];
  7060. sum = a*pow((inp_temperature - x[i]), 3) + b*pow((inp_temperature - x[i]), 2) + c*(inp_temperature - x[i]) + d;
  7061. }
  7062. return sum;
  7063. }
  7064. #ifdef PINDA_THERMISTOR
  7065. float temp_compensation_pinda_thermistor_offset(float temperature_pinda)
  7066. {
  7067. if (!temp_cal_active) return 0;
  7068. if (!calibration_status_pinda()) return 0;
  7069. return temp_comp_interpolation(temperature_pinda) / axis_steps_per_unit[Z_AXIS];
  7070. }
  7071. #endif //PINDA_THERMISTOR
  7072. void long_pause() //long pause print
  7073. {
  7074. st_synchronize();
  7075. //save currently set parameters to global variables
  7076. saved_feedmultiply = feedmultiply;
  7077. HotendTempBckp = degTargetHotend(active_extruder);
  7078. fanSpeedBckp = fanSpeed;
  7079. start_pause_print = millis();
  7080. //save position
  7081. pause_lastpos[X_AXIS] = current_position[X_AXIS];
  7082. pause_lastpos[Y_AXIS] = current_position[Y_AXIS];
  7083. pause_lastpos[Z_AXIS] = current_position[Z_AXIS];
  7084. pause_lastpos[E_AXIS] = current_position[E_AXIS];
  7085. //retract
  7086. current_position[E_AXIS] -= DEFAULT_RETRACTION;
  7087. plan_buffer_line(current_position[X_AXIS], current_position[Y_AXIS], current_position[Z_AXIS], current_position[E_AXIS], 400, active_extruder);
  7088. //lift z
  7089. current_position[Z_AXIS] += Z_PAUSE_LIFT;
  7090. if (current_position[Z_AXIS] > Z_MAX_POS) current_position[Z_AXIS] = Z_MAX_POS;
  7091. plan_buffer_line(current_position[X_AXIS], current_position[Y_AXIS], current_position[Z_AXIS], current_position[E_AXIS], 15, active_extruder);
  7092. //set nozzle target temperature to 0
  7093. setTargetHotend(0, 0);
  7094. setTargetHotend(0, 1);
  7095. setTargetHotend(0, 2);
  7096. //Move XY to side
  7097. current_position[X_AXIS] = X_PAUSE_POS;
  7098. current_position[Y_AXIS] = Y_PAUSE_POS;
  7099. plan_buffer_line(current_position[X_AXIS], current_position[Y_AXIS], current_position[Z_AXIS], current_position[E_AXIS], 50, active_extruder);
  7100. // Turn off the print fan
  7101. fanSpeed = 0;
  7102. st_synchronize();
  7103. }
  7104. void serialecho_temperatures() {
  7105. float tt = degHotend(active_extruder);
  7106. SERIAL_PROTOCOLPGM("T:");
  7107. SERIAL_PROTOCOL(tt);
  7108. SERIAL_PROTOCOLPGM(" E:");
  7109. SERIAL_PROTOCOL((int)active_extruder);
  7110. SERIAL_PROTOCOLPGM(" B:");
  7111. SERIAL_PROTOCOL_F(degBed(), 1);
  7112. SERIAL_PROTOCOLLN("");
  7113. }
  7114. extern uint32_t sdpos_atomic;
  7115. #ifdef UVLO_SUPPORT
  7116. void uvlo_()
  7117. {
  7118. unsigned long time_start = millis();
  7119. bool sd_print = card.sdprinting;
  7120. // Conserve power as soon as possible.
  7121. disable_x();
  7122. disable_y();
  7123. #ifdef TMC2130
  7124. tmc2130_set_current_h(Z_AXIS, 20);
  7125. tmc2130_set_current_r(Z_AXIS, 20);
  7126. tmc2130_set_current_h(E_AXIS, 20);
  7127. tmc2130_set_current_r(E_AXIS, 20);
  7128. #endif //TMC2130
  7129. // Indicate that the interrupt has been triggered.
  7130. // SERIAL_ECHOLNPGM("UVLO");
  7131. // Read out the current Z motor microstep counter. This will be later used
  7132. // for reaching the zero full step before powering off.
  7133. uint16_t z_microsteps = 0;
  7134. #ifdef TMC2130
  7135. z_microsteps = tmc2130_rd_MSCNT(Z_TMC2130_CS);
  7136. #endif //TMC2130
  7137. // Calculate the file position, from which to resume this print.
  7138. long sd_position = sdpos_atomic; //atomic sd position of last command added in queue
  7139. {
  7140. uint16_t sdlen_planner = planner_calc_sd_length(); //length of sd commands in planner
  7141. sd_position -= sdlen_planner;
  7142. uint16_t sdlen_cmdqueue = cmdqueue_calc_sd_length(); //length of sd commands in cmdqueue
  7143. sd_position -= sdlen_cmdqueue;
  7144. if (sd_position < 0) sd_position = 0;
  7145. }
  7146. // Backup the feedrate in mm/min.
  7147. int feedrate_bckp = blocks_queued() ? (block_buffer[block_buffer_tail].nominal_speed * 60.f) : feedrate;
  7148. // After this call, the planner queue is emptied and the current_position is set to a current logical coordinate.
  7149. // The logical coordinate will likely differ from the machine coordinate if the skew calibration and mesh bed leveling
  7150. // are in action.
  7151. planner_abort_hard();
  7152. // Store the current extruder position.
  7153. eeprom_update_float((float*)(EEPROM_UVLO_CURRENT_POSITION_E), st_get_position_mm(E_AXIS));
  7154. eeprom_update_byte((uint8_t*)EEPROM_UVLO_E_ABS, axis_relative_modes[3]?0:1);
  7155. // Clean the input command queue.
  7156. cmdqueue_reset();
  7157. card.sdprinting = false;
  7158. // card.closefile();
  7159. // Enable stepper driver interrupt to move Z axis.
  7160. // This should be fine as the planner and command queues are empty and the SD card printing is disabled.
  7161. //FIXME one may want to disable serial lines at this point of time to avoid interfering with the command queue,
  7162. // though it should not happen that the command queue is touched as the plan_buffer_line always succeed without blocking.
  7163. sei();
  7164. plan_buffer_line(
  7165. current_position[X_AXIS],
  7166. current_position[Y_AXIS],
  7167. current_position[Z_AXIS],
  7168. current_position[E_AXIS] - DEFAULT_RETRACTION,
  7169. 95, active_extruder);
  7170. st_synchronize();
  7171. disable_e0();
  7172. plan_buffer_line(
  7173. current_position[X_AXIS],
  7174. current_position[Y_AXIS],
  7175. current_position[Z_AXIS] + UVLO_Z_AXIS_SHIFT + float((1024 - z_microsteps + 7) >> 4) / axis_steps_per_unit[Z_AXIS],
  7176. current_position[E_AXIS] - DEFAULT_RETRACTION,
  7177. 40, active_extruder);
  7178. st_synchronize();
  7179. disable_e0();
  7180. plan_buffer_line(
  7181. current_position[X_AXIS],
  7182. current_position[Y_AXIS],
  7183. current_position[Z_AXIS] + UVLO_Z_AXIS_SHIFT + float((1024 - z_microsteps + 7) >> 4) / axis_steps_per_unit[Z_AXIS],
  7184. current_position[E_AXIS] - DEFAULT_RETRACTION,
  7185. 40, active_extruder);
  7186. st_synchronize();
  7187. disable_e0();
  7188. disable_z();
  7189. // Move Z up to the next 0th full step.
  7190. // Write the file position.
  7191. eeprom_update_dword((uint32_t*)(EEPROM_FILE_POSITION), sd_position);
  7192. // Store the mesh bed leveling offsets. This is 2*9=18 bytes, which takes 18*3.4us=52us in worst case.
  7193. for (int8_t mesh_point = 0; mesh_point < 9; ++ mesh_point) {
  7194. uint8_t ix = mesh_point % MESH_MEAS_NUM_X_POINTS; // from 0 to MESH_NUM_X_POINTS - 1
  7195. uint8_t iy = mesh_point / MESH_MEAS_NUM_X_POINTS;
  7196. // Scale the z value to 1u resolution.
  7197. int16_t v = mbl.active ? int16_t(floor(mbl.z_values[iy*3][ix*3] * 1000.f + 0.5f)) : 0;
  7198. eeprom_update_word((uint16_t*)(EEPROM_UVLO_MESH_BED_LEVELING+2*mesh_point), *reinterpret_cast<uint16_t*>(&v));
  7199. }
  7200. // Read out the current Z motor microstep counter. This will be later used
  7201. // for reaching the zero full step before powering off.
  7202. eeprom_update_word((uint16_t*)(EEPROM_UVLO_Z_MICROSTEPS), z_microsteps);
  7203. // Store the current position.
  7204. eeprom_update_float((float*)(EEPROM_UVLO_CURRENT_POSITION + 0), current_position[X_AXIS]);
  7205. eeprom_update_float((float*)(EEPROM_UVLO_CURRENT_POSITION + 4), current_position[Y_AXIS]);
  7206. eeprom_update_float((float*)(EEPROM_UVLO_CURRENT_POSITION_Z), current_position[Z_AXIS]);
  7207. // Store the current feed rate, temperatures, fan speed and extruder multipliers (flow rates)
  7208. EEPROM_save_B(EEPROM_UVLO_FEEDRATE, &feedrate_bckp);
  7209. eeprom_update_byte((uint8_t*)EEPROM_UVLO_TARGET_HOTEND, target_temperature[active_extruder]);
  7210. eeprom_update_byte((uint8_t*)EEPROM_UVLO_TARGET_BED, target_temperature_bed);
  7211. eeprom_update_byte((uint8_t*)EEPROM_UVLO_FAN_SPEED, fanSpeed);
  7212. eeprom_update_float((float*)(EEPROM_EXTRUDER_MULTIPLIER_0), extruder_multiplier[0]);
  7213. #if EXTRUDERS > 1
  7214. eeprom_update_float((float*)(EEPROM_EXTRUDER_MULTIPLIER_1), extruder_multiplier[1]);
  7215. #if EXTRUDERS > 2
  7216. eeprom_update_float((float*)(EEPROM_EXTRUDER_MULTIPLIER_2), extruder_multiplier[2]);
  7217. #endif
  7218. #endif
  7219. // Finaly store the "power outage" flag.
  7220. if(sd_print) eeprom_update_byte((uint8_t*)EEPROM_UVLO, 1);
  7221. st_synchronize();
  7222. SERIAL_ECHOPGM("stps");
  7223. MYSERIAL.println(tmc2130_rd_MSCNT(Z_AXIS));
  7224. disable_z();
  7225. // Increment power failure counter
  7226. eeprom_update_byte((uint8_t*)EEPROM_POWER_COUNT, eeprom_read_byte((uint8_t*)EEPROM_POWER_COUNT) + 1);
  7227. eeprom_update_word((uint16_t*)EEPROM_POWER_COUNT_TOT, eeprom_read_word((uint16_t*)EEPROM_POWER_COUNT_TOT) + 1);
  7228. SERIAL_ECHOLNPGM("UVLO - end");
  7229. MYSERIAL.println(millis() - time_start);
  7230. #if 0
  7231. // Move the print head to the side of the print until all the power stored in the power supply capacitors is depleted.
  7232. current_position[X_AXIS] = (current_position[X_AXIS] < 0.5f * (X_MIN_POS + X_MAX_POS)) ? X_MIN_POS : X_MAX_POS;
  7233. plan_buffer_line(current_position[X_AXIS], current_position[Y_AXIS], current_position[Z_AXIS], current_position[E_AXIS], 500, active_extruder);
  7234. st_synchronize();
  7235. #endif
  7236. cli();
  7237. volatile unsigned int ppcount = 0;
  7238. SET_OUTPUT(BEEPER);
  7239. WRITE(BEEPER, HIGH);
  7240. for(ppcount = 0; ppcount < 2000; ppcount ++){
  7241. asm("nop");//50ns on 20Mhz, 62.5ns on 16Mhz
  7242. }
  7243. WRITE(BEEPER, LOW);
  7244. while(1){
  7245. #if 1
  7246. WRITE(BEEPER, LOW);
  7247. for(ppcount = 0; ppcount < 8000; ppcount ++){
  7248. asm("nop");//50ns on 20Mhz, 62.5ns on 16Mhz
  7249. }
  7250. #endif
  7251. };
  7252. }
  7253. #endif //UVLO_SUPPORT
  7254. #if (defined(FANCHECK) && defined(TACH_1) && (TACH_1 >-1))
  7255. void setup_fan_interrupt() {
  7256. //INT7
  7257. DDRE &= ~(1 << 7); //input pin
  7258. PORTE &= ~(1 << 7); //no internal pull-up
  7259. //start with sensing rising edge
  7260. EICRB &= ~(1 << 6);
  7261. EICRB |= (1 << 7);
  7262. //enable INT7 interrupt
  7263. EIMSK |= (1 << 7);
  7264. }
  7265. // The fan interrupt is triggered at maximum 325Hz (may be a bit more due to component tollerances),
  7266. // and it takes 4.24 us to process (the interrupt invocation overhead not taken into account).
  7267. ISR(INT7_vect) {
  7268. //measuring speed now works for fanSpeed > 18 (approximately), which is sufficient because MIN_PRINT_FAN_SPEED is higher
  7269. if (fanSpeed < MIN_PRINT_FAN_SPEED) return;
  7270. if ((1 << 6) & EICRB) { //interrupt was triggered by rising edge
  7271. t_fan_rising_edge = millis_nc();
  7272. }
  7273. else { //interrupt was triggered by falling edge
  7274. if ((millis_nc() - t_fan_rising_edge) >= FAN_PULSE_WIDTH_LIMIT) {//this pulse was from sensor and not from pwm
  7275. fan_edge_counter[1] += 2; //we are currently counting all edges so lets count two edges for one pulse
  7276. }
  7277. }
  7278. EICRB ^= (1 << 6); //change edge
  7279. }
  7280. #endif
  7281. #ifdef UVLO_SUPPORT
  7282. void setup_uvlo_interrupt() {
  7283. DDRE &= ~(1 << 4); //input pin
  7284. PORTE &= ~(1 << 4); //no internal pull-up
  7285. //sensing falling edge
  7286. EICRB |= (1 << 0);
  7287. EICRB &= ~(1 << 1);
  7288. //enable INT4 interrupt
  7289. EIMSK |= (1 << 4);
  7290. }
  7291. ISR(INT4_vect) {
  7292. EIMSK &= ~(1 << 4); //disable INT4 interrupt to make sure that this code will be executed just once
  7293. SERIAL_ECHOLNPGM("INT4");
  7294. if (IS_SD_PRINTING) uvlo_();
  7295. }
  7296. void recover_print(uint8_t automatic) {
  7297. char cmd[30];
  7298. lcd_update_enable(true);
  7299. lcd_update(2);
  7300. lcd_setstatuspgm(_i("Recovering print "));////MSG_RECOVERING_PRINT c=20 r=1
  7301. recover_machine_state_after_power_panic(); //recover position, temperatures and extrude_multipliers
  7302. // Lift the print head, so one may remove the excess priming material.
  7303. if (current_position[Z_AXIS] < 25)
  7304. enquecommand_P(PSTR("G1 Z25 F800"));
  7305. // Home X and Y axes. Homing just X and Y shall not touch the babystep and the world2machine transformation status.
  7306. enquecommand_P(PSTR("G28 X Y"));
  7307. // Set the target bed and nozzle temperatures and wait.
  7308. sprintf_P(cmd, PSTR("M109 S%d"), target_temperature[active_extruder]);
  7309. enquecommand(cmd);
  7310. sprintf_P(cmd, PSTR("M190 S%d"), target_temperature_bed);
  7311. enquecommand(cmd);
  7312. enquecommand_P(PSTR("M83")); //E axis relative mode
  7313. //enquecommand_P(PSTR("G1 E5 F120")); //Extrude some filament to stabilize pessure
  7314. // If not automatically recoreverd (long power loss), extrude extra filament to stabilize
  7315. if(automatic == 0){
  7316. enquecommand_P(PSTR("G1 E5 F120")); //Extrude some filament to stabilize pessure
  7317. }
  7318. enquecommand_P(PSTR("G1 E" STRINGIFY(-DEFAULT_RETRACTION)" F480"));
  7319. // Mark the power panic status as inactive.
  7320. eeprom_update_byte((uint8_t*)EEPROM_UVLO, 0);
  7321. /*while ((abs(degHotend(0)- target_temperature[0])>5) || (abs(degBed() -target_temperature_bed)>3)) { //wait for heater and bed to reach target temp
  7322. delay_keep_alive(1000);
  7323. }*/
  7324. SERIAL_ECHOPGM("After waiting for temp:");
  7325. SERIAL_ECHOPGM("Current position X_AXIS:");
  7326. MYSERIAL.println(current_position[X_AXIS]);
  7327. SERIAL_ECHOPGM("Current position Y_AXIS:");
  7328. MYSERIAL.println(current_position[Y_AXIS]);
  7329. // Restart the print.
  7330. restore_print_from_eeprom();
  7331. SERIAL_ECHOPGM("current_position[Z_AXIS]:");
  7332. MYSERIAL.print(current_position[Z_AXIS]);
  7333. SERIAL_ECHOPGM("current_position[E_AXIS]:");
  7334. MYSERIAL.print(current_position[E_AXIS]);
  7335. }
  7336. void recover_machine_state_after_power_panic()
  7337. {
  7338. char cmd[30];
  7339. // 1) Recover the logical cordinates at the time of the power panic.
  7340. // The logical XY coordinates are needed to recover the machine Z coordinate corrected by the mesh bed leveling.
  7341. current_position[X_AXIS] = eeprom_read_float((float*)(EEPROM_UVLO_CURRENT_POSITION + 0));
  7342. current_position[Y_AXIS] = eeprom_read_float((float*)(EEPROM_UVLO_CURRENT_POSITION + 4));
  7343. // Recover the logical coordinate of the Z axis at the time of the power panic.
  7344. // The current position after power panic is moved to the next closest 0th full step.
  7345. current_position[Z_AXIS] = eeprom_read_float((float*)(EEPROM_UVLO_CURRENT_POSITION_Z)) +
  7346. UVLO_Z_AXIS_SHIFT + float((1024 - eeprom_read_word((uint16_t*)(EEPROM_UVLO_Z_MICROSTEPS)) + 7) >> 4) / axis_steps_per_unit[Z_AXIS];
  7347. if (eeprom_read_byte((uint8_t*)EEPROM_UVLO_E_ABS)) {
  7348. current_position[E_AXIS] = eeprom_read_float((float*)(EEPROM_UVLO_CURRENT_POSITION_E));
  7349. sprintf_P(cmd, PSTR("G92 E"));
  7350. dtostrf(current_position[E_AXIS], 6, 3, cmd + strlen(cmd));
  7351. enquecommand(cmd);
  7352. }
  7353. memcpy(destination, current_position, sizeof(destination));
  7354. SERIAL_ECHOPGM("recover_machine_state_after_power_panic, initial ");
  7355. print_world_coordinates();
  7356. // 2) Initialize the logical to physical coordinate system transformation.
  7357. world2machine_initialize();
  7358. // 3) Restore the mesh bed leveling offsets. This is 2*9=18 bytes, which takes 18*3.4us=52us in worst case.
  7359. mbl.active = false;
  7360. for (int8_t mesh_point = 0; mesh_point < 9; ++ mesh_point) {
  7361. uint8_t ix = mesh_point % MESH_MEAS_NUM_X_POINTS; // from 0 to MESH_NUM_X_POINTS - 1
  7362. uint8_t iy = mesh_point / MESH_MEAS_NUM_X_POINTS;
  7363. // Scale the z value to 10u resolution.
  7364. int16_t v;
  7365. eeprom_read_block(&v, (void*)(EEPROM_UVLO_MESH_BED_LEVELING+2*mesh_point), 2);
  7366. if (v != 0)
  7367. mbl.active = true;
  7368. mbl.z_values[iy][ix] = float(v) * 0.001f;
  7369. }
  7370. if (mbl.active)
  7371. mbl.upsample_3x3();
  7372. // SERIAL_ECHOPGM("recover_machine_state_after_power_panic, initial ");
  7373. // print_mesh_bed_leveling_table();
  7374. // 4) Load the baby stepping value, which is expected to be active at the time of power panic.
  7375. // The baby stepping value is used to reset the physical Z axis when rehoming the Z axis.
  7376. babystep_load();
  7377. // 5) Set the physical positions from the logical positions using the world2machine transformation and the active bed leveling.
  7378. plan_set_position(current_position[X_AXIS], current_position[Y_AXIS], current_position[Z_AXIS], current_position[E_AXIS]);
  7379. // 6) Power up the motors, mark their positions as known.
  7380. //FIXME Verfiy, whether the X and Y axes should be powered up here, as they will later be re-homed anyway.
  7381. axis_known_position[X_AXIS] = true; enable_x();
  7382. axis_known_position[Y_AXIS] = true; enable_y();
  7383. axis_known_position[Z_AXIS] = true; enable_z();
  7384. SERIAL_ECHOPGM("recover_machine_state_after_power_panic, initial ");
  7385. print_physical_coordinates();
  7386. // 7) Recover the target temperatures.
  7387. target_temperature[active_extruder] = eeprom_read_byte((uint8_t*)EEPROM_UVLO_TARGET_HOTEND);
  7388. target_temperature_bed = eeprom_read_byte((uint8_t*)EEPROM_UVLO_TARGET_BED);
  7389. // 8) Recover extruder multipilers
  7390. extruder_multiplier[0] = eeprom_read_float((float*)(EEPROM_EXTRUDER_MULTIPLIER_0));
  7391. #if EXTRUDERS > 1
  7392. extruder_multiplier[1] = eeprom_read_float((float*)(EEPROM_EXTRUDER_MULTIPLIER_1));
  7393. #if EXTRUDERS > 2
  7394. extruder_multiplier[2] = eeprom_read_float((float*)(EEPROM_EXTRUDER_MULTIPLIER_2));
  7395. #endif
  7396. #endif
  7397. }
  7398. void restore_print_from_eeprom() {
  7399. float x_rec, y_rec, z_pos;
  7400. int feedrate_rec;
  7401. uint8_t fan_speed_rec;
  7402. char cmd[30];
  7403. char* c;
  7404. char filename[13];
  7405. uint8_t depth = 0;
  7406. char dir_name[9];
  7407. fan_speed_rec = eeprom_read_byte((uint8_t*)EEPROM_UVLO_FAN_SPEED);
  7408. EEPROM_read_B(EEPROM_UVLO_FEEDRATE, &feedrate_rec);
  7409. SERIAL_ECHOPGM("Feedrate:");
  7410. MYSERIAL.println(feedrate_rec);
  7411. depth = eeprom_read_byte((uint8_t*)EEPROM_DIR_DEPTH);
  7412. MYSERIAL.println(int(depth));
  7413. for (int i = 0; i < depth; i++) {
  7414. for (int j = 0; j < 8; j++) {
  7415. dir_name[j] = eeprom_read_byte((uint8_t*)EEPROM_DIRS + j + 8 * i);
  7416. }
  7417. dir_name[8] = '\0';
  7418. MYSERIAL.println(dir_name);
  7419. card.chdir(dir_name);
  7420. }
  7421. for (int i = 0; i < 8; i++) {
  7422. filename[i] = eeprom_read_byte((uint8_t*)EEPROM_FILENAME + i);
  7423. }
  7424. filename[8] = '\0';
  7425. MYSERIAL.print(filename);
  7426. strcat_P(filename, PSTR(".gco"));
  7427. sprintf_P(cmd, PSTR("M23 %s"), filename);
  7428. for (c = &cmd[4]; *c; c++)
  7429. *c = tolower(*c);
  7430. enquecommand(cmd);
  7431. uint32_t position = eeprom_read_dword((uint32_t*)(EEPROM_FILE_POSITION));
  7432. SERIAL_ECHOPGM("Position read from eeprom:");
  7433. MYSERIAL.println(position);
  7434. // E axis relative mode.
  7435. enquecommand_P(PSTR("M83"));
  7436. // Move to the XY print position in logical coordinates, where the print has been killed.
  7437. strcpy_P(cmd, PSTR("G1 X")); strcat(cmd, ftostr32(eeprom_read_float((float*)(EEPROM_UVLO_CURRENT_POSITION + 0))));
  7438. strcat_P(cmd, PSTR(" Y")); strcat(cmd, ftostr32(eeprom_read_float((float*)(EEPROM_UVLO_CURRENT_POSITION + 4))));
  7439. strcat_P(cmd, PSTR(" F2000"));
  7440. enquecommand(cmd);
  7441. // Move the Z axis down to the print, in logical coordinates.
  7442. strcpy_P(cmd, PSTR("G1 Z")); strcat(cmd, ftostr32(eeprom_read_float((float*)(EEPROM_UVLO_CURRENT_POSITION_Z))));
  7443. enquecommand(cmd);
  7444. // Unretract.
  7445. enquecommand_P(PSTR("G1 E" STRINGIFY(2*DEFAULT_RETRACTION)" F480"));
  7446. // Set the feedrate saved at the power panic.
  7447. sprintf_P(cmd, PSTR("G1 F%d"), feedrate_rec);
  7448. enquecommand(cmd);
  7449. if (eeprom_read_byte((uint8_t*)EEPROM_UVLO_E_ABS))
  7450. {
  7451. float extruder_abs_pos = eeprom_read_float((float*)(EEPROM_UVLO_CURRENT_POSITION_E));
  7452. enquecommand_P(PSTR("M82")); //E axis abslute mode
  7453. }
  7454. // Set the fan speed saved at the power panic.
  7455. strcpy_P(cmd, PSTR("M106 S"));
  7456. strcat(cmd, itostr3(int(fan_speed_rec)));
  7457. enquecommand(cmd);
  7458. // Set a position in the file.
  7459. sprintf_P(cmd, PSTR("M26 S%lu"), position);
  7460. enquecommand(cmd);
  7461. // Start SD print.
  7462. enquecommand_P(PSTR("M24"));
  7463. }
  7464. #endif //UVLO_SUPPORT
  7465. ////////////////////////////////////////////////////////////////////////////////
  7466. // save/restore printing
  7467. void stop_and_save_print_to_ram(float z_move, float e_move)
  7468. {
  7469. if (saved_printing) return;
  7470. unsigned char nplanner_blocks;
  7471. unsigned char nlines;
  7472. uint16_t sdlen_planner;
  7473. uint16_t sdlen_cmdqueue;
  7474. cli();
  7475. if (card.sdprinting) {
  7476. nplanner_blocks = number_of_blocks();
  7477. saved_sdpos = sdpos_atomic; //atomic sd position of last command added in queue
  7478. sdlen_planner = planner_calc_sd_length(); //length of sd commands in planner
  7479. saved_sdpos -= sdlen_planner;
  7480. sdlen_cmdqueue = cmdqueue_calc_sd_length(); //length of sd commands in cmdqueue
  7481. saved_sdpos -= sdlen_cmdqueue;
  7482. saved_printing_type = PRINTING_TYPE_SD;
  7483. }
  7484. else if (is_usb_printing) { //reuse saved_sdpos for storing line number
  7485. saved_sdpos = gcode_LastN; //start with line number of command added recently to cmd queue
  7486. //reuse planner_calc_sd_length function for getting number of lines of commands in planner:
  7487. nlines = planner_calc_sd_length(); //number of lines of commands in planner
  7488. saved_sdpos -= nlines;
  7489. saved_sdpos -= buflen; //number of blocks in cmd buffer
  7490. saved_printing_type = PRINTING_TYPE_USB;
  7491. }
  7492. else {
  7493. //not sd printing nor usb printing
  7494. }
  7495. #if 0
  7496. SERIAL_ECHOPGM("SDPOS_ATOMIC="); MYSERIAL.println(sdpos_atomic, DEC);
  7497. SERIAL_ECHOPGM("SDPOS="); MYSERIAL.println(card.get_sdpos(), DEC);
  7498. SERIAL_ECHOPGM("SDLEN_PLAN="); MYSERIAL.println(sdlen_planner, DEC);
  7499. SERIAL_ECHOPGM("SDLEN_CMDQ="); MYSERIAL.println(sdlen_cmdqueue, DEC);
  7500. SERIAL_ECHOPGM("PLANNERBLOCKS="); MYSERIAL.println(int(nplanner_blocks), DEC);
  7501. SERIAL_ECHOPGM("SDSAVED="); MYSERIAL.println(saved_sdpos, DEC);
  7502. //SERIAL_ECHOPGM("SDFILELEN="); MYSERIAL.println(card.fileSize(), DEC);
  7503. {
  7504. card.setIndex(saved_sdpos);
  7505. SERIAL_ECHOLNPGM("Content of planner buffer: ");
  7506. for (unsigned int idx = 0; idx < sdlen_planner; ++ idx)
  7507. MYSERIAL.print(char(card.get()));
  7508. SERIAL_ECHOLNPGM("Content of command buffer: ");
  7509. for (unsigned int idx = 0; idx < sdlen_cmdqueue; ++ idx)
  7510. MYSERIAL.print(char(card.get()));
  7511. SERIAL_ECHOLNPGM("End of command buffer");
  7512. }
  7513. {
  7514. // Print the content of the planner buffer, line by line:
  7515. card.setIndex(saved_sdpos);
  7516. int8_t iline = 0;
  7517. for (unsigned char idx = block_buffer_tail; idx != block_buffer_head; idx = (idx + 1) & (BLOCK_BUFFER_SIZE - 1), ++ iline) {
  7518. SERIAL_ECHOPGM("Planner line (from file): ");
  7519. MYSERIAL.print(int(iline), DEC);
  7520. SERIAL_ECHOPGM(", length: ");
  7521. MYSERIAL.print(block_buffer[idx].sdlen, DEC);
  7522. SERIAL_ECHOPGM(", steps: (");
  7523. MYSERIAL.print(block_buffer[idx].steps_x, DEC);
  7524. SERIAL_ECHOPGM(",");
  7525. MYSERIAL.print(block_buffer[idx].steps_y, DEC);
  7526. SERIAL_ECHOPGM(",");
  7527. MYSERIAL.print(block_buffer[idx].steps_z, DEC);
  7528. SERIAL_ECHOPGM(",");
  7529. MYSERIAL.print(block_buffer[idx].steps_e, DEC);
  7530. SERIAL_ECHOPGM("), events: ");
  7531. MYSERIAL.println(block_buffer[idx].step_event_count, DEC);
  7532. for (int len = block_buffer[idx].sdlen; len > 0; -- len)
  7533. MYSERIAL.print(char(card.get()));
  7534. }
  7535. }
  7536. {
  7537. // Print the content of the command buffer, line by line:
  7538. int8_t iline = 0;
  7539. union {
  7540. struct {
  7541. char lo;
  7542. char hi;
  7543. } lohi;
  7544. uint16_t value;
  7545. } sdlen_single;
  7546. int _bufindr = bufindr;
  7547. for (int _buflen = buflen; _buflen > 0; ++ iline) {
  7548. if (cmdbuffer[_bufindr] == CMDBUFFER_CURRENT_TYPE_SDCARD) {
  7549. sdlen_single.lohi.lo = cmdbuffer[_bufindr + 1];
  7550. sdlen_single.lohi.hi = cmdbuffer[_bufindr + 2];
  7551. }
  7552. SERIAL_ECHOPGM("Buffer line (from buffer): ");
  7553. MYSERIAL.print(int(iline), DEC);
  7554. SERIAL_ECHOPGM(", type: ");
  7555. MYSERIAL.print(int(cmdbuffer[_bufindr]), DEC);
  7556. SERIAL_ECHOPGM(", len: ");
  7557. MYSERIAL.println(sdlen_single.value, DEC);
  7558. // Print the content of the buffer line.
  7559. MYSERIAL.println(cmdbuffer + _bufindr + CMDHDRSIZE);
  7560. SERIAL_ECHOPGM("Buffer line (from file): ");
  7561. MYSERIAL.print(int(iline), DEC);
  7562. MYSERIAL.println(int(iline), DEC);
  7563. for (; sdlen_single.value > 0; -- sdlen_single.value)
  7564. MYSERIAL.print(char(card.get()));
  7565. if (-- _buflen == 0)
  7566. break;
  7567. // First skip the current command ID and iterate up to the end of the string.
  7568. for (_bufindr += CMDHDRSIZE; cmdbuffer[_bufindr] != 0; ++ _bufindr) ;
  7569. // Second, skip the end of string null character and iterate until a nonzero command ID is found.
  7570. for (++ _bufindr; _bufindr < sizeof(cmdbuffer) && cmdbuffer[_bufindr] == 0; ++ _bufindr) ;
  7571. // If the end of the buffer was empty,
  7572. if (_bufindr == sizeof(cmdbuffer)) {
  7573. // skip to the start and find the nonzero command.
  7574. for (_bufindr = 0; cmdbuffer[_bufindr] == 0; ++ _bufindr) ;
  7575. }
  7576. }
  7577. }
  7578. #endif
  7579. #if 0
  7580. saved_feedrate2 = feedrate; //save feedrate
  7581. #else
  7582. // Try to deduce the feedrate from the first block of the planner.
  7583. // Speed is in mm/min.
  7584. saved_feedrate2 = blocks_queued() ? (block_buffer[block_buffer_tail].nominal_speed * 60.f) : feedrate;
  7585. #endif
  7586. planner_abort_hard(); //abort printing
  7587. memcpy(saved_pos, current_position, sizeof(saved_pos));
  7588. saved_active_extruder = active_extruder; //save active_extruder
  7589. saved_extruder_under_pressure = extruder_under_pressure; //extruder under pressure flag - currently unused
  7590. cmdqueue_reset(); //empty cmdqueue
  7591. card.sdprinting = false;
  7592. // card.closefile();
  7593. saved_printing = true;
  7594. // We may have missed a stepper timer interrupt. Be safe than sorry, reset the stepper timer before re-enabling interrupts.
  7595. st_reset_timer();
  7596. sei();
  7597. if ((z_move != 0) || (e_move != 0)) { // extruder or z move
  7598. #if 1
  7599. // Rather than calling plan_buffer_line directly, push the move into the command queue,
  7600. char buf[48];
  7601. // First unretract (relative extrusion)
  7602. strcpy_P(buf, PSTR("G1 E"));
  7603. dtostrf(e_move, 6, 3, buf + strlen(buf));
  7604. strcat_P(buf, PSTR(" F"));
  7605. dtostrf(retract_feedrate*60, 8, 3, buf + strlen(buf));
  7606. enquecommand(buf, false);
  7607. // Then lift Z axis
  7608. strcpy_P(buf, PSTR("G1 Z"));
  7609. dtostrf(saved_pos[Z_AXIS] + z_move, 8, 3, buf + strlen(buf));
  7610. strcat_P(buf, PSTR(" F"));
  7611. dtostrf(homing_feedrate[Z_AXIS], 8, 3, buf + strlen(buf));
  7612. // At this point the command queue is empty.
  7613. enquecommand(buf, false);
  7614. // If this call is invoked from the main Arduino loop() function, let the caller know that the command
  7615. // in the command queue is not the original command, but a new one, so it should not be removed from the queue.
  7616. repeatcommand_front();
  7617. #else
  7618. 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);
  7619. st_synchronize(); //wait moving
  7620. memcpy(current_position, saved_pos, sizeof(saved_pos));
  7621. memcpy(destination, current_position, sizeof(destination));
  7622. #endif
  7623. }
  7624. }
  7625. void restore_print_from_ram_and_continue(float e_move)
  7626. {
  7627. if (!saved_printing) return;
  7628. // for (int axis = X_AXIS; axis <= E_AXIS; axis++)
  7629. // current_position[axis] = st_get_position_mm(axis);
  7630. active_extruder = saved_active_extruder; //restore active_extruder
  7631. feedrate = saved_feedrate2; //restore feedrate
  7632. float e = saved_pos[E_AXIS] - e_move;
  7633. plan_set_e_position(e);
  7634. plan_buffer_line(saved_pos[X_AXIS], saved_pos[Y_AXIS], saved_pos[Z_AXIS], saved_pos[E_AXIS], homing_feedrate[Z_AXIS]/13, active_extruder);
  7635. st_synchronize();
  7636. memcpy(current_position, saved_pos, sizeof(saved_pos));
  7637. memcpy(destination, current_position, sizeof(destination));
  7638. if (saved_printing_type == PRINTING_TYPE_SD) { //was sd printing
  7639. card.setIndex(saved_sdpos);
  7640. sdpos_atomic = saved_sdpos;
  7641. card.sdprinting = true;
  7642. }
  7643. else if (saved_printing_type == PRINTING_TYPE_USB) { //was usb printing
  7644. gcode_LastN = saved_sdpos; //saved_sdpos was reused for storing line number when usb printing
  7645. FlushSerialRequestResend();
  7646. }
  7647. else {
  7648. //not sd printing nor usb printing
  7649. }
  7650. saved_printing = false;
  7651. }
  7652. void print_world_coordinates()
  7653. {
  7654. SERIAL_ECHOPGM("world coordinates: (");
  7655. MYSERIAL.print(current_position[X_AXIS], 3);
  7656. SERIAL_ECHOPGM(", ");
  7657. MYSERIAL.print(current_position[Y_AXIS], 3);
  7658. SERIAL_ECHOPGM(", ");
  7659. MYSERIAL.print(current_position[Z_AXIS], 3);
  7660. SERIAL_ECHOLNPGM(")");
  7661. }
  7662. void print_physical_coordinates()
  7663. {
  7664. SERIAL_ECHOPGM("physical coordinates: (");
  7665. MYSERIAL.print(st_get_position_mm(X_AXIS), 3);
  7666. SERIAL_ECHOPGM(", ");
  7667. MYSERIAL.print(st_get_position_mm(Y_AXIS), 3);
  7668. SERIAL_ECHOPGM(", ");
  7669. MYSERIAL.print(st_get_position_mm(Z_AXIS), 3);
  7670. SERIAL_ECHOLNPGM(")");
  7671. }
  7672. void print_mesh_bed_leveling_table()
  7673. {
  7674. SERIAL_ECHOPGM("mesh bed leveling: ");
  7675. for (int8_t y = 0; y < MESH_NUM_Y_POINTS; ++ y)
  7676. for (int8_t x = 0; x < MESH_NUM_Y_POINTS; ++ x) {
  7677. MYSERIAL.print(mbl.z_values[y][x], 3);
  7678. SERIAL_ECHOPGM(" ");
  7679. }
  7680. SERIAL_ECHOLNPGM("");
  7681. }
  7682. uint16_t print_time_remaining() {
  7683. uint16_t print_t = PRINT_TIME_REMAINING_INIT;
  7684. if (SilentModeMenu == SILENT_MODE_OFF) print_t = print_time_remaining_normal;
  7685. else print_t = print_time_remaining_silent;
  7686. if ((print_t != PRINT_TIME_REMAINING_INIT) && (feedmultiply != 0)) print_t = 100 * print_t / feedmultiply;
  7687. return print_t;
  7688. }
  7689. uint8_t print_percent_done() {
  7690. //in case that we have information from M73 gcode return percentage counted by slicer, else return percentage counted as byte_printed/filesize
  7691. uint8_t percent_done = 0;
  7692. if (SilentModeMenu == SILENT_MODE_OFF && print_percent_done_normal <= 100) {
  7693. percent_done = print_percent_done_normal;
  7694. }
  7695. else if (print_percent_done_silent <= 100) {
  7696. percent_done = print_percent_done_silent;
  7697. }
  7698. else {
  7699. percent_done = card.percentDone();
  7700. }
  7701. return percent_done;
  7702. }
  7703. static void print_time_remaining_init() {
  7704. print_time_remaining_normal = PRINT_TIME_REMAINING_INIT;
  7705. print_time_remaining_silent = PRINT_TIME_REMAINING_INIT;
  7706. print_percent_done_normal = PRINT_PERCENT_DONE_INIT;
  7707. print_percent_done_silent = PRINT_PERCENT_DONE_INIT;
  7708. }
  7709. #define FIL_LOAD_LENGTH 60