Marlin_main.cpp 298 KB

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