monkeycar_person.cpp 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  1. #include "monkeycar_person.h"
  2. #include "monkeycar_area.h"
  3. #include "monkeycar_bus.h"
  4. #include <climits>
  5. #include "log.h"
  6. #include "tool_time.h"
  7. const int num_fit_point[4]={5,10,20,30};
  8. monkey_person::monkey_person(std::shared_ptr<monkey_area> area)
  9. :m_area(area)
  10. ,m_getOffIndex(0)
  11. ,m_cur_distance(0x12345678)
  12. ,m_compensation_speed(0)
  13. ,m_timeval(0)
  14. ,m_adjustDis_timeval(0)
  15. ,m_timestamp(0)
  16. {
  17. m_timestamp = time(NULL) * 1000;
  18. m_linear_fit=new comp_linear_fit(&num_fit_point[0],&num_fit_point[0]+sizeof(num_fit_point)/sizeof(num_fit_point[0]));
  19. mp_monkey_fit.front() = new monkey_fit_k; //k
  20. mp_monkey_fit.back() = new monkey_fit_b; //b
  21. }
  22. monkey_person::~monkey_person()
  23. {
  24. delete m_linear_fit;
  25. for(auto & mp:mp_monkey_fit)
  26. delete mp;
  27. }
  28. void monkey_person::GetOnTheBus(std::shared_ptr<monkey_bus> bus,double speed)
  29. {
  30. //assignment the monkeycar_bus value
  31. m_bus = bus;
  32. m_cur_distance = m_fit_point.back().x_;
  33. m_timeval = tool_time::now_to_ms();
  34. m_adjustDis_timeval = m_timeval;
  35. m_fit_point.clear();
  36. m_compensation_speed = 0;
  37. //when get on the bus, init monkey_fit
  38. reset(speed);
  39. }
  40. void monkey_person::GetOffTheBus()
  41. {
  42. Set_AverageSpeed();
  43. m_bus.reset();
  44. resetOffIndex();
  45. m_fit_point.clear();
  46. m_compensation_speed = 0;
  47. }
  48. void monkey_person::Set_AverageSpeed()
  49. {
  50. //set average speed.
  51. //monkey_fit_k* fit_k = dynamic_cast<monkey_fit_k*>(mp_monkey_fit.front());
  52. //double k = fit_k->get_K();
  53. double k = mp_monkey_fit.front()->get_K();
  54. if (!m_bus.expired()&& k != 0)
  55. {
  56. //debug_print_syslog(0,"[framework_2 Set_AverageSpeed_getoff]%s,v:%f",getCardId().c_str(),k);
  57. m_bus.lock()->Set_AverageSpeed(k);
  58. }
  59. }
  60. void monkey_person::reset(double speed)
  61. {
  62. for (auto & mp:mp_monkey_fit)
  63. {
  64. mp->reset(speed);
  65. }
  66. }
  67. bool monkey_person::Judge_OffIndex()
  68. {
  69. m_getOffIndex++;
  70. if (m_getOffIndex == MAX_GETOFF_NUM)
  71. {
  72. return true;
  73. }
  74. return false;
  75. }
  76. void monkey_person::resetOffIndex()
  77. {
  78. m_getOffIndex = 0;
  79. }
  80. bool monkey_person::is_on_bus() const
  81. {
  82. return !m_bus.expired();
  83. }
  84. std::shared_ptr<monkey_bus> monkey_person::getBus()
  85. {
  86. return m_bus.lock();
  87. }
  88. point_2 monkey_person::get_position(double &speedx)
  89. {
  90. uint64_t timeval = tool_time::now_to_ms();
  91. if (m_bus.expired() || m_cur_distance == 0x12345678)
  92. {
  93. //debug_print_syslog(0,"[framework m_bus.expired()]cardId:%s",getCardId().c_str());
  94. m_timeval = timeval;
  95. return point_2::invalid_point();
  96. }
  97. //get shared_ptr
  98. //debug_print_syslog(0,"[framework get_positon]cardId:%s x:%.2f,time:%lld",getCardId().c_str(),m_cur_distance,m_timeval);
  99. //here should lock ...take care
  100. std::shared_ptr<monkey_bus> tbus = nullptr;
  101. if (!m_bus.expired())
  102. {
  103. tbus = m_bus.lock();
  104. }
  105. else
  106. {
  107. return point_2::invalid_point();
  108. }
  109. //get time val
  110. double val = 1.*(timeval - m_timeval)/1000;
  111. //get Distance
  112. double scale=m_area.lock()->getScale();
  113. double ds = tbus->getNextStep_Distance(val)/scale;
  114. m_cur_distance += ds;
  115. //compensation speed...
  116. m_cur_distance += m_compensation_speed * val;
  117. double total_length = m_area.lock()->get_total_length();
  118. speedx=(tbus->m_speed+m_compensation_speed*scale)*3.6;
  119. if (m_cur_distance < ZERO_)
  120. {
  121. m_cur_distance = 0;
  122. speedx=0;
  123. }
  124. else if (m_cur_distance >=total_length)
  125. {
  126. m_cur_distance = total_length;
  127. speedx=0;
  128. }
  129. //get point
  130. point_2 tmp = m_area.lock()->map(m_cur_distance);
  131. //debug_print_syslog(0,"[framework get_position]cardId:%s,d:%f",getCardId().c_str(),m_cur_distance);
  132. if (point_2::is_valid(tmp))
  133. {
  134. //debug_print_syslog(0,"[framework get_position__]cardId:%s,d:%f",getCardId().c_str(),m_cur_distance);
  135. m_timeval = timeval;
  136. return tmp;
  137. }
  138. m_timeval = timeval;
  139. return point_2::invalid_point();
  140. }
  141. bool monkey_person::on_step_map(const st_coord & pt_)
  142. {
  143. st_coord pt(pt_);
  144. if (!point_2::is_valid(pt))
  145. {
  146. return false;
  147. }
  148. m_timestamp = time(NULL)*1000;
  149. double d = m_area.lock()->map(pt.x_,pt.y_);
  150. if (d < ZERO_)
  151. {
  152. //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_);
  153. return false;
  154. }
  155. pt.x_ = d;
  156. if (!m_bus.expired())
  157. {
  158. for (auto & mp:mp_monkey_fit)
  159. {
  160. mp->push(1.*pt.gen_time_/1000.0,pt.x_*m_area.lock()->getScale());
  161. }
  162. }
  163. m_linear_fit->push(pt.gen_time_,pt.x_);
  164. if (m_fit_point.size() >= MAX_POINT_V)
  165. {
  166. m_fit_point.pop_front();
  167. }
  168. m_fit_point.push_back(pt);
  169. return true;
  170. }
  171. bool monkey_person::fit_speed(double*ret_speed)
  172. {
  173. int num_point=0;
  174. if(!get_num_point(&num_point))
  175. return false;
  176. //here multiply map_scale
  177. *ret_speed=m_linear_fit->getK(num_point)*m_area.lock()->getScale();
  178. //debug_print_syslog(0,"[framework fit_speed]cardId:%s,speed:%f",getCardId().c_str(),*ret_speed);
  179. return true;
  180. }
  181. bool monkey_person::get_num_point(int * p)
  182. {
  183. int index = 5;
  184. int i = 0;
  185. while (index <= 30)
  186. {
  187. if (init_another_list(index))
  188. {
  189. *p = index;
  190. return true;
  191. }
  192. i += 2;
  193. index = 5 * i;
  194. }
  195. return false;
  196. }
  197. bool monkey_person::init_another_list(size_t index)
  198. {
  199. if (m_fit_point.size() < index)
  200. {
  201. return false;
  202. }
  203. if (index < 5)
  204. {
  205. return false;
  206. }
  207. size_t begin_count = (*std::prev(m_fit_point.end(),index)).m_ct;
  208. size_t end_count = (*std::prev(m_fit_point.end())).m_ct;
  209. end_count = end_count>begin_count?end_count:end_count+USHRT_MAX;
  210. if (end_count - begin_count <= (index-5)*1/5+index)
  211. {
  212. return true;
  213. }
  214. return false;
  215. }
  216. void monkey_person::handle_monkeycar_fit(double time_second)
  217. {
  218. monkey_fit_b * fit_b = dynamic_cast<monkey_fit_b*>(mp_monkey_fit.back());
  219. if (!fit_b->get_B())
  220. {
  221. return;
  222. }
  223. double ori_d = fit_b->get_dist(time_second)/m_area.lock()->getScale();
  224. double compensation_speed = (ori_d-m_cur_distance)/(ADJUST_DISTANCE_TIMEVAL/2)*1000;
  225. setCompensationSpeed(compensation_speed);
  226. log_info("monkeycar framework_2 compensationSpeed]k:%f,b:%f,ori_dis:%f,cur_dis:%f,s:%f",fit_b->get_K(),fit_b->get_B(),ori_d,m_cur_distance,compensation_speed);
  227. }