My Marlin configs for Fabrikator Mini and CTC i3 Pro B
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

Marlin_main.cpp 185KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488548954905491549254935494549554965497549854995500550155025503550455055506550755085509551055115512551355145515551655175518551955205521552255235524552555265527552855295530553155325533553455355536553755385539554055415542554355445545554655475548554955505551555255535554555555565557555855595560556155625563556455655566556755685569557055715572557355745575557655775578557955805581558255835584558555865587558855895590559155925593559455955596559755985599560056015602560356045605560656075608560956105611561256135614561556165617561856195620562156225623562456255626562756285629563056315632563356345635563656375638563956405641564256435644564556465647564856495650565156525653565456555656565756585659566056615662566356645665566656675668566956705671567256735674567556765677567856795680568156825683568456855686568756885689569056915692569356945695569656975698569957005701570257035704570557065707570857095710571157125713571457155716571757185719572057215722572357245725572657275728572957305731573257335734573557365737573857395740574157425743574457455746574757485749575057515752575357545755575657575758575957605761576257635764576557665767576857695770577157725773577457755776577757785779578057815782578357845785578657875788578957905791579257935794579557965797579857995800580158025803580458055806580758085809581058115812581358145815581658175818581958205821582258235824582558265827582858295830583158325833583458355836583758385839584058415842584358445845584658475848584958505851585258535854585558565857585858595860586158625863586458655866586758685869587058715872587358745875587658775878587958805881588258835884588558865887588858895890589158925893589458955896589758985899590059015902590359045905590659075908590959105911591259135914591559165917591859195920592159225923592459255926592759285929593059315932593359345935593659375938593959405941594259435944594559465947594859495950595159525953595459555956595759585959596059615962596359645965596659675968596959705971597259735974597559765977597859795980598159825983598459855986598759885989599059915992599359945995599659975998599960006001600260036004600560066007600860096010601160126013
  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. #define SERVO_LEVELING (defined(ENABLE_AUTO_BED_LEVELING) && PROBE_SERVO_DEACTIVATION_DELAY > 0)
  31. #ifdef MESH_BED_LEVELING
  32. #include "mesh_bed_leveling.h"
  33. #endif
  34. #include "ultralcd.h"
  35. #include "planner.h"
  36. #include "stepper.h"
  37. #include "temperature.h"
  38. #include "motion_control.h"
  39. #include "cardreader.h"
  40. #include "watchdog.h"
  41. #include "ConfigurationStore.h"
  42. #include "language.h"
  43. #include "pins_arduino.h"
  44. #include "math.h"
  45. #ifdef BLINKM
  46. #include "BlinkM.h"
  47. #include "Wire.h"
  48. #endif
  49. #if NUM_SERVOS > 0
  50. #include "Servo.h"
  51. #endif
  52. #if HAS_DIGIPOTSS
  53. #include <SPI.h>
  54. #endif
  55. // look here for descriptions of G-codes: http://linuxcnc.org/handbook/gcode/g-code.html
  56. // http://objects.reprap.org/wiki/Mendel_User_Manual:_RepRapGCodes
  57. //Implemented Codes
  58. //-------------------
  59. // G0 -> G1
  60. // G1 - Coordinated Movement X Y Z E
  61. // G2 - CW ARC
  62. // G3 - CCW ARC
  63. // G4 - Dwell S<seconds> or P<milliseconds>
  64. // G10 - retract filament according to settings of M207
  65. // G11 - retract recover filament according to settings of M208
  66. // G28 - Home one or more axes
  67. // G29 - Detailed Z-Probe, probes the bed at 3 or more points. Will fail if you haven't homed yet.
  68. // G30 - Single Z Probe, probes bed at current XY location.
  69. // G31 - Dock sled (Z_PROBE_SLED only)
  70. // G32 - Undock sled (Z_PROBE_SLED only)
  71. // G90 - Use Absolute Coordinates
  72. // G91 - Use Relative Coordinates
  73. // G92 - Set current position to coordinates given
  74. // M Codes
  75. // M0 - Unconditional stop - Wait for user to press a button on the LCD (Only if ULTRA_LCD is enabled)
  76. // M1 - Same as M0
  77. // M17 - Enable/Power all stepper motors
  78. // M18 - Disable all stepper motors; same as M84
  79. // M20 - List SD card
  80. // M21 - Init SD card
  81. // M22 - Release SD card
  82. // M23 - Select SD file (M23 filename.g)
  83. // M24 - Start/resume SD print
  84. // M25 - Pause SD print
  85. // M26 - Set SD position in bytes (M26 S12345)
  86. // M27 - Report SD print status
  87. // M28 - Start SD write (M28 filename.g)
  88. // M29 - Stop SD write
  89. // M30 - Delete file from SD (M30 filename.g)
  90. // M31 - Output time since last M109 or SD card start to serial
  91. // M32 - Select file and start SD print (Can be used _while_ printing from SD card files):
  92. // syntax "M32 /path/filename#", or "M32 S<startpos bytes> !filename#"
  93. // Call gcode file : "M32 P !filename#" and return to caller file after finishing (similar to #include).
  94. // The '#' is necessary when calling from within sd files, as it stops buffer prereading
  95. // 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.
  96. // M48 - Measure Z_Probe repeatability. M48 [n # of points] [X position] [Y position] [V_erboseness #] [E_ngage Probe] [L # of legs of travel]
  97. // M80 - Turn on Power Supply
  98. // M81 - Turn off Power Supply
  99. // M82 - Set E codes absolute (default)
  100. // M83 - Set E codes relative while in Absolute Coordinates (G90) mode
  101. // M84 - Disable steppers until next move,
  102. // or use S<seconds> to specify an inactivity timeout, after which the steppers will be disabled. S0 to disable the timeout.
  103. // M85 - Set inactivity shutdown timer with parameter S<seconds>. To disable set zero (default)
  104. // M92 - Set axis_steps_per_unit - same syntax as G92
  105. // M104 - Set extruder target temp
  106. // M105 - Read current temp
  107. // M106 - Fan on
  108. // M107 - Fan off
  109. // M109 - Sxxx Wait for extruder current temp to reach target temp. Waits only when heating
  110. // Rxxx Wait for extruder current temp to reach target temp. Waits when heating and cooling
  111. // IF AUTOTEMP is enabled, S<mintemp> B<maxtemp> F<factor>. Exit autotemp by any M109 without F
  112. // M112 - Emergency stop
  113. // M114 - Output current position to serial port
  114. // M115 - Capabilities string
  115. // M117 - display message
  116. // M119 - Output Endstop status to serial port
  117. // M120 - Enable endstop detection
  118. // M121 - Disable endstop detection
  119. // M126 - Solenoid Air Valve Open (BariCUDA support by jmil)
  120. // M127 - Solenoid Air Valve Closed (BariCUDA vent to atmospheric pressure by jmil)
  121. // M128 - EtoP Open (BariCUDA EtoP = electricity to air pressure transducer by jmil)
  122. // M129 - EtoP Closed (BariCUDA EtoP = electricity to air pressure transducer by jmil)
  123. // M140 - Set bed target temp
  124. // 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.
  125. // M190 - Sxxx Wait for bed current temp to reach target temp. Waits only when heating
  126. // Rxxx Wait for bed current temp to reach target temp. Waits when heating and cooling
  127. // M200 D<millimeters>- set filament diameter and set E axis units to cubic millimeters (use S0 to set back to millimeters).
  128. // M201 - Set max acceleration in units/s^2 for print moves (M201 X1000 Y1000)
  129. // M202 - Set max acceleration in units/s^2 for travel moves (M202 X1000 Y1000) Unused in Marlin!!
  130. // M203 - Set maximum feedrate that your machine can sustain (M203 X200 Y200 Z300 E10000) in mm/sec
  131. // M204 - Set default acceleration: P for Printing moves, R for Retract only (no X, Y, Z) moves and T for Travel (non printing) moves (ex. M204 P800 T3000 R9000) in mm/sec^2
  132. // 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
  133. // M206 - Set additional homing offset
  134. // M207 - Set retract length S[positive mm] F[feedrate mm/min] Z[additional zlift/hop], stays in mm regardless of M200 setting
  135. // M208 - Set recover=unretract length S[positive mm surplus to the M207 S*] F[feedrate mm/sec]
  136. // 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.
  137. // M218 - Set hotend offset (in mm): T<extruder_number> X<offset_on_X> Y<offset_on_Y>
  138. // M220 S<factor in percent>- set speed factor override percentage
  139. // M221 S<factor in percent>- set extrude factor override percentage
  140. // M226 P<pin number> S<pin state>- Wait until the specified pin reaches the state required
  141. // M240 - Trigger a camera to take a photograph
  142. // M250 - Set LCD contrast C<contrast value> (value 0..63)
  143. // M280 - Set servo position absolute. P: servo index, S: angle or microseconds
  144. // M300 - Play beep sound S<frequency Hz> P<duration ms>
  145. // M301 - Set PID parameters P I and D
  146. // M302 - Allow cold extrudes, or set the minimum extrude S<temperature>.
  147. // M303 - PID relay autotune S<temperature> sets the target temperature. (default target temperature = 150C)
  148. // M304 - Set bed PID parameters P I and D
  149. // M380 - Activate solenoid on active extruder
  150. // M381 - Disable all solenoids
  151. // M400 - Finish all moves
  152. // M401 - Lower z-probe if present
  153. // M402 - Raise z-probe if present
  154. // M404 - N<dia in mm> Enter the nominal filament width (3mm, 1.75mm ) or will display nominal filament width without parameters
  155. // M405 - Turn on Filament Sensor extrusion control. Optional D<delay in cm> to set delay in centimeters between sensor and extruder
  156. // M406 - Turn off Filament Sensor extrusion control
  157. // M407 - Display measured filament diameter
  158. // M500 - Store parameters in EEPROM
  159. // M501 - Read parameters from EEPROM (if you need reset them after you changed them temporarily).
  160. // M502 - Revert to the default "factory settings". You still need to store them in EEPROM afterwards if you want to.
  161. // M503 - Print the current settings (from memory not from EEPROM). Use S0 to leave off headings.
  162. // M540 - Use S[0|1] to enable or disable the stop SD card print on endstop hit (requires ABORT_ON_ENDSTOP_HIT_FEATURE_ENABLED)
  163. // M600 - Pause for filament change X[pos] Y[pos] Z[relative lift] E[initial retract] L[later retract distance for removal]
  164. // M665 - Set delta configurations
  165. // M666 - Set delta endstop adjustment
  166. // M605 - Set dual x-carriage movement mode: S<mode> [ X<duplication x-offset> R<duplication temp offset> ]
  167. // M907 - Set digital trimpot motor current using axis codes.
  168. // M908 - Control digital trimpot directly.
  169. // M350 - Set microstepping mode.
  170. // M351 - Toggle MS1 MS2 pins directly.
  171. // ************ SCARA Specific - This can change to suit future G-code regulations
  172. // M360 - SCARA calibration: Move to cal-position ThetaA (0 deg calibration)
  173. // M361 - SCARA calibration: Move to cal-position ThetaB (90 deg calibration - steps per degree)
  174. // M362 - SCARA calibration: Move to cal-position PsiA (0 deg calibration)
  175. // M363 - SCARA calibration: Move to cal-position PsiB (90 deg calibration - steps per degree)
  176. // M364 - SCARA calibration: Move to cal-position PSIC (90 deg to Theta calibration position)
  177. // M365 - SCARA calibration: Scaling factor, X, Y, Z axis
  178. //************* SCARA End ***************
  179. // M928 - Start SD logging (M928 filename.g) - ended by M29
  180. // M999 - Restart after being stopped by error
  181. #ifdef SDSUPPORT
  182. CardReader card;
  183. #endif
  184. float homing_feedrate[] = HOMING_FEEDRATE;
  185. int homing_bump_divisor[] = HOMING_BUMP_DIVISOR;
  186. bool axis_relative_modes[] = AXIS_RELATIVE_MODES;
  187. int feedmultiply = 100; //100->1 200->2
  188. int saved_feedmultiply;
  189. int extruder_multiply[EXTRUDERS] = ARRAY_BY_EXTRUDERS(100, 100, 100, 100);
  190. bool volumetric_enabled = false;
  191. float filament_size[EXTRUDERS] = ARRAY_BY_EXTRUDERS(DEFAULT_NOMINAL_FILAMENT_DIA, DEFAULT_NOMINAL_FILAMENT_DIA, DEFAULT_NOMINAL_FILAMENT_DIA, DEFAULT_NOMINAL_FILAMENT_DIA);
  192. float volumetric_multiplier[EXTRUDERS] = ARRAY_BY_EXTRUDERS(1.0, 1.0, 1.0, 1.0);
  193. float current_position[NUM_AXIS] = { 0.0 };
  194. float home_offset[3] = { 0 };
  195. float min_pos[3] = { X_MIN_POS, Y_MIN_POS, Z_MIN_POS };
  196. float max_pos[3] = { X_MAX_POS, Y_MAX_POS, Z_MAX_POS };
  197. bool axis_known_position[3] = { false };
  198. uint8_t active_extruder = 0;
  199. int fanSpeed = 0;
  200. bool cancel_heatup = false;
  201. const char errormagic[] PROGMEM = "Error:";
  202. const char echomagic[] PROGMEM = "echo:";
  203. const char axis_codes[NUM_AXIS] = {'X', 'Y', 'Z', 'E'};
  204. static float destination[NUM_AXIS] = { 0 };
  205. static float offset[3] = { 0 };
  206. static float feedrate = 1500.0, next_feedrate, saved_feedrate;
  207. static long gcode_N, gcode_LastN, Stopped_gcode_LastN = 0;
  208. static bool relative_mode = false; //Determines Absolute or Relative Coordinates
  209. static char cmdbuffer[BUFSIZE][MAX_CMD_SIZE];
  210. static int bufindr = 0;
  211. static int bufindw = 0;
  212. static int buflen = 0;
  213. static char serial_char;
  214. static int serial_count = 0;
  215. static boolean comment_mode = false;
  216. static char *strchr_pointer; ///< A pointer to find chars in the command string (X, Y, Z, E, etc.)
  217. const char* queued_commands_P= NULL; /* pointer to the current line in the active sequence of commands, or NULL when none */
  218. const int sensitive_pins[] = SENSITIVE_PINS; ///< Sensitive pin list for M42
  219. // Inactivity shutdown
  220. unsigned long previous_millis_cmd = 0;
  221. static unsigned long max_inactive_time = 0;
  222. static unsigned long stepper_inactive_time = DEFAULT_STEPPER_DEACTIVE_TIME*1000l;
  223. unsigned long starttime = 0; ///< Print job start time
  224. unsigned long stoptime = 0; ///< Print job stop time
  225. static uint8_t target_extruder;
  226. bool Stopped = false;
  227. bool CooldownNoWait = true;
  228. bool target_direction;
  229. #ifdef ENABLE_AUTO_BED_LEVELING
  230. int xy_travel_speed = XY_TRAVEL_SPEED;
  231. float zprobe_zoffset = -Z_PROBE_OFFSET_FROM_EXTRUDER;
  232. #endif
  233. #if defined(Z_DUAL_ENDSTOPS) && !defined(DELTA)
  234. float z_endstop_adj = 0;
  235. #endif
  236. // Extruder offsets
  237. #if EXTRUDERS > 1
  238. #ifndef EXTRUDER_OFFSET_X
  239. #define EXTRUDER_OFFSET_X { 0 }
  240. #endif
  241. #ifndef EXTRUDER_OFFSET_Y
  242. #define EXTRUDER_OFFSET_Y { 0 }
  243. #endif
  244. float extruder_offset[][EXTRUDERS] = {
  245. EXTRUDER_OFFSET_X,
  246. EXTRUDER_OFFSET_Y
  247. #ifdef DUAL_X_CARRIAGE
  248. , { 0 } // supports offsets in XYZ plane
  249. #endif
  250. };
  251. #endif
  252. #ifdef SERVO_ENDSTOPS
  253. int servo_endstops[] = SERVO_ENDSTOPS;
  254. int servo_endstop_angles[] = SERVO_ENDSTOP_ANGLES;
  255. #endif
  256. #ifdef BARICUDA
  257. int ValvePressure = 0;
  258. int EtoPPressure = 0;
  259. #endif
  260. #ifdef FWRETRACT
  261. bool autoretract_enabled = false;
  262. bool retracted[EXTRUDERS] = { false };
  263. bool retracted_swap[EXTRUDERS] = { false };
  264. float retract_length = RETRACT_LENGTH;
  265. float retract_length_swap = RETRACT_LENGTH_SWAP;
  266. float retract_feedrate = RETRACT_FEEDRATE;
  267. float retract_zlift = RETRACT_ZLIFT;
  268. float retract_recover_length = RETRACT_RECOVER_LENGTH;
  269. float retract_recover_length_swap = RETRACT_RECOVER_LENGTH_SWAP;
  270. float retract_recover_feedrate = RETRACT_RECOVER_FEEDRATE;
  271. #endif // FWRETRACT
  272. #if defined(ULTIPANEL) && HAS_POWER_SWITCH
  273. bool powersupply =
  274. #ifdef PS_DEFAULT_OFF
  275. false
  276. #else
  277. true
  278. #endif
  279. ;
  280. #endif
  281. #ifdef DELTA
  282. float delta[3] = { 0 };
  283. #define SIN_60 0.8660254037844386
  284. #define COS_60 0.5
  285. float endstop_adj[3] = { 0 };
  286. // these are the default values, can be overriden with M665
  287. float delta_radius = DELTA_RADIUS;
  288. float delta_tower1_x = -SIN_60 * delta_radius; // front left tower
  289. float delta_tower1_y = -COS_60 * delta_radius;
  290. float delta_tower2_x = SIN_60 * delta_radius; // front right tower
  291. float delta_tower2_y = -COS_60 * delta_radius;
  292. float delta_tower3_x = 0; // back middle tower
  293. float delta_tower3_y = delta_radius;
  294. float delta_diagonal_rod = DELTA_DIAGONAL_ROD;
  295. float delta_diagonal_rod_2 = sq(delta_diagonal_rod);
  296. float delta_segments_per_second = DELTA_SEGMENTS_PER_SECOND;
  297. #ifdef ENABLE_AUTO_BED_LEVELING
  298. int delta_grid_spacing[2] = { 0, 0 };
  299. float bed_level[AUTO_BED_LEVELING_GRID_POINTS][AUTO_BED_LEVELING_GRID_POINTS];
  300. #endif
  301. #else
  302. static bool home_all_axis = true;
  303. #endif
  304. #ifdef SCARA
  305. static float delta[3] = { 0 };
  306. float axis_scaling[3] = { 1, 1, 1 }; // Build size scaling, default to 1
  307. #endif
  308. #ifdef FILAMENT_SENSOR
  309. //Variables for Filament Sensor input
  310. float filament_width_nominal = DEFAULT_NOMINAL_FILAMENT_DIA; //Set nominal filament width, can be changed with M404
  311. bool filament_sensor = false; //M405 turns on filament_sensor control, M406 turns it off
  312. float filament_width_meas = DEFAULT_MEASURED_FILAMENT_DIA; //Stores the measured filament diameter
  313. signed char measurement_delay[MAX_MEASUREMENT_DELAY+1]; //ring buffer to delay measurement store extruder factor after subtracting 100
  314. int delay_index1 = 0; //index into ring buffer
  315. int delay_index2 = -1; //index into ring buffer - set to -1 on startup to indicate ring buffer needs to be initialized
  316. float delay_dist = 0; //delay distance counter
  317. int meas_delay_cm = MEASUREMENT_DELAY_CM; //distance delay setting
  318. #endif
  319. #ifdef FILAMENT_RUNOUT_SENSOR
  320. static bool filrunoutEnqued = false;
  321. #endif
  322. #ifdef SDSUPPORT
  323. static bool fromsd[BUFSIZE];
  324. #endif
  325. #if NUM_SERVOS > 0
  326. Servo servos[NUM_SERVOS];
  327. #endif
  328. #ifdef CHDK
  329. unsigned long chdkHigh = 0;
  330. boolean chdkActive = false;
  331. #endif
  332. //===========================================================================
  333. //================================ Functions ================================
  334. //===========================================================================
  335. void get_arc_coordinates();
  336. bool setTargetedHotend(int code);
  337. void serial_echopair_P(const char *s_P, float v)
  338. { serialprintPGM(s_P); SERIAL_ECHO(v); }
  339. void serial_echopair_P(const char *s_P, double v)
  340. { serialprintPGM(s_P); SERIAL_ECHO(v); }
  341. void serial_echopair_P(const char *s_P, unsigned long v)
  342. { serialprintPGM(s_P); SERIAL_ECHO(v); }
  343. #ifdef SDSUPPORT
  344. #include "SdFatUtil.h"
  345. int freeMemory() { return SdFatUtil::FreeRam(); }
  346. #else
  347. extern "C" {
  348. extern unsigned int __bss_end;
  349. extern unsigned int __heap_start;
  350. extern void *__brkval;
  351. int freeMemory() {
  352. int free_memory;
  353. if ((int)__brkval == 0)
  354. free_memory = ((int)&free_memory) - ((int)&__bss_end);
  355. else
  356. free_memory = ((int)&free_memory) - ((int)__brkval);
  357. return free_memory;
  358. }
  359. }
  360. #endif //!SDSUPPORT
  361. //Injects the next command from the pending sequence of commands, when possible
  362. //Return false if and only if no command was pending
  363. static bool drain_queued_commands_P() {
  364. if (!queued_commands_P) return false;
  365. // Get the next 30 chars from the sequence of gcodes to run
  366. char cmd[30];
  367. strncpy_P(cmd, queued_commands_P, sizeof(cmd) - 1);
  368. cmd[sizeof(cmd) - 1] = '\0';
  369. // Look for the end of line, or the end of sequence
  370. size_t i = 0;
  371. char c;
  372. while((c = cmd[i]) && c != '\n') i++; // find the end of this gcode command
  373. cmd[i] = '\0';
  374. if (enquecommand(cmd)) { // buffer was not full (else we will retry later)
  375. if (c)
  376. queued_commands_P += i + 1; // move to next command
  377. else
  378. queued_commands_P = NULL; // will have no more commands in the sequence
  379. }
  380. return true;
  381. }
  382. //Record one or many commands to run from program memory.
  383. //Aborts the current queue, if any.
  384. //Note: drain_queued_commands_P() must be called repeatedly to drain the commands afterwards
  385. void enquecommands_P(const char* pgcode) {
  386. queued_commands_P = pgcode;
  387. drain_queued_commands_P(); // first command executed asap (when possible)
  388. }
  389. //adds a single command to the main command buffer, from RAM
  390. //that is really done in a non-safe way.
  391. //needs overworking someday
  392. //Returns false if it failed to do so
  393. bool enquecommand(const char *cmd)
  394. {
  395. if(*cmd==';')
  396. return false;
  397. if(buflen >= BUFSIZE)
  398. return false;
  399. //this is dangerous if a mixing of serial and this happens
  400. strcpy(&(cmdbuffer[bufindw][0]),cmd);
  401. SERIAL_ECHO_START;
  402. SERIAL_ECHOPGM(MSG_Enqueing);
  403. SERIAL_ECHO(cmdbuffer[bufindw]);
  404. SERIAL_ECHOLNPGM("\"");
  405. bufindw= (bufindw + 1)%BUFSIZE;
  406. buflen += 1;
  407. return true;
  408. }
  409. void setup_killpin()
  410. {
  411. #if HAS_KILL
  412. SET_INPUT(KILL_PIN);
  413. WRITE(KILL_PIN, HIGH);
  414. #endif
  415. }
  416. void setup_filrunoutpin()
  417. {
  418. #if HAS_FILRUNOUT
  419. pinMode(FILRUNOUT_PIN, INPUT);
  420. #ifdef ENDSTOPPULLUP_FIL_RUNOUT
  421. WRITE(FILLRUNOUT_PIN, HIGH);
  422. #endif
  423. #endif
  424. }
  425. // Set home pin
  426. void setup_homepin(void)
  427. {
  428. #if HAS_HOME
  429. SET_INPUT(HOME_PIN);
  430. WRITE(HOME_PIN, HIGH);
  431. #endif
  432. }
  433. void setup_photpin()
  434. {
  435. #if HAS_PHOTOGRAPH
  436. OUT_WRITE(PHOTOGRAPH_PIN, LOW);
  437. #endif
  438. }
  439. void setup_powerhold()
  440. {
  441. #if HAS_SUICIDE
  442. OUT_WRITE(SUICIDE_PIN, HIGH);
  443. #endif
  444. #if HAS_POWER_SWITCH
  445. #ifdef PS_DEFAULT_OFF
  446. OUT_WRITE(PS_ON_PIN, PS_ON_ASLEEP);
  447. #else
  448. OUT_WRITE(PS_ON_PIN, PS_ON_AWAKE);
  449. #endif
  450. #endif
  451. }
  452. void suicide()
  453. {
  454. #if HAS_SUICIDE
  455. OUT_WRITE(SUICIDE_PIN, LOW);
  456. #endif
  457. }
  458. void servo_init()
  459. {
  460. #if NUM_SERVOS >= 1 && HAS_SERVO_0
  461. servos[0].attach(SERVO0_PIN);
  462. #endif
  463. #if NUM_SERVOS >= 2 && HAS_SERVO_1
  464. servos[1].attach(SERVO1_PIN);
  465. #endif
  466. #if NUM_SERVOS >= 3 && HAS_SERVO_2
  467. servos[2].attach(SERVO2_PIN);
  468. #endif
  469. #if NUM_SERVOS >= 4 && HAS_SERVO_3
  470. servos[3].attach(SERVO3_PIN);
  471. #endif
  472. // Set position of Servo Endstops that are defined
  473. #ifdef SERVO_ENDSTOPS
  474. for (int i = 0; i < 3; i++)
  475. if (servo_endstops[i] >= 0)
  476. servos[servo_endstops[i]].write(servo_endstop_angles[i * 2 + 1]);
  477. #endif
  478. #if SERVO_LEVELING
  479. delay(PROBE_SERVO_DEACTIVATION_DELAY);
  480. servos[servo_endstops[Z_AXIS]].detach();
  481. #endif
  482. }
  483. void setup()
  484. {
  485. setup_killpin();
  486. setup_filrunoutpin();
  487. setup_powerhold();
  488. MYSERIAL.begin(BAUDRATE);
  489. SERIAL_PROTOCOLLNPGM("start");
  490. SERIAL_ECHO_START;
  491. // Check startup - does nothing if bootloader sets MCUSR to 0
  492. byte mcu = MCUSR;
  493. if(mcu & 1) SERIAL_ECHOLNPGM(MSG_POWERUP);
  494. if(mcu & 2) SERIAL_ECHOLNPGM(MSG_EXTERNAL_RESET);
  495. if(mcu & 4) SERIAL_ECHOLNPGM(MSG_BROWNOUT_RESET);
  496. if(mcu & 8) SERIAL_ECHOLNPGM(MSG_WATCHDOG_RESET);
  497. if(mcu & 32) SERIAL_ECHOLNPGM(MSG_SOFTWARE_RESET);
  498. MCUSR=0;
  499. SERIAL_ECHOPGM(MSG_MARLIN);
  500. SERIAL_ECHOLNPGM(STRING_VERSION);
  501. #ifdef STRING_VERSION_CONFIG_H
  502. #ifdef STRING_CONFIG_H_AUTHOR
  503. SERIAL_ECHO_START;
  504. SERIAL_ECHOPGM(MSG_CONFIGURATION_VER);
  505. SERIAL_ECHOPGM(STRING_VERSION_CONFIG_H);
  506. SERIAL_ECHOPGM(MSG_AUTHOR);
  507. SERIAL_ECHOLNPGM(STRING_CONFIG_H_AUTHOR);
  508. SERIAL_ECHOPGM("Compiled: ");
  509. SERIAL_ECHOLNPGM(__DATE__);
  510. #endif // STRING_CONFIG_H_AUTHOR
  511. #endif // STRING_VERSION_CONFIG_H
  512. SERIAL_ECHO_START;
  513. SERIAL_ECHOPGM(MSG_FREE_MEMORY);
  514. SERIAL_ECHO(freeMemory());
  515. SERIAL_ECHOPGM(MSG_PLANNER_BUFFER_BYTES);
  516. SERIAL_ECHOLN((int)sizeof(block_t)*BLOCK_BUFFER_SIZE);
  517. #ifdef SDSUPPORT
  518. for(int8_t i = 0; i < BUFSIZE; i++)
  519. {
  520. fromsd[i] = false;
  521. }
  522. #endif //!SDSUPPORT
  523. // loads data from EEPROM if available else uses defaults (and resets step acceleration rate)
  524. Config_RetrieveSettings();
  525. tp_init(); // Initialize temperature loop
  526. plan_init(); // Initialize planner;
  527. watchdog_init();
  528. st_init(); // Initialize stepper, this enables interrupts!
  529. setup_photpin();
  530. servo_init();
  531. lcd_init();
  532. _delay_ms(1000); // wait 1sec to display the splash screen
  533. #if HAS_CONTROLLERFAN
  534. SET_OUTPUT(CONTROLLERFAN_PIN); //Set pin used for driver cooling fan
  535. #endif
  536. #ifdef DIGIPOT_I2C
  537. digipot_i2c_init();
  538. #endif
  539. #ifdef Z_PROBE_SLED
  540. pinMode(SERVO0_PIN, OUTPUT);
  541. digitalWrite(SERVO0_PIN, LOW); // turn it off
  542. #endif // Z_PROBE_SLED
  543. setup_homepin();
  544. #ifdef STAT_LED_RED
  545. pinMode(STAT_LED_RED, OUTPUT);
  546. digitalWrite(STAT_LED_RED, LOW); // turn it off
  547. #endif
  548. #ifdef STAT_LED_BLUE
  549. pinMode(STAT_LED_BLUE, OUTPUT);
  550. digitalWrite(STAT_LED_BLUE, LOW); // turn it off
  551. #endif
  552. }
  553. void loop() {
  554. if (buflen < BUFSIZE - 1) get_command();
  555. #ifdef SDSUPPORT
  556. card.checkautostart(false);
  557. #endif
  558. if (buflen) {
  559. #ifdef SDSUPPORT
  560. if (card.saving) {
  561. if (strstr_P(cmdbuffer[bufindr], PSTR("M29")) == NULL) {
  562. card.write_command(cmdbuffer[bufindr]);
  563. if (card.logging)
  564. process_commands();
  565. else
  566. SERIAL_PROTOCOLLNPGM(MSG_OK);
  567. }
  568. else {
  569. card.closefile();
  570. SERIAL_PROTOCOLLNPGM(MSG_FILE_SAVED);
  571. }
  572. }
  573. else
  574. process_commands();
  575. #else
  576. process_commands();
  577. #endif // SDSUPPORT
  578. buflen--;
  579. bufindr = (bufindr + 1) % BUFSIZE;
  580. }
  581. // Check heater every n milliseconds
  582. manage_heater();
  583. manage_inactivity();
  584. checkHitEndstops();
  585. lcd_update();
  586. }
  587. void get_command()
  588. {
  589. if (drain_queued_commands_P()) // priority is given to non-serial commands
  590. return;
  591. while( MYSERIAL.available() > 0 && buflen < BUFSIZE) {
  592. serial_char = MYSERIAL.read();
  593. if(serial_char == '\n' ||
  594. serial_char == '\r' ||
  595. serial_count >= (MAX_CMD_SIZE - 1) )
  596. {
  597. // end of line == end of comment
  598. comment_mode = false;
  599. if(!serial_count) {
  600. // short cut for empty lines
  601. return;
  602. }
  603. cmdbuffer[bufindw][serial_count] = 0; //terminate string
  604. #ifdef SDSUPPORT
  605. fromsd[bufindw] = false;
  606. #endif //!SDSUPPORT
  607. if(strchr(cmdbuffer[bufindw], 'N') != NULL)
  608. {
  609. strchr_pointer = strchr(cmdbuffer[bufindw], 'N');
  610. gcode_N = (strtol(strchr_pointer + 1, NULL, 10));
  611. if(gcode_N != gcode_LastN+1 && (strstr_P(cmdbuffer[bufindw], PSTR("M110")) == NULL) ) {
  612. SERIAL_ERROR_START;
  613. SERIAL_ERRORPGM(MSG_ERR_LINE_NO);
  614. SERIAL_ERRORLN(gcode_LastN);
  615. //Serial.println(gcode_N);
  616. FlushSerialRequestResend();
  617. serial_count = 0;
  618. return;
  619. }
  620. if(strchr(cmdbuffer[bufindw], '*') != NULL)
  621. {
  622. byte checksum = 0;
  623. byte count = 0;
  624. while(cmdbuffer[bufindw][count] != '*') checksum = checksum^cmdbuffer[bufindw][count++];
  625. strchr_pointer = strchr(cmdbuffer[bufindw], '*');
  626. if(strtol(strchr_pointer + 1, NULL, 10) != checksum) {
  627. SERIAL_ERROR_START;
  628. SERIAL_ERRORPGM(MSG_ERR_CHECKSUM_MISMATCH);
  629. SERIAL_ERRORLN(gcode_LastN);
  630. FlushSerialRequestResend();
  631. serial_count = 0;
  632. return;
  633. }
  634. //if no errors, continue parsing
  635. }
  636. else
  637. {
  638. SERIAL_ERROR_START;
  639. SERIAL_ERRORPGM(MSG_ERR_NO_CHECKSUM);
  640. SERIAL_ERRORLN(gcode_LastN);
  641. FlushSerialRequestResend();
  642. serial_count = 0;
  643. return;
  644. }
  645. gcode_LastN = gcode_N;
  646. //if no errors, continue parsing
  647. }
  648. else // if we don't receive 'N' but still see '*'
  649. {
  650. if((strchr(cmdbuffer[bufindw], '*') != NULL))
  651. {
  652. SERIAL_ERROR_START;
  653. SERIAL_ERRORPGM(MSG_ERR_NO_LINENUMBER_WITH_CHECKSUM);
  654. SERIAL_ERRORLN(gcode_LastN);
  655. serial_count = 0;
  656. return;
  657. }
  658. }
  659. if((strchr(cmdbuffer[bufindw], 'G') != NULL)){
  660. strchr_pointer = strchr(cmdbuffer[bufindw], 'G');
  661. switch(strtol(strchr_pointer + 1, NULL, 10)){
  662. case 0:
  663. case 1:
  664. case 2:
  665. case 3:
  666. if (Stopped == true) {
  667. SERIAL_ERRORLNPGM(MSG_ERR_STOPPED);
  668. LCD_MESSAGEPGM(MSG_STOPPED);
  669. }
  670. break;
  671. default:
  672. break;
  673. }
  674. }
  675. //If command was e-stop process now
  676. if(strcmp(cmdbuffer[bufindw], "M112") == 0)
  677. kill();
  678. bufindw = (bufindw + 1)%BUFSIZE;
  679. buflen += 1;
  680. serial_count = 0; //clear buffer
  681. }
  682. else if(serial_char == '\\') { //Handle escapes
  683. if(MYSERIAL.available() > 0 && buflen < BUFSIZE) {
  684. // if we have one more character, copy it over
  685. serial_char = MYSERIAL.read();
  686. cmdbuffer[bufindw][serial_count++] = serial_char;
  687. }
  688. //otherwise do nothing
  689. }
  690. else { // its not a newline, carriage return or escape char
  691. if(serial_char == ';') comment_mode = true;
  692. if(!comment_mode) cmdbuffer[bufindw][serial_count++] = serial_char;
  693. }
  694. }
  695. #ifdef SDSUPPORT
  696. if(!card.sdprinting || serial_count!=0){
  697. return;
  698. }
  699. //'#' stops reading from SD to the buffer prematurely, so procedural macro calls are possible
  700. // if it occurs, stop_buffering is triggered and the buffer is ran dry.
  701. // this character _can_ occur in serial com, due to checksums. however, no checksums are used in SD printing
  702. static bool stop_buffering=false;
  703. if(buflen==0) stop_buffering=false;
  704. while( !card.eof() && buflen < BUFSIZE && !stop_buffering) {
  705. int16_t n=card.get();
  706. serial_char = (char)n;
  707. if(serial_char == '\n' ||
  708. serial_char == '\r' ||
  709. (serial_char == '#' && comment_mode == false) ||
  710. (serial_char == ':' && comment_mode == false) ||
  711. serial_count >= (MAX_CMD_SIZE - 1)||n==-1)
  712. {
  713. if(card.eof()){
  714. SERIAL_PROTOCOLLNPGM(MSG_FILE_PRINTED);
  715. stoptime=millis();
  716. char time[30];
  717. unsigned long t=(stoptime-starttime)/1000;
  718. int hours, minutes;
  719. minutes=(t/60)%60;
  720. hours=t/60/60;
  721. sprintf_P(time, PSTR("%i "MSG_END_HOUR" %i "MSG_END_MINUTE),hours, minutes);
  722. SERIAL_ECHO_START;
  723. SERIAL_ECHOLN(time);
  724. lcd_setstatus(time, true);
  725. card.printingHasFinished();
  726. card.checkautostart(true);
  727. }
  728. if(serial_char=='#')
  729. stop_buffering=true;
  730. if(!serial_count)
  731. {
  732. comment_mode = false; //for new command
  733. return; //if empty line
  734. }
  735. cmdbuffer[bufindw][serial_count] = 0; //terminate string
  736. // if(!comment_mode){
  737. fromsd[bufindw] = true;
  738. buflen += 1;
  739. bufindw = (bufindw + 1)%BUFSIZE;
  740. // }
  741. comment_mode = false; //for new command
  742. serial_count = 0; //clear buffer
  743. }
  744. else
  745. {
  746. if(serial_char == ';') comment_mode = true;
  747. if(!comment_mode) cmdbuffer[bufindw][serial_count++] = serial_char;
  748. }
  749. }
  750. #endif //SDSUPPORT
  751. }
  752. float code_value() {
  753. float ret;
  754. char *e = strchr(strchr_pointer, 'E');
  755. if (e) {
  756. *e = 0;
  757. ret = strtod(strchr_pointer+1, NULL);
  758. *e = 'E';
  759. }
  760. else
  761. ret = strtod(strchr_pointer+1, NULL);
  762. return ret;
  763. }
  764. long code_value_long() { return strtol(strchr_pointer + 1, NULL, 10); }
  765. int16_t code_value_short() { return (int16_t)strtol(strchr_pointer + 1, NULL, 10); }
  766. bool code_seen(char code) {
  767. strchr_pointer = strchr(cmdbuffer[bufindr], code);
  768. return (strchr_pointer != NULL); //Return True if a character was found
  769. }
  770. #define DEFINE_PGM_READ_ANY(type, reader) \
  771. static inline type pgm_read_any(const type *p) \
  772. { return pgm_read_##reader##_near(p); }
  773. DEFINE_PGM_READ_ANY(float, float);
  774. DEFINE_PGM_READ_ANY(signed char, byte);
  775. #define XYZ_CONSTS_FROM_CONFIG(type, array, CONFIG) \
  776. static const PROGMEM type array##_P[3] = \
  777. { X_##CONFIG, Y_##CONFIG, Z_##CONFIG }; \
  778. static inline type array(int axis) \
  779. { return pgm_read_any(&array##_P[axis]); }
  780. XYZ_CONSTS_FROM_CONFIG(float, base_min_pos, MIN_POS);
  781. XYZ_CONSTS_FROM_CONFIG(float, base_max_pos, MAX_POS);
  782. XYZ_CONSTS_FROM_CONFIG(float, base_home_pos, HOME_POS);
  783. XYZ_CONSTS_FROM_CONFIG(float, max_length, MAX_LENGTH);
  784. XYZ_CONSTS_FROM_CONFIG(float, home_bump_mm, HOME_BUMP_MM);
  785. XYZ_CONSTS_FROM_CONFIG(signed char, home_dir, HOME_DIR);
  786. #ifdef DUAL_X_CARRIAGE
  787. #define DXC_FULL_CONTROL_MODE 0
  788. #define DXC_AUTO_PARK_MODE 1
  789. #define DXC_DUPLICATION_MODE 2
  790. static int dual_x_carriage_mode = DEFAULT_DUAL_X_CARRIAGE_MODE;
  791. static float x_home_pos(int extruder) {
  792. if (extruder == 0)
  793. return base_home_pos(X_AXIS) + home_offset[X_AXIS];
  794. else
  795. // In dual carriage mode the extruder offset provides an override of the
  796. // second X-carriage offset when homed - otherwise X2_HOME_POS is used.
  797. // This allow soft recalibration of the second extruder offset position without firmware reflash
  798. // (through the M218 command).
  799. return (extruder_offset[X_AXIS][1] > 0) ? extruder_offset[X_AXIS][1] : X2_HOME_POS;
  800. }
  801. static int x_home_dir(int extruder) {
  802. return (extruder == 0) ? X_HOME_DIR : X2_HOME_DIR;
  803. }
  804. static float inactive_extruder_x_pos = X2_MAX_POS; // used in mode 0 & 1
  805. static bool active_extruder_parked = false; // used in mode 1 & 2
  806. static float raised_parked_position[NUM_AXIS]; // used in mode 1
  807. static unsigned long delayed_move_time = 0; // used in mode 1
  808. static float duplicate_extruder_x_offset = DEFAULT_DUPLICATION_X_OFFSET; // used in mode 2
  809. static float duplicate_extruder_temp_offset = 0; // used in mode 2
  810. bool extruder_duplication_enabled = false; // used in mode 2
  811. #endif //DUAL_X_CARRIAGE
  812. static void axis_is_at_home(int axis) {
  813. #ifdef DUAL_X_CARRIAGE
  814. if (axis == X_AXIS) {
  815. if (active_extruder != 0) {
  816. current_position[X_AXIS] = x_home_pos(active_extruder);
  817. min_pos[X_AXIS] = X2_MIN_POS;
  818. max_pos[X_AXIS] = max(extruder_offset[X_AXIS][1], X2_MAX_POS);
  819. return;
  820. }
  821. else if (dual_x_carriage_mode == DXC_DUPLICATION_MODE) {
  822. float xoff = home_offset[X_AXIS];
  823. current_position[X_AXIS] = base_home_pos(X_AXIS) + xoff;
  824. min_pos[X_AXIS] = base_min_pos(X_AXIS) + xoff;
  825. max_pos[X_AXIS] = min(base_max_pos(X_AXIS) + xoff, max(extruder_offset[X_AXIS][1], X2_MAX_POS) - duplicate_extruder_x_offset);
  826. return;
  827. }
  828. }
  829. #endif
  830. #ifdef SCARA
  831. float homeposition[3];
  832. if (axis < 2) {
  833. for (int i = 0; i < 3; i++) homeposition[i] = base_home_pos(i);
  834. // SERIAL_ECHOPGM("homeposition[x]= "); SERIAL_ECHO(homeposition[0]);
  835. // SERIAL_ECHOPGM("homeposition[y]= "); SERIAL_ECHOLN(homeposition[1]);
  836. // Works out real Homeposition angles using inverse kinematics,
  837. // and calculates homing offset using forward kinematics
  838. calculate_delta(homeposition);
  839. // SERIAL_ECHOPGM("base Theta= "); SERIAL_ECHO(delta[X_AXIS]);
  840. // SERIAL_ECHOPGM(" base Psi+Theta="); SERIAL_ECHOLN(delta[Y_AXIS]);
  841. for (int i = 0; i < 2; i++) delta[i] -= home_offset[i];
  842. // SERIAL_ECHOPGM("addhome X="); SERIAL_ECHO(home_offset[X_AXIS]);
  843. // SERIAL_ECHOPGM(" addhome Y="); SERIAL_ECHO(home_offset[Y_AXIS]);
  844. // SERIAL_ECHOPGM(" addhome Theta="); SERIAL_ECHO(delta[X_AXIS]);
  845. // SERIAL_ECHOPGM(" addhome Psi+Theta="); SERIAL_ECHOLN(delta[Y_AXIS]);
  846. calculate_SCARA_forward_Transform(delta);
  847. // SERIAL_ECHOPGM("Delta X="); SERIAL_ECHO(delta[X_AXIS]);
  848. // SERIAL_ECHOPGM(" Delta Y="); SERIAL_ECHOLN(delta[Y_AXIS]);
  849. current_position[axis] = delta[axis];
  850. // SCARA home positions are based on configuration since the actual limits are determined by the
  851. // inverse kinematic transform.
  852. min_pos[axis] = base_min_pos(axis); // + (delta[axis] - base_home_pos(axis));
  853. max_pos[axis] = base_max_pos(axis); // + (delta[axis] - base_home_pos(axis));
  854. }
  855. else {
  856. current_position[axis] = base_home_pos(axis) + home_offset[axis];
  857. min_pos[axis] = base_min_pos(axis) + home_offset[axis];
  858. max_pos[axis] = base_max_pos(axis) + home_offset[axis];
  859. }
  860. #else
  861. current_position[axis] = base_home_pos(axis) + home_offset[axis];
  862. min_pos[axis] = base_min_pos(axis) + home_offset[axis];
  863. max_pos[axis] = base_max_pos(axis) + home_offset[axis];
  864. #endif
  865. }
  866. /**
  867. * Some planner shorthand inline functions
  868. */
  869. inline void line_to_current_position() {
  870. plan_buffer_line(current_position[X_AXIS], current_position[Y_AXIS], current_position[Z_AXIS], current_position[E_AXIS], feedrate/60, active_extruder);
  871. }
  872. inline void line_to_z(float zPosition) {
  873. plan_buffer_line(current_position[X_AXIS], current_position[Y_AXIS], zPosition, current_position[E_AXIS], feedrate/60, active_extruder);
  874. }
  875. inline void line_to_destination() {
  876. plan_buffer_line(destination[X_AXIS], destination[Y_AXIS], destination[Z_AXIS], destination[E_AXIS], feedrate/60, active_extruder);
  877. }
  878. inline void sync_plan_position() {
  879. plan_set_position(current_position[X_AXIS], current_position[Y_AXIS], current_position[Z_AXIS], current_position[E_AXIS]);
  880. }
  881. #if defined(DELTA) || defined(SCARA)
  882. inline void sync_plan_position_delta() {
  883. calculate_delta(current_position);
  884. plan_set_position(delta[X_AXIS], delta[Y_AXIS], delta[Z_AXIS], current_position[E_AXIS]);
  885. }
  886. #endif
  887. inline void set_current_to_destination() { memcpy(current_position, destination, sizeof(current_position)); }
  888. inline void set_destination_to_current() { memcpy(destination, current_position, sizeof(destination)); }
  889. #ifdef ENABLE_AUTO_BED_LEVELING
  890. #ifdef DELTA
  891. /**
  892. * Calculate delta, start a line, and set current_position to destination
  893. */
  894. void prepare_move_raw() {
  895. refresh_cmd_timeout();
  896. calculate_delta(destination);
  897. plan_buffer_line(delta[X_AXIS], delta[Y_AXIS], delta[Z_AXIS], destination[E_AXIS], (feedrate/60)*(feedmultiply/100.0), active_extruder);
  898. set_current_to_destination();
  899. }
  900. #endif
  901. #ifdef AUTO_BED_LEVELING_GRID
  902. #ifndef DELTA
  903. static void set_bed_level_equation_lsq(double *plane_equation_coefficients) {
  904. vector_3 planeNormal = vector_3(-plane_equation_coefficients[0], -plane_equation_coefficients[1], 1);
  905. planeNormal.debug("planeNormal");
  906. plan_bed_level_matrix = matrix_3x3::create_look_at(planeNormal);
  907. //bedLevel.debug("bedLevel");
  908. //plan_bed_level_matrix.debug("bed level before");
  909. //vector_3 uncorrected_position = plan_get_position_mm();
  910. //uncorrected_position.debug("position before");
  911. vector_3 corrected_position = plan_get_position();
  912. //corrected_position.debug("position after");
  913. current_position[X_AXIS] = corrected_position.x;
  914. current_position[Y_AXIS] = corrected_position.y;
  915. current_position[Z_AXIS] = corrected_position.z;
  916. sync_plan_position();
  917. }
  918. #endif // !DELTA
  919. #else // !AUTO_BED_LEVELING_GRID
  920. static void set_bed_level_equation_3pts(float z_at_pt_1, float z_at_pt_2, float z_at_pt_3) {
  921. plan_bed_level_matrix.set_to_identity();
  922. vector_3 pt1 = vector_3(ABL_PROBE_PT_1_X, ABL_PROBE_PT_1_Y, z_at_pt_1);
  923. vector_3 pt2 = vector_3(ABL_PROBE_PT_2_X, ABL_PROBE_PT_2_Y, z_at_pt_2);
  924. vector_3 pt3 = vector_3(ABL_PROBE_PT_3_X, ABL_PROBE_PT_3_Y, z_at_pt_3);
  925. vector_3 planeNormal = vector_3::cross(pt1 - pt2, pt3 - pt2).get_normal();
  926. if (planeNormal.z < 0) {
  927. planeNormal.x = -planeNormal.x;
  928. planeNormal.y = -planeNormal.y;
  929. planeNormal.z = -planeNormal.z;
  930. }
  931. plan_bed_level_matrix = matrix_3x3::create_look_at(planeNormal);
  932. vector_3 corrected_position = plan_get_position();
  933. current_position[X_AXIS] = corrected_position.x;
  934. current_position[Y_AXIS] = corrected_position.y;
  935. current_position[Z_AXIS] = corrected_position.z;
  936. sync_plan_position();
  937. }
  938. #endif // !AUTO_BED_LEVELING_GRID
  939. static void run_z_probe() {
  940. #ifdef DELTA
  941. float start_z = current_position[Z_AXIS];
  942. long start_steps = st_get_position(Z_AXIS);
  943. // move down slowly until you find the bed
  944. feedrate = homing_feedrate[Z_AXIS] / 4;
  945. destination[Z_AXIS] = -10;
  946. prepare_move_raw();
  947. st_synchronize();
  948. endstops_hit_on_purpose(); // clear endstop hit flags
  949. // we have to let the planner know where we are right now as it is not where we said to go.
  950. long stop_steps = st_get_position(Z_AXIS);
  951. float mm = start_z - float(start_steps - stop_steps) / axis_steps_per_unit[Z_AXIS];
  952. current_position[Z_AXIS] = mm;
  953. sync_plan_position_delta();
  954. #else // !DELTA
  955. plan_bed_level_matrix.set_to_identity();
  956. feedrate = homing_feedrate[Z_AXIS];
  957. // move down until you find the bed
  958. float zPosition = -10;
  959. line_to_z(zPosition);
  960. st_synchronize();
  961. // we have to let the planner know where we are right now as it is not where we said to go.
  962. zPosition = st_get_position_mm(Z_AXIS);
  963. plan_set_position(current_position[X_AXIS], current_position[Y_AXIS], zPosition, current_position[E_AXIS]);
  964. // move up the retract distance
  965. zPosition += home_bump_mm(Z_AXIS);
  966. line_to_z(zPosition);
  967. st_synchronize();
  968. endstops_hit_on_purpose(); // clear endstop hit flags
  969. // move back down slowly to find bed
  970. if (homing_bump_divisor[Z_AXIS] >= 1)
  971. feedrate = homing_feedrate[Z_AXIS] / homing_bump_divisor[Z_AXIS];
  972. else {
  973. feedrate = homing_feedrate[Z_AXIS] / 10;
  974. SERIAL_ECHOLN("Warning: The Homing Bump Feedrate Divisor cannot be less than 1");
  975. }
  976. zPosition -= home_bump_mm(Z_AXIS) * 2;
  977. line_to_z(zPosition);
  978. st_synchronize();
  979. endstops_hit_on_purpose(); // clear endstop hit flags
  980. current_position[Z_AXIS] = st_get_position_mm(Z_AXIS);
  981. // make sure the planner knows where we are as it may be a bit different than we last said to move to
  982. sync_plan_position();
  983. #endif // !DELTA
  984. }
  985. /**
  986. *
  987. */
  988. static void do_blocking_move_to(float x, float y, float z) {
  989. float oldFeedRate = feedrate;
  990. #ifdef DELTA
  991. feedrate = XY_TRAVEL_SPEED;
  992. destination[X_AXIS] = x;
  993. destination[Y_AXIS] = y;
  994. destination[Z_AXIS] = z;
  995. prepare_move_raw();
  996. st_synchronize();
  997. #else
  998. feedrate = homing_feedrate[Z_AXIS];
  999. current_position[Z_AXIS] = z;
  1000. line_to_current_position();
  1001. st_synchronize();
  1002. feedrate = xy_travel_speed;
  1003. current_position[X_AXIS] = x;
  1004. current_position[Y_AXIS] = y;
  1005. line_to_current_position();
  1006. st_synchronize();
  1007. #endif
  1008. feedrate = oldFeedRate;
  1009. }
  1010. static void setup_for_endstop_move() {
  1011. saved_feedrate = feedrate;
  1012. saved_feedmultiply = feedmultiply;
  1013. feedmultiply = 100;
  1014. refresh_cmd_timeout();
  1015. enable_endstops(true);
  1016. }
  1017. static void clean_up_after_endstop_move() {
  1018. #ifdef ENDSTOPS_ONLY_FOR_HOMING
  1019. enable_endstops(false);
  1020. #endif
  1021. feedrate = saved_feedrate;
  1022. feedmultiply = saved_feedmultiply;
  1023. refresh_cmd_timeout();
  1024. }
  1025. static void deploy_z_probe() {
  1026. #ifdef SERVO_ENDSTOPS
  1027. // Engage Z Servo endstop if enabled
  1028. if (servo_endstops[Z_AXIS] >= 0) {
  1029. #if SERVO_LEVELING
  1030. servos[servo_endstops[Z_AXIS]].attach(0);
  1031. #endif
  1032. servos[servo_endstops[Z_AXIS]].write(servo_endstop_angles[Z_AXIS * 2]);
  1033. #if SERVO_LEVELING
  1034. delay(PROBE_SERVO_DEACTIVATION_DELAY);
  1035. servos[servo_endstops[Z_AXIS]].detach();
  1036. #endif
  1037. }
  1038. #elif defined(Z_PROBE_ALLEN_KEY)
  1039. feedrate = homing_feedrate[X_AXIS];
  1040. // Move to the start position to initiate deployment
  1041. destination[X_AXIS] = Z_PROBE_ALLEN_KEY_DEPLOY_X;
  1042. destination[Y_AXIS] = Z_PROBE_ALLEN_KEY_DEPLOY_Y;
  1043. destination[Z_AXIS] = Z_PROBE_ALLEN_KEY_DEPLOY_Z;
  1044. prepare_move_raw();
  1045. // Home X to touch the belt
  1046. feedrate = homing_feedrate[X_AXIS]/10;
  1047. destination[X_AXIS] = 0;
  1048. prepare_move_raw();
  1049. // Home Y for safety
  1050. feedrate = homing_feedrate[X_AXIS]/2;
  1051. destination[Y_AXIS] = 0;
  1052. prepare_move_raw();
  1053. st_synchronize();
  1054. #ifdef Z_PROBE_ENDSTOP
  1055. bool z_probe_endstop = (READ(Z_PROBE_PIN) != Z_PROBE_ENDSTOP_INVERTING);
  1056. if (z_probe_endstop)
  1057. #else
  1058. bool z_min_endstop = (READ(Z_MIN_PIN) != Z_MIN_ENDSTOP_INVERTING);
  1059. if (z_min_endstop)
  1060. #endif
  1061. {
  1062. if (!Stopped) {
  1063. SERIAL_ERROR_START;
  1064. SERIAL_ERRORLNPGM("Z-Probe failed to engage!");
  1065. LCD_ALERTMESSAGEPGM("Err: ZPROBE");
  1066. }
  1067. Stop();
  1068. }
  1069. #endif // Z_PROBE_ALLEN_KEY
  1070. }
  1071. static void stow_z_probe() {
  1072. #ifdef SERVO_ENDSTOPS
  1073. // Retract Z Servo endstop if enabled
  1074. if (servo_endstops[Z_AXIS] >= 0) {
  1075. #if Z_RAISE_AFTER_PROBING > 0
  1076. do_blocking_move_to(current_position[X_AXIS], current_position[Y_AXIS], current_position[Z_AXIS] + Z_RAISE_AFTER_PROBING);
  1077. st_synchronize();
  1078. #endif
  1079. #if SERVO_LEVELING
  1080. servos[servo_endstops[Z_AXIS]].attach(0);
  1081. #endif
  1082. servos[servo_endstops[Z_AXIS]].write(servo_endstop_angles[Z_AXIS * 2 + 1]);
  1083. #if SERVO_LEVELING
  1084. delay(PROBE_SERVO_DEACTIVATION_DELAY);
  1085. servos[servo_endstops[Z_AXIS]].detach();
  1086. #endif
  1087. }
  1088. #elif defined(Z_PROBE_ALLEN_KEY)
  1089. // Move up for safety
  1090. feedrate = homing_feedrate[X_AXIS];
  1091. destination[Z_AXIS] = current_position[Z_AXIS] + Z_RAISE_AFTER_PROBING;
  1092. prepare_move_raw();
  1093. // Move to the start position to initiate retraction
  1094. destination[X_AXIS] = Z_PROBE_ALLEN_KEY_STOW_X;
  1095. destination[Y_AXIS] = Z_PROBE_ALLEN_KEY_STOW_Y;
  1096. destination[Z_AXIS] = Z_PROBE_ALLEN_KEY_STOW_Z;
  1097. prepare_move_raw();
  1098. // Move the nozzle down to push the probe into retracted position
  1099. feedrate = homing_feedrate[Z_AXIS]/10;
  1100. destination[Z_AXIS] = current_position[Z_AXIS] - Z_PROBE_ALLEN_KEY_STOW_DEPTH;
  1101. prepare_move_raw();
  1102. // Move up for safety
  1103. feedrate = homing_feedrate[Z_AXIS]/2;
  1104. destination[Z_AXIS] = current_position[Z_AXIS] + Z_PROBE_ALLEN_KEY_STOW_DEPTH * 2;
  1105. prepare_move_raw();
  1106. // Home XY for safety
  1107. feedrate = homing_feedrate[X_AXIS]/2;
  1108. destination[X_AXIS] = 0;
  1109. destination[Y_AXIS] = 0;
  1110. prepare_move_raw();
  1111. st_synchronize();
  1112. #ifdef Z_PROBE_ENDSTOP
  1113. bool z_probe_endstop = (READ(Z_PROBE_PIN) != Z_PROBE_ENDSTOP_INVERTING);
  1114. if (!z_probe_endstop)
  1115. #else
  1116. bool z_min_endstop = (READ(Z_MIN_PIN) != Z_MIN_ENDSTOP_INVERTING);
  1117. if (!z_min_endstop)
  1118. #endif
  1119. {
  1120. if (!Stopped) {
  1121. SERIAL_ERROR_START;
  1122. SERIAL_ERRORLNPGM("Z-Probe failed to retract!");
  1123. LCD_ALERTMESSAGEPGM("Err: ZPROBE");
  1124. }
  1125. Stop();
  1126. }
  1127. #endif
  1128. }
  1129. enum ProbeAction {
  1130. ProbeStay = 0,
  1131. ProbeDeploy = BIT(0),
  1132. ProbeStow = BIT(1),
  1133. ProbeDeployAndStow = (ProbeDeploy | ProbeStow)
  1134. };
  1135. // Probe bed height at position (x,y), returns the measured z value
  1136. static float probe_pt(float x, float y, float z_before, ProbeAction retract_action=ProbeDeployAndStow, int verbose_level=1) {
  1137. // move to right place
  1138. do_blocking_move_to(current_position[X_AXIS], current_position[Y_AXIS], z_before);
  1139. do_blocking_move_to(x - X_PROBE_OFFSET_FROM_EXTRUDER, y - Y_PROBE_OFFSET_FROM_EXTRUDER, current_position[Z_AXIS]);
  1140. #if !defined(Z_PROBE_SLED) && !defined(Z_PROBE_ALLEN_KEY)
  1141. if (retract_action & ProbeDeploy) deploy_z_probe();
  1142. #endif
  1143. run_z_probe();
  1144. float measured_z = current_position[Z_AXIS];
  1145. #if Z_RAISE_BETWEEN_PROBINGS > 0
  1146. if (retract_action == ProbeStay) {
  1147. do_blocking_move_to(current_position[X_AXIS], current_position[Y_AXIS], current_position[Z_AXIS] + Z_RAISE_BETWEEN_PROBINGS);
  1148. st_synchronize();
  1149. }
  1150. #endif
  1151. #if !defined(Z_PROBE_SLED) && !defined(Z_PROBE_ALLEN_KEY)
  1152. if (retract_action & ProbeStow) stow_z_probe();
  1153. #endif
  1154. if (verbose_level > 2) {
  1155. SERIAL_PROTOCOLPGM(MSG_BED);
  1156. SERIAL_PROTOCOLPGM(" X: ");
  1157. SERIAL_PROTOCOL_F(x, 3);
  1158. SERIAL_PROTOCOLPGM(" Y: ");
  1159. SERIAL_PROTOCOL_F(y, 3);
  1160. SERIAL_PROTOCOLPGM(" Z: ");
  1161. SERIAL_PROTOCOL_F(measured_z, 3);
  1162. SERIAL_EOL;
  1163. }
  1164. return measured_z;
  1165. }
  1166. #ifdef DELTA
  1167. /**
  1168. * All DELTA leveling in the Marlin uses NONLINEAR_BED_LEVELING
  1169. */
  1170. static void extrapolate_one_point(int x, int y, int xdir, int ydir) {
  1171. if (bed_level[x][y] != 0.0) {
  1172. return; // Don't overwrite good values.
  1173. }
  1174. float a = 2*bed_level[x+xdir][y] - bed_level[x+xdir*2][y]; // Left to right.
  1175. float b = 2*bed_level[x][y+ydir] - bed_level[x][y+ydir*2]; // Front to back.
  1176. float c = 2*bed_level[x+xdir][y+ydir] - bed_level[x+xdir*2][y+ydir*2]; // Diagonal.
  1177. float median = c; // Median is robust (ignores outliers).
  1178. if (a < b) {
  1179. if (b < c) median = b;
  1180. if (c < a) median = a;
  1181. } else { // b <= a
  1182. if (c < b) median = b;
  1183. if (a < c) median = a;
  1184. }
  1185. bed_level[x][y] = median;
  1186. }
  1187. // Fill in the unprobed points (corners of circular print surface)
  1188. // using linear extrapolation, away from the center.
  1189. static void extrapolate_unprobed_bed_level() {
  1190. int half = (AUTO_BED_LEVELING_GRID_POINTS-1)/2;
  1191. for (int y = 0; y <= half; y++) {
  1192. for (int x = 0; x <= half; x++) {
  1193. if (x + y < 3) continue;
  1194. extrapolate_one_point(half-x, half-y, x>1?+1:0, y>1?+1:0);
  1195. extrapolate_one_point(half+x, half-y, x>1?-1:0, y>1?+1:0);
  1196. extrapolate_one_point(half-x, half+y, x>1?+1:0, y>1?-1:0);
  1197. extrapolate_one_point(half+x, half+y, x>1?-1:0, y>1?-1:0);
  1198. }
  1199. }
  1200. }
  1201. // Print calibration results for plotting or manual frame adjustment.
  1202. static void print_bed_level() {
  1203. for (int y = 0; y < AUTO_BED_LEVELING_GRID_POINTS; y++) {
  1204. for (int x = 0; x < AUTO_BED_LEVELING_GRID_POINTS; x++) {
  1205. SERIAL_PROTOCOL_F(bed_level[x][y], 2);
  1206. SERIAL_PROTOCOLCHAR(' ');
  1207. }
  1208. SERIAL_EOL;
  1209. }
  1210. }
  1211. // Reset calibration results to zero.
  1212. void reset_bed_level() {
  1213. for (int y = 0; y < AUTO_BED_LEVELING_GRID_POINTS; y++) {
  1214. for (int x = 0; x < AUTO_BED_LEVELING_GRID_POINTS; x++) {
  1215. bed_level[x][y] = 0.0;
  1216. }
  1217. }
  1218. }
  1219. #endif // DELTA
  1220. #endif // ENABLE_AUTO_BED_LEVELING
  1221. /**
  1222. * Home an individual axis
  1223. */
  1224. #define HOMEAXIS(LETTER) homeaxis(LETTER##_AXIS)
  1225. static void homeaxis(int axis) {
  1226. #define HOMEAXIS_DO(LETTER) \
  1227. ((LETTER##_MIN_PIN > -1 && LETTER##_HOME_DIR==-1) || (LETTER##_MAX_PIN > -1 && LETTER##_HOME_DIR==1))
  1228. if (axis == X_AXIS ? HOMEAXIS_DO(X) : axis == Y_AXIS ? HOMEAXIS_DO(Y) : axis == Z_AXIS ? HOMEAXIS_DO(Z) : 0) {
  1229. int axis_home_dir;
  1230. #ifdef DUAL_X_CARRIAGE
  1231. if (axis == X_AXIS) axis_home_dir = x_home_dir(active_extruder);
  1232. #else
  1233. axis_home_dir = home_dir(axis);
  1234. #endif
  1235. // Set the axis position as setup for the move
  1236. current_position[axis] = 0;
  1237. sync_plan_position();
  1238. // Engage Servo endstop if enabled
  1239. #if defined(SERVO_ENDSTOPS) && !defined(Z_PROBE_SLED)
  1240. #if SERVO_LEVELING
  1241. if (axis == Z_AXIS) deploy_z_probe(); else
  1242. #endif
  1243. {
  1244. if (servo_endstops[axis] > -1)
  1245. servos[servo_endstops[axis]].write(servo_endstop_angles[axis * 2]);
  1246. }
  1247. #endif // SERVO_ENDSTOPS && !Z_PROBE_SLED
  1248. #ifdef Z_DUAL_ENDSTOPS
  1249. if (axis == Z_AXIS) In_Homing_Process(true);
  1250. #endif
  1251. // Move towards the endstop until an endstop is triggered
  1252. destination[axis] = 1.5 * max_length(axis) * axis_home_dir;
  1253. feedrate = homing_feedrate[axis];
  1254. line_to_destination();
  1255. st_synchronize();
  1256. // Set the axis position as setup for the move
  1257. current_position[axis] = 0;
  1258. sync_plan_position();
  1259. // Move away from the endstop by the axis HOME_BUMP_MM
  1260. destination[axis] = -home_bump_mm(axis) * axis_home_dir;
  1261. line_to_destination();
  1262. st_synchronize();
  1263. // Slow down the feedrate for the next move
  1264. if (homing_bump_divisor[axis] >= 1)
  1265. feedrate = homing_feedrate[axis] / homing_bump_divisor[axis];
  1266. else {
  1267. feedrate = homing_feedrate[axis] / 10;
  1268. SERIAL_ECHOLNPGM("Warning: The Homing Bump Feedrate Divisor cannot be less than 1");
  1269. }
  1270. // Move slowly towards the endstop until triggered
  1271. destination[axis] = 2 * home_bump_mm(axis) * axis_home_dir;
  1272. line_to_destination();
  1273. st_synchronize();
  1274. #ifdef Z_DUAL_ENDSTOPS
  1275. if (axis == Z_AXIS) {
  1276. float adj = fabs(z_endstop_adj);
  1277. bool lockZ1;
  1278. if (axis_home_dir > 0) {
  1279. adj = -adj;
  1280. lockZ1 = (z_endstop_adj > 0);
  1281. }
  1282. else
  1283. lockZ1 = (z_endstop_adj < 0);
  1284. if (lockZ1) Lock_z_motor(true); else Lock_z2_motor(true);
  1285. sync_plan_position();
  1286. // Move to the adjusted endstop height
  1287. feedrate = homing_feedrate[axis];
  1288. destination[Z_AXIS] = adj;
  1289. line_to_destination();
  1290. st_synchronize();
  1291. if (lockZ1) Lock_z_motor(false); else Lock_z2_motor(false);
  1292. In_Homing_Process(false);
  1293. } // Z_AXIS
  1294. #endif
  1295. #ifdef DELTA
  1296. // retrace by the amount specified in endstop_adj
  1297. if (endstop_adj[axis] * axis_home_dir < 0) {
  1298. sync_plan_position();
  1299. destination[axis] = endstop_adj[axis];
  1300. line_to_destination();
  1301. st_synchronize();
  1302. }
  1303. #endif
  1304. // Set the axis position to its home position (plus home offsets)
  1305. axis_is_at_home(axis);
  1306. destination[axis] = current_position[axis];
  1307. feedrate = 0.0;
  1308. endstops_hit_on_purpose(); // clear endstop hit flags
  1309. axis_known_position[axis] = true;
  1310. // Retract Servo endstop if enabled
  1311. #ifdef SERVO_ENDSTOPS
  1312. if (servo_endstops[axis] > -1)
  1313. servos[servo_endstops[axis]].write(servo_endstop_angles[axis * 2 + 1]);
  1314. #endif
  1315. #if SERVO_LEVELING && !defined(Z_PROBE_SLED)
  1316. if (axis == Z_AXIS) stow_z_probe();
  1317. #endif
  1318. }
  1319. }
  1320. #ifdef FWRETRACT
  1321. void retract(bool retracting, bool swapretract = false) {
  1322. if (retracting == retracted[active_extruder]) return;
  1323. float oldFeedrate = feedrate;
  1324. set_destination_to_current();
  1325. if (retracting) {
  1326. feedrate = retract_feedrate * 60;
  1327. current_position[E_AXIS] += (swapretract ? retract_length_swap : retract_length) / volumetric_multiplier[active_extruder];
  1328. plan_set_e_position(current_position[E_AXIS]);
  1329. prepare_move();
  1330. if (retract_zlift > 0.01) {
  1331. current_position[Z_AXIS] -= retract_zlift;
  1332. #ifdef DELTA
  1333. sync_plan_position_delta();
  1334. #else
  1335. sync_plan_position();
  1336. #endif
  1337. prepare_move();
  1338. }
  1339. }
  1340. else {
  1341. if (retract_zlift > 0.01) {
  1342. current_position[Z_AXIS] += retract_zlift;
  1343. #ifdef DELTA
  1344. sync_plan_position_delta();
  1345. #else
  1346. sync_plan_position();
  1347. #endif
  1348. //prepare_move();
  1349. }
  1350. feedrate = retract_recover_feedrate * 60;
  1351. float move_e = swapretract ? retract_length_swap + retract_recover_length_swap : retract_length + retract_recover_length;
  1352. current_position[E_AXIS] -= move_e / volumetric_multiplier[active_extruder];
  1353. plan_set_e_position(current_position[E_AXIS]);
  1354. prepare_move();
  1355. }
  1356. feedrate = oldFeedrate;
  1357. retracted[active_extruder] = retract;
  1358. } // retract()
  1359. #endif // FWRETRACT
  1360. #ifdef Z_PROBE_SLED
  1361. #ifndef SLED_DOCKING_OFFSET
  1362. #define SLED_DOCKING_OFFSET 0
  1363. #endif
  1364. //
  1365. // Method to dock/undock a sled designed by Charles Bell.
  1366. //
  1367. // dock[in] If true, move to MAX_X and engage the electromagnet
  1368. // offset[in] The additional distance to move to adjust docking location
  1369. //
  1370. static void dock_sled(bool dock, int offset=0) {
  1371. if (!axis_known_position[X_AXIS] || !axis_known_position[Y_AXIS]) {
  1372. LCD_MESSAGEPGM(MSG_POSITION_UNKNOWN);
  1373. SERIAL_ECHO_START;
  1374. SERIAL_ECHOLNPGM(MSG_POSITION_UNKNOWN);
  1375. return;
  1376. }
  1377. if (dock) {
  1378. do_blocking_move_to(X_MAX_POS + SLED_DOCKING_OFFSET + offset, current_position[Y_AXIS], current_position[Z_AXIS]);
  1379. digitalWrite(SERVO0_PIN, LOW); // turn off magnet
  1380. } else {
  1381. float z_loc = current_position[Z_AXIS];
  1382. if (z_loc < Z_RAISE_BEFORE_PROBING + 5) z_loc = Z_RAISE_BEFORE_PROBING;
  1383. do_blocking_move_to(X_MAX_POS + SLED_DOCKING_OFFSET + offset, Y_PROBE_OFFSET_FROM_EXTRUDER, z_loc);
  1384. digitalWrite(SERVO0_PIN, HIGH); // turn on magnet
  1385. }
  1386. }
  1387. #endif // Z_PROBE_SLED
  1388. /**
  1389. *
  1390. * G-Code Handler functions
  1391. *
  1392. */
  1393. /**
  1394. * G0, G1: Coordinated movement of X Y Z E axes
  1395. */
  1396. inline void gcode_G0_G1() {
  1397. if (!Stopped) {
  1398. get_coordinates(); // For X Y Z E F
  1399. #ifdef FWRETRACT
  1400. if (autoretract_enabled)
  1401. if (!(code_seen('X') || code_seen('Y') || code_seen('Z')) && code_seen('E')) {
  1402. float echange = destination[E_AXIS] - current_position[E_AXIS];
  1403. // Is this move an attempt to retract or recover?
  1404. if ((echange < -MIN_RETRACT && !retracted[active_extruder]) || (echange > MIN_RETRACT && retracted[active_extruder])) {
  1405. current_position[E_AXIS] = destination[E_AXIS]; // hide the slicer-generated retract/recover from calculations
  1406. plan_set_e_position(current_position[E_AXIS]); // AND from the planner
  1407. retract(!retracted[active_extruder]);
  1408. return;
  1409. }
  1410. }
  1411. #endif //FWRETRACT
  1412. prepare_move();
  1413. //ClearToSend();
  1414. }
  1415. }
  1416. /**
  1417. * G2: Clockwise Arc
  1418. * G3: Counterclockwise Arc
  1419. */
  1420. inline void gcode_G2_G3(bool clockwise) {
  1421. if (!Stopped) {
  1422. get_arc_coordinates();
  1423. prepare_arc_move(clockwise);
  1424. }
  1425. }
  1426. /**
  1427. * G4: Dwell S<seconds> or P<milliseconds>
  1428. */
  1429. inline void gcode_G4() {
  1430. unsigned long codenum = 0;
  1431. LCD_MESSAGEPGM(MSG_DWELL);
  1432. if (code_seen('P')) codenum = code_value_long(); // milliseconds to wait
  1433. if (code_seen('S')) codenum = code_value_long() * 1000; // seconds to wait
  1434. st_synchronize();
  1435. refresh_cmd_timeout();
  1436. codenum += previous_millis_cmd; // keep track of when we started waiting
  1437. while (millis() < codenum) {
  1438. manage_heater();
  1439. manage_inactivity();
  1440. lcd_update();
  1441. }
  1442. }
  1443. #ifdef FWRETRACT
  1444. /**
  1445. * G10 - Retract filament according to settings of M207
  1446. * G11 - Recover filament according to settings of M208
  1447. */
  1448. inline void gcode_G10_G11(bool doRetract=false) {
  1449. #if EXTRUDERS > 1
  1450. if (doRetract) {
  1451. retracted_swap[active_extruder] = (code_seen('S') && code_value_short() == 1); // checks for swap retract argument
  1452. }
  1453. #endif
  1454. retract(doRetract
  1455. #if EXTRUDERS > 1
  1456. , retracted_swap[active_extruder]
  1457. #endif
  1458. );
  1459. }
  1460. #endif //FWRETRACT
  1461. /**
  1462. * G28: Home all axes according to settings
  1463. *
  1464. * Parameters
  1465. *
  1466. * None Home to all axes with no parameters.
  1467. * With QUICK_HOME enabled XY will home together, then Z.
  1468. *
  1469. * Cartesian parameters
  1470. *
  1471. * X Home to the X endstop
  1472. * Y Home to the Y endstop
  1473. * Z Home to the Z endstop
  1474. *
  1475. * If numbers are included with XYZ set the position as with G92
  1476. * Currently adds the home_offset, which may be wrong and removed soon.
  1477. *
  1478. * Xn Home X, setting X to n + home_offset[X_AXIS]
  1479. * Yn Home Y, setting Y to n + home_offset[Y_AXIS]
  1480. * Zn Home Z, setting Z to n + home_offset[Z_AXIS]
  1481. */
  1482. inline void gcode_G28() {
  1483. // For auto bed leveling, clear the level matrix
  1484. #ifdef ENABLE_AUTO_BED_LEVELING
  1485. plan_bed_level_matrix.set_to_identity();
  1486. #ifdef DELTA
  1487. reset_bed_level();
  1488. #endif
  1489. #endif
  1490. // For manual bed leveling deactivate the matrix temporarily
  1491. #ifdef MESH_BED_LEVELING
  1492. uint8_t mbl_was_active = mbl.active;
  1493. mbl.active = 0;
  1494. #endif
  1495. saved_feedrate = feedrate;
  1496. saved_feedmultiply = feedmultiply;
  1497. feedmultiply = 100;
  1498. refresh_cmd_timeout();
  1499. enable_endstops(true);
  1500. set_destination_to_current();
  1501. feedrate = 0.0;
  1502. #ifdef DELTA
  1503. // A delta can only safely home all axis at the same time
  1504. // all axis have to home at the same time
  1505. // Pretend the current position is 0,0,0
  1506. for (int i = X_AXIS; i <= Z_AXIS; i++) current_position[i] = 0;
  1507. sync_plan_position();
  1508. // Move all carriages up together until the first endstop is hit.
  1509. for (int i = X_AXIS; i <= Z_AXIS; i++) destination[i] = 3 * Z_MAX_LENGTH;
  1510. feedrate = 1.732 * homing_feedrate[X_AXIS];
  1511. line_to_destination();
  1512. st_synchronize();
  1513. endstops_hit_on_purpose(); // clear endstop hit flags
  1514. // Destination reached
  1515. for (int i = X_AXIS; i <= Z_AXIS; i++) current_position[i] = destination[i];
  1516. // take care of back off and rehome now we are all at the top
  1517. HOMEAXIS(X);
  1518. HOMEAXIS(Y);
  1519. HOMEAXIS(Z);
  1520. sync_plan_position_delta();
  1521. #else // NOT DELTA
  1522. bool homeX = code_seen(axis_codes[X_AXIS]),
  1523. homeY = code_seen(axis_codes[Y_AXIS]),
  1524. homeZ = code_seen(axis_codes[Z_AXIS]);
  1525. home_all_axis = !(homeX || homeY || homeZ) || (homeX && homeY && homeZ);
  1526. #if Z_HOME_DIR > 0 // If homing away from BED do Z first
  1527. if (home_all_axis || homeZ) HOMEAXIS(Z);
  1528. #elif !defined(Z_SAFE_HOMING) && defined(Z_RAISE_BEFORE_HOMING) && Z_RAISE_BEFORE_HOMING > 0
  1529. // Raise Z before homing any other axes
  1530. if (home_all_axis || homeZ) {
  1531. destination[Z_AXIS] = -Z_RAISE_BEFORE_HOMING * home_dir(Z_AXIS); // Set destination away from bed
  1532. feedrate = max_feedrate[Z_AXIS] * 60;
  1533. line_to_destination();
  1534. st_synchronize();
  1535. }
  1536. #endif
  1537. #ifdef QUICK_HOME
  1538. if (home_all_axis || (homeX && homeY)) { // First diagonal move
  1539. current_position[X_AXIS] = current_position[Y_AXIS] = 0;
  1540. #ifdef DUAL_X_CARRIAGE
  1541. int x_axis_home_dir = x_home_dir(active_extruder);
  1542. extruder_duplication_enabled = false;
  1543. #else
  1544. int x_axis_home_dir = home_dir(X_AXIS);
  1545. #endif
  1546. sync_plan_position();
  1547. float mlx = max_length(X_AXIS), mly = max_length(Y_AXIS),
  1548. mlratio = mlx>mly ? mly/mlx : mlx/mly;
  1549. destination[X_AXIS] = 1.5 * mlx * x_axis_home_dir;
  1550. destination[Y_AXIS] = 1.5 * mly * home_dir(Y_AXIS);
  1551. feedrate = min(homing_feedrate[X_AXIS], homing_feedrate[Y_AXIS]) * sqrt(mlratio * mlratio + 1);
  1552. line_to_destination();
  1553. st_synchronize();
  1554. axis_is_at_home(X_AXIS);
  1555. axis_is_at_home(Y_AXIS);
  1556. sync_plan_position();
  1557. destination[X_AXIS] = current_position[X_AXIS];
  1558. destination[Y_AXIS] = current_position[Y_AXIS];
  1559. line_to_destination();
  1560. feedrate = 0.0;
  1561. st_synchronize();
  1562. endstops_hit_on_purpose(); // clear endstop hit flags
  1563. current_position[X_AXIS] = destination[X_AXIS];
  1564. current_position[Y_AXIS] = destination[Y_AXIS];
  1565. #ifndef SCARA
  1566. current_position[Z_AXIS] = destination[Z_AXIS];
  1567. #endif
  1568. }
  1569. #endif // QUICK_HOME
  1570. // Home X
  1571. if (home_all_axis || homeX) {
  1572. #ifdef DUAL_X_CARRIAGE
  1573. int tmp_extruder = active_extruder;
  1574. extruder_duplication_enabled = false;
  1575. active_extruder = !active_extruder;
  1576. HOMEAXIS(X);
  1577. inactive_extruder_x_pos = current_position[X_AXIS];
  1578. active_extruder = tmp_extruder;
  1579. HOMEAXIS(X);
  1580. // reset state used by the different modes
  1581. memcpy(raised_parked_position, current_position, sizeof(raised_parked_position));
  1582. delayed_move_time = 0;
  1583. active_extruder_parked = true;
  1584. #else
  1585. HOMEAXIS(X);
  1586. #endif
  1587. }
  1588. // Home Y
  1589. if (home_all_axis || homeY) HOMEAXIS(Y);
  1590. // Set the X position, if included
  1591. // Adds the home_offset as well, which may be wrong
  1592. if (code_seen(axis_codes[X_AXIS])) {
  1593. float v = code_value();
  1594. if (v) current_position[X_AXIS] = v
  1595. #ifndef SCARA
  1596. + home_offset[X_AXIS]
  1597. #endif
  1598. ;
  1599. }
  1600. // Set the Y position, if included
  1601. // Adds the home_offset as well, which may be wrong
  1602. if (code_seen(axis_codes[Y_AXIS])) {
  1603. float v = code_value();
  1604. if (v) current_position[Y_AXIS] = v
  1605. #ifndef SCARA
  1606. + home_offset[Y_AXIS]
  1607. #endif
  1608. ;
  1609. }
  1610. // Home Z last if homing towards the bed
  1611. #if Z_HOME_DIR < 0
  1612. #ifndef Z_SAFE_HOMING
  1613. if (home_all_axis || homeZ) HOMEAXIS(Z);
  1614. #else // Z_SAFE_HOMING
  1615. if (home_all_axis) {
  1616. destination[X_AXIS] = round(Z_SAFE_HOMING_X_POINT - X_PROBE_OFFSET_FROM_EXTRUDER);
  1617. destination[Y_AXIS] = round(Z_SAFE_HOMING_Y_POINT - Y_PROBE_OFFSET_FROM_EXTRUDER);
  1618. destination[Z_AXIS] = -Z_RAISE_BEFORE_HOMING * home_dir(Z_AXIS); // Set destination away from bed
  1619. feedrate = XY_TRAVEL_SPEED;
  1620. current_position[Z_AXIS] = 0;
  1621. sync_plan_position();
  1622. line_to_destination();
  1623. st_synchronize();
  1624. current_position[X_AXIS] = destination[X_AXIS];
  1625. current_position[Y_AXIS] = destination[Y_AXIS];
  1626. HOMEAXIS(Z);
  1627. }
  1628. // Let's see if X and Y are homed and probe is inside bed area.
  1629. if (homeZ) {
  1630. if (axis_known_position[X_AXIS] && axis_known_position[Y_AXIS]) {
  1631. float cpx = current_position[X_AXIS], cpy = current_position[Y_AXIS];
  1632. if ( cpx >= X_MIN_POS - X_PROBE_OFFSET_FROM_EXTRUDER
  1633. && cpx <= X_MAX_POS - X_PROBE_OFFSET_FROM_EXTRUDER
  1634. && cpy >= Y_MIN_POS - Y_PROBE_OFFSET_FROM_EXTRUDER
  1635. && cpy <= Y_MAX_POS - Y_PROBE_OFFSET_FROM_EXTRUDER) {
  1636. current_position[Z_AXIS] = 0;
  1637. plan_set_position(cpx, cpy, 0, current_position[E_AXIS]);
  1638. destination[Z_AXIS] = -Z_RAISE_BEFORE_HOMING * home_dir(Z_AXIS); // Set destination away from bed
  1639. feedrate = max_feedrate[Z_AXIS] * 60; // max_feedrate is in mm/s. line_to_destination is feedrate/60.
  1640. line_to_destination();
  1641. st_synchronize();
  1642. HOMEAXIS(Z);
  1643. }
  1644. else {
  1645. LCD_MESSAGEPGM(MSG_ZPROBE_OUT);
  1646. SERIAL_ECHO_START;
  1647. SERIAL_ECHOLNPGM(MSG_ZPROBE_OUT);
  1648. }
  1649. }
  1650. else {
  1651. LCD_MESSAGEPGM(MSG_POSITION_UNKNOWN);
  1652. SERIAL_ECHO_START;
  1653. SERIAL_ECHOLNPGM(MSG_POSITION_UNKNOWN);
  1654. }
  1655. }
  1656. #endif // Z_SAFE_HOMING
  1657. #endif // Z_HOME_DIR < 0
  1658. // Set the Z position, if included
  1659. // Adds the home_offset as well, which may be wrong
  1660. if (code_seen(axis_codes[Z_AXIS])) {
  1661. float v = code_value();
  1662. if (v) current_position[Z_AXIS] = v + home_offset[Z_AXIS];
  1663. }
  1664. #if defined(ENABLE_AUTO_BED_LEVELING) && (Z_HOME_DIR < 0)
  1665. if (home_all_axis || homeZ) current_position[Z_AXIS] += zprobe_zoffset; // Add Z_Probe offset (the distance is negative)
  1666. #endif
  1667. sync_plan_position();
  1668. #endif // else DELTA
  1669. #ifdef SCARA
  1670. sync_plan_position_delta();
  1671. #endif
  1672. #ifdef ENDSTOPS_ONLY_FOR_HOMING
  1673. enable_endstops(false);
  1674. #endif
  1675. // For manual leveling move back to 0,0
  1676. #ifdef MESH_BED_LEVELING
  1677. if (mbl_was_active) {
  1678. current_position[X_AXIS] = mbl.get_x(0);
  1679. current_position[Y_AXIS] = mbl.get_y(0);
  1680. set_destination_to_current();
  1681. feedrate = homing_feedrate[X_AXIS];
  1682. line_to_destination();
  1683. st_synchronize();
  1684. current_position[Z_AXIS] = MESH_HOME_SEARCH_Z;
  1685. sync_plan_position();
  1686. mbl.active = 1;
  1687. }
  1688. #endif
  1689. feedrate = saved_feedrate;
  1690. feedmultiply = saved_feedmultiply;
  1691. refresh_cmd_timeout();
  1692. endstops_hit_on_purpose(); // clear endstop hit flags
  1693. }
  1694. #ifdef MESH_BED_LEVELING
  1695. enum MeshLevelingState { MeshReport, MeshStart, MeshNext };
  1696. /**
  1697. * G29: Mesh-based Z-Probe, probes a grid and produces a
  1698. * mesh to compensate for variable bed height
  1699. *
  1700. * Parameters With MESH_BED_LEVELING:
  1701. *
  1702. * S0 Produce a mesh report
  1703. * S1 Start probing mesh points
  1704. * S2 Probe the next mesh point
  1705. *
  1706. */
  1707. inline void gcode_G29() {
  1708. static int probe_point = -1;
  1709. MeshLevelingState state = code_seen('S') || code_seen('s') ? (MeshLevelingState)code_value_short() : MeshReport;
  1710. if (state < 0 || state > 2) {
  1711. SERIAL_PROTOCOLLNPGM("S out of range (0-2).");
  1712. return;
  1713. }
  1714. switch(state) {
  1715. case MeshReport:
  1716. if (mbl.active) {
  1717. SERIAL_PROTOCOLPGM("Num X,Y: ");
  1718. SERIAL_PROTOCOL(MESH_NUM_X_POINTS);
  1719. SERIAL_PROTOCOLCHAR(',');
  1720. SERIAL_PROTOCOL(MESH_NUM_Y_POINTS);
  1721. SERIAL_PROTOCOLPGM("\nZ search height: ");
  1722. SERIAL_PROTOCOL(MESH_HOME_SEARCH_Z);
  1723. SERIAL_PROTOCOLLNPGM("\nMeasured points:");
  1724. for (int y = 0; y < MESH_NUM_Y_POINTS; y++) {
  1725. for (int x = 0; x < MESH_NUM_X_POINTS; x++) {
  1726. SERIAL_PROTOCOLPGM(" ");
  1727. SERIAL_PROTOCOL_F(mbl.z_values[y][x], 5);
  1728. }
  1729. SERIAL_EOL;
  1730. }
  1731. }
  1732. else
  1733. SERIAL_PROTOCOLLNPGM("Mesh bed leveling not active.");
  1734. break;
  1735. case MeshStart:
  1736. mbl.reset();
  1737. probe_point = 0;
  1738. enquecommands_P(PSTR("G28\nG29 S2"));
  1739. break;
  1740. case MeshNext:
  1741. if (probe_point < 0) {
  1742. SERIAL_PROTOCOLLNPGM("Start mesh probing with \"G29 S1\" first.");
  1743. return;
  1744. }
  1745. int ix, iy;
  1746. if (probe_point == 0) {
  1747. // Set Z to a positive value before recording the first Z.
  1748. current_position[Z_AXIS] = MESH_HOME_SEARCH_Z;
  1749. sync_plan_position();
  1750. }
  1751. else {
  1752. // For others, save the Z of the previous point, then raise Z again.
  1753. ix = (probe_point - 1) % MESH_NUM_X_POINTS;
  1754. iy = (probe_point - 1) / MESH_NUM_X_POINTS;
  1755. if (iy & 1) ix = (MESH_NUM_X_POINTS - 1) - ix; // zig-zag
  1756. mbl.set_z(ix, iy, current_position[Z_AXIS]);
  1757. current_position[Z_AXIS] = MESH_HOME_SEARCH_Z;
  1758. plan_buffer_line(current_position[X_AXIS], current_position[Y_AXIS], current_position[Z_AXIS], current_position[E_AXIS], homing_feedrate[X_AXIS]/60, active_extruder);
  1759. st_synchronize();
  1760. }
  1761. // Is there another point to sample? Move there.
  1762. if (probe_point < MESH_NUM_X_POINTS * MESH_NUM_Y_POINTS) {
  1763. ix = probe_point % MESH_NUM_X_POINTS;
  1764. iy = probe_point / MESH_NUM_X_POINTS;
  1765. if (iy & 1) ix = (MESH_NUM_X_POINTS - 1) - ix; // zig-zag
  1766. current_position[X_AXIS] = mbl.get_x(ix);
  1767. current_position[Y_AXIS] = mbl.get_y(iy);
  1768. plan_buffer_line(current_position[X_AXIS], current_position[Y_AXIS], current_position[Z_AXIS], current_position[E_AXIS], homing_feedrate[X_AXIS]/60, active_extruder);
  1769. st_synchronize();
  1770. probe_point++;
  1771. }
  1772. else {
  1773. // After recording the last point, activate the mbl and home
  1774. SERIAL_PROTOCOLLNPGM("Mesh probing done.");
  1775. probe_point = -1;
  1776. mbl.active = 1;
  1777. enquecommands_P(PSTR("G28"));
  1778. }
  1779. } // switch(state)
  1780. }
  1781. #elif defined(ENABLE_AUTO_BED_LEVELING)
  1782. /**
  1783. * G29: Detailed Z-Probe, probes the bed at 3 or more points.
  1784. * Will fail if the printer has not been homed with G28.
  1785. *
  1786. * Enhanced G29 Auto Bed Leveling Probe Routine
  1787. *
  1788. * Parameters With AUTO_BED_LEVELING_GRID:
  1789. *
  1790. * P Set the size of the grid that will be probed (P x P points).
  1791. * Not supported by non-linear delta printer bed leveling.
  1792. * Example: "G29 P4"
  1793. *
  1794. * S Set the XY travel speed between probe points (in mm/min)
  1795. *
  1796. * D Dry-Run mode. Just evaluate the bed Topology - Don't apply
  1797. * or clean the rotation Matrix. Useful to check the topology
  1798. * after a first run of G29.
  1799. *
  1800. * V Set the verbose level (0-4). Example: "G29 V3"
  1801. *
  1802. * T Generate a Bed Topology Report. Example: "G29 P5 T" for a detailed report.
  1803. * This is useful for manual bed leveling and finding flaws in the bed (to
  1804. * assist with part placement).
  1805. * Not supported by non-linear delta printer bed leveling.
  1806. *
  1807. * F Set the Front limit of the probing grid
  1808. * B Set the Back limit of the probing grid
  1809. * L Set the Left limit of the probing grid
  1810. * R Set the Right limit of the probing grid
  1811. *
  1812. * Global Parameters:
  1813. *
  1814. * E/e By default G29 will engages the probe, test the bed, then disengage.
  1815. * Include "E" to engage/disengage the probe for each sample.
  1816. * There's no extra effect if you have a fixed probe.
  1817. * Usage: "G29 E" or "G29 e"
  1818. *
  1819. */
  1820. inline void gcode_G29() {
  1821. // Don't allow auto-leveling without homing first
  1822. if (!axis_known_position[X_AXIS] || !axis_known_position[Y_AXIS]) {
  1823. LCD_MESSAGEPGM(MSG_POSITION_UNKNOWN);
  1824. SERIAL_ECHO_START;
  1825. SERIAL_ECHOLNPGM(MSG_POSITION_UNKNOWN);
  1826. return;
  1827. }
  1828. int verbose_level = code_seen('V') || code_seen('v') ? code_value_short() : 1;
  1829. if (verbose_level < 0 || verbose_level > 4) {
  1830. SERIAL_ECHOLNPGM("?(V)erbose Level is implausible (0-4).");
  1831. return;
  1832. }
  1833. bool dryrun = code_seen('D') || code_seen('d'),
  1834. deploy_probe_for_each_reading = code_seen('E') || code_seen('e');
  1835. #ifdef AUTO_BED_LEVELING_GRID
  1836. #ifndef DELTA
  1837. bool do_topography_map = verbose_level > 2 || code_seen('T') || code_seen('t');
  1838. #endif
  1839. if (verbose_level > 0) {
  1840. SERIAL_PROTOCOLPGM("G29 Auto Bed Leveling\n");
  1841. if (dryrun) SERIAL_ECHOLNPGM("Running in DRY-RUN mode");
  1842. }
  1843. int auto_bed_leveling_grid_points = AUTO_BED_LEVELING_GRID_POINTS;
  1844. #ifndef DELTA
  1845. if (code_seen('P')) auto_bed_leveling_grid_points = code_value_short();
  1846. if (auto_bed_leveling_grid_points < 2) {
  1847. SERIAL_PROTOCOLPGM("?Number of probed (P)oints is implausible (2 minimum).\n");
  1848. return;
  1849. }
  1850. #endif
  1851. xy_travel_speed = code_seen('S') ? code_value_short() : XY_TRAVEL_SPEED;
  1852. int left_probe_bed_position = code_seen('L') ? code_value_short() : LEFT_PROBE_BED_POSITION,
  1853. right_probe_bed_position = code_seen('R') ? code_value_short() : RIGHT_PROBE_BED_POSITION,
  1854. front_probe_bed_position = code_seen('F') ? code_value_short() : FRONT_PROBE_BED_POSITION,
  1855. back_probe_bed_position = code_seen('B') ? code_value_short() : BACK_PROBE_BED_POSITION;
  1856. bool left_out_l = left_probe_bed_position < MIN_PROBE_X,
  1857. left_out = left_out_l || left_probe_bed_position > right_probe_bed_position - MIN_PROBE_EDGE,
  1858. right_out_r = right_probe_bed_position > MAX_PROBE_X,
  1859. right_out = right_out_r || right_probe_bed_position < left_probe_bed_position + MIN_PROBE_EDGE,
  1860. front_out_f = front_probe_bed_position < MIN_PROBE_Y,
  1861. front_out = front_out_f || front_probe_bed_position > back_probe_bed_position - MIN_PROBE_EDGE,
  1862. back_out_b = back_probe_bed_position > MAX_PROBE_Y,
  1863. back_out = back_out_b || back_probe_bed_position < front_probe_bed_position + MIN_PROBE_EDGE;
  1864. if (left_out || right_out || front_out || back_out) {
  1865. if (left_out) {
  1866. SERIAL_PROTOCOLPGM("?Probe (L)eft position out of range.\n");
  1867. left_probe_bed_position = left_out_l ? MIN_PROBE_X : right_probe_bed_position - MIN_PROBE_EDGE;
  1868. }
  1869. if (right_out) {
  1870. SERIAL_PROTOCOLPGM("?Probe (R)ight position out of range.\n");
  1871. right_probe_bed_position = right_out_r ? MAX_PROBE_X : left_probe_bed_position + MIN_PROBE_EDGE;
  1872. }
  1873. if (front_out) {
  1874. SERIAL_PROTOCOLPGM("?Probe (F)ront position out of range.\n");
  1875. front_probe_bed_position = front_out_f ? MIN_PROBE_Y : back_probe_bed_position - MIN_PROBE_EDGE;
  1876. }
  1877. if (back_out) {
  1878. SERIAL_PROTOCOLPGM("?Probe (B)ack position out of range.\n");
  1879. back_probe_bed_position = back_out_b ? MAX_PROBE_Y : front_probe_bed_position + MIN_PROBE_EDGE;
  1880. }
  1881. return;
  1882. }
  1883. #endif // AUTO_BED_LEVELING_GRID
  1884. #ifdef Z_PROBE_SLED
  1885. dock_sled(false); // engage (un-dock) the probe
  1886. #elif defined(Z_PROBE_ALLEN_KEY) //|| defined(SERVO_LEVELING)
  1887. deploy_z_probe();
  1888. #endif
  1889. st_synchronize();
  1890. if (!dryrun) {
  1891. // make sure the bed_level_rotation_matrix is identity or the planner will get it wrong
  1892. plan_bed_level_matrix.set_to_identity();
  1893. #ifdef DELTA
  1894. reset_bed_level();
  1895. #else //!DELTA
  1896. //vector_3 corrected_position = plan_get_position_mm();
  1897. //corrected_position.debug("position before G29");
  1898. vector_3 uncorrected_position = plan_get_position();
  1899. //uncorrected_position.debug("position during G29");
  1900. current_position[X_AXIS] = uncorrected_position.x;
  1901. current_position[Y_AXIS] = uncorrected_position.y;
  1902. current_position[Z_AXIS] = uncorrected_position.z;
  1903. sync_plan_position();
  1904. #endif // !DELTA
  1905. }
  1906. setup_for_endstop_move();
  1907. feedrate = homing_feedrate[Z_AXIS];
  1908. #ifdef AUTO_BED_LEVELING_GRID
  1909. // probe at the points of a lattice grid
  1910. const int xGridSpacing = (right_probe_bed_position - left_probe_bed_position) / (auto_bed_leveling_grid_points - 1),
  1911. yGridSpacing = (back_probe_bed_position - front_probe_bed_position) / (auto_bed_leveling_grid_points - 1);
  1912. #ifdef DELTA
  1913. delta_grid_spacing[0] = xGridSpacing;
  1914. delta_grid_spacing[1] = yGridSpacing;
  1915. float z_offset = Z_PROBE_OFFSET_FROM_EXTRUDER;
  1916. if (code_seen(axis_codes[Z_AXIS])) z_offset += code_value();
  1917. #else // !DELTA
  1918. // solve the plane equation ax + by + d = z
  1919. // A is the matrix with rows [x y 1] for all the probed points
  1920. // B is the vector of the Z positions
  1921. // 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
  1922. // so Vx = -a Vy = -b Vz = 1 (we want the vector facing towards positive Z
  1923. int abl2 = auto_bed_leveling_grid_points * auto_bed_leveling_grid_points;
  1924. double eqnAMatrix[abl2 * 3], // "A" matrix of the linear system of equations
  1925. eqnBVector[abl2], // "B" vector of Z points
  1926. mean = 0.0;
  1927. #endif // !DELTA
  1928. int probePointCounter = 0;
  1929. bool zig = true;
  1930. for (int yCount = 0; yCount < auto_bed_leveling_grid_points; yCount++) {
  1931. double yProbe = front_probe_bed_position + yGridSpacing * yCount;
  1932. int xStart, xStop, xInc;
  1933. if (zig) {
  1934. xStart = 0;
  1935. xStop = auto_bed_leveling_grid_points;
  1936. xInc = 1;
  1937. }
  1938. else {
  1939. xStart = auto_bed_leveling_grid_points - 1;
  1940. xStop = -1;
  1941. xInc = -1;
  1942. }
  1943. #ifndef DELTA
  1944. // If do_topography_map is set then don't zig-zag. Just scan in one direction.
  1945. // This gets the probe points in more readable order.
  1946. if (!do_topography_map) zig = !zig;
  1947. #endif
  1948. for (int xCount = xStart; xCount != xStop; xCount += xInc) {
  1949. double xProbe = left_probe_bed_position + xGridSpacing * xCount;
  1950. // raise extruder
  1951. float measured_z,
  1952. z_before = probePointCounter ? Z_RAISE_BETWEEN_PROBINGS + current_position[Z_AXIS] : Z_RAISE_BEFORE_PROBING;
  1953. #ifdef DELTA
  1954. // Avoid probing the corners (outside the round or hexagon print surface) on a delta printer.
  1955. float distance_from_center = sqrt(xProbe*xProbe + yProbe*yProbe);
  1956. if (distance_from_center > DELTA_PROBABLE_RADIUS) continue;
  1957. #endif //DELTA
  1958. ProbeAction act;
  1959. if (deploy_probe_for_each_reading) // G29 E - Stow between probes
  1960. act = ProbeDeployAndStow;
  1961. else if (yCount == 0 && xCount == 0)
  1962. act = ProbeDeploy;
  1963. else if (yCount == auto_bed_leveling_grid_points - 1 && xCount == auto_bed_leveling_grid_points - 1)
  1964. act = ProbeStow;
  1965. else
  1966. act = ProbeStay;
  1967. measured_z = probe_pt(xProbe, yProbe, z_before, act, verbose_level);
  1968. #ifndef DELTA
  1969. mean += measured_z;
  1970. eqnBVector[probePointCounter] = measured_z;
  1971. eqnAMatrix[probePointCounter + 0 * abl2] = xProbe;
  1972. eqnAMatrix[probePointCounter + 1 * abl2] = yProbe;
  1973. eqnAMatrix[probePointCounter + 2 * abl2] = 1;
  1974. #else
  1975. bed_level[xCount][yCount] = measured_z + z_offset;
  1976. #endif
  1977. probePointCounter++;
  1978. manage_heater();
  1979. manage_inactivity();
  1980. lcd_update();
  1981. } //xProbe
  1982. } //yProbe
  1983. clean_up_after_endstop_move();
  1984. #ifdef DELTA
  1985. if (!dryrun) extrapolate_unprobed_bed_level();
  1986. print_bed_level();
  1987. #else // !DELTA
  1988. // solve lsq problem
  1989. double *plane_equation_coefficients = qr_solve(abl2, 3, eqnAMatrix, eqnBVector);
  1990. mean /= abl2;
  1991. if (verbose_level) {
  1992. SERIAL_PROTOCOLPGM("Eqn coefficients: a: ");
  1993. SERIAL_PROTOCOL_F(plane_equation_coefficients[0], 8);
  1994. SERIAL_PROTOCOLPGM(" b: ");
  1995. SERIAL_PROTOCOL_F(plane_equation_coefficients[1], 8);
  1996. SERIAL_PROTOCOLPGM(" d: ");
  1997. SERIAL_PROTOCOL_F(plane_equation_coefficients[2], 8);
  1998. SERIAL_EOL;
  1999. if (verbose_level > 2) {
  2000. SERIAL_PROTOCOLPGM("Mean of sampled points: ");
  2001. SERIAL_PROTOCOL_F(mean, 8);
  2002. SERIAL_EOL;
  2003. }
  2004. }
  2005. // Show the Topography map if enabled
  2006. if (do_topography_map) {
  2007. SERIAL_PROTOCOLPGM(" \nBed Height Topography: \n");
  2008. SERIAL_PROTOCOLPGM("+-----------+\n");
  2009. SERIAL_PROTOCOLPGM("|...Back....|\n");
  2010. SERIAL_PROTOCOLPGM("|Left..Right|\n");
  2011. SERIAL_PROTOCOLPGM("|...Front...|\n");
  2012. SERIAL_PROTOCOLPGM("+-----------+\n");
  2013. for (int yy = auto_bed_leveling_grid_points - 1; yy >= 0; yy--) {
  2014. for (int xx = 0; xx < auto_bed_leveling_grid_points; xx++) {
  2015. int ind = yy * auto_bed_leveling_grid_points + xx;
  2016. float diff = eqnBVector[ind] - mean;
  2017. if (diff >= 0.0)
  2018. SERIAL_PROTOCOLPGM(" +"); // Include + for column alignment
  2019. else
  2020. SERIAL_PROTOCOLCHAR(' ');
  2021. SERIAL_PROTOCOL_F(diff, 5);
  2022. } // xx
  2023. SERIAL_EOL;
  2024. } // yy
  2025. SERIAL_EOL;
  2026. } //do_topography_map
  2027. if (!dryrun) set_bed_level_equation_lsq(plane_equation_coefficients);
  2028. free(plane_equation_coefficients);
  2029. #endif //!DELTA
  2030. #else // !AUTO_BED_LEVELING_GRID
  2031. // Actions for each probe
  2032. ProbeAction p1, p2, p3;
  2033. if (deploy_probe_for_each_reading)
  2034. p1 = p2 = p3 = ProbeDeployAndStow;
  2035. else
  2036. p1 = ProbeDeploy, p2 = ProbeStay, p3 = ProbeStow;
  2037. // Probe at 3 arbitrary points
  2038. float z_at_pt_1 = probe_pt(ABL_PROBE_PT_1_X, ABL_PROBE_PT_1_Y, Z_RAISE_BEFORE_PROBING, p1, verbose_level),
  2039. z_at_pt_2 = probe_pt(ABL_PROBE_PT_2_X, ABL_PROBE_PT_2_Y, current_position[Z_AXIS] + Z_RAISE_BETWEEN_PROBINGS, p2, verbose_level),
  2040. z_at_pt_3 = probe_pt(ABL_PROBE_PT_3_X, ABL_PROBE_PT_3_Y, current_position[Z_AXIS] + Z_RAISE_BETWEEN_PROBINGS, p3, verbose_level);
  2041. clean_up_after_endstop_move();
  2042. if (!dryrun) set_bed_level_equation_3pts(z_at_pt_1, z_at_pt_2, z_at_pt_3);
  2043. #endif // !AUTO_BED_LEVELING_GRID
  2044. #ifndef DELTA
  2045. if (verbose_level > 0)
  2046. plan_bed_level_matrix.debug(" \n\nBed Level Correction Matrix:");
  2047. if (!dryrun) {
  2048. // Correct the Z height difference from z-probe position and hotend tip position.
  2049. // The Z height on homing is measured by Z-Probe, but the probe is quite far from the hotend.
  2050. // When the bed is uneven, this height must be corrected.
  2051. float x_tmp = current_position[X_AXIS] + X_PROBE_OFFSET_FROM_EXTRUDER,
  2052. y_tmp = current_position[Y_AXIS] + Y_PROBE_OFFSET_FROM_EXTRUDER,
  2053. z_tmp = current_position[Z_AXIS],
  2054. 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)
  2055. apply_rotation_xyz(plan_bed_level_matrix, x_tmp, y_tmp, z_tmp); //Apply the correction sending the probe offset
  2056. current_position[Z_AXIS] = z_tmp - real_z + current_position[Z_AXIS]; //The difference is added to current position and sent to planner.
  2057. sync_plan_position();
  2058. }
  2059. #endif // !DELTA
  2060. #ifdef Z_PROBE_SLED
  2061. dock_sled(true, -SLED_DOCKING_OFFSET); // dock the probe, correcting for over-travel
  2062. #elif defined(Z_PROBE_ALLEN_KEY) //|| defined(SERVO_LEVELING)
  2063. stow_z_probe();
  2064. #endif
  2065. #ifdef Z_PROBE_END_SCRIPT
  2066. enquecommands_P(PSTR(Z_PROBE_END_SCRIPT));
  2067. st_synchronize();
  2068. #endif
  2069. }
  2070. #ifndef Z_PROBE_SLED
  2071. inline void gcode_G30() {
  2072. deploy_z_probe(); // Engage Z Servo endstop if available
  2073. st_synchronize();
  2074. // TODO: make sure the bed_level_rotation_matrix is identity or the planner will get set incorectly
  2075. setup_for_endstop_move();
  2076. feedrate = homing_feedrate[Z_AXIS];
  2077. run_z_probe();
  2078. SERIAL_PROTOCOLPGM(MSG_BED);
  2079. SERIAL_PROTOCOLPGM(" X: ");
  2080. SERIAL_PROTOCOL(current_position[X_AXIS] + 0.0001);
  2081. SERIAL_PROTOCOLPGM(" Y: ");
  2082. SERIAL_PROTOCOL(current_position[Y_AXIS] + 0.0001);
  2083. SERIAL_PROTOCOLPGM(" Z: ");
  2084. SERIAL_PROTOCOL(current_position[Z_AXIS] + 0.0001);
  2085. SERIAL_EOL;
  2086. clean_up_after_endstop_move();
  2087. stow_z_probe(); // Retract Z Servo endstop if available
  2088. }
  2089. #endif //!Z_PROBE_SLED
  2090. #endif //ENABLE_AUTO_BED_LEVELING
  2091. /**
  2092. * G92: Set current position to given X Y Z E
  2093. */
  2094. inline void gcode_G92() {
  2095. if (!code_seen(axis_codes[E_AXIS]))
  2096. st_synchronize();
  2097. bool didXYZ = false;
  2098. for (int i = 0; i < NUM_AXIS; i++) {
  2099. if (code_seen(axis_codes[i])) {
  2100. float v = current_position[i] = code_value();
  2101. if (i == E_AXIS)
  2102. plan_set_e_position(v);
  2103. else
  2104. didXYZ = true;
  2105. }
  2106. }
  2107. if (didXYZ) sync_plan_position();
  2108. }
  2109. #ifdef ULTIPANEL
  2110. /**
  2111. * M0: // M0 - Unconditional stop - Wait for user button press on LCD
  2112. * M1: // M1 - Conditional stop - Wait for user button press on LCD
  2113. */
  2114. inline void gcode_M0_M1() {
  2115. char *src = strchr_pointer + 2;
  2116. unsigned long codenum = 0;
  2117. bool hasP = false, hasS = false;
  2118. if (code_seen('P')) {
  2119. codenum = code_value_short(); // milliseconds to wait
  2120. hasP = codenum > 0;
  2121. }
  2122. if (code_seen('S')) {
  2123. codenum = code_value_short() * 1000UL; // seconds to wait
  2124. hasS = codenum > 0;
  2125. }
  2126. char* starpos = strchr(src, '*');
  2127. if (starpos != NULL) *(starpos) = '\0';
  2128. while (*src == ' ') ++src;
  2129. if (!hasP && !hasS && *src != '\0')
  2130. lcd_setstatus(src, true);
  2131. else {
  2132. LCD_MESSAGEPGM(MSG_USERWAIT);
  2133. #if defined(LCD_PROGRESS_BAR) && PROGRESS_MSG_EXPIRE > 0
  2134. dontExpireStatus();
  2135. #endif
  2136. }
  2137. lcd_ignore_click();
  2138. st_synchronize();
  2139. refresh_cmd_timeout();
  2140. if (codenum > 0) {
  2141. codenum += previous_millis_cmd; // keep track of when we started waiting
  2142. while(millis() < codenum && !lcd_clicked()) {
  2143. manage_heater();
  2144. manage_inactivity();
  2145. lcd_update();
  2146. }
  2147. lcd_ignore_click(false);
  2148. }
  2149. else {
  2150. if (!lcd_detected()) return;
  2151. while (!lcd_clicked()) {
  2152. manage_heater();
  2153. manage_inactivity();
  2154. lcd_update();
  2155. }
  2156. }
  2157. if (IS_SD_PRINTING)
  2158. LCD_MESSAGEPGM(MSG_RESUMING);
  2159. else
  2160. LCD_MESSAGEPGM(WELCOME_MSG);
  2161. }
  2162. #endif // ULTIPANEL
  2163. /**
  2164. * M17: Enable power on all stepper motors
  2165. */
  2166. inline void gcode_M17() {
  2167. LCD_MESSAGEPGM(MSG_NO_MOVE);
  2168. enable_all_steppers();
  2169. }
  2170. #ifdef SDSUPPORT
  2171. /**
  2172. * M20: List SD card to serial output
  2173. */
  2174. inline void gcode_M20() {
  2175. SERIAL_PROTOCOLLNPGM(MSG_BEGIN_FILE_LIST);
  2176. card.ls();
  2177. SERIAL_PROTOCOLLNPGM(MSG_END_FILE_LIST);
  2178. }
  2179. /**
  2180. * M21: Init SD Card
  2181. */
  2182. inline void gcode_M21() {
  2183. card.initsd();
  2184. }
  2185. /**
  2186. * M22: Release SD Card
  2187. */
  2188. inline void gcode_M22() {
  2189. card.release();
  2190. }
  2191. /**
  2192. * M23: Select a file
  2193. */
  2194. inline void gcode_M23() {
  2195. char* codepos = strchr_pointer + 4;
  2196. char* starpos = strchr(codepos, '*');
  2197. if (starpos) *starpos = '\0';
  2198. card.openFile(codepos, true);
  2199. }
  2200. /**
  2201. * M24: Start SD Print
  2202. */
  2203. inline void gcode_M24() {
  2204. card.startFileprint();
  2205. starttime = millis();
  2206. }
  2207. /**
  2208. * M25: Pause SD Print
  2209. */
  2210. inline void gcode_M25() {
  2211. card.pauseSDPrint();
  2212. }
  2213. /**
  2214. * M26: Set SD Card file index
  2215. */
  2216. inline void gcode_M26() {
  2217. if (card.cardOK && code_seen('S'))
  2218. card.setIndex(code_value_short());
  2219. }
  2220. /**
  2221. * M27: Get SD Card status
  2222. */
  2223. inline void gcode_M27() {
  2224. card.getStatus();
  2225. }
  2226. /**
  2227. * M28: Start SD Write
  2228. */
  2229. inline void gcode_M28() {
  2230. char* codepos = strchr_pointer + 4;
  2231. char* starpos = strchr(codepos, '*');
  2232. if (starpos) {
  2233. char* npos = strchr(cmdbuffer[bufindr], 'N');
  2234. strchr_pointer = strchr(npos, ' ') + 1;
  2235. *(starpos) = '\0';
  2236. }
  2237. card.openFile(codepos, false);
  2238. }
  2239. /**
  2240. * M29: Stop SD Write
  2241. * Processed in write to file routine above
  2242. */
  2243. inline void gcode_M29() {
  2244. // card.saving = false;
  2245. }
  2246. /**
  2247. * M30 <filename>: Delete SD Card file
  2248. */
  2249. inline void gcode_M30() {
  2250. if (card.cardOK) {
  2251. card.closefile();
  2252. char* starpos = strchr(strchr_pointer + 4, '*');
  2253. if (starpos) {
  2254. char* npos = strchr(cmdbuffer[bufindr], 'N');
  2255. strchr_pointer = strchr(npos, ' ') + 1;
  2256. *(starpos) = '\0';
  2257. }
  2258. card.removeFile(strchr_pointer + 4);
  2259. }
  2260. }
  2261. #endif
  2262. /**
  2263. * M31: Get the time since the start of SD Print (or last M109)
  2264. */
  2265. inline void gcode_M31() {
  2266. stoptime = millis();
  2267. unsigned long t = (stoptime - starttime) / 1000;
  2268. int min = t / 60, sec = t % 60;
  2269. char time[30];
  2270. sprintf_P(time, PSTR("%i min, %i sec"), min, sec);
  2271. SERIAL_ECHO_START;
  2272. SERIAL_ECHOLN(time);
  2273. lcd_setstatus(time);
  2274. autotempShutdown();
  2275. }
  2276. #ifdef SDSUPPORT
  2277. /**
  2278. * M32: Select file and start SD Print
  2279. */
  2280. inline void gcode_M32() {
  2281. if (card.sdprinting)
  2282. st_synchronize();
  2283. char* codepos = strchr_pointer + 4;
  2284. char* namestartpos = strchr(codepos, '!'); //find ! to indicate filename string start.
  2285. if (! namestartpos)
  2286. namestartpos = codepos; //default name position, 4 letters after the M
  2287. else
  2288. namestartpos++; //to skip the '!'
  2289. char* starpos = strchr(codepos, '*');
  2290. if (starpos) *(starpos) = '\0';
  2291. bool call_procedure = code_seen('P') && (strchr_pointer < namestartpos);
  2292. if (card.cardOK) {
  2293. card.openFile(namestartpos, true, !call_procedure);
  2294. if (code_seen('S') && strchr_pointer < namestartpos) // "S" (must occur _before_ the filename!)
  2295. card.setIndex(code_value_short());
  2296. card.startFileprint();
  2297. if (!call_procedure)
  2298. starttime = millis(); //procedure calls count as normal print time.
  2299. }
  2300. }
  2301. /**
  2302. * M928: Start SD Write
  2303. */
  2304. inline void gcode_M928() {
  2305. char* starpos = strchr(strchr_pointer + 5, '*');
  2306. if (starpos) {
  2307. char* npos = strchr(cmdbuffer[bufindr], 'N');
  2308. strchr_pointer = strchr(npos, ' ') + 1;
  2309. *(starpos) = '\0';
  2310. }
  2311. card.openLogFile(strchr_pointer + 5);
  2312. }
  2313. #endif // SDSUPPORT
  2314. /**
  2315. * M42: Change pin status via GCode
  2316. */
  2317. inline void gcode_M42() {
  2318. if (code_seen('S')) {
  2319. int pin_status = code_value_short(),
  2320. pin_number = LED_PIN;
  2321. if (code_seen('P') && pin_status >= 0 && pin_status <= 255)
  2322. pin_number = code_value_short();
  2323. for (int8_t i = 0; i < (int8_t)(sizeof(sensitive_pins) / sizeof(*sensitive_pins)); i++) {
  2324. if (sensitive_pins[i] == pin_number) {
  2325. pin_number = -1;
  2326. break;
  2327. }
  2328. }
  2329. #if HAS_FAN
  2330. if (pin_number == FAN_PIN) fanSpeed = pin_status;
  2331. #endif
  2332. if (pin_number > -1) {
  2333. pinMode(pin_number, OUTPUT);
  2334. digitalWrite(pin_number, pin_status);
  2335. analogWrite(pin_number, pin_status);
  2336. }
  2337. } // code_seen('S')
  2338. }
  2339. #if defined(ENABLE_AUTO_BED_LEVELING) && defined(Z_PROBE_REPEATABILITY_TEST)
  2340. // This is redundant since the SanityCheck.h already checks for a valid Z_PROBE_PIN, but here for clarity.
  2341. #ifdef Z_PROBE_ENDSTOP
  2342. #if !HAS_Z_PROBE
  2343. #error You must define Z_PROBE_PIN to enable Z-Probe repeatability calculation.
  2344. #endif
  2345. #elif !HAS_Z_MIN
  2346. #error You must define Z_MIN_PIN to enable Z-Probe repeatability calculation.
  2347. #endif
  2348. /**
  2349. * M48: Z-Probe repeatability measurement function.
  2350. *
  2351. * Usage:
  2352. * M48 <n#> <X#> <Y#> <V#> <E> <L#>
  2353. * P = Number of sampled points (4-50, default 10)
  2354. * X = Sample X position
  2355. * Y = Sample Y position
  2356. * V = Verbose level (0-4, default=1)
  2357. * E = Engage probe for each reading
  2358. * L = Number of legs of movement before probe
  2359. *
  2360. * This function assumes the bed has been homed. Specifically, that a G28 command
  2361. * as been issued prior to invoking the M48 Z-Probe repeatability measurement function.
  2362. * Any information generated by a prior G29 Bed leveling command will be lost and need to be
  2363. * regenerated.
  2364. *
  2365. * The number of samples will default to 10 if not specified. You can use upper or lower case
  2366. * letters for any of the options EXCEPT n. n must be in lower case because Marlin uses a capital
  2367. * N for its communication protocol and will get horribly confused if you send it a capital N.
  2368. */
  2369. inline void gcode_M48() {
  2370. double sum = 0.0, mean = 0.0, sigma = 0.0, sample_set[50];
  2371. int verbose_level = 1, n_samples = 10, n_legs = 0;
  2372. if (code_seen('V') || code_seen('v')) {
  2373. verbose_level = code_value_short();
  2374. if (verbose_level < 0 || verbose_level > 4 ) {
  2375. SERIAL_PROTOCOLPGM("?Verbose Level not plausible (0-4).\n");
  2376. return;
  2377. }
  2378. }
  2379. if (verbose_level > 0)
  2380. SERIAL_PROTOCOLPGM("M48 Z-Probe Repeatability test\n");
  2381. if (code_seen('P') || code_seen('p') || code_seen('n')) { // `n` for legacy support only - please use `P`!
  2382. n_samples = code_value_short();
  2383. if (n_samples < 4 || n_samples > 50) {
  2384. SERIAL_PROTOCOLPGM("?Sample size not plausible (4-50).\n");
  2385. return;
  2386. }
  2387. }
  2388. double X_probe_location, Y_probe_location,
  2389. X_current = X_probe_location = st_get_position_mm(X_AXIS),
  2390. Y_current = Y_probe_location = st_get_position_mm(Y_AXIS),
  2391. Z_current = st_get_position_mm(Z_AXIS),
  2392. Z_start_location = Z_current + Z_RAISE_BEFORE_PROBING,
  2393. ext_position = st_get_position_mm(E_AXIS);
  2394. bool deploy_probe_for_each_reading = code_seen('E') || code_seen('e');
  2395. if (code_seen('X') || code_seen('x')) {
  2396. X_probe_location = code_value() - X_PROBE_OFFSET_FROM_EXTRUDER;
  2397. if (X_probe_location < X_MIN_POS || X_probe_location > X_MAX_POS) {
  2398. SERIAL_PROTOCOLPGM("?X position out of range.\n");
  2399. return;
  2400. }
  2401. }
  2402. if (code_seen('Y') || code_seen('y')) {
  2403. Y_probe_location = code_value() - Y_PROBE_OFFSET_FROM_EXTRUDER;
  2404. if (Y_probe_location < Y_MIN_POS || Y_probe_location > Y_MAX_POS) {
  2405. SERIAL_PROTOCOLPGM("?Y position out of range.\n");
  2406. return;
  2407. }
  2408. }
  2409. if (code_seen('L') || code_seen('l')) {
  2410. n_legs = code_value_short();
  2411. if (n_legs == 1) n_legs = 2;
  2412. if (n_legs < 0 || n_legs > 15) {
  2413. SERIAL_PROTOCOLPGM("?Number of legs in movement not plausible (0-15).\n");
  2414. return;
  2415. }
  2416. }
  2417. //
  2418. // Do all the preliminary setup work. First raise the probe.
  2419. //
  2420. st_synchronize();
  2421. plan_bed_level_matrix.set_to_identity();
  2422. plan_buffer_line(X_current, Y_current, Z_start_location,
  2423. ext_position,
  2424. homing_feedrate[Z_AXIS] / 60,
  2425. active_extruder);
  2426. st_synchronize();
  2427. //
  2428. // Now get everything to the specified probe point So we can safely do a probe to
  2429. // get us close to the bed. If the Z-Axis is far from the bed, we don't want to
  2430. // use that as a starting point for each probe.
  2431. //
  2432. if (verbose_level > 2)
  2433. SERIAL_PROTOCOL("Positioning the probe...\n");
  2434. plan_buffer_line( X_probe_location, Y_probe_location, Z_start_location,
  2435. ext_position,
  2436. homing_feedrate[X_AXIS]/60,
  2437. active_extruder);
  2438. st_synchronize();
  2439. current_position[X_AXIS] = X_current = st_get_position_mm(X_AXIS);
  2440. current_position[Y_AXIS] = Y_current = st_get_position_mm(Y_AXIS);
  2441. current_position[Z_AXIS] = Z_current = st_get_position_mm(Z_AXIS);
  2442. current_position[E_AXIS] = ext_position = st_get_position_mm(E_AXIS);
  2443. //
  2444. // OK, do the inital probe to get us close to the bed.
  2445. // Then retrace the right amount and use that in subsequent probes
  2446. //
  2447. deploy_z_probe();
  2448. setup_for_endstop_move();
  2449. run_z_probe();
  2450. current_position[Z_AXIS] = Z_current = st_get_position_mm(Z_AXIS);
  2451. Z_start_location = st_get_position_mm(Z_AXIS) + Z_RAISE_BEFORE_PROBING;
  2452. plan_buffer_line( X_probe_location, Y_probe_location, Z_start_location,
  2453. ext_position,
  2454. homing_feedrate[X_AXIS]/60,
  2455. active_extruder);
  2456. st_synchronize();
  2457. current_position[Z_AXIS] = Z_current = st_get_position_mm(Z_AXIS);
  2458. if (deploy_probe_for_each_reading) stow_z_probe();
  2459. for (uint16_t n=0; n < n_samples; n++) {
  2460. do_blocking_move_to(X_probe_location, Y_probe_location, Z_start_location); // Make sure we are at the probe location
  2461. if (n_legs) {
  2462. unsigned long ms = millis();
  2463. double radius = ms % (X_MAX_LENGTH / 4), // limit how far out to go
  2464. theta = RADIANS(ms % 360L);
  2465. float dir = (ms & 0x0001) ? 1 : -1; // clockwise or counter clockwise
  2466. //SERIAL_ECHOPAIR("starting radius: ",radius);
  2467. //SERIAL_ECHOPAIR(" theta: ",theta);
  2468. //SERIAL_ECHOPAIR(" direction: ",dir);
  2469. //SERIAL_EOL;
  2470. for (int l = 0; l < n_legs - 1; l++) {
  2471. ms = millis();
  2472. theta += RADIANS(dir * (ms % 20L));
  2473. radius += (ms % 10L) - 5L;
  2474. if (radius < 0.0) radius = -radius;
  2475. X_current = X_probe_location + cos(theta) * radius;
  2476. Y_current = Y_probe_location + sin(theta) * radius;
  2477. X_current = constrain(X_current, X_MIN_POS, X_MAX_POS);
  2478. Y_current = constrain(Y_current, Y_MIN_POS, Y_MAX_POS);
  2479. if (verbose_level > 3) {
  2480. SERIAL_ECHOPAIR("x: ", X_current);
  2481. SERIAL_ECHOPAIR("y: ", Y_current);
  2482. SERIAL_EOL;
  2483. }
  2484. do_blocking_move_to(X_current, Y_current, Z_current);
  2485. } // n_legs loop
  2486. do_blocking_move_to(X_probe_location, Y_probe_location, Z_start_location); // Go back to the probe location
  2487. } // n_legs
  2488. if (deploy_probe_for_each_reading) {
  2489. deploy_z_probe();
  2490. delay(1000);
  2491. }
  2492. setup_for_endstop_move();
  2493. run_z_probe();
  2494. sample_set[n] = current_position[Z_AXIS];
  2495. //
  2496. // Get the current mean for the data points we have so far
  2497. //
  2498. sum = 0.0;
  2499. for (int j = 0; j <= n; j++) sum += sample_set[j];
  2500. mean = sum / (n + 1);
  2501. //
  2502. // Now, use that mean to calculate the standard deviation for the
  2503. // data points we have so far
  2504. //
  2505. sum = 0.0;
  2506. for (int j = 0; j <= n; j++) {
  2507. float ss = sample_set[j] - mean;
  2508. sum += ss * ss;
  2509. }
  2510. sigma = sqrt(sum / (n + 1));
  2511. if (verbose_level > 1) {
  2512. SERIAL_PROTOCOL(n+1);
  2513. SERIAL_PROTOCOLPGM(" of ");
  2514. SERIAL_PROTOCOL(n_samples);
  2515. SERIAL_PROTOCOLPGM(" z: ");
  2516. SERIAL_PROTOCOL_F(current_position[Z_AXIS], 6);
  2517. if (verbose_level > 2) {
  2518. SERIAL_PROTOCOLPGM(" mean: ");
  2519. SERIAL_PROTOCOL_F(mean,6);
  2520. SERIAL_PROTOCOLPGM(" sigma: ");
  2521. SERIAL_PROTOCOL_F(sigma,6);
  2522. }
  2523. }
  2524. if (verbose_level > 0) SERIAL_EOL;
  2525. plan_buffer_line(X_probe_location, Y_probe_location, Z_start_location, current_position[E_AXIS], homing_feedrate[Z_AXIS]/60, active_extruder);
  2526. st_synchronize();
  2527. if (deploy_probe_for_each_reading) {
  2528. stow_z_probe();
  2529. delay(1000);
  2530. }
  2531. }
  2532. if (!deploy_probe_for_each_reading) {
  2533. stow_z_probe();
  2534. delay(1000);
  2535. }
  2536. clean_up_after_endstop_move();
  2537. // enable_endstops(true);
  2538. if (verbose_level > 0) {
  2539. SERIAL_PROTOCOLPGM("Mean: ");
  2540. SERIAL_PROTOCOL_F(mean, 6);
  2541. SERIAL_EOL;
  2542. }
  2543. SERIAL_PROTOCOLPGM("Standard Deviation: ");
  2544. SERIAL_PROTOCOL_F(sigma, 6);
  2545. SERIAL_EOL; SERIAL_EOL;
  2546. }
  2547. #endif // ENABLE_AUTO_BED_LEVELING && Z_PROBE_REPEATABILITY_TEST
  2548. /**
  2549. * M104: Set hot end temperature
  2550. */
  2551. inline void gcode_M104() {
  2552. if (setTargetedHotend(104)) return;
  2553. if (code_seen('S')) {
  2554. float temp = code_value();
  2555. setTargetHotend(temp, target_extruder);
  2556. #ifdef DUAL_X_CARRIAGE
  2557. if (dual_x_carriage_mode == DXC_DUPLICATION_MODE && target_extruder == 0)
  2558. setTargetHotend1(temp == 0.0 ? 0.0 : temp + duplicate_extruder_temp_offset);
  2559. #endif
  2560. setWatch();
  2561. }
  2562. }
  2563. /**
  2564. * M105: Read hot end and bed temperature
  2565. */
  2566. inline void gcode_M105() {
  2567. if (setTargetedHotend(105)) return;
  2568. #if HAS_TEMP_0 || HAS_TEMP_BED
  2569. SERIAL_PROTOCOLPGM("ok");
  2570. #if HAS_TEMP_0
  2571. SERIAL_PROTOCOLPGM(" T:");
  2572. SERIAL_PROTOCOL_F(degHotend(target_extruder), 1);
  2573. SERIAL_PROTOCOLPGM(" /");
  2574. SERIAL_PROTOCOL_F(degTargetHotend(target_extruder), 1);
  2575. #endif
  2576. #if HAS_TEMP_BED
  2577. SERIAL_PROTOCOLPGM(" B:");
  2578. SERIAL_PROTOCOL_F(degBed(), 1);
  2579. SERIAL_PROTOCOLPGM(" /");
  2580. SERIAL_PROTOCOL_F(degTargetBed(), 1);
  2581. #endif
  2582. for (int8_t e = 0; e < EXTRUDERS; ++e) {
  2583. SERIAL_PROTOCOLPGM(" T");
  2584. SERIAL_PROTOCOL(e);
  2585. SERIAL_PROTOCOLCHAR(':');
  2586. SERIAL_PROTOCOL_F(degHotend(e), 1);
  2587. SERIAL_PROTOCOLPGM(" /");
  2588. SERIAL_PROTOCOL_F(degTargetHotend(e), 1);
  2589. }
  2590. #else // !HAS_TEMP_0 && !HAS_TEMP_BED
  2591. SERIAL_ERROR_START;
  2592. SERIAL_ERRORLNPGM(MSG_ERR_NO_THERMISTORS);
  2593. #endif
  2594. SERIAL_PROTOCOLPGM(" @:");
  2595. #ifdef EXTRUDER_WATTS
  2596. SERIAL_PROTOCOL((EXTRUDER_WATTS * getHeaterPower(target_extruder))/127);
  2597. SERIAL_PROTOCOLCHAR('W');
  2598. #else
  2599. SERIAL_PROTOCOL(getHeaterPower(target_extruder));
  2600. #endif
  2601. SERIAL_PROTOCOLPGM(" B@:");
  2602. #ifdef BED_WATTS
  2603. SERIAL_PROTOCOL((BED_WATTS * getHeaterPower(-1))/127);
  2604. SERIAL_PROTOCOLCHAR('W');
  2605. #else
  2606. SERIAL_PROTOCOL(getHeaterPower(-1));
  2607. #endif
  2608. #ifdef SHOW_TEMP_ADC_VALUES
  2609. #if HAS_TEMP_BED
  2610. SERIAL_PROTOCOLPGM(" ADC B:");
  2611. SERIAL_PROTOCOL_F(degBed(),1);
  2612. SERIAL_PROTOCOLPGM("C->");
  2613. SERIAL_PROTOCOL_F(rawBedTemp()/OVERSAMPLENR,0);
  2614. #endif
  2615. for (int8_t cur_extruder = 0; cur_extruder < EXTRUDERS; ++cur_extruder) {
  2616. SERIAL_PROTOCOLPGM(" T");
  2617. SERIAL_PROTOCOL(cur_extruder);
  2618. SERIAL_PROTOCOLCHAR(':');
  2619. SERIAL_PROTOCOL_F(degHotend(cur_extruder),1);
  2620. SERIAL_PROTOCOLPGM("C->");
  2621. SERIAL_PROTOCOL_F(rawHotendTemp(cur_extruder)/OVERSAMPLENR,0);
  2622. }
  2623. #endif
  2624. SERIAL_EOL;
  2625. }
  2626. #if HAS_FAN
  2627. /**
  2628. * M106: Set Fan Speed
  2629. */
  2630. inline void gcode_M106() { fanSpeed = code_seen('S') ? constrain(code_value_short(), 0, 255) : 255; }
  2631. /**
  2632. * M107: Fan Off
  2633. */
  2634. inline void gcode_M107() { fanSpeed = 0; }
  2635. #endif // HAS_FAN
  2636. /**
  2637. * M109: Wait for extruder(s) to reach temperature
  2638. */
  2639. inline void gcode_M109() {
  2640. if (setTargetedHotend(109)) return;
  2641. LCD_MESSAGEPGM(MSG_HEATING);
  2642. CooldownNoWait = code_seen('S');
  2643. if (CooldownNoWait || code_seen('R')) {
  2644. float temp = code_value();
  2645. setTargetHotend(temp, target_extruder);
  2646. #ifdef DUAL_X_CARRIAGE
  2647. if (dual_x_carriage_mode == DXC_DUPLICATION_MODE && target_extruder == 0)
  2648. setTargetHotend1(temp == 0.0 ? 0.0 : temp + duplicate_extruder_temp_offset);
  2649. #endif
  2650. }
  2651. #ifdef AUTOTEMP
  2652. autotemp_enabled = code_seen('F');
  2653. if (autotemp_enabled) autotemp_factor = code_value();
  2654. if (code_seen('S')) autotemp_min = code_value();
  2655. if (code_seen('B')) autotemp_max = code_value();
  2656. #endif
  2657. setWatch();
  2658. unsigned long timetemp = millis();
  2659. /* See if we are heating up or cooling down */
  2660. target_direction = isHeatingHotend(target_extruder); // true if heating, false if cooling
  2661. cancel_heatup = false;
  2662. #ifdef TEMP_RESIDENCY_TIME
  2663. long residencyStart = -1;
  2664. /* continue to loop until we have reached the target temp
  2665. _and_ until TEMP_RESIDENCY_TIME hasn't passed since we reached it */
  2666. while((!cancel_heatup)&&((residencyStart == -1) ||
  2667. (residencyStart >= 0 && (((unsigned int) (millis() - residencyStart)) < (TEMP_RESIDENCY_TIME * 1000UL)))) )
  2668. #else
  2669. while ( target_direction ? (isHeatingHotend(target_extruder)) : (isCoolingHotend(target_extruder)&&(CooldownNoWait==false)) )
  2670. #endif //TEMP_RESIDENCY_TIME
  2671. { // while loop
  2672. if (millis() > timetemp + 1000UL) { //Print temp & remaining time every 1s while waiting
  2673. SERIAL_PROTOCOLPGM("T:");
  2674. SERIAL_PROTOCOL_F(degHotend(target_extruder),1);
  2675. SERIAL_PROTOCOLPGM(" E:");
  2676. SERIAL_PROTOCOL((int)target_extruder);
  2677. #ifdef TEMP_RESIDENCY_TIME
  2678. SERIAL_PROTOCOLPGM(" W:");
  2679. if (residencyStart > -1) {
  2680. timetemp = ((TEMP_RESIDENCY_TIME * 1000UL) - (millis() - residencyStart)) / 1000UL;
  2681. SERIAL_PROTOCOLLN( timetemp );
  2682. }
  2683. else {
  2684. SERIAL_PROTOCOLLNPGM("?");
  2685. }
  2686. #else
  2687. SERIAL_EOL;
  2688. #endif
  2689. timetemp = millis();
  2690. }
  2691. manage_heater();
  2692. manage_inactivity();
  2693. lcd_update();
  2694. #ifdef TEMP_RESIDENCY_TIME
  2695. // start/restart the TEMP_RESIDENCY_TIME timer whenever we reach target temp for the first time
  2696. // or when current temp falls outside the hysteresis after target temp was reached
  2697. if ((residencyStart == -1 && target_direction && (degHotend(target_extruder) >= (degTargetHotend(target_extruder)-TEMP_WINDOW))) ||
  2698. (residencyStart == -1 && !target_direction && (degHotend(target_extruder) <= (degTargetHotend(target_extruder)+TEMP_WINDOW))) ||
  2699. (residencyStart > -1 && labs(degHotend(target_extruder) - degTargetHotend(target_extruder)) > TEMP_HYSTERESIS) )
  2700. {
  2701. residencyStart = millis();
  2702. }
  2703. #endif //TEMP_RESIDENCY_TIME
  2704. }
  2705. LCD_MESSAGEPGM(MSG_HEATING_COMPLETE);
  2706. refresh_cmd_timeout();
  2707. starttime = previous_millis_cmd;
  2708. }
  2709. #if HAS_TEMP_BED
  2710. /**
  2711. * M190: Sxxx Wait for bed current temp to reach target temp. Waits only when heating
  2712. * Rxxx Wait for bed current temp to reach target temp. Waits when heating and cooling
  2713. */
  2714. inline void gcode_M190() {
  2715. LCD_MESSAGEPGM(MSG_BED_HEATING);
  2716. CooldownNoWait = code_seen('S');
  2717. if (CooldownNoWait || code_seen('R'))
  2718. setTargetBed(code_value());
  2719. unsigned long timetemp = millis();
  2720. cancel_heatup = false;
  2721. target_direction = isHeatingBed(); // true if heating, false if cooling
  2722. while ( (target_direction)&&(!cancel_heatup) ? (isHeatingBed()) : (isCoolingBed()&&(CooldownNoWait==false)) ) {
  2723. unsigned long ms = millis();
  2724. if (ms > timetemp + 1000UL) { //Print Temp Reading every 1 second while heating up.
  2725. timetemp = ms;
  2726. float tt = degHotend(active_extruder);
  2727. SERIAL_PROTOCOLPGM("T:");
  2728. SERIAL_PROTOCOL(tt);
  2729. SERIAL_PROTOCOLPGM(" E:");
  2730. SERIAL_PROTOCOL((int)active_extruder);
  2731. SERIAL_PROTOCOLPGM(" B:");
  2732. SERIAL_PROTOCOL_F(degBed(), 1);
  2733. SERIAL_EOL;
  2734. }
  2735. manage_heater();
  2736. manage_inactivity();
  2737. lcd_update();
  2738. }
  2739. LCD_MESSAGEPGM(MSG_BED_DONE);
  2740. refresh_cmd_timeout();
  2741. }
  2742. #endif // HAS_TEMP_BED
  2743. /**
  2744. * M112: Emergency Stop
  2745. */
  2746. inline void gcode_M112() {
  2747. kill();
  2748. }
  2749. #ifdef BARICUDA
  2750. #if HAS_HEATER_1
  2751. /**
  2752. * M126: Heater 1 valve open
  2753. */
  2754. inline void gcode_M126() { ValvePressure = code_seen('S') ? constrain(code_value(), 0, 255) : 255; }
  2755. /**
  2756. * M127: Heater 1 valve close
  2757. */
  2758. inline void gcode_M127() { ValvePressure = 0; }
  2759. #endif
  2760. #if HAS_HEATER_2
  2761. /**
  2762. * M128: Heater 2 valve open
  2763. */
  2764. inline void gcode_M128() { EtoPPressure = code_seen('S') ? constrain(code_value(), 0, 255) : 255; }
  2765. /**
  2766. * M129: Heater 2 valve close
  2767. */
  2768. inline void gcode_M129() { EtoPPressure = 0; }
  2769. #endif
  2770. #endif //BARICUDA
  2771. /**
  2772. * M140: Set bed temperature
  2773. */
  2774. inline void gcode_M140() {
  2775. if (code_seen('S')) setTargetBed(code_value());
  2776. }
  2777. #if HAS_POWER_SWITCH
  2778. /**
  2779. * M80: Turn on Power Supply
  2780. */
  2781. inline void gcode_M80() {
  2782. OUT_WRITE(PS_ON_PIN, PS_ON_AWAKE); //GND
  2783. // If you have a switch on suicide pin, this is useful
  2784. // if you want to start another print with suicide feature after
  2785. // a print without suicide...
  2786. #if HAS_SUICIDE
  2787. OUT_WRITE(SUICIDE_PIN, HIGH);
  2788. #endif
  2789. #ifdef ULTIPANEL
  2790. powersupply = true;
  2791. LCD_MESSAGEPGM(WELCOME_MSG);
  2792. lcd_update();
  2793. #endif
  2794. }
  2795. #endif // HAS_POWER_SWITCH
  2796. /**
  2797. * M81: Turn off Power, including Power Supply, if there is one.
  2798. *
  2799. * This code should ALWAYS be available for EMERGENCY SHUTDOWN!
  2800. */
  2801. inline void gcode_M81() {
  2802. disable_heater();
  2803. st_synchronize();
  2804. disable_e0();
  2805. disable_e1();
  2806. disable_e2();
  2807. disable_e3();
  2808. finishAndDisableSteppers();
  2809. fanSpeed = 0;
  2810. delay(1000); // Wait 1 second before switching off
  2811. #if HAS_SUICIDE
  2812. st_synchronize();
  2813. suicide();
  2814. #elif HAS_POWER_SWITCH
  2815. OUT_WRITE(PS_ON_PIN, PS_ON_ASLEEP);
  2816. #endif
  2817. #ifdef ULTIPANEL
  2818. #if HAS_POWER_SWITCH
  2819. powersupply = false;
  2820. #endif
  2821. LCD_MESSAGEPGM(MACHINE_NAME " " MSG_OFF ".");
  2822. lcd_update();
  2823. #endif
  2824. }
  2825. /**
  2826. * M82: Set E codes absolute (default)
  2827. */
  2828. inline void gcode_M82() { axis_relative_modes[E_AXIS] = false; }
  2829. /**
  2830. * M82: Set E codes relative while in Absolute Coordinates (G90) mode
  2831. */
  2832. inline void gcode_M83() { axis_relative_modes[E_AXIS] = true; }
  2833. /**
  2834. * M18, M84: Disable all stepper motors
  2835. */
  2836. inline void gcode_M18_M84() {
  2837. if (code_seen('S')) {
  2838. stepper_inactive_time = code_value() * 1000;
  2839. }
  2840. else {
  2841. 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])));
  2842. if (all_axis) {
  2843. st_synchronize();
  2844. disable_e0();
  2845. disable_e1();
  2846. disable_e2();
  2847. disable_e3();
  2848. finishAndDisableSteppers();
  2849. }
  2850. else {
  2851. st_synchronize();
  2852. if (code_seen('X')) disable_x();
  2853. if (code_seen('Y')) disable_y();
  2854. if (code_seen('Z')) disable_z();
  2855. #if ((E0_ENABLE_PIN != X_ENABLE_PIN) && (E1_ENABLE_PIN != Y_ENABLE_PIN)) // Only enable on boards that have seperate ENABLE_PINS
  2856. if (code_seen('E')) {
  2857. disable_e0();
  2858. disable_e1();
  2859. disable_e2();
  2860. disable_e3();
  2861. }
  2862. #endif
  2863. }
  2864. }
  2865. }
  2866. /**
  2867. * M85: Set inactivity shutdown timer with parameter S<seconds>. To disable set zero (default)
  2868. */
  2869. inline void gcode_M85() {
  2870. if (code_seen('S')) max_inactive_time = code_value() * 1000;
  2871. }
  2872. /**
  2873. * M92: Set inactivity shutdown timer with parameter S<seconds>. To disable set zero (default)
  2874. */
  2875. inline void gcode_M92() {
  2876. for(int8_t i=0; i < NUM_AXIS; i++) {
  2877. if (code_seen(axis_codes[i])) {
  2878. if (i == E_AXIS) {
  2879. float value = code_value();
  2880. if (value < 20.0) {
  2881. float factor = axis_steps_per_unit[i] / value; // increase e constants if M92 E14 is given for netfab.
  2882. max_e_jerk *= factor;
  2883. max_feedrate[i] *= factor;
  2884. axis_steps_per_sqr_second[i] *= factor;
  2885. }
  2886. axis_steps_per_unit[i] = value;
  2887. }
  2888. else {
  2889. axis_steps_per_unit[i] = code_value();
  2890. }
  2891. }
  2892. }
  2893. }
  2894. /**
  2895. * M114: Output current position to serial port
  2896. */
  2897. inline void gcode_M114() {
  2898. SERIAL_PROTOCOLPGM("X:");
  2899. SERIAL_PROTOCOL(current_position[X_AXIS]);
  2900. SERIAL_PROTOCOLPGM(" Y:");
  2901. SERIAL_PROTOCOL(current_position[Y_AXIS]);
  2902. SERIAL_PROTOCOLPGM(" Z:");
  2903. SERIAL_PROTOCOL(current_position[Z_AXIS]);
  2904. SERIAL_PROTOCOLPGM(" E:");
  2905. SERIAL_PROTOCOL(current_position[E_AXIS]);
  2906. SERIAL_PROTOCOLPGM(MSG_COUNT_X);
  2907. SERIAL_PROTOCOL(float(st_get_position(X_AXIS))/axis_steps_per_unit[X_AXIS]);
  2908. SERIAL_PROTOCOLPGM(" Y:");
  2909. SERIAL_PROTOCOL(float(st_get_position(Y_AXIS))/axis_steps_per_unit[Y_AXIS]);
  2910. SERIAL_PROTOCOLPGM(" Z:");
  2911. SERIAL_PROTOCOL(float(st_get_position(Z_AXIS))/axis_steps_per_unit[Z_AXIS]);
  2912. SERIAL_EOL;
  2913. #ifdef SCARA
  2914. SERIAL_PROTOCOLPGM("SCARA Theta:");
  2915. SERIAL_PROTOCOL(delta[X_AXIS]);
  2916. SERIAL_PROTOCOLPGM(" Psi+Theta:");
  2917. SERIAL_PROTOCOL(delta[Y_AXIS]);
  2918. SERIAL_EOL;
  2919. SERIAL_PROTOCOLPGM("SCARA Cal - Theta:");
  2920. SERIAL_PROTOCOL(delta[X_AXIS]+home_offset[X_AXIS]);
  2921. SERIAL_PROTOCOLPGM(" Psi+Theta (90):");
  2922. SERIAL_PROTOCOL(delta[Y_AXIS]-delta[X_AXIS]-90+home_offset[Y_AXIS]);
  2923. SERIAL_EOL;
  2924. SERIAL_PROTOCOLPGM("SCARA step Cal - Theta:");
  2925. SERIAL_PROTOCOL(delta[X_AXIS]/90*axis_steps_per_unit[X_AXIS]);
  2926. SERIAL_PROTOCOLPGM(" Psi+Theta:");
  2927. SERIAL_PROTOCOL((delta[Y_AXIS]-delta[X_AXIS])/90*axis_steps_per_unit[Y_AXIS]);
  2928. SERIAL_EOL; SERIAL_EOL;
  2929. #endif
  2930. }
  2931. /**
  2932. * M115: Capabilities string
  2933. */
  2934. inline void gcode_M115() {
  2935. SERIAL_PROTOCOLPGM(MSG_M115_REPORT);
  2936. }
  2937. /**
  2938. * M117: Set LCD Status Message
  2939. */
  2940. inline void gcode_M117() {
  2941. char* codepos = strchr_pointer + 5;
  2942. char* starpos = strchr(codepos, '*');
  2943. if (starpos) *starpos = '\0';
  2944. lcd_setstatus(codepos);
  2945. }
  2946. /**
  2947. * M119: Output endstop states to serial output
  2948. */
  2949. inline void gcode_M119() {
  2950. SERIAL_PROTOCOLLN(MSG_M119_REPORT);
  2951. #if HAS_X_MIN
  2952. SERIAL_PROTOCOLPGM(MSG_X_MIN);
  2953. SERIAL_PROTOCOLLN(((READ(X_MIN_PIN)^X_MIN_ENDSTOP_INVERTING)?MSG_ENDSTOP_HIT:MSG_ENDSTOP_OPEN));
  2954. #endif
  2955. #if HAS_X_MAX
  2956. SERIAL_PROTOCOLPGM(MSG_X_MAX);
  2957. SERIAL_PROTOCOLLN(((READ(X_MAX_PIN)^X_MAX_ENDSTOP_INVERTING)?MSG_ENDSTOP_HIT:MSG_ENDSTOP_OPEN));
  2958. #endif
  2959. #if HAS_Y_MIN
  2960. SERIAL_PROTOCOLPGM(MSG_Y_MIN);
  2961. SERIAL_PROTOCOLLN(((READ(Y_MIN_PIN)^Y_MIN_ENDSTOP_INVERTING)?MSG_ENDSTOP_HIT:MSG_ENDSTOP_OPEN));
  2962. #endif
  2963. #if HAS_Y_MAX
  2964. SERIAL_PROTOCOLPGM(MSG_Y_MAX);
  2965. SERIAL_PROTOCOLLN(((READ(Y_MAX_PIN)^Y_MAX_ENDSTOP_INVERTING)?MSG_ENDSTOP_HIT:MSG_ENDSTOP_OPEN));
  2966. #endif
  2967. #if HAS_Z_MIN
  2968. SERIAL_PROTOCOLPGM(MSG_Z_MIN);
  2969. SERIAL_PROTOCOLLN(((READ(Z_MIN_PIN)^Z_MIN_ENDSTOP_INVERTING)?MSG_ENDSTOP_HIT:MSG_ENDSTOP_OPEN));
  2970. #endif
  2971. #if HAS_Z_MAX
  2972. SERIAL_PROTOCOLPGM(MSG_Z_MAX);
  2973. SERIAL_PROTOCOLLN(((READ(Z_MAX_PIN)^Z_MAX_ENDSTOP_INVERTING)?MSG_ENDSTOP_HIT:MSG_ENDSTOP_OPEN));
  2974. #endif
  2975. #if HAS_Z2_MAX
  2976. SERIAL_PROTOCOLPGM(MSG_Z2_MAX);
  2977. SERIAL_PROTOCOLLN(((READ(Z2_MAX_PIN)^Z2_MAX_ENDSTOP_INVERTING)?MSG_ENDSTOP_HIT:MSG_ENDSTOP_OPEN));
  2978. #endif
  2979. #if HAS_Z_PROBE
  2980. SERIAL_PROTOCOLPGM(MSG_Z_PROBE);
  2981. SERIAL_PROTOCOLLN(((READ(Z_PROBE_PIN)^Z_PROBE_ENDSTOP_INVERTING)?MSG_ENDSTOP_HIT:MSG_ENDSTOP_OPEN));
  2982. #endif
  2983. }
  2984. /**
  2985. * M120: Enable endstops
  2986. */
  2987. inline void gcode_M120() { enable_endstops(false); }
  2988. /**
  2989. * M121: Disable endstops
  2990. */
  2991. inline void gcode_M121() { enable_endstops(true); }
  2992. #ifdef BLINKM
  2993. /**
  2994. * M150: Set Status LED Color - Use R-U-B for R-G-B
  2995. */
  2996. inline void gcode_M150() {
  2997. SendColors(
  2998. code_seen('R') ? (byte)code_value_short() : 0,
  2999. code_seen('U') ? (byte)code_value_short() : 0,
  3000. code_seen('B') ? (byte)code_value_short() : 0
  3001. );
  3002. }
  3003. #endif // BLINKM
  3004. /**
  3005. * M200: Set filament diameter and set E axis units to cubic millimeters (use S0 to set back to millimeters).
  3006. * T<extruder>
  3007. * D<millimeters>
  3008. */
  3009. inline void gcode_M200() {
  3010. int tmp_extruder = active_extruder;
  3011. if (code_seen('T')) {
  3012. tmp_extruder = code_value_short();
  3013. if (tmp_extruder >= EXTRUDERS) {
  3014. SERIAL_ECHO_START;
  3015. SERIAL_ECHO(MSG_M200_INVALID_EXTRUDER);
  3016. return;
  3017. }
  3018. }
  3019. if (code_seen('D')) {
  3020. float diameter = code_value();
  3021. // setting any extruder filament size disables volumetric on the assumption that
  3022. // slicers either generate in extruder values as cubic mm or as as filament feeds
  3023. // for all extruders
  3024. volumetric_enabled = (diameter != 0.0);
  3025. if (volumetric_enabled) {
  3026. filament_size[tmp_extruder] = diameter;
  3027. // make sure all extruders have some sane value for the filament size
  3028. for (int i=0; i<EXTRUDERS; i++)
  3029. if (! filament_size[i]) filament_size[i] = DEFAULT_NOMINAL_FILAMENT_DIA;
  3030. }
  3031. }
  3032. else {
  3033. //reserved for setting filament diameter via UFID or filament measuring device
  3034. return;
  3035. }
  3036. calculate_volumetric_multipliers();
  3037. }
  3038. /**
  3039. * M201: Set max acceleration in units/s^2 for print moves (M201 X1000 Y1000)
  3040. */
  3041. inline void gcode_M201() {
  3042. for (int8_t i=0; i < NUM_AXIS; i++) {
  3043. if (code_seen(axis_codes[i])) {
  3044. max_acceleration_units_per_sq_second[i] = code_value();
  3045. }
  3046. }
  3047. // 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)
  3048. reset_acceleration_rates();
  3049. }
  3050. #if 0 // Not used for Sprinter/grbl gen6
  3051. inline void gcode_M202() {
  3052. for(int8_t i=0; i < NUM_AXIS; i++) {
  3053. if(code_seen(axis_codes[i])) axis_travel_steps_per_sqr_second[i] = code_value() * axis_steps_per_unit[i];
  3054. }
  3055. }
  3056. #endif
  3057. /**
  3058. * M203: Set maximum feedrate that your machine can sustain (M203 X200 Y200 Z300 E10000) in mm/sec
  3059. */
  3060. inline void gcode_M203() {
  3061. for (int8_t i=0; i < NUM_AXIS; i++) {
  3062. if (code_seen(axis_codes[i])) {
  3063. max_feedrate[i] = code_value();
  3064. }
  3065. }
  3066. }
  3067. /**
  3068. * M204: Set Accelerations in mm/sec^2 (M204 P1200 R3000 T3000)
  3069. *
  3070. * P = Printing moves
  3071. * R = Retract only (no X, Y, Z) moves
  3072. * T = Travel (non printing) moves
  3073. *
  3074. * Also sets minimum segment time in ms (B20000) to prevent buffer under-runs and M20 minimum feedrate
  3075. */
  3076. inline void gcode_M204() {
  3077. if (code_seen('S')) { // Kept for legacy compatibility. Should NOT BE USED for new developments.
  3078. acceleration = code_value();
  3079. travel_acceleration = acceleration;
  3080. SERIAL_ECHOPAIR("Setting Print and Travel Acceleration: ", acceleration );
  3081. SERIAL_EOL;
  3082. }
  3083. if (code_seen('P')) {
  3084. acceleration = code_value();
  3085. SERIAL_ECHOPAIR("Setting Print Acceleration: ", acceleration );
  3086. SERIAL_EOL;
  3087. }
  3088. if (code_seen('R')) {
  3089. retract_acceleration = code_value();
  3090. SERIAL_ECHOPAIR("Setting Retract Acceleration: ", retract_acceleration );
  3091. SERIAL_EOL;
  3092. }
  3093. if (code_seen('T')) {
  3094. travel_acceleration = code_value();
  3095. SERIAL_ECHOPAIR("Setting Travel Acceleration: ", travel_acceleration );
  3096. SERIAL_EOL;
  3097. }
  3098. }
  3099. /**
  3100. * M205: Set Advanced Settings
  3101. *
  3102. * S = Min Feed Rate (mm/s)
  3103. * T = Min Travel Feed Rate (mm/s)
  3104. * B = Min Segment Time (µs)
  3105. * X = Max XY Jerk (mm/s/s)
  3106. * Z = Max Z Jerk (mm/s/s)
  3107. * E = Max E Jerk (mm/s/s)
  3108. */
  3109. inline void gcode_M205() {
  3110. if (code_seen('S')) minimumfeedrate = code_value();
  3111. if (code_seen('T')) mintravelfeedrate = code_value();
  3112. if (code_seen('B')) minsegmenttime = code_value();
  3113. if (code_seen('X')) max_xy_jerk = code_value();
  3114. if (code_seen('Z')) max_z_jerk = code_value();
  3115. if (code_seen('E')) max_e_jerk = code_value();
  3116. }
  3117. /**
  3118. * M206: Set Additional Homing Offset (X Y Z). SCARA aliases T=X, P=Y
  3119. */
  3120. inline void gcode_M206() {
  3121. for (int8_t i=X_AXIS; i <= Z_AXIS; i++) {
  3122. if (code_seen(axis_codes[i])) {
  3123. home_offset[i] = code_value();
  3124. }
  3125. }
  3126. #ifdef SCARA
  3127. if (code_seen('T')) home_offset[X_AXIS] = code_value(); // Theta
  3128. if (code_seen('P')) home_offset[Y_AXIS] = code_value(); // Psi
  3129. #endif
  3130. }
  3131. #ifdef DELTA
  3132. /**
  3133. * M665: Set delta configurations
  3134. *
  3135. * L = diagonal rod
  3136. * R = delta radius
  3137. * S = segments per second
  3138. */
  3139. inline void gcode_M665() {
  3140. if (code_seen('L')) delta_diagonal_rod = code_value();
  3141. if (code_seen('R')) delta_radius = code_value();
  3142. if (code_seen('S')) delta_segments_per_second = code_value();
  3143. recalc_delta_settings(delta_radius, delta_diagonal_rod);
  3144. }
  3145. /**
  3146. * M666: Set delta endstop adjustment
  3147. */
  3148. inline void gcode_M666() {
  3149. for (int8_t i = 0; i < 3; i++) {
  3150. if (code_seen(axis_codes[i])) {
  3151. endstop_adj[i] = code_value();
  3152. }
  3153. }
  3154. }
  3155. #elif defined(Z_DUAL_ENDSTOPS)
  3156. /**
  3157. * M666: For Z Dual Endstop setup, set z axis offset to the z2 axis.
  3158. */
  3159. inline void gcode_M666() {
  3160. if (code_seen('Z')) z_endstop_adj = code_value();
  3161. SERIAL_ECHOPAIR("Z Endstop Adjustment set to (mm):", z_endstop_adj );
  3162. SERIAL_EOL;
  3163. }
  3164. #endif // DELTA
  3165. #ifdef FWRETRACT
  3166. /**
  3167. * M207: Set retract length S[positive mm] F[feedrate mm/min] Z[additional zlift/hop]
  3168. */
  3169. inline void gcode_M207() {
  3170. if (code_seen('S')) retract_length = code_value();
  3171. if (code_seen('F')) retract_feedrate = code_value() / 60;
  3172. if (code_seen('Z')) retract_zlift = code_value();
  3173. }
  3174. /**
  3175. * M208: Set retract recover length S[positive mm surplus to the M207 S*] F[feedrate mm/min]
  3176. */
  3177. inline void gcode_M208() {
  3178. if (code_seen('S')) retract_recover_length = code_value();
  3179. if (code_seen('F')) retract_recover_feedrate = code_value() / 60;
  3180. }
  3181. /**
  3182. * M209: Enable automatic retract (M209 S1)
  3183. * detect if the slicer did not support G10/11: every normal extrude-only move will be classified as retract depending on the direction.
  3184. */
  3185. inline void gcode_M209() {
  3186. if (code_seen('S')) {
  3187. int t = code_value_short();
  3188. switch(t) {
  3189. case 0:
  3190. autoretract_enabled = false;
  3191. break;
  3192. case 1:
  3193. autoretract_enabled = true;
  3194. break;
  3195. default:
  3196. SERIAL_ECHO_START;
  3197. SERIAL_ECHOPGM(MSG_UNKNOWN_COMMAND);
  3198. SERIAL_ECHO(cmdbuffer[bufindr]);
  3199. SERIAL_ECHOLNPGM("\"");
  3200. return;
  3201. }
  3202. for (int i=0; i<EXTRUDERS; i++) retracted[i] = false;
  3203. }
  3204. }
  3205. #endif // FWRETRACT
  3206. #if EXTRUDERS > 1
  3207. /**
  3208. * M218 - set hotend offset (in mm), T<extruder_number> X<offset_on_X> Y<offset_on_Y>
  3209. */
  3210. inline void gcode_M218() {
  3211. if (setTargetedHotend(218)) return;
  3212. if (code_seen('X')) extruder_offset[X_AXIS][target_extruder] = code_value();
  3213. if (code_seen('Y')) extruder_offset[Y_AXIS][target_extruder] = code_value();
  3214. #ifdef DUAL_X_CARRIAGE
  3215. if (code_seen('Z')) extruder_offset[Z_AXIS][target_extruder] = code_value();
  3216. #endif
  3217. SERIAL_ECHO_START;
  3218. SERIAL_ECHOPGM(MSG_HOTEND_OFFSET);
  3219. for (int e = 0; e < EXTRUDERS; e++) {
  3220. SERIAL_CHAR(' ');
  3221. SERIAL_ECHO(extruder_offset[X_AXIS][e]);
  3222. SERIAL_CHAR(',');
  3223. SERIAL_ECHO(extruder_offset[Y_AXIS][e]);
  3224. #ifdef DUAL_X_CARRIAGE
  3225. SERIAL_CHAR(',');
  3226. SERIAL_ECHO(extruder_offset[Z_AXIS][e]);
  3227. #endif
  3228. }
  3229. SERIAL_EOL;
  3230. }
  3231. #endif // EXTRUDERS > 1
  3232. /**
  3233. * M220: Set speed percentage factor, aka "Feed Rate" (M220 S95)
  3234. */
  3235. inline void gcode_M220() {
  3236. if (code_seen('S')) feedmultiply = code_value();
  3237. }
  3238. /**
  3239. * M221: Set extrusion percentage (M221 T0 S95)
  3240. */
  3241. inline void gcode_M221() {
  3242. if (code_seen('S')) {
  3243. int sval = code_value();
  3244. if (code_seen('T')) {
  3245. if (setTargetedHotend(221)) return;
  3246. extruder_multiply[target_extruder] = sval;
  3247. }
  3248. else {
  3249. extruder_multiply[active_extruder] = sval;
  3250. }
  3251. }
  3252. }
  3253. /**
  3254. * M226: Wait until the specified pin reaches the state required (M226 P<pin> S<state>)
  3255. */
  3256. inline void gcode_M226() {
  3257. if (code_seen('P')) {
  3258. int pin_number = code_value();
  3259. int pin_state = code_seen('S') ? code_value() : -1; // required pin state - default is inverted
  3260. if (pin_state >= -1 && pin_state <= 1) {
  3261. for (int8_t i = 0; i < (int8_t)(sizeof(sensitive_pins)/sizeof(*sensitive_pins)); i++) {
  3262. if (sensitive_pins[i] == pin_number) {
  3263. pin_number = -1;
  3264. break;
  3265. }
  3266. }
  3267. if (pin_number > -1) {
  3268. int target = LOW;
  3269. st_synchronize();
  3270. pinMode(pin_number, INPUT);
  3271. switch(pin_state){
  3272. case 1:
  3273. target = HIGH;
  3274. break;
  3275. case 0:
  3276. target = LOW;
  3277. break;
  3278. case -1:
  3279. target = !digitalRead(pin_number);
  3280. break;
  3281. }
  3282. while(digitalRead(pin_number) != target) {
  3283. manage_heater();
  3284. manage_inactivity();
  3285. lcd_update();
  3286. }
  3287. } // pin_number > -1
  3288. } // pin_state -1 0 1
  3289. } // code_seen('P')
  3290. }
  3291. #if NUM_SERVOS > 0
  3292. /**
  3293. * M280: Set servo position absolute. P: servo index, S: angle or microseconds
  3294. */
  3295. inline void gcode_M280() {
  3296. int servo_index = code_seen('P') ? code_value() : -1;
  3297. int servo_position = 0;
  3298. if (code_seen('S')) {
  3299. servo_position = code_value();
  3300. if ((servo_index >= 0) && (servo_index < NUM_SERVOS)) {
  3301. #if SERVO_LEVELING
  3302. servos[servo_index].attach(0);
  3303. #endif
  3304. servos[servo_index].write(servo_position);
  3305. #if SERVO_LEVELING
  3306. delay(PROBE_SERVO_DEACTIVATION_DELAY);
  3307. servos[servo_index].detach();
  3308. #endif
  3309. }
  3310. else {
  3311. SERIAL_ECHO_START;
  3312. SERIAL_ECHO("Servo ");
  3313. SERIAL_ECHO(servo_index);
  3314. SERIAL_ECHOLN(" out of range");
  3315. }
  3316. }
  3317. else if (servo_index >= 0) {
  3318. SERIAL_PROTOCOL(MSG_OK);
  3319. SERIAL_PROTOCOL(" Servo ");
  3320. SERIAL_PROTOCOL(servo_index);
  3321. SERIAL_PROTOCOL(": ");
  3322. SERIAL_PROTOCOL(servos[servo_index].read());
  3323. SERIAL_EOL;
  3324. }
  3325. }
  3326. #endif // NUM_SERVOS > 0
  3327. #if defined(LARGE_FLASH) && (BEEPER > 0 || defined(ULTRALCD) || defined(LCD_USE_I2C_BUZZER))
  3328. /**
  3329. * M300: Play beep sound S<frequency Hz> P<duration ms>
  3330. */
  3331. inline void gcode_M300() {
  3332. int beepS = code_seen('S') ? code_value() : 110;
  3333. int beepP = code_seen('P') ? code_value() : 1000;
  3334. if (beepS > 0) {
  3335. #if BEEPER > 0
  3336. tone(BEEPER, beepS);
  3337. delay(beepP);
  3338. noTone(BEEPER);
  3339. #elif defined(ULTRALCD)
  3340. lcd_buzz(beepS, beepP);
  3341. #elif defined(LCD_USE_I2C_BUZZER)
  3342. lcd_buzz(beepP, beepS);
  3343. #endif
  3344. }
  3345. else {
  3346. delay(beepP);
  3347. }
  3348. }
  3349. #endif // LARGE_FLASH && (BEEPER>0 || ULTRALCD || LCD_USE_I2C_BUZZER)
  3350. #ifdef PIDTEMP
  3351. /**
  3352. * M301: Set PID parameters P I D (and optionally C)
  3353. */
  3354. inline void gcode_M301() {
  3355. // multi-extruder PID patch: M301 updates or prints a single extruder's PID values
  3356. // default behaviour (omitting E parameter) is to update for extruder 0 only
  3357. int e = code_seen('E') ? code_value() : 0; // extruder being updated
  3358. if (e < EXTRUDERS) { // catch bad input value
  3359. if (code_seen('P')) PID_PARAM(Kp, e) = code_value();
  3360. if (code_seen('I')) PID_PARAM(Ki, e) = scalePID_i(code_value());
  3361. if (code_seen('D')) PID_PARAM(Kd, e) = scalePID_d(code_value());
  3362. #ifdef PID_ADD_EXTRUSION_RATE
  3363. if (code_seen('C')) PID_PARAM(Kc, e) = code_value();
  3364. #endif
  3365. updatePID();
  3366. SERIAL_PROTOCOL(MSG_OK);
  3367. #ifdef PID_PARAMS_PER_EXTRUDER
  3368. SERIAL_PROTOCOL(" e:"); // specify extruder in serial output
  3369. SERIAL_PROTOCOL(e);
  3370. #endif // PID_PARAMS_PER_EXTRUDER
  3371. SERIAL_PROTOCOL(" p:");
  3372. SERIAL_PROTOCOL(PID_PARAM(Kp, e));
  3373. SERIAL_PROTOCOL(" i:");
  3374. SERIAL_PROTOCOL(unscalePID_i(PID_PARAM(Ki, e)));
  3375. SERIAL_PROTOCOL(" d:");
  3376. SERIAL_PROTOCOL(unscalePID_d(PID_PARAM(Kd, e)));
  3377. #ifdef PID_ADD_EXTRUSION_RATE
  3378. SERIAL_PROTOCOL(" c:");
  3379. //Kc does not have scaling applied above, or in resetting defaults
  3380. SERIAL_PROTOCOL(PID_PARAM(Kc, e));
  3381. #endif
  3382. SERIAL_EOL;
  3383. }
  3384. else {
  3385. SERIAL_ECHO_START;
  3386. SERIAL_ECHOLN(MSG_INVALID_EXTRUDER);
  3387. }
  3388. }
  3389. #endif // PIDTEMP
  3390. #ifdef PIDTEMPBED
  3391. inline void gcode_M304() {
  3392. if (code_seen('P')) bedKp = code_value();
  3393. if (code_seen('I')) bedKi = scalePID_i(code_value());
  3394. if (code_seen('D')) bedKd = scalePID_d(code_value());
  3395. updatePID();
  3396. SERIAL_PROTOCOL(MSG_OK);
  3397. SERIAL_PROTOCOL(" p:");
  3398. SERIAL_PROTOCOL(bedKp);
  3399. SERIAL_PROTOCOL(" i:");
  3400. SERIAL_PROTOCOL(unscalePID_i(bedKi));
  3401. SERIAL_PROTOCOL(" d:");
  3402. SERIAL_PROTOCOL(unscalePID_d(bedKd));
  3403. SERIAL_EOL;
  3404. }
  3405. #endif // PIDTEMPBED
  3406. #if defined(CHDK) || HAS_PHOTOGRAPH
  3407. /**
  3408. * M240: Trigger a camera by emulating a Canon RC-1
  3409. * See http://www.doc-diy.net/photo/rc-1_hacked/
  3410. */
  3411. inline void gcode_M240() {
  3412. #ifdef CHDK
  3413. OUT_WRITE(CHDK, HIGH);
  3414. chdkHigh = millis();
  3415. chdkActive = true;
  3416. #elif HAS_PHOTOGRAPH
  3417. const uint8_t NUM_PULSES = 16;
  3418. const float PULSE_LENGTH = 0.01524;
  3419. for (int i = 0; i < NUM_PULSES; i++) {
  3420. WRITE(PHOTOGRAPH_PIN, HIGH);
  3421. _delay_ms(PULSE_LENGTH);
  3422. WRITE(PHOTOGRAPH_PIN, LOW);
  3423. _delay_ms(PULSE_LENGTH);
  3424. }
  3425. delay(7.33);
  3426. for (int i = 0; i < NUM_PULSES; i++) {
  3427. WRITE(PHOTOGRAPH_PIN, HIGH);
  3428. _delay_ms(PULSE_LENGTH);
  3429. WRITE(PHOTOGRAPH_PIN, LOW);
  3430. _delay_ms(PULSE_LENGTH);
  3431. }
  3432. #endif // !CHDK && HAS_PHOTOGRAPH
  3433. }
  3434. #endif // CHDK || PHOTOGRAPH_PIN
  3435. #ifdef DOGLCD
  3436. /**
  3437. * M250: Read and optionally set the LCD contrast
  3438. */
  3439. inline void gcode_M250() {
  3440. if (code_seen('C')) lcd_setcontrast(code_value_short() & 0x3F);
  3441. SERIAL_PROTOCOLPGM("lcd contrast value: ");
  3442. SERIAL_PROTOCOL(lcd_contrast);
  3443. SERIAL_EOL;
  3444. }
  3445. #endif // DOGLCD
  3446. #ifdef PREVENT_DANGEROUS_EXTRUDE
  3447. /**
  3448. * M302: Allow cold extrudes, or set the minimum extrude S<temperature>.
  3449. */
  3450. inline void gcode_M302() {
  3451. set_extrude_min_temp(code_seen('S') ? code_value() : 0);
  3452. }
  3453. #endif // PREVENT_DANGEROUS_EXTRUDE
  3454. /**
  3455. * M303: PID relay autotune
  3456. * S<temperature> sets the target temperature. (default target temperature = 150C)
  3457. * E<extruder> (-1 for the bed)
  3458. * C<cycles>
  3459. */
  3460. inline void gcode_M303() {
  3461. int e = code_seen('E') ? code_value_short() : 0;
  3462. int c = code_seen('C') ? code_value_short() : 5;
  3463. float temp = code_seen('S') ? code_value() : (e < 0 ? 70.0 : 150.0);
  3464. PID_autotune(temp, e, c);
  3465. }
  3466. #ifdef SCARA
  3467. bool SCARA_move_to_cal(uint8_t delta_x, uint8_t delta_y) {
  3468. //SoftEndsEnabled = false; // Ignore soft endstops during calibration
  3469. //SERIAL_ECHOLN(" Soft endstops disabled ");
  3470. if (! Stopped) {
  3471. //get_coordinates(); // For X Y Z E F
  3472. delta[X_AXIS] = delta_x;
  3473. delta[Y_AXIS] = delta_y;
  3474. calculate_SCARA_forward_Transform(delta);
  3475. destination[X_AXIS] = delta[X_AXIS]/axis_scaling[X_AXIS];
  3476. destination[Y_AXIS] = delta[Y_AXIS]/axis_scaling[Y_AXIS];
  3477. prepare_move();
  3478. //ClearToSend();
  3479. return true;
  3480. }
  3481. return false;
  3482. }
  3483. /**
  3484. * M360: SCARA calibration: Move to cal-position ThetaA (0 deg calibration)
  3485. */
  3486. inline bool gcode_M360() {
  3487. SERIAL_ECHOLN(" Cal: Theta 0 ");
  3488. return SCARA_move_to_cal(0, 120);
  3489. }
  3490. /**
  3491. * M361: SCARA calibration: Move to cal-position ThetaB (90 deg calibration - steps per degree)
  3492. */
  3493. inline bool gcode_M361() {
  3494. SERIAL_ECHOLN(" Cal: Theta 90 ");
  3495. return SCARA_move_to_cal(90, 130);
  3496. }
  3497. /**
  3498. * M362: SCARA calibration: Move to cal-position PsiA (0 deg calibration)
  3499. */
  3500. inline bool gcode_M362() {
  3501. SERIAL_ECHOLN(" Cal: Psi 0 ");
  3502. return SCARA_move_to_cal(60, 180);
  3503. }
  3504. /**
  3505. * M363: SCARA calibration: Move to cal-position PsiB (90 deg calibration - steps per degree)
  3506. */
  3507. inline bool gcode_M363() {
  3508. SERIAL_ECHOLN(" Cal: Psi 90 ");
  3509. return SCARA_move_to_cal(50, 90);
  3510. }
  3511. /**
  3512. * M364: SCARA calibration: Move to cal-position PSIC (90 deg to Theta calibration position)
  3513. */
  3514. inline bool gcode_M364() {
  3515. SERIAL_ECHOLN(" Cal: Theta-Psi 90 ");
  3516. return SCARA_move_to_cal(45, 135);
  3517. }
  3518. /**
  3519. * M365: SCARA calibration: Scaling factor, X, Y, Z axis
  3520. */
  3521. inline void gcode_M365() {
  3522. for (int8_t i = X_AXIS; i <= Z_AXIS; i++) {
  3523. if (code_seen(axis_codes[i])) {
  3524. axis_scaling[i] = code_value();
  3525. }
  3526. }
  3527. }
  3528. #endif // SCARA
  3529. #ifdef EXT_SOLENOID
  3530. void enable_solenoid(uint8_t num) {
  3531. switch(num) {
  3532. case 0:
  3533. OUT_WRITE(SOL0_PIN, HIGH);
  3534. break;
  3535. #if HAS_SOLENOID_1
  3536. case 1:
  3537. OUT_WRITE(SOL1_PIN, HIGH);
  3538. break;
  3539. #endif
  3540. #if HAS_SOLENOID_2
  3541. case 2:
  3542. OUT_WRITE(SOL2_PIN, HIGH);
  3543. break;
  3544. #endif
  3545. #if HAS_SOLENOID_3
  3546. case 3:
  3547. OUT_WRITE(SOL3_PIN, HIGH);
  3548. break;
  3549. #endif
  3550. default:
  3551. SERIAL_ECHO_START;
  3552. SERIAL_ECHOLNPGM(MSG_INVALID_SOLENOID);
  3553. break;
  3554. }
  3555. }
  3556. void enable_solenoid_on_active_extruder() { enable_solenoid(active_extruder); }
  3557. void disable_all_solenoids() {
  3558. OUT_WRITE(SOL0_PIN, LOW);
  3559. OUT_WRITE(SOL1_PIN, LOW);
  3560. OUT_WRITE(SOL2_PIN, LOW);
  3561. OUT_WRITE(SOL3_PIN, LOW);
  3562. }
  3563. /**
  3564. * M380: Enable solenoid on the active extruder
  3565. */
  3566. inline void gcode_M380() { enable_solenoid_on_active_extruder(); }
  3567. /**
  3568. * M381: Disable all solenoids
  3569. */
  3570. inline void gcode_M381() { disable_all_solenoids(); }
  3571. #endif // EXT_SOLENOID
  3572. /**
  3573. * M400: Finish all moves
  3574. */
  3575. inline void gcode_M400() { st_synchronize(); }
  3576. #if defined(ENABLE_AUTO_BED_LEVELING) && (defined(SERVO_ENDSTOPS) || defined(Z_PROBE_ALLEN_KEY)) && not defined(Z_PROBE_SLED)
  3577. /**
  3578. * M401: Engage Z Servo endstop if available
  3579. */
  3580. inline void gcode_M401() { deploy_z_probe(); }
  3581. /**
  3582. * M402: Retract Z Servo endstop if enabled
  3583. */
  3584. inline void gcode_M402() { stow_z_probe(); }
  3585. #endif
  3586. #ifdef FILAMENT_SENSOR
  3587. /**
  3588. * M404: Display or set the nominal filament width (3mm, 1.75mm ) W<3.0>
  3589. */
  3590. inline void gcode_M404() {
  3591. #if HAS_FILWIDTH
  3592. if (code_seen('W')) {
  3593. filament_width_nominal = code_value();
  3594. }
  3595. else {
  3596. SERIAL_PROTOCOLPGM("Filament dia (nominal mm):");
  3597. SERIAL_PROTOCOLLN(filament_width_nominal);
  3598. }
  3599. #endif
  3600. }
  3601. /**
  3602. * M405: Turn on filament sensor for control
  3603. */
  3604. inline void gcode_M405() {
  3605. if (code_seen('D')) meas_delay_cm = code_value();
  3606. if (meas_delay_cm > MAX_MEASUREMENT_DELAY) meas_delay_cm = MAX_MEASUREMENT_DELAY;
  3607. if (delay_index2 == -1) { //initialize the ring buffer if it has not been done since startup
  3608. int temp_ratio = widthFil_to_size_ratio();
  3609. for (delay_index1 = 0; delay_index1 < MAX_MEASUREMENT_DELAY + 1; ++delay_index1)
  3610. measurement_delay[delay_index1] = temp_ratio - 100; //subtract 100 to scale within a signed byte
  3611. delay_index1 = delay_index2 = 0;
  3612. }
  3613. filament_sensor = true;
  3614. //SERIAL_PROTOCOLPGM("Filament dia (measured mm):");
  3615. //SERIAL_PROTOCOL(filament_width_meas);
  3616. //SERIAL_PROTOCOLPGM("Extrusion ratio(%):");
  3617. //SERIAL_PROTOCOL(extruder_multiply[active_extruder]);
  3618. }
  3619. /**
  3620. * M406: Turn off filament sensor for control
  3621. */
  3622. inline void gcode_M406() { filament_sensor = false; }
  3623. /**
  3624. * M407: Get measured filament diameter on serial output
  3625. */
  3626. inline void gcode_M407() {
  3627. SERIAL_PROTOCOLPGM("Filament dia (measured mm):");
  3628. SERIAL_PROTOCOLLN(filament_width_meas);
  3629. }
  3630. #endif // FILAMENT_SENSOR
  3631. /**
  3632. * M500: Store settings in EEPROM
  3633. */
  3634. inline void gcode_M500() {
  3635. Config_StoreSettings();
  3636. }
  3637. /**
  3638. * M501: Read settings from EEPROM
  3639. */
  3640. inline void gcode_M501() {
  3641. Config_RetrieveSettings();
  3642. }
  3643. /**
  3644. * M502: Revert to default settings
  3645. */
  3646. inline void gcode_M502() {
  3647. Config_ResetDefault();
  3648. }
  3649. /**
  3650. * M503: print settings currently in memory
  3651. */
  3652. inline void gcode_M503() {
  3653. Config_PrintSettings(code_seen('S') && code_value() == 0);
  3654. }
  3655. #ifdef ABORT_ON_ENDSTOP_HIT_FEATURE_ENABLED
  3656. /**
  3657. * M540: Set whether SD card print should abort on endstop hit (M540 S<0|1>)
  3658. */
  3659. inline void gcode_M540() {
  3660. if (code_seen('S')) abort_on_endstop_hit = (code_value() > 0);
  3661. }
  3662. #endif // ABORT_ON_ENDSTOP_HIT_FEATURE_ENABLED
  3663. #ifdef CUSTOM_M_CODE_SET_Z_PROBE_OFFSET
  3664. inline void gcode_SET_Z_PROBE_OFFSET() {
  3665. float value;
  3666. if (code_seen('Z')) {
  3667. value = code_value();
  3668. if (Z_PROBE_OFFSET_RANGE_MIN <= value && value <= Z_PROBE_OFFSET_RANGE_MAX) {
  3669. zprobe_zoffset = -value; // compare w/ line 278 of ConfigurationStore.cpp
  3670. SERIAL_ECHO_START;
  3671. SERIAL_ECHOLNPGM(MSG_ZPROBE_ZOFFSET " " MSG_OK);
  3672. SERIAL_EOL;
  3673. }
  3674. else {
  3675. SERIAL_ECHO_START;
  3676. SERIAL_ECHOPGM(MSG_ZPROBE_ZOFFSET);
  3677. SERIAL_ECHOPGM(MSG_Z_MIN);
  3678. SERIAL_ECHO(Z_PROBE_OFFSET_RANGE_MIN);
  3679. SERIAL_ECHOPGM(MSG_Z_MAX);
  3680. SERIAL_ECHO(Z_PROBE_OFFSET_RANGE_MAX);
  3681. SERIAL_EOL;
  3682. }
  3683. }
  3684. else {
  3685. SERIAL_ECHO_START;
  3686. SERIAL_ECHOLNPGM(MSG_ZPROBE_ZOFFSET " : ");
  3687. SERIAL_ECHO(-zprobe_zoffset);
  3688. SERIAL_EOL;
  3689. }
  3690. }
  3691. #endif // CUSTOM_M_CODE_SET_Z_PROBE_OFFSET
  3692. #ifdef FILAMENTCHANGEENABLE
  3693. /**
  3694. * M600: Pause for filament change X[pos] Y[pos] Z[relative lift] E[initial retract] L[later retract distance for removal]
  3695. */
  3696. inline void gcode_M600() {
  3697. float target[NUM_AXIS], lastpos[NUM_AXIS], fr60 = feedrate / 60;
  3698. for (int i=0; i<NUM_AXIS; i++)
  3699. target[i] = lastpos[i] = current_position[i];
  3700. #define BASICPLAN plan_buffer_line(target[X_AXIS], target[Y_AXIS], target[Z_AXIS], target[E_AXIS], fr60, active_extruder);
  3701. #ifdef DELTA
  3702. #define RUNPLAN calculate_delta(target); BASICPLAN
  3703. #else
  3704. #define RUNPLAN BASICPLAN
  3705. #endif
  3706. //retract by E
  3707. if (code_seen('E')) target[E_AXIS] += code_value();
  3708. #ifdef FILAMENTCHANGE_FIRSTRETRACT
  3709. else target[E_AXIS] += FILAMENTCHANGE_FIRSTRETRACT;
  3710. #endif
  3711. RUNPLAN;
  3712. //lift Z
  3713. if (code_seen('Z')) target[Z_AXIS] += code_value();
  3714. #ifdef FILAMENTCHANGE_ZADD
  3715. else target[Z_AXIS] += FILAMENTCHANGE_ZADD;
  3716. #endif
  3717. RUNPLAN;
  3718. //move xy
  3719. if (code_seen('X')) target[X_AXIS] = code_value();
  3720. #ifdef FILAMENTCHANGE_XPOS
  3721. else target[X_AXIS] = FILAMENTCHANGE_XPOS;
  3722. #endif
  3723. if (code_seen('Y')) target[Y_AXIS] = code_value();
  3724. #ifdef FILAMENTCHANGE_YPOS
  3725. else target[Y_AXIS] = FILAMENTCHANGE_YPOS;
  3726. #endif
  3727. RUNPLAN;
  3728. if (code_seen('L')) target[E_AXIS] += code_value();
  3729. #ifdef FILAMENTCHANGE_FINALRETRACT
  3730. else target[E_AXIS] += FILAMENTCHANGE_FINALRETRACT;
  3731. #endif
  3732. RUNPLAN;
  3733. //finish moves
  3734. st_synchronize();
  3735. //disable extruder steppers so filament can be removed
  3736. disable_e0();
  3737. disable_e1();
  3738. disable_e2();
  3739. disable_e3();
  3740. delay(100);
  3741. LCD_ALERTMESSAGEPGM(MSG_FILAMENTCHANGE);
  3742. uint8_t cnt = 0;
  3743. while (!lcd_clicked()) {
  3744. cnt++;
  3745. manage_heater();
  3746. manage_inactivity(true);
  3747. lcd_update();
  3748. if (cnt == 0) {
  3749. #if BEEPER > 0
  3750. OUT_WRITE(BEEPER,HIGH);
  3751. delay(3);
  3752. WRITE(BEEPER,LOW);
  3753. delay(3);
  3754. #else
  3755. #if !defined(LCD_FEEDBACK_FREQUENCY_HZ) || !defined(LCD_FEEDBACK_FREQUENCY_DURATION_MS)
  3756. lcd_buzz(1000/6, 100);
  3757. #else
  3758. lcd_buzz(LCD_FEEDBACK_FREQUENCY_DURATION_MS, LCD_FEEDBACK_FREQUENCY_HZ);
  3759. #endif
  3760. #endif
  3761. }
  3762. } // while(!lcd_clicked)
  3763. //return to normal
  3764. if (code_seen('L')) target[E_AXIS] -= code_value();
  3765. #ifdef FILAMENTCHANGE_FINALRETRACT
  3766. else target[E_AXIS] -= FILAMENTCHANGE_FINALRETRACT;
  3767. #endif
  3768. current_position[E_AXIS] = target[E_AXIS]; //the long retract of L is compensated by manual filament feeding
  3769. plan_set_e_position(current_position[E_AXIS]);
  3770. RUNPLAN; //should do nothing
  3771. lcd_reset_alert_level();
  3772. #ifdef DELTA
  3773. calculate_delta(lastpos);
  3774. plan_buffer_line(delta[X_AXIS], delta[Y_AXIS], delta[Z_AXIS], target[E_AXIS], fr60, active_extruder); //move xyz back
  3775. plan_buffer_line(delta[X_AXIS], delta[Y_AXIS], delta[Z_AXIS], lastpos[E_AXIS], fr60, active_extruder); //final untretract
  3776. #else
  3777. plan_buffer_line(lastpos[X_AXIS], lastpos[Y_AXIS], target[Z_AXIS], target[E_AXIS], fr60, active_extruder); //move xy back
  3778. plan_buffer_line(lastpos[X_AXIS], lastpos[Y_AXIS], lastpos[Z_AXIS], target[E_AXIS], fr60, active_extruder); //move z back
  3779. plan_buffer_line(lastpos[X_AXIS], lastpos[Y_AXIS], lastpos[Z_AXIS], lastpos[E_AXIS], fr60, active_extruder); //final untretract
  3780. #endif
  3781. #ifdef FILAMENT_RUNOUT_SENSOR
  3782. filrunoutEnqued = false;
  3783. #endif
  3784. }
  3785. #endif // FILAMENTCHANGEENABLE
  3786. #ifdef DUAL_X_CARRIAGE
  3787. /**
  3788. * M605: Set dual x-carriage movement mode
  3789. *
  3790. * M605 S0: Full control mode. The slicer has full control over x-carriage movement
  3791. * M605 S1: Auto-park mode. The inactive head will auto park/unpark without slicer involvement
  3792. * M605 S2 [Xnnn] [Rmmm]: Duplication mode. The second extruder will duplicate the first with nnn
  3793. * millimeters x-offset and an optional differential hotend temperature of
  3794. * mmm degrees. E.g., with "M605 S2 X100 R2" the second extruder will duplicate
  3795. * the first with a spacing of 100mm in the x direction and 2 degrees hotter.
  3796. *
  3797. * Note: the X axis should be homed after changing dual x-carriage mode.
  3798. */
  3799. inline void gcode_M605() {
  3800. st_synchronize();
  3801. if (code_seen('S')) dual_x_carriage_mode = code_value();
  3802. switch(dual_x_carriage_mode) {
  3803. case DXC_DUPLICATION_MODE:
  3804. if (code_seen('X')) duplicate_extruder_x_offset = max(code_value(), X2_MIN_POS - x_home_pos(0));
  3805. if (code_seen('R')) duplicate_extruder_temp_offset = code_value();
  3806. SERIAL_ECHO_START;
  3807. SERIAL_ECHOPGM(MSG_HOTEND_OFFSET);
  3808. SERIAL_CHAR(' ');
  3809. SERIAL_ECHO(extruder_offset[X_AXIS][0]);
  3810. SERIAL_CHAR(',');
  3811. SERIAL_ECHO(extruder_offset[Y_AXIS][0]);
  3812. SERIAL_CHAR(' ');
  3813. SERIAL_ECHO(duplicate_extruder_x_offset);
  3814. SERIAL_CHAR(',');
  3815. SERIAL_ECHOLN(extruder_offset[Y_AXIS][1]);
  3816. break;
  3817. case DXC_FULL_CONTROL_MODE:
  3818. case DXC_AUTO_PARK_MODE:
  3819. break;
  3820. default:
  3821. dual_x_carriage_mode = DEFAULT_DUAL_X_CARRIAGE_MODE;
  3822. break;
  3823. }
  3824. active_extruder_parked = false;
  3825. extruder_duplication_enabled = false;
  3826. delayed_move_time = 0;
  3827. }
  3828. #endif // DUAL_X_CARRIAGE
  3829. /**
  3830. * M907: Set digital trimpot motor current using axis codes X, Y, Z, E, B, S
  3831. */
  3832. inline void gcode_M907() {
  3833. #if HAS_DIGIPOTSS
  3834. for (int i=0;i<NUM_AXIS;i++)
  3835. if (code_seen(axis_codes[i])) digipot_current(i, code_value());
  3836. if (code_seen('B')) digipot_current(4, code_value());
  3837. if (code_seen('S')) for (int i=0; i<=4; i++) digipot_current(i, code_value());
  3838. #endif
  3839. #ifdef MOTOR_CURRENT_PWM_XY_PIN
  3840. if (code_seen('X')) digipot_current(0, code_value());
  3841. #endif
  3842. #ifdef MOTOR_CURRENT_PWM_Z_PIN
  3843. if (code_seen('Z')) digipot_current(1, code_value());
  3844. #endif
  3845. #ifdef MOTOR_CURRENT_PWM_E_PIN
  3846. if (code_seen('E')) digipot_current(2, code_value());
  3847. #endif
  3848. #ifdef DIGIPOT_I2C
  3849. // this one uses actual amps in floating point
  3850. for (int i=0;i<NUM_AXIS;i++) if(code_seen(axis_codes[i])) digipot_i2c_set_current(i, code_value());
  3851. // for each additional extruder (named B,C,D,E..., channels 4,5,6,7...)
  3852. 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());
  3853. #endif
  3854. }
  3855. #if HAS_DIGIPOTSS
  3856. /**
  3857. * M908: Control digital trimpot directly (M908 P<pin> S<current>)
  3858. */
  3859. inline void gcode_M908() {
  3860. digitalPotWrite(
  3861. code_seen('P') ? code_value() : 0,
  3862. code_seen('S') ? code_value() : 0
  3863. );
  3864. }
  3865. #endif // HAS_DIGIPOTSS
  3866. #if HAS_MICROSTEPS
  3867. // M350 Set microstepping mode. Warning: Steps per unit remains unchanged. S code sets stepping mode for all drivers.
  3868. inline void gcode_M350() {
  3869. if(code_seen('S')) for(int i=0;i<=4;i++) microstep_mode(i,code_value());
  3870. for(int i=0;i<NUM_AXIS;i++) if(code_seen(axis_codes[i])) microstep_mode(i,(uint8_t)code_value());
  3871. if(code_seen('B')) microstep_mode(4,code_value());
  3872. microstep_readings();
  3873. }
  3874. /**
  3875. * M351: Toggle MS1 MS2 pins directly with axis codes X Y Z E B
  3876. * S# determines MS1 or MS2, X# sets the pin high/low.
  3877. */
  3878. inline void gcode_M351() {
  3879. if (code_seen('S')) switch(code_value_short()) {
  3880. case 1:
  3881. for(int i=0;i<NUM_AXIS;i++) if (code_seen(axis_codes[i])) microstep_ms(i, code_value(), -1);
  3882. if (code_seen('B')) microstep_ms(4, code_value(), -1);
  3883. break;
  3884. case 2:
  3885. for(int i=0;i<NUM_AXIS;i++) if (code_seen(axis_codes[i])) microstep_ms(i, -1, code_value());
  3886. if (code_seen('B')) microstep_ms(4, -1, code_value());
  3887. break;
  3888. }
  3889. microstep_readings();
  3890. }
  3891. #endif // HAS_MICROSTEPS
  3892. /**
  3893. * M999: Restart after being stopped
  3894. */
  3895. inline void gcode_M999() {
  3896. Stopped = false;
  3897. lcd_reset_alert_level();
  3898. gcode_LastN = Stopped_gcode_LastN;
  3899. FlushSerialRequestResend();
  3900. }
  3901. inline void gcode_T() {
  3902. int tmp_extruder = code_value();
  3903. if (tmp_extruder >= EXTRUDERS) {
  3904. SERIAL_ECHO_START;
  3905. SERIAL_CHAR('T');
  3906. SERIAL_ECHO(tmp_extruder);
  3907. SERIAL_ECHOLN(MSG_INVALID_EXTRUDER);
  3908. }
  3909. else {
  3910. target_extruder = tmp_extruder;
  3911. #if EXTRUDERS > 1
  3912. bool make_move = false;
  3913. #endif
  3914. if (code_seen('F')) {
  3915. #if EXTRUDERS > 1
  3916. make_move = true;
  3917. #endif
  3918. next_feedrate = code_value();
  3919. if (next_feedrate > 0.0) feedrate = next_feedrate;
  3920. }
  3921. #if EXTRUDERS > 1
  3922. if (tmp_extruder != active_extruder) {
  3923. // Save current position to return to after applying extruder offset
  3924. set_destination_to_current();
  3925. #ifdef DUAL_X_CARRIAGE
  3926. if (dual_x_carriage_mode == DXC_AUTO_PARK_MODE && Stopped == false &&
  3927. (delayed_move_time != 0 || current_position[X_AXIS] != x_home_pos(active_extruder))) {
  3928. // Park old head: 1) raise 2) move to park position 3) lower
  3929. plan_buffer_line(current_position[X_AXIS], current_position[Y_AXIS], current_position[Z_AXIS] + TOOLCHANGE_PARK_ZLIFT,
  3930. current_position[E_AXIS], max_feedrate[Z_AXIS], active_extruder);
  3931. plan_buffer_line(x_home_pos(active_extruder), current_position[Y_AXIS], current_position[Z_AXIS] + TOOLCHANGE_PARK_ZLIFT,
  3932. current_position[E_AXIS], max_feedrate[X_AXIS], active_extruder);
  3933. plan_buffer_line(x_home_pos(active_extruder), current_position[Y_AXIS], current_position[Z_AXIS],
  3934. current_position[E_AXIS], max_feedrate[Z_AXIS], active_extruder);
  3935. st_synchronize();
  3936. }
  3937. // apply Y & Z extruder offset (x offset is already used in determining home pos)
  3938. current_position[Y_AXIS] = current_position[Y_AXIS] -
  3939. extruder_offset[Y_AXIS][active_extruder] +
  3940. extruder_offset[Y_AXIS][tmp_extruder];
  3941. current_position[Z_AXIS] = current_position[Z_AXIS] -
  3942. extruder_offset[Z_AXIS][active_extruder] +
  3943. extruder_offset[Z_AXIS][tmp_extruder];
  3944. active_extruder = tmp_extruder;
  3945. // This function resets the max/min values - the current position may be overwritten below.
  3946. axis_is_at_home(X_AXIS);
  3947. if (dual_x_carriage_mode == DXC_FULL_CONTROL_MODE) {
  3948. current_position[X_AXIS] = inactive_extruder_x_pos;
  3949. inactive_extruder_x_pos = destination[X_AXIS];
  3950. }
  3951. else if (dual_x_carriage_mode == DXC_DUPLICATION_MODE) {
  3952. active_extruder_parked = (active_extruder == 0); // this triggers the second extruder to move into the duplication position
  3953. if (active_extruder == 0 || active_extruder_parked)
  3954. current_position[X_AXIS] = inactive_extruder_x_pos;
  3955. else
  3956. current_position[X_AXIS] = destination[X_AXIS] + duplicate_extruder_x_offset;
  3957. inactive_extruder_x_pos = destination[X_AXIS];
  3958. extruder_duplication_enabled = false;
  3959. }
  3960. else {
  3961. // record raised toolhead position for use by unpark
  3962. memcpy(raised_parked_position, current_position, sizeof(raised_parked_position));
  3963. raised_parked_position[Z_AXIS] += TOOLCHANGE_UNPARK_ZLIFT;
  3964. active_extruder_parked = true;
  3965. delayed_move_time = 0;
  3966. }
  3967. #else // !DUAL_X_CARRIAGE
  3968. // Offset extruder (only by XY)
  3969. for (int i=X_AXIS; i<=Y_AXIS; i++)
  3970. current_position[i] += extruder_offset[i][tmp_extruder] - extruder_offset[i][active_extruder];
  3971. // Set the new active extruder and position
  3972. active_extruder = tmp_extruder;
  3973. #endif // !DUAL_X_CARRIAGE
  3974. #ifdef DELTA
  3975. sync_plan_position_delta();
  3976. #else
  3977. sync_plan_position();
  3978. #endif
  3979. // Move to the old position if 'F' was in the parameters
  3980. if (make_move && !Stopped) prepare_move();
  3981. }
  3982. #ifdef EXT_SOLENOID
  3983. st_synchronize();
  3984. disable_all_solenoids();
  3985. enable_solenoid_on_active_extruder();
  3986. #endif // EXT_SOLENOID
  3987. #endif // EXTRUDERS > 1
  3988. SERIAL_ECHO_START;
  3989. SERIAL_ECHO(MSG_ACTIVE_EXTRUDER);
  3990. SERIAL_PROTOCOLLN((int)active_extruder);
  3991. }
  3992. }
  3993. /**
  3994. * Process Commands and dispatch them to handlers
  3995. * This is called from the main loop()
  3996. */
  3997. void process_commands() {
  3998. if (code_seen('G')) {
  3999. int gCode = code_value_short();
  4000. switch(gCode) {
  4001. // G0, G1
  4002. case 0:
  4003. case 1:
  4004. gcode_G0_G1();
  4005. break;
  4006. // G2, G3
  4007. #ifndef SCARA
  4008. case 2: // G2 - CW ARC
  4009. case 3: // G3 - CCW ARC
  4010. gcode_G2_G3(gCode == 2);
  4011. break;
  4012. #endif
  4013. // G4 Dwell
  4014. case 4:
  4015. gcode_G4();
  4016. break;
  4017. #ifdef FWRETRACT
  4018. case 10: // G10: retract
  4019. case 11: // G11: retract_recover
  4020. gcode_G10_G11(gCode == 10);
  4021. break;
  4022. #endif //FWRETRACT
  4023. case 28: // G28: Home all axes, one at a time
  4024. gcode_G28();
  4025. break;
  4026. #if defined(ENABLE_AUTO_BED_LEVELING) || defined(MESH_BED_LEVELING)
  4027. case 29: // G29 Detailed Z-Probe, probes the bed at 3 or more points.
  4028. gcode_G29();
  4029. break;
  4030. #endif
  4031. #ifdef ENABLE_AUTO_BED_LEVELING
  4032. #ifndef Z_PROBE_SLED
  4033. case 30: // G30 Single Z Probe
  4034. gcode_G30();
  4035. break;
  4036. #else // Z_PROBE_SLED
  4037. case 31: // G31: dock the sled
  4038. case 32: // G32: undock the sled
  4039. dock_sled(gCode == 31);
  4040. break;
  4041. #endif // Z_PROBE_SLED
  4042. #endif // ENABLE_AUTO_BED_LEVELING
  4043. case 90: // G90
  4044. relative_mode = false;
  4045. break;
  4046. case 91: // G91
  4047. relative_mode = true;
  4048. break;
  4049. case 92: // G92
  4050. gcode_G92();
  4051. break;
  4052. }
  4053. }
  4054. else if (code_seen('M')) {
  4055. switch(code_value_short()) {
  4056. #ifdef ULTIPANEL
  4057. case 0: // M0 - Unconditional stop - Wait for user button press on LCD
  4058. case 1: // M1 - Conditional stop - Wait for user button press on LCD
  4059. gcode_M0_M1();
  4060. break;
  4061. #endif // ULTIPANEL
  4062. case 17:
  4063. gcode_M17();
  4064. break;
  4065. #ifdef SDSUPPORT
  4066. case 20: // M20 - list SD card
  4067. gcode_M20(); break;
  4068. case 21: // M21 - init SD card
  4069. gcode_M21(); break;
  4070. case 22: //M22 - release SD card
  4071. gcode_M22(); break;
  4072. case 23: //M23 - Select file
  4073. gcode_M23(); break;
  4074. case 24: //M24 - Start SD print
  4075. gcode_M24(); break;
  4076. case 25: //M25 - Pause SD print
  4077. gcode_M25(); break;
  4078. case 26: //M26 - Set SD index
  4079. gcode_M26(); break;
  4080. case 27: //M27 - Get SD status
  4081. gcode_M27(); break;
  4082. case 28: //M28 - Start SD write
  4083. gcode_M28(); break;
  4084. case 29: //M29 - Stop SD write
  4085. gcode_M29(); break;
  4086. case 30: //M30 <filename> Delete File
  4087. gcode_M30(); break;
  4088. case 32: //M32 - Select file and start SD print
  4089. gcode_M32(); break;
  4090. case 928: //M928 - Start SD write
  4091. gcode_M928(); break;
  4092. #endif //SDSUPPORT
  4093. case 31: //M31 take time since the start of the SD print or an M109 command
  4094. gcode_M31();
  4095. break;
  4096. case 42: //M42 -Change pin status via gcode
  4097. gcode_M42();
  4098. break;
  4099. #if defined(ENABLE_AUTO_BED_LEVELING) && defined(Z_PROBE_REPEATABILITY_TEST)
  4100. case 48: // M48 Z-Probe repeatability
  4101. gcode_M48();
  4102. break;
  4103. #endif // ENABLE_AUTO_BED_LEVELING && Z_PROBE_REPEATABILITY_TEST
  4104. case 104: // M104
  4105. gcode_M104();
  4106. break;
  4107. case 112: // M112 Emergency Stop
  4108. gcode_M112();
  4109. break;
  4110. case 140: // M140 Set bed temp
  4111. gcode_M140();
  4112. break;
  4113. case 105: // M105 Read current temperature
  4114. gcode_M105();
  4115. return;
  4116. break;
  4117. case 109: // M109 Wait for temperature
  4118. gcode_M109();
  4119. break;
  4120. #if HAS_TEMP_BED
  4121. case 190: // M190 - Wait for bed heater to reach target.
  4122. gcode_M190();
  4123. break;
  4124. #endif // HAS_TEMP_BED
  4125. #if HAS_FAN
  4126. case 106: //M106 Fan On
  4127. gcode_M106();
  4128. break;
  4129. case 107: //M107 Fan Off
  4130. gcode_M107();
  4131. break;
  4132. #endif // HAS_FAN
  4133. #ifdef BARICUDA
  4134. // PWM for HEATER_1_PIN
  4135. #if HAS_HEATER_1
  4136. case 126: // M126 valve open
  4137. gcode_M126();
  4138. break;
  4139. case 127: // M127 valve closed
  4140. gcode_M127();
  4141. break;
  4142. #endif // HAS_HEATER_1
  4143. // PWM for HEATER_2_PIN
  4144. #if HAS_HEATER_2
  4145. case 128: // M128 valve open
  4146. gcode_M128();
  4147. break;
  4148. case 129: // M129 valve closed
  4149. gcode_M129();
  4150. break;
  4151. #endif // HAS_HEATER_2
  4152. #endif // BARICUDA
  4153. #if HAS_POWER_SWITCH
  4154. case 80: // M80 - Turn on Power Supply
  4155. gcode_M80();
  4156. break;
  4157. #endif // HAS_POWER_SWITCH
  4158. case 81: // M81 - Turn off Power, including Power Supply, if possible
  4159. gcode_M81();
  4160. break;
  4161. case 82:
  4162. gcode_M82();
  4163. break;
  4164. case 83:
  4165. gcode_M83();
  4166. break;
  4167. case 18: //compatibility
  4168. case 84: // M84
  4169. gcode_M18_M84();
  4170. break;
  4171. case 85: // M85
  4172. gcode_M85();
  4173. break;
  4174. case 92: // M92
  4175. gcode_M92();
  4176. break;
  4177. case 115: // M115
  4178. gcode_M115();
  4179. break;
  4180. case 117: // M117 display message
  4181. gcode_M117();
  4182. break;
  4183. case 114: // M114
  4184. gcode_M114();
  4185. break;
  4186. case 120: // M120
  4187. gcode_M120();
  4188. break;
  4189. case 121: // M121
  4190. gcode_M121();
  4191. break;
  4192. case 119: // M119
  4193. gcode_M119();
  4194. break;
  4195. //TODO: update for all axis, use for loop
  4196. #ifdef BLINKM
  4197. case 150: // M150
  4198. gcode_M150();
  4199. break;
  4200. #endif //BLINKM
  4201. case 200: // M200 D<millimeters> set filament diameter and set E axis units to cubic millimeters (use S0 to set back to millimeters).
  4202. gcode_M200();
  4203. break;
  4204. case 201: // M201
  4205. gcode_M201();
  4206. break;
  4207. #if 0 // Not used for Sprinter/grbl gen6
  4208. case 202: // M202
  4209. gcode_M202();
  4210. break;
  4211. #endif
  4212. case 203: // M203 max feedrate mm/sec
  4213. gcode_M203();
  4214. break;
  4215. case 204: // M204 acclereration S normal moves T filmanent only moves
  4216. gcode_M204();
  4217. break;
  4218. 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
  4219. gcode_M205();
  4220. break;
  4221. case 206: // M206 additional homing offset
  4222. gcode_M206();
  4223. break;
  4224. #ifdef DELTA
  4225. case 665: // M665 set delta configurations L<diagonal_rod> R<delta_radius> S<segments_per_sec>
  4226. gcode_M665();
  4227. break;
  4228. #endif
  4229. #if defined(DELTA) || defined(Z_DUAL_ENDSTOPS)
  4230. case 666: // M666 set delta / dual endstop adjustment
  4231. gcode_M666();
  4232. break;
  4233. #endif
  4234. #ifdef FWRETRACT
  4235. case 207: //M207 - set retract length S[positive mm] F[feedrate mm/min] Z[additional zlift/hop]
  4236. gcode_M207();
  4237. break;
  4238. case 208: // M208 - set retract recover length S[positive mm surplus to the M207 S*] F[feedrate mm/min]
  4239. gcode_M208();
  4240. break;
  4241. 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.
  4242. gcode_M209();
  4243. break;
  4244. #endif // FWRETRACT
  4245. #if EXTRUDERS > 1
  4246. case 218: // M218 - set hotend offset (in mm), T<extruder_number> X<offset_on_X> Y<offset_on_Y>
  4247. gcode_M218();
  4248. break;
  4249. #endif
  4250. case 220: // M220 S<factor in percent>- set speed factor override percentage
  4251. gcode_M220();
  4252. break;
  4253. case 221: // M221 S<factor in percent>- set extrude factor override percentage
  4254. gcode_M221();
  4255. break;
  4256. case 226: // M226 P<pin number> S<pin state>- Wait until the specified pin reaches the state required
  4257. gcode_M226();
  4258. break;
  4259. #if NUM_SERVOS > 0
  4260. case 280: // M280 - set servo position absolute. P: servo index, S: angle or microseconds
  4261. gcode_M280();
  4262. break;
  4263. #endif // NUM_SERVOS > 0
  4264. #if defined(LARGE_FLASH) && (BEEPER > 0 || defined(ULTRALCD) || defined(LCD_USE_I2C_BUZZER))
  4265. case 300: // M300 - Play beep tone
  4266. gcode_M300();
  4267. break;
  4268. #endif // LARGE_FLASH && (BEEPER>0 || ULTRALCD || LCD_USE_I2C_BUZZER)
  4269. #ifdef PIDTEMP
  4270. case 301: // M301
  4271. gcode_M301();
  4272. break;
  4273. #endif // PIDTEMP
  4274. #ifdef PIDTEMPBED
  4275. case 304: // M304
  4276. gcode_M304();
  4277. break;
  4278. #endif // PIDTEMPBED
  4279. #if defined(CHDK) || HAS_PHOTOGRAPH
  4280. case 240: // M240 Triggers a camera by emulating a Canon RC-1 : http://www.doc-diy.net/photo/rc-1_hacked/
  4281. gcode_M240();
  4282. break;
  4283. #endif // CHDK || PHOTOGRAPH_PIN
  4284. #ifdef DOGLCD
  4285. case 250: // M250 Set LCD contrast value: C<value> (value 0..63)
  4286. gcode_M250();
  4287. break;
  4288. #endif // DOGLCD
  4289. #ifdef PREVENT_DANGEROUS_EXTRUDE
  4290. case 302: // allow cold extrudes, or set the minimum extrude temperature
  4291. gcode_M302();
  4292. break;
  4293. #endif // PREVENT_DANGEROUS_EXTRUDE
  4294. case 303: // M303 PID autotune
  4295. gcode_M303();
  4296. break;
  4297. #ifdef SCARA
  4298. case 360: // M360 SCARA Theta pos1
  4299. if (gcode_M360()) return;
  4300. break;
  4301. case 361: // M361 SCARA Theta pos2
  4302. if (gcode_M361()) return;
  4303. break;
  4304. case 362: // M362 SCARA Psi pos1
  4305. if (gcode_M362()) return;
  4306. break;
  4307. case 363: // M363 SCARA Psi pos2
  4308. if (gcode_M363()) return;
  4309. break;
  4310. case 364: // M364 SCARA Psi pos3 (90 deg to Theta)
  4311. if (gcode_M364()) return;
  4312. break;
  4313. case 365: // M365 Set SCARA scaling for X Y Z
  4314. gcode_M365();
  4315. break;
  4316. #endif // SCARA
  4317. case 400: // M400 finish all moves
  4318. gcode_M400();
  4319. break;
  4320. #if defined(ENABLE_AUTO_BED_LEVELING) && (defined(SERVO_ENDSTOPS) || defined(Z_PROBE_ALLEN_KEY)) && not defined(Z_PROBE_SLED)
  4321. case 401:
  4322. gcode_M401();
  4323. break;
  4324. case 402:
  4325. gcode_M402();
  4326. break;
  4327. #endif
  4328. #ifdef FILAMENT_SENSOR
  4329. case 404: //M404 Enter the nominal filament width (3mm, 1.75mm ) N<3.0> or display nominal filament width
  4330. gcode_M404();
  4331. break;
  4332. case 405: //M405 Turn on filament sensor for control
  4333. gcode_M405();
  4334. break;
  4335. case 406: //M406 Turn off filament sensor for control
  4336. gcode_M406();
  4337. break;
  4338. case 407: //M407 Display measured filament diameter
  4339. gcode_M407();
  4340. break;
  4341. #endif // FILAMENT_SENSOR
  4342. case 500: // M500 Store settings in EEPROM
  4343. gcode_M500();
  4344. break;
  4345. case 501: // M501 Read settings from EEPROM
  4346. gcode_M501();
  4347. break;
  4348. case 502: // M502 Revert to default settings
  4349. gcode_M502();
  4350. break;
  4351. case 503: // M503 print settings currently in memory
  4352. gcode_M503();
  4353. break;
  4354. #ifdef ABORT_ON_ENDSTOP_HIT_FEATURE_ENABLED
  4355. case 540:
  4356. gcode_M540();
  4357. break;
  4358. #endif
  4359. #ifdef CUSTOM_M_CODE_SET_Z_PROBE_OFFSET
  4360. case CUSTOM_M_CODE_SET_Z_PROBE_OFFSET:
  4361. gcode_SET_Z_PROBE_OFFSET();
  4362. break;
  4363. #endif // CUSTOM_M_CODE_SET_Z_PROBE_OFFSET
  4364. #ifdef FILAMENTCHANGEENABLE
  4365. case 600: //Pause for filament change X[pos] Y[pos] Z[relative lift] E[initial retract] L[later retract distance for removal]
  4366. gcode_M600();
  4367. break;
  4368. #endif // FILAMENTCHANGEENABLE
  4369. #ifdef DUAL_X_CARRIAGE
  4370. case 605:
  4371. gcode_M605();
  4372. break;
  4373. #endif // DUAL_X_CARRIAGE
  4374. case 907: // M907 Set digital trimpot motor current using axis codes.
  4375. gcode_M907();
  4376. break;
  4377. #if HAS_DIGIPOTSS
  4378. case 908: // M908 Control digital trimpot directly.
  4379. gcode_M908();
  4380. break;
  4381. #endif // HAS_DIGIPOTSS
  4382. #if HAS_MICROSTEPS
  4383. case 350: // M350 Set microstepping mode. Warning: Steps per unit remains unchanged. S code sets stepping mode for all drivers.
  4384. gcode_M350();
  4385. break;
  4386. case 351: // M351 Toggle MS1 MS2 pins directly, S# determines MS1 or MS2, X# sets the pin high/low.
  4387. gcode_M351();
  4388. break;
  4389. #endif // HAS_MICROSTEPS
  4390. case 999: // M999: Restart after being Stopped
  4391. gcode_M999();
  4392. break;
  4393. }
  4394. }
  4395. else if (code_seen('T')) {
  4396. gcode_T();
  4397. }
  4398. else {
  4399. SERIAL_ECHO_START;
  4400. SERIAL_ECHOPGM(MSG_UNKNOWN_COMMAND);
  4401. SERIAL_ECHO(cmdbuffer[bufindr]);
  4402. SERIAL_ECHOLNPGM("\"");
  4403. }
  4404. ClearToSend();
  4405. }
  4406. void FlushSerialRequestResend() {
  4407. //char cmdbuffer[bufindr][100]="Resend:";
  4408. MYSERIAL.flush();
  4409. SERIAL_PROTOCOLPGM(MSG_RESEND);
  4410. SERIAL_PROTOCOLLN(gcode_LastN + 1);
  4411. ClearToSend();
  4412. }
  4413. void ClearToSend() {
  4414. refresh_cmd_timeout();
  4415. #ifdef SDSUPPORT
  4416. if (fromsd[bufindr]) return;
  4417. #endif
  4418. SERIAL_PROTOCOLLNPGM(MSG_OK);
  4419. }
  4420. void get_coordinates() {
  4421. for (int i = 0; i < NUM_AXIS; i++) {
  4422. if (code_seen(axis_codes[i]))
  4423. destination[i] = code_value() + (axis_relative_modes[i] || relative_mode ? current_position[i] : 0);
  4424. else
  4425. destination[i] = current_position[i];
  4426. }
  4427. if (code_seen('F')) {
  4428. next_feedrate = code_value();
  4429. if (next_feedrate > 0.0) feedrate = next_feedrate;
  4430. }
  4431. }
  4432. void get_arc_coordinates() {
  4433. #ifdef SF_ARC_FIX
  4434. bool relative_mode_backup = relative_mode;
  4435. relative_mode = true;
  4436. #endif
  4437. get_coordinates();
  4438. #ifdef SF_ARC_FIX
  4439. relative_mode = relative_mode_backup;
  4440. #endif
  4441. offset[0] = code_seen('I') ? code_value() : 0;
  4442. offset[1] = code_seen('J') ? code_value() : 0;
  4443. }
  4444. void clamp_to_software_endstops(float target[3])
  4445. {
  4446. if (min_software_endstops) {
  4447. if (target[X_AXIS] < min_pos[X_AXIS]) target[X_AXIS] = min_pos[X_AXIS];
  4448. if (target[Y_AXIS] < min_pos[Y_AXIS]) target[Y_AXIS] = min_pos[Y_AXIS];
  4449. float negative_z_offset = 0;
  4450. #ifdef ENABLE_AUTO_BED_LEVELING
  4451. if (Z_PROBE_OFFSET_FROM_EXTRUDER < 0) negative_z_offset = negative_z_offset + Z_PROBE_OFFSET_FROM_EXTRUDER;
  4452. if (home_offset[Z_AXIS] < 0) negative_z_offset = negative_z_offset + home_offset[Z_AXIS];
  4453. #endif
  4454. if (target[Z_AXIS] < min_pos[Z_AXIS]+negative_z_offset) target[Z_AXIS] = min_pos[Z_AXIS]+negative_z_offset;
  4455. }
  4456. if (max_software_endstops) {
  4457. if (target[X_AXIS] > max_pos[X_AXIS]) target[X_AXIS] = max_pos[X_AXIS];
  4458. if (target[Y_AXIS] > max_pos[Y_AXIS]) target[Y_AXIS] = max_pos[Y_AXIS];
  4459. if (target[Z_AXIS] > max_pos[Z_AXIS]) target[Z_AXIS] = max_pos[Z_AXIS];
  4460. }
  4461. }
  4462. #ifdef DELTA
  4463. void recalc_delta_settings(float radius, float diagonal_rod) {
  4464. delta_tower1_x = -SIN_60 * radius; // front left tower
  4465. delta_tower1_y = -COS_60 * radius;
  4466. delta_tower2_x = SIN_60 * radius; // front right tower
  4467. delta_tower2_y = -COS_60 * radius;
  4468. delta_tower3_x = 0.0; // back middle tower
  4469. delta_tower3_y = radius;
  4470. delta_diagonal_rod_2 = sq(diagonal_rod);
  4471. }
  4472. void calculate_delta(float cartesian[3]) {
  4473. delta[X_AXIS] = sqrt(delta_diagonal_rod_2
  4474. - sq(delta_tower1_x-cartesian[X_AXIS])
  4475. - sq(delta_tower1_y-cartesian[Y_AXIS])
  4476. ) + cartesian[Z_AXIS];
  4477. delta[Y_AXIS] = sqrt(delta_diagonal_rod_2
  4478. - sq(delta_tower2_x-cartesian[X_AXIS])
  4479. - sq(delta_tower2_y-cartesian[Y_AXIS])
  4480. ) + cartesian[Z_AXIS];
  4481. delta[Z_AXIS] = sqrt(delta_diagonal_rod_2
  4482. - sq(delta_tower3_x-cartesian[X_AXIS])
  4483. - sq(delta_tower3_y-cartesian[Y_AXIS])
  4484. ) + cartesian[Z_AXIS];
  4485. /*
  4486. SERIAL_ECHOPGM("cartesian x="); SERIAL_ECHO(cartesian[X_AXIS]);
  4487. SERIAL_ECHOPGM(" y="); SERIAL_ECHO(cartesian[Y_AXIS]);
  4488. SERIAL_ECHOPGM(" z="); SERIAL_ECHOLN(cartesian[Z_AXIS]);
  4489. SERIAL_ECHOPGM("delta x="); SERIAL_ECHO(delta[X_AXIS]);
  4490. SERIAL_ECHOPGM(" y="); SERIAL_ECHO(delta[Y_AXIS]);
  4491. SERIAL_ECHOPGM(" z="); SERIAL_ECHOLN(delta[Z_AXIS]);
  4492. */
  4493. }
  4494. #ifdef ENABLE_AUTO_BED_LEVELING
  4495. // Adjust print surface height by linear interpolation over the bed_level array.
  4496. void adjust_delta(float cartesian[3]) {
  4497. if (delta_grid_spacing[0] == 0 || delta_grid_spacing[1] == 0) return; // G29 not done!
  4498. int half = (AUTO_BED_LEVELING_GRID_POINTS - 1) / 2;
  4499. float h1 = 0.001 - half, h2 = half - 0.001,
  4500. grid_x = max(h1, min(h2, cartesian[X_AXIS] / delta_grid_spacing[0])),
  4501. grid_y = max(h1, min(h2, cartesian[Y_AXIS] / delta_grid_spacing[1]));
  4502. int floor_x = floor(grid_x), floor_y = floor(grid_y);
  4503. float ratio_x = grid_x - floor_x, ratio_y = grid_y - floor_y,
  4504. z1 = bed_level[floor_x + half][floor_y + half],
  4505. z2 = bed_level[floor_x + half][floor_y + half + 1],
  4506. z3 = bed_level[floor_x + half + 1][floor_y + half],
  4507. z4 = bed_level[floor_x + half + 1][floor_y + half + 1],
  4508. left = (1 - ratio_y) * z1 + ratio_y * z2,
  4509. right = (1 - ratio_y) * z3 + ratio_y * z4,
  4510. offset = (1 - ratio_x) * left + ratio_x * right;
  4511. delta[X_AXIS] += offset;
  4512. delta[Y_AXIS] += offset;
  4513. delta[Z_AXIS] += offset;
  4514. /*
  4515. SERIAL_ECHOPGM("grid_x="); SERIAL_ECHO(grid_x);
  4516. SERIAL_ECHOPGM(" grid_y="); SERIAL_ECHO(grid_y);
  4517. SERIAL_ECHOPGM(" floor_x="); SERIAL_ECHO(floor_x);
  4518. SERIAL_ECHOPGM(" floor_y="); SERIAL_ECHO(floor_y);
  4519. SERIAL_ECHOPGM(" ratio_x="); SERIAL_ECHO(ratio_x);
  4520. SERIAL_ECHOPGM(" ratio_y="); SERIAL_ECHO(ratio_y);
  4521. SERIAL_ECHOPGM(" z1="); SERIAL_ECHO(z1);
  4522. SERIAL_ECHOPGM(" z2="); SERIAL_ECHO(z2);
  4523. SERIAL_ECHOPGM(" z3="); SERIAL_ECHO(z3);
  4524. SERIAL_ECHOPGM(" z4="); SERIAL_ECHO(z4);
  4525. SERIAL_ECHOPGM(" left="); SERIAL_ECHO(left);
  4526. SERIAL_ECHOPGM(" right="); SERIAL_ECHO(right);
  4527. SERIAL_ECHOPGM(" offset="); SERIAL_ECHOLN(offset);
  4528. */
  4529. }
  4530. #endif // ENABLE_AUTO_BED_LEVELING
  4531. #endif // DELTA
  4532. #ifdef MESH_BED_LEVELING
  4533. #if !defined(MIN)
  4534. #define MIN(_v1, _v2) (((_v1) < (_v2)) ? (_v1) : (_v2))
  4535. #endif // ! MIN
  4536. // This function is used to split lines on mesh borders so each segment is only part of one mesh area
  4537. void mesh_plan_buffer_line(float x, float y, float z, const float e, float feed_rate, const uint8_t &extruder, uint8_t x_splits=0xff, uint8_t y_splits=0xff)
  4538. {
  4539. if (!mbl.active) {
  4540. plan_buffer_line(x, y, z, e, feed_rate, extruder);
  4541. set_current_to_destination();
  4542. return;
  4543. }
  4544. int pix = mbl.select_x_index(current_position[X_AXIS]);
  4545. int piy = mbl.select_y_index(current_position[Y_AXIS]);
  4546. int ix = mbl.select_x_index(x);
  4547. int iy = mbl.select_y_index(y);
  4548. pix = MIN(pix, MESH_NUM_X_POINTS-2);
  4549. piy = MIN(piy, MESH_NUM_Y_POINTS-2);
  4550. ix = MIN(ix, MESH_NUM_X_POINTS-2);
  4551. iy = MIN(iy, MESH_NUM_Y_POINTS-2);
  4552. if (pix == ix && piy == iy) {
  4553. // Start and end on same mesh square
  4554. plan_buffer_line(x, y, z, e, feed_rate, extruder);
  4555. set_current_to_destination();
  4556. return;
  4557. }
  4558. float nx, ny, ne, normalized_dist;
  4559. if (ix > pix && (x_splits) & BIT(ix)) {
  4560. nx = mbl.get_x(ix);
  4561. normalized_dist = (nx - current_position[X_AXIS])/(x - current_position[X_AXIS]);
  4562. ny = current_position[Y_AXIS] + (y - current_position[Y_AXIS]) * normalized_dist;
  4563. ne = current_position[E_AXIS] + (e - current_position[E_AXIS]) * normalized_dist;
  4564. x_splits ^= BIT(ix);
  4565. } else if (ix < pix && (x_splits) & BIT(pix)) {
  4566. nx = mbl.get_x(pix);
  4567. normalized_dist = (nx - current_position[X_AXIS])/(x - current_position[X_AXIS]);
  4568. ny = current_position[Y_AXIS] + (y - current_position[Y_AXIS]) * normalized_dist;
  4569. ne = current_position[E_AXIS] + (e - current_position[E_AXIS]) * normalized_dist;
  4570. x_splits ^= BIT(pix);
  4571. } else if (iy > piy && (y_splits) & BIT(iy)) {
  4572. ny = mbl.get_y(iy);
  4573. normalized_dist = (ny - current_position[Y_AXIS])/(y - current_position[Y_AXIS]);
  4574. nx = current_position[X_AXIS] + (x - current_position[X_AXIS]) * normalized_dist;
  4575. ne = current_position[E_AXIS] + (e - current_position[E_AXIS]) * normalized_dist;
  4576. y_splits ^= BIT(iy);
  4577. } else if (iy < piy && (y_splits) & BIT(piy)) {
  4578. ny = mbl.get_y(piy);
  4579. normalized_dist = (ny - current_position[Y_AXIS])/(y - current_position[Y_AXIS]);
  4580. nx = current_position[X_AXIS] + (x - current_position[X_AXIS]) * normalized_dist;
  4581. ne = current_position[E_AXIS] + (e - current_position[E_AXIS]) * normalized_dist;
  4582. y_splits ^= BIT(piy);
  4583. } else {
  4584. // Already split on a border
  4585. plan_buffer_line(x, y, z, e, feed_rate, extruder);
  4586. set_current_to_destination();
  4587. return;
  4588. }
  4589. // Do the split and look for more borders
  4590. destination[X_AXIS] = nx;
  4591. destination[Y_AXIS] = ny;
  4592. destination[E_AXIS] = ne;
  4593. mesh_plan_buffer_line(nx, ny, z, ne, feed_rate, extruder, x_splits, y_splits);
  4594. destination[X_AXIS] = x;
  4595. destination[Y_AXIS] = y;
  4596. destination[E_AXIS] = e;
  4597. mesh_plan_buffer_line(x, y, z, e, feed_rate, extruder, x_splits, y_splits);
  4598. }
  4599. #endif // MESH_BED_LEVELING
  4600. void prepare_move() {
  4601. clamp_to_software_endstops(destination);
  4602. refresh_cmd_timeout();
  4603. #ifdef SCARA //for now same as delta-code
  4604. float difference[NUM_AXIS];
  4605. for (int8_t i = 0; i < NUM_AXIS; i++) difference[i] = destination[i] - current_position[i];
  4606. float cartesian_mm = sqrt( sq(difference[X_AXIS]) +
  4607. sq(difference[Y_AXIS]) +
  4608. sq(difference[Z_AXIS]));
  4609. if (cartesian_mm < 0.000001) { cartesian_mm = abs(difference[E_AXIS]); }
  4610. if (cartesian_mm < 0.000001) { return; }
  4611. float seconds = 6000 * cartesian_mm / feedrate / feedmultiply;
  4612. int steps = max(1, int(scara_segments_per_second * seconds));
  4613. //SERIAL_ECHOPGM("mm="); SERIAL_ECHO(cartesian_mm);
  4614. //SERIAL_ECHOPGM(" seconds="); SERIAL_ECHO(seconds);
  4615. //SERIAL_ECHOPGM(" steps="); SERIAL_ECHOLN(steps);
  4616. for (int s = 1; s <= steps; s++) {
  4617. float fraction = float(s) / float(steps);
  4618. for(int8_t i = 0; i < NUM_AXIS; i++) {
  4619. destination[i] = current_position[i] + difference[i] * fraction;
  4620. }
  4621. calculate_delta(destination);
  4622. //SERIAL_ECHOPGM("destination[X_AXIS]="); SERIAL_ECHOLN(destination[X_AXIS]);
  4623. //SERIAL_ECHOPGM("destination[Y_AXIS]="); SERIAL_ECHOLN(destination[Y_AXIS]);
  4624. //SERIAL_ECHOPGM("destination[Z_AXIS]="); SERIAL_ECHOLN(destination[Z_AXIS]);
  4625. //SERIAL_ECHOPGM("delta[X_AXIS]="); SERIAL_ECHOLN(delta[X_AXIS]);
  4626. //SERIAL_ECHOPGM("delta[Y_AXIS]="); SERIAL_ECHOLN(delta[Y_AXIS]);
  4627. //SERIAL_ECHOPGM("delta[Z_AXIS]="); SERIAL_ECHOLN(delta[Z_AXIS]);
  4628. plan_buffer_line(delta[X_AXIS], delta[Y_AXIS], delta[Z_AXIS],
  4629. destination[E_AXIS], feedrate*feedmultiply/60/100.0,
  4630. active_extruder);
  4631. }
  4632. #endif // SCARA
  4633. #ifdef DELTA
  4634. float difference[NUM_AXIS];
  4635. for (int8_t i=0; i < NUM_AXIS; i++) difference[i] = destination[i] - current_position[i];
  4636. float cartesian_mm = sqrt(sq(difference[X_AXIS]) +
  4637. sq(difference[Y_AXIS]) +
  4638. sq(difference[Z_AXIS]));
  4639. if (cartesian_mm < 0.000001) cartesian_mm = abs(difference[E_AXIS]);
  4640. if (cartesian_mm < 0.000001) return;
  4641. float seconds = 6000 * cartesian_mm / feedrate / feedmultiply;
  4642. int steps = max(1, int(delta_segments_per_second * seconds));
  4643. // SERIAL_ECHOPGM("mm="); SERIAL_ECHO(cartesian_mm);
  4644. // SERIAL_ECHOPGM(" seconds="); SERIAL_ECHO(seconds);
  4645. // SERIAL_ECHOPGM(" steps="); SERIAL_ECHOLN(steps);
  4646. for (int s = 1; s <= steps; s++) {
  4647. float fraction = float(s) / float(steps);
  4648. for (int8_t i = 0; i < NUM_AXIS; i++) destination[i] = current_position[i] + difference[i] * fraction;
  4649. calculate_delta(destination);
  4650. #ifdef ENABLE_AUTO_BED_LEVELING
  4651. adjust_delta(destination);
  4652. #endif
  4653. plan_buffer_line(delta[X_AXIS], delta[Y_AXIS], delta[Z_AXIS],
  4654. destination[E_AXIS], feedrate*feedmultiply/60/100.0,
  4655. active_extruder);
  4656. }
  4657. #endif // DELTA
  4658. #ifdef DUAL_X_CARRIAGE
  4659. if (active_extruder_parked) {
  4660. if (dual_x_carriage_mode == DXC_DUPLICATION_MODE && active_extruder == 0) {
  4661. // move duplicate extruder into correct duplication position.
  4662. plan_set_position(inactive_extruder_x_pos, current_position[Y_AXIS], current_position[Z_AXIS], current_position[E_AXIS]);
  4663. plan_buffer_line(current_position[X_AXIS] + duplicate_extruder_x_offset, current_position[Y_AXIS], current_position[Z_AXIS],
  4664. current_position[E_AXIS], max_feedrate[X_AXIS], 1);
  4665. sync_plan_position();
  4666. st_synchronize();
  4667. extruder_duplication_enabled = true;
  4668. active_extruder_parked = false;
  4669. }
  4670. else if (dual_x_carriage_mode == DXC_AUTO_PARK_MODE) { // handle unparking of head
  4671. if (current_position[E_AXIS] == destination[E_AXIS]) {
  4672. // this is a travel move - skit it but keep track of current position (so that it can later
  4673. // be used as start of first non-travel move)
  4674. if (delayed_move_time != 0xFFFFFFFFUL) {
  4675. set_current_to_destination();
  4676. if (destination[Z_AXIS] > raised_parked_position[Z_AXIS])
  4677. raised_parked_position[Z_AXIS] = destination[Z_AXIS];
  4678. delayed_move_time = millis();
  4679. return;
  4680. }
  4681. }
  4682. delayed_move_time = 0;
  4683. // unpark extruder: 1) raise, 2) move into starting XY position, 3) lower
  4684. plan_buffer_line(raised_parked_position[X_AXIS], raised_parked_position[Y_AXIS], raised_parked_position[Z_AXIS], current_position[E_AXIS], max_feedrate[Z_AXIS], active_extruder);
  4685. plan_buffer_line(current_position[X_AXIS], current_position[Y_AXIS], raised_parked_position[Z_AXIS],
  4686. current_position[E_AXIS], min(max_feedrate[X_AXIS],max_feedrate[Y_AXIS]), active_extruder);
  4687. plan_buffer_line(current_position[X_AXIS], current_position[Y_AXIS], current_position[Z_AXIS],
  4688. current_position[E_AXIS], max_feedrate[Z_AXIS], active_extruder);
  4689. active_extruder_parked = false;
  4690. }
  4691. }
  4692. #endif // DUAL_X_CARRIAGE
  4693. #if !defined(DELTA) && !defined(SCARA)
  4694. // Do not use feedmultiply for E or Z only moves
  4695. if ( (current_position[X_AXIS] == destination [X_AXIS]) && (current_position[Y_AXIS] == destination [Y_AXIS])) {
  4696. line_to_destination();
  4697. }
  4698. else {
  4699. #ifdef MESH_BED_LEVELING
  4700. mesh_plan_buffer_line(destination[X_AXIS], destination[Y_AXIS], destination[Z_AXIS], destination[E_AXIS], (feedrate/60)*(feedmultiply/100.0), active_extruder);
  4701. return;
  4702. #else
  4703. plan_buffer_line(destination[X_AXIS], destination[Y_AXIS], destination[Z_AXIS], destination[E_AXIS], (feedrate/60)*(feedmultiply/100.0), active_extruder);
  4704. #endif // MESH_BED_LEVELING
  4705. }
  4706. #endif // !(DELTA || SCARA)
  4707. set_current_to_destination();
  4708. }
  4709. void prepare_arc_move(char isclockwise) {
  4710. float r = hypot(offset[X_AXIS], offset[Y_AXIS]); // Compute arc radius for mc_arc
  4711. // Trace the arc
  4712. mc_arc(current_position, destination, offset, X_AXIS, Y_AXIS, Z_AXIS, feedrate*feedmultiply/60/100.0, r, isclockwise, active_extruder);
  4713. // As far as the parser is concerned, the position is now == target. In reality the
  4714. // motion control system might still be processing the action and the real tool position
  4715. // in any intermediate location.
  4716. set_current_to_destination();
  4717. refresh_cmd_timeout();
  4718. }
  4719. #if HAS_CONTROLLERFAN
  4720. unsigned long lastMotor = 0; // Last time a motor was turned on
  4721. unsigned long lastMotorCheck = 0; // Last time the state was checked
  4722. void controllerFan() {
  4723. uint32_t ms = millis();
  4724. if (ms >= lastMotorCheck + 2500) { // Not a time critical function, so we only check every 2500ms
  4725. lastMotorCheck = ms;
  4726. if (X_ENABLE_READ == X_ENABLE_ON || Y_ENABLE_READ == Y_ENABLE_ON || Z_ENABLE_READ == Z_ENABLE_ON || soft_pwm_bed > 0
  4727. || E0_ENABLE_READ == E_ENABLE_ON // If any of the drivers are enabled...
  4728. #if EXTRUDERS > 1
  4729. || E1_ENABLE_READ == E_ENABLE_ON
  4730. #if HAS_X2_ENABLE
  4731. || X2_ENABLE_READ == X_ENABLE_ON
  4732. #endif
  4733. #if EXTRUDERS > 2
  4734. || E2_ENABLE_READ == E_ENABLE_ON
  4735. #if EXTRUDERS > 3
  4736. || E3_ENABLE_READ == E_ENABLE_ON
  4737. #endif
  4738. #endif
  4739. #endif
  4740. ) {
  4741. lastMotor = ms; //... set time to NOW so the fan will turn on
  4742. }
  4743. uint8_t speed = (lastMotor == 0 || ms >= lastMotor + (CONTROLLERFAN_SECS * 1000UL)) ? 0 : CONTROLLERFAN_SPEED;
  4744. // allows digital or PWM fan output to be used (see M42 handling)
  4745. digitalWrite(CONTROLLERFAN_PIN, speed);
  4746. analogWrite(CONTROLLERFAN_PIN, speed);
  4747. }
  4748. }
  4749. #endif
  4750. #ifdef SCARA
  4751. void calculate_SCARA_forward_Transform(float f_scara[3])
  4752. {
  4753. // Perform forward kinematics, and place results in delta[3]
  4754. // The maths and first version has been done by QHARLEY . Integrated into masterbranch 06/2014 and slightly restructured by Joachim Cerny in June 2014
  4755. float x_sin, x_cos, y_sin, y_cos;
  4756. //SERIAL_ECHOPGM("f_delta x="); SERIAL_ECHO(f_scara[X_AXIS]);
  4757. //SERIAL_ECHOPGM(" y="); SERIAL_ECHO(f_scara[Y_AXIS]);
  4758. x_sin = sin(f_scara[X_AXIS]/SCARA_RAD2DEG) * Linkage_1;
  4759. x_cos = cos(f_scara[X_AXIS]/SCARA_RAD2DEG) * Linkage_1;
  4760. y_sin = sin(f_scara[Y_AXIS]/SCARA_RAD2DEG) * Linkage_2;
  4761. y_cos = cos(f_scara[Y_AXIS]/SCARA_RAD2DEG) * Linkage_2;
  4762. // SERIAL_ECHOPGM(" x_sin="); SERIAL_ECHO(x_sin);
  4763. // SERIAL_ECHOPGM(" x_cos="); SERIAL_ECHO(x_cos);
  4764. // SERIAL_ECHOPGM(" y_sin="); SERIAL_ECHO(y_sin);
  4765. // SERIAL_ECHOPGM(" y_cos="); SERIAL_ECHOLN(y_cos);
  4766. delta[X_AXIS] = x_cos + y_cos + SCARA_offset_x; //theta
  4767. delta[Y_AXIS] = x_sin + y_sin + SCARA_offset_y; //theta+phi
  4768. //SERIAL_ECHOPGM(" delta[X_AXIS]="); SERIAL_ECHO(delta[X_AXIS]);
  4769. //SERIAL_ECHOPGM(" delta[Y_AXIS]="); SERIAL_ECHOLN(delta[Y_AXIS]);
  4770. }
  4771. void calculate_delta(float cartesian[3]){
  4772. //reverse kinematics.
  4773. // Perform reversed kinematics, and place results in delta[3]
  4774. // The maths and first version has been done by QHARLEY . Integrated into masterbranch 06/2014 and slightly restructured by Joachim Cerny in June 2014
  4775. float SCARA_pos[2];
  4776. static float SCARA_C2, SCARA_S2, SCARA_K1, SCARA_K2, SCARA_theta, SCARA_psi;
  4777. SCARA_pos[X_AXIS] = cartesian[X_AXIS] * axis_scaling[X_AXIS] - SCARA_offset_x; //Translate SCARA to standard X Y
  4778. SCARA_pos[Y_AXIS] = cartesian[Y_AXIS] * axis_scaling[Y_AXIS] - SCARA_offset_y; // With scaling factor.
  4779. #if (Linkage_1 == Linkage_2)
  4780. SCARA_C2 = ( ( sq(SCARA_pos[X_AXIS]) + sq(SCARA_pos[Y_AXIS]) ) / (2 * (float)L1_2) ) - 1;
  4781. #else
  4782. SCARA_C2 = ( sq(SCARA_pos[X_AXIS]) + sq(SCARA_pos[Y_AXIS]) - (float)L1_2 - (float)L2_2 ) / 45000;
  4783. #endif
  4784. SCARA_S2 = sqrt( 1 - sq(SCARA_C2) );
  4785. SCARA_K1 = Linkage_1 + Linkage_2 * SCARA_C2;
  4786. SCARA_K2 = Linkage_2 * SCARA_S2;
  4787. SCARA_theta = ( atan2(SCARA_pos[X_AXIS],SCARA_pos[Y_AXIS])-atan2(SCARA_K1, SCARA_K2) ) * -1;
  4788. SCARA_psi = atan2(SCARA_S2,SCARA_C2);
  4789. delta[X_AXIS] = SCARA_theta * SCARA_RAD2DEG; // Multiply by 180/Pi - theta is support arm angle
  4790. delta[Y_AXIS] = (SCARA_theta + SCARA_psi) * SCARA_RAD2DEG; // - equal to sub arm angle (inverted motor)
  4791. delta[Z_AXIS] = cartesian[Z_AXIS];
  4792. /*
  4793. SERIAL_ECHOPGM("cartesian x="); SERIAL_ECHO(cartesian[X_AXIS]);
  4794. SERIAL_ECHOPGM(" y="); SERIAL_ECHO(cartesian[Y_AXIS]);
  4795. SERIAL_ECHOPGM(" z="); SERIAL_ECHOLN(cartesian[Z_AXIS]);
  4796. SERIAL_ECHOPGM("scara x="); SERIAL_ECHO(SCARA_pos[X_AXIS]);
  4797. SERIAL_ECHOPGM(" y="); SERIAL_ECHOLN(SCARA_pos[Y_AXIS]);
  4798. SERIAL_ECHOPGM("delta x="); SERIAL_ECHO(delta[X_AXIS]);
  4799. SERIAL_ECHOPGM(" y="); SERIAL_ECHO(delta[Y_AXIS]);
  4800. SERIAL_ECHOPGM(" z="); SERIAL_ECHOLN(delta[Z_AXIS]);
  4801. SERIAL_ECHOPGM("C2="); SERIAL_ECHO(SCARA_C2);
  4802. SERIAL_ECHOPGM(" S2="); SERIAL_ECHO(SCARA_S2);
  4803. SERIAL_ECHOPGM(" Theta="); SERIAL_ECHO(SCARA_theta);
  4804. SERIAL_ECHOPGM(" Psi="); SERIAL_ECHOLN(SCARA_psi);
  4805. SERIAL_ECHOLN(" ");*/
  4806. }
  4807. #endif
  4808. #ifdef TEMP_STAT_LEDS
  4809. static bool blue_led = false;
  4810. static bool red_led = false;
  4811. static uint32_t stat_update = 0;
  4812. void handle_status_leds(void) {
  4813. float max_temp = 0.0;
  4814. if(millis() > stat_update) {
  4815. stat_update += 500; // Update every 0.5s
  4816. for (int8_t cur_extruder = 0; cur_extruder < EXTRUDERS; ++cur_extruder) {
  4817. max_temp = max(max_temp, degHotend(cur_extruder));
  4818. max_temp = max(max_temp, degTargetHotend(cur_extruder));
  4819. }
  4820. #if HAS_TEMP_BED
  4821. max_temp = max(max_temp, degTargetBed());
  4822. max_temp = max(max_temp, degBed());
  4823. #endif
  4824. if((max_temp > 55.0) && (red_led == false)) {
  4825. digitalWrite(STAT_LED_RED, 1);
  4826. digitalWrite(STAT_LED_BLUE, 0);
  4827. red_led = true;
  4828. blue_led = false;
  4829. }
  4830. if((max_temp < 54.0) && (blue_led == false)) {
  4831. digitalWrite(STAT_LED_RED, 0);
  4832. digitalWrite(STAT_LED_BLUE, 1);
  4833. red_led = false;
  4834. blue_led = true;
  4835. }
  4836. }
  4837. }
  4838. #endif
  4839. void enable_all_steppers() {
  4840. enable_x();
  4841. enable_y();
  4842. enable_z();
  4843. enable_e0();
  4844. enable_e1();
  4845. enable_e2();
  4846. enable_e3();
  4847. }
  4848. void disable_all_steppers() {
  4849. disable_x();
  4850. disable_y();
  4851. disable_z();
  4852. disable_e0();
  4853. disable_e1();
  4854. disable_e2();
  4855. disable_e3();
  4856. }
  4857. /**
  4858. * Manage several activities:
  4859. * - Check for Filament Runout
  4860. * - Keep the command buffer full
  4861. * - Check for maximum inactive time between commands
  4862. * - Check for maximum inactive time between stepper commands
  4863. * - Check if pin CHDK needs to go LOW
  4864. * - Check for KILL button held down
  4865. * - Check for HOME button held down
  4866. * - Check if cooling fan needs to be switched on
  4867. * - Check if an idle but hot extruder needs filament extruded (EXTRUDER_RUNOUT_PREVENT)
  4868. */
  4869. void manage_inactivity(bool ignore_stepper_queue/*=false*/) {
  4870. #if HAS_FILRUNOUT
  4871. if (card.sdprinting && !(READ(FILRUNOUT_PIN) ^ FIL_RUNOUT_INVERTING))
  4872. filrunout();
  4873. #endif
  4874. if (buflen < BUFSIZE - 1) get_command();
  4875. unsigned long ms = millis();
  4876. if (max_inactive_time && ms > previous_millis_cmd + max_inactive_time) kill();
  4877. if (stepper_inactive_time && ms > previous_millis_cmd + stepper_inactive_time
  4878. && !ignore_stepper_queue && !blocks_queued())
  4879. disable_all_steppers();
  4880. #ifdef CHDK // Check if pin should be set to LOW after M240 set it to HIGH
  4881. if (chdkActive && ms > chdkHigh + CHDK_DELAY) {
  4882. chdkActive = false;
  4883. WRITE(CHDK, LOW);
  4884. }
  4885. #endif
  4886. #if HAS_KILL
  4887. // Check if the kill button was pressed and wait just in case it was an accidental
  4888. // key kill key press
  4889. // -------------------------------------------------------------------------------
  4890. static int killCount = 0; // make the inactivity button a bit less responsive
  4891. const int KILL_DELAY = 750;
  4892. if (!READ(KILL_PIN))
  4893. killCount++;
  4894. else if (killCount > 0)
  4895. killCount--;
  4896. // Exceeded threshold and we can confirm that it was not accidental
  4897. // KILL the machine
  4898. // ----------------------------------------------------------------
  4899. if (killCount >= KILL_DELAY) kill();
  4900. #endif
  4901. #if HAS_HOME
  4902. // Check to see if we have to home, use poor man's debouncer
  4903. // ---------------------------------------------------------
  4904. static int homeDebounceCount = 0; // poor man's debouncing count
  4905. const int HOME_DEBOUNCE_DELAY = 750;
  4906. if (!READ(HOME_PIN)) {
  4907. if (!homeDebounceCount) {
  4908. enquecommands_P(PSTR("G28"));
  4909. LCD_ALERTMESSAGEPGM(MSG_AUTO_HOME);
  4910. }
  4911. if (homeDebounceCount < HOME_DEBOUNCE_DELAY)
  4912. homeDebounceCount++;
  4913. else
  4914. homeDebounceCount = 0;
  4915. }
  4916. #endif
  4917. #if HAS_CONTROLLERFAN
  4918. controllerFan(); // Check if fan should be turned on to cool stepper drivers down
  4919. #endif
  4920. #ifdef EXTRUDER_RUNOUT_PREVENT
  4921. if (ms > previous_millis_cmd + EXTRUDER_RUNOUT_SECONDS * 1000)
  4922. if (degHotend(active_extruder) > EXTRUDER_RUNOUT_MINTEMP) {
  4923. bool oldstatus;
  4924. switch(active_extruder) {
  4925. case 0:
  4926. oldstatus = E0_ENABLE_READ;
  4927. enable_e0();
  4928. break;
  4929. #if EXTRUDERS > 1
  4930. case 1:
  4931. oldstatus = E1_ENABLE_READ;
  4932. enable_e1();
  4933. break;
  4934. #if EXTRUDERS > 2
  4935. case 2:
  4936. oldstatus = E2_ENABLE_READ;
  4937. enable_e2();
  4938. break;
  4939. #if EXTRUDERS > 3
  4940. case 3:
  4941. oldstatus = E3_ENABLE_READ;
  4942. enable_e3();
  4943. break;
  4944. #endif
  4945. #endif
  4946. #endif
  4947. }
  4948. float oldepos = current_position[E_AXIS], oldedes = destination[E_AXIS];
  4949. plan_buffer_line(destination[X_AXIS], destination[Y_AXIS], destination[Z_AXIS],
  4950. destination[E_AXIS] + EXTRUDER_RUNOUT_EXTRUDE * EXTRUDER_RUNOUT_ESTEPS / axis_steps_per_unit[E_AXIS],
  4951. EXTRUDER_RUNOUT_SPEED / 60. * EXTRUDER_RUNOUT_ESTEPS / axis_steps_per_unit[E_AXIS], active_extruder);
  4952. current_position[E_AXIS] = oldepos;
  4953. destination[E_AXIS] = oldedes;
  4954. plan_set_e_position(oldepos);
  4955. previous_millis_cmd = ms; // refresh_cmd_timeout()
  4956. st_synchronize();
  4957. switch(active_extruder) {
  4958. case 0:
  4959. E0_ENABLE_WRITE(oldstatus);
  4960. break;
  4961. #if EXTRUDERS > 1
  4962. case 1:
  4963. E1_ENABLE_WRITE(oldstatus);
  4964. break;
  4965. #if EXTRUDERS > 2
  4966. case 2:
  4967. E2_ENABLE_WRITE(oldstatus);
  4968. break;
  4969. #if EXTRUDERS > 3
  4970. case 3:
  4971. E3_ENABLE_WRITE(oldstatus);
  4972. break;
  4973. #endif
  4974. #endif
  4975. #endif
  4976. }
  4977. }
  4978. #endif
  4979. #ifdef DUAL_X_CARRIAGE
  4980. // handle delayed move timeout
  4981. if (delayed_move_time && ms > delayed_move_time + 1000 && !Stopped) {
  4982. // travel moves have been received so enact them
  4983. delayed_move_time = 0xFFFFFFFFUL; // force moves to be done
  4984. set_destination_to_current();
  4985. prepare_move();
  4986. }
  4987. #endif
  4988. #ifdef TEMP_STAT_LEDS
  4989. handle_status_leds();
  4990. #endif
  4991. check_axes_activity();
  4992. }
  4993. void kill()
  4994. {
  4995. cli(); // Stop interrupts
  4996. disable_heater();
  4997. disable_all_steppers();
  4998. #if HAS_POWER_SWITCH
  4999. pinMode(PS_ON_PIN, INPUT);
  5000. #endif
  5001. SERIAL_ERROR_START;
  5002. SERIAL_ERRORLNPGM(MSG_ERR_KILLED);
  5003. LCD_ALERTMESSAGEPGM(MSG_KILLED);
  5004. // FMC small patch to update the LCD before ending
  5005. sei(); // enable interrupts
  5006. for (int i = 5; i--; lcd_update()) delay(200); // Wait a short time
  5007. cli(); // disable interrupts
  5008. suicide();
  5009. while(1) { /* Intentionally left empty */ } // Wait for reset
  5010. }
  5011. #ifdef FILAMENT_RUNOUT_SENSOR
  5012. void filrunout()
  5013. {
  5014. if filrunoutEnqued == false {
  5015. filrunoutEnqued = true;
  5016. enquecommand("M600");
  5017. }
  5018. }
  5019. #endif
  5020. void Stop()
  5021. {
  5022. disable_heater();
  5023. if(Stopped == false) {
  5024. Stopped = true;
  5025. Stopped_gcode_LastN = gcode_LastN; // Save last g_code for restart
  5026. SERIAL_ERROR_START;
  5027. SERIAL_ERRORLNPGM(MSG_ERR_STOPPED);
  5028. LCD_MESSAGEPGM(MSG_STOPPED);
  5029. }
  5030. }
  5031. bool IsStopped() { return Stopped; };
  5032. #ifdef FAST_PWM_FAN
  5033. void setPwmFrequency(uint8_t pin, int val)
  5034. {
  5035. val &= 0x07;
  5036. switch(digitalPinToTimer(pin))
  5037. {
  5038. #if defined(TCCR0A)
  5039. case TIMER0A:
  5040. case TIMER0B:
  5041. // TCCR0B &= ~(_BV(CS00) | _BV(CS01) | _BV(CS02));
  5042. // TCCR0B |= val;
  5043. break;
  5044. #endif
  5045. #if defined(TCCR1A)
  5046. case TIMER1A:
  5047. case TIMER1B:
  5048. // TCCR1B &= ~(_BV(CS10) | _BV(CS11) | _BV(CS12));
  5049. // TCCR1B |= val;
  5050. break;
  5051. #endif
  5052. #if defined(TCCR2)
  5053. case TIMER2:
  5054. case TIMER2:
  5055. TCCR2 &= ~(_BV(CS10) | _BV(CS11) | _BV(CS12));
  5056. TCCR2 |= val;
  5057. break;
  5058. #endif
  5059. #if defined(TCCR2A)
  5060. case TIMER2A:
  5061. case TIMER2B:
  5062. TCCR2B &= ~(_BV(CS20) | _BV(CS21) | _BV(CS22));
  5063. TCCR2B |= val;
  5064. break;
  5065. #endif
  5066. #if defined(TCCR3A)
  5067. case TIMER3A:
  5068. case TIMER3B:
  5069. case TIMER3C:
  5070. TCCR3B &= ~(_BV(CS30) | _BV(CS31) | _BV(CS32));
  5071. TCCR3B |= val;
  5072. break;
  5073. #endif
  5074. #if defined(TCCR4A)
  5075. case TIMER4A:
  5076. case TIMER4B:
  5077. case TIMER4C:
  5078. TCCR4B &= ~(_BV(CS40) | _BV(CS41) | _BV(CS42));
  5079. TCCR4B |= val;
  5080. break;
  5081. #endif
  5082. #if defined(TCCR5A)
  5083. case TIMER5A:
  5084. case TIMER5B:
  5085. case TIMER5C:
  5086. TCCR5B &= ~(_BV(CS50) | _BV(CS51) | _BV(CS52));
  5087. TCCR5B |= val;
  5088. break;
  5089. #endif
  5090. }
  5091. }
  5092. #endif //FAST_PWM_FAN
  5093. bool setTargetedHotend(int code){
  5094. target_extruder = active_extruder;
  5095. if (code_seen('T')) {
  5096. target_extruder = code_value_short();
  5097. if (target_extruder >= EXTRUDERS) {
  5098. SERIAL_ECHO_START;
  5099. switch(code){
  5100. case 104:
  5101. SERIAL_ECHO(MSG_M104_INVALID_EXTRUDER);
  5102. break;
  5103. case 105:
  5104. SERIAL_ECHO(MSG_M105_INVALID_EXTRUDER);
  5105. break;
  5106. case 109:
  5107. SERIAL_ECHO(MSG_M109_INVALID_EXTRUDER);
  5108. break;
  5109. case 218:
  5110. SERIAL_ECHO(MSG_M218_INVALID_EXTRUDER);
  5111. break;
  5112. case 221:
  5113. SERIAL_ECHO(MSG_M221_INVALID_EXTRUDER);
  5114. break;
  5115. }
  5116. SERIAL_ECHOLN(target_extruder);
  5117. return true;
  5118. }
  5119. }
  5120. return false;
  5121. }
  5122. float calculate_volumetric_multiplier(float diameter) {
  5123. if (!volumetric_enabled || diameter == 0) return 1.0;
  5124. float d2 = diameter * 0.5;
  5125. return 1.0 / (M_PI * d2 * d2);
  5126. }
  5127. void calculate_volumetric_multipliers() {
  5128. for (int i=0; i<EXTRUDERS; i++)
  5129. volumetric_multiplier[i] = calculate_volumetric_multiplier(filament_size[i]);
  5130. }