ant.h 6.4 KB

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