system_executor.cpp 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751
  1. //
  2. // Created by huli on 2020/7/2.
  3. //
  4. #include "system_executor.h"
  5. #include "../tool/common_data.h"
  6. #include "../tool/measure_filter.h"
  7. #include "../message/measure_message.pb.h"
  8. #include "../system/system_communication.h"
  9. #include "../wanji_lidar/wanji_manager.h"
  10. #include "../velodyne_lidar/velodyne_manager.h"
  11. // std::ofstream g_debug_file; // 用于测试滤波功能
  12. System_executor::System_executor()
  13. {
  14. }
  15. System_executor::~System_executor()
  16. {
  17. system_executor_uninit();
  18. }
  19. //初始化
  20. Error_manager System_executor::system_executor_init(int threads_size, int terminal_id)
  21. {
  22. m_thread_pool.thread_pool_init(threads_size);
  23. m_system_executor_status = SYSTEM_EXECUTOR_READY;
  24. m_terminal_id = terminal_id;
  25. return Error_code::SUCCESS;
  26. }
  27. //反初始化
  28. Error_manager System_executor::system_executor_uninit()
  29. {
  30. // if(g_debug_file.is_open())
  31. // g_debug_file.close();
  32. m_thread_pool.thread_pool_uninit();
  33. m_system_executor_status = SYSTEM_EXECUTOR_UNKNOW;
  34. return Error_code::SUCCESS;
  35. }
  36. //检查消息是否有效, 主要检查消息类型和接受者, 判断这条消息是不是给我的.
  37. Error_manager System_executor::check_msg(Communication_message* p_msg)
  38. {
  39. if ( p_msg == NULL )
  40. {
  41. return Error_manager(Error_code::POINTER_IS_NULL, Error_level::MINOR_ERROR,
  42. " POINTER IS NULL ");
  43. }
  44. //检查消息类型
  45. switch ( p_msg->get_message_type() )
  46. {
  47. case Communication_message::Message_type::eGround_detect_request_msg:
  48. {
  49. //检查接受人
  50. if ( p_msg->get_receiver() == Communication_message::Communicator::eGround_measurer)
  51. {
  52. message::Ground_detect_request_msg t_ground_detect_request_msg;
  53. //针对消息类型, 对消息进行二次解析
  54. if (t_ground_detect_request_msg.ParseFromString(p_msg->get_message_buf()))
  55. {
  56. // //检查终端id
  57. // 普爱不检查终端id
  58. // 楚天检查终端id
  59. // if ( t_ground_detect_request_msg.terminal_id() == m_terminal_id )
  60. // {
  61. return Error_code::SUCCESS;
  62. // }
  63. }
  64. }
  65. break;
  66. }
  67. default :
  68. ;
  69. break;
  70. }
  71. //无效的消息,
  72. return Error_manager(Error_code::INVALID_MESSAGE, Error_level::NEGLIGIBLE_ERROR,
  73. " INVALID_MESSAGE error ");
  74. }
  75. //检查执行者的状态, 判断能否处理这条消息,
  76. Error_manager System_executor::check_executer(Communication_message* p_msg)
  77. {
  78. //huli DDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDD 记得删除
  79. return Error_code::SUCCESS;
  80. if ( p_msg == NULL )
  81. {
  82. return Error_manager(Error_code::POINTER_IS_NULL, Error_level::MINOR_ERROR,
  83. " POINTER IS NULL ");
  84. }
  85. Error_manager t_error;
  86. //通过 p_msg->get_message_type() 和 p_msg->get_receiver() 找到处理模块的实例对象, 查询执行人是否可以处理这条消息
  87. switch ( p_msg->get_message_type() )
  88. {
  89. case Communication_message::Message_type::eGround_detect_request_msg:
  90. {
  91. Error_manager t_executor_result = System_executor::get_instance_references().check_status();
  92. if (t_executor_result == SUCCESS)
  93. {
  94. return Error_code::SUCCESS;
  95. }
  96. else
  97. {
  98. //整合所有的错误码
  99. t_error.compare_and_cover_error(t_executor_result);
  100. if (t_error.get_error_level() == NEGLIGIBLE_ERROR)//一级故障,轻微故障,
  101. {
  102. LOG(INFO) << "executer_is_busy , ";
  103. //返回繁忙之后, 通信模块1秒后再次调用check
  104. return Error_code::COMMUNICATION_EXCUTER_IS_BUSY;
  105. }
  106. else//返回二级故障,可以封装一条答复信息, 返回错误码
  107. {
  108. message::Ground_detect_request_msg t_ground_detect_request_msg;
  109. //针对消息类型, 对消息进行二次解析
  110. if (t_ground_detect_request_msg.ParseFromString(p_msg->get_message_buf()))
  111. {
  112. //创建一条答复消息
  113. message::Ground_detect_response_msg t_ground_detect_response_msg;
  114. t_ground_detect_response_msg.mutable_base_info()->set_msg_type(message::Message_type::eGround_detect_response_msg);
  115. t_ground_detect_response_msg.mutable_base_info()->set_timeout_ms(5000);
  116. t_ground_detect_response_msg.mutable_base_info()->set_sender(message::Communicator::eGround_measurer);
  117. t_ground_detect_response_msg.mutable_base_info()->set_receiver(message::Communicator::eMain);
  118. t_ground_detect_response_msg.set_command_key(t_ground_detect_request_msg.command_key());
  119. t_ground_detect_response_msg.mutable_id_struct()->set_terminal_id(t_ground_detect_request_msg.id_struct().terminal_id());
  120. t_ground_detect_response_msg.mutable_error_manager()->set_error_code(t_error.get_error_code());
  121. t_ground_detect_response_msg.mutable_error_manager()->set_error_level((message::Error_level)t_error.get_error_level());
  122. t_ground_detect_response_msg.mutable_error_manager()->set_error_description(t_error.get_error_description());
  123. std::string t_msg = t_ground_detect_response_msg.SerializeAsString();
  124. System_communication::get_instance_references().encapsulate_msg(t_msg);
  125. LOG(INFO) << " System_executor::check_executer executer status error "<< this;
  126. return t_error;
  127. }
  128. else
  129. {
  130. LOG(INFO) << " System_executor::check_executer second PARSE_ERROR "<< this;
  131. return Error_manager(Error_code::SYSTEM_EXECUTOR_PARSE_ERROR, Error_level::MINOR_ERROR,
  132. " message::Measure_request_msg ParseFromString error ");
  133. }
  134. }
  135. }
  136. break;
  137. }
  138. default :
  139. ;
  140. break;
  141. }
  142. return t_error;
  143. }
  144. //处理消息的执行函数
  145. Error_manager System_executor::execute_msg(Communication_message* p_msg)
  146. {
  147. if ( p_msg == NULL )
  148. {
  149. return Error_manager(Error_code::POINTER_IS_NULL, Error_level::MINOR_ERROR,
  150. " POINTER IS NULL ");
  151. }
  152. // std::cout << " huli test ::333333333333333333333333333333:: " << " p_msg->get_message_type() = " << p_msg->get_message_type() << std::endl;
  153. switch ( p_msg->get_message_type() )
  154. {
  155. case Communication_message::eGround_detect_request_msg:
  156. {
  157. message::Ground_detect_request_msg t_ground_detect_request_msg;
  158. //针对消息类型, 对消息进行二次解析
  159. if (t_ground_detect_request_msg.ParseFromString(p_msg->get_message_buf()) && t_ground_detect_request_msg.id_struct().has_terminal_id())
  160. {
  161. //往线程池添加执行任务, 之后会唤醒一个线程去执行他.
  162. m_thread_pool.enqueue(&System_executor::execute_for_measure, this,
  163. t_ground_detect_request_msg.command_key(), t_ground_detect_request_msg.id_struct().terminal_id(),
  164. p_msg->get_receive_time());
  165. }
  166. else
  167. {
  168. return Error_manager(Error_code::SYSTEM_EXECUTOR_PARSE_ERROR, Error_level::MINOR_ERROR,
  169. " message::Measure_request_msg ParseFromString error ");
  170. }
  171. break;
  172. }
  173. default:
  174. {
  175. }
  176. }
  177. return Error_code::SUCCESS;
  178. }
  179. //检查状态
  180. Error_manager System_executor::check_status()
  181. {
  182. if ( m_system_executor_status == SYSTEM_EXECUTOR_READY )
  183. {
  184. if ( m_thread_pool.thread_is_full_load() == false )
  185. {
  186. return Error_code::SUCCESS;
  187. }
  188. else
  189. {
  190. return Error_manager(Error_code::SYSTEM_EXECUTOR_STATUS_BUSY, Error_level::NEGLIGIBLE_ERROR,
  191. " System_executor::check_status error ");
  192. }
  193. }
  194. else
  195. {
  196. return Error_manager(Error_code::SYSTEM_EXECUTOR_STATUS_ERROR, Error_level::MINOR_ERROR,
  197. " System_executor::check_status error ");
  198. }
  199. }
  200. //定时发送状态信息
  201. Error_manager System_executor::encapsulate_send_status()
  202. {
  203. static int cloud_count=-1;
  204. cloud_count++;
  205. // if(!g_debug_file.is_open())
  206. // {
  207. // g_debug_file.open("./filter_debug_result.txt", std::ios::app);
  208. // }
  209. Error_manager t_error;
  210. //创建一条状态消息
  211. message::Ground_status_msg t_ground_status_msg;
  212. t_ground_status_msg.mutable_base_info()->set_msg_type(message::Message_type::eGround_status_msg);
  213. t_ground_status_msg.mutable_base_info()->set_timeout_ms(5000);
  214. t_ground_status_msg.mutable_base_info()->set_sender(message::Communicator::eGround_measurer);
  215. t_ground_status_msg.mutable_base_info()->set_receiver(message::Communicator::eEmpty);
  216. // t_ground_status_msg.set_terminal_id(m_terminal_id);
  217. t_ground_status_msg.mutable_id_struct()->set_terminal_id(m_terminal_id);
  218. // 创建各区域状态消息,
  219. // 注意!!!目前公共消息名字依旧使用wj,不做修改
  220. // manager
  221. if (WJ_VELO == 1)
  222. {
  223. Velodyne_manager::Velodyne_manager_status t_velodyne_manager_status = Velodyne_manager::get_instance_references().get_status();
  224. t_ground_status_msg.set_wanji_manager_status((message::Wanji_manager_status)t_velodyne_manager_status);
  225. // lidar
  226. std::map<int, Velodyne_lidar_device *> t_velodyne_lidar_device_map = Velodyne_manager::get_instance_references().get_velodyne_lidar_device_map();
  227. std::map<int, Velodyne_lidar_device::Velodyne_lidar_device_status> t_velodyne_lidar_status_map;
  228. for (auto iter = t_velodyne_lidar_device_map.begin(); iter != t_velodyne_lidar_device_map.end(); ++iter)
  229. {
  230. t_velodyne_lidar_status_map.emplace(std::pair<int, Velodyne_lidar_device::Velodyne_lidar_device_status>(iter->second->get_lidar_id(), iter->second->get_status()));
  231. }
  232. // region
  233. std::map<int, Ground_region *> t_ground_region_map = Velodyne_manager::get_instance_references().get_ground_region_map();
  234. int region_index = 0;
  235. for (auto iter = t_ground_region_map.begin(); iter != t_ground_region_map.end(); ++iter)
  236. {
  237. // 以t_ground_status_msg为模板创建各区域心跳消息
  238. message::Ground_status_msg t_multi_status_msg;
  239. t_multi_status_msg.CopyFrom(t_ground_status_msg);
  240. t_multi_status_msg.mutable_id_struct()->set_terminal_id(iter->second->get_terminal_id());
  241. t_multi_status_msg.set_region_worker_status((message::Region_worker_status)(iter->second->get_status()));
  242. velodyne::Region t_param = iter->second->get_param();
  243. for (size_t j = 0; j < t_param.lidar_exts_size(); j++)
  244. {
  245. std::map<int, Velodyne_lidar_device::Velodyne_lidar_device_status>::iterator t_status_iter = t_velodyne_lidar_status_map.find(t_param.lidar_exts(j).lidar_id());
  246. if (t_status_iter == t_velodyne_lidar_status_map.end())
  247. {
  248. LOG(WARNING) << "lidar status " << t_param.lidar_exts(j).lidar_id() << " cannot be found, param error";
  249. }
  250. else
  251. {
  252. t_multi_status_msg.add_wanji_lidar_device_status((message::Wanji_lidar_device_status)t_status_iter->second);
  253. }
  254. }
  255. //velodyne雷达的自动定位信息
  256. Common_data::Car_wheel_information t_car_wheel_information;
  257. t_error = (*iter).second->get_last_wheel_information(&t_car_wheel_information, std::chrono::system_clock::now());
  258. // 获取区域点云填入信息
  259. pcl::PointCloud<pcl::PointXYZ>::Ptr t_region_cloud = pcl::PointCloud<pcl::PointXYZ>::Ptr(new pcl::PointCloud<pcl::PointXYZ>);
  260. iter->second->get_region_cloud(t_region_cloud, Ground_region::Region_cloud_type::filtered);
  261. if (cloud_count == 5)
  262. {
  263. std::string t_filename = std::string("region_") + std::to_string(iter->first) + "_cloud.txt";
  264. save_cloud_txt(t_region_cloud, t_filename);
  265. LOG(INFO) << "region " << iter->first << " cloud has been saved in " + t_filename;
  266. }
  267. // for (size_t j = 0; j < t_region_cloud->size(); j++)
  268. // {
  269. // message::Cloud_coordinate *tp_cloud = t_multi_status_msg.add_cloud();
  270. // tp_cloud->set_x(t_region_cloud->points[j].x);
  271. // tp_cloud->set_y(t_region_cloud->points[j].y);
  272. // tp_cloud->set_z(t_region_cloud->points[j].z);
  273. // }
  274. //无论定位结果如何, 都要填充数据, 即使全部写0, 必须保证通信格式正确.
  275. message::Locate_information t_locate_information;
  276. //= t_multi_status_msg.add_locate_information_realtime();
  277. t_locate_information.set_locate_x(t_car_wheel_information.car_center_x);
  278. t_locate_information.set_locate_y(t_car_wheel_information.car_center_y);
  279. t_locate_information.set_locate_angle(t_car_wheel_information.car_angle);
  280. t_locate_information.set_locate_length(0);
  281. t_locate_information.set_locate_width(0);
  282. t_locate_information.set_locate_height(0);
  283. t_locate_information.set_locate_wheel_base(t_car_wheel_information.car_wheel_base);
  284. t_locate_information.set_locate_wheel_width(t_car_wheel_information.car_wheel_width);
  285. t_locate_information.set_locate_front_theta(t_car_wheel_information.car_front_theta);
  286. t_locate_information.set_locate_correct(t_car_wheel_information.correctness);
  287. t_locate_information.set_uniformed_car_x(t_car_wheel_information.uniform_car_x);
  288. t_locate_information.set_uniformed_car_y(t_car_wheel_information.uniform_car_y);
  289. t_multi_status_msg.mutable_locate_information_realtime()->CopyFrom(t_locate_information);
  290. // 当前超界提示仅保留一项
  291. // 20211026已修改,分离为电子围栏状态与超界状态,只有测量正确时超界状态才有意义
  292. t_multi_status_msg.set_border_status(t_car_wheel_information.range_status);
  293. if (!t_car_wheel_information.correctness)
  294. {
  295. if (t_region_cloud->size() > 0)
  296. t_multi_status_msg.set_ground_status(message::Ground_statu::Noise);
  297. else
  298. t_multi_status_msg.set_ground_status(message::Ground_statu::Nothing);
  299. }
  300. else if (t_car_wheel_information.range_status == int(Ground_region::Range_status::Range_correct))
  301. {
  302. t_multi_status_msg.set_ground_status(message::Ground_statu::Car_correct);
  303. }
  304. else
  305. {
  306. t_multi_status_msg.set_ground_status(message::Ground_statu::Car_border_reached);
  307. // 更新待提示错误信息
  308. std::string t_error_str;
  309. if (t_car_wheel_information.range_status & Ground_region::Range_status::Range_front != 0)
  310. {
  311. t_error_str.append("前超界 ");
  312. }
  313. if (t_car_wheel_information.range_status & Ground_region::Range_status::Range_back != 0)
  314. {
  315. t_error_str.append("后超界 ");
  316. }
  317. if (t_car_wheel_information.range_status & Ground_region::Range_status::Range_left != 0)
  318. {
  319. t_error_str.append("左超界 ");
  320. }
  321. if (t_car_wheel_information.range_status & Ground_region::Range_status::Range_right != 0)
  322. {
  323. t_error_str.append("右超界 ");
  324. }
  325. if (t_car_wheel_information.range_status & Ground_region::Range_status::Range_bottom != 0)
  326. {
  327. t_error_str.append("底盘超界 ");
  328. }
  329. if (t_car_wheel_information.range_status & Ground_region::Range_status::Range_top != 0)
  330. {
  331. t_error_str.append("顶超界 ");
  332. }
  333. if (t_car_wheel_information.range_status & Ground_region::Range_status::Range_car_width != 0)
  334. {
  335. t_error_str.append("车宽超界 ");
  336. }
  337. if (t_car_wheel_information.range_status & Ground_region::Range_status::Range_car_wheelbase != 0)
  338. {
  339. t_error_str.append("轴距超界 ");
  340. }
  341. if (t_car_wheel_information.range_status & Ground_region::Range_status::Range_angle_anti_clock != 0)
  342. {
  343. t_error_str.append("车辆角度左超界 ");
  344. }
  345. if (t_car_wheel_information.range_status & Ground_region::Range_status::Range_angle_clock != 0)
  346. {
  347. t_error_str.append("车辆角度右超界 ");
  348. }
  349. if (t_car_wheel_information.range_status & Ground_region::Range_status::Range_steering_wheel_nozero != 0)
  350. {
  351. t_error_str.append("车辆前轮角超界 ");
  352. }
  353. t_error.set_error_description(t_error_str);
  354. }
  355. t_multi_status_msg.mutable_error_manager()->set_error_code(t_error.get_error_code());
  356. t_multi_status_msg.mutable_error_manager()->set_error_level((message::Error_level)t_error.get_error_level());
  357. t_multi_status_msg.mutable_error_manager()->set_error_description(t_error.get_error_description());
  358. std::string t_msg = t_multi_status_msg.SerializeAsString();
  359. System_communication::get_instance_references().encapsulate_msg(t_msg);
  360. if (t_multi_status_msg.id_struct().terminal_id() == DISP_TERM_ID)
  361. std::cout << t_multi_status_msg.DebugString() << std::endl
  362. << std::endl;
  363. }
  364. }
  365. else if (WJ_VELO == 0)
  366. {
  367. // wj_support 20220718
  368. Wanji_manager::Wanji_manager_status t_wj_manager_status = Wanji_manager::get_instance_references().get_status();
  369. t_ground_status_msg.set_wanji_manager_status((message::Wanji_manager_status)t_wj_manager_status);
  370. // lidar
  371. std::map<int, Wanji_lidar_device *> t_wj_lidar_device_map = Wanji_manager::get_instance_references().get_wanji_lidar_device_map();
  372. std::map<int, Wanji_lidar_device::Wanji_lidar_device_status> t_wj_lidar_status_map;
  373. for (auto iter = t_wj_lidar_device_map.begin(); iter != t_wj_lidar_device_map.end(); ++iter)
  374. {
  375. t_wj_lidar_status_map.emplace(std::pair<int, Wanji_lidar_device::Wanji_lidar_device_status>(iter->second->get_lidar_id(), iter->second->get_status()));
  376. }
  377. // region
  378. std::map<int, Region_worker *> t_region_worker_map = Wanji_manager::get_instance_references().get_region_worker_map();
  379. int region_index = 0;
  380. for (auto iter = t_region_worker_map.begin(); iter != t_region_worker_map.end(); ++iter)
  381. {
  382. // 以t_ground_status_msg为模板创建各区域心跳消息
  383. message::Ground_status_msg t_multi_status_msg;
  384. t_multi_status_msg.CopyFrom(t_ground_status_msg);
  385. t_multi_status_msg.mutable_id_struct()->set_terminal_id(iter->second->get_terminal_id());
  386. t_multi_status_msg.set_region_worker_status((message::Region_worker_status)(iter->second->get_status()));
  387. wj::Region t_param = iter->second->get_param();
  388. for (size_t j = 0; j < t_param.lidar_exts_size(); j++)
  389. {
  390. std::map<int, Wanji_lidar_device::Wanji_lidar_device_status>::iterator t_status_iter = t_wj_lidar_status_map.find(t_param.lidar_exts(j).lidar_id());
  391. if (t_status_iter == t_wj_lidar_status_map.end())
  392. {
  393. LOG(WARNING) << "lidar status " << t_param.lidar_exts(j).lidar_id() << " cannot be found, param error";
  394. }
  395. else
  396. {
  397. t_multi_status_msg.add_wanji_lidar_device_status((message::Wanji_lidar_device_status)t_status_iter->second);
  398. }
  399. }
  400. //velodyne雷达的自动定位信息
  401. Common_data::Car_wheel_information t_car_wheel_information;
  402. t_error = (*iter).second->get_last_wheel_information(&t_car_wheel_information, std::chrono::system_clock::now());
  403. // 获取区域点云填入信息
  404. pcl::PointCloud<pcl::PointXYZ>::Ptr t_region_cloud = pcl::PointCloud<pcl::PointXYZ>::Ptr(new pcl::PointCloud<pcl::PointXYZ>);
  405. iter->second->get_region_cloud(t_region_cloud);
  406. if (cloud_count == 5)
  407. {
  408. std::string t_filename = std::string("region_") + std::to_string(iter->first) + "_cloud.txt";
  409. save_cloud_txt(t_region_cloud, t_filename);
  410. LOG(INFO) << "region " << iter->first << " cloud has been saved in " + t_filename;
  411. }
  412. // for (size_t j = 0; j < t_region_cloud->size(); j++)
  413. // {
  414. // message::Cloud_coordinate *tp_cloud = t_multi_status_msg.add_cloud();
  415. // tp_cloud->set_x(t_region_cloud->points[j].x);
  416. // tp_cloud->set_y(t_region_cloud->points[j].y);
  417. // tp_cloud->set_z(t_region_cloud->points[j].z);
  418. // }
  419. //无论定位结果如何, 都要填充数据, 即使全部写0, 必须保证通信格式正确.
  420. message::Locate_information t_locate_information;
  421. //= t_multi_status_msg.add_locate_information_realtime();
  422. t_locate_information.set_locate_x(t_car_wheel_information.car_center_x);
  423. t_locate_information.set_locate_y(t_car_wheel_information.car_center_y);
  424. t_locate_information.set_locate_angle(t_car_wheel_information.car_angle);
  425. t_locate_information.set_locate_length(0);
  426. t_locate_information.set_locate_width(0);
  427. t_locate_information.set_locate_height(0);
  428. t_locate_information.set_locate_wheel_base(t_car_wheel_information.car_wheel_base);
  429. t_locate_information.set_locate_wheel_width(t_car_wheel_information.car_wheel_width);
  430. t_locate_information.set_locate_front_theta(t_car_wheel_information.car_front_theta);
  431. t_locate_information.set_locate_correct(t_car_wheel_information.correctness);
  432. t_locate_information.set_uniformed_car_x(t_car_wheel_information.uniform_car_x);
  433. t_locate_information.set_uniformed_car_y(t_car_wheel_information.uniform_car_y);
  434. t_multi_status_msg.mutable_locate_information_realtime()->CopyFrom(t_locate_information);
  435. // 当前超界提示仅保留一项
  436. // 20211026已修改,分离为电子围栏状态与超界状态,只有测量正确时超界状态才有意义
  437. t_multi_status_msg.set_border_status(t_car_wheel_information.range_status);
  438. if (!t_car_wheel_information.correctness)
  439. {
  440. if (t_region_cloud->size() > 0)
  441. t_multi_status_msg.set_ground_status(message::Ground_statu::Noise);
  442. else
  443. t_multi_status_msg.set_ground_status(message::Ground_statu::Nothing);
  444. }
  445. else if (t_car_wheel_information.range_status == int(Ground_region::Range_status::Range_correct))
  446. {
  447. t_multi_status_msg.set_ground_status(message::Ground_statu::Car_correct);
  448. }
  449. else
  450. {
  451. t_multi_status_msg.set_ground_status(message::Ground_statu::Car_border_reached);
  452. // 更新待提示错误信息
  453. std::string t_error_str;
  454. if (t_car_wheel_information.range_status & Ground_region::Range_status::Range_front != 0)
  455. {
  456. t_error_str.append("前超界 ");
  457. }
  458. if (t_car_wheel_information.range_status & Ground_region::Range_status::Range_back != 0)
  459. {
  460. t_error_str.append("后超界 ");
  461. }
  462. if (t_car_wheel_information.range_status & Ground_region::Range_status::Range_left != 0)
  463. {
  464. t_error_str.append("左超界 ");
  465. }
  466. if (t_car_wheel_information.range_status & Ground_region::Range_status::Range_right != 0)
  467. {
  468. t_error_str.append("右超界 ");
  469. }
  470. if (t_car_wheel_information.range_status & Ground_region::Range_status::Range_bottom != 0)
  471. {
  472. t_error_str.append("底盘超界 ");
  473. }
  474. if (t_car_wheel_information.range_status & Ground_region::Range_status::Range_top != 0)
  475. {
  476. t_error_str.append("顶超界 ");
  477. }
  478. if (t_car_wheel_information.range_status & Ground_region::Range_status::Range_car_width != 0)
  479. {
  480. t_error_str.append("车宽超界 ");
  481. }
  482. if (t_car_wheel_information.range_status & Ground_region::Range_status::Range_car_wheelbase != 0)
  483. {
  484. t_error_str.append("轴距超界 ");
  485. }
  486. if (t_car_wheel_information.range_status & Ground_region::Range_status::Range_angle_anti_clock != 0)
  487. {
  488. t_error_str.append("车辆角度左超界 ");
  489. }
  490. if (t_car_wheel_information.range_status & Ground_region::Range_status::Range_angle_clock != 0)
  491. {
  492. t_error_str.append("车辆角度右超界 ");
  493. }
  494. if (t_car_wheel_information.range_status & Ground_region::Range_status::Range_steering_wheel_nozero != 0)
  495. {
  496. t_error_str.append("车辆前轮角超界 ");
  497. }
  498. t_error.set_error_description(t_error_str);
  499. }
  500. t_multi_status_msg.mutable_error_manager()->set_error_code(t_error.get_error_code());
  501. t_multi_status_msg.mutable_error_manager()->set_error_level((message::Error_level)t_error.get_error_level());
  502. t_multi_status_msg.mutable_error_manager()->set_error_description(t_error.get_error_description());
  503. std::string t_msg = t_multi_status_msg.SerializeAsString();
  504. System_communication::get_instance_references().encapsulate_msg(t_msg);
  505. if (t_multi_status_msg.id_struct().terminal_id() == DISP_TERM_ID)
  506. LOG(INFO) << t_multi_status_msg.DebugString();
  507. }
  508. }
  509. // std::cout << " huli test :::: " << " t_ground_status_msg.DebugString() = " << std::endl;
  510. // std::cout << t_ground_status_msg.DebugString() << std::endl;
  511. // std::cout << " huli test :::: " << " t_ground_status_msg.DebugString() = " << t_ground_status_msg.DebugString() << std::endl;
  512. return Error_code::SUCCESS;
  513. }
  514. //判断是否为待机,如果已经准备好,则可以执行任务。
  515. bool System_executor::is_ready()
  516. {
  517. if (m_system_executor_status == SYSTEM_EXECUTOR_READY && m_thread_pool.thread_is_full_load() == false)
  518. {
  519. return true;
  520. }
  521. else
  522. {
  523. return false;
  524. }
  525. }
  526. System_executor::System_executor_status System_executor::get_system_executor_status()
  527. {
  528. return m_system_executor_status;
  529. }
  530. int System_executor::get_terminal_id()
  531. {
  532. return m_terminal_id;
  533. }
  534. //雷达感测定位 的处理函数
  535. //input::command_id, 消息指令id, 由主控制系统生成的唯一码
  536. //input::command_id, 终端id, 对应具体的某个车位
  537. //return::void, 没有返回, 执行结果直接生成一条答复消息, 然后通过通信返回
  538. void System_executor::execute_for_measure(std::string command_info, int terminal_id, std::chrono::system_clock::time_point receive_time)
  539. {
  540. Error_manager t_error;
  541. Error_manager t_result;
  542. LOG(INFO) << " System_executor::execute_for_measure run " << this;
  543. //第1步, 按照时间生成中间文件的保存路径
  544. time_t nowTime;
  545. nowTime = time(NULL);
  546. struct tm *sysTime = localtime(&nowTime);
  547. char t_save_path[256] = {0};
  548. sprintf(t_save_path, "../data/%04d%02d%02d_%02d%02d%02d",
  549. sysTime->tm_year + 1900, sysTime->tm_mon + 1, sysTime->tm_mday, sysTime->tm_hour, sysTime->tm_min, sysTime->tm_sec);
  550. //第2步, 创建万集管理模块的任务单, 并发送到 wanji_manager
  551. // Wanji_manager_task t_wanji_manager_task ;
  552. // Common_data::Car_wheel_information t_car_information_by_wanji;
  553. // t_wanji_manager_task.task_init(TASK_CREATED,"",NULL,std::chrono::milliseconds(3000),
  554. // terminal_id, receive_time);
  555. // t_error = Task_command_manager::get_instance_references().execute_task(&t_wanji_manager_task);
  556. // 或者创建velodyne管理模块任务单,并发送到velodyne_manager
  557. Velodyne_manager_task t_velodyne_manager_task;
  558. Common_data::Car_wheel_information t_car_information_by_velodyne;
  559. t_velodyne_manager_task.task_init(TASK_CREATED, "", NULL, std::chrono::milliseconds(3000),
  560. terminal_id, receive_time);
  561. t_error = Task_command_manager::get_instance_references().execute_task(&t_velodyne_manager_task);
  562. //第3步, 等待任务单完成
  563. if (t_error != Error_code::SUCCESS)
  564. {
  565. LOG(INFO) << " Velodyne_manager/Velodyne_manager execute_task error " << this;
  566. }
  567. else
  568. {
  569. // //判断任务是否结束, 阻塞 等待, 直到任务完成或者超时
  570. // while ( t_wanji_manager_task.is_task_end() == false)
  571. // {
  572. // if ( t_wanji_manager_task.is_over_time() )
  573. // {
  574. // //超时处理。取消任务。
  575. // Wanji_manager::get_instance_pointer()->cancel_task(&t_wanji_manager_task);
  576. // t_error.error_manager_reset(Error_code::WJ_MANAGER_TASK_OVER_TIME, Error_level::MINOR_ERROR,
  577. // " t_wanjestaui_manager_task is_over_time ");
  578. // t_wanji_manager_task.set_task_error_manager(t_error);
  579. // }
  580. // else
  581. // {
  582. // //继续等待
  583. // std::this_thread::sleep_for(std::chrono::microseconds(1));
  584. // std::this_thread::yield();
  585. // }
  586. // }
  587. // //提取任务单 的错误码
  588. // t_error = t_wanji_manager_task.get_task_error_manager();
  589. // if ( t_error == Error_code::SUCCESS )
  590. // {
  591. // t_car_information_by_wanji = t_wanji_manager_task.get_car_wheel_information();
  592. // }
  593. // else
  594. // {
  595. // LOG(INFO) << " wanji_manager_task error :::::::" << t_wanji_manager_task.get_task_error_manager().to_string() << this;
  596. // }
  597. // ------------------- 切换为velodyne ------------------
  598. //判断任务是否结束, 阻塞 等待, 直到任务完成或者超时
  599. while (t_velodyne_manager_task.is_task_end() == false)
  600. {
  601. if (t_velodyne_manager_task.is_over_time())
  602. {
  603. //超时处理。取消任务。
  604. Velodyne_manager::get_instance_pointer()->cancel_task(&t_velodyne_manager_task);
  605. t_error.error_manager_reset(Error_code::VELODYNE_MANAGER_TASK_OVER_TIME, Error_level::MINOR_ERROR,
  606. " t_velodyne_manager_task is_over_time ");
  607. t_velodyne_manager_task.set_task_error_manager(t_error);
  608. }
  609. else
  610. {
  611. //继续等待
  612. std::this_thread::sleep_for(std::chrono::microseconds(1));
  613. std::this_thread::yield();
  614. }
  615. }
  616. //提取任务单 的错误码
  617. t_error = t_velodyne_manager_task.get_task_error_manager();
  618. if (t_error == Error_code::SUCCESS)
  619. {
  620. t_car_information_by_velodyne = t_velodyne_manager_task.get_car_wheel_information();
  621. }
  622. else
  623. {
  624. LOG(INFO) << " velodyne_manager_task error :::::::" << t_velodyne_manager_task.get_task_error_manager().to_string() << this;
  625. }
  626. }
  627. t_result.compare_and_cover_error(t_error);
  628. //measure 测量模块的最终结果
  629. Common_data::Car_measure_information t_car_information_result;
  630. //第4步, 生成反馈消息
  631. // if(t_car_information_by_wanji.correctness == true )
  632. // {
  633. // t_car_information_result.center_x = t_car_information_by_wanji.center_x;
  634. // t_car_information_result.center_y = t_car_information_by_wanji.center_y;
  635. // t_car_information_result.car_angle = t_car_information_by_wanji.car_angle;
  636. // t_car_information_result.wheel_base = t_car_information_by_wanji.wheel_base;
  637. // t_car_information_result.wheel_width = t_car_information_by_wanji.wheel_width;
  638. // t_car_information_result.correctness = true;
  639. // }else{
  640. // t_result.set_error_level_down(NEGLIGIBLE_ERROR);
  641. // }
  642. // ------------------- 切换为velodyne ------------------
  643. if (t_car_information_by_velodyne.correctness == true)
  644. {
  645. t_car_information_result.car_center_x = t_car_information_by_velodyne.car_center_x;
  646. t_car_information_result.car_center_y = t_car_information_by_velodyne.car_center_y;
  647. t_car_information_result.car_angle = t_car_information_by_velodyne.car_angle;
  648. t_car_information_result.car_wheel_base = t_car_information_by_velodyne.car_wheel_base;
  649. t_car_information_result.car_wheel_width = t_car_information_by_velodyne.car_wheel_width;
  650. t_car_information_result.correctness = true;
  651. }
  652. else
  653. {
  654. t_result.set_error_level_down(NEGLIGIBLE_ERROR);
  655. }
  656. //第七步, 创建一条答复消息
  657. message::Ground_detect_response_msg t_ground_detect_response_msg;
  658. t_ground_detect_response_msg.mutable_base_info()->set_msg_type(message::Message_type::eGround_detect_response_msg);
  659. t_ground_detect_response_msg.mutable_base_info()->set_timeout_ms(5000);
  660. t_ground_detect_response_msg.mutable_base_info()->set_sender(message::Communicator::eGround_measurer);
  661. t_ground_detect_response_msg.mutable_base_info()->set_receiver(message::Communicator::eMain);
  662. t_ground_detect_response_msg.set_command_key(command_info);
  663. t_ground_detect_response_msg.mutable_id_struct()->set_terminal_id(terminal_id);
  664. t_ground_detect_response_msg.mutable_error_manager()->set_error_code(t_result.get_error_code());
  665. t_ground_detect_response_msg.mutable_error_manager()->set_error_level((message::Error_level)t_result.get_error_level());
  666. t_ground_detect_response_msg.mutable_error_manager()->set_error_description(t_result.get_error_description());
  667. t_ground_detect_response_msg.mutable_locate_information()->set_locate_x(t_car_information_result.car_center_x);
  668. t_ground_detect_response_msg.mutable_locate_information()->set_locate_y(t_car_information_result.car_center_y);
  669. t_ground_detect_response_msg.mutable_locate_information()->set_locate_angle(t_car_information_result.car_angle);
  670. t_ground_detect_response_msg.mutable_locate_information()->set_locate_length(t_car_information_result.car_length);
  671. t_ground_detect_response_msg.mutable_locate_information()->set_locate_width(t_car_information_result.car_width);
  672. t_ground_detect_response_msg.mutable_locate_information()->set_locate_height(t_car_information_result.car_height);
  673. t_ground_detect_response_msg.mutable_locate_information()->set_locate_wheel_base(t_car_information_result.car_wheel_base);
  674. t_ground_detect_response_msg.mutable_locate_information()->set_locate_wheel_width(t_car_information_result.car_wheel_width);
  675. t_ground_detect_response_msg.mutable_locate_information()->set_locate_front_theta(t_car_information_result.car_front_theta);
  676. t_ground_detect_response_msg.mutable_locate_information()->set_locate_correct(t_car_information_result.correctness);
  677. std::string t_msg = t_ground_detect_response_msg.SerializeAsString();
  678. System_communication::get_instance_references().encapsulate_msg(t_msg);
  679. std::cout << "huli t_measure_response_msg = " << t_ground_detect_response_msg.DebugString() << std::endl;
  680. return;
  681. }