velodyne_manager.cpp 21 KB

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