locate_manager.cpp 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700
  1. //
  2. // Created by zx on 2019/12/30.
  3. //
  4. #include "locate_manager.h"
  5. #include "../tool/proto_tool.h"
  6. #include "locate_parameter.pb.h"
  7. #include <pcl/filters//voxel_grid.h>
  8. #include <pcl/filters/passthrough.h>
  9. #include <glog/logging.h>
  10. #include <string.h>
  11. Locate_manager::Locate_manager()
  12. {
  13. mp_point_sift = NULL;
  14. mp_cnn3d = NULL;
  15. m_locate_manager_status = LOCATE_MANAGER_UNKNOW;
  16. m_locate_manager_working_flag = false;
  17. mp_locate_manager_thread = NULL;
  18. mp_locate_manager_task = NULL;
  19. mp_locate_information = NULL;
  20. m_save_flag = false;
  21. // m_save_path = "";
  22. }
  23. Locate_manager::~Locate_manager()
  24. {
  25. Locate_manager_uninit();
  26. }
  27. //初始化 定位 管理模块。如下三选一LOCATE_PARAMETER_PATH
  28. Error_manager Locate_manager::Locate_manager_init()
  29. {
  30. return Locate_manager_init_from_protobuf(LOCATE_PARAMETER_PATH);
  31. }
  32. //初始化 定位 管理模块。从文件读取
  33. Error_manager Locate_manager::Locate_manager_init_from_protobuf(std::string prototxt_path)
  34. {
  35. Measure::LocateParameter t_locate_parameters;
  36. if(! proto_tool::read_proto_param(prototxt_path,t_locate_parameters) )
  37. {
  38. return Error_manager(LOCATER_MANAGER_READ_PROTOBUF_ERROR,MINOR_ERROR,"Locate_manager read_proto_param failed");
  39. }
  40. return Locate_manager_init_from_protobuf(t_locate_parameters);
  41. }
  42. //初始化 定位 管理模块。从protobuf读取
  43. Error_manager Locate_manager::Locate_manager_init_from_protobuf(Measure::LocateParameter& locate_parameters)
  44. {
  45. LOG(INFO) << " ---Locate_manager_init_from_protobuf run--- "<< this;
  46. Error_manager t_error;
  47. m_locate_manager_working_flag = false;
  48. int point_size = locate_parameters.seg_parameter().point_size();
  49. int cls_num = locate_parameters.seg_parameter().cls_num();
  50. double freq = locate_parameters.seg_parameter().freq();
  51. std::string graph = locate_parameters.seg_parameter().graph();
  52. std::string cpkt = locate_parameters.seg_parameter().cpkt();
  53. Measure::Area3d area = locate_parameters.seg_parameter().area();
  54. Cloud_box t_cloud_box;
  55. t_cloud_box.x_min = area.x_min();
  56. t_cloud_box.x_max = area.x_max();
  57. t_cloud_box.y_min = area.y_min();
  58. t_cloud_box.y_max = area.y_max();
  59. t_cloud_box.z_min = area.z_min();
  60. t_cloud_box.z_max = area.z_max();
  61. mp_point_sift = new Point_sift_segmentation(point_size,cls_num,freq,t_cloud_box);
  62. std::string graph_file = graph;
  63. std::string cpkt_file = cpkt;
  64. std::cout << "graph_file" << graph_file << std::endl;
  65. std::cout << "cpkt_file" << cpkt_file << std::endl;
  66. t_error=mp_point_sift->init(graph_file,cpkt_file);
  67. if(t_error!=SUCCESS)
  68. {
  69. LOG(ERROR)<<t_error.to_string();
  70. return t_error;
  71. }
  72. LOG(INFO)<<" pointSift init SUCCESS, graph = "<< graph;
  73. int t_cnn3d_length = locate_parameters.net_3dcnn_parameter().length();
  74. int t_cnn3d_width = locate_parameters.net_3dcnn_parameter().width();
  75. int t_cnn3d_height = locate_parameters.net_3dcnn_parameter().height();
  76. int t_cnn3d_nclass = locate_parameters.net_3dcnn_parameter().nclass();
  77. int t_cnn3d_freq = locate_parameters.net_3dcnn_parameter().freq();
  78. mp_cnn3d = new Cnn3d_segmentation(t_cnn3d_length, t_cnn3d_width, t_cnn3d_height, t_cnn3d_freq, t_cnn3d_nclass);
  79. std::string weights = locate_parameters.net_3dcnn_parameter().weights_file();
  80. t_error=mp_cnn3d->init(weights);
  81. if(t_error!=SUCCESS)
  82. {
  83. LOG(ERROR)<<t_error.to_string();
  84. return t_error;
  85. }
  86. LOG(INFO)<<" 3dcnn Init SUCCESS ";
  87. m_locate_manager_status = LOCATE_MANAGER_READY;
  88. //启动雷达管理模块的内部线程。默认wait。
  89. m_locate_manager_condition.reset(false, false, false);
  90. mp_locate_manager_thread = new std::thread(&Locate_manager::thread_work, this);
  91. return SUCCESS;
  92. }
  93. //反初始化 定位 管理模块。
  94. Error_manager Locate_manager::Locate_manager_uninit()
  95. {
  96. LOG(INFO) << " ---Locate_manager_uninit run--- "<< this;
  97. //关闭线程
  98. if (mp_locate_manager_thread)
  99. {
  100. m_locate_manager_condition.kill_all();
  101. }
  102. //回收线程的资源
  103. if (mp_locate_manager_thread)
  104. {
  105. mp_locate_manager_thread->join();
  106. delete mp_locate_manager_thread;
  107. mp_locate_manager_thread = NULL;
  108. }
  109. //回收内存
  110. if ( mp_point_sift !=NULL )
  111. {
  112. delete mp_point_sift;
  113. mp_point_sift = NULL;
  114. }
  115. if ( mp_cnn3d !=NULL )
  116. {
  117. delete mp_cnn3d;
  118. mp_cnn3d = NULL;
  119. }
  120. m_locate_manager_status = LOCATE_MANAGER_UNKNOW;
  121. m_locate_manager_working_flag = false;
  122. return Error_code::SUCCESS;
  123. }
  124. //对外的接口函数,负责接受并处理任务单,
  125. //input:p_locate_task 定位任务单,基类的指针,指向子类的实例,(多态)
  126. Error_manager Locate_manager::execute_task(Task_Base* p_locate_task)
  127. {
  128. LOG(INFO) << " ---Locate_manager::execute_task run--- "<< this;
  129. Error_manager t_error;
  130. Error_manager t_result;
  131. //检查指针
  132. if (p_locate_task == NULL) {
  133. return Error_manager(Error_code::POINTER_IS_NULL, Error_level::MINOR_ERROR,
  134. "Laser_manager::execute_task failed, POINTER_IS_NULL");
  135. }
  136. //检查任务类型,
  137. if (p_locate_task->get_task_type() != LOCATE_MANGER_TASK)
  138. {
  139. return Error_manager(Error_code::LOCATER_MANAGER_TASK_TYPE_ERROR, Error_level::MINOR_ERROR,
  140. "Locate_manager::execute_task get_task_type() != LOCATE_MANGER_TASK ");
  141. }
  142. //检查接收方的状态
  143. std::cout << "m_locate_manager_status"<<m_locate_manager_status << std::endl;
  144. t_error = check_status();
  145. if ( t_error != SUCCESS )
  146. {
  147. t_result.compare_and_cover_error(t_error);
  148. }
  149. else
  150. {
  151. //接受任务,并将任务的状态改为TASK_SIGNED已签收
  152. mp_locate_manager_task = (Locate_manager_task *) p_locate_task;
  153. mp_locate_manager_task->set_task_statu(TASK_SIGNED);
  154. //检查消息内容是否正确,
  155. //检查三维点云指针
  156. if (mp_locate_manager_task->get_task_point_cloud_map() == NULL)
  157. {
  158. t_error.error_manager_reset(Error_code::POINTER_IS_NULL,
  159. Error_level::MINOR_ERROR,
  160. "execute_task mp_task_point_cloud is null");
  161. t_result.compare_and_cover_error(t_error);
  162. }
  163. else if ( mp_locate_manager_task->get_task_cloud_lock() == NULL )
  164. {
  165. t_error.error_manager_reset(Error_code::POINTER_IS_NULL,
  166. Error_level::MINOR_ERROR,
  167. "execute_task mp_task_cloud_lock is null");
  168. t_result.compare_and_cover_error(t_error);
  169. }
  170. else
  171. {
  172. //校验map的大小
  173. if ( (mp_locate_manager_task->get_task_point_cloud_map()->size() ==1 && mp_locate_manager_task->get_cloud_aggregation_flag() == true)
  174. || ( (mp_locate_manager_task->get_task_point_cloud_map()->size() == CNN3D_WHEEL_NUMBER
  175. || mp_locate_manager_task->get_task_point_cloud_map()->size() == CNN3D_WHEEL_NUMBER-1 )
  176. && mp_locate_manager_task->get_cloud_aggregation_flag() == false) )
  177. {
  178. //解析任务单,将任务单的数据保存在本地.
  179. mp_locate_information = mp_locate_manager_task->get_task_locate_information_ex();
  180. m_save_flag = mp_locate_manager_task->get_task_save_flag();
  181. m_save_path = mp_locate_manager_task->get_task_save_path();
  182. //启动定位管理模块,的核心工作线程
  183. m_locate_manager_status = LOCATE_MANAGER_SIFT;
  184. m_locate_manager_working_flag = true;
  185. m_locate_manager_condition.notify_all(true);
  186. //通知 thread_work 子线程启动。
  187. //将任务的状态改为 TASK_WORKING 处理中
  188. mp_locate_manager_task->set_task_statu(TASK_WORKING);
  189. }
  190. else
  191. {
  192. t_error.error_manager_reset(Error_code::LOCATER_MANAGER_CLOUD_MAP_ERROR,
  193. Error_level::MINOR_ERROR,
  194. "execute_task input map error");
  195. t_result.compare_and_cover_error(t_error);
  196. }
  197. }
  198. //return 之前要填充任务单里面的错误码.
  199. if (t_result != Error_code::SUCCESS)
  200. {
  201. //忽略轻微故障
  202. if ( t_result.get_error_level() >= Error_level::MINOR_ERROR)
  203. {
  204. //将任务的状态改为 TASK_ERROR 结束错误
  205. mp_locate_manager_task->set_task_statu(TASK_ERROR);
  206. }
  207. //返回错误码
  208. mp_locate_manager_task->set_task_error_manager(t_result);
  209. }
  210. }
  211. return t_result;
  212. }
  213. //检查状态,是否正常运行
  214. Error_manager Locate_manager::check_status()
  215. {
  216. if ( m_locate_manager_status == LOCATE_MANAGER_READY )
  217. {
  218. return Error_code::SUCCESS;
  219. }
  220. else if ( m_locate_manager_status == LOCATE_MANAGER_SIFT
  221. || m_locate_manager_status == LOCATE_MANAGER_CAR
  222. || m_locate_manager_status == LOCATE_MANAGER_WHEEL)
  223. {
  224. return Error_manager(Error_code::LOCATER_MANAGER_STATUS_BUSY, Error_level::NEGLIGIBLE_ERROR,
  225. " Locate_manager::check_status error ");
  226. }
  227. else
  228. {
  229. return Error_manager(Error_code::LOCATER_MANAGER_STATUS_ERROR, Error_level::MINOR_ERROR,
  230. " Locate_manager::check_status error ");
  231. }
  232. return Error_code::SUCCESS;
  233. }
  234. //结束任务单,里面会根据任务的故障等级修正雷达管理模块的状态和任务单的状态
  235. Error_manager Locate_manager::end_task()
  236. {
  237. LOG(INFO) << " ---Locate_manager::end_task run---"<< this;
  238. //关闭子线程
  239. m_locate_manager_working_flag=false;
  240. m_locate_manager_condition.notify_all(false);
  241. //释放缓存
  242. mp_locate_information = NULL;
  243. m_cloud_wheel_map.clear();
  244. m_cloud_car_map.clear();
  245. //在结束任务单时,将雷达任务状态改为 TASK_OVER 已结束
  246. if(mp_locate_manager_task !=NULL)
  247. {
  248. //判断任务单的错误等级,
  249. if ( mp_locate_manager_task->get_task_error_manager().get_error_level() < Error_level::MINOR_ERROR)
  250. {
  251. if ( m_locate_manager_status == LOCATE_MANAGER_SIFT
  252. || m_locate_manager_status == LOCATE_MANAGER_CAR
  253. || m_locate_manager_status == LOCATE_MANAGER_WHEEL)
  254. {
  255. //故障等级为 NORMAL NEGLIGIBLE_ERROR MINOR_ERROR,则恢复待机,可以接受下一次的任务单
  256. m_locate_manager_status = LOCATE_MANAGER_READY;
  257. }
  258. //else 状态不变
  259. //强制改为TASK_OVER,不管它当前在做什么。
  260. mp_locate_manager_task->set_task_statu(TASK_OVER);
  261. }
  262. else
  263. {
  264. //故障等级为 MAJOR_ERROR CRITICAL_ERROR,则雷达故障,不可以再接受任务单。
  265. m_locate_manager_status = LOCATE_MANAGER_FAULT;
  266. //强制改为 TASK_ERROR,不管它当前在做什么。
  267. mp_locate_manager_task->set_task_statu(TASK_ERROR);
  268. }
  269. }
  270. return Error_code::SUCCESS;
  271. }
  272. //取消任务单,由发送方提前取消任务单
  273. Error_manager Locate_manager::cancel_task()
  274. {
  275. end_task();
  276. //强制改为 TASK_DEAD,不管它当前在做什么。
  277. mp_locate_manager_task->set_task_statu(TASK_DEAD);
  278. return Error_code::SUCCESS;
  279. }
  280. //判断是否为待机,如果已经准备好,则可以执行任务。
  281. bool Locate_manager::is_ready()
  282. {
  283. return m_locate_manager_status == LOCATE_MANAGER_READY;;
  284. }
  285. Locate_manager::Locate_manager_status Locate_manager::get_locate_manager_status()
  286. {
  287. return m_locate_manager_status;
  288. }
  289. //mp_locate_manager_thread 线程执行函数,
  290. //thread_work 内部线程负责locate定位分析整车的信息,并且回收汇总雷达的数据
  291. void Locate_manager::thread_work()
  292. {
  293. LOG(INFO) << " -------------------------mp_locate_manager_thread start "<< this;
  294. Error_manager t_error;
  295. Error_manager t_result;
  296. //定位管理的独立线程,
  297. while (m_locate_manager_condition.is_alive())
  298. {
  299. m_locate_manager_condition.wait();
  300. if ( m_locate_manager_condition.is_alive() )
  301. {
  302. std::this_thread::yield();
  303. //重新循环必须清除错误码.
  304. t_error.error_manager_clear_all();
  305. t_result.error_manager_clear_all();
  306. if ( mp_locate_manager_task == NULL )
  307. {
  308. m_locate_manager_status = LOCATE_MANAGER_FAULT;
  309. m_locate_manager_working_flag = false;
  310. m_locate_manager_condition.notify_all(false);
  311. }
  312. else if (mp_locate_manager_task->get_task_point_cloud_map() == NULL)
  313. {
  314. t_error.error_manager_reset(Error_code::POINTER_IS_NULL,
  315. Error_level::MINOR_ERROR,
  316. "thread_work mp_task_point_cloud is null");
  317. t_result.compare_and_cover_error(t_error);
  318. //因为故障,而提前结束任务.
  319. mp_locate_manager_task->compare_and_cover_task_error_manager(t_result);
  320. end_task();
  321. }
  322. else if ( mp_locate_manager_task->get_task_cloud_lock() == NULL )
  323. {
  324. t_error.error_manager_reset(Error_code::POINTER_IS_NULL,
  325. Error_level::MINOR_ERROR,
  326. "thread_work mp_task_cloud_lock is null");
  327. t_result.compare_and_cover_error(t_error);
  328. //因为故障,而提前结束任务.
  329. mp_locate_manager_task->compare_and_cover_task_error_manager(t_result);
  330. end_task();
  331. }
  332. //第一步
  333. //point_sift 点筛选分割 , 将输入点云分割为车身和轮胎,
  334. //注:地面会在box切割的时候,使用z轴 z_min 直接切除
  335. else if(m_locate_manager_status == LOCATE_MANAGER_SIFT)
  336. {
  337. m_cloud_wheel_map.clear();
  338. m_cloud_car_map.clear();
  339. //定位筛选,将输入点云map拆分为车轮和车身的map
  340. t_error = locate_manager_sift();
  341. t_result.compare_and_cover_error(t_error);
  342. //故障汇总, 只处理2级以上的故障
  343. if(t_result.get_error_level() >= MINOR_ERROR)
  344. {
  345. m_locate_manager_status = LOCATE_MANAGER_FAULT;
  346. mp_locate_manager_task->set_task_statu(TASK_ERROR);
  347. //因为故障,而提前结束任务.
  348. mp_locate_manager_task->compare_and_cover_task_error_manager(t_result);
  349. end_task();
  350. }
  351. else
  352. {
  353. //成功则 进入到下一个阶段,
  354. m_locate_manager_status = LOCATE_MANAGER_CAR;
  355. }
  356. }
  357. //第二步
  358. //_measure_height 计算汽车的长宽高
  359. else if(m_locate_manager_status == LOCATE_MANAGER_CAR)
  360. {
  361. //计算汽车长宽高,
  362. t_error = locate_manager_locate_car();
  363. t_result.compare_and_cover_error(t_error);
  364. //故障汇总, 只处理2级以上的故障
  365. if(t_result.get_error_level() >= MINOR_ERROR)
  366. {
  367. m_locate_manager_status = LOCATE_MANAGER_FAULT;
  368. mp_locate_manager_task->set_task_statu(TASK_ERROR);
  369. //因为故障,而提前结束任务.
  370. mp_locate_manager_task->compare_and_cover_task_error_manager(t_result);
  371. end_task();
  372. }
  373. else
  374. {
  375. //成功则 进入到下一个阶段,
  376. m_locate_manager_status = LOCATE_MANAGER_WHEEL;
  377. }
  378. }
  379. //第三步
  380. //locate_wheel 解析车轮信息
  381. else if(m_locate_manager_status == LOCATE_MANAGER_WHEEL)
  382. {
  383. //解析车轮信息,
  384. t_error = locate_manager_locate_wheel();
  385. t_result.compare_and_cover_error(t_error);
  386. //故障汇总, 只处理2级以上的故障
  387. if(t_result.get_error_level() >= MINOR_ERROR)
  388. {
  389. m_locate_manager_status = LOCATE_MANAGER_FAULT;
  390. mp_locate_manager_task->set_task_statu(TASK_ERROR);
  391. //因为故障,而提前结束任务.
  392. mp_locate_manager_task->compare_and_cover_task_error_manager(t_result);
  393. end_task();
  394. }
  395. else
  396. {
  397. m_locate_manager_working_flag = false;
  398. m_locate_manager_condition.set_pass_ever(false);
  399. //正常结束任务
  400. mp_locate_manager_task->compare_and_cover_task_error_manager(t_result);
  401. end_task();
  402. }
  403. }
  404. }
  405. }
  406. LOG(INFO) << " ---------------------------mp_locate_manager_thread end :"<<this;
  407. return;
  408. }
  409. //定位筛选,将输入点云map拆分为车轮和车身的map.(数据可以直接从locate_manager内部获取)
  410. Error_manager Locate_manager::locate_manager_sift()
  411. {
  412. if ( mp_locate_manager_task == NULL )
  413. {
  414. return Error_manager(Error_code::POINTER_IS_NULL, Error_level::MINOR_ERROR,
  415. " Locate_manager::locate_manager_sift() error ");
  416. }
  417. else if (mp_locate_manager_task->get_task_point_cloud_map() == NULL)
  418. {
  419. return Error_manager(Error_code::POINTER_IS_NULL,
  420. Error_level::MINOR_ERROR,
  421. "locate_manager_sift mp_task_point_cloud is null");
  422. }
  423. else if ( mp_locate_manager_task->get_task_cloud_lock() == NULL )
  424. {
  425. return Error_manager(Error_code::POINTER_IS_NULL,
  426. Error_level::MINOR_ERROR,
  427. "locate_manager_sift mp_task_cloud_lock is null");
  428. }
  429. Error_manager t_error;
  430. Error_manager t_result;
  431. //获取任务单的输入点云
  432. std::unique_lock<std::mutex> lck (*(mp_locate_manager_task->get_task_cloud_lock()));
  433. std::map<int, pcl::PointCloud<pcl::PointXYZ>::Ptr> *tp_task_point_cloud_map = mp_locate_manager_task->get_task_point_cloud_map();
  434. //清除map
  435. m_cloud_wheel_map.clear();
  436. m_cloud_car_map.clear();
  437. //遍历任务单的输入点云map
  438. for (const auto &map_iter : *tp_task_point_cloud_map)
  439. {
  440. if (map_iter.second.get() == NULL)
  441. {
  442. return Error_manager(Error_code::POINTER_IS_NULL, Error_level::MINOR_ERROR,
  443. " Locate_manager::locate_manager_sift() pcl::PointCloud<pcl::PointXYZ>::Ptr is NULL ");
  444. }
  445. //提前为拆分之后的点云分配内存.
  446. pcl::PointCloud<pcl::PointXYZRGB>::Ptr tp_cloud_wheel(new pcl::PointCloud<pcl::PointXYZRGB>);
  447. pcl::PointCloud<pcl::PointXYZRGB>::Ptr tp_cloud_car(new pcl::PointCloud<pcl::PointXYZRGB>);
  448. //利用PointSift从场景中分割车辆点云
  449. t_error = locate_sift(map_iter.second, tp_cloud_wheel, tp_cloud_car, m_save_flag, m_save_path);
  450. if (t_error != Error_code::SUCCESS)
  451. {
  452. char buf[256] = {0};
  453. sprintf(buf, " map.id = %d, locate_sift error", map_iter.first );
  454. t_error.add_error_description(buf, strlen(buf) );
  455. t_result.compare_and_cover_error(t_error);
  456. //注:这里不直接return,而是要把map全部执行完成
  457. }
  458. else
  459. {
  460. //将结果存入本地的中间缓存,此时map点云是有内存的,后续要记得回收
  461. m_cloud_wheel_map[map_iter.first] = tp_cloud_wheel;
  462. m_cloud_car_map[map_iter.first] = tp_cloud_car;
  463. }
  464. }
  465. return t_result;
  466. }
  467. //定位筛选,对具体的点云进行操作,分离出车轮和车身.
  468. //input::cloud输入点云
  469. //output::cloud_wheel输出车轮点云
  470. //output::cloud_car输出车身点云
  471. //work_dir:中间文件保存路径
  472. Error_manager Locate_manager::locate_sift(pcl::PointCloud<pcl::PointXYZ>::Ptr p_cloud_in,
  473. pcl::PointCloud<pcl::PointXYZRGB>::Ptr& p_cloud_wheel,pcl::PointCloud<pcl::PointXYZRGB>::Ptr& p_cloud_car,
  474. bool save_flag, std::string work_dir)
  475. {
  476. if(p_cloud_in.get()==0)
  477. {
  478. return Error_manager(LOCATER_SIFT_INPUT_CLOUD_UNINIT,MINOR_ERROR,"sift input cloud uninit");
  479. }
  480. if(p_cloud_in->size()==0)
  481. {
  482. return Error_manager(LOCATER_SIFT_INPUT_CLOUD_EMPTY,MINOR_ERROR,"locate_sift input cloud empty");
  483. }
  484. if(mp_point_sift==NULL)
  485. {
  486. return Error_manager(LOCATER_POINTSIFT_UNINIT,MINOR_ERROR,"Point Sift unInit");
  487. }
  488. Error_manager t_error;
  489. //分割车辆点云
  490. std::vector<pcl::PointCloud<pcl::PointXYZRGB>::Ptr> segmentation_clouds;
  491. t_error=mp_point_sift->segmentation(p_cloud_in, segmentation_clouds, save_flag, work_dir);
  492. if(t_error!=SUCCESS)
  493. {
  494. return t_error;
  495. }
  496. //第0类即是轮胎点云,第1类为车身点云
  497. pcl::copyPointCloud(*segmentation_clouds[0], *p_cloud_wheel);
  498. pcl::copyPointCloud(*segmentation_clouds[1], *p_cloud_car);
  499. return SUCCESS;
  500. }
  501. //计算汽车高度,
  502. Error_manager Locate_manager::locate_manager_locate_car()
  503. {
  504. if ( mp_locate_manager_task == NULL )
  505. {
  506. return Error_manager(Error_code::POINTER_IS_NULL, Error_level::MINOR_ERROR,
  507. " Locate_manager::locate_manager_locate_car() error ");
  508. }
  509. else if (mp_locate_manager_task->get_task_point_cloud_map() == NULL)
  510. {
  511. return Error_manager(Error_code::POINTER_IS_NULL,
  512. Error_level::MINOR_ERROR,
  513. "locate_manager_locate_car mp_task_point_cloud is null");
  514. }
  515. else if ( mp_locate_manager_task->get_task_cloud_lock() == NULL )
  516. {
  517. return Error_manager(Error_code::POINTER_IS_NULL,
  518. Error_level::MINOR_ERROR,
  519. "locate_manager_locate_car mp_task_cloud_lock is null");
  520. }
  521. Error_manager t_error;
  522. pcl::PointXYZRGB t_point3d_min, t_point3d_max;
  523. std::unique_lock<std::mutex> lck (*(mp_locate_manager_task->get_task_cloud_lock()));
  524. for (auto iter = m_cloud_car_map.begin(); iter != m_cloud_car_map.end(); ++iter)
  525. {
  526. pcl::PointXYZRGB min, max;
  527. t_error = locate_car(iter->second, min, max);
  528. if ( t_error != Error_code::SUCCESS )
  529. {
  530. return t_error;
  531. }
  532. //比较选出整个map点云的边界
  533. if ( t_point3d_min.x <= min.x )
  534. {
  535. t_point3d_min.x = min.x;
  536. }
  537. if ( t_point3d_min.y <= min.y )
  538. {
  539. t_point3d_min.y = min.y;
  540. }
  541. if ( t_point3d_min.z <= min.z )
  542. {
  543. t_point3d_min.z = min.z;
  544. }
  545. if ( t_point3d_max.x >= max.x )
  546. {
  547. t_point3d_max.x = max.x;
  548. }
  549. if ( t_point3d_max.y >= max.y )
  550. {
  551. t_point3d_max.y = max.y;
  552. }
  553. if ( t_point3d_max.z >= max.z )
  554. {
  555. t_point3d_max.z = max.z;
  556. }
  557. }
  558. mp_locate_information->locate_length = t_point3d_max.y - t_point3d_min.y;
  559. mp_locate_information->locate_width = t_point3d_max.x - t_point3d_min.x;;
  560. mp_locate_information->locate_height = t_point3d_max.z;
  561. //注意了:在雷达扫描时, 就已经将地面标定位为 z = 0
  562. return Error_code::SUCCESS;
  563. }
  564. //根据汽车点云计算汽车的边界
  565. Error_manager Locate_manager::locate_car(pcl::PointCloud<pcl::PointXYZRGB>::Ptr cloud_car, pcl::PointXYZRGB& min, pcl::PointXYZRGB& max)
  566. {
  567. if(cloud_car.get()==0)
  568. {
  569. return Error_manager(LOCATER_MEASURE_HEIGHT_CLOUD_UNINIT,MINOR_ERROR,"measure height input cloud uninit");
  570. }
  571. if(cloud_car->size()==0)
  572. {
  573. return Error_manager(LOCATER_MEASURE_HEIGHT_CLOUD_EMPTY,MINOR_ERROR,"measure height input cloud is empty");
  574. }
  575. //获取点云的边界
  576. pcl::getMinMax3D(*cloud_car,min,max);
  577. return SUCCESS;
  578. }
  579. //计算汽车的车轮信息
  580. Error_manager Locate_manager::locate_manager_locate_wheel()
  581. {
  582. if ( mp_locate_manager_task == NULL )
  583. {
  584. return Error_manager(Error_code::POINTER_IS_NULL, Error_level::MINOR_ERROR,
  585. " Locate_manager::locate_manager_locate_car() error ");
  586. }
  587. else if (mp_locate_manager_task->get_task_point_cloud_map() == NULL)
  588. {
  589. return Error_manager(Error_code::POINTER_IS_NULL,
  590. Error_level::MINOR_ERROR,
  591. "locate_manager_locate_car mp_task_point_cloud is null");
  592. }
  593. else if ( mp_locate_manager_task->get_task_cloud_lock() == NULL )
  594. {
  595. return Error_manager(Error_code::POINTER_IS_NULL,
  596. Error_level::MINOR_ERROR,
  597. "locate_manager_locate_car mp_task_cloud_lock is null");
  598. }
  599. if(mp_cnn3d==NULL)
  600. {
  601. return Error_manager(LOCATER_3DCNN_UNINIT,MINOR_ERROR,"locate_wheel 3dcnn is not init");
  602. }
  603. Error_manager t_error;
  604. std::unique_lock<std::mutex> lck (*(mp_locate_manager_task->get_task_cloud_lock()));
  605. //解析车轮
  606. t_error=mp_cnn3d->analytic_wheel(m_cloud_wheel_map,mp_locate_manager_task->get_cloud_aggregation_flag(),
  607. mp_locate_manager_task->get_task_locate_information_ex(),
  608. m_save_flag,m_save_path);
  609. if ( t_error != Error_code::SUCCESS )
  610. {
  611. return t_error;
  612. }
  613. return Error_code::SUCCESS;
  614. }
  615. //保存点云成txt到文件
  616. void Locate_manager::save_cloud_txt(pcl::PointCloud<pcl::PointXYZ>::Ptr cloud,std::string save_file)
  617. {
  618. FILE* pfile=fopen(save_file.c_str(),"w");
  619. for(int i=0;i<cloud->size();++i)
  620. {
  621. fprintf(pfile,"%.3f %.3f %.3f\n",cloud->points[i].x,cloud->points[i].y,cloud->points[i].z);
  622. }
  623. fclose(pfile);
  624. }