locate_manager.cpp 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695
  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::MINOR_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. //mp_locate_manager_thread 线程执行函数,
  286. //thread_work 内部线程负责locate定位分析整车的信息,并且回收汇总雷达的数据
  287. void Locate_manager::thread_work()
  288. {
  289. LOG(INFO) << " -------------------------mp_locate_manager_thread start "<< this;
  290. Error_manager t_error;
  291. Error_manager t_result;
  292. //定位管理的独立线程,
  293. while (m_locate_manager_condition.is_alive())
  294. {
  295. m_locate_manager_condition.wait();
  296. if ( m_locate_manager_condition.is_alive() )
  297. {
  298. std::this_thread::yield();
  299. //重新循环必须清除错误码.
  300. t_error.error_manager_clear_all();
  301. t_result.error_manager_clear_all();
  302. if ( mp_locate_manager_task == NULL )
  303. {
  304. m_locate_manager_status = LOCATE_MANAGER_FAULT;
  305. m_locate_manager_working_flag = false;
  306. m_locate_manager_condition.notify_all(false);
  307. }
  308. else if (mp_locate_manager_task->get_task_point_cloud_map() == NULL)
  309. {
  310. t_error.error_manager_reset(Error_code::POINTER_IS_NULL,
  311. Error_level::MINOR_ERROR,
  312. "thread_work mp_task_point_cloud is null");
  313. t_result.compare_and_cover_error(t_error);
  314. //因为故障,而提前结束任务.
  315. mp_locate_manager_task->compare_and_cover_task_error_manager(t_result);
  316. end_task();
  317. }
  318. else if ( mp_locate_manager_task->get_task_cloud_lock() == NULL )
  319. {
  320. t_error.error_manager_reset(Error_code::POINTER_IS_NULL,
  321. Error_level::MINOR_ERROR,
  322. "thread_work mp_task_cloud_lock is null");
  323. t_result.compare_and_cover_error(t_error);
  324. //因为故障,而提前结束任务.
  325. mp_locate_manager_task->compare_and_cover_task_error_manager(t_result);
  326. end_task();
  327. }
  328. //第一步
  329. //point_sift 点筛选分割 , 将输入点云分割为车身和轮胎,
  330. //注:地面会在box切割的时候,使用z轴 z_min 直接切除
  331. else if(m_locate_manager_status == LOCATE_MANAGER_SIFT)
  332. {
  333. m_cloud_wheel_map.clear();
  334. m_cloud_car_map.clear();
  335. //定位筛选,将输入点云map拆分为车轮和车身的map
  336. t_error = locate_manager_sift();
  337. t_result.compare_and_cover_error(t_error);
  338. //故障汇总, 只处理2级以上的故障
  339. if(t_result.get_error_level() >= MINOR_ERROR)
  340. {
  341. m_locate_manager_status = LOCATE_MANAGER_FAULT;
  342. mp_locate_manager_task->set_task_statu(TASK_ERROR);
  343. //因为故障,而提前结束任务.
  344. mp_locate_manager_task->compare_and_cover_task_error_manager(t_result);
  345. end_task();
  346. }
  347. else
  348. {
  349. //成功则 进入到下一个阶段,
  350. m_locate_manager_status = LOCATE_MANAGER_CAR;
  351. }
  352. }
  353. //第二步
  354. //_measure_height 计算汽车的长宽高
  355. else if(m_locate_manager_status == LOCATE_MANAGER_CAR)
  356. {
  357. //计算汽车长宽高,
  358. t_error = locate_manager_locate_car();
  359. t_result.compare_and_cover_error(t_error);
  360. //故障汇总, 只处理2级以上的故障
  361. if(t_result.get_error_level() >= MINOR_ERROR)
  362. {
  363. m_locate_manager_status = LOCATE_MANAGER_FAULT;
  364. mp_locate_manager_task->set_task_statu(TASK_ERROR);
  365. //因为故障,而提前结束任务.
  366. mp_locate_manager_task->compare_and_cover_task_error_manager(t_result);
  367. end_task();
  368. }
  369. else
  370. {
  371. //成功则 进入到下一个阶段,
  372. m_locate_manager_status = LOCATE_MANAGER_WHEEL;
  373. }
  374. }
  375. //第三步
  376. //locate_wheel 解析车轮信息
  377. else if(m_locate_manager_status == LOCATE_MANAGER_WHEEL)
  378. {
  379. //解析车轮信息,
  380. t_error = locate_manager_locate_wheel();
  381. t_result.compare_and_cover_error(t_error);
  382. //故障汇总, 只处理2级以上的故障
  383. if(t_result.get_error_level() >= MINOR_ERROR)
  384. {
  385. m_locate_manager_status = LOCATE_MANAGER_FAULT;
  386. mp_locate_manager_task->set_task_statu(TASK_ERROR);
  387. //因为故障,而提前结束任务.
  388. mp_locate_manager_task->compare_and_cover_task_error_manager(t_result);
  389. end_task();
  390. }
  391. else
  392. {
  393. m_locate_manager_working_flag = false;
  394. m_locate_manager_condition.set_pass_ever(false);
  395. //正常结束任务
  396. mp_locate_manager_task->compare_and_cover_task_error_manager(t_result);
  397. end_task();
  398. }
  399. }
  400. }
  401. }
  402. LOG(INFO) << " ---------------------------mp_locate_manager_thread end :"<<this;
  403. return;
  404. }
  405. //定位筛选,将输入点云map拆分为车轮和车身的map.(数据可以直接从locate_manager内部获取)
  406. Error_manager Locate_manager::locate_manager_sift()
  407. {
  408. if ( mp_locate_manager_task == NULL )
  409. {
  410. return Error_manager(Error_code::POINTER_IS_NULL, Error_level::MINOR_ERROR,
  411. " Locate_manager::locate_manager_sift() error ");
  412. }
  413. else if (mp_locate_manager_task->get_task_point_cloud_map() == NULL)
  414. {
  415. return Error_manager(Error_code::POINTER_IS_NULL,
  416. Error_level::MINOR_ERROR,
  417. "locate_manager_sift mp_task_point_cloud is null");
  418. }
  419. else if ( mp_locate_manager_task->get_task_cloud_lock() == NULL )
  420. {
  421. return Error_manager(Error_code::POINTER_IS_NULL,
  422. Error_level::MINOR_ERROR,
  423. "locate_manager_sift mp_task_cloud_lock is null");
  424. }
  425. Error_manager t_error;
  426. Error_manager t_result;
  427. //获取任务单的输入点云
  428. std::unique_lock<std::mutex> lck (*(mp_locate_manager_task->get_task_cloud_lock()));
  429. std::map<int, pcl::PointCloud<pcl::PointXYZ>::Ptr> *tp_task_point_cloud_map = mp_locate_manager_task->get_task_point_cloud_map();
  430. //清除map
  431. m_cloud_wheel_map.clear();
  432. m_cloud_car_map.clear();
  433. //遍历任务单的输入点云map
  434. for (const auto &map_iter : *tp_task_point_cloud_map)
  435. {
  436. if (map_iter.second.get() == NULL)
  437. {
  438. return Error_manager(Error_code::POINTER_IS_NULL, Error_level::MINOR_ERROR,
  439. " Locate_manager::locate_manager_sift() pcl::PointCloud<pcl::PointXYZ>::Ptr is NULL ");
  440. }
  441. //提前为拆分之后的点云分配内存.
  442. pcl::PointCloud<pcl::PointXYZRGB>::Ptr tp_cloud_wheel(new pcl::PointCloud<pcl::PointXYZRGB>);
  443. pcl::PointCloud<pcl::PointXYZRGB>::Ptr tp_cloud_car(new pcl::PointCloud<pcl::PointXYZRGB>);
  444. //利用PointSift从场景中分割车辆点云
  445. t_error = locate_sift(map_iter.second, tp_cloud_wheel, tp_cloud_car, m_save_flag, m_save_path);
  446. if (t_error != Error_code::SUCCESS)
  447. {
  448. char buf[256] = {0};
  449. sprintf(buf, " map.id = %d, locate_sift error", map_iter.first );
  450. t_error.add_error_description(buf, strlen(buf) );
  451. t_result.compare_and_cover_error(t_error);
  452. //注:这里不直接return,而是要把map全部执行完成
  453. }
  454. else
  455. {
  456. //将结果存入本地的中间缓存,此时map点云是有内存的,后续要记得回收
  457. m_cloud_wheel_map[map_iter.first] = tp_cloud_wheel;
  458. m_cloud_car_map[map_iter.first] = tp_cloud_car;
  459. }
  460. }
  461. return t_result;
  462. }
  463. //定位筛选,对具体的点云进行操作,分离出车轮和车身.
  464. //input::cloud输入点云
  465. //output::cloud_wheel输出车轮点云
  466. //output::cloud_car输出车身点云
  467. //work_dir:中间文件保存路径
  468. Error_manager Locate_manager::locate_sift(pcl::PointCloud<pcl::PointXYZ>::Ptr p_cloud_in,
  469. pcl::PointCloud<pcl::PointXYZRGB>::Ptr& p_cloud_wheel,pcl::PointCloud<pcl::PointXYZRGB>::Ptr& p_cloud_car,
  470. bool save_flag, std::string work_dir)
  471. {
  472. if(p_cloud_in.get()==0)
  473. {
  474. return Error_manager(LOCATER_SIFT_INPUT_CLOUD_UNINIT,MINOR_ERROR,"sift input cloud uninit");
  475. }
  476. if(p_cloud_in->size()==0)
  477. {
  478. return Error_manager(LOCATER_SIFT_INPUT_CLOUD_EMPTY,MINOR_ERROR,"locate_sift input cloud empty");
  479. }
  480. if(mp_point_sift==NULL)
  481. {
  482. return Error_manager(LOCATER_POINTSIFT_UNINIT,MINOR_ERROR,"Point Sift unInit");
  483. }
  484. Error_manager t_error;
  485. //分割车辆点云
  486. std::vector<pcl::PointCloud<pcl::PointXYZRGB>::Ptr> segmentation_clouds;
  487. t_error=mp_point_sift->segmentation(p_cloud_in, segmentation_clouds, save_flag, work_dir);
  488. if(t_error!=SUCCESS)
  489. {
  490. return t_error;
  491. }
  492. //第0类即是轮胎点云,第1类为车身点云
  493. pcl::copyPointCloud(*segmentation_clouds[0], *p_cloud_wheel);
  494. pcl::copyPointCloud(*segmentation_clouds[1], *p_cloud_car);
  495. return SUCCESS;
  496. }
  497. //计算汽车高度,
  498. Error_manager Locate_manager::locate_manager_locate_car()
  499. {
  500. if ( mp_locate_manager_task == NULL )
  501. {
  502. return Error_manager(Error_code::POINTER_IS_NULL, Error_level::MINOR_ERROR,
  503. " Locate_manager::locate_manager_locate_car() error ");
  504. }
  505. else if (mp_locate_manager_task->get_task_point_cloud_map() == NULL)
  506. {
  507. return Error_manager(Error_code::POINTER_IS_NULL,
  508. Error_level::MINOR_ERROR,
  509. "locate_manager_locate_car mp_task_point_cloud is null");
  510. }
  511. else if ( mp_locate_manager_task->get_task_cloud_lock() == NULL )
  512. {
  513. return Error_manager(Error_code::POINTER_IS_NULL,
  514. Error_level::MINOR_ERROR,
  515. "locate_manager_locate_car mp_task_cloud_lock is null");
  516. }
  517. Error_manager t_error;
  518. pcl::PointXYZRGB t_point3d_min, t_point3d_max;
  519. std::unique_lock<std::mutex> lck (*(mp_locate_manager_task->get_task_cloud_lock()));
  520. for (auto iter = m_cloud_car_map.begin(); iter != m_cloud_car_map.end(); ++iter)
  521. {
  522. pcl::PointXYZRGB min, max;
  523. t_error = locate_car(iter->second, min, max);
  524. if ( t_error != Error_code::SUCCESS )
  525. {
  526. return t_error;
  527. }
  528. //比较选出整个map点云的边界
  529. if ( t_point3d_min.x <= min.x )
  530. {
  531. t_point3d_min.x = min.x;
  532. }
  533. if ( t_point3d_min.y <= min.y )
  534. {
  535. t_point3d_min.y = min.y;
  536. }
  537. if ( t_point3d_min.z <= min.z )
  538. {
  539. t_point3d_min.z = min.z;
  540. }
  541. if ( t_point3d_max.x >= max.x )
  542. {
  543. t_point3d_max.x = max.x;
  544. }
  545. if ( t_point3d_max.y >= max.y )
  546. {
  547. t_point3d_max.y = max.y;
  548. }
  549. if ( t_point3d_max.z >= max.z )
  550. {
  551. t_point3d_max.z = max.z;
  552. }
  553. }
  554. mp_locate_information->locate_length = t_point3d_max.y - t_point3d_min.y;
  555. mp_locate_information->locate_width = t_point3d_max.x - t_point3d_min.x;;
  556. mp_locate_information->locate_height = t_point3d_max.z;
  557. //注意了:在雷达扫描时, 就已经将地面标定位为 z = 0
  558. return Error_code::SUCCESS;
  559. }
  560. //根据汽车点云计算汽车的边界
  561. Error_manager Locate_manager::locate_car(pcl::PointCloud<pcl::PointXYZRGB>::Ptr cloud_car, pcl::PointXYZRGB& min, pcl::PointXYZRGB& max)
  562. {
  563. if(cloud_car.get()==0)
  564. {
  565. return Error_manager(LOCATER_MEASURE_HEIGHT_CLOUD_UNINIT,MINOR_ERROR,"measure height input cloud uninit");
  566. }
  567. if(cloud_car->size()==0)
  568. {
  569. return Error_manager(LOCATER_MEASURE_HEIGHT_CLOUD_EMPTY,MINOR_ERROR,"measure height input cloud is empty");
  570. }
  571. //获取点云的边界
  572. pcl::getMinMax3D(*cloud_car,min,max);
  573. return SUCCESS;
  574. }
  575. //计算汽车的车轮信息
  576. Error_manager Locate_manager::locate_manager_locate_wheel()
  577. {
  578. if ( mp_locate_manager_task == NULL )
  579. {
  580. return Error_manager(Error_code::POINTER_IS_NULL, Error_level::MINOR_ERROR,
  581. " Locate_manager::locate_manager_locate_car() error ");
  582. }
  583. else if (mp_locate_manager_task->get_task_point_cloud_map() == NULL)
  584. {
  585. return Error_manager(Error_code::POINTER_IS_NULL,
  586. Error_level::MINOR_ERROR,
  587. "locate_manager_locate_car mp_task_point_cloud is null");
  588. }
  589. else if ( mp_locate_manager_task->get_task_cloud_lock() == NULL )
  590. {
  591. return Error_manager(Error_code::POINTER_IS_NULL,
  592. Error_level::MINOR_ERROR,
  593. "locate_manager_locate_car mp_task_cloud_lock is null");
  594. }
  595. if(mp_cnn3d==NULL)
  596. {
  597. return Error_manager(LOCATER_3DCNN_UNINIT,MINOR_ERROR,"locate_wheel 3dcnn is not init");
  598. }
  599. Error_manager t_error;
  600. std::unique_lock<std::mutex> lck (*(mp_locate_manager_task->get_task_cloud_lock()));
  601. //解析车轮
  602. t_error=mp_cnn3d->analytic_wheel(m_cloud_wheel_map,mp_locate_manager_task->get_cloud_aggregation_flag(),
  603. mp_locate_manager_task->get_task_locate_information_ex(),
  604. m_save_flag,m_save_path);
  605. if ( t_error != Error_code::SUCCESS )
  606. {
  607. return t_error;
  608. }
  609. return Error_code::SUCCESS;
  610. }
  611. //保存点云成txt到文件
  612. void Locate_manager::save_cloud_txt(pcl::PointCloud<pcl::PointXYZ>::Ptr cloud,std::string save_file)
  613. {
  614. FILE* pfile=fopen(save_file.c_str(),"w");
  615. for(int i=0;i<cloud->size();++i)
  616. {
  617. fprintf(pfile,"%.3f %.3f %.3f\n",cloud->points[i].x,cloud->points[i].y,cloud->points[i].z);
  618. }
  619. fclose(pfile);
  620. }