ant.h 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310
  1. #ifndef _ANT_LIST_HPP_
  2. #define _ANT_LIST_HPP_
  3. #include <math.h>
  4. #include <array>
  5. #include <deque>
  6. #include <tuple>
  7. #include <memory>
  8. #include <algorithm>
  9. #include <sstream>
  10. #include "log.h"
  11. #include "line.h"
  12. #include "point.h"
  13. #include "write-copy.h"
  14. class client;
  15. struct path
  16. {
  17. std::array<line_v,2> m_line;
  18. std::array<double,2> m_slope;
  19. path()
  20. {
  21. for(auto &d:m_slope)
  22. d=0;
  23. }
  24. std::string to_str() const
  25. {
  26. std::stringstream ss;
  27. for(int i=0;i<2;i++)
  28. {
  29. ss<<"line:" <<m_line[i].to_string()<<"slope:"<<m_slope[i]<< " cos:"<<m_line[i].cos()<<" sin:"<<m_line[i].sin()<<" | ";
  30. }
  31. return ss.str();
  32. }
  33. bool vaild() const
  34. {
  35. return !m_line[0].empty();
  36. }
  37. line_v & operator[](int i)
  38. {
  39. return m_line[i];
  40. }
  41. const line_v & operator[](int i) const
  42. {
  43. return m_line[i];
  44. }
  45. };
  46. //?
  47. struct algo_config
  48. {
  49. const char*desc;
  50. int min_msg_cnt;
  51. int best_msg_cnt;
  52. double min_wait_time;
  53. double max_wait_time;
  54. };
  55. //
  56. struct ant :point
  57. {
  58. std::vector<path> m_path;
  59. path & operator[](int i)
  60. {
  61. return m_path[i];
  62. }
  63. const path & operator[](int i) const
  64. {
  65. return m_path[i];
  66. }
  67. size_t size() const
  68. {
  69. return m_path.size();
  70. }
  71. std::vector<point> getsol(const double &dist) const
  72. {
  73. std::vector<point> v;
  74. for(const auto & p : m_path)
  75. {
  76. double d = dist;
  77. if(p.vaild())
  78. {
  79. point pt;
  80. if(dist <= p.m_line[0].length() || (dist > p.m_line[0].length() && p.m_line[1].empty()))
  81. {
  82. d += d*p.m_slope[0];
  83. pt = point(p.m_line[0][0].x + d*p.m_line[0].cos() , p.m_line[0][0].y + d*p.m_line[0].sin());
  84. }
  85. else
  86. {
  87. d -= p.m_line[0].length();
  88. d += d*p.m_slope[1];
  89. pt = point(p.m_line[1][0].x+d*p.m_line[1].cos(),p.m_line[1][0].y+d*p.m_line[1].sin());
  90. }
  91. v.push_back(pt);
  92. //std_info("get_sol:x:%.2f,y:%.2f",pt.x,pt.y);
  93. }
  94. else
  95. std_error("%s","ant::getsol empty path..");
  96. }
  97. return std::move(v);
  98. }
  99. };
  100. /**
  101. * @brief 分站位置 1井上,2井下
  102. */
  103. enum UP_DOWN_SITE
  104. {
  105. ///井上分站
  106. UP_SITE=1,
  107. ///井下分站
  108. DOWN_SITE=2
  109. };
  110. struct site:point
  111. {
  112. static algo_config g_config[];
  113. int m_algo; //TOF:0,TDOA:1
  114. int m_num_dims; //1维:0,2维:1,3维:2
  115. double m_scale = 2.0; // 地图比例尺
  116. point m_position;
  117. int index()const;
  118. const algo_config&config()const;
  119. int id()const
  120. {
  121. return m_id;
  122. }
  123. site(int id=-1);
  124. mutable double m_height=1.5;
  125. int m_id;
  126. bool m_path_empty;
  127. std::array<ant,2> m_ant;
  128. mutable double m_ant_dist=0;
  129. mutable double m_ant_dist_sum_new=0;
  130. mutable int m_ant_dist_cnt_new=0;
  131. ///分站位置 UP_DOWN_SITE
  132. int m_up_down;
  133. std::shared_ptr<client> m_clt=nullptr;
  134. void set_client(std::shared_ptr<client>& clt);
  135. point get_dstp(const point pt) const
  136. {
  137. point tmp;
  138. for(const auto & p : m_ant[0].m_path)
  139. {
  140. for(int i=0;i<2;i++)
  141. {
  142. if(!p[i].empty())
  143. {
  144. if(p[i].contain(pt,0.01))
  145. {
  146. //if(i==0)
  147. // return *this;
  148. //else
  149. tmp = p[i][0];
  150. }
  151. }
  152. }
  153. }
  154. return tmp;
  155. }
  156. void count_ant_dist(double dist_tof1, double dist_tof2)const
  157. {
  158. if(dist_tof1<10 || dist_tof2<10)
  159. return;
  160. double dist = fabs(dist_tof1 - dist_tof2);
  161. if(dist>5)
  162. return;
  163. m_ant_dist_sum_new += dist;
  164. m_ant_dist_cnt_new++;
  165. if(m_ant_dist_cnt_new >= 2500)
  166. {
  167. m_ant_dist = m_ant_dist_sum_new / m_ant_dist_cnt_new;
  168. m_ant_dist_sum_new = 0;
  169. m_ant_dist_cnt_new = 0;
  170. }
  171. }
  172. void swap()
  173. {
  174. auto v0 = m_ant[0].m_path;
  175. auto v1 = m_ant[1].m_path;
  176. std::copy (std::begin(v0),std::end(v0),std::back_inserter(m_ant[1].m_path));
  177. std::copy (std::begin(v1),std::end(v1),std::back_inserter(m_ant[0].m_path));
  178. }
  179. double ant_dist()const
  180. {
  181. return m_ant[0].dist(m_ant[1]);
  182. }
  183. bool is_path_empty()const
  184. {
  185. return m_path_empty;
  186. }
  187. bool have_valid_path()const
  188. {
  189. return m_id != -1 && ant_dist() > 0.1;
  190. }
  191. std::string to_string()const
  192. {
  193. std::stringstream ss;
  194. ss<<"site_id:"<<m_id<<"x:"<<x<<" y: "<<y<<" scale:"<<m_scale;
  195. for(const auto a:m_ant)
  196. {
  197. ss<<"<";
  198. for(const auto p:a.m_path)
  199. {
  200. ss<<"{"<<p.to_str()<<"}";
  201. }
  202. ss<<">";
  203. }
  204. return ss.str();
  205. }
  206. const point&path(int i)const
  207. {
  208. static point p;
  209. if(i>=(int)m_ant[0].m_path.size())
  210. return p ;
  211. return m_ant[0].m_path[i].m_line[0][1];
  212. }
  213. std::vector<point> solving(int ant_id, double dist)const
  214. {
  215. const ant &a = m_ant[ant_id];
  216. if(dist<50 && dist>0)
  217. {
  218. if(dist<m_height)
  219. {
  220. m_height=dist;
  221. dist=0;
  222. }
  223. else
  224. {
  225. dist=sqrt(dist*dist-m_height*m_height);
  226. }
  227. }
  228. return std::move(a.getsol(dist));
  229. }
  230. ant operator[](int i)
  231. {
  232. return m_ant[i];
  233. }
  234. const ant operator[](int i) const
  235. {
  236. return m_ant[i];
  237. }
  238. };
  239. struct sit_list:single_base<sit_list,int,std::shared_ptr<site>>
  240. {
  241. void load(const char*ant_file,const char*path_file)
  242. {
  243. read_sit_list(ant_file);
  244. read_ant_path(path_file);
  245. }
  246. void load_from_db()
  247. {
  248. load("data_reader_antenna.txt","path_tof.txt");
  249. }
  250. void read_sit_list(const char*fname);
  251. void read_ant_path(const char*fname);
  252. };
  253. struct loc_message
  254. {
  255. site m_sit;
  256. uint64_t m_num_ticks; //tof时间片m_tof或tdoa相对root时间
  257. uint64_t m_loc_time;
  258. uint32_t m_card_id;
  259. int32_t m_card_ct;
  260. int8_t m_card_type;
  261. int8_t m_ant_id;
  262. int16_t m_rav;
  263. int16_t m_acc;
  264. uint16_t m_sync_ct;
  265. uint16_t m_rssi;
  266. loc_message();
  267. loc_message(site s,uint64_t num_ticks,uint64_t timestamp,
  268. uint32_t cardid,int32_t ct,int8_t type,int8_t antid,
  269. int16_t rav,int16_t acc,uint16_t sync_ct,uint16_t rssi)
  270. :m_sit(s)
  271. ,m_num_ticks(num_ticks)
  272. ,m_loc_time(timestamp)
  273. ,m_card_id(cardid)
  274. ,m_card_ct(ct)
  275. ,m_card_type(type)
  276. ,m_ant_id(antid)
  277. ,m_rav(rav)
  278. ,m_acc(acc)
  279. ,m_sync_ct(sync_ct)
  280. ,m_rssi(rssi)
  281. {}
  282. int tool_index()const;
  283. };
  284. #endif