velodyne_manager.cpp 24 KB

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