system_executor.cpp 54 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211
  1. //
  2. // Created by huli on 2020/7/2.
  3. //
  4. #include "system_executor.h"
  5. // std::ofstream g_debug_file; // 用于测试滤波功能
  6. System_executor::System_executor()
  7. {
  8. }
  9. System_executor::~System_executor()
  10. {
  11. system_executor_uninit();
  12. }
  13. //初始化
  14. Error_manager System_executor::system_executor_init(int threads_size, int terminal_id)
  15. {
  16. m_thread_pool.thread_pool_init(threads_size);
  17. m_system_executor_status = SYSTEM_EXECUTOR_READY;
  18. m_terminal_id = terminal_id;
  19. return Error_code::SUCCESS;
  20. }
  21. //反初始化
  22. Error_manager System_executor::system_executor_uninit()
  23. {
  24. // if(g_debug_file.is_open())
  25. // g_debug_file.close();
  26. m_thread_pool.thread_pool_uninit();
  27. m_system_executor_status = SYSTEM_EXECUTOR_UNKNOW;
  28. return Error_code::SUCCESS;
  29. }
  30. //检查消息是否有效, 主要检查消息类型和接受者, 判断这条消息是不是给我的.
  31. Error_manager System_executor::check_msg(Communication_message* p_msg)
  32. {
  33. if ( p_msg == NULL )
  34. {
  35. return Error_manager(Error_code::POINTER_IS_NULL, Error_level::MINOR_ERROR,
  36. " POINTER IS NULL ");
  37. }
  38. //检查消息类型
  39. switch ( p_msg->get_message_type() )
  40. {
  41. case Communication_message::Message_type::eGround_detect_request_msg:
  42. {
  43. //检查接受人
  44. if ( p_msg->get_receiver() == Communication_message::Communicator::eGround_measurer)
  45. {
  46. message::Ground_detect_request_msg t_ground_detect_request_msg;
  47. //针对消息类型, 对消息进行二次解析
  48. if (t_ground_detect_request_msg.ParseFromString(p_msg->get_message_buf()))
  49. {
  50. // //检查终端id
  51. // 普爱不检查终端id
  52. // 楚天检查终端id
  53. // if ( t_ground_detect_request_msg.terminal_id() == m_terminal_id )
  54. // {
  55. return Error_code::SUCCESS;
  56. // }
  57. }
  58. }
  59. break;
  60. }
  61. default :
  62. ;
  63. break;
  64. }
  65. //无效的消息,
  66. return Error_manager(Error_code::INVALID_MESSAGE, Error_level::NEGLIGIBLE_ERROR,
  67. " INVALID_MESSAGE error ");
  68. }
  69. //检查执行者的状态, 判断能否处理这条消息,
  70. Error_manager System_executor::check_executer(Communication_message* p_msg)
  71. {
  72. //huli DDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDD 记得删除
  73. return Error_code::SUCCESS;
  74. if ( p_msg == NULL )
  75. {
  76. return Error_manager(Error_code::POINTER_IS_NULL, Error_level::MINOR_ERROR,
  77. " POINTER IS NULL ");
  78. }
  79. Error_manager t_error;
  80. //通过 p_msg->get_message_type() 和 p_msg->get_receiver() 找到处理模块的实例对象, 查询执行人是否可以处理这条消息
  81. switch ( p_msg->get_message_type() )
  82. {
  83. case Communication_message::Message_type::eGround_detect_request_msg:
  84. {
  85. Error_manager t_executor_result = System_executor::get_instance_references().check_status();
  86. if (t_executor_result == SUCCESS)
  87. {
  88. return Error_code::SUCCESS;
  89. }
  90. else
  91. {
  92. //整合所有的错误码
  93. t_error.compare_and_cover_error(t_executor_result);
  94. if (t_error.get_error_level() == NEGLIGIBLE_ERROR)//一级故障,轻微故障,
  95. {
  96. LOG(INFO) << "executer_is_busy , ";
  97. //返回繁忙之后, 通信模块1秒后再次调用check
  98. return Error_code::COMMUNICATION_EXCUTER_IS_BUSY;
  99. }
  100. else//返回二级故障,可以封装一条答复信息, 返回错误码
  101. {
  102. message::Ground_detect_request_msg t_ground_detect_request_msg;
  103. //针对消息类型, 对消息进行二次解析
  104. if (t_ground_detect_request_msg.ParseFromString(p_msg->get_message_buf()))
  105. {
  106. //创建一条答复消息
  107. message::Ground_detect_response_msg t_ground_detect_response_msg;
  108. t_ground_detect_response_msg.mutable_base_info()->set_msg_type(message::Message_type::eGround_detect_response_msg);
  109. t_ground_detect_response_msg.mutable_base_info()->set_timeout_ms(5000);
  110. t_ground_detect_response_msg.mutable_base_info()->set_sender(message::Communicator::eGround_measurer);
  111. t_ground_detect_response_msg.mutable_base_info()->set_receiver(message::Communicator::eMain);
  112. t_ground_detect_response_msg.set_command_key(t_ground_detect_request_msg.command_key());
  113. t_ground_detect_response_msg.mutable_id_struct()->set_terminal_id(t_ground_detect_request_msg.id_struct().terminal_id());
  114. t_ground_detect_response_msg.mutable_error_manager()->set_error_code(t_error.get_error_code());
  115. t_ground_detect_response_msg.mutable_error_manager()->set_error_level((message::Error_level)t_error.get_error_level());
  116. t_ground_detect_response_msg.mutable_error_manager()->set_error_description(t_error.get_error_description());
  117. std::string t_msg = t_ground_detect_response_msg.SerializeAsString();
  118. System_communication::get_instance_references().encapsulate_msg(t_msg);
  119. LOG(INFO) << " System_executor::check_executer executer status error "<< this;
  120. return t_error;
  121. }
  122. else
  123. {
  124. LOG(INFO) << " System_executor::check_executer second PARSE_ERROR "<< this;
  125. return Error_manager(Error_code::SYSTEM_EXECUTOR_PARSE_ERROR, Error_level::MINOR_ERROR,
  126. " message::Measure_request_msg ParseFromString error ");
  127. }
  128. }
  129. }
  130. break;
  131. }
  132. default :
  133. ;
  134. break;
  135. }
  136. return t_error;
  137. }
  138. //处理消息的执行函数
  139. Error_manager System_executor::execute_msg(Communication_message* p_msg)
  140. {
  141. if ( p_msg == NULL )
  142. {
  143. return Error_manager(Error_code::POINTER_IS_NULL, Error_level::MINOR_ERROR,
  144. " POINTER IS NULL ");
  145. }
  146. // std::cout << " huli test ::333333333333333333333333333333:: " << " p_msg->get_message_type() = " << p_msg->get_message_type() << std::endl;
  147. switch ( p_msg->get_message_type() )
  148. {
  149. case Communication_message::eGround_detect_request_msg:
  150. {
  151. message::Ground_detect_request_msg t_ground_detect_request_msg;
  152. //针对消息类型, 对消息进行二次解析
  153. if (t_ground_detect_request_msg.ParseFromString(p_msg->get_message_buf()) && t_ground_detect_request_msg.id_struct().has_terminal_id())
  154. {
  155. //往线程池添加执行任务, 之后会唤醒一个线程去执行他.
  156. m_thread_pool.enqueue(&System_executor::execute_for_measure, this,
  157. t_ground_detect_request_msg.command_key(), t_ground_detect_request_msg.id_struct().terminal_id(),
  158. p_msg->get_receive_time());
  159. }
  160. else
  161. {
  162. return Error_manager(Error_code::SYSTEM_EXECUTOR_PARSE_ERROR, Error_level::MINOR_ERROR,
  163. " message::Measure_request_msg ParseFromString error ");
  164. }
  165. break;
  166. }
  167. default:
  168. {
  169. }
  170. }
  171. return Error_code::SUCCESS;
  172. }
  173. //检查状态
  174. Error_manager System_executor::check_status()
  175. {
  176. if ( m_system_executor_status == SYSTEM_EXECUTOR_READY )
  177. {
  178. if ( m_thread_pool.thread_is_full_load() == false )
  179. {
  180. return Error_code::SUCCESS;
  181. }
  182. else
  183. {
  184. return Error_manager(Error_code::SYSTEM_EXECUTOR_STATUS_BUSY, Error_level::NEGLIGIBLE_ERROR,
  185. " System_executor::check_status error ");
  186. }
  187. }
  188. else
  189. {
  190. return Error_manager(Error_code::SYSTEM_EXECUTOR_STATUS_ERROR, Error_level::MINOR_ERROR,
  191. " System_executor::check_status error ");
  192. }
  193. }
  194. // 更新消息中关于定位结果与超界信息的内容
  195. bool System_executor::update_measure_info(message::Ground_status_msg &msg, Common_data::Car_wheel_information &measure_info, int cloud_size)
  196. {
  197. Error_manager t_error;
  198. //无论定位结果如何, 都要填充数据, 即使全部写0, 必须保证通信格式正确.
  199. message::Locate_information t_locate_information;
  200. //= t_multi_status_msg.add_locate_information_realtime();
  201. t_locate_information.set_locate_x(measure_info.car_center_x);
  202. t_locate_information.set_locate_y(measure_info.car_center_y);
  203. t_locate_information.set_locate_angle(measure_info.car_angle);
  204. t_locate_information.set_locate_length(0);
  205. t_locate_information.set_locate_width(0);
  206. t_locate_information.set_locate_height(0);
  207. t_locate_information.set_locate_wheel_base(measure_info.car_wheel_base);
  208. t_locate_information.set_locate_wheel_width(measure_info.car_wheel_width);
  209. t_locate_information.set_locate_front_theta(measure_info.car_front_theta);
  210. t_locate_information.set_locate_correct(measure_info.correctness);
  211. t_locate_information.set_uniformed_car_x(measure_info.uniform_car_x);
  212. t_locate_information.set_uniformed_car_y(measure_info.uniform_car_y);
  213. msg.mutable_locate_information_realtime()->CopyFrom(t_locate_information);
  214. // 当前超界提示仅保留一项
  215. // 20211026已修改,分离为电子围栏状态与超界状态,只有测量正确时超界状态才有意义
  216. msg.set_border_status(measure_info.range_status);
  217. if (!measure_info.correctness)
  218. {
  219. if (cloud_size > 0)
  220. msg.set_ground_status(message::Ground_statu::Noise);
  221. else
  222. msg.set_ground_status(message::Ground_statu::Nothing);
  223. }
  224. else if (measure_info.range_status == int(Ground_region::Range_status::Range_correct))
  225. {
  226. msg.set_ground_status(message::Ground_statu::Car_correct);
  227. }
  228. else
  229. {
  230. msg.set_ground_status(message::Ground_statu::Car_border_reached);
  231. // 更新待提示错误信息
  232. std::string t_error_str;
  233. if (measure_info.range_status & Ground_region::Range_status::Range_front != 0)
  234. {
  235. t_error_str.append("前超界 ");
  236. }
  237. if (measure_info.range_status & Ground_region::Range_status::Range_back != 0)
  238. {
  239. t_error_str.append("后超界 ");
  240. }
  241. if (measure_info.range_status & Ground_region::Range_status::Range_left != 0)
  242. {
  243. t_error_str.append("左超界 ");
  244. }
  245. if (measure_info.range_status & Ground_region::Range_status::Range_right != 0)
  246. {
  247. t_error_str.append("右超界 ");
  248. }
  249. if (measure_info.range_status & Ground_region::Range_status::Range_bottom != 0)
  250. {
  251. t_error_str.append("底盘超界 ");
  252. }
  253. if (measure_info.range_status & Ground_region::Range_status::Range_top != 0)
  254. {
  255. t_error_str.append("顶超界 ");
  256. }
  257. if (measure_info.range_status & Ground_region::Range_status::Range_car_width != 0)
  258. {
  259. t_error_str.append("车宽超界 ");
  260. }
  261. if (measure_info.range_status & Ground_region::Range_status::Range_car_wheelbase != 0)
  262. {
  263. t_error_str.append("轴距超界 ");
  264. }
  265. if (measure_info.range_status & Ground_region::Range_status::Range_angle_anti_clock != 0)
  266. {
  267. t_error_str.append("车辆角度左超界 ");
  268. }
  269. if (measure_info.range_status & Ground_region::Range_status::Range_angle_clock != 0)
  270. {
  271. t_error_str.append("车辆角度右超界 ");
  272. }
  273. if (measure_info.range_status & Ground_region::Range_status::Range_steering_wheel_nozero != 0)
  274. {
  275. t_error_str.append("车辆前轮角超界 ");
  276. }
  277. t_error.set_error_description(t_error_str);
  278. }
  279. msg.mutable_error_manager()->set_error_code(t_error.get_error_code());
  280. msg.mutable_error_manager()->set_error_level((message::Error_level)t_error.get_error_level());
  281. msg.mutable_error_manager()->set_error_description(t_error.get_error_description());
  282. return true;
  283. }
  284. // ***************************** rabbitmq *********************************
  285. Error_manager System_executor::encapsulate_send_mq_status()
  286. {
  287. static int cloud_count=-1;
  288. cloud_count++;
  289. // if(!g_debug_file.is_open())
  290. // {
  291. // g_debug_file.open("./filter_debug_result.txt", std::ios::app);
  292. // }
  293. Error_manager t_error;
  294. //创建一条状态消息
  295. message::Ground_status_msg t_ground_status_msg;
  296. t_ground_status_msg.mutable_base_info()->set_msg_type(message::Message_type::eGround_status_msg);
  297. t_ground_status_msg.mutable_base_info()->set_timeout_ms(5000);
  298. t_ground_status_msg.mutable_base_info()->set_sender(message::Communicator::eGround_measurer);
  299. t_ground_status_msg.mutable_base_info()->set_receiver(message::Communicator::eEmpty);
  300. // t_ground_status_msg.set_terminal_id(m_terminal_id);
  301. t_ground_status_msg.mutable_id_struct()->set_terminal_id(m_terminal_id);
  302. // 创建各区域状态消息,
  303. // 注意!!!目前公共消息名字依旧使用wj,不做修改
  304. // manager
  305. #if WJ_VELO == 1
  306. {
  307. Velodyne_manager::Velodyne_manager_status t_velodyne_manager_status = Velodyne_manager::get_instance_references().get_status();
  308. t_ground_status_msg.set_wanji_manager_status((message::Wanji_manager_status)t_velodyne_manager_status);
  309. // lidar
  310. std::map<int, Velodyne_lidar_device *> t_velodyne_lidar_device_map = Velodyne_manager::get_instance_references().get_velodyne_lidar_device_map();
  311. std::map<int, Velodyne_lidar_device::Velodyne_lidar_device_status> t_velodyne_lidar_status_map;
  312. for (auto iter = t_velodyne_lidar_device_map.begin(); iter != t_velodyne_lidar_device_map.end(); ++iter)
  313. {
  314. 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()));
  315. }
  316. // region
  317. std::map<int, Ground_region *> t_ground_region_map = Velodyne_manager::get_instance_references().get_ground_region_map();
  318. int region_index = 0;
  319. for (auto iter = t_ground_region_map.begin(); iter != t_ground_region_map.end(); ++iter)
  320. {
  321. // 以t_ground_status_msg为模板创建各区域心跳消息
  322. message::Ground_status_msg t_multi_status_msg;
  323. t_multi_status_msg.CopyFrom(t_ground_status_msg);
  324. t_multi_status_msg.mutable_id_struct()->set_terminal_id(iter->second->get_terminal_id());
  325. t_multi_status_msg.set_region_worker_status((message::Region_worker_status)(iter->second->get_status()));
  326. velodyne::Region t_param = iter->second->get_param();
  327. for (size_t j = 0; j < t_param.lidar_exts_size(); j++)
  328. {
  329. 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());
  330. if (t_status_iter == t_velodyne_lidar_status_map.end())
  331. {
  332. LOG(WARNING) << "lidar status " << t_param.lidar_exts(j).lidar_id() << " cannot be found, param error";
  333. }
  334. else
  335. {
  336. t_multi_status_msg.add_wanji_lidar_device_status((message::Wanji_lidar_device_status)t_status_iter->second);
  337. }
  338. }
  339. //velodyne雷达的自动定位信息
  340. Common_data::Car_wheel_information t_car_wheel_information;
  341. t_error = (*iter).second->get_last_wheel_information(&t_car_wheel_information, std::chrono::system_clock::now());
  342. // 获取区域点云填入信息
  343. pcl::PointCloud<pcl::PointXYZ>::Ptr t_region_cloud = pcl::PointCloud<pcl::PointXYZ>::Ptr(new pcl::PointCloud<pcl::PointXYZ>);
  344. iter->second->get_region_cloud(t_region_cloud, Ground_region::Region_cloud_type::filtered);
  345. if (cloud_count == 5)
  346. {
  347. std::string t_filename = std::string("region_") + std::to_string(iter->first) + "_cloud.txt";
  348. save_cloud_txt(t_region_cloud, t_filename);
  349. LOG(INFO) << "region " << iter->first << " cloud has been saved in " + t_filename;
  350. }
  351. // for (size_t j = 0; j < t_region_cloud->size(); j++)
  352. // {
  353. // message::Cloud_coordinate *tp_cloud = t_multi_status_msg.add_cloud();
  354. // tp_cloud->set_x(t_region_cloud->points[j].x);
  355. // tp_cloud->set_y(t_region_cloud->points[j].y);
  356. // tp_cloud->set_z(t_region_cloud->points[j].z);
  357. // }
  358. update_measure_info(t_multi_status_msg, t_car_wheel_information, t_region_cloud->size());
  359. std::string t_msg = t_multi_status_msg.SerializeAsString();
  360. System_communication::get_instance_references().encapsulate_msg(t_msg);
  361. if (t_multi_status_msg.id_struct().terminal_id() == DISP_TERM_ID)
  362. std::cout << t_multi_status_msg.DebugString() << std::endl
  363. << std::endl;
  364. }
  365. }
  366. #elif WJ_VELO == 0
  367. {
  368. // wj_support 20220718
  369. Wanji_manager::Wanji_manager_status t_wj_manager_status = Wanji_manager::get_instance_references().get_status();
  370. t_ground_status_msg.set_wanji_manager_status((message::Wanji_manager_status)t_wj_manager_status);
  371. // lidar
  372. std::map<int, Wanji_lidar_device *> t_wj_lidar_device_map = Wanji_manager::get_instance_references().get_wanji_lidar_device_map();
  373. std::map<int, Wanji_lidar_device::Wanji_lidar_device_status> t_wj_lidar_status_map;
  374. for (auto iter = t_wj_lidar_device_map.begin(); iter != t_wj_lidar_device_map.end(); ++iter)
  375. {
  376. 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()));
  377. }
  378. // region
  379. std::map<int, Region_worker *> t_region_worker_map = Wanji_manager::get_instance_references().get_region_worker_map();
  380. int region_index = 0;
  381. for (auto iter = t_region_worker_map.begin(); iter != t_region_worker_map.end(); ++iter)
  382. {
  383. // 以t_ground_status_msg为模板创建各区域心跳消息
  384. message::Ground_status_msg t_multi_status_msg;
  385. t_multi_status_msg.CopyFrom(t_ground_status_msg);
  386. t_multi_status_msg.mutable_id_struct()->set_terminal_id(iter->second->get_terminal_id());
  387. t_multi_status_msg.set_region_worker_status((message::Region_worker_status)(iter->second->get_status()));
  388. wj::Region t_param = iter->second->get_param();
  389. for (size_t j = 0; j < t_param.lidar_exts_size(); j++)
  390. {
  391. 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());
  392. if (t_status_iter == t_wj_lidar_status_map.end())
  393. {
  394. LOG(WARNING) << "lidar status " << t_param.lidar_exts(j).lidar_id() << " cannot be found, param error";
  395. }
  396. else
  397. {
  398. t_multi_status_msg.add_wanji_lidar_device_status((message::Wanji_lidar_device_status)t_status_iter->second);
  399. }
  400. }
  401. //velodyne雷达的自动定位信息
  402. Common_data::Car_wheel_information t_car_wheel_information;
  403. t_error = (*iter).second->get_last_wheel_information(&t_car_wheel_information, std::chrono::system_clock::now());
  404. // 获取区域点云填入信息
  405. pcl::PointCloud<pcl::PointXYZ>::Ptr t_region_cloud = pcl::PointCloud<pcl::PointXYZ>::Ptr(new pcl::PointCloud<pcl::PointXYZ>);
  406. iter->second->get_region_cloud(t_region_cloud);
  407. if (cloud_count == 5)
  408. {
  409. std::string t_filename = std::string("region_") + std::to_string(iter->first) + "_cloud.txt";
  410. save_cloud_txt(t_region_cloud, t_filename);
  411. LOG(INFO) << "region " << iter->first << " cloud has been saved in " + t_filename;
  412. }
  413. // for (size_t j = 0; j < t_region_cloud->size(); j++)
  414. // {
  415. // message::Cloud_coordinate *tp_cloud = t_multi_status_msg.add_cloud();
  416. // tp_cloud->set_x(t_region_cloud->points[j].x);
  417. // tp_cloud->set_y(t_region_cloud->points[j].y);
  418. // tp_cloud->set_z(t_region_cloud->points[j].z);
  419. // }
  420. update_measure_info(t_multi_status_msg, t_car_wheel_information, t_region_cloud->size());
  421. std::string t_msg = t_multi_status_msg.SerializeAsString();
  422. System_communication::get_instance_references().encapsulate_msg(t_msg);
  423. if (t_multi_status_msg.id_struct().terminal_id() == DISP_TERM_ID)
  424. LOG(INFO) << t_multi_status_msg.DebugString();
  425. }
  426. }
  427. // std::cout << " huli test :::: " << " t_ground_status_msg.DebugString() = " << std::endl;
  428. // std::cout << t_ground_status_msg.DebugString() << std::endl;
  429. // std::cout << " huli test :::: " << " t_ground_status_msg.DebugString() = " << t_ground_status_msg.DebugString() << std::endl;
  430. #else
  431. {
  432. // mix vlp16 and wj716
  433. // 以多线雷达为主
  434. Velodyne_manager::Velodyne_manager_status t_velodyne_manager_status = Velodyne_manager::get_instance_references().get_status();
  435. t_ground_status_msg.set_wanji_manager_status((message::Wanji_manager_status)t_velodyne_manager_status);
  436. // vlp16 lidar
  437. std::map<int, Velodyne_lidar_device *> t_velodyne_lidar_device_map = Velodyne_manager::get_instance_references().get_velodyne_lidar_device_map();
  438. std::map<int, Velodyne_lidar_device::Velodyne_lidar_device_status> t_velodyne_lidar_status_map;
  439. for (auto iter = t_velodyne_lidar_device_map.begin(); iter != t_velodyne_lidar_device_map.end(); ++iter)
  440. {
  441. 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()));
  442. }
  443. // wj lidar
  444. #if LIDAR_TYPE == 1
  445. std::map<int, Wanji_lidar_device *> t_wj_lidar_device_map = Wanji_manager::get_instance_references().get_wanji_lidar_device_map();
  446. std::map<int, Wanji_lidar_device::Wanji_lidar_device_status> t_wj_lidar_status_map;
  447. for (auto iter = t_wj_lidar_device_map.begin(); iter != t_wj_lidar_device_map.end(); ++iter)
  448. {
  449. 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()));
  450. }
  451. #else
  452. std::map<int, Wanji_716N_lidar_device *> t_wj_lidar_device_map = Wanji_manager::get_instance_references().get_wanji_lidar_device_map();
  453. std::map<int, Wanji_716N_lidar_device::Wanji_716N_device_status> t_wj_lidar_status_map;
  454. for (auto iter = t_wj_lidar_device_map.begin(); iter != t_wj_lidar_device_map.end(); ++iter)
  455. {
  456. t_wj_lidar_status_map.emplace(std::pair<int, Wanji_716N_lidar_device::Wanji_716N_device_status>(iter->second->get_lidar_id(), iter->second->get_status()));
  457. }
  458. #endif
  459. // wj region
  460. std::map<int, Region_worker *> t_region_worker_map = Wanji_manager::get_instance_references().get_region_worker_map();
  461. // vlp16 region
  462. std::map<int, Ground_region *> t_ground_region_map = Velodyne_manager::get_instance_references().get_ground_region_map();
  463. int region_index = 0;
  464. for (auto iter = t_ground_region_map.begin(); iter != t_ground_region_map.end(); ++iter)
  465. {
  466. // 以t_ground_status_msg为模板创建各区域心跳消息
  467. message::Ground_status_msg t_multi_status_msg;
  468. t_multi_status_msg.CopyFrom(t_ground_status_msg);
  469. t_multi_status_msg.mutable_id_struct()->set_terminal_id(iter->second->get_terminal_id());
  470. t_multi_status_msg.set_region_worker_status((message::Region_worker_status)(iter->second->get_status()));
  471. // 查找多线区域内对应多线激光雷达
  472. velodyne::Region t_param = iter->second->get_param();
  473. for (size_t j = 0; j < t_param.lidar_exts_size(); j++)
  474. {
  475. 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());
  476. if (t_status_iter == t_velodyne_lidar_status_map.end())
  477. {
  478. LOG(WARNING) << "vlp16 lidar status " << t_param.lidar_exts(j).lidar_id() << " cannot be found, param error";
  479. }
  480. else
  481. {
  482. t_multi_status_msg.add_wanji_lidar_device_status((message::Wanji_lidar_device_status)t_status_iter->second);
  483. }
  484. }
  485. //velodyne雷达的自动定位信息
  486. Common_data::Car_wheel_information t_car_wheel_information;
  487. t_error = (*iter).second->get_last_wheel_information(&t_car_wheel_information, std::chrono::system_clock::now());
  488. // 获取区域点云填入信息
  489. pcl::PointCloud<pcl::PointXYZ>::Ptr t_region_cloud = pcl::PointCloud<pcl::PointXYZ>::Ptr(new pcl::PointCloud<pcl::PointXYZ>);
  490. iter->second->get_region_cloud(t_region_cloud, Ground_region::Region_cloud_type::filtered);
  491. if (cloud_count == 5)
  492. {
  493. std::string t_filename = std::string("vlp16_region_") + std::to_string(iter->first) + "_cloud.txt";
  494. save_cloud_txt(t_region_cloud, t_filename);
  495. LOG(INFO) << " vlp16 region " << iter->first << " cloud has been saved in " + t_filename;
  496. }
  497. // for (size_t j = 0; j < t_region_cloud->size(); j++)
  498. // {
  499. // message::Cloud_coordinate *tp_cloud = t_multi_status_msg.add_cloud();
  500. // tp_cloud->set_x(t_region_cloud->points[j].x);
  501. // tp_cloud->set_y(t_region_cloud->points[j].y);
  502. // tp_cloud->set_z(t_region_cloud->points[j].z);
  503. // }
  504. // 检查vlp16区域与wj区域对应关系,wj存在对应区域则填充设备状态,并融合测量结果
  505. auto t_wj_region_iter = t_region_worker_map.find(iter->first);
  506. if(t_wj_region_iter != t_region_worker_map.end())
  507. {
  508. wj::Region t_param = t_wj_region_iter->second->get_param();
  509. //
  510. for (size_t j = 0; j < t_param.lidar_exts_size(); j++)
  511. {
  512. #if LIDAR_TYPE == 1
  513. 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());
  514. #else
  515. std::map<int, Wanji_716N_lidar_device::Wanji_716N_device_status>::iterator t_status_iter = t_wj_lidar_status_map.find(t_param.lidar_exts(j).lidar_id());
  516. #endif
  517. if (t_status_iter == t_wj_lidar_status_map.end())
  518. {
  519. LOG(WARNING) << "wj lidar status " << t_param.lidar_exts(j).lidar_id() << " cannot be found, param error";
  520. }
  521. else
  522. {
  523. t_multi_status_msg.add_wanji_lidar_device_status((message::Wanji_lidar_device_status)t_status_iter->second);
  524. }
  525. }
  526. //wj雷达的自动定位信息
  527. Common_data::Car_wheel_information t_wj_car_wheel_information;
  528. t_error = t_wj_region_iter->second->get_last_wheel_information(&t_wj_car_wheel_information, std::chrono::system_clock::now());
  529. // 获取区域点云填入信息
  530. pcl::PointCloud<pcl::PointXYZ>::Ptr t_wj_region_cloud = pcl::PointCloud<pcl::PointXYZ>::Ptr(new pcl::PointCloud<pcl::PointXYZ>);
  531. t_wj_region_iter->second->get_region_cloud(t_wj_region_cloud);
  532. if (cloud_count == 8)
  533. {
  534. std::string t_filename = std::string("wj_region_") + std::to_string(t_wj_region_iter->first) + "_cloud.txt";
  535. save_cloud_txt(t_wj_region_cloud, t_filename);
  536. LOG(INFO) << "wj region " << t_wj_region_iter->first << " cloud has been saved in " + t_filename;
  537. }
  538. // for (size_t j = 0; j < t_wj_region_cloud->size(); j++)
  539. // {
  540. // message::Cloud_coordinate *tp_cloud = t_multi_status_msg.add_cloud();
  541. // tp_cloud->set_x(t_wj_region_cloud->points[j].x);
  542. // tp_cloud->set_y(t_wj_region_cloud->points[j].y);
  543. // tp_cloud->set_z(t_wj_region_cloud->points[j].z);
  544. // }
  545. // LOG_IF(WARNING, iter->second->get_terminal_id() == DISP_TERM_ID)<<"\n"<<t_car_wheel_information.to_string()<<"\n"
  546. // <<t_wj_car_wheel_information.to_string()<<"\n"
  547. // <<t_car_wheel_information.range_status<<", "<<t_wj_car_wheel_information.range_status;
  548. // 融合测量结果,验证中心点差异x<0.03, y<0.06, 角度差异ang<0.6, 轴距差异wb<0.055
  549. double dx = t_car_wheel_information.car_center_x - t_wj_car_wheel_information.car_center_x;
  550. double dy = t_car_wheel_information.car_center_y - t_wj_car_wheel_information.car_center_y;
  551. double dang = t_car_wheel_information.car_angle - t_wj_car_wheel_information.car_angle;
  552. double dwb = t_car_wheel_information.car_wheel_base - t_wj_car_wheel_information.car_wheel_base;
  553. if(fabs(dx) < 0.03 && fabs(dy) < 0.06 && fabs(dang) < 0.6 && fabs(dwb) < 0.055)
  554. {
  555. t_car_wheel_information.car_wheel_base = t_car_wheel_information.car_wheel_base*0.3 + t_car_wheel_information.car_wheel_base*0.7;
  556. }else
  557. {
  558. LOG_IF(WARNING, iter->second->get_terminal_id() == DISP_TERM_ID)<<
  559. "detect mismatch, region "<<iter->first<<", dx dy dang dwb: "<<dx<<", "<<dy<<", "<<dang<<", "<< dwb << std::endl
  560. << t_car_wheel_information.to_string() << std::endl
  561. << t_wj_car_wheel_information.to_string() << std::endl;
  562. }
  563. }else{
  564. LOG_IF(WARNING, iter->second->get_terminal_id() == DISP_TERM_ID)<<"region mismatch, vlp16 "<<iter->first<<", wj doesn't have corresponding region id";
  565. }
  566. update_measure_info(t_multi_status_msg, t_car_wheel_information, t_region_cloud->size());
  567. if(t_multi_status_msg.id_struct().terminal_id() == 4 ||
  568. t_multi_status_msg.id_struct().terminal_id() == 0||
  569. t_multi_status_msg.id_struct().terminal_id() == 5||
  570. t_multi_status_msg.id_struct().terminal_id() == 1||
  571. t_multi_status_msg.id_struct().terminal_id() == 3||
  572. t_multi_status_msg.id_struct().terminal_id() == 2)
  573. {
  574. // rabbitmq new measure info
  575. measure_info t_multi_measure_info_msg;
  576. t_multi_measure_info_msg.set_cx(t_car_wheel_information.uniform_car_x);
  577. t_multi_measure_info_msg.set_cy(t_car_wheel_information.uniform_car_y);
  578. t_multi_measure_info_msg.set_theta(t_car_wheel_information.car_angle);
  579. t_multi_measure_info_msg.set_length(0.0f);
  580. t_multi_measure_info_msg.set_width(t_car_wheel_information.car_wheel_width);
  581. t_multi_measure_info_msg.set_height(0.0f);
  582. t_multi_measure_info_msg.set_wheelbase(t_car_wheel_information.car_wheel_base);
  583. t_multi_measure_info_msg.set_front_theta(t_car_wheel_information.car_front_theta);
  584. t_multi_measure_info_msg.set_border_statu(t_car_wheel_information.range_status);
  585. auto t_ground_status = t_multi_status_msg.ground_status();
  586. if(t_ground_status == message::Ground_statu::Car_correct)
  587. {
  588. t_multi_measure_info_msg.set_ground_status(0);
  589. }else if(t_ground_status == message::Ground_statu::Nothing)
  590. {
  591. t_multi_measure_info_msg.set_ground_status(1);
  592. }else if(t_ground_status == message::Ground_statu::Noise)
  593. {
  594. t_multi_measure_info_msg.set_ground_status(2);
  595. }
  596. else if(t_ground_status == message::Ground_statu::Car_border_reached)
  597. {
  598. t_multi_measure_info_msg.set_ground_status(3);
  599. }else
  600. {
  601. t_multi_measure_info_msg.set_ground_status(2);
  602. std::cout<<"system executor 661, original status "<<t_ground_status<<std::endl;
  603. }
  604. // LOG(WARNING) << t_multi_status_msg.error_manager().error_description()<<std::endl;
  605. std::string t_msg = t_multi_measure_info_msg.DebugString();
  606. System_communication_mq::get_instance_references().encapsulate_status_msg(t_msg, t_multi_status_msg.id_struct().terminal_id());
  607. // if (t_multi_status_msg.id_struct().terminal_id() == 1)
  608. // {
  609. // LOG(WARNING) << "-------------------------------------------------------" <<std::endl;
  610. // LOG(WARNING) << "t_msg = " << t_msg <<std::endl;
  611. // LOG(WARNING) << "+++++++++++++++++++++++++++++++++++++++++++++++++++++++" <<std::endl;
  612. // LOG(WARNING) << "t_multi_status_msg.ground_status() = " << t_ground_status <<std::endl;
  613. // LOG(WARNING) << "t_multi_measure_info_msg.set_ground_status(0) = " << t_multi_measure_info_msg.ground_status() <<std::endl;
  614. // }
  615. }
  616. if (t_multi_status_msg.id_struct().terminal_id() == DISP_TERM_ID)
  617. {
  618. // LOG(WARNING) << t_car_wheel_information.range_status<<std::endl;
  619. LOG(INFO) << t_multi_status_msg.DebugString() << std::endl
  620. << std::endl;
  621. }
  622. }
  623. }
  624. #endif
  625. return Error_code::SUCCESS;
  626. }
  627. // ***************************** nanomsg *********************************
  628. //定时发送状态信息
  629. Error_manager System_executor::encapsulate_send_status()
  630. {
  631. static int cloud_count=-1;
  632. cloud_count++;
  633. // if(!g_debug_file.is_open())
  634. // {
  635. // g_debug_file.open("./filter_debug_result.txt", std::ios::app);
  636. // }
  637. Error_manager t_error;
  638. //创建一条状态消息
  639. message::Ground_status_msg t_ground_status_msg;
  640. t_ground_status_msg.mutable_base_info()->set_msg_type(message::Message_type::eGround_status_msg);
  641. t_ground_status_msg.mutable_base_info()->set_timeout_ms(5000);
  642. t_ground_status_msg.mutable_base_info()->set_sender(message::Communicator::eGround_measurer);
  643. t_ground_status_msg.mutable_base_info()->set_receiver(message::Communicator::eEmpty);
  644. // t_ground_status_msg.set_terminal_id(m_terminal_id);
  645. t_ground_status_msg.mutable_id_struct()->set_terminal_id(m_terminal_id);
  646. // 创建各区域状态消息,
  647. // 注意!!!目前公共消息名字依旧使用wj,不做修改
  648. // manager
  649. #if WJ_VELO == 1
  650. {
  651. Velodyne_manager::Velodyne_manager_status t_velodyne_manager_status = Velodyne_manager::get_instance_references().get_status();
  652. t_ground_status_msg.set_wanji_manager_status((message::Wanji_manager_status)t_velodyne_manager_status);
  653. // lidar
  654. std::map<int, Velodyne_lidar_device *> t_velodyne_lidar_device_map = Velodyne_manager::get_instance_references().get_velodyne_lidar_device_map();
  655. std::map<int, Velodyne_lidar_device::Velodyne_lidar_device_status> t_velodyne_lidar_status_map;
  656. for (auto iter = t_velodyne_lidar_device_map.begin(); iter != t_velodyne_lidar_device_map.end(); ++iter)
  657. {
  658. 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()));
  659. }
  660. // region
  661. std::map<int, Ground_region *> t_ground_region_map = Velodyne_manager::get_instance_references().get_ground_region_map();
  662. int region_index = 0;
  663. for (auto iter = t_ground_region_map.begin(); iter != t_ground_region_map.end(); ++iter)
  664. {
  665. // 以t_ground_status_msg为模板创建各区域心跳消息
  666. message::Ground_status_msg t_multi_status_msg;
  667. t_multi_status_msg.CopyFrom(t_ground_status_msg);
  668. t_multi_status_msg.mutable_id_struct()->set_terminal_id(iter->second->get_terminal_id());
  669. t_multi_status_msg.set_region_worker_status((message::Region_worker_status)(iter->second->get_status()));
  670. velodyne::Region t_param = iter->second->get_param();
  671. for (size_t j = 0; j < t_param.lidar_exts_size(); j++)
  672. {
  673. 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());
  674. if (t_status_iter == t_velodyne_lidar_status_map.end())
  675. {
  676. LOG(WARNING) << "lidar status " << t_param.lidar_exts(j).lidar_id() << " cannot be found, param error";
  677. }
  678. else
  679. {
  680. t_multi_status_msg.add_wanji_lidar_device_status((message::Wanji_lidar_device_status)t_status_iter->second);
  681. }
  682. }
  683. //velodyne雷达的自动定位信息
  684. Common_data::Car_wheel_information t_car_wheel_information;
  685. t_error = (*iter).second->get_last_wheel_information(&t_car_wheel_information, std::chrono::system_clock::now());
  686. // 获取区域点云填入信息
  687. pcl::PointCloud<pcl::PointXYZ>::Ptr t_region_cloud = pcl::PointCloud<pcl::PointXYZ>::Ptr(new pcl::PointCloud<pcl::PointXYZ>);
  688. iter->second->get_region_cloud(t_region_cloud, Ground_region::Region_cloud_type::filtered);
  689. // if (cloud_count == 5)
  690. // {
  691. // std::string t_filename = std::string("region_") + std::to_string(iter->first) + "_cloud.txt";
  692. // save_cloud_txt(t_region_cloud, t_filename);
  693. // LOG(INFO) << "region " << iter->first << " cloud has been saved in " + t_filename;
  694. // }
  695. // for (size_t j = 0; j < t_region_cloud->size(); j++)
  696. // {
  697. // message::Cloud_coordinate *tp_cloud = t_multi_status_msg.add_cloud();
  698. // tp_cloud->set_x(t_region_cloud->points[j].x);
  699. // tp_cloud->set_y(t_region_cloud->points[j].y);
  700. // tp_cloud->set_z(t_region_cloud->points[j].z);
  701. // }
  702. update_measure_info(t_multi_status_msg, t_car_wheel_information, t_region_cloud->size());
  703. std::string t_msg = t_multi_status_msg.SerializeAsString();
  704. System_communication::get_instance_references().encapsulate_msg(t_msg);
  705. if (t_multi_status_msg.id_struct().terminal_id() == DISP_TERM_ID)
  706. std::cout << t_multi_status_msg.DebugString() << std::endl
  707. << std::endl;
  708. }
  709. }
  710. #elif WJ_VELO == 0
  711. {
  712. // wj_support 20220718
  713. Wanji_manager::Wanji_manager_status t_wj_manager_status = Wanji_manager::get_instance_references().get_status();
  714. t_ground_status_msg.set_wanji_manager_status((message::Wanji_manager_status)t_wj_manager_status);
  715. // lidar
  716. std::map<int, Wanji_lidar_device *> t_wj_lidar_device_map = Wanji_manager::get_instance_references().get_wanji_lidar_device_map();
  717. std::map<int, Wanji_lidar_device::Wanji_lidar_device_status> t_wj_lidar_status_map;
  718. for (auto iter = t_wj_lidar_device_map.begin(); iter != t_wj_lidar_device_map.end(); ++iter)
  719. {
  720. 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()));
  721. }
  722. // region
  723. std::map<int, Region_worker *> t_region_worker_map = Wanji_manager::get_instance_references().get_region_worker_map();
  724. int region_index = 0;
  725. for (auto iter = t_region_worker_map.begin(); iter != t_region_worker_map.end(); ++iter)
  726. {
  727. // 以t_ground_status_msg为模板创建各区域心跳消息
  728. message::Ground_status_msg t_multi_status_msg;
  729. t_multi_status_msg.CopyFrom(t_ground_status_msg);
  730. t_multi_status_msg.mutable_id_struct()->set_terminal_id(iter->second->get_terminal_id());
  731. t_multi_status_msg.set_region_worker_status((message::Region_worker_status)(iter->second->get_status()));
  732. wj::Region t_param = iter->second->get_param();
  733. for (size_t j = 0; j < t_param.lidar_exts_size(); j++)
  734. {
  735. 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());
  736. if (t_status_iter == t_wj_lidar_status_map.end())
  737. {
  738. LOG(WARNING) << "lidar status " << t_param.lidar_exts(j).lidar_id() << " cannot be found, param error";
  739. }
  740. else
  741. {
  742. t_multi_status_msg.add_wanji_lidar_device_status((message::Wanji_lidar_device_status)t_status_iter->second);
  743. }
  744. }
  745. //velodyne雷达的自动定位信息
  746. Common_data::Car_wheel_information t_car_wheel_information;
  747. t_error = (*iter).second->get_last_wheel_information(&t_car_wheel_information, std::chrono::system_clock::now());
  748. // 获取区域点云填入信息
  749. pcl::PointCloud<pcl::PointXYZ>::Ptr t_region_cloud = pcl::PointCloud<pcl::PointXYZ>::Ptr(new pcl::PointCloud<pcl::PointXYZ>);
  750. iter->second->get_region_cloud(t_region_cloud);
  751. // if (cloud_count == 5)
  752. // {
  753. // std::string t_filename = std::string("region_") + std::to_string(iter->first) + "_cloud.txt";
  754. // save_cloud_txt(t_region_cloud, t_filename);
  755. // LOG(INFO) << "region " << iter->first << " cloud has been saved in " + t_filename;
  756. // }
  757. // for (size_t j = 0; j < t_region_cloud->size(); j++)
  758. // {
  759. // message::Cloud_coordinate *tp_cloud = t_multi_status_msg.add_cloud();
  760. // tp_cloud->set_x(t_region_cloud->points[j].x);
  761. // tp_cloud->set_y(t_region_cloud->points[j].y);
  762. // tp_cloud->set_z(t_region_cloud->points[j].z);
  763. // }
  764. update_measure_info(t_multi_status_msg, t_car_wheel_information, t_region_cloud->size());
  765. std::string t_msg = t_multi_status_msg.SerializeAsString();
  766. System_communication::get_instance_references().encapsulate_msg(t_msg);
  767. if (t_multi_status_msg.id_struct().terminal_id() == DISP_TERM_ID)
  768. LOG(INFO) << t_multi_status_msg.DebugString();
  769. }
  770. }
  771. // std::cout << " huli test :::: " << " t_ground_status_msg.DebugString() = " << std::endl;
  772. // std::cout << t_ground_status_msg.DebugString() << std::endl;
  773. // std::cout << " huli test :::: " << " t_ground_status_msg.DebugString() = " << t_ground_status_msg.DebugString() << std::endl;
  774. #else
  775. {
  776. // mix vlp16 and wj716
  777. // 以多线雷达为主
  778. Velodyne_manager::Velodyne_manager_status t_velodyne_manager_status = Velodyne_manager::get_instance_references().get_status();
  779. t_ground_status_msg.set_wanji_manager_status((message::Wanji_manager_status)t_velodyne_manager_status);
  780. // vlp16 lidar
  781. std::map<int, Velodyne_lidar_device *> t_velodyne_lidar_device_map = Velodyne_manager::get_instance_references().get_velodyne_lidar_device_map();
  782. std::map<int, Velodyne_lidar_device::Velodyne_lidar_device_status> t_velodyne_lidar_status_map;
  783. for (auto iter = t_velodyne_lidar_device_map.begin(); iter != t_velodyne_lidar_device_map.end(); ++iter)
  784. {
  785. 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()));
  786. }
  787. // wj lidar
  788. #if LIDAR_TYPE == 1
  789. std::map<int, Wanji_lidar_device *> t_wj_lidar_device_map = Wanji_manager::get_instance_references().get_wanji_lidar_device_map();
  790. std::map<int, Wanji_lidar_device::Wanji_lidar_device_status> t_wj_lidar_status_map;
  791. for (auto iter = t_wj_lidar_device_map.begin(); iter != t_wj_lidar_device_map.end(); ++iter)
  792. {
  793. 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()));
  794. }
  795. #else
  796. std::map<int, Wanji_716N_lidar_device *> t_wj_lidar_device_map = Wanji_manager::get_instance_references().get_wanji_lidar_device_map();
  797. std::map<int, Wanji_716N_lidar_device::Wanji_716N_device_status> t_wj_lidar_status_map;
  798. for (auto iter = t_wj_lidar_device_map.begin(); iter != t_wj_lidar_device_map.end(); ++iter)
  799. {
  800. t_wj_lidar_status_map.emplace(std::pair<int, Wanji_716N_lidar_device::Wanji_716N_device_status>(iter->second->get_lidar_id(), iter->second->get_status()));
  801. }
  802. #endif
  803. // wj region
  804. std::map<int, Region_worker *> t_region_worker_map = Wanji_manager::get_instance_references().get_region_worker_map();
  805. // vlp16 region
  806. std::map<int, Ground_region *> t_ground_region_map = Velodyne_manager::get_instance_references().get_ground_region_map();
  807. int region_index = 0;
  808. for (auto iter = t_ground_region_map.begin(); iter != t_ground_region_map.end(); ++iter)
  809. {
  810. // 以t_ground_status_msg为模板创建各区域心跳消息
  811. message::Ground_status_msg t_multi_status_msg;
  812. t_multi_status_msg.CopyFrom(t_ground_status_msg);
  813. t_multi_status_msg.mutable_id_struct()->set_terminal_id(iter->second->get_terminal_id());
  814. t_multi_status_msg.set_region_worker_status((message::Region_worker_status)(iter->second->get_status()));
  815. // 查找多线区域内对应多线激光雷达
  816. velodyne::Region t_param = iter->second->get_param();
  817. for (size_t j = 0; j < t_param.lidar_exts_size(); j++)
  818. {
  819. 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());
  820. if (t_status_iter == t_velodyne_lidar_status_map.end())
  821. {
  822. LOG(WARNING) << "vlp16 lidar status " << t_param.lidar_exts(j).lidar_id() << " cannot be found, param error";
  823. }
  824. else
  825. {
  826. t_multi_status_msg.add_wanji_lidar_device_status((message::Wanji_lidar_device_status)t_status_iter->second);
  827. }
  828. }
  829. //velodyne雷达的自动定位信息
  830. Common_data::Car_wheel_information t_car_wheel_information;
  831. t_error = (*iter).second->get_last_wheel_information(&t_car_wheel_information, std::chrono::system_clock::now());
  832. // 获取区域点云填入信息
  833. // 20221211 changed by yct, nanomsg dont save cloud
  834. pcl::PointCloud<pcl::PointXYZ>::Ptr t_region_cloud = pcl::PointCloud<pcl::PointXYZ>::Ptr(new pcl::PointCloud<pcl::PointXYZ>);
  835. iter->second->get_region_cloud(t_region_cloud, Ground_region::Region_cloud_type::filtered);
  836. // if (cloud_count == 5)
  837. // {
  838. // std::string t_filename = std::string("vlp16_region_") + std::to_string(iter->first) + "_cloud.txt";
  839. // save_cloud_txt(t_region_cloud, t_filename);
  840. // LOG(INFO) << "vlp16 region " << iter->first << " cloud has been saved in " + t_filename;
  841. // }
  842. // for (size_t j = 0; j < t_region_cloud->size(); j++)
  843. // {
  844. // message::Cloud_coordinate *tp_cloud = t_multi_status_msg.add_cloud();
  845. // tp_cloud->set_x(t_region_cloud->points[j].x);
  846. // tp_cloud->set_y(t_region_cloud->points[j].y);
  847. // tp_cloud->set_z(t_region_cloud->points[j].z);
  848. // }
  849. // 检查vlp16区域与wj区域对应关系,wj存在对应区域则填充设备状态,并融合测量结果
  850. auto t_wj_region_iter = t_region_worker_map.find(iter->first);
  851. if(t_wj_region_iter != t_region_worker_map.end())
  852. {
  853. wj::Region t_param = t_wj_region_iter->second->get_param();
  854. //
  855. for (size_t j = 0; j < t_param.lidar_exts_size(); j++)
  856. {
  857. #if LIDAR_TYPE == 1
  858. 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());
  859. #else
  860. std::map<int, Wanji_716N_lidar_device::Wanji_716N_device_status>::iterator t_status_iter = t_wj_lidar_status_map.find(t_param.lidar_exts(j).lidar_id());
  861. #endif
  862. if (t_status_iter == t_wj_lidar_status_map.end())
  863. {
  864. LOG(WARNING) << "wj lidar status " << t_param.lidar_exts(j).lidar_id() << " cannot be found, param error";
  865. }
  866. else
  867. {
  868. t_multi_status_msg.add_wanji_lidar_device_status((message::Wanji_lidar_device_status)t_status_iter->second);
  869. }
  870. }
  871. //wj雷达的自动定位信息
  872. Common_data::Car_wheel_information t_wj_car_wheel_information;
  873. t_error = t_wj_region_iter->second->get_last_wheel_information(&t_wj_car_wheel_information, std::chrono::system_clock::now());
  874. // 获取区域点云填入信息
  875. pcl::PointCloud<pcl::PointXYZ>::Ptr t_wj_region_cloud = pcl::PointCloud<pcl::PointXYZ>::Ptr(new pcl::PointCloud<pcl::PointXYZ>);
  876. t_wj_region_iter->second->get_region_cloud(t_wj_region_cloud);
  877. // if (cloud_count == 8)
  878. // {
  879. // std::string t_filename = std::string("wj_region_") + std::to_string(t_wj_region_iter->first) + "_cloud.txt";
  880. // save_cloud_txt(t_wj_region_cloud, t_filename);
  881. // LOG(INFO) << "wj region " << t_wj_region_iter->first << " cloud has been saved in " + t_filename;
  882. // }
  883. // for (size_t j = 0; j < t_wj_region_cloud->size(); j++)
  884. // {
  885. // message::Cloud_coordinate *tp_cloud = t_multi_status_msg.add_cloud();
  886. // tp_cloud->set_x(t_wj_region_cloud->points[j].x);
  887. // tp_cloud->set_y(t_wj_region_cloud->points[j].y);
  888. // tp_cloud->set_z(t_wj_region_cloud->points[j].z);
  889. // }
  890. // LOG_IF(WARNING, iter->second->get_terminal_id() == DISP_TERM_ID)<<"\n"<<t_car_wheel_information.to_string()<<"\n"
  891. // <<t_wj_car_wheel_information.to_string()<<"\n"
  892. // <<t_car_wheel_information.range_status<<", "<<t_wj_car_wheel_information.range_status;
  893. // 融合测量结果,验证中心点差异x<0.03, y<0.06, 角度差异ang<0.6, 轴距差异wb<0.055
  894. double dx = t_car_wheel_information.car_center_x - t_wj_car_wheel_information.car_center_x;
  895. double dy = t_car_wheel_information.car_center_y - t_wj_car_wheel_information.car_center_y;
  896. double dang = t_car_wheel_information.car_angle - t_wj_car_wheel_information.car_angle;
  897. double dwb = t_car_wheel_information.car_wheel_base - t_wj_car_wheel_information.car_wheel_base;
  898. // 使用rabbit mq 中函数计算偏差与提示,此处不处理!!
  899. if(fabs(dx) < 0.03 && fabs(dy) < 0.06 && fabs(dang) < 0.6 && fabs(dwb) < 0.055)
  900. {
  901. // t_car_wheel_information.car_wheel_base = t_car_wheel_information.car_wheel_base*0.3 + t_car_wheel_information.car_wheel_base*0.7;
  902. }else
  903. {
  904. // LOG_IF(WARNING, iter->second->get_terminal_id() == DISP_TERM_ID)<<
  905. // "detect mismatch, region "<<iter->first<<", dx dy dang dwb: "<<dx<<", "<<dy<<", "<<dang<<", "<< dwb << std::endl
  906. // << t_car_wheel_information.to_string() << std::endl
  907. // << t_wj_car_wheel_information.to_string() << std::endl;
  908. }
  909. }else{
  910. LOG_IF(WARNING, iter->second->get_terminal_id() == DISP_TERM_ID)<<"region mismatch, vlp16 "<<iter->first<<", wj doesn't have corresponding region id";
  911. }
  912. update_measure_info(t_multi_status_msg, t_car_wheel_information, t_region_cloud->size());
  913. std::string t_msg = t_multi_status_msg.SerializeAsString();
  914. System_communication::get_instance_references().encapsulate_msg(t_msg);
  915. // if (t_multi_status_msg.id_struct().terminal_id() == DISP_TERM_ID)
  916. // std::cout << t_multi_status_msg.DebugString() << std::endl
  917. // << std::endl;
  918. }
  919. }
  920. #endif
  921. return Error_code::SUCCESS;
  922. }
  923. //判断是否为待机,如果已经准备好,则可以执行任务。
  924. bool System_executor::is_ready()
  925. {
  926. if (m_system_executor_status == SYSTEM_EXECUTOR_READY && m_thread_pool.thread_is_full_load() == false)
  927. {
  928. return true;
  929. }
  930. else
  931. {
  932. return false;
  933. }
  934. }
  935. System_executor::System_executor_status System_executor::get_system_executor_status()
  936. {
  937. return m_system_executor_status;
  938. }
  939. int System_executor::get_terminal_id()
  940. {
  941. return m_terminal_id;
  942. }
  943. //雷达感测定位 的处理函数
  944. //input::command_id, 消息指令id, 由主控制系统生成的唯一码
  945. //input::command_id, 终端id, 对应具体的某个车位
  946. //return::void, 没有返回, 执行结果直接生成一条答复消息, 然后通过通信返回
  947. void System_executor::execute_for_measure(std::string command_info, int terminal_id, std::chrono::system_clock::time_point receive_time)
  948. {
  949. Error_manager t_error;
  950. Error_manager t_result;
  951. LOG(INFO) << " System_executor::execute_for_measure run " << this;
  952. //第1步, 按照时间生成中间文件的保存路径
  953. time_t nowTime;
  954. nowTime = time(NULL);
  955. struct tm *sysTime = localtime(&nowTime);
  956. char t_save_path[256] = {0};
  957. sprintf(t_save_path, "../data/%04d%02d%02d_%02d%02d%02d",
  958. sysTime->tm_year + 1900, sysTime->tm_mon + 1, sysTime->tm_mday, sysTime->tm_hour, sysTime->tm_min, sysTime->tm_sec);
  959. //第2步, 创建万集管理模块的任务单, 并发送到 wanji_manager
  960. // Wanji_manager_task t_wanji_manager_task ;
  961. // Common_data::Car_wheel_information t_car_information_by_wanji;
  962. // t_wanji_manager_task.task_init(TASK_CREATED,"",NULL,std::chrono::milliseconds(3000),
  963. // terminal_id, receive_time);
  964. // t_error = Task_command_manager::get_instance_references().execute_task(&t_wanji_manager_task);
  965. // 或者创建velodyne管理模块任务单,并发送到velodyne_manager
  966. Velodyne_manager_task t_velodyne_manager_task;
  967. Common_data::Car_wheel_information t_car_information_by_velodyne;
  968. t_velodyne_manager_task.task_init(TASK_CREATED, "", NULL, std::chrono::milliseconds(3000),
  969. terminal_id, receive_time);
  970. t_error = Task_command_manager::get_instance_references().execute_task(&t_velodyne_manager_task);
  971. //第3步, 等待任务单完成
  972. if (t_error != Error_code::SUCCESS)
  973. {
  974. LOG(INFO) << " Velodyne_manager/Velodyne_manager execute_task error " << this;
  975. }
  976. else
  977. {
  978. // //判断任务是否结束, 阻塞 等待, 直到任务完成或者超时
  979. // while ( t_wanji_manager_task.is_task_end() == false)
  980. // {
  981. // if ( t_wanji_manager_task.is_over_time() )
  982. // {
  983. // //超时处理。取消任务。
  984. // Wanji_manager::get_instance_pointer()->cancel_task(&t_wanji_manager_task);
  985. // t_error.error_manager_reset(Error_code::WJ_MANAGER_TASK_OVER_TIME, Error_level::MINOR_ERROR,
  986. // " t_wanjestaui_manager_task is_over_time ");
  987. // t_wanji_manager_task.set_task_error_manager(t_error);
  988. // }
  989. // else
  990. // {
  991. // //继续等待
  992. // std::this_thread::sleep_for(std::chrono::microseconds(1));
  993. // std::this_thread::yield();
  994. // }
  995. // }
  996. // //提取任务单 的错误码
  997. // t_error = t_wanji_manager_task.get_task_error_manager();
  998. // if ( t_error == Error_code::SUCCESS )
  999. // {
  1000. // t_car_information_by_wanji = t_wanji_manager_task.get_car_wheel_information();
  1001. // }
  1002. // else
  1003. // {
  1004. // LOG(INFO) << " wanji_manager_task error :::::::" << t_wanji_manager_task.get_task_error_manager().to_string() << this;
  1005. // }
  1006. // ------------------- 切换为velodyne ------------------
  1007. //判断任务是否结束, 阻塞 等待, 直到任务完成或者超时
  1008. while (t_velodyne_manager_task.is_task_end() == false)
  1009. {
  1010. if (t_velodyne_manager_task.is_over_time())
  1011. {
  1012. //超时处理。取消任务。
  1013. Velodyne_manager::get_instance_pointer()->cancel_task(&t_velodyne_manager_task);
  1014. t_error.error_manager_reset(Error_code::VELODYNE_MANAGER_TASK_OVER_TIME, Error_level::MINOR_ERROR,
  1015. " t_velodyne_manager_task is_over_time ");
  1016. t_velodyne_manager_task.set_task_error_manager(t_error);
  1017. }
  1018. else
  1019. {
  1020. //继续等待
  1021. std::this_thread::sleep_for(std::chrono::microseconds(1));
  1022. std::this_thread::yield();
  1023. }
  1024. }
  1025. //提取任务单 的错误码
  1026. t_error = t_velodyne_manager_task.get_task_error_manager();
  1027. if (t_error == Error_code::SUCCESS)
  1028. {
  1029. t_car_information_by_velodyne = t_velodyne_manager_task.get_car_wheel_information();
  1030. }
  1031. else
  1032. {
  1033. LOG(INFO) << " velodyne_manager_task error :::::::" << t_velodyne_manager_task.get_task_error_manager().to_string() << this;
  1034. }
  1035. }
  1036. t_result.compare_and_cover_error(t_error);
  1037. //measure 测量模块的最终结果
  1038. Common_data::Car_measure_information t_car_information_result;
  1039. //第4步, 生成反馈消息
  1040. // if(t_car_information_by_wanji.correctness == true )
  1041. // {
  1042. // t_car_information_result.center_x = t_car_information_by_wanji.center_x;
  1043. // t_car_information_result.center_y = t_car_information_by_wanji.center_y;
  1044. // t_car_information_result.car_angle = t_car_information_by_wanji.car_angle;
  1045. // t_car_information_result.wheel_base = t_car_information_by_wanji.wheel_base;
  1046. // t_car_information_result.wheel_width = t_car_information_by_wanji.wheel_width;
  1047. // t_car_information_result.correctness = true;
  1048. // }else{
  1049. // t_result.set_error_level_down(NEGLIGIBLE_ERROR);
  1050. // }
  1051. // ------------------- 切换为velodyne ------------------
  1052. if (t_car_information_by_velodyne.correctness == true)
  1053. {
  1054. t_car_information_result.car_center_x = t_car_information_by_velodyne.car_center_x;
  1055. t_car_information_result.car_center_y = t_car_information_by_velodyne.car_center_y;
  1056. t_car_information_result.car_angle = t_car_information_by_velodyne.car_angle;
  1057. t_car_information_result.car_wheel_base = t_car_information_by_velodyne.car_wheel_base;
  1058. t_car_information_result.car_wheel_width = t_car_information_by_velodyne.car_wheel_width;
  1059. t_car_information_result.correctness = true;
  1060. }
  1061. else
  1062. {
  1063. t_result.set_error_level_down(NEGLIGIBLE_ERROR);
  1064. }
  1065. //第七步, 创建一条答复消息
  1066. message::Ground_detect_response_msg t_ground_detect_response_msg;
  1067. t_ground_detect_response_msg.mutable_base_info()->set_msg_type(message::Message_type::eGround_detect_response_msg);
  1068. t_ground_detect_response_msg.mutable_base_info()->set_timeout_ms(5000);
  1069. t_ground_detect_response_msg.mutable_base_info()->set_sender(message::Communicator::eGround_measurer);
  1070. t_ground_detect_response_msg.mutable_base_info()->set_receiver(message::Communicator::eMain);
  1071. t_ground_detect_response_msg.set_command_key(command_info);
  1072. t_ground_detect_response_msg.mutable_id_struct()->set_terminal_id(terminal_id);
  1073. t_ground_detect_response_msg.mutable_error_manager()->set_error_code(t_result.get_error_code());
  1074. t_ground_detect_response_msg.mutable_error_manager()->set_error_level((message::Error_level)t_result.get_error_level());
  1075. t_ground_detect_response_msg.mutable_error_manager()->set_error_description(t_result.get_error_description());
  1076. t_ground_detect_response_msg.mutable_locate_information()->set_locate_x(t_car_information_result.car_center_x);
  1077. t_ground_detect_response_msg.mutable_locate_information()->set_locate_y(t_car_information_result.car_center_y);
  1078. t_ground_detect_response_msg.mutable_locate_information()->set_locate_angle(t_car_information_result.car_angle);
  1079. t_ground_detect_response_msg.mutable_locate_information()->set_locate_length(t_car_information_result.car_length);
  1080. t_ground_detect_response_msg.mutable_locate_information()->set_locate_width(t_car_information_result.car_width);
  1081. t_ground_detect_response_msg.mutable_locate_information()->set_locate_height(t_car_information_result.car_height);
  1082. t_ground_detect_response_msg.mutable_locate_information()->set_locate_wheel_base(t_car_information_result.car_wheel_base);
  1083. t_ground_detect_response_msg.mutable_locate_information()->set_locate_wheel_width(t_car_information_result.car_wheel_width);
  1084. t_ground_detect_response_msg.mutable_locate_information()->set_locate_front_theta(t_car_information_result.car_front_theta);
  1085. t_ground_detect_response_msg.mutable_locate_information()->set_locate_correct(t_car_information_result.correctness);
  1086. std::string t_msg = t_ground_detect_response_msg.SerializeAsString();
  1087. System_communication::get_instance_references().encapsulate_msg(t_msg);
  1088. std::cout << "huli t_measure_response_msg = " << t_ground_detect_response_msg.DebugString() << std::endl;
  1089. return;
  1090. }