dispatch_ground_lidar.cpp 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305
  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. //胡力 新增 接受雷达雷达 通信周期 超过1秒就打印
  63. int time_key = 10+m_ground_lidar_id;
  64. Time_tool::get_instance_references().time_end(time_key);
  65. if ( Time_tool::get_instance_references().timetool_map.find(time_key)!=Time_tool::get_instance_references().timetool_map.end() )
  66. {
  67. if ( Time_tool::get_instance_references().timetool_map[time_key].t_time_difference >= std::chrono::milliseconds(1000) )
  68. {
  69. Time_tool::get_instance_references().cout_time_millisecond(time_key);
  70. double dieoutTime=(double)Time_tool::get_instance_references().timetool_map[time_key].t_time_difference.count()/1000000;
  71. LOG(INFO) << "计时器:"<<time_key<<" 计时的时间为:" <<dieoutTime<<" 毫秒" << " --- " << this;
  72. }
  73. }
  74. Time_tool::get_instance_references().time_start(time_key);
  75. //胡力 新增 接受雷达雷达 通信周期 超过1秒就打印
  76. return Error_code::SUCCESS;
  77. }
  78. Dispatch_ground_lidar::Dispatch_ground_lidar_status Dispatch_ground_lidar::get_dispatch_ground_lidar_status()
  79. {
  80. return m_dispatch_ground_lidar_status;
  81. }
  82. //执行外界任务的执行函数
  83. void Dispatch_ground_lidar::execute_thread_fun()
  84. {
  85. LOG(INFO) << " Dispatch_ground_lidar::execute_thread_fun() start " << this;
  86. Error_manager t_error;
  87. while (m_execute_condition.is_alive())
  88. {
  89. m_execute_condition.wait();
  90. if (m_execute_condition.is_alive())
  91. {
  92. std::this_thread::sleep_for(std::chrono::milliseconds(1));
  93. // std::this_thread::sleep_for(std::chrono::seconds(1));
  94. std::this_thread::yield();
  95. if (m_ground_status_msg_updata_flag)
  96. {
  97. std::unique_lock<std::mutex> t_lock(Dispatch_communication::get_instance_references().m_data_lock);
  98. std::unique_lock<std::mutex> t_lock2(m_lock);
  99. check_measure_info(m_measure_info);
  100. //将nnxx的protobuf 转化为 snap7的DB块, 把店面雷达数据转发给plc
  101. // int t_inlet_id = m_ground_status_msg.mutable_id_struct()->terminal_id() % 2;
  102. int t_inlet_id = m_ground_lidar_id;
  103. 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];
  104. 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];
  105. 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];
  106. if (p_request->m_request_communication_mode != 2)
  107. {
  108. p_response_data->m_response_heartbeat = 1+p_response_data->m_response_heartbeat;
  109. p_response_data->m_response_communication_mode = p_request->m_request_communication_mode;
  110. p_response_data->m_response_refresh_command = p_request->m_request_refresh_command;
  111. // p_response_data->m_response_car_center_x = m_ground_status_msg.locate_information_realtime().locate_x();
  112. // p_response_data->m_response_car_center_y = m_ground_status_msg.locate_information_realtime().locate_y();
  113. // p_response_data->m_response_car_angle = m_ground_status_msg.locate_information_realtime().locate_angle();
  114. // p_response_data->m_response_car_front_theta = m_ground_status_msg.locate_information_realtime().locate_front_theta();
  115. // p_response_data->m_response_car_length = m_ground_status_msg.locate_information_realtime().locate_length();
  116. // p_response_data->m_response_car_width = m_ground_status_msg.locate_information_realtime().locate_width();
  117. // p_response_data->m_response_car_height = m_ground_status_msg.locate_information_realtime().locate_height();
  118. // p_response_data->m_response_car_wheel_base = m_ground_status_msg.locate_information_realtime().locate_wheel_base();
  119. // p_response_data->m_response_car_wheel_width = m_ground_status_msg.locate_information_realtime().locate_wheel_width();
  120. // p_response_data->m_response_uniformed_car_x = m_ground_status_msg.locate_information_realtime().uniformed_car_x();
  121. // p_response_data->m_response_uniformed_car_y = m_ground_status_msg.locate_information_realtime().uniformed_car_y();
  122. // p_response_data->m_response_locate_correct = m_ground_status_msg.locate_information_realtime().locate_correct();
  123. if ( m_measure_info.ground_status() == 0 ) //ground_status :ok 1,nothing 2,noise 3,border
  124. {
  125. p_response_data->m_response_car_center_x = m_measure_info.cx();
  126. p_response_data->m_response_car_center_y = m_measure_info.cy();
  127. p_response_data->m_response_car_angle = m_measure_info.theta();
  128. p_response_data->m_response_car_front_theta = m_measure_info.front_theta();
  129. p_response_data->m_response_car_length = m_measure_info.length();
  130. p_response_data->m_response_car_width = m_measure_info.width();
  131. p_response_data->m_response_car_height = m_measure_info.height();
  132. p_response_data->m_response_car_wheel_base = m_measure_info.wheelbase();
  133. p_response_data->m_response_car_wheel_width = m_measure_info.width();
  134. p_response_data->m_response_uniformed_car_x = m_measure_info.cx();
  135. p_response_data->m_response_uniformed_car_y = m_measure_info.cy();
  136. p_response_data->m_response_locate_correct = 1; //locate_correct 0=无效, 1=有效,
  137. p_response_data->m_response_ground_status = m_measure_info.ground_status(); //0 ok 1,nothing 2,noise 3,border
  138. p_response_data->m_response_border_status = m_measure_info.border_statu(); //超界状态描述, 按位运算
  139. p_response_data->m_response_motion_status = m_measure_info.motion_statu(); //运动状态,0=运动, 1=静止
  140. }
  141. else
  142. {
  143. p_response_data->m_response_car_center_x = m_measure_info.cx();
  144. p_response_data->m_response_car_center_y = m_measure_info.cy();
  145. p_response_data->m_response_car_angle = m_measure_info.theta();
  146. p_response_data->m_response_car_front_theta = m_measure_info.front_theta();
  147. p_response_data->m_response_car_length = m_measure_info.length();
  148. p_response_data->m_response_car_width = m_measure_info.width();
  149. p_response_data->m_response_car_height = m_measure_info.height();
  150. p_response_data->m_response_car_wheel_base = m_measure_info.wheelbase();
  151. p_response_data->m_response_car_wheel_width = m_measure_info.width();
  152. p_response_data->m_response_uniformed_car_x = m_measure_info.cx();
  153. p_response_data->m_response_uniformed_car_y = m_measure_info.cy();
  154. p_response_data->m_response_locate_correct = 0;
  155. p_response_data->m_response_ground_status = m_measure_info.ground_status(); //0 ok 1,nothing 2,noise 3,border
  156. p_response_data->m_response_border_status = m_measure_info.border_statu(); //超界状态描述, 按位运算
  157. p_response_data->m_response_motion_status = m_measure_info.motion_statu(); //运动状态,0=运动, 1=静止
  158. }
  159. // LOG(WARNING) << "------------------------------------------------------------ "<<std::endl;
  160. // LOG(WARNING) << "m_measure_info = " << m_measure_info.ground_status() <<std::endl;
  161. // LOG(WARNING) << "m_measure_info = " << m_measure_info.DebugString() <<std::endl;
  162. // LOG(WARNING) << "m_measure_info.wheelbase() = " << m_measure_info.wheelbase() <<std::endl;
  163. // LOG(WARNING) << "m_measure_info.ground_status() = " << m_measure_info.ground_status() <<std::endl;
  164. // LOG(WARNING) << "p_response_data->m_response_locate_correct = " << p_response_data->m_response_locate_correct <<std::endl;
  165. // printf("p_response_data->m_response_locate_correct = %d, \n", p_response_data->m_response_locate_correct);
  166. // LOG(WARNING) << "-++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ "<<std::endl;
  167. }
  168. else if (p_response_data->m_response_refresh_command == p_request->m_request_refresh_command)
  169. {
  170. p_response_data->m_response_heartbeat = 1+p_response_data->m_response_heartbeat;
  171. p_response_data->m_response_communication_mode = p_request->m_request_communication_mode;
  172. p_response_data->m_response_refresh_command = p_request->m_request_refresh_command;
  173. }
  174. else
  175. {
  176. p_response_data->m_response_heartbeat = 1+p_response_data->m_response_heartbeat;
  177. p_response_data->m_response_communication_mode = p_request->m_request_communication_mode;
  178. p_response_data->m_response_refresh_command = p_request->m_request_refresh_command;
  179. // p_response_data->m_response_car_center_x = m_ground_status_msg.locate_information_realtime().locate_x();
  180. // p_response_data->m_response_car_center_y = m_ground_status_msg.locate_information_realtime().locate_y();
  181. // p_response_data->m_response_car_angle = m_ground_status_msg.locate_information_realtime().locate_angle();
  182. // p_response_data->m_response_car_front_theta = m_ground_status_msg.locate_information_realtime().locate_front_theta();
  183. // p_response_data->m_response_car_length = m_ground_status_msg.locate_information_realtime().locate_length();
  184. // p_response_data->m_response_car_width = m_ground_status_msg.locate_information_realtime().locate_width();
  185. // p_response_data->m_response_car_height = m_ground_status_msg.locate_information_realtime().locate_height();
  186. // p_response_data->m_response_car_wheel_base = m_ground_status_msg.locate_information_realtime().locate_wheel_base();
  187. // p_response_data->m_response_car_wheel_width = m_ground_status_msg.locate_information_realtime().locate_wheel_width();
  188. // p_response_data->m_response_uniformed_car_x = m_ground_status_msg.locate_information_realtime().uniformed_car_x();
  189. // p_response_data->m_response_uniformed_car_y = m_ground_status_msg.locate_information_realtime().uniformed_car_y();
  190. // p_response_data->m_response_locate_correct = m_ground_status_msg.locate_information_realtime().locate_correct();
  191. if ( m_measure_info.ground_status() == 0 ) //ground_status :ok 1,nothing 2,noise 3,border
  192. {
  193. p_response_data->m_response_car_center_x = m_measure_info.cx();
  194. p_response_data->m_response_car_center_y = m_measure_info.cy();
  195. p_response_data->m_response_car_angle = m_measure_info.theta();
  196. p_response_data->m_response_car_front_theta = m_measure_info.front_theta();
  197. p_response_data->m_response_car_length = m_measure_info.length();
  198. p_response_data->m_response_car_width = m_measure_info.width();
  199. p_response_data->m_response_car_height = m_measure_info.height();
  200. p_response_data->m_response_car_wheel_base = m_measure_info.wheelbase();
  201. p_response_data->m_response_car_wheel_width = m_measure_info.width();
  202. p_response_data->m_response_uniformed_car_x = m_measure_info.cx();
  203. p_response_data->m_response_uniformed_car_y = m_measure_info.cy();
  204. p_response_data->m_response_locate_correct = 1; //locate_correct 0=无效, 1=有效,
  205. p_response_data->m_response_ground_status = m_measure_info.ground_status(); //0 ok 1,nothing 2,noise 3,border
  206. p_response_data->m_response_border_status = m_measure_info.border_statu(); //超界状态描述, 按位运算
  207. p_response_data->m_response_motion_status = m_measure_info.motion_statu(); //运动状态,0=运动, 1=静止
  208. }
  209. else
  210. {
  211. p_response_data->m_response_car_center_x = m_measure_info.cx();
  212. p_response_data->m_response_car_center_y = m_measure_info.cy();
  213. p_response_data->m_response_car_angle = m_measure_info.theta();
  214. p_response_data->m_response_car_front_theta = m_measure_info.front_theta();
  215. p_response_data->m_response_car_length = m_measure_info.length();
  216. p_response_data->m_response_car_width = m_measure_info.width();
  217. p_response_data->m_response_car_height = m_measure_info.height();
  218. p_response_data->m_response_car_wheel_base = m_measure_info.wheelbase();
  219. p_response_data->m_response_car_wheel_width = m_measure_info.width();
  220. p_response_data->m_response_uniformed_car_x = m_measure_info.cx();
  221. p_response_data->m_response_uniformed_car_y = m_measure_info.cy();
  222. p_response_data->m_response_locate_correct = 0;
  223. p_response_data->m_response_ground_status = m_measure_info.ground_status(); //0 ok 1,nothing 2,noise 3,border
  224. p_response_data->m_response_border_status = m_measure_info.border_statu(); //超界状态描述, 按位运算
  225. p_response_data->m_response_motion_status = m_measure_info.motion_statu(); //运动状态,0=运动, 1=静止
  226. }
  227. }
  228. m_ground_status_msg_updata_flag = false;
  229. }
  230. //判断超时
  231. {
  232. std::unique_lock<std::mutex> t_lock3(m_lock);
  233. if (std::chrono::system_clock::now() - m_ground_status_msg_updata_time > std::chrono::seconds(5))
  234. {
  235. m_dispatch_ground_lidar_status = DISPATCH_GROUND_LIDAR_DISCONNECT;
  236. }
  237. else
  238. {
  239. m_dispatch_ground_lidar_status = DISPATCH_GROUND_LIDAR_READY;
  240. }
  241. }
  242. }
  243. }
  244. }
  245. //检查感测消息
  246. Error_manager Dispatch_ground_lidar::check_measure_info(measure_info &t_measure_info)
  247. {
  248. if ( t_measure_info.theta() > 10 ||
  249. t_measure_info.theta() < -10)
  250. {
  251. LOG(INFO) << " 地面雷达数据异常, t_measure_info = " << t_measure_info.DebugString().c_str() << " --- " ;
  252. }
  253. return Error_code::SUCCESS;
  254. }