Marlin_main.cpp 227 KB

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