classdef.cpp 146 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723572457255726572757285729573057315732573357345735573657375738573957405741574257435744574557465747574857495750575157525753575457555756575757585759576057615762576357645765576657675768576957705771577257735774577557765777577857795780578157825783578457855786578757885789579057915792579357945795579657975798579958005801580258035804580558065807580858095810581158125813581458155816581758185819582058215822582358245825582658275828582958305831583258335834583558365837583858395840584158425843584458455846584758485849585058515852585358545855585658575858585958605861586258635864586558665867586858695870587158725873587458755876587758785879588058815882588358845885588658875888588958905891589258935894589558965897589858995900590159025903590459055906590759085909591059115912591359145915591659175918591959205921592259235924592559265927592859295930593159325933593459355936593759385939594059415942594359445945594659475948594959505951595259535954595559565957595859595960596159625963596459655966596759685969597059715972597359745975597659775978597959805981598259835984598559865987598859895990599159925993599459955996599759985999600060016002600360046005600660076008600960106011601260136014601560166017601860196020602160226023602460256026602760286029603060316032603360346035603660376038603960406041604260436044604560466047604860496050605160526053605460556056605760586059606060616062606360646065606660676068606960706071607260736074607560766077607860796080608160826083608460856086608760886089609060916092609360946095609660976098609961006101610261036104610561066107610861096110611161126113611461156116611761186119612061216122612361246125612661276128612961306131613261336134613561366137613861396140614161426143614461456146614761486149615061516152615361546155615661576158615961606161616261636164616561666167
  1. #include "stdafx.h"
  2. #include "classdef.h"
  3. #include "constdef.h"
  4. #include "structdef.h"
  5. #include "locate_algorithm.h"
  6. #include "./system_basic_info/SystemAnalysis.h"
  7. #include "ProcessRemodule.h"
  8. #include "log_process_module.h"
  9. #include "Functions/Functions.h"
  10. #include "algorithm/Fit.h"
  11. #include "./mylog/log_module.h"
  12. #include <math.h>
  13. #pragma warning(disable: 4244)
  14. unsigned int g_nAlgoFailedCounts[ALGO_LOC_TOTAL] = {0};
  15. unsigned int g_nAlgoFailedCycleCounts[ALGO_LOC_TOTAL] = {0};
  16. ULONGLONG g_ullCurTime = 0;
  17. Card::Card( string cardid, int cardtype, double z_offset, double offset_x /*= 12.0*/, double offset_y /*=12.0*/ )
  18. {
  19. InitializeCriticalSectionAndSpinCount(&m_csCard, MAXCRITICALSECTIONSPINCOUNT);
  20. card_type = cardtype;
  21. card_id = cardid;
  22. coor_offset_x = offset_x;
  23. coor_offset_y = offset_y;
  24. this->z_offset = z_offset;
  25. ::GetLocalTime(&deal_time);
  26. ::GetLocalTime(&enter_area_time);
  27. down_time = up_time = enter_reader_time = rec_time = time(NULL);
  28. time_over_time = time_area_over_time = time_area_forbidden = time_over_speed = time_low_power = time(NULL);
  29. last_locate_time = time(NULL);
  30. //坐标初始值为0
  31. //x = y = z = stored_x = stored_y = stored_z = 0;
  32. x = y = z = stored_x = stored_y = stored_z = INVALID_COORDINATE;
  33. output_x = output_y = 0;
  34. xx = yy = zz = 0;
  35. x1 = y1 = z1 = x2 = y2 = z2 = x3 = y3 = z3 = x4 = y4 = z4 = 0;
  36. a = 0;
  37. t = 0;
  38. init_postion = false;
  39. is_first_location = true;
  40. is_driving_face_start = 0;
  41. right_x = right_y = right_z = left_x = left_y = left_z = 0;
  42. m_nMoveDirection = 0;
  43. map_id = map_id_old = area_id = reader_id = 0;
  44. map_scale = 1.0;
  45. state = 0;
  46. state_moving = 0;
  47. state_biz = 0;
  48. pos_state = pos_state_old = PDT_INIT;
  49. pos_state_count = 0;
  50. pos_state_confirm_times = 1;
  51. dept_id = group_id = occlevel_id = 0;
  52. power_state = power_state_last = 0;
  53. status_help = status_area_over_time = status_area_forbidden = status_area_over_speed = 0;
  54. status_over_speed = status_over_time = status_power = status_lost = status_call = 0;
  55. sync_num = 0;
  56. isdealed = isreceive = is_pos_changed = is_hist = is_need_cal = false;
  57. is_area_over_time = is_mine_over_time = false;
  58. id = name = number = department = group = worktype = "";
  59. driver_id = "";
  60. reader_tickcount = time_stamp = time_stamp_last = 0;
  61. p_dists_locate = new _coordinate*[DIST_COUNT];
  62. time_stamp_max = 0;
  63. for(int i = 0; i < DIST_COUNT; i++){
  64. p_dists_locate[i] = NULL;
  65. }
  66. is_deal_by_algo = false;
  67. pReaderPathMap = NULL;
  68. pTdoaReaderPathMap = NULL;
  69. last_s_locate_reader[0] = last_s_locate_reader[1] = -1;
  70. m_syncNumList.clear();
  71. m_dFirstDiff = m_dSecondDiff = 0.0;
  72. m_nCalcSyncNum = 0;
  73. m_nSyncNumInList = 0;
  74. last_locate.tt = 0;
  75. last_locate.a = last_locate.antenna_id = last_locate.reader_id = last_locate.t = 0;
  76. last_locate.d = last_locate.d_offset = last_locate.v = 0.0;
  77. last_locate.x = last_locate.y = last_locate.z = INVALID_COORDINATE;
  78. last_x = last_y = last_z = INVALID_COORDINATE;
  79. locate = nullptr;
  80. m_bUseFilter = false;
  81. m_nFilterType = NO_FILTER;
  82. m_pKalmanFilter = nullptr;
  83. m_nLastLocateT = 0;
  84. origin_locate.x = origin_locate.y = origin_locate.z = origin_locate.v = 0.0;
  85. last_vx = last_vy = 0.0;
  86. v = 0.0;
  87. ins_weight = INS_WEIGHT;
  88. uwb_weight = UWB_WEIGHT;
  89. acce_cur_state = 0;
  90. acce_last_state = 0;
  91. b_long_interval = false;
  92. accelerate_state = accelerate_state_last = ACCELERATE_INIT_STATE; //默认初始的为-10
  93. ins_direction = 0;
  94. direction = 0;
  95. antenna_angle = antenna_angle_last = 0;
  96. time_stamp_last = 0;
  97. reader_id_last = 0;
  98. m_nLightGroupId = -1; //默认为没控制灯组
  99. b_enter_intersection = false;
  100. is_red_light = false;
  101. light_id = 0;
  102. time_red_light = time(NULL);
  103. m_nOutputPosState = 0;
  104. m_nIsRailroad = 0;
  105. mileage = 0;
  106. diff_direction_counts = 0;
  107. m_nStream = 0;
  108. for(int i = 0; i < CARD_EVENT_COUNT; i++){
  109. m_event_list[i] = 0;
  110. }
  111. p_reader = nullptr;
  112. _dists.swap(DistQueMap());
  113. vt_his_speed.resize(0);
  114. his_pos.resize(0);
  115. is_ref_pos = false;
  116. is_fit_pos = false;
  117. cur_fit_nums = 0;
  118. have_fit_pos = false;
  119. have_long_fit_pos = false;
  120. count_idle = 0;
  121. count_change_direction = 0;
  122. is_over_interval = false;
  123. bInitalCellPath = false;
  124. curCellReaderName = "";
  125. curCellId = 0;
  126. originCellId = 0;
  127. nIncrease = 0;
  128. change_cell_dir = 0;
  129. nStartLocateCounts = 0;
  130. call_type = 0;
  131. vt_deal_call_reader_id.resize(0);
  132. recv_call_time = time(NULL);
  133. landmark_id = 0;
  134. landmark_dis = 0;
  135. landmark_direction = NODIRECTORY;
  136. <<<<<<< HEAD
  137. isIdling = false;
  138. data_cell_time = 0;
  139. calc_cell_time = 0;
  140. last_cell = nullptr;
  141. ::GetLocalTime(&cellDealTime);
  142. //hEvent = CreateEvent(NULL,TRUE,FALSE,NULL);
  143. //ResetEvent(hEvent);
  144. =======
  145. drive_face_location_write_time = 0;
  146. >>>>>>> newprotocal
  147. }
  148. Card::Card( void )
  149. {
  150. }
  151. void Card::reset()
  152. {
  153. //以下参数与算法相关
  154. //坐标初始值为0
  155. //TRACE(_T("reset card info \r\n"));
  156. m_nLastLocateT = 0;
  157. time_stamp_last = 0;
  158. b_long_interval = false;
  159. is_first_location = true;
  160. last_locate.sync_num = 0;
  161. //while (_dists.size()>0)
  162. //{
  163. // _dists.pop_front();
  164. //}
  165. if (m_syncNumList.size() > 0)
  166. {
  167. m_syncNumList.erase(m_syncNumList.begin(),m_syncNumList.end());
  168. m_syncNumList.resize(0);
  169. }
  170. if (vt_his_speed.size()>0)
  171. {
  172. vt_his_speed.erase(vt_his_speed.begin(),vt_his_speed.end());
  173. vt_his_speed.resize(0);
  174. }
  175. if (his_pos.size() > 0)
  176. {
  177. his_pos.erase(his_pos.begin(),his_pos.end());
  178. his_pos.resize(0);
  179. have_fit_pos = false;
  180. }
  181. if (long_his_pos.size() > 0)
  182. {
  183. long_his_pos.erase(long_his_pos.begin(),long_his_pos.end());
  184. long_his_pos.resize(0);
  185. have_long_fit_pos = false;
  186. count_idle = 0;
  187. }
  188. cur_fit_nums = 0;
  189. count_idle = 0;
  190. locate = nullptr;
  191. //卡尔曼参数重置
  192. if (m_pKalmanFilter!=nullptr)
  193. {
  194. m_pKalmanFilter->m_nCounts = 0;
  195. m_pKalmanFilter->Initial(0.2);
  196. }
  197. //清除历史格子列表
  198. if (his_cell.size() > 0)
  199. {
  200. his_cell.erase(his_cell.begin(),his_cell.end());
  201. }
  202. }
  203. void Card::set_reader(std::shared_ptr<Reader> preader) // 设置卡时间
  204. {
  205. this->rec_time = time(NULL);
  206. if(preader->reader_id != this->reader_id){ // 所在分站没有发生变化
  207. this->p_reader = preader.get();
  208. this->reader_id = preader->reader_id;
  209. this->enter_reader_time = this->rec_time;
  210. this->map_id_old = this->map_id;
  211. this->map_id = preader->map_id;
  212. this->map_scale = preader->map_scale;
  213. }
  214. if(this->pos_state == this->p_reader->pos_state){
  215. if(this->pos_state_count <= pos_state_confirm_times)
  216. {
  217. this->pos_state_count++;
  218. }
  219. }
  220. else // 井上井下位置变化
  221. {
  222. this->pos_state = p_reader->pos_state;
  223. this->pos_state_count = 0;
  224. }
  225. }
  226. /*
  227. * 设置地图集覆盖范围,适用TOF
  228. *
  229. */
  230. void Card::set_reader_path_tof(std::shared_ptr<ReaderPathMap> rpm)
  231. {
  232. this->pReaderPathMap = rpm;
  233. }
  234. bool Card::checkStream(double x1,double y1,double x2,double y2,int nStream)
  235. {
  236. //方向一致,返回true,否则返回false
  237. //去除采集刚启动,last_locate的坐标为零而导致判断车辆上下行方向错误的问题
  238. //车辆上下行确定
  239. //3个条件:起点(x1,y1),终点(x2,y2)
  240. //1.x1==x2的情况下,y2>y1为下行
  241. //2.y1==y2的情况下,x1>x2为下行
  242. //3.x2>x1且y2>y1为下行
  243. //其他情况为上行
  244. int stream = 0;
  245. if (((x1 == x2 || abs(x1 - x2) < 1E-2) && y1 < y2 )
  246. ||((y1 == y2 || abs(y1 - y2) < 1E-2) && x1 > x2)
  247. ||(x1 < x2 && y1 < y2))
  248. {
  249. stream = DOWN_STREAM;
  250. }
  251. else
  252. {
  253. stream = UP_STREAM;
  254. }
  255. return nStream == stream;
  256. }
  257. /*
  258. * 设置地图集覆盖范围,适用TDOA
  259. *
  260. */
  261. void Card::set_reader_path_tdoa(std::shared_ptr<TDOAReaderPathMap> trpm)
  262. {
  263. this->pTdoaReaderPathMap = trpm;
  264. }
  265. bool operator==(sync_data& a,sync_data&b){
  266. return a.sync_num == b.sync_num;
  267. }
  268. /*
  269. * 此函数主要判断此坐标是否需要形成json输出给webserver
  270. *
  271. * param
  272. * acce_state 算法中保存的当前加速度计状态
  273. * acce_state_last 算法中保存的上一次加速度计状态
  274. *
  275. * return
  276. * 无
  277. */
  278. void Card::inspect_coordinate(int acce_state)
  279. {
  280. this->isoutput = false;
  281. char log[200] = {0};
  282. //如果是静止或者怠速状态,每次的定位坐标不变化,此时要求采集不再将此数据送往webserver//人卡02
  283. if (acce_state == STATE_ACCE_STATIC)
  284. {
  285. this->isoutput = false;
  286. }
  287. else
  288. {
  289. if (this->x == this->output_x && this->y == this->output_y) //last_x,last_y
  290. {
  291. count_idle++;
  292. this->isoutput = false;
  293. }else{
  294. //如果运动状态时,
  295. int nSign = 0;
  296. int nState = 0;
  297. if (checkStream(this->output_x,this->output_y,this->x,this->y,this->m_nStream))
  298. {
  299. double distance = sqrt(pow(this->x - this->output_x,2) + pow(this->y - this->output_y,2)) * this->map_scale;
  300. if(distance > this->map_scale) //this->map_scale)
  301. {
  302. //两点之间的距离是否大于一个0.5米,满足条件,输出json
  303. this->isoutput = true;
  304. this->output_x = this->x;
  305. this->output_y = this->y;
  306. count_idle = 0;
  307. }else{
  308. count_idle++;
  309. }
  310. }
  311. else
  312. {
  313. if( count_change_direction > 3){
  314. // 认为掉头,输出给web
  315. this->m_nMoveDirection = nSign;
  316. this->isoutput = true;
  317. this->output_x = this->x;
  318. this->output_y = this->y;
  319. count_idle = 0;
  320. count_change_direction = 0;
  321. }else{
  322. // 认为是抖动,不输出给web
  323. count_change_direction++;
  324. count_idle++;
  325. }
  326. }
  327. }
  328. }
  329. }
  330. /*
  331. * TOF定位算法
  332. *
  333. * param
  334. * cnt 数据条数
  335. *
  336. * return
  337. * 无
  338. *
  339. */
  340. void Card::algo_tof(int cnt)
  341. {
  342. }
  343. /*
  344. * TDOA算法
  345. *
  346. * param
  347. * cnt 数据条数
  348. *
  349. * return
  350. * 无
  351. *
  352. */
  353. void Card::algo_tdoa(int cnt)
  354. {
  355. LOCATION_SYSTEM_BRANCH(LOCATION_SYSTEM_BRANCH_140);
  356. GetLocalTime(&m_afmData.st);
  357. if(cnt < 2){
  358. LOCATION_SYSTEM_BRANCH(LOCATION_SYSTEM_BRANCH_141);
  359. m_afmData.bStatus = true;
  360. m_afmData.strCardId = this->card_id;
  361. m_afmData.nType = ALGO_FAILED_CONDITION_15;
  362. ALGORITHM_FAILED(ALGO_FAILED_CONDITION_15);
  363. return;
  364. }
  365. //主要处理当相同卡的时间戳的数据中存在同步序号大于5的情况,如果有大于5的数据则丢弃此数据
  366. int k = 0;
  367. unsigned short dst = 0;
  368. unsigned short st = 0;
  369. bool bRet = false;
  370. mp_dists_locate.clear();
  371. // 多个基站的时间同步序号相差大于阈值,不参与计算
  372. for(DistMap::iterator it = _dists.front().distmap.begin(); it != _dists.front().distmap.end(); ++it,k++){
  373. if(k==0){
  374. st = it->second->sync_num;
  375. }else{
  376. dst = (st - (unsigned short)it->second->sync_num);
  377. if ((st > (unsigned short)it->second->sync_num && dst > MAX_SYNCTIME_DIFF_NUM) ||
  378. (st < (unsigned short)it->second->sync_num && dst < MAX_SYNCTIME_DIFF_NUM_CROSS_PERIOD))
  379. {
  380. LOCATION_SYSTEM_BRANCH(LOCATION_SYSTEM_BRANCH_142);
  381. bRet = true;
  382. //此处需要输出计数
  383. break;
  384. }
  385. }
  386. mp_dists_locate.insert(make_pair(it->second->tt, it->second));
  387. }
  388. if(bRet){
  389. LOCATION_SYSTEM_BRANCH(LOCATION_SYSTEM_BRANCH_143);
  390. this->x = this->last_locate.x;
  391. this->y = this->last_locate.y;
  392. this->z = 0;
  393. m_afmData.bStatus = true;
  394. m_afmData.strCardId = this->card_id;
  395. m_afmData.nType = ALGO_FAILED_CONDITION_2;
  396. ALGORITHM_FAILED(ALGO_FAILED_CONDITION_2);
  397. return;
  398. }
  399. std::shared_ptr<ReceiveDataMap> pRdm = std::make_shared<ReceiveDataMap>();
  400. pRdm->clear();
  401. bRet = false;
  402. int maxSyncTimes = 0;
  403. //保存加速度当前状态和上次状态
  404. int acce_state = 0;
  405. int acce_state_last = 0;
  406. int ins_direction = 0;
  407. int card_time_stamp = 0;
  408. map<unsigned long long,std::shared_ptr<_coordinate>>::iterator it_mpdl = mp_dists_locate.begin();
  409. int i = 0;
  410. for(;it_mpdl!=mp_dists_locate.end();++it_mpdl){
  411. if(i==0){
  412. card_time_stamp = it_mpdl->second->t;
  413. maxSyncTimes = it_mpdl->second->sync_num;
  414. acce_state = it_mpdl->second->acce_state;
  415. acce_state_last = it_mpdl->second->acce_state_last;
  416. ins_direction = it_mpdl->second->ins_direction;
  417. }
  418. else{
  419. if(maxSyncTimes < it_mpdl->second->sync_num){
  420. maxSyncTimes = it_mpdl->second->sync_num;
  421. acce_state = it_mpdl->second->acce_state;
  422. acce_state_last = it_mpdl->second->acce_state_last;
  423. ins_direction = it_mpdl->second->ins_direction;
  424. }
  425. }
  426. ReceiveDataMap::iterator prdm_it = pRdm->find(it_mpdl->second->tt);
  427. if(prdm_it == pRdm->end()){
  428. if(it_mpdl->second->tt == LLONG_MAX ){
  429. //如果同步时间戳存在异常值,则不走算法定位,直接返回上一次结果值
  430. bRet = true;
  431. //此处需要输出计数
  432. LOCATION_SYSTEM_BRANCH(LOCATION_SYSTEM_BRANCH_144);
  433. break; // 存在一个异常值就退出算法
  434. // continue; // 将异常值过滤掉,不参与算法,其他正常值照常参与计算
  435. }
  436. //保存信息用于定位
  437. std::shared_ptr<ReceiveData> prd = std::make_shared<ReceiveData>();
  438. prd->reader_id = it_mpdl->second->reader_id;
  439. prd->antenna_id = it_mpdl->second->antenna_id;
  440. prd->rec_time_stamp = it_mpdl->second->tt;
  441. prd->x = it_mpdl->second->x*this->map_scale;
  442. prd->y = it_mpdl->second->y*this->map_scale;
  443. prd->z = it_mpdl->second->z*this->map_scale;
  444. prd->special = it_mpdl->second->special;
  445. if (prd->rec_time_stamp > 0)
  446. {
  447. pRdm->insert(make_pair(prd->rec_time_stamp,prd));
  448. }
  449. }
  450. i++;
  451. }
  452. // if(pRdm.size() < 2)
  453. // {
  454. // bRet = true; // 筛选后,数据不满足定位要求,退出算法
  455. // }
  456. //存在异常值,直接返回上一次结果值
  457. if(bRet){
  458. LOCATION_SYSTEM_BRANCH(LOCATION_SYSTEM_BRANCH_145);
  459. pRdm->clear();
  460. this->x = this->last_locate.x;
  461. this->y = this->last_locate.y;
  462. this->z = 0;
  463. m_afmData.bStatus = true;
  464. m_afmData.strCardId = this->card_id;
  465. m_afmData.nCardStamp = it_mpdl->first;
  466. m_afmData.nType = ALGO_FAILED_CONDITION_3;
  467. ALGORITHM_FAILED(ALGO_FAILED_CONDITION_3);
  468. return;
  469. }
  470. //time_stamp_last特指卡的计数序号,非分站的
  471. //当卡需要新定位的计数序列号小于上一次成功定位的序列号,则此次不定位,避免定位结果的回退
  472. // 本次序号小于上次序号,存在如下情况
  473. // 1、跨周期,如本次0,上次为65535,另外进入盲区也可能跨周期后回来
  474. // 2、卡重置,从0开始
  475. // 3、序号较连续,新数据先到,旧数据后到,会造成跳动
  476. if (this->time_stamp_last != 0 && this->time_stamp_last > this->time_stamp_cal)
  477. {
  478. if((unsigned short)this->time_stamp_last - (unsigned short)this->time_stamp_cal < MAX_SYNCTIME_DELAY_NUM)
  479. {
  480. // 小于5,认为会影响数据
  481. pRdm->clear();
  482. this->x = this->last_locate.x;
  483. this->y = this->last_locate.y;
  484. this->z = 0;
  485. m_afmData.bStatus = true;
  486. m_afmData.strCardId = this->card_id;
  487. m_afmData.nCardStamp = this->time_stamp_cal;
  488. m_afmData.nType = ALGO_FAILED_CONDITION_4;
  489. ALGORITHM_FAILED(ALGO_FAILED_CONDITION_4);
  490. //此处需要输出计数
  491. return;
  492. }
  493. }
  494. this->time_stamp_last = this->time_stamp_cal;
  495. int nCount = 0;
  496. std::unique_ptr<POS> p = nullptr;
  497. std::unique_ptr<POS> p2 = nullptr;
  498. bool bOutput = false;
  499. this->z = 0;
  500. i = 0;
  501. if(pRdm->size() > 1){
  502. LOCATION_SYSTEM_BRANCH(LOCATION_SYSTEM_BRANCH_147);
  503. this->m_nCalcSyncNum = maxSyncTimes;
  504. //p = LocateAlgorithm::Pos(pRdm, pTdoaReaderPathMap);
  505. //算法逻辑修改:
  506. //1.当开始两分站之间有地图集时,走原来的算法逻辑
  507. //2.当开始两分站之间无地图集时,走两两遍历的算法逻辑
  508. ReceiveDataMap::iterator first = pRdm->begin();
  509. ReceiveDataMap::iterator second = first;
  510. //偏移到第二个元素
  511. std::advance(second,1);
  512. bool bFind = false;
  513. bool bExitPath = false;
  514. //如果两级都能找到才运行继续后续操作,否则,表明没有此路径地图集
  515. TDOAReaderPathMap::iterator rdm_it = pTdoaReaderPathMap->find(first->second->reader_id);
  516. if(rdm_it != pTdoaReaderPathMap->end()){
  517. //表示地图集中存在第一个分站的路径集
  518. bFind = true;
  519. }
  520. if (bFind)
  521. {
  522. //确认第一个和第二个分站之间是否有地图集
  523. ReaderPathMap::iterator rpm_it = pTdoaReaderPathMap->find(first->second->reader_id)->second->find(second->second->reader_id);
  524. if(rpm_it == pTdoaReaderPathMap->find(first->second->reader_id)->second->end()){
  525. bExitPath = false;
  526. }else{
  527. bExitPath = true;
  528. }
  529. }
  530. if (bExitPath)
  531. {
  532. //以第一个基站作为基准分站进行坐标计算
  533. p = LocateAlgorithm::Pos(pRdm, pTdoaReaderPathMap);
  534. }else{
  535. //pRdm内的数据双重遍历进行坐标计算
  536. p = LocateAlgorithm::LocatePos(pRdm, pTdoaReaderPathMap);
  537. if(fabs(p->posx) < 1E-4 && fabs(p->posy) < 1E-4 ){
  538. p->posx = INVALID_COORDINATE;
  539. p->posy = INVALID_COORDINATE;
  540. p->posz = INVALID_COORDINATE;
  541. }
  542. }
  543. //std::shared_ptr<POS> p3 = LocateAlgorithm::Pos(pRdm);
  544. //p = LocateAlgorithm::Pos(pRdm);
  545. if (p==nullptr)
  546. {
  547. //p = std::make_shared<POS>();
  548. //p->posx = INVALID_COORDINATE;
  549. //p->posy = INVALID_COORDINATE;
  550. //p->posz = INVALID_COORDINATE;
  551. }else{
  552. //debug_print_syslog(0, "CalcCardPosition cardid position by 3D, cx: %f, cy: %f, cz: %f",
  553. // p->posx, p->posy, p->posz);
  554. }
  555. this->origin_locate.x = p->posx / (this->map_scale*1.0);
  556. this->origin_locate.y = p->posy / (this->map_scale*1.0);
  557. this->origin_locate.z = p->posz / (this->map_scale*1.0);
  558. double interval_time = 0.2; // 当ReadFileTest时,没有set_reader,给默认值
  559. if(this->p_reader){
  560. interval_time = this->p_reader->reader_interval_time;
  561. }
  562. double deltaT = 0.0;
  563. double cvx = 0;
  564. double cvy = 0;
  565. double cv = 0;
  566. double cx = 0;
  567. double cy = 0;
  568. double cz = 0;
  569. int nSign = 1;
  570. sync_data sdNew;
  571. sync_data sd;
  572. cx = p->posx / (this->map_scale*1.0);
  573. cy = p->posy / (this->map_scale*1.0);
  574. cz = p->posz / (this->map_scale*1.0);
  575. #ifdef ALGORITHM_TYPE_INS
  576. int acce_direction = 0; //加速度计速度方向 0-静止,1-前进,-1-后退
  577. int uwb_direction = 0; //uwb速度方向,同上
  578. bool bUseKalman = false;
  579. bool bOriginLocate = false; //原始定位是否成功
  580. bool bDirectReturn = false; //是否直接返回本次定位结果
  581. #endif
  582. if(p->posx != INVALID_COORDINATE && p->posy != INVALID_COORDINATE){
  583. LOCATION_SYSTEM_BRANCH(LOCATION_SYSTEM_BRANCH_148);
  584. #ifdef ALGORITHM_TYPE_INS
  585. bOriginLocate = true; //原始定位成功
  586. #endif
  587. //定位成功
  588. if(this->m_nLastLocateT == 0){
  589. LOCATION_SYSTEM_BRANCH(LOCATION_SYSTEM_BRANCH_149);
  590. if (m_syncNumList.size() > 0)
  591. {
  592. m_syncNumList.erase(m_syncNumList.begin(),m_syncNumList.end());
  593. m_syncNumList.resize(0);
  594. }
  595. sdNew.sync_num = maxSyncTimes;
  596. sdNew.x = cx;
  597. sdNew.y = cy;
  598. sdNew.vx = 0;
  599. sdNew.vy = 0;
  600. sdNew.update = false;
  601. m_syncNumList.push_back(sdNew);
  602. #ifdef ALGORITHM_TYPE_INS
  603. uwb_direction = 1;
  604. #endif
  605. }
  606. else{
  607. LOCATION_SYSTEM_BRANCH(LOCATION_SYSTEM_BRANCH_150);
  608. //现在的关于同步序号的处理是这样的:
  609. //如果定位成功,就把这次定位成功的同步数据:同步序号,坐标;x,y方向的速度,扔到一个队列里,
  610. //后来定位成功的就会先根据同步序号差用加速度抛一次;
  611. //抛不掉,就用队列里的同步数据(从后往前找),找到第一个与当前同步序号相差大于5的同步数据来进行第二次计算速度以及加速度,
  612. //如果加速度大于5,就不要此次的定位数据,
  613. //如果通过加速度判断就将队列中从头开始到此同步数据的所有元素都丢弃,并插入新的此次同步数据
  614. this->m_dFirstDiff = p->dDiff[0];
  615. this->m_dSecondDiff = p->dDiff[1];
  616. if (this->b_long_interval)
  617. {
  618. LOCATION_SYSTEM_BRANCH(LOCATION_SYSTEM_BRANCH_151);
  619. //此段代码用于将上一次定位是根据两个时间差是个很大值而定位出的结果
  620. //当后续定位时就和最近的定位结果进行比较
  621. //例如:当上一次同步序号是14321,它定位时比较的同步序号是14200,时间差大于20多秒
  622. //这时我们就将b_long_interval置为true
  623. //当本次定位,同步序号是14326,,这时就需要根据最近的14321进行判断
  624. list<sync_data>::reverse_iterator it = m_syncNumList.rbegin();
  625. sync_data sdl = *it;
  626. //以下计算deltaT还需要考虑卡的同步序号轮回的情况。
  627. if (maxSyncTimes > it->sync_num)
  628. {
  629. LOCATION_SYSTEM_BRANCH(LOCATION_SYSTEM_BRANCH_152);
  630. deltaT = (maxSyncTimes - sdl.sync_num)*interval_time;
  631. }
  632. else
  633. {
  634. deltaT = (maxSyncTimes + 65536 - sdl.sync_num)*interval_time;
  635. }
  636. if (deltaT < 10 && deltaT > 0)
  637. {
  638. LOCATION_SYSTEM_BRANCH(LOCATION_SYSTEM_BRANCH_155);
  639. this->b_long_interval = false;
  640. }
  641. //避免同一个同步序号下存在多个不同卡序号
  642. if (deltaT < 1E-2)
  643. {
  644. LOCATION_SYSTEM_BRANCH(LOCATION_SYSTEM_BRANCH_156);
  645. deltaT = 0.2;
  646. }
  647. cvx = (cx - sdl.x)*this->map_scale/deltaT;
  648. cvy = (cy - sdl.y)*this->map_scale/deltaT;
  649. double avx = (cvx - sdl.vx) / deltaT;
  650. double avy = (cvy - sdl.vy) / deltaT;
  651. double av = sqrt(pow(avx,2) + pow(avy,2));
  652. //车卡的加速度
  653. switch(this->card_type){
  654. case CT_PERSON:
  655. LOCATION_SYSTEM_BRANCH(LOCATION_SYSTEM_BRANCH_157);
  656. if(av > PERSON_ACCELERATE_THRESHOLD){
  657. LOCATION_SYSTEM_BRANCH(LOCATION_SYSTEM_BRANCH_158);
  658. this->x = this->last_locate.x;
  659. this->y = this->last_locate.y;
  660. this->b_long_interval = false;
  661. pRdm->clear();
  662. m_afmData.bStatus = true;
  663. m_afmData.strCardId = this->card_id;
  664. m_afmData.nType = ALGO_FAILED_CONDITION_5;
  665. ALGORITHM_FAILED(ALGO_FAILED_CONDITION_5);
  666. return;
  667. }
  668. break;
  669. case CT_VEHICLE:
  670. LOCATION_SYSTEM_BRANCH(LOCATION_SYSTEM_BRANCH_159);
  671. if(av > VECHILE_ACCELERATE_THRESHOLD){
  672. LOCATION_SYSTEM_BRANCH(LOCATION_SYSTEM_BRANCH_160);
  673. //保留上次结果
  674. this->x = this->last_locate.x;
  675. this->y = this->last_locate.y;
  676. this->b_long_interval = false;
  677. pRdm->clear();
  678. m_afmData.bStatus = true;
  679. m_afmData.strCardId = this->card_id;
  680. m_afmData.nType = ALGO_FAILED_CONDITION_6;
  681. ALGORITHM_FAILED(ALGO_FAILED_CONDITION_6);
  682. return;
  683. }
  684. break;
  685. }
  686. deltaT = 0;
  687. cvx = cvy = 0;
  688. }
  689. //从队列尾部开始查找,找到第一个同步序号与当前计算卡的同步序号相差5个以上的数据
  690. list<sync_data>::reverse_iterator it;
  691. bool bOverflow = false;
  692. for(it = m_syncNumList.rbegin();it!=m_syncNumList.rend();it++){
  693. if(maxSyncTimes - it->sync_num >= 5){
  694. LOCATION_SYSTEM_BRANCH(LOCATION_SYSTEM_BRANCH_161);
  695. sd = *it;
  696. break;
  697. }
  698. else{
  699. if(maxSyncTimes - it->sync_num < 0 && maxSyncTimes < 100){
  700. LOCATION_SYSTEM_BRANCH(LOCATION_SYSTEM_BRANCH_162);
  701. //如果最新同步号小于列表中的同步号则
  702. if(maxSyncTimes + 65536 - it->sync_num >=5 ){//5
  703. LOCATION_SYSTEM_BRANCH(LOCATION_SYSTEM_BRANCH_163);
  704. bOverflow = true;
  705. sd = *it;
  706. }
  707. }else{
  708. continue;
  709. }
  710. }
  711. }
  712. //根据溢出条件来计算deltaT
  713. if(bOverflow){
  714. LOCATION_SYSTEM_BRANCH(LOCATION_SYSTEM_BRANCH_164);
  715. deltaT = (maxSyncTimes + 65536 - sd.sync_num)*interval_time;
  716. }else{
  717. LOCATION_SYSTEM_BRANCH(LOCATION_SYSTEM_BRANCH_165);
  718. deltaT = (maxSyncTimes - sd.sync_num)*interval_time;
  719. }
  720. //速度正负的判断:以x轴,y轴正向运动为正
  721. //如果x相等,则y2 - y1 > 0为正
  722. //其他情况,则x2 - x1 > 0 为正
  723. if(cx == sd.x){
  724. if(cy > sd.y){
  725. nSign = 1;
  726. }else{
  727. nSign = -1;
  728. }
  729. }else{
  730. if(cx > sd.x){
  731. nSign = 1;
  732. }else{
  733. nSign = -1;
  734. }
  735. }
  736. #ifdef ALGORITHM_TYPE_INS
  737. uwb_direction = (nSign == 1)?1:-1; //uwb形式判断不出静止或者怠速
  738. #endif
  739. //使用间隔来修正速度
  740. if(deltaT - 1.0 >= 0){
  741. LOCATION_SYSTEM_BRANCH(LOCATION_SYSTEM_BRANCH_166);
  742. if (deltaT > 10)
  743. {
  744. LOCATION_SYSTEM_BRANCH(LOCATION_SYSTEM_BRANCH_167);
  745. this->b_long_interval = true;
  746. }
  747. //转为m/s
  748. cvx = (cx - sd.x)*this->map_scale/deltaT;
  749. cvy = (cy - sd.y)*this->map_scale/deltaT;
  750. cv = sqrt(pow(cvx,2) + pow(cvy,2));
  751. cv = cv*nSign;
  752. double avx = (cvx - sd.vx) / deltaT;
  753. double avy = (cvy - sd.vy) / deltaT;
  754. double av = sqrt(pow(avx,2) + pow(avy,2));
  755. //车卡的加速度
  756. switch(this->card_type){
  757. case CT_PERSON:
  758. LOCATION_SYSTEM_BRANCH(LOCATION_SYSTEM_BRANCH_168);
  759. if(av > PERSON_ACCELERATE_THRESHOLD){
  760. LOCATION_SYSTEM_BRANCH(LOCATION_SYSTEM_BRANCH_169);
  761. this->x = this->last_locate.x;
  762. this->y = this->last_locate.y;
  763. pRdm->clear();
  764. m_afmData.bStatus = true;
  765. m_afmData.strCardId = this->card_id;
  766. m_afmData.nType = ALGO_FAILED_CONDITION_5;
  767. ALGORITHM_FAILED(ALGO_FAILED_CONDITION_5);
  768. return;
  769. }
  770. break;
  771. case CT_VEHICLE:
  772. LOCATION_SYSTEM_BRANCH(LOCATION_SYSTEM_BRANCH_170);
  773. if(av > VECHILE_ACCELERATE_THRESHOLD){
  774. LOCATION_SYSTEM_BRANCH(LOCATION_SYSTEM_BRANCH_171);
  775. //保留上次结果
  776. this->x = this->last_locate.x;
  777. this->y = this->last_locate.y;
  778. pRdm->clear();
  779. m_afmData.bStatus = true;
  780. m_afmData.strCardId = this->card_id;
  781. m_afmData.nType = ALGO_FAILED_CONDITION_6;
  782. ALGORITHM_FAILED(ALGO_FAILED_CONDITION_6);
  783. return;
  784. }
  785. break;
  786. }
  787. this->last_locate.acceleration = av;
  788. this->last_vx = cvx;
  789. this->last_vy = cvy;
  790. cv = cv*3.6;
  791. LOCATION_SYSTEM_BRANCH(LOCATION_SYSTEM_BRANCH_172);
  792. //速度的限制
  793. if(fabs(cv) > MAX_VECHILE_SPEED){
  794. LOCATION_SYSTEM_BRANCH(LOCATION_SYSTEM_BRANCH_173);
  795. this->x = this->last_locate.x;
  796. this->y = this->last_locate.y;
  797. pRdm->clear();
  798. m_afmData.bStatus = true;
  799. m_afmData.strCardId = this->card_id;
  800. m_afmData.nType = ALGO_FAILED_CONDITION_7;
  801. ALGORITHM_FAILED(ALGO_FAILED_CONDITION_7);
  802. return;
  803. }
  804. //删除第一个元素到tmp(含)之间的所有元素
  805. bool bStartDel = false;
  806. for(list<sync_data>::reverse_iterator tmp = m_syncNumList.rbegin();tmp != m_syncNumList.rend();)
  807. {
  808. if(bStartDel){
  809. tmp = list<sync_data>::reverse_iterator(m_syncNumList.erase((++tmp).base()));
  810. }else{
  811. if(*tmp == sd){
  812. bStartDel = true;
  813. }
  814. ++tmp;
  815. }
  816. }
  817. //更新值为当前值并插入队列
  818. sdNew.sync_num = maxSyncTimes;
  819. this->m_nSyncNumInList = sd.sync_num;
  820. sdNew.vx = cvx;
  821. sdNew.vy = cvy;
  822. sdNew.update = true;
  823. }else{
  824. cv = this->origin_locate.v;
  825. }
  826. }
  827. }
  828. #ifdef ALGORITHM_TYPE_INS
  829. else{
  830. LOCATION_SYSTEM_BRANCH(LOCATION_SYSTEM_BRANCH_174);
  831. uwb_direction = ins_direction;
  832. }
  833. #endif
  834. #ifdef ALGORITHM_TYPE_INS
  835. bRet = false;
  836. //0,1,2分别表示静止(含怠速),运动,运动中刹车
  837. switch (acce_state)
  838. {
  839. case 0:
  840. LOCATION_SYSTEM_BRANCH(LOCATION_SYSTEM_BRANCH_175);
  841. acce_direction = 0;
  842. if(acce_state_last!=0){
  843. LOCATION_SYSTEM_BRANCH(LOCATION_SYSTEM_BRANCH_176);
  844. //说明从其他状态变为静止(怠速)状态
  845. //返回这次定位结果
  846. if (bOriginLocate)
  847. {
  848. LOCATION_SYSTEM_BRANCH(LOCATION_SYSTEM_BRANCH_177);
  849. bDirectReturn = true; //直接返回本次定位结果
  850. }
  851. }else{
  852. //返回上一次定位结果
  853. LOCATION_SYSTEM_BRANCH(LOCATION_SYSTEM_BRANCH_178);
  854. bDirectReturn = false;
  855. }
  856. bUseKalman = false;
  857. //bRet = true;
  858. break;
  859. case 1://需要判断状态是否发生了变化
  860. LOCATION_SYSTEM_BRANCH(LOCATION_SYSTEM_BRANCH_179);
  861. if (acce_state_last == ACCELERATE_INIT_STATE)
  862. {
  863. LOCATION_SYSTEM_BRANCH(LOCATION_SYSTEM_BRANCH_180);
  864. //第一次获得卡的状态,需要确定首次方向
  865. acce_direction = uwb_direction; //将UWB定出的速度方向赋值给它
  866. }else{
  867. LOCATION_SYSTEM_BRANCH(LOCATION_SYSTEM_BRANCH_181);
  868. acce_direction = acce_state_last;
  869. }
  870. //bRet = false;
  871. break;
  872. case 2:
  873. case 3:
  874. LOCATION_SYSTEM_BRANCH(LOCATION_SYSTEM_BRANCH_182);
  875. acce_direction = 0; //此处将速度方向设为静止,也就是0
  876. //bRet = false;
  877. break;
  878. }
  879. if (acce_state!=acce_last_state)
  880. {
  881. // 如果状态发生了改变,则加速度计的权重需要重置
  882. LOCATION_SYSTEM_BRANCH(LOCATION_SYSTEM_BRANCH_183);
  883. this->ins_weight = INS_WEIGHT;
  884. }
  885. double cweight = 0;
  886. if(!bRet){
  887. LOCATION_SYSTEM_BRANCH(LOCATION_SYSTEM_BRANCH_184);
  888. cweight = this->ins_weight * acce_direction + this->uwb_weight*uwb_direction;
  889. //如果计算出的权重在合适范围内,就逐渐降低惯导的权限
  890. //如果计算出的权重超过范围,则重置惯导的权重为90%
  891. if(cweight>=INS_WEIGHT*-1&&cweight<=INS_WEIGHT){
  892. LOCATION_SYSTEM_BRANCH(LOCATION_SYSTEM_BRANCH_185);
  893. cweight = abs(cweight);
  894. }else{
  895. LOCATION_SYSTEM_BRANCH(LOCATION_SYSTEM_BRANCH_186);
  896. cweight = INS_WEIGHT;
  897. }
  898. if(cweight*uwb_direction < 0){
  899. //惯导和uwb定位方向不一致,定位失败
  900. //增加一次判断,与上一次的进行比对,如果和上一次的一致,则送进卡尔曼滤波
  901. LOCATION_SYSTEM_BRANCH(LOCATION_SYSTEM_BRANCH_187);
  902. if(cweight*acce_state_last > 0){
  903. LOCATION_SYSTEM_BRANCH(LOCATION_SYSTEM_BRANCH_188);
  904. bUseKalman = true;
  905. }else{
  906. LOCATION_SYSTEM_BRANCH(LOCATION_SYSTEM_BRANCH_189);
  907. bUseKalman = false;
  908. }
  909. }else{
  910. //惯导和uwb定位方向一致,定位成功
  911. //送进卡尔曼滤波
  912. LOCATION_SYSTEM_BRANCH(LOCATION_SYSTEM_BRANCH_190);
  913. bUseKalman = true;
  914. }
  915. }
  916. if (abs(cweight) > 0)
  917. {
  918. LOCATION_SYSTEM_BRANCH(LOCATION_SYSTEM_BRANCH_191);
  919. ins_direction = cweight/abs(cweight) > 0?1:-1;
  920. }else{
  921. //如果加速度计的状态权重为零
  922. LOCATION_SYSTEM_BRANCH(LOCATION_SYSTEM_BRANCH_192);
  923. ins_direction = uwb_direction;
  924. }
  925. this->ins_weight = cweight;
  926. this->ins_direction = ins_direction;
  927. this->acce_last_state = acce_last_state;
  928. //this->accelerate_state_last = this->acce_cur_state = acce_state;
  929. #endif
  930. this->accelerate_state_last = this->acce_cur_state = acce_state;
  931. if(m_nFilterType == FILTER_KALMAN){
  932. LOCATION_SYSTEM_BRANCH(LOCATION_SYSTEM_BRANCH_193);
  933. #ifdef ALGORITHM_TYPE_INS
  934. if(bUseKalman){
  935. #endif
  936. LOCATION_SYSTEM_BRANCH(LOCATION_SYSTEM_BRANCH_194);
  937. //也需要考虑轮回
  938. //double kalman_detal_t = (maxSyncTimes - this->last_locate.st)*interval_time;
  939. double kalman_detal_t = 0;
  940. if (maxSyncTimes - this->last_locate.sync_num < 0 && maxSyncTimes < 100)
  941. {
  942. kalman_detal_t = (maxSyncTimes + 65535 - this->last_locate.sync_num)*interval_time;
  943. }else{
  944. kalman_detal_t = (maxSyncTimes - this->last_locate.sync_num)*interval_time;
  945. LOCATION_SYSTEM_BRANCH(LOCATION_SYSTEM_BRANCH_195);
  946. }
  947. //通过卡尔曼滤波处理
  948. if(p->posx == INVALID_COORDINATE && p->posy == INVALID_COORDINATE){
  949. m_afmData.nCardStamp = m_nCalcSyncNum;
  950. LOCATION_SYSTEM_BRANCH(LOCATION_SYSTEM_BRANCH_196);
  951. if(this->m_pKalmanFilter->m_nCounts < 3 || this->m_pKalmanFilter->m_pCar->P(0,0) > 2 || kalman_detal_t > 3){
  952. //P(0,0):连续时间(大于2s)都定位失败
  953. //deltaT>3:距离上次成功定位时间间隔为3s
  954. LOCATION_SYSTEM_BRANCH(LOCATION_SYSTEM_BRANCH_197);
  955. this->x = this->last_locate.x;
  956. this->y = this->last_locate.y;
  957. this->z = 0;
  958. pRdm->clear();
  959. m_afmData.bStatus = true;
  960. m_afmData.strCardId = this->card_id;
  961. this->m_pKalmanFilter->m_bFlag = false;
  962. m_afmData.nType = ALGO_FAILED_CONDITION_8;
  963. ALGORITHM_FAILED(ALGO_FAILED_CONDITION_8);
  964. return;
  965. }
  966. if(this->m_pKalmanFilter->m_nCounts >= 3){
  967. //只有三次以上才允许使用kalman滤波以下的函数
  968. LOCATION_SYSTEM_BRANCH(LOCATION_SYSTEM_BRANCH_198);
  969. this->m_pKalmanFilter->Predict(kalman_detal_t);
  970. this->z = 0;
  971. sdNew.update = true;
  972. }
  973. }else{
  974. LOCATION_SYSTEM_BRANCH(LOCATION_SYSTEM_BRANCH_199);
  975. this->m_pKalmanFilter->m_bFlag = true;
  976. this->m_pKalmanFilter->m_nCounts++;
  977. this->m_pKalmanFilter->m_pCar->z(0,0) = cx * this->map_scale;
  978. this->m_pKalmanFilter->m_pCar->z(1,0) = cvx;
  979. this->m_pKalmanFilter->m_pCar->z(2,0) = cy * this->map_scale;
  980. this->m_pKalmanFilter->m_pCar->z(3,0) = cvy;
  981. if(this->m_pKalmanFilter->m_nCounts == 1){
  982. //第一次直接赋值
  983. LOCATION_SYSTEM_BRANCH(LOCATION_SYSTEM_BRANCH_200);
  984. this->m_pKalmanFilter->m_pCar->x = this->m_pKalmanFilter->m_pCar->z;
  985. }
  986. if(this->m_pKalmanFilter->m_nCounts == 2){
  987. //两次处理
  988. LOCATION_SYSTEM_BRANCH(LOCATION_SYSTEM_BRANCH_201);
  989. 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;
  990. 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;
  991. this->m_pKalmanFilter->m_pCar->x = this->m_pKalmanFilter->m_pCar->z;
  992. }
  993. if(this->m_pKalmanFilter->m_nCounts >= 3){
  994. LOCATION_SYSTEM_BRANCH(LOCATION_SYSTEM_BRANCH_202);
  995. //只有三次以上才允许使用kalman滤波以下的函数
  996. //this->m_pKalmanFilter->Predict_Correct(deltaT);
  997. this->m_pKalmanFilter->Predict_Correct(kalman_detal_t);
  998. sdNew.update = true;
  999. if(deltaT!=0){
  1000. LOCATION_SYSTEM_BRANCH(LOCATION_SYSTEM_BRANCH_203);
  1001. this->m_pKalmanFilter->m_pCar->x(1,0) = (this->m_pKalmanFilter->m_pCar->x(0,0) - sd.x*this->map_scale)/deltaT;
  1002. this->m_pKalmanFilter->m_pCar->x(3,0) = (this->m_pKalmanFilter->m_pCar->x(2,0) - sd.y*this->map_scale)/deltaT;
  1003. }
  1004. }
  1005. }
  1006. if(p->nFirstReader == -1 && p->nSecondReader == -1){
  1007. LOCATION_SYSTEM_BRANCH(LOCATION_SYSTEM_BRANCH_204);
  1008. p->nFirstReader = last_s_locate_reader[0];
  1009. p->nSecondReader = last_s_locate_reader[1];
  1010. }
  1011. //增加地图集的判定,判断定位结果是否在地图集上
  1012. //如果不在地图集上,需要再次定位
  1013. //需要带出定位结果的分站信息,
  1014. //利用地图集中分站信息再次定位
  1015. //std::shared_ptr<POS> kalman_p(new POS());
  1016. std::shared_ptr<POS> kalman_p = std::make_shared<POS>();
  1017. kalman_p->nFirstReader = p->nFirstReader;
  1018. kalman_p->nSecondReader = p->nSecondReader;
  1019. kalman_p->posx = this->m_pKalmanFilter->m_pCar->x(0,0);//* this->map_scale
  1020. kalman_p->posy = this->m_pKalmanFilter->m_pCar->x(2,0);//* this->map_scale
  1021. kalman_p->pos_radius = p->pos_radius;
  1022. //二维定位不需要再判定在地图集上了
  1023. if(!LocateAlgorithm::IsOnMap(kalman_p,pTdoaReaderPathMap)){
  1024. //再一次定位到地图集上
  1025. LOCATION_SYSTEM_BRANCH(LOCATION_SYSTEM_BRANCH_205);
  1026. //std::shared_ptr<POS> cp = LocateAlgorithm::Pos(kalman_p,pTdoaReaderPathMap);
  1027. // 定位结果在地图集外,映射到地图集上
  1028. std::shared_ptr<POS> cp = LocateAlgorithm::MappingToPath(kalman_p,pTdoaReaderPathMap);
  1029. if(cp != nullptr){
  1030. if (cp->posx != INVALID_COORDINATE && cp->posy !=INVALID_COORDINATE)
  1031. {
  1032. LOCATION_SYSTEM_BRANCH(LOCATION_SYSTEM_BRANCH_206);
  1033. this->m_pKalmanFilter->m_pCar->x(0,0) = cp->posx;
  1034. this->m_pKalmanFilter->m_pCar->x(2,0) = cp->posy;
  1035. }
  1036. }
  1037. cp.reset();
  1038. }
  1039. this->x = this->m_pKalmanFilter->m_pCar->x(0,0) / this->map_scale;
  1040. this->y = this->m_pKalmanFilter->m_pCar->x(2,0) / this->map_scale;
  1041. if (abs(this->last_locate.x) > 1E-5 || abs(this->last_locate.y) > 1E-5)
  1042. {
  1043. //去除采集刚启动,last_locate的坐标为零而导致判断车辆上下行方向错误的问题
  1044. //车辆上下行确定
  1045. //3个条件:起点(x1,y1),终点(x2,y2)
  1046. //1.x1==x2的情况下,y2>y1为下行
  1047. //2.y1==y2的情况下,x1>x2为下行
  1048. //3.x1>x2且y2>y1为下行
  1049. //其他情况为上行
  1050. if ((this->last_locate.x == this->x && this->y > this->last_locate.y)
  1051. ||(this->last_locate.x > this->x && this->y == this->last_locate.y)
  1052. ||(this->last_locate.x > this->x && this->y > this->last_locate.y))
  1053. {
  1054. this->m_nStream = DOWN_STREAM;
  1055. }
  1056. else
  1057. {
  1058. this->m_nStream = UP_STREAM;
  1059. }
  1060. }
  1061. nSign = 1;
  1062. if(this->m_pKalmanFilter->m_pCar->x(1,0) == 0){
  1063. if(this->m_pKalmanFilter->m_pCar->x(3,0)>0){
  1064. nSign = 1;
  1065. }
  1066. }else{
  1067. if(this->m_pKalmanFilter->m_pCar->x(1,0) > 0){
  1068. nSign = 1;
  1069. }else{
  1070. nSign = -1;
  1071. }
  1072. }
  1073. this->m_nMoveDirection = nSign;
  1074. //algo_calc_offset();
  1075. this->last_locate.x = this->x = this->m_pKalmanFilter->m_pCar->x(0,0) / this->map_scale;
  1076. this->last_locate.y = this->y = this->m_pKalmanFilter->m_pCar->x(2,0) / this->map_scale;
  1077. this->last_locate.z = this->z;
  1078. this->last_locate.sync_num = maxSyncTimes;
  1079. last_s_locate_reader[0] = p->nFirstReader;
  1080. last_s_locate_reader[1] = p->nSecondReader;
  1081. this->m_pKalmanFilter->m_pCar->t = this->m_nLastLocateT = maxSyncTimes;
  1082. //速度的计算采用求平均的方式
  1083. double speed = sqrt(pow(this->m_pKalmanFilter->m_pCar->x(1,0),2) + pow(this->m_pKalmanFilter->m_pCar->x(3,0),2));
  1084. speed *=3.6; //转为km/h
  1085. if (vt_his_speed.size()==3)//10
  1086. {
  1087. vt_his_speed.pop_front();
  1088. }
  1089. vt_his_speed.push_back(speed);
  1090. int total = 0;
  1091. double sum_speed = 0;
  1092. for (list<double>::iterator it = vt_his_speed.begin();it != vt_his_speed.end();++it)
  1093. {
  1094. if (*it > 0)
  1095. {
  1096. sum_speed += *it;
  1097. total++;
  1098. }
  1099. }
  1100. double av = 0;
  1101. if (total>0)
  1102. {
  1103. av = sum_speed / total;
  1104. }
  1105. if (this->acce_cur_state == STATE_ACCE_STATIC)
  1106. {
  1107. this->v = 0;
  1108. }
  1109. else
  1110. {
  1111. this->v = av*nSign;
  1112. }
  1113. cvx = this->m_pKalmanFilter->m_pCar->x(1,0);
  1114. cvy = this->m_pKalmanFilter->m_pCar->x(3,0);
  1115. this->last_locate.v = this->v;
  1116. this->origin_locate.v = cv;
  1117. ALGORITHM_FAILED(ALGO_LOC_SUCCESSED);
  1118. LOCATION_SYSTEM_BRANCH(LOCATION_SYSTEM_BRANCH_207);
  1119. if(sdNew.update){
  1120. sdNew.sync_num = maxSyncTimes;
  1121. sdNew.x = this->x;
  1122. sdNew.y = this->y;
  1123. sdNew.vx = cvx;
  1124. sdNew.vy = cvy;
  1125. m_syncNumList.push_back(sdNew);
  1126. LOCATION_SYSTEM_BRANCH(LOCATION_SYSTEM_BRANCH_208);
  1127. }
  1128. //校验机会3次,3次内跳回去就丢弃此次计算结果
  1129. if (this->x != INVALID_COORDINATE && this->y != INVALID_COORDINATE)
  1130. {
  1131. int nDirection = 0;
  1132. if (!algo_is_same_direction(this->x,this->y,0))
  1133. {
  1134. this->x = this->last_locate.x;
  1135. this->y = this->last_locate.y;
  1136. }
  1137. }
  1138. #ifdef ALGORITHM_TYPE_INS
  1139. }else{
  1140. //这组数据的处理方法是:
  1141. //如果第一次成功定位,但状态是静止,就取第一次成功定位值(这是为了处理当第一次成功定位,但状态是静止的,此时取上一次定位值为零的问题),
  1142. //后续如果不管定位成功还是失败,只要状态是静止的,就输出上一次成功定位值,并更新同步序号。
  1143. //取上次结果
  1144. LOCATION_SYSTEM_BRANCH(LOCATION_SYSTEM_BRANCH_209);
  1145. if (bDirectReturn)
  1146. {
  1147. LOCATION_SYSTEM_BRANCH(LOCATION_SYSTEM_BRANCH_210);
  1148. if(this->map_scale > 0){
  1149. LOCATION_SYSTEM_BRANCH(LOCATION_SYSTEM_BRANCH_211);
  1150. this->last_locate.x = this->x = p->posx / (this->map_scale*1.0);
  1151. this->last_locate.y = this->y = p->posy / (this->map_scale*1.0);
  1152. this->last_locate.z = this->z = p->posz / (this->map_scale*1.0);
  1153. this->last_locate.v = this->v = cv;
  1154. }
  1155. }else{
  1156. LOCATION_SYSTEM_BRANCH(LOCATION_SYSTEM_BRANCH_212);
  1157. this->x = this->last_locate.x;
  1158. this->y = this->last_locate.y;
  1159. this->z = -5;//this->last_locate.z
  1160. this->v = this->last_locate.v;
  1161. }
  1162. this->m_nLastLocateT = this->m_nCalcSyncNum = this->last_locate.st = this->sync_num = maxSyncTimes;
  1163. if(sdNew.update){
  1164. LOCATION_SYSTEM_BRANCH(LOCATION_SYSTEM_BRANCH_213);
  1165. sdNew.sync_num = maxSyncTimes;
  1166. sdNew.x = this->x;
  1167. sdNew.y = this->y;
  1168. sdNew.vx = cvx;
  1169. sdNew.vy = cvy;
  1170. m_syncNumList.push_back(sdNew);
  1171. this->m_nSyncNumInList = maxSyncTimes;
  1172. }
  1173. this->is_deal_by_algo = true;
  1174. }
  1175. #endif
  1176. }else{
  1177. //TRACE(_T("no kalman . \n"));
  1178. //最新通过算法算出的结果
  1179. LOCATION_SYSTEM_BRANCH(LOCATION_SYSTEM_BRANCH_214);
  1180. if(p->posx == INVALID_COORDINATE || p->posy == INVALID_COORDINATE){
  1181. LOCATION_SYSTEM_BRANCH(LOCATION_SYSTEM_BRANCH_215);
  1182. this->x = this->last_locate.x;
  1183. this->y = this->last_locate.y;
  1184. this->z = this->last_locate.z;
  1185. }else{
  1186. LOCATION_SYSTEM_BRANCH(LOCATION_SYSTEM_BRANCH_216);
  1187. if(this->map_scale > 0){
  1188. LOCATION_SYSTEM_BRANCH(LOCATION_SYSTEM_BRANCH_216);
  1189. this->x = p->posx / (this->map_scale*1.0);
  1190. this->y = p->posy / (this->map_scale*1.0);
  1191. this->z = p->posz / (this->map_scale*1.0);
  1192. }
  1193. double speed = (fabs(this->v) + fabs(cv))/2;
  1194. this->v = speed*nSign;
  1195. this->last_locate.x = this->x;
  1196. this->last_locate.y = this->y;
  1197. this->last_locate.z = this->z;
  1198. this->last_locate.v = this->v;
  1199. this->m_nLastLocateT = this->last_locate.sync_num = maxSyncTimes;
  1200. }
  1201. this->a = 0;
  1202. }
  1203. }
  1204. LOCATION_SYSTEM_BRANCH(LOCATION_SYSTEM_BRANCH_217);
  1205. pRdm->clear();
  1206. }
  1207. int Card::algo_tdoa_1d(int cnt)
  1208. {
  1209. int ret = 0;
  1210. //1.数据有效性判断
  1211. bool b_is_single = false;
  1212. ret = CheckDistData(cnt);
  1213. if(ret){
  1214. return 1;
  1215. }
  1216. //2.组装数据
  1217. std::shared_ptr<ReceiveDataMap> pRdm = std::make_shared<ReceiveDataMap>();
  1218. pRdm->clear();
  1219. ret = AssembleDistData(pRdm);
  1220. if(ret){
  1221. OutputCmdLog(2);
  1222. }
  1223. // 获取两次定位的时间差数据
  1224. GetDeltaT(mp_dists_locate);
  1225. //保证每个ct都输出一个坐标:
  1226. //特殊情况一:单条记录也输出
  1227. std::shared_ptr<POS> pos = std::make_shared<POS>();
  1228. //3.算法定位
  1229. std::vector<std::shared_ptr<POS>> udm_pos;
  1230. udm_pos.resize(0);
  1231. //time_stamp_cal == 46163
  1232. //time_stamp_cal == 18639
  1233. ret = LocateAlgorithm::CalcTdoaPosition(pRdm,pTdoaReaderPathMap,udm_pos);
  1234. //4.从多解中筛选出一个解,
  1235. //存在两种可能:
  1236. //a.可能无解返回非0,
  1237. //b.可能有解,但解不正确,比如解的位置在4727,-100,但选出的解是4727,-200
  1238. ret = ChooseOneSolution(pRdm, udm_pos, pos);
  1239. if (ret == 0)
  1240. {
  1241. std::shared_ptr<POS> tmp_pos = std::make_shared<POS>();
  1242. *tmp_pos = *pos;
  1243. if (ret == 0)
  1244. {
  1245. //5.如果有解,则对唯一解做合法性验证
  1246. //主要验证条件为:加速度和速度
  1247. ret = CheckSolution(tmp_pos);
  1248. }else{
  1249. tmp_pos->posx = INVALID_COORDINATE;
  1250. tmp_pos->posy = INVALID_COORDINATE;
  1251. tmp_pos->cx = INVALID_COORDINATE / (map_scale*1.0);
  1252. tmp_pos->cy = INVALID_COORDINATE / (map_scale*1.0);
  1253. }
  1254. //无论正确与否,保存原始值
  1255. SaveOriginDataBeforeFilter(tmp_pos);
  1256. //6.卡尔曼滤波,两种情况:
  1257. //a.解通过了合法性验证,
  1258. //b.无解,但在2s内
  1259. //则使用卡尔曼滤波进行滤波处理
  1260. if (FILTER_KALMAN == m_nFilterType)
  1261. {
  1262. ret = KalmanFilterProcess(tmp_pos);
  1263. }
  1264. if (tmp_pos->posx != INVALID_COORDINATE && tmp_pos->posy != INVALID_COORDINATE)
  1265. {
  1266. *pos = *tmp_pos;
  1267. }else{
  1268. if (ret==0)
  1269. {
  1270. //解存在,解可能异常
  1271. //判断是否回退,至少第三次定位才走这,回退了使用上一次的
  1272. TRACE(_T("no sol x: %f,y%f\r\n"),pos->posx,pos->posy);
  1273. }else{
  1274. //解异常
  1275. }
  1276. }
  1277. }
  1278. bool bRet = false;
  1279. //是否在地图集上
  1280. bRet = LocateAlgorithm::IsOnMap(pos,pTdoaReaderPathMap);
  1281. if (bRet)
  1282. {
  1283. //检查是否回退
  1284. ret = SaveCardAlgoData(pos);
  1285. }
  1286. return ret;
  1287. }
  1288. int Card::algo_tdoa_2d(int cnt)
  1289. {
  1290. int ret = 0;
  1291. //1.数据有效性判断
  1292. ret = CheckDistData(cnt);
  1293. if(ret) return ret;
  1294. //2.组装数据
  1295. std::shared_ptr<ReceiveDataMap> pRdm = std::make_shared<ReceiveDataMap>();
  1296. pRdm->clear();
  1297. ret = AssembleDistData(pRdm);
  1298. if(ret) return ret;
  1299. //3.算法定位
  1300. std::shared_ptr<POS> pos = std::make_shared<POS>();
  1301. pos = LocateAlgorithm::TdoaLocate2d(pRdm);
  1302. if (pos == nullptr)
  1303. {
  1304. return 0;
  1305. }else{
  1306. pos->cx = pos->posx / (map_scale*1.0);
  1307. pos->cy = pos->posy / (map_scale*1.0);
  1308. pos->cz = pos->posz / (map_scale*1.0);
  1309. isoutput = true;
  1310. }
  1311. //5.唯一解合法性验证
  1312. ret = CheckSolution(pos);
  1313. if(ret) {
  1314. return ret;
  1315. }
  1316. //保存原始值
  1317. SaveOriginDataBeforeFilter(pos);
  1318. //6.滤波
  1319. if (FILTER_KALMAN == m_nFilterType)
  1320. {
  1321. ret = KalmanFilterProcess(pos);
  1322. }
  1323. if (ret)
  1324. {
  1325. return ret;
  1326. }
  1327. //7.数据保存
  1328. ret = SaveCardAlgoData(pos);
  1329. if (ret)
  1330. {
  1331. return ret;
  1332. }
  1333. return ret;
  1334. }
  1335. int Card::algo_tdoa_3d(int cnt)
  1336. {
  1337. if (cnt < 3)
  1338. {
  1339. return 1;
  1340. }
  1341. int sync_num = 0;
  1342. int k = 0;
  1343. bool ret = false;
  1344. mp_dists_locate.clear();
  1345. for (DistMap::iterator it = _dists.front().distmap.begin();it!=_dists.front().distmap.end();++it,k++)
  1346. {
  1347. if (k==0)
  1348. {
  1349. sync_num = it->second->sync_num;
  1350. }
  1351. else
  1352. {
  1353. int diff = abs(sync_num - it->second->sync_num);
  1354. if (diff >= 5)
  1355. {
  1356. ret = true;
  1357. break;
  1358. }
  1359. }
  1360. if (it->second->tt == LLONG_MAX)
  1361. {
  1362. ret = true;
  1363. break;
  1364. }
  1365. mp_dists_locate.insert(make_pair(it->second->tt,it->second));
  1366. }
  1367. if (ret)
  1368. {
  1369. return 1;
  1370. }
  1371. //构造数据
  1372. int i = 0;
  1373. int max_sync_time = 0;
  1374. std::shared_ptr<ReceiveDataMap> pRdm = std::make_shared<ReceiveDataMap>();
  1375. pRdm->clear();
  1376. for (map<unsigned long long ,std::shared_ptr<_coordinate>>::iterator it = mp_dists_locate.begin();it!=mp_dists_locate.end();++it)
  1377. {
  1378. int card_time_stamp = 0;
  1379. if (i==0)
  1380. {
  1381. max_sync_time = it->second->sync_num;
  1382. card_time_stamp = it->second->t;
  1383. }else{
  1384. if (max_sync_time < it->second->sync_num)
  1385. {
  1386. max_sync_time = it->second->sync_num;
  1387. card_time_stamp = it->second->t;
  1388. }
  1389. }
  1390. ReceiveDataMap::iterator rdm_it = pRdm->find(it->second->tt);
  1391. if (rdm_it == pRdm->end())
  1392. {
  1393. //保存信息用于定位
  1394. std::shared_ptr<ReceiveData> prd = std::make_shared<ReceiveData>();
  1395. prd->reader_id = it->second->reader_id;
  1396. prd->antenna_id = it->second->antenna_id;
  1397. prd->rec_time_stamp = it->second->tt;
  1398. prd->x = it->second->x*this->map_scale;
  1399. prd->y = it->second->y*this->map_scale;
  1400. prd->z = it->second->z*this->map_scale;
  1401. prd->special = it->second->special;
  1402. if (prd->rec_time_stamp > 0)
  1403. {
  1404. pRdm->insert(make_pair(prd->rec_time_stamp,prd));
  1405. }
  1406. }
  1407. i++;
  1408. }
  1409. if (pRdm->size() >2)
  1410. {
  1411. m_nCalcSyncNum = max_sync_time;
  1412. }
  1413. return 0;
  1414. }
  1415. /*
  1416. * 根据算法计算新位置的坐标以及卡的上一次坐标,计算车辆偏移坐标
  1417. *
  1418. * param
  1419. * 无
  1420. *
  1421. * return
  1422. * 无locatepos
  1423. *
  1424. */
  1425. void Card::algo_calc_offset()
  1426. {
  1427. double offset = CHAMBER_WIDTH/4;
  1428. if (this->last_locate.x == this->x)
  1429. {
  1430. //判断车的方向
  1431. switch (this->m_nMoveDirection)
  1432. {
  1433. case 1:
  1434. right_y = left_y = this->y;
  1435. right_x = this->x - offset;
  1436. left_x = this->x + offset;
  1437. break;
  1438. case -1:
  1439. right_y = left_y = this->y;
  1440. right_x = this->x + offset;
  1441. left_x = this->x - offset;
  1442. break;
  1443. default:
  1444. break;
  1445. }
  1446. }else if(this->last_locate.y == this->y){
  1447. //这个分支单独拆出来是因为斜率为零,求0的倒数是错误
  1448. //判断车的方向
  1449. switch (this->m_nMoveDirection)
  1450. {
  1451. case 1:
  1452. right_x = left_x = this->x;
  1453. right_y = this->y + offset;
  1454. left_y = this->y - offset;
  1455. break;
  1456. case -1:
  1457. right_x = left_x = this->x;
  1458. right_y = this->y - offset;
  1459. left_y = this->y + offset;
  1460. break;
  1461. default:
  1462. break;
  1463. }
  1464. }else{
  1465. double k = (this->y - this->last_locate.y)/(this->x - this->last_locate.x);
  1466. double b1 = this->y - k*this->x;
  1467. double b2 = this->y - 1*this->x/k;
  1468. double right_d = 0;
  1469. double left_d = 0;
  1470. right_d = offset*sqrt(pow(k,2) + 1) + b2 - b1;
  1471. left_d = -1*offset*sqrt(pow(k,2) + 1) + b2 - b1;
  1472. switch (this->m_nMoveDirection)
  1473. {
  1474. case 1:
  1475. this->right_x = k*right_d/(pow(k,2) - 1);
  1476. this->right_y = this->right_x*1/k + b2;
  1477. this->left_x = k*left_d/(pow(k,2) - 1);
  1478. this->left_y = this->left_x*1/k + b2;
  1479. break;
  1480. case -1:
  1481. this->right_x = k*left_d/(pow(k,2) - 1);
  1482. this->right_y = this->right_x*1/k + b2;
  1483. this->left_x = k*right_d/(pow(k,2) - 1);
  1484. this->left_y = this->left_x*1/k + b2;
  1485. break;
  1486. default:
  1487. break;
  1488. }
  1489. }
  1490. }
  1491. bool Card::algo_is_same_direction(double x,double y,double z)
  1492. {
  1493. int sign = 0;
  1494. if(x == last_locate.x){
  1495. if(y > last_locate.y){
  1496. sign = 1;
  1497. }else{
  1498. sign = -1;
  1499. }
  1500. }else{
  1501. if(x > last_locate.x){
  1502. sign = 1;
  1503. }else{
  1504. sign = -1;
  1505. }
  1506. }
  1507. if (sign!=m_nMoveDirection)
  1508. {
  1509. diff_direction_counts++;
  1510. }else{
  1511. diff_direction_counts = 0;
  1512. }
  1513. if (diff_direction_counts > 0 && diff_direction_counts <3)
  1514. {
  1515. return true;
  1516. }else{
  1517. diff_direction_counts = 0;
  1518. return false;
  1519. }
  1520. }
  1521. bool Card::is_pos_state_changed() // 考勤
  1522. {
  1523. bool ret = false;
  1524. if(pos_state != pos_state_old)
  1525. {
  1526. if(pos_state_count >= pos_state_confirm_times)
  1527. {
  1528. pos_state_old = pos_state;
  1529. ret = true;
  1530. }
  1531. }
  1532. return ret ;
  1533. //return (this->last_area_type_id != this->cur_area_type_id && 0 == (this->cur_area_type_id & this->last_area_type_id));
  1534. }
  1535. /*
  1536. * 将数据加入到队列中,但相同卡的接受时间戳的数据满足一定条件时,将相应的数据丢入算法进行定位计算,
  1537. * 计算完后删除相应的数据,释放内存
  1538. *
  1539. * param
  1540. * dist 定位数据
  1541. *
  1542. * return
  1543. * 无
  1544. */
  1545. void Card::add_dist(_coordinate* dist)
  1546. {
  1547. EnterCriticalSection(&m_csCard);
  1548. string s = concat(dist->reader_id, dist->antenna_id);
  1549. int idx = FindDistMap(dist->t);
  1550. if(-1 == idx){
  1551. DistQueMapItem dq;
  1552. dq.cardstamp = dist->t;
  1553. _dists.push_back(dq);
  1554. }else{
  1555. }
  1556. //如果_dists.size()的大小始终为1,错误原因可能是卡在数据库中不存在
  1557. if(_dists.size() >= MAX_DIST_CACHE){ // 超过缓存数量限制
  1558. // 计算并删除第一个
  1559. get_coordinate();
  1560. remove_dist_head();
  1561. }
  1562. if (this->x != this->last_x || this->y != this->last_y)
  1563. {
  1564. this->b_pos_change = true;
  1565. }else{
  1566. this->b_pos_change = false;
  1567. }
  1568. LeaveCriticalSection(&m_csCard);
  1569. }
  1570. void Card::add_dist(std::shared_ptr<_coordinate> dist)
  1571. {
  1572. LOCATION_SYSTEM_BRANCH(LOCATION_SYSTEM_BRANCH_134);
  1573. EnterCriticalSection(&m_csCard);
  1574. string s = concat(dist->reader_id, dist->antenna_id);
  1575. int idx = FindDistMap(dist->t);
  1576. if(-1 == idx){
  1577. LOCATION_SYSTEM_BRANCH(LOCATION_SYSTEM_BRANCH_135);
  1578. DistQueMapItem dq;
  1579. dq.cardstamp = dist->t;
  1580. dq.distmap[s] = dist;
  1581. _dists.push_back(dq);
  1582. }else{
  1583. LOCATION_SYSTEM_BRANCH(LOCATION_SYSTEM_BRANCH_136);
  1584. _dists[idx].distmap[s] = dist;
  1585. }
  1586. if(_dists.size() >= MAX_DIST_CACHE){ //超过缓存数量限制
  1587. // 计算并删除第一个
  1588. LOCATION_SYSTEM_BRANCH(LOCATION_SYSTEM_BRANCH_137);
  1589. get_coordinate(); //调用计算过程
  1590. _dists.pop_front();
  1591. }
  1592. if (this->x != this->last_x || this->y != this->last_y)
  1593. {
  1594. LOCATION_SYSTEM_BRANCH(LOCATION_SYSTEM_BRANCH_138);
  1595. this->b_pos_change = true;
  1596. }else{
  1597. LOCATION_SYSTEM_BRANCH(LOCATION_SYSTEM_BRANCH_139);
  1598. this->b_pos_change = false;
  1599. }
  1600. LeaveCriticalSection(&m_csCard);
  1601. }
  1602. time_t Card::get_working_time()
  1603. {
  1604. //return (card_type == CT_VEHICLE) ? leave_park_time : down_time;
  1605. return down_time;
  1606. }
  1607. int Card::get_effictive_dist_count( int offset /*= 0*/ )
  1608. {
  1609. if(0 == _dists.size()) return 0;
  1610. time_stamp_cal = _dists.front().cardstamp;
  1611. return (_dists.front().distmap.size());
  1612. }
  1613. /*
  1614. * Card析构类
  1615. */
  1616. Card::~Card(void)
  1617. {
  1618. if(init_postion){
  1619. for(int i = DIST_COUNT - 1;i >= 0;i--){
  1620. if(p_dists_locate[i] != NULL){
  1621. delete p_dists_locate[i];
  1622. p_dists_locate[i] = NULL;
  1623. }
  1624. }
  1625. if(p_dists_locate){
  1626. delete[] p_dists_locate;
  1627. p_dists_locate = NULL;
  1628. }
  1629. }
  1630. _dists.clear();
  1631. DeleteCriticalSection(&m_csCard);
  1632. }
  1633. double Card::get_speed()
  1634. {
  1635. return fabs(v);
  1636. }
  1637. /*
  1638. * 采用TOF或者TDOA算法进行定位计算
  1639. *
  1640. * param
  1641. * cnt ------ _dists数据条数
  1642. *
  1643. * return
  1644. * 无返回值
  1645. *
  1646. */
  1647. void Card::get_coordinate( int cnt )
  1648. {
  1649. #ifdef ALGORITHM_TOF
  1650. algo_tof(cnt);
  1651. #elif defined ALGORITHM_TYPE_TDOA
  1652. //algo_tdoa(cnt);
  1653. int ret = 0;
  1654. //ret = algo_tdoa_1d(cnt);
  1655. //车辆定位20170705
  1656. if(this->card_type == CT_VEHICLE)
  1657. {
  1658. ret = DiscreteLocate(cnt);//数据正常20170705
  1659. }
  1660. else if(this->card_type == CT_PERSON)
  1661. {
  1662. ret = PersonLocation(cnt);
  1663. }
  1664. //algo_tdoa_2d(cnt);
  1665. #else
  1666. #endif
  1667. //0代表成功写了格子,尚未入库
  1668. if (ret == 0)
  1669. {
  1670. //写日记flag
  1671. isNeedWrited = true;
  1672. inspect_coordinate(this->acce_cur_state);
  1673. }
  1674. if(_isnan(this->x) || _isnan(this->y) || _isnan(this->z)){
  1675. this->x = this->last_x;
  1676. this->y = this->last_y;
  1677. this->z = this->last_z;
  1678. this->output_x = this->x;
  1679. this->output_y = this->y;
  1680. }
  1681. if (isNeedWrited || nStartLocateCounts<6)
  1682. {
  1683. std::string flag1 = is_fit_pos?"Y":"N";
  1684. int nSource1 = 0;
  1685. if (last_cell)
  1686. {
  1687. nSource1 = last_cell->dataSource;
  1688. }
  1689. char chLog1[300] = {0};
  1690. sprintf_s(chLog1,"cardid: %s, sync_num: %d, ct: %d, ox: %f, oy: %f, x: %f,y: %f, readerName: %s, cellId : %d, originId: %d, oReaderName: %s, ov: %f, cv: %f, fit: %s, source: %d",
  1691. card_id.c_str(),
  1692. m_nCalcSyncNum,
  1693. time_stamp_cal,
  1694. origin_locate.x,
  1695. origin_locate.y,
  1696. x,
  1697. y,
  1698. curCellReaderName.c_str(),
  1699. curCellId,
  1700. originCellId,
  1701. originCellReaderName.c_str(),
  1702. origin_locate.v,
  1703. get_speed(),
  1704. flag1.c_str(),
  1705. nSource1);
  1706. std::string strLog1 = "";
  1707. strLog1 = chLog1;
  1708. Log::write_log(FILE_TYPE::KALMAN_S,strLog1,true);
  1709. isNeedWrited = false;
  1710. }
  1711. }
  1712. void Card::get_coordinate()
  1713. {
  1714. get_coordinate(get_effictive_dist_count());
  1715. }
  1716. /*
  1717. * 根据状态值获得状态文本描述
  1718. *
  1719. * param
  1720. * 无
  1721. *
  1722. * return
  1723. * 状态文本描述
  1724. *
  1725. */
  1726. std::string Card::get_state_text()
  1727. {
  1728. string ret = "";
  1729. state = 0;
  1730. state_biz = 0;
  1731. if(status_help == STATUS_ERROR){
  1732. state += STATUS_HELP;
  1733. state_biz += STATUS_HELP;
  1734. ret += "呼救,";
  1735. }
  1736. if(status_area_over_time == STATUS_ERROR){
  1737. state += STATUS_AREA_OVER_TIME;
  1738. state_biz += STATUS_AREA_OVER_TIME;
  1739. ret += "区域超时,";
  1740. }else if(status_area_over_time == STATUS_ERROR){
  1741. state += STATUS_OVER_TIME;
  1742. state_biz += STATUS_OVER_TIME;
  1743. ret += "超时,";
  1744. }
  1745. if(status_area_over_speed == STATUS_ERROR){
  1746. state += STATUS_AREA_OVER_SPEED;
  1747. state_biz += STATUS_AREA_OVER_SPEED;
  1748. ret += "区域超速,";
  1749. }else if(status_over_speed == STATUS_ERROR){
  1750. state += STATUS_OVER_SPEED;
  1751. state_biz += STATUS_OVER_SPEED;
  1752. ret += "超速,";
  1753. }
  1754. if(status_area_forbidden == STATUS_ERROR){
  1755. state += STATUS_AREA_FORBIDDEN;
  1756. state_biz += STATUS_AREA_FORBIDDEN;
  1757. ret += "进入限制区域,";
  1758. }
  1759. if(status_call == STATUS_ERROR){
  1760. state += STATUS_CALL;
  1761. state_biz += STATUS_CALL;
  1762. ret += "呼叫,";
  1763. }
  1764. if(status_lost == STATUS_ERROR){
  1765. state += STATUS_LOST;
  1766. state_biz += STATUS_LOST;
  1767. ret += "进入盲区,";
  1768. }
  1769. if(power_state == STATUS_ERROR){
  1770. state += STATUS_POWER_LOWER;
  1771. ret += "电量低,";
  1772. }else if(power_state == STATUS_ERROR_SERIOUS){
  1773. state += STATUS_POWER_LOWER_SERIOUS;
  1774. ret += "电量极低,";
  1775. }
  1776. if(ret.length() > 0){
  1777. ret = ret.substr(0, ret.length() - 1);
  1778. }else{
  1779. ret = "正常";
  1780. }
  1781. return ret;
  1782. }
  1783. std::string Card::get_acc_text()
  1784. {
  1785. string ret = "";
  1786. state_moving = (accelerate_state & 0x01)? STATUS_MOVING_MOTION : STATUS_MOVING_MOTIONLESS;
  1787. if(state_moving == STATUS_MOVING_MOTIONLESS){
  1788. ret += "静止";
  1789. }else if(state_moving == STATUS_MOVING_MOTION){
  1790. if(CT_VEHICLE == card_type && 0 == v)
  1791. {
  1792. state_moving = STATUS_MOVING_IDLING;
  1793. ret += "怠速";
  1794. }
  1795. else
  1796. {
  1797. ret += "运动";
  1798. }
  1799. }else if(state_moving == STATUS_MOVING_IDLING)
  1800. {
  1801. ret += "怠速";
  1802. }
  1803. return ret;
  1804. }
  1805. std::string Card::concat( int reader_id, int ant_id )
  1806. {
  1807. char s[10];
  1808. sprintf_s(s, "%d-%d", reader_id, ant_id);
  1809. return s;
  1810. }
  1811. /*
  1812. * 滤波功能设置
  1813. *
  1814. * param
  1815. * nType ------ 滤波类型
  1816. *
  1817. * return
  1818. * 无
  1819. */
  1820. void Card::EnableFilter(int nType)
  1821. {
  1822. //如果无滤波类型直接返回
  1823. if(nType == NO_FILTER){
  1824. return;
  1825. }
  1826. //开启滤波功能,设置滤波类型
  1827. m_bUseFilter = TRUE;
  1828. m_nFilterType = nType;
  1829. switch(nType){
  1830. case FILTER_KALMAN:
  1831. //分配卡尔曼滤波类型变量并初始化参数
  1832. /*if(m_pKalmanFilter == NULL){
  1833. m_pKalmanFilter = new CKalmanFilter();
  1834. m_pKalmanFilter->Initial(0.2);
  1835. m_pKalmanFilter->m_bFlag = false;
  1836. }*/
  1837. if(m_pKalmanFilter == nullptr){
  1838. std::unique_ptr<CKalmanFilter> p(new CKalmanFilter());
  1839. m_pKalmanFilter = std::move(p);
  1840. m_pKalmanFilter->Initial(0.2);
  1841. m_pKalmanFilter->m_bFlag = false;
  1842. }
  1843. break;
  1844. default:
  1845. break;
  1846. }
  1847. }
  1848. void Card::remove_dist_head()
  1849. {
  1850. DistMap tmp = _dists.front().distmap;
  1851. if(tmp.size() > 0 ){
  1852. DistMap::iterator it_mp_dist = tmp.begin();
  1853. for(it_mp_dist;it_mp_dist != tmp.end();){
  1854. it_mp_dist = tmp.erase(it_mp_dist);
  1855. }
  1856. }
  1857. _dists.pop_front();
  1858. }
  1859. Reader::Reader(void)
  1860. {
  1861. reader_id = device_type_id = pos_state = map_id = area_id = temperature = tick_count= 0;
  1862. sync_level = 0xFF;
  1863. reader_state = reader_state_old = STATUS_DEVICE_NORMAL;
  1864. m_nIsSpecial = -1;
  1865. last_send_time = rec_time = reader_time = lost_time = time(NULL);
  1866. sync_rootId = 0;
  1867. map_scale = 1.0;
  1868. reader_x = reader_y = reader_z = reader_angle = reader_interval_time = -9999.0;
  1869. reader_name = ip = "";
  1870. for(int i = 0;i < ANTENNA_COUNT;i++){
  1871. ant[i] = nullptr;
  1872. }
  1873. for(int i = 0;i < ADHOC_COUNT;i++){
  1874. adhoc[i] = nullptr;
  1875. }
  1876. bIsInitCoverage = false;
  1877. init_ctrl_reader_state = false;
  1878. for(int i = 0; i < READER_EVENT_COUNT; i++){
  1879. m_event_list[i] = 0;
  1880. }
  1881. }
  1882. Reader::~Reader(void)
  1883. {
  1884. }
  1885. std::string Reader::get_state_text()
  1886. {
  1887. string ret = "";
  1888. if(reader_state == STATUS_DEVICE_ERROR){
  1889. ret = "故障";
  1890. }else if(reader_state == STATUS_DEVICE_NORMAL){
  1891. ret = "正常";
  1892. }
  1893. return ret;
  1894. }
  1895. Antenna::Antenna(void)
  1896. {
  1897. antenna_angle = 0;
  1898. antenna_id = 0;
  1899. antenna_x = 0;
  1900. antenna_y = 0;
  1901. antenna_z = 0;
  1902. }
  1903. Antenna::~Antenna(void)
  1904. {
  1905. }
  1906. Area::Area(void)
  1907. {
  1908. is_att = 1;
  1909. polygon_count = 0;
  1910. polygon = NULL;
  1911. map_id = area_id = area_type_id = 0 ;
  1912. area_name = area_type_name = path = "";
  1913. over_count_person = over_time_person = under_count_person = under_time_person = 0;
  1914. over_count_vehicle = over_time_vehicle = under_count_vehicle = under_time_vehicle = 0;
  1915. count_person = count_vehicle = count_card = 0;
  1916. is_area_over_time_person = is_area_over_time_vehicle = false;
  1917. count_area_over_time_person = count_area_over_time_vehicle = 0;
  1918. time_over_time_person = time_over_time_vehicle = time(NULL);
  1919. is_area_over_count_person = is_area_over_count_vehicle = false;
  1920. count_area_over_count_person = count_area_over_count_vehicle = 0;
  1921. time_over_count_person = time_over_count_vehicle = time(NULL);
  1922. is_area_forbidden_person = is_area_forbidden_vehicle = false;
  1923. count_area_forbidden_person = count_area_forbidden_vehicle = 0;
  1924. time_forbidden_person = time_forbidden_vehicle = time(NULL);
  1925. over_speed_vehicle = 0;
  1926. area_card_list_person = std::make_shared<CardMap>();
  1927. area_card_list_vehicle = std::make_shared<CardMap>();
  1928. area_card_list_over_speed = std::make_shared<CardMap>();
  1929. for(int i = 0; i < AREA_EVENT_COUNT; i++){
  1930. m_event_list[i] = 0;
  1931. }
  1932. }
  1933. Area::~Area(void)
  1934. {
  1935. if(polygon){
  1936. delete[] polygon;
  1937. polygon = NULL;
  1938. }
  1939. }
  1940. void Area::init_border(string sz_path)
  1941. {
  1942. if(sz_path == ""){
  1943. return ;
  1944. }
  1945. std::vector<std::string> vec = split(sz_path, " ");
  1946. std::vector<std::string>::iterator it = vec.begin();
  1947. if(polygon){
  1948. delete[] polygon;
  1949. polygon = NULL;
  1950. }
  1951. polygon = new _point[vec.size()];
  1952. polygon_count = 0;
  1953. for(; it != vec.end(); ++it){
  1954. std::vector<std::string> subvec = split(it->c_str(), ",");
  1955. _point p;
  1956. p.x = get_vertex(subvec[0]);
  1957. p.y = get_vertex(subvec[1]);
  1958. p.z = 0;
  1959. polygon[polygon_count] = p;
  1960. polygon_count++;
  1961. }
  1962. }
  1963. std::vector<std::string> Area::split( std::string str,std::string pattern )
  1964. {
  1965. std::string::size_type pos;
  1966. std::vector<std::string> result;
  1967. str+=pattern;//扩展字符串以方便操作
  1968. unsigned int size=str.size();
  1969. for(unsigned int i=0; i<size; i++){
  1970. pos=str.find(pattern,i);
  1971. if(pos<size){
  1972. std::string s=str.substr(i,pos-i);
  1973. result.push_back(s);
  1974. i=pos+pattern.size()-1;
  1975. }
  1976. }
  1977. return result;
  1978. }
  1979. double Area::get_vertex( std::string src)
  1980. {
  1981. std::string dest = "";
  1982. for(unsigned int i = 0; i < src.length(); i++){
  1983. if((src[i] >= '0' && src[i]<='9') || src[i]=='-' || src[i] == '.'){
  1984. dest += src[i];
  1985. }
  1986. }
  1987. return atof(dest.c_str());
  1988. }
  1989. bool Area::is_in_polygon( _point p )
  1990. {
  1991. if(polygon == NULL){
  1992. return false;
  1993. }
  1994. int counter = 0;
  1995. int i;
  1996. double xinters;
  1997. _point p1,p2;
  1998. p1 = polygon[0];
  1999. for (int i=1;i<= polygon_count;i++) {
  2000. p2 = polygon[i % polygon_count];
  2001. if (p.y > MIN(p1.y,p2.y)) {
  2002. if (p.y <= MAX(p1.y,p2.y)) {
  2003. if (p.x <= MAX(p1.x,p2.x)) {
  2004. if (p1.y != p2.y) {
  2005. xinters = (p.y-p1.y)*(p2.x-p1.x)/(p2.y-p1.y)+p1.x;
  2006. if (p1.x == p2.x || p.x <= xinters)
  2007. counter++;
  2008. }
  2009. }
  2010. }
  2011. }
  2012. p1 = p2;
  2013. }
  2014. //TRACE(_T("counter : %d \n"),counter);
  2015. return (counter % 2 == 0) ? false : true;
  2016. }
  2017. int Area::is_special()
  2018. {
  2019. return (area_type_id == AREA_TYPE_NO_COVER) ? 1: 0;
  2020. }
  2021. int Card::FindDistMap( int cardstamp )
  2022. {
  2023. int idx = -1;
  2024. for(int i = _dists.size() - 1; i >= 0; i--){
  2025. if(_dists[i].cardstamp == cardstamp ){
  2026. return i;
  2027. }
  2028. }
  2029. return idx;
  2030. }
  2031. int Card::KalmanFilterProcess(std::shared_ptr<POS>& pos)
  2032. {
  2033. if (abs(pos->posx) < 1E-4 && abs(pos->posy) < 1E-4)
  2034. {
  2035. return 1;
  2036. }
  2037. double kalman_detal_t = 0;
  2038. double interval_time = 0.2;
  2039. //存在分站在计数序号为1000掉电,但重新上电会导致此时的计算值偏大
  2040. if (m_nCalcSyncNum - this->last_locate.sync_num < 0 && m_nCalcSyncNum < 100)
  2041. {
  2042. kalman_detal_t = (m_nCalcSyncNum + 65535 - this->last_locate.sync_num)*interval_time;
  2043. }else{
  2044. kalman_detal_t = (m_nCalcSyncNum - this->last_locate.sync_num)*interval_time;
  2045. }
  2046. //如果定位失败,通过卡尔曼滤波处理
  2047. if(pos->posx == INVALID_COORDINATE && pos->posy == INVALID_COORDINATE){
  2048. m_afmData.nCardStamp = m_nCalcSyncNum;
  2049. if(this->m_pKalmanFilter->m_nCounts < 3 || this->m_pKalmanFilter->m_pCar->P(0,0) > 2 || kalman_detal_t > 3){
  2050. //P(0,0):连续时间(大于2s)都定位失败
  2051. //deltaT>3:距离上次成功定位时间间隔为3s
  2052. //this->x = this->last_locate.x;
  2053. //this->y = this->last_locate.y;
  2054. pos->cx = INVALID_COORDINATE / map_scale;
  2055. pos->cy = INVALID_COORDINATE / map_scale;
  2056. pos->posx = INVALID_COORDINATE;
  2057. pos->posy = INVALID_COORDINATE;
  2058. pos->reason = KALMAN_FILTER_LONG_INTERVAL;
  2059. //this->z = 0;
  2060. m_afmData.bStatus = true;
  2061. m_afmData.strCardId = this->card_id;
  2062. this->m_pKalmanFilter->m_bFlag = false;
  2063. m_afmData.nType = ALGO_FAILED_CONDITION_8;
  2064. ALGORITHM_FAILED(ALGO_FAILED_CONDITION_8);
  2065. return KALMAN_FILTER_LONG_INTERVAL;
  2066. }
  2067. if(this->m_pKalmanFilter->m_nCounts >= 3){
  2068. //只有三次以上才允许使用kalman滤波以下的函数
  2069. this->m_pKalmanFilter->Predict(kalman_detal_t);
  2070. this->z = 0;
  2071. pos->update = true;
  2072. }
  2073. }else{
  2074. pos->update = true;
  2075. this->m_pKalmanFilter->m_bFlag = true;
  2076. this->m_pKalmanFilter->m_nCounts++;
  2077. this->m_pKalmanFilter->m_pCar->z(0,0) = pos->posx;
  2078. this->m_pKalmanFilter->m_pCar->z(1,0) = pos->cvx;
  2079. this->m_pKalmanFilter->m_pCar->z(2,0) = pos->posy;
  2080. this->m_pKalmanFilter->m_pCar->z(3,0) = pos->cvy;
  2081. if(this->m_pKalmanFilter->m_nCounts == 1){
  2082. //第一次直接赋值
  2083. this->m_pKalmanFilter->m_pCar->x = this->m_pKalmanFilter->m_pCar->z;
  2084. }
  2085. if(this->m_pKalmanFilter->m_nCounts == 2){
  2086. //两次处理
  2087. if (abs(pos->diff_reader_sync_num) > 1E-5)
  2088. {
  2089. 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;
  2090. 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;
  2091. this->m_pKalmanFilter->m_pCar->x = this->m_pKalmanFilter->m_pCar->z;
  2092. }
  2093. }
  2094. if(this->m_pKalmanFilter->m_nCounts >= 3){
  2095. //只有三次以上才允许使用kalman滤波以下的函数
  2096. //this->m_pKalmanFilter->Predict_Correct(deltaT);
  2097. this->m_pKalmanFilter->Predict_Correct(kalman_detal_t);
  2098. if(fabs(pos->diff_reader_sync_num) > 1E-5){
  2099. 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;
  2100. 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;
  2101. }
  2102. }
  2103. }
  2104. if (pos->update)
  2105. {
  2106. //如果经过卡尔曼处理之后
  2107. if(pos->nFirstReader == 0 && pos->nSecondReader == 0){
  2108. pos->nFirstReader = last_s_locate_reader[0];
  2109. pos->nSecondReader = last_s_locate_reader[1];
  2110. }
  2111. //二维定位不需要如下内容
  2112. //增加地图集的判定,判断定位结果是否在地图集上
  2113. //如果不在地图集上,需要再次定位
  2114. //需要带出定位结果的分站信息,
  2115. //利用地图集中分站信息再次定位
  2116. std::shared_ptr<POS> kalman_p = std::make_shared<POS>();
  2117. kalman_p->posx = this->m_pKalmanFilter->m_pCar->x(0,0);
  2118. kalman_p->posy = this->m_pKalmanFilter->m_pCar->x(2,0);
  2119. ////二维定位不需要再判定在地图集上了
  2120. if(!LocateAlgorithm::IsOnMap(kalman_p,pTdoaReaderPathMap)){
  2121. //再一次定位到地图集上
  2122. LOCATION_SYSTEM_BRANCH(LOCATION_SYSTEM_BRANCH_205);
  2123. std::shared_ptr<POS> cp = LocateAlgorithm::Pos(kalman_p,pTdoaReaderPathMap);
  2124. if(cp != nullptr){
  2125. if (cp->posx != INVALID_COORDINATE && cp->posy !=INVALID_COORDINATE)
  2126. {
  2127. LOCATION_SYSTEM_BRANCH(LOCATION_SYSTEM_BRANCH_206);
  2128. this->m_pKalmanFilter->m_pCar->x(0,0) = cp->posx;
  2129. this->m_pKalmanFilter->m_pCar->x(2,0) = cp->posy;
  2130. }
  2131. }
  2132. cp.reset();
  2133. }
  2134. pos->posx = this->m_pKalmanFilter->m_pCar->x(0,0);
  2135. pos->posy = this->m_pKalmanFilter->m_pCar->x(2,0);
  2136. pos->cvx = this->m_pKalmanFilter->m_pCar->x(1,0);
  2137. pos->cvy = this->m_pKalmanFilter->m_pCar->x(3,0);
  2138. pos->cx = pos->posx / (map_scale*1.0);
  2139. pos->cy = pos->posy / (map_scale*1.0);
  2140. }
  2141. return 0;
  2142. }
  2143. int Card::InitAlgoParam()
  2144. {
  2145. m_nLastLocateT = 0;
  2146. last_s_locate_reader[0] = last_s_locate_reader[1] = 0;
  2147. ins_weight = 1; //加速度计状态权重
  2148. uwb_weight = 9; //uwb状态权重
  2149. acce_cur_state = 0; //在算法中保存当前加速度状态,保存这两个值的原因是在多线程情况下accelerate_state和accelerate_state_last状态可能变化
  2150. acce_last_state = 0; //在算法中保存上一次加速度状态
  2151. is_first_location = true;
  2152. if (vt_his_speed.size() > 0)
  2153. {
  2154. vt_his_speed.resize(0);
  2155. }
  2156. return 0;
  2157. }
  2158. /*
  2159. * 检查溢出
  2160. */
  2161. bool Card::CheckCrossCycle()
  2162. {
  2163. //time_stamp_last特指卡的计数序号,非分站的
  2164. //当卡需要新定位的计数序列号小于上一次成功定位的序列号,则此次不定位,避免定位结果的回退
  2165. if (this->time_stamp_last != 0 && this->time_stamp_last > this->time_stamp_cal)
  2166. {
  2167. string str_last = "";
  2168. string str_cur = "";
  2169. str_last = CFunctions::int2string(this->time_stamp_last);
  2170. str_cur = CFunctions::int2string(this->time_stamp_cal);
  2171. //如何解决卡的ct为2000时,卡重启,从0开始计数的问题?
  2172. if (str_last.length() >= 3 && str_cur.length() <= 2)
  2173. {
  2174. //满足此条件,表示标示卡的计数序号走完了65535的周期
  2175. return true;
  2176. }
  2177. else{
  2178. return false;
  2179. }
  2180. }
  2181. return true;
  2182. }
  2183. /*
  2184. * 判断是否回跳:依据上下行来判断
  2185. *
  2186. * return
  2187. * 如果回跳,则返回true,否则返回false
  2188. */
  2189. bool Card::isRebound(std::shared_ptr<POS> pos)
  2190. {
  2191. if (m_nStream == 0)
  2192. {
  2193. return false;
  2194. }
  2195. //车辆上下行确定
  2196. if (abs(this->last_locate.x) > 1E-5 || abs(this->last_locate.y) > 1E-5)
  2197. {
  2198. //去除采集刚启动,last_locate的坐标为零而导致判断车辆上下行方向错误的问题
  2199. //3个条件:起点(x1,y1),终点(x2,y2)
  2200. //1.x1==x2的情况下,y2>y1为下行
  2201. //2.y1==y2的情况下,x1>x2为下行
  2202. //3.x1>x2且y2>y1为下行
  2203. //其他情况为上行
  2204. int nStream = 0;
  2205. if ((this->last_locate.x == pos->cx && pos->cy > this->last_locate.y)
  2206. ||(this->last_locate.x > pos->cx && pos->cy == this->last_locate.y)
  2207. ||(this->last_locate.x > pos->cx && pos->cy > this->last_locate.y))
  2208. {
  2209. nStream = DOWN_STREAM;
  2210. }
  2211. else
  2212. {
  2213. nStream = UP_STREAM;
  2214. }
  2215. if (nStream != this->m_nStream)
  2216. {
  2217. return true;
  2218. }
  2219. }
  2220. return false;
  2221. }
  2222. bool Card::isRebound(std::shared_ptr<Cell> cell)
  2223. {
  2224. int nCurIncrease = 0;
  2225. if (last_cell->id > cell->id)
  2226. {
  2227. nCurIncrease = -1;
  2228. }else if(last_cell->id < cell->id){
  2229. nCurIncrease = 1;
  2230. }else{
  2231. nCurIncrease = 0;
  2232. }
  2233. if (nCurIncrease*this->nIncrease < 0)
  2234. {
  2235. //存在反向
  2236. return true;
  2237. }
  2238. return false;
  2239. }
  2240. /*
  2241. * 组装拟合数据,主要是前REF_POSITION_NUM次的定位数据信息
  2242. *
  2243. * param
  2244. * pos 定位坐标
  2245. *
  2246. * return
  2247. * 成功返回0
  2248. */
  2249. int Card::UpdateFittingData(std::shared_ptr<POS> pos)
  2250. {
  2251. if (cur_ref_totals == REF_POSITION_NUM - 1)
  2252. {
  2253. for (int i = 0;i < cur_ref_totals-1;i++)
  2254. {
  2255. fitting_v[i] = fitting_v[i + 1];
  2256. fitting_ct[i] = fitting_ct[i + 1];
  2257. fitting_x[i] = fitting_x[i + 1];
  2258. fitting_y[i] = fitting_y[i + 1];
  2259. }
  2260. }
  2261. fitting_v[cur_ref_totals] = v;
  2262. fitting_ct[cur_ref_totals] = m_nCalcSyncNum;
  2263. fitting_x[cur_ref_totals] = pos->posx;
  2264. fitting_y[cur_ref_totals] = pos->posy;
  2265. if (cur_ref_totals < REF_POSITION_NUM - 1)
  2266. {
  2267. cur_ref_totals++;
  2268. }
  2269. return 0;
  2270. }
  2271. int Card::CalcFittingData()
  2272. {
  2273. /*if (cur_ref_totals != REF_POSITION_NUM - 1)
  2274. {
  2275. if (Fitting::VAR(fitting_v) < 25)
  2276. {
  2277. double fitting_coe_x[REF_POSITION_NUM] = {0};
  2278. double fitting_coe_y[REF_POSITION_NUM] = {0};
  2279. Fitting::EMatrix(fitting_ct,fitting_x,REF_POSITION_NUM,3,fitting_coe_x);
  2280. Fitting::EMatrix(fitting_ct,fitting_y,REF_POSITION_NUM,3,fitting_coe_y);
  2281. for (size_t i=0;i<FIT_POSITION_NUM;i++)
  2282. {
  2283. fit_new_x[i] = Fitting::polyval(fitting_coe_x,fitting_ct[cur_ref_totals] + i);
  2284. fit_new_y[i] = Fitting::polyval(fitting_coe_y,fitting_ct[cur_ref_totals] + i);
  2285. }
  2286. }
  2287. }*/
  2288. //如果滤波失败或者其他条件失败,
  2289. //则通过直线回归算法拟合出三个解
  2290. vector<double> vx,vy,vt;
  2291. vx.resize(0);
  2292. vy.resize(0);
  2293. vt.resize(0);
  2294. for (list<std::shared_ptr<POS>>::iterator it = his_pos.begin();it!=his_pos.end();++it)
  2295. {
  2296. vt.push_back((*it)->card_count);
  2297. vx.push_back((*it)->posx);
  2298. vy.push_back((*it)->posy);
  2299. }
  2300. //线性回归拟合,从5个历史点中取出3个预测点
  2301. Fit fit_x;
  2302. fit_x.linearFit(vt,vx);
  2303. Fit fit_y;
  2304. fit_y.linearFit(vt,vy);
  2305. for (int i = 0;i < FIT_POSITION_NUM;i++)
  2306. {
  2307. fit_new_x[i] = 0;
  2308. fit_new_y[i] = 0;
  2309. }
  2310. for (int i = 1;i <= FIT_POSITION_NUM;i++)
  2311. {
  2312. fit_new_x[i-1] = fit_x.getY(vt[4] + i);
  2313. fit_new_y[i-1] = fit_y.getY(vt[4] + i);
  2314. }
  2315. have_fit_pos = true;
  2316. return 0;
  2317. }
  2318. /*
  2319. * 计算线性拟合数据,
  2320. * 当满足如下两个条件:
  2321. * a.之前定位成功,
  2322. * b.参考数据小于5,
  2323. * c.连续取拟合数据等于3次了,
  2324. * 则不计算拟合数据
  2325. *
  2326. * param
  2327. * ret 定位状态
  2328. * pos 定位结果
  2329. *
  2330. * return
  2331. * 成功获得拟合数据返回0,否则返回1
  2332. *
  2333. */
  2334. int Card::CalcFittingData(int ret,std::shared_ptr<POS>& pos)
  2335. {
  2336. if (his_pos.size() < FIT_POSITION_NUM || cur_fit_nums >= 3)
  2337. {
  2338. if (cur_fit_nums == 3)
  2339. {
  2340. have_fit_pos = false;
  2341. }
  2342. return 1;
  2343. }
  2344. if (ret == 0 && pos->reason == 0)
  2345. {
  2346. return 2;
  2347. }
  2348. if (cur_fit_nums == 0)
  2349. {
  2350. //如果滤波失败或者其他条件失败,
  2351. //则通过直线回归算法拟合出三个解
  2352. vector<double> vx,vy,vt;
  2353. vx.resize(0);
  2354. vy.resize(0);
  2355. vt.resize(0);
  2356. for (list<std::shared_ptr<POS>>::iterator it = his_pos.begin();it!=his_pos.end();++it)
  2357. {
  2358. vt.push_back((*it)->card_count);
  2359. vx.push_back((*it)->posx);
  2360. vy.push_back((*it)->posy);
  2361. }
  2362. //线性回归拟合,从5个历史点中取出3个预测点
  2363. Fit fit_x;
  2364. fit_x.linearFit(vt,vx);
  2365. Fit fit_y;
  2366. fit_y.linearFit(vt,vy);
  2367. for (int i = 1;i <= FIT_POSITION_NUM;i++)
  2368. {
  2369. fit_new_x[i-1] = fit_x.getY(vt[4] + i);
  2370. fit_new_y[i-1] = fit_y.getY(vt[4] + i);
  2371. }
  2372. }
  2373. int index = cur_fit_nums;
  2374. //判断
  2375. double kx = fit_new_x[index];
  2376. double ky = fit_new_y[index];
  2377. std::shared_ptr<POS> ks = std::make_shared<POS>();
  2378. ks->posx = fit_new_x[index] ;
  2379. ks->posy = fit_new_y[index] ;
  2380. ks->cx = ks->posx / (1.0*map_scale);
  2381. ks->cy = ks->posy / (1.0*map_scale);
  2382. //判断拟合数据是否在地图集上,如果不在返回1
  2383. if (!LocateAlgorithm::IsOnMap(ks,pTdoaReaderPathMap))
  2384. {
  2385. //不在地图集上,则返回
  2386. return 1;
  2387. }
  2388. pos->posx = ks->posx;
  2389. pos->posy = ks->posy;
  2390. pos->cx = ks->cx;
  2391. pos->cy = ks->cy;
  2392. if (fabs(pos->diff_reader_sync_num) > 1E-4)
  2393. {
  2394. pos->cvx = (pos->cx - last_locate.x)/pos->diff_reader_sync_num;
  2395. pos->cvy = (pos->cy - last_locate.y)/pos->diff_reader_sync_num;
  2396. }
  2397. cur_fit_nums++;
  2398. pos->reason = 0;
  2399. pos->is_fit = true;
  2400. have_fit_pos = true;
  2401. return 0;
  2402. }
  2403. /*
  2404. * 通过多项式拟合计算数据,
  2405. * 当满足如下两个条件:
  2406. * a.之前定位成功,
  2407. * b.参考数据小于5,
  2408. * c.连续取拟合数据等于3次了,
  2409. * 则不计算拟合数据
  2410. *
  2411. * param
  2412. * ret 定位状态
  2413. * pos 定位结果
  2414. *
  2415. * return
  2416. * 成功获得拟合数据返回0,否则返回1
  2417. *
  2418. */
  2419. int Card::CalcLongFittingData(int ret,std::shared_ptr<POS>& pos)
  2420. {
  2421. int nums = FIT_POSITION_NUM*4;
  2422. if (long_his_pos.size() < nums || cur_fit_nums >= nums)
  2423. {
  2424. if (cur_fit_nums == nums)
  2425. {
  2426. have_fit_pos = false;
  2427. }
  2428. return 1;
  2429. }
  2430. if (ret == 0 && pos->reason == 0)
  2431. {
  2432. return 2;
  2433. }
  2434. if (cur_fit_nums <= 3)
  2435. {
  2436. //如果滤波失败或者其他条件失败,
  2437. //则通过多项式拟合算法拟合出20个解
  2438. vector<double> vx,vy,vt;
  2439. vx.resize(0);
  2440. vy.resize(0);
  2441. vt.resize(0);
  2442. for (list<std::shared_ptr<POS>>::iterator it = long_his_pos.begin();it!=long_his_pos.end();++it)
  2443. {
  2444. vt.push_back((*it)->card_count);
  2445. vx.push_back((*it)->posx);
  2446. vy.push_back((*it)->posy);
  2447. }
  2448. //多项式拟合,从20个历史点中拟合出20个预测点
  2449. Fit fit_x;
  2450. fit_x.polyfit(vt,vx,2);
  2451. Fit fit_y;
  2452. fit_y.polyfit(vt,vy,2);
  2453. for (int i = 1;i <= nums;i++)
  2454. {
  2455. long_fit_new_x[i-1] = fit_x.getY(vt[nums - 1] + i);
  2456. long_fit_new_y[i-1] = fit_y.getY(vt[nums - 1] + i);
  2457. }
  2458. }
  2459. int index = cur_fit_nums - 3;
  2460. //判断
  2461. double kx = long_fit_new_x[index];
  2462. double ky = long_fit_new_y[index];
  2463. std::shared_ptr<POS> ks = std::make_shared<POS>();
  2464. ks->posx = long_fit_new_x[index] ;
  2465. ks->posy = long_fit_new_y[index] ;
  2466. ks->cx = ks->posx / (1.0*map_scale);
  2467. ks->cy = ks->posy / (1.0*map_scale);
  2468. //判断拟合数据是否在地图集上,如果不在返回1
  2469. if (!LocateAlgorithm::IsOnMap(ks,pTdoaReaderPathMap))
  2470. {
  2471. //不在地图集上,则返回
  2472. return 1;
  2473. }
  2474. pos->posx = ks->posx;
  2475. pos->posy = ks->posy;
  2476. pos->cx = ks->cx;
  2477. pos->cy = ks->cy;
  2478. if (fabs(pos->diff_reader_sync_num) > 1E-4)
  2479. {
  2480. pos->cvx = (pos->cx - last_locate.x)/pos->diff_reader_sync_num;
  2481. pos->cvy = (pos->cy - last_locate.y)/pos->diff_reader_sync_num;
  2482. }
  2483. cur_fit_nums++;
  2484. pos->reason = 0;
  2485. pos->is_fit = true;
  2486. have_fit_pos = true;
  2487. return 0;
  2488. }
  2489. int Card::CalcLongFittingData()
  2490. {
  2491. int nums = FIT_POSITION_NUM*4;
  2492. //如果滤波失败或者其他条件失败,
  2493. //则通过多项式拟合算法拟合出20个解
  2494. vector<double> vx,vy,vt;
  2495. vx.resize(0);
  2496. vy.resize(0);
  2497. vt.resize(0);
  2498. for (list<std::shared_ptr<POS>>::iterator it = long_his_pos.begin();it!=long_his_pos.end();++it)
  2499. {
  2500. vt.push_back((*it)->card_count);
  2501. vx.push_back((*it)->posx);
  2502. vy.push_back((*it)->posy);
  2503. }
  2504. //多项式拟合,从20个历史点中拟合出20个预测点
  2505. Fit fit_x;
  2506. fit_x.polyfit(vt,vx,2);
  2507. Fit fit_y;
  2508. fit_y.polyfit(vt,vy,2);
  2509. for (int i = 0;i < nums;i++)
  2510. {
  2511. long_fit_new_x[i] = 0;
  2512. long_fit_new_y[i] = 0;
  2513. }
  2514. double diff_x = 0 , diff_y = 0;
  2515. double dx[FIT_POSITION_NUM*4] = {0};
  2516. double dy[FIT_POSITION_NUM*4] = {0};
  2517. for (int i = 1;i <= nums;i++)
  2518. {
  2519. long_fit_new_x[i-1] = fit_x.getY(vt[nums - 1] + i);
  2520. dx[i-1] = long_fit_new_x[i-1];
  2521. long_fit_new_y[i-1] = fit_y.getY(vt[nums - 1] + i);
  2522. dy[i-1] = long_fit_new_y[i-1];
  2523. }
  2524. double start_x = 0.0;
  2525. double end_x = 0.0;
  2526. double start_y = 0.0;
  2527. double end_y = 0.0;
  2528. start_x = long_fit_new_x[4];
  2529. start_y = long_fit_new_y[4];
  2530. end_x = long_fit_new_x[5];
  2531. end_y = long_fit_new_y[5];
  2532. double distance = sqrt(pow(start_x - end_x,2)+pow(start_y - end_y,2));
  2533. double percent = 0.5;
  2534. for (int i = 5;i < nums;i++)
  2535. {
  2536. distance *= percent;
  2537. if (abs(long_fit_new_x[i-1] - long_fit_new_x[i]) < 1E-4)
  2538. {
  2539. //表示x相等,在y轴上
  2540. if (long_fit_new_y[i-1] < long_fit_new_y[i])
  2541. {
  2542. //如果下一次拟合值比当前拟合值大
  2543. dy[i] = dy[i-1] + distance;
  2544. }else{
  2545. dy[i] = dy[i-1] - distance;
  2546. }
  2547. }else{
  2548. //计算斜率
  2549. double k = (long_fit_new_y[i-1] - long_fit_new_y[i])/(long_fit_new_x[i-1] - long_fit_new_x[i]);
  2550. //在有斜率的地方
  2551. double arg = atan(k);
  2552. if (long_fit_new_x[i-1] < long_fit_new_x[i] && long_fit_new_y[i-1] < long_fit_new_y[i])
  2553. {
  2554. dx[i] = dx[i-1] + cos(arg)*distance;
  2555. dy[i] = dy[i-1] + sin(arg)*distance;
  2556. }
  2557. else
  2558. {
  2559. dx[i] = dx[i-1] - cos(arg)*distance;
  2560. dy[i] = dy[i-1] - sin(arg)*distance;
  2561. }
  2562. }
  2563. //使用下一个点和第六点的距离取50%
  2564. distance = sqrt(pow(dx[i-1] - end_x,2) + pow(dy[i] - end_y,2));
  2565. }
  2566. for (int i = 0;i < nums;i++)
  2567. {
  2568. long_fit_new_x[i] = dx[i];
  2569. long_fit_new_y[i] = dy[i];
  2570. }
  2571. have_long_fit_pos = true;
  2572. return 0;
  2573. }
  2574. std::shared_ptr<POS> Card::GetPosFromFittingData()
  2575. {
  2576. if ((!have_fit_pos&&!have_long_fit_pos)
  2577. || (have_fit_pos && cur_fit_nums >= 3&&!have_long_fit_pos)
  2578. || (have_fit_pos && have_long_fit_pos && cur_fit_nums >= 20))
  2579. {
  2580. //1.如果5点拟合和20点拟合无数据,则无法获取
  2581. //2.如果有5点拟合,但无20点拟合,获取拟合数据超过3,则无法获取
  2582. //3.如果有5点拟合,且有20点拟合,而且获取拟合数据超过20,则无法获取
  2583. return nullptr;
  2584. }
  2585. std::shared_ptr<POS> p = std::make_shared<POS>();
  2586. //没有拟合20个点的数据
  2587. if (!have_long_fit_pos)
  2588. {
  2589. if (have_fit_pos)
  2590. {
  2591. p->posx = fit_new_x[cur_fit_nums];
  2592. p->posy = fit_new_y[cur_fit_nums];
  2593. p->cx = p->posx / (map_scale*1.0);
  2594. p->cy = p->posy / (map_scale*1.0);
  2595. p->is_fit = true;
  2596. p->reason = 0;
  2597. cur_fit_nums++;
  2598. }
  2599. }else{
  2600. //有拟合20个点的数据
  2601. p->posx = long_fit_new_x[cur_fit_nums];
  2602. p->posy = long_fit_new_y[cur_fit_nums];
  2603. p->cx = p->posx / (map_scale*1.0);
  2604. p->cy = p->posy / (map_scale*1.0);
  2605. p->is_fit = true;
  2606. p->reason = 0;
  2607. cur_fit_nums++;
  2608. }
  2609. if (p->posx == INVALID_COORDINATE && p->posy == INVALID_COORDINATE)
  2610. {
  2611. p = nullptr;
  2612. }
  2613. return p;
  2614. }
  2615. int Card::GetPosFromFittingData(std::shared_ptr<POS>& pos)
  2616. {
  2617. if (have_fit_pos || have_long_fit_pos)
  2618. {
  2619. //如果拟合了数据,则直接取拟合数据
  2620. std::shared_ptr<POS> fit_p = GetPosFromFittingData();
  2621. if (fit_p == nullptr)
  2622. {
  2623. return 1;
  2624. }
  2625. pos->posx = fit_p->posx;
  2626. pos->posy = fit_p->posy;
  2627. pos->cx = fit_p->cx;
  2628. pos->cy = fit_p->cy;
  2629. if (m_nCalcSyncNum - this->last_locate.sync_num < 0 && m_nCalcSyncNum < 100)
  2630. {
  2631. pos->diff_reader_sync_num = (m_nCalcSyncNum + 65536 - this->last_locate.sync_num)*0.2;
  2632. }else{
  2633. pos->diff_reader_sync_num = (m_nCalcSyncNum - this->last_locate.sync_num)*0.2;
  2634. }
  2635. if (!LocateAlgorithm::IsOnMap(pos,pTdoaReaderPathMap))
  2636. {
  2637. pos->reason = 1;
  2638. return 1;
  2639. }else{
  2640. //如果在地图集上,则计算相关参数
  2641. if (fabs(pos->diff_reader_sync_num) > 1E-4)
  2642. {
  2643. pos->cvx = (pos->cx - last_locate.x)/pos->diff_reader_sync_num;
  2644. pos->cvy = (pos->cy - last_locate.y)/pos->diff_reader_sync_num;
  2645. }else{
  2646. pos->cvx = last_vx;
  2647. pos->cvy = last_vy;
  2648. }
  2649. cur_fit_nums++;
  2650. pos->reason = 0;
  2651. pos->is_fit = true;
  2652. }
  2653. }else{
  2654. return 1;
  2655. }
  2656. return 0;
  2657. }
  2658. int Card::CheckSolutionByFit(int ret,std::shared_ptr<POS>& pos)
  2659. {
  2660. //拟合预测解
  2661. is_ref_pos = true;
  2662. //std::shared_ptr<POS> fit_pos = std::make_shared<POS>();
  2663. std::shared_ptr<POS> fit_pos = GetPosFromFittingData();
  2664. if (fit_pos == nullptr)
  2665. {
  2666. return 1;
  2667. }
  2668. fit_pos->diff_reader_sync_num = pos->diff_reader_sync_num;
  2669. int fit_ret = fit_pos->reason;
  2670. /*if (cur_fit_nums<3)
  2671. {
  2672. fit_ret = CalcFittingData(ret,fit_pos);
  2673. }
  2674. else
  2675. {
  2676. fit_ret = CalcLongFittingData(ret,fit_pos);
  2677. }*/
  2678. //存在如下情况,解是个错误解
  2679. if (ret && fit_ret == 0)
  2680. {
  2681. //如果加速度和速度失败了或选解选不出来,而且有拟合数据了
  2682. //则直接使用拟合解
  2683. pos->posx = fit_pos->posx;
  2684. pos->posy = fit_pos->posy;
  2685. pos->cx = fit_pos->cx;
  2686. pos->cy = fit_pos->cy;
  2687. pos->cvx = fit_pos->cvx;
  2688. pos->cvy = fit_pos->cvy;
  2689. pos->is_fit = fit_pos->is_fit;
  2690. //如果是拟合出来的点就不能作为参考点
  2691. is_ref_pos = !pos->is_fit;
  2692. }else{
  2693. if (fit_ret == 0)
  2694. {
  2695. //如果有拟合数据
  2696. //如果选出了一个解,需要判断此解和预测值得距离
  2697. double distance = 0.0;
  2698. distance = sqrt(pow(pos->posx - fit_pos->posx,2) + pow(pos->posy - fit_pos->posy,2));
  2699. if (distance > 10)
  2700. {
  2701. //如果定位出的解和拟合解的距离差大于10,则认为拟合解可信
  2702. pos->posx = fit_pos->posx;
  2703. pos->posy = fit_pos->posy;
  2704. pos->cx = fit_pos->cx;
  2705. pos->cy = fit_pos->cy;
  2706. pos->cvx = fit_pos->cvx;
  2707. pos->cvy = fit_pos->cvy;
  2708. pos->is_fit = fit_pos->is_fit;
  2709. //如果是拟合出来的点就不能作为参考点
  2710. is_ref_pos = !pos->is_fit;
  2711. }
  2712. }
  2713. if (fit_ret && ret == 0)
  2714. {
  2715. //表示没有拟合值,但解是可信的,则直接返回0
  2716. return 0;
  2717. }
  2718. }
  2719. return fit_ret;
  2720. }
  2721. int Card::CheckSolutionBySpeed(std::shared_ptr<POS>& pos)
  2722. {
  2723. if (pos->posx == INVALID_COORDINATE && pos->posy == INVALID_COORDINATE)
  2724. {
  2725. return 1;
  2726. }
  2727. //double speed = sqrt(pow(pos->cvx,2) + pow(pos->cvy,2));
  2728. if (m_nLastLocateT != 0)
  2729. {
  2730. //如果第一次定位不做速度的判别
  2731. //double speed = sqrt(pow(pos->cx - last_locate.x,2) + pow(pos->cy - last_locate.y,2));
  2732. double speed = sqrt(pow(pos->cvx,2) + pow(pos->cvy,2));
  2733. speed *= 3.6; //转为km/h
  2734. if (speed > MAX_VECHILE_SPEED)
  2735. {
  2736. return 1;
  2737. }
  2738. }
  2739. return 0;
  2740. }
  2741. //根据点的信息获得格子信息
  2742. std::shared_ptr<Cell> Card::Position2Cell(std::shared_ptr<POS> pos)
  2743. {
  2744. if (mpCellPath == nullptr)
  2745. {
  2746. return nullptr;
  2747. }
  2748. std::shared_ptr<Cell> cell = nullptr;
  2749. //获得格子列表
  2750. std::string name1 = CFunctions::getCellName(pos->nFirstReader,pos->nSecondReader);
  2751. std::string name2 = CFunctions::getCellName(pos->nSecondReader,pos->nFirstReader);
  2752. int nIdx = 0;
  2753. CellPathMap::iterator it = mpCellPath->find(name1);
  2754. if (it == mpCellPath->end())
  2755. {
  2756. it = mpCellPath->find(name2);
  2757. if (it == mpCellPath->end())
  2758. {
  2759. return nullptr;
  2760. }else{
  2761. nIdx = 2;
  2762. }
  2763. }else{
  2764. nIdx = 1;
  2765. }
  2766. //其他方式,判断点距离某个分站的距离,使用距离除以间隔即可
  2767. std::list<std::shared_ptr<Cell>>::iterator it_cell = it->second.end();
  2768. for (it_cell = it->second.begin();it_cell != it->second.end();++it_cell)
  2769. {
  2770. //获得cell,判断点是否在cell内部
  2771. point p;
  2772. p.x = pos->posx;
  2773. p.y = pos->posy;
  2774. if (LocateAlgorithm::PointIsInRect(p,(*it_cell)->top,(*it_cell)->left,(*it_cell)->bottom,(*it_cell)->right))
  2775. {
  2776. cell = (*it_cell);
  2777. switch (nIdx)
  2778. {
  2779. case 1:
  2780. cell->strSubjectReader = name1;
  2781. break;
  2782. case 2:
  2783. cell->strSubjectReader = name2;
  2784. break;
  2785. default:
  2786. break;
  2787. }
  2788. break;
  2789. }
  2790. }
  2791. if (cell)
  2792. {
  2793. if (cell->strSubjectReader == "")
  2794. {
  2795. return nullptr;
  2796. }
  2797. }else{
  2798. return nullptr;
  2799. }
  2800. return cell;
  2801. }
  2802. //把中心点的坐标赋值给pos
  2803. std::shared_ptr<POS> Card::Cell2Position(std::shared_ptr<Cell>& cell)
  2804. {
  2805. if (cell == nullptr)
  2806. {
  2807. return nullptr;
  2808. }
  2809. std::shared_ptr<POS> pos = std::make_shared<POS>();
  2810. //把cell的中心点的坐标给cell
  2811. pos->posx = (cell->top.x + cell->bottom.x)/2;
  2812. pos->posy = (cell->top.y + cell->bottom.y)/2;
  2813. pos->cx = pos->posx/map_scale;
  2814. pos->cy = pos->posy/map_scale;
  2815. pos->update = true;
  2816. pos->is_fit = cell->isFit;
  2817. return pos;
  2818. }
  2819. int Card::PersonLocation(int cnt)
  2820. {
  2821. int ret = 0;
  2822. std::shared_ptr<Cell> cell = nullptr;
  2823. std::shared_ptr<Cell> tmp_cell = std::make_shared<Cell>();
  2824. is_fit_pos = false;
  2825. TRACE(_T("+ ct : %d \r\n"),time_stamp_cal);
  2826. //1.数据有效性判断
  2827. ret = CheckDistData(cnt);
  2828. if(ret){
  2829. //time_stamp_cal== 14931 14878
  2830. originCellId = 0;
  2831. originCellReaderName = "";
  2832. return 1;
  2833. }else{
  2834. //2.组装数据
  2835. std::shared_ptr<ReceiveDataMap> pRdm = std::make_shared<ReceiveDataMap>();
  2836. pRdm->clear();
  2837. ret = AssembleDistData(pRdm);
  2838. if(ret){
  2839. return 1;
  2840. }else{
  2841. if (pRdm->size()<=0)
  2842. {
  2843. originCellId = 0;
  2844. originCellReaderName = "";
  2845. return 1;
  2846. }
  2847. TRACE(_T("++ ct : %d \r\n"),time_stamp_cal);
  2848. //time_stamp_cal==12298
  2849. std::shared_ptr<POS> pos = std::make_shared<POS>();
  2850. //3.算法定位
  2851. std::vector<std::shared_ptr<POS>> udm_pos;
  2852. udm_pos.resize(0);
  2853. ret = LocateAlgorithm::CalcTdoaPosition(pRdm,pTdoaReaderPathMap,udm_pos);
  2854. //4.从多解中筛选出一个解,存在两种可能:
  2855. //a.可能无解返回非0,
  2856. //b.可能有解,但解不正确,比如解的位置在4727,-100,但选出的解是4727,-200
  2857. ret = ChooseOneSolution(pRdm, udm_pos, pos);
  2858. SaveOriginDataBeforeFilter(pos);
  2859. if (ret)
  2860. {
  2861. //根据上一次数据计算格子数,并根据上一次方向进行偏移
  2862. originCellId = 0;
  2863. originCellReaderName = "";
  2864. return 1;
  2865. }else{
  2866. std::shared_ptr<POS> tmp_pos = std::make_shared<POS>();
  2867. //获取分站信息
  2868. bool bRet = false;
  2869. bRet = LocateAlgorithm::IsOnMap(pos,pTdoaReaderPathMap);
  2870. if (!bRet)
  2871. {
  2872. return 1;
  2873. }
  2874. *tmp_pos = *pos;
  2875. if (ret == 0)
  2876. {
  2877. //人卡数据处理20170705
  2878. //time_stamp_cal== 2635
  2879. cell = Position2Cell(tmp_pos);//哈哈哈哈哈哈哈哈哈
  2880. if (cell && cell->id != 0)
  2881. {
  2882. if(cell->id == 184)
  2883. {
  2884. TRACE(_T("cell->id == 184"));
  2885. }
  2886. originCellId = cell->id;
  2887. originCellReaderName = cell->strSubjectReader;
  2888. cell->card_stamp_time = time_stamp_cal;
  2889. ::GetLocalTime(&cell->deal_time);
  2890. //保存格子到单元格,用于计算怠速判断
  2891. if (his_cell.size() > 100)
  2892. {
  2893. his_cell.pop_front();
  2894. }
  2895. his_cell.push_back(cell);//his_cell保存车辆怠速
  2896. if (nStartLocateCounts>=1&&his_cell.size() > 1)
  2897. {
  2898. //检查是否超限
  2899. isOverThreshold();//检查大跳
  2900. }
  2901. *tmp_cell = *cell;
  2902. nStartLocateCounts++;//统计原始定位次数
  2903. }else{
  2904. originCellId = 0;
  2905. originCellReaderName = "";
  2906. }
  2907. if (!CheckStartRule())//检查启动规则
  2908. {
  2909. return 1;
  2910. }
  2911. if (last_cell && cell)//cell是本次定位 last_cell 是下次定位
  2912. {
  2913. //20170704只判断怠速
  2914. int nCounts = 0;
  2915. if (cell->strSubjectReader == last_cell->strSubjectReader)
  2916. {
  2917. nCounts = ceil(cell->id - last_cell->id);//cell->id - last_cell->id两次格子的步进 ceil向上取整
  2918. }
  2919. else{
  2920. double distance = sqrt(pow(tmp_pos->cx - this->x,2) + pow(tmp_pos->cy - this->y,2)) * map_scale;
  2921. nCounts = distance / CELL_WIDTH;
  2922. }
  2923. //允许调整按ct号的最大格子数
  2924. int diff_ct = abs(cell->card_stamp_time - last_cell->card_stamp_time);
  2925. int thre_value ;
  2926. if(CT_VEHICLE == this->card_type )
  2927. {
  2928. thre_value = MAX_CELL_FORWARD_JUMP;//各自固定长度
  2929. }
  2930. else if(CT_PERSON == this->card_type )
  2931. {
  2932. thre_value = 3;//各自固定长度
  2933. }
  2934. if (diff_ct > 0)
  2935. {
  2936. if(CT_VEHICLE == this->card_type)
  2937. {
  2938. thre_value = MAX_CELL_FORWARD_JUMP*diff_ct;
  2939. }
  2940. else if(CT_PERSON == this->card_type )
  2941. {
  2942. thre_value = 3*diff_ct;
  2943. }
  2944. }
  2945. //怠速保留 0704实现
  2946. //越界,向前大跳或向后大跳//人卡01
  2947. //有对应,越界()
  2948. if (nCounts >= thre_value)//步进大于阈值 //两次成功定位的数据处理好后竟然大于阈值 //向前/后大跳
  2949. {
  2950. isIdling = false;
  2951. if (this->nIncrease != 0)//方向 =0是没有方向
  2952. {
  2953. int nCalcStep = 0;
  2954. if (last_cell->nStep < 1)//上个格子距离上上个格子的步进
  2955. {
  2956. nCalcStep = 1;
  2957. }else{
  2958. nCalcStep = last_cell->nStep;//匀速 ----大跳按匀速处理
  2959. }
  2960. cell = GetNextCell(last_cell,this->nIncrease,nCalcStep);//本次的格子
  2961. if (cell)
  2962. {
  2963. cell->isFit = true;
  2964. }
  2965. }
  2966. }else if (!CheckIdleStatus())//检查是否怠速
  2967. {
  2968. isIdling = false;
  2969. //非怠速
  2970. }else{
  2971. //怠速
  2972. isIdling = true;
  2973. acce_cur_state = STATE_ACCE_IDLING;
  2974. //如果怠速了,则直接取上一次的结果
  2975. if (last_cell)
  2976. {
  2977. cell = last_cell;
  2978. }
  2979. }
  2980. //1.越界:定位id相对于上一个实际id超过15格以上,直接根据上一个v来设置实际id
  2981. //2.加速:定位id速度高于上一个实际的id的v,采取lv*a+cv*b
  2982. //3.减速:定位id速度高于上一个实际的id的v,采取lv*a+cv*b
  2983. //4.匀速:匀速前进
  2984. //5.回跳:定位id显示回跳,5次以内,保持匀速前进或减速前进,5次掉头开始掉头
  2985. //6.怠速:
  2986. //7.静止:
  2987. //越界
  2988. //double distance = sqrt(pow(tmp_pos->cx - this->x,2) + pow(tmp_pos->cy - this->y,2)) * map_scale;
  2989. //int nCounts = ceil(distance / CELL_WIDTH);
  2990. //
  2991. ////允许调整按ct号的最大格子数
  2992. //int diff_ct = abs(cell->card_stamp_time - last_cell->card_stamp_time);
  2993. //int thre_value = MAX_CELL_FORWARD_JUMP;
  2994. //if (diff_ct > 0)
  2995. //{
  2996. // thre_value = MAX_CELL_FORWARD_JUMP*diff_ct;
  2997. //}
  2998. ////怠速保留 0704实现
  2999. ////越界,向前大跳或向后大跳
  3000. //if (nCounts >= thre_value)
  3001. //{
  3002. // if (isRebound(tmp_pos))
  3003. // {
  3004. // //如果回退,则是向后大跳
  3005. // //如果5次以内回退
  3006. // if (change_cell_dir < MAX_CHANGE_DIR_COUNTS)
  3007. // {
  3008. // int calcStep = 0;
  3009. // if (last_cell)
  3010. // {
  3011. // //在不知下一时刻如何走的情况下,不做减速,
  3012. // nCounts = last_cell->nStep;
  3013. // //20170703 17:03 根据本次定位格子id和上一次格子id的格子差值取半减速
  3014. // //calcStep = abs((originCellId - last_cell->id)/2);
  3015. // }
  3016. // if (abs(calcStep) < 1E-2)
  3017. // {
  3018. // calcStep = 1;
  3019. // }
  3020. // if (this->nIncrease != 0)
  3021. // {
  3022. // cell = GetNextCell(last_cell,this->nIncrease,calcStep);
  3023. // if (cell)
  3024. // {
  3025. // cell->isFit = true;
  3026. // }
  3027. // }
  3028. // change_cell_dir++;
  3029. // }else{
  3030. // change_cell_dir = 0;
  3031. // }
  3032. // }
  3033. // else
  3034. // {
  3035. // //否则是向前大跳
  3036. // //按上一次速度做匀速运动
  3037. // if (last_cell)
  3038. // {
  3039. // nCounts = last_cell->nStep;
  3040. // }
  3041. // if (abs(nCounts) < 1E-2)
  3042. // {
  3043. // nCounts = 1;
  3044. // }
  3045. // if (this->nIncrease != 0)
  3046. // {
  3047. // cell = GetNextCell(last_cell,this->nIncrease,nCounts);
  3048. // //cell = GetNextCell(last_cell,this->nIncrease,nCounts,originCellReaderName);
  3049. // //cell = GetNextCell(last_cell,this->nIncrease,nCounts,tmp_cell);
  3050. // if (cell)
  3051. // {
  3052. // cell->isFit = true;
  3053. // }
  3054. // }
  3055. // }
  3056. //}else if (!CheckIdleStatus())
  3057. //{
  3058. // //如果不是怠速
  3059. // //if (isRebound(tmp_pos))
  3060. // if (isRebound(cell))
  3061. // {
  3062. // //如果5次以内回退
  3063. // if (change_cell_dir < MAX_CHANGE_DIR_COUNTS)
  3064. // {
  3065. // int nCounts = 0;
  3066. // if (last_cell)
  3067. // {
  3068. // //在不知下一时刻如何走的情况下,不做减速,
  3069. // nCounts = last_cell->nStep;
  3070. // //nCounts = ceil(last_cell->nStep/2); //20170703 16:46 做取半减速
  3071. // }
  3072. // if (abs(nCounts) < 1E-2)
  3073. // {
  3074. // nCounts = 1;
  3075. // }
  3076. // if (this->nIncrease != 0)
  3077. // {
  3078. // //cell = GetNextCell(last_cell,this->nIncrease,nCounts,originCellReaderName);
  3079. // cell = GetNextCell(last_cell,this->nIncrease,nCounts);
  3080. // if (cell)
  3081. // {
  3082. // cell->isFit = true;
  3083. // }
  3084. // }
  3085. // change_cell_dir++;
  3086. // }else{
  3087. // change_cell_dir = 0;
  3088. // }
  3089. // }else{
  3090. // //不回退,判断是否大跳
  3091. // //判断是否加速、减速、匀速
  3092. // //如果权值相同,以下三个可以使用同一逻辑处理
  3093. // //int calcStep = 0;
  3094. // //if (nCounts > last_cell->nStep)
  3095. // //{
  3096. // // //加速,权值可调
  3097. // // int step = ceil(THIS_V_POWER * last_cell->nStep + LAST_V_POWER * nCounts);
  3098. // // //2017/07/02新增
  3099. // // switch (this->nIncrease)
  3100. // // {
  3101. // // case 1:
  3102. // // //如果步进后的格子id会超过原始定位格子id,
  3103. // // //则取原始定位格子和上一次定位格子的差
  3104. // // if (step + last_cell->id >= originCellId)
  3105. // // {
  3106. // // calcStep = abs(last_cell->id - originCellId);
  3107. // // }
  3108. // // else
  3109. // // {
  3110. // // calcStep = step;
  3111. // // }
  3112. // // break;
  3113. // // case -1:
  3114. // // if (last_cell->id - step >= originCellId)
  3115. // // {
  3116. // // calcStep = step;
  3117. // // }
  3118. // // else
  3119. // // {
  3120. // // calcStep = abs(last_cell->id - originCellId);
  3121. // // }
  3122. // // break;
  3123. // // }
  3124. // // if (abs(calcStep) < 1E-2)
  3125. // // {
  3126. // // calcStep = 1;
  3127. // // }
  3128. // // if (this->nIncrease != 0)
  3129. // // {
  3130. // // cell = GetNextCell(last_cell,this->nIncrease,calcStep,tmp_cell);
  3131. // // if (cell)
  3132. // // {
  3133. // // cell->isFit = true;
  3134. // // }
  3135. // // }
  3136. // //}
  3137. // //else if (nCounts < last_cell->nStep)
  3138. // //{
  3139. // // //减速,权值可调
  3140. // // //采取向下取整,并考虑方向性
  3141. // // int step = 0;
  3142. // // if (last_cell)
  3143. // // {
  3144. // // step = originCellId - last_cell->id;
  3145. // // }
  3146. // // calcStep = ceil(THIS_V_POWER * last_cell->nStep + LAST_V_POWER * nCounts);
  3147. // // if (abs(calcStep) < 1E-2)
  3148. // // {
  3149. // // calcStep = 1;
  3150. // // }
  3151. // // if (this->nIncrease != 0)
  3152. // // {
  3153. // // cell = GetNextCell(last_cell,this->nIncrease,calcStep,tmp_cell);
  3154. // // if (cell)
  3155. // // {
  3156. // // cell->isFit = true;
  3157. // // }
  3158. // // }
  3159. // //}else
  3160. // //{
  3161. // // if (last_cell->id > tmp_cell->id && this->nIncrease == -1)
  3162. // // {
  3163. // // calcStep = ceil(abs(last_cell->id - tmp_cell->id)/2);
  3164. // // }
  3165. // // //匀速运动,权值可调
  3166. // // if (this->nIncrease != 0)
  3167. // // {
  3168. // // cell = GetNextCell(last_cell,this->nIncrease,calcStep,tmp_cell);
  3169. // // if (cell)
  3170. // // {
  3171. // // cell->isFit = true;
  3172. // // }
  3173. // // }
  3174. // //}
  3175. // }
  3176. //}else{
  3177. // acce_cur_state = STATE_ACCE_IDLING;
  3178. // //如果怠速了,则直接取上一次的结果
  3179. // if (last_cell)
  3180. // {
  3181. // cell = last_cell;
  3182. // }
  3183. //}
  3184. }
  3185. }
  3186. }
  3187. }
  3188. }
  3189. if (cell)
  3190. {
  3191. SaveCardAlgoData(cell);
  3192. }
  3193. return ret;
  3194. }
  3195. int Card::DiscreteLocate(int cnt)
  3196. {
  3197. ULONGLONG nowTickCounts = ::GetTickCount();
  3198. //如果last_cell存在,那说明不是第一次定位,如果不存在,那么继续往下走。
  3199. if (last_cell)
  3200. {
  3201. SYSTEMTIME curSt;
  3202. ::GetLocalTime(&curSt);
  3203. int ret = 0;
  3204. ret = CFunctions::CompareSystemTime(curSt,last_cell->interval_time);
  3205. if (ret < 0)
  3206. {
  3207. return 1;
  3208. }
  3209. }
  3210. int ret = 0;
  3211. std::shared_ptr<Cell> cell = nullptr;
  3212. std::shared_ptr<Cell> tmp_cell = std::make_shared<Cell>();
  3213. is_fit_pos = false;
  3214. //1.数据有效性判断
  3215. ret = CheckDistData(cnt);
  3216. if(ret){
  3217. //time_stamp_cal== 14931 14878
  3218. originCellId = 0;
  3219. originCellReaderName = "";
  3220. return 1;
  3221. }else{
  3222. //2.组装数据
  3223. std::shared_ptr<ReceiveDataMap> pRdm = std::make_shared<ReceiveDataMap>();
  3224. pRdm->clear();
  3225. ret = AssembleDistData(pRdm);
  3226. if(ret){
  3227. return 1;
  3228. }else{
  3229. if (pRdm->size()<=0)
  3230. {
  3231. originCellId = 0;
  3232. originCellReaderName = "";
  3233. return 1;
  3234. }
  3235. //time_stamp_cal==12298
  3236. std::shared_ptr<POS> pos = std::make_shared<POS>();
  3237. //3.算法定位
  3238. std::vector<std::shared_ptr<POS>> udm_pos;
  3239. udm_pos.resize(0);
  3240. ret = LocateAlgorithm::CalcTdoaPosition(pRdm,pTdoaReaderPathMap,udm_pos);
  3241. //4.从多解中筛选出一个解,存在两种可能:
  3242. //a.可能无解返回非0,
  3243. //b.可能有解,但解不正确,比如解的位置在4727,-100,但选出的解是4727,-200
  3244. ret = ChooseOneSolution(pRdm, udm_pos, pos);
  3245. SaveOriginDataBeforeFilter(pos);
  3246. if (ret)
  3247. {
  3248. //根据上一次数据计算格子数,并根据上一次方向进行偏移
  3249. originCellId = 0;
  3250. originCellReaderName = "";
  3251. return 1;
  3252. }else{
  3253. std::shared_ptr<POS> tmp_pos = std::make_shared<POS>();
  3254. //获取分站信息
  3255. bool bRet = false;
  3256. bRet = LocateAlgorithm::IsOnMap(pos,pTdoaReaderPathMap);
  3257. if (!bRet)
  3258. {
  3259. return 1;
  3260. }
  3261. *tmp_pos = *pos;
  3262. if (ret == 0)
  3263. {
  3264. //车辆数据处理20170705
  3265. //time_stamp_cal== 3954
  3266. cell = Position2Cell(tmp_pos);
  3267. if (cell && cell->id != 0)
  3268. {
  3269. originCellId = cell->id;
  3270. originCellReaderName = cell->strSubjectReader;
  3271. cell->card_stamp_time = time_stamp_cal;
  3272. //获取处理格子的系统时间
  3273. ::GetLocalTime(&cell->deal_time);
  3274. //保存卡的格子处理时间
  3275. ::GetLocalTime(&cellDealTime);
  3276. //保存格子到单元格,用于计算怠速判断
  3277. if (his_cell.size() > 100)
  3278. {
  3279. his_cell.pop_front();
  3280. }
  3281. his_cell.push_back(cell);
  3282. if (nStartLocateCounts>=1&&his_cell.size() > 1)
  3283. {
  3284. //检查是否超限
  3285. isOverThreshold();
  3286. if (card_id == "0020000001043" && nStartLocateCounts ==5)
  3287. {
  3288. int a = 0;
  3289. int b = 1;
  3290. a = b;
  3291. }
  3292. }
  3293. *tmp_cell = *cell;
  3294. nStartLocateCounts++;
  3295. }else{
  3296. originCellId = 0;
  3297. originCellReaderName = "";
  3298. }
  3299. if (!CheckStartRule())
  3300. {
  3301. return 1;
  3302. }
  3303. if (nStartLocateCounts == 5)
  3304. {
  3305. int a = 0;
  3306. int b = 1;
  3307. a = b;
  3308. }
  3309. if (last_cell && cell)
  3310. {
  3311. //只判断怠速
  3312. int nCounts = 0;
  3313. if (cell->strSubjectReader == last_cell->strSubjectReader)
  3314. {
  3315. nCounts = ceil(cell->id - last_cell->id);
  3316. }
  3317. else{
  3318. double distance = sqrt(pow(tmp_pos->cx - this->x,2) + pow(tmp_pos->cy - this->y,2)) * map_scale;
  3319. nCounts = distance / CELL_WIDTH;
  3320. }
  3321. //允许调整按ct号的最大格子数
  3322. int diff_ct = abs(cell->card_stamp_time - last_cell->card_stamp_time);
  3323. int thre_value = MAX_CELL_FORWARD_JUMP;
  3324. if (diff_ct > 0)
  3325. {
  3326. thre_value = MAX_CELL_FORWARD_JUMP*diff_ct;
  3327. }
  3328. //怠速保留 0704实现
  3329. //越界,向前大跳或向后大跳
  3330. if (nCounts >= thre_value)
  3331. {
  3332. isIdling = false;
  3333. if (this->nIncrease != 0)
  3334. {
  3335. int nCalcStep = 0;
  3336. if (last_cell->nStep < 1)
  3337. {
  3338. nCalcStep = 1;
  3339. }else{
  3340. nCalcStep = last_cell->nStep;
  3341. }
  3342. cell = GetNextCell(last_cell,this->nIncrease,nCalcStep);
  3343. if (cell)
  3344. {
  3345. cell->isFit = true;
  3346. }
  3347. }
  3348. }else if (!CheckIdleStatus())
  3349. {
  3350. isIdling = false;
  3351. //非怠速
  3352. }else{
  3353. isIdling = true;
  3354. acce_cur_state = STATE_ACCE_IDLING;
  3355. //如果怠速了,则直接取上一次的结果
  3356. if (last_cell)
  3357. {
  3358. cell = last_cell;
  3359. }
  3360. }
  3361. //1.越界:定位id相对于上一个实际id超过15格以上,直接根据上一个v来设置实际id
  3362. //2.加速:定位id速度高于上一个实际的id的v,采取lv*a+cv*b
  3363. //3.减速:定位id速度高于上一个实际的id的v,采取lv*a+cv*b
  3364. //4.匀速:匀速前进
  3365. //5.回跳:定位id显示回跳,5次以内,保持匀速前进或减速前进,5次掉头开始掉头
  3366. //6.怠速:
  3367. //7.静止:
  3368. //越界
  3369. //double distance = sqrt(pow(tmp_pos->cx - this->x,2) + pow(tmp_pos->cy - this->y,2)) * map_scale;
  3370. //int nCounts = ceil(distance / CELL_WIDTH);
  3371. //
  3372. ////允许调整按ct号的最大格子数
  3373. //int diff_ct = abs(cell->card_stamp_time - last_cell->card_stamp_time);
  3374. //int thre_value = MAX_CELL_FORWARD_JUMP;
  3375. //if (diff_ct > 0)
  3376. //{
  3377. // thre_value = MAX_CELL_FORWARD_JUMP*diff_ct;
  3378. //}
  3379. ////怠速保留 0704实现
  3380. ////越界,向前大跳或向后大跳
  3381. //if (nCounts >= thre_value)
  3382. //{
  3383. // if (isRebound(tmp_pos))
  3384. // {
  3385. // //如果回退,则是向后大跳
  3386. // //如果5次以内回退
  3387. // if (change_cell_dir < MAX_CHANGE_DIR_COUNTS)
  3388. // {
  3389. // int calcStep = 0;
  3390. // if (last_cell)
  3391. // {
  3392. // //在不知下一时刻如何走的情况下,不做减速,
  3393. // nCounts = last_cell->nStep;
  3394. // //20170703 17:03 根据本次定位格子id和上一次格子id的格子差值取半减速
  3395. // //calcStep = abs((originCellId - last_cell->id)/2);
  3396. // }
  3397. // if (abs(calcStep) < 1E-2)
  3398. // {
  3399. // calcStep = 1;
  3400. // }
  3401. // if (this->nIncrease != 0)
  3402. // {
  3403. // cell = GetNextCell(last_cell,this->nIncrease,calcStep);
  3404. // if (cell)
  3405. // {
  3406. // cell->isFit = true;
  3407. // }
  3408. // }
  3409. // change_cell_dir++;
  3410. // }else{
  3411. // change_cell_dir = 0;
  3412. // }
  3413. // }
  3414. // else
  3415. // {
  3416. // //否则是向前大跳
  3417. // //按上一次速度做匀速运动
  3418. // if (last_cell)
  3419. // {
  3420. // nCounts = last_cell->nStep;
  3421. // }
  3422. // if (abs(nCounts) < 1E-2)
  3423. // {
  3424. // nCounts = 1;
  3425. // }
  3426. // if (this->nIncrease != 0)
  3427. // {
  3428. // cell = GetNextCell(last_cell,this->nIncrease,nCounts);
  3429. // //cell = GetNextCell(last_cell,this->nIncrease,nCounts,originCellReaderName);
  3430. // //cell = GetNextCell(last_cell,this->nIncrease,nCounts,tmp_cell);
  3431. // if (cell)
  3432. // {
  3433. // cell->isFit = true;
  3434. // }
  3435. // }
  3436. // }
  3437. //}else if (!CheckIdleStatus())
  3438. //{
  3439. // //如果不是怠速
  3440. // //if (isRebound(tmp_pos))
  3441. // if (isRebound(cell))
  3442. // {
  3443. // //如果5次以内回退
  3444. // if (change_cell_dir < MAX_CHANGE_DIR_COUNTS)
  3445. // {
  3446. // int nCounts = 0;
  3447. // if (last_cell)
  3448. // {
  3449. // //在不知下一时刻如何走的情况下,不做减速,
  3450. // nCounts = last_cell->nStep;
  3451. // //nCounts = ceil(last_cell->nStep/2); //20170703 16:46 做取半减速
  3452. // }
  3453. // if (abs(nCounts) < 1E-2)
  3454. // {
  3455. // nCounts = 1;
  3456. // }
  3457. // if (this->nIncrease != 0)
  3458. // {
  3459. // //cell = GetNextCell(last_cell,this->nIncrease,nCounts,originCellReaderName);
  3460. // cell = GetNextCell(last_cell,this->nIncrease,nCounts);
  3461. // if (cell)
  3462. // {
  3463. // cell->isFit = true;
  3464. // }
  3465. // }
  3466. // change_cell_dir++;
  3467. // }else{
  3468. // change_cell_dir = 0;
  3469. // }
  3470. // }else{
  3471. // //不回退,判断是否大跳
  3472. // //判断是否加速、减速、匀速
  3473. // //如果权值相同,以下三个可以使用同一逻辑处理
  3474. // //int calcStep = 0;
  3475. // //if (nCounts > last_cell->nStep)
  3476. // //{
  3477. // // //加速,权值可调
  3478. // // int step = ceil(THIS_V_POWER * last_cell->nStep + LAST_V_POWER * nCounts);
  3479. // // //2017/07/02新增
  3480. // // switch (this->nIncrease)
  3481. // // {
  3482. // // case 1:
  3483. // // //如果步进后的格子id会超过原始定位格子id,
  3484. // // //则取原始定位格子和上一次定位格子的差
  3485. // // if (step + last_cell->id >= originCellId)
  3486. // // {
  3487. // // calcStep = abs(last_cell->id - originCellId);
  3488. // // }
  3489. // // else
  3490. // // {
  3491. // // calcStep = step;
  3492. // // }
  3493. // // break;
  3494. // // case -1:
  3495. // // if (last_cell->id - step >= originCellId)
  3496. // // {
  3497. // // calcStep = step;
  3498. // // }
  3499. // // else
  3500. // // {
  3501. // // calcStep = abs(last_cell->id - originCellId);
  3502. // // }
  3503. // // break;
  3504. // // }
  3505. // // if (abs(calcStep) < 1E-2)
  3506. // // {
  3507. // // calcStep = 1;
  3508. // // }
  3509. // // if (this->nIncrease != 0)
  3510. // // {
  3511. // // cell = GetNextCell(last_cell,this->nIncrease,calcStep,tmp_cell);
  3512. // // if (cell)
  3513. // // {
  3514. // // cell->isFit = true;
  3515. // // }
  3516. // // }
  3517. // //}
  3518. // //else if (nCounts < last_cell->nStep)
  3519. // //{
  3520. // // //减速,权值可调
  3521. // // //采取向下取整,并考虑方向性
  3522. // // int step = 0;
  3523. // // if (last_cell)
  3524. // // {
  3525. // // step = originCellId - last_cell->id;
  3526. // // }
  3527. // // calcStep = ceil(THIS_V_POWER * last_cell->nStep + LAST_V_POWER * nCounts);
  3528. // // if (abs(calcStep) < 1E-2)
  3529. // // {
  3530. // // calcStep = 1;
  3531. // // }
  3532. // // if (this->nIncrease != 0)
  3533. // // {
  3534. // // cell = GetNextCell(last_cell,this->nIncrease,calcStep,tmp_cell);
  3535. // // if (cell)
  3536. // // {
  3537. // // cell->isFit = true;
  3538. // // }
  3539. // // }
  3540. // //}else
  3541. // //{
  3542. // // if (last_cell->id > tmp_cell->id && this->nIncrease == -1)
  3543. // // {
  3544. // // calcStep = ceil(abs(last_cell->id - tmp_cell->id)/2);
  3545. // // }
  3546. // // //匀速运动,权值可调
  3547. // // if (this->nIncrease != 0)
  3548. // // {
  3549. // // cell = GetNextCell(last_cell,this->nIncrease,calcStep,tmp_cell);
  3550. // // if (cell)
  3551. // // {
  3552. // // cell->isFit = true;
  3553. // // }
  3554. // // }
  3555. // //}
  3556. // }
  3557. //}else{
  3558. // acce_cur_state = STATE_ACCE_IDLING;
  3559. // //如果怠速了,则直接取上一次的结果
  3560. // if (last_cell)
  3561. // {
  3562. // cell = last_cell;
  3563. // }
  3564. //}
  3565. }
  3566. }
  3567. }
  3568. }
  3569. }
  3570. if (cell)
  3571. {
  3572. SaveCardAlgoData(cell);
  3573. }
  3574. return ret;
  3575. }
  3576. std::shared_ptr<Cell> Card::GetNextCell(std::shared_ptr<Cell> lastCell,int increase,int nIndex,std::shared_ptr<Cell> newCell)
  3577. {
  3578. if (lastCell->id == 0)
  3579. {
  3580. return nullptr;
  3581. }
  3582. std::shared_ptr<Cell> cell = std::make_shared<Cell>();
  3583. int nFindId = 0;
  3584. bool bFind = false;
  3585. std::string findReaderName = "";
  3586. if (last_cell && newCell == nullptr)
  3587. {
  3588. //处理500ms定时器的处理
  3589. int nNewCellId = 0;
  3590. findReaderName = lastCell->strSubjectReader;
  3591. if (findReaderName == "")
  3592. {
  3593. return nullptr;
  3594. }
  3595. switch (increase)
  3596. {
  3597. case 1:
  3598. nNewCellId = lastCell->id + nIndex;
  3599. if (nNewCellId > mpCellPath->find(lastCell->strSubjectReader)->second.size())
  3600. {
  3601. nFindId = mpCellPath->find(lastCell->strSubjectReader)->second.size();
  3602. cell->isChangePath = false;
  3603. }else{
  3604. nFindId = nNewCellId;
  3605. }
  3606. break;
  3607. case -1:
  3608. nNewCellId = lastCell->id - nIndex;
  3609. if (nNewCellId < 0)
  3610. {
  3611. nFindId = 1;
  3612. cell->isChangePath = false;
  3613. }else{
  3614. nFindId = nNewCellId;
  3615. }
  3616. break;
  3617. default:
  3618. break;
  3619. }
  3620. if (findReaderName == "")
  3621. {
  3622. return nullptr;
  3623. }
  3624. for (std::list<std::shared_ptr<Cell>>::iterator it = mpCellPath->find(findReaderName)->second.begin();it != mpCellPath->find(findReaderName)->second.end();++it)
  3625. {
  3626. if ((*it)->id == nFindId)
  3627. {
  3628. cell->id = (*it)->id;
  3629. cell->top = (*it)->top;
  3630. cell->left = (*it)->left;
  3631. cell->bottom = (*it)->bottom;
  3632. cell->right = (*it)->right;
  3633. cell->isHasBranch = (*it)->isHasBranch;
  3634. cell->maxReaderId = (*it)->maxReaderId;
  3635. cell->minReaderId = (*it)->minReaderId;
  3636. break;
  3637. }
  3638. }
  3639. }else if (last_cell && newCell)
  3640. {
  3641. //处理两种情况,一种是分站内的格子移动,另一种是跨分站格子的移动
  3642. if (last_cell->strSubjectReader == newCell->strSubjectReader)
  3643. {
  3644. cell->isChangePath = false;
  3645. findReaderName = lastCell->strSubjectReader;
  3646. cell->strSubjectReader = findReaderName;
  3647. //分站内的格子移动
  3648. switch (increase)
  3649. {
  3650. case 1:
  3651. if (lastCell->id + nIndex > mpCellPath->find(lastCell->strSubjectReader)->second.size())
  3652. {
  3653. //存在跨分站的问题,先找到路径中的最大分站
  3654. //根据路径集中找到最大分站的列表,
  3655. nFindId = mpCellPath->find(lastCell->strSubjectReader)->second.size();
  3656. }else{
  3657. nFindId = lastCell->id + nIndex;
  3658. }
  3659. break;
  3660. case -1:
  3661. if (lastCell->id - nIndex < 0)
  3662. {
  3663. //nFindId = 1;
  3664. //存在跨分站的问题
  3665. //先找到路径中的最小分站
  3666. //根据路径集中找到最小分站的列表,
  3667. nFindId = 1;
  3668. }else{
  3669. nFindId = lastCell->id - nIndex;
  3670. }
  3671. break;
  3672. default:
  3673. cell = newCell;
  3674. break;
  3675. }
  3676. }else{
  3677. //另一种是跨分站格子的移动
  3678. cell->isChangePath = true;
  3679. findReaderName = newCell->strSubjectReader;
  3680. if (findReaderName != "")
  3681. {
  3682. cell->strSubjectReader = findReaderName;
  3683. }
  3684. int oldCellOffset = 0;
  3685. int newCellOffset = 0;
  3686. int CellOffset = 0;
  3687. if (lastCell->maxReaderId == newCell->maxReaderId)
  3688. {
  3689. oldCellOffset = mpCellPath->find(lastCell->strSubjectReader)->second.size() - lastCell->id;
  3690. newCellOffset = mpCellPath->find(newCell->strSubjectReader)->second.size() - newCell->id;
  3691. CellOffset = ceil(sqrt(pow(oldCellOffset,2)+pow(newCellOffset,2)));
  3692. nFindId = mpCellPath->find(newCell->strSubjectReader)->second.size() - abs(CellOffset - oldCellOffset);
  3693. if (nFindId == 0)
  3694. {
  3695. nFindId = mpCellPath->find(newCell->strSubjectReader)->second.size();
  3696. }
  3697. nIncrease = -1;
  3698. }else if (lastCell->maxReaderId == newCell->minReaderId)
  3699. {
  3700. oldCellOffset = mpCellPath->find(lastCell->strSubjectReader)->second.size() - lastCell->id;
  3701. newCellOffset = newCell->id;
  3702. CellOffset = ceil(sqrt(pow(oldCellOffset,2) + pow(newCellOffset,2)));
  3703. nFindId = abs(CellOffset - oldCellOffset);
  3704. if (nFindId == 0)
  3705. {
  3706. nFindId = 1;
  3707. }
  3708. nIncrease = 1;
  3709. }else if (lastCell->minReaderId == newCell->maxReaderId)
  3710. {
  3711. oldCellOffset = lastCell->id;
  3712. newCellOffset = mpCellPath->find(newCell->strSubjectReader)->second.size() - newCell->id;
  3713. CellOffset = ceil(sqrt(pow(oldCellOffset,2)+pow(newCellOffset,2)));
  3714. nFindId = mpCellPath->find(newCell->strSubjectReader)->second.size() - abs(CellOffset - oldCellOffset);
  3715. if (nFindId == 0)
  3716. {
  3717. nFindId = mpCellPath->find(newCell->strSubjectReader)->second.size();
  3718. }
  3719. nIncrease = -1;
  3720. }else if (lastCell->minReaderId == newCell->minReaderId)
  3721. {
  3722. oldCellOffset = lastCell->id;
  3723. newCellOffset = newCell->id;
  3724. CellOffset = ceil(sqrt(pow(oldCellOffset,2)+pow(newCellOffset,2)));
  3725. nFindId = abs(CellOffset - oldCellOffset);
  3726. if (nFindId == 0)
  3727. {
  3728. nFindId = 1;
  3729. }
  3730. nIncrease = 1;
  3731. }else{
  3732. findReaderName = "";
  3733. }
  3734. }
  3735. if (findReaderName == "")
  3736. {
  3737. return nullptr;
  3738. }
  3739. for (std::list<std::shared_ptr<Cell>>::iterator it = mpCellPath->find(findReaderName)->second.begin();it != mpCellPath->find(findReaderName)->second.end();++it)
  3740. {
  3741. if ((*it)->id == nFindId)
  3742. {
  3743. cell->id = (*it)->id;
  3744. cell->top = (*it)->top;
  3745. cell->left = (*it)->left;
  3746. cell->bottom = (*it)->bottom;
  3747. cell->right = (*it)->right;
  3748. cell->isHasBranch = (*it)->isHasBranch;
  3749. cell->maxReaderId = (*it)->maxReaderId;
  3750. cell->minReaderId = (*it)->minReaderId;
  3751. break;
  3752. }
  3753. }
  3754. }
  3755. return cell;
  3756. }
  3757. std::shared_ptr<Cell> Card::GetNextCell(std::shared_ptr<Cell> lastCell,int increase,int nIndex)
  3758. {
  3759. if (lastCell == nullptr || lastCell->id == 0)
  3760. {
  3761. return nullptr;
  3762. }
  3763. std::shared_ptr<Cell> cell = std::make_shared<Cell>();
  3764. int step = 0 , nFindId = 0;
  3765. step = lastCell->id + nIndex*increase;
  3766. int nSize = 0;
  3767. nSize = mpCellPath->find(lastCell->strSubjectReader)->second.size();
  3768. if (step > nSize)
  3769. {
  3770. nFindId = nSize;
  3771. }else if (step < 0)
  3772. {
  3773. nFindId = 1;
  3774. }else{
  3775. nFindId = step;
  3776. }
  3777. bool bFind = false;
  3778. for (std::list<std::shared_ptr<Cell>>::iterator it = mpCellPath->find(lastCell->strSubjectReader)->second.begin();it != mpCellPath->find(lastCell->strSubjectReader)->second.end();++it)
  3779. {
  3780. if ((*it)->id == nFindId)
  3781. {
  3782. cell = *it;
  3783. cell->interval_time = lastCell->interval_time;
  3784. bFind = true;
  3785. break;
  3786. }
  3787. }
  3788. if (!bFind)
  3789. {
  3790. return nullptr;
  3791. }
  3792. return cell;
  3793. }
  3794. /*
  3795. * 如果怠速返回true,否则返回false
  3796. */
  3797. bool Card::CheckIdleStatus()
  3798. {
  3799. std::mutex mu;
  3800. std::lock_guard<mutex> lg(mu);
  3801. if (his_cell.size() < 5)
  3802. {
  3803. return false;
  3804. }
  3805. int flag = 0;
  3806. /*std::list<std::shared_ptr<Cell>>::iterator ref = his_cell.begin();
  3807. for (std::list<std::shared_ptr<Cell>>::iterator it = his_cell.begin();it != his_cell.end();++it)
  3808. {
  3809. if ((*it)->id != (*ref)->id)
  3810. {
  3811. flag = 1;
  3812. break;
  3813. }
  3814. }*/
  3815. int i = 0;
  3816. std::list<std::shared_ptr<Cell>>::reverse_iterator ref = his_cell.rbegin();
  3817. for (std::list<std::shared_ptr<Cell>>::reverse_iterator ref_it = his_cell.rbegin();ref_it != his_cell.rend();++ref_it)
  3818. {
  3819. if ((*ref)->id != (*ref_it)->id)
  3820. {
  3821. flag = 1;
  3822. break;
  3823. }
  3824. i++;
  3825. if (i == 4)
  3826. {
  3827. break;
  3828. }
  3829. }
  3830. if (flag == 0)
  3831. {
  3832. return true;
  3833. }
  3834. //拟合法
  3835. std::vector<int> vt,vc;
  3836. vt.resize(0);
  3837. vc.resize(0);
  3838. //反向取MAX_FIT_DATA_COUNTS个元素
  3839. double val[MAX_FIT_DATA_COUNTS] = {0};
  3840. i = MAX_FIT_DATA_COUNTS - 1;
  3841. for (std::list<std::shared_ptr<Cell>>::reverse_iterator it = his_cell.rbegin();it != his_cell.rend();++it)
  3842. {
  3843. if (!(*it)->isOverThre)//大跳数据不参与怠速判断
  3844. {
  3845. //如果非越界,则参与计算怠速判断,
  3846. val[i] = (*it)->id;
  3847. i--;
  3848. }
  3849. if (i < 0)
  3850. {
  3851. break;
  3852. }
  3853. }
  3854. int nAcs = 0;
  3855. int nDesc = 0;
  3856. //统计方向
  3857. for (int k = 0; k < MAX_FIT_DATA_COUNTS - 1;k++)
  3858. {
  3859. if (val[k] < val[k+1])
  3860. {
  3861. nAcs++;
  3862. }else{
  3863. nDesc++;
  3864. }
  3865. }
  3866. if (nAcs > nDesc)
  3867. {
  3868. nIncrease = 1;
  3869. }else if (nAcs < nDesc)
  3870. {
  3871. nIncrease = -1;
  3872. }else
  3873. {
  3874. //相等的话就保持原来的方向
  3875. }
  3876. if (card_id == "0020000001002")
  3877. {
  3878. TRACE(_T("nIncrease : %d \r\n"),nIncrease);
  3879. }
  3880. i = 0;
  3881. for (; i < MAX_FIT_DATA_COUNTS;i++)
  3882. {
  3883. vt.push_back(i+1);
  3884. vc.push_back(val[i]);
  3885. }
  3886. Fit fit;
  3887. fit.linearFit(vt,vc);
  3888. if (fit.getR() < FIT_RELIABILITY)
  3889. {
  3890. acce_cur_state = ACCELERATE_STATE::STATE_ACCE_IDLING;
  3891. return true;
  3892. }
  3893. return false;
  3894. }
  3895. /*
  3896. * 功能:确定启动规则,如果怠速,则不能运动,否则开始运动
  3897. *
  3898. * param
  3899. * 无
  3900. *
  3901. * return
  3902. * 怠速 返回false
  3903. * 运动 返回true
  3904. */
  3905. bool Card::CheckStartRule()
  3906. {
  3907. //前5次不运动
  3908. if (nStartLocateCounts < 5)//哈哈哈 先注释掉
  3909. {
  3910. return false;
  3911. }
  3912. //如果第六次开始还是怠速状态,则不运动
  3913. if (CheckIdleStatus())
  3914. {
  3915. return false;
  3916. }
  3917. return true;
  3918. }
  3919. /*
  3920. * 作用:检查是否大跳
  3921. *
  3922. * param
  3923. * cell 当前定位格子信息
  3924. *
  3925. * return
  3926. * 如果存在大跳,则返回true;否则,返回false
  3927. *
  3928. */
  3929. bool Card::isOverThreshold()
  3930. {
  3931. int cnt = 0;
  3932. /*cnt = abs(cell->card_stamp_time - last_cell->card_stamp_time);
  3933. int diff_cell_id = abs(cell->id - last_cell->id);
  3934. if (diff_cell_id > cnt * MAX_CELL_FORWARD_JUMP)
  3935. {
  3936. cell->isOverThre = true;
  3937. return true;
  3938. }*/
  3939. //判定方式改为反向遍历取相邻两个元素进行判断
  3940. std::list<std::shared_ptr<Cell>>::reverse_iterator it = his_cell.rbegin();
  3941. std::list<std::shared_ptr<Cell>>::reverse_iterator it_last = it;
  3942. std::advance(it_last,1);
  3943. if (it == his_cell.rend() || it_last == his_cell.rend())
  3944. {
  3945. return false;
  3946. }
  3947. cnt = abs((*it)->card_stamp_time - (*it_last)->card_stamp_time);
  3948. int diff_cell_id = abs((*it)->id - (*it_last)->id);
  3949. if (diff_cell_id > cnt * MAX_CELL_FORWARD_JUMP)
  3950. {
  3951. (*it)->isOverThre = true;
  3952. return true;
  3953. }
  3954. return false;
  3955. }
  3956. int Card::SetCellPathMap(std::shared_ptr<CellPathMap> pCellPath)
  3957. {
  3958. this->mpCellPath = pCellPath;
  3959. return 0;
  3960. }
  3961. int Card::ChangeSystemTime(SYSTEMTIME& st,const int condition)
  3962. {
  3963. int nMs = st.wMilliseconds;
  3964. int nSecond = st.wSecond;
  3965. int nMinute = st.wMinute;
  3966. int nHour = st.wHour;
  3967. if (condition == 0)
  3968. {
  3969. if (st.wMilliseconds < 500)
  3970. {
  3971. nMs = 0;
  3972. }else{
  3973. nMs = 500;
  3974. }
  3975. }
  3976. else
  3977. {
  3978. if (st.wMilliseconds == 0)
  3979. {
  3980. nMs = 500;
  3981. }else if(st.wMilliseconds == 500){
  3982. nMs = 0;
  3983. nSecond += 1;
  3984. if (nSecond >= 60)
  3985. {
  3986. nSecond = 0;
  3987. nMinute += 1;
  3988. if (nMinute >= 60)
  3989. {
  3990. nMinute = 0;
  3991. nHour += 1;
  3992. }
  3993. }
  3994. }
  3995. }
  3996. st.wMilliseconds = nMs;
  3997. st.wSecond = nSecond;
  3998. st.wMinute = nMinute;
  3999. st.wHour = nHour;
  4000. return 0;
  4001. }
  4002. int Card::ChangeSystemTime(SYSTEMTIME& st)
  4003. {
  4004. int nMs = st.wMilliseconds;
  4005. int nSecond = st.wSecond;
  4006. int nMinute = st.wMinute;
  4007. int nHour = st.wHour;
  4008. //如果毫秒数大于1000,向秒进一格
  4009. if (st.wMilliseconds + 500 >= 1000)
  4010. {
  4011. nMs = st.wMilliseconds + 500 - 1000;
  4012. //如果秒大于60,向分进一格
  4013. if (st.wSecond + 1 >= 60)
  4014. {
  4015. nSecond = st.wSecond + 1 - 60;
  4016. //如果分大于60,向时进一格
  4017. if (st.wMinute + 1 >= 60)
  4018. {
  4019. nMinute = st.wMinute + 1 - 60;
  4020. //如果时大于24,向天进一格
  4021. if (st.wHour + 1 >= 24)
  4022. {
  4023. nHour = st.wHour + 1 - 24;
  4024. }else{
  4025. nHour = st.wHour + 1;
  4026. }
  4027. }else{
  4028. nMinute = st.wMinute + 1;
  4029. }
  4030. }else{
  4031. nSecond = st.wSecond + 1;
  4032. }
  4033. }else{
  4034. nMs = st.wMilliseconds + 500;
  4035. }
  4036. st.wMilliseconds = nMs;
  4037. st.wSecond = nSecond;
  4038. st.wMinute = nMinute;
  4039. st.wHour = nHour;
  4040. return 0;
  4041. }
  4042. //void Card::handle_data_thread(LPVOID pContext)
  4043. //{
  4044. // CYAServerDlg* pDlg = (CYAServerDlg*)pContext;
  4045. // while (TRUE)
  4046. // {
  4047. // WaitForSingleObject(hEvent,INFINITE);
  4048. // ResetEvent(hEvent);
  4049. // char sql[LENGTH_SQL] = {0};
  4050. // char chTime[30] = {0};
  4051. // char chIntervalTime[30] = {0};
  4052. // sprintf_s(chTime,
  4053. // 30,
  4054. // "%u-%u-%u %u:%u:%u.%u",
  4055. // cellDealTime.wYear,
  4056. // cellDealTime.wMonth,
  4057. // cellDealTime.wDay,
  4058. // cellDealTime.wHour,
  4059. // cellDealTime.wMinute,
  4060. // cellDealTime.wSecond,
  4061. // cellDealTime.wMilliseconds);
  4062. //
  4063. // sprintf_s(chIntervalTime,
  4064. // 30,
  4065. // "%u-%u-%u %u:%u:%u.%u",
  4066. // last_cell->interval_time.wYear,
  4067. // last_cell->interval_time.wMonth,
  4068. // last_cell->interval_time.wDay,
  4069. // last_cell->interval_time.wHour,
  4070. // last_cell->interval_time.wMinute,
  4071. // last_cell->interval_time.wSecond,
  4072. // last_cell->interval_time.wMilliseconds);
  4073. //
  4074. // sprintf_s(sql,
  4075. // "insert ignore into his_cell_location(card_id,cur_time,interval_time,origin_reader_name,origin_cell_id,real_reader_name,real_cell_id,card_stamp_time,isFit,speed,mileage,map_id,area_id) values('%s','%s','%s','%s',%d,'%s',%d,%d,%d,%.4f,%.4f,%d,%d);",
  4076. // card_id.c_str(),
  4077. // chTime,
  4078. // chIntervalTime,
  4079. // originCellReaderName.c_str(),
  4080. // originCellId,
  4081. // curCellReaderName.c_str(),
  4082. // curCellId,
  4083. // time_stamp_last,
  4084. // is_fit_pos,
  4085. // get_speed(),
  4086. // mileage,
  4087. // map_id,
  4088. // area_id);
  4089. //
  4090. // send_sql_message(sql);
  4091. // pDlg->send_json_data(JSON_CMD_VALUE_PUSH,pDlg->get_json_position());
  4092. // }
  4093. //}
  4094. int Card::OutputCmdLog(int n)
  4095. {
  4096. if (card_id != "0020000001047")
  4097. {
  4098. return 0;
  4099. }
  4100. CString strOutput = _T("");
  4101. CString tmp = _T("");
  4102. tmp = this->card_id.c_str();
  4103. strOutput += tmp;
  4104. tmp =_T("");
  4105. tmp.Format(_T(",sync num:%d"),this->m_nLastLocateT);
  4106. strOutput += tmp;
  4107. tmp =_T("");
  4108. tmp.Format(_T(",card num:%d"),this->time_stamp_cal);
  4109. strOutput += tmp;
  4110. tmp =_T("");
  4111. tmp.Format(_T(",sync list size :%d \r\n"),m_syncNumList.size());
  4112. strOutput += tmp;
  4113. TRACE(strOutput);
  4114. tmp = _T("");
  4115. tmp.Format(_T("exit %d \r\n"),n);
  4116. TRACE(tmp);
  4117. return 0;
  4118. }
  4119. void Card::set_speed( double v )
  4120. {
  4121. }
  4122. // 检查dist数据有效性
  4123. int Card::CheckDistData(int cnt)
  4124. {
  4125. LOCATION_SYSTEM_BRANCH(LOCATION_SYSTEM_BRANCH_140);
  4126. GetLocalTime(&m_afmData.st);
  4127. // dist数据少于两条直接退出
  4128. if(cnt < 2){
  4129. LOCATION_SYSTEM_BRANCH(LOCATION_SYSTEM_BRANCH_141);
  4130. m_afmData.bStatus = true;
  4131. m_afmData.strCardId = this->card_id;
  4132. m_afmData.nType = ALGO_FAILED_CONDITION_15;
  4133. ALGORITHM_FAILED(ALGO_FAILED_CONDITION_15);
  4134. return DIST_COUNT_LESS_THAN_TWO;
  4135. }
  4136. //主要处理当相同卡的时间戳的数据中存在同步序号大于5的情况,如果有大于5的数据则丢弃此数据
  4137. int k = 0;
  4138. int dst = 0;
  4139. int st = 0;
  4140. bool bRet = false;
  4141. // 获取最大时间同步值
  4142. for(DistMap::iterator it = _dists.front().distmap.begin(); it != _dists.front().distmap.end(); ++it,k++){
  4143. if(k==0){
  4144. st = it->second->sync_num;
  4145. }else{
  4146. if(st < it->second->sync_num){
  4147. //未考虑跨周期
  4148. st = it->second->sync_num;
  4149. }
  4150. }
  4151. }
  4152. map<unsigned long long,std::shared_ptr<_coordinate>> mp_dists_locate_ex;
  4153. mp_dists_locate_ex.clear();
  4154. bool bExist = false;
  4155. // 筛选掉线性插值异常的数据
  4156. for(DistMap::iterator it = _dists.front().distmap.begin(); it != _dists.front().distmap.end(); ++it){
  4157. //如果同步时间戳存在异常值,则不走算法定位,直接返回上一次结果值
  4158. if(LLONG_MAX == it->second->tt){
  4159. bExist = true;
  4160. LOCATION_SYSTEM_BRANCH(LOCATION_SYSTEM_BRANCH_144);
  4161. }else
  4162. {
  4163. mp_dists_locate_ex.insert(make_pair(it->second->tt,it->second));
  4164. }
  4165. }
  4166. if (bExist)
  4167. {
  4168. //因为存在异常数据,如果抛弃了几条,剩下的数据定位错误会导致后续所有定位成功的都失败
  4169. return DIST_COUNT_LESS_FOR_TIMESTAMP_ERROR;
  4170. }
  4171. if(mp_dists_locate_ex.size() < 2){
  4172. LOCATION_SYSTEM_BRANCH(LOCATION_SYSTEM_BRANCH_143);
  4173. this->x = this->last_locate.x;
  4174. this->y = this->last_locate.y;
  4175. this->z = 0;
  4176. m_afmData.bStatus = true;
  4177. m_afmData.strCardId = this->card_id;
  4178. m_afmData.nType = ALGO_FAILED_CONDITION_2;
  4179. ALGORITHM_FAILED(ALGO_FAILED_CONDITION_2);
  4180. return DIST_COUNT_LESS_FOR_TIMESTAMP_ERROR;
  4181. }
  4182. // 筛选掉同步序号与最大值差5的数据
  4183. mp_dists_locate.clear();
  4184. map<unsigned long long,std::shared_ptr<_coordinate>>::iterator it_mpdl = mp_dists_locate_ex.begin();
  4185. for(;it_mpdl!=mp_dists_locate_ex.end();++it_mpdl){
  4186. if(st - it_mpdl->second->sync_num < 5){
  4187. mp_dists_locate.insert(make_pair(it_mpdl->second->tt, it_mpdl->second));
  4188. }else{
  4189. LOCATION_SYSTEM_BRANCH(LOCATION_SYSTEM_BRANCH_142);
  4190. }
  4191. }
  4192. if(mp_dists_locate.size() < 2){
  4193. LOCATION_SYSTEM_BRANCH(LOCATION_SYSTEM_BRANCH_143);
  4194. this->x = this->last_locate.x;
  4195. this->y = this->last_locate.y;
  4196. this->z = 0;
  4197. m_afmData.bStatus = true;
  4198. m_afmData.strCardId = this->card_id;
  4199. m_afmData.nType = ALGO_FAILED_CONDITION_2;
  4200. ALGORITHM_FAILED(ALGO_FAILED_CONDITION_2);
  4201. return DIST_COUNT_LESS_FOR_SYNC_NUM_DIFFER_FIVE;
  4202. }
  4203. return 0;
  4204. }
  4205. int Card::AssembleDistData(std::shared_ptr<ReceiveDataMap> pRdm)
  4206. {
  4207. int maxSyncTimes = 0;
  4208. //保存加速度当前状态和上次状态
  4209. int acce_state = 0;
  4210. int acce_state_last = 0;
  4211. int ins_direction = 0;
  4212. int card_time_stamp = 0;
  4213. map<unsigned long long,std::shared_ptr<_coordinate>>::iterator it_mpdl = mp_dists_locate.begin();
  4214. int i = 0;
  4215. for(;it_mpdl!=mp_dists_locate.end();++it_mpdl){
  4216. if(i==0){
  4217. card_time_stamp = it_mpdl->second->t;
  4218. maxSyncTimes = it_mpdl->second->sync_num;
  4219. acce_state = it_mpdl->second->acce_state;
  4220. acce_state_last = it_mpdl->second->acce_state_last;
  4221. ins_direction = it_mpdl->second->ins_direction;
  4222. }
  4223. else{
  4224. if(maxSyncTimes < it_mpdl->second->sync_num){
  4225. maxSyncTimes = it_mpdl->second->sync_num;
  4226. acce_state = it_mpdl->second->acce_state;
  4227. acce_state_last = it_mpdl->second->acce_state_last;
  4228. ins_direction = it_mpdl->second->ins_direction;
  4229. }
  4230. }
  4231. ReceiveDataMap::iterator prdm_it = pRdm->find(it_mpdl->second->tt);
  4232. if(prdm_it == pRdm->end()){
  4233. //保存信息用于定位
  4234. std::shared_ptr<ReceiveData> prd = std::make_shared<ReceiveData>();
  4235. prd->reader_id = it_mpdl->second->reader_id;
  4236. prd->antenna_id = it_mpdl->second->antenna_id;
  4237. prd->rec_time_stamp = it_mpdl->second->tt;
  4238. prd->x = it_mpdl->second->x*this->map_scale;
  4239. prd->y = it_mpdl->second->y*this->map_scale;
  4240. prd->z = it_mpdl->second->z*this->map_scale;
  4241. prd->special = it_mpdl->second->special;
  4242. if (prd->rec_time_stamp > 0)
  4243. {
  4244. pRdm->insert(make_pair(prd->rec_time_stamp,prd));
  4245. }
  4246. }
  4247. i++;
  4248. }
  4249. m_nCalcSyncNum = maxSyncTimes;
  4250. acce_cur_state = acce_state;
  4251. return 0;
  4252. }
  4253. int Card::SaveCardAlgoData(std::shared_ptr<POS>& pos)
  4254. {
  4255. this->x = pos->cx;
  4256. this->y = pos->cy;
  4257. this->z = pos->cz;
  4258. if (m_nLastLocateT!=0 && this->last_locate.x != INVALID_COORDINATE && this->last_locate.y != INVALID_COORDINATE)
  4259. {
  4260. if (abs(this->last_locate.x) > 1E-5 || abs(this->last_locate.y) > 1E-5)
  4261. {
  4262. //去除采集刚启动,last_locate的坐标为零而导致判断车辆上下行方向错误的问题
  4263. //车辆上下行确定
  4264. //3个条件:起点(x1,y1),终点(x2,y2)
  4265. //1.x1==x2的情况下,y2>y1为下行
  4266. //2.y1==y2的情况下,x1>x2为下行
  4267. //3.x1>x2且y2>y1为下行
  4268. //其他情况为上行
  4269. int nStream = 0;
  4270. if ((this->last_locate.x == this->x && this->y > this->last_locate.y)
  4271. ||(this->last_locate.x > this->x && this->y == this->last_locate.y)
  4272. ||(this->last_locate.x > this->x && this->y > this->last_locate.y))
  4273. {
  4274. nStream = DOWN_STREAM;
  4275. }
  4276. else
  4277. {
  4278. nStream = UP_STREAM;
  4279. }
  4280. if (pos->is_back)
  4281. {
  4282. }else{
  4283. this->m_nStream = nStream;
  4284. }
  4285. }
  4286. }
  4287. int nSign = 1;
  4288. if(pos->cvx == 0){
  4289. if(pos->cvy > 0){
  4290. nSign = 1;
  4291. }
  4292. }else{
  4293. if(pos->cvx > 0){
  4294. nSign = 1;
  4295. }else{
  4296. nSign = -1;
  4297. }
  4298. }
  4299. if (m_nLastLocateT!=0)
  4300. {
  4301. //此变量用于计算减速求新坐标而减速的距离
  4302. this->last_locate.d_offset = sqrt(pow(pos->cx - this->last_locate.x,2) + pow(pos->cy - this->last_locate.y,2));
  4303. }
  4304. this->m_nMoveDirection = nSign;
  4305. this->last_locate.x = this->x = pos->cx;
  4306. this->last_locate.y = this->y = pos->cy;
  4307. this->last_locate.z = this->z = 0;
  4308. this->last_locate.sync_num = this->m_nCalcSyncNum;
  4309. this->last_locate.acceleration = pos->av;
  4310. this->last_vx = pos->cvx;
  4311. this->last_vy = pos->cvy;
  4312. last_s_locate_reader[0] = pos->nFirstReader;
  4313. last_s_locate_reader[1] = pos->nSecondReader;
  4314. is_fit_pos = pos->is_fit;
  4315. int nlast = this->m_nLastLocateT;
  4316. this->m_pKalmanFilter->m_pCar->t = this->m_nLastLocateT = this->m_nCalcSyncNum;
  4317. //速度的计算采用求平均的方式
  4318. double speed = sqrt(pow(pos->cvx,2) + pow(pos->cvy,2));
  4319. speed *=3.6; //转为km/h
  4320. if (vt_his_speed.size()==3)
  4321. {
  4322. vt_his_speed.pop_front();
  4323. }
  4324. if (speed > 1E-4)
  4325. {
  4326. vt_his_speed.push_back(speed);
  4327. }
  4328. int total = 0;
  4329. double sum_speed = 0;
  4330. for (list<double>::iterator it = vt_his_speed.begin();it != vt_his_speed.end();++it)
  4331. {
  4332. if (*it > 0)
  4333. {
  4334. sum_speed += *it;
  4335. total++;
  4336. }
  4337. }
  4338. double av = 0;
  4339. if (total>0)
  4340. {
  4341. av = sum_speed / total;
  4342. }
  4343. if (this->acce_cur_state == STATE_ACCE_STATIC)
  4344. {
  4345. this->v = 0;
  4346. }
  4347. else
  4348. {
  4349. this->v = av*nSign;
  4350. }
  4351. if (cur_fit_nums == FIT_POSITION_NUM*4)
  4352. {
  4353. this->v = 0;
  4354. }
  4355. this->last_locate.v = this->v;
  4356. if(pos->update){
  4357. sync_data sdNew;
  4358. sdNew.sync_num = this->m_nCalcSyncNum;
  4359. sdNew.x = this->x;
  4360. sdNew.y = this->y;
  4361. sdNew.vx = this->last_vx;
  4362. sdNew.vy = this->last_vy;
  4363. m_syncNumList.push_back(sdNew);
  4364. }
  4365. //保存历史数据
  4366. std::shared_ptr<POS> p = std::make_shared<POS>();
  4367. p->posx = this->x*map_scale;
  4368. p->posy = this->y*map_scale;
  4369. p->card_count = this->time_stamp_cal;
  4370. //拟合点作为参考点
  4371. //拟合数据的计算
  4372. if (his_pos.size() == REF_POSITION_NUM)
  4373. {
  4374. his_pos.pop_front();
  4375. }
  4376. his_pos.push_back(p);
  4377. int nRef = REF_POSITION_NUM * 4;
  4378. if (long_his_pos.size() == nRef)
  4379. {
  4380. long_his_pos.pop_front();
  4381. }
  4382. long_his_pos.push_back(p);
  4383. if(is_ref_pos)
  4384. {
  4385. //只有为参考点时,才会进行拟合点的计算
  4386. if (his_pos.size() == REF_POSITION_NUM)
  4387. {
  4388. if (!have_long_fit_pos)
  4389. {
  4390. CalcFittingData();
  4391. cur_fit_nums = 0;
  4392. }
  4393. }
  4394. if (long_his_pos.size() == nRef)
  4395. {
  4396. CalcLongFittingData();
  4397. cur_fit_nums = 0;
  4398. }
  4399. }
  4400. return 0;
  4401. }
  4402. /*
  4403. * 作用:保存算法计算结果数据
  4404. *
  4405. * param
  4406. * cell 单元格
  4407. *
  4408. * return
  4409. * 返回0
  4410. *
  4411. */
  4412. int Card::SaveCardAlgoData(std::shared_ptr<Cell>& cell)
  4413. {
  4414. //计算卡本次相对于上一次的步进格子数
  4415. int nStep = 0;
  4416. SYSTEMTIME sysit;
  4417. std::string strTime = "";
  4418. if (cell && last_cell)
  4419. {
  4420. if (!cell->isChangePath)
  4421. {
  4422. nStep = abs(cell->id - last_cell->id);
  4423. }else{
  4424. nStep = cell->nStep;
  4425. }
  4426. }
  4427. if (this->nStartLocateCounts > 5)
  4428. {
  4429. if (last_cell)
  4430. {
  4431. if (CFunctions::CheckSystemTimeValid(last_cell->interval_time))
  4432. {
  4433. sysit = last_cell->interval_time;
  4434. strTime = CFunctions::systime2string(last_cell->interval_time);
  4435. }
  4436. }
  4437. }
  4438. //格子换算成坐标
  4439. std::shared_ptr<POS> pos = std::make_shared<POS>();
  4440. pos = Cell2Position(cell);
  4441. curCellReaderName = cell->strSubjectReader;
  4442. curCellId = cell->id;
  4443. // std::mutex mu;
  4444. // mu.lock();
  4445. // //更新格子信息
  4446. // last_cell = cell;
  4447. // last_cell->nStep = nStep;
  4448. // last_cell->dataSource = 1;
  4449. // last_cell->card_stamp_time = time_stamp_cal;
  4450. // time_stamp_last = time_stamp_cal;
  4451. //
  4452. // if (this->nStartLocateCounts == 5)
  4453. // {
  4454. // ::GetLocalTime(&last_cell->interval_time);
  4455. // if (last_cell->interval_time.wMilliseconds < 500)
  4456. // {
  4457. // last_cell->interval_time.wMilliseconds = 0;
  4458. // }
  4459. // else
  4460. // {
  4461. // last_cell->interval_time.wMilliseconds = 500;
  4462. // }
  4463. // }else if(this->nStartLocateCounts > 5){
  4464. // last_cell->interval_time = sysit;
  4465. // ChangeSystemTime(last_cell->interval_time);
  4466. // }
  4467. //
  4468. //#ifdef DEBUG
  4469. // TRACE(_T("savecardalgodata: interval_time = %s\r\n"),CFunctions::c2wc(strTime.c_str()));
  4470. //#else
  4471. // char log[200] = {0};
  4472. //
  4473. // if (strTime != "")
  4474. // {
  4475. // //sprintf_s(log,"savecardalgodata: interval_time = %s",strTime.c_str());
  4476. // //std::string strLog = log;
  4477. // //Log::write_log(FILE_TYPE::SYS_S,strLog,true);
  4478. // }
  4479. //
  4480. //#endif // DEBUG
  4481. // locate_cells.push_back(last_cell);
  4482. // mu.unlock();
  4483. //更新格子信息
  4484. cell->nStep = nStep;
  4485. cell->dataSource = 1;
  4486. cell->card_stamp_time = time_stamp_cal;
  4487. time_stamp_last = time_stamp_cal;
  4488. std::mutex mu;
  4489. mu.lock();
  4490. if (this->nStartLocateCounts == 5)
  4491. {
  4492. if (card_id == "0020000001043")
  4493. {
  4494. int a = 0;
  4495. int b = 1;
  4496. a = b;
  4497. }
  4498. ::GetLocalTime(&cell->interval_time);
  4499. if (cell->interval_time.wMilliseconds < 500)
  4500. {
  4501. cell->interval_time.wMilliseconds = 0;
  4502. }
  4503. else
  4504. {
  4505. cell->interval_time.wMilliseconds = 500;
  4506. }
  4507. }else if(this->nStartLocateCounts > 5){
  4508. if (last_cell && CFunctions::CompareSystemTime(cell->interval_time,last_cell->interval_time) != 0)
  4509. {
  4510. cell->interval_time = last_cell->interval_time;
  4511. ChangeSystemTime(cell->interval_time);
  4512. }
  4513. }
  4514. if (cell)
  4515. {
  4516. last_cell = cell;
  4517. locate_cells.push_back(last_cell);
  4518. }else
  4519. return 1;
  4520. //ResetEvent(hEvent);
  4521. ::PostMessage(pMainWnd,WM_CARD_SEND_JSON,(WPARAM)this,(LPARAM)last_cell.get());
  4522. //::PostMessage(pMainWnd,WM_CARD_SEND_JSON,(WPARAM)this,(LPARAM)0);
  4523. mu.unlock();
  4524. if (!is_fit_pos && m_nLastLocateT != 0)
  4525. {
  4526. //确保第一次不会进来算速度
  4527. if (this->x != INVALID_COORDINATE && this->y != INVALID_COORDINATE)
  4528. {
  4529. //pos为本次定位坐标,(x,y)为上一次定位坐标
  4530. double distance = sqrt(pow(pos->cx - this->x,2) + pow(pos->cy - this->y,2))*map_scale;
  4531. this->mileage = distance;
  4532. int nCounts = ceil(distance/CELL_WIDTH);
  4533. this->v = nCounts*CELL_WIDTH/(SEND_CELL_INTERVAL/1000.0);
  4534. this->v*=3.6;
  4535. }
  4536. }else{
  4537. this->v = this->v / 2;
  4538. }
  4539. if (this->v > 30)
  4540. {
  4541. this->v = 25;
  4542. }
  4543. this->x = pos->cx;
  4544. this->y = pos->cy;
  4545. this->z = 0;
  4546. if (m_nLastLocateT != 0 && this->last_locate.x != INVALID_COORDINATE && this->last_locate.y != INVALID_COORDINATE)
  4547. {
  4548. if (abs(this->last_locate.x) > 1E-5 || abs(this->last_locate.y) > 1E-5)
  4549. {
  4550. //去除采集刚启动,last_locate的坐标为零而导致判断车辆上下行方向错误的问题
  4551. //车辆上下行确定
  4552. //3个条件:起点(x1,y1),终点(x2,y2)
  4553. //1.x1==x2的情况下,y2>y1为下行
  4554. //2.y1==y2的情况下,x1>x2为下行
  4555. //3.x1>x2且y2>y1为下行
  4556. //其他情况为上行
  4557. int nStream = 0;
  4558. if ((this->last_locate.x == this->x && this->y > this->last_locate.y)
  4559. ||(this->last_locate.x > this->x && this->y == this->last_locate.y)
  4560. ||(this->last_locate.x > this->x && this->y > this->last_locate.y))
  4561. {
  4562. nStream = DOWN_STREAM;
  4563. }
  4564. else
  4565. {
  4566. nStream = UP_STREAM;
  4567. }
  4568. this->m_nStream = nStream;
  4569. }
  4570. }
  4571. //如果怠速,则速度置为零
  4572. if (CheckIdleStatus())
  4573. {
  4574. this->v = 0;
  4575. }
  4576. is_fit_pos = pos->is_fit;
  4577. this->last_locate.x = pos->cx;
  4578. this->last_locate.y = pos->cy;
  4579. this->last_locate.z = 0;
  4580. this->last_locate.sync_num = this->m_nCalcSyncNum;
  4581. this->m_nLastLocateT = this->m_nCalcSyncNum;
  4582. this->last_locate.v = this->v;
  4583. return 0;
  4584. }
  4585. int Card::CheckSolution(std::shared_ptr<POS>& p)
  4586. {
  4587. //定位成功
  4588. double cx = p->cx;
  4589. double cy = p->cy;
  4590. double cz = p->cz;
  4591. double cvx = 0.0,cvy = 0.0,cvz = 0.0;
  4592. double av = 0.0;
  4593. if(this->m_nLastLocateT == 0){
  4594. /*sync_data sdNew;
  4595. sdNew.sync_num = this->m_nCalcSyncNum;
  4596. sdNew.x = cx;
  4597. sdNew.y = cy;
  4598. sdNew.vx = 0;
  4599. sdNew.vy = 0;
  4600. sdNew.update = false;
  4601. m_syncNumList.push_back(sdNew);*/
  4602. p->cx = cx;
  4603. p->cy = cy;
  4604. p->cvx = 0;
  4605. p->cvy = 0;
  4606. p->update = false;
  4607. }else{
  4608. //现在的关于同步序号的处理是这样的:
  4609. //如果定位成功,就把这次定位成功的同步数据:同步序号,坐标;x,y方向的速度,扔到一个队列里,
  4610. //后来定位成功的就会先根据同步序号差用加速度抛一次;
  4611. //抛不掉,就用队列里的同步数据(从后往前找),找到第一个与当前同步序号相差大于5的同步数据来进行第二次计算速度以及加速度,
  4612. //如果加速度大于5,就不要此次的定位数据,
  4613. //如果通过加速度判断就将队列中从头开始到此同步数据的所有元素都丢弃,并插入新的此次同步数据
  4614. double interval_time = 0.2;
  4615. double deltaT = 0;
  4616. sync_data sd;
  4617. if (this->b_long_interval)
  4618. {
  4619. //此段代码用于将上一次定位是根据两个时间差是个很大值而定位出的结果
  4620. //当后续定位时就和最近的定位结果进行比较
  4621. //例如:当上一次同步序号是14321,它定位时比较的同步序号是14200,时间差大于20多秒
  4622. //这时我们就将b_long_interval置为true
  4623. //当本次定位,同步序号是14326,,这时就需要根据最近的14321进行判断
  4624. list<sync_data>::reverse_iterator it = m_syncNumList.rbegin();
  4625. sync_data sd = *it;
  4626. //以下计算deltaT还需要考虑卡的同步序号轮回的情况。
  4627. if (m_nCalcSyncNum > it->sync_num)
  4628. //if(!CheckCrossCycle())
  4629. {
  4630. deltaT = (m_nCalcSyncNum - sd.sync_num)*interval_time;
  4631. }
  4632. else
  4633. {
  4634. deltaT = (m_nCalcSyncNum + 65536 - sd.sync_num)*interval_time;
  4635. }
  4636. if (deltaT < 10 && deltaT > 0)
  4637. {
  4638. this->b_long_interval = false;
  4639. }
  4640. //避免同一个同步序号下存在多个不同卡序号
  4641. if (deltaT < 1E-2)
  4642. {
  4643. deltaT = 0.2;
  4644. }
  4645. p->diff_reader_sync_num = deltaT;
  4646. p->ref_x = sd.x;
  4647. p->ref_y = sd.y;
  4648. cvx = (cx - sd.x)*this->map_scale/deltaT;
  4649. cvy = (cy - sd.y)*this->map_scale/deltaT;
  4650. double avx = (cvx - sd.vx) / deltaT;
  4651. double avy = (cvy - sd.vy) / deltaT;
  4652. av = sqrt(pow(avx,2) + pow(avy,2));
  4653. //车卡的加速度
  4654. switch(this->card_type){
  4655. case CT_PERSON:
  4656. if(av > PERSON_ACCELERATE_THRESHOLD){
  4657. p->reason = CHECK_PERSON_ACCE_OVER_SPEED;
  4658. this->x = this->last_locate.x;
  4659. this->y = this->last_locate.y;
  4660. this->b_long_interval = false;
  4661. m_afmData.bStatus = true;
  4662. m_afmData.strCardId = this->card_id;
  4663. m_afmData.nType = ALGO_FAILED_CONDITION_5;
  4664. ALGORITHM_FAILED(ALGO_FAILED_CONDITION_5);
  4665. return CHECK_PERSON_ACCE_OVER_SPEED;
  4666. }
  4667. break;
  4668. case CT_VEHICLE:
  4669. if(av > VECHILE_ACCELERATE_THRESHOLD){
  4670. //保留上次结果
  4671. p->reason = CHECK_VEHICLE_ACCE_OVER_SPEED;
  4672. //this->x = this->last_locate.x;
  4673. //this->y = this->last_locate.y;
  4674. p->posx = INVALID_COORDINATE;
  4675. p->posy = INVALID_COORDINATE;
  4676. p->cx = INVALID_COORDINATE / (map_scale*1.0);
  4677. p->cy = INVALID_COORDINATE / (map_scale*1.0);
  4678. this->b_long_interval = false;
  4679. m_afmData.bStatus = true;
  4680. m_afmData.strCardId = this->card_id;
  4681. m_afmData.nType = ALGO_FAILED_CONDITION_6;
  4682. ALGORITHM_FAILED(ALGO_FAILED_CONDITION_6);
  4683. return CHECK_VEHICLE_ACCE_OVER_SPEED;
  4684. }
  4685. break;
  4686. }
  4687. }else{
  4688. //从队列尾部开始查找,找到第一个同步序号与当前计算卡的同步序号相差5个以上的数据
  4689. list<sync_data>::reverse_iterator it;
  4690. bool bOverflow = false;
  4691. //sync_data sd;
  4692. bool bfind =false;
  4693. for(it = m_syncNumList.rbegin();it!=m_syncNumList.rend();it++){
  4694. if(m_nCalcSyncNum - it->sync_num >= 5){
  4695. bfind = true;
  4696. sd = *it;
  4697. break;
  4698. }else{
  4699. if(m_nCalcSyncNum - it->sync_num < 0 && m_nCalcSyncNum < 100){
  4700. //如果最新同步号小于列表中的同步号则
  4701. if(m_nCalcSyncNum + 65536 - it->sync_num >=5 ){//5
  4702. bOverflow = true;
  4703. bfind = true;
  4704. sd = *it;
  4705. }
  4706. }else{
  4707. continue;
  4708. }
  4709. }//end else
  4710. }//end for
  4711. //这里不能对前5次定位的数据做加速度处理,不然会出现抛不掉的
  4712. //需要处理,那么第一次定位的成功很重要
  4713. //处理前5次的
  4714. /*if(!bfind){
  4715. sd = *(m_syncNumList.begin());
  4716. }*/
  4717. //根据溢出条件来计算deltaT
  4718. if(bOverflow){
  4719. deltaT = (m_nCalcSyncNum + 65536 - sd.sync_num)*interval_time;
  4720. }else{
  4721. deltaT = (m_nCalcSyncNum - sd.sync_num)*interval_time;
  4722. }
  4723. p->diff_reader_sync_num = deltaT;
  4724. p->ref_x = sd.x;
  4725. p->ref_y = sd.y;
  4726. //速度正负的判断:以x轴,y轴正向运动为正
  4727. //如果x相等,则y2 - y1 > 0为正
  4728. //其他情况,则x2 - x1 > 0 为正
  4729. int nSign = 1;
  4730. if(cx == sd.x){
  4731. if(cy > sd.y){
  4732. nSign = 1;
  4733. }else{
  4734. nSign = -1;
  4735. }
  4736. }else{
  4737. if(cx > sd.x){
  4738. nSign = 1;
  4739. }else{
  4740. nSign = -1;
  4741. }
  4742. }
  4743. //转为m/s
  4744. cvx = (cx - sd.x)*this->map_scale/deltaT;
  4745. cvy = (cy - sd.y)*this->map_scale/deltaT;
  4746. double cv = sqrt(pow(cvx,2) + pow(cvy,2));
  4747. cv = cv*nSign;
  4748. double avx = (cvx - sd.vx) / deltaT;
  4749. double avy = (cvy - sd.vy) / deltaT;
  4750. double av = sqrt(pow(avx,2) + pow(avy,2));
  4751. //车卡的加速度
  4752. switch(this->card_type){
  4753. case CT_PERSON:
  4754. if(av > PERSON_ACCELERATE_THRESHOLD){
  4755. p->reason = CHECK_PERSON_ACCE_OVER_SPEED;
  4756. this->x = this->last_locate.x;
  4757. this->y = this->last_locate.y;
  4758. m_afmData.bStatus = true;
  4759. m_afmData.strCardId = this->card_id;
  4760. m_afmData.nType = ALGO_FAILED_CONDITION_5;
  4761. ALGORITHM_FAILED(ALGO_FAILED_CONDITION_5);
  4762. return CHECK_PERSON_ACCE_OVER_SPEED;
  4763. }
  4764. break;
  4765. case CT_VEHICLE:
  4766. if(av > VECHILE_ACCELERATE_THRESHOLD){
  4767. //保留上次结果
  4768. p->reason = CHECK_VEHICLE_ACCE_OVER_SPEED;
  4769. //this->x = this->last_locate.x;
  4770. //this->y = this->last_locate.y;
  4771. p->posx = INVALID_COORDINATE;
  4772. p->posy = INVALID_COORDINATE;
  4773. p->cx = INVALID_COORDINATE / (map_scale*1.0);
  4774. p->cy = INVALID_COORDINATE / (map_scale*1.0);
  4775. m_afmData.bStatus = true;
  4776. m_afmData.strCardId = this->card_id;
  4777. m_afmData.nType = ALGO_FAILED_CONDITION_6;
  4778. ALGORITHM_FAILED(ALGO_FAILED_CONDITION_6);
  4779. return CHECK_VEHICLE_ACCE_OVER_SPEED;
  4780. }
  4781. break;
  4782. }
  4783. cv = cv*3.6;
  4784. //速度的限制
  4785. if(fabs(cv) > MAX_VECHILE_SPEED){
  4786. p->reason = CHECK_VEHICLE_OVER_SPEED;
  4787. //this->x = this->last_locate.x;
  4788. //this->y = this->last_locate.y;
  4789. p->posx = INVALID_COORDINATE;
  4790. p->posy = INVALID_COORDINATE;
  4791. p->cx = INVALID_COORDINATE / (map_scale*1.0);
  4792. p->cy = INVALID_COORDINATE / (map_scale*1.0);
  4793. m_afmData.bStatus = true;
  4794. m_afmData.strCardId = this->card_id;
  4795. m_afmData.nType = ALGO_FAILED_CONDITION_7;
  4796. ALGORITHM_FAILED(ALGO_FAILED_CONDITION_7);
  4797. return CHECK_VEHICLE_OVER_SPEED;
  4798. }
  4799. //使用间隔来修正速度
  4800. if(deltaT - 1.0 >= 0){
  4801. if (deltaT > 10 && bfind)
  4802. {
  4803. this->b_long_interval = true;
  4804. }
  4805. //删除第一个元素到tmp(含)之间的所有元素
  4806. bool bStartDel = false;
  4807. for(list<sync_data>::reverse_iterator tmp = m_syncNumList.rbegin();tmp != m_syncNumList.rend();)
  4808. {
  4809. if(bStartDel){
  4810. tmp = list<sync_data>::reverse_iterator(m_syncNumList.erase((++tmp).base()));
  4811. }else{
  4812. if(*tmp == sd){
  4813. bStartDel = true;
  4814. }
  4815. ++tmp;
  4816. }
  4817. }
  4818. }
  4819. if (!bfind)
  4820. {
  4821. cvx = 0;
  4822. cvy = 0;
  4823. cvz = 0;
  4824. av = 0;
  4825. }
  4826. }
  4827. this->m_nSyncNumInList = sd.sync_num;
  4828. p->update = true;
  4829. }
  4830. p->cvx = cvx;
  4831. p->cvy = cvy;
  4832. p->cvz = cvz;
  4833. p->av = av;
  4834. return 0;
  4835. }
  4836. /*
  4837. * 通过趋向性判断解的可靠性,暂通过上下行判断
  4838. *
  4839. * param
  4840. * p 当前定位坐标信息
  4841. *
  4842. * return
  4843. * 通过验证返回0,否则返回错误码
  4844. *
  4845. */
  4846. int Card::CheckSulutionByStream(std::shared_ptr<POS> p)
  4847. {
  4848. if (m_nStream == 0)
  4849. {
  4850. return 0;
  4851. }
  4852. if (abs(this->last_locate.x) > 1E-5 || abs(this->last_locate.y) > 1E-5)
  4853. {
  4854. //去除采集刚启动,last_locate的坐标为零而导致判断车辆上下行方向错误的问题
  4855. //车辆上下行确定
  4856. //3个条件:起点(x1,y1),终点(x2,y2)
  4857. //1.x1==x2的情况下,y2>y1为下行
  4858. //2.y1==y2的情况下,x1>x2为下行
  4859. //3.x1>x2且y2>y1为下行
  4860. //其他情况为上行
  4861. int nStream = 0;
  4862. double cx = p->posx / (map_scale*1.0);
  4863. double cy = p->posy / (map_scale*1.0);
  4864. if ((this->last_locate.x == cx && cy > this->last_locate.y)
  4865. ||(this->last_locate.x > cx && cy == this->last_locate.y)
  4866. ||(this->last_locate.x > cx && cy > this->last_locate.y))
  4867. {
  4868. nStream = DOWN_STREAM;
  4869. }
  4870. else
  4871. {
  4872. nStream = UP_STREAM;
  4873. }
  4874. if (nStream != m_nStream)
  4875. {
  4876. return 1;
  4877. }
  4878. }
  4879. return 0;
  4880. }
  4881. bool Card::IsExistPath(int left,int right)
  4882. {
  4883. TDOAReaderPathMap::iterator it = pTdoaReaderPathMap->find(left);
  4884. if (it == pTdoaReaderPathMap->end())
  4885. {
  4886. return false;
  4887. }
  4888. ReaderPathMap::iterator it_s = it->second->find(right);
  4889. if (it_s == it->second->end())
  4890. {
  4891. return false;
  4892. }
  4893. return true;
  4894. }
  4895. int Card::CopySolution(std::shared_ptr<POS> source,std::shared_ptr<POS>& dest)
  4896. {
  4897. dest->posx = source->posx;
  4898. dest->posy = source->posy;
  4899. dest->cvx = source->cvx;
  4900. dest->cvy = source->cvy;
  4901. return 0;
  4902. }
  4903. int Card::SaveOriginDataBeforeFilter(std::shared_ptr<POS> pos)
  4904. {
  4905. origin_locate.x = pos->cx;
  4906. origin_locate.y = pos->cy;
  4907. origin_locate.z = pos->cz;
  4908. origin_locate.v = sqrt(pow(pos->cvx,2) + pow(pos->cvy,2))*3.6; //*3.6转为km/h
  4909. origin_locate.sync_num = this->m_nCalcSyncNum;
  4910. return 0;
  4911. }
  4912. int Card::ChooseOneSolution(std::shared_ptr<ReceiveDataMap> pRdm, std::vector<std::shared_ptr<POS>> udm_pos, std::shared_ptr<POS>& pos )
  4913. {
  4914. if(0 == udm_pos.size()){ // 没有解
  4915. TRACE(_T("没有接"));
  4916. return SOLUTION_NO_SOLUTION;
  4917. }
  4918. // 只有一个解
  4919. if(1 == udm_pos.size()){
  4920. //判断此位置距离分站的距离是否为4米范围内
  4921. ReceiveDataMap::iterator it_first = pRdm->end();
  4922. ReceiveDataMap::iterator it_second = pRdm->end();
  4923. for (ReceiveDataMap::iterator it = pRdm->begin();it != pRdm->end();++it)
  4924. {
  4925. if (it->second->reader_id == udm_pos.at(0)->nFirstReader)
  4926. {
  4927. it_first = it;
  4928. }
  4929. if (it->second->reader_id == udm_pos.at(0)->nSecondReader)
  4930. {
  4931. it_second = it;
  4932. }
  4933. }
  4934. double dist = 0.0;
  4935. if (it_first != pRdm->end())
  4936. {
  4937. dist = sqrt(pow(udm_pos.at(0)->posx - it_first->second->x,2)+pow(udm_pos.at(0)->posy - it_first->second->y,2));
  4938. if (dist < NEAR_READER && it_first->second->special == 0)
  4939. {
  4940. TRACE(_T("4002"));
  4941. return SOLUTION_NEAR_READER;
  4942. }
  4943. }
  4944. if (it_second != pRdm->end())
  4945. {
  4946. dist = sqrt(pow(udm_pos.at(0)->posx - it_second->second->x,2)+pow(udm_pos.at(0)->posy - it_second->second->y,2));
  4947. if (dist < NEAR_READER && it_second->second->special == 0)
  4948. {
  4949. TRACE(_T("40002"));
  4950. return SOLUTION_NEAR_READER;
  4951. }
  4952. }
  4953. pos->posx = udm_pos.at(0)->posx;
  4954. pos->posy = udm_pos.at(0)->posy;
  4955. pos->posz = udm_pos.at(0)->posz;
  4956. pos->cx = pos->posx / (this->map_scale*1.0);
  4957. pos->cy = pos->posy / (this->map_scale*1.0);
  4958. pos->cz = pos->posz / (this->map_scale*1.0);
  4959. return 0;
  4960. }
  4961. ReceiveDataMap::iterator itf = pRdm->begin();
  4962. int bf = itf->second->reader_id;
  4963. std::advance(itf,1);
  4964. int bs = itf->second->reader_id;
  4965. // 有多个解,含两解或两解以上
  4966. //判断依据:选取无地图集的分站数据来校正所有的解,
  4967. //解可靠的条件如下:1解与无地图集的时间戳距离差最小,2且此距离差不大于10米
  4968. for (ReceiveDataMap::reverse_iterator it_first = pRdm->rbegin();it_first!=pRdm->rend();++it_first)
  4969. {
  4970. ReceiveDataMap::reverse_iterator it_second = it_first;
  4971. std::advance(it_second,1);
  4972. if (it_second == pRdm->rend())
  4973. {
  4974. //找到尾部了还未找到合适解,即认为无解
  4975. pos->reason = 1;
  4976. break;
  4977. }
  4978. //反向查找,还要去掉这两个之间有地图集的数据
  4979. int nfr = it_first->second->reader_id;
  4980. int nfs = it_second->second->reader_id;
  4981. if (bf!=nfs && bs!=nfr)
  4982. {
  4983. if (IsExistPath(nfr,nfs))
  4984. {
  4985. continue;
  4986. }
  4987. }
  4988. int nCounts = 0;
  4989. for (std::vector<std::shared_ptr<POS>>::iterator it_pos = udm_pos.begin();it_pos != udm_pos.end();++it_pos)
  4990. {
  4991. //此点在这两个线段内,才允许使用此判断条件
  4992. _point p,l1,l2;
  4993. p.x = (*it_pos)->posx;
  4994. p.y = (*it_pos)->posy;
  4995. l1.x = it_first->second->x;
  4996. l1.y = it_first->second->y;
  4997. l2.x = it_second->second->x;
  4998. l2.y = it_second->second->y;
  4999. if (!LocateAlgorithm::IsInLine(p,l1,l2))
  5000. {
  5001. nCounts++;
  5002. continue;
  5003. }
  5004. double distance_reader = 0;
  5005. distance_reader = fabs(sqrt(pow((*it_pos)->posx - it_first->second->x,2) + pow((*it_pos)->posy - it_first->second->y,2)) - sqrt(pow((*it_pos)->posx - it_second->second->x,2) + pow((*it_pos)->posy - it_second->second->y,2)));
  5006. long long dt = it_first->second->rec_time_stamp - it_second->second->rec_time_stamp;
  5007. double distance_tt = 0;
  5008. distance_tt = fabs(CFunctions::getDistance(dt,CFunctions::TDOA));
  5009. (*it_pos)->dis_diff = fabs(distance_reader - distance_tt);
  5010. }
  5011. if (nCounts == udm_pos.size())
  5012. {
  5013. continue;
  5014. }
  5015. double dMinDiff = 99999.9;
  5016. std::vector<std::shared_ptr<POS>>::iterator it_find = udm_pos.end();
  5017. for (std::vector<std::shared_ptr<POS>>::iterator it_pos = udm_pos.begin();it_pos != udm_pos.end();++it_pos)
  5018. {
  5019. if ((*it_pos)->dis_diff < dMinDiff)
  5020. {
  5021. _point p,l1,l2;
  5022. p.x = p.y = p.z = l1.x = l1.y = l1.z = l2.x = l2.y = l2.z = 0;
  5023. p.x = (*it_pos)->posx;
  5024. p.y = (*it_pos)->posy;
  5025. for (ReceiveDataMap::iterator it = pRdm->begin();it != pRdm->end();++it)
  5026. {
  5027. if (it->second->reader_id == (*it_pos)->nFirstReader)
  5028. {
  5029. l1.x = it->second->x;
  5030. l1.y = it->second->y;
  5031. }
  5032. if (it->second->reader_id == (*it_pos)->nSecondReader)
  5033. {
  5034. l2.x = it->second->x;
  5035. l2.y = it->second->y;
  5036. }
  5037. }
  5038. if (!LocateAlgorithm::IsInLine(p,l1,l2))
  5039. {
  5040. continue;
  5041. }else{
  5042. if (((*it_pos)->nFirstReader == nfr &&(*it_pos)->nSecondReader == nfs)||
  5043. ((*it_pos)->nFirstReader == nfs &&(*it_pos)->nSecondReader == nfr))
  5044. {
  5045. continue;
  5046. }else{
  5047. dMinDiff = (*it_pos)->dis_diff;
  5048. it_find = it_pos;
  5049. }
  5050. }
  5051. }
  5052. }
  5053. if (it_find != udm_pos.end())
  5054. {
  5055. if ((*it_find)->dis_diff < 10)
  5056. {
  5057. //找到即退出
  5058. //pos = *it_find;
  5059. pos->posx = (*it_find)->posx;
  5060. pos->posy = (*it_find)->posy;
  5061. pos->posz = (*it_find)->posz;
  5062. pos->reason = 0;
  5063. break;
  5064. }
  5065. }
  5066. }
  5067. if (pos->reason == 0)
  5068. {
  5069. pos->cx = pos->posx / (this->map_scale*1.0);
  5070. pos->cy = pos->posy / (this->map_scale*1.0);
  5071. pos->cz = pos->posz / (this->map_scale*1.0);
  5072. }
  5073. // 未计算出结果
  5074. if(pos->posx == INVALID_COORDINATE || pos->posy == INVALID_COORDINATE){
  5075. LOCATION_SYSTEM_BRANCH(LOCATION_SYSTEM_BRANCH_148);
  5076. TRACE(_T("未计算出结果"));
  5077. return SOLUTION_NO_SOLUTION;
  5078. }else{
  5079. bool bExistSolution = true;
  5080. //对这唯一解最最后一步筛选,在分站1米内
  5081. //增加这个条件主要是为了解决,当距离最近的分站数据未收到的情况,
  5082. //比如实际数据应该是103,102,101,,但目前收到的数据为102,101,但定位到102附近且在102和101之间
  5083. for (ReceiveDataMap::iterator it = pRdm->begin();it != pRdm->end();++it)
  5084. {
  5085. double distance = 0.0;
  5086. distance = sqrt(pow(it->second->x - pos->posx,2) + pow(it->second->y - pos->posy,2));
  5087. if (distance < 1)
  5088. {
  5089. bExistSolution = false;
  5090. break;
  5091. }
  5092. }
  5093. //无解
  5094. if (!bExistSolution)
  5095. {
  5096. std::shared_ptr<POS> tmp = std::make_shared<POS>();
  5097. pos->posx = tmp->posx;
  5098. pos->posy = tmp->posy;
  5099. pos->cx = 0;
  5100. pos->cy = 0;
  5101. pos->cz = 0;
  5102. TRACE(_T("无解"));
  5103. return SOLUTION_NO_SOLUTION;
  5104. }
  5105. }
  5106. return 0;
  5107. }
  5108. int Card::GetDeltaT( map<unsigned long long,std::shared_ptr<_coordinate>> dl )
  5109. {
  5110. unsigned long long revTime = 0;
  5111. int rid = 0, sn = 0, ct = 0;
  5112. map<unsigned long long,std::shared_ptr<_coordinate>>::iterator it_mpdl = dl.begin();
  5113. int i = 0;
  5114. for(; it_mpdl != dl.end(); ++it_mpdl){
  5115. if(0 == it_mpdl->second->tt){
  5116. continue;
  5117. }
  5118. if(this->locate && it_mpdl->second->reader_id == this->locate->ReaderId()){
  5119. revTime = it_mpdl->second->tt;
  5120. rid = it_mpdl->second->reader_id;
  5121. sn = it_mpdl->second->sync_num;
  5122. ct = it_mpdl->second->t;
  5123. break;
  5124. }
  5125. if(0 == revTime){
  5126. revTime = it_mpdl->second->tt;
  5127. rid = it_mpdl->second->reader_id;
  5128. sn = it_mpdl->second->sync_num;
  5129. ct = it_mpdl->second->t;
  5130. }
  5131. else if( revTime < it_mpdl->second->tt){ // 跨周期,可能会取到较大值
  5132. revTime = it_mpdl->second->tt;
  5133. rid = it_mpdl->second->reader_id;
  5134. sn = it_mpdl->second->sync_num;
  5135. ct = it_mpdl->second->t;
  5136. }
  5137. }
  5138. std::shared_ptr<nspLocate::LocateRecord> loc = std::make_shared<nspLocate::LocateRecord>(revTime, rid, sn, ct);
  5139. loc->getDeltaT(this->locate);
  5140. this->locate = loc;
  5141. return 0;
  5142. }
  5143. void Card::PosState( int val )
  5144. {
  5145. pos_state = val;
  5146. pos_state_count = pos_state_confirm_times;
  5147. }
  5148. int const Card::PosState()
  5149. {
  5150. if(pos_state_count >= pos_state_confirm_times )
  5151. {
  5152. return pos_state;
  5153. }
  5154. return pos_state_old;
  5155. }
  5156. void Card::PosStateOld( int val )
  5157. {
  5158. pos_state_old = val;
  5159. }
  5160. // 只显示优先级最高的状态
  5161. int const Card::StateBiz()
  5162. {
  5163. state_biz = 0;
  5164. if(status_help == STATUS_ERROR){
  5165. state_biz = STATUS_HELP;
  5166. return state_biz;
  5167. }
  5168. if(status_lost == STATUS_ERROR){
  5169. state_biz = STATUS_LOST;
  5170. return state_biz;
  5171. }
  5172. if(status_area_forbidden == STATUS_ERROR){
  5173. state_biz += STATUS_AREA_FORBIDDEN;
  5174. return state_biz;
  5175. }
  5176. if(status_area_over_speed == STATUS_ERROR){
  5177. state_biz = STATUS_AREA_OVER_SPEED;
  5178. return state_biz;
  5179. }
  5180. if(status_over_speed == STATUS_ERROR){
  5181. state_biz = STATUS_OVER_SPEED;
  5182. return state_biz;
  5183. }
  5184. if(status_area_over_time == STATUS_ERROR){
  5185. state_biz = STATUS_AREA_OVER_TIME;
  5186. return state_biz;
  5187. }
  5188. if(status_area_over_time == STATUS_ERROR){
  5189. state_biz = STATUS_OVER_TIME;
  5190. return state_biz;
  5191. }
  5192. if(status_call == STATUS_ERROR){
  5193. state_biz = STATUS_CALL;
  5194. return state_biz;
  5195. }
  5196. return state_biz;
  5197. }
  5198. MapInfo::MapInfo( void )
  5199. {
  5200. }
  5201. MapInfo::~MapInfo( void )
  5202. {
  5203. }
  5204. Dept::Dept( int id, string name )
  5205. {
  5206. dept_id = id;
  5207. dept_name = name;
  5208. }
  5209. Dept::Dept()
  5210. {
  5211. }
  5212. Dept::~Dept()
  5213. {
  5214. }
  5215. OccLevel::OccLevel(int id )
  5216. {
  5217. occlevel_id = id;
  5218. }
  5219. OccLevel::OccLevel()
  5220. {
  5221. }
  5222. OccLevel::~OccLevel()
  5223. {
  5224. }
  5225. Adhoc::Adhoc()
  5226. {
  5227. adhoc_id = 0;
  5228. x = 0;
  5229. y = 0;
  5230. z = 0;
  5231. idx = 0;
  5232. }
  5233. Adhoc::~Adhoc()
  5234. {
  5235. }
  5236. Light::Light()
  5237. {
  5238. m_nID = m_nMapID = m_nAreaID = m_nLightsGroupID = m_nReaderID = m_nSectionID = m_nPort = m_nState = m_nStream = 0;
  5239. device_type_id = 0;
  5240. m_nOldState = 0;
  5241. m_nShape = 0;
  5242. x = y = z = 0.0;
  5243. m_nIsCtrl = LIGHT_CTRL_STATE::CTRL_STATE_AUTO;
  5244. m_strIP = m_strName = m_strLabel = "";
  5245. rec_time = time(NULL);
  5246. alarm_start_time = time(NULL);
  5247. last_send_time = time(NULL);
  5248. }
  5249. int Light::get_light_state()
  5250. {
  5251. return m_nState;
  5252. }
  5253. Chamber::Chamber()
  5254. {
  5255. m_nID = m_nIsUsed = m_nMapID = m_nState = m_nSectionID = 0;
  5256. m_strPath = m_strName = m_strLabel = "";
  5257. }
  5258. Chamber::~Chamber()
  5259. {
  5260. }
  5261. /*
  5262. * 初始化区域边界
  5263. *
  5264. * param
  5265. * path 边界字符串描述
  5266. *
  5267. * return
  5268. * 成功返回0,失败返回1
  5269. */
  5270. int BaseArea::init_border(string path)
  5271. {
  5272. if(path == "" || path.find(',') == std::string::npos){
  5273. return 1;
  5274. }
  5275. std::vector<std::string> vec = Split(path, " ");
  5276. std::vector<std::string>::iterator it = vec.begin();
  5277. if(m_pPolygon){
  5278. delete[] m_pPolygon;
  5279. m_pPolygon = NULL;
  5280. }
  5281. m_pPolygon = new _point[vec.size()];
  5282. m_nPolygonCount = 0;
  5283. for(; it != vec.end(); ++it){
  5284. std::vector<std::string> subvec = Split(it->c_str(), ",");
  5285. _point p;
  5286. p.x = GetVertex(subvec[0]);
  5287. p.y = GetVertex(subvec[1]);
  5288. p.z = 0;
  5289. m_pPolygon[m_nPolygonCount] = p;
  5290. m_nPolygonCount++;
  5291. }
  5292. return 0;
  5293. }
  5294. bool BaseArea::IsInPolygon(_point p)
  5295. {
  5296. if(m_pPolygon == NULL){
  5297. return false;
  5298. }
  5299. int counter = 0;
  5300. int i;
  5301. double xinters;
  5302. _point p1,p2;
  5303. p1 = m_pPolygon[0];
  5304. for (int i=1;i<= m_nPolygonCount;i++) {
  5305. p2 = m_pPolygon[i % m_nPolygonCount];
  5306. if (p.y > MIN(p1.y,p2.y)) {
  5307. if (p.y <= MAX(p1.y,p2.y)) {
  5308. if (p.x <= MAX(p1.x,p2.x)) {
  5309. if (p1.y != p2.y) {
  5310. xinters = (p.y-p1.y)*(p2.x-p1.x)/(p2.y-p1.y)+p1.x;
  5311. if (p1.x == p2.x || p.x <= xinters)
  5312. counter++;
  5313. }
  5314. }
  5315. }
  5316. }
  5317. p1 = p2;
  5318. }
  5319. return (counter % 2 == 0) ? false : true;
  5320. }
  5321. double BaseArea::GetVertex(std::string src)
  5322. {
  5323. std::string dest = "";
  5324. for(unsigned int i = 0; i < src.length(); i++){
  5325. if((src[i] >= '0' && src[i]<='9') || src[i]=='-' || src[i] == '.'){
  5326. dest += src[i];
  5327. }
  5328. }
  5329. return atof(dest.c_str());
  5330. }
  5331. std::vector<std::string> BaseArea::Split(std::string str,std::string pattern)
  5332. {
  5333. std::string::size_type pos;
  5334. std::vector<std::string> result;
  5335. str+=pattern;//扩展字符串以方便操作
  5336. unsigned int size=str.size();
  5337. for(unsigned int i=0; i<size; i++){
  5338. pos=str.find(pattern,i);
  5339. if(pos<size){
  5340. std::string s=str.substr(i,pos-i);
  5341. result.push_back(s);
  5342. i=pos+pattern.size()-1;
  5343. }
  5344. }
  5345. return result;
  5346. }
  5347. Section::Section()
  5348. {
  5349. m_nID = m_nMapId = m_nState = 0;
  5350. m_strName = m_strLabel = m_strPath = "";
  5351. for(int i = 0; i < SECTION_EVENT_COUNT; i++){
  5352. m_event_list[i] = 0;
  5353. }
  5354. }
  5355. bool Section::is_has_chamber()
  5356. {
  5357. bool bIsHasEmptyChamber = false;
  5358. if (mp_champer_list.size()<=0)
  5359. {
  5360. return bIsHasEmptyChamber;
  5361. }
  5362. ChamberMap::iterator it = mp_champer_list.begin();
  5363. for (;it!=mp_champer_list.end();++it)
  5364. {
  5365. if (it->second->m_nIsUsed == false)
  5366. {
  5367. bIsHasEmptyChamber = true;
  5368. break;
  5369. }
  5370. }
  5371. return bIsHasEmptyChamber;
  5372. }
  5373. int Section::get_section_state()
  5374. {
  5375. int nTotals = 0;
  5376. nTotals = mp_vehicle_list.size();
  5377. switch (nTotals)
  5378. {
  5379. case 0:
  5380. m_nState = SECTION_STATE_NORMAL;
  5381. break;
  5382. case 1:
  5383. m_nState = SECTION_STATE_BUSY;
  5384. break;
  5385. case 2:
  5386. m_nState = SECTION_STATE_CONGESTION;
  5387. break;
  5388. default:
  5389. m_nState = SECTION_STATE_CONGESTION;
  5390. break;
  5391. }
  5392. return m_nState;
  5393. }
  5394. int Section::get_section_vehicle_counts()
  5395. {
  5396. return mp_vehicle_list.size();
  5397. }
  5398. int Section::get_section_staffer_counts()
  5399. {
  5400. return mp_staffer_list.size();
  5401. }
  5402. LightsGroup::LightsGroup()
  5403. {
  5404. m_bIsUsed = false;
  5405. m_nID = m_nState = 0;
  5406. m_strLabel = m_strName = m_strVechileId = "";
  5407. }
  5408. /*
  5409. * 判断红绿灯组中是否有此灯
  5410. *
  5411. * param
  5412. * pLight 灯对象
  5413. *
  5414. * return
  5415. * 存在返回true,不存在返回false
  5416. */
  5417. bool LightsGroup::isExist(std::shared_ptr<Light> pLight)
  5418. {
  5419. bool bRet = false;
  5420. for (LightMap::iterator it = mp_lights_list.begin();it != mp_lights_list.end();++it)
  5421. {
  5422. //如果红绿灯的id,地图id,路段id,都和参数一致,且灯状态可用,即为找到
  5423. if (it->second->m_nID == pLight->m_nID
  5424. && it->second->m_nMapID == pLight->m_nMapID
  5425. && it->second->m_nSectionID == pLight->m_nSectionID
  5426. && it->second->m_nState == 0)
  5427. {
  5428. bRet = true;
  5429. }
  5430. }
  5431. return bRet;
  5432. }
  5433. /*
  5434. * 判断灯组内灯的颜色是否一致
  5435. *
  5436. * param
  5437. * nColor 指定颜色(红色或绿色)
  5438. *
  5439. * return
  5440. * 相同返回true,否则返回false
  5441. *
  5442. */
  5443. bool LightsGroup::isAllLightColor(int nColor)
  5444. {
  5445. bool bRet = true;
  5446. for (LightMap::iterator it = mp_lights_list.begin();it != mp_lights_list.end();++it)
  5447. {
  5448. if (it->second->m_nColor != nColor)
  5449. {
  5450. bRet = false;
  5451. }
  5452. }
  5453. return bRet;
  5454. }
  5455. PatrolTask::PatrolTask()
  5456. {
  5457. patrol_task_id = patrol_path_id = 0;
  5458. card_id = "";
  5459. starffer_id = "";
  5460. start_time = end_time = time(NULL);
  5461. mpPoint.swap(map<unsigned int, std::shared_ptr<PatrolPoint>>());
  5462. cur_point_idx = 1;
  5463. enter_time = leave_time = time(NULL);
  5464. state = stay_state = duration_stay = 0;
  5465. is_in_cur_point = false;
  5466. }
  5467. PatrolTask::~PatrolTask()
  5468. {
  5469. }
  5470. PatrolPoint::PatrolPoint()
  5471. {
  5472. idx = 0;
  5473. patrol_point_id = 0;
  5474. map_id = 0;
  5475. x = y = z = 0;
  5476. ranging = 0; // 距离范围
  5477. duration_last = duration_stay_min = duration_stay_max = duration_ranging = 0;
  5478. }
  5479. PatrolPoint::~PatrolPoint()
  5480. {
  5481. }
  5482. BanShift::BanShift()
  5483. {
  5484. }
  5485. BanShift::BanShift( int id, std::string s, std::string e )
  5486. {
  5487. shift_id = id;
  5488. start_time = s;
  5489. end_time = e;
  5490. }
  5491. BanShift::~BanShift()
  5492. {
  5493. }