dispatch_ground_lidar.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268
  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_ground_lidar_uninit();
  15. }
  16. //调度地面雷达 初始化
  17. Error_manager Dispatch_ground_lidar::dispatch_ground_lidar_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_ground_lidar_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. //调度地面雷达 执行状态消息
  56. Error_manager Dispatch_ground_lidar::execute_for_ground_status_msg_new(measure_info &t_measure_info)
  57. {
  58. std::unique_lock<std::mutex> t_lock2(m_lock);
  59. m_measure_info = t_measure_info;
  60. m_ground_status_msg_updata_time = std::chrono::system_clock::now();
  61. m_ground_status_msg_updata_flag = true;
  62. return Error_code::SUCCESS;
  63. }
  64. Dispatch_ground_lidar::Dispatch_ground_lidar_status Dispatch_ground_lidar::get_dispatch_ground_lidar_status()
  65. {
  66. return m_dispatch_ground_lidar_status;
  67. }
  68. //执行外界任务的执行函数
  69. void Dispatch_ground_lidar::execute_thread_fun()
  70. {
  71. LOG(INFO) << " Dispatch_ground_lidar::execute_thread_fun() start " << this;
  72. Error_manager t_error;
  73. while (m_execute_condition.is_alive())
  74. {
  75. m_execute_condition.wait();
  76. if (m_execute_condition.is_alive())
  77. {
  78. std::this_thread::sleep_for(std::chrono::milliseconds(1));
  79. // std::this_thread::sleep_for(std::chrono::seconds(1));
  80. std::this_thread::yield();
  81. if (m_ground_status_msg_updata_flag)
  82. {
  83. std::unique_lock<std::mutex> t_lock(Dispatch_communication::get_instance_references().m_data_lock);
  84. std::unique_lock<std::mutex> t_lock2(m_lock);
  85. //将nnxx的protobuf 转化为 snap7的DB块, 把店面雷达数据转发给plc
  86. // int t_inlet_id = m_ground_status_msg.mutable_id_struct()->terminal_id() % 2;
  87. int t_inlet_id = m_ground_lidar_id;
  88. 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];
  89. 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];
  90. 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];
  91. if (p_request->m_request_communication_mode != 2)
  92. {
  93. p_response_data->m_response_heartbeat = 1+p_response_data->m_response_heartbeat;
  94. p_response_data->m_response_communication_mode = p_request->m_request_communication_mode;
  95. p_response_data->m_response_refresh_command = p_request->m_request_refresh_command;
  96. // p_response_data->m_response_car_center_x = m_ground_status_msg.locate_information_realtime().locate_x();
  97. // p_response_data->m_response_car_center_y = m_ground_status_msg.locate_information_realtime().locate_y();
  98. // p_response_data->m_response_car_angle = m_ground_status_msg.locate_information_realtime().locate_angle();
  99. // p_response_data->m_response_car_front_theta = m_ground_status_msg.locate_information_realtime().locate_front_theta();
  100. // p_response_data->m_response_car_length = m_ground_status_msg.locate_information_realtime().locate_length();
  101. // p_response_data->m_response_car_width = m_ground_status_msg.locate_information_realtime().locate_width();
  102. // p_response_data->m_response_car_height = m_ground_status_msg.locate_information_realtime().locate_height();
  103. // p_response_data->m_response_car_wheel_base = m_ground_status_msg.locate_information_realtime().locate_wheel_base();
  104. // p_response_data->m_response_car_wheel_width = m_ground_status_msg.locate_information_realtime().locate_wheel_width();
  105. // p_response_data->m_response_uniformed_car_x = m_ground_status_msg.locate_information_realtime().uniformed_car_x();
  106. // p_response_data->m_response_uniformed_car_y = m_ground_status_msg.locate_information_realtime().uniformed_car_y();
  107. // p_response_data->m_response_locate_correct = m_ground_status_msg.locate_information_realtime().locate_correct();
  108. if ( m_measure_info.ground_status() == 0 ) //ground_status :ok 1,nothing 2,noise 3,border
  109. {
  110. p_response_data->m_response_car_center_x = m_measure_info.cx();
  111. p_response_data->m_response_car_center_y = m_measure_info.cy();
  112. p_response_data->m_response_car_angle = m_measure_info.theta();
  113. p_response_data->m_response_car_front_theta = m_measure_info.front_theta();
  114. p_response_data->m_response_car_length = m_measure_info.length();
  115. p_response_data->m_response_car_width = m_measure_info.width();
  116. p_response_data->m_response_car_height = m_measure_info.height();
  117. p_response_data->m_response_car_wheel_base = m_measure_info.wheelbase();
  118. p_response_data->m_response_car_wheel_width = m_measure_info.width();
  119. p_response_data->m_response_uniformed_car_x = m_measure_info.cx();
  120. p_response_data->m_response_uniformed_car_y = m_measure_info.cy();
  121. p_response_data->m_response_locate_correct = 1; //locate_correct 0=无效, 1=有效,
  122. p_response_data->m_response_ground_status = m_measure_info.ground_status(); //0 ok 1,nothing 2,noise 3,border
  123. }
  124. else
  125. {
  126. p_response_data->m_response_car_center_x = 0;
  127. p_response_data->m_response_car_center_y = 0;
  128. p_response_data->m_response_car_angle = 0;
  129. p_response_data->m_response_car_front_theta = 0;
  130. p_response_data->m_response_car_length = 0;
  131. p_response_data->m_response_car_width = 0;
  132. p_response_data->m_response_car_height = 0;
  133. p_response_data->m_response_car_wheel_base = 0;
  134. p_response_data->m_response_car_wheel_width = 0;
  135. p_response_data->m_response_uniformed_car_x = 0;
  136. p_response_data->m_response_uniformed_car_y = 0;
  137. p_response_data->m_response_locate_correct = 0;
  138. p_response_data->m_response_ground_status = m_measure_info.ground_status(); //0 ok 1,nothing 2,noise 3,border
  139. }
  140. // LOG(WARNING) << "------------------------------------------------------------ "<<std::endl;
  141. // LOG(WARNING) << "m_measure_info = " << m_measure_info.ground_status() <<std::endl;
  142. // LOG(WARNING) << "m_measure_info = " << m_measure_info.DebugString() <<std::endl;
  143. // LOG(WARNING) << "m_measure_info.wheelbase() = " << m_measure_info.wheelbase() <<std::endl;
  144. // LOG(WARNING) << "m_measure_info.ground_status() = " << m_measure_info.ground_status() <<std::endl;
  145. // LOG(WARNING) << "p_response_data->m_response_locate_correct = " << p_response_data->m_response_locate_correct <<std::endl;
  146. // printf("p_response_data->m_response_locate_correct = %d, \n", p_response_data->m_response_locate_correct);
  147. // LOG(WARNING) << "-++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ "<<std::endl;
  148. }
  149. else if (p_response_data->m_response_refresh_command == p_request->m_request_refresh_command)
  150. {
  151. p_response_data->m_response_heartbeat = 1+p_response_data->m_response_heartbeat;
  152. p_response_data->m_response_communication_mode = p_request->m_request_communication_mode;
  153. p_response_data->m_response_refresh_command = p_request->m_request_refresh_command;
  154. }
  155. else
  156. {
  157. p_response_data->m_response_heartbeat = 1+p_response_data->m_response_heartbeat;
  158. p_response_data->m_response_communication_mode = p_request->m_request_communication_mode;
  159. p_response_data->m_response_refresh_command = p_request->m_request_refresh_command;
  160. // p_response_data->m_response_car_center_x = m_ground_status_msg.locate_information_realtime().locate_x();
  161. // p_response_data->m_response_car_center_y = m_ground_status_msg.locate_information_realtime().locate_y();
  162. // p_response_data->m_response_car_angle = m_ground_status_msg.locate_information_realtime().locate_angle();
  163. // p_response_data->m_response_car_front_theta = m_ground_status_msg.locate_information_realtime().locate_front_theta();
  164. // p_response_data->m_response_car_length = m_ground_status_msg.locate_information_realtime().locate_length();
  165. // p_response_data->m_response_car_width = m_ground_status_msg.locate_information_realtime().locate_width();
  166. // p_response_data->m_response_car_height = m_ground_status_msg.locate_information_realtime().locate_height();
  167. // p_response_data->m_response_car_wheel_base = m_ground_status_msg.locate_information_realtime().locate_wheel_base();
  168. // p_response_data->m_response_car_wheel_width = m_ground_status_msg.locate_information_realtime().locate_wheel_width();
  169. // p_response_data->m_response_uniformed_car_x = m_ground_status_msg.locate_information_realtime().uniformed_car_x();
  170. // p_response_data->m_response_uniformed_car_y = m_ground_status_msg.locate_information_realtime().uniformed_car_y();
  171. // p_response_data->m_response_locate_correct = m_ground_status_msg.locate_information_realtime().locate_correct();
  172. if ( m_measure_info.ground_status() == 0 ) //ground_status :ok 1,nothing 2,noise 3,border
  173. {
  174. p_response_data->m_response_car_center_x = m_measure_info.cx();
  175. p_response_data->m_response_car_center_y = m_measure_info.cy();
  176. p_response_data->m_response_car_angle = m_measure_info.theta();
  177. p_response_data->m_response_car_front_theta = m_measure_info.front_theta();
  178. p_response_data->m_response_car_length = m_measure_info.length();
  179. p_response_data->m_response_car_width = m_measure_info.width();
  180. p_response_data->m_response_car_height = m_measure_info.height();
  181. p_response_data->m_response_car_wheel_base = m_measure_info.wheelbase();
  182. p_response_data->m_response_car_wheel_width = m_measure_info.width();
  183. p_response_data->m_response_uniformed_car_x = m_measure_info.cx();
  184. p_response_data->m_response_uniformed_car_y = m_measure_info.cy();
  185. p_response_data->m_response_locate_correct = 1; //locate_correct 0=无效, 1=有效,
  186. p_response_data->m_response_ground_status = m_measure_info.ground_status(); //0 ok 1,nothing 2,noise 3,border
  187. }
  188. else
  189. {
  190. p_response_data->m_response_car_center_x = 0;
  191. p_response_data->m_response_car_center_y = 0;
  192. p_response_data->m_response_car_angle = 0;
  193. p_response_data->m_response_car_front_theta = 0;
  194. p_response_data->m_response_car_length = 0;
  195. p_response_data->m_response_car_width = 0;
  196. p_response_data->m_response_car_height = 0;
  197. p_response_data->m_response_car_wheel_base = 0;
  198. p_response_data->m_response_car_wheel_width = 0;
  199. p_response_data->m_response_uniformed_car_x = 0;
  200. p_response_data->m_response_uniformed_car_y = 0;
  201. p_response_data->m_response_locate_correct = 0;
  202. p_response_data->m_response_ground_status = m_measure_info.ground_status(); //0 ok 1,nothing 2,noise 3,border
  203. }
  204. }
  205. m_ground_status_msg_updata_flag = false;
  206. }
  207. //判断超时
  208. {
  209. std::unique_lock<std::mutex> t_lock3(m_lock);
  210. if (std::chrono::system_clock::now() - m_ground_status_msg_updata_time > std::chrono::seconds(5))
  211. {
  212. m_dispatch_ground_lidar_status = DISPATCH_GROUND_LIDAR_DISCONNECT;
  213. }
  214. else
  215. {
  216. m_dispatch_ground_lidar_status = DISPATCH_GROUND_LIDAR_READY;
  217. }
  218. }
  219. }
  220. }
  221. }