Marlin_main.cpp 248 KB

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