monkeycar_person.cpp 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300
  1. #include "stdafx.h"
  2. #include "monkeycar_person.h"
  3. #include "monkeycar_area.h"
  4. #include "algorithm/Fit.h"
  5. const int num_fit_point[4]={5,10,20,30};
  6. monkey_person::monkey_person(std::shared_ptr<Card> ptr,monkey_area* pma)
  7. :m_getOffIndex(0)
  8. ,m_ori_ct(0)
  9. ,m_cur_distance(0x12345678)
  10. ,m_compensation_speed(0)
  11. ,m_timeval(0)
  12. ,m_adjustDis_timeval(0)
  13. ,m_timestamp(0)
  14. {
  15. m_timestamp = time(NULL) * 1000;
  16. m_go_up_point.clear();
  17. //m_bus = nullptr;
  18. m_person = std::make_shared<person>();
  19. m_person->m_card = ptr;
  20. ptr->set_card_Ptr(m_person);
  21. //std::shared_ptr<area> ma(pma);
  22. m_person->m_area = pma;
  23. m_linear_fit=new comp_linear_fit(&num_fit_point[0],&num_fit_point[0]+sizeof(num_fit_point)/sizeof(num_fit_point[0]));
  24. //init monkey_fit
  25. mp_monkey_fit.front() = new monkey_fit_k; //k
  26. mp_monkey_fit.back() = new monkey_fit_b; //b
  27. }
  28. monkey_person::~monkey_person()
  29. {
  30. delete m_linear_fit;
  31. //free mem
  32. for(auto & mp:mp_monkey_fit)
  33. {
  34. delete mp;
  35. }
  36. m_person->m_card->resetPtr();
  37. }
  38. void monkey_person::GetOnTheBus(std::shared_ptr<monkey_bus> bus,double speed)
  39. {
  40. //assignment the monkeycar_bus value
  41. m_bus = bus;
  42. //
  43. m_go_up_point.setTime(m_person->m_card->m_ct_time);
  44. m_go_up_point.setCT(m_person->m_card->time_stamp);
  45. m_go_up_point.setX(m_person->m_card->origin_locate.x);
  46. m_go_up_point.setX(m_person->m_card->origin_locate.y);
  47. m_cur_distance = m_fit_point.back().x_;
  48. m_timeval = cacti::Timestamp::getNowTime().getAsMilliseconds();
  49. m_adjustDis_timeval = m_timeval;
  50. m_fit_point.clear();
  51. m_compensation_speed = 0;
  52. //when get on the bus, init monkey_fit
  53. reset(speed);
  54. }
  55. void monkey_person::GetOffTheBus()
  56. {
  57. Set_AverageSpeed();
  58. m_bus.reset();
  59. m_go_up_point.clear();
  60. //m_cur_point.clear();
  61. resetOffIndex();
  62. m_fit_point.clear();
  63. m_compensation_speed = 0;
  64. }
  65. void monkey_person::Set_AverageSpeed()
  66. {
  67. //set average speed.
  68. monkey_fit_k* fit_k = dynamic_cast<monkey_fit_k*>(mp_monkey_fit.front());
  69. double k = fit_k->get_K();
  70. if (!m_bus.expired()&& k != 0)
  71. {
  72. m_bus.lock()->Set_AverageSpeed(k);
  73. }
  74. }
  75. void monkey_person::reset(double speed)
  76. {
  77. for (auto & mp:mp_monkey_fit)
  78. {
  79. mp->reset(speed);
  80. }
  81. }
  82. bool monkey_person::Judge_OffIndex()
  83. {
  84. m_getOffIndex++;
  85. if (m_getOffIndex == MAX_GETOFF_NUM)
  86. {
  87. return true;
  88. }
  89. return false;
  90. }
  91. void monkey_person::resetOffIndex()
  92. {
  93. m_getOffIndex = 0;
  94. }
  95. std::string & monkey_person::getCardId()
  96. {
  97. return m_person->m_card->card_id;
  98. }
  99. bool monkey_person::is_on_bus() const
  100. {
  101. return !m_bus.expired();
  102. }
  103. point_2 monkey_person::get_position()
  104. {
  105. u64 timeval = cacti::Timestamp::getNowTime().getAsMilliseconds();
  106. if (m_bus.expired() || m_cur_distance == 0x12345678)
  107. {
  108. //debug_print_syslog(0,"[framework m_bus.expired()]cardId:%s",getCardId().c_str());
  109. m_timeval = timeval;
  110. return point_2::invalid_point();
  111. }
  112. //get shared_ptr
  113. monkey_area* mp_ptr =nullptr;
  114. mp_ptr = dynamic_cast<monkey_area*>(m_person->m_area);
  115. //here should lock ...take care
  116. std::shared_ptr<monkey_bus> tbus = nullptr;
  117. if (!m_bus.expired())
  118. {
  119. tbus = m_bus.lock();
  120. }
  121. else
  122. {
  123. return point_2::invalid_point();
  124. }
  125. //get time val
  126. double val = 1.*(timeval - m_timeval)/cacti::Timestamp::ONE_SEC_IN_MILLISEC;
  127. //get Distance
  128. double ds = tbus->getNextStep_Distance(val)/m_person->m_card->map_scale;
  129. m_cur_distance += ds;
  130. //compensation speed...
  131. m_cur_distance += m_compensation_speed * val;
  132. double total_length = mp_ptr->get_total_length();
  133. if (m_cur_distance < ZERO_)
  134. {
  135. m_cur_distance = 0;
  136. }
  137. else if (m_cur_distance >=total_length)
  138. {
  139. m_cur_distance = total_length;
  140. }
  141. //get point
  142. point_2 tmp = mp_ptr->map(m_cur_distance);
  143. //debug_print_syslog(0,"[framework get_position]cardId:%s,d:%f",getCardId().c_str(),m_cur_distance);
  144. if (point_2::is_valid(tmp))
  145. {
  146. //debug_print_syslog(0,"[framework get_position__]cardId:%s,d:%f",getCardId().c_str(),m_cur_distance);
  147. m_timeval = timeval;
  148. return tmp;
  149. }
  150. m_timeval = timeval;
  151. return point_2::invalid_point();
  152. }
  153. bool monkey_person::save_point()
  154. {
  155. return is_on_bus();
  156. }
  157. bool monkey_person::screen_Point(u16 ct,double d)
  158. {
  159. bool ret = true;
  160. u32 tct = m_ori_ct < ct ? m_ori_ct + USHRT_MAX : m_ori_ct;
  161. if (!m_fit_point.empty() && tct - ct == 1)
  162. {
  163. double last_d = m_fit_point.back().x_;
  164. double ad = abs(last_d - d);
  165. if (ad == 0 || ad > 4.0)
  166. {
  167. //debug_print_syslog(0,"[framework on_step_map____+++____]cardId:%s, LastDis:%.2f CurDis:%.2f ct:%d lastCT:%d ",getCardId().c_str(),last_d,d,tct,ct);
  168. ret = false;
  169. }
  170. }
  171. return ret;
  172. }
  173. bool monkey_person::on_step_map(POS_21::fp_path & fppath)
  174. {
  175. u16 ct = m_ori_ct;
  176. st_coord pt=m_person->pin_point(m_ori_ct);
  177. if (!point_2::is_valid(pt))
  178. {
  179. return false;
  180. }
  181. //debug_print_syslog(0,"[framework on_step_map]cardId:%s,orix:%.2f,oriy:%.2f ct:%d",getCardId().c_str(),pt.x_,pt.y_,pt.m_ct);
  182. m_timestamp = time(NULL)*1000;
  183. double d = fppath.map(pt.x_,pt.y_);
  184. if (d < ZERO_)
  185. {
  186. debug_print_syslog(0,"[framework d<0]cardId:%s,orix:%.2f,oriy:%.2f dis:%.2f ct:%d time:%ld",getCardId().c_str(),pt.x_,pt.y_,d,pt.m_ct,pt.gen_time_);
  187. return false;
  188. }
  189. pt.x_ = d;
  190. do
  191. {
  192. //if(!screen_Point(ct,d))
  193. // return false;
  194. // break;
  195. {
  196. if (!m_bus.expired())
  197. {
  198. for (auto & mp:mp_monkey_fit)
  199. {
  200. mp->push(1.*pt.gen_time_/1000.0,pt.x_*m_person->m_card->map_scale);
  201. }
  202. }
  203. }
  204. } while (false);
  205. m_linear_fit->push(pt.gen_time_,pt.x_);
  206. if (m_fit_point.size() >= MAX_POINT_V)
  207. {
  208. m_fit_point.pop_front();
  209. }
  210. m_fit_point.push_back(pt);
  211. //debug_print_syslog(0,"[framework on_step_map________]cardId:%s,orix:%.2f,oriy:%.2f dis:%.2f ct:%d time:%ld ===",getCardId().c_str(),pt.x_,pt.y_,d,pt.m_ct,pt.gen_time_);
  212. return true;
  213. }
  214. bool monkey_person::fit_speed(double*ret_speed)
  215. {
  216. int num_point=0;
  217. if(!get_num_point(&num_point))
  218. return false;
  219. //here multiply map_scale
  220. *ret_speed=m_linear_fit->getK(num_point)*m_person->m_card->map_scale;
  221. //debug_print_syslog(0,"[framework fit_speed]cardId:%s,speed:%f",getCardId().c_str(),*ret_speed);
  222. return true;
  223. }
  224. bool monkey_person::get_num_point(int * p)
  225. {
  226. int index = 5;
  227. int i = 0;
  228. while (index <= 30)
  229. {
  230. if (init_another_list(index))
  231. {
  232. *p = index;
  233. return true;
  234. }
  235. i += 2;
  236. index = 5 * i;
  237. }
  238. return false;
  239. }
  240. bool monkey_person::init_another_list(size_t index)
  241. {
  242. if (m_fit_point.size() < index)
  243. {
  244. return false;
  245. }
  246. if (index < 5)
  247. {
  248. return false;
  249. }
  250. size_t begin_count = (*std::prev(m_fit_point.end(),index)).m_ct;
  251. size_t end_count = (*std::prev(m_fit_point.end())).m_ct;
  252. end_count = end_count>begin_count?end_count:end_count+USHRT_MAX;
  253. if (end_count - begin_count <= (index-5)*1/5+index)
  254. {
  255. return true;
  256. }
  257. return false;
  258. }
  259. void monkey_person::updata_position()
  260. {
  261. }
  262. void monkey_person::handle_monkeycar_fit(double time_second)
  263. {
  264. monkey_fit_b * fit_b = dynamic_cast<monkey_fit_b*>(mp_monkey_fit.back());
  265. if (!fit_b->get_B())
  266. {
  267. return;
  268. }
  269. double ori_d = fit_b->get_dist(time_second)/m_person->m_card->map_scale;
  270. double compensation_speed = (ori_d-m_cur_distance)/(ADJUST_DISTANCE_TIMEVAL/2)*1000;
  271. setCompensationSpeed(compensation_speed);
  272. //debug_print_syslog(0,"[framework_2 compensationSpeed]%s,k:%f,b:%f,ori_dis:%f,cur_dis:%f,s:%f",getCardId().c_str(),fit_b->get_K(),fit_b->get_B(),ori_d,m_cur_distance,compensation_speed);
  273. }