Marlin_main.cpp 182 KB

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