My Marlin configs for Fabrikator Mini and CTC i3 Pro B
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

Marlin_main.cpp 156KB

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