Marlin_main.cpp 241 KB

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