Marlin_main.cpp 282 KB

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