Marlin_main.cpp 283 KB

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