classdef.cpp 38 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590
  1. #include "stdafx.h"
  2. #include "classdef.h"
  3. #include "constdef.h"
  4. #include "structdef.h"
  5. #include "locate_algorithm.h"
  6. #include <math.h>
  7. Card::Card( string cardid, int cardtype, double z_offset, double offset_x /*= 12.0*/, double offset_y /*=12.0*/ )
  8. {
  9. InitializeCriticalSectionAndSpinCount(&m_csCard, MAXCRITICALSECTIONSPINCOUNT);
  10. card_type = cardtype;
  11. card_id = cardid;
  12. coor_offset_x = offset_x;
  13. coor_offset_y = offset_y;
  14. this->z_offset = z_offset;
  15. GetSystemTime(&deal_time);
  16. //GetSystemTime(&down_time);
  17. down_time = up_time = enter_area_time = enter_reader_time = rec_time = time(NULL);
  18. time_over_time = time_area_over_time = time_area_forbidden = time_over_speed = time_low_power = time(NULL);
  19. x = y = z = last_x = last_y = last_z = stored_x = stored_y = stored_z = 0;
  20. xx = yy = zz = 0;
  21. x1 = y1 = z1 = x2 = y2 = z2 = x3 = y3 = z3 = x4 = y4 = z4 = 0;
  22. a = 0;
  23. t = 0;
  24. init_postion = false;
  25. is_first_location = true;
  26. map_id = map_id_old = area_id = reader_id = 0;
  27. map_scale = 1.0;
  28. state = 0;
  29. state_moving = 0;
  30. state_biz = 0;
  31. pos_state = pos_state_old = DT_INIT;
  32. pos_state_park = pos_state_park_old = AAT_ENTER;
  33. pos_state_count = pos_state_park_count = 0;
  34. dept_id = group_id = 0;
  35. power_state = power_state_last = 0;
  36. status_help = status_area_over_time = status_area_forbidden = status_area_over_speed = 0;
  37. status_over_speed = status_over_time = status_power = status_lost = status_call = 0;
  38. sync_num = 0;
  39. isdealed = isreceive = is_pos_changed = is_hist = is_need_cal = false;
  40. is_area_over_time = is_mine_over_time = false;
  41. id = name = number = department = group = worktype = "";
  42. reader_tickcount = time_stamp = time_stamp_last = 0;
  43. p_dists = new _coordinate*[DIST_COUNT];
  44. p_dists_locate = new _coordinate*[DIST_COUNT];
  45. time_stamp_max = 0;
  46. for(int i = 0; i < DIST_COUNT; i++){
  47. p_dists[i] = NULL;
  48. p_dists_locate[i] = NULL;
  49. }
  50. is_deal_by_algo = false;
  51. pReaderPathMap = NULL;
  52. pTdoaReaderPathMap = NULL;
  53. last_s_locate_reader[0] = last_s_locate_reader[1] = -1;
  54. m_syncNumList.clear();
  55. m_dFirstDiff = m_dSecondDiff = 0.0;
  56. m_nCalcSyncNum = 0;
  57. m_nSyncNumInList = 0;
  58. last_locate.tt = 0;
  59. last_locate.a = last_locate.antenna_id = last_locate.reader_id = last_locate.t = 0;
  60. last_locate.x = last_locate.y = last_locate.z = last_locate.d = last_locate.d_offset = last_locate.v = 0.0;
  61. m_bUseFilter = false;
  62. m_nFilterType = NO_FILTER;
  63. m_pKalmanFilter = NULL;
  64. m_nLastLocateT = 0;
  65. origin_locate.x = origin_locate.y = origin_locate.z = origin_locate.v = 0.0;
  66. last_vx = last_vy = 0.0;
  67. v = 0.0;
  68. ins_weight = INS_WEIGHT;
  69. uwb_weight = UWB_WEIGHT;
  70. acce_cur_state = 0;
  71. acce_last_state = 0;
  72. b_long_interval = false;
  73. accelerate_state = accelerate_state_last = ACCELERATE_INIT_STATE; //默认初始的为-10
  74. ins_direction = 0;
  75. direction = 0;
  76. antenna_angle = antenna_angle_last = 0;
  77. time_stamp_last = 0;
  78. reader_id_last = 0;
  79. _dists.swap(DistQueMap());
  80. //m_pKalmanFilter = new CKalmanFilter();
  81. //m_pKalmanFilter->Initial(1);
  82. //m_pKalmanFilter->m_bFlag = false;
  83. }
  84. Card::Card( void )
  85. {
  86. }
  87. //void Card::set_pos_state( int val )
  88. //{
  89. // if(val == pos_state) {
  90. // pos_state_count++;
  91. // return;
  92. // }
  93. // pos_state = val;
  94. // pos_state_count = 1;
  95. //}
  96. void Card::set_reader( Reader* preader ) // 设置卡时间
  97. {
  98. //TRACE(_T("time memcpy 11\n"));
  99. this->rec_time = time(NULL);
  100. if(preader->reader_id == this->reader_id){ // 所在分站没有发生变化
  101. this->pos_state_count++;
  102. }else{ // 分站变化
  103. this->p_reader = preader;
  104. this->reader_id = preader->reader_id;
  105. this->enter_reader_time = this->rec_time;
  106. this->map_id_old = this->map_id;
  107. this->map_id = preader->map_id;
  108. this->map_scale = preader->map_scale;
  109. }
  110. }
  111. /*
  112. * 设置地图集覆盖范围,适用TOF
  113. *
  114. */
  115. void Card::set_reader_path(ReaderPathMap* rpm)
  116. {
  117. this->pReaderPathMap = rpm;
  118. }
  119. /*
  120. * 设置地图集覆盖范围,适用TDOA
  121. *
  122. */
  123. void Card::set_reader_path(TDOAReaderPathMap* trpm)
  124. {
  125. this->pTdoaReaderPathMap = trpm;
  126. }
  127. bool operator==(sync_data& a,sync_data&b){
  128. return a.sync_num == b.sync_num;
  129. }
  130. /*
  131. * 此函数主要判断此坐标是否需要形成json输出给webserver
  132. *
  133. * param
  134. * acce_state 算法中保存的当前加速度计状态
  135. * acce_state_last 算法中保存的上一次加速度计状态
  136. *
  137. * return
  138. * 无
  139. */
  140. void Card::inspect_coordinate(int acce_state)
  141. {
  142. this->isoutput = false;
  143. /*CString str = _T("");
  144. str.Format(_T("state : %d \r\n"),acce_state);
  145. TRACE(str);*/
  146. //如果是静止或者怠速状态,每次的定位坐标不变化,此时要求采集不再将此数据送往webserver
  147. if (acce_state == STATE_ACCE_STATIC)
  148. {
  149. this->isoutput = false;
  150. }
  151. else
  152. {
  153. if (this->x == this->last_x && this->y == this->last_y)
  154. {
  155. this->isoutput = false;
  156. }else{
  157. //如果运动状态时,
  158. int nSign = 0;
  159. int nState = 0;
  160. if (this->x == this->last_x)
  161. {
  162. if (this->y >= this->last_y)
  163. {
  164. nSign = 1;
  165. }else{
  166. nSign = -1;
  167. }
  168. }else{
  169. if (this->x > this->last_x)
  170. {
  171. nSign = 1;
  172. }else{
  173. nSign = -1;
  174. }
  175. }
  176. if (nSign == this->direction)
  177. {
  178. this->isoutput = true;
  179. }
  180. else
  181. {
  182. double distance = sqrt(pow(this->x - this->last_x,2) + pow(this->y - this->last_y,2));
  183. if ( distance > this->map_scale)
  184. {
  185. //两点之间的距离是否大于一个像素的距离,满足条件,输出json
  186. this->isoutput = true;
  187. //否则只输出到日志
  188. }else{
  189. this->isoutput = false;
  190. }
  191. /*char str_output[100] = {0};
  192. sprintf_s(str_output,"x: %f , y: %f , lx: %f, ly: %f ,nSign: %d, lstd: %d, scale: %f ,distance: %f \r\n",this->x,this->y,this->last_x,this->last_y,nSign,this->direction,this->map_scale,distance);
  193. str = _T("");
  194. str = str_output;
  195. TRACE(str);*/
  196. }
  197. this->direction = nSign;
  198. }
  199. }
  200. }
  201. void Card::algo_tof(int cnt)
  202. {
  203. int i = 0, nCount = 0;
  204. POS *p = NULL;
  205. POS tmpPos;
  206. tmpPos.posx = 0;
  207. tmpPos.posy = 0;
  208. tmpPos.pos_radius = 0;
  209. int difT = 1;
  210. bool bfound = false;
  211. DistMap::iterator it = _dists.front().distmap.begin();
  212. if(cnt > 2){
  213. for(; it != _dists.front().distmap.end(); ++it){
  214. bfound = false;
  215. // 判断是否已经加入到列表
  216. for(int j=0; j < i; j++){
  217. if(p_dists_locate[j]->reader_id == it->second->reader_id){
  218. bfound = true;
  219. break;
  220. }
  221. }
  222. if(i < DIST_COUNT && !bfound){
  223. p_dists_locate[i] = it->second;
  224. i++;
  225. }
  226. }
  227. nCount = i;
  228. }else{
  229. for(; it != _dists.front().distmap.end(); ++it,i++){
  230. p_dists_locate[i] = it->second;
  231. }
  232. nCount = cnt;
  233. }
  234. if(!init_postion){
  235. this->x = p_dists_locate[0]->x;
  236. this->y = p_dists_locate[0]->y;
  237. this->z = p_dists_locate[0]->z;
  238. this->a = p_dists_locate[0]->a;
  239. this->distance = p_dists_locate[0]->d;
  240. this->t = p_dists_locate[0]->t;
  241. this->last_x = p_dists_locate[0]->x;
  242. this->last_y = p_dists_locate[0]->y;
  243. this->last_z = p_dists_locate[0]->z;
  244. init_postion = true;
  245. }
  246. int sta_num = 0;
  247. int ant = 0;
  248. double dist = 0.0;
  249. INFO_PRE info_pre;
  250. info_pre.t = p_dists_locate[0]->t;
  251. info_pre.detaT = 0;
  252. info_pre.sta_num = p_dists_locate[0]->reader_id - 1;
  253. info_pre.ant = p_dists_locate[0]->antenna_id;
  254. info_pre.dist = p_dists_locate[0]->d;
  255. for(int i = 1;i < nCount;i++){
  256. sta_num = p_dists_locate[i]->reader_id - 1;
  257. ant = p_dists_locate[i]->antenna_id;
  258. dist = p_dists_locate[i]->d;
  259. p = LocateAlgorithm::Pos(*(this->pReaderPathMap),sta_num,ant,dist,info_pre);
  260. if(p == NULL)
  261. {
  262. continue;
  263. }
  264. if(p->pos_radius < 2.0){
  265. if(i == 1){
  266. tmpPos.posx = p->posx;
  267. tmpPos.posy = p->posy;
  268. tmpPos.pos_radius = p->pos_radius;
  269. }else{
  270. double posx = (tmpPos.posx + p->posx)/2;
  271. double posy = (tmpPos.posy + p->posy)/2;
  272. double radius = (tmpPos.pos_radius + p->pos_radius)/2;
  273. tmpPos.posx = posx;
  274. tmpPos.posy = posy;
  275. tmpPos.pos_radius = radius;
  276. }
  277. }
  278. if(p){
  279. delete p;
  280. p = NULL;
  281. }
  282. }
  283. if(tmpPos.posx != 0 && tmpPos.posy != 0){
  284. int difT = info_pre.t - this->t;
  285. double newPosX = tmpPos.posx / (this->map_scale*1.0);
  286. double newPosY = tmpPos.posy / (this->map_scale*1.0);
  287. double d = sqrt(pow(newPosX - this->last_locate.x,2)+pow(newPosY - this->y,2))*this->map_scale;
  288. if(difT==0){
  289. this->last_locate.v = 0;
  290. }else{
  291. this->last_locate.v = d/difT;
  292. }
  293. this->last_locate.x = tmpPos.posx / (this->map_scale*1.0);
  294. this->last_locate.y = tmpPos.posy / (this->map_scale*1.0);
  295. this->last_locate.t = info_pre.t;
  296. }
  297. this->x = this->last_locate.x;
  298. this->y = this->last_locate.y;
  299. this->v = ((int)(this->last_locate.v*1000))/1000.0;
  300. this->z = 0;
  301. this->a = 0;
  302. }
  303. void Card::algo_tdoa(int cnt)
  304. {
  305. if(cnt < 2){
  306. return;
  307. }
  308. //主要处理当相同卡的时间戳的数据中存在同步序号大于5的情况,如果有大于5的数据则丢弃此数据
  309. DistMap::iterator it = _dists.front().distmap.begin();
  310. int k = 0;
  311. int dst = 0;
  312. int st = 0;
  313. bool bRet = false;
  314. for(; it != _dists.front().distmap.end(); ++it,k++){
  315. if(k==0){
  316. st = it->second->st;
  317. }else{
  318. dst = abs(st - it->second->st);
  319. if(dst >= 5){
  320. bRet = true;
  321. break;
  322. }
  323. }
  324. p_dists_locate[k] = it->second;
  325. }
  326. if(bRet){
  327. this->x = this->last_locate.x;
  328. this->y = this->last_locate.y;
  329. this->z = INVALID_COORDINATE;
  330. return;
  331. }
  332. //HeapSort(p_dists_locate,k);
  333. SelectSort(p_dists_locate,k);
  334. ReceiveDataMap pRdm;
  335. pRdm.clear();
  336. bRet = false;
  337. int maxSyncTimes = 0;
  338. //保存加速度当前状态和上次状态
  339. int acce_state = 0;
  340. int acce_state_last = 0;
  341. int ins_direction = 0;
  342. int card_time_stamp = 0;
  343. for(int i=0;i<k;i++){
  344. if(i==0){
  345. card_time_stamp = p_dists_locate[i]->t;
  346. maxSyncTimes = p_dists_locate[i]->st;
  347. acce_state = p_dists_locate[i]->acce_state;
  348. acce_state_last = p_dists_locate[i]->acce_state_last;
  349. ins_direction = p_dists_locate[i]->ins_direction;
  350. }
  351. else{
  352. if(maxSyncTimes < p_dists_locate[i]->st){
  353. maxSyncTimes = p_dists_locate[i]->st;
  354. acce_state = p_dists_locate[i]->acce_state;
  355. acce_state_last = p_dists_locate[i]->acce_state_last;
  356. ins_direction = p_dists_locate[i]->ins_direction;
  357. }
  358. }
  359. ReceiveDataMap::iterator prdm_it = pRdm.find(p_dists_locate[i]->reader_id);
  360. if(prdm_it == pRdm.end()){
  361. if(p_dists_locate[i]->tt == LLONG_MAX ){
  362. //如果同步时间戳存在异常值,则不走算法定位,直接返回上一次结果值
  363. bRet = true;
  364. break;
  365. }
  366. //保存信息用于定位
  367. ReceiveData prd;
  368. prd.reader_id = p_dists_locate[i]->reader_id;
  369. prd.antenna_id = p_dists_locate[i]->antenna_id;
  370. prd.rec_time_stamp = p_dists_locate[i]->tt;
  371. prd.x = p_dists_locate[i]->x*this->map_scale;
  372. prd.y = p_dists_locate[i]->y*this->map_scale;
  373. prd.z = p_dists_locate[i]->z*this->map_scale;
  374. prd.special = p_dists_locate[i]->special;
  375. pRdm[prd.reader_id] = prd;
  376. }
  377. }
  378. //存在异常,直接返回上一次结果值
  379. if(bRet){
  380. pRdm.clear();
  381. this->x = this->last_locate.x;
  382. this->y = this->last_locate.y;
  383. this->z = INVALID_COORDINATE;
  384. return;
  385. }
  386. if (this->time_stamp_last > this->time_stamp_cal)
  387. {
  388. this->x = this->last_locate.x;
  389. this->y = this->last_locate.y;
  390. this->z = INVALID_COORDINATE;
  391. return;
  392. }
  393. this->time_stamp_last = this->time_stamp_cal;
  394. int i = 0, nCount = 0;
  395. POS *p = NULL;
  396. bool bOutput = false;
  397. this->z = 0;
  398. if(pTdoaReaderPathMap->size() > 0 && pRdm.size() > 1){
  399. this->m_nCalcSyncNum = maxSyncTimes;
  400. p = LocateAlgorithm::Pos(&pRdm,*pTdoaReaderPathMap);
  401. this->origin_locate.x = p->posx / (this->map_scale*1.0);
  402. this->origin_locate.y = p->posy / (this->map_scale*1.0);
  403. //TRACE(_T("%d,%f,%f"),this->time_stamp_cal,p->posx/this->map_scale,p->posy/this->map_scale);
  404. bool bRet = false;
  405. bRet = LocateAlgorithm::CheckPosInValid(p,&pRdm,this->map_scale);
  406. if(bRet){
  407. p->posx = INVALID_COORDINATE ;
  408. p->posy = INVALID_COORDINATE ;
  409. p->posz = -2 ;
  410. }
  411. double interval_time = this->p_reader->reader_interval_time;
  412. double deltaT = 0.0;
  413. double cvx = 0;
  414. double cvy = 0;
  415. double cv = 0;
  416. double cx = 0;
  417. double cy = 0;
  418. double cz = 0;
  419. int nSign = 1;
  420. sync_data sdNew;
  421. sync_data sd;
  422. cx = p->posx / (this->map_scale*1.0);
  423. cy = p->posy / (this->map_scale*1.0);
  424. cz = p->posz / (this->map_scale*1.0);
  425. #ifdef ALGORITHM_TYPE_INS
  426. int acce_direction = 0; //加速度计速度方向 0-静止,1-前进,-1-后退
  427. int uwb_direction = 0; //uwb速度方向,同上
  428. bool bUseKalman = false;
  429. bool bOriginLocate = false; //原始定位是否成功
  430. bool bDirectReturn = false; //是否直接返回本次定位结果
  431. #endif
  432. if(p->posx != INVALID_COORDINATE && p->posy != INVALID_COORDINATE){
  433. #ifdef ALGORITHM_TYPE_INS
  434. bOriginLocate = true; //原始定位成功
  435. #endif
  436. //定位成功
  437. if(this->m_nLastLocateT == 0){
  438. sdNew.sync_num = maxSyncTimes;
  439. sdNew.x = cx;
  440. sdNew.y = cy;
  441. sdNew.vx = 0;
  442. sdNew.vy = 0;
  443. sdNew.update = false;
  444. m_syncNumList.push_back(sdNew);
  445. #ifdef ALGORITHM_TYPE_INS
  446. uwb_direction = 1;
  447. #endif
  448. }
  449. else{
  450. //现在的关于同步序号的处理是这样的:
  451. //如果定位成功,就把这次定位成功的同步数据:同步序号,坐标;x,y方向的速度,扔到一个队列里,
  452. //后来定位成功的就会先根据同步序号差用加速度抛一次;
  453. //抛不掉,就用队列里的同步数据(从后往前找),找到第一个与当前同步序号相差大于5的同步数据来进行第二次计算速度以及加速度,
  454. //如果加速度大于5,就不要此次的定位数据,
  455. //如果通过加速度判断就将队列中从头开始到此同步数据的所有元素都丢弃,并插入新的此次同步数据
  456. this->m_dFirstDiff = p->dFirstDiff;
  457. this->m_dSecondDiff = p->dSecondDiff;
  458. if (this->b_long_interval)
  459. {
  460. //此段代码用于将上一次定位是根据两个时间差是个很大值而定位出的结果
  461. //当后续定位时就和最近的定位结果进行比较
  462. //例如:当上一次同步序号是14321,它定位时比较的同步序号是14200,时间差大于20多秒
  463. //这时我们就将b_long_interval置为true
  464. //当本次定位,同步序号是14326,,这时就需要根据最近的14321进行判断
  465. list<sync_data>::reverse_iterator it = m_syncNumList.rbegin();
  466. sync_data sdl = *it;
  467. deltaT = (maxSyncTimes - sdl.sync_num)*interval_time;
  468. //避免同一个同步序号下存在多个不同卡序号
  469. if (deltaT < 1E-2)
  470. {
  471. deltaT = 0.2;
  472. }
  473. cvx = (cx - sd.x)*this->map_scale/deltaT;
  474. cvy = (cy - sd.y)*this->map_scale/deltaT;
  475. double avx = (cvx - sd.vx) / deltaT;
  476. double avy = (cvy - sd.vy) / deltaT;
  477. double av = sqrt(pow(avx,2) + pow(avy,2));
  478. //车卡的加速度
  479. switch(this->card_type){
  480. case CT_PERSON:
  481. if(av > PERSON_ACCELERATE_THRESHOLD){
  482. this->x = this->last_locate.x;
  483. this->y = this->last_locate.y;
  484. return;
  485. }
  486. break;
  487. case CT_VEHICLE:
  488. if(av > VECHILE_ACCELERATE_THRESHOLD){
  489. //保留上次结果
  490. this->x = this->last_locate.x;
  491. this->y = this->last_locate.y;
  492. this->b_long_interval = false;
  493. return;
  494. }
  495. break;
  496. }
  497. deltaT = 0;
  498. cvx = cvy = 0;
  499. }
  500. //从队列尾部开始查找,找到第一个同步序号与当前计算卡的同步序号相差5个以上的数据
  501. //list<sync_data>::reverse_iterator it = m_syncNumList.rbegin();
  502. list<sync_data>::reverse_iterator it;
  503. bool bOverflow = false;
  504. for(it = m_syncNumList.rbegin();it!=m_syncNumList.rend();it++){
  505. if(maxSyncTimes - it->sync_num >= 5){
  506. sd = *it;
  507. break;
  508. }
  509. else{
  510. if(maxSyncTimes - it->sync_num < 0 && maxSyncTimes < 100){
  511. //如果最新同步号小于列表中的同步号则
  512. if(maxSyncTimes + 65536 - it->sync_num >=5 ){
  513. bOverflow = true;
  514. sd = *it;
  515. }
  516. }else{
  517. continue;
  518. }
  519. }
  520. }
  521. //根据溢出条件来计算deltaT
  522. if(bOverflow){
  523. deltaT = (maxSyncTimes + 65536 - sd.sync_num)*interval_time;
  524. }else{
  525. deltaT = (maxSyncTimes - sd.sync_num)*interval_time;
  526. }
  527. //使用间隔来修正速度
  528. if(deltaT - 1.0 >= 0){
  529. if (deltaT > 10)
  530. {
  531. this->b_long_interval = true;
  532. }
  533. //转为m/s
  534. cvx = (cx - sd.x)*this->map_scale/deltaT;
  535. cvy = (cy - sd.y)*this->map_scale/deltaT;
  536. //速度正负的判断:以x轴,y轴正向运动为正
  537. //如果x相等,则y2 - y1 > 0为正
  538. //其他情况,则x2 - x1 > 0 为正
  539. if(cx == sd.x){
  540. if(cy > sd.y){
  541. nSign = 1;
  542. }else{
  543. nSign = -1;
  544. }
  545. }else{
  546. if(cx > sd.x){
  547. nSign = 1;
  548. }else{
  549. nSign = -1;
  550. }
  551. }
  552. cv = sqrt(pow(cvx,2) + pow(cvy,2));
  553. cv = cv*nSign;
  554. double avx = (cvx - sd.vx) / deltaT;
  555. double avy = (cvy - sd.vy) / deltaT;
  556. double av = sqrt(pow(avx,2) + pow(avy,2));
  557. //车卡的加速度
  558. switch(this->card_type){
  559. case CT_PERSON:
  560. if(av > PERSON_ACCELERATE_THRESHOLD){
  561. this->x = this->last_locate.x;
  562. this->y = this->last_locate.y;
  563. return;
  564. }
  565. break;
  566. case CT_VEHICLE:
  567. if(av > VECHILE_ACCELERATE_THRESHOLD){
  568. //保留上次结果
  569. this->x = this->last_locate.x;
  570. this->y = this->last_locate.y;
  571. return;
  572. }
  573. break;
  574. }
  575. this->last_locate.acceleration = av;
  576. this->last_vx = cvx;
  577. this->last_vy = cvy;
  578. cv = cv*3.6;
  579. //速度的限制
  580. if(fabs(cv) > MAX_VECHILE_SPEED){
  581. this->x = this->last_locate.x;
  582. this->y = this->last_locate.y;
  583. return;
  584. }
  585. //删除第一个元素到tmp(含)之间的所有元素
  586. bool bStartDel = false;
  587. for(list<sync_data>::reverse_iterator tmp = m_syncNumList.rbegin();tmp != m_syncNumList.rend();)
  588. {
  589. if(bStartDel){
  590. tmp = list<sync_data>::reverse_iterator(m_syncNumList.erase((++tmp).base()));
  591. }else{
  592. if(*tmp == sd){
  593. bStartDel = true;
  594. }
  595. ++tmp;
  596. }
  597. }
  598. //更新值为当前值并插入队列
  599. //sync_data sdNew;
  600. sdNew.sync_num = maxSyncTimes;
  601. this->m_nSyncNumInList = sd.sync_num;
  602. //sdNew.x = cx;
  603. //sdNew.y = cy;
  604. sdNew.vx = cvx;
  605. sdNew.vy = cvy;
  606. //m_syncNumList.push_back(sdNew);
  607. sdNew.update = true;
  608. #ifdef ALGORITHM_TYPE_INS
  609. uwb_direction = (nSign == 1)?1:-1; //uwb形式判断不出静止或者怠速
  610. #endif
  611. }else{
  612. //cv = this->last_locate.v;
  613. cv = this->origin_locate.v;
  614. #ifdef ALGORITHM_TYPE_INS
  615. uwb_direction = ins_direction;//不用this->accelerate_state_last此值是因为进入算法后这个值可能已经被更新了
  616. #endif
  617. }
  618. }
  619. }
  620. #ifdef ALGORITHM_TYPE_INS
  621. else{
  622. uwb_direction = ins_direction;
  623. }
  624. #endif
  625. #ifdef ALGORITHM_TYPE_INS
  626. bRet = false;
  627. //0,1,2分别表示静止(含怠速),运动,运动中刹车
  628. switch (acce_state)
  629. {
  630. case 0:
  631. acce_direction = 0;
  632. if(acce_state_last!=0){
  633. //说明从其他状态变为静止(怠速)状态
  634. //返回这次定位结果
  635. if (bOriginLocate)
  636. {
  637. bDirectReturn = true; //直接返回本次定位结果
  638. }
  639. }else{
  640. //返回上一次定位结果
  641. bDirectReturn = false;
  642. }
  643. bUseKalman = false;
  644. //bRet = true;
  645. break;
  646. case 1://需要判断状态是否发生了变化
  647. if (acce_state_last == ACCELERATE_INIT_STATE)
  648. {
  649. //第一次获得卡的状态,需要确定首次方向
  650. acce_direction = uwb_direction; //将UWB定出的速度方向赋值给它
  651. }else{
  652. acce_direction = acce_state_last;
  653. }
  654. //bRet = false;
  655. break;
  656. case 2:
  657. case 3:
  658. acce_direction = 0; //此处将速度方向设为静止,也就是0
  659. //bRet = false;
  660. break;
  661. }
  662. if (acce_state!=acce_last_state)
  663. {
  664. // 如果状态发生了改变,则加速度计的权重需要重置
  665. this->ins_weight = INS_WEIGHT;
  666. }
  667. double cweight = 0;
  668. if(!bRet){
  669. cweight = this->ins_weight * acce_direction + this->uwb_weight*uwb_direction;
  670. //如果计算出的权重在合适范围内,就逐渐降低惯导的权限
  671. //如果计算出的权重超过范围,则重置惯导的权重为90%
  672. if(cweight>=INS_WEIGHT*-1&&cweight<=INS_WEIGHT){
  673. cweight = abs(cweight);
  674. }else{
  675. cweight = INS_WEIGHT;
  676. }
  677. if(cweight*uwb_direction < 0){
  678. //惯导和uwb定位方向不一致,定位失败
  679. //增加一次判断,与上一次的进行比对,如果和上一次的一致,则送进卡尔曼滤波
  680. if(cweight*acce_state_last > 0){
  681. bUseKalman = true;
  682. }else{
  683. bUseKalman = false;
  684. }
  685. }else{
  686. //惯导和uwb定位方向一致,定位成功
  687. //送进卡尔曼滤波
  688. bUseKalman = true;
  689. }
  690. }
  691. if (abs(cweight) > 0)
  692. {
  693. ins_direction = cweight/abs(cweight) > 0?1:-1;
  694. }else{
  695. //如果加速度计的状态权重为零
  696. ins_direction = uwb_direction;
  697. }
  698. this->ins_weight = cweight;
  699. this->ins_direction = ins_direction;
  700. this->acce_last_state = acce_last_state;
  701. this->accelerate_state_last = this->acce_cur_state = acce_state;
  702. #endif
  703. this->accelerate_state_last = this->acce_cur_state = acce_state;
  704. if(m_nFilterType == FILTER_KALMAN){
  705. #ifdef ALGORITHM_TYPE_INS
  706. if(bUseKalman){
  707. #endif
  708. double kalman_detal_t = (maxSyncTimes - this->last_locate.st)*interval_time;
  709. //通过卡尔曼滤波处理
  710. if(p->posx == INVALID_COORDINATE && p->posy == INVALID_COORDINATE){
  711. this->m_pKalmanFilter->m_bFlag = false;
  712. if(this->m_pKalmanFilter->m_nCounts < 3 || this->m_pKalmanFilter->m_pCar->P(0,0) > 2 || kalman_detal_t > 3){
  713. //P(0,0):连续时间(大于2s)都定位失败
  714. //deltaT>3:距离上次成功定位时间间隔为3s
  715. this->x = this->last_locate.x;
  716. this->y = this->last_locate.y;
  717. this->z = -3;
  718. return;
  719. }
  720. if(this->m_pKalmanFilter->m_nCounts >= 3){
  721. //只有三次以上才允许使用kalman滤波以下的函数
  722. //this->m_pKalmanFilter->Predict(deltaT);
  723. this->m_pKalmanFilter->Predict(kalman_detal_t);
  724. //this->x = this->m_pKalmanFilter->m_pCar->x(0,0) / this->map_scale;
  725. //this->y = this->m_pKalmanFilter->m_pCar->x(2,0) / this->map_scale;
  726. this->z = -4;
  727. sdNew.update = true;
  728. }
  729. }else{
  730. this->m_pKalmanFilter->m_bFlag = true;
  731. this->m_pKalmanFilter->m_nCounts++;
  732. this->m_pKalmanFilter->m_pCar->z(0,0) = cx * this->map_scale;
  733. this->m_pKalmanFilter->m_pCar->z(1,0) = cvx;
  734. this->m_pKalmanFilter->m_pCar->z(2,0) = cy * this->map_scale;
  735. this->m_pKalmanFilter->m_pCar->z(3,0) = cvy;
  736. if(this->m_pKalmanFilter->m_nCounts == 1){
  737. //第一次直接赋值
  738. this->m_pKalmanFilter->m_pCar->x = this->m_pKalmanFilter->m_pCar->z;
  739. }
  740. if(this->m_pKalmanFilter->m_nCounts == 2){
  741. //两次处理
  742. 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;
  743. 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;
  744. this->m_pKalmanFilter->m_pCar->x = this->m_pKalmanFilter->m_pCar->z;
  745. }
  746. if(this->m_pKalmanFilter->m_nCounts >= 3){
  747. //只有三次以上才允许使用kalman滤波以下的函数
  748. //this->m_pKalmanFilter->Predict_Correct(deltaT);
  749. this->m_pKalmanFilter->Predict_Correct(kalman_detal_t);
  750. sdNew.update = true;
  751. if(deltaT!=0){
  752. /*this->m_pKalmanFilter->m_pCar->x(1,0) = (this->m_pKalmanFilter->m_pCar->x(0,0) - this->last_locate.x)/deltaT;
  753. this->m_pKalmanFilter->m_pCar->x(3,0) = (this->m_pKalmanFilter->m_pCar->x(2,0) - this->last_locate.y)/deltaT;*/
  754. this->m_pKalmanFilter->m_pCar->x(1,0) = (this->m_pKalmanFilter->m_pCar->x(0,0) - sd.x*this->map_scale)/deltaT;
  755. this->m_pKalmanFilter->m_pCar->x(3,0) = (this->m_pKalmanFilter->m_pCar->x(2,0) - sd.y*this->map_scale)/deltaT;
  756. }
  757. }
  758. }
  759. if(p->nFirstReader == -1 && p->nSecondReader == -1){
  760. p->nFirstReader = last_s_locate_reader[0];
  761. p->nSecondReader = last_s_locate_reader[1];
  762. }
  763. //增加地图集的判定,判断定位结果是否在地图集上
  764. //如果不在地图集上,需要再次定位
  765. //需要带出定位结果的分站信息,
  766. //利用地图集中分站信息再次定位
  767. POS kalman_p;
  768. kalman_p.nFirstReader = p->nFirstReader;
  769. kalman_p.nSecondReader = p->nSecondReader;
  770. kalman_p.posx = this->m_pKalmanFilter->m_pCar->x(0,0);//* this->map_scale
  771. kalman_p.posy = this->m_pKalmanFilter->m_pCar->x(2,0);//* this->map_scale
  772. kalman_p.pos_radius = p->pos_radius;
  773. if(!LocateAlgorithm::IsOnMap(&kalman_p,*pTdoaReaderPathMap)){
  774. //再一次定位到地图集上
  775. POS* cp = LocateAlgorithm::Pos(&kalman_p,*pTdoaReaderPathMap);
  776. if(cp != NULL){
  777. this->m_pKalmanFilter->m_pCar->x(0,0) = cp->posx;
  778. this->m_pKalmanFilter->m_pCar->x(2,0) = cp->posy;
  779. }
  780. if(cp){
  781. delete cp;
  782. cp = NULL;
  783. }
  784. }
  785. this->last_locate.x = this->x = this->m_pKalmanFilter->m_pCar->x(0,0) / this->map_scale;
  786. this->last_locate.y = this->y = this->m_pKalmanFilter->m_pCar->x(2,0) / this->map_scale;
  787. this->last_locate.z = this->z;
  788. this->last_locate.st = maxSyncTimes;
  789. last_s_locate_reader[0] = p->nFirstReader;
  790. last_s_locate_reader[1] = p->nSecondReader;
  791. this->m_pKalmanFilter->m_pCar->t = this->m_nLastLocateT = maxSyncTimes;
  792. this->v = sqrt(pow(this->m_pKalmanFilter->m_pCar->x(1,0),2) + pow(this->m_pKalmanFilter->m_pCar->x(3,0),2)); //*this->map_scale
  793. this->v*=3.6; //转为km/h
  794. cvx = this->m_pKalmanFilter->m_pCar->x(1,0);
  795. cvy = this->m_pKalmanFilter->m_pCar->x(3,0);
  796. nSign = 1;
  797. if(this->m_pKalmanFilter->m_pCar->x(1,0) == 0){
  798. if(this->m_pKalmanFilter->m_pCar->x(3,0)>0){
  799. nSign = 1;
  800. }
  801. }else{
  802. if(this->m_pKalmanFilter->m_pCar->x(1,0) > 0){
  803. nSign = 1;
  804. }else{
  805. nSign = -1;
  806. }
  807. }
  808. this->v*=nSign;
  809. this->last_locate.v = this->v;
  810. this->origin_locate.v = cv;
  811. if(sdNew.update){
  812. sdNew.sync_num = maxSyncTimes;
  813. sdNew.x = this->x;
  814. sdNew.y = this->y;
  815. sdNew.vx = cvx;
  816. sdNew.vy = cvy;
  817. m_syncNumList.push_back(sdNew);
  818. }
  819. #ifdef ALGORITHM_TYPE_INS
  820. }else{
  821. //这组数据的处理方法是:
  822. //如果第一次成功定位,但状态是静止,就取第一次成功定位值(这是为了处理当第一次成功定位,但状态是静止的,此时取上一次定位值为零的问题),
  823. //后续如果不管定位成功还是失败,只要状态是静止的,就输出上一次成功定位值,并更新同步序号。
  824. //取上次结果
  825. if (bDirectReturn)
  826. {
  827. if(this->map_scale > 0){
  828. this->last_locate.x = this->x = p->posx / (this->map_scale*1.0);
  829. this->last_locate.y = this->y = p->posy / (this->map_scale*1.0);
  830. this->last_locate.z = this->z = p->posz / (this->map_scale*1.0);
  831. this->last_locate.v = this->v = cv;
  832. }
  833. }else{
  834. this->x = this->last_locate.x;
  835. this->y = this->last_locate.y;
  836. this->z = -5;//this->last_locate.z
  837. this->v = this->last_locate.v;
  838. }
  839. /*if(this->last_locate.x == 0 && this->last_locate.y == 0){
  840. }else{
  841. }
  842. */
  843. this->m_nLastLocateT = this->m_nCalcSyncNum = this->last_locate.st = this->sync_num = maxSyncTimes;
  844. if(sdNew.update){
  845. sdNew.sync_num = maxSyncTimes;
  846. sdNew.x = this->x;
  847. sdNew.y = this->y;
  848. sdNew.vx = cvx;
  849. sdNew.vy = cvy;
  850. m_syncNumList.push_back(sdNew);
  851. this->m_nSyncNumInList = maxSyncTimes;
  852. }
  853. this->is_deal_by_algo = true;
  854. }
  855. #endif
  856. }else{
  857. //TRACE(_T("no kalman . \n"));
  858. //最新通过算法算出的结果
  859. if(p->posx == INVALID_COORDINATE || p->posy == INVALID_COORDINATE){
  860. this->x = this->last_locate.x;
  861. this->y = this->last_locate.y;
  862. this->z = this->last_locate.z;
  863. }else{
  864. if(this->map_scale > 0){
  865. this->x = p->posx / (this->map_scale*1.0);
  866. this->y = p->posy / (this->map_scale*1.0);
  867. this->z = p->posz / (this->map_scale*1.0);
  868. }
  869. this->v = cv;
  870. this->last_locate.x = this->x;
  871. this->last_locate.y = this->y;
  872. this->last_locate.z = this->z;
  873. this->last_locate.v = this->v;
  874. this->m_nLastLocateT = this->last_locate.st = maxSyncTimes;
  875. }
  876. this->a = 0;
  877. }
  878. }
  879. if(p){
  880. delete p;
  881. p = NULL;
  882. }
  883. pRdm.clear();
  884. }
  885. bool Card::is_pos_state_changed( int nval ) // 考勤
  886. {
  887. if(this->pos_state == this->pos_state_old){
  888. if(this->pos_state_count < nval){
  889. this->pos_state_count++;
  890. }
  891. return false;
  892. }
  893. //if(this->pos_state_count < nval ){ // 未达到确认次数
  894. // this->pos_state_count++;
  895. // return false;
  896. //}
  897. this->pos_state_old = this->pos_state;
  898. this->pos_state_count = 1;
  899. return true;
  900. //if(last_area_type_id != cur_area_type_id){
  901. // if(0 ==last_area_type_id || 0==cur_area_type_id){
  902. // return true;
  903. // }
  904. //}
  905. //return false;
  906. //return (this->last_area_type_id != this->cur_area_type_id && 0 == (this->cur_area_type_id & this->last_area_type_id));
  907. }
  908. void Card::add_dist(_coordinate* dist)
  909. {
  910. EnterCriticalSection(&m_csCard);
  911. string s = concat(dist->reader_id, dist->antenna_id);
  912. int idx = FindDistMap(dist->t);
  913. if(-1 == idx){
  914. DistQueMapItem dq;
  915. dq.cardstamp = dist->t;
  916. dq.distmap[s] = dist;
  917. _dists.push_back(dq);
  918. }else{
  919. _dists[idx].distmap[s] = dist;
  920. }
  921. if(_dists.size() >= MAX_DIST_CACHE){ // 超过缓存数量限制
  922. // 计算并删除第一个
  923. get_coordinate();
  924. remove_dist_head();
  925. }
  926. if (this->x != this->last_x || this->y != this->last_y)
  927. {
  928. this->b_pos_change = true;
  929. }else{
  930. this->b_pos_change = false;
  931. }
  932. LeaveCriticalSection(&m_csCard);
  933. }
  934. time_t Card::get_working_time()
  935. {
  936. //return (card_type == CT_VEHICLE) ? leave_park_time : down_time;
  937. return down_time;
  938. }
  939. int Card::get_effictive_dist_count( int offset /*= 0*/ )
  940. {
  941. if(0 == _dists.size()) return 0;
  942. time_stamp_cal = _dists.front().cardstamp;
  943. return (_dists.front().distmap.size());
  944. //int nTimeStamp;
  945. //if(time_stamp_max == 0 && offset != 0){
  946. // nTimeStamp = 0xFFFF;
  947. //}else{
  948. // nTimeStamp = time_stamp_max - offset;
  949. //}
  950. //int ret = 0, tmp = -1;
  951. //for(int i = 0; i < DIST_COUNT; i++){
  952. // if(NULL == p_dists[i]) break;
  953. // if(p_dists[i]->t == nTimeStamp){
  954. // ret++;
  955. // }
  956. //}
  957. //return ret;
  958. }
  959. Card::~Card(void)
  960. {
  961. if(m_pKalmanFilter){
  962. delete m_pKalmanFilter;
  963. m_pKalmanFilter = NULL;
  964. }
  965. for(int i = DIST_COUNT - 1;i >= 0;i--){
  966. if(p_dists[i] != NULL){
  967. delete p_dists[i];
  968. p_dists[i] = NULL;
  969. }
  970. }
  971. if(p_dists){
  972. delete[] p_dists;
  973. p_dists = NULL;
  974. }
  975. if(init_postion){
  976. for(int i = DIST_COUNT - 1;i >= 0;i--){
  977. if(p_dists_locate[i] != NULL){
  978. delete p_dists_locate[i];
  979. p_dists_locate[i] = NULL;
  980. }
  981. }
  982. if(p_dists_locate){
  983. delete[] p_dists_locate;
  984. p_dists_locate = NULL;
  985. }
  986. }
  987. //_dists.swap(DistQueMap());
  988. _dists.clear();
  989. DeleteCriticalSection(&m_csCard);
  990. }
  991. void Card::get_coordinate_2d( int cnt )
  992. {
  993. //double d_dist[3];
  994. //Point2 pt2, pt2_1;
  995. //for(int i = 0; i < cnt; i++){
  996. // d_dist[i] = p_dists_locate[i]->d;
  997. //}
  998. //for(int i=0; i< cnt; i++){
  999. // if(p_dists[i]->reader_id == 1){
  1000. // p2_anchors[0].x = p_dists[i]->x;
  1001. // p2_anchors[0].y = p_dists[i]->y;
  1002. // d_dist[0] = p_dists[i]->d;
  1003. // }else if(p_dists[i]->reader_id == 2){
  1004. // p2_anchors[1].x = p_dists[i]->x;
  1005. // p2_anchors[1].y = p_dists[i]->y;
  1006. // d_dist[1] = p_dists[i]->d;
  1007. // }if(p_dists[i]->reader_id == 3){
  1008. // p2_anchors[2].x = p_dists[i]->x;
  1009. // p2_anchors[2].y = p_dists[i]->y;
  1010. // d_dist[2] = p_dists[i]->d;
  1011. // }
  1012. //}
  1013. //Mat mat(3, 1, d_dist);
  1014. //if(is_anchor_changed){
  1015. // cal_location->setAnchors(p2_anchors);
  1016. // pt2 = cal_location->initPos(mat);
  1017. //} else{
  1018. // pt2 = cal_location->estiPos(mat);
  1019. //}
  1020. //pt2_1 = cal_location->getError();
  1021. //x = pt2.x;
  1022. //y = pt2.y;
  1023. //z = 0;
  1024. //x1 = pt2_1.x;
  1025. //y1 = pt2_1.y;
  1026. //z1 = 0;
  1027. }
  1028. double Card::get_speed()
  1029. {
  1030. double speed = 0;
  1031. // 计算速度
  1032. return speed;
  1033. }
  1034. /*
  1035. * 采用TOF或者TDOA算法进行定位计算
  1036. *
  1037. * param
  1038. * cnt ------ _dists数据条数
  1039. *
  1040. * return
  1041. * 无返回值
  1042. *
  1043. */
  1044. void Card::get_coordinate( int cnt )
  1045. {
  1046. #ifdef ALGORITHM_TOF
  1047. algo_tof(cnt);
  1048. #elif defined ALGORITHM_TYPE_TDOA
  1049. algo_tdoa(cnt);
  1050. #else
  1051. #endif
  1052. inspect_coordinate(this->acce_cur_state);
  1053. if(_isnan(this->x) || _isnan(this->y) || _isnan(this->z)){
  1054. this->x = this->last_x;
  1055. this->y = this->last_y;
  1056. this->z = this->last_z;
  1057. }
  1058. }
  1059. void Card::get_coordinate()
  1060. {
  1061. get_coordinate(get_effictive_dist_count());
  1062. }
  1063. std::string Card::get_state_text()
  1064. {
  1065. string ret = "";
  1066. state = 0;
  1067. state_biz = 0;
  1068. if(status_help == STATUS_ERROR){
  1069. state += STATUS_HELP;
  1070. state_biz += STATUS_HELP;
  1071. ret += "呼救,";
  1072. }
  1073. if(status_area_over_time == STATUS_ERROR){
  1074. state += STATUS_AREA_OVER_TIME;
  1075. state_biz += STATUS_AREA_OVER_TIME;
  1076. ret += "区域超时,";
  1077. }else if(status_area_over_time == STATUS_ERROR){
  1078. state += STATUS_OVER_TIME;
  1079. state_biz += STATUS_OVER_TIME;
  1080. ret += "超时,";
  1081. }
  1082. if(status_area_over_speed == STATUS_ERROR){
  1083. state += STATUS_AREA_OVER_SPEED;
  1084. state_biz += STATUS_AREA_OVER_SPEED;
  1085. ret += "区域超速,";
  1086. }else if(status_over_speed == STATUS_ERROR){
  1087. state += STATUS_OVER_SPEED;
  1088. state_biz += STATUS_OVER_SPEED;
  1089. ret += "超速,";
  1090. }
  1091. if(status_area_forbidden == STATUS_ERROR){
  1092. state += STATUS_AREA_FORBIDDEN;
  1093. state_biz += STATUS_AREA_FORBIDDEN;
  1094. ret += "进入限制区域,";
  1095. }
  1096. if(status_call == STATUS_ERROR){
  1097. state += STATUS_CALL;
  1098. state_biz += STATUS_CALL;
  1099. ret += "呼叫,";
  1100. }
  1101. if(status_lost == STATUS_ERROR){
  1102. state += STATUS_LOST;
  1103. state_biz += STATUS_LOST;
  1104. ret += "进入盲区,";
  1105. }
  1106. if(power_state == STATUS_ERROR){
  1107. state += STATUS_POWER_LOWER;
  1108. ret += "电量低,";
  1109. }else if(power_state == STATUS_ERROR_SERIOUS){
  1110. state += STATUS_POWER_LOWER_SERIOUS;
  1111. ret += "电量极低,";
  1112. }
  1113. if(ret.length() > 0){
  1114. ret = ret.substr(0, ret.length() - 1);
  1115. }else{
  1116. ret = "正常";
  1117. }
  1118. return ret;
  1119. }
  1120. std::string Card::get_acc_text()
  1121. {
  1122. string ret = "";
  1123. state_moving = (accelerate_state && 0x01)? STATUS_ERROR : STATUS_NORMAL;
  1124. if(state_moving == STATUS_NORMAL){
  1125. ret += "静止";
  1126. }else if(state_moving == STATUS_ERROR){
  1127. ret += "运动";
  1128. }
  1129. return ret;
  1130. }
  1131. std::string Card::concat( int reader_id, int ant_id )
  1132. {
  1133. char s[10];
  1134. sprintf_s(s, "%d-%d", reader_id, ant_id);
  1135. return s;
  1136. }
  1137. /*
  1138. * 滤波功能设置
  1139. *
  1140. * param
  1141. * nType ------ 滤波类型
  1142. *
  1143. * return
  1144. * 无
  1145. */
  1146. void Card::EnableFilter(int nType)
  1147. {
  1148. //如果无滤波类型直接返回
  1149. if(nType == NO_FILTER){
  1150. return;
  1151. }
  1152. //开启滤波功能,设置滤波类型
  1153. m_bUseFilter = TRUE;
  1154. m_nFilterType = nType;
  1155. switch(nType){
  1156. case FILTER_KALMAN:
  1157. //分配卡尔曼滤波类型变量并初始化参数
  1158. if(m_pKalmanFilter == NULL){
  1159. m_pKalmanFilter = new CKalmanFilter();
  1160. m_pKalmanFilter->Initial(0.2);
  1161. m_pKalmanFilter->m_bFlag = false;
  1162. }
  1163. break;
  1164. default:
  1165. break;
  1166. }
  1167. }
  1168. void Card::remove_dist_head()
  1169. {
  1170. DistMap tmp = _dists.front().distmap;
  1171. if(tmp.size() > 0 ){
  1172. DistMap::iterator it_mp_dist = tmp.begin();
  1173. for(it_mp_dist;it_mp_dist != tmp.end();){
  1174. _coordinate* tmp_coord = (_coordinate*)(it_mp_dist->second);
  1175. tmp.erase(it_mp_dist++);
  1176. if(tmp_coord){
  1177. delete tmp_coord;
  1178. tmp_coord = NULL;
  1179. }
  1180. it_mp_dist = tmp.begin();
  1181. }
  1182. }
  1183. _dists.pop_front();
  1184. //_dists.erase(_dists.begin());
  1185. }
  1186. Reader::Reader(void)
  1187. {
  1188. rec_time = reader_time = lost_time = time(NULL);
  1189. map_scale = 1.0;
  1190. sync_level = 0xFF;
  1191. state = STATUS_DEVICE_NORMAL;
  1192. for(int i = 0;i < ANTENNA_COUNT;i++){
  1193. ant[i] = NULL;
  1194. }
  1195. for(int i = 0;i < ADHOC_COUNT;i++){
  1196. adhoc[i] = NULL;
  1197. }
  1198. bIsInitCoverage = false;
  1199. reader_interval_time = 0.0;
  1200. m_nIsSpecial = -1;
  1201. }
  1202. Reader::~Reader(void)
  1203. {
  1204. for(int i = 0;i < ANTENNA_COUNT;i++){
  1205. Antenna* tmp = NULL;
  1206. tmp = ant[i];
  1207. if(tmp){
  1208. delete tmp;
  1209. tmp = NULL;
  1210. ant[i] = NULL;
  1211. }
  1212. }
  1213. for(int i = 0; i < ADHOC_COUNT;i++){
  1214. Adhoc * tmp = NULL;
  1215. tmp = adhoc[i];
  1216. if(tmp){
  1217. delete tmp;
  1218. tmp = NULL;
  1219. adhoc[i] = NULL;
  1220. }
  1221. }
  1222. }
  1223. std::string Reader::get_state_text()
  1224. {
  1225. string ret = "";
  1226. if(state == STATUS_DEVICE_ERROR){
  1227. ret = "故障";
  1228. }else if(state == STATUS_DEVICE_NORMAL){
  1229. ret = "正常";
  1230. }
  1231. return ret;
  1232. }
  1233. Antenna::Antenna(void)
  1234. {
  1235. antenna_angle = 0;
  1236. antenna_id = 0;
  1237. antenna_x = 0;
  1238. antenna_y = 0;
  1239. antenna_z = 0;
  1240. }
  1241. Antenna::~Antenna(void)
  1242. {
  1243. }
  1244. Area::Area(void)
  1245. {
  1246. is_att = 1;
  1247. polygon_count = 0;
  1248. polygon = NULL;
  1249. map_id = area_id = area_type_id = 0 ;
  1250. area_name = area_type_name = path = "";
  1251. over_count_person = over_time_person = under_count_person = under_time_person = 0;
  1252. over_count_vehicle = over_time_vehicle = under_count_vehicle = under_time_vehicle = 0;
  1253. count_person = count_vehicle = count_card = 0;
  1254. is_area_over_time_person = is_area_over_time_vehicle = false;
  1255. count_area_over_time_person = count_area_over_time_vehicle = 0;
  1256. time_over_time_person = time_over_time_vehicle = time(NULL);
  1257. is_area_over_count_person = is_area_over_count_vehicle = false;
  1258. count_area_over_count_person = count_area_over_count_vehicle = 0;
  1259. time_over_count_person = time_over_count_vehicle = time(NULL);
  1260. is_area_forbidden_person = is_area_forbidden_vehicle = 0;
  1261. count_area_forbidden_person = count_area_forbidden_vehicle = 0;
  1262. time_forbidden_person = time_forbidden_vehicle = time(NULL);
  1263. }
  1264. Area::~Area(void)
  1265. {
  1266. if(polygon){
  1267. delete[] polygon;
  1268. polygon = NULL;
  1269. }
  1270. }
  1271. void Area::init_border(string sz_path)
  1272. {
  1273. //std::vector<std::string> vec = split(sz_path, ",");
  1274. //if(vec.size() >= 4){
  1275. // rect_left = atoi(vec[0].c_str());
  1276. // rect_top = atoi(vec[1].c_str());
  1277. // rect_right = atoi(vec[2].c_str());
  1278. // rect_bottom = atoi(vec[3].c_str());
  1279. //}else{
  1280. // rect_left = rect_right = rect_top = rect_bottom = 0;
  1281. //}
  1282. if(sz_path == ""){
  1283. return ;
  1284. }
  1285. std::vector<std::string> vec = split(sz_path, " ");
  1286. std::vector<std::string>::iterator it = vec.begin();
  1287. if(polygon){
  1288. delete[] polygon;
  1289. polygon = NULL;
  1290. }
  1291. polygon = new _point[vec.size()];
  1292. polygon_count = 0;
  1293. for(; it != vec.end(); ++it){
  1294. std::vector<std::string> subvec = split(it->c_str(), ",");
  1295. _point p;
  1296. p.x = get_vertex(subvec[0]);
  1297. p.y = get_vertex(subvec[1]);
  1298. p.z = 0;
  1299. polygon[polygon_count] = p;
  1300. polygon_count++;
  1301. }
  1302. }
  1303. std::vector<std::string> Area::split( std::string str,std::string pattern )
  1304. {
  1305. std::string::size_type pos;
  1306. std::vector<std::string> result;
  1307. str+=pattern;//扩展字符串以方便操作
  1308. unsigned int size=str.size();
  1309. for(unsigned int i=0; i<size; i++){
  1310. pos=str.find(pattern,i);
  1311. if(pos<size){
  1312. std::string s=str.substr(i,pos-i);
  1313. result.push_back(s);
  1314. i=pos+pattern.size()-1;
  1315. }
  1316. }
  1317. return result;
  1318. }
  1319. double Area::get_vertex( std::string src)
  1320. {
  1321. std::string dest = "";
  1322. for(unsigned int i = 0; i < src.length(); i++){
  1323. if((src[i] >= '0' && src[i]<='9') || src[i]=='-' || src[i] == '.'){
  1324. dest += src[i];
  1325. }
  1326. }
  1327. return atof(dest.c_str());
  1328. }
  1329. bool Area::is_in_polygon( _point p )
  1330. {
  1331. if(polygon == NULL){
  1332. return false;
  1333. }
  1334. int counter = 0;
  1335. int i;
  1336. double xinters;
  1337. _point p1,p2;
  1338. p1 = polygon[0];
  1339. for (int i=1;i<= polygon_count;i++) {
  1340. p2 = polygon[i % polygon_count];
  1341. if (p.y > MIN(p1.y,p2.y)) {
  1342. if (p.y <= MAX(p1.y,p2.y)) {
  1343. if (p.x <= MAX(p1.x,p2.x)) {
  1344. if (p1.y != p2.y) {
  1345. xinters = (p.y-p1.y)*(p2.x-p1.x)/(p2.y-p1.y)+p1.x;
  1346. if (p1.x == p2.x || p.x <= xinters)
  1347. counter++;
  1348. }
  1349. }
  1350. }
  1351. }
  1352. p1 = p2;
  1353. }
  1354. //TRACE(_T("counter : %d \n"),counter);
  1355. return (counter % 2 == 0) ? false : true;
  1356. }
  1357. int Card::FindDistMap( int cardstamp )
  1358. {
  1359. int idx = -1;
  1360. for(int i = _dists.size() - 1; i >= 0; i--){
  1361. if(_dists[i].cardstamp == cardstamp ){
  1362. return i;
  1363. }
  1364. }
  1365. return idx;
  1366. }
  1367. bool Card::is_pos_state_pack_changed( int nval )
  1368. {
  1369. if(this->pos_state_park == this->pos_state_park_old){
  1370. if(this->pos_state_park_count < nval){
  1371. this->pos_state_park_count++;
  1372. }
  1373. return false;
  1374. }
  1375. //if(this->pos_state_park_count < nval ){ // 未达到确认次数
  1376. // this->pos_state_park_count++;
  1377. // return false;
  1378. //}
  1379. this->pos_state_park_old = this->pos_state_park;
  1380. this->pos_state_park_count = 1;
  1381. return true;
  1382. }
  1383. MapInfo::MapInfo( void )
  1384. {
  1385. }
  1386. MapInfo::~MapInfo( void )
  1387. {
  1388. }
  1389. Dept::Dept( int id, string name )
  1390. {
  1391. dept_id = id;
  1392. dept_name = name;
  1393. }
  1394. Dept::Dept()
  1395. {
  1396. }
  1397. Dept::~Dept()
  1398. {
  1399. }
  1400. Adhoc::Adhoc()
  1401. {
  1402. adhoc_id = 0;
  1403. x = 0;
  1404. y = 0;
  1405. z = 0;
  1406. idx = 0;
  1407. }
  1408. Adhoc::~Adhoc()
  1409. {
  1410. }