car.h 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444
  1. #ifndef __CAR_HPP__
  2. #define __CAR_HPP__
  3. #include "pointSmooth/point.h"
  4. #include "zlist.h"
  5. #include "zstream.h"
  6. #include "message_file.h"
  7. #include "message.h"
  8. #include "ant.h"
  9. #include "loc_point.h"
  10. #include "line_fit.h"
  11. #include "line.h"
  12. #include "card_path.h"
  13. #include <stdio.h>
  14. #include <list>
  15. #include <algorithm>
  16. #include <deque>
  17. #include <map>
  18. #include <memory>
  19. #include "atomic_lock.h"
  20. #include "cacti/kernel/Mutex.h"
  21. #include "his_location.h"
  22. #include "traffic_info.h"
  23. #define _SMOOTH
  24. NAMESPACE_POINT_BEGIN(NAMESPACE_POINT)
  25. class card_interface
  26. {
  27. public:
  28. card_interface()
  29. :_isMotionless(false)
  30. ,_count(0)
  31. {
  32. }
  33. virtual int push_optimized_data(loc_point&lp) = 0;
  34. virtual int getCardType() = 0;
  35. virtual bool smoothFlag() = 0;
  36. virtual const std::string &cardId() = 0;
  37. virtual const uint64_t CtTime()=0;
  38. virtual const uint64_t get_last_recv_time()=0;
  39. virtual const int getX() = 0;
  40. virtual const int getY() = 0;
  41. virtual void do_alarm(const std::size_t ow)=0;
  42. virtual void PrintString(const std::string &&str) = 0;
  43. virtual bool getBigCarFlag() = 0;
  44. virtual double getv() = 0;
  45. virtual int getAreaId() = 0;
  46. virtual int getAcc() =0;
  47. inline int getCount()
  48. {
  49. return _count;
  50. }
  51. public:
  52. bool _isMotionless;
  53. int _count;
  54. std::vector<std::string> m_v;
  55. std::map<std::string,short> m_m;
  56. std::list<std::shared_ptr<TrafficLightGroup>> m_arrTraLightGroup;
  57. std::size_t size()
  58. {
  59. return m_arrTraLightGroup.size();
  60. }
  61. void motionlessFlag()
  62. {
  63. if (0 == getAcc())
  64. _count ++;
  65. else
  66. _count = 0;
  67. }
  68. void clear()
  69. {
  70. m_arrTraLightGroup.clear();
  71. }
  72. void pop_front()
  73. {
  74. m_arrTraLightGroup.pop_front();
  75. }
  76. void pop_back()
  77. {
  78. m_arrTraLightGroup.pop_back();
  79. }
  80. void push_back(std::shared_ptr<TrafficLightGroup> tlg)
  81. {
  82. //std::lock_guard<std::mutex> guard(m_mtx);
  83. m_arrTraLightGroup.push_back(tlg);
  84. }
  85. void push_front(std::shared_ptr<TrafficLightGroup> tlg)
  86. {
  87. //std::lock_guard<std::mutex> guard(m_mtx);
  88. m_arrTraLightGroup.push_front(tlg);
  89. }
  90. void put(std::shared_ptr<TrafficLightGroup> tlg)
  91. {
  92. if(size()>2)
  93. return;
  94. //std::lock_guard<std::mutex> guard(m_mtx);
  95. m_arrTraLightGroup.push_back(tlg);
  96. double x = getX();
  97. double y = getY();
  98. m_arrTraLightGroup.sort([&](std::shared_ptr<TrafficLightGroup> one ,std::shared_ptr<TrafficLightGroup> two){
  99. double d1 = one->dist(x,y);
  100. double d2 = two->dist(x,y);
  101. return d1 < d2;
  102. });
  103. }
  104. std::shared_ptr<TrafficLightGroup> front()
  105. {
  106. //std::lock_guard<std::mutex> guard(m_mtx);
  107. return m_arrTraLightGroup.front();
  108. }
  109. std::shared_ptr<TrafficLightGroup> back()
  110. {
  111. //std::lock_guard<std::mutex> guard(m_mtx);
  112. return m_arrTraLightGroup.back();
  113. }
  114. bool find(std::shared_ptr<TrafficLightGroup> t)
  115. {
  116. for(auto x:m_arrTraLightGroup)
  117. if(x==t)
  118. return true;
  119. return false;
  120. }
  121. bool operator[](const std::string &s)
  122. {
  123. if (m_m[s]<15)
  124. {
  125. m_m[s]++;
  126. return false;
  127. }
  128. else
  129. {
  130. return true;
  131. }
  132. }
  133. const bool operator[](const std::string &s) const
  134. {
  135. auto it = m_m.find(s);
  136. if (it != m_m.end())
  137. {
  138. if(it->second<15)
  139. return false;
  140. else
  141. {
  142. return true;
  143. }
  144. }
  145. return false;
  146. }
  147. };
  148. struct solpoint:point
  149. {
  150. solpoint()
  151. :m_score(100)
  152. {
  153. }
  154. double m_score;
  155. bool operator<(const solpoint&p)const
  156. {
  157. return m_score<p.m_score;
  158. }
  159. void set_sol(const point&p,double score=100)
  160. {
  161. set(p);
  162. this->m_score=score;
  163. }
  164. double score()const
  165. {
  166. return m_score;
  167. }
  168. };
  169. struct base_card
  170. {
  171. public:
  172. const static int max_histime=60; //保留最后60s的数据
  173. zlist<loc_point,128> m_d;
  174. line m_line;
  175. int m_id,m_ct;
  176. point m_sol[4];
  177. point*m_sol_p;
  178. loc_info m_last_li;
  179. const site*m_last_site;
  180. fit_batch m_fitk,m_fita;
  181. fit_result m_cur_fit;
  182. loc_point* m_begin;
  183. loc_point* m_last;
  184. int m_filter_man_count; // in filter_by_fit
  185. // implemented by li
  186. //const site*m_current_site;
  187. //record of last fitting data
  188. bool m_last_fit_valid;
  189. fit_result m_last_fit;
  190. double m_last_fit_k ;
  191. double m_last_fit_kb;
  192. double m_last_fit_ka;
  193. double m_last_fit_xo ;
  194. double m_last_fit_yo;
  195. double m_last_fit_md_x;
  196. double m_last_fit_md_y;
  197. double m_last_fit_time_sec;
  198. double m_last_fit_nearest_time_sec; //latest time_sec within estimation span
  199. double m_last_time_sec;
  200. //record of last receiving time comment it out if using timer
  201. double m_last_receive_time_sec;
  202. double m_last_receive_ct;
  203. const site*m_last_receive_sit;
  204. //parameters
  205. double m_fit_differ;
  206. double m_pos_differ;
  207. // turning
  208. bool m_if_turning;
  209. point m_turning_pt, m_turning_ept;
  210. // smooth the dist----test
  211. bool smooth_initial_setting;
  212. double smooth_speed;
  213. double smooth_speed_presentation;
  214. int smooth_speed_presentation_cnt;
  215. point smooth_last_position;
  216. double smooth_last_time_sec;
  217. point smooth_last_true_position;
  218. line smooth_line;
  219. bool smooth_line_reset; //if line reset
  220. bool smooth_halt_condition; //if halting
  221. int smooth_halt_count; //halting count
  222. point smooth_halt_position; //position while begin halting
  223. point smooth_halt_position_plus;
  224. point smooth_halt_position_minus;
  225. // convert between pt and dist //
  226. double m_simple_rec_kx ;
  227. double m_simple_rec_ky ;
  228. std::shared_ptr<card_interface> m_card;
  229. // 1-20 implemented
  230. std::atomic<int> mutex;
  231. struct push_data_point:point
  232. {
  233. const site *sit;
  234. point dstp;
  235. int ct;
  236. bool valid; // if valid
  237. int stop_cnt; // if calculate speed
  238. loc_point lp;
  239. };
  240. zlist<push_data_point,16> m_push_list;
  241. // history location
  242. double his_speed;
  243. double his_speed_presentation;
  244. double his_last_time;
  245. point his_last_pt;
  246. line his_line;
  247. public:
  248. base_card();
  249. base_card(std::shared_ptr<card_interface> &c,int id=-1);
  250. void log(const char * );
  251. void base_card_initiate();
  252. int last_ct()const;
  253. void reset_sol();
  254. int sol_count()const;
  255. //point door
  256. void on_tof_data(const site*sit,const loc_info&li);
  257. //remove_history.
  258. void remove_history();
  259. //chose one point
  260. void select_solution(const site*sit);
  261. bool select_solution_impl(const site*sit);
  262. bool select_solution_impl_person(const site* sit);//人卡选点处理
  263. void save_k();
  264. //find m_d last parameter that is not empty.
  265. int find_last(int start);
  266. //find m_d first para that is not empty/
  267. int find_first(int start);
  268. //make a line between first and last this is not empty/
  269. bool make_line();
  270. //select point..
  271. point select_solution0(const site * sit);
  272. bool filter_by_fit(const site*sit);
  273. bool filter_by_speed(const site * sit);
  274. bool filter_by_acc(loc_point&c,std::array<solpoint,4>&v,double a);
  275. fit_result* best_fit_raw(int num_point=0,int start=0,int end=-1);
  276. const fit_result* best_fit()const;
  277. // void solution0();
  278. bool solution0(const site*sit);
  279. point revise_by_history(point pt, const site*sit, int64_t m_time, int &revise_flag);
  280. double estimate_point_by_history(const site*sit, double m_time_sec);
  281. void estimate_missing_point_by_history(const site*sit, double m_time_sec, int m_ct);
  282. double convert_pt_to_dist(point &pt, const site*sit);
  283. point convert_dist_to_pt(double dist, const site*sit);
  284. // turning
  285. void reset_turning();
  286. void detect_turning(point &mpt, const site*sit);
  287. double calc_turning_angle(point &a, point &b);
  288. void turning_mapping(point &rpt, const site*sit);
  289. void smooth_reset();
  290. bool smooth_initiate(point &pt, double t, const site*sit);
  291. void smooth_dist_car(point &pt, double t, int ct, const site*sit, point dstp, loc_point *m_lp = nullptr);
  292. void smooth_dist_man(point &pt, double t, int ct, const site*sit, point dstp, loc_point *m_lp = nullptr);
  293. void smooth_set_loc_point(double t, int ct, const site*sit, loc_point *lp);
  294. void generate_list(point &pt, const site*sit, bool is_whole_list);
  295. void put_single_loc_point(point &pt, const site*sit, int ct, loc_point &lp);
  296. void put_loc_point(point &pt, const site*sit, int ct, loc_point &lp);
  297. loc_point grab_loc_point();
  298. void his_reset();
  299. std::vector<his_location> make_up_more_points_for_history(point &pt, uint64_t t,const site *sit);
  300. };
  301. typedef std::map<std::string,std::shared_ptr<base_card>> Card_List;
  302. class base_card_list
  303. {
  304. public:
  305. typedef cacti::RecursiveMutex::ScopedLock ScopedLock;
  306. ~base_card_list()
  307. {
  308. m_cardlist.clear();
  309. }
  310. void erase(std::string cardid)
  311. {
  312. ScopedLock lp(m_monitor);
  313. Card_List::iterator iter = m_cardlist.find(cardid);
  314. if (iter != m_cardlist.end())
  315. {
  316. m_cardlist.erase(iter);
  317. }
  318. }
  319. void insert(std::string cardid,std::shared_ptr<base_card>& c)
  320. {
  321. ScopedLock lp(m_monitor);
  322. m_cardlist.insert(std::make_pair(cardid,c));
  323. }
  324. const std::shared_ptr<base_card> operator [](std::string cardid) const
  325. {
  326. Card_List::const_iterator iter = m_cardlist.find(cardid);
  327. if (iter != m_cardlist.end())
  328. {
  329. return iter->second;
  330. }
  331. else
  332. {
  333. return nullptr;
  334. }
  335. }
  336. std::shared_ptr<base_card> operator [](std::string cardid)
  337. {
  338. ScopedLock lp(m_monitor);
  339. Card_List::iterator iter = m_cardlist.find(cardid);
  340. if (iter != m_cardlist.end())
  341. {
  342. return iter->second;
  343. }
  344. else
  345. {
  346. return nullptr;
  347. }
  348. }
  349. private:
  350. Card_List m_cardlist;
  351. cacti::RecursiveMutex m_monitor;
  352. };
  353. #if 0
  354. int main()
  355. {
  356. std::unique_ptr<sit_list> sites(new sit_list());
  357. sites->load("data_reader_antenna.txt","dat_reader_path_tof.txt");
  358. // printf("%s\n",sites->to_string().c_str());
  359. fflush(stdout);
  360. // message_file mf(stdin);
  361. message_file mf("../raw_s_20171214.log.01");
  362. uint64_t ms;
  363. char buf[2048];
  364. std::unique_ptr<std::array<card,2000>> cards(new std::array<card,2000>());
  365. for(int i=0,len=cards->size();i<len;i++)
  366. cards->at(i).m_id=i;
  367. int len;
  368. while((len=mf.get_line(&ms,buf,sizeof(buf))))
  369. {
  370. loc_package lp(ms,buf);
  371. for(int i=0,ct=lp.count();i<ct;i++)
  372. {
  373. loc_info li=lp.get(i);
  374. if(li.m_card_type!=2) continue;
  375. if(li.m_card_id!=1222) continue;
  376. // if(li.m_card_id!=1011) continue;
  377. card&c=cards->at(li.m_card_id);
  378. printf("[lemon]t=%ld,sit=%d,ant=%d,card=%d,ct=%d,tof=%d,rav=%d,acc=%d,rsp=%.1f\n"
  379. ,li.m_loc_time,lp.m_sit_id, li.m_ant_id, c.m_id ,li.m_card_ct,li.m_tof
  380. ,li.m_rav,li.m_acc,li.m_card_sp );
  381. c.on_tof_data(&(*sites)[lp.m_sit_id],li);
  382. }
  383. }
  384. return 0;
  385. }
  386. #endif
  387. NAMESPACE_POINT_END(NAMESPACE_POINT)
  388. #endif