wanji_manager.cpp 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675
  1. //
  2. // Created by huli on 2020/9/9.
  3. //
  4. #include "wanji_manager.h"
  5. #include "../tool/proto_tool.h"
  6. Wanji_manager::Wanji_manager()
  7. {
  8. m_wanji_manager_status = E_UNKNOWN;
  9. // mp_cloud_collection = pcl::PointCloud<pcl::PointXYZ>::Ptr(new pcl::PointCloud<pcl::PointXYZ>);
  10. m_cloud_update_time = std::chrono::system_clock::now() - std::chrono::hours(1);
  11. mp_collect_cloud_thread = NULL;
  12. mp_execute_thread = NULL;
  13. mp_wanji_manager_task = NULL;
  14. }
  15. Wanji_manager::~Wanji_manager()
  16. {
  17. wanji_manager_uninit();
  18. }
  19. //初始化 雷达 管理模块。如下三选一
  20. Error_manager Wanji_manager::wanji_manager_init(int terminal_id)
  21. {
  22. return wanji_manager_init_from_protobuf(WANJI_MANAGER_PARAMETER_PATH, terminal_id);
  23. }
  24. //初始化 雷达 管理模块。从文件读取
  25. Error_manager Wanji_manager::wanji_manager_init_from_protobuf(std::string prototxt_path, int terminal_id)
  26. {
  27. wj::wjManagerParams t_wanji_parameters;
  28. if(! proto_tool::read_proto_param(prototxt_path,t_wanji_parameters) )
  29. {
  30. return Error_manager(WJ_MANAGER_READ_PROTOBUF_ERROR,MINOR_ERROR,"Wanji_manager read_proto_param failed");
  31. }
  32. return wanji_manager_init_from_protobuf(t_wanji_parameters, terminal_id);
  33. }
  34. //初始化 雷达 管理模块。从protobuf读取
  35. Error_manager Wanji_manager::wanji_manager_init_from_protobuf(wj::wjManagerParams& wanji_parameters, int terminal_id)
  36. {
  37. LOG(INFO) << " ---wanji_manager_init_from_protobuf run--- "<< this;
  38. Error_manager t_error;
  39. if ( m_wanji_manager_status != E_UNKNOWN)
  40. {
  41. return Error_manager(Error_code::WJ_MANAGER_INIT_ERROR, Error_level::MINOR_ERROR,
  42. " Wanji_manager::wanji_manager_init_from_protobuf init repeat error ");
  43. }
  44. m_terminal_id = terminal_id;
  45. // //创建雷达设备
  46. // int t_wanji_lidar_size = wanji_parameters.wj_lidar_size();
  47. // for(int i=0;i<t_wanji_lidar_size;++i)
  48. // {
  49. // Wanji_lidar_device* p_wanji_lidar_device(new Wanji_lidar_device);
  50. // t_error = p_wanji_lidar_device->init(wanji_parameters.wj_lidar(i));
  51. // // LOG(WARNING) << wanji_parameters.wj_lidar(i).DebugString();
  52. // if ( t_error != Error_code::SUCCESS )
  53. // {
  54. // delete(p_wanji_lidar_device);
  55. // return t_error;
  56. // }
  57. // m_wanji_lidar_device_map[i] = (p_wanji_lidar_device);
  58. // }
  59. // //创建预测算法
  60. // int t_region_workers_size = wanji_parameters.regions_size();
  61. // for(int i=0;i<t_region_workers_size;++i)
  62. // {
  63. // Region_worker* p_region_worker(new Region_worker);
  64. // Point2D_tool::Point2D_box t_point2d_box;
  65. // t_point2d_box.x_min = wanji_parameters.regions(i).minx();
  66. // t_point2d_box.x_max = wanji_parameters.regions(i).maxx();
  67. // t_point2d_box.y_min = wanji_parameters.regions(i).miny();
  68. // t_point2d_box.y_max = wanji_parameters.regions(i).maxy();
  69. // // changed by yct, 传入终端index,仅普爱使用
  70. // t_error = p_region_worker->init(t_point2d_box, &m_cloud_collection_mutex, mp_cloud_collection, i);
  71. // if ( t_error != Error_code::SUCCESS )
  72. // {
  73. // delete(p_region_worker);
  74. // return t_error;
  75. // }
  76. // m_region_worker_map[i] = p_region_worker;
  77. // }
  78. // 20220714 changed by yct
  79. // 根据是否处于分布式模式,决定初始化设备数量
  80. //创建预测算法
  81. std::set<int> lidar_selection_set;
  82. for (int i = 0; i < wanji_parameters.regions_size(); ++i)
  83. {
  84. // 非分布式则创建所有区域,否则只创建对应区域
  85. if (!wanji_parameters.distribution_mode() || (wanji_parameters.distribution_mode() && wanji_parameters.regions(i).region_id() == m_terminal_id))
  86. {
  87. // 分布式模式记录待创建雷达编号
  88. if(wanji_parameters.distribution_mode())
  89. {
  90. for (size_t j = 0; j < wanji_parameters.regions(i).lidar_exts_size(); j++)
  91. {
  92. lidar_selection_set.emplace(wanji_parameters.regions(i).lidar_exts(j).lidar_id());
  93. }
  94. }
  95. Region_worker *p_region_worker(new Region_worker);
  96. t_error = p_region_worker->init(wanji_parameters.regions(i));
  97. if (t_error != Error_code::SUCCESS)
  98. {
  99. delete (p_region_worker);
  100. return t_error;
  101. }
  102. m_region_worker_map[wanji_parameters.regions(i).region_id()] = p_region_worker;
  103. LOG(WARNING) << "region worker " << std::to_string(wanji_parameters.regions(i).region_id()) << " created!!!";
  104. }
  105. }
  106. // 检查是否存在该区域
  107. if(wanji_parameters.distribution_mode() && m_region_worker_map.find(m_terminal_id) == m_region_worker_map.end())
  108. {
  109. LOG(ERROR) << "param error, cannot find region "<<m_terminal_id;
  110. return Error_manager(Error_code::PARAMETER_ERROR, Error_level::MINOR_ERROR,
  111. " wanji_manager::wanji_manager_init_from_protobuf region defined in param cannot be found.");
  112. }
  113. //创建雷达设备
  114. for(int i=0;i<wanji_parameters.wj_lidar_size();++i)
  115. {
  116. // 非分布式模式创建所有雷达,否则只创建对应区域标记的雷达
  117. if (!wanji_parameters.distribution_mode() || (wanji_parameters.distribution_mode() && lidar_selection_set.find(wanji_parameters.wj_lidar(i).lidar_id()) != lidar_selection_set.end() ))
  118. {
  119. #if LIDAR_TYPE == 1
  120. Wanji_lidar_device *p_wj_lidar_device(new Wanji_lidar_device);
  121. #else
  122. Wanji_716N_lidar_device *p_wj_lidar_device(new Wanji_716N_lidar_device);
  123. #endif
  124. // Wanji_lidar_device *p_wj_lidar_device(new Wanji_lidar_device);
  125. t_error = p_wj_lidar_device->init(wanji_parameters.wj_lidar(i));
  126. if (t_error != Error_code::SUCCESS)
  127. {
  128. delete (p_wj_lidar_device);
  129. return t_error;
  130. }
  131. m_wanji_lidar_device_map[wanji_parameters.wj_lidar(i).lidar_id()] = p_wj_lidar_device;
  132. LOG(WARNING) << "wj lidar " << std::to_string(wanji_parameters.wj_lidar(i).lidar_id()) << " created!!!";
  133. // 根据创建的雷达与区域,初始化点云缓存map
  134. for (std::map<int, Region_worker *>::iterator iter = m_region_worker_map.begin(); iter != m_region_worker_map.end(); iter++)
  135. {
  136. wj::Region t_region_param = iter->second->get_param();
  137. std::string t_region_lidar_key = generate_region_lidar_key(iter->first, wanji_parameters.wj_lidar(i).lidar_id());
  138. // 遍历区域内激光内参列表,找到lidar_id对应内参
  139. for (size_t j = 0; j < t_region_param.lidar_exts_size(); j++)
  140. {
  141. if(t_region_param.lidar_exts(j).lidar_id() == wanji_parameters.wj_lidar(i).lidar_id())
  142. {
  143. // 未找到则创建
  144. if (m_region_laser_to_cloud_collection_map.find(t_region_lidar_key) == m_region_laser_to_cloud_collection_map.end())
  145. {
  146. m_region_laser_to_cloud_collection_map.emplace(std::pair<std::string,
  147. std::shared_ptr<Timestamped_cloud_wj>>(t_region_lidar_key, std::shared_ptr<Timestamped_cloud_wj>(new Timestamped_cloud_wj(t_region_param.lidar_exts(j).calib()))));
  148. LOG(WARNING) << "pair of (lidar " << std::to_string(wanji_parameters.wj_lidar(i).lidar_id()) <<", region "<<std::to_string(iter->first) << ") created!!!";
  149. }
  150. else
  151. {
  152. LOG(ERROR) << "region lidar repeated? " << t_region_lidar_key;
  153. }
  154. }
  155. }
  156. }
  157. }
  158. }
  159. // 检查设备与区域个数,不可为0
  160. if(m_region_worker_map.size()<=0 || m_wanji_lidar_device_map.size()<=0)
  161. {
  162. LOG(ERROR) << "param error, region or lidar size error";
  163. return Error_manager(Error_code::PARAMETER_ERROR, Error_level::MINOR_ERROR,
  164. " wanji_manager::wanji_manager_init_from_protobuf region or lidar size is 0");
  165. }
  166. //启动 收集点云的线程。默认 。
  167. m_collect_cloud_condition.reset(false, false, false);
  168. mp_collect_cloud_thread = new std::thread(&Wanji_manager::collect_cloud_thread_fun, this);
  169. //启动 执行的线程。默认 。
  170. m_execute_condition.reset(false, false, false);
  171. mp_execute_thread = new std::thread(&Wanji_manager::execute_thread_fun, this);
  172. m_wanji_manager_status = E_READY;
  173. return Error_code::SUCCESS;
  174. }
  175. //反初始化 雷达 管理模块。
  176. Error_manager Wanji_manager::wanji_manager_uninit()
  177. {
  178. LOG(INFO) << " ---wanji_manager_uninit run--- "<< this;
  179. //关闭线程
  180. if (mp_collect_cloud_thread)
  181. {
  182. m_collect_cloud_condition.kill_all();
  183. }
  184. if (mp_execute_thread)
  185. {
  186. m_execute_condition.kill_all();
  187. }
  188. //回收线程的资源
  189. if (mp_collect_cloud_thread)
  190. {
  191. mp_collect_cloud_thread->join();
  192. delete mp_collect_cloud_thread;
  193. mp_collect_cloud_thread = NULL;
  194. }
  195. if (mp_execute_thread)
  196. {
  197. mp_execute_thread->join();
  198. delete mp_execute_thread;
  199. mp_execute_thread = NULL;
  200. }
  201. //回收雷达设备
  202. for (auto iter = m_wanji_lidar_device_map.begin(); iter != m_wanji_lidar_device_map.end(); ++iter)
  203. {
  204. delete(iter->second);
  205. }
  206. m_wanji_lidar_device_map.clear();
  207. for (auto iter = m_region_worker_map.begin(); iter != m_region_worker_map.end(); ++iter)
  208. {
  209. delete(iter->second);
  210. }
  211. m_region_worker_map.clear();
  212. m_wanji_manager_status = E_UNKNOWN;
  213. return Error_code::SUCCESS;
  214. }
  215. //对外的接口函数,负责接受并处理任务单,
  216. Error_manager Wanji_manager::execute_task(Task_Base* p_wanji_manager_task)
  217. {
  218. LOG(INFO) << " ---Wanji_manager::execute_task run--- "<< this;
  219. Error_manager t_error;
  220. Error_manager t_result;
  221. //检查指针
  222. if (p_wanji_manager_task == NULL) {
  223. return Error_manager(Error_code::POINTER_IS_NULL, Error_level::MINOR_ERROR,
  224. "Wanji_manager::execute_task failed, POINTER_IS_NULL");
  225. }
  226. //检查任务类型,
  227. if (p_wanji_manager_task->get_task_type() != WANJI_MANAGER_TASK)
  228. {
  229. return Error_manager(Error_code::WJ_MANAGER_TASK_TYPE_ERROR, Error_level::MINOR_ERROR,
  230. "laser task type error != WANJI_MANAGER_TASK ");
  231. }
  232. //检查接收方的状态
  233. t_error = check_status();
  234. if ( t_error != SUCCESS )
  235. {
  236. t_result.compare_and_cover_error(t_error);
  237. }
  238. else
  239. {
  240. //接受任务,并将任务的状态改为TASK_SIGNED已签收
  241. mp_wanji_manager_task = (Wanji_manager_task *) p_wanji_manager_task;
  242. mp_wanji_manager_task->set_task_statu(TASK_SIGNED);
  243. //启动雷达管理模块,的核心工作线程
  244. m_wanji_manager_status = E_BUSY;
  245. m_execute_condition.notify_all(true);
  246. //将任务的状态改为 TASK_WORKING 处理中
  247. mp_wanji_manager_task->set_task_statu(TASK_WORKING);
  248. //return 之前要填充任务单里面的错误码.
  249. if (t_result != Error_code::SUCCESS)
  250. {
  251. //忽略轻微故障
  252. if ( t_result.get_error_level() >= Error_level::MINOR_ERROR)
  253. {
  254. //将任务的状态改为 TASK_ERROR 结束错误
  255. mp_wanji_manager_task->set_task_statu(TASK_ERROR);
  256. }
  257. //返回错误码
  258. mp_wanji_manager_task->compare_and_cover_task_error_manager(t_result);
  259. }
  260. }
  261. return t_result;
  262. }
  263. //结束任务单,里面会根据任务的故障等级修正雷达管理模块的状态和任务单的状态
  264. Error_manager Wanji_manager::end_task()
  265. {
  266. m_execute_condition.notify_all(false);
  267. if ( m_wanji_manager_status == E_BUSY ||
  268. m_wanji_manager_status == E_ISSUED_SCAN ||
  269. m_wanji_manager_status == E_WAIT_SCAN ||
  270. m_wanji_manager_status == E_ISSUED_DETECT ||
  271. m_wanji_manager_status == E_WAIT_DETECT )
  272. {
  273. m_wanji_manager_status = E_READY;
  274. }
  275. //else 状态不变
  276. //在结束任务单时,将雷达任务状态改为 TASK_OVER 已结束
  277. if(mp_wanji_manager_task !=NULL)
  278. {
  279. //判断任务单的错误等级,
  280. if ( mp_wanji_manager_task->get_task_error_manager().get_error_level() < Error_level::MINOR_ERROR)
  281. {
  282. //强制改为TASK_OVER,不管它当前在做什么。
  283. mp_wanji_manager_task->set_task_statu(TASK_OVER);
  284. }
  285. else
  286. {
  287. //强制改为 TASK_ERROR,不管它当前在做什么。
  288. mp_wanji_manager_task->set_task_statu(TASK_ERROR);
  289. }
  290. }
  291. return Error_code::SUCCESS;
  292. }
  293. //取消任务单,由发送方提前取消任务单
  294. Error_manager Wanji_manager::cancel_task(Task_Base* p_wanji_manager_task)
  295. {
  296. //关闭子线程
  297. m_execute_condition.notify_all(false);
  298. //确保内部线程已经停下
  299. while (m_execute_condition.is_working())
  300. {
  301. }
  302. //强制改为 TASK_DEAD,不管它当前在做什么。
  303. mp_wanji_manager_task->set_task_statu(TASK_DEAD);
  304. if ( m_wanji_manager_status == E_BUSY ||
  305. m_wanji_manager_status == E_ISSUED_SCAN ||
  306. m_wanji_manager_status == E_WAIT_SCAN ||
  307. m_wanji_manager_status == E_ISSUED_DETECT ||
  308. m_wanji_manager_status == E_WAIT_DETECT )
  309. {
  310. m_wanji_manager_status = E_READY;
  311. }
  312. //else 状态不变
  313. return Error_code::SUCCESS;
  314. }
  315. //检查雷达状态,是否正常运行
  316. Error_manager Wanji_manager::check_status()
  317. {
  318. if ( m_wanji_manager_status == E_READY )
  319. {
  320. return Error_code::SUCCESS;
  321. }
  322. else if ( m_wanji_manager_status == E_BUSY ||
  323. m_wanji_manager_status == E_ISSUED_SCAN ||
  324. m_wanji_manager_status == E_WAIT_SCAN ||
  325. m_wanji_manager_status == E_ISSUED_DETECT ||
  326. m_wanji_manager_status == E_WAIT_DETECT )
  327. {
  328. return Error_manager(Error_code::WJ_MANAGER_STATUS_BUSY, Error_level::NEGLIGIBLE_ERROR,
  329. " Laser_manager::check_status error ");
  330. }
  331. else
  332. {
  333. return Error_manager(Error_code::WJ_MANAGER_STATUS_ERROR, Error_level::MINOR_ERROR,
  334. " Laser_manager::check_status error ");
  335. }
  336. return Error_code::SUCCESS;
  337. }
  338. //判断是否为待机,如果已经准备好,则可以执行任务。
  339. bool Wanji_manager::is_ready()
  340. {
  341. return m_wanji_manager_status == E_READY;
  342. }
  343. Wanji_manager::Wanji_manager_status Wanji_manager::get_status()
  344. {
  345. return m_wanji_manager_status;
  346. }
  347. #if LIDAR_TYPE == 1
  348. std::map<int, Wanji_lidar_device*> & Wanji_manager::get_wanji_lidar_device_map()
  349. #else
  350. std::map<int, Wanji_716N_lidar_device*> & Wanji_manager::get_wanji_lidar_device_map()
  351. #endif
  352. {
  353. return m_wanji_lidar_device_map;
  354. }
  355. std::map<int, Region_worker *> & Wanji_manager::get_region_worker_map()
  356. {
  357. return m_region_worker_map;
  358. }
  359. //自动收集点云的线程函数
  360. void Wanji_manager::collect_cloud_thread_fun()
  361. {
  362. LOG(INFO) << " Wanji_manager::collect_cloud_thread_fun() start "<< this;
  363. Error_manager t_error;
  364. Error_manager t_result;
  365. static int cloud_count=-1;
  366. while (m_collect_cloud_condition.is_alive())
  367. {
  368. cloud_count++;
  369. //暂时固定为一个扫描周期, 就循环一次
  370. //后期可以根据每个小雷达的刷新情况, 实时更改总点云.....
  371. m_collect_cloud_condition.wait_for_millisecond(WANJI_716_SCAN_CYCLE_MS);
  372. if ( m_collect_cloud_condition.is_alive() )
  373. {
  374. std::chrono::system_clock::time_point t_command_time = std::chrono::system_clock::now();
  375. {
  376. //全局加锁, 并清空点云.
  377. std::unique_lock<std::mutex> t_lock(m_cloud_collection_mutex);
  378. // mp_cloud_collection->clear();
  379. //重新收集最近的点云, 不允许阻塞
  380. // added by yct, 测试雷达功能
  381. pcl::PointCloud<pcl::PointXYZ>::Ptr t_cloud = pcl::PointCloud<pcl::PointXYZ>::Ptr(new pcl::PointCloud<pcl::PointXYZ>);
  382. for (auto iter = m_wanji_lidar_device_map.begin(); iter != m_wanji_lidar_device_map.end(); ++iter)
  383. {
  384. t_cloud->clear();
  385. t_error = iter->second->get_last_cloud(t_cloud,t_command_time);
  386. if (t_error != Error_code::SUCCESS)
  387. {
  388. t_result.compare_and_cover_error(t_error);
  389. LOG_EVERY_N(WARNING, 9000) << "cloud timeout lidar id: "<<iter->first;
  390. // 将该雷达对应所有区域点云清空
  391. }else if(cloud_count==5){
  392. std::string t_filename = std::string("./wj_intrin_cloud_")+std::to_string(iter->first)+".txt";
  393. save_cloud_txt(t_cloud, t_filename);
  394. LOG(INFO) << "lidar intrin cloud has been saved in " + t_filename;
  395. }
  396. // 点云放入对应缓存
  397. for (std::map<int, Region_worker *>::iterator iter_region = m_region_worker_map.begin(); iter_region != m_region_worker_map.end(); iter_region++)
  398. {
  399. if (!iter_region->second->check_lidar_inside(iter->first))
  400. continue;
  401. std::string t_region_lidar_key = generate_region_lidar_key(iter_region->first, iter->first);
  402. // 未找到则报错
  403. if (m_region_laser_to_cloud_collection_map.find(t_region_lidar_key) == m_region_laser_to_cloud_collection_map.end())
  404. {
  405. LOG(ERROR) << "current lidar cloud not initialized: " << t_region_lidar_key;
  406. }
  407. else
  408. {
  409. if (t_error != SUCCESS)
  410. {
  411. m_region_laser_to_cloud_collection_map[t_region_lidar_key]->clearCloud();
  412. // LOG(WARNING) << "编号" << iter->first << "雷达在区域" << iter_region->first << "中点云清除 " << t_error.to_string();
  413. }
  414. else
  415. {
  416. // LOG(WARNING) << "map count: "<< m_region_laser_to_cloud_collection_map.size();
  417. m_region_laser_to_cloud_collection_map[t_region_lidar_key]->setCloud(t_cloud);
  418. // LOG(WARNING) << "编号" << iter->first << "雷达在区域" << iter_region->first << "中点云 " << t_cloud->size();
  419. }
  420. }
  421. }
  422. }
  423. update_region_cloud();
  424. // if ( t_result == SUCCESS && mp_cloud_collection->size()>0 )
  425. // {
  426. // m_cloud_update_time = std::chrono::system_clock::now();
  427. // //成功则唤醒预测算法
  428. // start_auto_detect();
  429. // }
  430. // else
  431. // {
  432. // mp_cloud_collection->clear();
  433. // //失败则关闭预测算法
  434. // stop_auto_detect();
  435. // // LOG(WARNING) << t_result.to_string();
  436. // }
  437. }
  438. }
  439. t_result.error_manager_clear_all();
  440. }
  441. LOG(INFO) << " Wanji_manager::collect_cloud_thread_fun() end "<< this;
  442. return;
  443. }
  444. //更新点云,自行计算
  445. Error_manager Wanji_manager::update_region_cloud()
  446. {
  447. // static std::chrono::steady_clock::time_point t0 = std::chrono::steady_clock::now();
  448. for (auto iter = m_region_worker_map.begin(); iter != m_region_worker_map.end(); ++iter)
  449. {
  450. pcl::PointCloud<pcl::PointXYZ>::Ptr t_region_cloud = pcl::PointCloud<pcl::PointXYZ>::Ptr(new pcl::PointCloud<pcl::PointXYZ>);
  451. // 每个区域需要所有雷达点云,任何一个缺失都不可进入计算流程。
  452. // 所有点云都缺失,则可能是当前区域空闲,此时不进行计算,也不弹出警告
  453. // 在遍历完雷达后,该数组中存在4个元素才可进入计算流程
  454. std::vector<int> corresponding_lidar_ids;
  455. std::vector<int> corresponding_lidar_cloud_size;
  456. // 遍历激光列表,找到终端对应点云数据,经过外参变换后叠加更新区域点云
  457. for (auto iter_lidar = m_wanji_lidar_device_map.begin(); iter_lidar != m_wanji_lidar_device_map.end(); ++iter_lidar)
  458. {
  459. if(!iter->second->check_lidar_inside(iter_lidar->first))
  460. continue;
  461. std::string t_region_lidar_key = generate_region_lidar_key(iter->first, iter_lidar->first);
  462. //将点云经过外参变换后,添加到对应区域中
  463. if(m_region_laser_to_cloud_collection_map.find(t_region_lidar_key) != m_region_laser_to_cloud_collection_map.end())
  464. {
  465. std::shared_ptr<Timestamped_cloud_wj> t_timed_cloud = m_region_laser_to_cloud_collection_map[t_region_lidar_key];
  466. if (t_timed_cloud->m_cloud->size() > 0)
  467. {
  468. t_region_cloud->operator+=(*t_timed_cloud->transCloud());
  469. corresponding_lidar_ids.push_back(iter_lidar->first);
  470. corresponding_lidar_cloud_size.push_back(t_timed_cloud->m_cloud->size());
  471. // LOG(WARNING) << "编号" << iter_lidar->first << "雷达在入口" << iter->first << "中点云已添加" << t_timed_cloud->m_cloud->size();
  472. }
  473. else
  474. {
  475. LOG_EVERY_N(WARNING, 1000) << "编号" << iter_lidar->first << "雷达在入口" << iter->first << "中未获得点云。";
  476. continue;
  477. }
  478. }
  479. else
  480. {
  481. LOG_EVERY_N(WARNING, 1) << "编号" << iter_lidar->first << "雷达在入口" << iter->first << "中未初始化完全。";
  482. continue;
  483. }
  484. }
  485. if(corresponding_lidar_ids.size() != corresponding_lidar_cloud_size.size())
  486. {
  487. LOG_EVERY_N(ERROR, 1) << "入口" << iter->first << "出现访问不可达路径情况,请检查!!!!!";
  488. continue;
  489. }
  490. // 必须传入空点云以避免入口状态不更新
  491. // wj 4角校验不可简单根据点数判断
  492. if(corresponding_lidar_ids.size() == 4 ){
  493. // 检查两边点云个数,若差异过大则清除点云,传入空点云以更新结果
  494. int max_cloud_size = std::max(corresponding_lidar_cloud_size[0], corresponding_lidar_cloud_size[1]);
  495. max_cloud_size = std::max(corresponding_lidar_cloud_size[2], max_cloud_size);
  496. max_cloud_size = std::max(corresponding_lidar_cloud_size[3], max_cloud_size);
  497. int min_cloud_size = std::min(corresponding_lidar_cloud_size[0], corresponding_lidar_cloud_size[1]);
  498. min_cloud_size = std::min(corresponding_lidar_cloud_size[2], min_cloud_size);
  499. min_cloud_size = std::min(corresponding_lidar_cloud_size[2], min_cloud_size);
  500. // 极限相差10倍,或一边仅不到10个点,则清除点云,传入空点云以更新结果
  501. if ((min_cloud_size < 10 && max_cloud_size > 30) || (min_cloud_size >= 10 && max_cloud_size > 10 * min_cloud_size + 10))
  502. {
  503. LOG_EVERY_N(ERROR, 60) << "入口" << iter->first << "两片点云差异过大,分别为:"<<max_cloud_size<<"个与"<<min_cloud_size<<"个,请检查!!! ";
  504. // 清除点云
  505. t_region_cloud->clear();
  506. }
  507. else
  508. {
  509. // 不用修改点云内容
  510. }
  511. }
  512. else if (corresponding_lidar_ids.size() < 4)
  513. {
  514. LOG_EVERY_N(WARNING, 60) << "检测到入口" << iter->first << "点云缺失, 找到" << corresponding_lidar_ids.size() << "个雷达的点云";
  515. // 只有半边点云,将其清除
  516. t_region_cloud->clear();
  517. }
  518. else{
  519. // 此入口可能暂时空闲,左右都没有点
  520. // 清除点云,确保传入为空点云
  521. t_region_cloud->clear();
  522. }
  523. // 统一到此处更新点云,异常情况下将点云清除,保证入口正常更新
  524. iter->second->update_cloud(t_region_cloud);
  525. // std::chrono::steady_clock::time_point t1 = std::chrono::steady_clock::now();
  526. // std::chrono::duration<double> time_used_update = std::chrono::duration_cast<std::chrono::duration<double>>(t1 - t0);
  527. // t0 = std::chrono::steady_clock::now();
  528. // LOG(WARNING) << "send to region " << iter->first << " with cloud size: " << t_region_cloud->size() ;
  529. }
  530. return Error_code::SUCCESS;
  531. }
  532. //开始自动预测的算法线程
  533. Error_manager Wanji_manager::start_auto_detect()
  534. {
  535. for (auto iter = m_region_worker_map.begin(); iter != m_region_worker_map.end(); ++iter)
  536. {
  537. iter->second->start_auto_detect();
  538. }
  539. return Error_code::SUCCESS;
  540. }
  541. //关闭自动预测的算法线程
  542. Error_manager Wanji_manager::stop_auto_detect()
  543. {
  544. for (auto iter = m_region_worker_map.begin(); iter != m_region_worker_map.end(); ++iter)
  545. {
  546. iter->second->stop_auto_detect();
  547. }
  548. return Error_code::SUCCESS;
  549. }
  550. //执行外界任务的执行函数
  551. void Wanji_manager::execute_thread_fun()
  552. {
  553. LOG(INFO) << " Wanji_manager::execute_thread_fun() start "<< this;
  554. Error_manager t_error;
  555. Common_data::Car_wheel_information t_car_wheel_information;
  556. //雷达管理的独立线程,负责控制管理所有的雷达。
  557. while (m_execute_condition.is_alive())
  558. {
  559. m_execute_condition.wait();
  560. if ( m_execute_condition.is_alive() )
  561. {
  562. std::this_thread::yield();
  563. //重新循环必须清除错误码.
  564. t_error.error_manager_clear_all();
  565. if ( mp_wanji_manager_task == NULL )
  566. {
  567. //忽略任务, 直接停止线程
  568. m_wanji_manager_status = E_READY;
  569. m_execute_condition.notify_all(false);
  570. }
  571. //万集内部全自动运行, 只需要根据时间去获取想要的就行了.
  572. //目前楚天是唯一的预测算法, 如果后续有多个出入口,则使用任务单的终端id来获取指定的.
  573. if ( m_region_worker_map.size() > 0 && m_region_worker_map.find(mp_wanji_manager_task->get_terminal_id()) != m_region_worker_map.end())
  574. {
  575. t_error = m_region_worker_map[mp_wanji_manager_task->get_terminal_id()]->get_current_wheel_information(&t_car_wheel_information, mp_wanji_manager_task->get_command_time());
  576. if ( t_error == Error_code::SUCCESS )
  577. {
  578. //添加车轮的预测结果
  579. mp_wanji_manager_task->set_car_wheel_information(t_car_wheel_information);
  580. //正常完成任务
  581. end_task();
  582. }
  583. else
  584. {
  585. //如果在指令时间1秒后都没有成功获取结果, 返回错误原因
  586. if ( std::chrono::system_clock::now() - m_region_worker_map[mp_wanji_manager_task->get_terminal_id()]->get_detect_updata_time() < std::chrono::milliseconds(WANJI_MANAGER_EXECUTE_TIMEOUT_MS) )
  587. {
  588. //没有超时, 那么就等1ms, 然后重新循环
  589. std::this_thread::sleep_for(std::chrono::milliseconds(1));
  590. std::this_thread::yield();
  591. }
  592. else
  593. {
  594. //因为故障,而提前结束任务.
  595. mp_wanji_manager_task->compare_and_cover_task_error_manager(t_error);
  596. end_task();
  597. }
  598. }
  599. }
  600. }
  601. }
  602. LOG(INFO) << " Wanji_manager::execute_thread_fun() end "<< this;
  603. return;
  604. }