1
0

monkeycar_bus.cpp 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. #include <functional>
  2. #include "monkeycar_bus.h"
  3. #include "monkeycar_area.h"
  4. #include "monkeycar_person.h"
  5. #include "log.h"
  6. #include "tool_time.h"
  7. #ifndef WIN32
  8. #include <unistd.h>
  9. #endif
  10. monkey_bus::monkey_bus(double speed, int direct)
  11. :m_speed(speed)
  12. ,m_direct(direct)
  13. ,m_stop(false)
  14. {
  15. m_thread.reset(new std::thread(std::bind(&monkey_bus::adjust_monkeyperson_distance,this)));
  16. }
  17. void monkey_bus::adjust_monkeyperson_distance()
  18. {
  19. bool cflag = false;
  20. std::vector<std::shared_ptr<monkey_person>> v;
  21. while (!m_stop)
  22. {
  23. v.clear();
  24. uint64_t nowTime = tool_time::tool_time::now_to_ms();
  25. {
  26. std::lock_guard<std::mutex> lk(m_mtx);
  27. std::for_each(person_list_.begin(),person_list_.end(),[&](const std::shared_ptr<monkey_person> mp){
  28. if (nowTime - mp->m_adjustDis_timeval >= ADJUST_DISTANCE_TIMEVAL/2 && !cflag)
  29. {
  30. mp->setCompensationSpeed(0);
  31. cflag = true;
  32. }
  33. if (nowTime - mp->m_adjustDis_timeval >= ADJUST_DISTANCE_TIMEVAL)
  34. {
  35. mp->m_adjustDis_timeval = nowTime;
  36. mp->handle_monkeycar_fit(1.*(nowTime-monkey_area::m_startTime)/1000.0);
  37. cflag = false;
  38. }
  39. });
  40. std::for_each(person_list_.begin(),person_list_.end(),[&](const std::shared_ptr<monkey_person> mp){
  41. if (nowTime-mp->m_timestamp > LONG_TIME_NO_DATA)
  42. v.push_back(mp);
  43. });
  44. }
  45. std::for_each(v.begin(),v.end(),[&](const std::shared_ptr<monkey_person>& mp){
  46. getOffTheBus(mp);
  47. });
  48. std::this_thread::sleep_for(std::chrono::milliseconds(100));
  49. }
  50. }
  51. bool monkey_bus::test_get_on(double speed)
  52. {
  53. double s=speed>0?speed:-speed;
  54. double ms=m_speed>0?m_speed:-m_speed;
  55. log_info("monkeycar test_get_on:%.2f,%.2f",s,ms);
  56. log_info("monkeycar ...........:%.2f,%.2f",ms*ON_BUS_SPEED_LIMIT,ms*ON_BUS_SPEED_MAX_LIMIT);
  57. if (s >= ms*ON_BUS_SPEED_LIMIT && s < ms*ON_BUS_SPEED_MAX_LIMIT)
  58. {
  59. log_info("monkeycar test_get_on_true:%.2f,%.2f",speed,m_speed);
  60. return true;
  61. }
  62. return false;
  63. }
  64. bool monkey_bus::test_get_off(std::shared_ptr<monkey_person> mp,double speed)
  65. {
  66. bool ret = false;
  67. double s=speed>0?speed:-speed;
  68. double ms=m_speed>0?m_speed:-m_speed;
  69. ret = (speed > ZERO_ ? NEGTIVE_DIRECTION:POSTIVE_DIRECTION) == m_direct ;
  70. if (ret)
  71. {
  72. if (s < ms*OFF_BUS_SPEED_LIMIT)
  73. {
  74. if(mp->Judge_OffIndex())
  75. {
  76. ret = false;
  77. }
  78. }
  79. else
  80. {
  81. mp->resetOffIndex();
  82. }
  83. }
  84. //debug_print_syslog(0,"[lemon the second choice true___]cardId:%s,v:%f,state:%s",mp->getCardId().c_str(),speed,ret?"ON":"OFF");
  85. return ret;
  86. }
  87. void monkey_bus::Set_AverageSpeed(double speed)
  88. {
  89. m_speed = m_speed * 0.99 + speed * 0.01;
  90. }
  91. void monkey_bus::getOffTheBus(std::shared_ptr<monkey_person> mp)
  92. {
  93. //can not free here .
  94. mp->GetOffTheBus();
  95. std::lock_guard<std::mutex> lk(m_mtx);
  96. auto it = std::find(person_list_.begin(),person_list_.end(),mp);
  97. if (it != person_list_.end())
  98. {
  99. person_list_.erase(it);
  100. }
  101. }
  102. void monkey_bus::getOnTheBus(std::shared_ptr<monkey_person> mp,std::shared_ptr<monkey_bus> bus)
  103. {
  104. log_info("monkeycar getOn");
  105. mp->GetOnTheBus(bus,m_speed);
  106. std::lock_guard<std::mutex> lk(m_mtx);
  107. person_list_.push_back(mp);
  108. }