dispatch_ground_lidar.cpp 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  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. // std::cout << " huli test :::: " << " ---------------------------------------------------- = " << std::endl;
  54. // std::cout << " huli test :::: " << " m_ground_status_msg = " << m_ground_status_msg.DebugString() << std::endl;
  55. return Error_code::SUCCESS;
  56. }
  57. Dispatch_ground_lidar::Dispatch_ground_lidar_status Dispatch_ground_lidar::get_dispatch_ground_lidar_status()
  58. {
  59. return m_dispatch_ground_lidar_status;
  60. }
  61. //执行外界任务的执行函数
  62. void Dispatch_ground_lidar::execute_thread_fun()
  63. {
  64. LOG(INFO) << " Dispatch_ground_lidar::execute_thread_fun() start " << this;
  65. Error_manager t_error;
  66. while (m_execute_condition.is_alive())
  67. {
  68. m_execute_condition.wait();
  69. if (m_execute_condition.is_alive())
  70. {
  71. std::this_thread::sleep_for(std::chrono::milliseconds(1));
  72. // std::this_thread::sleep_for(std::chrono::seconds(1));
  73. std::this_thread::yield();
  74. if (m_ground_status_msg_updata_flag)
  75. {
  76. std::unique_lock<std::mutex> t_lock(Dispatch_communication::get_instance_references().m_data_lock);
  77. std::unique_lock<std::mutex> t_lock2(m_lock);
  78. //将nnxx的protobuf 转化为 snap7的DB块, 把店面雷达数据转发给plc
  79. int t_inlet_id = m_ground_status_msg.terminal_id() % 2;
  80. 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];
  81. 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];
  82. 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];
  83. if (p_request->m_request_communication_mode != 2)
  84. {
  85. p_response_data->m_response_heartbeat = 1-p_response_data->m_response_heartbeat;
  86. p_response_data->m_response_communication_mode = p_request->m_request_communication_mode;
  87. p_response_data->m_response_refresh_command = p_request->m_request_refresh_command;
  88. p_response_data->m_response_car_center_x = m_ground_status_msg.locate_information_realtime().locate_x();
  89. p_response_data->m_response_car_center_y = m_ground_status_msg.locate_information_realtime().locate_y();
  90. p_response_data->m_response_car_angle = m_ground_status_msg.locate_information_realtime().locate_angle()-90;
  91. p_response_data->m_response_car_front_theta = m_ground_status_msg.locate_information_realtime().locate_front_theta();
  92. p_response_data->m_response_car_length = m_ground_status_msg.locate_information_realtime().locate_length();
  93. p_response_data->m_response_car_width = m_ground_status_msg.locate_information_realtime().locate_width();
  94. p_response_data->m_response_car_height = m_ground_status_msg.locate_information_realtime().locate_height();
  95. p_response_data->m_response_car_wheel_base = m_ground_status_msg.locate_information_realtime().locate_wheel_base();
  96. p_response_data->m_response_car_wheel_width = m_ground_status_msg.locate_information_realtime().locate_wheel_width();
  97. p_response_data->m_response_locate_correct = m_ground_status_msg.locate_information_realtime().locate_correct();
  98. }
  99. else if (p_response_data->m_response_refresh_command == p_request->m_request_refresh_command)
  100. {
  101. p_response_data->m_response_heartbeat = 1-p_response_data->m_response_heartbeat;
  102. p_response_data->m_response_communication_mode = p_request->m_request_communication_mode;
  103. p_response_data->m_response_refresh_command = p_request->m_request_refresh_command;
  104. }
  105. else
  106. {
  107. p_response_data->m_response_heartbeat = 1-p_response_data->m_response_heartbeat;
  108. p_response_data->m_response_communication_mode = p_request->m_request_communication_mode;
  109. p_response_data->m_response_refresh_command = p_request->m_request_refresh_command;
  110. p_response_data->m_response_car_center_x = m_ground_status_msg.locate_information_realtime().locate_x();
  111. p_response_data->m_response_car_center_y = m_ground_status_msg.locate_information_realtime().locate_y();
  112. p_response_data->m_response_car_angle = m_ground_status_msg.locate_information_realtime().locate_angle()-90;
  113. p_response_data->m_response_car_front_theta = m_ground_status_msg.locate_information_realtime().locate_front_theta();
  114. p_response_data->m_response_car_length = m_ground_status_msg.locate_information_realtime().locate_length();
  115. p_response_data->m_response_car_width = m_ground_status_msg.locate_information_realtime().locate_width();
  116. p_response_data->m_response_car_height = m_ground_status_msg.locate_information_realtime().locate_height();
  117. p_response_data->m_response_car_wheel_base = m_ground_status_msg.locate_information_realtime().locate_wheel_base();
  118. p_response_data->m_response_car_wheel_width = m_ground_status_msg.locate_information_realtime().locate_wheel_width();
  119. p_response_data->m_response_locate_correct = m_ground_status_msg.locate_information_realtime().locate_correct();
  120. }
  121. m_ground_status_msg_updata_flag = false;
  122. }
  123. //判断超时
  124. {
  125. std::unique_lock<std::mutex> t_lock3(m_lock);
  126. if (std::chrono::system_clock::now() - m_ground_status_msg_updata_time > std::chrono::seconds(5))
  127. {
  128. m_dispatch_ground_lidar_status = DISPATCH_GROUND_LIDAR_DISCONNECT;
  129. }
  130. else
  131. {
  132. m_dispatch_ground_lidar_status = DISPATCH_GROUND_LIDAR_READY;
  133. }
  134. }
  135. }
  136. }
  137. }