Marlin_main.cpp 282 KB

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