Marlin_main.cpp 230 KB

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