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 179KB

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