classdef.cpp 189 KB

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