Marlin_main.cpp 237 KB

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