Marlin_main.cpp 246 KB

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