Marlin_main.cpp 227 KB

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