classdef.cpp 195 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723572457255726572757285729573057315732573357345735573657375738573957405741574257435744574557465747574857495750575157525753575457555756575757585759576057615762576357645765576657675768576957705771577257735774577557765777577857795780578157825783578457855786578757885789579057915792579357945795579657975798579958005801580258035804580558065807580858095810581158125813581458155816581758185819582058215822582358245825582658275828582958305831583258335834583558365837583858395840584158425843584458455846584758485849585058515852585358545855585658575858585958605861586258635864586558665867586858695870587158725873587458755876587758785879588058815882588358845885588658875888588958905891589258935894589558965897589858995900590159025903590459055906590759085909591059115912591359145915591659175918591959205921592259235924592559265927592859295930593159325933593459355936593759385939594059415942594359445945594659475948594959505951595259535954595559565957595859595960596159625963596459655966596759685969597059715972597359745975597659775978597959805981598259835984598559865987598859895990599159925993599459955996599759985999600060016002600360046005600660076008600960106011601260136014601560166017601860196020602160226023602460256026602760286029603060316032603360346035603660376038603960406041604260436044604560466047604860496050605160526053605460556056605760586059606060616062606360646065606660676068606960706071607260736074607560766077607860796080608160826083608460856086608760886089609060916092609360946095609660976098609961006101610261036104610561066107610861096110611161126113611461156116611761186119612061216122612361246125612661276128612961306131613261336134613561366137613861396140614161426143614461456146614761486149615061516152615361546155615661576158615961606161616261636164616561666167616861696170617161726173617461756176617761786179618061816182618361846185618661876188618961906191619261936194619561966197619861996200620162026203620462056206620762086209621062116212621362146215621662176218621962206221622262236224622562266227622862296230623162326233623462356236623762386239624062416242624362446245624662476248624962506251625262536254625562566257625862596260626162626263626462656266626762686269627062716272627362746275627662776278627962806281628262836284628562866287628862896290629162926293629462956296629762986299630063016302630363046305630663076308630963106311631263136314631563166317631863196320632163226323632463256326632763286329633063316332633363346335633663376338633963406341634263436344634563466347634863496350635163526353635463556356635763586359636063616362636363646365636663676368636963706371637263736374637563766377637863796380638163826383638463856386638763886389639063916392639363946395639663976398639964006401640264036404640564066407640864096410641164126413641464156416641764186419642064216422642364246425642664276428642964306431643264336434643564366437643864396440644164426443644464456446644764486449645064516452645364546455645664576458645964606461646264636464646564666467646864696470647164726473647464756476647764786479648064816482648364846485648664876488648964906491649264936494649564966497649864996500650165026503650465056506650765086509651065116512651365146515651665176518651965206521652265236524652565266527652865296530653165326533653465356536653765386539654065416542654365446545654665476548654965506551655265536554655565566557655865596560656165626563656465656566656765686569657065716572657365746575657665776578657965806581658265836584658565866587658865896590659165926593659465956596659765986599660066016602660366046605660666076608660966106611661266136614661566166617661866196620662166226623662466256626662766286629663066316632663366346635663666376638663966406641664266436644664566466647664866496650665166526653665466556656665766586659666066616662666366646665666666676668666966706671667266736674667566766677667866796680668166826683668466856686668766886689669066916692669366946695669666976698669967006701670267036704670567066707670867096710671167126713671467156716671767186719672067216722672367246725672667276728672967306731673267336734673567366737673867396740674167426743674467456746674767486749675067516752675367546755675667576758675967606761676267636764676567666767676867696770677167726773677467756776677767786779678067816782678367846785678667876788678967906791679267936794679567966797679867996800680168026803680468056806680768086809681068116812681368146815681668176818681968206821682268236824682568266827682868296830683168326833683468356836683768386839684068416842684368446845684668476848684968506851685268536854685568566857685868596860686168626863686468656866686768686869687068716872687368746875687668776878687968806881688268836884688568866887688868896890689168926893689468956896689768986899690069016902690369046905690669076908690969106911691269136914691569166917691869196920692169226923692469256926692769286929693069316932693369346935693669376938693969406941694269436944694569466947694869496950695169526953695469556956695769586959696069616962696369646965696669676968696969706971697269736974697569766977697869796980698169826983698469856986698769886989699069916992699369946995699669976998699970007001700270037004700570067007700870097010701170127013701470157016701770187019702070217022702370247025702670277028702970307031703270337034703570367037703870397040704170427043704470457046704770487049705070517052705370547055705670577058705970607061706270637064706570667067706870697070707170727073707470757076707770787079708070817082708370847085708670877088708970907091709270937094709570967097709870997100710171027103710471057106710771087109711071117112711371147115711671177118711971207121712271237124712571267127712871297130713171327133713471357136713771387139714071417142714371447145714671477148714971507151715271537154715571567157715871597160716171627163716471657166716771687169717071717172717371747175717671777178717971807181718271837184718571867187718871897190719171927193719471957196719771987199720072017202720372047205720672077208720972107211721272137214721572167217721872197220722172227223722472257226722772287229723072317232723372347235723672377238723972407241724272437244724572467247724872497250725172527253725472557256725772587259726072617262726372647265726672677268726972707271727272737274727572767277727872797280728172827283728472857286728772887289729072917292729372947295729672977298729973007301730273037304730573067307730873097310731173127313731473157316731773187319732073217322732373247325732673277328732973307331733273337334733573367337733873397340734173427343734473457346734773487349735073517352735373547355735673577358735973607361736273637364736573667367736873697370737173727373737473757376737773787379738073817382738373847385738673877388738973907391739273937394739573967397739873997400740174027403740474057406740774087409741074117412741374147415741674177418741974207421742274237424742574267427742874297430743174327433743474357436743774387439744074417442744374447445744674477448744974507451745274537454745574567457745874597460746174627463746474657466746774687469747074717472747374747475747674777478747974807481748274837484748574867487748874897490749174927493749474957496749774987499750075017502750375047505750675077508750975107511751275137514751575167517751875197520752175227523752475257526752775287529753075317532753375347535753675377538753975407541754275437544754575467547754875497550755175527553755475557556755775587559756075617562756375647565756675677568756975707571757275737574757575767577757875797580758175827583758475857586758775887589759075917592759375947595759675977598759976007601760276037604760576067607760876097610761176127613761476157616761776187619762076217622762376247625762676277628762976307631763276337634763576367637763876397640764176427643764476457646764776487649765076517652765376547655765676577658765976607661766276637664766576667667766876697670767176727673767476757676767776787679768076817682768376847685768676877688768976907691769276937694769576967697769876997700770177027703770477057706770777087709771077117712771377147715771677177718771977207721772277237724772577267727772877297730773177327733773477357736773777387739774077417742774377447745774677477748774977507751775277537754775577567757775877597760776177627763776477657766776777687769777077717772777377747775777677777778777977807781778277837784778577867787778877897790779177927793779477957796779777987799780078017802780378047805780678077808780978107811781278137814781578167817781878197820782178227823782478257826782778287829783078317832783378347835783678377838783978407841784278437844784578467847784878497850785178527853785478557856785778587859786078617862786378647865786678677868786978707871787278737874787578767877787878797880788178827883788478857886788778887889789078917892789378947895789678977898789979007901790279037904790579067907790879097910791179127913791479157916791779187919792079217922792379247925792679277928792979307931793279337934793579367937793879397940794179427943794479457946794779487949795079517952795379547955795679577958795979607961796279637964796579667967796879697970797179727973797479757976797779787979798079817982798379847985798679877988798979907991799279937994799579967997799879998000800180028003800480058006800780088009801080118012801380148015801680178018801980208021802280238024802580268027802880298030803180328033803480358036803780388039804080418042804380448045804680478048804980508051805280538054805580568057805880598060806180628063806480658066806780688069807080718072807380748075807680778078807980808081808280838084808580868087808880898090809180928093809480958096809780988099810081018102810381048105810681078108810981108111811281138114811581168117811881198120812181228123812481258126812781288129813081318132813381348135813681378138813981408141814281438144814581468147814881498150815181528153815481558156815781588159816081618162816381648165816681678168816981708171817281738174817581768177817881798180818181828183818481858186818781888189819081918192819381948195819681978198819982008201820282038204820582068207820882098210821182128213821482158216
  1. #include "stdafx.h"
  2. #include <math.h>
  3. #include <algorithm>
  4. #include "classdef.h"
  5. #include "constdef.h"
  6. #include "structdef.h"
  7. #include "locate_algorithm.h"
  8. #include "./system_basic_info/SystemAnalysis.h"
  9. #include "ProcessRemodule.h"
  10. #include "log_process_module.h"
  11. #include "Functions/Functions.h"
  12. #include "algorithm/Fit.h"
  13. #include "./mylog/log_module.h"
  14. #include <boost/make_shared.hpp>
  15. #include "../Common/matrix/_Matrix.h"
  16. #include "algorithm/base/const_data.h"
  17. #pragma warning(disable: 4244)
  18. using namespace std;
  19. unsigned int g_nAlgoFailedCounts[ALGO_LOC_TOTAL] = {0};
  20. unsigned int g_nAlgoFailedCycleCounts[ALGO_LOC_TOTAL] = {0};
  21. DWORD g_ullCurTime = 0;
  22. unsigned long long m_Start_time = time(NULL)*1000;
  23. Card::Card( string cardid, int cardtype, double z_offset)
  24. :m_bigCarFlag(false),m_overSpeed(0),m_overAreaSpeed(0),m_isWarning(false),m_minDistanceToWarningpoint(DBL_MAX),m_warning_threshold(DBL_MAX),m_CardHasBeenInDrivingfaceAlarm(false),
  25. m_DrivingfaceAlarmFlag(-1),m_drivingface_index(0),m_drivingface_index_del(0),m_warning_point_alarm(false),m_disToWarningPoint(0),m_wp_id(0),m_workface_index(0),m_workface_index_del(0),shift_id(0),m_indexForRcvOneReader(0),m_indexCardSpeed(0),need_display(1),speed_limit_after_reset(0),final_v(0)
  26. ,m_area_changed(false),m_oldv(0),power_alarm_display(1),power_change_flag(false),status_help_cancel(0),m_ct_time(0)
  27. {
  28. time_center_stamp = 0;
  29. m_warning_point_id = -1;
  30. BOOL bRet = InitializeCriticalSectionAndSpinCount(&m_csCard, MAXCRITICALSECTIONSPINCOUNT);
  31. is_drivingface_Initialization=false;
  32. card_type = cardtype;
  33. card_id = cardid;
  34. this->z_offset = z_offset;
  35. x = y = z = stored_x = stored_y = stored_z = INVALID_COORDINATE;
  36. last_correct_position_info_time = time(NULL);
  37. att_start_time_latest = att_end_time_latest = time(NULL);
  38. time_low_power = time(NULL);
  39. map_scale = 1.0;
  40. dept_id = group_id = occ_id = level_id = worktype_id = vehice_type_id = 0;
  41. power_state = power_state_last = 0;
  42. id = 0;
  43. driver_id = "";
  44. shift_type_id = 1;
  45. is_registered = true;
  46. is_deal_by_algo = false;
  47. pTofReaderPathMap = NULL;
  48. pTdoaReaderPathMap = NULL;
  49. m_syncNumList.clear();
  50. this->driving_face_cur_shift_start_x = this->driving_face_cur_shift_start_y = this->driving_face_cur_shift_start_z= this->driving_face_cur_shift_x= this->driving_face_cur_shift_y=this->driving_face_cur_shift_z = 0;
  51. m_bUseFilter = false;
  52. m_nFilterType = NO_FILTER;
  53. m_pKalmanFilter = nullptr;
  54. ins_weight = INS_WEIGHT;
  55. uwb_weight = UWB_WEIGHT;
  56. m_nLightGroupId = -1; //默认为没控制灯组
  57. light_id = 0;
  58. m_nOutputPosState = OUTPUT_POS::RIGHT_POS;
  59. m_nStream = INIT_STREAM;
  60. for(int i = 0; i < CARD_EVENT_COUNT; i++){
  61. m_event_list[i] = 0;
  62. }
  63. is_fit_pos = false;
  64. is_over_interval = false;
  65. call_type = 0;
  66. rec_time = time(NULL);
  67. last_correct_position_info_time = time(NULL);
  68. cur_ins_pos = std::shared_ptr<POS>();
  69. m_curshift_finish_length = 0;
  70. m_cur_total_finish_length = 0;
  71. ins_request_check = YES_INS_REQUEST;
  72. ins_gyp_angle = -1;
  73. ins_gyp_pos = nullptr;
  74. ins_cal_type = -1;
  75. ins_request_check = -1;
  76. cal_location3 = new CalLocation3(KALMAN_OFFSET_COOR_TRI, KALMAN_OFFSET_MOVE_TRI, KALMAN_OFFSET_RANGING_TRI, KALMAN_INTERVAL_TRI);
  77. p3_anchors = new Point3[ANCHOR_COUNT];
  78. cal_location = new CalLocation(KALMAN_OFFSET_COOR, KALMAN_OFFSET_MOVE, KALMAN_OFFSET_RANGING, KALMAN_INTERVAL);
  79. p2_anchors = new Point2[3];
  80. is_anchor_changed = false;
  81. is_init_kalman = false;
  82. ins_req_status_algo = -1;
  83. is_send_ins_check = false;
  84. is_del_data = false;
  85. this->resetKalmanInfo();
  86. pTaylorWls = std::make_shared<Tdoa::TaylorWls>();
  87. pSensorManager = nullptr;
  88. pAcceSensor = nullptr;
  89. pGyroscopeSensor = nullptr;
  90. pSensorManager = std::make_shared<algorithm::base::SensorManager>();
  91. if (pSensorManager!= nullptr)
  92. {
  93. pAcceSensor = pSensorManager->get_sensor(algorithm::base::SensorType::Sensor_Accelerometer);
  94. pGyroscopeSensor = pSensorManager->get_sensor(algorithm::base::SensorType::Sensor_Gyroscope);
  95. }
  96. is_use_acc_filter = false;
  97. this->resetAllBaseInfo();
  98. this->reset();
  99. mp_person_in_vechicle_list = std::make_shared<CardMap>();
  100. recevie_by_moving_reader = 0;
  101. person_on_card = 0; //默认为下车状态
  102. min_distance = 99999.9;
  103. possible_vehicle_id = "";
  104. relative_speed = 0.0;
  105. satisfy_off_vehicle_counts = satisfy_on_vehicle_counts = 0;
  106. package_is_over = false;
  107. no_need_add_dist = false;
  108. recv_signal_power = 0.0;
  109. pos_state_count = 0;
  110. pos_state_confirm_times = 1;
  111. m_cb_state.set_capacity(10); // 设置10个开关机标志位,10个中有7个为1则为开机,有7个为0则为关机
  112. for (int index = 0; index < 10; index++)// 循环队列初始化
  113. {
  114. m_cb_state.push_back(0);
  115. }
  116. m_cb_position.resize(0); //设置60个
  117. m_b_first_record = true;
  118. m_startup_time = 0;
  119. m_bOpen = false;
  120. m_nCardId = 0;
  121. m_b_first_calc = true;
  122. m_base_position.set(0,0);
  123. m_lastrecv_time = 0;
  124. m_record_cycle_start_time = time(NULL);
  125. m_record_avg_dist = 0;
  126. m_record_max_dist = 0;
  127. m_record_min_dist = 999999.9;
  128. m_record_totals = 0;
  129. m_fScheduleStartupTime = 0.0;
  130. m_dbTimeInterval = 0;
  131. m_indexCardSpeed=m_indexCardSpeed_normal=m_indexMineOverSpeed=m_indexMineOverSpeed_normal=0;
  132. is_mine_over_speed = is_area_over_speed = false;
  133. m_arg=0x12345678;m_last_time=0;
  134. }
  135. void Card::resetAllBaseInfo()
  136. {
  137. m_dbTimeInterval = 0;
  138. ::GetLocalTime(&deal_time);
  139. ::GetLocalTime(&enter_area_time);
  140. down_time = up_time = enter_reader_time = rec_time = time(NULL);
  141. time_over_time = time_area_over_time = time_area_forbidden = time_over_speed = time(NULL);
  142. last_locate_time = time(NULL);
  143. //坐标初始值为0
  144. x = y = z = stored_x = stored_y = stored_z = INVALID_COORDINATE;
  145. m_indexCardSpeed=m_indexCardSpeed_normal=m_indexMineOverSpeed=m_indexMineOverSpeed_normal=0;
  146. output_x = output_y = 0;
  147. a = 0;
  148. t = 0;
  149. init_postion = false;
  150. b_pos_change = false;
  151. n_pos_no_change_times = 0;
  152. is_idling = false;
  153. idle_count = 0;
  154. is_driving_face_start = 0;
  155. right_x = right_y = right_z = left_x = left_y = left_z = 0;
  156. m_nMoveDirection = 0;
  157. is_mine_over_speed = is_area_over_speed = false;
  158. map_id = map_id_old = area_id = reader_id = 0;
  159. state = 0;
  160. state_moving = 0;
  161. state_meeting = 0;
  162. bInSpecailArea = false;
  163. state_biz = 0;
  164. pos_state = pos_state_old = PDT_INIT;
  165. pos_state_count = 0;
  166. pos_state_confirm_times = 1;
  167. status_help = status_area_over_time = status_area_forbidden = status_area_over_speed = 0;
  168. status_over_speed = status_over_time = status_power = status_lost = status_call = 0;
  169. sync_num = 0;
  170. isdealed = isreceive = is_hist = false;
  171. is_area_over_time = is_mine_over_time = false;
  172. reader_tickcount = time_stamp;
  173. time_stamp_last = 0;
  174. time_stamp_max = 0;
  175. is_deal_by_algo = false;
  176. m_syncNumList.clear();
  177. m_nCalcSyncNum = 0;
  178. m_nSyncNumInList = 0;
  179. last_locate.tt = 0;
  180. last_locate.a = last_locate.antenna_id = last_locate.reader_id = last_locate.t = 0;
  181. last_locate.d = last_locate.d_offset = last_locate.v = 0.0;
  182. last_locate.x = INVALID_COORDINATE;
  183. last_locate.y = INVALID_COORDINATE;
  184. last_locate.z = INVALID_COORDINATE;
  185. error_locate.x = error_locate.y = error_locate.z = 0;
  186. last_x = last_y = last_z = INVALID_COORDINATE;
  187. locate = nullptr;
  188. m_nLastLocateT = 0;
  189. origin_locate.x = origin_locate.y = origin_locate.z = origin_locate.v = 0.0;
  190. last_vx = last_vy = 0.0;
  191. v = 0.0;
  192. acce_cur_state = 0;
  193. acce_last_state = 0;
  194. b_long_interval = false;
  195. accelerate_state = accelerate_state_last = ACCELERATE_INIT_STATE; //默认初始的为-10
  196. ins_direction = 0;
  197. antenna_angle = 0;
  198. b_enter_intersection = false;
  199. is_red_light = false;
  200. time_red_light = time(NULL);
  201. m_nIsRailroad = 0;
  202. mileage = 0;
  203. diff_direction_counts = 0;
  204. p_reader = nullptr;
  205. _dists.swap(DistQueMap());
  206. vt_his_speed.resize(0);
  207. his_pos.resize(0);
  208. is_ref_pos = false;
  209. cur_fit_nums = 0;
  210. have_fit_pos = false;
  211. have_long_fit_pos = false;
  212. count_idle = 0;
  213. count_change_direction = 0;
  214. call_type_id = 0;
  215. vt_deal_call_reader_id.resize(0);
  216. recv_call_time = time(NULL);
  217. final_v = 0;
  218. landmark_id = 0;
  219. landmark_dis = 0;
  220. landmark_direction = NODIRECTORY;
  221. drive_face_location_write_time = 0;
  222. idle_pos_list.resize(0);
  223. time_last_tof = time(NULL);
  224. time_cur_algo = time(NULL);
  225. m_drivingface_start_x = 0;
  226. m_drivingface_start_y = 0;
  227. m_drivingface_start_z = 0;
  228. m_drivingface_start_time.clear();
  229. m_arg=0x12345678;m_last_time=0;
  230. }
  231. void Card::make_arg()
  232. {
  233. NAMESPACE_POINT::point p1(x,y);NAMESPACE_POINT::point p2(last_x,last_y);
  234. if (p1.invalid() || p2.invalid())
  235. return;
  236. if (p1.dist(p2)<0.5)
  237. return;
  238. if ( m_ct_time-m_last_time<5*1000)
  239. {
  240. m_arg = std::arg(std::complex<double>(x,y)-std::complex<double>(last_x,last_y));
  241. NAMESPACE_POINT::line_v l(NAMESPACE_POINT::point(last_x,last_y),NAMESPACE_POINT::point(x,y));
  242. m_line=l;
  243. debug_print_syslog(0,"[vehicle_alarm_arg ] %s,arg:%f (%.2f,%.2f)--(%.2f,%.2f)",card_id.c_str(),m_arg,last_x,last_y,x,y);
  244. }
  245. }
  246. /*
  247. * 重置算法参数:当信号消失,重置算法相关的参数
  248. *
  249. * param
  250. * 无
  251. *
  252. * return
  253. * 无
  254. *
  255. */
  256. void Card::reset()
  257. {
  258. //以下参数与算法相关
  259. //坐标初始值为0
  260. m_nLastLocateT = 0;
  261. time_stamp_cal_last = 0;
  262. b_long_interval = false;
  263. is_first_location = true;
  264. last_locate.sync_num = 0;
  265. is_algo_first_location = true;
  266. while (_dists.size()>0)
  267. {
  268. //TRACE(_T("33333"));
  269. _dists.pop_front();
  270. }
  271. if (m_syncNumList.size() > 0)
  272. {
  273. m_syncNumList.erase(m_syncNumList.begin(),m_syncNumList.end());
  274. m_syncNumList.resize(0);
  275. }
  276. if (vt_his_speed.size()>0)
  277. {
  278. vt_his_speed.erase(vt_his_speed.begin(),vt_his_speed.end());
  279. vt_his_speed.resize(0);
  280. }
  281. if (his_pos.size() > 0)
  282. {
  283. his_pos.erase(his_pos.begin(),his_pos.end());
  284. his_pos.resize(0);
  285. have_fit_pos = false;
  286. }
  287. if (long_his_pos.size() > 0)
  288. {
  289. long_his_pos.erase(long_his_pos.begin(),long_his_pos.end());
  290. long_his_pos.resize(0);
  291. have_long_fit_pos = false;
  292. count_idle = 0;
  293. }
  294. cur_fit_nums = 0;
  295. count_idle = 0;
  296. locate = nullptr;
  297. //卡尔曼参数重置
  298. if (m_pKalmanFilter!=nullptr)
  299. {
  300. m_pKalmanFilter->m_nCounts = 0;
  301. m_pKalmanFilter->Initial(0.2);
  302. }
  303. //tof相关数据重置
  304. time_last_tof = time(NULL);
  305. this->pGyroscopeSensor->SetValue(0.0);
  306. this->pAcceSensor->SetValue(0.0);
  307. pos_state = PDT_INIT;
  308. pos_state_old = PDT_DOWN;
  309. pos_state_count = 0;
  310. pos_state_confirm_times = 1;
  311. }
  312. void Card::set_reader(std::shared_ptr<Reader> preader) // 设置卡时间
  313. {
  314. this->rec_time = time(NULL);
  315. if(preader->reader_id != this->reader_id){ // 所在分站没有发生变化
  316. this->p_reader = preader.get();
  317. this->reader_id = preader->reader_id;
  318. this->enter_reader_time = this->rec_time;
  319. this->map_id_old = this->map_id;
  320. this->map_id = preader->map_id;
  321. this->map_scale = preader->map_scale;
  322. }
  323. }
  324. /*
  325. * 设置地图集覆盖范围,适用TOF
  326. *
  327. * param
  328. * trpm tdoa地图集
  329. *
  330. * return
  331. * 无
  332. *
  333. */
  334. void Card::set_reader_path_tof(std::shared_ptr<ReaderPathMap> rpm)
  335. {
  336. this->pTofReaderPathMap = rpm;
  337. }
  338. /*
  339. * 设置地图集覆盖范围,适用TDOA
  340. *
  341. * param
  342. * trpm tdoa地图集
  343. *
  344. * return
  345. * 无
  346. *
  347. */
  348. void Card::set_reader_path_tdoa(std::shared_ptr<TDOAReaderPathMap> trpm)
  349. {
  350. this->pTdoaReaderPathMap = trpm;
  351. }
  352. bool operator==(sync_data& a,sync_data&b){
  353. return a.sync_num == b.sync_num;
  354. }
  355. /*
  356. * 此函数主要判断此坐标是否需要形成json输出给webserver
  357. *
  358. * param
  359. * acce_state 算法中保存的当前加速度计状态
  360. * acce_state_last 算法中保存的上一次加速度计状态
  361. *
  362. * return
  363. * 无
  364. */
  365. int Card::inspect_coordinate(int acce_state)
  366. {
  367. this->isoutput = false;
  368. char log[200] = {0};
  369. //如果是静止或者怠速状态,每次的定位坐标不变化,此时要求采集不再将此数据送往webserver
  370. if (STATE_ACCE_STATIC == acce_state || STATE_ACCE_IDLING == acce_state)
  371. {
  372. this->isoutput = false;
  373. }
  374. else
  375. {
  376. if (this->x == this->output_x && this->y == this->output_y) //last_x,last_y
  377. {
  378. count_idle++;
  379. this->isoutput = false;
  380. }else{
  381. //如果运动状态时,
  382. int nSign = 0;
  383. int nState = 0;
  384. if (CheckStreamUniformity(this->output_x,this->output_y,this->x,this->y,this->m_nStream))
  385. {
  386. double distance = sqrt(pow(this->x - this->output_x,2) + pow(this->y - this->output_y,2)) * this->map_scale;
  387. if(distance > this->map_scale) //this->map_scale)
  388. {
  389. //两点之间的距离是否大于一个0.5米,满足条件,输出json
  390. this->isoutput = true;
  391. this->output_x = this->x;
  392. this->output_y = this->y;
  393. count_idle = 0;
  394. }else{
  395. count_idle++;
  396. }
  397. }
  398. else
  399. {
  400. if( count_change_direction >= MAX_REBOUND_COUNTS){
  401. // 认为掉头,输出给web
  402. this->isoutput = true;
  403. this->output_x = this->x;
  404. this->output_y = this->y;
  405. count_idle = 0;
  406. count_change_direction = 0;
  407. }else{
  408. // 认为是抖动,不输出给web
  409. count_idle++;
  410. }
  411. }
  412. }
  413. }
  414. return 0;
  415. }
  416. /*
  417. * 检查当前定位坐标和nStream方向是否一致
  418. *
  419. * param
  420. * x1,y1 起点的坐标
  421. * x2,y2 终点的坐标
  422. * nStream 上一次的方向
  423. *
  424. * return
  425. * 如果算出的方向和nStream一致,则返回true;否则返回false
  426. *
  427. */
  428. bool Card::CheckStreamUniformity(double x1,double y1,double x2,double y2,int nStream)
  429. {
  430. return nStream == GetStream(x1,y1,x2,y2);
  431. }
  432. /*
  433. * TOF定位算法
  434. *
  435. * param
  436. * cnt 数据条数
  437. *
  438. * return
  439. * 无
  440. *
  441. */
  442. void Card::algo_tof(int cnt)
  443. {
  444. }
  445. /*
  446. * TOF一维定位算法
  447. *
  448. * param
  449. * cnt 数据条数
  450. *
  451. * return
  452. * 成功返回0,否则返回非零值
  453. *
  454. */
  455. int Card::algo_tof_1d(int cnt)
  456. {
  457. if (LOCATEDATATYPE::LDT_TOF == ranging_type)
  458. {
  459. if (cnt < 1)
  460. {
  461. return 1;
  462. }
  463. }
  464. else
  465. {
  466. if (cnt < 2)
  467. {
  468. return 2;
  469. }
  470. }
  471. is_del_data = true;
  472. int nRet = 0;
  473. //组装数据部分
  474. map<unsigned long long, std::shared_ptr<_coordinate>> tmp_dists_locate;
  475. nRet = AssembleDistData(tmp_dists_locate);
  476. if (nRet)
  477. {
  478. return 3;
  479. }
  480. map<unsigned long long, std::shared_ptr<_coordinate>>::iterator it = tmp_dists_locate.begin();
  481. time_stamp_cal = it->second->t;
  482. double dMaxDistance = 1000.0;
  483. int key = 0;
  484. //修正由于高度所引入的误差
  485. for (map<unsigned long long, std::shared_ptr<_coordinate>>::iterator it_tmp = tmp_dists_locate.begin();it_tmp != tmp_dists_locate.end();++it_tmp)
  486. {
  487. double distance = 0.0;
  488. /*if(it_tmp->second->d < TOF_HEIGHT_ERROR)
  489. {
  490. continue;
  491. }*/
  492. #ifdef _DEBUG
  493. distance = sqrt(pow(it_tmp->second->d,2) - pow(TOF_HEIGHT_ERROR,2));//TOF_HEIGHT_ERROR
  494. #else
  495. distance = sqrt(pow(it_tmp->second->d,2) - pow(TOF_HEIGHT_ERROR,2));
  496. #endif
  497. it_tmp->second->d = distance;
  498. if (distance < dMaxDistance)
  499. {
  500. key = it_tmp->first;
  501. dMaxDistance = distance;
  502. }
  503. }
  504. //获取进算法的时间参与运算
  505. TOF_REFER_DATA refer_data;
  506. get_ref_data_from_dists(tmp_dists_locate,key,refer_data);
  507. std::shared_ptr<POS> p = nullptr;
  508. if (tmp_dists_locate.size() < 2)
  509. {
  510. if (tmp_dists_locate.size() == 1)
  511. {
  512. if (last_locate.x == INVALID_COORDINATE && last_locate.y == INVALID_COORDINATE)
  513. {
  514. return 4;
  515. }
  516. //如果当前计算的数据是单条数据,且卡的ct序号相差15以上,则此单条数据不参与计算
  517. if (time_stamp_cal - time_stamp_cal_last > 15)
  518. {
  519. return 5;
  520. }
  521. //根据一条距离来进行测试
  522. if (p == nullptr)
  523. {
  524. p = std::make_shared<POS>();
  525. }
  526. int nRet = 0;
  527. nRet = get_position_by_one_distance(pTofReaderPathMap,p,refer_data);
  528. if (nRet)
  529. {
  530. return 6;
  531. }
  532. }else{
  533. return 7;
  534. }
  535. }else{
  536. //如果数据条数大于2,走如下逻辑
  537. //数据条数大于2,会存在如下异常:即t时刻,天线距离 a<b,t+1时刻,天线距离 a>b 导致无法定位
  538. std::shared_ptr<POS> tmpPos = std::make_shared<POS>();
  539. bool isExist = false;
  540. int nReaderId = 0; //分站id
  541. int nAntennaIndex = 0; //天线索引
  542. double dist = 0.0; //距离
  543. //保存所有定位结果
  544. std::vector<std::shared_ptr<POS>> udm_pos;
  545. udm_pos.resize(0);
  546. for(it = tmp_dists_locate.begin();it != tmp_dists_locate.end();++it){
  547. nReaderId = it->second->reader_id;
  548. nAntennaIndex = it->second->antenna_id;
  549. dist = it->second->d;
  550. //如果为参考分站,则不计算,直接寻找下一个数据
  551. if (nReaderId == refer_data.nReaderId && nAntennaIndex == refer_data.nAntennaIndex && abs(dist - refer_data.dDistance) < ZERO_PRECISION)
  552. {
  553. continue;
  554. }
  555. //计算定位结果
  556. p = LocateAlgorithm::tof_locate_1d(pTofReaderPathMap,nReaderId,nAntennaIndex,dist,refer_data);
  557. if(p == nullptr || !CheckPositionValid(p))
  558. {
  559. continue;
  560. }
  561. isExist = true;
  562. udm_pos.push_back(p);
  563. }
  564. //如果不存在解,直接返回
  565. if (!isExist)
  566. {
  567. return 8;
  568. }else{
  569. double distance = 0.0;
  570. //求平均之前,对所有解过滤一次
  571. for (std::vector<std::shared_ptr<POS>>::iterator it = udm_pos.begin();it != udm_pos.end();)
  572. {
  573. //每个点都计算与参考点的距离,如果过大则抛弃
  574. distance = sqrt(pow((*it)->posx - refer_data.x*map_scale,2) + pow((*it)->posy - refer_data.y*map_scale,2));
  575. if (fabs(distance - refer_data.dDistance) > 10)
  576. {
  577. it = udm_pos.erase(it);
  578. }else{
  579. ++it;
  580. }
  581. }
  582. //对多解求平均
  583. double posx = 0;
  584. double posy = 0;
  585. double posz = 0;
  586. //选取误差最小的解
  587. double range = 999999.0;
  588. for (std::vector<std::shared_ptr<POS>>::iterator it_pos = udm_pos.begin();it_pos != udm_pos.end();++it_pos)
  589. {
  590. if((*it_pos)->pos_radius < range){
  591. range = (*it_pos)->pos_radius;
  592. posx = (*it_pos)->posx;
  593. posy = (*it_pos)->posy;
  594. posz = (*it_pos)->posz;
  595. }
  596. }
  597. p->posx = posx;
  598. p->posy = posy;
  599. p->posz = posz;
  600. if (!is_algo_first_location)
  601. {
  602. double dt = 0.0;
  603. if (fabs(GetDeltaT() - 0) < ZERO_PRECISION)
  604. {
  605. dt = 2;
  606. }else{
  607. dt = GetDeltaT();
  608. }
  609. p->cvx = fabs(p->posx - last_locate.x*map_scale)/dt;
  610. p->cvy = fabs(p->posx - last_locate.x*map_scale)/dt;
  611. }
  612. p->cx = p->posx/map_scale;
  613. p->cy = p->posy/map_scale;
  614. p->cz = p->posz/map_scale;
  615. }
  616. }
  617. //通过将定位结果与某个分站两天线的测距方向进行比对,如果有异常,则抛弃,都符合则保留
  618. if(position_data_filter_by_direction(p,tmp_dists_locate)){
  619. return 9;
  620. }
  621. //求完解,对解做加速度检验处理
  622. //if (!CheckSolutionTof(p))
  623. {
  624. //通过加速度被抛弃,利用参考数据来做圆
  625. //return 10;
  626. }
  627. //optimization_pos(p);
  628. //最后保存
  629. if (CheckPositionValid(p))
  630. {
  631. SaveCardAlgoData(p);
  632. }
  633. return 0;
  634. }
  635. int Card::algo_tof_2d(int cnt)
  636. {
  637. //二维定位
  638. //map<unsigned long long, std::shared_ptr<_coordinate>> tmp_dists_locate;
  639. //AssembleDistData(tmp_dists_locate);
  640. //for (map<unsigned long long, std::shared_ptr<_coordinate>>::iterator it = tmp_dists_locate.begin();it != tmp_dists_locate.end();++it)
  641. //{
  642. // x(i) = it->second->x / map_scale;
  643. // y(i) = it->second->y / map_scale;
  644. // d(i) = it->second->d;
  645. // i++;
  646. // if(i > 2)
  647. // break;
  648. //}
  649. //
  650. //h = 0;
  651. //for (int i = 0;i < 3;i++)
  652. //{
  653. // pos(i,0) = x(i);
  654. // pos(i,1) = y(i);
  655. // d(i) = sqrt(pow(d(0),2) - pow(h,2));
  656. //}
  657. //Eigen::Matrix2d A(2,2);
  658. //Eigen::Matrix2d B(2,2);
  659. //
  660. //for (int i = 0; i < 2;i++)
  661. //{
  662. // for (int j = 0; j < 2;j++)
  663. // {
  664. // temp = pos(i+1,j) - pos(i,j);
  665. // A(i,j) = temp;
  666. // }
  667. //}
  668. //for (int i = 0;i < 2;i++)
  669. //{
  670. // temp = pow(pos(i+1,0),2);
  671. // temp += pow(pos(i+1,1),2);
  672. // temp -= pow(pos(i,0),2);
  673. // temp -= pow(pos(i,1),2);
  674. // temp -= (pow(d(i+1,i+1),2) - pow(d(i,1),2));
  675. // temp /=2;
  676. // B(i,0) = temp;
  677. //}
  678. ////行列式
  679. ////double det = A.determinant();
  680. ////Eigen::Vector2d C;
  681. //Eigen::Matrix2d C;
  682. //C = A.inverse()*B;
  683. //this->x = C(0);
  684. //this->y = C(1);
  685. //逆矩阵
  686. //A.inverse();
  687. //转置
  688. //A.transpose();
  689. map<unsigned long long, std::shared_ptr<_coordinate>> tmp_dists_locate;
  690. AssembleDistData(tmp_dists_locate);
  691. map<unsigned long long, std::shared_ptr<_coordinate>>::iterator it = tmp_dists_locate.begin();
  692. time_stamp_cal = it->second->t;
  693. std::shared_ptr<ReceiveDataTofVector> pRdtv = std::make_shared<ReceiveDataTofVector>();
  694. for (auto it = tmp_dists_locate.begin();it != tmp_dists_locate.end();++it)
  695. {
  696. std::shared_ptr<ReceiveDataTof> rdt = std::make_shared<ReceiveDataTof>();
  697. rdt->x = it->second->x;
  698. rdt->y = it->second->y;
  699. rdt->z = it->second->z;
  700. rdt->reader_id = it->second->reader_id;
  701. rdt->distance = it->second->d;
  702. //rdt->distance = sqrt(pow(it->second->d,2)-pow(1.5,2));
  703. rdt->antenna_id = it->second->antenna_id;
  704. pRdtv->push_back(rdt);
  705. }
  706. std::shared_ptr<algorithm::POS> sp_pos = LocateAlgorithm::tof_locate_2d(pRdtv);
  707. sp_pos->cx = sp_pos->posx*map_scale;
  708. sp_pos->cy = sp_pos->posy*map_scale;
  709. //求完解,对解做加速度检验处理
  710. //if (!CheckSolutionTof(sp_pos))
  711. //{
  712. // //通过加速度被抛弃
  713. // return 10;
  714. //}
  715. //最后保存
  716. if (CheckPositionValid(sp_pos))
  717. {
  718. SaveCardAlgoData(sp_pos);
  719. }
  720. return 0;
  721. }
  722. int Card::algo_tof_3d(int cnt)
  723. {
  724. //三维定位
  725. /*TRACE(_T("data counts : %d \r\n"),cnt);
  726. if (cnt < 3)
  727. {
  728. return 1;
  729. }
  730. double distances[4];
  731. map<unsigned long long, std::shared_ptr<_coordinate>> tmp_dists_locate;
  732. AssembleDistData(tmp_dists_locate);
  733. int i = 0;
  734. for (map<unsigned long long, std::shared_ptr<_coordinate>>::iterator it = tmp_dists_locate.begin();it != tmp_dists_locate.end();++it)
  735. {
  736. distances[i] = it->second->d;
  737. i++;
  738. }
  739. Point3 pt3,pt3_1;
  740. Mat mat(4,1,distances);
  741. set_anchors(tmp_dists_locate);
  742. if (is_anchor_changed)
  743. {
  744. pt3 = cal_location3->initPos(mat);
  745. }
  746. else
  747. {
  748. pt3 = cal_location3->estiPos(mat);
  749. }
  750. pt3_1 = cal_location3->getError();
  751. x = pt3.x;
  752. y = pt3.y;
  753. z = pt3.z;*/
  754. return 0;
  755. }
  756. /*
  757. * TDOA算法
  758. *
  759. * param
  760. * cnt 数据条数
  761. *
  762. * return
  763. * 无
  764. *
  765. */
  766. void Card::algo_tdoa(int cnt)
  767. {
  768. LOCATION_SYSTEM_BRANCH(LOCATION_SYSTEM_BRANCH_140);
  769. GetLocalTime(&m_afmData.st);
  770. if(cnt < 2){
  771. LOCATION_SYSTEM_BRANCH(LOCATION_SYSTEM_BRANCH_141);
  772. m_afmData.bStatus = true;
  773. m_afmData.strCardId = this->card_id;
  774. m_afmData.nCardStamp = _dists.front().cardstamp;
  775. m_afmData.nType = ALGO_FAILED_CONDITION_15;
  776. ALGORITHM_FAILED(ALGO_FAILED_CONDITION_15);
  777. return;
  778. }
  779. //主要处理当相同卡的时间戳的数据中存在同步序号大于5的情况,如果有大于5的数据则丢弃此数据
  780. int k = 0;
  781. int dst = 0;
  782. int st = 0;
  783. bool bRet = false;
  784. mp_dists_locate.clear();
  785. for(DistMap::iterator it = _dists.front().distmap.begin(); it != _dists.front().distmap.end(); ++it,k++){
  786. if(k==0){
  787. st = it->second->sync_num;
  788. }else{
  789. dst = abs(st - it->second->sync_num);
  790. if(dst >= 5){
  791. LOCATION_SYSTEM_BRANCH(LOCATION_SYSTEM_BRANCH_142);
  792. bRet = true;
  793. //此处需要输出计数
  794. break;
  795. }
  796. }
  797. mp_dists_locate.insert(make_pair(it->second->tt,it->second));
  798. }
  799. if(bRet){
  800. LOCATION_SYSTEM_BRANCH(LOCATION_SYSTEM_BRANCH_143);
  801. this->x = this->last_locate.x;
  802. this->y = this->last_locate.y;
  803. this->z = 0;
  804. //this->z = INVALID_COORDINATE;
  805. m_afmData.bStatus = true;
  806. m_afmData.strCardId = this->card_id;
  807. m_afmData.nCardStamp = _dists.front().cardstamp;
  808. m_afmData.nType = ALGO_FAILED_CONDITION_2;
  809. ALGORITHM_FAILED(ALGO_FAILED_CONDITION_2);
  810. return;
  811. }
  812. std::shared_ptr<ReceiveDataMap> pRdm(new ReceiveDataMap);
  813. pRdm->clear();
  814. bRet = false;
  815. int maxSyncTimes = 0;
  816. //保存加速度当前状态和上次状态
  817. int acce_state = 0;
  818. int acce_state_last = 0;
  819. int ins_direction = 0;
  820. int card_time_stamp = 0;
  821. map<unsigned long long,std::shared_ptr<_coordinate>>::iterator it_mpdl = mp_dists_locate.begin();
  822. int i = 0;
  823. for(;it_mpdl!=mp_dists_locate.end();++it_mpdl){
  824. if(i==0){
  825. card_time_stamp = it_mpdl->second->t;
  826. maxSyncTimes = it_mpdl->second->sync_num;
  827. acce_state = it_mpdl->second->acce_state;
  828. acce_state_last = it_mpdl->second->acce_state_last;
  829. ins_direction = it_mpdl->second->ins_direction;
  830. }
  831. else{
  832. if(maxSyncTimes < it_mpdl->second->sync_num){
  833. maxSyncTimes = it_mpdl->second->sync_num;
  834. acce_state = it_mpdl->second->acce_state;
  835. acce_state_last = it_mpdl->second->acce_state_last;
  836. ins_direction = it_mpdl->second->ins_direction;
  837. }
  838. }
  839. ReceiveDataMap::iterator prdm_it = pRdm->find(it_mpdl->second->tt);
  840. if(prdm_it == pRdm->end()){
  841. if(it_mpdl->second->tt == LLONG_MAX ){
  842. //如果同步时间戳存在异常值,则不走算法定位,直接返回上一次结果值
  843. bRet = true;
  844. //此处需要输出计数
  845. LOCATION_SYSTEM_BRANCH(LOCATION_SYSTEM_BRANCH_144);
  846. break;
  847. }
  848. //保存信息用于定位
  849. std::shared_ptr<ReceiveData> prd(new ReceiveData());
  850. prd->reader_id = it_mpdl->second->reader_id;
  851. prd->antenna_id = it_mpdl->second->antenna_id;
  852. prd->rec_time_stamp = it_mpdl->second->tt;
  853. prd->x = it_mpdl->second->x*this->map_scale;
  854. prd->y = it_mpdl->second->y*this->map_scale;
  855. prd->z = it_mpdl->second->z*this->map_scale;
  856. prd->special = it_mpdl->second->special;
  857. pRdm->insert(make_pair(prd->rec_time_stamp,prd));
  858. }
  859. i++;
  860. }
  861. //存在异常值,直接返回上一次结果值
  862. if(bRet){
  863. LOCATION_SYSTEM_BRANCH(LOCATION_SYSTEM_BRANCH_145);
  864. pRdm->clear();
  865. this->x = this->last_locate.x;
  866. this->y = this->last_locate.y;
  867. this->z = 0;
  868. //this->z = INVALID_COORDINATE;
  869. m_afmData.bStatus = true;
  870. m_afmData.strCardId = this->card_id;
  871. m_afmData.nCardStamp = it_mpdl->first;
  872. m_afmData.nType = ALGO_FAILED_CONDITION_3;
  873. ALGORITHM_FAILED(ALGO_FAILED_CONDITION_3);
  874. return;
  875. }
  876. //当卡需要新定位的计数序列号大于上一次成功定位的序列号,则此次不定位,避免定位结果的回退
  877. if (this->time_stamp_cal_last > this->time_stamp_cal)
  878. {
  879. if ((this->time_stamp_cal - this->time_stamp_last) < MAX_SYNCTIME_DELAY_NUM)//120
  880. {
  881. //满足此条件,表名标示卡的计数序号走完了65535的周期
  882. }
  883. else{
  884. this->x = this->last_locate.x;
  885. this->y = this->last_locate.y;
  886. //this->z = INVALID_COORDINATE;
  887. this->z = 0;
  888. m_afmData.bStatus = true;
  889. m_afmData.strCardId = this->card_id;
  890. m_afmData.nCardStamp = this->time_stamp_cal;
  891. m_afmData.nType = ALGO_FAILED_CONDITION_4;
  892. ALGORITHM_FAILED(ALGO_FAILED_CONDITION_4);
  893. //此处需要输出计数
  894. return;
  895. }
  896. }
  897. this->time_stamp_cal_last = this->time_stamp_cal;
  898. int nCount = 0;
  899. //std::unique_ptr<POS> p = nullptr;
  900. std::shared_ptr<POS> p = nullptr;
  901. bool bOutput = false;
  902. this->z = 0;
  903. i = 0;
  904. if(pTdoaReaderPathMap->size() > 0 && pRdm->size() > 1){
  905. LOCATION_SYSTEM_BRANCH(LOCATION_SYSTEM_BRANCH_147);
  906. this->m_nCalcSyncNum = maxSyncTimes;
  907. p = LocateAlgorithm::Pos(pRdm,pTdoaReaderPathMap);
  908. this->origin_locate.x = p->posx / (this->map_scale*1.0);
  909. this->origin_locate.y = p->posy / (this->map_scale*1.0);
  910. this->origin_locate.z = p->posz / (this->map_scale*1.0);
  911. //校验机会3次,3次内跳回去就丢弃此次计算结果
  912. if (p->posx != INVALID_COORDINATE && p->posy != INVALID_COORDINATE)
  913. {
  914. int nDirection = 0;
  915. if (!algo_is_same_direction(this->origin_locate.x,this->origin_locate.y,0))
  916. {
  917. this->x = this->last_locate.x;
  918. this->y = this->last_locate.y;
  919. return;
  920. }
  921. }
  922. double interval_time = this->p_reader->reader_interval_time;
  923. double deltaT = 0.0;
  924. double cvx = 0;
  925. double cvy = 0;
  926. double cv = 0;
  927. double cx = 0;
  928. double cy = 0;
  929. double cz = 0;
  930. int nSign = 1;
  931. sync_data sdNew;
  932. sync_data sd;
  933. cx = p->posx / (this->map_scale*1.0);
  934. cy = p->posy / (this->map_scale*1.0);
  935. cz = p->posz / (this->map_scale*1.0);
  936. #ifdef ALGORITHM_TYPE_INS
  937. int acce_direction = 0; //加速度计速度方向 0-静止,1-前进,-1-后退
  938. int uwb_direction = 0; //uwb速度方向,同上
  939. bool bUseKalman = false;
  940. bool bOriginLocate = false; //原始定位是否成功
  941. bool bDirectReturn = false; //是否直接返回本次定位结果
  942. #endif
  943. if(p->posx != INVALID_COORDINATE && p->posy != INVALID_COORDINATE){
  944. LOCATION_SYSTEM_BRANCH(LOCATION_SYSTEM_BRANCH_148);
  945. #ifdef ALGORITHM_TYPE_INS
  946. bOriginLocate = true; //原始定位成功
  947. #endif
  948. //定位成功
  949. if(this->m_nLastLocateT == 0){
  950. LOCATION_SYSTEM_BRANCH(LOCATION_SYSTEM_BRANCH_149);
  951. sdNew.sync_num = maxSyncTimes;
  952. sdNew.x = cx;
  953. sdNew.y = cy;
  954. sdNew.vx = 0;
  955. sdNew.vy = 0;
  956. sdNew.update = false;
  957. m_syncNumList.push_back(sdNew);
  958. #ifdef ALGORITHM_TYPE_INS
  959. uwb_direction = 1;
  960. #endif
  961. }
  962. else{
  963. LOCATION_SYSTEM_BRANCH(LOCATION_SYSTEM_BRANCH_150);
  964. //现在的关于同步序号的处理是这样的:
  965. //如果定位成功,就把这次定位成功的同步数据:同步序号,坐标;x,y方向的速度,扔到一个队列里,
  966. //后来定位成功的就会先根据同步序号差用加速度抛一次;
  967. //抛不掉,就用队列里的同步数据(从后往前找),找到第一个与当前同步序号相差大于5的同步数据来进行第二次计算速度以及加速度,
  968. //如果加速度大于5,就不要此次的定位数据,
  969. //如果通过加速度判断就将队列中从头开始到此同步数据的所有元素都丢弃,并插入新的此次同步数据
  970. if (this->b_long_interval)
  971. {
  972. LOCATION_SYSTEM_BRANCH(LOCATION_SYSTEM_BRANCH_151);
  973. //此段代码用于将上一次定位是根据两个时间差是个很大值而定位出的结果
  974. //当后续定位时就和最近的定位结果进行比较
  975. //例如:当上一次同步序号是14321,它定位时比较的同步序号是14200,时间差大于20多秒
  976. //这时我们就将b_long_interval置为true
  977. //当本次定位,同步序号是14326,,这时就需要根据最近的14321进行判断
  978. list<sync_data>::reverse_iterator it = m_syncNumList.rbegin();
  979. sync_data sdl = *it;
  980. //以下计算deltaT还需要考虑卡的同步序号轮回的情况。
  981. if (maxSyncTimes > it->sync_num)
  982. {
  983. LOCATION_SYSTEM_BRANCH(LOCATION_SYSTEM_BRANCH_152);
  984. deltaT = (maxSyncTimes - sdl.sync_num)*interval_time;
  985. }
  986. else
  987. {
  988. deltaT = (maxSyncTimes + 65536 - sdl.sync_num)*interval_time;
  989. }
  990. if (deltaT < 10 && deltaT > 0)
  991. {
  992. LOCATION_SYSTEM_BRANCH(LOCATION_SYSTEM_BRANCH_155);
  993. this->b_long_interval = false;
  994. }
  995. //避免同一个同步序号下存在多个不同卡序号
  996. if (deltaT < 1E-2)
  997. {
  998. LOCATION_SYSTEM_BRANCH(LOCATION_SYSTEM_BRANCH_156);
  999. deltaT = 0.2;
  1000. }
  1001. cvx = (cx - sdl.x)*this->map_scale/deltaT;
  1002. cvy = (cy - sdl.y)*this->map_scale/deltaT;
  1003. double avx = (cvx - sdl.vx) / deltaT;
  1004. double avy = (cvy - sdl.vy) / deltaT;
  1005. double av = sqrt(pow(avx,2) + pow(avy,2));
  1006. //车卡的加速度
  1007. switch(this->card_type){
  1008. case CT_PERSON:
  1009. LOCATION_SYSTEM_BRANCH(LOCATION_SYSTEM_BRANCH_157);
  1010. if(av > PERSON_ACCELERATE_THRESHOLD){
  1011. LOCATION_SYSTEM_BRANCH(LOCATION_SYSTEM_BRANCH_158);
  1012. this->x = this->last_locate.x;
  1013. this->y = this->last_locate.y;
  1014. this->b_long_interval = false;
  1015. m_afmData.bStatus = true;
  1016. m_afmData.strCardId = this->card_id;
  1017. m_afmData.nType = ALGO_FAILED_CONDITION_5;
  1018. ALGORITHM_FAILED(ALGO_FAILED_CONDITION_5);
  1019. return;
  1020. }
  1021. break;
  1022. case CT_VEHICLE:
  1023. LOCATION_SYSTEM_BRANCH(LOCATION_SYSTEM_BRANCH_159);
  1024. if(av > VECHILE_ACCELERATE_THRESHOLD){
  1025. LOCATION_SYSTEM_BRANCH(LOCATION_SYSTEM_BRANCH_160);
  1026. //保留上次结果
  1027. this->x = this->last_locate.x;
  1028. this->y = this->last_locate.y;
  1029. this->b_long_interval = false;
  1030. m_afmData.bStatus = true;
  1031. m_afmData.strCardId = this->card_id;
  1032. m_afmData.nType = ALGO_FAILED_CONDITION_6;
  1033. ALGORITHM_FAILED(ALGO_FAILED_CONDITION_6);
  1034. return;
  1035. }
  1036. break;
  1037. }
  1038. deltaT = 0;
  1039. cvx = cvy = 0;
  1040. }
  1041. //从队列尾部开始查找,找到第一个同步序号与当前计算卡的同步序号相差5个以上的数据
  1042. list<sync_data>::reverse_iterator it;
  1043. bool bOverflow = false;
  1044. for(it = m_syncNumList.rbegin();it!=m_syncNumList.rend();it++){
  1045. if(maxSyncTimes - it->sync_num >= 5){
  1046. LOCATION_SYSTEM_BRANCH(LOCATION_SYSTEM_BRANCH_161);
  1047. sd = *it;
  1048. break;
  1049. }
  1050. else{
  1051. if(maxSyncTimes - it->sync_num < 0 && maxSyncTimes < 100){
  1052. LOCATION_SYSTEM_BRANCH(LOCATION_SYSTEM_BRANCH_162);
  1053. //如果最新同步号小于列表中的同步号则
  1054. if(maxSyncTimes + 65536 - it->sync_num >=5 ){//5
  1055. LOCATION_SYSTEM_BRANCH(LOCATION_SYSTEM_BRANCH_163);
  1056. bOverflow = true;
  1057. sd = *it;
  1058. }
  1059. }else{
  1060. continue;
  1061. }
  1062. }
  1063. }
  1064. //根据溢出条件来计算deltaT
  1065. if(bOverflow){
  1066. LOCATION_SYSTEM_BRANCH(LOCATION_SYSTEM_BRANCH_164);
  1067. deltaT = (maxSyncTimes + 65536 - sd.sync_num)*interval_time;
  1068. }else{
  1069. LOCATION_SYSTEM_BRANCH(LOCATION_SYSTEM_BRANCH_165);
  1070. deltaT = (maxSyncTimes - sd.sync_num)*interval_time;
  1071. }
  1072. //速度正负的判断:以x轴,y轴正向运动为正
  1073. //如果x相等,则y2 - y1 > 0为正
  1074. //其他情况,则x2 - x1 > 0 为正
  1075. if(cx == sd.x){
  1076. if(cy > sd.y){
  1077. nSign = 1;
  1078. }else{
  1079. nSign = -1;
  1080. }
  1081. }else{
  1082. if(cx > sd.x){
  1083. nSign = 1;
  1084. }else{
  1085. nSign = -1;
  1086. }
  1087. }
  1088. #ifdef ALGORITHM_TYPE_INS
  1089. uwb_direction = (nSign == 1)?1:-1; //uwb形式判断不出静止或者怠速
  1090. #endif
  1091. //使用间隔来修正速度
  1092. if(deltaT - 1.0 >= 0){
  1093. LOCATION_SYSTEM_BRANCH(LOCATION_SYSTEM_BRANCH_166);
  1094. if (deltaT > 10)
  1095. {
  1096. LOCATION_SYSTEM_BRANCH(LOCATION_SYSTEM_BRANCH_167);
  1097. this->b_long_interval = true;
  1098. }
  1099. //转为m/s
  1100. cvx = (cx - sd.x)*this->map_scale/deltaT;
  1101. cvy = (cy - sd.y)*this->map_scale/deltaT;
  1102. cv = sqrt(pow(cvx,2) + pow(cvy,2));
  1103. cv = cv*nSign;
  1104. double avx = (cvx - sd.vx) / deltaT;
  1105. double avy = (cvy - sd.vy) / deltaT;
  1106. double av = sqrt(pow(avx,2) + pow(avy,2));
  1107. //车卡的加速度
  1108. switch(this->card_type){
  1109. case CT_PERSON:
  1110. LOCATION_SYSTEM_BRANCH(LOCATION_SYSTEM_BRANCH_168);
  1111. if(av > PERSON_ACCELERATE_THRESHOLD){
  1112. LOCATION_SYSTEM_BRANCH(LOCATION_SYSTEM_BRANCH_169);
  1113. this->x = this->last_locate.x;
  1114. this->y = this->last_locate.y;
  1115. m_afmData.bStatus = true;
  1116. m_afmData.strCardId = this->card_id;
  1117. m_afmData.nType = ALGO_FAILED_CONDITION_5;
  1118. ALGORITHM_FAILED(ALGO_FAILED_CONDITION_5);
  1119. return;
  1120. }
  1121. break;
  1122. case CT_VEHICLE:
  1123. LOCATION_SYSTEM_BRANCH(LOCATION_SYSTEM_BRANCH_170);
  1124. if(av > VECHILE_ACCELERATE_THRESHOLD){
  1125. LOCATION_SYSTEM_BRANCH(LOCATION_SYSTEM_BRANCH_171);
  1126. //保留上次结果
  1127. this->x = this->last_locate.x;
  1128. this->y = this->last_locate.y;
  1129. m_afmData.bStatus = true;
  1130. m_afmData.strCardId = this->card_id;
  1131. m_afmData.nType = ALGO_FAILED_CONDITION_6;
  1132. ALGORITHM_FAILED(ALGO_FAILED_CONDITION_6);
  1133. return;
  1134. }
  1135. break;
  1136. }
  1137. this->last_locate.acceleration = av;
  1138. this->last_vx = cvx;
  1139. this->last_vy = cvy;
  1140. cv = cv*3.6;
  1141. LOCATION_SYSTEM_BRANCH(LOCATION_SYSTEM_BRANCH_172);
  1142. //速度的限制
  1143. if(fabs(cv) > MAX_VECHILE_SPEED){
  1144. LOCATION_SYSTEM_BRANCH(LOCATION_SYSTEM_BRANCH_173);
  1145. this->x = this->last_locate.x;
  1146. this->y = this->last_locate.y;
  1147. m_afmData.bStatus = true;
  1148. m_afmData.strCardId = this->card_id;
  1149. m_afmData.nType = ALGO_FAILED_CONDITION_7;
  1150. ALGORITHM_FAILED(ALGO_FAILED_CONDITION_7);
  1151. return;
  1152. }
  1153. //删除第一个元素到tmp(含)之间的所有元素
  1154. bool bStartDel = false;
  1155. for(list<sync_data>::reverse_iterator tmp = m_syncNumList.rbegin();tmp != m_syncNumList.rend();)
  1156. {
  1157. if(bStartDel){
  1158. tmp = list<sync_data>::reverse_iterator(m_syncNumList.erase((++tmp).base()));
  1159. }else{
  1160. if(*tmp == sd){
  1161. bStartDel = true;
  1162. }
  1163. ++tmp;
  1164. }
  1165. }
  1166. //更新值为当前值并插入队列
  1167. sdNew.sync_num = maxSyncTimes;
  1168. this->m_nSyncNumInList = sd.sync_num;
  1169. sdNew.vx = cvx;
  1170. sdNew.vy = cvy;
  1171. sdNew.update = true;
  1172. }else{
  1173. cv = this->origin_locate.v;
  1174. }
  1175. }
  1176. }
  1177. #ifdef ALGORITHM_TYPE_INS
  1178. else{
  1179. LOCATION_SYSTEM_BRANCH(LOCATION_SYSTEM_BRANCH_174);
  1180. uwb_direction = ins_direction;
  1181. }
  1182. #endif
  1183. #ifdef ALGORITHM_TYPE_INS
  1184. bRet = false;
  1185. //0,1,2分别表示静止(含怠速),运动,运动中刹车
  1186. switch (acce_state)
  1187. {
  1188. case 0:
  1189. LOCATION_SYSTEM_BRANCH(LOCATION_SYSTEM_BRANCH_175);
  1190. acce_direction = 0;
  1191. if(acce_state_last!=0){
  1192. LOCATION_SYSTEM_BRANCH(LOCATION_SYSTEM_BRANCH_176);
  1193. //说明从其他状态变为静止(怠速)状态
  1194. //返回这次定位结果
  1195. if (bOriginLocate)
  1196. {
  1197. LOCATION_SYSTEM_BRANCH(LOCATION_SYSTEM_BRANCH_177);
  1198. bDirectReturn = true; //直接返回本次定位结果
  1199. }
  1200. }else{
  1201. //返回上一次定位结果
  1202. LOCATION_SYSTEM_BRANCH(LOCATION_SYSTEM_BRANCH_178);
  1203. bDirectReturn = false;
  1204. }
  1205. bUseKalman = false;
  1206. //bRet = true;
  1207. break;
  1208. case 1://需要判断状态是否发生了变化
  1209. LOCATION_SYSTEM_BRANCH(LOCATION_SYSTEM_BRANCH_179);
  1210. if (acce_state_last == ACCELERATE_INIT_STATE)
  1211. {
  1212. LOCATION_SYSTEM_BRANCH(LOCATION_SYSTEM_BRANCH_180);
  1213. //第一次获得卡的状态,需要确定首次方向
  1214. acce_direction = uwb_direction; //将UWB定出的速度方向赋值给它
  1215. }else{
  1216. LOCATION_SYSTEM_BRANCH(LOCATION_SYSTEM_BRANCH_181);
  1217. acce_direction = acce_state_last;
  1218. }
  1219. //bRet = false;
  1220. break;
  1221. case 2:
  1222. case 3:
  1223. LOCATION_SYSTEM_BRANCH(LOCATION_SYSTEM_BRANCH_182);
  1224. acce_direction = 0; //此处将速度方向设为静止,也就是0
  1225. //bRet = false;
  1226. break;
  1227. }
  1228. if (acce_state!=acce_last_state)
  1229. {
  1230. // 如果状态发生了改变,则加速度计的权重需要重置
  1231. LOCATION_SYSTEM_BRANCH(LOCATION_SYSTEM_BRANCH_183);
  1232. this->ins_weight = INS_WEIGHT;
  1233. }
  1234. double cweight = 0;
  1235. if(!bRet){
  1236. LOCATION_SYSTEM_BRANCH(LOCATION_SYSTEM_BRANCH_184);
  1237. cweight = this->ins_weight * acce_direction + this->uwb_weight*uwb_direction;
  1238. //如果计算出的权重在合适范围内,就逐渐降低惯导的权限
  1239. //如果计算出的权重超过范围,则重置惯导的权重为90%
  1240. if(cweight>=INS_WEIGHT*-1&&cweight<=INS_WEIGHT){
  1241. LOCATION_SYSTEM_BRANCH(LOCATION_SYSTEM_BRANCH_185);
  1242. cweight = abs(cweight);
  1243. }else{
  1244. LOCATION_SYSTEM_BRANCH(LOCATION_SYSTEM_BRANCH_186);
  1245. cweight = INS_WEIGHT;
  1246. }
  1247. if(cweight*uwb_direction < 0){
  1248. //惯导和uwb定位方向不一致,定位失败
  1249. //增加一次判断,与上一次的进行比对,如果和上一次的一致,则送进卡尔曼滤波
  1250. LOCATION_SYSTEM_BRANCH(LOCATION_SYSTEM_BRANCH_187);
  1251. if(cweight*acce_state_last > 0){
  1252. LOCATION_SYSTEM_BRANCH(LOCATION_SYSTEM_BRANCH_188);
  1253. bUseKalman = true;
  1254. }else{
  1255. LOCATION_SYSTEM_BRANCH(LOCATION_SYSTEM_BRANCH_189);
  1256. bUseKalman = false;
  1257. }
  1258. }else{
  1259. //惯导和uwb定位方向一致,定位成功
  1260. //送进卡尔曼滤波
  1261. LOCATION_SYSTEM_BRANCH(LOCATION_SYSTEM_BRANCH_190);
  1262. bUseKalman = true;
  1263. }
  1264. }
  1265. if (abs(cweight) > 0)
  1266. {
  1267. LOCATION_SYSTEM_BRANCH(LOCATION_SYSTEM_BRANCH_191);
  1268. ins_direction = cweight/abs(cweight) > 0?1:-1;
  1269. }else{
  1270. //如果加速度计的状态权重为零
  1271. LOCATION_SYSTEM_BRANCH(LOCATION_SYSTEM_BRANCH_192);
  1272. ins_direction = uwb_direction;
  1273. }
  1274. this->ins_weight = cweight;
  1275. this->ins_direction = ins_direction;
  1276. this->acce_last_state = acce_last_state;
  1277. //this->accelerate_state_last = this->acce_cur_state = acce_state;
  1278. #endif
  1279. this->accelerate_state_last = this->acce_cur_state = acce_state;
  1280. if(m_nFilterType == FILTER_KALMAN){
  1281. LOCATION_SYSTEM_BRANCH(LOCATION_SYSTEM_BRANCH_193);
  1282. #ifdef ALGORITHM_TYPE_INS
  1283. if(bUseKalman){
  1284. #endif
  1285. LOCATION_SYSTEM_BRANCH(LOCATION_SYSTEM_BRANCH_194);
  1286. //也需要考虑轮回
  1287. //double kalman_detal_t = (maxSyncTimes - this->last_locate.st)*interval_time;
  1288. double kalman_detal_t = 0;
  1289. if (maxSyncTimes - this->last_locate.sync_num < 0 && maxSyncTimes < 100)
  1290. {
  1291. kalman_detal_t = (maxSyncTimes + 65535 - this->last_locate.sync_num)*interval_time;
  1292. }else{
  1293. kalman_detal_t = (maxSyncTimes - this->last_locate.sync_num)*interval_time;
  1294. LOCATION_SYSTEM_BRANCH(LOCATION_SYSTEM_BRANCH_195);
  1295. }
  1296. //通过卡尔曼滤波处理
  1297. if(p->posx == INVALID_COORDINATE && p->posy == INVALID_COORDINATE){
  1298. LOCATION_SYSTEM_BRANCH(LOCATION_SYSTEM_BRANCH_196);
  1299. if(this->m_pKalmanFilter->m_nCounts < 3 || this->m_pKalmanFilter->m_pCar->P(0,0) > 2 || kalman_detal_t > 3){
  1300. //P(0,0):连续时间(大于2s)都定位失败
  1301. //deltaT>3:距离上次成功定位时间间隔为3s
  1302. LOCATION_SYSTEM_BRANCH(LOCATION_SYSTEM_BRANCH_197);
  1303. this->x = this->last_locate.x;
  1304. this->y = this->last_locate.y;
  1305. this->z = 0;
  1306. m_afmData.bStatus = true;
  1307. m_afmData.strCardId = this->card_id;
  1308. this->m_pKalmanFilter->m_bFlag = false;
  1309. m_afmData.nType = ALGO_FAILED_CONDITION_8;
  1310. ALGORITHM_FAILED(ALGO_FAILED_CONDITION_8);
  1311. return;
  1312. }
  1313. if(this->m_pKalmanFilter->m_nCounts >= 3){
  1314. //只有三次以上才允许使用kalman滤波以下的函数
  1315. LOCATION_SYSTEM_BRANCH(LOCATION_SYSTEM_BRANCH_198);
  1316. this->m_pKalmanFilter->Predict(kalman_detal_t);
  1317. this->z = 0;
  1318. sdNew.update = true;
  1319. }
  1320. }else{
  1321. LOCATION_SYSTEM_BRANCH(LOCATION_SYSTEM_BRANCH_199);
  1322. this->m_pKalmanFilter->m_bFlag = true;
  1323. this->m_pKalmanFilter->m_nCounts++;
  1324. this->m_pKalmanFilter->m_pCar->z(0,0) = cx * this->map_scale;
  1325. this->m_pKalmanFilter->m_pCar->z(1,0) = cvx;
  1326. this->m_pKalmanFilter->m_pCar->z(2,0) = cy * this->map_scale;
  1327. this->m_pKalmanFilter->m_pCar->z(3,0) = cvy;
  1328. if(this->m_pKalmanFilter->m_nCounts == 1){
  1329. //第一次直接赋值
  1330. LOCATION_SYSTEM_BRANCH(LOCATION_SYSTEM_BRANCH_200);
  1331. this->m_pKalmanFilter->m_pCar->x = this->m_pKalmanFilter->m_pCar->z;
  1332. }
  1333. if(this->m_pKalmanFilter->m_nCounts == 2){
  1334. //两次处理
  1335. LOCATION_SYSTEM_BRANCH(LOCATION_SYSTEM_BRANCH_201);
  1336. this->m_pKalmanFilter->m_pCar->z(1, 0) = (this->m_pKalmanFilter->m_pCar->z(0, 0) - this->m_pKalmanFilter->m_pCar->x(0, 0))/deltaT;
  1337. this->m_pKalmanFilter->m_pCar->z(3, 0) = (this->m_pKalmanFilter->m_pCar->z(2, 0) - this->m_pKalmanFilter->m_pCar->x(2, 0))/deltaT;
  1338. this->m_pKalmanFilter->m_pCar->x = this->m_pKalmanFilter->m_pCar->z;
  1339. }
  1340. if(this->m_pKalmanFilter->m_nCounts >= 3){
  1341. LOCATION_SYSTEM_BRANCH(LOCATION_SYSTEM_BRANCH_202);
  1342. //只有三次以上才允许使用kalman滤波以下的函数
  1343. //this->m_pKalmanFilter->Predict_Correct(deltaT);
  1344. this->m_pKalmanFilter->Predict_Correct(kalman_detal_t);
  1345. sdNew.update = true;
  1346. if(deltaT!=0){
  1347. LOCATION_SYSTEM_BRANCH(LOCATION_SYSTEM_BRANCH_203);
  1348. this->m_pKalmanFilter->m_pCar->x(1,0) = (this->m_pKalmanFilter->m_pCar->x(0,0) - sd.x*this->map_scale)/deltaT;
  1349. this->m_pKalmanFilter->m_pCar->x(3,0) = (this->m_pKalmanFilter->m_pCar->x(2,0) - sd.y*this->map_scale)/deltaT;
  1350. }
  1351. }
  1352. }
  1353. if(p->nFirstReader == -1 && p->nSecondReader == -1){
  1354. LOCATION_SYSTEM_BRANCH(LOCATION_SYSTEM_BRANCH_204);
  1355. }
  1356. //增加地图集的判定,判断定位结果是否在地图集上
  1357. //如果不在地图集上,需要再次定位
  1358. //需要带出定位结果的分站信息,
  1359. //利用地图集中分站信息再次定位
  1360. //std::shared_ptr<POS> kalman_p(new POS());
  1361. std::shared_ptr<POS> kalman_p = std::make_shared<POS>();
  1362. kalman_p->nFirstReader = p->nFirstReader;
  1363. kalman_p->nSecondReader = p->nSecondReader;
  1364. kalman_p->posx = this->m_pKalmanFilter->m_pCar->x(0,0);//* this->map_scale
  1365. kalman_p->posy = this->m_pKalmanFilter->m_pCar->x(2,0);//* this->map_scale
  1366. kalman_p->pos_radius = p->pos_radius;
  1367. if(!LocateAlgorithm::IsOnMap(kalman_p,pTdoaReaderPathMap)){
  1368. //再一次定位到地图集上
  1369. LOCATION_SYSTEM_BRANCH(LOCATION_SYSTEM_BRANCH_205);
  1370. std::shared_ptr<POS> cp = LocateAlgorithm::Pos(kalman_p,pTdoaReaderPathMap);
  1371. if(cp != nullptr){
  1372. LOCATION_SYSTEM_BRANCH(LOCATION_SYSTEM_BRANCH_206);
  1373. this->m_pKalmanFilter->m_pCar->x(0,0) = cp->posx;
  1374. this->m_pKalmanFilter->m_pCar->x(2,0) = cp->posy;
  1375. }
  1376. cp.reset();
  1377. }
  1378. //车辆上下行确定
  1379. //3个条件:起点(x1,y1),终点(x2,y2)
  1380. //1.x1==x2的情况下,y2>y1为下行
  1381. //2.y1==y2的情况下,x1>x2为下行
  1382. //3.x1>x2且y2>y1为下行
  1383. //其他情况为上行
  1384. if ((this->last_locate.x == this->x && this->y > this->last_locate.y)
  1385. ||(this->last_locate.x > this->x && this->y == this->last_locate.y)
  1386. ||(this->last_locate.x > this->x && this->y > this->last_locate.y))
  1387. {
  1388. this->m_nStream = DOWN_STREAM;
  1389. }
  1390. else
  1391. {
  1392. this->m_nStream = UP_STREAM;
  1393. }
  1394. this->x = this->m_pKalmanFilter->m_pCar->x(0,0) / this->map_scale;
  1395. this->y = this->m_pKalmanFilter->m_pCar->x(2,0) / this->map_scale;
  1396. nSign = 1;
  1397. if(this->m_pKalmanFilter->m_pCar->x(1,0) == 0){
  1398. if(this->m_pKalmanFilter->m_pCar->x(3,0)>0){
  1399. nSign = 1;
  1400. }
  1401. }else{
  1402. if(this->m_pKalmanFilter->m_pCar->x(1,0) > 0){
  1403. nSign = 1;
  1404. }else{
  1405. nSign = -1;
  1406. }
  1407. }
  1408. this->m_nMoveDirection = nSign;
  1409. algo_calc_offset();
  1410. //求里程
  1411. if (!this->is_first_location)
  1412. {
  1413. this->mileage = sqrt(pow(this->x - this->last_locate.x,2) + pow(this->y - this->last_locate.y,2))*this->map_scale/1000.0;
  1414. }
  1415. else{
  1416. this->is_first_location = false;
  1417. }
  1418. this->last_locate.x = this->x = this->m_pKalmanFilter->m_pCar->x(0,0) / this->map_scale;
  1419. this->last_locate.y = this->y = this->m_pKalmanFilter->m_pCar->x(2,0) / this->map_scale;
  1420. this->last_locate.z = this->z;
  1421. this->last_locate.sync_num = maxSyncTimes;
  1422. this->m_pKalmanFilter->m_pCar->t = this->m_nLastLocateT = maxSyncTimes;
  1423. //速度的计算采用求平均的方式
  1424. double speed = sqrt(pow(this->m_pKalmanFilter->m_pCar->x(1,0),2) + pow(this->m_pKalmanFilter->m_pCar->x(3,0),2));
  1425. speed *=3.6; //转为km/h
  1426. double av = (fabs(this->v) + fabs(speed))/2.0;
  1427. this->v = av*nSign;
  1428. //this->v = sqrt(pow(this->m_pKalmanFilter->m_pCar->x(1,0),2) + pow(this->m_pKalmanFilter->m_pCar->x(3,0),2)); //*this->map_scale
  1429. //this->v*=3.6; //转为km/h
  1430. cvx = this->m_pKalmanFilter->m_pCar->x(1,0);
  1431. cvy = this->m_pKalmanFilter->m_pCar->x(3,0);
  1432. this->last_locate.v = this->v;
  1433. this->origin_locate.v = cv;
  1434. ALGORITHM_FAILED(ALGO_LOC_SUCCESSED);
  1435. LOCATION_SYSTEM_BRANCH(LOCATION_SYSTEM_BRANCH_207);
  1436. if(sdNew.update){
  1437. sdNew.sync_num = maxSyncTimes;
  1438. sdNew.x = this->x;
  1439. sdNew.y = this->y;
  1440. sdNew.vx = cvx;
  1441. sdNew.vy = cvy;
  1442. m_syncNumList.push_back(sdNew);
  1443. LOCATION_SYSTEM_BRANCH(LOCATION_SYSTEM_BRANCH_208);
  1444. }
  1445. #ifdef ALGORITHM_TYPE_INS
  1446. }else{
  1447. //这组数据的处理方法是:
  1448. //如果第一次成功定位,但状态是静止,就取第一次成功定位值(这是为了处理当第一次成功定位,但状态是静止的,此时取上一次定位值为零的问题),
  1449. //后续如果不管定位成功还是失败,只要状态是静止的,就输出上一次成功定位值,并更新同步序号。
  1450. //取上次结果
  1451. LOCATION_SYSTEM_BRANCH(LOCATION_SYSTEM_BRANCH_209);
  1452. if (bDirectReturn)
  1453. {
  1454. LOCATION_SYSTEM_BRANCH(LOCATION_SYSTEM_BRANCH_210);
  1455. if(this->map_scale > 0){
  1456. LOCATION_SYSTEM_BRANCH(LOCATION_SYSTEM_BRANCH_211);
  1457. this->last_locate.x = this->x = p->posx / (this->map_scale*1.0);
  1458. this->last_locate.y = this->y = p->posy / (this->map_scale*1.0);
  1459. this->last_locate.z = this->z = p->posz / (this->map_scale*1.0);
  1460. this->last_locate.v = this->v = cv;
  1461. }
  1462. }else{
  1463. LOCATION_SYSTEM_BRANCH(LOCATION_SYSTEM_BRANCH_212);
  1464. this->x = this->last_locate.x;
  1465. this->y = this->last_locate.y;
  1466. this->z = -5;//this->last_locate.z
  1467. this->v = this->last_locate.v;
  1468. }
  1469. this->m_nLastLocateT = this->m_nCalcSyncNum = this->last_locate.st = this->sync_num = maxSyncTimes;
  1470. if(sdNew.update){
  1471. LOCATION_SYSTEM_BRANCH(LOCATION_SYSTEM_BRANCH_213);
  1472. sdNew.sync_num = maxSyncTimes;
  1473. sdNew.x = this->x;
  1474. sdNew.y = this->y;
  1475. sdNew.vx = cvx;
  1476. sdNew.vy = cvy;
  1477. m_syncNumList.push_back(sdNew);
  1478. this->m_nSyncNumInList = maxSyncTimes;
  1479. }
  1480. this->is_deal_by_algo = true;
  1481. }
  1482. #endif
  1483. }else{
  1484. //TRACE(_T("no kalman . \n"));
  1485. //最新通过算法算出的结果
  1486. LOCATION_SYSTEM_BRANCH(LOCATION_SYSTEM_BRANCH_214);
  1487. if(p->posx == INVALID_COORDINATE || p->posy == INVALID_COORDINATE){
  1488. LOCATION_SYSTEM_BRANCH(LOCATION_SYSTEM_BRANCH_215);
  1489. this->x = this->last_locate.x;
  1490. this->y = this->last_locate.y;
  1491. this->z = this->last_locate.z;
  1492. }else{
  1493. LOCATION_SYSTEM_BRANCH(LOCATION_SYSTEM_BRANCH_216);
  1494. if(this->map_scale > 0){
  1495. LOCATION_SYSTEM_BRANCH(LOCATION_SYSTEM_BRANCH_216);
  1496. this->x = p->posx / (this->map_scale*1.0);
  1497. this->y = p->posy / (this->map_scale*1.0);
  1498. this->z = p->posz / (this->map_scale*1.0);
  1499. }
  1500. double speed = (fabs(this->v) + fabs(cv))/2;
  1501. this->v = speed*nSign;
  1502. this->last_locate.x = this->x;
  1503. this->last_locate.y = this->y;
  1504. this->last_locate.z = this->z;
  1505. this->last_locate.v = this->v;
  1506. this->m_nLastLocateT = this->last_locate.sync_num = maxSyncTimes;
  1507. }
  1508. this->a = 0;
  1509. }
  1510. }
  1511. LOCATION_SYSTEM_BRANCH(LOCATION_SYSTEM_BRANCH_217);
  1512. pRdm->clear();
  1513. }
  1514. /*
  1515. * tdoa一维定位
  1516. *
  1517. * param
  1518. * cnt 数据条数
  1519. *
  1520. * return
  1521. * 成功输出定位结果返回0,否则返回非零值
  1522. *
  1523. */
  1524. int Card::algo_tdoa_1d(int cnt)
  1525. {
  1526. int ret = 0;
  1527. //1.数据有效性判断
  1528. ret = CheckDistData(cnt);
  1529. if(ret){
  1530. return ret;
  1531. }
  1532. //2.组装数据
  1533. std::shared_ptr<ReceiveDataMap> pRdm = std::make_shared<ReceiveDataMap>();
  1534. pRdm->clear();
  1535. ret = AssembleDistData(pRdm);
  1536. if(ret){
  1537. return ret;
  1538. }
  1539. //3.算法定位
  1540. //保存结果解
  1541. std::shared_ptr<POS> pos = std::make_shared<POS>();
  1542. //保存所有可能解
  1543. std::vector<std::shared_ptr<POS>> udm_pos;
  1544. udm_pos.resize(0);
  1545. //根据分站数据计算所有可能解,并将所有可能解保存到udm_pos中
  1546. ret = LocateAlgorithm::CalcTdoaPosition(pRdm,pTdoaReaderPathMap,udm_pos);
  1547. bool bIsBack = false;
  1548. if (ret == 0)
  1549. {
  1550. //4.从多解中筛选出一个解,存在两种可能:
  1551. //a.可能无解返回非0,
  1552. //b.可能有解,但解不正确,比如解的位置在4727,-100,但选出的解是4727,-200
  1553. ret = ChooseOneSolution(pRdm, udm_pos, pos);
  1554. //无论正确与否,保存原始值
  1555. SaveOriginDataBeforeFilter(pos);
  1556. if (ret == 0)
  1557. {
  1558. //calibration_pos_data(pos);
  1559. //5.如果有解,则对唯一解做合法性验证
  1560. //主要验证条件为:加速度和速度
  1561. ret = CheckSolution(pos);
  1562. if (ret == 0)
  1563. {
  1564. //回退的判断应该建立在非怠速的情况下,如果怠速则不走回退的逻辑
  1565. //if (!IsIdleStatus(pos))
  1566. //{
  1567. // //对通过加速度验证的解进行是否回退的判断
  1568. // if (IsRebound(pos))
  1569. // {
  1570. // if (count_change_direction < MAX_REBOUND_COUNTS)
  1571. // {
  1572. // CalcPositionBySpeed(pos,last_locate.v);
  1573. // //if (pos->posx != INVALID_COORDINATE && pos->posy != INVALID_COORDINATE)
  1574. // if(CheckPositionValid(pos))
  1575. // {
  1576. // pos->cx = pos->posx / (map_scale*1.0);
  1577. // pos->cy = pos->posy / (map_scale*1.0);
  1578. // pos->update = true;
  1579. // pos->is_fit = true;
  1580. // count_change_direction++;
  1581. // }else{
  1582. // //这里可能需要加逻辑处理,避免一次回跳太多
  1583. // count_change_direction = 0;
  1584. // bIsBack = true;
  1585. // }
  1586. // }
  1587. // else
  1588. // {
  1589. // count_change_direction = 0;
  1590. // }
  1591. // }
  1592. //}
  1593. }else{
  1594. //加速度校验失败
  1595. //加速度或速度抛弃的结果
  1596. if (!IsRebound(pos))
  1597. {
  1598. //如果不是回退,说明了是往前大跳
  1599. //加速追
  1600. }else{
  1601. //说明是往回大跳
  1602. //减速走
  1603. bIsBack = true;
  1604. }
  1605. }
  1606. }else{
  1607. //选解失败
  1608. return 1;
  1609. }
  1610. }
  1611. if (acce_cur_state == ACCELERATE_STATE::STATE_ACCE_MOTION && !bIsBack)
  1612. {
  1613. //6.卡尔曼滤波,两种情况:
  1614. //a.解通过了合法性验证,b.无解,但在2s内
  1615. //则使用卡尔曼滤波进行滤波处理
  1616. if (FILTER_KALMAN == m_nFilterType)
  1617. {
  1618. ret = KalmanFilterProcess(pos);
  1619. }
  1620. }
  1621. bool bRet = LocateAlgorithm::IsOnMap(pos,pTdoaReaderPathMap);
  1622. if (bRet)
  1623. {
  1624. //7.数据保存
  1625. ret = SaveCardAlgoData(pos);
  1626. }
  1627. return ret;
  1628. }
  1629. /*
  1630. * tdoa二维定位
  1631. *
  1632. * param
  1633. * cnt 数据记录条数
  1634. *
  1635. * return
  1636. * 成功返回0,失败返回非零值
  1637. *
  1638. */
  1639. int Card::algo_tdoa_2d(int cnt)
  1640. {
  1641. int ret = 0;
  1642. //1.数据有效性判断
  1643. ret = CheckDistData(cnt);
  1644. if(ret) return ret;
  1645. //2.组装数据
  1646. std::shared_ptr<ReceiveDataMap> pRdm = std::make_shared<ReceiveDataMap>();
  1647. pRdm->clear();
  1648. ret = AssembleDistData(pRdm);
  1649. if(ret) return ret;
  1650. char chlog[MAX_LOG_LENGTH] = {0};
  1651. string log = "";
  1652. sprintf_s(chlog,"card_id: %s , ct: %d, size: %d,",card_id.c_str(),time_stamp_cal,pRdm->size());
  1653. log+=chlog;
  1654. ReceiveDataMap::iterator it_test = pRdm->begin();
  1655. ReceiveDataMap::iterator it_test_2 = it_test;
  1656. std::advance(it_test_2,1);
  1657. for (it_test_2;it_test_2 != pRdm->end();++it_test_2)
  1658. {
  1659. memset(chlog,0,20*sizeof(char));
  1660. unsigned long long dt = 0;
  1661. dt = it_test_2->second->rec_time_stamp - it_test->second->rec_time_stamp;
  1662. sprintf_s(chlog,"%d-%d: %ld,",it_test_2->second->reader_id,it_test->second->reader_id,dt);
  1663. log+=chlog;
  1664. }
  1665. if (log != "")
  1666. {
  1667. Log::write_log(FILE_TYPE::SYS_S,log,true);
  1668. }
  1669. //3.算法定位
  1670. std::shared_ptr<POS> pos = std::make_shared<POS>();
  1671. //pos = LocateAlgorithm::TdoaLocate2d(pRdm);
  1672. //pos = LocateAlgorithm::tdoa_locate_2d_by_fang(pRdm);
  1673. //多个定位结果的输出
  1674. //无序3个元素的输入,将会已第一个元素作为基准进行计算
  1675. std::shared_ptr<ReceiveDataUnorderedMap> pUnOrderedRdm = std::make_shared<ReceiveDataUnorderedMap>();
  1676. pUnOrderedRdm->clear();
  1677. //多个解保存到vector数组内
  1678. std::vector<std::shared_ptr<POS>> vt_pos;
  1679. vt_pos.resize(0);
  1680. //2017/11/28 add by zhuyf
  1681. /*if (is_algo_first_location)
  1682. {
  1683. pos = LocateAlgorithm::tdoa_locate_2d_by_fang(pRdm);
  1684. if (!CheckPositionValid(pos))
  1685. {
  1686. return 1;
  1687. }
  1688. Position p;
  1689. p.x = pos->posx;
  1690. p.y = pos->posy;
  1691. pTaylorWls->init_position(p);
  1692. }else{
  1693. std::shared_ptr<algorithm::base::ReceiveDataUnorderedMap> pTdoaUnOrderedRdm = std::make_shared<algorithm::base::ReceiveDataUnorderedMap>();
  1694. pTdoaUnOrderedRdm->clear();
  1695. for (auto it_item = pRdm->begin();it_item != pRdm->end();++it_item)
  1696. {
  1697. std::shared_ptr<algorithm::base::ReceiveData> pTdoaReceiveData = std::make_shared<algorithm::base::ReceiveData>();
  1698. pTdoaReceiveData->antenna_id = it_item->second->antenna_id;
  1699. pTdoaReceiveData->reader_id = it_item->second->reader_id;
  1700. pTdoaReceiveData->rec_time_stamp = it_item->second->rec_time_stamp;
  1701. pTdoaReceiveData->special = it_item->second->special;
  1702. pTdoaReceiveData->x = it_item->second->x;
  1703. pTdoaReceiveData->y = it_item->second->y;
  1704. pTdoaReceiveData->z = it_item->second->z;
  1705. pTdoaUnOrderedRdm->insert(std::make_pair(it_item->first,pTdoaReceiveData));
  1706. }
  1707. TRACE(_T("ct: %d\r\n"),time_stamp_cal);
  1708. Position p;
  1709. p = pTaylorWls->calc_position(pTdoaUnOrderedRdm);
  1710. pos->posx = p.x;
  1711. pos->posy = p.y;
  1712. }*/
  1713. //方法二
  1714. //如果元素为4个元素,则只取其中三个进行计算即可
  1715. //if (pRdm->size() > 3)
  1716. //{
  1717. // for (ReceiveDataMap::iterator it_first = pRdm->begin();it_first != pRdm->end();++it_first)
  1718. // {
  1719. // for (ReceiveDataMap::iterator it_second = pRdm->begin();it_second != pRdm->end();++it_second)
  1720. // {
  1721. // if (it_second->first != it_first->first)
  1722. // {
  1723. // pUnOrderedRdm->insert(std::make_pair(it_second->first,it_second->second));
  1724. // }
  1725. // }
  1726. // //构建完数据后,进行定位计算
  1727. // std::vector<std::shared_ptr<POS>> vt_tmp_pos = LocateAlgorithm::tdoa_locate_2d_by_fang(pUnOrderedRdm);
  1728. //
  1729. // memset(chlog,0,MAX_LOG_LENGTH*sizeof(char));
  1730. // std::string output_log = "";
  1731. // sprintf_s(chlog,"ct: %d, The number of solutions: %d,",time_stamp_cal,vt_tmp_pos.size());
  1732. // output_log+=chlog;
  1733. // if (vt_tmp_pos.size() > 0)
  1734. // {
  1735. // int i = 0;
  1736. // output_log += "reader number: ";
  1737. // for (ReceiveDataUnorderedMap::iterator it_reader = pUnOrderedRdm->begin();it_reader != pUnOrderedRdm->end();++it_reader)
  1738. // {
  1739. // memset(chlog,0,MAX_LOG_LENGTH*sizeof(char));
  1740. // sprintf_s(chlog,"%d,",it_reader->second->reader_id);
  1741. // output_log+=chlog;
  1742. // }
  1743. // output_log += ", positions: ";
  1744. // for (std::vector<std::shared_ptr<POS>>::iterator tmp_pos = vt_tmp_pos.begin();tmp_pos != vt_tmp_pos.end();++tmp_pos)
  1745. // {
  1746. // memset(chlog,0,MAX_LOG_LENGTH*sizeof(char));
  1747. // sprintf_s(chlog,"x%d : %.3f,y%d : %.3f",i,(*tmp_pos)->posx,i,(*tmp_pos)->posy);
  1748. // output_log+=chlog;
  1749. // i++;
  1750. // }
  1751. // }
  1752. // Log::write_log(FILE_TYPE::SYS_S,output_log,true);
  1753. //
  1754. // for (std::vector<std::shared_ptr<POS>>::iterator tmp_pos = vt_tmp_pos.begin();tmp_pos != vt_tmp_pos.end();++tmp_pos)
  1755. // {
  1756. // vt_pos.push_back(*tmp_pos);
  1757. // }
  1758. // pUnOrderedRdm->clear();
  1759. // }
  1760. // if (vt_pos.size() > 1)
  1761. // {
  1762. // double x = 0.0 ,y = 0.0,z = 0.0;
  1763. // for (std::vector<std::shared_ptr<POS>>::iterator tmp_pos = vt_pos.begin();tmp_pos != vt_pos.end();++tmp_pos)
  1764. // {
  1765. // x+=(*tmp_pos)->posx;
  1766. // y+=(*tmp_pos)->posy;
  1767. // z+=(*tmp_pos)->posz;
  1768. // }
  1769. // pos->posx = x/vt_pos.size();
  1770. // pos->posy = y/vt_pos.size();
  1771. // pos->posz = z/vt_pos.size();
  1772. // }else if(vt_pos.size() == 1){
  1773. // pos->cx = vt_pos.at(0)->posx / (map_scale*1.0);
  1774. // pos->cy = vt_pos.at(0)->posx / (map_scale*1.0);
  1775. // pos->cz = vt_pos.at(0)->posx / (map_scale*1.0);
  1776. // }
  1777. //}else{
  1778. // pos = LocateAlgorithm::tdoa_locate_2d_by_fang(pRdm);
  1779. //}
  1780. //原始方法
  1781. for (ReceiveDataMap::iterator it_first = pRdm->begin();it_first != pRdm->end();++it_first)
  1782. {
  1783. pUnOrderedRdm->insert(std::make_pair(it_first->first,it_first->second));
  1784. std::string reader_list = "";
  1785. char log[MAX_LOG_LENGTH] = {0};
  1786. sprintf_s(log,"card_id : %s, ct : %d, reader_list: %d",card_id.c_str(),time_stamp_cal,it_first->second->reader_id);
  1787. reader_list = log;
  1788. for (ReceiveDataMap::iterator it_second = pRdm->begin();it_second != pRdm->end();++it_second)
  1789. {
  1790. memset(log,0,MAX_LOG_LENGTH*sizeof(char));
  1791. std::string r_log = "";
  1792. //ReceiveDataUnorderedMap::iterator it_first_element = pUnOrderedRdm->begin();
  1793. if (it_second->first != it_first->first)
  1794. {
  1795. pUnOrderedRdm->insert(std::make_pair(it_second->first,it_second->second));
  1796. sprintf_s(log,"-%d",it_second->second->reader_id);
  1797. r_log = log;
  1798. }
  1799. reader_list += r_log;
  1800. }
  1801. //构建完数据后,进行定位计算
  1802. //std::shared_ptr<POS> p = std::make_shared<POS>();
  1803. std::vector<std::shared_ptr<POS>> calc_vt_pos;
  1804. calc_vt_pos.resize(0);
  1805. calc_vt_pos = LocateAlgorithm::tdoa_locate_2d_by_fang(pUnOrderedRdm);
  1806. std::string pos_list = "";
  1807. for (auto it_pos = calc_vt_pos.begin();it_pos != calc_vt_pos.end();++it_pos)
  1808. {
  1809. memset(log,0,MAX_LOG_LENGTH*sizeof(char));
  1810. std::string p_log = "";
  1811. sprintf_s(log,"x: %.2f, y %.2f",(*it_pos)->posx,(*it_pos)->posy);
  1812. p_log = log;
  1813. pos_list+=p_log;
  1814. vt_pos.push_back(*it_pos);
  1815. }
  1816. std::string str_log = "";
  1817. str_log = reader_list + " : " + pos_list;
  1818. Log::write_log(FILE_TYPE::KALMAN_S,str_log,true);
  1819. //vt_pos.push_back(p);
  1820. pUnOrderedRdm->clear();
  1821. }
  1822. unsigned int size = vt_pos.size();
  1823. switch(size){
  1824. case 0:
  1825. break;
  1826. default:
  1827. double sum_x = 0.0,sum_y = 0.0,sum_z = 0.0;;
  1828. for (std::vector<std::shared_ptr<POS>>::iterator it = vt_pos.begin();it != vt_pos.end();++it)
  1829. {
  1830. sum_x += (*it)->posx;
  1831. sum_y += (*it)->posy;
  1832. sum_z += (*it)->posz;
  1833. }
  1834. pos->posx = sum_x/size;
  1835. pos->posy = sum_y/size;
  1836. pos->posz = sum_z/size;
  1837. break;
  1838. }
  1839. //无论多少条数据,二维定位只以最小的三条进行定位
  1840. //pos = LocateAlgorithm::tdoa_locate_2d_by_fang(pRdm);
  1841. if (pos == nullptr)
  1842. {
  1843. return 0;
  1844. }else{
  1845. pos->cx = pos->posx / (map_scale*1.0);
  1846. pos->cy = pos->posy / (map_scale*1.0);
  1847. pos->cz = pos->posz / (map_scale*1.0);
  1848. isoutput = true;
  1849. }
  1850. //保存原始值
  1851. SaveOriginDataBeforeFilter(pos);
  1852. if (!CheckPositionValid(pos))
  1853. {
  1854. return 1;
  1855. }
  1856. //5.唯一解合法性验证
  1857. //ret = CheckSolution(pos);
  1858. //ret = check_solution_tdoa_2d(pos);
  1859. if(ret) {
  1860. char log[MAX_LOG_LENGTH] = {0};
  1861. sprintf_s(log,"cardid: %s, ct: %d, x: %.2f, y: %.2f",card_id.c_str(),time_stamp_cal,pos->cx,pos->cy);
  1862. Log::write_log(KALMAN_S,log,true);
  1863. return ret;
  1864. }
  1865. //6.滤波
  1866. if (FILTER_KALMAN == m_nFilterType)
  1867. {
  1868. //ret = KalmanFilterProcess(pos);
  1869. }
  1870. if (ret)
  1871. {
  1872. return ret;
  1873. }
  1874. pos->dimension = _2D;
  1875. //7.数据保存
  1876. ret = SaveCardAlgoData(pos);
  1877. if (ret)
  1878. {
  1879. return ret;
  1880. }
  1881. return ret;
  1882. }
  1883. int Card::algo_tdoa_3d(int cnt)
  1884. {
  1885. int ret = 0;
  1886. //1.数据有效性判断
  1887. ret = CheckDistData(cnt);
  1888. if(ret) return ret;
  1889. //2.组装数据
  1890. std::shared_ptr<ReceiveDataMap> pRdm = std::make_shared<ReceiveDataMap>();
  1891. pRdm->clear();
  1892. ret = AssembleDistData(pRdm);
  1893. if(ret) return ret;
  1894. //3.算法定位
  1895. std::shared_ptr<POS> pos = std::make_shared<POS>();
  1896. pos = LocateAlgorithm::TdoaLocate3d(pRdm);
  1897. if (pos == nullptr)
  1898. {
  1899. return 0;
  1900. }else{
  1901. pos->cx = pos->posx / (map_scale*1.0);
  1902. pos->cy = pos->posy / (map_scale*1.0);
  1903. pos->cz = pos->posz / (map_scale*1.0);
  1904. isoutput = true;
  1905. }
  1906. //5.唯一解合法性验证
  1907. ret = CheckSolution(pos);
  1908. if(ret) {
  1909. return ret;
  1910. }
  1911. //保存原始值
  1912. SaveOriginDataBeforeFilter(pos);
  1913. //6.滤波
  1914. if (FILTER_KALMAN == m_nFilterType)
  1915. {
  1916. ret = KalmanFilterProcess(pos);
  1917. }
  1918. if (ret)
  1919. {
  1920. return ret;
  1921. }
  1922. //7.数据保存
  1923. ret = SaveCardAlgoData(pos);
  1924. if (ret)
  1925. {
  1926. return ret;
  1927. }
  1928. return ret;
  1929. }
  1930. /*
  1931. * 根据算法计算新位置的坐标以及卡的上一次坐标,计算车辆偏移坐标
  1932. *
  1933. * param
  1934. * 无
  1935. *
  1936. * return
  1937. * 无locatepos
  1938. *
  1939. */
  1940. void Card::algo_calc_offset()
  1941. {
  1942. if(is_first_location)
  1943. {
  1944. right_x = left_x = this->x, right_y = right_y = this->y, right_z = left_z = this->z;
  1945. return ;
  1946. }
  1947. double offset = CHAMBER_WIDTH / 4;
  1948. // 根据方向判断,如何靠右行驶
  1949. if(abs(this->x - this->last_locate.x) <= 1E-4)
  1950. {
  1951. this->right_y = this->left_y = this->y;
  1952. if(this->y >= this->last_locate.y)
  1953. {
  1954. this->right_x = this->x - offset;
  1955. this->left_x = this->x + offset;
  1956. }
  1957. else
  1958. {
  1959. this->right_x = this->x + offset;
  1960. this->left_x = this->x - offset;
  1961. }
  1962. }
  1963. else
  1964. {
  1965. if(abs(this->y - this->last_locate.y) <= 1E-4)
  1966. {
  1967. this->right_x = this->left_x = this->x;
  1968. if(this->x > this->last_locate.x)
  1969. {
  1970. this->right_y = this->y + offset;
  1971. this->left_y = this->y - offset;
  1972. }
  1973. else
  1974. {
  1975. this->right_y = this->y - offset;
  1976. this->left_y = this->y + offset;
  1977. }
  1978. }
  1979. else
  1980. {
  1981. double k = -1.0 / (this->y - this->last_locate.y)/(this->x - this->last_locate.x);
  1982. double m = this->y - k * this->x;
  1983. double a = 1 + pow(k, 2);
  1984. double b = 2 * (k * m - this->y * k - this->x);
  1985. double c = pow(m, 2) + pow(this->x, 2) + pow(this->y, 2) - pow(offset, 2) - 2 * m * this->y;
  1986. if(this->last_locate.x < this->x && this->last_locate.y < this->y)
  1987. {
  1988. this->right_x = -0.5 * (b + sqrt(pow(b, 2) - 4 * a * c)) / a;
  1989. this->left_x = - 0.5 * (b - sqrt(pow(b, 2) - 4 * a * c)) / a;
  1990. }
  1991. else if(this->last_locate.x < this->x && this->last_locate.y > this->y)
  1992. {
  1993. this->right_x = -0.5 * (b - sqrt(pow(b, 2) - 4 * a * c)) / a;
  1994. this->left_x = - 0.5 * (b + sqrt(pow(b, 2) - 4 * a * c)) / a;
  1995. }
  1996. else if(this->last_locate.x > this->x && this->last_locate.y < this->y)
  1997. {
  1998. this->right_x = -0.5 * (b + sqrt(pow(b, 2) - 4 * a * c)) / a;
  1999. this->left_x = - 0.5 * (b - sqrt(pow(b, 2) - 4 * a * c)) / a;
  2000. }
  2001. else if(this->last_locate.x > this->x && this->last_locate.y > this->y)
  2002. {
  2003. this->right_x = -0.5 * (b - sqrt(pow(b, 2) - 4 * a * c)) / a;
  2004. this->left_x = - 0.5 * (b + sqrt(pow(b, 2) - 4 * a * c)) / a;
  2005. }
  2006. this->right_y = this->right_x * k + m;
  2007. this->left_y = this->left_x * k + m;
  2008. }
  2009. }
  2010. }
  2011. double Card::x_offset_after()
  2012. {
  2013. //if (!m_GetOnOffMonkeyCar)
  2014. {
  2015. return this->x;
  2016. }
  2017. //else
  2018. // {
  2019. // return m_modeling_x;
  2020. //}
  2021. //return this->x;
  2022. //if(this->is_hist)
  2023. //{
  2024. // return this->x;
  2025. //}
  2026. //return (OUTPUT_POS::LEFT_POS == m_nOutputPosState) ? this->left_x : this->right_x;
  2027. }
  2028. double Card::y_offset_after()
  2029. {
  2030. //if (!m_GetOnOffMonkeyCar)
  2031. {
  2032. return this->y;
  2033. }
  2034. //else
  2035. //{
  2036. // return this->m_modeling_y;
  2037. //}
  2038. //if(this->is_hist)
  2039. //{
  2040. // return this->y;
  2041. //}
  2042. //return (OUTPUT_POS::LEFT_POS == m_nOutputPosState) ? this->left_y : this->right_y;
  2043. }
  2044. double Card::z_offset_after()
  2045. {
  2046. return this->z;
  2047. }
  2048. bool Card::algo_is_same_direction(double x,double y,double z)
  2049. {
  2050. int sign = 0;
  2051. if(x == last_locate.x){
  2052. if(y > last_locate.y){
  2053. sign = 1;
  2054. }else{
  2055. sign = -1;
  2056. }
  2057. }else{
  2058. if(x > last_locate.x){
  2059. sign = 1;
  2060. }else{
  2061. sign = -1;
  2062. }
  2063. }
  2064. if (sign!=m_nMoveDirection)
  2065. {
  2066. diff_direction_counts++;
  2067. }else{
  2068. diff_direction_counts = 0;
  2069. }
  2070. if (diff_direction_counts > 0 && diff_direction_counts <3)
  2071. {
  2072. return true;
  2073. }else{
  2074. diff_direction_counts = 0;
  2075. return false;
  2076. }
  2077. }
  2078. bool Card::is_pos_state_changed() // 考勤
  2079. {
  2080. bool ret = false;
  2081. debug_print_syslog(0,"[is_pos_state_changed]%s,old:%d,%d",card_id.c_str(),pos_state_old,pos_state);
  2082. if(pos_state != pos_state_old)
  2083. {
  2084. if((PDT_INIT == pos_state_old && PDT_UP == pos_state ) || (PDT_INIT == pos_state && PDT_UP == pos_state_old) )
  2085. {
  2086. // lihongzhen 2017/8/18 无信号状态变为有信号状态,记录时间
  2087. if(PDT_INIT == pos_state_old)
  2088. {
  2089. att_start_time_latest = time(NULL);
  2090. }
  2091. pos_state = pos_state_old = PDT_UP;
  2092. pos_state_count = pos_state_confirm_times;
  2093. ret = false;
  2094. }
  2095. else if(pos_state_count >= pos_state_confirm_times)
  2096. {
  2097. if(PDT_DOWN == pos_state_old)
  2098. {
  2099. att_end_time_latest = time(NULL);
  2100. }
  2101. pos_state_old = pos_state;
  2102. ret = true;
  2103. }
  2104. }
  2105. return ret;
  2106. }
  2107. /*
  2108. * 将数据加入到队列中,但相同卡的接受时间戳的数据满足一定条件时,将相应的数据丢入算法进行定位计算,
  2109. * 计算完后删除相应的数据,释放内存
  2110. *
  2111. * param
  2112. * dist 定位数据
  2113. *
  2114. * return
  2115. * 无
  2116. */
  2117. void Card::add_dist(std::shared_ptr<_coordinate> dist)
  2118. {
  2119. LOCATION_SYSTEM_BRANCH(LOCATION_SYSTEM_BRANCH_134);
  2120. EnterCriticalSection(&m_csCard);
  2121. string s = concat(dist->reader_id, dist->antenna_id);
  2122. int idx = FindDistMap(dist->t);
  2123. if(-1 == idx){
  2124. LOCATION_SYSTEM_BRANCH(LOCATION_SYSTEM_BRANCH_135);
  2125. DistQueMapItem dq;
  2126. dq.cardstamp = dist->t;
  2127. dq.distmap[s] = dist;
  2128. _dists.push_back(dq);
  2129. }else{
  2130. LOCATION_SYSTEM_BRANCH(LOCATION_SYSTEM_BRANCH_136);
  2131. _dists[idx].distmap[s] = dist;
  2132. }
  2133. int ct = dist->t;
  2134. //add by zhuyf 2017/09/29
  2135. int cache_dists = 0; //缓存数据条数
  2136. switch (ranging_type)
  2137. {
  2138. case LOCATEDATATYPE::LDT_TOF:
  2139. cache_dists = LOCATE_DATA_NUMS::FOUR;
  2140. break;
  2141. case LOCATEDATATYPE::LDT_TDOA:
  2142. cache_dists = LOCATE_DATA_NUMS::THREE;
  2143. break;
  2144. case LOCATEDATATYPE::LDT_TOF_INS:
  2145. //由于惯导的tof数据不同ct是间隔10s发送的,如果要设置为2,那么下发校准坐标就会延迟10s,
  2146. //所以为了保证实时性,即只需要相同ct的两条tof数据即可定位
  2147. cache_dists = LOCATE_DATA_NUMS::ONE;
  2148. break;
  2149. default:
  2150. cache_dists = MAX_DIST_CACHE;
  2151. break;
  2152. }
  2153. TRACE(_T("ct: %d,size: %d.\n"),ct,_dists.size());
  2154. if (_dists.size() >= cache_dists)// || package_is_over
  2155. {
  2156. get_coordinate();
  2157. if (is_del_data)
  2158. {
  2159. #if ENABLE_DEBUG_2D_TEST_LOG
  2160. char log[MAX_LOG_LENGTH];
  2161. memset(log,0,MAX_LOG_LENGTH*sizeof(char));
  2162. sprintf_s(log,"pop data: card_id: %s, ct: %d",card_id.c_str(),_dists.front().distmap.begin()->second->t);
  2163. Log::write_log(FILE_TYPE::SYS_S,log,true);
  2164. #endif
  2165. _dists.pop_front();
  2166. distmap.erase(distmap.begin(),distmap.end());
  2167. is_del_data = false;
  2168. package_is_over = false;
  2169. }
  2170. if (LOCATEDATATYPE::LDT_TOF_INS == ranging_type && _dists.size() > 1)
  2171. {
  2172. _dists.pop_front();
  2173. is_del_data = false;
  2174. get_coordinate();
  2175. }
  2176. }
  2177. if (this->x != this->last_x || this->y != this->last_y)
  2178. {
  2179. LOCATION_SYSTEM_BRANCH(LOCATION_SYSTEM_BRANCH_138);
  2180. algo_calc_offset();
  2181. this->n_pos_no_change_times = 0;
  2182. this->b_pos_change = true;
  2183. }else{
  2184. LOCATION_SYSTEM_BRANCH(LOCATION_SYSTEM_BRANCH_139);
  2185. this->n_pos_no_change_times++;
  2186. this->b_pos_change = false;
  2187. }
  2188. LeaveCriticalSection(&m_csCard);
  2189. }
  2190. time_t Card::get_working_time()
  2191. {
  2192. return down_time;
  2193. }
  2194. int Card::get_effictive_dist_count( int offset /*= 0*/ )
  2195. {
  2196. if(0 == _dists.size()) return 0;
  2197. time_stamp_cal = _dists.front().cardstamp;
  2198. return (_dists.front().distmap.size());
  2199. }
  2200. /*
  2201. * Card析构类
  2202. */
  2203. Card::~Card(void)
  2204. {
  2205. _dists.clear();
  2206. DeleteCriticalSection(&m_csCard);
  2207. }
  2208. double Card::get_speed()
  2209. {
  2210. //if(this->idle_count >= IDLE_CONFIRM_TIME || STATUS_LOST == this->status_lost || this->n_pos_no_change_times >= POS_NO_CHANGE_TIMES)
  2211. //{
  2212. // return 0;
  2213. //}
  2214. if (fabs(final_v) < ZERO_PRECISION)
  2215. {
  2216. return 0;
  2217. }
  2218. double ret_v = ceil(fabs(final_v));
  2219. //if (ret_v >= 50) // TODO: 为了演示暂时修正输出值
  2220. //{
  2221. // ret_v = 40;
  2222. //}
  2223. return ret_v;
  2224. }
  2225. /*
  2226. * 采用TOF或者TDOA算法进行定位计算
  2227. *
  2228. * param
  2229. * cnt ------ _dists数据条数
  2230. *
  2231. * return
  2232. * 无返回值
  2233. *
  2234. */
  2235. void Card::get_coordinate(int cnt)
  2236. {
  2237. int ret = 0;
  2238. switch(ranging_type){
  2239. case LOCATEDATATYPE::LDT_TDOA:
  2240. algo_tdoa_1d(cnt);
  2241. break;
  2242. case LOCATEDATATYPE::LDT_TOF_INS:
  2243. ret = algo_tof_1d(cnt);
  2244. if (ret != 0)
  2245. {
  2246. reader_id_algo = -1;
  2247. }else{
  2248. need_display = false;
  2249. }
  2250. break;
  2251. case LOCATEDATATYPE::LDT_TOF:
  2252. //ret = algo_tof_1d(cnt);
  2253. ret = new_algo_tof_1d(cnt);
  2254. //ret = algo_tof_raw_1d(distmap);
  2255. /*char log[MAX_LOG_LENGTH];
  2256. memset(log,0,MAX_LOG_LENGTH*sizeof(char));
  2257. sprintf_s(log,"card_id: %s,ct: %d, error: %d,algo_time: %s",card_id.c_str(),time_stamp_cal,ret,CFunctions::time_t2string(time_cur_algo).c_str());
  2258. Log::write_log(FILE_TYPE::ALGO_S,log,true);*/
  2259. break;
  2260. default:
  2261. ;
  2262. }
  2263. if (ret == 0)
  2264. {
  2265. m_indexForRcvOneReader = 0;
  2266. inspect_coordinate(this->acce_cur_state);
  2267. /*char log[MAX_LOG_LENGTH];
  2268. memset(log,0,MAX_LOG_LENGTH*sizeof(char));
  2269. sprintf_s(log,"card_id: %s, x: %.3f, y: %.3f,ct: %d, size: %d",card_id.c_str(),origin_locate.x,origin_locate.y,time_stamp_last,distmap.size());
  2270. Log::write_log(FILE_TYPE::SYS_S,log,true);*/
  2271. }
  2272. else if(DIST_COUNT_LESS_THAN_TWO == ret)
  2273. {
  2274. m_indexForRcvOneReader++;
  2275. }
  2276. else
  2277. {
  2278. m_indexForRcvOneReader=0;
  2279. }
  2280. if (m_indexForRcvOneReader >= INDEXFORRECONEREADER)
  2281. {
  2282. this->v = 0;
  2283. }
  2284. if(_isnan(this->x) || _isnan(this->y) || _isnan(this->z)){
  2285. this->x = this->last_x;
  2286. this->y = this->last_y;
  2287. this->z = this->last_z;
  2288. this->output_x = this->x;
  2289. this->output_y = this->y;
  2290. }
  2291. }
  2292. /*
  2293. * 选择算法调用种类
  2294. *
  2295. * param
  2296. * 无
  2297. *
  2298. * return
  2299. * 无
  2300. *
  2301. */
  2302. void Card::get_coordinate()
  2303. {
  2304. //识别定位是采用一维定位还是二维或三维定位
  2305. int dim = 0;
  2306. dim = get_dimension(); //检查数据,看采用定位方法
  2307. int cnt = 0;
  2308. cnt = distmap.size();
  2309. switch(dim){
  2310. case _1D:
  2311. if (LOCATEDATATYPE::LDT_TOF == ranging_type || LOCATEDATATYPE::LDT_TDOA == ranging_type)
  2312. {
  2313. get_coordinate(cnt);
  2314. }else if (LOCATEDATATYPE::LDT_TOF_INS == ranging_type ||
  2315. LOCATEDATATYPE::LDT_TDOA_INS == ranging_type)
  2316. {
  2317. get_coordinate(get_effictive_dist_count());
  2318. }
  2319. break;
  2320. case _2D:
  2321. if (LOCATEDATATYPE::LDT_TOF == ranging_type)
  2322. {
  2323. //get_coordinate_2d(cnt);
  2324. algo_tof_2d(cnt);
  2325. }else if (LOCATEDATATYPE::LDT_TDOA == ranging_type)
  2326. {
  2327. algo_tdoa_2d(cnt);
  2328. }
  2329. break;
  2330. case _3D:
  2331. if (LOCATEDATATYPE::LDT_TOF == ranging_type)
  2332. {
  2333. get_coordinate_3d(cnt);
  2334. }else if (LOCATEDATATYPE::LDT_TDOA == ranging_type)
  2335. {
  2336. algo_tdoa_3d(cnt);
  2337. }
  2338. break;
  2339. }
  2340. is_del_data = true;
  2341. }
  2342. /*
  2343. * 根据状态值获得状态文本描述
  2344. *
  2345. * param
  2346. * 无
  2347. *
  2348. * return
  2349. * 状态文本描述
  2350. *
  2351. */
  2352. int Card::get_state()
  2353. {
  2354. this->state = 0;
  2355. //电量极低
  2356. if(power_state == STATUS_ERROR_SERIOUS){
  2357. state = STATUS_POWER_LOWER_SERIOUS;
  2358. }
  2359. return state;
  2360. }
  2361. int Card::get_state_moving()
  2362. {
  2363. if(STATUS_LOST == this->status_lost)
  2364. {
  2365. state_moving = STATE_ACCE_STATIC;
  2366. }
  2367. return state_moving;
  2368. }
  2369. std::string Card::concat( int reader_id, int ant_id )
  2370. {
  2371. char s[10];
  2372. sprintf_s(s, "%d-%d", reader_id, ant_id);
  2373. return s;
  2374. }
  2375. /*
  2376. * 滤波功能设置
  2377. *
  2378. * param
  2379. * nType ------ 滤波类型
  2380. *
  2381. * return
  2382. * 无
  2383. */
  2384. void Card::EnableFilter(int nType)
  2385. {
  2386. //如果无滤波类型直接返回
  2387. if(nType == NO_FILTER){
  2388. return;
  2389. }
  2390. //开启滤波功能,设置滤波类型
  2391. m_bUseFilter = TRUE;
  2392. m_nFilterType = nType;
  2393. switch(nType){
  2394. case FILTER_KALMAN:
  2395. //分配卡尔曼滤波类型变量并初始化参数
  2396. if(m_pKalmanFilter == nullptr){
  2397. std::unique_ptr<CKalmanFilter> p(new CKalmanFilter());
  2398. m_pKalmanFilter = std::move(p);
  2399. m_pKalmanFilter->Initial(0.2);
  2400. m_pKalmanFilter->m_bFlag = false;
  2401. }
  2402. break;
  2403. default:
  2404. break;
  2405. }
  2406. }
  2407. void Card::remove_dist_head()
  2408. {
  2409. DistMap tmp = _dists.front().distmap;
  2410. if(tmp.size() > 0 ){
  2411. DistMap::iterator it_mp_dist = tmp.begin();
  2412. for(it_mp_dist;it_mp_dist != tmp.end();){
  2413. it_mp_dist = tmp.erase(it_mp_dist);
  2414. }
  2415. }
  2416. _dists.pop_front();
  2417. }
  2418. Reader::Reader(void)
  2419. {
  2420. reader_id = device_type_id = pos_state = map_id = area_id = temperature = tick_count= 0;
  2421. sync_level = 0xFF;
  2422. reader_state = reader_state_old = STATUS_DEVICE_NORMAL;
  2423. m_nIsSpecial = -1;
  2424. last_send_time = rec_time = reader_time = lost_time = time(NULL);
  2425. sync_rootId = 0;
  2426. map_scale = 1.0;
  2427. reader_x = reader_y = reader_z = reader_angle = reader_interval_time = -9999.0;
  2428. reader_name = ip = "";
  2429. for(int i = 0;i < ANTENNA_COUNT;i++){
  2430. ant[i] = nullptr;
  2431. }
  2432. for(int i = 0;i < ADHOC_COUNT;i++){
  2433. adhoc[i] = nullptr;
  2434. }
  2435. bIsInitCoverage = false;
  2436. init_ctrl_reader_state = false;
  2437. for(int i = 0; i < READER_EVENT_COUNT; i++){
  2438. m_event_list[i] = 0;
  2439. }
  2440. reader_package_end_pos = 0;
  2441. last_parse_card_id = "";
  2442. pBoundaryArea = nullptr;
  2443. is_simulation_card = b_simulation_status = false;
  2444. reader_dir = -1;
  2445. relay_counts = -1;
  2446. m_start_simulation_time = time(NULL);
  2447. m_vt_his_position.resize(0);
  2448. m_bNeedPowerAlarm = false;
  2449. }
  2450. Reader::~Reader(void)
  2451. {
  2452. }
  2453. std::string Reader::get_state_text()
  2454. {
  2455. string ret = "";
  2456. if(reader_state == STATUS_DEVICE_ERROR){
  2457. ret = "故障";
  2458. }else if(reader_state == STATUS_DEVICE_NORMAL){
  2459. ret = "正常";
  2460. }
  2461. return ret;
  2462. }
  2463. Antenna::Antenna(void)
  2464. {
  2465. antenna_angle = 0;
  2466. antenna_id = 0;
  2467. antenna_x = 0;
  2468. antenna_y = 0;
  2469. antenna_z = 0;
  2470. }
  2471. Antenna::~Antenna(void)
  2472. {
  2473. }
  2474. Area::Area(void)
  2475. {
  2476. is_att = 1;
  2477. polygon_count = 0;
  2478. polygon = NULL;
  2479. map_id = area_id = area_type_id = 0 ;
  2480. area_name = area_type_name = path = "";
  2481. over_count_person = over_time_person = under_count_person = under_time_person = 0;
  2482. over_count_vehicle = over_time_vehicle = under_count_vehicle = under_time_vehicle = 0;
  2483. count_person = count_vehicle = count_card = 0;
  2484. is_area_over_time_person = is_area_over_time_vehicle = false;
  2485. count_area_over_time_person = count_area_over_time_vehicle = 0;
  2486. time_over_time_person = time_over_time_vehicle = time(NULL);
  2487. is_area_over_count_person = is_area_over_count_vehicle = false;
  2488. count_area_over_count_person = count_area_over_count_vehicle = 0;
  2489. time_over_count_person = time_over_count_vehicle = time(NULL);
  2490. is_area_forbidden_person = is_area_forbidden_vehicle = false;
  2491. count_area_forbidden_person = count_area_forbidden_vehicle = 0;
  2492. time_forbidden_person = time_forbidden_vehicle = time(NULL);
  2493. over_speed_vehicle = 0;
  2494. area_card_list_person = std::make_shared<CardMap>();
  2495. area_card_list_vehicle = std::make_shared<CardMap>();
  2496. area_card_list_over_speed = std::make_shared<CardMap>();
  2497. for(int i = 0; i < AREA_EVENT_COUNT; i++){
  2498. m_event_list[i] = 0;
  2499. }
  2500. }
  2501. Area::~Area(void)
  2502. {
  2503. if(polygon){
  2504. delete[] polygon;
  2505. polygon = NULL;
  2506. }
  2507. }
  2508. void Area::init_border(string sz_path)
  2509. {
  2510. if(sz_path == ""){
  2511. return ;
  2512. }
  2513. std::vector<std::string> vec = split(sz_path, " ");
  2514. std::vector<std::string>::iterator it = vec.begin();
  2515. if(polygon){
  2516. delete[] polygon;
  2517. polygon = NULL;
  2518. }
  2519. polygon = new _point[vec.size()];
  2520. polygon_count = 0;
  2521. for(; it != vec.end(); ++it){
  2522. if (*it == "")
  2523. {
  2524. continue;
  2525. }
  2526. std::vector<std::string> subvec = split(it->c_str(), ",");
  2527. _point p;
  2528. p.x = get_vertex(subvec[0]);
  2529. p.y = get_vertex(subvec[1]);
  2530. p.z = 0;
  2531. polygon[polygon_count] = p;
  2532. polygon_count++;
  2533. }
  2534. }
  2535. std::vector<std::string> Area::split( std::string str,std::string pattern )
  2536. {
  2537. std::string::size_type pos;
  2538. std::vector<std::string> result;
  2539. str+=pattern;//扩展字符串以方便操作
  2540. unsigned int size=str.size();
  2541. for(unsigned int i=0; i<size; i++){
  2542. pos=str.find(pattern,i);
  2543. if(pos<size){
  2544. std::string s=str.substr(i,pos-i);
  2545. result.push_back(s);
  2546. i=pos+pattern.size()-1;
  2547. }
  2548. }
  2549. return result;
  2550. }
  2551. double Area::get_vertex( std::string src)
  2552. {
  2553. std::string dest = "";
  2554. for(unsigned int i = 0; i < src.length(); i++){
  2555. if((src[i] >= '0' && src[i]<='9') || src[i]=='-' || src[i] == '.'){
  2556. dest += src[i];
  2557. }
  2558. }
  2559. return atof(dest.c_str());
  2560. }
  2561. bool Area::is_in_polygon( _point p )
  2562. {
  2563. if(polygon == NULL){
  2564. return false;
  2565. }
  2566. int counter = 0;
  2567. int i;
  2568. double xinters;
  2569. _point p1,p2;
  2570. p1 = polygon[0];
  2571. for (int i=1;i<= polygon_count;i++) {
  2572. p2 = polygon[i % polygon_count];
  2573. if (p.y > MIN(p1.y,p2.y)) {
  2574. if (p.y <= MAX(p1.y,p2.y)) {
  2575. if (p.x <= MAX(p1.x,p2.x)) {
  2576. if (p1.y != p2.y) {
  2577. xinters = (p.y-p1.y)*(p2.x-p1.x)/(p2.y-p1.y)+p1.x;
  2578. if (p1.x == p2.x || p.x <= xinters)
  2579. counter++;
  2580. }
  2581. }
  2582. }
  2583. }
  2584. p1 = p2;
  2585. }
  2586. return (counter % 2 == 0) ? false : true;
  2587. }
  2588. bool Area::is_special()
  2589. {
  2590. return (area_type_id == AREA_TYPE_NO_COVER) ? true : false;
  2591. }
  2592. int Card::FindDistMap( int cardstamp )
  2593. {
  2594. int idx = -1;
  2595. for(int i = _dists.size() - 1; i >= 0; i--){
  2596. if(_dists[i].cardstamp == cardstamp ){
  2597. return i;
  2598. }
  2599. }
  2600. return idx;
  2601. }
  2602. /*
  2603. * 卡尔曼滤波处理模块
  2604. *
  2605. * param
  2606. * pos 定位结果
  2607. *
  2608. * return
  2609. * 处理成功返回0,否则返回非零值
  2610. *
  2611. */
  2612. int Card::KalmanFilterProcess(std::shared_ptr<POS>& pos)
  2613. {
  2614. int bRet = 0;
  2615. switch (ranging_type)
  2616. {
  2617. case LOCATEDATATYPE::LDT_TDOA:
  2618. bRet = kalman_filter_process_tdoa(pos);
  2619. break;
  2620. case LOCATEDATATYPE::LDT_TOF:
  2621. bRet = kalman_filter_process_tof(pos);
  2622. break;
  2623. default:
  2624. break;
  2625. }
  2626. return bRet;
  2627. }
  2628. /*
  2629. * 组装拟合数据,主要是前REF_POSITION_NUM次的定位数据信息
  2630. *
  2631. * param
  2632. * pos 定位坐标
  2633. *
  2634. * return
  2635. * 成功返回0
  2636. */
  2637. int Card::UpdateFittingData(std::shared_ptr<POS> pos)
  2638. {
  2639. if (cur_ref_totals == REF_POSITION_NUM - 1)
  2640. {
  2641. for (int i = 0;i < cur_ref_totals-1;i++)
  2642. {
  2643. fitting_v[i] = fitting_v[i + 1];
  2644. fitting_ct[i] = fitting_ct[i + 1];
  2645. fitting_x[i] = fitting_x[i + 1];
  2646. fitting_y[i] = fitting_y[i + 1];
  2647. }
  2648. }
  2649. fitting_v[cur_ref_totals] = v;
  2650. fitting_ct[cur_ref_totals] = m_nCalcSyncNum;
  2651. fitting_x[cur_ref_totals] = pos->posx;
  2652. fitting_y[cur_ref_totals] = pos->posy;
  2653. if (cur_ref_totals < REF_POSITION_NUM - 1)
  2654. {
  2655. cur_ref_totals++;
  2656. }
  2657. return 0;
  2658. }
  2659. int Card::CalcFittingData(list<std::shared_ptr<POS>> & list_pos)
  2660. {
  2661. //如果滤波失败或者其他条件失败,
  2662. //则通过直线回归算法拟合出三个解
  2663. vector<double> vx,vy,vt;
  2664. vx.resize(0);
  2665. vy.resize(0);
  2666. vt.resize(0);
  2667. for (list<std::shared_ptr<POS>>::iterator it = list_pos.begin();it!=list_pos.end();++it)
  2668. {
  2669. vt.push_back((*it)->card_count);
  2670. vx.push_back((*it)->posx);
  2671. vy.push_back((*it)->posy);
  2672. }
  2673. //线性回归拟合,从5个历史点中取出3个预测点
  2674. //Fit fit_x;
  2675. fit_x.linearFit(vt,vx);
  2676. //Fit fit_y;
  2677. fit_y.linearFit(vt,vy);
  2678. for (int i = 0;i < FIT_POSITION_NUM;i++)
  2679. {
  2680. fit_new_x[i] = 0;
  2681. fit_new_y[i] = 0;
  2682. }
  2683. for (int i = 1;i <= FIT_POSITION_NUM;i++)
  2684. {
  2685. fit_new_x[i-1] = fit_x.getY(vt[4] + i);
  2686. fit_new_y[i-1] = fit_y.getY(vt[4] + i);
  2687. }
  2688. have_fit_pos = true;
  2689. return 0;
  2690. }
  2691. /*
  2692. * 计算线性拟合数据,
  2693. * 当满足如下两个条件:
  2694. * a.之前定位成功,
  2695. * b.参考数据小于5,
  2696. * c.连续取拟合数据等于3次了,
  2697. * 则不计算拟合数据
  2698. *
  2699. * param
  2700. * ret 定位状态
  2701. * pos 定位结果
  2702. *
  2703. * return
  2704. * 成功获得拟合数据返回0,否则返回1
  2705. *
  2706. */
  2707. int Card::CalcFittingData(int ret,std::shared_ptr<POS>& pos)
  2708. {
  2709. if (his_pos.size() < FIT_POSITION_NUM || cur_fit_nums >= 3)
  2710. {
  2711. if (cur_fit_nums == 3)
  2712. {
  2713. have_fit_pos = false;
  2714. }
  2715. return 1;
  2716. }
  2717. if (ret == 0 && pos->reason == 0)
  2718. {
  2719. return 2;
  2720. }
  2721. if (cur_fit_nums == 0)
  2722. {
  2723. //如果滤波失败或者其他条件失败,
  2724. //则通过直线回归算法拟合出三个解
  2725. vector<double> vx,vy,vt;
  2726. vx.resize(0);
  2727. vy.resize(0);
  2728. vt.resize(0);
  2729. for (list<std::shared_ptr<POS>>::iterator it = his_pos.begin();it!=his_pos.end();++it)
  2730. {
  2731. vt.push_back((*it)->card_count);
  2732. vx.push_back((*it)->posx);
  2733. vy.push_back((*it)->posy);
  2734. }
  2735. //线性回归拟合,从5个历史点中取出3个预测点
  2736. Fit fit_x;
  2737. fit_x.linearFit(vt,vx);
  2738. Fit fit_y;
  2739. fit_y.linearFit(vt,vy);
  2740. for (int i = 1;i <= FIT_POSITION_NUM;i++)
  2741. {
  2742. fit_new_x[i-1] = fit_x.getY(vt[4] + i);
  2743. fit_new_y[i-1] = fit_y.getY(vt[4] + i);
  2744. }
  2745. }
  2746. int index = cur_fit_nums;
  2747. //判断
  2748. double kx = fit_new_x[index];
  2749. double ky = fit_new_y[index];
  2750. std::shared_ptr<POS> ks = std::make_shared<POS>();
  2751. ks->posx = fit_new_x[index] ;
  2752. ks->posy = fit_new_y[index] ;
  2753. ks->cx = ks->posx / (1.0*map_scale);
  2754. ks->cy = ks->posy / (1.0*map_scale);
  2755. //判断拟合数据是否在地图集上,如果不在返回1
  2756. if (!LocateAlgorithm::IsOnMap(ks,pTdoaReaderPathMap))
  2757. {
  2758. //不在地图集上,则返回
  2759. return 1;
  2760. }
  2761. pos->posx = ks->posx;
  2762. pos->posy = ks->posy;
  2763. pos->cx = ks->cx;
  2764. pos->cy = ks->cy;
  2765. if (fabs(pos->diff_reader_sync_num) > 1E-4)
  2766. {
  2767. pos->cvx = (pos->cx - last_locate.x)/pos->diff_reader_sync_num;
  2768. pos->cvy = (pos->cy - last_locate.y)/pos->diff_reader_sync_num;
  2769. }
  2770. cur_fit_nums++;
  2771. pos->reason = 0;
  2772. pos->is_fit = true;
  2773. have_fit_pos = true;
  2774. return 0;
  2775. }
  2776. /*
  2777. * 通过多项式拟合计算数据,
  2778. * 当满足如下两个条件:
  2779. * a.之前定位成功,
  2780. * b.参考数据小于5,
  2781. * c.连续取拟合数据等于3次了,
  2782. * 则不计算拟合数据
  2783. *
  2784. * param
  2785. * ret 定位状态
  2786. * pos 定位结果
  2787. *
  2788. * return
  2789. * 成功获得拟合数据返回0,否则返回1
  2790. *
  2791. */
  2792. int Card::CalcLongFittingData(int ret,std::shared_ptr<POS>& pos)
  2793. {
  2794. int nums = FIT_POSITION_NUM*4;
  2795. if (long_his_pos.size() < nums || cur_fit_nums >= nums)
  2796. {
  2797. if (cur_fit_nums == nums)
  2798. {
  2799. have_fit_pos = false;
  2800. }
  2801. return 1;
  2802. }
  2803. if (ret == 0 && pos->reason == 0)
  2804. {
  2805. return 2;
  2806. }
  2807. if (cur_fit_nums <= 3)
  2808. {
  2809. //如果滤波失败或者其他条件失败,
  2810. //则通过多项式拟合算法拟合出20个解
  2811. vector<double> vx,vy,vt;
  2812. vx.resize(0);
  2813. vy.resize(0);
  2814. vt.resize(0);
  2815. for (list<std::shared_ptr<POS>>::iterator it = long_his_pos.begin();it!=long_his_pos.end();++it)
  2816. {
  2817. vt.push_back((*it)->card_count);
  2818. vx.push_back((*it)->posx);
  2819. vy.push_back((*it)->posy);
  2820. }
  2821. //多项式拟合,从20个历史点中拟合出20个预测点
  2822. Fit fit_x;
  2823. fit_x.polyfit(vt,vx,2);
  2824. Fit fit_y;
  2825. fit_y.polyfit(vt,vy,2);
  2826. for (int i = 1;i <= nums;i++)
  2827. {
  2828. long_fit_new_x[i-1] = fit_x.getY(vt[nums - 1] + i);
  2829. long_fit_new_y[i-1] = fit_y.getY(vt[nums - 1] + i);
  2830. }
  2831. }
  2832. int index = cur_fit_nums - 3;
  2833. //判断
  2834. double kx = long_fit_new_x[index];
  2835. double ky = long_fit_new_y[index];
  2836. std::shared_ptr<POS> ks = std::make_shared<POS>();
  2837. ks->posx = long_fit_new_x[index] ;
  2838. ks->posy = long_fit_new_y[index] ;
  2839. ks->cx = ks->posx / (1.0*map_scale);
  2840. ks->cy = ks->posy / (1.0*map_scale);
  2841. //判断拟合数据是否在地图集上,如果不在返回1
  2842. if (!LocateAlgorithm::IsOnMap(ks,pTdoaReaderPathMap))
  2843. {
  2844. //不在地图集上,则返回
  2845. return 1;
  2846. }
  2847. pos->posx = ks->posx;
  2848. pos->posy = ks->posy;
  2849. pos->cx = ks->cx;
  2850. pos->cy = ks->cy;
  2851. if (fabs(pos->diff_reader_sync_num) > 1E-4)
  2852. {
  2853. pos->cvx = (pos->cx - last_locate.x)/pos->diff_reader_sync_num;
  2854. pos->cvy = (pos->cy - last_locate.y)/pos->diff_reader_sync_num;
  2855. }
  2856. cur_fit_nums++;
  2857. pos->reason = 0;
  2858. pos->is_fit = true;
  2859. have_fit_pos = true;
  2860. return 0;
  2861. }
  2862. int Card::CalcLongFittingData()
  2863. {
  2864. int nums = FIT_POSITION_NUM*4;
  2865. //如果滤波失败或者其他条件失败,
  2866. //则通过多项式拟合算法拟合出20个解
  2867. vector<double> vx,vy,vt;
  2868. vx.resize(0);
  2869. vy.resize(0);
  2870. vt.resize(0);
  2871. for (list<std::shared_ptr<POS>>::iterator it = long_his_pos.begin();it!=long_his_pos.end();++it)
  2872. {
  2873. vt.push_back((*it)->card_count);
  2874. vx.push_back((*it)->posx);
  2875. vy.push_back((*it)->posy);
  2876. }
  2877. //多项式拟合,从20个历史点中拟合出20个预测点
  2878. Fit fit_x;
  2879. fit_x.polyfit(vt,vx,2);
  2880. Fit fit_y;
  2881. fit_y.polyfit(vt,vy,2);
  2882. for (int i = 0;i < nums;i++)
  2883. {
  2884. long_fit_new_x[i] = 0;
  2885. long_fit_new_y[i] = 0;
  2886. }
  2887. double diff_x = 0 , diff_y = 0;
  2888. double dx[FIT_POSITION_NUM*4] = {0};
  2889. double dy[FIT_POSITION_NUM*4] = {0};
  2890. for (int i = 1;i <= nums;i++)
  2891. {
  2892. long_fit_new_x[i-1] = fit_x.getY(vt[nums - 1] + i);
  2893. dx[i-1] = long_fit_new_x[i-1];
  2894. long_fit_new_y[i-1] = fit_y.getY(vt[nums - 1] + i);
  2895. dy[i-1] = long_fit_new_y[i-1];
  2896. }
  2897. double start_x = 0.0;
  2898. double end_x = 0.0;
  2899. double start_y = 0.0;
  2900. double end_y = 0.0;
  2901. start_x = long_fit_new_x[4];
  2902. start_y = long_fit_new_y[4];
  2903. end_x = long_fit_new_x[5];
  2904. end_y = long_fit_new_y[5];
  2905. double distance = sqrt(pow(start_x - end_x,2)+pow(start_y - end_y,2));
  2906. double percent = 0.5;
  2907. for (int i = 5;i < nums;i++)
  2908. {
  2909. distance *= percent;
  2910. if (abs(long_fit_new_x[i-1] - long_fit_new_x[i]) < 1E-4)
  2911. {
  2912. //表示x相等,在y轴上
  2913. if (long_fit_new_y[i-1] < long_fit_new_y[i])
  2914. {
  2915. //如果下一次拟合值比当前拟合值大
  2916. dy[i] = dy[i-1] + distance;
  2917. }else{
  2918. dy[i] = dy[i-1] - distance;
  2919. }
  2920. }else{
  2921. //计算斜率
  2922. double k = (long_fit_new_y[i-1] - long_fit_new_y[i])/(long_fit_new_x[i-1] - long_fit_new_x[i]);
  2923. //在有斜率的地方
  2924. double arg = atan(k);
  2925. if (long_fit_new_x[i-1] < long_fit_new_x[i] && long_fit_new_y[i-1] < long_fit_new_y[i])
  2926. {
  2927. dx[i] = dx[i-1] + cos(arg)*distance;
  2928. dy[i] = dy[i-1] + sin(arg)*distance;
  2929. }
  2930. else
  2931. {
  2932. dx[i] = dx[i-1] - cos(arg)*distance;
  2933. dy[i] = dy[i-1] - sin(arg)*distance;
  2934. }
  2935. }
  2936. //使用下一个点和第六点的距离取50%
  2937. distance = sqrt(pow(dx[i-1] - end_x,2) + pow(dy[i] - end_y,2));
  2938. }
  2939. for (int i = 0;i < nums;i++)
  2940. {
  2941. long_fit_new_x[i] = dx[i];
  2942. long_fit_new_y[i] = dy[i];
  2943. }
  2944. have_long_fit_pos = true;
  2945. return 0;
  2946. }
  2947. std::shared_ptr<POS> Card::GetPosFromFittingData()
  2948. {
  2949. if ((!have_fit_pos&&!have_long_fit_pos)
  2950. || (have_fit_pos && cur_fit_nums >= 3&&!have_long_fit_pos)
  2951. || (have_fit_pos && have_long_fit_pos && cur_fit_nums >= 20))
  2952. {
  2953. //1.如果5点拟合和20点拟合无数据,则无法获取
  2954. //2.如果有5点拟合,但无20点拟合,获取拟合数据超过3,则无法获取
  2955. //3.如果有5点拟合,且有20点拟合,而且获取拟合数据超过20,则无法获取
  2956. return nullptr;
  2957. }
  2958. std::shared_ptr<POS> p = std::make_shared<POS>();
  2959. //没有拟合20个点的数据
  2960. if (!have_long_fit_pos)
  2961. {
  2962. if (have_fit_pos)
  2963. {
  2964. p->posx = fit_new_x[cur_fit_nums];
  2965. p->posy = fit_new_y[cur_fit_nums];
  2966. p->cx = p->posx / (map_scale*1.0);
  2967. p->cy = p->posy / (map_scale*1.0);
  2968. p->is_fit = true;
  2969. p->reason = 0;
  2970. cur_fit_nums++;
  2971. }
  2972. }else{
  2973. //有拟合20个点的数据
  2974. p->posx = long_fit_new_x[cur_fit_nums];
  2975. p->posy = long_fit_new_y[cur_fit_nums];
  2976. p->cx = p->posx / (map_scale*1.0);
  2977. p->cy = p->posy / (map_scale*1.0);
  2978. p->is_fit = true;
  2979. p->reason = 0;
  2980. cur_fit_nums++;
  2981. }
  2982. if (p->posx == INVALID_COORDINATE && p->posy == INVALID_COORDINATE)
  2983. {
  2984. p = nullptr;
  2985. }
  2986. return p;
  2987. }
  2988. int Card::GetPosFromFittingData(std::shared_ptr<POS>& pos)
  2989. {
  2990. if (have_fit_pos || have_long_fit_pos)
  2991. {
  2992. //如果拟合了数据,则直接取拟合数据
  2993. std::shared_ptr<POS> fit_p = GetPosFromFittingData();
  2994. if (fit_p == nullptr)
  2995. {
  2996. return 1;
  2997. }
  2998. pos->posx = fit_p->posx;
  2999. pos->posy = fit_p->posy;
  3000. pos->cx = fit_p->cx;
  3001. pos->cy = fit_p->cy;
  3002. if (m_nCalcSyncNum - this->last_locate.sync_num < 0 && m_nCalcSyncNum < 100)
  3003. {
  3004. pos->diff_reader_sync_num = (m_nCalcSyncNum + 65536 - this->last_locate.sync_num)*0.2;
  3005. }else{
  3006. pos->diff_reader_sync_num = (m_nCalcSyncNum - this->last_locate.sync_num)*0.2;
  3007. }
  3008. if (!LocateAlgorithm::IsOnMap(pos,pTdoaReaderPathMap))
  3009. {
  3010. pos->reason = 1;
  3011. return 1;
  3012. }else{
  3013. //如果在地图集上,则计算相关参数
  3014. if (fabs(pos->diff_reader_sync_num) > 1E-4)
  3015. {
  3016. pos->cvx = (pos->cx - last_locate.x)/pos->diff_reader_sync_num;
  3017. pos->cvy = (pos->cy - last_locate.y)/pos->diff_reader_sync_num;
  3018. }else{
  3019. pos->cvx = last_vx;
  3020. pos->cvy = last_vy;
  3021. }
  3022. cur_fit_nums++;
  3023. pos->reason = 0;
  3024. pos->is_fit = true;
  3025. }
  3026. }else{
  3027. return 1;
  3028. }
  3029. return 0;
  3030. }
  3031. /*
  3032. * 处理上传的惯导数据的校准功能:含初始坐标和陀螺仪角度校准
  3033. *
  3034. * param
  3035. *
  3036. * return
  3037. * 成功返回0,失败返回1
  3038. *
  3039. */
  3040. int Card::deal_ins_cal_data()
  3041. {
  3042. int nRet = 0;
  3043. std::shared_ptr<POS> cur_uwb_pos = std::make_shared<POS>();
  3044. cur_uwb_pos->posx = this->x*map_scale;
  3045. cur_uwb_pos->posy = this->y*map_scale;
  3046. if (CheckPositionValid(cur_uwb_pos))
  3047. {
  3048. if (lt_uwb_pos.size() == 0 && lt_ins_pos.size() == 0)
  3049. {
  3050. ins_cal_type = CAL_DATA_POS;
  3051. lt_uwb_pos.push_back(cur_uwb_pos);
  3052. lt_ins_pos.push_back(cur_uwb_pos);
  3053. }else{
  3054. if (lt_uwb_pos.size() >= 2)
  3055. {
  3056. lt_uwb_pos.pop_front();
  3057. }
  3058. lt_uwb_pos.push_back(cur_uwb_pos);
  3059. //为了保证相同时刻uwb定位数据和惯导坐标的一致性
  3060. //数据保存到校准队列中
  3061. if (lt_ins_pos.size() >= 2)
  3062. {
  3063. lt_ins_pos.pop_front();
  3064. }
  3065. lt_ins_pos.push_back(cur_ins_pos);
  3066. }
  3067. }
  3068. if (lt_ins_pos.size() == 2 && lt_uwb_pos.size() == 2)
  3069. {
  3070. ins_cal_type = CAL_DATA_ALL;
  3071. double k1 = 0.0,k2 = 0.0;
  3072. int s1 = 0, s2 = 0;
  3073. std::shared_ptr<POS> uwb_start_pos = nullptr,uwb_end_pos = nullptr;
  3074. std::shared_ptr<POS> ins_start_pos = nullptr,ins_end_pos = nullptr;
  3075. uwb_start_pos = *(lt_uwb_pos.begin());
  3076. uwb_end_pos = *(lt_uwb_pos.rbegin());
  3077. s1 = calc_slope(uwb_start_pos,uwb_end_pos,k1);
  3078. ins_start_pos = *(lt_ins_pos.begin());
  3079. ins_end_pos = *(lt_ins_pos.rbegin());
  3080. s2 = calc_slope(ins_start_pos,ins_end_pos,k2);
  3081. //else if 后的为如果某条直线的斜率不存在
  3082. if (s1 == 0 && s2 == 0)
  3083. {
  3084. //如果直线斜率都存在,才可以计算校准角度
  3085. this->ins_gyp_angle = atan((k2 - k1)/(1+k1*k2));
  3086. }else if (s1 == -1 && s2 == 0)
  3087. {
  3088. //如果k1直线斜率不存在,及为平行于y轴的直线
  3089. if (abs(k2) < ZERO_PRECISION)
  3090. {
  3091. //如果k2的斜率为零,则k2直线和k1直线垂直
  3092. this->ins_gyp_angle = 90.0;
  3093. }else{
  3094. this->ins_gyp_angle = atan(k2);
  3095. }
  3096. }else if(s1 == 0 && s2 == -1){
  3097. //如果k2直线斜率不存在,及为平行于y轴的直线
  3098. if (abs(k1) < ZERO_PRECISION)
  3099. {
  3100. //如果k1的斜率为零,则k1直线和k2直线垂直
  3101. this->ins_gyp_angle = 90.0;
  3102. }else{
  3103. this->ins_gyp_angle = atan(k1);
  3104. }
  3105. }else{
  3106. nRet = 1;
  3107. }
  3108. }else{
  3109. //数据不够,无法校准
  3110. nRet = 1;
  3111. }
  3112. return nRet;
  3113. }
  3114. /*
  3115. * 根据起始点和结束点计算所在直线的斜率
  3116. *
  3117. * param
  3118. * start 起点坐标
  3119. * end 终点坐标
  3120. * slope 计算后的斜率
  3121. * return
  3122. * 斜率存在返回0,否则返回-1
  3123. *
  3124. */
  3125. int Card::calc_slope(std::shared_ptr<POS> start,std::shared_ptr<POS> end,double & slope)
  3126. {
  3127. int nRet = -1;
  3128. if (abs(end->posx - start->posx) < ZERO_PRECISION)
  3129. {
  3130. //斜率不存在
  3131. }else{
  3132. slope = (end->posy - start->posy)/(end->posx - start->posx);
  3133. nRet = 0;
  3134. }
  3135. return nRet;
  3136. }
  3137. int Card::CheckSolutionByFit(int ret,std::shared_ptr<POS>& pos)
  3138. {
  3139. int fit_ret = 0;
  3140. //拟合预测解
  3141. is_ref_pos = true;
  3142. std::shared_ptr<POS> fit_pos = GetPosFromFittingData();
  3143. if (fit_pos == nullptr)
  3144. {
  3145. return 1;
  3146. }
  3147. //存在如下情况,解是个错误解
  3148. if (ret && fit_ret == 0)
  3149. {
  3150. //如果加速度和速度失败了或选解选不出来,而且有拟合数据了
  3151. //则直接使用拟合解
  3152. pos->posx = fit_pos->posx;
  3153. pos->posy = fit_pos->posy;
  3154. pos->cx = fit_pos->cx;
  3155. pos->cy = fit_pos->cy;
  3156. pos->cvx = fit_pos->cvx;
  3157. pos->cvy = fit_pos->cvy;
  3158. pos->is_fit = fit_pos->is_fit;
  3159. //如果是拟合出来的点就不能作为参考点
  3160. is_ref_pos = !pos->is_fit;
  3161. }else{
  3162. if (fit_ret == 0)
  3163. {
  3164. //如果有拟合数据
  3165. //如果选出了一个解,需要判断此解和预测值得距离
  3166. double distance = 0.0;
  3167. distance = sqrt(pow(pos->posx - fit_pos->posx,2) + pow(pos->posy - fit_pos->posy,2));
  3168. if (distance > 10)
  3169. {
  3170. //如果定位出的解和拟合解的距离差大于10,则认为拟合解可信
  3171. pos->posx = fit_pos->posx;
  3172. pos->posy = fit_pos->posy;
  3173. pos->cx = fit_pos->cx;
  3174. pos->cy = fit_pos->cy;
  3175. pos->cvx = fit_pos->cvx;
  3176. pos->cvy = fit_pos->cvy;
  3177. pos->is_fit = fit_pos->is_fit;
  3178. //如果是拟合出来的点就不能作为参考点
  3179. is_ref_pos = !pos->is_fit;
  3180. }
  3181. }
  3182. if (fit_ret && ret == 0)
  3183. {
  3184. //表示没有拟合值,但解是可信的,则直接返回0
  3185. return 0;
  3186. }
  3187. }
  3188. return fit_ret;
  3189. }
  3190. int Card::CheckSolutionBySpeed(std::shared_ptr<POS>& pos)
  3191. {
  3192. if (pos->posx == INVALID_COORDINATE && pos->posy == INVALID_COORDINATE)
  3193. {
  3194. return 1;
  3195. }
  3196. //double speed = sqrt(pow(pos->cvx,2) + pow(pos->cvy,2));
  3197. if (m_nLastLocateT != 0)
  3198. {
  3199. //如果第一次定位不做速度的判别
  3200. double speed = sqrt(pow(pos->cvx,2) + pow(pos->cvy,2));
  3201. speed *= 3.6; //转为km/h
  3202. if (speed > MAX_VECHILE_SPEED)
  3203. {
  3204. return 1;
  3205. }
  3206. }
  3207. return 0;
  3208. }
  3209. bool Card::CheckCardCtValid()
  3210. {
  3211. bool bRet = true;
  3212. // 本次序号小于上次序号,存在如下情况
  3213. // 1、跨周期,如本次0,上次为65535,另外进入盲区也可能跨周期后回来
  3214. // 2、卡重置,从0开始,初始状态值为-1
  3215. // 3、序号较连续,新数据先到,旧数据后到,会造成跳动
  3216. if (this->time_stamp_cal < this->time_stamp_cal_last)
  3217. {
  3218. if (this->time_stamp_cal - this->time_stamp_last < MAX_SYNCTIME_DELAY_NUM)
  3219. {
  3220. }
  3221. else
  3222. {
  3223. // 小于5,认为会影响数据
  3224. this->x = this->last_locate.x;
  3225. this->y = this->last_locate.y;
  3226. this->z = 0;
  3227. m_afmData.bStatus = true;
  3228. m_afmData.strCardId = this->card_id;
  3229. m_afmData.nCardStamp = this->time_stamp_cal;
  3230. m_afmData.nType = ALGO_FAILED_CONDITION_4;
  3231. ALGORITHM_FAILED(ALGO_FAILED_CONDITION_4);
  3232. //此处需要输出计数
  3233. return false;
  3234. }
  3235. }
  3236. return bRet;
  3237. }
  3238. void Card::set_speed( double v )
  3239. {
  3240. //this->final_v = v; // lyl added: final_v only can be controled by pull_data()
  3241. }
  3242. // 检查dist数据有效性
  3243. int Card::CheckDistData(int cnt)
  3244. {
  3245. LOCATION_SYSTEM_BRANCH(LOCATION_SYSTEM_BRANCH_140);
  3246. GetLocalTime(&m_afmData.st);
  3247. // dist数据少于两条直接退出
  3248. if(cnt < 2){
  3249. LOCATION_SYSTEM_BRANCH(LOCATION_SYSTEM_BRANCH_141);
  3250. m_afmData.bStatus = true;
  3251. m_afmData.strCardId = this->card_id;
  3252. m_afmData.nType = ALGO_FAILED_CONDITION_15;
  3253. ALGORITHM_FAILED(ALGO_FAILED_CONDITION_15);
  3254. return DIST_COUNT_LESS_THAN_TWO;
  3255. }
  3256. //主要处理当相同卡的时间戳的数据中存在同步序号大于5的情况,如果有大于5的数据则丢弃此数据
  3257. int k = 0;
  3258. int dst = 0;
  3259. int st = 0;
  3260. bool bRet = false;
  3261. // 获取最大时间同步值
  3262. for(DistMap::iterator it = _dists.front().distmap.begin(); it != _dists.front().distmap.end(); ++it,k++){
  3263. if(k==0){
  3264. st = it->second->sync_num;
  3265. }else{
  3266. if(st < it->second->sync_num){
  3267. //未考虑跨周期
  3268. st = it->second->sync_num;
  3269. }
  3270. }
  3271. }
  3272. map<unsigned long long,std::shared_ptr<_coordinate>> mp_dists_locate_ex;
  3273. mp_dists_locate_ex.clear();
  3274. bool bExist = false;
  3275. // 筛选掉线性插值异常的数据
  3276. for(DistMap::iterator it = _dists.front().distmap.begin(); it != _dists.front().distmap.end(); ++it){
  3277. //如果同步时间戳存在异常值,则不走算法定位,直接返回上一次结果值
  3278. if(LLONG_MAX == it->second->tt){
  3279. bExist = true;
  3280. LOCATION_SYSTEM_BRANCH(LOCATION_SYSTEM_BRANCH_144);
  3281. }else
  3282. {
  3283. mp_dists_locate_ex.insert(make_pair(it->second->tt,it->second));
  3284. }
  3285. }
  3286. if (bExist)
  3287. {
  3288. //因为存在异常数据,如果抛弃了几条,剩下的数据定位错误会导致后续所有定位成功的都失败
  3289. return DIST_COUNT_LESS_FOR_TIMESTAMP_ERROR;
  3290. }
  3291. if(mp_dists_locate_ex.size() < 2){
  3292. LOCATION_SYSTEM_BRANCH(LOCATION_SYSTEM_BRANCH_143);
  3293. this->x = this->last_locate.x;
  3294. this->y = this->last_locate.y;
  3295. this->z = 0;
  3296. m_afmData.bStatus = true;
  3297. m_afmData.strCardId = this->card_id;
  3298. m_afmData.nType = ALGO_FAILED_CONDITION_2;
  3299. ALGORITHM_FAILED(ALGO_FAILED_CONDITION_2);
  3300. return DIST_COUNT_LESS_FOR_TIMESTAMP_ERROR;
  3301. }
  3302. // 筛选掉同步序号与最大值差5的数据
  3303. mp_dists_locate.clear();
  3304. map<unsigned long long,std::shared_ptr<_coordinate>>::iterator it_mpdl = mp_dists_locate_ex.begin();
  3305. for(;it_mpdl!=mp_dists_locate_ex.end();++it_mpdl){
  3306. if(st - it_mpdl->second->sync_num < 5){
  3307. mp_dists_locate.insert(make_pair(it_mpdl->second->tt, it_mpdl->second));
  3308. }else{
  3309. LOCATION_SYSTEM_BRANCH(LOCATION_SYSTEM_BRANCH_142);
  3310. }
  3311. }
  3312. if(mp_dists_locate.size() < 2){
  3313. LOCATION_SYSTEM_BRANCH(LOCATION_SYSTEM_BRANCH_143);
  3314. this->x = this->last_locate.x;
  3315. this->y = this->last_locate.y;
  3316. this->z = 0;
  3317. m_afmData.bStatus = true;
  3318. m_afmData.strCardId = this->card_id;
  3319. m_afmData.nType = ALGO_FAILED_CONDITION_2;
  3320. ALGORITHM_FAILED(ALGO_FAILED_CONDITION_2);
  3321. return DIST_COUNT_LESS_FOR_SYNC_NUM_DIFFER_FIVE;
  3322. }
  3323. if (!CheckCardCtValid())
  3324. {
  3325. //如果卡的ct存在当前比之前的小,且是未跨周期的情况下,则认为此组数据无效
  3326. return DIST_COUNT_CARD_CUR_CT_LESS_LAST;
  3327. }
  3328. return 0;
  3329. }
  3330. /*
  3331. * 组装数据
  3332. *
  3333. * param
  3334. * pRdm
  3335. *
  3336. * return
  3337. * 成功返回0,失败返回非零值
  3338. *
  3339. */
  3340. int Card::AssembleDistData(std::shared_ptr<ReceiveDataMap> pRdm)
  3341. {
  3342. int maxSyncTimes = 0;
  3343. //保存加速度当前状态和上次状态
  3344. int acce_state = 0;
  3345. int acce_state_last = 0;
  3346. int ins_direction = 0;
  3347. int card_time_stamp = 0;
  3348. map<unsigned long long,std::shared_ptr<_coordinate>>::iterator it_mpdl = mp_dists_locate.begin();
  3349. int i = 0;
  3350. for(;it_mpdl!=mp_dists_locate.end();++it_mpdl){
  3351. if(i==0){
  3352. card_time_stamp = it_mpdl->second->t;
  3353. maxSyncTimes = it_mpdl->second->sync_num;
  3354. acce_state = it_mpdl->second->acce_state;
  3355. acce_state_last = it_mpdl->second->acce_state_last;
  3356. ins_direction = it_mpdl->second->ins_direction;
  3357. }
  3358. else{
  3359. if(maxSyncTimes < it_mpdl->second->sync_num){
  3360. maxSyncTimes = it_mpdl->second->sync_num;
  3361. acce_state = it_mpdl->second->acce_state;
  3362. acce_state_last = it_mpdl->second->acce_state_last;
  3363. ins_direction = it_mpdl->second->ins_direction;
  3364. }
  3365. }
  3366. ReceiveDataMap::iterator prdm_it = pRdm->find(it_mpdl->second->tt);
  3367. if(prdm_it == pRdm->end()){
  3368. //保存信息用于定位
  3369. std::shared_ptr<ReceiveData> prd = std::make_shared<ReceiveData>();
  3370. prd->reader_id = it_mpdl->second->reader_id;
  3371. prd->antenna_id = it_mpdl->second->antenna_id;
  3372. prd->rec_time_stamp = it_mpdl->second->tt;
  3373. prd->x = it_mpdl->second->x*this->map_scale;
  3374. prd->y = it_mpdl->second->y*this->map_scale;
  3375. prd->z = it_mpdl->second->z*this->map_scale;
  3376. prd->special = it_mpdl->second->special;
  3377. if (prd->rec_time_stamp > 0)
  3378. {
  3379. pRdm->insert(make_pair(prd->rec_time_stamp,prd));
  3380. }
  3381. }
  3382. i++;
  3383. }
  3384. m_nCalcSyncNum = maxSyncTimes;
  3385. acce_cur_state = acce_state;
  3386. time_stamp_cal = card_time_stamp;
  3387. return 0;
  3388. }
  3389. /*
  3390. * 组装数据
  3391. *
  3392. * param
  3393. *
  3394. * return
  3395. *
  3396. *
  3397. */
  3398. int Card::AssembleDistData(map<unsigned long long, std::shared_ptr<_coordinate>>& p_dist_locate)
  3399. {
  3400. int i = 0;
  3401. int nCount = 0;
  3402. bool bFound = false;
  3403. unordered_map<int,std::list<double>> check_dist_data;
  3404. for(DistMap::iterator it = _dists.front().distmap.begin(); it != _dists.front().distmap.end(); ++it){
  3405. bFound = false;
  3406. int tct = 0;
  3407. tct = it->second->t;
  3408. // 判断是否已经加入到列表
  3409. for(int j = 0; j < i; j++){
  3410. if(p_dist_locate.find(it->second->reader_id) != p_dist_locate.end()){
  3411. //已存在,则退出查找for循环
  3412. bFound = true;
  3413. break;
  3414. }
  3415. }
  3416. if(i < DIST_COUNT && !bFound){
  3417. if (abs(it->second->d + 1.0) < ZERO_PRECISION)
  3418. {
  3419. //即计算的距离为-1,抛弃此数据
  3420. continue;
  3421. }else{
  3422. p_dist_locate.insert(make_pair(i,(it->second)));
  3423. check_dist_data[it->second->reader_id].push_back(it->second->d);
  3424. }
  3425. i++;
  3426. }
  3427. }
  3428. //二维或三维定位不需要地图路径集概念
  3429. //做一次筛选,如果dist中的分站在地图集中不存在,则删除此数据
  3430. //避免如下情况的出现:608,502分站相同ct值的数据,但502分站无地图集,此时会走两条数据的处理逻辑,这是错误的
  3431. /*for (map<unsigned long long, std::shared_ptr<_coordinate>>::iterator it_dist = p_dist_locate.begin();it_dist != p_dist_locate.end();)
  3432. {
  3433. int nReaderId = 0;
  3434. nReaderId = it_dist->second->reader_id;
  3435. ReaderPathMap::iterator it_tof_path = pTofReaderPathMap->find(nReaderId);
  3436. if (it_tof_path == pTofReaderPathMap->end())
  3437. {
  3438. it_dist = p_dist_locate.erase(it_dist);
  3439. }else{
  3440. ++it_dist;
  3441. }
  3442. }*/
  3443. nCount = p_dist_locate.size();
  3444. //如果存在两天线的测距距离是一样的情况,抛弃此次的数据
  3445. if (nCount >= 2)
  3446. {
  3447. bool bFind = false;
  3448. for (map<unsigned long long, std::shared_ptr<_coordinate>>::iterator tmp_dist = p_dist_locate.begin();tmp_dist != p_dist_locate.end();++tmp_dist)
  3449. {
  3450. bFind = false;
  3451. map<unsigned long long, std::shared_ptr<_coordinate>>::iterator comp_dist = tmp_dist;
  3452. std::advance(comp_dist,1);
  3453. for (;comp_dist != p_dist_locate.end();comp_dist++)
  3454. {
  3455. if (abs(comp_dist->second->d - tmp_dist->second->d) < ZERO_PRECISION && comp_dist->second->reader_id == tmp_dist->second->reader_id)
  3456. {
  3457. bFind = true;
  3458. break;
  3459. }
  3460. }
  3461. if (bFind)
  3462. {
  3463. break;
  3464. }
  3465. }
  3466. if (bFind)
  3467. {
  3468. return 1;
  3469. }
  3470. }
  3471. //检查数据,如果相同分站的两天线的距离差大于3米,正常值在1~2米,则此分站的数据被抛弃
  3472. for (unordered_map<int,std::list<double>>::iterator it = check_dist_data.begin();it != check_dist_data.end();++it)
  3473. {
  3474. //如果相同分站有两条距离数据,即表明此分站两天线都有测距信息
  3475. if ((*it).second.size() == 2)
  3476. {
  3477. //获取两天线的距离
  3478. std::list<double>::iterator it_first = it->second.begin();
  3479. std::list<double>::iterator it_second = it_first;
  3480. std::advance(it_second,1);
  3481. //计算两天线的距离差,如果大于3,则从p_dist_locate中删除此分站的信息
  3482. double distance_diff = 0.0;
  3483. distance_diff = fabs(*it_first - *it_second);
  3484. if (distance_diff > 3)
  3485. {
  3486. //进入此条件,表示两分站的测距有问题
  3487. for (map<unsigned long long, std::shared_ptr<_coordinate>>::iterator tmp_dist = p_dist_locate.begin();tmp_dist != p_dist_locate.end();)
  3488. {
  3489. //从定位数据中删除此分站的数据
  3490. if (tmp_dist->second->reader_id == it->first)
  3491. {
  3492. tmp_dist = p_dist_locate.erase(tmp_dist);
  3493. }else{
  3494. ++tmp_dist;
  3495. }
  3496. }
  3497. }
  3498. }
  3499. }
  3500. nCount = p_dist_locate.size();
  3501. if (nCount <= 0)
  3502. {
  3503. return 1;
  3504. }
  3505. return 0;
  3506. }
  3507. int Card::SaveCardAlgoData(std::shared_ptr<POS>& pos)
  3508. {
  3509. int nRet = 0;
  3510. if (ranging_type == LOCATEDATATYPE::LDT_TDOA)
  3511. {
  3512. switch (pos->dimension)
  3513. {
  3514. case _1D:
  3515. nRet = SaveTdoaData(pos);
  3516. break;
  3517. case _2D:
  3518. nRet = save_tdoa_data_2d(pos);
  3519. break;
  3520. case _3D:
  3521. break;
  3522. }
  3523. }else if (ranging_type == LOCATEDATATYPE::LDT_TOF ||ranging_type == LOCATEDATATYPE::LDT_TOF_INS)
  3524. {
  3525. this->is_deal_by_algo = true;
  3526. nRet = SaveTofData(pos);
  3527. }
  3528. if (nRet == 0)
  3529. {
  3530. last_correct_position_info_time = time(NULL);
  3531. }
  3532. return nRet;
  3533. }
  3534. /*
  3535. * 保存tof定位结果数据
  3536. *
  3537. * param
  3538. * pos 定位结果
  3539. *
  3540. * return
  3541. * 成功返回0,否则返回1
  3542. */
  3543. int Card::SaveTofData(const std::shared_ptr<POS> pos)
  3544. {
  3545. int _algorithm_path = 0;
  3546. if(!CheckPositionValid(pos)){
  3547. return 1;
  3548. }else{
  3549. //int _ret = m_position_optimizer->push_raw_data(_data_object);
  3550. origin_locate.x = pos->cx;
  3551. origin_locate.y = pos->cy;
  3552. if (is_algo_first_location)
  3553. {
  3554. is_algo_first_location = false;
  3555. }
  3556. switch(card_type){
  3557. case CT_VEHICLE:
  3558. {
  3559. //如果是车,查看其中是否有已上车的人卡,将已上车的人卡坐标绑定为车的坐标
  3560. /*for (CardMap::iterator it_card = mp_person_in_vechicle_list->begin();it_card != mp_person_in_vechicle_list->end();++it_card)
  3561. {
  3562. if (PERSON_ON_VEHICLE_STATUS::PERSON_ON_VEHICLE == it_card->second->person_on_card)
  3563. {
  3564. it_card->second->x = pos->posx;
  3565. it_card->second->y = pos->posy;
  3566. }
  3567. }*/
  3568. }
  3569. break;
  3570. case CT_PERSON:
  3571. {
  3572. }
  3573. break;
  3574. }
  3575. char chLog[200] = {0};
  3576. sprintf_s(chLog,"card:%s, ct:%d, x:%.2f, y:%.2f, reader:%d "
  3577. , this->card_id.c_str(), this->time_stamp_cal
  3578. , pos->cx, pos->cy
  3579. , p_reader->reader_id
  3580. );
  3581. std::string log = chLog;
  3582. Log::write_log(FILE_TYPE::RAW_LOCATION_DATA,log,true);
  3583. time_last_tof = time_cur_algo;
  3584. x = pos->cx;
  3585. y = pos->cy;
  3586. origin_locate.x = this->last_locate.x = pos->cx;
  3587. origin_locate.y = this->last_locate.y = pos->cy;
  3588. origin_locate.t = time_stamp_cal;
  3589. this->t = this->last_locate.t = time_stamp_cal;
  3590. this->time_stamp_cal_last = time_stamp_cal;
  3591. }
  3592. return 0;
  3593. }
  3594. /*
  3595. * 判断正向,反向
  3596. *
  3597. * param
  3598. * (x1,y1) 起点坐标
  3599. * (x2,y2) 终点坐标
  3600. *
  3601. * return
  3602. *
  3603. */
  3604. // int Card::get_direction(double x1,double y1,double x2,double y2)
  3605. // {
  3606. //
  3607. // int nStream = 0;
  3608. // //车辆上下行确定
  3609. // //3个条件:起点(x1,y1),终点(x2,y2)
  3610. // //1.x1==x2的情况下,y2>y1为下行
  3611. // //2.y1==y2的情况下,x2<x1为下行
  3612. // //3.x2<x1且y2>y1为下行
  3613. // //其他情况为上行
  3614. // if (((x1 == x2 || abs(x1 - x2) < 1E-2) && y1 < y2 )
  3615. // ||((y1 == y2 || abs(y1 - y2) < 1E-2) && x2 < x1)
  3616. // ||(x1 > x2 && y1 < y2))
  3617. // {
  3618. // nStream = NEGTIVE_DIRECTION;
  3619. // }
  3620. // else
  3621. // {
  3622. // nStream = POSTIVE_DIRECTION;
  3623. // }
  3624. //
  3625. // return nStream;
  3626. // }
  3627. int Card::SaveTdoaData(const std::shared_ptr<POS> pos)
  3628. {
  3629. //if (IsIdleStatus(pos))
  3630. if(false)
  3631. {
  3632. this->x = this->last_locate.x;
  3633. this->y = this->last_locate.y;
  3634. this->z = this->last_locate.z;
  3635. }else{
  3636. this->x = pos->cx;
  3637. this->y = pos->cy;
  3638. this->z = pos->cz;
  3639. Position p;
  3640. p.x = pos->posx;
  3641. p.y = pos->posy;
  3642. pTaylorWls->set_position(p);
  3643. char chLog[MAX_LOG_LENGTH] = {0};
  3644. std::string log = "";
  3645. #ifdef _DEBUG
  3646. sprintf_s(chLog,"card:%s, ct:%d, x:%.2f, y:%.2f, reader:%d"
  3647. , this->card_id.c_str(), this->time_stamp_cal
  3648. , pos->cx, pos->cy
  3649. , 0);
  3650. #else
  3651. sprintf_s(chLog,"card:%s, ct:%d, x:%.2f, y:%.2f, reader:%d"
  3652. , this->card_id.c_str(), this->time_stamp_cal
  3653. , pos->cx, pos->cy
  3654. , p_reader->reader_id);
  3655. #endif
  3656. log = chLog;
  3657. Log::write_log(FILE_TYPE::RAW_LOCATION_DATA,log,true);
  3658. //非怠速状态
  3659. if (!is_algo_first_location)
  3660. {
  3661. //第一定位不计算上下行方向
  3662. if (abs(this->last_locate.x) > 1E-5 || abs(this->last_locate.y) > 1E-5)
  3663. {
  3664. //获得上下行方向
  3665. this->m_nStream = GetStream(this->last_locate.x,this->last_locate.y,this->x,this->y);
  3666. }
  3667. }
  3668. this->last_locate.x = this->x;
  3669. this->last_locate.y = this->y;
  3670. this->last_locate.z = this->z;
  3671. this->last_locate.sync_num = this->m_nCalcSyncNum;
  3672. this->last_locate.acceleration = pos->av;
  3673. this->time_stamp_cal_last = this->time_stamp_cal;
  3674. int nSign = 1;
  3675. if(pos->cvx == 0){
  3676. if(pos->cvy > 0){
  3677. nSign = 1;
  3678. }
  3679. }else{
  3680. if(pos->cvx > 0){
  3681. nSign = 1;
  3682. }else{
  3683. nSign = -1;
  3684. }
  3685. }
  3686. this->m_nMoveDirection = nSign;
  3687. if (!is_algo_first_location)
  3688. {
  3689. //第一次定位不计算速度
  3690. //速度的计算采用求平均的方式
  3691. double speed = sqrt(pow(pos->cvx,2) + pow(pos->cvy,2));
  3692. speed *=3.6; //转为km/h
  3693. if (vt_his_speed.size() == 3)
  3694. {
  3695. vt_his_speed.pop_front();
  3696. }
  3697. if (speed > 1E-4)
  3698. {
  3699. vt_his_speed.push_back(speed);
  3700. }
  3701. int total = 0;
  3702. double sum_speed = 0;
  3703. for (list<double>::iterator it = vt_his_speed.begin();it != vt_his_speed.end();++it)
  3704. {
  3705. if (*it > 0)
  3706. {
  3707. sum_speed += *it;
  3708. total++;
  3709. }
  3710. }
  3711. double av = 0;
  3712. if (total > 0)
  3713. {
  3714. av = sum_speed / total;
  3715. }
  3716. if (STATE_ACCE_STATIC == this->acce_cur_state || STATE_ACCE_IDLING == this->acce_cur_state)
  3717. {
  3718. this->v = 0;
  3719. }
  3720. else
  3721. {
  3722. this->v = av*nSign;
  3723. }
  3724. }
  3725. }
  3726. this->last_locate.sync_num = this->m_nCalcSyncNum;
  3727. this->m_pKalmanFilter->m_pCar->t = this->m_nLastLocateT = this->m_nCalcSyncNum;
  3728. this->last_vx = pos->cvx;
  3729. this->last_vy = pos->cvy;
  3730. if (is_algo_first_location)
  3731. {
  3732. is_algo_first_location = false;
  3733. }
  3734. if (FIT_POSITION_NUM*4 == cur_fit_nums)
  3735. {
  3736. this->v = 0;
  3737. }
  3738. this->last_locate.v = this->v;
  3739. if(pos->update){
  3740. sync_data sdNew;
  3741. sdNew.sync_num = this->m_nCalcSyncNum;
  3742. sdNew.x = this->x;
  3743. sdNew.y = this->y;
  3744. sdNew.vx = this->last_vx;
  3745. sdNew.vy = this->last_vy;
  3746. m_syncNumList.push_back(sdNew);
  3747. }
  3748. //保存历史数据
  3749. std::shared_ptr<POS> p = std::make_shared<POS>();
  3750. p->posx = this->x*map_scale;
  3751. p->posy = this->y*map_scale;
  3752. p->card_count = this->time_stamp_cal;
  3753. //拟合点作为参考点
  3754. //拟合数据的计算
  3755. if (his_pos.size() == REF_POSITION_NUM)
  3756. {
  3757. his_pos.pop_front();
  3758. }
  3759. his_pos.push_back(p);
  3760. int nRef = REF_POSITION_NUM * 4;
  3761. if (long_his_pos.size() == nRef)
  3762. {
  3763. long_his_pos.pop_front();
  3764. }
  3765. long_his_pos.push_back(p);
  3766. if(is_ref_pos)
  3767. {
  3768. //保存历史数据
  3769. std::shared_ptr<POS> p = std::make_shared<POS>();
  3770. p->posx = this->x*map_scale;
  3771. p->posy = this->y*map_scale;
  3772. p->card_count = this->time_stamp_cal;
  3773. //拟合数据的计算
  3774. if (REF_POSITION_NUM == his_pos.size())
  3775. {
  3776. his_pos.pop_front();
  3777. }
  3778. his_pos.push_back(p);
  3779. if (REF_POSITION_NUM == his_pos.size())
  3780. {
  3781. if (!have_long_fit_pos)
  3782. {
  3783. CalcFittingData(his_pos);
  3784. cur_fit_nums = 0;
  3785. }
  3786. }
  3787. int nRef = REF_POSITION_NUM * 4;
  3788. if (long_his_pos.size() == nRef)
  3789. {
  3790. long_his_pos.pop_front();
  3791. }
  3792. long_his_pos.push_back(p);
  3793. if (long_his_pos.size() == nRef)
  3794. {
  3795. CalcLongFittingData();
  3796. cur_fit_nums = 0;
  3797. }
  3798. }
  3799. return 0;
  3800. }
  3801. /*
  3802. * 通过加速度或速度阈值来检查p定位结果的正确性
  3803. *
  3804. * param
  3805. * p 定位结果
  3806. *
  3807. * return
  3808. * 如果通过检查返回0,否则返回非零值
  3809. */
  3810. int Card::CheckSolution(std::shared_ptr<POS>& p)
  3811. {
  3812. //定位成功
  3813. double cx = p->cx;
  3814. double cy = p->cy;
  3815. double cz = p->cz;
  3816. double cvx = 0.0,cvy = 0.0,cvz = 0.0;
  3817. double av = 0.0;
  3818. if(is_algo_first_location){
  3819. sync_data sdNew;
  3820. sdNew.sync_num = this->m_nCalcSyncNum;
  3821. sdNew.x = cx;
  3822. sdNew.y = cy;
  3823. sdNew.vx = 0;
  3824. sdNew.vy = 0;
  3825. sdNew.update = false;
  3826. m_syncNumList.push_back(sdNew);
  3827. }else{
  3828. //现在的关于同步序号的处理是这样的:
  3829. //如果定位成功,就把这次定位成功的同步数据:同步序号,坐标;x,y方向的速度,扔到一个队列里,
  3830. //后来定位成功的就会先根据同步序号差用加速度抛一次;
  3831. //抛不掉,就用队列里的同步数据(从后往前找),找到第一个与当前同步序号相差大于5的同步数据来进行第二次计算速度以及加速度,
  3832. //如果加速度大于5,就不要此次的定位数据,
  3833. //如果通过加速度判断就将队列中从头开始到此同步数据的所有元素都丢弃,并插入新的此次同步数据
  3834. double interval_time = algorithm::base::ANCHOR_SYNCHRONIZATION_PERIOD;
  3835. double deltaT = 0;
  3836. sync_data sd;
  3837. if (this->b_long_interval)
  3838. {
  3839. //此段代码用于将上一次定位是根据两个时间差是个很大值而定位出的结果
  3840. //当后续定位时就和最近的定位结果进行比较
  3841. //例如:当上一次同步序号是14321,它定位时比较的同步序号是14200,时间差大于20多秒
  3842. //这时我们就将b_long_interval置为true
  3843. //当本次定位,同步序号是14326,,这时就需要根据最近的14321进行判断
  3844. list<sync_data>::reverse_iterator it = m_syncNumList.rbegin();
  3845. sync_data sd = *it;
  3846. //以下计算deltaT还需要考虑卡的同步序号轮回的情况。
  3847. if (m_nCalcSyncNum > it->sync_num)
  3848. {
  3849. deltaT = (m_nCalcSyncNum - sd.sync_num)*interval_time;
  3850. }
  3851. else
  3852. {
  3853. deltaT = (m_nCalcSyncNum + 65536 - sd.sync_num)*interval_time;
  3854. }
  3855. if (deltaT < 10 && deltaT > 0)
  3856. {
  3857. this->b_long_interval = false;
  3858. }
  3859. //避免同一个同步序号下存在多个不同卡序号
  3860. if (deltaT < 1E-2)
  3861. {
  3862. deltaT = 0.2;
  3863. }
  3864. p->diff_reader_sync_num = deltaT;
  3865. p->ref_x = sd.x;
  3866. p->ref_y = sd.y;
  3867. cvx = abs((cx - sd.x)*this->map_scale/deltaT);
  3868. cvy = abs((cy - sd.y)*this->map_scale/deltaT);
  3869. double avx = (cvx - sd.vx) / deltaT;
  3870. double avy = (cvy - sd.vy) / deltaT;
  3871. av = sqrt(pow(avx,2) + pow(avy,2));
  3872. //车卡的加速度
  3873. switch(this->card_type){
  3874. case CT_PERSON:
  3875. if(av > PERSON_ACCELERATE_THRESHOLD){
  3876. p->reason = CHECK_PERSON_ACCE_OVER_SPEED;
  3877. p->posx = INVALID_COORDINATE;
  3878. p->posy = INVALID_COORDINATE;
  3879. p->posz = INVALID_COORDINATE;
  3880. p->cx = p->posx / map_scale;
  3881. p->cy = p->posy / map_scale;
  3882. p->cz = p->posz / map_scale;
  3883. this->b_long_interval = false;
  3884. m_afmData.bStatus = true;
  3885. m_afmData.strCardId = this->card_id;
  3886. m_afmData.nType = ALGO_FAILED_CONDITION_5;
  3887. ALGORITHM_FAILED(ALGO_FAILED_CONDITION_5);
  3888. return CHECK_PERSON_ACCE_OVER_SPEED;
  3889. }
  3890. break;
  3891. case CT_VEHICLE:
  3892. if(av > VECHILE_ACCELERATE_THRESHOLD){
  3893. //保留上次结果
  3894. p->reason = CHECK_VEHICLE_ACCE_OVER_SPEED;
  3895. p->posx = INVALID_COORDINATE;
  3896. p->posy = INVALID_COORDINATE;
  3897. p->posz = INVALID_COORDINATE;
  3898. p->cx = p->posx / map_scale;
  3899. p->cy = p->posy / map_scale;
  3900. p->cz = p->posz / map_scale;
  3901. this->b_long_interval = false;
  3902. m_afmData.bStatus = true;
  3903. m_afmData.strCardId = this->card_id;
  3904. m_afmData.nType = ALGO_FAILED_CONDITION_6;
  3905. ALGORITHM_FAILED(ALGO_FAILED_CONDITION_6);
  3906. return CHECK_VEHICLE_ACCE_OVER_SPEED;
  3907. }
  3908. break;
  3909. }
  3910. }else{
  3911. //从队列尾部开始查找,找到第一个同步序号与当前计算卡的同步序号相差5个以上的数据
  3912. list<sync_data>::reverse_iterator it;
  3913. bool bOverflow = false;
  3914. bool bfind =false;
  3915. for(it = m_syncNumList.rbegin();it!=m_syncNumList.rend();it++){
  3916. if(m_nCalcSyncNum - it->sync_num >= 5){
  3917. bfind = true;
  3918. sd = *it;
  3919. break;
  3920. }else{
  3921. if(m_nCalcSyncNum - it->sync_num < 0 && m_nCalcSyncNum < 100){
  3922. //如果最新同步号小于列表中的同步号则
  3923. if(m_nCalcSyncNum + 65536 - it->sync_num >=5 ){//5
  3924. bOverflow = true;
  3925. bfind = true;
  3926. sd = *it;
  3927. }
  3928. }else{
  3929. continue;
  3930. }
  3931. }//end else
  3932. }//end for
  3933. //根据溢出条件来计算deltaT
  3934. if(bOverflow){
  3935. deltaT = (m_nCalcSyncNum + 65536 - sd.sync_num)*interval_time;
  3936. }else{
  3937. deltaT = (m_nCalcSyncNum - sd.sync_num)*interval_time;
  3938. }
  3939. p->diff_reader_sync_num = deltaT;
  3940. p->ref_x = sd.x;
  3941. p->ref_y = sd.y;
  3942. //速度正负的判断:以x轴,y轴正向运动为正
  3943. //如果x相等,则y2 - y1 > 0为正
  3944. //其他情况,则x2 - x1 > 0 为正
  3945. int nSign = 1;
  3946. if(cx == sd.x){
  3947. if(cy > sd.y){
  3948. nSign = 1;
  3949. }else{
  3950. nSign = -1;
  3951. }
  3952. }else{
  3953. if(cx > sd.x){
  3954. nSign = 1;
  3955. }else{
  3956. nSign = -1;
  3957. }
  3958. }
  3959. //转为m/s
  3960. cvx = abs((cx - sd.x)*this->map_scale/deltaT);
  3961. cvy = abs((cy - sd.y)*this->map_scale/deltaT);
  3962. double cv = sqrt(pow(cvx,2) + pow(cvy,2));
  3963. cv = cv*nSign;
  3964. double avx = (cvx - sd.vx) / deltaT;
  3965. double avy = (cvy - sd.vy) / deltaT;
  3966. double av = sqrt(pow(avx,2) + pow(avy,2));
  3967. //车卡的加速度
  3968. switch(this->card_type){
  3969. case CT_PERSON:
  3970. if(av > PERSON_ACCELERATE_THRESHOLD){
  3971. p->reason = CHECK_PERSON_ACCE_OVER_SPEED;
  3972. p->posx = INVALID_COORDINATE;
  3973. p->posy = INVALID_COORDINATE;
  3974. p->posz = INVALID_COORDINATE;
  3975. p->cx = p->posx / map_scale;
  3976. p->cy = p->posy / map_scale;
  3977. p->cz = p->posz / map_scale;
  3978. m_afmData.bStatus = true;
  3979. m_afmData.strCardId = this->card_id;
  3980. m_afmData.nType = ALGO_FAILED_CONDITION_5;
  3981. ALGORITHM_FAILED(ALGO_FAILED_CONDITION_5);
  3982. return CHECK_PERSON_ACCE_OVER_SPEED;
  3983. }
  3984. break;
  3985. case CT_VEHICLE:
  3986. if(av > VECHILE_ACCELERATE_THRESHOLD){
  3987. //保留上次结果
  3988. p->reason = CHECK_VEHICLE_ACCE_OVER_SPEED;
  3989. p->posx = INVALID_COORDINATE;
  3990. p->posy = INVALID_COORDINATE;
  3991. p->posz = INVALID_COORDINATE;
  3992. p->cx = p->posx / map_scale;
  3993. p->cy = p->posy / map_scale;
  3994. p->cz = p->posz / map_scale;
  3995. m_afmData.bStatus = true;
  3996. m_afmData.strCardId = this->card_id;
  3997. m_afmData.nType = ALGO_FAILED_CONDITION_6;
  3998. ALGORITHM_FAILED(ALGO_FAILED_CONDITION_6);
  3999. return CHECK_VEHICLE_ACCE_OVER_SPEED;
  4000. }
  4001. break;
  4002. }
  4003. cv = cv*3.6;
  4004. double max_speed = 0.0;
  4005. switch (this->card_type)
  4006. {
  4007. case CT_PERSON:
  4008. max_speed = MAX_PERSON_SPEED;
  4009. break;
  4010. case CT_VEHICLE:
  4011. max_speed = MAX_VECHILE_SPEED;
  4012. break;
  4013. default:
  4014. break;
  4015. }
  4016. //速度的限制
  4017. if(fabs(cv) > max_speed){
  4018. p->reason = CHECK_VEHICLE_OVER_SPEED;
  4019. p->posx = INVALID_COORDINATE;
  4020. p->posy = INVALID_COORDINATE;
  4021. p->posz = INVALID_COORDINATE;
  4022. p->cx = p->posx / map_scale;
  4023. p->cy = p->posy / map_scale;
  4024. p->cz = p->posz / map_scale;
  4025. m_afmData.bStatus = true;
  4026. m_afmData.strCardId = this->card_id;
  4027. m_afmData.nType = ALGO_FAILED_CONDITION_7;
  4028. ALGORITHM_FAILED(ALGO_FAILED_CONDITION_7);
  4029. return CHECK_VEHICLE_OVER_SPEED;
  4030. }
  4031. //使用间隔来修正速度
  4032. if(deltaT - 1.0 >= 0){
  4033. if (deltaT > 10 && bfind)
  4034. {
  4035. this->b_long_interval = true;
  4036. }
  4037. //删除第一个元素到tmp(含)之间的所有元素
  4038. bool bStartDel = false;
  4039. for(list<sync_data>::reverse_iterator tmp = m_syncNumList.rbegin();tmp != m_syncNumList.rend();)
  4040. {
  4041. if(bStartDel){
  4042. tmp = list<sync_data>::reverse_iterator(m_syncNumList.erase((++tmp).base()));
  4043. }else{
  4044. if(*tmp == sd){
  4045. bStartDel = true;
  4046. }
  4047. ++tmp;
  4048. }
  4049. }
  4050. }
  4051. if (!bfind)
  4052. {
  4053. cvx = 0;
  4054. cvy = 0;
  4055. cvz = 0;
  4056. av = 0;
  4057. }
  4058. }
  4059. this->m_nSyncNumInList = sd.sync_num;
  4060. p->update = true;
  4061. }
  4062. p->cvx = cvx;
  4063. p->cvy = cvy;
  4064. p->cvz = cvz;
  4065. p->av = av;
  4066. return 0;
  4067. }
  4068. /*
  4069. * 通过校验后的数据, 保存到 m_syncNumList 容器
  4070. *
  4071. * param
  4072. * position 定位结果
  4073. */
  4074. void Card::saveToSyncNumDataList(int sync_num, double pixelX, double pixelY, double speedX, double speedY, bool updated)
  4075. {
  4076. sync_data sdNew;
  4077. sdNew.sync_num = sync_num;
  4078. sdNew.x = pixelX;
  4079. sdNew.y = pixelY;
  4080. sdNew.vx = speedX;
  4081. sdNew.vy = speedY;
  4082. sdNew.update = updated;
  4083. m_syncNumList.push_back(sdNew);
  4084. }
  4085. /*
  4086. * 通过趋向性判断解的可靠性,暂通过上下行判断
  4087. *
  4088. * param
  4089. * p 当前定位坐标信息
  4090. *
  4091. * return
  4092. * 通过验证返回0,否则返回错误码
  4093. *
  4094. */
  4095. int Card::CheckSulutionByStream(std::shared_ptr<POS> p)
  4096. {
  4097. if (INIT_STREAM == m_nStream)
  4098. {
  4099. return 0;
  4100. }
  4101. if (abs(this->last_locate.x) > 1E-5 || abs(this->last_locate.y) > 1E-5)
  4102. {
  4103. //去除采集刚启动,last_locate的坐标为零而导致判断车辆上下行方向错误的问题
  4104. //车辆上下行确定
  4105. //3个条件:起点(x1,y1),终点(x2,y2)
  4106. //1.x1==x2的情况下,y2>y1为下行
  4107. //2.y1==y2的情况下,x1>x2为下行
  4108. //3.x1>x2且y2>y1为下行
  4109. //其他情况为上行
  4110. int nStream = 0;
  4111. double cx = p->posx / (map_scale*1.0);
  4112. double cy = p->posy / (map_scale*1.0);
  4113. if ((this->last_locate.x == cx && cy > this->last_locate.y)
  4114. ||(this->last_locate.x > cx && cy == this->last_locate.y)
  4115. ||(this->last_locate.x > cx && cy > this->last_locate.y))
  4116. {
  4117. nStream = DOWN_STREAM;
  4118. }
  4119. else
  4120. {
  4121. nStream = UP_STREAM;
  4122. }
  4123. if (nStream != m_nStream)
  4124. {
  4125. return 1;
  4126. }
  4127. }
  4128. return 0;
  4129. }
  4130. bool Card::IsExistPath(int left,int right)
  4131. {
  4132. TDOAReaderPathMap::iterator it = pTdoaReaderPathMap->find(left);
  4133. if (it == pTdoaReaderPathMap->end())
  4134. {
  4135. return false;
  4136. }
  4137. ReaderPathMap::iterator it_s = it->second->find(right);
  4138. if (it_s == it->second->end())
  4139. {
  4140. return false;
  4141. }
  4142. return true;
  4143. }
  4144. /*
  4145. * 判断是否为怠速状态
  4146. *
  4147. * param
  4148. * pos 定位结果
  4149. *
  4150. * return
  4151. * 如果怠速则返回true,否则返回false
  4152. *
  4153. */
  4154. bool Card::IsIdleStatus(std::shared_ptr<POS> pos)
  4155. {
  4156. // 只根据最后位置比较,位移小于预设值认为是怠速
  4157. is_idling = sqrt(pow(pos->cx - this->x, 2) + pow(pos->cy - this->y, 2)) < MIN_IDLE_THRESHOLD ? true : false;
  4158. if(is_idling)
  4159. {
  4160. if(idle_count < IDLE_CONFIRM_TIME)
  4161. {
  4162. idle_count++;
  4163. }
  4164. }
  4165. else
  4166. {
  4167. idle_count = 0;
  4168. }
  4169. return is_idling;
  4170. bool bRet = false;
  4171. if (idle_pos_list.size() < REF_POSITION_NUM)
  4172. {
  4173. return false;
  4174. }
  4175. double acq = 0.3; //精度范围
  4176. _point cp;
  4177. cp.x = cp.y = cp.z = 0;
  4178. int i = 0;
  4179. double ax = 0,ay = 0;
  4180. //怠速队列数据够REF_POSITION_NUM个,计算怠速队列中数据的平均值;不够则直接返回
  4181. //if (idle_pos_list.size() == REF_POSITION_NUM)
  4182. {
  4183. for (std::list<std::shared_ptr<POS>>::iterator it = idle_pos_list.begin(); it != idle_pos_list.end();++it)
  4184. {
  4185. ax += (*it)->posx;
  4186. ay += (*it)->posy;
  4187. }
  4188. ax /= REF_POSITION_NUM;
  4189. ay /= REF_POSITION_NUM;
  4190. }
  4191. i = 0;
  4192. //计算怠速队列中数据是否都在以平均值为中心,半径是1米的范围内;如果都在,则为怠速,否则为非怠速
  4193. for (std::list<std::shared_ptr<POS>>::iterator it = idle_pos_list.begin(); it != idle_pos_list.end();++it)
  4194. {
  4195. double distance = 0;
  4196. distance = sqrt(pow((*it)->posx - ax,2) + pow((*it)->posy - ay,2));
  4197. if (distance < 1)
  4198. {
  4199. i++;
  4200. }else{
  4201. break;
  4202. }
  4203. }
  4204. if (i == 5)
  4205. {
  4206. bRet = true;
  4207. }
  4208. return bRet;
  4209. }
  4210. /*
  4211. * 判断定位结果回退,根据本次的上下行和上一次的上下行方向是否一致
  4212. *
  4213. * param
  4214. * pos 定位结果
  4215. *
  4216. * return
  4217. * 如果回退返回true,否则返回false
  4218. *
  4219. */
  4220. bool Card::IsRebound(std::shared_ptr<POS> pos)
  4221. {
  4222. //如果是第一次定位,则不判断回退
  4223. if (is_algo_first_location)
  4224. {
  4225. return false;
  4226. }
  4227. //如果上一次无方向或者本次定位结果异常,则不判断是否回退
  4228. if (m_nStream == 0 || !CheckPositionValid(pos))
  4229. {
  4230. return false;
  4231. }
  4232. int nStream = 0;
  4233. //上一次定位为起点,本次定位结果为终点,判断上下行方向
  4234. nStream = GetStream(last_locate.x,last_locate.y,pos->cx,pos->cy);
  4235. return nStream == this->m_nStream?false:true;
  4236. }
  4237. /*
  4238. * 检查解的有效性:1.非法值检查;2.指定无效数检查;3.零值检查
  4239. *
  4240. * param
  4241. * pos 定位结果
  4242. *
  4243. * return
  4244. * 如果pos中的posx,posy有效则返回true;否则返回false
  4245. *
  4246. */
  4247. bool Card::CheckPositionValid(const std::shared_ptr<POS> pos)
  4248. {
  4249. if (pos == nullptr)
  4250. {
  4251. return false;
  4252. }else if (_isnan(pos->posx) || _isnan(pos->posy))
  4253. {
  4254. return false;
  4255. }else if (pos->posx == INVALID_COORDINATE || pos->posy == INVALID_COORDINATE)
  4256. {
  4257. return false;
  4258. }else if (abs(pos->posx) < ZERO_PRECISION && abs(pos->posy) < ZERO_PRECISION)
  4259. {
  4260. //如果以后地图有(0,0)坐标的话,就需要删除此判断条件
  4261. return false;
  4262. }
  4263. return true;
  4264. }
  4265. bool Card::CheckPositionValid(const double& x,const double& y)
  4266. {
  4267. if (_isnan(x) || _isnan(y))
  4268. {
  4269. return false;
  4270. }else if (x == INVALID_COORDINATE || y == INVALID_COORDINATE)
  4271. {
  4272. return false;
  4273. }else if (abs(x) < HIGH_ZERO_PRECISION && abs(y) < HIGH_ZERO_PRECISION)
  4274. {
  4275. //如果以后地图有(0,0)坐标的话,就需要删除此判断条件
  4276. return false;
  4277. }
  4278. return true;
  4279. }
  4280. /*
  4281. * tof定位:根据一条距离及历史定位结果进行定位
  4282. *
  4283. * param
  4284. * pos 定位结果
  4285. *
  4286. * return
  4287. * 成功定位返回0,否则返回非零值
  4288. *
  4289. */
  4290. int Card::get_position_by_one_distance(std::shared_ptr<TOFReaderPathMap> trpm,std::shared_ptr<POS>& pos,const TOF_REFER_DATA refer_data)
  4291. {
  4292. //如果是第一次定位,则无法计算,必须要依赖历史值
  4293. if (is_algo_first_location)
  4294. {
  4295. return 1;
  4296. }
  4297. //如果上次坐标是无定位结果的,则无法处理
  4298. if (last_locate.x == INVALID_COORDINATE && last_locate.y == INVALID_COORDINATE)
  4299. {
  4300. return 1;
  4301. }
  4302. int nReaderId = 0;
  4303. nReaderId = refer_data.nReaderId;
  4304. int seg_num = 0;
  4305. //如果分站不存在,则返回
  4306. if(trpm->find(nReaderId) == trpm->end())
  4307. {
  4308. return 1;
  4309. }
  4310. seg_num = trpm->find(nReaderId)->second->nRealCalcPoints - 1;
  4311. //判断路径集的点数是否大于两个点,两点才可以构成线段路径
  4312. if(seg_num < 0 ){
  4313. return 1;
  4314. }
  4315. int j = 0;
  4316. int m = 0;
  4317. int nCounts = 0;
  4318. double xcross[MAX_READER_TDOA_PATH_NUMS] = {0};
  4319. double ycross[MAX_READER_TDOA_PATH_NUMS] = {0};
  4320. //对每一个线段所在的直线和以天线为圆心的圆求解,最多为两个解
  4321. for(int i = 0;i < seg_num;i++){
  4322. //如果线段两端点的坐标不存在,则寻找下一个线段
  4323. if(trpm->find(nReaderId)->second->px[i] == -1 || trpm->find(nReaderId)->second->px[i+1] == -1)
  4324. {
  4325. continue;
  4326. }
  4327. //求线段所在直线和分站readerid的antenna_idx天线为圆心,半径为dist的圆的两个交点
  4328. std::shared_ptr<SOLUTION> r = LocateAlgorithm::GetPos(trpm,nReaderId,refer_data.nAntennaIndex,refer_data.dDistance,i);
  4329. //如果解不存在,则继续寻找下一个线段
  4330. if(r == nullptr || r->nCount <= 0){
  4331. continue;
  4332. }
  4333. for (int j = 0;j<r->nCount;j++)
  4334. {
  4335. xcross[m] = r->x[j];
  4336. ycross[m] = r->y[j];
  4337. m++;
  4338. }
  4339. }
  4340. double v[MAX_READER_TDOA_PATH_NUMS] = {0};
  4341. double dt = 0;
  4342. dt = GetDeltaT();
  4343. int nNotOverThre = 0;
  4344. int nOverThre = 0;
  4345. for (int i = 0;i < m;i++)
  4346. {
  4347. double distance = 0;
  4348. distance = sqrt(pow(abs(xcross[i] - last_locate.x*map_scale),2) + pow(abs(ycross[i] - last_locate.y*map_scale),2));
  4349. v[i] = distance/dt;
  4350. v[i] *= 3.6;
  4351. double speed_thre = 0.0;
  4352. switch(card_type){
  4353. case CT_VEHICLE:
  4354. speed_thre = MAX_VECHILE_SPEED;
  4355. break;
  4356. case CT_PERSON:
  4357. speed_thre = MAX_PERSON_SPEED;
  4358. break;
  4359. }
  4360. if (v[i] > speed_thre)
  4361. {
  4362. nOverThre++;
  4363. }else{
  4364. nNotOverThre++;
  4365. }
  4366. }
  4367. double minV = 0.0;
  4368. int nIndex = -1;
  4369. for (int i = 0;i < m;i++)
  4370. {
  4371. if (i == 0)
  4372. {
  4373. minV = v[i];
  4374. nIndex = i;
  4375. continue;
  4376. }
  4377. if (v[i] < minV)
  4378. {
  4379. minV = v[i];
  4380. nIndex = i;
  4381. }
  4382. }
  4383. if (nOverThre > 0 && nNotOverThre > 0)
  4384. {
  4385. if (nIndex == -1)
  4386. {
  4387. return 1;
  4388. }
  4389. pos->posx = xcross[nIndex];
  4390. pos->posy = ycross[nIndex];
  4391. pos->posz = 0;
  4392. pos->cx = pos->posx/map_scale;
  4393. pos->cy = pos->posy/map_scale;
  4394. pos->cz = pos->posz/map_scale;
  4395. return 0;
  4396. }else{
  4397. if (m == 1)
  4398. {
  4399. pos->posx = xcross[nIndex];
  4400. pos->posy = ycross[nIndex];
  4401. pos->posz = 0;
  4402. pos->cx = pos->posx/map_scale;
  4403. pos->cy = pos->posy/map_scale;
  4404. pos->cz = pos->posz/map_scale;
  4405. return 0;
  4406. }else{
  4407. return 1;
  4408. }
  4409. }
  4410. return 0;
  4411. }
  4412. /*
  4413. * 检查tof定位解的有效性,通过速度和加速度方式校验
  4414. *
  4415. * param
  4416. * pos 定位结果
  4417. *
  4418. * return
  4419. * 通过检验返回true,否则返回false;
  4420. *
  4421. */
  4422. bool Card::CheckSolutionTof(std::shared_ptr<POS>& pos)
  4423. {
  4424. if (is_algo_first_location)
  4425. {
  4426. return true;
  4427. }
  4428. bool bRet = false;
  4429. double delta = 0.0;
  4430. delta = GetDeltaT();
  4431. if (delta > ZERO_PRECISION){
  4432. double distance = 0;
  4433. distance = sqrt(pow(pos->posx - last_locate.x*map_scale,2) + pow(pos->posy - last_locate.y*map_scale,2));
  4434. //计算速度
  4435. double v = 0;
  4436. v = distance/delta;
  4437. //计算加速度
  4438. double a = 0;
  4439. a = (v - final_v) / delta;
  4440. v *= 3.6;
  4441. double acce_thre = 0.0;
  4442. double speed_thre = 0.0;
  4443. switch(card_type){
  4444. case CT_VEHICLE:
  4445. acce_thre = VECHILE_ACCELERATE_THRESHOLD;
  4446. speed_thre = MAX_VECHILE_SPEED;
  4447. break;
  4448. case CT_PERSON:
  4449. acce_thre = PERSON_ACCELERATE_THRESHOLD;
  4450. speed_thre = MAX_PERSON_SPEED;
  4451. break;
  4452. }
  4453. if (a > acce_thre)
  4454. {
  4455. bRet = false;
  4456. }else{
  4457. pos->cvx = (pos->posx - last_locate.x*map_scale) / delta;
  4458. pos->cvy = (pos->posy - last_locate.y*map_scale) / delta;
  4459. bRet = true;
  4460. }
  4461. if (v > speed_thre)
  4462. {
  4463. bRet = false;
  4464. }
  4465. }
  4466. if (!bRet)
  4467. {
  4468. double output_v = 0.0;
  4469. if (fabs(final_v) > 100)
  4470. {
  4471. output_v = 100;
  4472. }
  4473. char log[MAX_LOG_LENGTH] = {0};
  4474. sprintf_s(log,"card_id: %s, ct: %d, distance: %.3f, posx: %.3f, posy: %.3f, last_locate.x: %.3f, last_locate.y: %.3f, deltaT: %d, a:%.3f, v: %.3f",/*, algo_time: %s, time_last: %s"*/
  4475. card_id.c_str(),
  4476. time_stamp_cal,
  4477. distance,
  4478. pos->posx/map_scale,
  4479. pos->posy/map_scale,
  4480. last_locate.x,
  4481. last_locate.y,
  4482. delta,
  4483. a,
  4484. output_v
  4485. );
  4486. Log::write_log(FILE_TYPE::ALGO_S,log,true);
  4487. }
  4488. return bRet;
  4489. }
  4490. /*
  4491. * 根据dists获取参考数据
  4492. *
  4493. * param
  4494. * tmp_dists_locate
  4495. * key
  4496. *
  4497. * return
  4498. * 返回参考数据
  4499. *
  4500. */
  4501. int Card::get_ref_data_from_dists(map<unsigned long long, std::shared_ptr<_coordinate>> tmp_dists_locate,const int& key,TOF_REFER_DATA& refer_data)
  4502. {
  4503. map<unsigned long long, std::shared_ptr<_coordinate>>::iterator it = tmp_dists_locate.find(key);
  4504. if (it != tmp_dists_locate.end())
  4505. {
  4506. time_cur_algo = it->second->cur_time;
  4507. reader_id_algo = it->second->reader_id;
  4508. refer_data.nCardTimeStamp = it->second->t;
  4509. refer_data.llDeltaTime = 0;
  4510. refer_data.nReaderId = it->second->reader_id;
  4511. refer_data.nAntennaIndex = it->second->antenna_id;
  4512. refer_data.dDistance = it->second->d;
  4513. refer_data.x = it->second->x;
  4514. refer_data.y = it->second->y;
  4515. refer_data.z = it->second->z;
  4516. refer_data.a = it->second->a;
  4517. }
  4518. return 0;
  4519. }
  4520. /*
  4521. * 1.先将在同一个分站的两天线的距离以及天线坐标保存
  4522. * 2.计算定位结果距离相同分站的两天线的距离差
  4523. * 3.根据距离差的方向和原有的天线距离的方向进行判断,如果一致,保留结果,如果不一致,则不保留此结果
  4524. *
  4525. * param
  4526. * pos 定位结果
  4527. * p_dist_locate 原始定位数据结果
  4528. *
  4529. * return
  4530. * 如果方向一致,返回0,否则返回1
  4531. */
  4532. int Card::position_data_filter_by_direction(std::shared_ptr<POS> pos,map<unsigned long long, std::shared_ptr<_coordinate>>& p_dist_locate)
  4533. {
  4534. //如果只有一条数据,则不用使用此逻辑处理
  4535. if (p_dist_locate.size() < 2)
  4536. {
  4537. return 0;
  4538. }
  4539. if (last_locate.x == INVALID_COORDINATE && last_locate.y == INVALID_COORDINATE)
  4540. {
  4541. return 0;
  4542. }
  4543. typedef struct Data{
  4544. double distance;
  4545. double x;
  4546. double y;
  4547. double z;
  4548. int antenna_id;
  4549. Data(){
  4550. distance = x = y = z = 0.0;
  4551. antenna_id = 0;
  4552. }
  4553. Data& operator=(Data& d){
  4554. this->antenna_id = d.antenna_id;
  4555. this->distance = d.distance;
  4556. this->x = d.x;
  4557. this->y = d.y;
  4558. this->z = d.z;
  4559. return *this;
  4560. }
  4561. }data;
  4562. //将分站的双天线的距离及坐标信息保存下来
  4563. unordered_map<int,unordered_map<int,data>> check_dist_data;
  4564. for(map<unsigned long long, std::shared_ptr<_coordinate>>::iterator it = p_dist_locate.begin(); it != p_dist_locate.end(); ++it){
  4565. data val;
  4566. val.distance = it->second->d;
  4567. val.x = it->second->x*map_scale;
  4568. val.y = it->second->y*map_scale;
  4569. val.antenna_id = it->second->antenna_id;
  4570. check_dist_data[it->second->reader_id].insert(std::make_pair(it->second->antenna_id,val));
  4571. }
  4572. bool bNotExist = true;
  4573. for (unordered_map<int,unordered_map<int,data>>::iterator it = check_dist_data.begin();it != check_dist_data.end();++it)
  4574. {
  4575. if (it->second.size() > 1)
  4576. {
  4577. int nOldSign = 0;
  4578. int nNewSign = 0;
  4579. //计算原分站双天线的距离差方向
  4580. data d1;
  4581. data d2;
  4582. for (unordered_map<int,data>::iterator it_ant = check_dist_data[it->first].begin();it_ant != check_dist_data[it->first].end();++it_ant)
  4583. {
  4584. switch (it_ant->second.antenna_id)
  4585. {
  4586. case 0:
  4587. d1 = it_ant->second;
  4588. break;
  4589. case 1:
  4590. d2 = it_ant->second;
  4591. break;
  4592. }
  4593. }
  4594. if (d1.distance > d2.distance)
  4595. {
  4596. nOldSign = 1;
  4597. }else{
  4598. nOldSign = -1;
  4599. }
  4600. //计算坐标距离分站双天线的方向
  4601. double distance0 = 0.0;
  4602. double distance1 = 0.0;
  4603. distance0 = sqrt(pow(pos->posx - d1.x,2) + pow(pos->posy - d1.y,2));
  4604. distance1 = sqrt(pow(pos->posx - d2.x,2) + pow(pos->posy - d2.y,2));
  4605. if (distance0 > distance1)
  4606. {
  4607. nNewSign = 1;
  4608. }
  4609. else
  4610. {
  4611. nNewSign = -1;
  4612. }
  4613. if (nOldSign == nNewSign)
  4614. {
  4615. continue;
  4616. }else{
  4617. //只要对一个分站的两天线不满足,则直接退出
  4618. bNotExist = false;
  4619. break;
  4620. }
  4621. }
  4622. }
  4623. if (!bNotExist)
  4624. {
  4625. return 1;
  4626. }
  4627. return 0;
  4628. }
  4629. /*
  4630. * 判断上下行方向
  4631. *
  4632. * param
  4633. * (x1,y1) 起点坐标
  4634. * (x2,y2) 终点坐标
  4635. *
  4636. * return
  4637. * 上行为1,下行为2,无方向为0
  4638. *
  4639. */
  4640. int Card::GetStream(double x1,double y1,double x2,double y2)
  4641. {
  4642. int nStream = 0;
  4643. //车辆上下行确定
  4644. //3个条件:起点(x1,y1),终点(x2,y2)
  4645. //1.x1==x2的情况下,y2>y1为下行
  4646. //2.y1==y2的情况下,x2>x1为下行
  4647. //3.x2>x1且y2>y1为下行
  4648. //x2<x1 y2>y1
  4649. //其他情况为上行
  4650. if (((x1 == x2 || abs(x1 - x2) < 1E-2) && y1 < y2 )
  4651. ||((y1 == y2 || abs(y1 - y2) < 1E-2) && x2 > x1)
  4652. ||(x1 < x2 && y1 < y2))
  4653. {
  4654. nStream = DOWN_STREAM;
  4655. }
  4656. else
  4657. {
  4658. nStream = UP_STREAM;
  4659. }
  4660. return nStream;
  4661. }
  4662. /*
  4663. * 根据上一次的速度计算本次的新坐标
  4664. *
  4665. * param
  4666. * pos 定位坐标
  4667. * v 上一次的运动速度
  4668. *
  4669. * return
  4670. * 成功返回0,并将计算出的新坐标保存到pos结果中
  4671. *
  4672. */
  4673. int Card::CalcPositionBySpeed(std::shared_ptr<POS>& pos,double v)
  4674. {
  4675. double distance = 0;
  4676. distance = v*pos->diff_reader_sync_num/3.6; //转为m/s
  4677. //如果两次定位的卡的ct号相差1
  4678. if (abs(time_stamp_cal - time_stamp_cal_last) == 1)
  4679. {
  4680. //时间按0.2s计算
  4681. //速度如果按30km/h计算,则位移为1.6米左右
  4682. //速度如果按20km/h计算,则位移为1.1米左右
  4683. //速度如果按10km/h计算,则位移为0.5米左右
  4684. distance = 1;
  4685. }
  4686. //x相等
  4687. if (abs(pos->cx - last_locate.x) < ZERO_PRECISION)
  4688. {
  4689. int nSign = 0;
  4690. /*if (last_locate.y > pos->cy)
  4691. {
  4692. nSign = 1;
  4693. }
  4694. if (last_locate.y < pos->cy)
  4695. {
  4696. nSign = -1;
  4697. }*/
  4698. if (this->m_nStream == DOWN_STREAM)
  4699. {
  4700. //下行
  4701. //pos->cy = nSign*(last_locate.y + distance/map_scale);
  4702. pos->cy = last_locate.y + distance/map_scale;
  4703. }else if(this->m_nStream == UP_STREAM){
  4704. //上行
  4705. //pos->cy = nSign*(last_locate.y - distance/map_scale);
  4706. pos->cy = last_locate.y - distance/map_scale;
  4707. }
  4708. }else{
  4709. //在有斜率的地方
  4710. double arg = atan((pos->cy - last_locate.y)/(pos->cx - last_locate.x));
  4711. double calc_x = 0,calc_y = 0;
  4712. if (this->m_nStream == DOWN_STREAM)
  4713. {
  4714. //下行
  4715. calc_x = last_locate.x + cos(arg)*distance/map_scale;
  4716. calc_y = last_locate.y + sin(arg)*distance/map_scale;
  4717. }else if(this->m_nStream == UP_STREAM){
  4718. //上行
  4719. calc_x = last_locate.x - cos(arg)*distance/map_scale;
  4720. calc_y = last_locate.y - sin(arg)*distance/map_scale;
  4721. }
  4722. pos->cx = calc_x;
  4723. pos->cy = calc_y;
  4724. }
  4725. pos->posx = pos->cx*map_scale;
  4726. pos->posy = pos->cy*map_scale;
  4727. return 0;
  4728. }
  4729. /*
  4730. * 保存原始定位信息
  4731. *
  4732. * param
  4733. * pos 定位结果
  4734. *
  4735. * return
  4736. * 如果成功则为零
  4737. */
  4738. int Card::SaveOriginDataBeforeFilter(std::shared_ptr<POS> pos)
  4739. {
  4740. origin_locate.x = pos->cx;
  4741. origin_locate.y = pos->cy;
  4742. origin_locate.z = pos->cz;
  4743. origin_locate.v = sqrt(pow(pos->cvx,2) + pow(pos->cvy,2))*3.6; //*3.6转为km/h
  4744. origin_locate.sync_num = this->m_nCalcSyncNum;
  4745. if (CheckPositionValid(pos))
  4746. {
  4747. char log[MAX_LOG_LENGTH] = {0};
  4748. sprintf_s(log,"cardid: %s, ct: %d, x: %.2f, y: %.2f",card_id.c_str(),time_stamp_cal,pos->cx,pos->cy);
  4749. Log::write_log(FILE_TYPE::MOVING_READER_PARSE_DATA,log,true);
  4750. }
  4751. return 0;
  4752. }
  4753. /*
  4754. * 从多解中选择合适的解,会有两种结果:1.选出一个合适解;2.选不出合适解
  4755. *
  4756. * param
  4757. * pRdm 分站定位数据集
  4758. * udm_pos 多个可能解
  4759. * pos 保存最终解
  4760. *
  4761. * return
  4762. * 选出合适解,将解保存到pos参数中,并返回0;选不出解,则返回非零错误码
  4763. *
  4764. */
  4765. int Card::ChooseOneSolution(std::shared_ptr<ReceiveDataMap> pRdm, std::vector<std::shared_ptr<POS>> udm_pos, std::shared_ptr<POS>& pos )
  4766. {
  4767. if(0 == udm_pos.size()){ // 没有解
  4768. return SOLUTION_NO_SOLUTION;
  4769. }
  4770. // 只有一个解
  4771. if(1 == udm_pos.size()){
  4772. //判断此位置距离分站的距离是否为4米范围内
  4773. ReceiveDataMap::iterator it_first = pRdm->end();
  4774. ReceiveDataMap::iterator it_second = pRdm->end();
  4775. for (ReceiveDataMap::iterator it = pRdm->begin();it != pRdm->end();++it)
  4776. {
  4777. if (it->second->reader_id == udm_pos.at(0)->nFirstReader)
  4778. {
  4779. it_first = it;
  4780. }
  4781. if (it->second->reader_id == udm_pos.at(0)->nSecondReader)
  4782. {
  4783. it_second = it;
  4784. }
  4785. }
  4786. if(it_first == pRdm->end() || it_second == pRdm->end())
  4787. {
  4788. return SOLUTION_NOT_EXIST_READRE;
  4789. }
  4790. double dist = 0.0;
  4791. if (it_first != pRdm->end())
  4792. {
  4793. dist = sqrt(pow(udm_pos.at(0)->posx - it_first->second->x,2)+pow(udm_pos.at(0)->posy - it_first->second->y,2));
  4794. if (dist < NEAR_READER && it_first->second->special == 0)
  4795. {
  4796. return SOLUTION_NEAR_READER;
  4797. }
  4798. }
  4799. if (it_second != pRdm->end())
  4800. {
  4801. dist = sqrt(pow(udm_pos.at(0)->posx - it_second->second->x,2)+pow(udm_pos.at(0)->posy - it_second->second->y,2));
  4802. if (dist < NEAR_READER && it_second->second->special == 0)
  4803. {
  4804. return SOLUTION_NEAR_READER;
  4805. }
  4806. }
  4807. pos->posx = udm_pos.at(0)->posx;
  4808. pos->posy = udm_pos.at(0)->posy;
  4809. pos->posz = udm_pos.at(0)->posz;
  4810. pos->cx = pos->posx / (this->map_scale*1.0);
  4811. pos->cy = pos->posy / (this->map_scale*1.0);
  4812. pos->cz = pos->posz / (this->map_scale*1.0);
  4813. TRACE(_T("solutions: ct:%d, r1: %d, r2:%d, x:%.4f y:%.4f\r\n"), this->time_stamp_cal, it_first->second->reader_id, it_second->second->reader_id, pos->cx, pos->cy);
  4814. return 0;
  4815. }
  4816. ReceiveDataMap::iterator itf = pRdm->begin();
  4817. int bf = itf->second->reader_id;
  4818. std::advance(itf,1);
  4819. int bs = itf->second->reader_id;
  4820. // 有多个解,含两解或两解以上
  4821. //判断依据:选取无地图集的分站数据来校正所有的解,
  4822. //解可靠的条件如下:1解与无地图集的时间戳距离差最小,2且此距离差不大于10米
  4823. for (ReceiveDataMap::reverse_iterator it_first = pRdm->rbegin();it_first!=pRdm->rend();++it_first)
  4824. {
  4825. ReceiveDataMap::reverse_iterator it_second = it_first;
  4826. std::advance(it_second,1);
  4827. if (it_second == pRdm->rend())
  4828. {
  4829. //找到尾部了还未找到合适解,即认为无解
  4830. pos->reason = 1;
  4831. break;
  4832. }
  4833. //反向查找,还要去掉这两个之间有地图集的数据
  4834. int nfr = it_first->second->reader_id;
  4835. int nfs = it_second->second->reader_id;
  4836. if (bf!=nfs && bs!=nfr)
  4837. {
  4838. if (IsExistPath(nfr,nfs))
  4839. {
  4840. continue;
  4841. }
  4842. }
  4843. int nCounts = 0;
  4844. for (std::vector<std::shared_ptr<POS>>::iterator it_pos = udm_pos.begin();it_pos != udm_pos.end();++it_pos)
  4845. {
  4846. //此点在这两个线段内,才允许使用此判断条件
  4847. _point p,l1,l2;
  4848. p.x = (*it_pos)->posx;
  4849. p.y = (*it_pos)->posy;
  4850. l1.x = it_first->second->x;
  4851. l1.y = it_first->second->y;
  4852. l2.x = it_second->second->x;
  4853. l2.y = it_second->second->y;
  4854. TRACE(_T("solutions: ct:%d, r1: %d, r2:%d, x:%.4f y:%.4f\r\n"), this->time_stamp_cal, it_first->second->reader_id, it_second->second->reader_id, p.x/2.0, p.y/2.0);
  4855. if (!LocateAlgorithm::IsInLine(p,l1,l2))
  4856. {
  4857. nCounts++;
  4858. continue;
  4859. }
  4860. double distance_reader = 0;
  4861. distance_reader = fabs(sqrt(
  4862. pow((*it_pos)->posx - it_first->second->x,2) + pow((*it_pos)->posy - it_first->second->y,2)
  4863. ) -
  4864. sqrt(
  4865. pow((*it_pos)->posx - it_second->second->x,2) + pow((*it_pos)->posy - it_second->second->y,2)
  4866. ));
  4867. long long dt = it_first->second->rec_time_stamp - it_second->second->rec_time_stamp;
  4868. double distance_tt = 0;
  4869. distance_tt = fabs(CFunctions::getDistance(dt,CFunctions::TDOA));
  4870. (*it_pos)->dis_diff = fabs(distance_reader - distance_tt);
  4871. }
  4872. if (nCounts == udm_pos.size())
  4873. {
  4874. continue;
  4875. }
  4876. double dMinDiff = 99999.9;
  4877. std::vector<std::shared_ptr<POS>>::iterator it_find = udm_pos.end();
  4878. for (std::vector<std::shared_ptr<POS>>::iterator it_pos = udm_pos.begin();it_pos != udm_pos.end();++it_pos)
  4879. {
  4880. if ((*it_pos)->dis_diff < dMinDiff)
  4881. {
  4882. _point p,l1,l2;
  4883. p.x = p.y = p.z = l1.x = l1.y = l1.z = l2.x = l2.y = l2.z = 0;
  4884. p.x = (*it_pos)->posx;
  4885. p.y = (*it_pos)->posy;
  4886. for (ReceiveDataMap::iterator it = pRdm->begin();it != pRdm->end();++it)
  4887. {
  4888. if (it->second->reader_id == (*it_pos)->nFirstReader)
  4889. {
  4890. l1.x = it->second->x;
  4891. l1.y = it->second->y;
  4892. }
  4893. if (it->second->reader_id == (*it_pos)->nSecondReader)
  4894. {
  4895. l2.x = it->second->x;
  4896. l2.y = it->second->y;
  4897. }
  4898. }
  4899. if (!LocateAlgorithm::IsInLine(p,l1,l2))
  4900. {
  4901. continue;
  4902. }else{
  4903. if (((*it_pos)->nFirstReader == nfr &&(*it_pos)->nSecondReader == nfs)||
  4904. ((*it_pos)->nFirstReader == nfs &&(*it_pos)->nSecondReader == nfr))
  4905. {
  4906. continue;
  4907. }else{
  4908. dMinDiff = (*it_pos)->dis_diff;
  4909. it_find = it_pos;
  4910. }
  4911. }
  4912. }
  4913. }
  4914. if (it_find != udm_pos.end())
  4915. {
  4916. if ((*it_find)->dis_diff < MIN_DIFFER_DISTANCE)
  4917. {
  4918. //找到即退出
  4919. //pos = *it_find;
  4920. pos->posx = (*it_find)->posx;
  4921. pos->posy = (*it_find)->posy;
  4922. pos->posz = (*it_find)->posz;
  4923. pos->reason = 0;
  4924. break;
  4925. }
  4926. }
  4927. }
  4928. if (pos->reason == 0)
  4929. {
  4930. pos->cx = pos->posx / (this->map_scale*1.0);
  4931. pos->cy = pos->posy / (this->map_scale*1.0);
  4932. pos->cz = pos->posz / (this->map_scale*1.0);
  4933. }
  4934. // 未计算出结果
  4935. if(pos->posx == INVALID_COORDINATE || pos->posy == INVALID_COORDINATE){
  4936. LOCATION_SYSTEM_BRANCH(LOCATION_SYSTEM_BRANCH_148);
  4937. return SOLUTION_NO_SOLUTION;
  4938. }else{
  4939. bool bExistSolution = true;
  4940. //对这唯一解最最后一步筛选,在分站1米内
  4941. //增加这个条件主要是为了解决,当距离最近的分站数据未收到的情况,
  4942. //比如实际数据应该是103,102,101,,但目前收到的数据为102,101,但定位到102附近且在102和101之间
  4943. for (ReceiveDataMap::iterator it = pRdm->begin();it != pRdm->end();++it)
  4944. {
  4945. double distance = 0.0;
  4946. distance = sqrt(pow(it->second->x - pos->posx,2) + pow(it->second->y - pos->posy,2));
  4947. if (distance < 1)
  4948. {
  4949. bExistSolution = false;
  4950. break;
  4951. }
  4952. }
  4953. //无解
  4954. if (!bExistSolution)
  4955. {
  4956. std::shared_ptr<POS> tmp = std::make_shared<POS>();
  4957. pos->posx = tmp->posx;
  4958. pos->posy = tmp->posy;
  4959. pos->cx = 0;
  4960. pos->cy = 0;
  4961. pos->cz = 0;
  4962. return SOLUTION_NO_SOLUTION;
  4963. }
  4964. }
  4965. return 0;
  4966. }
  4967. string Card::store_data_card( int tag ,double xd, double yd)
  4968. {
  4969. char sql[LENGTH_SQL] = {0};
  4970. char _time[STR_LEN_TIME], _time_ex[STR_LEN_TIME];
  4971. //struct tm local_time, local_time_ex;
  4972. time_t t = time(NULL)+10;
  4973. xd = (xd == 0 ? x_offset_after():xd);
  4974. yd = (yd == 0 ? y_offset_after():yd);
  4975. struct tm p;
  4976. localtime_s(&p,&t);
  4977. strftime(_time, 30, "%Y-%m-%d %H:%M:%S", &p);
  4978. switch(tag){
  4979. case HIS_AREA_LOCATION_LEAVE:
  4980. //sprintf_s(_time,
  4981. // STR_LEN_TIME,
  4982. // "%u-%u-%u %u:%u:%u.%u",
  4983. // deal_time.wYear,deal_time.wMonth,deal_time.wDay,
  4984. // deal_time.wHour,deal_time.wMinute,deal_time.wSecond,deal_time.wMilliseconds);
  4985. sprintf_s(_time_ex,
  4986. STR_LEN_TIME,
  4987. "%u-%u-%u %u:%u:%u.%u",
  4988. enter_area_time.wYear,enter_area_time.wMonth,enter_area_time.wDay,
  4989. enter_area_time.wHour,enter_area_time.wMinute,enter_area_time.wSecond,enter_area_time.wMilliseconds);
  4990. sprintf_s(sql, LENGTH_SQL,
  4991. "call add_area_staff(%s, %d, %d, %d, '%s','%s');",
  4992. card_id.c_str(), id, area_id, map_id, _time_ex, _time);
  4993. break;
  4994. case HIS_LOCATION:
  4995. sprintf_s(_time,
  4996. STR_LEN_TIME,
  4997. "%u-%u-%u %u:%u:%u.%u",
  4998. deal_time.wYear,deal_time.wMonth,deal_time.wDay,
  4999. deal_time.wHour,deal_time.wMinute,deal_time.wSecond,deal_time.wMilliseconds);
  5000. sprintf_s(sql, LENGTH_SQL,
  5001. "INSERT IGNORE INTO his_location_staff(card_id, staff_id, cur_time, x, y, z, map_id, area_id, state,speed,mileage, landmark_id, direction_mapper_id, landmark_dist) VALUES(%s, %d, '%s', %.3f, %.3f, %.3f, %d, %d, %d,%.3f,%.4f, %d, %d, %.4f);",
  5002. card_id.c_str(), id, _time, xd, yd, z_offset_after(), map_id, area_id, state,get_speed(),mileage, landmark_id, landmark_direction, landmark_dis);
  5003. break;
  5004. default:
  5005. return sql;
  5006. }
  5007. return sql;
  5008. }
  5009. /*
  5010. * 对pos结果进行校验
  5011. *
  5012. * param
  5013. * pos 定位结果
  5014. *
  5015. * return
  5016. * 正确执行返回0,失败返回非零值
  5017. *
  5018. */
  5019. int Card::calibration_pos_data(std::shared_ptr<POS> pos)
  5020. {
  5021. //5.如果有解,则对唯一解做合法性验证
  5022. //主要验证条件为:加速度和速度
  5023. int nRet = 0;
  5024. nRet = CheckSolution(pos);
  5025. if (nRet == 0)
  5026. {
  5027. //对通过加速度验证的解进行是否回退的判断
  5028. if (IsRebound(pos))
  5029. {
  5030. if (count_change_direction < MAX_REBOUND_COUNTS)
  5031. {
  5032. CalcPositionBySpeed(pos,last_locate.v);
  5033. if(CheckPositionValid(pos))
  5034. {
  5035. pos->cx = pos->posx / (map_scale*1.0);
  5036. pos->cy = pos->posy / (map_scale*1.0);
  5037. pos->update = true;
  5038. pos->is_fit = true;
  5039. count_change_direction++;
  5040. }else{
  5041. //这里可能需要加逻辑处理,避免一次回跳太多
  5042. count_change_direction = 0;
  5043. bIsBack = true;
  5044. }
  5045. }
  5046. else
  5047. {
  5048. count_change_direction = 0;
  5049. }
  5050. }
  5051. }else{
  5052. //加速度校验失败
  5053. //加速度或速度抛弃的结果
  5054. if (!IsRebound(pos))
  5055. {
  5056. //如果不是回退,说明了是往前大跳
  5057. //加速追
  5058. }else{
  5059. //说明是往回大跳
  5060. //减速走
  5061. bIsBack = true;
  5062. }
  5063. }
  5064. return 0;
  5065. }
  5066. int Card::optimization_pos(std::shared_ptr<POS> pos)
  5067. {
  5068. int bRet = 0;
  5069. bRet = KalmanFilterProcess(pos);
  5070. return bRet;
  5071. }
  5072. int Card::kalman_filter_process_tdoa(std::shared_ptr<POS> pos)
  5073. {
  5074. //卡尔曼的时间差
  5075. double kalman_detal_t = 0;
  5076. //分站同步序号的间隔时间,单位为秒
  5077. double interval_time = 0.2;
  5078. kalman_detal_t = GetDeltaT();
  5079. if (abs(pos->diff_reader_sync_num) < ZERO_PRECISION)
  5080. {
  5081. pos->diff_reader_sync_num = kalman_detal_t;
  5082. }
  5083. //如果定位失败,通过卡尔曼滤波处理
  5084. //if(pos->posx == INVALID_COORDINATE && pos->posy == INVALID_COORDINATE){
  5085. if(!CheckPositionValid(pos)){
  5086. m_afmData.nCardStamp = m_nCalcSyncNum;
  5087. if(this->m_pKalmanFilter->m_nCounts < 3 || this->m_pKalmanFilter->m_pCar->P(0,0) > 2 || kalman_detal_t > 3){
  5088. //if(this->m_pKalmanFilter->m_nCounts < 3 || kalman_detal_t > 3){
  5089. //P(0,0):连续时间(大于2s)都定位失败
  5090. //deltaT>3:距离上次成功定位时间间隔为3s
  5091. this->x = this->last_locate.x;
  5092. this->y = this->last_locate.y;
  5093. this->z = 0;
  5094. m_afmData.bStatus = true;
  5095. m_afmData.strCardId = this->card_id;
  5096. this->m_pKalmanFilter->m_bFlag = false;
  5097. m_afmData.nType = ALGO_FAILED_CONDITION_8;
  5098. ALGORITHM_FAILED(ALGO_FAILED_CONDITION_8);
  5099. return KALMAN_FILTER_LONG_INTERVAL;
  5100. }
  5101. if(this->m_pKalmanFilter->m_nCounts >= 3){
  5102. //只有三次以上才允许使用kalman滤波以下的函数
  5103. this->m_pKalmanFilter->Predict(kalman_detal_t);
  5104. this->z = 0;
  5105. pos->update = true;
  5106. }
  5107. }else{
  5108. if (!is_algo_first_location)
  5109. {
  5110. //避免第一次时加入了两次
  5111. pos->update = true;
  5112. }
  5113. this->m_pKalmanFilter->m_bFlag = true;
  5114. this->m_pKalmanFilter->m_nCounts++;
  5115. this->m_pKalmanFilter->m_pCar->z(0,0) = pos->posx;
  5116. this->m_pKalmanFilter->m_pCar->z(1,0) = pos->cvx;
  5117. this->m_pKalmanFilter->m_pCar->z(2,0) = pos->posy;
  5118. this->m_pKalmanFilter->m_pCar->z(3,0) = pos->cvy;
  5119. if(this->m_pKalmanFilter->m_nCounts == 1){
  5120. //第一次直接赋值
  5121. this->m_pKalmanFilter->m_pCar->x = this->m_pKalmanFilter->m_pCar->z;
  5122. }
  5123. if(this->m_pKalmanFilter->m_nCounts == 2){
  5124. //两次处理
  5125. this->m_pKalmanFilter->m_pCar->z(1, 0) = (this->m_pKalmanFilter->m_pCar->z(0, 0) - this->m_pKalmanFilter->m_pCar->x(0, 0))/pos->diff_reader_sync_num;
  5126. this->m_pKalmanFilter->m_pCar->z(3, 0) = (this->m_pKalmanFilter->m_pCar->z(2, 0) - this->m_pKalmanFilter->m_pCar->x(2, 0))/pos->diff_reader_sync_num;
  5127. this->m_pKalmanFilter->m_pCar->x = this->m_pKalmanFilter->m_pCar->z;
  5128. }
  5129. if(this->m_pKalmanFilter->m_nCounts >= 3){
  5130. //只有三次以上才允许使用kalman滤波以下的函数
  5131. //this->m_pKalmanFilter->Predict_Correct(deltaT);
  5132. this->m_pKalmanFilter->Predict_Correct(kalman_detal_t);
  5133. if(fabs(pos->diff_reader_sync_num) > ZERO_PRECISION){
  5134. if (abs(pos->ref_x) < ZERO_PRECISION && abs(pos->ref_y) < ZERO_PRECISION)
  5135. {
  5136. }else{
  5137. this->m_pKalmanFilter->m_pCar->x(1,0) = (this->m_pKalmanFilter->m_pCar->x(0,0) - pos->ref_x*this->map_scale)/pos->diff_reader_sync_num;
  5138. this->m_pKalmanFilter->m_pCar->x(3,0) = (this->m_pKalmanFilter->m_pCar->x(2,0) - pos->ref_y*this->map_scale)/pos->diff_reader_sync_num;
  5139. }
  5140. }
  5141. }
  5142. }
  5143. if (pos->update)
  5144. {
  5145. //二维定位不需要如下内容
  5146. //增加地图集的判定,判断定位结果是否在地图集上
  5147. //如果不在地图集上,需要再次定位
  5148. //需要带出定位结果的分站信息,
  5149. //利用地图集中分站信息再次定位
  5150. std::shared_ptr<POS> kalman_p = std::make_shared<POS>();
  5151. kalman_p->posx = this->m_pKalmanFilter->m_pCar->x(0,0);
  5152. kalman_p->posy = this->m_pKalmanFilter->m_pCar->x(2,0);
  5153. ////二维定位不需要再判定在地图集上了
  5154. if(!LocateAlgorithm::IsOnMap(kalman_p,pTdoaReaderPathMap)){
  5155. //再一次定位到地图集上
  5156. LOCATION_SYSTEM_BRANCH(LOCATION_SYSTEM_BRANCH_205);
  5157. std::shared_ptr<POS> cp = LocateAlgorithm::MappingToPath(kalman_p,pTdoaReaderPathMap);
  5158. if(cp != nullptr){
  5159. if (cp->posx != INVALID_COORDINATE && cp->posy !=INVALID_COORDINATE)
  5160. {
  5161. LOCATION_SYSTEM_BRANCH(LOCATION_SYSTEM_BRANCH_206);
  5162. this->m_pKalmanFilter->m_pCar->x(0,0) = cp->posx;
  5163. this->m_pKalmanFilter->m_pCar->x(2,0) = cp->posy;
  5164. }
  5165. }
  5166. cp.reset();
  5167. }
  5168. pos->posx = this->m_pKalmanFilter->m_pCar->x(0,0);
  5169. pos->posy = this->m_pKalmanFilter->m_pCar->x(2,0);
  5170. pos->cvx = this->m_pKalmanFilter->m_pCar->x(1,0);
  5171. pos->cvy = this->m_pKalmanFilter->m_pCar->x(3,0);
  5172. pos->cx = pos->posx / (map_scale*1.0);
  5173. pos->cy = pos->posy / (map_scale*1.0);
  5174. }
  5175. return 0;
  5176. }
  5177. int Card::kalman_filter_process_tof(std::shared_ptr<POS> pos)
  5178. {
  5179. //卡尔曼的时间差
  5180. double kalman_detal_t = 0;
  5181. kalman_detal_t = GetDeltaT();
  5182. //如果定位失败,通过卡尔曼滤波处理
  5183. if(!CheckPositionValid(pos)){
  5184. m_afmData.nCardStamp = m_nCalcSyncNum;
  5185. if(this->m_pKalmanFilter->m_nCounts < 3 || kalman_detal_t > 6){
  5186. //deltaT>6:距离上次成功定位时间间隔为6s
  5187. this->x = this->last_locate.x;
  5188. this->y = this->last_locate.y;
  5189. this->z = 0;
  5190. m_afmData.bStatus = true;
  5191. m_afmData.strCardId = this->card_id;
  5192. this->m_pKalmanFilter->m_bFlag = false;
  5193. m_afmData.nType = ALGO_FAILED_CONDITION_8;
  5194. ALGORITHM_FAILED(ALGO_FAILED_CONDITION_8);
  5195. return KALMAN_FILTER_LONG_INTERVAL;
  5196. }
  5197. if(this->m_pKalmanFilter->m_nCounts >= 3){
  5198. //只有三次以上才允许使用kalman滤波以下的函数
  5199. this->m_pKalmanFilter->Predict(kalman_detal_t);
  5200. this->z = 0;
  5201. pos->update = true;
  5202. }
  5203. }else{
  5204. if (!is_algo_first_location)
  5205. {
  5206. //避免第一次时加入了两次
  5207. pos->update = true;
  5208. }
  5209. this->m_pKalmanFilter->m_bFlag = true;
  5210. this->m_pKalmanFilter->m_nCounts++;
  5211. this->m_pKalmanFilter->m_pCar->z(0,0) = pos->posx;
  5212. this->m_pKalmanFilter->m_pCar->z(1,0) = pos->cvx;
  5213. this->m_pKalmanFilter->m_pCar->z(2,0) = pos->posy;
  5214. this->m_pKalmanFilter->m_pCar->z(3,0) = pos->cvy;
  5215. if(this->m_pKalmanFilter->m_nCounts == 1){
  5216. //第一次直接赋值
  5217. this->m_pKalmanFilter->m_pCar->x = this->m_pKalmanFilter->m_pCar->z;
  5218. }
  5219. if(this->m_pKalmanFilter->m_nCounts == 2){
  5220. //两次处理
  5221. this->m_pKalmanFilter->m_pCar->z(1, 0) = (this->m_pKalmanFilter->m_pCar->z(0, 0) - this->m_pKalmanFilter->m_pCar->x(0, 0))/kalman_detal_t;
  5222. this->m_pKalmanFilter->m_pCar->z(3, 0) = (this->m_pKalmanFilter->m_pCar->z(2, 0) - this->m_pKalmanFilter->m_pCar->x(2, 0))/kalman_detal_t;
  5223. this->m_pKalmanFilter->m_pCar->x = this->m_pKalmanFilter->m_pCar->z;
  5224. }
  5225. if(this->m_pKalmanFilter->m_nCounts >= 3){
  5226. //只有三次以上才允许使用kalman滤波以下的函数
  5227. //this->m_pKalmanFilter->Predict_Correct(deltaT);
  5228. this->m_pKalmanFilter->Predict_Correct(kalman_detal_t);
  5229. if(fabs(pos->diff_reader_sync_num) > ZERO_PRECISION){
  5230. if (last_locate.x != INVALID_COORDINATE && last_locate.y != INVALID_COORDINATE)
  5231. {
  5232. this->m_pKalmanFilter->m_pCar->x(1,0) = (this->m_pKalmanFilter->m_pCar->x(0,0) - last_locate.x*this->map_scale)/kalman_detal_t;
  5233. this->m_pKalmanFilter->m_pCar->x(3,0) = (this->m_pKalmanFilter->m_pCar->x(2,0) - last_locate.y*this->map_scale)/kalman_detal_t;
  5234. }
  5235. }
  5236. }
  5237. }
  5238. if (pos->update)
  5239. {
  5240. //二维定位不需要如下内容
  5241. //增加地图集的判定,判断定位结果是否在地图集上
  5242. //如果不在地图集上,需要再次定位
  5243. //需要带出定位结果的分站信息,
  5244. //利用地图集中分站信息再次定位
  5245. std::shared_ptr<POS> kalman_p = std::make_shared<POS>();
  5246. kalman_p->posx = this->m_pKalmanFilter->m_pCar->x(0,0);
  5247. kalman_p->posy = this->m_pKalmanFilter->m_pCar->x(2,0);
  5248. ////二维定位不需要再判定在地图集上了
  5249. if(!LocateAlgorithm::IsOnMap(kalman_p,pTdoaReaderPathMap)){
  5250. //再一次定位到地图集上
  5251. LOCATION_SYSTEM_BRANCH(LOCATION_SYSTEM_BRANCH_205);
  5252. std::shared_ptr<POS> cp = LocateAlgorithm::MappingToPath(kalman_p,pTdoaReaderPathMap);
  5253. if(cp != nullptr){
  5254. if (cp->posx != INVALID_COORDINATE && cp->posy !=INVALID_COORDINATE)
  5255. {
  5256. LOCATION_SYSTEM_BRANCH(LOCATION_SYSTEM_BRANCH_206);
  5257. this->m_pKalmanFilter->m_pCar->x(0,0) = cp->posx;
  5258. this->m_pKalmanFilter->m_pCar->x(2,0) = cp->posy;
  5259. }
  5260. }
  5261. cp.reset();
  5262. }
  5263. pos->posx = this->m_pKalmanFilter->m_pCar->x(0,0);
  5264. pos->posy = this->m_pKalmanFilter->m_pCar->x(2,0);
  5265. pos->cvx = this->m_pKalmanFilter->m_pCar->x(1,0);
  5266. pos->cvy = this->m_pKalmanFilter->m_pCar->x(3,0);
  5267. pos->cx = pos->posx / (map_scale*1.0);
  5268. pos->cy = pos->posy / (map_scale*1.0);
  5269. }
  5270. return 0;
  5271. }
  5272. int Card::GetDeltaT( map<unsigned long long,std::shared_ptr<_coordinate>> dl )
  5273. {
  5274. unsigned long long revTime = 0;
  5275. int rid = 0, sn = 0, ct = 0;
  5276. map<unsigned long long,std::shared_ptr<_coordinate>>::iterator it_mpdl = dl.begin();
  5277. int i = 0;
  5278. for(; it_mpdl != dl.end(); ++it_mpdl){
  5279. if(0 == it_mpdl->second->tt){
  5280. continue;
  5281. }
  5282. if(this->locate && it_mpdl->second->reader_id == this->locate->ReaderId()){
  5283. revTime = it_mpdl->second->tt;
  5284. rid = it_mpdl->second->reader_id;
  5285. sn = it_mpdl->second->sync_num;
  5286. ct = it_mpdl->second->t;
  5287. break;
  5288. }
  5289. if(0 == revTime){
  5290. revTime = it_mpdl->second->tt;
  5291. rid = it_mpdl->second->reader_id;
  5292. sn = it_mpdl->second->sync_num;
  5293. ct = it_mpdl->second->t;
  5294. }
  5295. else if( revTime < it_mpdl->second->tt){ // 跨周期,可能会取到较大值
  5296. revTime = it_mpdl->second->tt;
  5297. rid = it_mpdl->second->reader_id;
  5298. sn = it_mpdl->second->sync_num;
  5299. ct = it_mpdl->second->t;
  5300. }
  5301. }
  5302. std::shared_ptr<nspLocate::LocateRecord> loc = std::make_shared<nspLocate::LocateRecord>(revTime, rid, sn, ct);
  5303. loc->getDeltaT(this->locate);
  5304. this->locate = loc;
  5305. return 0;
  5306. }
  5307. /*
  5308. * 获得tdoa和tof 卡的时间差
  5309. *
  5310. * param
  5311. * 无
  5312. *
  5313. * return
  5314. * 返回值为计算后的时间差
  5315. *
  5316. */
  5317. double Card::GetDeltaT()
  5318. {
  5319. double dt = 0;
  5320. switch (ranging_type)
  5321. {
  5322. case LOCATEDATATYPE::LDT_TOF:
  5323. case LOCATEDATATYPE::LDT_TOF_INS:
  5324. case LOCATEDATATYPE::LDT_TDOA_INS:
  5325. dt = difftime(time_cur_algo,time_last_tof);
  5326. if(dt < ZERO_PRECISION)
  5327. {
  5328. switch (this->card_type)
  5329. {
  5330. case CT_VEHICLE:
  5331. dt = 1 * (time_stamp_cal - time_stamp_cal_last);
  5332. break;
  5333. case CT_PERSON:
  5334. dt = 2 * (time_stamp_cal - time_stamp_cal_last);
  5335. break;
  5336. default:
  5337. dt = 2 * (time_stamp_cal - time_stamp_cal_last);
  5338. break;
  5339. }
  5340. }
  5341. break;
  5342. case LOCATEDATATYPE::LDT_TDOA:
  5343. //存在分站在计数序号为1000掉电,但重新上电会导致此时的计算值偏大
  5344. if (m_nCalcSyncNum - this->last_locate.sync_num < 0 && m_nCalcSyncNum < 100)
  5345. {
  5346. dt = (m_nCalcSyncNum + 65535 - this->last_locate.sync_num)*READER_SYNC_INTERVAL_TIME;
  5347. }else{
  5348. dt = (m_nCalcSyncNum - this->last_locate.sync_num)*READER_SYNC_INTERVAL_TIME;
  5349. }
  5350. if (abs(dt) < ZERO_PRECISION)
  5351. {
  5352. dt = 0.2;
  5353. }
  5354. break;
  5355. default:
  5356. break;
  5357. }
  5358. return dt;
  5359. }
  5360. /*
  5361. * 获得定位维数,根据条件而定
  5362. *
  5363. * param
  5364. * 无
  5365. *
  5366. * return
  5367. * 成功返回指定定位维数,否则返回失败(0)
  5368. *
  5369. */
  5370. int Card::get_dimension()
  5371. {
  5372. //拆分数据为三维,二维,一维的定位数据
  5373. split_dist_data();
  5374. //如果三维数据可定位,则采用三维定位
  5375. if (dist_3d.size() > _3D)
  5376. {
  5377. distmap = dist_3d;
  5378. return _3D;
  5379. }else if (dist_2d.size() > _2D)
  5380. {
  5381. //如果二维定位可定位,则采用二维定位
  5382. distmap = dist_2d;
  5383. return _2D;
  5384. }else if (dist_1d.size() >= _1D)
  5385. {
  5386. //如果采用一维定位可定位,则采用一维定位
  5387. distmap = dist_1d;
  5388. return _1D;
  5389. }
  5390. return 0;
  5391. }
  5392. /*
  5393. * 拆分dist数据为一维,二维,三维数据
  5394. *
  5395. * param
  5396. * 无
  5397. * return
  5398. * 成功返回0,失败返回1
  5399. *
  5400. */
  5401. int Card::split_dist_data()
  5402. {
  5403. //将数据按分站维数进行拆分
  5404. if (dist_1d.size() > 0)
  5405. {
  5406. dist_1d.erase(dist_1d.begin(),dist_1d.end());
  5407. }
  5408. if (dist_2d.size() > 0)
  5409. {
  5410. dist_2d.erase(dist_2d.begin(),dist_2d.end());
  5411. }
  5412. if (dist_3d.size() > 0)
  5413. {
  5414. dist_3d.erase(dist_3d.begin(),dist_3d.end());
  5415. }
  5416. for(auto it : _dists.front().distmap)
  5417. {
  5418. switch(it.second->dimension){
  5419. case _1D:
  5420. dist_1d.insert(std::make_pair(it.first,it.second));
  5421. break;
  5422. case _2D:
  5423. dist_2d.insert(std::make_pair(it.first,it.second));
  5424. break;
  5425. case _3D:
  5426. dist_3d.insert(std::make_pair(it.first,it.second));
  5427. break;
  5428. }
  5429. }
  5430. return 0;
  5431. }
  5432. /*
  5433. * 处理点和其定位分站相关边界的关系
  5434. *
  5435. * param
  5436. * p 定位点坐标
  5437. *
  5438. * return
  5439. * 在边界区域内返回0,否则返回非零值
  5440. *
  5441. */
  5442. int Card::deal_position_boundary(_point& p)
  5443. {
  5444. //1.判断点是否在边界内,在直接返回0,表示成功
  5445. if (position_is_in_boundary(p))
  5446. {
  5447. #if ENABLE_DEBUG_2D_TEST_LOG
  5448. char log[MAX_LOG_LENGTH];
  5449. memset(log,0,MAX_LOG_LENGTH*sizeof(char));
  5450. sprintf_s(log,"the p is in boundary. p.x : %.3f, p.y : %.3f",p.x,p,y);
  5451. Log::write_log(FILE_TYPE::SYS_S,log,true);
  5452. #endif
  5453. return 0;
  5454. }
  5455. //2.不在的话,判断此点和上一个定位位置直接的距离是否大于指定阈值d,
  5456. double distance = 0.0;
  5457. if (is_first_location){
  5458. #if ENABLE_DEBUG_2D_TEST_LOG
  5459. char log[MAX_LOG_LENGTH];
  5460. memset(log,0,MAX_LOG_LENGTH*sizeof(char));
  5461. sprintf_s(log,"2d location : it is first location.");
  5462. Log::write_log(FILE_TYPE::SYS_S,log,true);
  5463. #endif
  5464. return 0;
  5465. }else{
  5466. if (last_locate.x == INVALID_COORDINATE && last_locate.y == INVALID_COORDINATE)
  5467. {
  5468. #if ENABLE_DEBUG_2D_TEST_LOG
  5469. char log[MAX_LOG_LENGTH];
  5470. memset(log,0,MAX_LOG_LENGTH*sizeof(char));
  5471. sprintf_s(log,"2d location : last_locate pos is invalid.");
  5472. Log::write_log(FILE_TYPE::SYS_S,log,true);
  5473. #endif
  5474. }else{
  5475. distance = sqrt(pow(p.x - last_locate.x,2) + pow(p.y - last_locate.y,2));
  5476. if (distance < CHAMBER_WIDTH*map_scale)
  5477. {
  5478. //a.小于的话,将新位置垂直映射到边界上
  5479. _point newp = mapping_position_to_boundary(p);
  5480. if (newp.x != INVALID_COORDINATE && newp.y != INVALID_COORDINATE)
  5481. {
  5482. p.x = newp.x;
  5483. p.y = newp.y;
  5484. p.z = newp.z;
  5485. }else{
  5486. #if ENABLE_DEBUG_2D_TEST_LOG
  5487. char log[MAX_LOG_LENGTH];
  5488. memset(log,0,MAX_LOG_LENGTH*sizeof(char));
  5489. sprintf_s(log,"mapping position failed.");
  5490. Log::write_log(FILE_TYPE::SYS_S,log,true);
  5491. #endif
  5492. }
  5493. }else{
  5494. #if ENABLE_DEBUG_2D_TEST_LOG
  5495. char log[MAX_LOG_LENGTH];
  5496. memset(log,0,MAX_LOG_LENGTH*sizeof(char));
  5497. sprintf_s(log,"2d location is discard!!!");
  5498. Log::write_log(FILE_TYPE::SYS_S,log,true);
  5499. #endif
  5500. //b.大于的话,抛弃此次定位结果
  5501. return 1;
  5502. }
  5503. }
  5504. }
  5505. return 0;
  5506. }
  5507. /*
  5508. * 判断点是否在边界区域内
  5509. *
  5510. * param
  5511. * p 定位点坐标
  5512. *
  5513. * return
  5514. * 如果在边界区域内,返回true,否则返回false
  5515. *
  5516. */
  5517. bool Card::position_is_in_boundary(_point p)
  5518. {
  5519. if (p_reader == NULL || p_reader->pBoundaryArea == nullptr)
  5520. {
  5521. #if ENABLE_DEBUG_2D_TEST_LOG
  5522. std::string error_log = "";
  5523. if (p_reader == NULL)
  5524. {
  5525. error_log = "p_reader is null.";
  5526. }else if(p_reader->pBoundaryArea == nullptr){
  5527. error_log = "p_reader->pBoundaryArea is nullptr.";
  5528. }
  5529. char log[MAX_LOG_LENGTH];
  5530. memset(log,0,MAX_LOG_LENGTH*sizeof(char));
  5531. sprintf_s(log,"2d location : position out of boundary ,error message is %s.",error_log.c_str());
  5532. Log::write_log(FILE_TYPE::SYS_S,log,true);
  5533. #endif
  5534. return false;
  5535. }
  5536. _point tp;
  5537. //tp.x = p.x / map_scale;
  5538. //tp.y = p.y / map_scale;
  5539. tp.x = p.x ;
  5540. tp.y = p.y ;
  5541. #if ENABLE_DEBUG_2D_TEST_LOG
  5542. char log[MAX_LOG_LENGTH];
  5543. memset(log,0,MAX_LOG_LENGTH*sizeof(char));
  5544. sprintf_s(log,"position_is_in_boundary(): p.x: %.3f,p.y: %.3f,.x : %.3f, tp.y : %.3f, boundary_path: %s.",p.x,p.y,tp.x,tp.y,p_reader->pBoundaryArea->path.c_str());
  5545. Log::write_log(FILE_TYPE::SYS_S,log,true);
  5546. #endif
  5547. return p_reader->pBoundaryArea->IsInPolygon(p);
  5548. }
  5549. /*
  5550. * 映射边界区域外的点到边界上
  5551. *
  5552. * param
  5553. * p 定位点坐标
  5554. *
  5555. * return
  5556. * 成功映射,返回在边界上的点,否则返回坐标为异常数据
  5557. *
  5558. */
  5559. _point Card::mapping_position_to_boundary(_point p)
  5560. {
  5561. _point newp;
  5562. newp.x = newp.y = newp.z = INVALID_COORDINATE;
  5563. if (p_reader == NULL)
  5564. {
  5565. return newp;
  5566. }
  5567. if (p_reader->pBoundaryArea == nullptr)
  5568. {
  5569. return newp;
  5570. }
  5571. double min_x = 99999.0 , min_y = 99999.0, max_x = -99999.0, max_y = -99999.0;
  5572. if (p_reader->pBoundaryArea->m_pPolygon == NULL)
  5573. {
  5574. return newp;
  5575. }
  5576. for (int i = 0;i < p_reader->pBoundaryArea->m_nPolygonCount;i++)
  5577. {
  5578. if (p_reader->pBoundaryArea->m_pPolygon[i].x < min_x)
  5579. {
  5580. min_x = p_reader->pBoundaryArea->m_pPolygon[i].x;
  5581. }
  5582. if (p_reader->pBoundaryArea->m_pPolygon[i].x > max_x)
  5583. {
  5584. max_x = p_reader->pBoundaryArea->m_pPolygon[i].x;
  5585. }
  5586. if (p_reader->pBoundaryArea->m_pPolygon[i].y < min_y)
  5587. {
  5588. min_y = p_reader->pBoundaryArea->m_pPolygon[i].y;
  5589. }
  5590. if (p_reader->pBoundaryArea->m_pPolygon[i].y > max_y)
  5591. {
  5592. max_y = p_reader->pBoundaryArea->m_pPolygon[i].y;
  5593. }
  5594. }
  5595. int orientation = -1;
  5596. //已知矩形四个顶点分别是(xi,yi)(其中i = 1,2,3,4),点(x,y)和此矩形的关系如下
  5597. //1.当x > max(xi)时:
  5598. //a.y>max(y),点在矩形的右上角;
  5599. //b.y<min(y),点在矩形的右下角;
  5600. //c.y>min(y)&&y<max(y),点在矩形的右边;
  5601. //2.当x < min(xi)时:
  5602. //a.y>max(y),点在矩形的左上角;
  5603. //b.y<min(y),点在矩形的左下角;
  5604. //c.y>min(y)&&y<max(y),点在矩形的左边;
  5605. //3.当x>min(xi)且x<max(xi)时:
  5606. //a.y>max(y),点在矩形的正上边
  5607. //b.y<max(y),点在矩形的正下边
  5608. if (p.x > max_x)
  5609. {
  5610. if (p.y > max_y)
  5611. {
  5612. orientation = Orientation::RIGHT_UP;
  5613. }else if(p.y < min_y)
  5614. {
  5615. orientation = Orientation::RIGHT_DOWN;
  5616. }else{
  5617. orientation = Orientation::RIGHT;
  5618. }
  5619. }
  5620. else if(p.x < min_x)
  5621. {
  5622. if (p.y > max_y)
  5623. {
  5624. orientation = Orientation::LEFT_UP;
  5625. }else if (p.y < min_y)
  5626. {
  5627. orientation = Orientation::LEFT_DOWN;
  5628. }else{
  5629. orientation = Orientation::LEFT;
  5630. }
  5631. }else{
  5632. if (p.y > max_y)
  5633. {
  5634. orientation = Orientation::UP;
  5635. }else if(p.y < min_y){
  5636. orientation = Orientation::DOWN;
  5637. }
  5638. }
  5639. if (orientation == -1)
  5640. {
  5641. return newp;
  5642. }
  5643. switch (orientation)
  5644. {
  5645. case Orientation::UP:
  5646. newp.x = p.x;
  5647. newp.y = max_y;
  5648. break;
  5649. case Orientation::DOWN:
  5650. newp.x = p.x;
  5651. newp.y = min_y;
  5652. break;
  5653. case Orientation::LEFT:
  5654. newp.x = min_x;
  5655. newp.y = p.y;
  5656. break;
  5657. case Orientation::RIGHT:
  5658. newp.x = max_x;
  5659. newp.y = p.y;
  5660. break;
  5661. case Orientation::LEFT_UP:
  5662. newp.x = min_x;
  5663. newp.y = max_y;
  5664. break;
  5665. case Orientation::LEFT_DOWN:
  5666. newp.x = min_x;
  5667. newp.y = min_y;
  5668. break;
  5669. case Orientation::RIGHT_UP:
  5670. newp.x = max_x;
  5671. newp.y = max_y;
  5672. break;
  5673. case Orientation::RIGHT_DOWN:
  5674. newp.x = max_x;
  5675. newp.y = min_y;
  5676. break;
  5677. default:
  5678. break;
  5679. }
  5680. return newp;
  5681. }
  5682. /*
  5683. * tdoa二维定位的加速度或速度过滤方法
  5684. *
  5685. * param
  5686. * p 定位坐标
  5687. *
  5688. * return
  5689. * 成功返回0,否则返回非零值
  5690. *
  5691. */
  5692. int Card::check_solution_tdoa_2d(std::shared_ptr<POS>& p)
  5693. {
  5694. // 如果第一次定位则不需检查
  5695. if (is_algo_first_location)
  5696. {
  5697. return 0;
  5698. }
  5699. double distance = 0.0, speed = 0.0, acc = 0.0,dt = 0;
  5700. if (!is_use_acc_filter)
  5701. {
  5702. if (avg_coordinate.size() >=5)
  5703. {
  5704. HostServer::Position avg_pos;
  5705. for (auto it = avg_coordinate.begin();it != avg_coordinate.end();++it)
  5706. {
  5707. avg_pos.x += (*it)->x;
  5708. avg_pos.y += (*it)->y;
  5709. avg_pos.z += (*it)->z;
  5710. }
  5711. avg_pos.x /=5;
  5712. avg_pos.y /=5;
  5713. avg_pos.z /=5;
  5714. distance = sqrt(pow(p->posx - avg_pos.x*map_scale,2) + pow(p->posy - avg_pos.y*map_scale,2));
  5715. is_use_acc_filter = true;
  5716. }else{
  5717. return 0;
  5718. }
  5719. }else{
  5720. distance = sqrt(pow(p->posx - last_locate.x*map_scale,2) + pow(p->posy - last_locate.y*map_scale,2));
  5721. }
  5722. double base_period = 0.0;
  5723. //base_period = algorithm::base::ANCHOR_SYNCHRONIZATION_PERIOD;
  5724. base_period = algorithm::base::CARD_BROADCAST_PERIOD;
  5725. // 计算时间差,考虑跨周期的处理逻辑
  5726. if (time_stamp_cal > time_stamp_cal_last)
  5727. {
  5728. dt = (time_stamp_cal - time_stamp_cal_last)*base_period;
  5729. }else{
  5730. dt = (65536 - (time_stamp_cal - time_stamp_cal_last))*base_period;
  5731. }
  5732. //如果时间差为零
  5733. if (dt < ZERO_PRECISION)
  5734. {
  5735. dt = base_period;
  5736. }
  5737. // 计算速度
  5738. speed = distance / dt;
  5739. // 检查速度的合理性
  5740. if (speed > algorithm::base::TDOA_MAX_PERSON_SPEED)
  5741. {
  5742. return 1;
  5743. }
  5744. // 如果上一次无定位结果,则不计算加速度
  5745. if (last_locate.x == INVALID_COORDINATE && last_locate.y == INVALID_COORDINATE)
  5746. {
  5747. return 0;
  5748. }
  5749. // 计算加速度
  5750. acc = fabs(speed - v)/dt;
  5751. // 检查加速度的合理性
  5752. if (acc > algorithm::base::TDOA_MAX_ACCELERATION_SPEED)
  5753. {
  5754. return 2;
  5755. }
  5756. return 0;
  5757. }
  5758. /*
  5759. * 保存二维定位的数据
  5760. *
  5761. * param
  5762. * p 定位结果
  5763. *
  5764. * return
  5765. * 如果成功返回0,否则返回非零值
  5766. *
  5767. */
  5768. int Card::save_tdoa_data_2d(std::shared_ptr<POS>& p)
  5769. {
  5770. //测试,处理,将x,y的坐标做平均处理输出
  5771. //x = (last_locate.x + p->cx)/2;
  5772. //y = (last_locate.y + p->cy)/2;
  5773. x = p->cx;
  5774. y = p->cy;
  5775. last_locate.x = p->cx;
  5776. last_locate.y = p->cy;
  5777. time_stamp_cal_last = time_stamp_cal;
  5778. if (is_algo_first_location)
  5779. {
  5780. is_algo_first_location = false;
  5781. }
  5782. if (avg_coordinate.size() <= 5)
  5783. {
  5784. std::shared_ptr<HostServer::Position> position = std::make_shared<HostServer::Position>();
  5785. position->x = x;
  5786. position->y = y;
  5787. position->z = 0;
  5788. avg_coordinate.push_back(position);
  5789. }
  5790. Position pos;
  5791. pos.x = p->posx;
  5792. pos.y = p->posy;
  5793. pTaylorWls->set_position(pos);
  5794. char chLog[MAX_LOG_LENGTH] = {0};
  5795. std::string log = "";
  5796. #ifdef _DEBUG
  5797. sprintf_s(chLog,"card:%s, ct:%d, x:%.2f, y:%.2f, reader:%d"
  5798. , this->card_id.c_str(), this->time_stamp_cal
  5799. , pos.x, pos.y
  5800. , 0);
  5801. #else
  5802. sprintf_s(chLog,"card:%s, ct:%d, x:%.2f, y:%.2f, reader:%d"
  5803. , this->card_id.c_str(), this->time_stamp_cal
  5804. , pos.x, pos.y
  5805. , p_reader->reader_id);
  5806. #endif
  5807. log = chLog;
  5808. Log::write_log(FILE_TYPE::RAW_LOCATION_DATA,log,true);
  5809. return 0;
  5810. }
  5811. void Card::PosState( int val )
  5812. {
  5813. pos_state = val;
  5814. pos_state_count = pos_state_confirm_times;
  5815. }
  5816. int const Card::PosState()
  5817. {
  5818. if(pos_state_count >= pos_state_confirm_times )
  5819. {
  5820. return pos_state;
  5821. }
  5822. return pos_state_old;
  5823. }
  5824. void Card::PosStateOld( int val )
  5825. {
  5826. pos_state_old = val;
  5827. }
  5828. // 只显示优先级最高的状态
  5829. int const Card::StateBiz()
  5830. {
  5831. this->state_biz = 0; // 按优先级显示state_biz,优先级顺序:
  5832. // 进入盲区 > 呼救 > 呼叫 > 限制区域 > 区域超时 > 井下超时 > 超速 > 正常
  5833. if(status_area_over_speed == STATUS_ERROR){
  5834. state_biz = STATUS_AREA_OVER_SPEED;
  5835. }
  5836. if(status_area_over_time == STATUS_ERROR){
  5837. state_biz = STATUS_OVER_TIME;
  5838. }
  5839. if(status_area_forbidden == STATUS_ERROR){
  5840. state_biz = STATUS_AREA_FORBIDDEN;
  5841. }
  5842. if(status_area_over_time == STATUS_ERROR){
  5843. state_biz = STATUS_AREA_OVER_TIME;
  5844. }
  5845. if(status_call == STATUS_ERROR){
  5846. state_biz = STATUS_CALL;
  5847. }
  5848. if(status_help == STATUS_ERROR){
  5849. state_biz = STATUS_HELP;
  5850. }
  5851. // lihongzhen 2017/8/28 车辆取消盲区状态显示,只显示人员,用来清除盲区卡
  5852. //if(CT_PERSON == this->card_type)
  5853. //{
  5854. if(STATUS_LOST == status_lost)
  5855. {
  5856. state_biz = STATUS_LOST;
  5857. }
  5858. //}
  5859. return state_biz;
  5860. }
  5861. int Card::new_algo_tof_1d(int cnt)
  5862. {
  5863. auto first = _dists.begin();
  5864. //first = get_iterator(first);
  5865. if (first==_dists.end())
  5866. {
  5867. return 1;
  5868. }
  5869. auto second = first;
  5870. std::advance(second,1);
  5871. if (second==_dists.end())
  5872. {
  5873. return 2;
  5874. }
  5875. //second = get_iterator(second);
  5876. /*if (second==_dists.end())
  5877. {
  5878. return 1;
  5879. }*/
  5880. auto third = second;
  5881. std::advance(third,1);
  5882. if (third==_dists.end())
  5883. {
  5884. return 3;
  5885. }
  5886. //third = get_iterator(third);
  5887. if (first == _dists.end()||second==_dists.end()||third==_dists.end())
  5888. {
  5889. return 4;
  5890. }
  5891. AlgoParamData d[3];
  5892. int i = 0;
  5893. for (auto it:_dists)
  5894. {
  5895. for (auto it_dm:it.distmap)
  5896. {
  5897. d[i].ant[it_dm.second->antenna_id].x = it_dm.second->x;
  5898. d[i].ant[it_dm.second->antenna_id].y = it_dm.second->y;
  5899. d[i].ct = it_dm.second->t;
  5900. d[i].distance[it_dm.second->antenna_id] = it_dm.second->d;
  5901. d[i].anchor_id = it_dm.second->reader_id;
  5902. }
  5903. i++;
  5904. if (i>2)
  5905. {
  5906. break;
  5907. }
  5908. }
  5909. int ct = d[1].ct;
  5910. if (d[0].anchor_id!=d[1].anchor_id)
  5911. {
  5912. d[0].ct = 0;
  5913. d[0].distance[0] = d[1].distance[0];
  5914. d[0].distance[1] = d[1].distance[1];
  5915. d[0].ant[0].x = d[1].ant[0].x;
  5916. d[0].ant[0].y = d[1].ant[0].y;
  5917. d[0].ant[1].x = d[1].ant[1].x;
  5918. d[0].ant[1].y = d[1].ant[1].y;
  5919. }else if (d[1].anchor_id != d[2].anchor_id)
  5920. {
  5921. d[2].ct = 0;
  5922. d[2].distance[0] = d[1].distance[0];
  5923. d[2].distance[1] = d[1].distance[1];
  5924. d[2].ant[0].x = d[1].ant[0].x;
  5925. d[2].ant[0].y = d[1].ant[0].y;
  5926. d[2].ant[1].x = d[1].ant[1].x;
  5927. d[2].ant[1].y = d[1].ant[1].y;
  5928. }
  5929. double cx = CalculateX(d[0].ant[0].x*map_scale, d[0].ant[1].x*map_scale, d[0].ct, d[1].ct, last_locate.x*map_scale,
  5930. d[1].distance[0], d[1].distance[1], d[0].distance[0], d[0].distance[1], d[2].distance[0], d[2].distance[1]);
  5931. if (cx != INVALID_COORDINATE)
  5932. {
  5933. double speed = 0.0;
  5934. //增加加速度的过滤
  5935. if (last_locate.t != d[1].ct)
  5936. {
  5937. double new_x = cx/map_scale;
  5938. double unit = 1.0;
  5939. switch (this->card_type)
  5940. {
  5941. case CT_PERSON:
  5942. unit = 2.0;
  5943. case CT_VEHICLE:
  5944. unit = 1.0;
  5945. default:
  5946. unit = 1.0;
  5947. break;
  5948. }
  5949. speed = abs((new_x - last_locate.x)*map_scale/((last_locate.t - d[1].ct)*unit));
  5950. double acc = abs(speed - last_locate.v)/pow((last_locate.t - d[1].ct)*unit,2);
  5951. bool pass = false;
  5952. switch (card_type)
  5953. {
  5954. case CT_PERSON:
  5955. if (acc < PERSON_ACCELERATE_THRESHOLD)
  5956. {
  5957. pass = true;
  5958. }
  5959. break;
  5960. case CT_VEHICLE:
  5961. if (acc < VECHILE_ACCELERATE_THRESHOLD)
  5962. {
  5963. pass = true;
  5964. }
  5965. break;
  5966. }
  5967. if (pass)
  5968. {
  5969. this->x = cx/map_scale;
  5970. this->y = d[1].ant[0].y;
  5971. this->v = speed;
  5972. last_locate.x = this->x;
  5973. last_locate.y = d[1].ant[0].y;
  5974. last_locate.t = d[1].ct;
  5975. last_locate.v = speed;
  5976. this->t = d[1].ct;
  5977. char log[100] = {0};
  5978. sprintf_s(log,"calc: card_id: %s,ct: %d, x: %.3f, y: %.3f, acc: %.3f.",card_id.c_str(),d[1].ct,x,y,acc);
  5979. Log::write_log(FILE_TYPE::SYS_S,log,true);
  5980. }
  5981. }
  5982. }
  5983. return cx!=INVALID_COORDINATE?0:1;
  5984. }
  5985. //获得含有双天线的数据
  5986. DistQueMap::iterator Card::get_iterator(DistQueMap::iterator it)
  5987. {
  5988. if (it==_dists.end())
  5989. {
  5990. return std::move(it);
  5991. }
  5992. for (;it!=_dists.end();)
  5993. {
  5994. //删除其中的单条天线数据的元素
  5995. if (it->distmap.size()<=1)
  5996. {
  5997. //it = _dists.erase(++it);
  5998. ++it;
  5999. continue;
  6000. }
  6001. break;
  6002. }
  6003. return std::move(it);
  6004. }
  6005. /*
  6006. * 函数说明
  6007. * 参数
  6008. * L1:天线1的坐标
  6009. * L2:天线2的坐标
  6010. * last_ct:本分站上次收到本卡的数据ct值
  6011. * ct:本分站本次收到本卡的数据ct值
  6012. * last_x:本分站上次定位本卡的坐标
  6013. * dis1:本次本分站天线1对本卡的测距值
  6014. * dis2:本次本分站天线2对本卡的测距值
  6015. * last_dis1:上次本分站天线1对本卡的测距值
  6016. * last_dis2:上次本分站天线2对本卡的测距值
  6017. * next_dis1:下次本分站天线1对本卡的测距值
  6018. * next_dis2:下次本分站天线2对本卡的测距值
  6019. * 返回值
  6020. * 本次定位坐标
  6021. */
  6022. double Card::CalculateX(int L1, int L2, int last_ct, int ct, double last_x0, double dis1, double dis2, double last_dis1, double last_dis2, double next_dis1, double next_dis2)
  6023. {
  6024. //double x, x0, xt, xt1, xt2;
  6025. //int ctd;
  6026. //int L;
  6027. //double d1 = dis1;
  6028. //double d2 = dis2;
  6029. //double last_d1 = last_dis1;
  6030. //double last_d2 = last_dis2;
  6031. //double next_d1 = next_dis1;
  6032. //double next_d2 = next_dis2;
  6033. //ctd = ct - last_ct;
  6034. //x0 = (last_x0 - L1) * Sign(L2 - L1);
  6035. //L = abs(L1 - L2);
  6036. //if (d1 < IGNORE_H_DISTANCE && d2 < IGNORE_H_DISTANCE)//此情况下需考虑卡与天线的高差
  6037. //{
  6038. // if (d1 < H)
  6039. // d1 = H;
  6040. // if (d2 < H)
  6041. // d2 = H;
  6042. // d1 = sqrt(d1 * d1 - H * H);
  6043. // d2 = sqrt(d2 * d2 - H * H);
  6044. //}
  6045. //double dd = d1 - d2;
  6046. //x = L / 2 + Sign(dd) * (d1 + d2) / 2;//计算坐标的公式
  6047. ////如果两根天线测距都比H小,说明就在天线下方,且不是NLOS,直接返回0
  6048. //if (abs(d1) < ABSOLUTE0 && abs(d2) < ABSOLUTE0)
  6049. // return L1;
  6050. //if (last_d1 < IGNORE_H_DISTANCE && last_d2 < IGNORE_H_DISTANCE)//此情况下需考虑卡与天线的高差
  6051. //{
  6052. // if (last_d1 < H)
  6053. // last_d1 = H;
  6054. // if (last_d2 < H)
  6055. // last_d2 = H;
  6056. // last_d1 = sqrt(last_d1 * last_d1 - H * H);
  6057. // last_d2 = sqrt(last_d2 * last_d2 - H * H);
  6058. //}
  6059. //double last_dd = last_d1 - last_d2;
  6060. //double last_x = L / 2 + Sign(last_d1 - last_d2) * (last_d1 + last_d2) / 2;//初步计算上一次的坐标
  6061. //if (next_d1 < IGNORE_H_DISTANCE && next_d2 < IGNORE_H_DISTANCE)//此情况下需考虑卡与天线的高差
  6062. //{
  6063. // if (next_d1 < H)
  6064. // next_d1 = H;
  6065. // if (next_d2 < H)
  6066. // next_d2 = H;
  6067. // next_d1 = sqrt(next_d1 * next_d1 - H * H);
  6068. // next_d2 = sqrt(next_d2 * next_d2 - H * H);
  6069. //}
  6070. //double next_dd = next_d1 - next_d2;
  6071. //double next_x = L / 2 + Sign(next_d1 - next_d2) * (next_d1 + next_d2) / 2;//初步计算下一次的坐标
  6072. ////首次定位要确保正确
  6073. //if (ctd > 5)
  6074. //{
  6075. // if (abs(x - next_x) < card_type * MAX_SPEED)//本次定位与下次定位符合运动规律,可以定位
  6076. // return L1 + Sign(L2 - L1) * x;
  6077. // //locating_err_count++;
  6078. // return INVALID_COORDINATE;
  6079. //}
  6080. ////卡从分站的一边移动到分站的另一边时,需特殊处理
  6081. //if (Sign(x0) != Sign(dd) || Sign(next_x) != Sign(dd))
  6082. // if (Sign(x0) != Sign(next_x))
  6083. // if (abs(x0) < card_type * MAX_SPEED || abs(next_x) < card_type * MAX_SPEED || abs(x) < card_type * MAX_SPEED)
  6084. // {
  6085. // xt = (x0 + next_x) / 2;
  6086. // return L1 + Sign(L2 - L1) * xt;
  6087. // }
  6088. ////其它情况正常处理
  6089. ////测得的2天线距离差在阈值范围内,并且距离差的符号与前一条数据或后一条数据的符号相同,即数据没有“跳”,认为定位无误
  6090. //double dda = abs(d1 - d2);//两个天线测距差的绝对值
  6091. //if ((dda > L - 2 * E) && (dda < L + 2 * E) && (Sign(dd) == Sign(last_dd) || Sign(dd) == Sign(next_dd)))
  6092. //{
  6093. // return L1 + Sign(L2 - L1) * x;
  6094. //}
  6095. //xt1 = Sign(x0) * d1;//其它情况下,本次定位方向与上次定位相同
  6096. //xt2 = L + Sign(x0) * d2;
  6097. //xt = (xt1 + xt2) / 2;
  6098. //if ((abs(xt1 - x0) < ctd * card_type * MAX_SPEED) && (abs(xt2 - x0) < ctd * card_type * MAX_SPEED))//如果2个天线的测距值都符合运动规律,就以平均值为准计算坐标
  6099. //{
  6100. // return L1 + Sign(L2 - L1) * xt;
  6101. //}
  6102. //if (abs(xt1 - x0) < ctd * card_type * MAX_SPEED )//如果第一个天线的测距值符合运动规律,就以第一根天线的测距值为准计算坐标
  6103. //{
  6104. // return L1 + Sign(L2 - L1) * xt1;
  6105. //}
  6106. //if (abs(d2) > L)//此处判断是确保xt符号与x0符号一致
  6107. //{
  6108. // if (abs(xt2 - x0) < ctd * card_type * MAX_SPEED)//如果第二个天线的测距值符合运动规律,就以第二根天线的测距值为准计算坐标
  6109. // {
  6110. // return L1 + Sign(L2 - L1) * xt2;
  6111. // }
  6112. //}
  6113. //if (abs(x - last_x) < card_type * MAX_SPEED)//本次定位与上次定位符合运动规律,可以定位
  6114. // return L1 + Sign(L2 - L1) * x;
  6115. //if (abs(x - next_x) < card_type * MAX_SPEED)//本次定位与下次定位符合运动规律,可以定位
  6116. // return L1 + Sign(L2 - L1) * x;
  6117. ////locating_err_count++;
  6118. double x, x0, xt, xt1, xt2;
  6119. int ctd;
  6120. double L;
  6121. double d1 = dis1;
  6122. double d2 = dis2;
  6123. double last_d1 = last_dis1;
  6124. double last_d2 = last_dis2;
  6125. double next_d1 = next_dis1;
  6126. double next_d2 = next_dis2;
  6127. ctd = ct - last_ct;
  6128. x0 = (last_x0 - L1) * Sign(L2 - L1);
  6129. L = abs(L1 - L2);
  6130. if (d1 < IGNORE_H_DISTANCE && d2 < IGNORE_H_DISTANCE)//此情况下需考虑卡与天线的高差
  6131. {
  6132. if (d1 < H)
  6133. d1 = H;
  6134. if (d2 < H)
  6135. d2 = H;
  6136. d1 = sqrt(d1 * d1 - H * H);
  6137. d2 = sqrt(d2 * d2 - H * H);
  6138. }
  6139. double dd = d1 - d2;
  6140. double dda = abs(d1 - d2);//两个天线测距差的绝对值
  6141. //cout << "dis1: " << dis1 << ", ";
  6142. //cout << "dis2: " << dis2 << ", ";
  6143. //cout << "dis1 - dis2 = " << dis1 - dis2 << " ";
  6144. //cout << "d1: " << d1 << ", ";
  6145. //cout << "d2: " << d2 << ", ";
  6146. //cout << "d1 - d2 = " << dd << ", ";
  6147. x = L / 2 + Sign(dd) * (d1 + d2) / 2;//计算坐标的公式
  6148. //如果两根天线测距都比H小,说明就在天线下方,且不是NLOS,直接返回0
  6149. if (abs(d1) < ABSOLUTE0 && abs(d2) < ABSOLUTE0)
  6150. return L1;
  6151. if (last_d1 < IGNORE_H_DISTANCE && last_d2 < IGNORE_H_DISTANCE)//此情况下需考虑卡与天线的高差
  6152. {
  6153. if (last_d1 < H)
  6154. last_d1 = H;
  6155. if (last_d2 < H)
  6156. last_d2 = H;
  6157. last_d1 = sqrt(last_d1 * last_d1 - H * H);
  6158. last_d2 = sqrt(last_d2 * last_d2 - H * H);
  6159. }
  6160. double last_dd = last_d1 - last_d2;
  6161. double last_x = L / 2 + Sign(last_d1 - last_d2) * (last_d1 + last_d2) / 2;//初步计算上一次的坐标
  6162. if (next_d1 < IGNORE_H_DISTANCE && next_d2 < IGNORE_H_DISTANCE)//此情况下需考虑卡与天线的高差
  6163. {
  6164. if (next_d1 < H)
  6165. next_d1 = H;
  6166. if (next_d2 < H)
  6167. next_d2 = H;
  6168. next_d1 = sqrt(next_d1 * next_d1 - H * H);
  6169. next_d2 = sqrt(next_d2 * next_d2 - H * H);
  6170. }
  6171. double next_dd = next_d1 - next_d2;
  6172. double next_x = L / 2 + Sign(next_d1 - next_d2) * (next_d1 + next_d2) / 2;//初步计算下一次的坐标
  6173. //首次定位要确保正确
  6174. if (ctd > 5 )
  6175. {
  6176. if ((dda > L - 2.5 * E) && (dda < L + 2.5 * E) && (abs(x - next_x) < card_type * MAX_SPEED))//本次定位与下次定位符合运动规律,可以定位
  6177. return L1 + Sign(L2 - L1) * x;
  6178. //locating_err_count++;
  6179. return INVALID_COORDINATE;
  6180. }
  6181. //卡从分站的一边移动到分站的另一边时,需特殊处理
  6182. if (Sign(x0) != Sign(dd) || Sign(next_x) != Sign(dd))
  6183. if (Sign(x0) != Sign(next_x))
  6184. if (abs(x0) < card_type * MAX_SPEED || abs(next_x) < card_type * MAX_SPEED || abs(x) < card_type * MAX_SPEED)
  6185. {
  6186. xt = (x0 + next_x) / 2;
  6187. return L1 + Sign(L2 - L1) * xt;
  6188. }
  6189. //其它情况正常处理
  6190. //测得的2天线距离差在阈值范围内,并且距离差的符号与前一条数据的符号相同,即数据没有“跳”,认为定位无误
  6191. if ((dda > L - 2 * E) && (dda < L + 2 * E) && (Sign(dd) == Sign(last_dd) ))
  6192. {
  6193. return L1 + Sign(L2 - L1) * x;
  6194. }
  6195. xt1 = Sign(x0) * d1;//其它情况下,本次定位方向与上次定位相同
  6196. xt2 = L + Sign(x0) * d2;
  6197. xt = (xt1 + xt2) / 2;
  6198. if ((abs(xt1 - x0) < ctd * card_type * MAX_SPEED) && (abs(xt2 - x0) < ctd * card_type * MAX_SPEED))//如果2个天线的测距值都符合运动规律,就以平均值为准计算坐标
  6199. {
  6200. return L1 + Sign(L2 - L1) * xt;
  6201. }
  6202. if (abs(xt1 - x0) < ctd * card_type * MAX_SPEED )//如果第一个天线的测距值符合运动规律,就以第一根天线的测距值为准计算坐标
  6203. {
  6204. return L1 + Sign(L2 - L1) * xt1;
  6205. }
  6206. if (abs(d2) > L)//此处判断是确保xt符号与x0符号一致
  6207. {
  6208. if (abs(xt2 - x0) < ctd * card_type * MAX_SPEED)//如果第二个天线的测距值符合运动规律,就以第二根天线的测距值为准计算坐标
  6209. {
  6210. return L1 + Sign(L2 - L1) * xt2;
  6211. }
  6212. }
  6213. if (abs(x - last_x) < card_type * MAX_SPEED)//本次定位与上次定位符合运动规律,可以定位
  6214. return L1 + Sign(L2 - L1) * x;
  6215. if (abs(x - next_x) < card_type * MAX_SPEED)//本次定位与下次定位符合运动规律,可以定位
  6216. return L1 + Sign(L2 - L1) * x;
  6217. //locating_err_count++;
  6218. return INVALID_COORDINATE;//以上条件都不满足,本次定位失败
  6219. }
  6220. double Card::CalculateX(double L1, double L2, int last_ct, int ct, double last_x0, double dis1, double dis2, double last_dis1, double last_dis2, double next_dis1, double next_dis2)
  6221. {
  6222. double x, x0, xt, xt1, xt2;
  6223. int ctd;
  6224. double L;
  6225. double d1 = dis1;
  6226. double d2 = dis2;
  6227. double last_d1 = last_dis1;
  6228. double last_d2 = last_dis2;
  6229. double next_d1 = next_dis1;
  6230. double next_d2 = next_dis2;
  6231. ctd = ct - last_ct;
  6232. if (ctd < 0)//ct跨周期的情况
  6233. ctd += 65536;
  6234. x0 = (last_x0 - L1) * Sign(L2 - L1);
  6235. L = abs(L1 - L2);
  6236. if (d1 < IGNORE_H_DISTANCE && d2 < IGNORE_H_DISTANCE)//此情况下需考虑卡与天线的高差
  6237. {
  6238. if (d1 < H)
  6239. d1 = H;
  6240. if (d2 < H)
  6241. d2 = H;
  6242. d1 = sqrt(d1 * d1 - H * H);
  6243. d2 = sqrt(d2 * d2 - H * H);
  6244. }
  6245. double dd = d1 - d2;
  6246. double dda = abs(d1 - d2);//两个天线测距差的绝对值
  6247. /*cout << "dis1: " << dis1 << ", ";
  6248. cout << "dis2: " << dis2 << ", ";
  6249. cout << "dis1 - dis2 = " << dis1 - dis2 << " ";
  6250. cout << "d1: " << d1 << ", ";
  6251. cout << "d2: " << d2 << ", ";
  6252. cout << "d1 - d2 = " << dd << ", ";*/
  6253. x = L / 2 + Sign(dd) * (d1 + d2) / 2;//计算坐标的公式
  6254. if (abs(dis1) < ABSOLUTE0 )//只有dis2的单天线数据的情况,争取单天线数据也能定位
  6255. {
  6256. if (abs(ctd) > 5)//单天线数据不用于首次定位
  6257. return INVALID_COORDINATE;
  6258. xt2 = L + Sign(x0) * d2;
  6259. if (abs(xt2 - x0) < ctd * card_type * MAX_SPEED)//本次定位与上次定位符合运动规律,可以定位
  6260. return L1 + Sign(L2 - L1) * xt2;
  6261. }
  6262. if (abs(dis2) < ABSOLUTE0 )//只有dis1的单天线数据的情况,争取单天线数据也能定位
  6263. {
  6264. if (abs(ctd) > 5)//单天线数据不用于首次定位
  6265. return INVALID_COORDINATE;
  6266. xt1 = Sign(x0) * d1;;
  6267. if (abs(xt1 - x0) < ctd * card_type * MAX_SPEED)//本次定位与上次定位符合运动规律,可以定位
  6268. return L1 + Sign(L2 - L1) * xt1;
  6269. }
  6270. //如果两根天线测距都比H小,说明就在天线下方,且不是NLOS,直接返回0
  6271. if (abs(d1) < ABSOLUTE0 && abs(d2) < ABSOLUTE0)
  6272. return L1;
  6273. if (last_d1 < IGNORE_H_DISTANCE && last_d2 < IGNORE_H_DISTANCE)//此情况下需考虑卡与天线的高差
  6274. {
  6275. if (last_d1 < H)
  6276. last_d1 = H;
  6277. if (last_d2 < H)
  6278. last_d2 = H;
  6279. last_d1 = sqrt(last_d1 * last_d1 - H * H);
  6280. last_d2 = sqrt(last_d2 * last_d2 - H * H);
  6281. }
  6282. double last_dd = last_d1 - last_d2;
  6283. double last_x = L / 2 + Sign(last_d1 - last_d2) * (last_d1 + last_d2) / 2;//初步计算上一次的坐标
  6284. if (next_d1 < IGNORE_H_DISTANCE && next_d2 < IGNORE_H_DISTANCE)//此情况下需考虑卡与天线的高差
  6285. {
  6286. if (next_d1 < H)
  6287. next_d1 = H;
  6288. if (next_d2 < H)
  6289. next_d2 = H;
  6290. next_d1 = sqrt(next_d1 * next_d1 - H * H);
  6291. next_d2 = sqrt(next_d2 * next_d2 - H * H);
  6292. }
  6293. double next_dd = next_d1 - next_d2;
  6294. double next_x = L / 2 + Sign(next_d1 - next_d2) * (next_d1 + next_d2) / 2;//初步计算下一次的坐标
  6295. //首次定位要确保正确
  6296. if (ctd > 5 )
  6297. {
  6298. if ((dda > L - 2.5 * E) && (dda < L + 2.5 * E) && (abs(x - next_x) < card_type * MAX_SPEED))//本次定位与下次定位符合运动规律,可以定位
  6299. return L1 + Sign(L2 - L1) * x;
  6300. //locating_err_count++;
  6301. return INVALID_COORDINATE;
  6302. }
  6303. //卡从分站的一边移动到分站的另一边时,需特殊处理
  6304. if (Sign(x0) != Sign(dd) || Sign(next_x) != Sign(dd))
  6305. if (Sign(x0) != Sign(next_x))
  6306. if (abs(x0) < card_type * MAX_SPEED || abs(next_x) < card_type * MAX_SPEED || abs(x) < card_type * MAX_SPEED)
  6307. {
  6308. xt = (x0 + next_x) / 2;
  6309. return L1 + Sign(L2 - L1) * xt;
  6310. }
  6311. //其它情况正常处理
  6312. //测得的2天线距离差在阈值范围内,并且距离差的符号与前一条数据的符号相同,即数据没有“跳”,认为定位无误
  6313. if ((dda > L - 2 * E) && (dda < L + 2 * E) && (Sign(dd) == Sign(last_dd) ))
  6314. return L1 + Sign(L2 - L1) * x;
  6315. xt1 = Sign(x0) * d1;//其它情况下,本次定位方向与上次定位相同
  6316. xt2 = L + Sign(x0) * d2;
  6317. xt = (xt1 + xt2) / 2;
  6318. if ((abs(xt1 - x0) < ctd * card_type * MAX_SPEED) && (abs(xt2 - x0) < ctd * card_type * MAX_SPEED))//如果2个天线的测距值都符合运动规律,就以平均值为准计算坐标
  6319. return L1 + Sign(L2 - L1) * xt;
  6320. if (abs(xt1 - x0) < ctd * card_type * MAX_SPEED )//如果第一个天线的测距值符合运动规律,就以第一根天线的测距值为准计算坐标
  6321. return L1 + Sign(L2 - L1) * xt1;
  6322. if (abs(d2) > L)//此处判断是确保xt符号与x0符号一致
  6323. if (abs(xt2 - x0) < ctd * card_type * MAX_SPEED)//如果第二个天线的测距值符合运动规律,就以第二根天线的测距值为准计算坐标
  6324. return L1 + Sign(L2 - L1) * xt2;
  6325. if (abs(x - last_x) < card_type * MAX_SPEED)//本次定位与上次定位符合运动规律,可以定位
  6326. return L1 + Sign(L2 - L1) * x;
  6327. if (abs(x - next_x) < card_type * MAX_SPEED)//本次定位与下次定位符合运动规律,可以定位
  6328. return L1 + Sign(L2 - L1) * x;
  6329. //locating_err_count++;
  6330. return INVALID_COORDINATE;//以上条件都不满足,本次定位失败
  6331. }
  6332. int Card::Sign(double x)
  6333. {
  6334. if(x == 0)
  6335. return 0;
  6336. if(x > 0)
  6337. return 1;
  6338. return -1;
  6339. }
  6340. void Card::set_reader_path(std::shared_ptr<ReaderPathMap> rpm, std::shared_ptr<TDOAReaderPathMap> trpm)
  6341. {
  6342. this->set_reader_path_tof(rpm);
  6343. this->set_reader_path_tdoa(trpm);
  6344. }
  6345. void Card::cal_pos_by_cache()
  6346. {
  6347. EnterCriticalSection(&m_csCard);
  6348. if(_dists.size() > 0){
  6349. // 超过缓存数量限制
  6350. LOCATION_SYSTEM_BRANCH(LOCATION_SYSTEM_BRANCH_137);
  6351. get_coordinate();
  6352. _dists.pop_front();
  6353. }
  6354. if (this->x != this->last_x || this->y != this->last_y)
  6355. {
  6356. LOCATION_SYSTEM_BRANCH(LOCATION_SYSTEM_BRANCH_138);
  6357. if (CT_VEHICLE == this->card_type)
  6358. {
  6359. //只有车辆需要进行如下偏移计算
  6360. algo_calc_offset();
  6361. }
  6362. this->n_pos_no_change_times = 0;
  6363. this->b_pos_change = true;
  6364. }else{
  6365. LOCATION_SYSTEM_BRANCH(LOCATION_SYSTEM_BRANCH_139);
  6366. this->n_pos_no_change_times++;
  6367. this->b_pos_change = false;
  6368. }
  6369. LeaveCriticalSection(&m_csCard);
  6370. }
  6371. int Card::dist_cache_size()
  6372. {
  6373. return _dists.size();
  6374. }
  6375. MapInfo::MapInfo( void )
  6376. {
  6377. }
  6378. MapInfo::~MapInfo( void )
  6379. {
  6380. }
  6381. Dept::Dept( int id, string name )
  6382. {
  6383. dept_id = id;
  6384. dept_name = name;
  6385. }
  6386. OccLevel::OccLevel(int id )
  6387. {
  6388. occlevel_id = id;
  6389. }
  6390. Adhoc::Adhoc()
  6391. {
  6392. adhoc_id = 0;
  6393. x = 0;
  6394. y = 0;
  6395. z = 0;
  6396. idx = 0;
  6397. }
  6398. Adhoc::~Adhoc()
  6399. {
  6400. }
  6401. Light::Light()
  6402. {
  6403. m_nID = m_nMapID = m_nAreaID = m_nLightsGroupID = m_nReaderID = m_nSectionID = m_nPort = m_nState = m_nStream = 0;
  6404. device_type_id = 0;
  6405. m_nOldState = 0;
  6406. m_nShape = 0;
  6407. x = y = z = 0.0;
  6408. m_nIsCtrl = LIGHT_CTRL_STATE::CTRL_STATE_AUTO;
  6409. m_strIP = m_strName = m_strLabel = "";
  6410. rec_time = time(NULL);
  6411. alarm_start_time = time(NULL);
  6412. last_send_time = time(NULL);
  6413. }
  6414. int Light::get_light_state()
  6415. {
  6416. return m_nState;
  6417. }
  6418. Chamber::Chamber()
  6419. {
  6420. m_nID = m_nIsUsed = m_nMapID = m_nState = m_nSectionID = 0;
  6421. m_strPath = m_strName = m_strLabel = "";
  6422. }
  6423. Chamber::~Chamber()
  6424. {
  6425. }
  6426. /*
  6427. * 初始化区域边界
  6428. *
  6429. * param
  6430. * path 边界字符串描述
  6431. *
  6432. * return
  6433. * 成功返回0,失败返回1
  6434. */
  6435. int BaseArea::init_border(string path)
  6436. {
  6437. if(path == "" || path.find(',') == std::string::npos){
  6438. return 1;
  6439. }
  6440. std::vector<std::string> vec = Split(path, " ");
  6441. std::vector<std::string>::iterator it = vec.begin();
  6442. if(m_pPolygon){
  6443. delete[] m_pPolygon;
  6444. m_pPolygon = NULL;
  6445. }
  6446. m_pPolygon = new _point[vec.size()];
  6447. m_nPolygonCount = 0;
  6448. for(; it != vec.end(); ++it){
  6449. std::vector<std::string> subvec = Split(it->c_str(), ",");
  6450. _point p;
  6451. p.x = GetVertex(subvec[0]);
  6452. p.y = GetVertex(subvec[1]);
  6453. p.z = 0;
  6454. m_pPolygon[m_nPolygonCount] = p;
  6455. m_nPolygonCount++;
  6456. }
  6457. return 0;
  6458. }
  6459. bool BaseArea::IsInPolygon(_point p)
  6460. {
  6461. if(m_pPolygon == NULL){
  6462. return false;
  6463. }
  6464. int counter = 0;
  6465. int i;
  6466. double xinters;
  6467. _point p1,p2;
  6468. p1 = m_pPolygon[0];
  6469. for (int i=1;i<= m_nPolygonCount;i++) {
  6470. p2 = m_pPolygon[i % m_nPolygonCount];
  6471. if (p.y > MIN(p1.y,p2.y)) {
  6472. if (p.y <= MAX(p1.y,p2.y)) {
  6473. if (p.x <= MAX(p1.x,p2.x)) {
  6474. if (p1.y != p2.y) {
  6475. xinters = (p.y-p1.y)*(p2.x-p1.x)/(p2.y-p1.y)+p1.x;
  6476. if (p1.x == p2.x || p.x <= xinters)
  6477. counter++;
  6478. }
  6479. }
  6480. }
  6481. }
  6482. p1 = p2;
  6483. }
  6484. return (counter % 2 == 0) ? false : true;
  6485. }
  6486. double BaseArea::GetVertex(std::string src)
  6487. {
  6488. std::string dest = "";
  6489. for(unsigned int i = 0; i < src.length(); i++){
  6490. if((src[i] >= '0' && src[i]<='9') || src[i]=='-' || src[i] == '.'){
  6491. dest += src[i];
  6492. }
  6493. }
  6494. return atof(dest.c_str());
  6495. }
  6496. std::vector<std::string> BaseArea::Split(std::string str,std::string pattern)
  6497. {
  6498. std::string::size_type pos;
  6499. std::vector<std::string> result;
  6500. str+=pattern;//扩展字符串以方便操作
  6501. unsigned int size=str.size();
  6502. for(unsigned int i=0; i<size; i++){
  6503. pos=str.find(pattern,i);
  6504. if(pos<size){
  6505. std::string s=str.substr(i,pos-i);
  6506. result.push_back(s);
  6507. i=pos+pattern.size()-1;
  6508. }
  6509. }
  6510. return result;
  6511. }
  6512. Section::Section()
  6513. {
  6514. m_nID = m_nMapId = m_nState = 0;
  6515. m_strName = m_strLabel = m_strPath = "";
  6516. for(int i = 0; i < SECTION_EVENT_COUNT; i++){
  6517. m_event_list[i] = 0;
  6518. }
  6519. }
  6520. bool Section::is_has_chamber()
  6521. {
  6522. bool bIsHasEmptyChamber = false;
  6523. if (mp_champer_list.size()<=0)
  6524. {
  6525. return bIsHasEmptyChamber;
  6526. }
  6527. ChamberMap::iterator it = mp_champer_list.begin();
  6528. for (;it!=mp_champer_list.end();++it)
  6529. {
  6530. if (it->second->m_nIsUsed == false)
  6531. {
  6532. bIsHasEmptyChamber = true;
  6533. break;
  6534. }
  6535. }
  6536. return bIsHasEmptyChamber;
  6537. }
  6538. int Section::get_section_state()
  6539. {
  6540. int nTotals = 0;
  6541. nTotals = mp_vehicle_list.size();
  6542. switch (nTotals)
  6543. {
  6544. case 0:
  6545. m_nState = SECTION_STATE_NORMAL;
  6546. break;
  6547. case 1:
  6548. m_nState = SECTION_STATE_BUSY;
  6549. break;
  6550. case 2:
  6551. m_nState = SECTION_STATE_CONGESTION;
  6552. break;
  6553. default:
  6554. m_nState = SECTION_STATE_CONGESTION;
  6555. break;
  6556. }
  6557. return m_nState;
  6558. }
  6559. int Section::get_section_vehicle_counts()
  6560. {
  6561. return mp_vehicle_list.size();
  6562. }
  6563. int Section::get_section_staffer_counts()
  6564. {
  6565. return mp_staffer_list.size();
  6566. }
  6567. LightsGroup::LightsGroup()
  6568. {
  6569. m_bIsUsed = false;
  6570. m_nID = m_nState = 0;
  6571. m_strLabel = m_strName = m_strVechileId = "";
  6572. }
  6573. /*
  6574. * 判断红绿灯组中是否有此灯
  6575. *
  6576. * param
  6577. * pLight 灯对象
  6578. *
  6579. * return
  6580. * 存在返回true,不存在返回false
  6581. */
  6582. bool LightsGroup::isExist(std::shared_ptr<Light> pLight)
  6583. {
  6584. bool bRet = false;
  6585. for (LightMap::iterator it = mp_lights_list.begin();it != mp_lights_list.end();++it)
  6586. {
  6587. //如果红绿灯的id,地图id,路段id,都和参数一致,且灯状态可用,即为找到
  6588. if (it->second->m_nID == pLight->m_nID
  6589. && it->second->m_nMapID == pLight->m_nMapID
  6590. && it->second->m_nSectionID == pLight->m_nSectionID
  6591. && it->second->m_nState == 0)
  6592. {
  6593. bRet = true;
  6594. }
  6595. }
  6596. return bRet;
  6597. }
  6598. /*
  6599. * 判断灯组内灯的颜色是否一致
  6600. *
  6601. * param
  6602. * nColor 指定颜色(红色或绿色)
  6603. *
  6604. * return
  6605. * 相同返回true,否则返回false
  6606. *
  6607. */
  6608. bool LightsGroup::isAllLightColor(int nColor)
  6609. {
  6610. bool bRet = true;
  6611. for (LightMap::iterator it = mp_lights_list.begin();it != mp_lights_list.end();++it)
  6612. {
  6613. if (it->second->m_nColor != nColor)
  6614. {
  6615. bRet = false;
  6616. }
  6617. }
  6618. return bRet;
  6619. }
  6620. PatrolTask::PatrolTask()
  6621. {
  6622. patrol_task_id = patrol_path_id = 0;
  6623. card_id = "";
  6624. starffer_id = "";
  6625. start_time = end_time = time(NULL);
  6626. mpPoint.swap(map<unsigned int, std::shared_ptr<PatrolPoint>>());
  6627. cur_point_idx = 1;
  6628. enter_time = leave_time = time(NULL);
  6629. state = stay_state = duration_stay = 0;
  6630. is_in_cur_point = false;
  6631. }
  6632. PatrolTask::~PatrolTask()
  6633. {
  6634. }
  6635. PatrolPoint::PatrolPoint()
  6636. {
  6637. idx = 0;
  6638. patrol_point_id = 0;
  6639. map_id = 0;
  6640. x = y = z = 0;
  6641. ranging = 0; // 距离范围
  6642. duration_last = duration_stay_min = duration_stay_max = duration_ranging = 0;
  6643. }
  6644. PatrolPoint::~PatrolPoint()
  6645. {
  6646. }
  6647. BanShift::BanShift()
  6648. {
  6649. }
  6650. BanShift::BanShift( int id, std::string s, std::string e, int tid )
  6651. {
  6652. shift_id = id;
  6653. start_time = s;
  6654. end_time = e;
  6655. shift_type_id = tid;
  6656. }
  6657. BanShift::~BanShift()
  6658. {
  6659. }
  6660. void Card::resetKalmanInfo()
  6661. {
  6662. cal_location = new CalLocation(KALMAN_OFFSET_COOR, KALMAN_OFFSET_MOVE, KALMAN_OFFSET_RANGING, KALMAN_INTERVAL);
  6663. p2_anchors = new Point2[ANCHOR_COUNT];
  6664. //cal_location3 = new CalLocation3(KALMAN_OFFSET_COOR_TRI, KALMAN_OFFSET_COOR_TRI, KALMAN_OFFSET_COOR_TRI,
  6665. // KALMAN_OFFSET_MOVE_TRI, KALMAN_OFFSET_MOVE_TRI, 0, KALMAN_OFFSET_RANGING_TRI, KALMAN_INTERVAL_TRI);
  6666. cal_location3 = new CalLocation3(KALMAN_OFFSET_COOR_TRI, KALMAN_OFFSET_MOVE_TRI, KALMAN_OFFSET_RANGING_TRI, KALMAN_INTERVAL_TRI);
  6667. p3_anchors = new Point3[ANCHOR_COUNT];
  6668. is_init_kalman = false;
  6669. p_dists = new std::shared_ptr<_coordinate>[DIST_COUNT];
  6670. p_dists_locate = new std::shared_ptr<_coordinate>[DIST_COUNT];
  6671. for(int i = 0; i < DIST_COUNT; i++){
  6672. p_dists[i] = NULL;
  6673. p_dists_locate[i] = NULL;
  6674. }
  6675. }
  6676. void Card::set_anchors(int cnt)
  6677. {
  6678. bool bExist = false;
  6679. for(auto it : distmap)
  6680. {
  6681. bExist = false;
  6682. for(int i=0; i<cnt; i++)
  6683. {
  6684. if(cnt > 3)
  6685. {
  6686. if(p3_anchors[i].x == it.second->x && p3_anchors[i].y == it.second->y &&
  6687. p3_anchors[i].z == it.second->z)
  6688. {
  6689. bExist = true;
  6690. break;
  6691. }
  6692. }
  6693. else if(cnt > 2)
  6694. {
  6695. if(p2_anchors[i].x == it.second->x && p2_anchors[i].y == it.second->y)
  6696. {
  6697. bExist = true;
  6698. break;
  6699. }
  6700. }
  6701. }
  6702. if(!bExist)
  6703. {
  6704. is_anchor_changed = true;
  6705. break;
  6706. }
  6707. }
  6708. if(is_anchor_changed)
  6709. {
  6710. int i = 0;
  6711. if(cnt > 3)
  6712. {
  6713. for(auto it: distmap)
  6714. {
  6715. p3_anchors[i].x = it.second->x;
  6716. p3_anchors[i].y = it.second->y;
  6717. p3_anchors[i].z = it.second->z;
  6718. i++;
  6719. }
  6720. }
  6721. else if(cnt > 2)
  6722. {
  6723. //for(auto it: _dists.front().distmap)
  6724. for(auto it: distmap)
  6725. {
  6726. p2_anchors[i].x = it.second->x;
  6727. p2_anchors[i].y = it.second->y;
  6728. i++;
  6729. }
  6730. }
  6731. }
  6732. }
  6733. void Card::get_coordinate_2d(int cnt)
  6734. {
  6735. #ifdef ENABLE_DEBUG_2D_TEST_LOG
  6736. char log[MAX_LOG_LENGTH];
  6737. memset(log,0,MAX_LOG_LENGTH*sizeof(char));
  6738. sprintf_s(log,"enter 2d location: card_id: %s, ct: %d",card_id.c_str(),time_stamp);
  6739. Log::write_log(FILE_TYPE::SYS_S,log,true);
  6740. #endif
  6741. double d_dist[3];
  6742. Point2 pt2, pt2_1;
  6743. set_anchors(cnt);
  6744. int i = 0;
  6745. for(auto it : distmap)
  6746. {
  6747. for(int j=0; j<cnt; j++)
  6748. {
  6749. if(p2_anchors[j].x == it.second->x && p2_anchors[j].y == it.second->y)
  6750. {
  6751. //d_dist[j] = it.second->d;
  6752. // lihongzhen 2017/10/24 利用投影计算二维空间值
  6753. d_dist[j] = sqrt(pow(it.second->d,2) - pow(it.second->z, 2));
  6754. }
  6755. }
  6756. if (i == 0)
  6757. {
  6758. time_stamp_cal = it.second->t;
  6759. time_cur_algo = it.second->cur_time;
  6760. }
  6761. i++;
  6762. }
  6763. Mat mat(3, 1, d_dist);
  6764. if(is_anchor_changed){
  6765. cal_location->setAnchors(p2_anchors);
  6766. pt2 = cal_location->initPos(mat);
  6767. is_anchor_changed = false;
  6768. } else{
  6769. pt2 = cal_location->estiPos(mat);
  6770. }
  6771. pt2_1 = cal_location->getError();
  6772. _point p;
  6773. p.x = pt2.x;
  6774. p.y = pt2.y;
  6775. #ifdef ENABLE_DEBUG_2D_TEST_LOG
  6776. memset(log,0,MAX_LOG_LENGTH*sizeof(char));
  6777. sprintf_s(log,"2d location before handling:card_id: %s, ct: %d, x : %.3f, y: %.3f",card_id.c_str(),time_stamp,p.x,p.y);
  6778. Log::write_log(FILE_TYPE::SYS_S,log,true);
  6779. #endif
  6780. if(deal_position_boundary(p)){
  6781. #ifdef ENABLE_DEBUG_2D_TEST_LOG
  6782. memset(log,0,MAX_LOG_LENGTH*sizeof(char));
  6783. sprintf_s(log,"2d location failed:card_id: %s, ct: %d, x : %.3f, y: %.3f",card_id.c_str(),time_stamp,p.x,p.y);
  6784. Log::write_log(FILE_TYPE::SYS_S,log,true);
  6785. #endif
  6786. return;
  6787. }
  6788. //if(deal_position_boundary(p)){
  6789. // return;
  6790. //}
  6791. error_locate.x = pt2_1.x, error_locate.y = pt2_1.y;
  6792. if (!CheckPositionValid(error_locate.x,error_locate.y))
  6793. {
  6794. error_locate.x = error_locate.y = INVALID_COORDINATE;
  6795. }
  6796. last_locate.x = x = p.x;
  6797. last_locate.y = y = p.y;
  6798. z = 0;
  6799. time_last_tof = time_cur_algo;
  6800. this->t = this->last_locate.t = time_stamp_cal;
  6801. this->time_stamp_last = time_stamp_cal;
  6802. is_deal_by_algo = true;
  6803. if (is_first_location)
  6804. {
  6805. is_first_location = false;
  6806. }
  6807. #ifdef ENABLE_DEBUG_2D_TEST_LOG
  6808. memset(log,0,MAX_LOG_LENGTH*sizeof(char));
  6809. sprintf_s(log,"2d location output: card_id: %s, ct: %d, x : %.3f, y: %.3f",card_id.c_str(),time_stamp,x,y);
  6810. Log::write_log(FILE_TYPE::SYS_S,log,true);
  6811. #endif
  6812. }
  6813. void Card::get_coordinate_2d_nofilter(int cnt)
  6814. {
  6815. double d[3];
  6816. double solution[2];
  6817. _Matrix pi;
  6818. matrix_set(&pi, 3, 2);
  6819. matrix_init(&pi);
  6820. //获得坐标
  6821. int i = 0;
  6822. for(auto it : distmap)
  6823. {
  6824. matrix_write(&pi, i, 0, it.second->x);
  6825. matrix_write(&pi, i, 1, it.second->y);
  6826. d[i] = sqrt(pow(it.second->d,2) - pow(it.second->z,2));
  6827. if (i == 0)
  6828. {
  6829. time_stamp_cal = it.second->t;
  6830. }
  6831. i++;
  6832. }
  6833. _point p;
  6834. if (locate_func(&pi, d, solution) > 0)
  6835. {
  6836. p.x = solution[0];
  6837. p.y = solution[1];
  6838. last_locate.x = x = p.x;
  6839. last_locate.y = y = p.y;
  6840. z = 0;
  6841. this->time_stamp_last = time_stamp_cal;
  6842. is_deal_by_algo = true;
  6843. if (is_first_location)
  6844. {
  6845. is_first_location = false;
  6846. }
  6847. }
  6848. }
  6849. void Card::get_coordinate_3d(int cnt)
  6850. {
  6851. double d_dist[ANCHOR_COUNT];
  6852. Point3 pt3, pt3_1;
  6853. set_anchors(cnt);
  6854. int i = 0;
  6855. for(auto it : distmap)
  6856. {
  6857. for(int j=0; j<cnt; j++)
  6858. {
  6859. if(p3_anchors[j].x == it.second->x && p3_anchors[j].y == it.second->y &&
  6860. p3_anchors[j].z == it.second->z)
  6861. {
  6862. d_dist[j] = it.second->d;
  6863. }
  6864. }
  6865. if (i == 0)
  6866. {
  6867. time_stamp_cal = it.second->t;
  6868. }
  6869. i++;
  6870. }
  6871. Mat mat(ANCHOR_COUNT, 1, d_dist);
  6872. if(is_anchor_changed){
  6873. cal_location3->setAnchors(p3_anchors, ANCHOR_COUNT);
  6874. pt3 = cal_location3->initPos(mat);
  6875. is_anchor_changed = false;
  6876. } else{
  6877. pt3 = cal_location3->estiPos(mat);
  6878. }
  6879. x = pt3.x;
  6880. y = pt3.y;
  6881. z = pt3.z;
  6882. this->time_stamp_cal_last = time_stamp_cal;
  6883. is_deal_by_algo = true;
  6884. }
  6885. void Card::get_coordinate_old(int cnt)
  6886. {
  6887. if(cnt > 3)
  6888. {
  6889. get_coordinate_3d(cnt);
  6890. }
  6891. else if(cnt > 2)
  6892. {
  6893. get_coordinate_2d(cnt);
  6894. }
  6895. }
  6896. void Card::add_dist_ex(std::shared_ptr<_coordinate> dist)
  6897. {
  6898. bool b_exist = false;
  6899. int min_stamp = dist->t, min_stamp_index = -1;
  6900. time_stamp_max = dist->t;
  6901. for(int i = 0; i < DIST_COUNT; i++ ){
  6902. if(p_dists[i] == NULL){
  6903. p_dists[i] = dist;
  6904. is_anchor_changed = true;
  6905. b_exist = true;
  6906. break;
  6907. }
  6908. if(p_dists[i]->x == dist->x && p_dists[i]->y == dist->y
  6909. && p_dists[i]->z == dist->z && p_dists[i]->a == dist->a){ // 同一分站,新数据有效
  6910. dist->d_offset = p_dists[i]->d_offset; // 同一分站的偏移
  6911. if(dist->t >= p_dists[i]->t){
  6912. p_dists[i] = dist;
  6913. }else if(abs(dist->t - p_dists[i]->t) > 100){
  6914. p_dists[i] = dist;
  6915. }
  6916. b_exist = true;
  6917. break;
  6918. }
  6919. if(min_stamp >= p_dists[i]->t && abs(min_stamp-p_dists[i]->t) <100){
  6920. min_stamp = p_dists[i]->t;
  6921. min_stamp_index = i;
  6922. }else if(min_stamp < p_dists[i]->t && abs(min_stamp - p_dists[i]->t) >= 100){
  6923. min_stamp = p_dists[i]->t;
  6924. min_stamp_index = i;
  6925. }
  6926. }
  6927. if(!b_exist){
  6928. p_dists[min_stamp_index] = dist;
  6929. is_anchor_changed = true;
  6930. }
  6931. get_coordinate_old(get_effictive_dist_count_ex());
  6932. is_deal_by_algo = true;
  6933. if (this->x != this->last_x || this->y != this->last_y)
  6934. {
  6935. b_pos_change = true;
  6936. }
  6937. else
  6938. {
  6939. b_pos_change = false;
  6940. }
  6941. }
  6942. int Card::get_effictive_dist_count_ex()
  6943. {
  6944. int ret = 0, tmp = -1;
  6945. for(int i = 0; i < DIST_COUNT; i++){
  6946. if(p_dists[i] == NULL) break;
  6947. if(p_dists[i]->t == time_stamp_max){
  6948. ret++;
  6949. }
  6950. }
  6951. return ret;
  6952. }
  6953. LeaderArrange::LeaderArrange(string cardid)
  6954. {
  6955. card_id = cardid;
  6956. starttime = time(NULL);
  6957. endtime = time(NULL);
  6958. }
  6959. LeaderArrange::~ LeaderArrange()
  6960. {
  6961. }
  6962. VehicleTypeAttRule::VehicleTypeAttRule(int vtid, int pre, int back)
  6963. {
  6964. this->vehicle_type_id = vtid;
  6965. this->offset_pre = pre;
  6966. this->offset_back = back;
  6967. }
  6968. VehicleTypeAttRule::~VehicleTypeAttRule()
  6969. {
  6970. this->Clear();
  6971. }
  6972. void VehicleTypeAttRule::Clear()
  6973. {
  6974. this->vecArea.clear();
  6975. }
  6976. void VehicleTypeAttRule::AddArea(int area_id)
  6977. {
  6978. std::vector<int>::iterator itArea = std::find(this->vecArea.begin(), this->vecArea.end(), area_id);
  6979. if(itArea == this->vecArea.end())
  6980. {
  6981. this->vecArea.push_back(area_id);
  6982. }
  6983. }
  6984. const std::string& Card::cardId()
  6985. {
  6986. return card_id;
  6987. }
  6988. const uint64_t Card::CtTime()
  6989. {
  6990. return m_ct_time;
  6991. }
  6992. const int Card::getX()
  6993. {
  6994. return (int)CFunctions::round(this->x,0);
  6995. }
  6996. int Card::getAreaId()
  6997. {
  6998. return area_id;
  6999. }
  7000. int Card::getAcc()
  7001. {
  7002. return accelerate_state;
  7003. }
  7004. double Card::getv()
  7005. {
  7006. return final_v;
  7007. }
  7008. const int Card::getY()
  7009. {
  7010. return -(int)CFunctions::round(this->y,0);
  7011. }
  7012. void Card::do_alarm(const std::size_t ow)
  7013. {
  7014. //
  7015. }
  7016. const uint64_t Card::get_last_recv_time()
  7017. {
  7018. return rec_time;
  7019. }
  7020. void Card::PrintString(const std::string &&s)
  7021. {
  7022. debug_print_syslog(0,"[LemonHash Card%s]%s",card_id.c_str(),s.c_str());
  7023. }
  7024. int Card::getCardType()
  7025. {
  7026. return card_type;
  7027. }
  7028. bool Card::getBigCarFlag()
  7029. {
  7030. return m_bigCarFlag;
  7031. }
  7032. //人卡,掘进机,采煤机返回false,其他返回true
  7033. bool Card::smoothFlag()
  7034. {
  7035. return false;
  7036. if (card_type == 0x01 || VT_DIGGINGCOAL_MACHINE == vehice_type_id || VT_DRIVINGFACE_MACHINE == vehice_type_id)
  7037. {
  7038. return false;
  7039. }
  7040. return true;
  7041. }
  7042. //将定位坐标赋值给卡,以便卡执行业务逻辑
  7043. int Card::push_optimized_data(NAMESPACE_POINT::loc_point&lp)
  7044. {
  7045. //debug_print_syslog(0,"[lemon push data] %s,x:%f,y:%f,ct:%d",card_id.c_str(),lp.x,lp.y,lp.m_ct);
  7046. //this->x = lp.x;
  7047. //this->y = -lp.y;
  7048. //this->time_center_stamp = lp.m_ct;
  7049. ////this->final_v = 0;
  7050. ////this->state_moving = 0;
  7051. ////this->m_nStream = 1;
  7052. //this->origin_locate.x = this->x;
  7053. //this->origin_locate.y = this->y;
  7054. //不在猴车上的人需要如下判断;
  7055. //掘进机或采煤机需要如下判断;
  7056. if ((CT_PERSON == card_type && state != STATE_ACCE_GETON_MONKEYCAR) || ((VT_DIGGINGCOAL_MACHINE == vehice_type_id || VT_DRIVINGFACE_MACHINE == vehice_type_id)))
  7057. {
  7058. NAMESPACE_POINT::point p;
  7059. p.set(lp.x,-lp.y);
  7060. if (INVALID_COORDINATE != last_locate.x && INVALID_COORDINATE != last_locate.y)
  7061. {
  7062. double speed = 0.0, acc = 0.0, dt = 0.0,dist = 0.0;
  7063. dt = abs(lp.m_time - last_locate.cur_time)/1000.0;
  7064. NAMESPACE_POINT::loc_point llp;
  7065. llp.set(last_locate.x,last_locate.y);
  7066. NAMESPACE_POINT::loc_point cp;
  7067. cp.set(lp.x,-lp.y);
  7068. dist = cp.dist_direct(llp)*map_scale;
  7069. speed = abs(dist)/dt;
  7070. acc = abs(speed - last_locate.v)/dt;
  7071. lp.m_speed = speed;
  7072. switch (card_type)
  7073. {
  7074. case CT_PERSON:
  7075. if (acc > PERSON_ACCELERATE_THRESHOLD||speed*3.6 > MAX_PERSON_SPEED)
  7076. {
  7077. return 0;
  7078. }
  7079. break;
  7080. case CT_COALMINING_MACHINE:
  7081. case CT_TUNNELBORING_MACHINE:
  7082. if (acc > VECHILE_ACCELERATE_THRESHOLD||speed*3.6 > MAX_VECHILE_SPEED*0.06)
  7083. {
  7084. return 0;
  7085. }
  7086. break;
  7087. }
  7088. if (acc > PERSON_ACCELERATE_THRESHOLD||speed*3.6 > MAX_PERSON_SPEED)
  7089. {
  7090. debug_print_syslog(0,"[not push data] %s,x:%f,y:%f,ct:%d",card_id.c_str(),lp.x,lp.y,lp.m_ct);
  7091. return 0;
  7092. }
  7093. }
  7094. last_locate.x = lp.x;
  7095. last_locate.y = -lp.y;
  7096. last_locate.cur_time = lp.m_time;
  7097. last_locate.t = lp.m_ct;
  7098. last_locate.v = lp.m_speed;
  7099. }
  7100. this->x = lp.x;
  7101. this->y = -lp.y;
  7102. this->time_center_stamp = lp.m_ct;
  7103. //this->final_v = 0;
  7104. //this->state_moving = 0;
  7105. //this->m_nStream = 1;
  7106. this->origin_locate.x = this->x;
  7107. this->origin_locate.y = this->y;
  7108. debug_print_syslog(0,"[lemon push data] %s,x:%f,y:%f,ct:%d",card_id.c_str(),lp.x,lp.y,lp.m_ct);
  7109. return 0;
  7110. }
  7111. int Card::algo_tof_raw_1d(const DistMap& dm)
  7112. {
  7113. if (LOCATEDATATYPE::LDT_TOF == ranging_type)
  7114. {
  7115. if (dm.size() < 1)
  7116. {
  7117. return 1;
  7118. }
  7119. }
  7120. else
  7121. {
  7122. if (dm.size() < 2)
  7123. {
  7124. return 2;
  7125. }
  7126. }
  7127. is_del_data = true;
  7128. int nRet = 0;
  7129. //组装数据部分
  7130. map<unsigned long long, std::shared_ptr<_coordinate>> tmp_dists_locate;
  7131. int i = 0;
  7132. int nCount = 0;
  7133. bool bFound = false;
  7134. unordered_map<int,std::list<double>> check_dist_data;
  7135. for(DistMap::iterator it = distmap.begin(); it != distmap.end(); ++it){
  7136. bFound = false;
  7137. int tct = 0;
  7138. tct = it->second->t;
  7139. // 判断是否已经加入到列表
  7140. for(int j = 0; j < i; j++){
  7141. if(tmp_dists_locate.find(it->second->reader_id) != tmp_dists_locate.end()){
  7142. //已存在,则退出查找for循环
  7143. bFound = true;
  7144. break;
  7145. }
  7146. }
  7147. if(i < DIST_COUNT && !bFound){
  7148. if (abs(it->second->d + 1.0) < ZERO_PRECISION)
  7149. {
  7150. //即计算的距离为-1,抛弃此数据
  7151. continue;
  7152. }else{
  7153. tmp_dists_locate.insert(make_pair(i,(it->second)));
  7154. check_dist_data[it->second->reader_id].push_back(it->second->d);
  7155. }
  7156. i++;
  7157. }
  7158. }
  7159. map<unsigned long long, std::shared_ptr<_coordinate>>::iterator it = tmp_dists_locate.begin();
  7160. time_stamp_cal = it->second->t;
  7161. double dMaxDistance = 1000.0;
  7162. //修正由于高度所引入的误差
  7163. for (map<unsigned long long, std::shared_ptr<_coordinate>>::iterator it_tmp = tmp_dists_locate.begin();it_tmp != tmp_dists_locate.end();++it_tmp)
  7164. {
  7165. double distance = 0.0;
  7166. #ifdef _DEBUG
  7167. distance = sqrt(pow(it_tmp->second->d,2) - pow(TOF_HEIGHT_ERROR,2));//TOF_HEIGHT_ERROR
  7168. #else
  7169. distance = sqrt(pow(it_tmp->second->d,2) - pow(TOF_HEIGHT_ERROR,2));
  7170. #endif
  7171. it_tmp->second->d = distance;
  7172. if (distance < dMaxDistance)
  7173. {
  7174. dMaxDistance = distance;
  7175. }
  7176. }
  7177. // nRet = m_position_optimizer->push_raw_data(tmp_dists_locate);
  7178. //std::vector<std::shared_ptr<_coordinate>> vt_pos;
  7179. //vt_pos.resize(0);
  7180. //vt_pos = calc_locate_tof_raw_1d(tmp_dists_locate);
  7181. //i = 0;
  7182. //std::string log = "";
  7183. //char chlog[MAX_LOG_LENGTH] = {0};
  7184. //sprintf_s(chlog,"ct:%d , ",time_stamp_cal);
  7185. //log+=chlog;
  7186. //for (std::vector<std::shared_ptr<_coordinate>>::iterator it_pos = vt_pos.begin(); it_pos != vt_pos.end();++it_pos )
  7187. //{
  7188. // memset(chlog,0,MAX_LOG_LENGTH*sizeof(char));
  7189. // sprintf_s(chlog,"x%d: %.3f ,y%d: %.3f ,",i,(*it_pos)->x,i,(*it_pos)->y);
  7190. // log += chlog;
  7191. // i++;
  7192. //}
  7193. //Log::write_log(FILE_TYPE::SYS_S,log,true);
  7194. return 0;
  7195. }
  7196. const std::vector<std::shared_ptr<_coordinate>> Card::calc_locate_tof_raw_1d(map<unsigned long long, std::shared_ptr<_coordinate>> p_dists_locate) const
  7197. {
  7198. std::vector<std::shared_ptr<_coordinate>> vt_pos;
  7199. for (map<unsigned long long, std::shared_ptr<_coordinate>>::iterator it = p_dists_locate.begin();it != p_dists_locate.end();++it)
  7200. {
  7201. if(pTofReaderPathMap->find(it->second->reader_id) == pTofReaderPathMap->end())
  7202. {
  7203. continue;
  7204. }
  7205. int seg_num = 0;
  7206. seg_num = pTofReaderPathMap->find(reader_id)->second->nRealCalcPoints - 1;
  7207. //判断路径集的点数是否大于两个点,两点才可以构成线段路径
  7208. if(seg_num < 0 ){
  7209. continue;
  7210. }
  7211. double d[MAX_READER_TDOA_PATH_NUMS] = {0};
  7212. double xcross[MAX_READER_TDOA_PATH_NUMS] = {0};
  7213. double ycross[MAX_READER_TDOA_PATH_NUMS] = {0};
  7214. int j = 0;
  7215. int m = 0;
  7216. int nCounts = 0;
  7217. //对每一个线段所在的直线和以天线为圆心的圆求解,最多为两个解
  7218. for(int i = 0;i < seg_num;i++){
  7219. //如果线段两端点的坐标不存在,则寻找下一个线段
  7220. if(pTofReaderPathMap->find(reader_id)->second->px[i] == -1 || pTofReaderPathMap->find(reader_id)->second->px[i+1] == -1)
  7221. {
  7222. continue;
  7223. }
  7224. //求线段所在直线和分站readerid的antenna_idx天线为圆心,半径为dist的圆的两个交点
  7225. std::shared_ptr<SOLUTION> r = LocateAlgorithm::GetPos(pTofReaderPathMap,it->second->reader_id,it->second->antenna_id,it->second->d,i);
  7226. //如果解不存在,则继续寻找下一个线段
  7227. if(r == nullptr || r->nCount <= 0){
  7228. continue;
  7229. }
  7230. //如果解存在,则保存两个解
  7231. for(j = 0;j < r->nCount;j++){
  7232. std::shared_ptr<_coordinate> p = std::make_shared<_coordinate>();
  7233. p->x = r->x[j];
  7234. p->y = r->y[j];
  7235. p->reader_id = it->second->reader_id;
  7236. p->antenna_id = it->second->antenna_id;
  7237. p->d = it->second->d;
  7238. p->t = it->second->t;
  7239. vt_pos.push_back(p);
  7240. }
  7241. }
  7242. }
  7243. return std::move(vt_pos);
  7244. }
  7245. int Card::get_antenna_xyz(int reader_id, int antenna_id,double& x,double& y, double& z)
  7246. {
  7247. x = pTofReaderPathMap->find(reader_id)->second->x[antenna_id];
  7248. y = pTofReaderPathMap->find(reader_id)->second->y[antenna_id];
  7249. z = pTofReaderPathMap->find(reader_id)->second->z[antenna_id];
  7250. return 0;
  7251. }
  7252. int Card::get_key(double x, double y, double& key)
  7253. {
  7254. double distance = 999999999.0;
  7255. double offset = 0.0;
  7256. double reader_x1 = 0.0;
  7257. double reader_y1 = 0.0;
  7258. double reader_x2 = 0.0;
  7259. double reader_y2 = 0.0;
  7260. for(auto it = pTofReaderPathMap->begin(); it != pTofReaderPathMap->end(); it++)
  7261. {
  7262. double x_temp = 0.0;
  7263. double y_temp = 0.0;
  7264. x_temp = (*it).second->x[0];
  7265. y_temp = (*it).second->y[0];
  7266. offset = sqrt(std::pow(x_temp - x, 2) + std::pow(y_temp - y, 2));
  7267. if(offset < distance)
  7268. {
  7269. distance = offset;
  7270. reader_x2 = reader_x1;
  7271. reader_y2 = reader_y1;
  7272. reader_x1 = x_temp;
  7273. reader_y1 = y_temp;
  7274. }
  7275. }
  7276. std::stringstream _ss_log;
  7277. _ss_log << "++++++++++print_key,card:" << card_id
  7278. <<", reader_x2:"<<reader_x2
  7279. <<", reader_y2:"<<reader_y2
  7280. <<", reader_x1:"<<reader_x1
  7281. <<", reader_y1:"<<reader_y1
  7282. <<", "<<__LINE__<<", "<<__FUNCTION__;
  7283. //Log::write_log(FILE_TYPE::POSITION_ORIGIN, _ss_log.str(), true);
  7284. key = abs((reader_y2 - reader_y1) / (reader_x2 - reader_x1));
  7285. return 0;
  7286. }
  7287. /*
  7288. * 参数p用于选解,得出的解需要和此解距离远的
  7289. */
  7290. void Card::calc_tunnel_boring_machine_pos(const op::point& p)
  7291. {
  7292. op::point loc_reader_p(p_reader->ant[antenna_id]->antenna_x*map_scale,p_reader->ant[antenna_id]->antenna_y*map_scale);
  7293. op::point line[2];
  7294. auto it = pTofReaderPathMap->find(reader_id);
  7295. if (it!= pTofReaderPathMap->end())
  7296. {
  7297. for (size_t i=0;i<2;++i)
  7298. {
  7299. line[i].set(it->second->px[i],it->second->py[i]);
  7300. }
  7301. }
  7302. std::vector<op::point> vt_pos_list = LocateAlgorithm::calc_pos(loc_reader_p,distance,line);
  7303. op::point pos;
  7304. if (vt_pos_list.size()>=2)
  7305. {
  7306. double t_distance = 0.0;
  7307. size_t index = 0;
  7308. for (size_t i = 0;i<vt_pos_list.size();++i)
  7309. {
  7310. if (_isnan(vt_pos_list[i].x)||_isnan(vt_pos_list[i].y))
  7311. {
  7312. continue;
  7313. }
  7314. double ttd = sqrt(pow(vt_pos_list[i].x - p.x,2)+pow(vt_pos_list[i].y - p.y,2));
  7315. if (ttd > t_distance)
  7316. {
  7317. t_distance = ttd;
  7318. index = i;
  7319. }
  7320. }
  7321. pos = vt_pos_list[index];
  7322. }
  7323. if (CT_SIMULATE_CARD != card_type)
  7324. {
  7325. double speed = 0.0;
  7326. if (INVALID_COORDINATE != last_locate.x && INVALID_COORDINATE != last_locate.y)
  7327. {
  7328. double acc = 0.0, dt = 0.0,dist = 0.0;
  7329. dt = abs(rec_time - last_locate.cur_time);
  7330. NAMESPACE_POINT::loc_point llp;
  7331. llp.set(last_locate.x,last_locate.y);
  7332. NAMESPACE_POINT::loc_point cp;
  7333. cp.set(pos.x,pos.y);
  7334. dist = cp.dist_direct(llp)*map_scale;
  7335. speed = abs(dist)/dt;
  7336. acc = abs(speed - last_locate.v)/dt;
  7337. switch (card_type)
  7338. {
  7339. case CT_TUNNELBORING_MACHINE:
  7340. if (acc > 0.01||speed*3.6 > 1)
  7341. {
  7342. return;
  7343. }
  7344. break;
  7345. }
  7346. if (acc > 0.01||speed*3.6 > MAX_PERSON_SPEED)
  7347. {
  7348. debug_print_syslog(0,"[Drivingface machine] %s,x:%f,y:%f,ct:%d",card_id.c_str(),pos.x,pos.y,sync_num);
  7349. return;
  7350. }
  7351. }
  7352. last_locate.v = speed;
  7353. }
  7354. this->x = pos.x/map_scale;
  7355. this->y = pos.y/map_scale;
  7356. this->origin_locate.x = this->x;
  7357. this->origin_locate.y = this->y;
  7358. last_locate.x = pos.x;
  7359. last_locate.y = pos.y;
  7360. last_locate.cur_time = rec_time;
  7361. last_locate.t = sync_num;
  7362. debug_print_syslog(0,"[three rates: Drivingface machine] %s,x:%f,y:%f,ct:%d",card_id.c_str(),pos.x,pos.y,sync_num);
  7363. #ifdef DEBUG
  7364. char log[MSG_LENGTH_MAX];
  7365. memset(log,0,MSG_LENGTH_MAX*sizeof(char));
  7366. sprintf_s(log,"card_id: %s, ct: %d, x: %.3f, y: %.3f",card_id.c_str(),this->t,this->x,this->y);
  7367. Log::write_log(FILE_TYPE::LOCATE_TOF_S,log,true);
  7368. #endif
  7369. }