Marlin_main.cpp 238 KB

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