Marlin_main.cpp 269 KB

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