classdef.cpp 107 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467
  1. #include "stdafx.h"
  2. #include "classdef.h"
  3. #include "constdef.h"
  4. #include "structdef.h"
  5. #include "locate_algorithm.h"
  6. #include "./system_basic_info/SystemAnalysis.h"
  7. #include "ProcessRemodule.h"
  8. #include "log_process_module.h"
  9. #include "Functions/Functions.h"
  10. #include "algorithm/Fit.h"
  11. #include "./mylog/log_module.h"
  12. #include <math.h>
  13. #pragma warning(disable: 4244)
  14. unsigned int g_nAlgoFailedCounts[ALGO_LOC_TOTAL] = {0};
  15. unsigned int g_nAlgoFailedCycleCounts[ALGO_LOC_TOTAL] = {0};
  16. DWORD g_ullCurTime = 0;
  17. Card::Card( string cardid, int cardtype, double z_offset, double offset_x /*= 12.0*/, double offset_y /*=12.0*/ )
  18. {
  19. BOOL bRet = InitializeCriticalSectionAndSpinCount(&m_csCard, MAXCRITICALSECTIONSPINCOUNT);
  20. card_type = cardtype;
  21. card_id = cardid;
  22. coor_offset_x = offset_x;
  23. coor_offset_y = offset_y;
  24. this->z_offset = z_offset;
  25. ::GetLocalTime(&deal_time);
  26. ::GetLocalTime(&enter_area_time);
  27. down_time = up_time = enter_reader_time = rec_time = time(NULL);
  28. time_over_time = time_area_over_time = time_area_forbidden = time_over_speed = time_low_power = time(NULL);
  29. last_locate_time = time(NULL);
  30. //坐标初始值为0
  31. //x = y = z = stored_x = stored_y = stored_z = 0;
  32. x = y = z = stored_x = stored_y = stored_z = INVALID_COORDINATE;
  33. output_x = output_y = 0;
  34. xx = yy = zz = 0;
  35. x1 = y1 = z1 = x2 = y2 = z2 = x3 = y3 = z3 = x4 = y4 = z4 = 0;
  36. a = 0;
  37. t = 0;
  38. init_postion = false;
  39. is_first_location = true;
  40. is_algo_first_location = true;
  41. b_pos_change = false;
  42. is_driving_face_start = 0;
  43. right_x = right_y = right_z = left_x = left_y = left_z = 0;
  44. m_nMoveDirection = 0;
  45. map_id = map_id_old = area_id = reader_id = 0;
  46. map_scale = 1.0;
  47. state = 0;
  48. state_moving = 0;
  49. state_biz = 0;
  50. pos_state = pos_state_old = PDT_INIT;
  51. pos_state_count = 0;
  52. pos_state_confirm_times = 1;
  53. dept_id = group_id = occlevel_id = 0;
  54. power_state = power_state_last = 0;
  55. status_help = status_area_over_time = status_area_forbidden = status_area_over_speed = 0;
  56. status_over_speed = status_over_time = status_power = status_lost = status_call = 0;
  57. sync_num = 0;
  58. isdealed = isreceive = is_pos_changed = is_hist = is_need_cal = false;
  59. is_area_over_time = is_mine_over_time = false;
  60. id = name = number = department = group = worktype = "";
  61. driver_id = "";
  62. reader_tickcount = time_stamp = time_stamp_last = 0;
  63. p_dists_locate = new _coordinate*[DIST_COUNT];
  64. time_stamp_max = 0;
  65. for(int i = 0; i < DIST_COUNT; i++){
  66. p_dists_locate[i] = NULL;
  67. }
  68. is_deal_by_algo = false;
  69. pReaderPathMap = NULL;
  70. pTdoaReaderPathMap = NULL;
  71. last_s_locate_reader[0] = last_s_locate_reader[1] = -1;
  72. m_syncNumList.clear();
  73. m_dFirstDiff = m_dSecondDiff = 0.0;
  74. m_nCalcSyncNum = 0;
  75. m_nSyncNumInList = 0;
  76. last_locate.tt = 0;
  77. last_locate.a = last_locate.antenna_id = last_locate.reader_id = last_locate.t = 0;
  78. last_locate.d = last_locate.d_offset = last_locate.v = 0.0;
  79. last_locate.x = last_locate.y = last_locate.z = INVALID_COORDINATE;
  80. last_x = last_y = last_z = INVALID_COORDINATE;
  81. locate = nullptr;
  82. m_bUseFilter = false;
  83. m_nFilterType = NO_FILTER;
  84. m_pKalmanFilter = nullptr;
  85. m_nLastLocateT = 0;
  86. origin_locate.x = origin_locate.y = origin_locate.z = origin_locate.v = 0.0;
  87. last_vx = last_vy = 0.0;
  88. v = 0.0;
  89. ins_weight = INS_WEIGHT;
  90. uwb_weight = UWB_WEIGHT;
  91. acce_cur_state = 0;
  92. acce_last_state = 0;
  93. b_long_interval = false;
  94. accelerate_state = accelerate_state_last = ACCELERATE_INIT_STATE; //默认初始的为-10
  95. ins_direction = 0;
  96. direction = 0;
  97. antenna_angle = antenna_angle_last = 0;
  98. time_stamp_last = 0;
  99. reader_id_last = 0;
  100. m_nLightGroupId = -1; //默认为没控制灯组
  101. b_enter_intersection = false;
  102. is_red_light = false;
  103. light_id = 0;
  104. time_red_light = time(NULL);
  105. m_nOutputPosState = 0;
  106. m_nIsRailroad = 0;
  107. mileage = 0;
  108. diff_direction_counts = 0;
  109. m_nStream = 0;
  110. for(int i = 0; i < CARD_EVENT_COUNT; i++){
  111. m_event_list[i] = 0;
  112. }
  113. p_reader = nullptr;
  114. _dists.swap(DistQueMap());
  115. vt_his_speed.resize(0);
  116. his_pos.resize(0);
  117. is_ref_pos = false;
  118. cur_fit_nums = 0;
  119. have_fit_pos = false;
  120. have_long_fit_pos = false;
  121. count_idle = 0;
  122. count_change_direction = 0;
  123. call_type = 0;
  124. vt_deal_call_reader_id.resize(0);
  125. recv_call_time = time(NULL);
  126. landmark_id = 0;
  127. landmark_dis = 0;
  128. landmark_direction = NODIRECTORY;
  129. drive_face_location_write_time = 0;
  130. idle_pos_list.resize(0);
  131. }
  132. Card::Card( void )
  133. :m_isWarning(false),m_minDistanceToWarningpoint(DBL_MAX),m_warning_threshold(DBL_MAX),m_CardHasBeenInDrivingfaceAlarm(false),
  134. m_DrivingfaceAlarmFlag(-1)
  135. {
  136. m_warning_point_id = -1;
  137. }
  138. void Card::reset()
  139. {
  140. //以下参数与算法相关
  141. //坐标初始值为0
  142. TRACE(_T("reset card info \r\n"));
  143. char msg[100] = {0};
  144. sprintf_s(msg,"card_id = %s, execute reset card info",card_id.c_str());
  145. Log::write_log(FILE_TYPE::SYS_S,msg,true);
  146. m_nLastLocateT = 0;
  147. time_stamp_last = 0;
  148. b_long_interval = false;
  149. is_first_location = true;
  150. last_locate.sync_num = 0;
  151. is_algo_first_location = true;
  152. while (_dists.size()>0)
  153. {
  154. _dists.pop_front();
  155. }
  156. if (m_syncNumList.size() > 0)
  157. {
  158. m_syncNumList.erase(m_syncNumList.begin(),m_syncNumList.end());
  159. m_syncNumList.resize(0);
  160. }
  161. if (vt_his_speed.size()>0)
  162. {
  163. vt_his_speed.erase(vt_his_speed.begin(),vt_his_speed.end());
  164. vt_his_speed.resize(0);
  165. }
  166. if (his_pos.size() > 0)
  167. {
  168. his_pos.erase(his_pos.begin(),his_pos.end());
  169. his_pos.resize(0);
  170. have_fit_pos = false;
  171. }
  172. if (long_his_pos.size() > 0)
  173. {
  174. long_his_pos.erase(long_his_pos.begin(),long_his_pos.end());
  175. long_his_pos.resize(0);
  176. have_long_fit_pos = false;
  177. count_idle = 0;
  178. }
  179. cur_fit_nums = 0;
  180. count_idle = 0;
  181. locate = nullptr;
  182. //卡尔曼参数重置
  183. if (m_pKalmanFilter!=nullptr)
  184. {
  185. m_pKalmanFilter->m_nCounts = 0;
  186. m_pKalmanFilter->Initial(0.2);
  187. }
  188. }
  189. void Card::set_reader(std::shared_ptr<Reader> preader) // 设置卡时间
  190. {
  191. this->rec_time = time(NULL);
  192. if(preader->reader_id != this->reader_id){ // 所在分站没有发生变化
  193. this->p_reader = preader.get();
  194. this->reader_id = preader->reader_id;
  195. this->enter_reader_time = this->rec_time;
  196. this->map_id_old = this->map_id;
  197. this->map_id = preader->map_id;
  198. this->map_scale = preader->map_scale;
  199. }
  200. if(this->pos_state == this->p_reader->pos_state){
  201. if(this->pos_state_count <= pos_state_confirm_times)
  202. {
  203. this->pos_state_count++;
  204. }
  205. }
  206. else // 井上井下位置变化
  207. {
  208. this->pos_state = p_reader->pos_state;
  209. this->pos_state_count = 1;
  210. }
  211. }
  212. /*
  213. * 设置地图集覆盖范围,适用TOF
  214. *
  215. */
  216. void Card::set_reader_path_tof(std::shared_ptr<ReaderPathMap> rpm)
  217. {
  218. this->pReaderPathMap = rpm;
  219. }
  220. /*
  221. * 设置地图集覆盖范围,适用TDOA
  222. *
  223. */
  224. void Card::set_reader_path_tdoa(std::shared_ptr<TDOAReaderPathMap> trpm)
  225. {
  226. this->pTdoaReaderPathMap = trpm;
  227. }
  228. bool operator==(sync_data& a,sync_data&b){
  229. return a.sync_num == b.sync_num;
  230. }
  231. /*
  232. * 此函数主要判断此坐标是否需要形成json输出给webserver
  233. *
  234. * param
  235. * acce_state 算法中保存的当前加速度计状态
  236. * acce_state_last 算法中保存的上一次加速度计状态
  237. *
  238. * return
  239. * 无
  240. */
  241. void Card::inspect_coordinate(int acce_state)
  242. {
  243. this->isoutput = false;
  244. char log[200] = {0};
  245. //如果是静止或者怠速状态,每次的定位坐标不变化,此时要求采集不再将此数据送往webserver
  246. if (STATE_ACCE_STATIC == acce_state || STATE_ACCE_IDLING == acce_state)
  247. {
  248. this->isoutput = false;
  249. }
  250. else
  251. {
  252. if (this->x == this->output_x && this->y == this->output_y) //last_x,last_y
  253. {
  254. count_idle++;
  255. this->isoutput = false;
  256. }else{
  257. //如果运动状态时,
  258. int nSign = 0;
  259. int nState = 0;
  260. if (this->x == this->output_x)
  261. {
  262. if (this->y > this->output_y)
  263. {
  264. nSign = 1;
  265. }else{
  266. nSign = -1;
  267. }
  268. }else{
  269. if (this->x > this->output_x)
  270. {
  271. nSign = 1;
  272. }else{
  273. nSign = -1;
  274. }
  275. }
  276. if (nSign == this->m_nMoveDirection)
  277. {
  278. count_change_direction = 0; // 未连续掉头状态,认为是抖动,不改变方向,重置计数
  279. double distance = sqrt(pow(this->x - this->output_x,2) + pow(this->y - this->output_y,2)) * this->map_scale;
  280. if(distance > 0.5) //this->map_scale)
  281. {
  282. //两点之间的距离是否大于一个0.5米,满足条件,输出json
  283. this->isoutput = true;
  284. this->output_x = this->x;
  285. this->output_y = this->y;
  286. count_idle = 0;
  287. TRACE(_T("1. send pos json \r\n"));
  288. }else{
  289. count_idle++;
  290. }
  291. }
  292. else
  293. {
  294. if( count_change_direction > 3){
  295. // 认为掉头,输出给web
  296. this->m_nMoveDirection = nSign;
  297. this->isoutput = true;
  298. this->output_x = this->x;
  299. this->output_y = this->y;
  300. count_idle = 0;
  301. count_change_direction = 0;
  302. TRACE(_T("2. send pos json \r\n"));
  303. }else{
  304. // 认为是抖动,不输出给web
  305. count_change_direction++;
  306. count_idle++;
  307. }
  308. }
  309. }
  310. }
  311. }
  312. /*
  313. * TOF定位算法
  314. *
  315. * param
  316. * cnt 数据条数
  317. *
  318. * return
  319. * 无
  320. *
  321. */
  322. void Card::algo_tof(int cnt)
  323. {
  324. //int i = 0, nCount = 0;
  325. //POS *p = NULL;
  326. //POS tmpPos;
  327. //tmpPos.posx = 0;
  328. //tmpPos.posy = 0;
  329. //tmpPos.pos_radius = 0;
  330. //int difT = 1;
  331. //bool bfound = false;
  332. //DistMap::iterator it = _dists.front().distmap.begin();
  333. //if(cnt > 2){
  334. // for(; it != _dists.front().distmap.end(); ++it){
  335. // bfound = false;
  336. // // 判断是否已经加入到列表
  337. // for(int j=0; j < i; j++){
  338. // if(p_dists_locate[j]->reader_id == it->second->reader_id){
  339. // bfound = true;
  340. // break;
  341. // }
  342. // }
  343. // if(i < DIST_COUNT && !bfound){
  344. // p_dists_locate[i] = it->second;
  345. // i++;
  346. // }
  347. // }
  348. // nCount = i;
  349. //}else{
  350. // for(; it != _dists.front().distmap.end(); ++it,i++){
  351. // p_dists_locate[i] = it->second;
  352. // }
  353. // nCount = cnt;
  354. //}
  355. //if(!init_postion){
  356. // this->x = p_dists_locate[0]->x;
  357. // this->y = p_dists_locate[0]->y;
  358. // this->z = p_dists_locate[0]->z;
  359. // this->a = p_dists_locate[0]->a;
  360. // this->distance = p_dists_locate[0]->d;
  361. // this->t = p_dists_locate[0]->t;
  362. // this->last_x = p_dists_locate[0]->x;
  363. // this->last_y = p_dists_locate[0]->y;
  364. // this->last_z = p_dists_locate[0]->z;
  365. // init_postion = true;
  366. //}
  367. //int sta_num = 0;
  368. //int ant = 0;
  369. //double dist = 0.0;
  370. //INFO_PRE info_pre;
  371. //info_pre.t = p_dists_locate[0]->t;
  372. //info_pre.detaT = 0;
  373. //info_pre.sta_num = p_dists_locate[0]->reader_id - 1;
  374. //info_pre.ant = p_dists_locate[0]->antenna_id;
  375. //info_pre.dist = p_dists_locate[0]->d;
  376. //for(int i = 1;i < nCount;i++){
  377. // sta_num = p_dists_locate[i]->reader_id - 1;
  378. // ant = p_dists_locate[i]->antenna_id;
  379. // dist = p_dists_locate[i]->d;
  380. //
  381. // p = LocateAlgorithm::Pos(*(this->pReaderPathMap),sta_num,ant,dist,info_pre);
  382. // if(p == NULL)
  383. // {
  384. // continue;
  385. // }
  386. // if(p->pos_radius < 2.0){
  387. // if(i == 1){
  388. // tmpPos.posx = p->posx;
  389. // tmpPos.posy = p->posy;
  390. // tmpPos.pos_radius = p->pos_radius;
  391. // }else{
  392. // double posx = (tmpPos.posx + p->posx)/2;
  393. // double posy = (tmpPos.posy + p->posy)/2;
  394. // double radius = (tmpPos.pos_radius + p->pos_radius)/2;
  395. // tmpPos.posx = posx;
  396. // tmpPos.posy = posy;
  397. // tmpPos.pos_radius = radius;
  398. // }
  399. // }
  400. // if(p){
  401. // delete p;
  402. // p = NULL;
  403. // }
  404. //}
  405. //if(tmpPos.posx != 0 && tmpPos.posy != 0){
  406. // int difT = info_pre.t - this->t;
  407. // double newPosX = tmpPos.posx / (this->map_scale*1.0);
  408. // double newPosY = tmpPos.posy / (this->map_scale*1.0);
  409. // double d = sqrt(pow(newPosX - this->last_locate.x,2)+pow(newPosY - this->y,2))*this->map_scale;
  410. // if(difT==0){
  411. // this->last_locate.v = 0;
  412. // }else{
  413. // this->last_locate.v = d/difT;
  414. // }
  415. // this->last_locate.x = tmpPos.posx / (this->map_scale*1.0);
  416. // this->last_locate.y = tmpPos.posy / (this->map_scale*1.0);
  417. // this->last_locate.t = info_pre.t;
  418. //}
  419. //this->x = this->last_locate.x;
  420. //this->y = this->last_locate.y;
  421. //this->v = ((int)(this->last_locate.v*1000))/1000.0;
  422. //this->z = 0;
  423. //this->a = 0;
  424. }
  425. /*
  426. * TDOA算法
  427. *
  428. * param
  429. * cnt 数据条数
  430. *
  431. * return
  432. * 无
  433. *
  434. */
  435. void Card::algo_tdoa(int cnt)
  436. {
  437. LOCATION_SYSTEM_BRANCH(LOCATION_SYSTEM_BRANCH_140);
  438. GetLocalTime(&m_afmData.st);
  439. if(cnt < 2){
  440. LOCATION_SYSTEM_BRANCH(LOCATION_SYSTEM_BRANCH_141);
  441. m_afmData.bStatus = true;
  442. m_afmData.strCardId = this->card_id;
  443. m_afmData.nType = ALGO_FAILED_CONDITION_15;
  444. ALGORITHM_FAILED(ALGO_FAILED_CONDITION_15);
  445. return;
  446. }
  447. //主要处理当相同卡的时间戳的数据中存在同步序号大于5的情况,如果有大于5的数据则丢弃此数据
  448. int k = 0;
  449. unsigned short dst = 0;
  450. unsigned short st = 0;
  451. bool bRet = false;
  452. mp_dists_locate.clear();
  453. // 多个基站的时间同步序号相差大于阈值,不参与计算
  454. for(DistMap::iterator it = _dists.front().distmap.begin(); it != _dists.front().distmap.end(); ++it,k++){
  455. if(k==0){
  456. st = it->second->sync_num;
  457. }else{
  458. dst = (st - (unsigned short)it->second->sync_num);
  459. if ((st > (unsigned short)it->second->sync_num && dst > MAX_SYNCTIME_DIFF_NUM) ||
  460. (st < (unsigned short)it->second->sync_num && dst < MAX_SYNCTIME_DIFF_NUM_CROSS_PERIOD))
  461. {
  462. LOCATION_SYSTEM_BRANCH(LOCATION_SYSTEM_BRANCH_142);
  463. bRet = true;
  464. //此处需要输出计数
  465. break;
  466. }
  467. }
  468. mp_dists_locate.insert(make_pair(it->second->tt, it->second));
  469. }
  470. if(bRet){
  471. LOCATION_SYSTEM_BRANCH(LOCATION_SYSTEM_BRANCH_143);
  472. this->x = this->last_locate.x;
  473. this->y = this->last_locate.y;
  474. this->z = 0;
  475. m_afmData.bStatus = true;
  476. m_afmData.strCardId = this->card_id;
  477. m_afmData.nType = ALGO_FAILED_CONDITION_2;
  478. ALGORITHM_FAILED(ALGO_FAILED_CONDITION_2);
  479. return;
  480. }
  481. std::shared_ptr<ReceiveDataMap> pRdm = std::make_shared<ReceiveDataMap>();
  482. pRdm->clear();
  483. bRet = false;
  484. int maxSyncTimes = 0;
  485. //保存加速度当前状态和上次状态
  486. int acce_state = 0;
  487. int acce_state_last = 0;
  488. int ins_direction = 0;
  489. int card_time_stamp = 0;
  490. map<unsigned long long,std::shared_ptr<_coordinate>>::iterator it_mpdl = mp_dists_locate.begin();
  491. int i = 0;
  492. for(;it_mpdl!=mp_dists_locate.end();++it_mpdl){
  493. if(i==0){
  494. card_time_stamp = it_mpdl->second->t;
  495. maxSyncTimes = it_mpdl->second->sync_num;
  496. acce_state = it_mpdl->second->acce_state;
  497. acce_state_last = it_mpdl->second->acce_state_last;
  498. ins_direction = it_mpdl->second->ins_direction;
  499. }
  500. else{
  501. if(maxSyncTimes < it_mpdl->second->sync_num){
  502. maxSyncTimes = it_mpdl->second->sync_num;
  503. acce_state = it_mpdl->second->acce_state;
  504. acce_state_last = it_mpdl->second->acce_state_last;
  505. ins_direction = it_mpdl->second->ins_direction;
  506. }
  507. }
  508. ReceiveDataMap::iterator prdm_it = pRdm->find(it_mpdl->second->tt);
  509. if(prdm_it == pRdm->end()){
  510. if(it_mpdl->second->tt == LLONG_MAX ){
  511. //如果同步时间戳存在异常值,则不走算法定位,直接返回上一次结果值
  512. bRet = true;
  513. //此处需要输出计数
  514. LOCATION_SYSTEM_BRANCH(LOCATION_SYSTEM_BRANCH_144);
  515. break; // 存在一个异常值就退出算法
  516. // continue; // 将异常值过滤掉,不参与算法,其他正常值照常参与计算
  517. }
  518. //保存信息用于定位
  519. std::shared_ptr<ReceiveData> prd = std::make_shared<ReceiveData>();
  520. prd->reader_id = it_mpdl->second->reader_id;
  521. prd->antenna_id = it_mpdl->second->antenna_id;
  522. prd->rec_time_stamp = it_mpdl->second->tt;
  523. prd->x = it_mpdl->second->x*this->map_scale;
  524. prd->y = it_mpdl->second->y*this->map_scale;
  525. prd->z = it_mpdl->second->z*this->map_scale;
  526. prd->special = it_mpdl->second->special;
  527. if (prd->rec_time_stamp > 0)
  528. {
  529. pRdm->insert(make_pair(prd->rec_time_stamp,prd));
  530. }
  531. }
  532. i++;
  533. }
  534. //存在异常值,直接返回上一次结果值
  535. if(bRet){
  536. LOCATION_SYSTEM_BRANCH(LOCATION_SYSTEM_BRANCH_145);
  537. pRdm->clear();
  538. this->x = this->last_locate.x;
  539. this->y = this->last_locate.y;
  540. this->z = 0;
  541. m_afmData.bStatus = true;
  542. m_afmData.strCardId = this->card_id;
  543. m_afmData.nCardStamp = it_mpdl->first;
  544. m_afmData.nType = ALGO_FAILED_CONDITION_3;
  545. ALGORITHM_FAILED(ALGO_FAILED_CONDITION_3);
  546. return;
  547. }
  548. //time_stamp_last特指卡的计数序号,非分站的
  549. //当卡需要新定位的计数序列号小于上一次成功定位的序列号,则此次不定位,避免定位结果的回退
  550. // 本次序号小于上次序号,存在如下情况
  551. // 1、跨周期,如本次0,上次为65535,另外进入盲区也可能跨周期后回来
  552. // 2、卡重置,从0开始
  553. // 3、序号较连续,新数据先到,旧数据后到,会造成跳动
  554. if (this->time_stamp_last != 0 && this->time_stamp_last > this->time_stamp_cal)
  555. {
  556. if((unsigned short)this->time_stamp_last - (unsigned short)this->time_stamp_cal < MAX_SYNCTIME_DELAY_NUM)
  557. {
  558. // 小于5,认为会影响数据
  559. pRdm->clear();
  560. this->x = this->last_locate.x;
  561. this->y = this->last_locate.y;
  562. this->z = 0;
  563. m_afmData.bStatus = true;
  564. m_afmData.strCardId = this->card_id;
  565. m_afmData.nCardStamp = this->time_stamp_cal;
  566. m_afmData.nType = ALGO_FAILED_CONDITION_4;
  567. ALGORITHM_FAILED(ALGO_FAILED_CONDITION_4);
  568. //此处需要输出计数
  569. return;
  570. }
  571. }
  572. this->time_stamp_last = this->time_stamp_cal;
  573. int nCount = 0;
  574. std::unique_ptr<POS> p = nullptr;
  575. //std::shared_ptr<POS> p = nullptr;
  576. std::unique_ptr<POS> p2 = nullptr;
  577. bool bOutput = false;
  578. this->z = 0;
  579. i = 0;
  580. if(pRdm->size() > 1){
  581. LOCATION_SYSTEM_BRANCH(LOCATION_SYSTEM_BRANCH_147);
  582. this->m_nCalcSyncNum = maxSyncTimes;
  583. //p = LocateAlgorithm::Pos(pRdm, pTdoaReaderPathMap);
  584. //算法逻辑修改:
  585. //1.当开始两分站之间有地图集时,走原来的算法逻辑
  586. //2.当开始两分站之间无地图集时,走两两遍历的算法逻辑
  587. ReceiveDataMap::iterator first = pRdm->begin();
  588. ReceiveDataMap::iterator second = first;
  589. //偏移到第二个元素
  590. std::advance(second,1);
  591. bool bFind = false;
  592. bool bExitPath = false;
  593. //如果两级都能找到才运行继续后续操作,否则,表明没有此路径地图集
  594. TDOAReaderPathMap::iterator rdm_it = pTdoaReaderPathMap->find(first->second->reader_id);
  595. if(rdm_it != pTdoaReaderPathMap->end()){
  596. //表示地图集中存在第一个分站的路径集
  597. bFind = true;
  598. }
  599. if (bFind)
  600. {
  601. //确认第一个和第二个分站之间是否有地图集
  602. ReaderPathMap::iterator rpm_it = pTdoaReaderPathMap->find(first->second->reader_id)->second->find(second->second->reader_id);
  603. if(rpm_it == pTdoaReaderPathMap->find(first->second->reader_id)->second->end()){
  604. bExitPath = false;
  605. }else{
  606. bExitPath = true;
  607. }
  608. }
  609. if (bExitPath)
  610. {
  611. //以第一个基站作为基准分站进行坐标计算
  612. p = LocateAlgorithm::Pos(pRdm, pTdoaReaderPathMap);
  613. }else{
  614. //pRdm内的数据双重遍历进行坐标计算
  615. p = LocateAlgorithm::LocatePos(pRdm, pTdoaReaderPathMap);
  616. if(fabs(p->posx) < 1E-4 && fabs(p->posy) < 1E-4 ){
  617. p->posx = INVALID_COORDINATE;
  618. p->posy = INVALID_COORDINATE;
  619. p->posz = INVALID_COORDINATE;
  620. }
  621. }
  622. this->origin_locate.x = p->posx / (this->map_scale*1.0);
  623. this->origin_locate.y = p->posy / (this->map_scale*1.0);
  624. this->origin_locate.z = p->posz / (this->map_scale*1.0);
  625. ////校验机会3次,3次内跳回去就丢弃此次计算结果
  626. //if (p->posx != INVALID_COORDINATE && p->posy != INVALID_COORDINATE)
  627. //{
  628. // int nDirection = 0;
  629. // if (!algo_is_same_direction(this->origin_locate.x,this->origin_locate.y,0))
  630. // {
  631. // this->x = this->last_locate.x;
  632. // this->y = this->last_locate.y;
  633. // return;
  634. // }
  635. //}
  636. double interval_time = 0.2; // 当ReadFileTest时,没有set_reader,给默认值
  637. if(this->p_reader){
  638. interval_time = this->p_reader->reader_interval_time;
  639. }
  640. double deltaT = 0.0;
  641. double cvx = 0;
  642. double cvy = 0;
  643. double cv = 0;
  644. double cx = 0;
  645. double cy = 0;
  646. double cz = 0;
  647. int nSign = 1;
  648. sync_data sdNew;
  649. sync_data sd;
  650. cx = p->posx / (this->map_scale*1.0);
  651. cy = p->posy / (this->map_scale*1.0);
  652. cz = p->posz / (this->map_scale*1.0);
  653. #ifdef ALGORITHM_TYPE_INS
  654. int acce_direction = 0; //加速度计速度方向 0-静止,1-前进,-1-后退
  655. int uwb_direction = 0; //uwb速度方向,同上
  656. bool bUseKalman = false;
  657. bool bOriginLocate = false; //原始定位是否成功
  658. bool bDirectReturn = false; //是否直接返回本次定位结果
  659. #endif
  660. if(p->posx != INVALID_COORDINATE && p->posy != INVALID_COORDINATE){
  661. LOCATION_SYSTEM_BRANCH(LOCATION_SYSTEM_BRANCH_148);
  662. #ifdef ALGORITHM_TYPE_INS
  663. bOriginLocate = true; //原始定位成功
  664. #endif
  665. //定位成功
  666. if(is_algo_first_location){
  667. LOCATION_SYSTEM_BRANCH(LOCATION_SYSTEM_BRANCH_149);
  668. if (m_syncNumList.size() > 0)
  669. {
  670. m_syncNumList.erase(m_syncNumList.begin(),m_syncNumList.end());
  671. m_syncNumList.resize(0);
  672. }
  673. sdNew.sync_num = maxSyncTimes;
  674. sdNew.x = cx;
  675. sdNew.y = cy;
  676. sdNew.vx = 0;
  677. sdNew.vy = 0;
  678. sdNew.update = false;
  679. m_syncNumList.push_back(sdNew);
  680. #ifdef ALGORITHM_TYPE_INS
  681. uwb_direction = 1;
  682. #endif
  683. }
  684. else{
  685. LOCATION_SYSTEM_BRANCH(LOCATION_SYSTEM_BRANCH_150);
  686. //现在的关于同步序号的处理是这样的:
  687. //如果定位成功,就把这次定位成功的同步数据:同步序号,坐标;x,y方向的速度,扔到一个队列里,
  688. //后来定位成功的就会先根据同步序号差用加速度抛一次;
  689. //抛不掉,就用队列里的同步数据(从后往前找),找到第一个与当前同步序号相差大于5的同步数据来进行第二次计算速度以及加速度,
  690. //如果加速度大于5,就不要此次的定位数据,
  691. //如果通过加速度判断就将队列中从头开始到此同步数据的所有元素都丢弃,并插入新的此次同步数据
  692. this->m_dFirstDiff = p->dDiff[0];
  693. this->m_dSecondDiff = p->dDiff[1];
  694. if (this->b_long_interval)
  695. {
  696. LOCATION_SYSTEM_BRANCH(LOCATION_SYSTEM_BRANCH_151);
  697. //此段代码用于将上一次定位是根据两个时间差是个很大值而定位出的结果
  698. //当后续定位时就和最近的定位结果进行比较
  699. //例如:当上一次同步序号是14321,它定位时比较的同步序号是14200,时间差大于20多秒
  700. //这时我们就将b_long_interval置为true
  701. //当本次定位,同步序号是14326,,这时就需要根据最近的14321进行判断
  702. list<sync_data>::reverse_iterator it = m_syncNumList.rbegin();
  703. sync_data sdl = *it;
  704. //以下计算deltaT还需要考虑卡的同步序号轮回的情况。
  705. if (maxSyncTimes > it->sync_num)
  706. {
  707. LOCATION_SYSTEM_BRANCH(LOCATION_SYSTEM_BRANCH_152);
  708. deltaT = (maxSyncTimes - sdl.sync_num)*interval_time;
  709. }
  710. else
  711. {
  712. deltaT = (maxSyncTimes + 65536 - sdl.sync_num)*interval_time;
  713. }
  714. if (deltaT < 10 && deltaT > 0)
  715. {
  716. LOCATION_SYSTEM_BRANCH(LOCATION_SYSTEM_BRANCH_155);
  717. this->b_long_interval = false;
  718. }
  719. //避免同一个同步序号下存在多个不同卡序号
  720. if (deltaT < 1E-2)
  721. {
  722. LOCATION_SYSTEM_BRANCH(LOCATION_SYSTEM_BRANCH_156);
  723. deltaT = 0.2;
  724. }
  725. cvx = (cx - sdl.x)*this->map_scale/deltaT;
  726. cvy = (cy - sdl.y)*this->map_scale/deltaT;
  727. double avx = (cvx - sdl.vx) / deltaT;
  728. double avy = (cvy - sdl.vy) / deltaT;
  729. double av = sqrt(pow(avx,2) + pow(avy,2));
  730. //车卡的加速度
  731. switch(this->card_type){
  732. case CT_PERSON:
  733. LOCATION_SYSTEM_BRANCH(LOCATION_SYSTEM_BRANCH_157);
  734. if(av > PERSON_ACCELERATE_THRESHOLD){
  735. LOCATION_SYSTEM_BRANCH(LOCATION_SYSTEM_BRANCH_158);
  736. this->x = this->last_locate.x;
  737. this->y = this->last_locate.y;
  738. this->b_long_interval = false;
  739. pRdm->clear();
  740. m_afmData.bStatus = true;
  741. m_afmData.strCardId = this->card_id;
  742. m_afmData.nType = ALGO_FAILED_CONDITION_5;
  743. ALGORITHM_FAILED(ALGO_FAILED_CONDITION_5);
  744. return;
  745. }
  746. break;
  747. case CT_VEHICLE:
  748. LOCATION_SYSTEM_BRANCH(LOCATION_SYSTEM_BRANCH_159);
  749. if(av > VECHILE_ACCELERATE_THRESHOLD){
  750. LOCATION_SYSTEM_BRANCH(LOCATION_SYSTEM_BRANCH_160);
  751. //保留上次结果
  752. this->x = this->last_locate.x;
  753. this->y = this->last_locate.y;
  754. this->b_long_interval = false;
  755. pRdm->clear();
  756. m_afmData.bStatus = true;
  757. m_afmData.strCardId = this->card_id;
  758. m_afmData.nType = ALGO_FAILED_CONDITION_6;
  759. ALGORITHM_FAILED(ALGO_FAILED_CONDITION_6);
  760. return;
  761. }
  762. break;
  763. }
  764. deltaT = 0;
  765. cvx = cvy = 0;
  766. }
  767. //从队列尾部开始查找,找到第一个同步序号与当前计算卡的同步序号相差5个以上的数据
  768. list<sync_data>::reverse_iterator it;
  769. bool bOverflow = false;
  770. for(it = m_syncNumList.rbegin();it!=m_syncNumList.rend();it++){
  771. if(maxSyncTimes - it->sync_num >= 5){
  772. LOCATION_SYSTEM_BRANCH(LOCATION_SYSTEM_BRANCH_161);
  773. sd = *it;
  774. break;
  775. }
  776. else{
  777. if(maxSyncTimes - it->sync_num < 0 && maxSyncTimes < 100){
  778. LOCATION_SYSTEM_BRANCH(LOCATION_SYSTEM_BRANCH_162);
  779. //如果最新同步号小于列表中的同步号则
  780. if(maxSyncTimes + 65536 - it->sync_num >=5 ){//5
  781. LOCATION_SYSTEM_BRANCH(LOCATION_SYSTEM_BRANCH_163);
  782. bOverflow = true;
  783. sd = *it;
  784. break;
  785. }
  786. }else{
  787. continue;
  788. }
  789. }
  790. }
  791. //根据溢出条件来计算deltaT
  792. if(bOverflow){
  793. LOCATION_SYSTEM_BRANCH(LOCATION_SYSTEM_BRANCH_164);
  794. deltaT = (maxSyncTimes + 65536 - sd.sync_num)*interval_time;
  795. }else{
  796. LOCATION_SYSTEM_BRANCH(LOCATION_SYSTEM_BRANCH_165);
  797. deltaT = (maxSyncTimes - sd.sync_num)*interval_time;
  798. }
  799. //速度正负的判断:以x轴,y轴正向运动为正
  800. //如果x相等,则y2 - y1 > 0为正
  801. //其他情况,则x2 - x1 > 0 为正
  802. if(cx == sd.x){
  803. if(cy > sd.y){
  804. nSign = 1;
  805. }else{
  806. nSign = -1;
  807. }
  808. }else{
  809. if(cx > sd.x){
  810. nSign = 1;
  811. }else{
  812. nSign = -1;
  813. }
  814. }
  815. #ifdef ALGORITHM_TYPE_INS
  816. uwb_direction = (nSign == 1)?1:-1; //uwb形式判断不出静止或者怠速
  817. #endif
  818. //使用间隔来修正速度
  819. if(deltaT - 1.0 >= 0){
  820. LOCATION_SYSTEM_BRANCH(LOCATION_SYSTEM_BRANCH_166);
  821. if (deltaT > 10)
  822. {
  823. LOCATION_SYSTEM_BRANCH(LOCATION_SYSTEM_BRANCH_167);
  824. this->b_long_interval = true;
  825. }
  826. //转为m/s
  827. cvx = (cx - sd.x)*this->map_scale/deltaT;
  828. cvy = (cy - sd.y)*this->map_scale/deltaT;
  829. cv = sqrt(pow(cvx,2) + pow(cvy,2));
  830. cv = cv*nSign;
  831. double avx = (cvx - sd.vx) / deltaT;
  832. double avy = (cvy - sd.vy) / deltaT;
  833. double av = sqrt(pow(avx,2) + pow(avy,2));
  834. //车卡的加速度
  835. switch(this->card_type){
  836. case CT_PERSON:
  837. LOCATION_SYSTEM_BRANCH(LOCATION_SYSTEM_BRANCH_168);
  838. if(av > PERSON_ACCELERATE_THRESHOLD){
  839. LOCATION_SYSTEM_BRANCH(LOCATION_SYSTEM_BRANCH_169);
  840. this->x = this->last_locate.x;
  841. this->y = this->last_locate.y;
  842. pRdm->clear();
  843. m_afmData.bStatus = true;
  844. m_afmData.strCardId = this->card_id;
  845. m_afmData.nType = ALGO_FAILED_CONDITION_5;
  846. ALGORITHM_FAILED(ALGO_FAILED_CONDITION_5);
  847. return;
  848. }
  849. break;
  850. case CT_VEHICLE:
  851. LOCATION_SYSTEM_BRANCH(LOCATION_SYSTEM_BRANCH_170);
  852. if(av > VECHILE_ACCELERATE_THRESHOLD){
  853. LOCATION_SYSTEM_BRANCH(LOCATION_SYSTEM_BRANCH_171);
  854. //保留上次结果
  855. this->x = this->last_locate.x;
  856. this->y = this->last_locate.y;
  857. pRdm->clear();
  858. m_afmData.bStatus = true;
  859. m_afmData.strCardId = this->card_id;
  860. m_afmData.nType = ALGO_FAILED_CONDITION_6;
  861. ALGORITHM_FAILED(ALGO_FAILED_CONDITION_6);
  862. return;
  863. }
  864. break;
  865. }
  866. this->last_locate.acceleration = av;
  867. this->last_vx = cvx;
  868. this->last_vy = cvy;
  869. cv = cv*3.6;
  870. LOCATION_SYSTEM_BRANCH(LOCATION_SYSTEM_BRANCH_172);
  871. //速度的限制
  872. if(fabs(cv) > MAX_VECHILE_SPEED){
  873. LOCATION_SYSTEM_BRANCH(LOCATION_SYSTEM_BRANCH_173);
  874. this->x = this->last_locate.x;
  875. this->y = this->last_locate.y;
  876. pRdm->clear();
  877. m_afmData.bStatus = true;
  878. m_afmData.strCardId = this->card_id;
  879. m_afmData.nType = ALGO_FAILED_CONDITION_7;
  880. ALGORITHM_FAILED(ALGO_FAILED_CONDITION_7);
  881. return;
  882. }
  883. //删除第一个元素到tmp(含)之间的所有元素
  884. bool bStartDel = false;
  885. for(list<sync_data>::reverse_iterator tmp = m_syncNumList.rbegin();tmp != m_syncNumList.rend();)
  886. {
  887. if(bStartDel){
  888. tmp = list<sync_data>::reverse_iterator(m_syncNumList.erase((++tmp).base()));
  889. }else{
  890. if(*tmp == sd){
  891. bStartDel = true;
  892. }
  893. ++tmp;
  894. }
  895. }
  896. //更新值为当前值并插入队列
  897. sdNew.sync_num = maxSyncTimes;
  898. this->m_nSyncNumInList = sd.sync_num;
  899. sdNew.vx = cvx;
  900. sdNew.vy = cvy;
  901. sdNew.update = true;
  902. }else{
  903. cv = this->origin_locate.v;
  904. }
  905. }
  906. }
  907. #ifdef ALGORITHM_TYPE_INS
  908. else{
  909. LOCATION_SYSTEM_BRANCH(LOCATION_SYSTEM_BRANCH_174);
  910. uwb_direction = ins_direction;
  911. }
  912. #endif
  913. #ifdef ALGORITHM_TYPE_INS
  914. bRet = false;
  915. //0,1,2分别表示静止(含怠速),运动,运动中刹车
  916. switch (acce_state)
  917. {
  918. case 0:
  919. LOCATION_SYSTEM_BRANCH(LOCATION_SYSTEM_BRANCH_175);
  920. acce_direction = 0;
  921. if(acce_state_last!=0){
  922. LOCATION_SYSTEM_BRANCH(LOCATION_SYSTEM_BRANCH_176);
  923. //说明从其他状态变为静止(怠速)状态
  924. //返回这次定位结果
  925. if (bOriginLocate)
  926. {
  927. LOCATION_SYSTEM_BRANCH(LOCATION_SYSTEM_BRANCH_177);
  928. bDirectReturn = true; //直接返回本次定位结果
  929. }
  930. }else{
  931. //返回上一次定位结果
  932. LOCATION_SYSTEM_BRANCH(LOCATION_SYSTEM_BRANCH_178);
  933. bDirectReturn = false;
  934. }
  935. bUseKalman = false;
  936. //bRet = true;
  937. break;
  938. case 1://需要判断状态是否发生了变化
  939. LOCATION_SYSTEM_BRANCH(LOCATION_SYSTEM_BRANCH_179);
  940. if (acce_state_last == ACCELERATE_INIT_STATE)
  941. {
  942. LOCATION_SYSTEM_BRANCH(LOCATION_SYSTEM_BRANCH_180);
  943. //第一次获得卡的状态,需要确定首次方向
  944. acce_direction = uwb_direction; //将UWB定出的速度方向赋值给它
  945. }else{
  946. LOCATION_SYSTEM_BRANCH(LOCATION_SYSTEM_BRANCH_181);
  947. acce_direction = acce_state_last;
  948. }
  949. //bRet = false;
  950. break;
  951. case 2:
  952. case 3:
  953. LOCATION_SYSTEM_BRANCH(LOCATION_SYSTEM_BRANCH_182);
  954. acce_direction = 0; //此处将速度方向设为静止,也就是0
  955. //bRet = false;
  956. break;
  957. }
  958. if (acce_state!=acce_last_state)
  959. {
  960. // 如果状态发生了改变,则加速度计的权重需要重置
  961. LOCATION_SYSTEM_BRANCH(LOCATION_SYSTEM_BRANCH_183);
  962. this->ins_weight = INS_WEIGHT;
  963. }
  964. double cweight = 0;
  965. if(!bRet){
  966. LOCATION_SYSTEM_BRANCH(LOCATION_SYSTEM_BRANCH_184);
  967. cweight = this->ins_weight * acce_direction + this->uwb_weight*uwb_direction;
  968. //如果计算出的权重在合适范围内,就逐渐降低惯导的权限
  969. //如果计算出的权重超过范围,则重置惯导的权重为90%
  970. if(cweight>=INS_WEIGHT*-1&&cweight<=INS_WEIGHT){
  971. LOCATION_SYSTEM_BRANCH(LOCATION_SYSTEM_BRANCH_185);
  972. cweight = abs(cweight);
  973. }else{
  974. LOCATION_SYSTEM_BRANCH(LOCATION_SYSTEM_BRANCH_186);
  975. cweight = INS_WEIGHT;
  976. }
  977. if(cweight*uwb_direction < 0){
  978. //惯导和uwb定位方向不一致,定位失败
  979. //增加一次判断,与上一次的进行比对,如果和上一次的一致,则送进卡尔曼滤波
  980. LOCATION_SYSTEM_BRANCH(LOCATION_SYSTEM_BRANCH_187);
  981. if(cweight*acce_state_last > 0){
  982. LOCATION_SYSTEM_BRANCH(LOCATION_SYSTEM_BRANCH_188);
  983. bUseKalman = true;
  984. }else{
  985. LOCATION_SYSTEM_BRANCH(LOCATION_SYSTEM_BRANCH_189);
  986. bUseKalman = false;
  987. }
  988. }else{
  989. //惯导和uwb定位方向一致,定位成功
  990. //送进卡尔曼滤波
  991. LOCATION_SYSTEM_BRANCH(LOCATION_SYSTEM_BRANCH_190);
  992. bUseKalman = true;
  993. }
  994. }
  995. if (abs(cweight) > 0)
  996. {
  997. LOCATION_SYSTEM_BRANCH(LOCATION_SYSTEM_BRANCH_191);
  998. ins_direction = cweight/abs(cweight) > 0?1:-1;
  999. }else{
  1000. //如果加速度计的状态权重为零
  1001. LOCATION_SYSTEM_BRANCH(LOCATION_SYSTEM_BRANCH_192);
  1002. ins_direction = uwb_direction;
  1003. }
  1004. this->ins_weight = cweight;
  1005. this->ins_direction = ins_direction;
  1006. this->acce_last_state = acce_last_state;
  1007. //this->accelerate_state_last = this->acce_cur_state = acce_state;
  1008. #endif
  1009. this->accelerate_state_last = this->acce_cur_state = acce_state;
  1010. if(m_nFilterType == FILTER_KALMAN){
  1011. LOCATION_SYSTEM_BRANCH(LOCATION_SYSTEM_BRANCH_193);
  1012. #ifdef ALGORITHM_TYPE_INS
  1013. if(bUseKalman){
  1014. #endif
  1015. LOCATION_SYSTEM_BRANCH(LOCATION_SYSTEM_BRANCH_194);
  1016. //也需要考虑轮回
  1017. //double kalman_detal_t = (maxSyncTimes - this->last_locate.st)*interval_time;
  1018. double kalman_detal_t = 0;
  1019. if (maxSyncTimes - this->last_locate.sync_num < 0 && maxSyncTimes < 100)
  1020. {
  1021. kalman_detal_t = (maxSyncTimes + 65535 - this->last_locate.sync_num)*interval_time;
  1022. }else{
  1023. kalman_detal_t = (maxSyncTimes - this->last_locate.sync_num)*interval_time;
  1024. LOCATION_SYSTEM_BRANCH(LOCATION_SYSTEM_BRANCH_195);
  1025. }
  1026. //通过卡尔曼滤波处理
  1027. if(p->posx == INVALID_COORDINATE && p->posy == INVALID_COORDINATE){
  1028. m_afmData.nCardStamp = m_nCalcSyncNum;
  1029. LOCATION_SYSTEM_BRANCH(LOCATION_SYSTEM_BRANCH_196);
  1030. if(this->m_pKalmanFilter->m_nCounts < 3 || this->m_pKalmanFilter->m_pCar->P(0,0) > 2 || kalman_detal_t > 3){
  1031. //P(0,0):连续时间(大于2s)都定位失败
  1032. //deltaT>3:距离上次成功定位时间间隔为3s
  1033. LOCATION_SYSTEM_BRANCH(LOCATION_SYSTEM_BRANCH_197);
  1034. this->x = this->last_locate.x;
  1035. this->y = this->last_locate.y;
  1036. this->z = 0;
  1037. pRdm->clear();
  1038. m_afmData.bStatus = true;
  1039. m_afmData.strCardId = this->card_id;
  1040. this->m_pKalmanFilter->m_bFlag = false;
  1041. m_afmData.nType = ALGO_FAILED_CONDITION_8;
  1042. ALGORITHM_FAILED(ALGO_FAILED_CONDITION_8);
  1043. return;
  1044. }
  1045. if(this->m_pKalmanFilter->m_nCounts >= 3){
  1046. //只有三次以上才允许使用kalman滤波以下的函数
  1047. LOCATION_SYSTEM_BRANCH(LOCATION_SYSTEM_BRANCH_198);
  1048. this->m_pKalmanFilter->Predict(kalman_detal_t);
  1049. this->z = 0;
  1050. sdNew.update = true;
  1051. }
  1052. }else{
  1053. LOCATION_SYSTEM_BRANCH(LOCATION_SYSTEM_BRANCH_199);
  1054. this->m_pKalmanFilter->m_bFlag = true;
  1055. this->m_pKalmanFilter->m_nCounts++;
  1056. this->m_pKalmanFilter->m_pCar->z(0,0) = cx * this->map_scale;
  1057. this->m_pKalmanFilter->m_pCar->z(1,0) = cvx;
  1058. this->m_pKalmanFilter->m_pCar->z(2,0) = cy * this->map_scale;
  1059. this->m_pKalmanFilter->m_pCar->z(3,0) = cvy;
  1060. if(this->m_pKalmanFilter->m_nCounts == 1){
  1061. //第一次直接赋值
  1062. LOCATION_SYSTEM_BRANCH(LOCATION_SYSTEM_BRANCH_200);
  1063. this->m_pKalmanFilter->m_pCar->x = this->m_pKalmanFilter->m_pCar->z;
  1064. }
  1065. if(this->m_pKalmanFilter->m_nCounts == 2){
  1066. //两次处理
  1067. LOCATION_SYSTEM_BRANCH(LOCATION_SYSTEM_BRANCH_201);
  1068. 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;
  1069. 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;
  1070. this->m_pKalmanFilter->m_pCar->x = this->m_pKalmanFilter->m_pCar->z;
  1071. }
  1072. if(this->m_pKalmanFilter->m_nCounts >= 3){
  1073. LOCATION_SYSTEM_BRANCH(LOCATION_SYSTEM_BRANCH_202);
  1074. //只有三次以上才允许使用kalman滤波以下的函数
  1075. //this->m_pKalmanFilter->Predict_Correct(deltaT);
  1076. this->m_pKalmanFilter->Predict_Correct(kalman_detal_t);
  1077. sdNew.update = true;
  1078. if(deltaT!=0){
  1079. LOCATION_SYSTEM_BRANCH(LOCATION_SYSTEM_BRANCH_203);
  1080. this->m_pKalmanFilter->m_pCar->x(1,0) = (this->m_pKalmanFilter->m_pCar->x(0,0) - sd.x*this->map_scale)/deltaT;
  1081. this->m_pKalmanFilter->m_pCar->x(3,0) = (this->m_pKalmanFilter->m_pCar->x(2,0) - sd.y*this->map_scale)/deltaT;
  1082. }
  1083. }
  1084. }
  1085. if(p->nFirstReader == -1 && p->nSecondReader == -1){
  1086. LOCATION_SYSTEM_BRANCH(LOCATION_SYSTEM_BRANCH_204);
  1087. p->nFirstReader = last_s_locate_reader[0];
  1088. p->nSecondReader = last_s_locate_reader[1];
  1089. }
  1090. //增加地图集的判定,判断定位结果是否在地图集上
  1091. //如果不在地图集上,需要再次定位
  1092. //需要带出定位结果的分站信息,
  1093. //利用地图集中分站信息再次定位
  1094. //std::shared_ptr<POS> kalman_p(new POS());
  1095. std::shared_ptr<POS> kalman_p = std::make_shared<POS>();
  1096. kalman_p->nFirstReader = p->nFirstReader;
  1097. kalman_p->nSecondReader = p->nSecondReader;
  1098. kalman_p->posx = this->m_pKalmanFilter->m_pCar->x(0,0);//* this->map_scale
  1099. kalman_p->posy = this->m_pKalmanFilter->m_pCar->x(2,0);//* this->map_scale
  1100. kalman_p->pos_radius = p->pos_radius;
  1101. //二维定位不需要再判定在地图集上了
  1102. if(!LocateAlgorithm::IsOnMap(kalman_p,pTdoaReaderPathMap)){
  1103. //再一次定位到地图集上
  1104. LOCATION_SYSTEM_BRANCH(LOCATION_SYSTEM_BRANCH_205);
  1105. //std::shared_ptr<POS> cp = LocateAlgorithm::Pos(kalman_p,pTdoaReaderPathMap);
  1106. // 定位结果在地图集外,映射到地图集上
  1107. std::shared_ptr<POS> cp = LocateAlgorithm::MappingToPath(kalman_p,pTdoaReaderPathMap);
  1108. if(cp != nullptr){
  1109. if (cp->posx != INVALID_COORDINATE && cp->posy !=INVALID_COORDINATE)
  1110. {
  1111. LOCATION_SYSTEM_BRANCH(LOCATION_SYSTEM_BRANCH_206);
  1112. this->m_pKalmanFilter->m_pCar->x(0,0) = cp->posx;
  1113. this->m_pKalmanFilter->m_pCar->x(2,0) = cp->posy;
  1114. }
  1115. }
  1116. cp.reset();
  1117. }
  1118. this->x = this->m_pKalmanFilter->m_pCar->x(0,0) / this->map_scale;
  1119. this->y = this->m_pKalmanFilter->m_pCar->x(2,0) / this->map_scale;
  1120. if (abs(this->last_locate.x) > 1E-5 || abs(this->last_locate.y) > 1E-5)
  1121. {
  1122. //去除采集刚启动,last_locate的坐标为零而导致判断车辆上下行方向错误的问题
  1123. //车辆上下行确定
  1124. //3个条件:起点(x1,y1),终点(x2,y2)
  1125. //1.x1==x2的情况下,y2>y1为下行
  1126. //2.y1==y2的情况下,x1>x2为下行
  1127. //3.x1>x2且y2>y1为下行
  1128. //其他情况为上行
  1129. if ((this->last_locate.x == this->x && this->y > this->last_locate.y)
  1130. ||(this->last_locate.x > this->x && this->y == this->last_locate.y)
  1131. ||(this->last_locate.x > this->x && this->y > this->last_locate.y))
  1132. {
  1133. this->m_nStream = DOWN_STREAM;
  1134. }
  1135. else
  1136. {
  1137. this->m_nStream = UP_STREAM;
  1138. }
  1139. }
  1140. nSign = 1;
  1141. if(this->m_pKalmanFilter->m_pCar->x(1,0) == 0){
  1142. if(this->m_pKalmanFilter->m_pCar->x(3,0)>0){
  1143. nSign = 1;
  1144. }
  1145. }else{
  1146. if(this->m_pKalmanFilter->m_pCar->x(1,0) > 0){
  1147. nSign = 1;
  1148. }else{
  1149. nSign = -1;
  1150. }
  1151. }
  1152. this->m_nMoveDirection = nSign;
  1153. //algo_calc_offset();
  1154. this->last_locate.x = this->x;
  1155. this->last_locate.y = this->y;
  1156. this->last_locate.z = this->z;
  1157. this->last_locate.sync_num = maxSyncTimes;
  1158. last_s_locate_reader[0] = p->nFirstReader;
  1159. last_s_locate_reader[1] = p->nSecondReader;
  1160. this->m_pKalmanFilter->m_pCar->t = this->m_nLastLocateT = maxSyncTimes;
  1161. //速度的计算采用求平均的方式
  1162. double speed = sqrt(pow(this->m_pKalmanFilter->m_pCar->x(1,0),2) + pow(this->m_pKalmanFilter->m_pCar->x(3,0),2));
  1163. speed *=3.6; //转为km/h
  1164. if (vt_his_speed.size()==3)//10
  1165. {
  1166. vt_his_speed.pop_front();
  1167. }
  1168. vt_his_speed.push_back(speed);
  1169. int total = 0;
  1170. double sum_speed = 0;
  1171. for (list<double>::iterator it = vt_his_speed.begin();it != vt_his_speed.end();++it)
  1172. {
  1173. if (*it > 0)
  1174. {
  1175. sum_speed += *it;
  1176. total++;
  1177. }
  1178. }
  1179. double av = 0;
  1180. if (total>0)
  1181. {
  1182. av = sum_speed / total;
  1183. }
  1184. if (this->acce_cur_state == STATE_ACCE_STATIC)
  1185. {
  1186. this->v = 0;
  1187. }
  1188. else
  1189. {
  1190. this->v = av*nSign;
  1191. }
  1192. cvx = this->m_pKalmanFilter->m_pCar->x(1,0);
  1193. cvy = this->m_pKalmanFilter->m_pCar->x(3,0);
  1194. this->last_locate.v = this->v;
  1195. this->origin_locate.v = cv;
  1196. //如果是第一次定位,则下一次即为非第一次定位了
  1197. if (this->is_algo_first_location)
  1198. {
  1199. this->is_algo_first_location = false;
  1200. }
  1201. ALGORITHM_FAILED(ALGO_LOC_SUCCESSED);
  1202. LOCATION_SYSTEM_BRANCH(LOCATION_SYSTEM_BRANCH_207);
  1203. if(sdNew.update){
  1204. sdNew.sync_num = maxSyncTimes;
  1205. sdNew.x = this->x;
  1206. sdNew.y = this->y;
  1207. sdNew.vx = cvx;
  1208. sdNew.vy = cvy;
  1209. m_syncNumList.push_back(sdNew);
  1210. LOCATION_SYSTEM_BRANCH(LOCATION_SYSTEM_BRANCH_208);
  1211. }
  1212. //校验机会3次,3次内跳回去就丢弃此次计算结果
  1213. if (this->x != INVALID_COORDINATE && this->y != INVALID_COORDINATE)
  1214. {
  1215. int nDirection = 0;
  1216. if (!algo_is_same_direction(this->x,this->y,0))
  1217. {
  1218. this->x = this->last_locate.x;
  1219. this->y = this->last_locate.y;
  1220. }
  1221. }
  1222. #ifdef ALGORITHM_TYPE_INS
  1223. }else{
  1224. //这组数据的处理方法是:
  1225. //如果第一次成功定位,但状态是静止,就取第一次成功定位值(这是为了处理当第一次成功定位,但状态是静止的,此时取上一次定位值为零的问题),
  1226. //后续如果不管定位成功还是失败,只要状态是静止的,就输出上一次成功定位值,并更新同步序号。
  1227. //取上次结果
  1228. LOCATION_SYSTEM_BRANCH(LOCATION_SYSTEM_BRANCH_209);
  1229. if (bDirectReturn)
  1230. {
  1231. LOCATION_SYSTEM_BRANCH(LOCATION_SYSTEM_BRANCH_210);
  1232. if(this->map_scale > 0){
  1233. LOCATION_SYSTEM_BRANCH(LOCATION_SYSTEM_BRANCH_211);
  1234. this->last_locate.x = this->x = p->posx / (this->map_scale*1.0);
  1235. this->last_locate.y = this->y = p->posy / (this->map_scale*1.0);
  1236. this->last_locate.z = this->z = p->posz / (this->map_scale*1.0);
  1237. this->last_locate.v = this->v = cv;
  1238. }
  1239. }else{
  1240. LOCATION_SYSTEM_BRANCH(LOCATION_SYSTEM_BRANCH_212);
  1241. this->x = this->last_locate.x;
  1242. this->y = this->last_locate.y;
  1243. this->z = -5;//this->last_locate.z
  1244. this->v = this->last_locate.v;
  1245. }
  1246. this->m_nLastLocateT = this->m_nCalcSyncNum = this->last_locate.st = this->sync_num = maxSyncTimes;
  1247. if(sdNew.update){
  1248. LOCATION_SYSTEM_BRANCH(LOCATION_SYSTEM_BRANCH_213);
  1249. sdNew.sync_num = maxSyncTimes;
  1250. sdNew.x = this->x;
  1251. sdNew.y = this->y;
  1252. sdNew.vx = cvx;
  1253. sdNew.vy = cvy;
  1254. m_syncNumList.push_back(sdNew);
  1255. this->m_nSyncNumInList = maxSyncTimes;
  1256. }
  1257. this->is_deal_by_algo = true;
  1258. }
  1259. #endif
  1260. }else{
  1261. //最新通过算法算出的结果
  1262. LOCATION_SYSTEM_BRANCH(LOCATION_SYSTEM_BRANCH_214);
  1263. if(p->posx == INVALID_COORDINATE || p->posy == INVALID_COORDINATE){
  1264. LOCATION_SYSTEM_BRANCH(LOCATION_SYSTEM_BRANCH_215);
  1265. this->x = this->last_locate.x;
  1266. this->y = this->last_locate.y;
  1267. this->z = this->last_locate.z;
  1268. }else{
  1269. LOCATION_SYSTEM_BRANCH(LOCATION_SYSTEM_BRANCH_216);
  1270. if(this->map_scale > 0){
  1271. LOCATION_SYSTEM_BRANCH(LOCATION_SYSTEM_BRANCH_216);
  1272. this->x = p->posx / (this->map_scale*1.0);
  1273. this->y = p->posy / (this->map_scale*1.0);
  1274. this->z = p->posz / (this->map_scale*1.0);
  1275. }
  1276. double speed = (fabs(this->v) + fabs(cv))/2;
  1277. this->v = speed*nSign;
  1278. this->last_locate.x = this->x;
  1279. this->last_locate.y = this->y;
  1280. this->last_locate.z = this->z;
  1281. this->last_locate.v = this->v;
  1282. this->m_nLastLocateT = this->last_locate.sync_num = maxSyncTimes;
  1283. }
  1284. this->a = 0;
  1285. }
  1286. }
  1287. LOCATION_SYSTEM_BRANCH(LOCATION_SYSTEM_BRANCH_217);
  1288. pRdm->clear();
  1289. }
  1290. int Card::algo_tdoa_1d(int cnt)
  1291. {
  1292. int ret = 0;
  1293. //1.数据有效性判断
  1294. ret = CheckDistData(cnt);
  1295. if(ret){
  1296. return ret;
  1297. }
  1298. //2.组装数据
  1299. std::shared_ptr<ReceiveDataMap> pRdm = std::make_shared<ReceiveDataMap>();
  1300. pRdm->clear();
  1301. ret = AssembleDistData(pRdm);
  1302. if(ret){
  1303. return ret;
  1304. }
  1305. // 获取两次定位的时间差数据
  1306. GetDeltaT(mp_dists_locate);
  1307. //3.算法定位
  1308. std::shared_ptr<POS> pos = std::make_shared<POS>();
  1309. std::vector<std::shared_ptr<POS>> udm_pos;
  1310. udm_pos.resize(0);
  1311. ret = LocateAlgorithm::CalcTdoaPosition(pRdm,pTdoaReaderPathMap,udm_pos);
  1312. if (ret == 0)
  1313. {
  1314. //4.从多解中筛选出一个解,存在两种可能:
  1315. //a.可能无解返回非0,
  1316. //b.可能有解,但解不正确,比如解的位置在4727,-100,但选出的解是4727,-200
  1317. ret = ChooseOneSolution(pRdm, udm_pos, pos);
  1318. if (IsIdleStatus(pos))
  1319. {
  1320. acce_cur_state = ACCELERATE_STATE::STATE_ACCE_IDLING; //怠速状态
  1321. #ifdef _DEBUG
  1322. char msg[100] = {0};
  1323. sprintf_s(msg,"card:%s is idling,the card's ct is %d",card_id.c_str(),time_stamp_cal);
  1324. Log::write_log(FILE_TYPE::SYS_S,msg,true);
  1325. #endif
  1326. }
  1327. //无论正确与否,保存原始值
  1328. SaveOriginDataBeforeFilter(pos);
  1329. if (ret == 0)
  1330. {
  1331. //5.如果有解,则对唯一解做合法性验证
  1332. //主要验证条件为:加速度和速度
  1333. ret = CheckSolution(pos);
  1334. }
  1335. }
  1336. //6.卡尔曼滤波,两种情况:
  1337. //a.解通过了合法性验证,b.无解,但在2s内
  1338. //则使用卡尔曼滤波进行滤波处理
  1339. if (FILTER_KALMAN == m_nFilterType)
  1340. {
  1341. ret = KalmanFilterProcess(pos);
  1342. }
  1343. bool bRet = LocateAlgorithm::IsOnMap(pos,pTdoaReaderPathMap);
  1344. if (bRet)
  1345. {
  1346. //7.数据保存
  1347. ret = SaveCardAlgoData(pos);
  1348. }
  1349. return ret;
  1350. }
  1351. int Card::algo_tdoa_2d(int cnt)
  1352. {
  1353. int ret = 0;
  1354. //1.数据有效性判断
  1355. ret = CheckDistData(cnt);
  1356. if(ret) return ret;
  1357. //2.组装数据
  1358. std::shared_ptr<ReceiveDataMap> pRdm = std::make_shared<ReceiveDataMap>();
  1359. pRdm->clear();
  1360. ret = AssembleDistData(pRdm);
  1361. if(ret) return ret;
  1362. //3.算法定位
  1363. std::shared_ptr<POS> pos = std::make_shared<POS>();
  1364. pos = LocateAlgorithm::TdoaLocate2d(pRdm);
  1365. if (pos == nullptr)
  1366. {
  1367. return 0;
  1368. }else{
  1369. pos->cx = pos->posx / (map_scale*1.0);
  1370. pos->cy = pos->posy / (map_scale*1.0);
  1371. pos->cz = pos->posz / (map_scale*1.0);
  1372. isoutput = true;
  1373. }
  1374. //5.唯一解合法性验证
  1375. ret = CheckSolution(pos);
  1376. if(ret) {
  1377. return ret;
  1378. }
  1379. //保存原始值
  1380. SaveOriginDataBeforeFilter(pos);
  1381. //6.滤波
  1382. if (FILTER_KALMAN == m_nFilterType)
  1383. {
  1384. ret = KalmanFilterProcess(pos);
  1385. }
  1386. if (ret)
  1387. {
  1388. return ret;
  1389. }
  1390. //7.数据保存
  1391. ret = SaveCardAlgoData(pos);
  1392. if (ret)
  1393. {
  1394. return ret;
  1395. }
  1396. return ret;
  1397. }
  1398. int Card::algo_tdoa_3d(int cnt)
  1399. {
  1400. if (cnt < 3)
  1401. {
  1402. return 1;
  1403. }
  1404. int sync_num = 0;
  1405. int k = 0;
  1406. bool ret = false;
  1407. mp_dists_locate.clear();
  1408. for (DistMap::iterator it = _dists.front().distmap.begin();it!=_dists.front().distmap.end();++it,k++)
  1409. {
  1410. if (k==0)
  1411. {
  1412. sync_num = it->second->sync_num;
  1413. }
  1414. else
  1415. {
  1416. int diff = abs(sync_num - it->second->sync_num);
  1417. if (diff >= 5)
  1418. {
  1419. ret = true;
  1420. break;
  1421. }
  1422. }
  1423. if (it->second->tt == LLONG_MAX)
  1424. {
  1425. ret = true;
  1426. break;
  1427. }
  1428. mp_dists_locate.insert(make_pair(it->second->tt,it->second));
  1429. }
  1430. if (ret)
  1431. {
  1432. return 1;
  1433. }
  1434. //构造数据
  1435. int i = 0;
  1436. int max_sync_time = 0;
  1437. std::shared_ptr<ReceiveDataMap> pRdm = std::make_shared<ReceiveDataMap>();
  1438. pRdm->clear();
  1439. for (map<unsigned long long ,std::shared_ptr<_coordinate>>::iterator it = mp_dists_locate.begin();it!=mp_dists_locate.end();++it)
  1440. {
  1441. int card_time_stamp = 0;
  1442. if (i==0)
  1443. {
  1444. max_sync_time = it->second->sync_num;
  1445. card_time_stamp = it->second->t;
  1446. }else{
  1447. if (max_sync_time < it->second->sync_num)
  1448. {
  1449. max_sync_time = it->second->sync_num;
  1450. card_time_stamp = it->second->t;
  1451. }
  1452. }
  1453. ReceiveDataMap::iterator rdm_it = pRdm->find(it->second->tt);
  1454. if (rdm_it == pRdm->end())
  1455. {
  1456. //保存信息用于定位
  1457. std::shared_ptr<ReceiveData> prd = std::make_shared<ReceiveData>();
  1458. prd->reader_id = it->second->reader_id;
  1459. prd->antenna_id = it->second->antenna_id;
  1460. prd->rec_time_stamp = it->second->tt;
  1461. prd->x = it->second->x*this->map_scale;
  1462. prd->y = it->second->y*this->map_scale;
  1463. prd->z = it->second->z*this->map_scale;
  1464. prd->special = it->second->special;
  1465. if (prd->rec_time_stamp > 0)
  1466. {
  1467. pRdm->insert(make_pair(prd->rec_time_stamp,prd));
  1468. }
  1469. }
  1470. i++;
  1471. }
  1472. //if (time_stamp_last !=0 && time_stamp_last > time_stamp_cal)
  1473. //{
  1474. // if ((time_stamp_last + time_stamp_cal)%65535 < 2)
  1475. // {
  1476. // return;
  1477. // }
  1478. //}
  1479. if (pRdm->size() >2)
  1480. {
  1481. m_nCalcSyncNum = max_sync_time;
  1482. std::shared_ptr<POS> p = nullptr;
  1483. //p = LocateAlgorithm::Pos(pRdm);
  1484. }
  1485. return 0;
  1486. }
  1487. /*
  1488. * 根据算法计算新位置的坐标以及卡的上一次坐标,计算车辆偏移坐标
  1489. *
  1490. * param
  1491. * 无
  1492. *
  1493. * return
  1494. * 无locatepos
  1495. *
  1496. */
  1497. void Card::algo_calc_offset()
  1498. {
  1499. double offset = CHAMBER_WIDTH/4;
  1500. if (this->last_locate.x == this->x)
  1501. {
  1502. //判断车的方向
  1503. switch (this->m_nMoveDirection)
  1504. {
  1505. case 1:
  1506. right_y = left_y = this->y;
  1507. right_x = this->x - offset;
  1508. left_x = this->x + offset;
  1509. break;
  1510. case -1:
  1511. right_y = left_y = this->y;
  1512. right_x = this->x + offset;
  1513. left_x = this->x - offset;
  1514. break;
  1515. default:
  1516. break;
  1517. }
  1518. }else if(this->last_locate.y == this->y){
  1519. //这个分支单独拆出来是因为斜率为零,求0的倒数是错误
  1520. //判断车的方向
  1521. switch (this->m_nMoveDirection)
  1522. {
  1523. case 1:
  1524. right_x = left_x = this->x;
  1525. right_y = this->y + offset;
  1526. left_y = this->y - offset;
  1527. break;
  1528. case -1:
  1529. right_x = left_x = this->x;
  1530. right_y = this->y - offset;
  1531. left_y = this->y + offset;
  1532. break;
  1533. default:
  1534. break;
  1535. }
  1536. }else{
  1537. double k = (this->y - this->last_locate.y)/(this->x - this->last_locate.x);
  1538. double b1 = this->y - k*this->x;
  1539. double b2 = this->y - 1*this->x/k;
  1540. double right_d = 0;
  1541. double left_d = 0;
  1542. right_d = offset*sqrt(pow(k,2) + 1) + b2 - b1;
  1543. left_d = -1*offset*sqrt(pow(k,2) + 1) + b2 - b1;
  1544. switch (this->m_nMoveDirection)
  1545. {
  1546. case 1:
  1547. this->right_x = k*right_d/(pow(k,2) - 1);
  1548. this->right_y = this->right_x*1/k + b2;
  1549. this->left_x = k*left_d/(pow(k,2) - 1);
  1550. this->left_y = this->left_x*1/k + b2;
  1551. break;
  1552. case -1:
  1553. this->right_x = k*left_d/(pow(k,2) - 1);
  1554. this->right_y = this->right_x*1/k + b2;
  1555. this->left_x = k*right_d/(pow(k,2) - 1);
  1556. this->left_y = this->left_x*1/k + b2;
  1557. break;
  1558. default:
  1559. break;
  1560. }
  1561. }
  1562. }
  1563. bool Card::algo_is_same_direction(double x,double y,double z)
  1564. {
  1565. int sign = 0;
  1566. if(x == last_locate.x){
  1567. if(y > last_locate.y){
  1568. sign = 1;
  1569. }else{
  1570. sign = -1;
  1571. }
  1572. }else{
  1573. if(x > last_locate.x){
  1574. sign = 1;
  1575. }else{
  1576. sign = -1;
  1577. }
  1578. }
  1579. if (sign!=m_nMoveDirection)
  1580. {
  1581. diff_direction_counts++;
  1582. }else{
  1583. diff_direction_counts = 0;
  1584. }
  1585. if (diff_direction_counts > 0 && diff_direction_counts <3)
  1586. {
  1587. return true;
  1588. }else{
  1589. diff_direction_counts = 0;
  1590. return false;
  1591. }
  1592. }
  1593. bool Card::is_pos_state_changed() // 考勤
  1594. {
  1595. bool ret = false;
  1596. if(pos_state != pos_state_old)
  1597. {
  1598. if((PDT_INIT == pos_state_old && PDT_UP == pos_state ) || (PDT_INIT == pos_state && PDT_UP == pos_state_old) )
  1599. {
  1600. pos_state = pos_state_old = PDT_UP;
  1601. pos_state_count = pos_state_confirm_times;
  1602. ret = false;
  1603. }
  1604. else if(pos_state_count >= pos_state_confirm_times)
  1605. {
  1606. pos_state_old = pos_state;
  1607. ret = true;
  1608. }
  1609. }
  1610. return ret ;
  1611. //return (this->last_area_type_id != this->cur_area_type_id && 0 == (this->cur_area_type_id & this->last_area_type_id));
  1612. }
  1613. /*
  1614. * 将数据加入到队列中,但相同卡的接受时间戳的数据满足一定条件时,将相应的数据丢入算法进行定位计算,
  1615. * 计算完后删除相应的数据,释放内存
  1616. *
  1617. * param
  1618. * dist 定位数据
  1619. *
  1620. * return
  1621. * 无
  1622. */
  1623. void Card::add_dist(_coordinate* dist)
  1624. {
  1625. EnterCriticalSection(&m_csCard);
  1626. string s = concat(dist->reader_id, dist->antenna_id);
  1627. int idx = FindDistMap(dist->t);
  1628. if(-1 == idx){
  1629. DistQueMapItem dq;
  1630. dq.cardstamp = dist->t;
  1631. _dists.push_back(dq);
  1632. }else{
  1633. //_dists[idx].distmap[s] = dist;
  1634. }
  1635. if(_dists.size() >= MAX_DIST_CACHE){ // 超过缓存数量限制
  1636. // 计算并删除第一个
  1637. get_coordinate();
  1638. remove_dist_head();
  1639. }
  1640. if (this->x != this->last_x || this->y != this->last_y)
  1641. {
  1642. this->b_pos_change = true;
  1643. }else{
  1644. this->b_pos_change = false;
  1645. }
  1646. LeaveCriticalSection(&m_csCard);
  1647. }
  1648. void Card::add_dist(std::shared_ptr<_coordinate> dist)
  1649. {
  1650. LOCATION_SYSTEM_BRANCH(LOCATION_SYSTEM_BRANCH_134);
  1651. EnterCriticalSection(&m_csCard);
  1652. string s = concat(dist->reader_id, dist->antenna_id);
  1653. int idx = FindDistMap(dist->t);
  1654. if(-1 == idx){
  1655. LOCATION_SYSTEM_BRANCH(LOCATION_SYSTEM_BRANCH_135);
  1656. DistQueMapItem dq;
  1657. dq.cardstamp = dist->t;
  1658. dq.distmap[s] = dist;
  1659. _dists.push_back(dq);
  1660. }else{
  1661. LOCATION_SYSTEM_BRANCH(LOCATION_SYSTEM_BRANCH_136);
  1662. _dists[idx].distmap[s] = dist;
  1663. }
  1664. if(_dists.size() >= MAX_DIST_CACHE){ // 超过缓存数量限制
  1665. // 计算并删除第一个
  1666. LOCATION_SYSTEM_BRANCH(LOCATION_SYSTEM_BRANCH_137);
  1667. get_coordinate();
  1668. _dists.pop_front();
  1669. }
  1670. if (this->x != this->last_x || this->y != this->last_y)
  1671. {
  1672. LOCATION_SYSTEM_BRANCH(LOCATION_SYSTEM_BRANCH_138);
  1673. this->b_pos_change = true;
  1674. }else{
  1675. LOCATION_SYSTEM_BRANCH(LOCATION_SYSTEM_BRANCH_139);
  1676. this->b_pos_change = false;
  1677. }
  1678. LeaveCriticalSection(&m_csCard);
  1679. }
  1680. time_t Card::get_working_time()
  1681. {
  1682. //return (card_type == CT_VEHICLE) ? leave_park_time : down_time;
  1683. return down_time;
  1684. }
  1685. int Card::get_effictive_dist_count( int offset /*= 0*/ )
  1686. {
  1687. if(0 == _dists.size()) return 0;
  1688. time_stamp_cal = _dists.front().cardstamp;
  1689. return (_dists.front().distmap.size());
  1690. }
  1691. /*
  1692. * Card析构类
  1693. */
  1694. Card::~Card(void)
  1695. {
  1696. if(init_postion){
  1697. for(int i = DIST_COUNT - 1;i >= 0;i--){
  1698. if(p_dists_locate[i] != NULL){
  1699. delete p_dists_locate[i];
  1700. p_dists_locate[i] = NULL;
  1701. }
  1702. }
  1703. if(p_dists_locate){
  1704. delete[] p_dists_locate;
  1705. p_dists_locate = NULL;
  1706. }
  1707. }
  1708. _dists.clear();
  1709. DeleteCriticalSection(&m_csCard);
  1710. }
  1711. double Card::get_speed()
  1712. {
  1713. return fabs(v);
  1714. }
  1715. /*
  1716. * 采用TOF或者TDOA算法进行定位计算
  1717. *
  1718. * param
  1719. * cnt ------ _dists数据条数
  1720. *
  1721. * return
  1722. * 无返回值
  1723. *
  1724. */
  1725. void Card::get_coordinate( int cnt )
  1726. {
  1727. #ifdef ALGORITHM_TOF
  1728. algo_tof(cnt);
  1729. #elif defined ALGORITHM_TYPE_TDOA
  1730. //algo_tdoa(cnt);
  1731. int ret = 0;
  1732. ret = algo_tdoa_1d(cnt);
  1733. //algo_tdoa_2d(cnt);
  1734. #else
  1735. #endif
  1736. if (ret == 0)
  1737. {
  1738. inspect_coordinate(this->acce_cur_state);
  1739. }
  1740. if(_isnan(this->x) || _isnan(this->y) || _isnan(this->z)){
  1741. this->x = this->last_x;
  1742. this->y = this->last_y;
  1743. this->z = this->last_z;
  1744. this->output_x = this->x;
  1745. this->output_y = this->y;
  1746. }
  1747. }
  1748. void Card::get_coordinate()
  1749. {
  1750. get_coordinate(get_effictive_dist_count());
  1751. }
  1752. /*
  1753. * 根据状态值获得状态文本描述
  1754. *
  1755. * param
  1756. * 无
  1757. *
  1758. * return
  1759. * 状态文本描述
  1760. *
  1761. */
  1762. std::string Card::get_state_text()
  1763. {
  1764. string ret = "";
  1765. state = 0;
  1766. state_biz = 0;
  1767. if(status_help == STATUS_ERROR){
  1768. state += STATUS_HELP;
  1769. state_biz += STATUS_HELP;
  1770. ret += "呼救,";
  1771. }
  1772. if(status_area_over_time == STATUS_ERROR){
  1773. state += STATUS_AREA_OVER_TIME;
  1774. state_biz += STATUS_AREA_OVER_TIME;
  1775. ret += "区域超时,";
  1776. }else if(status_area_over_time == STATUS_ERROR){
  1777. state += STATUS_OVER_TIME;
  1778. state_biz += STATUS_OVER_TIME;
  1779. ret += "超时,";
  1780. }
  1781. if(status_area_over_speed == STATUS_ERROR){
  1782. state += STATUS_AREA_OVER_SPEED;
  1783. state_biz += STATUS_AREA_OVER_SPEED;
  1784. ret += "区域超速,";
  1785. }else if(status_over_speed == STATUS_ERROR){
  1786. state += STATUS_OVER_SPEED;
  1787. state_biz += STATUS_OVER_SPEED;
  1788. ret += "超速,";
  1789. }
  1790. if(status_area_forbidden == STATUS_ERROR){
  1791. state += STATUS_AREA_FORBIDDEN;
  1792. state_biz += STATUS_AREA_FORBIDDEN;
  1793. ret += "进入限制区域,";
  1794. }
  1795. if(status_call == STATUS_ERROR){
  1796. state += STATUS_CALL;
  1797. state_biz += STATUS_CALL;
  1798. ret += "呼叫,";
  1799. }
  1800. if(status_lost == STATUS_ERROR){
  1801. state += STATUS_LOST;
  1802. state_biz += STATUS_LOST;
  1803. ret += "进入盲区,";
  1804. }
  1805. if(power_state == STATUS_ERROR){
  1806. state += STATUS_POWER_LOWER;
  1807. ret += "电量低,";
  1808. }else if(power_state == STATUS_ERROR_SERIOUS){
  1809. state += STATUS_POWER_LOWER_SERIOUS;
  1810. ret += "电量极低,";
  1811. }
  1812. if(ret.length() > 0){
  1813. ret = ret.substr(0, ret.length() - 1);
  1814. }else{
  1815. ret = "正常";
  1816. }
  1817. return ret;
  1818. }
  1819. std::string Card::get_acc_text()
  1820. {
  1821. string ret = "";
  1822. state_moving = (accelerate_state & 0x01)? STATE_ACCE_MOTION : STATE_ACCE_STATIC;
  1823. if(state_moving == STATE_ACCE_STATIC){
  1824. ret += "静止";
  1825. }else if(state_moving == STATE_ACCE_MOTION){
  1826. if(CT_VEHICLE == card_type && 0 == v)
  1827. {
  1828. state_moving = STATE_ACCE_IDLING;
  1829. ret += "怠速";
  1830. }
  1831. else
  1832. {
  1833. ret += "运动";
  1834. }
  1835. }else if(state_moving == STATE_ACCE_IDLING)
  1836. {
  1837. ret += "怠速";
  1838. }
  1839. return ret;
  1840. }
  1841. std::string Card::concat( int reader_id, int ant_id )
  1842. {
  1843. char s[10];
  1844. sprintf_s(s, "%d-%d", reader_id, ant_id);
  1845. return s;
  1846. }
  1847. /*
  1848. * 滤波功能设置
  1849. *
  1850. * param
  1851. * nType ------ 滤波类型
  1852. *
  1853. * return
  1854. * 无
  1855. */
  1856. void Card::EnableFilter(int nType)
  1857. {
  1858. //如果无滤波类型直接返回
  1859. if(nType == NO_FILTER){
  1860. return;
  1861. }
  1862. //开启滤波功能,设置滤波类型
  1863. m_bUseFilter = TRUE;
  1864. m_nFilterType = nType;
  1865. switch(nType){
  1866. case FILTER_KALMAN:
  1867. //分配卡尔曼滤波类型变量并初始化参数
  1868. /*if(m_pKalmanFilter == NULL){
  1869. m_pKalmanFilter = new CKalmanFilter();
  1870. m_pKalmanFilter->Initial(0.2);
  1871. m_pKalmanFilter->m_bFlag = false;
  1872. }*/
  1873. if(m_pKalmanFilter == nullptr){
  1874. std::unique_ptr<CKalmanFilter> p(new CKalmanFilter());
  1875. m_pKalmanFilter = std::move(p);
  1876. m_pKalmanFilter->Initial(0.2);
  1877. m_pKalmanFilter->m_bFlag = false;
  1878. }
  1879. break;
  1880. default:
  1881. break;
  1882. }
  1883. }
  1884. void Card::remove_dist_head()
  1885. {
  1886. DistMap tmp = _dists.front().distmap;
  1887. if(tmp.size() > 0 ){
  1888. DistMap::iterator it_mp_dist = tmp.begin();
  1889. for(it_mp_dist;it_mp_dist != tmp.end();){
  1890. it_mp_dist = tmp.erase(it_mp_dist);
  1891. }
  1892. }
  1893. _dists.pop_front();
  1894. }
  1895. Reader::Reader(void)
  1896. {
  1897. reader_id = device_type_id = pos_state = map_id = area_id = temperature = tick_count= 0;
  1898. sync_level = 0xFF;
  1899. reader_state = reader_state_old = STATUS_DEVICE_NORMAL;
  1900. m_nIsSpecial = -1;
  1901. last_send_time = rec_time = reader_time = lost_time = time(NULL);
  1902. sync_rootId = 0;
  1903. map_scale = 1.0;
  1904. reader_x = reader_y = reader_z = reader_angle = reader_interval_time = -9999.0;
  1905. reader_name = ip = "";
  1906. for(int i = 0;i < ANTENNA_COUNT;i++){
  1907. ant[i] = nullptr;
  1908. }
  1909. for(int i = 0;i < ADHOC_COUNT;i++){
  1910. adhoc[i] = nullptr;
  1911. }
  1912. bIsInitCoverage = false;
  1913. init_ctrl_reader_state = false;
  1914. for(int i = 0; i < READER_EVENT_COUNT; i++){
  1915. m_event_list[i] = 0;
  1916. }
  1917. }
  1918. Reader::~Reader(void)
  1919. {
  1920. }
  1921. std::string Reader::get_state_text()
  1922. {
  1923. string ret = "";
  1924. if(reader_state == STATUS_DEVICE_ERROR){
  1925. ret = "故障";
  1926. }else if(reader_state == STATUS_DEVICE_NORMAL){
  1927. ret = "正常";
  1928. }
  1929. return ret;
  1930. }
  1931. Antenna::Antenna(void)
  1932. {
  1933. antenna_angle = 0;
  1934. antenna_id = 0;
  1935. antenna_x = 0;
  1936. antenna_y = 0;
  1937. antenna_z = 0;
  1938. }
  1939. Antenna::~Antenna(void)
  1940. {
  1941. }
  1942. Area::Area(void)
  1943. {
  1944. is_att = 1;
  1945. polygon_count = 0;
  1946. polygon = NULL;
  1947. map_id = area_id = area_type_id = 0 ;
  1948. area_name = area_type_name = path = "";
  1949. over_count_person = over_time_person = under_count_person = under_time_person = 0;
  1950. over_count_vehicle = over_time_vehicle = under_count_vehicle = under_time_vehicle = 0;
  1951. count_person = count_vehicle = count_card = 0;
  1952. is_area_over_time_person = is_area_over_time_vehicle = false;
  1953. count_area_over_time_person = count_area_over_time_vehicle = 0;
  1954. time_over_time_person = time_over_time_vehicle = time(NULL);
  1955. is_area_over_count_person = is_area_over_count_vehicle = false;
  1956. count_area_over_count_person = count_area_over_count_vehicle = 0;
  1957. time_over_count_person = time_over_count_vehicle = time(NULL);
  1958. is_area_forbidden_person = is_area_forbidden_vehicle = false;
  1959. count_area_forbidden_person = count_area_forbidden_vehicle = 0;
  1960. time_forbidden_person = time_forbidden_vehicle = time(NULL);
  1961. over_speed_vehicle = 0;
  1962. area_card_list_person = std::make_shared<CardMap>();
  1963. area_card_list_vehicle = std::make_shared<CardMap>();
  1964. area_card_list_over_speed = std::make_shared<CardMap>();
  1965. for(int i = 0; i < AREA_EVENT_COUNT; i++){
  1966. m_event_list[i] = 0;
  1967. }
  1968. }
  1969. Area::~Area(void)
  1970. {
  1971. if(polygon){
  1972. delete[] polygon;
  1973. polygon = NULL;
  1974. }
  1975. }
  1976. void Area::init_border(string sz_path)
  1977. {
  1978. //std::vector<std::string> vec = split(sz_path, ",");
  1979. //if(vec.size() >= 4){
  1980. // rect_left = atoi(vec[0].c_str());
  1981. // rect_top = atoi(vec[1].c_str());
  1982. // rect_right = atoi(vec[2].c_str());
  1983. // rect_bottom = atoi(vec[3].c_str());
  1984. //}else{
  1985. // rect_left = rect_right = rect_top = rect_bottom = 0;
  1986. //}
  1987. if(sz_path == ""){
  1988. return ;
  1989. }
  1990. std::vector<std::string> vec = split(sz_path, " ");
  1991. std::vector<std::string>::iterator it = vec.begin();
  1992. if(polygon){
  1993. delete[] polygon;
  1994. polygon = NULL;
  1995. }
  1996. polygon = new _point[vec.size()];
  1997. polygon_count = 0;
  1998. for(; it != vec.end(); ++it){
  1999. std::vector<std::string> subvec = split(it->c_str(), ",");
  2000. _point p;
  2001. p.x = get_vertex(subvec[0]);
  2002. p.y = get_vertex(subvec[1]);
  2003. p.z = 0;
  2004. polygon[polygon_count] = p;
  2005. polygon_count++;
  2006. }
  2007. }
  2008. std::vector<std::string> Area::split( std::string str,std::string pattern )
  2009. {
  2010. std::string::size_type pos;
  2011. std::vector<std::string> result;
  2012. str+=pattern;//扩展字符串以方便操作
  2013. unsigned int size=str.size();
  2014. for(unsigned int i=0; i<size; i++){
  2015. pos=str.find(pattern,i);
  2016. if(pos<size){
  2017. std::string s=str.substr(i,pos-i);
  2018. result.push_back(s);
  2019. i=pos+pattern.size()-1;
  2020. }
  2021. }
  2022. return result;
  2023. }
  2024. double Area::get_vertex( std::string src)
  2025. {
  2026. std::string dest = "";
  2027. for(unsigned int i = 0; i < src.length(); i++){
  2028. if((src[i] >= '0' && src[i]<='9') || src[i]=='-' || src[i] == '.'){
  2029. dest += src[i];
  2030. }
  2031. }
  2032. return atof(dest.c_str());
  2033. }
  2034. bool Area::is_in_polygon( _point p )
  2035. {
  2036. if(polygon == NULL){
  2037. return false;
  2038. }
  2039. int counter = 0;
  2040. int i;
  2041. double xinters;
  2042. _point p1,p2;
  2043. p1 = polygon[0];
  2044. for (int i=1;i<= polygon_count;i++) {
  2045. p2 = polygon[i % polygon_count];
  2046. if (p.y > MIN(p1.y,p2.y)) {
  2047. if (p.y <= MAX(p1.y,p2.y)) {
  2048. if (p.x <= MAX(p1.x,p2.x)) {
  2049. if (p1.y != p2.y) {
  2050. xinters = (p.y-p1.y)*(p2.x-p1.x)/(p2.y-p1.y)+p1.x;
  2051. if (p1.x == p2.x || p.x <= xinters)
  2052. counter++;
  2053. }
  2054. }
  2055. }
  2056. }
  2057. p1 = p2;
  2058. }
  2059. //TRACE(_T("counter : %d \n"),counter);
  2060. return (counter % 2 == 0) ? false : true;
  2061. }
  2062. int Area::is_special()
  2063. {
  2064. return (area_type_id == AREA_TYPE_NO_COVER) ? 1: 0;
  2065. }
  2066. int Card::FindDistMap( int cardstamp )
  2067. {
  2068. int idx = -1;
  2069. for(int i = _dists.size() - 1; i >= 0; i--){
  2070. if(_dists[i].cardstamp == cardstamp ){
  2071. return i;
  2072. }
  2073. }
  2074. return idx;
  2075. }
  2076. int Card::KalmanFilterProcess(std::shared_ptr<POS>& pos)
  2077. {
  2078. double kalman_detal_t = 0;
  2079. double interval_time = 0.2;
  2080. //存在分站在计数序号为1000掉电,但重新上电会导致此时的计算值偏大
  2081. if (m_nCalcSyncNum - this->last_locate.sync_num < 0 && m_nCalcSyncNum < 100)
  2082. {
  2083. kalman_detal_t = (m_nCalcSyncNum + 65535 - this->last_locate.sync_num)*interval_time;
  2084. }else{
  2085. kalman_detal_t = (m_nCalcSyncNum - this->last_locate.sync_num)*interval_time;
  2086. }
  2087. /*if (CheckCrossCycle())
  2088. {
  2089. kalman_detal_t = (m_nCalcSyncNum + 65535 - this->last_locate.sync_num)*interval_time;
  2090. }
  2091. else
  2092. {
  2093. kalman_detal_t = (m_nCalcSyncNum - this->last_locate.sync_num)*interval_time;
  2094. }*/
  2095. //如果定位失败,通过卡尔曼滤波处理
  2096. if(pos->posx == INVALID_COORDINATE && pos->posy == INVALID_COORDINATE){
  2097. m_afmData.nCardStamp = m_nCalcSyncNum;
  2098. if(this->m_pKalmanFilter->m_nCounts < 3 || this->m_pKalmanFilter->m_pCar->P(0,0) > 2 || kalman_detal_t > 3){
  2099. //P(0,0):连续时间(大于2s)都定位失败
  2100. //deltaT>3:距离上次成功定位时间间隔为3s
  2101. this->x = this->last_locate.x;
  2102. this->y = this->last_locate.y;
  2103. this->z = 0;
  2104. m_afmData.bStatus = true;
  2105. m_afmData.strCardId = this->card_id;
  2106. this->m_pKalmanFilter->m_bFlag = false;
  2107. m_afmData.nType = ALGO_FAILED_CONDITION_8;
  2108. ALGORITHM_FAILED(ALGO_FAILED_CONDITION_8);
  2109. return KALMAN_FILTER_LONG_INTERVAL;
  2110. }
  2111. if(this->m_pKalmanFilter->m_nCounts >= 3){
  2112. //只有三次以上才允许使用kalman滤波以下的函数
  2113. this->m_pKalmanFilter->Predict(kalman_detal_t);
  2114. this->z = 0;
  2115. pos->update = true;
  2116. }
  2117. }else{
  2118. pos->update = true;
  2119. this->m_pKalmanFilter->m_bFlag = true;
  2120. this->m_pKalmanFilter->m_nCounts++;
  2121. this->m_pKalmanFilter->m_pCar->z(0,0) = pos->posx;
  2122. this->m_pKalmanFilter->m_pCar->z(1,0) = pos->cvx;
  2123. this->m_pKalmanFilter->m_pCar->z(2,0) = pos->posy;
  2124. this->m_pKalmanFilter->m_pCar->z(3,0) = pos->cvy;
  2125. /*if (card_id == "0020000001047")
  2126. {
  2127. CString tmp = _T("");
  2128. tmp.Format(_T("x = %.4f , y = %.4f , cvx = %.4f ,cvy = %.4f ,dt = %.4f \r\n"),pos->posx,pos->posy,pos->cvx,pos->cvy,pos->diff_reader_sync_num);
  2129. TRACE(tmp);
  2130. }*/
  2131. if(this->m_pKalmanFilter->m_nCounts == 1){
  2132. //第一次直接赋值
  2133. this->m_pKalmanFilter->m_pCar->x = this->m_pKalmanFilter->m_pCar->z;
  2134. }
  2135. if(this->m_pKalmanFilter->m_nCounts == 2){
  2136. //两次处理
  2137. 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;
  2138. 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;
  2139. this->m_pKalmanFilter->m_pCar->x = this->m_pKalmanFilter->m_pCar->z;
  2140. }
  2141. if(this->m_pKalmanFilter->m_nCounts >= 3){
  2142. //只有三次以上才允许使用kalman滤波以下的函数
  2143. //this->m_pKalmanFilter->Predict_Correct(deltaT);
  2144. this->m_pKalmanFilter->Predict_Correct(kalman_detal_t);
  2145. if(fabs(pos->diff_reader_sync_num) > 1E-5){
  2146. 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;
  2147. 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;
  2148. }
  2149. }
  2150. }
  2151. if (pos->update)
  2152. {
  2153. //如果经过卡尔曼处理之后
  2154. if(pos->nFirstReader == 0 && pos->nSecondReader == 0){
  2155. pos->nFirstReader = last_s_locate_reader[0];
  2156. pos->nSecondReader = last_s_locate_reader[1];
  2157. }
  2158. //二维定位不需要如下内容
  2159. //增加地图集的判定,判断定位结果是否在地图集上
  2160. //如果不在地图集上,需要再次定位
  2161. //需要带出定位结果的分站信息,
  2162. //利用地图集中分站信息再次定位
  2163. std::shared_ptr<POS> kalman_p = std::make_shared<POS>();
  2164. kalman_p->posx = this->m_pKalmanFilter->m_pCar->x(0,0);
  2165. kalman_p->posy = this->m_pKalmanFilter->m_pCar->x(2,0);
  2166. ////二维定位不需要再判定在地图集上了
  2167. if(!LocateAlgorithm::IsOnMap(kalman_p,pTdoaReaderPathMap)){
  2168. //再一次定位到地图集上
  2169. LOCATION_SYSTEM_BRANCH(LOCATION_SYSTEM_BRANCH_205);
  2170. //std::shared_ptr<POS> cp = LocateAlgorithm::Pos(kalman_p,pTdoaReaderPathMap);
  2171. std::shared_ptr<POS> cp = LocateAlgorithm::MappingToPath(kalman_p,pTdoaReaderPathMap);
  2172. if(cp != nullptr){
  2173. if (cp->posx != INVALID_COORDINATE && cp->posy !=INVALID_COORDINATE)
  2174. {
  2175. LOCATION_SYSTEM_BRANCH(LOCATION_SYSTEM_BRANCH_206);
  2176. this->m_pKalmanFilter->m_pCar->x(0,0) = cp->posx;
  2177. this->m_pKalmanFilter->m_pCar->x(2,0) = cp->posy;
  2178. }
  2179. }
  2180. cp.reset();
  2181. }
  2182. pos->posx = this->m_pKalmanFilter->m_pCar->x(0,0);
  2183. pos->posy = this->m_pKalmanFilter->m_pCar->x(2,0);
  2184. pos->cvx = this->m_pKalmanFilter->m_pCar->x(1,0);
  2185. pos->cvy = this->m_pKalmanFilter->m_pCar->x(3,0);
  2186. pos->cx = pos->posx / (map_scale*1.0);
  2187. pos->cy = pos->posy / (map_scale*1.0);
  2188. }
  2189. return 0;
  2190. }
  2191. bool Card::CheckCrossCycle()
  2192. {
  2193. //time_stamp_last特指卡的计数序号,非分站的
  2194. //当卡需要新定位的计数序列号小于上一次成功定位的序列号,则此次不定位,避免定位结果的回退
  2195. if (this->time_stamp_last != 0 && this->time_stamp_last > this->time_stamp_cal)
  2196. {
  2197. string str_last = "";
  2198. string str_cur = "";
  2199. str_last = CFunctions::int2string(this->time_stamp_last);
  2200. str_cur = CFunctions::int2string(this->time_stamp_cal);
  2201. if (str_last.length() == 5 && str_cur.length() <= 2)
  2202. {
  2203. //满足此条件,表示标示卡的计数序号走完了65535的周期
  2204. return true;
  2205. }
  2206. else{
  2207. return false;
  2208. }
  2209. }
  2210. return true;
  2211. }
  2212. /*
  2213. * 组装拟合数据,主要是前REF_POSITION_NUM次的定位数据信息
  2214. *
  2215. * param
  2216. * pos 定位坐标
  2217. *
  2218. * return
  2219. * 成功返回0
  2220. */
  2221. int Card::UpdateFittingData(std::shared_ptr<POS> pos)
  2222. {
  2223. if (cur_ref_totals == REF_POSITION_NUM - 1)
  2224. {
  2225. for (int i = 0;i < cur_ref_totals-1;i++)
  2226. {
  2227. fitting_v[i] = fitting_v[i + 1];
  2228. fitting_ct[i] = fitting_ct[i + 1];
  2229. fitting_x[i] = fitting_x[i + 1];
  2230. fitting_y[i] = fitting_y[i + 1];
  2231. }
  2232. }
  2233. fitting_v[cur_ref_totals] = v;
  2234. fitting_ct[cur_ref_totals] = m_nCalcSyncNum;
  2235. fitting_x[cur_ref_totals] = pos->posx;
  2236. fitting_y[cur_ref_totals] = pos->posy;
  2237. if (cur_ref_totals < REF_POSITION_NUM - 1)
  2238. {
  2239. cur_ref_totals++;
  2240. }
  2241. return 0;
  2242. }
  2243. int Card::CalcFittingData()
  2244. {
  2245. /*if (cur_ref_totals != REF_POSITION_NUM - 1)
  2246. {
  2247. if (Fitting::VAR(fitting_v) < 25)
  2248. {
  2249. double fitting_coe_x[REF_POSITION_NUM] = {0};
  2250. double fitting_coe_y[REF_POSITION_NUM] = {0};
  2251. Fitting::EMatrix(fitting_ct,fitting_x,REF_POSITION_NUM,3,fitting_coe_x);
  2252. Fitting::EMatrix(fitting_ct,fitting_y,REF_POSITION_NUM,3,fitting_coe_y);
  2253. for (size_t i=0;i<FIT_POSITION_NUM;i++)
  2254. {
  2255. fit_new_x[i] = Fitting::polyval(fitting_coe_x,fitting_ct[cur_ref_totals] + i);
  2256. fit_new_y[i] = Fitting::polyval(fitting_coe_y,fitting_ct[cur_ref_totals] + i);
  2257. }
  2258. }
  2259. }*/
  2260. //如果滤波失败或者其他条件失败,
  2261. //则通过直线回归算法拟合出三个解
  2262. vector<double> vx,vy,vt;
  2263. vx.resize(0);
  2264. vy.resize(0);
  2265. vt.resize(0);
  2266. for (list<std::shared_ptr<POS>>::iterator it = his_pos.begin();it!=his_pos.end();++it)
  2267. {
  2268. vt.push_back((*it)->card_count);
  2269. vx.push_back((*it)->posx);
  2270. vy.push_back((*it)->posy);
  2271. }
  2272. //线性回归拟合,从5个历史点中取出3个预测点
  2273. Fit fit_x;
  2274. fit_x.linearFit(vt,vx);
  2275. Fit fit_y;
  2276. fit_y.linearFit(vt,vy);
  2277. for (int i = 0;i < FIT_POSITION_NUM;i++)
  2278. {
  2279. fit_new_x[i] = 0;
  2280. fit_new_y[i] = 0;
  2281. }
  2282. for (int i = 1;i <= FIT_POSITION_NUM;i++)
  2283. {
  2284. fit_new_x[i-1] = fit_x.getY(vt[4] + i);
  2285. fit_new_y[i-1] = fit_y.getY(vt[4] + i);
  2286. }
  2287. have_fit_pos = true;
  2288. if (have_fit_pos)
  2289. {
  2290. TRACE(_T("have fit pos \r\n"));
  2291. }
  2292. return 0;
  2293. }
  2294. /*
  2295. * 计算线性拟合数据,
  2296. * 当满足如下两个条件:
  2297. * a.之前定位成功,
  2298. * b.参考数据小于5,
  2299. * c.连续取拟合数据等于3次了,
  2300. * 则不计算拟合数据
  2301. *
  2302. * param
  2303. * ret 定位状态
  2304. * pos 定位结果
  2305. *
  2306. * return
  2307. * 成功获得拟合数据返回0,否则返回1
  2308. *
  2309. */
  2310. int Card::CalcFittingData(int ret,std::shared_ptr<POS>& pos)
  2311. {
  2312. if (his_pos.size() < FIT_POSITION_NUM || cur_fit_nums >= 3)
  2313. {
  2314. if (cur_fit_nums == 3)
  2315. {
  2316. have_fit_pos = false;
  2317. }
  2318. return 1;
  2319. }
  2320. if (ret == 0 && pos->reason == 0)
  2321. {
  2322. return 2;
  2323. }
  2324. if (cur_fit_nums == 0)
  2325. {
  2326. //如果滤波失败或者其他条件失败,
  2327. //则通过直线回归算法拟合出三个解
  2328. vector<double> vx,vy,vt;
  2329. vx.resize(0);
  2330. vy.resize(0);
  2331. vt.resize(0);
  2332. for (list<std::shared_ptr<POS>>::iterator it = his_pos.begin();it!=his_pos.end();++it)
  2333. {
  2334. vt.push_back((*it)->card_count);
  2335. vx.push_back((*it)->posx);
  2336. vy.push_back((*it)->posy);
  2337. }
  2338. //线性回归拟合,从5个历史点中取出3个预测点
  2339. Fit fit_x;
  2340. fit_x.linearFit(vt,vx);
  2341. Fit fit_y;
  2342. fit_y.linearFit(vt,vy);
  2343. for (int i = 1;i <= FIT_POSITION_NUM;i++)
  2344. {
  2345. fit_new_x[i-1] = fit_x.getY(vt[4] + i);
  2346. fit_new_y[i-1] = fit_y.getY(vt[4] + i);
  2347. }
  2348. }
  2349. int index = cur_fit_nums;
  2350. //判断
  2351. double kx = fit_new_x[index];
  2352. double ky = fit_new_y[index];
  2353. std::shared_ptr<POS> ks = std::make_shared<POS>();
  2354. ks->posx = fit_new_x[index] ;
  2355. ks->posy = fit_new_y[index] ;
  2356. ks->cx = ks->posx / (1.0*map_scale);
  2357. ks->cy = ks->posy / (1.0*map_scale);
  2358. //判断拟合数据是否在地图集上,如果不在返回1
  2359. if (!LocateAlgorithm::IsOnMap(ks,pTdoaReaderPathMap))
  2360. {
  2361. //不在地图集上,则返回
  2362. return 1;
  2363. }
  2364. pos->posx = ks->posx;
  2365. pos->posy = ks->posy;
  2366. pos->cx = ks->cx;
  2367. pos->cy = ks->cy;
  2368. if (fabs(pos->diff_reader_sync_num) > 1E-4)
  2369. {
  2370. pos->cvx = (pos->cx - last_locate.x)/pos->diff_reader_sync_num;
  2371. pos->cvy = (pos->cy - last_locate.y)/pos->diff_reader_sync_num;
  2372. }
  2373. cur_fit_nums++;
  2374. pos->reason = 0;
  2375. pos->is_fit = true;
  2376. have_fit_pos = true;
  2377. return 0;
  2378. }
  2379. /*
  2380. * 通过多项式拟合计算数据,
  2381. * 当满足如下两个条件:
  2382. * a.之前定位成功,
  2383. * b.参考数据小于5,
  2384. * c.连续取拟合数据等于3次了,
  2385. * 则不计算拟合数据
  2386. *
  2387. * param
  2388. * ret 定位状态
  2389. * pos 定位结果
  2390. *
  2391. * return
  2392. * 成功获得拟合数据返回0,否则返回1
  2393. *
  2394. */
  2395. int Card::CalcLongFittingData(int ret,std::shared_ptr<POS>& pos)
  2396. {
  2397. int nums = FIT_POSITION_NUM*4;
  2398. if (long_his_pos.size() < nums || cur_fit_nums >= nums)
  2399. {
  2400. if (cur_fit_nums == nums)
  2401. {
  2402. have_fit_pos = false;
  2403. }
  2404. return 1;
  2405. }
  2406. if (ret == 0 && pos->reason == 0)
  2407. {
  2408. return 2;
  2409. }
  2410. if (cur_fit_nums <= 3)
  2411. {
  2412. //如果滤波失败或者其他条件失败,
  2413. //则通过多项式拟合算法拟合出20个解
  2414. vector<double> vx,vy,vt;
  2415. vx.resize(0);
  2416. vy.resize(0);
  2417. vt.resize(0);
  2418. for (list<std::shared_ptr<POS>>::iterator it = long_his_pos.begin();it!=long_his_pos.end();++it)
  2419. {
  2420. vt.push_back((*it)->card_count);
  2421. vx.push_back((*it)->posx);
  2422. vy.push_back((*it)->posy);
  2423. }
  2424. //多项式拟合,从20个历史点中拟合出20个预测点
  2425. Fit fit_x;
  2426. fit_x.polyfit(vt,vx,2);
  2427. Fit fit_y;
  2428. fit_y.polyfit(vt,vy,2);
  2429. for (int i = 1;i <= nums;i++)
  2430. {
  2431. long_fit_new_x[i-1] = fit_x.getY(vt[nums - 1] + i);
  2432. long_fit_new_y[i-1] = fit_y.getY(vt[nums - 1] + i);
  2433. }
  2434. }
  2435. int index = cur_fit_nums - 3;
  2436. //判断
  2437. double kx = long_fit_new_x[index];
  2438. double ky = long_fit_new_y[index];
  2439. std::shared_ptr<POS> ks = std::make_shared<POS>();
  2440. ks->posx = long_fit_new_x[index] ;
  2441. ks->posy = long_fit_new_y[index] ;
  2442. ks->cx = ks->posx / (1.0*map_scale);
  2443. ks->cy = ks->posy / (1.0*map_scale);
  2444. //判断拟合数据是否在地图集上,如果不在返回1
  2445. if (!LocateAlgorithm::IsOnMap(ks,pTdoaReaderPathMap))
  2446. {
  2447. //不在地图集上,则返回
  2448. return 1;
  2449. }
  2450. pos->posx = ks->posx;
  2451. pos->posy = ks->posy;
  2452. pos->cx = ks->cx;
  2453. pos->cy = ks->cy;
  2454. if (fabs(pos->diff_reader_sync_num) > 1E-4)
  2455. {
  2456. pos->cvx = (pos->cx - last_locate.x)/pos->diff_reader_sync_num;
  2457. pos->cvy = (pos->cy - last_locate.y)/pos->diff_reader_sync_num;
  2458. }
  2459. cur_fit_nums++;
  2460. pos->reason = 0;
  2461. pos->is_fit = true;
  2462. have_fit_pos = true;
  2463. return 0;
  2464. }
  2465. int Card::CalcLongFittingData()
  2466. {
  2467. int nums = FIT_POSITION_NUM*4;
  2468. //如果滤波失败或者其他条件失败,
  2469. //则通过多项式拟合算法拟合出20个解
  2470. vector<double> vx,vy,vt;
  2471. vx.resize(0);
  2472. vy.resize(0);
  2473. vt.resize(0);
  2474. for (list<std::shared_ptr<POS>>::iterator it = long_his_pos.begin();it!=long_his_pos.end();++it)
  2475. {
  2476. vt.push_back((*it)->card_count);
  2477. vx.push_back((*it)->posx);
  2478. vy.push_back((*it)->posy);
  2479. }
  2480. //多项式拟合,从20个历史点中拟合出20个预测点
  2481. Fit fit_x;
  2482. fit_x.polyfit(vt,vx,2);
  2483. Fit fit_y;
  2484. fit_y.polyfit(vt,vy,2);
  2485. for (int i = 0;i < nums;i++)
  2486. {
  2487. long_fit_new_x[i] = 0;
  2488. long_fit_new_y[i] = 0;
  2489. }
  2490. double diff_x = 0 , diff_y = 0;
  2491. for (int i = 1;i <= nums;i++)
  2492. {
  2493. long_fit_new_x[i-1] = fit_x.getY(vt[nums - 1] + i);
  2494. long_fit_new_y[i-1] = fit_y.getY(vt[nums - 1] + i);
  2495. if (i>=2)
  2496. {
  2497. diff_x += abs(long_fit_new_x[i-1] - long_fit_new_x[i-2]);
  2498. diff_y += abs(long_fit_new_y[i-1] - long_fit_new_y[i-2]);
  2499. }
  2500. }
  2501. double diff = 0.0;
  2502. int interval = 0;
  2503. double d_val = 0.0;
  2504. if (diff_x < 1 && diff_y > 10)
  2505. {
  2506. diff = 0.0;
  2507. diff += abs(long_fit_new_y[FIT_POSITION_NUM - 1] - long_fit_new_y[0]);
  2508. interval = nums - FIT_POSITION_NUM;
  2509. d_val = diff / interval;
  2510. for (int i = FIT_POSITION_NUM;i<nums;i++)
  2511. {
  2512. long_fit_new_y[i] = long_fit_new_y[FIT_POSITION_NUM - 1] + (i - FIT_POSITION_NUM + 1)*d_val;
  2513. }
  2514. }else if(diff_x > 1 && diff_y < 1){
  2515. diff = 0.0;
  2516. diff += abs(long_fit_new_x[FIT_POSITION_NUM - 1] + long_fit_new_x[0]);
  2517. interval = nums - FIT_POSITION_NUM;
  2518. d_val = diff / interval;
  2519. for (int i = FIT_POSITION_NUM;i<nums;i++)
  2520. {
  2521. long_fit_new_x[i] = long_fit_new_x[FIT_POSITION_NUM - 1] + (i - FIT_POSITION_NUM + 1)*d_val;
  2522. }
  2523. }
  2524. have_long_fit_pos = true;
  2525. return 0;
  2526. }
  2527. std::shared_ptr<POS> Card::GetPosFromFittingData()
  2528. {
  2529. std::shared_ptr<POS> p = std::make_shared<POS>();
  2530. //没有拟合20个点的数据
  2531. if (!have_long_fit_pos)
  2532. {
  2533. if (have_fit_pos)
  2534. {
  2535. p->posx = fit_new_x[cur_fit_nums];
  2536. p->posy = fit_new_y[cur_fit_nums];
  2537. p->cx = p->posx / (map_scale*1.0);
  2538. p->cy = p->posy / (map_scale*1.0);
  2539. p->is_fit = true;
  2540. }
  2541. }else{
  2542. //有拟合20个点的数据
  2543. p->posx = long_fit_new_x[cur_fit_nums];
  2544. p->posy = long_fit_new_y[cur_fit_nums];
  2545. p->cx = p->posx / (map_scale*1.0);
  2546. p->cy = p->posy / (map_scale*1.0);
  2547. p->is_fit = true;
  2548. }
  2549. return p;
  2550. }
  2551. int Card::GetPosFromFittingData(std::shared_ptr<POS>& pos)
  2552. {
  2553. if (have_fit_pos || have_long_fit_pos)
  2554. {
  2555. //如果拟合了数据,则直接取拟合数据
  2556. std::shared_ptr<POS> fit_p = GetPosFromFittingData();
  2557. pos->posx = fit_p->posx;
  2558. pos->posy = fit_p->posy;
  2559. pos->cx = fit_p->cx;
  2560. pos->cy = fit_p->cy;
  2561. if (CheckCrossCycle())
  2562. {
  2563. pos->diff_reader_sync_num = (m_nCalcSyncNum + 65536 - this->last_locate.sync_num)*0.2;
  2564. }else{
  2565. pos->diff_reader_sync_num = (m_nCalcSyncNum - this->last_locate.sync_num)*0.2;
  2566. }
  2567. if (!LocateAlgorithm::IsOnMap(pos,pTdoaReaderPathMap))
  2568. {
  2569. //判断拟合数据是否在地图集上,
  2570. //如果不在,则重置为上一次定位结果
  2571. pos->posx = last_locate.x * map_scale;
  2572. pos->posy = last_locate.y * map_scale;
  2573. pos->cx = last_locate.x;
  2574. pos->cy = last_locate.y;
  2575. pos->cvx = last_vx;
  2576. pos->cvy = last_vy;
  2577. }else{
  2578. //如果在地图集上,则计算相关参数
  2579. if (fabs(pos->diff_reader_sync_num) > 1E-4)
  2580. {
  2581. pos->cvx = (pos->cx - last_locate.x)/pos->diff_reader_sync_num;
  2582. pos->cvy = (pos->cy - last_locate.y)/pos->diff_reader_sync_num;
  2583. }else{
  2584. pos->cvx = last_vx;
  2585. pos->cvy = last_vy;
  2586. }
  2587. cur_fit_nums++;
  2588. pos->reason = 0;
  2589. pos->is_fit = true;
  2590. }
  2591. }else{
  2592. return 1;
  2593. }
  2594. return 0;
  2595. }
  2596. int Card::CheckSolutionByFit(int ret,std::shared_ptr<POS>& pos)
  2597. {
  2598. //拟合预测解
  2599. is_ref_pos = true;
  2600. std::shared_ptr<POS> fit_pos = std::make_shared<POS>();
  2601. fit_pos->diff_reader_sync_num = pos->diff_reader_sync_num;
  2602. int fit_ret = 0;
  2603. //fit_ret = CalcFittingData(ret,fit_pos);
  2604. if (cur_fit_nums<3)
  2605. {
  2606. fit_ret = CalcFittingData(ret,fit_pos);
  2607. }
  2608. else
  2609. {
  2610. fit_ret = CalcLongFittingData(ret,fit_pos);
  2611. }
  2612. //存在如下情况,解是个错误解
  2613. if (ret && fit_ret == 0)
  2614. {
  2615. //如果加速度和速度失败了或选解选不出来,而且有拟合数据了
  2616. //则直接使用拟合解
  2617. pos->posx = fit_pos->posx;
  2618. pos->posy = fit_pos->posy;
  2619. pos->cx = fit_pos->cx;
  2620. pos->cy = fit_pos->cy;
  2621. pos->cvx = fit_pos->cvx;
  2622. pos->cvy = fit_pos->cvy;
  2623. pos->is_fit = fit_pos->is_fit;
  2624. //如果是拟合出来的点就不能作为参考点
  2625. is_ref_pos = !pos->is_fit;
  2626. }else{
  2627. if (fit_ret == 0)
  2628. {
  2629. //如果有拟合数据
  2630. //如果选出了一个解,需要判断此解和预测值得距离
  2631. double distance = 0.0;
  2632. distance = sqrt(pow(pos->posx - fit_pos->posx,2) + pow(pos->posy - fit_pos->posy,2));
  2633. if (distance > 1)
  2634. {
  2635. pos->posx = fit_pos->posx;
  2636. pos->posy = fit_pos->posy;
  2637. pos->cx = fit_pos->cx;
  2638. pos->cy = fit_pos->cy;
  2639. pos->cvx = fit_pos->cvx;
  2640. pos->cvy = fit_pos->cvy;
  2641. pos->is_fit = fit_pos->is_fit;
  2642. //如果是拟合出来的点就不能作为参考点
  2643. is_ref_pos = !pos->is_fit;
  2644. }
  2645. }
  2646. if (fit_ret && ret == 0)
  2647. {
  2648. //表示没有拟合值,但解是可信的,则直接返回0
  2649. return 0;
  2650. }
  2651. }
  2652. return fit_ret;
  2653. }
  2654. int Card::CheckSolutionBySpeed(std::shared_ptr<POS>& pos)
  2655. {
  2656. //double speed = sqrt(pow(pos->cvx,2) + pow(pos->cvy,2));
  2657. if (m_nLastLocateT != 0)
  2658. {
  2659. //如果第一次定位不做速度的判别
  2660. //double speed = sqrt(pow(pos->cx - last_locate.x,2) + pow(pos->cy - last_locate.y,2));
  2661. double speed = sqrt(pow(pos->cvx,2) + pow(pos->cvy,2));
  2662. speed *= 3.6; //转为km/h
  2663. if (speed > MAX_VECHILE_SPEED)
  2664. {
  2665. return 1;
  2666. }
  2667. }
  2668. return 0;
  2669. }
  2670. bool Card::CheckCardCtValid()
  2671. {
  2672. bool bRet = true;
  2673. // 本次序号小于上次序号,存在如下情况
  2674. // 1、跨周期,如本次0,上次为65535,另外进入盲区也可能跨周期后回来
  2675. // 2、卡重置,从0开始
  2676. // 3、序号较连续,新数据先到,旧数据后到,会造成跳动
  2677. if (this->time_stamp_last != 0 && this->time_stamp_last > this->time_stamp_cal)
  2678. {
  2679. if((unsigned short)this->time_stamp_last - (unsigned short)this->time_stamp_cal < MAX_SYNCTIME_DELAY_NUM)
  2680. {
  2681. // 小于5,认为会影响数据
  2682. //pRdm->clear();
  2683. this->x = this->last_locate.x;
  2684. this->y = this->last_locate.y;
  2685. this->z = 0;
  2686. m_afmData.bStatus = true;
  2687. m_afmData.strCardId = this->card_id;
  2688. m_afmData.nCardStamp = this->time_stamp_cal;
  2689. m_afmData.nType = ALGO_FAILED_CONDITION_4;
  2690. ALGORITHM_FAILED(ALGO_FAILED_CONDITION_4);
  2691. //此处需要输出计数
  2692. return false;
  2693. }
  2694. }
  2695. this->time_stamp_last = this->time_stamp_cal;
  2696. return bRet;
  2697. }
  2698. void Card::set_speed( double v )
  2699. {
  2700. }
  2701. // 检查dist数据有效性
  2702. int Card::CheckDistData(int cnt)
  2703. {
  2704. LOCATION_SYSTEM_BRANCH(LOCATION_SYSTEM_BRANCH_140);
  2705. GetLocalTime(&m_afmData.st);
  2706. // dist数据少于两条直接退出
  2707. if(cnt < 2){
  2708. LOCATION_SYSTEM_BRANCH(LOCATION_SYSTEM_BRANCH_141);
  2709. m_afmData.bStatus = true;
  2710. m_afmData.strCardId = this->card_id;
  2711. m_afmData.nType = ALGO_FAILED_CONDITION_15;
  2712. ALGORITHM_FAILED(ALGO_FAILED_CONDITION_15);
  2713. if (mp_dists_locate.size() > 0)
  2714. {
  2715. m_nCalcSyncNum = mp_dists_locate.begin()->second->sync_num;
  2716. }
  2717. return DIST_COUNT_LESS_THAN_TWO;
  2718. }
  2719. //主要处理当相同卡的时间戳的数据中存在同步序号大于5的情况,如果有大于5的数据则丢弃此数据
  2720. int k = 0;
  2721. int dst = 0;
  2722. int st = 0;
  2723. bool bRet = false;
  2724. // 获取最大时间同步值
  2725. for(DistMap::iterator it = _dists.front().distmap.begin(); it != _dists.front().distmap.end(); ++it,k++){
  2726. if(k==0){
  2727. st = it->second->sync_num;
  2728. }else{
  2729. if(st < it->second->sync_num){ // 未考虑跨周期问题
  2730. st = it->second->sync_num;
  2731. }
  2732. }
  2733. }
  2734. map<unsigned long long,std::shared_ptr<_coordinate>> mp_dists_locate_ex;
  2735. mp_dists_locate_ex.clear();
  2736. // 筛选掉线性插值异常的数据
  2737. for(DistMap::iterator it = _dists.front().distmap.begin(); it != _dists.front().distmap.end(); ++it){
  2738. //如果同步时间戳存在异常值,则不走算法定位,直接返回上一次结果值
  2739. if(LLONG_MAX == it->second->tt){
  2740. LOCATION_SYSTEM_BRANCH(LOCATION_SYSTEM_BRANCH_144);
  2741. }else
  2742. {
  2743. mp_dists_locate_ex.insert(make_pair(it->second->tt,it->second));
  2744. }
  2745. }
  2746. if(mp_dists_locate_ex.size() < 2){
  2747. LOCATION_SYSTEM_BRANCH(LOCATION_SYSTEM_BRANCH_143);
  2748. this->x = this->last_locate.x;
  2749. this->y = this->last_locate.y;
  2750. this->z = 0;
  2751. m_afmData.bStatus = true;
  2752. m_afmData.strCardId = this->card_id;
  2753. m_afmData.nType = ALGO_FAILED_CONDITION_2;
  2754. ALGORITHM_FAILED(ALGO_FAILED_CONDITION_2);
  2755. return DIST_COUNT_LESS_FOR_TIMESTAMP_ERROR;
  2756. }
  2757. // 筛选掉同步序号与最大值差5的数据
  2758. mp_dists_locate.clear();
  2759. map<unsigned long long,std::shared_ptr<_coordinate>>::iterator it_mpdl = mp_dists_locate_ex.begin();
  2760. for(;it_mpdl!=mp_dists_locate_ex.end();++it_mpdl){
  2761. if(st - it_mpdl->second->sync_num < 5){
  2762. mp_dists_locate.insert(make_pair(it_mpdl->second->tt, it_mpdl->second));
  2763. }else{
  2764. LOCATION_SYSTEM_BRANCH(LOCATION_SYSTEM_BRANCH_142);
  2765. }
  2766. }
  2767. if(mp_dists_locate.size() < 2){
  2768. LOCATION_SYSTEM_BRANCH(LOCATION_SYSTEM_BRANCH_143);
  2769. this->x = this->last_locate.x;
  2770. this->y = this->last_locate.y;
  2771. this->z = 0;
  2772. m_afmData.bStatus = true;
  2773. m_afmData.strCardId = this->card_id;
  2774. m_afmData.nType = ALGO_FAILED_CONDITION_2;
  2775. ALGORITHM_FAILED(ALGO_FAILED_CONDITION_2);
  2776. return DIST_COUNT_LESS_FOR_SYNC_NUM_DIFFER_FIVE;
  2777. }
  2778. if (!CheckCardCtValid())
  2779. {
  2780. //如果卡的ct存在当前比之前的小,且是未跨周期的情况下,则认为此组数据无效
  2781. return DIST_COUNT_CARD_CUR_CT_LESS_LAST;
  2782. }
  2783. return 0;
  2784. }
  2785. int Card::AssembleDistData(std::shared_ptr<ReceiveDataMap> pRdm)
  2786. {
  2787. int maxSyncTimes = 0;
  2788. //保存加速度当前状态和上次状态
  2789. int acce_state = 0;
  2790. int acce_state_last = 0;
  2791. int ins_direction = 0;
  2792. int card_time_stamp = 0;
  2793. map<unsigned long long,std::shared_ptr<_coordinate>>::iterator it_mpdl = mp_dists_locate.begin();
  2794. int i = 0;
  2795. for(;it_mpdl!=mp_dists_locate.end();++it_mpdl){
  2796. if(i==0){
  2797. card_time_stamp = it_mpdl->second->t;
  2798. maxSyncTimes = it_mpdl->second->sync_num;
  2799. acce_state = it_mpdl->second->acce_state;
  2800. acce_state_last = it_mpdl->second->acce_state_last;
  2801. ins_direction = it_mpdl->second->ins_direction;
  2802. }
  2803. else{
  2804. if(maxSyncTimes < it_mpdl->second->sync_num){
  2805. maxSyncTimes = it_mpdl->second->sync_num;
  2806. acce_state = it_mpdl->second->acce_state;
  2807. acce_state_last = it_mpdl->second->acce_state_last;
  2808. ins_direction = it_mpdl->second->ins_direction;
  2809. }
  2810. }
  2811. ReceiveDataMap::iterator prdm_it = pRdm->find(it_mpdl->second->tt);
  2812. if(prdm_it == pRdm->end()){
  2813. //保存信息用于定位
  2814. std::shared_ptr<ReceiveData> prd = std::make_shared<ReceiveData>();
  2815. prd->reader_id = it_mpdl->second->reader_id;
  2816. prd->antenna_id = it_mpdl->second->antenna_id;
  2817. prd->rec_time_stamp = it_mpdl->second->tt;
  2818. prd->x = it_mpdl->second->x*this->map_scale;
  2819. prd->y = it_mpdl->second->y*this->map_scale;
  2820. prd->z = it_mpdl->second->z*this->map_scale;
  2821. prd->special = it_mpdl->second->special;
  2822. if (prd->rec_time_stamp > 0)
  2823. {
  2824. pRdm->insert(make_pair(prd->rec_time_stamp,prd));
  2825. }
  2826. }
  2827. i++;
  2828. }
  2829. m_nCalcSyncNum = maxSyncTimes;
  2830. acce_cur_state = acce_state;
  2831. return 0;
  2832. }
  2833. int Card::SaveCardAlgoData(std::shared_ptr<POS>& pos)
  2834. {
  2835. this->x = pos->cx;
  2836. this->y = pos->cy;
  2837. this->z = pos->cz;
  2838. if (!is_algo_first_location)
  2839. {
  2840. if (abs(this->last_locate.x) > 1E-5 || abs(this->last_locate.y) > 1E-5)
  2841. {
  2842. //去除采集刚启动,last_locate的坐标为零而导致判断车辆上下行方向错误的问题
  2843. //车辆上下行确定
  2844. //3个条件:起点last_locate(x1,y1),终点(x2,y2)
  2845. //1.x1==x2的情况下,y2>y1为下行
  2846. //2.y1==y2的情况下,x1>x2为下行
  2847. //3.x1<x2且y2>y1为下行
  2848. //其他情况为上行
  2849. int nStream = 0;
  2850. if ((this->last_locate.x == this->x && this->y > this->last_locate.y)
  2851. ||(this->last_locate.x > this->x && this->y == this->last_locate.y)
  2852. ||(this->last_locate.x < this->x && this->y > this->last_locate.y))
  2853. {
  2854. nStream = DOWN_STREAM;
  2855. }
  2856. else
  2857. {
  2858. nStream = UP_STREAM;
  2859. }
  2860. }
  2861. }
  2862. int nSign = 1;
  2863. if(pos->cvx == 0){
  2864. if(pos->cvy > 0){
  2865. nSign = 1;
  2866. }
  2867. }else{
  2868. if(pos->cvx > 0){
  2869. nSign = 1;
  2870. }else{
  2871. nSign = -1;
  2872. }
  2873. }
  2874. this->m_nMoveDirection = nSign;
  2875. if (this->acce_cur_state == STATE_ACCE_IDLING)
  2876. {
  2877. this->x = this->last_locate.x;
  2878. this->y = this->last_locate.y;
  2879. this->z = this->last_locate.z;
  2880. }
  2881. else
  2882. {
  2883. this->last_locate.x = this->x;
  2884. this->last_locate.y = this->y;
  2885. this->last_locate.z = this->z = 0;
  2886. }
  2887. this->last_locate.sync_num = this->m_nCalcSyncNum;
  2888. this->last_locate.acceleration = pos->av;
  2889. this->last_vx = pos->cvx;
  2890. this->last_vy = pos->cvy;
  2891. last_s_locate_reader[0] = pos->nFirstReader;
  2892. last_s_locate_reader[1] = pos->nSecondReader;
  2893. int nlast = this->m_nLastLocateT;
  2894. this->m_pKalmanFilter->m_pCar->t = this->m_nLastLocateT = this->m_nCalcSyncNum;
  2895. if (is_algo_first_location)
  2896. {
  2897. is_algo_first_location = false;
  2898. }
  2899. //速度的计算采用求平均的方式
  2900. double speed = sqrt(pow(pos->cvx,2) + pow(pos->cvy,2));
  2901. speed *=3.6; //转为km/h
  2902. if (vt_his_speed.size()==3)
  2903. {
  2904. vt_his_speed.pop_front();
  2905. }
  2906. if (speed > 1E-4)
  2907. {
  2908. vt_his_speed.push_back(speed);
  2909. }
  2910. int total = 0;
  2911. double sum_speed = 0;
  2912. for (list<double>::iterator it = vt_his_speed.begin();it != vt_his_speed.end();++it)
  2913. {
  2914. if (*it > 0)
  2915. {
  2916. sum_speed += *it;
  2917. total++;
  2918. }
  2919. }
  2920. double av = 0;
  2921. if (total>0)
  2922. {
  2923. av = sum_speed / total;
  2924. }
  2925. if (this->acce_cur_state == STATE_ACCE_STATIC || this->acce_cur_state == STATE_ACCE_IDLING)
  2926. {
  2927. this->v = 0;
  2928. }
  2929. else
  2930. {
  2931. this->v = av*nSign;
  2932. }
  2933. if (cur_fit_nums == FIT_POSITION_NUM*4)
  2934. {
  2935. this->v = 0;
  2936. }
  2937. this->last_locate.v = this->v;
  2938. if(pos->update){
  2939. sync_data sdNew;
  2940. sdNew.sync_num = this->m_nCalcSyncNum;
  2941. sdNew.x = this->x;
  2942. sdNew.y = this->y;
  2943. sdNew.vx = this->last_vx;
  2944. sdNew.vy = this->last_vy;
  2945. m_syncNumList.push_back(sdNew);
  2946. }
  2947. if(is_ref_pos)
  2948. {
  2949. //保存历史数据
  2950. std::shared_ptr<POS> p = std::make_shared<POS>();
  2951. p->posx = this->x*map_scale;
  2952. p->posy = this->y*map_scale;
  2953. p->card_count = this->time_stamp_cal;
  2954. //拟合数据的计算
  2955. if (his_pos.size() == REF_POSITION_NUM)
  2956. {
  2957. his_pos.pop_front();
  2958. }
  2959. his_pos.push_back(p);
  2960. if (his_pos.size() == REF_POSITION_NUM)
  2961. {
  2962. if (!have_long_fit_pos)
  2963. {
  2964. CalcFittingData();
  2965. cur_fit_nums = 0;
  2966. }
  2967. }
  2968. int nRef = REF_POSITION_NUM * 4;
  2969. if (long_his_pos.size() == nRef)
  2970. {
  2971. long_his_pos.pop_front();
  2972. }
  2973. long_his_pos.push_back(p);
  2974. if (long_his_pos.size() == nRef)
  2975. {
  2976. CalcLongFittingData();
  2977. cur_fit_nums = 0;
  2978. }
  2979. }
  2980. return 0;
  2981. }
  2982. /*
  2983. * 通过加速度或速度阈值来检查p定位结果的正确性
  2984. *
  2985. * param
  2986. * p 定位结果
  2987. *
  2988. * return
  2989. * 如果通过检查返回0,否则返回非零值
  2990. */
  2991. int Card::CheckSolution(std::shared_ptr<POS>& p)
  2992. {
  2993. //定位成功
  2994. double cx = p->cx;
  2995. double cy = p->cy;
  2996. double cz = p->cz;
  2997. double cvx = 0.0,cvy = 0.0,cvz = 0.0;
  2998. double av = 0.0;
  2999. if(is_algo_first_location){
  3000. sync_data sdNew;
  3001. sdNew.sync_num = this->m_nCalcSyncNum;
  3002. sdNew.x = cx;
  3003. sdNew.y = cy;
  3004. sdNew.vx = 0;
  3005. sdNew.vy = 0;
  3006. sdNew.update = false;
  3007. m_syncNumList.push_back(sdNew);
  3008. }else{
  3009. //现在的关于同步序号的处理是这样的:
  3010. //如果定位成功,就把这次定位成功的同步数据:同步序号,坐标;x,y方向的速度,扔到一个队列里,
  3011. //后来定位成功的就会先根据同步序号差用加速度抛一次;
  3012. //抛不掉,就用队列里的同步数据(从后往前找),找到第一个与当前同步序号相差大于5的同步数据来进行第二次计算速度以及加速度,
  3013. //如果加速度大于5,就不要此次的定位数据,
  3014. //如果通过加速度判断就将队列中从头开始到此同步数据的所有元素都丢弃,并插入新的此次同步数据
  3015. double interval_time = 0.2;
  3016. double deltaT = 0;
  3017. sync_data sd;
  3018. if (this->b_long_interval)
  3019. {
  3020. //此段代码用于将上一次定位是根据两个时间差是个很大值而定位出的结果
  3021. //当后续定位时就和最近的定位结果进行比较
  3022. //例如:当上一次同步序号是14321,它定位时比较的同步序号是14200,时间差大于20多秒
  3023. //这时我们就将b_long_interval置为true
  3024. //当本次定位,同步序号是14326,,这时就需要根据最近的14321进行判断
  3025. list<sync_data>::reverse_iterator it = m_syncNumList.rbegin();
  3026. sync_data sd = *it;
  3027. //以下计算deltaT还需要考虑卡的同步序号轮回的情况。
  3028. if (m_nCalcSyncNum > it->sync_num)
  3029. {
  3030. deltaT = (m_nCalcSyncNum - sd.sync_num)*interval_time;
  3031. }
  3032. else
  3033. {
  3034. deltaT = (m_nCalcSyncNum + 65536 - sd.sync_num)*interval_time;
  3035. }
  3036. if (deltaT < 10 && deltaT > 0)
  3037. {
  3038. this->b_long_interval = false;
  3039. }
  3040. //避免同一个同步序号下存在多个不同卡序号
  3041. if (deltaT < 1E-2)
  3042. {
  3043. deltaT = 0.2;
  3044. }
  3045. p->diff_reader_sync_num = deltaT;
  3046. p->ref_x = sd.x;
  3047. p->ref_y = sd.y;
  3048. cvx = (cx - sd.x)*this->map_scale/deltaT;
  3049. cvy = (cy - sd.y)*this->map_scale/deltaT;
  3050. double avx = (cvx - sd.vx) / deltaT;
  3051. double avy = (cvy - sd.vy) / deltaT;
  3052. av = sqrt(pow(avx,2) + pow(avy,2));
  3053. //车卡的加速度
  3054. switch(this->card_type){
  3055. case CT_PERSON:
  3056. if(av > PERSON_ACCELERATE_THRESHOLD){
  3057. p->reason = CHECK_PERSON_ACCE_OVER_SPEED;
  3058. //this->x = this->last_locate.x;
  3059. //this->y = this->last_locate.y;
  3060. p->posx = INVALID_COORDINATE;
  3061. p->posy = INVALID_COORDINATE;
  3062. p->posz = INVALID_COORDINATE;
  3063. p->cx = p->posx / map_scale;
  3064. p->cy = p->posy / map_scale;
  3065. p->cz = p->posz / map_scale;
  3066. this->b_long_interval = false;
  3067. m_afmData.bStatus = true;
  3068. m_afmData.strCardId = this->card_id;
  3069. m_afmData.nType = ALGO_FAILED_CONDITION_5;
  3070. ALGORITHM_FAILED(ALGO_FAILED_CONDITION_5);
  3071. return CHECK_PERSON_ACCE_OVER_SPEED;
  3072. }
  3073. break;
  3074. case CT_VEHICLE:
  3075. if(av > VECHILE_ACCELERATE_THRESHOLD){
  3076. //保留上次结果
  3077. p->reason = CHECK_VEHICLE_ACCE_OVER_SPEED;
  3078. //this->x = this->last_locate.x;
  3079. //this->y = this->last_locate.y;
  3080. p->posx = INVALID_COORDINATE;
  3081. p->posy = INVALID_COORDINATE;
  3082. p->posz = INVALID_COORDINATE;
  3083. p->cx = p->posx / map_scale;
  3084. p->cy = p->posy / map_scale;
  3085. p->cz = p->posz / map_scale;
  3086. this->b_long_interval = false;
  3087. m_afmData.bStatus = true;
  3088. m_afmData.strCardId = this->card_id;
  3089. m_afmData.nType = ALGO_FAILED_CONDITION_6;
  3090. ALGORITHM_FAILED(ALGO_FAILED_CONDITION_6);
  3091. return CHECK_VEHICLE_ACCE_OVER_SPEED;
  3092. }
  3093. break;
  3094. }
  3095. }else{
  3096. //从队列尾部开始查找,找到第一个同步序号与当前计算卡的同步序号相差5个以上的数据
  3097. list<sync_data>::reverse_iterator it;
  3098. bool bOverflow = false;
  3099. //sync_data sd;
  3100. bool bfind =false;
  3101. for(it = m_syncNumList.rbegin();it!=m_syncNumList.rend();it++){
  3102. if(m_nCalcSyncNum - it->sync_num >= 5){
  3103. bfind = true;
  3104. sd = *it;
  3105. break;
  3106. }else{
  3107. if(m_nCalcSyncNum - it->sync_num < 0 && m_nCalcSyncNum < 100){
  3108. //如果最新同步号小于列表中的同步号则
  3109. if(m_nCalcSyncNum + 65536 - it->sync_num >=5 ){//5
  3110. bOverflow = true;
  3111. bfind = true;
  3112. sd = *it;
  3113. }
  3114. }else{
  3115. continue;
  3116. }
  3117. }//end else
  3118. }//end for
  3119. //这里不能对前5次定位的数据做加速度处理,不然会出现抛不掉的
  3120. //需要处理,那么第一次定位的成功很重要
  3121. //处理前5次的
  3122. /*if(!bfind){
  3123. sd = *(m_syncNumList.begin());
  3124. }*/
  3125. //根据溢出条件来计算deltaT
  3126. if(bOverflow){
  3127. deltaT = (m_nCalcSyncNum + 65536 - sd.sync_num)*interval_time;
  3128. }else{
  3129. deltaT = (m_nCalcSyncNum - sd.sync_num)*interval_time;
  3130. }
  3131. p->diff_reader_sync_num = deltaT;
  3132. p->ref_x = sd.x;
  3133. p->ref_y = sd.y;
  3134. //速度正负的判断:以x轴,y轴正向运动为正
  3135. //如果x相等,则y2 - y1 > 0为正
  3136. //其他情况,则x2 - x1 > 0 为正
  3137. int nSign = 1;
  3138. if(cx == sd.x){
  3139. if(cy > sd.y){
  3140. nSign = 1;
  3141. }else{
  3142. nSign = -1;
  3143. }
  3144. }else{
  3145. if(cx > sd.x){
  3146. nSign = 1;
  3147. }else{
  3148. nSign = -1;
  3149. }
  3150. }
  3151. //转为m/s
  3152. cvx = (cx - sd.x)*this->map_scale/deltaT;
  3153. cvy = (cy - sd.y)*this->map_scale/deltaT;
  3154. double cv = sqrt(pow(cvx,2) + pow(cvy,2));
  3155. cv = cv*nSign;
  3156. double avx = (cvx - sd.vx) / deltaT;
  3157. double avy = (cvy - sd.vy) / deltaT;
  3158. double av = sqrt(pow(avx,2) + pow(avy,2));
  3159. //车卡的加速度
  3160. switch(this->card_type){
  3161. case CT_PERSON:
  3162. if(av > PERSON_ACCELERATE_THRESHOLD){
  3163. p->reason = CHECK_PERSON_ACCE_OVER_SPEED;
  3164. //this->x = this->last_locate.x;
  3165. //this->y = this->last_locate.y;
  3166. p->posx = INVALID_COORDINATE;
  3167. p->posy = INVALID_COORDINATE;
  3168. p->posz = INVALID_COORDINATE;
  3169. p->cx = p->posx / map_scale;
  3170. p->cy = p->posy / map_scale;
  3171. p->cz = p->posz / map_scale;
  3172. m_afmData.bStatus = true;
  3173. m_afmData.strCardId = this->card_id;
  3174. m_afmData.nType = ALGO_FAILED_CONDITION_5;
  3175. ALGORITHM_FAILED(ALGO_FAILED_CONDITION_5);
  3176. return CHECK_PERSON_ACCE_OVER_SPEED;
  3177. }
  3178. break;
  3179. case CT_VEHICLE:
  3180. if(av > VECHILE_ACCELERATE_THRESHOLD){
  3181. //保留上次结果
  3182. p->reason = CHECK_VEHICLE_ACCE_OVER_SPEED;
  3183. //this->x = this->last_locate.x;
  3184. //this->y = this->last_locate.y;
  3185. p->posx = INVALID_COORDINATE;
  3186. p->posy = INVALID_COORDINATE;
  3187. p->posz = INVALID_COORDINATE;
  3188. p->cx = p->posx / map_scale;
  3189. p->cy = p->posy / map_scale;
  3190. p->cz = p->posz / map_scale;
  3191. m_afmData.bStatus = true;
  3192. m_afmData.strCardId = this->card_id;
  3193. m_afmData.nType = ALGO_FAILED_CONDITION_6;
  3194. ALGORITHM_FAILED(ALGO_FAILED_CONDITION_6);
  3195. return CHECK_VEHICLE_ACCE_OVER_SPEED;
  3196. }
  3197. break;
  3198. }
  3199. cv = cv*3.6;
  3200. //速度的限制
  3201. if(fabs(cv) > MAX_VECHILE_SPEED){
  3202. p->reason = CHECK_VEHICLE_OVER_SPEED;
  3203. //this->x = this->last_locate.x;
  3204. //this->y = this->last_locate.y;
  3205. p->posx = INVALID_COORDINATE;
  3206. p->posy = INVALID_COORDINATE;
  3207. p->posz = INVALID_COORDINATE;
  3208. p->cx = p->posx / map_scale;
  3209. p->cy = p->posy / map_scale;
  3210. p->cz = p->posz / map_scale;
  3211. m_afmData.bStatus = true;
  3212. m_afmData.strCardId = this->card_id;
  3213. m_afmData.nType = ALGO_FAILED_CONDITION_7;
  3214. ALGORITHM_FAILED(ALGO_FAILED_CONDITION_7);
  3215. return CHECK_VEHICLE_OVER_SPEED;
  3216. }
  3217. //使用间隔来修正速度
  3218. if(deltaT - 1.0 >= 0){
  3219. if (deltaT > 10 && bfind)
  3220. {
  3221. this->b_long_interval = true;
  3222. }
  3223. //删除第一个元素到tmp(含)之间的所有元素
  3224. bool bStartDel = false;
  3225. for(list<sync_data>::reverse_iterator tmp = m_syncNumList.rbegin();tmp != m_syncNumList.rend();)
  3226. {
  3227. if(bStartDel){
  3228. tmp = list<sync_data>::reverse_iterator(m_syncNumList.erase((++tmp).base()));
  3229. }else{
  3230. if(*tmp == sd){
  3231. bStartDel = true;
  3232. }
  3233. ++tmp;
  3234. }
  3235. }
  3236. }
  3237. if (!bfind)
  3238. {
  3239. cvx = 0;
  3240. cvy = 0;
  3241. cvz = 0;
  3242. av = 0;
  3243. }
  3244. }
  3245. this->m_nSyncNumInList = sd.sync_num;
  3246. p->update = true;
  3247. }
  3248. p->cvx = cvx;
  3249. p->cvy = cvy;
  3250. p->cvz = cvz;
  3251. p->av = av;
  3252. return 0;
  3253. }
  3254. /*
  3255. * 通过趋向性判断解的可靠性,暂通过上下行判断
  3256. *
  3257. * param
  3258. * p 当前定位坐标信息
  3259. *
  3260. * return
  3261. * 通过验证返回0,否则返回错误码
  3262. *
  3263. */
  3264. int Card::CheckSulutionByStream(std::shared_ptr<POS> p)
  3265. {
  3266. if (m_nStream == 0)
  3267. {
  3268. return 0;
  3269. }
  3270. if (abs(this->last_locate.x) > 1E-5 || abs(this->last_locate.y) > 1E-5)
  3271. {
  3272. //去除采集刚启动,last_locate的坐标为零而导致判断车辆上下行方向错误的问题
  3273. //车辆上下行确定
  3274. //3个条件:起点(x1,y1),终点(x2,y2)
  3275. //1.x1==x2的情况下,y2>y1为下行
  3276. //2.y1==y2的情况下,x1>x2为下行
  3277. //3.x1>x2且y2>y1为下行
  3278. //其他情况为上行
  3279. int nStream = 0;
  3280. double cx = p->posx / (map_scale*1.0);
  3281. double cy = p->posy / (map_scale*1.0);
  3282. if ((this->last_locate.x == cx && cy > this->last_locate.y)
  3283. ||(this->last_locate.x > cx && cy == this->last_locate.y)
  3284. ||(this->last_locate.x > cx && cy > this->last_locate.y))
  3285. {
  3286. nStream = DOWN_STREAM;
  3287. }
  3288. else
  3289. {
  3290. nStream = UP_STREAM;
  3291. }
  3292. if (nStream != m_nStream)
  3293. {
  3294. return 1;
  3295. }
  3296. }
  3297. return 0;
  3298. }
  3299. bool Card::IsExistPath(int left,int right)
  3300. {
  3301. TDOAReaderPathMap::iterator it = pTdoaReaderPathMap->find(left);
  3302. if (it == pTdoaReaderPathMap->end())
  3303. {
  3304. return false;
  3305. }
  3306. ReaderPathMap::iterator it_s = it->second->find(right);
  3307. if (it_s == it->second->end())
  3308. {
  3309. return false;
  3310. }
  3311. return true;
  3312. }
  3313. bool Card::IsIdleStatus(std::shared_ptr<POS> pos)
  3314. {
  3315. bool bRet = false;
  3316. if (idle_pos_list.size() == REF_POSITION_NUM)
  3317. {
  3318. idle_pos_list.pop_front();
  3319. }
  3320. idle_pos_list.push_back(pos);
  3321. double acq = 0.3; //精度范围
  3322. _point cp;
  3323. cp.x = cp.y = cp.z = 0;
  3324. int i = 0;
  3325. double ax = 0,ay = 0;
  3326. if (idle_pos_list.size() == REF_POSITION_NUM)
  3327. {
  3328. for (std::list<std::shared_ptr<POS>>::iterator it = idle_pos_list.begin(); it != idle_pos_list.end();++it)
  3329. {
  3330. ax += (*it)->posx;
  3331. ay += (*it)->posy;
  3332. }
  3333. ax /= REF_POSITION_NUM;
  3334. ay /= REF_POSITION_NUM;
  3335. }else{
  3336. return bRet;
  3337. }
  3338. i = 0;
  3339. for (std::list<std::shared_ptr<POS>>::iterator it = idle_pos_list.begin(); it != idle_pos_list.end();++it)
  3340. {
  3341. double distance = 0;
  3342. distance = sqrt(pow((*it)->posx - ax,2) + pow((*it)->posy - ay,2));
  3343. if (distance < 1)
  3344. {
  3345. i++;
  3346. }else{
  3347. break;
  3348. }
  3349. }
  3350. if (i == 5)
  3351. {
  3352. bRet = true;
  3353. }
  3354. return bRet;
  3355. }
  3356. /*
  3357. * save origin locate infomation
  3358. *
  3359. * param
  3360. * pos the value of location
  3361. *
  3362. * return
  3363. * zero if successed.
  3364. */
  3365. int Card::SaveOriginDataBeforeFilter(std::shared_ptr<POS> pos)
  3366. {
  3367. origin_locate.x = pos->cx;
  3368. origin_locate.y = pos->cy;
  3369. origin_locate.z = pos->cz;
  3370. origin_locate.v = sqrt(pow(pos->cvx,2) + pow(pos->cvy,2))*3.6; //*3.6转为km/h
  3371. origin_locate.sync_num = this->m_nCalcSyncNum;
  3372. return 0;
  3373. }
  3374. int Card::ChooseOneSolution(std::shared_ptr<ReceiveDataMap> pRdm, std::vector<std::shared_ptr<POS>> udm_pos, std::shared_ptr<POS>& pos )
  3375. {
  3376. if(0 == udm_pos.size()){ // 没有解
  3377. return SOLUTION_NO_SOLUTION;
  3378. }
  3379. // 只有一个解
  3380. if(1 == udm_pos.size()){
  3381. //判断此位置距离分站的距离是否为4米范围内
  3382. ReceiveDataMap::iterator it_first = pRdm->end();
  3383. ReceiveDataMap::iterator it_second = pRdm->end();
  3384. for (ReceiveDataMap::iterator it = pRdm->begin();it != pRdm->end();++it)
  3385. {
  3386. if (it->second->reader_id == udm_pos.at(0)->nFirstReader)
  3387. {
  3388. it_first = it;
  3389. }
  3390. if (it->second->reader_id == udm_pos.at(0)->nSecondReader)
  3391. {
  3392. it_second = it;
  3393. }
  3394. }
  3395. double dist = 0.0;
  3396. dist = sqrt(pow(udm_pos.at(0)->posx - it_first->second->x,2)+pow(udm_pos.at(0)->posy - it_first->second->y,2));
  3397. if (dist < NEAR_READER && it_first->second->special == 0)
  3398. {
  3399. return SOLUTION_NEAR_READER;
  3400. }
  3401. dist = sqrt(pow(udm_pos.at(0)->posx - it_second->second->x,2)+pow(udm_pos.at(0)->posy - it_second->second->y,2));
  3402. if (dist < NEAR_READER && it_second->second->special == 0)
  3403. {
  3404. return SOLUTION_NEAR_READER;
  3405. }
  3406. //*pos = *udm_pos.at(0);
  3407. pos->posx = udm_pos.at(0)->posx;
  3408. pos->posy = udm_pos.at(0)->posy;
  3409. pos->posz = udm_pos.at(0)->posz;
  3410. pos->cx = pos->posx / (this->map_scale*1.0);
  3411. pos->cy = pos->posy / (this->map_scale*1.0);
  3412. pos->cz = pos->posz / (this->map_scale*1.0);
  3413. return 0;
  3414. }
  3415. ReceiveDataMap::iterator itf = pRdm->begin();
  3416. int bf = itf->second->reader_id;
  3417. std::advance(itf,1);
  3418. int bs = itf->second->reader_id;
  3419. // 有多个解,含两解或两解以上
  3420. //判断依据:选取无地图集的分站数据来校正所有的解,
  3421. //解可靠的条件如下:1解与无地图集的时间戳距离差最小,2且此距离差不大于10米
  3422. for (ReceiveDataMap::reverse_iterator it_first = pRdm->rbegin();it_first!=pRdm->rend();++it_first)
  3423. {
  3424. ReceiveDataMap::reverse_iterator it_second = it_first;
  3425. std::advance(it_second,1);
  3426. if (it_second == pRdm->rend())
  3427. {
  3428. //找到尾部了还未找到合适解,即认为无解
  3429. pos->reason = 1;
  3430. break;
  3431. }
  3432. //反向查找,还要去掉这两个之间有地图集的数据
  3433. int nfr = it_first->second->reader_id;
  3434. int nfs = it_second->second->reader_id;
  3435. if (bf!=nfs && bs!=nfr)
  3436. {
  3437. if (IsExistPath(nfr,nfs))
  3438. {
  3439. continue;
  3440. }
  3441. }
  3442. int nCounts = 0;
  3443. for (std::vector<std::shared_ptr<POS>>::iterator it_pos = udm_pos.begin();it_pos != udm_pos.end();++it_pos)
  3444. {
  3445. //此点在这两个线段内,才允许使用此判断条件
  3446. _point p,l1,l2;
  3447. p.x = (*it_pos)->posx;
  3448. p.y = (*it_pos)->posy;
  3449. l1.x = it_first->second->x;
  3450. l1.y = it_first->second->y;
  3451. l2.x = it_second->second->x;
  3452. l2.y = it_second->second->y;
  3453. if (!LocateAlgorithm::IsInLine(p,l1,l2))
  3454. {
  3455. nCounts++;
  3456. continue;
  3457. }
  3458. double distance_reader = 0;
  3459. distance_reader = fabs(sqrt(pow((*it_pos)->posx - it_first->second->x,2) + pow((*it_pos)->posy - it_first->second->y,2)) - sqrt(pow((*it_pos)->posx - it_second->second->x,2) + pow((*it_pos)->posy - it_second->second->y,2)));
  3460. long long dt = it_first->second->rec_time_stamp - it_second->second->rec_time_stamp;
  3461. double distance_tt = 0;
  3462. distance_tt = fabs(CFunctions::getDistance(dt,CFunctions::TDOA));
  3463. (*it_pos)->dis_diff = fabs(distance_reader - distance_tt);
  3464. }
  3465. if (nCounts == udm_pos.size())
  3466. {
  3467. continue;
  3468. }
  3469. double dMinDiff = 99999.9;
  3470. std::vector<std::shared_ptr<POS>>::iterator it_find = udm_pos.end();
  3471. for (std::vector<std::shared_ptr<POS>>::iterator it_pos = udm_pos.begin();it_pos != udm_pos.end();++it_pos)
  3472. {
  3473. if ((*it_pos)->dis_diff < dMinDiff)
  3474. {
  3475. _point p,l1,l2;
  3476. p.x = (*it_pos)->posx;
  3477. p.y = (*it_pos)->posy;
  3478. for (ReceiveDataMap::iterator it = pRdm->begin();it != pRdm->end();++it)
  3479. {
  3480. if (it->second->reader_id == (*it_pos)->nFirstReader)
  3481. {
  3482. l1.x = it->second->x;
  3483. l1.y = it->second->y;
  3484. }
  3485. if (it->second->reader_id == (*it_pos)->nSecondReader)
  3486. {
  3487. l2.x = it->second->x;
  3488. l2.y = it->second->y;
  3489. }
  3490. }
  3491. if (!LocateAlgorithm::IsInLine(p,l1,l2))
  3492. {
  3493. continue;
  3494. }else{
  3495. if (((*it_pos)->nFirstReader == nfr &&(*it_pos)->nSecondReader == nfs)||
  3496. ((*it_pos)->nFirstReader == nfs &&(*it_pos)->nSecondReader == nfr))
  3497. {
  3498. continue;
  3499. }else{
  3500. dMinDiff = (*it_pos)->dis_diff;
  3501. it_find = it_pos;
  3502. }
  3503. }
  3504. }
  3505. }
  3506. if (it_find != udm_pos.end())
  3507. {
  3508. if ((*it_find)->dis_diff < 10)
  3509. {
  3510. //找到即退出
  3511. //pos = *it_find;
  3512. pos->posx = (*it_find)->posx;
  3513. pos->posy = (*it_find)->posy;
  3514. pos->posz = (*it_find)->posz;
  3515. pos->reason = 0;
  3516. break;
  3517. }
  3518. }
  3519. }
  3520. if (pos->reason == 0)
  3521. {
  3522. pos->cx = pos->posx / (this->map_scale*1.0);
  3523. pos->cy = pos->posy / (this->map_scale*1.0);
  3524. pos->cz = pos->posz / (this->map_scale*1.0);
  3525. }
  3526. // 未计算出结果
  3527. if(pos->posx == INVALID_COORDINATE || pos->posy == INVALID_COORDINATE){
  3528. LOCATION_SYSTEM_BRANCH(LOCATION_SYSTEM_BRANCH_148);
  3529. return SOLUTION_NO_SOLUTION;
  3530. }else{
  3531. bool bExistSolution = true;
  3532. //对这唯一解最最后一步筛选,在分站1米内
  3533. //增加这个条件主要是为了解决,当距离最近的分站数据未收到的情况,
  3534. //比如实际数据应该是103,102,101,,但目前收到的数据为102,101,但定位到102附近且在102和101之间
  3535. for (ReceiveDataMap::iterator it = pRdm->begin();it != pRdm->end();++it)
  3536. {
  3537. double distance = 0.0;
  3538. distance = sqrt(pow(it->second->x - pos->posx,2) + pow(it->second->y - pos->posy,2));
  3539. if (distance < 1)
  3540. {
  3541. bExistSolution = false;
  3542. break;
  3543. }
  3544. }
  3545. //无解
  3546. if (!bExistSolution)
  3547. {
  3548. std::shared_ptr<POS> tmp = std::make_shared<POS>();
  3549. pos->posx = tmp->posx;
  3550. pos->posy = tmp->posy;
  3551. pos->cx = 0;
  3552. pos->cy = 0;
  3553. pos->cz = 0;
  3554. return SOLUTION_NO_SOLUTION;
  3555. }
  3556. }
  3557. return 0;
  3558. }
  3559. int Card::GetDeltaT( map<unsigned long long,std::shared_ptr<_coordinate>> dl )
  3560. {
  3561. unsigned long long revTime = 0;
  3562. int rid = 0, sn = 0, ct = 0;
  3563. map<unsigned long long,std::shared_ptr<_coordinate>>::iterator it_mpdl = dl.begin();
  3564. int i = 0;
  3565. for(; it_mpdl != dl.end(); ++it_mpdl){
  3566. if(0 == it_mpdl->second->tt){
  3567. continue;
  3568. }
  3569. if(this->locate && it_mpdl->second->reader_id == this->locate->ReaderId()){
  3570. revTime = it_mpdl->second->tt;
  3571. rid = it_mpdl->second->reader_id;
  3572. sn = it_mpdl->second->sync_num;
  3573. ct = it_mpdl->second->t;
  3574. break;
  3575. }
  3576. if(0 == revTime){
  3577. revTime = it_mpdl->second->tt;
  3578. rid = it_mpdl->second->reader_id;
  3579. sn = it_mpdl->second->sync_num;
  3580. ct = it_mpdl->second->t;
  3581. }
  3582. else if( revTime < it_mpdl->second->tt){ // 跨周期,可能会取到较大值
  3583. revTime = it_mpdl->second->tt;
  3584. rid = it_mpdl->second->reader_id;
  3585. sn = it_mpdl->second->sync_num;
  3586. ct = it_mpdl->second->t;
  3587. }
  3588. }
  3589. std::shared_ptr<nspLocate::LocateRecord> loc = std::make_shared<nspLocate::LocateRecord>(revTime, rid, sn, ct);
  3590. loc->getDeltaT(this->locate);
  3591. this->locate = loc;
  3592. return 0;
  3593. }
  3594. void Card::PosState( int val )
  3595. {
  3596. pos_state = val;
  3597. pos_state_count = pos_state_confirm_times;
  3598. }
  3599. int const Card::PosState()
  3600. {
  3601. if(pos_state_count >= pos_state_confirm_times )
  3602. {
  3603. return pos_state;
  3604. }
  3605. return pos_state_old;
  3606. }
  3607. void Card::PosStateOld( int val )
  3608. {
  3609. pos_state_old = val;
  3610. }
  3611. // 只显示优先级最高的状态
  3612. int const Card::StateBiz()
  3613. {
  3614. state_biz = 0;
  3615. if(status_help == STATUS_ERROR){
  3616. state_biz = STATUS_HELP;
  3617. return state_biz;
  3618. }
  3619. if(status_lost == STATUS_ERROR){
  3620. state_biz = STATUS_LOST;
  3621. return state_biz;
  3622. }
  3623. if(status_area_forbidden == STATUS_ERROR){
  3624. state_biz += STATUS_AREA_FORBIDDEN;
  3625. return state_biz;
  3626. }
  3627. if(status_area_over_speed == STATUS_ERROR){
  3628. state_biz = STATUS_AREA_OVER_SPEED;
  3629. return state_biz;
  3630. }
  3631. if(status_over_speed == STATUS_ERROR){
  3632. state_biz = STATUS_OVER_SPEED;
  3633. return state_biz;
  3634. }
  3635. if(status_area_over_time == STATUS_ERROR){
  3636. state_biz = STATUS_AREA_OVER_TIME;
  3637. return state_biz;
  3638. }
  3639. if(status_area_over_time == STATUS_ERROR){
  3640. state_biz = STATUS_OVER_TIME;
  3641. return state_biz;
  3642. }
  3643. if(status_call == STATUS_ERROR){
  3644. state_biz = STATUS_CALL;
  3645. return state_biz;
  3646. }
  3647. return state_biz;
  3648. }
  3649. MapInfo::MapInfo( void )
  3650. {
  3651. }
  3652. MapInfo::~MapInfo( void )
  3653. {
  3654. }
  3655. Dept::Dept( int id, string name )
  3656. {
  3657. dept_id = id;
  3658. dept_name = name;
  3659. }
  3660. Dept::Dept()
  3661. {
  3662. }
  3663. Dept::~Dept()
  3664. {
  3665. }
  3666. OccLevel::OccLevel(int id )
  3667. {
  3668. occlevel_id = id;
  3669. }
  3670. OccLevel::OccLevel()
  3671. {
  3672. }
  3673. OccLevel::~OccLevel()
  3674. {
  3675. }
  3676. Adhoc::Adhoc()
  3677. {
  3678. adhoc_id = 0;
  3679. x = 0;
  3680. y = 0;
  3681. z = 0;
  3682. idx = 0;
  3683. }
  3684. Adhoc::~Adhoc()
  3685. {
  3686. }
  3687. Light::Light()
  3688. {
  3689. m_nID = m_nMapID = m_nAreaID = m_nLightsGroupID = m_nReaderID = m_nSectionID = m_nPort = m_nState = m_nStream = 0;
  3690. device_type_id = 0;
  3691. m_nOldState = 0;
  3692. m_nShape = 0;
  3693. x = y = z = 0.0;
  3694. m_nIsCtrl = LIGHT_CTRL_STATE::CTRL_STATE_AUTO;
  3695. m_strIP = m_strName = m_strLabel = "";
  3696. rec_time = time(NULL);
  3697. alarm_start_time = time(NULL);
  3698. last_send_time = time(NULL);
  3699. }
  3700. int Light::get_light_state()
  3701. {
  3702. return m_nState;
  3703. }
  3704. Chamber::Chamber()
  3705. {
  3706. m_nID = m_nIsUsed = m_nMapID = m_nState = m_nSectionID = 0;
  3707. m_strPath = m_strName = m_strLabel = "";
  3708. }
  3709. Chamber::~Chamber()
  3710. {
  3711. }
  3712. /*
  3713. * 初始化区域边界
  3714. *
  3715. * param
  3716. * path 边界字符串描述
  3717. *
  3718. * return
  3719. * 成功返回0,失败返回1
  3720. */
  3721. int BaseArea::init_border(string path)
  3722. {
  3723. if(path == "" || path.find(',') == std::string::npos){
  3724. return 1;
  3725. }
  3726. std::vector<std::string> vec = Split(path, " ");
  3727. std::vector<std::string>::iterator it = vec.begin();
  3728. if(m_pPolygon){
  3729. delete[] m_pPolygon;
  3730. m_pPolygon = NULL;
  3731. }
  3732. m_pPolygon = new _point[vec.size()];
  3733. m_nPolygonCount = 0;
  3734. for(; it != vec.end(); ++it){
  3735. std::vector<std::string> subvec = Split(it->c_str(), ",");
  3736. _point p;
  3737. p.x = GetVertex(subvec[0]);
  3738. p.y = GetVertex(subvec[1]);
  3739. p.z = 0;
  3740. m_pPolygon[m_nPolygonCount] = p;
  3741. m_nPolygonCount++;
  3742. }
  3743. return 0;
  3744. }
  3745. bool BaseArea::IsInPolygon(_point p)
  3746. {
  3747. if(m_pPolygon == NULL){
  3748. return false;
  3749. }
  3750. int counter = 0;
  3751. int i;
  3752. double xinters;
  3753. _point p1,p2;
  3754. p1 = m_pPolygon[0];
  3755. for (int i=1;i<= m_nPolygonCount;i++) {
  3756. p2 = m_pPolygon[i % m_nPolygonCount];
  3757. if (p.y > MIN(p1.y,p2.y)) {
  3758. if (p.y <= MAX(p1.y,p2.y)) {
  3759. if (p.x <= MAX(p1.x,p2.x)) {
  3760. if (p1.y != p2.y) {
  3761. xinters = (p.y-p1.y)*(p2.x-p1.x)/(p2.y-p1.y)+p1.x;
  3762. if (p1.x == p2.x || p.x <= xinters)
  3763. counter++;
  3764. }
  3765. }
  3766. }
  3767. }
  3768. p1 = p2;
  3769. }
  3770. return (counter % 2 == 0) ? false : true;
  3771. }
  3772. double BaseArea::GetVertex(std::string src)
  3773. {
  3774. std::string dest = "";
  3775. for(unsigned int i = 0; i < src.length(); i++){
  3776. if((src[i] >= '0' && src[i]<='9') || src[i]=='-' || src[i] == '.'){
  3777. dest += src[i];
  3778. }
  3779. }
  3780. return atof(dest.c_str());
  3781. }
  3782. std::vector<std::string> BaseArea::Split(std::string str,std::string pattern)
  3783. {
  3784. std::string::size_type pos;
  3785. std::vector<std::string> result;
  3786. str+=pattern;//扩展字符串以方便操作
  3787. unsigned int size=str.size();
  3788. for(unsigned int i=0; i<size; i++){
  3789. pos=str.find(pattern,i);
  3790. if(pos<size){
  3791. std::string s=str.substr(i,pos-i);
  3792. result.push_back(s);
  3793. i=pos+pattern.size()-1;
  3794. }
  3795. }
  3796. return result;
  3797. }
  3798. Section::Section()
  3799. {
  3800. m_nID = m_nMapId = m_nState = 0;
  3801. m_strName = m_strLabel = m_strPath = "";
  3802. for(int i = 0; i < SECTION_EVENT_COUNT; i++){
  3803. m_event_list[i] = 0;
  3804. }
  3805. }
  3806. bool Section::is_has_chamber()
  3807. {
  3808. bool bIsHasEmptyChamber = false;
  3809. if (mp_champer_list.size()<=0)
  3810. {
  3811. return bIsHasEmptyChamber;
  3812. }
  3813. ChamberMap::iterator it = mp_champer_list.begin();
  3814. for (;it!=mp_champer_list.end();++it)
  3815. {
  3816. if (it->second->m_nIsUsed == false)
  3817. {
  3818. bIsHasEmptyChamber = true;
  3819. break;
  3820. }
  3821. }
  3822. return bIsHasEmptyChamber;
  3823. }
  3824. int Section::get_section_state()
  3825. {
  3826. int nTotals = 0;
  3827. nTotals = mp_vehicle_list.size();
  3828. switch (nTotals)
  3829. {
  3830. case 0:
  3831. m_nState = SECTION_STATE_NORMAL;
  3832. break;
  3833. case 1:
  3834. m_nState = SECTION_STATE_BUSY;
  3835. break;
  3836. case 2:
  3837. m_nState = SECTION_STATE_CONGESTION;
  3838. break;
  3839. default:
  3840. m_nState = SECTION_STATE_CONGESTION;
  3841. break;
  3842. }
  3843. return m_nState;
  3844. }
  3845. int Section::get_section_vehicle_counts()
  3846. {
  3847. return mp_vehicle_list.size();
  3848. }
  3849. int Section::get_section_staffer_counts()
  3850. {
  3851. return mp_staffer_list.size();
  3852. }
  3853. LightsGroup::LightsGroup()
  3854. {
  3855. m_bIsUsed = false;
  3856. m_nID = m_nState = 0;
  3857. m_strLabel = m_strName = m_strVechileId = "";
  3858. }
  3859. /*
  3860. * 判断红绿灯组中是否有此灯
  3861. *
  3862. * param
  3863. * pLight 灯对象
  3864. *
  3865. * return
  3866. * 存在返回true,不存在返回false
  3867. */
  3868. bool LightsGroup::isExist(std::shared_ptr<Light> pLight)
  3869. {
  3870. bool bRet = false;
  3871. for (LightMap::iterator it = mp_lights_list.begin();it != mp_lights_list.end();++it)
  3872. {
  3873. //如果红绿灯的id,地图id,路段id,都和参数一致,且灯状态可用,即为找到
  3874. if (it->second->m_nID == pLight->m_nID
  3875. && it->second->m_nMapID == pLight->m_nMapID
  3876. && it->second->m_nSectionID == pLight->m_nSectionID
  3877. && it->second->m_nState == 0)
  3878. {
  3879. bRet = true;
  3880. }
  3881. }
  3882. return bRet;
  3883. }
  3884. /*
  3885. * 判断灯组内灯的颜色是否一致
  3886. *
  3887. * param
  3888. * nColor 指定颜色(红色或绿色)
  3889. *
  3890. * return
  3891. * 相同返回true,否则返回false
  3892. *
  3893. */
  3894. bool LightsGroup::isAllLightColor(int nColor)
  3895. {
  3896. bool bRet = true;
  3897. for (LightMap::iterator it = mp_lights_list.begin();it != mp_lights_list.end();++it)
  3898. {
  3899. if (it->second->m_nColor != nColor)
  3900. {
  3901. bRet = false;
  3902. }
  3903. }
  3904. return bRet;
  3905. }
  3906. PatrolTask::PatrolTask()
  3907. {
  3908. patrol_task_id = patrol_path_id = 0;
  3909. card_id = "";
  3910. starffer_id = "";
  3911. start_time = end_time = time(NULL);
  3912. mpPoint.swap(map<unsigned int, std::shared_ptr<PatrolPoint>>());
  3913. cur_point_idx = 1;
  3914. enter_time = leave_time = time(NULL);
  3915. state = stay_state = duration_stay = 0;
  3916. is_in_cur_point = false;
  3917. }
  3918. PatrolTask::~PatrolTask()
  3919. {
  3920. }
  3921. PatrolPoint::PatrolPoint()
  3922. {
  3923. idx = 0;
  3924. patrol_point_id = 0;
  3925. map_id = 0;
  3926. x = y = z = 0;
  3927. ranging = 0; // 距离范围
  3928. duration_last = duration_stay_min = duration_stay_max = duration_ranging = 0;
  3929. }
  3930. PatrolPoint::~PatrolPoint()
  3931. {
  3932. }
  3933. BanShift::BanShift()
  3934. {
  3935. }
  3936. BanShift::BanShift( int id, std::string s, std::string e )
  3937. {
  3938. shift_id = id;
  3939. start_time = s;
  3940. end_time = e;
  3941. }
  3942. BanShift::~BanShift()
  3943. {
  3944. }