Marlin_main.cpp 197 KB

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