classdef.cpp 105 KB

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