Marlin_main.cpp 224 KB

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