Marlin_main.cpp 184 KB

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