Marlin_main.cpp 179 KB

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