Marlin_main.cpp 240 KB

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