Marlin_main.cpp 240 KB

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