dispatch_ground_lidar.cpp 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. //
  2. // Created by huli on 2021/9/25.
  3. //
  4. #include "dispatch_ground_lidar.h"
  5. Dispatch_ground_lidar::Dispatch_ground_lidar()
  6. {
  7. m_dispatch_ground_lidar_status = DISPATCH_GROUND_LIDAR_UNKNOW;
  8. m_plc_id = 0;
  9. m_ground_lidar_id = 0;
  10. m_ground_status_msg_updata_flag = false;
  11. }
  12. Dispatch_ground_lidar::~Dispatch_ground_lidar()
  13. {
  14. dispatch_plc_uninit();
  15. }
  16. //调度地面雷达 初始化
  17. Error_manager Dispatch_ground_lidar::dispatch_plc_init(int plc_id, int ground_lidar_id)
  18. {
  19. m_plc_id = plc_id;
  20. m_ground_lidar_id = ground_lidar_id;
  21. mp_execute_thread = nullptr ;
  22. m_ground_status_msg_updata_time = std::chrono::system_clock::now();
  23. m_ground_status_msg_updata_flag = false;
  24. // 线程默认开启
  25. m_execute_condition.reset(false, true, false);
  26. mp_execute_thread = new std::thread(&Dispatch_ground_lidar::execute_thread_fun, this);
  27. m_dispatch_ground_lidar_status = DISPATCH_GROUND_LIDAR_READY;
  28. return Error_code::SUCCESS;
  29. }
  30. //调度地面雷达 反初始化
  31. Error_manager Dispatch_ground_lidar::dispatch_plc_uninit()
  32. {
  33. if (mp_execute_thread)
  34. {
  35. m_execute_condition.kill_all();
  36. }
  37. if (mp_execute_thread)
  38. {
  39. mp_execute_thread->join();
  40. delete mp_execute_thread;
  41. mp_execute_thread = NULL;
  42. }
  43. m_dispatch_ground_lidar_status = Dispatch_ground_lidar::DISPATCH_GROUND_LIDAR_UNKNOW;
  44. return Error_code::SUCCESS;
  45. }
  46. //调度地面雷达 执行状态消息
  47. Error_manager Dispatch_ground_lidar::execute_for_ground_status_msg(message::Ground_status_msg &ground_status_msg)
  48. {
  49. std::unique_lock<std::mutex> t_lock2(m_lock);
  50. m_ground_status_msg = ground_status_msg;
  51. m_ground_status_msg_updata_time = std::chrono::system_clock::now();
  52. m_ground_status_msg_updata_flag = true;
  53. return Error_code::SUCCESS;
  54. }
  55. Dispatch_ground_lidar::Dispatch_ground_lidar_status Dispatch_ground_lidar::get_dispatch_ground_lidar_status()
  56. {
  57. return m_dispatch_ground_lidar_status;
  58. }
  59. //执行外界任务的执行函数
  60. void Dispatch_ground_lidar::execute_thread_fun()
  61. {
  62. LOG(INFO) << " Dispatch_ground_lidar::execute_thread_fun() start " << this;
  63. Error_manager t_error;
  64. while (m_execute_condition.is_alive())
  65. {
  66. m_execute_condition.wait();
  67. if (m_execute_condition.is_alive())
  68. {
  69. std::this_thread::sleep_for(std::chrono::milliseconds(1));
  70. // std::this_thread::sleep_for(std::chrono::seconds(1));
  71. std::this_thread::yield();
  72. if (m_ground_status_msg_updata_flag)
  73. {
  74. std::unique_lock<std::mutex> t_lock(Dispatch_communication::get_instance_references().m_data_lock);
  75. std::unique_lock<std::mutex> t_lock2(m_lock);
  76. //将nnxx的protobuf 转化为 snap7的DB块, 把店面雷达数据转发给plc
  77. int t_inlet_id = m_ground_status_msg.terminal_id() % 2;
  78. Dispatch_communication::Ground_lidar_response_from_manager_to_plc_for_data *p_response_data = &Dispatch_communication::get_instance_references().m_ground_lidar_response_from_manager_to_plc_for_data[t_inlet_id];
  79. Dispatch_communication::Ground_lidar_response_from_manager_to_plc_for_key *p_response_key = &Dispatch_communication::get_instance_references().m_ground_lidar_response_from_manager_to_plc_for_key[t_inlet_id];
  80. Dispatch_communication::Ground_lidar_request_from_plc_to_manager *p_request = &Dispatch_communication::get_instance_references().m_ground_lidar_request_from_plc_to_manager[t_inlet_id];
  81. if (p_request->m_request_communication_mode == 0)
  82. {
  83. p_response_key->m_response_heartbeat++;
  84. p_response_key->m_response_communication_mode = p_request->m_request_communication_mode;
  85. p_response_key->m_response_refresh_command = p_request->m_request_refresh_command;
  86. p_response_data->m_response_car_center_x = m_ground_status_msg.locate_information_realtime().locate_x();
  87. p_response_data->m_response_car_center_y = m_ground_status_msg.locate_information_realtime().locate_y();
  88. p_response_data->m_response_car_angle = m_ground_status_msg.locate_information_realtime().locate_angle();
  89. p_response_data->m_response_car_front_theta = m_ground_status_msg.locate_information_realtime().locate_front_theta();
  90. p_response_data->m_response_car_length = m_ground_status_msg.locate_information_realtime().locate_length();
  91. p_response_data->m_response_car_width = m_ground_status_msg.locate_information_realtime().locate_width();
  92. p_response_data->m_response_car_height = m_ground_status_msg.locate_information_realtime().locate_height();
  93. p_response_data->m_response_car_wheel_base = m_ground_status_msg.locate_information_realtime().locate_wheel_base();
  94. p_response_data->m_response_car_wheel_width = m_ground_status_msg.locate_information_realtime().locate_wheel_width();
  95. p_response_data->m_response_locate_correct = m_ground_status_msg.locate_information_realtime().locate_correct();
  96. }
  97. else if (p_response_key->m_response_refresh_command == p_request->m_request_refresh_command)
  98. {
  99. p_response_key->m_response_heartbeat++;
  100. p_response_key->m_response_communication_mode = p_request->m_request_communication_mode;
  101. p_response_key->m_response_refresh_command = p_request->m_request_refresh_command;
  102. }
  103. else
  104. {
  105. p_response_key->m_response_heartbeat++;
  106. p_response_key->m_response_communication_mode = p_request->m_request_communication_mode;
  107. p_response_key->m_response_refresh_command = p_request->m_request_refresh_command;
  108. p_response_data->m_response_car_center_x = m_ground_status_msg.locate_information_realtime().locate_x();
  109. p_response_data->m_response_car_center_y = m_ground_status_msg.locate_information_realtime().locate_y();
  110. p_response_data->m_response_car_angle = m_ground_status_msg.locate_information_realtime().locate_angle();
  111. p_response_data->m_response_car_front_theta = m_ground_status_msg.locate_information_realtime().locate_front_theta();
  112. p_response_data->m_response_car_length = m_ground_status_msg.locate_information_realtime().locate_length();
  113. p_response_data->m_response_car_width = m_ground_status_msg.locate_information_realtime().locate_width();
  114. p_response_data->m_response_car_height = m_ground_status_msg.locate_information_realtime().locate_height();
  115. p_response_data->m_response_car_wheel_base = m_ground_status_msg.locate_information_realtime().locate_wheel_base();
  116. p_response_data->m_response_car_wheel_width = m_ground_status_msg.locate_information_realtime().locate_wheel_width();
  117. p_response_data->m_response_locate_correct = m_ground_status_msg.locate_information_realtime().locate_correct();
  118. }
  119. m_ground_status_msg_updata_flag = false;
  120. }
  121. //判断超时
  122. {
  123. std::unique_lock<std::mutex> t_lock3(m_lock);
  124. if (std::chrono::system_clock::now() - m_ground_status_msg_updata_time > std::chrono::seconds(5))
  125. {
  126. m_dispatch_ground_lidar_status = DISPATCH_GROUND_LIDAR_DISCONNECT;
  127. }
  128. else
  129. {
  130. m_dispatch_ground_lidar_status = DISPATCH_GROUND_LIDAR_READY;
  131. }
  132. }
  133. }
  134. }
  135. }