Marlin_main.cpp 227 KB

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