Marlin_main.cpp 181 KB

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