ground_region.cpp 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800
  1. //
  2. // Created by zx on 2021/5/20.
  3. //
  4. #include "ground_region.h"
  5. #include <pcl/common/transforms.h>
  6. #include <pcl/filters/statistical_outlier_removal.h>
  7. #include <pcl/filters/voxel_grid.h>
  8. #include <pcl/segmentation/extract_clusters.h>
  9. #include <fcntl.h>
  10. // 测量结果滤波,不影响现有结构
  11. #include "../tool/measure_filter.h"
  12. //欧式聚类*******************************************************
  13. std::vector<pcl::PointCloud<pcl::PointXYZ>::Ptr> Ground_region::segmentation(pcl::PointCloud<pcl::PointXYZ>::Ptr sor_cloud)
  14. {
  15. std::vector<pcl::PointIndices> ece_inlier;
  16. pcl::search::KdTree<pcl::PointXYZ>::Ptr tree(new pcl::search::KdTree<pcl::PointXYZ>);
  17. pcl::EuclideanClusterExtraction<pcl::PointXYZ> ece;
  18. ece.setInputCloud(sor_cloud);
  19. ece.setClusterTolerance(0.07);
  20. ece.setMinClusterSize(20);
  21. ece.setMaxClusterSize(20000);
  22. ece.setSearchMethod(tree);
  23. ece.extract(ece_inlier);
  24. std::vector<pcl::PointCloud<pcl::PointXYZ>::Ptr> segmentation_clouds;
  25. for (int i = 0; i < ece_inlier.size(); i++)
  26. {
  27. pcl::PointCloud<pcl::PointXYZ>::Ptr cloud_copy(new pcl::PointCloud<pcl::PointXYZ>);
  28. std::vector<int> ece_inlier_ext = ece_inlier[i].indices;
  29. copyPointCloud(*sor_cloud, ece_inlier_ext, *cloud_copy); //按照索引提取点云数据
  30. segmentation_clouds.push_back(cloud_copy);
  31. }
  32. return segmentation_clouds;
  33. }
  34. /**
  35. * @description: distance between two points
  36. * @param {Point2f} p1
  37. * @param {Point2f} p2
  38. * @return the distance
  39. */
  40. double Ground_region::distance(cv::Point2f p1, cv::Point2f p2)
  41. {
  42. return sqrt(pow(p1.x - p2.x, 2.0) + pow(p1.y - p2.y, 2.0));
  43. }
  44. /**
  45. * @description: point rectangle detect
  46. * @param points detect if points obey the rectangle rule
  47. * @return wether forms a rectangle
  48. */
  49. bool Ground_region::isRect(std::vector<cv::Point2f> &points)
  50. {
  51. if (points.size() == 4)
  52. {
  53. double L[3] = {0.0};
  54. L[0] = distance(points[0], points[1]);
  55. L[1] = distance(points[1], points[2]);
  56. L[2] = distance(points[0], points[2]);
  57. double max_l = L[0];
  58. double l1 = L[1];
  59. double l2 = L[2];
  60. cv::Point2f ps = points[0], pt = points[1];
  61. cv::Point2f pc = points[2];
  62. for (int i = 1; i < 3; ++i)
  63. {
  64. if (L[i] > max_l)
  65. {
  66. max_l = L[i];
  67. l1 = L[abs(i + 1) % 3];
  68. l2 = L[abs(i + 2) % 3];
  69. ps = points[i % 3];
  70. pt = points[(i + 1) % 3];
  71. pc = points[(i + 2) % 3];
  72. }
  73. }
  74. //直角边与坐标轴的夹角 <20°
  75. float thresh = 20.0 * M_PI / 180.0;
  76. cv::Point2f vct(pt.x - pc.x, pt.y - pc.y);
  77. float angle = atan2(vct.y, vct.x);
  78. if (!(fabs(angle) < thresh || (M_PI_2 - fabs(angle) < thresh)))
  79. {
  80. //std::cout<<" 4 wheel axis angle : "<<angle<<std::endl;
  81. return false;
  82. }
  83. double cosa = (l1 * l1 + l2 * l2 - max_l * max_l) / (2.0 * l1 * l2);
  84. if (fabs(cosa) >= 0.15)
  85. {
  86. /*char description[255]={0};
  87. sprintf(description,"angle cos value(%.2f) >0.13 ",cosa);
  88. std::cout<<description<<std::endl;*/
  89. return false;
  90. }
  91. float width = std::min(l1, l2);
  92. float length = std::max(l1, l2);
  93. if (width < 1.400 || width > 1.900 || length > 3.300 || length < 2.200)
  94. {
  95. /*char description[255]={0};
  96. sprintf(description,"width<1400 || width >1900 || length >3300 ||length < 2200 l:%.1f,w:%.1f",length,width);
  97. std::cout<<description<<std::endl;*/
  98. return false;
  99. }
  100. double d = distance(pc, points[3]);
  101. cv::Point2f center1 = (ps + pt) * 0.5;
  102. cv::Point2f center2 = (pc + points[3]) * 0.5;
  103. if (fabs(d - max_l) > max_l * 0.1 || distance(center1, center2) > 0.150)
  104. {
  105. /*std::cout << "d:" << d << " maxl:" << max_l << " center1:" << center1 << " center2:" << center2<<std::endl;
  106. char description[255]={0};
  107. sprintf(description,"Verify failed-4 fabs(d - max_l) > max_l * 0.1 || distance(center1, center2) > 0.150 ");
  108. std::cout<<description<<std::endl;*/
  109. return false;
  110. }
  111. //std::cout << "d:" << d << " maxl:" << max_l << " center1:" << center1 << " center2:" << center2<<std::endl;
  112. return true;
  113. }
  114. else if (points.size() == 3)
  115. {
  116. double L[3] = {0.0};
  117. L[0] = distance(points[0], points[1]);
  118. L[1] = distance(points[1], points[2]);
  119. L[2] = distance(points[0], points[2]);
  120. double max_l = L[0];
  121. double l1 = L[1];
  122. double l2 = L[2];
  123. int max_index = 0;
  124. cv::Point2f ps = points[0], pt = points[1];
  125. cv::Point2f pc = points[2];
  126. for (int i = 1; i < 3; ++i)
  127. {
  128. if (L[i] > max_l)
  129. {
  130. max_index = i;
  131. max_l = L[i];
  132. l1 = L[abs(i + 1) % 3];
  133. l2 = L[abs(i + 2) % 3];
  134. ps = points[i % 3];
  135. pt = points[(i + 1) % 3];
  136. pc = points[(i + 2) % 3];
  137. }
  138. }
  139. //直角边与坐标轴的夹角 <20°
  140. float thresh = 20.0 * M_PI / 180.0;
  141. cv::Point2f vct(pt.x - pc.x, pt.y - pc.y);
  142. float angle = atan2(vct.y, vct.x);
  143. if (!(fabs(angle) < thresh || (M_PI_2 - fabs(angle) < thresh)))
  144. {
  145. //std::cout<<" 4 wheel axis angle : "<<angle<<std::endl;
  146. return false;
  147. }
  148. double cosa = (l1 * l1 + l2 * l2 - max_l * max_l) / (2.0 * l1 * l2);
  149. if (fabs(cosa) >= 0.15)
  150. {
  151. /*char description[255]={0};
  152. sprintf(description,"3 wheels angle cos value(%.2f) >0.13 ",cosa);
  153. std::cout<<description<<std::endl;*/
  154. return false;
  155. }
  156. double l = std::max(l1, l2);
  157. double w = std::min(l1, l2);
  158. if (l > 2.100 && l < 3.300 && w > 1.400 && w < 2.100)
  159. {
  160. //生成第四个点
  161. cv::Point2f vec1 = ps - pc;
  162. cv::Point2f vec2 = pt - pc;
  163. cv::Point2f point4 = (vec1 + vec2) + pc;
  164. points.push_back(point4);
  165. /*char description[255]={0};
  166. sprintf(description,"3 wheels rectangle cos angle=%.2f,L=%.1f, w=%.1f",cosa,l,w);
  167. std::cout<<description<<std::endl;*/
  168. return true;
  169. }
  170. else
  171. {
  172. /*char description[255]={0};
  173. sprintf(description,"3 wheels rectangle verify Failed cos angle=%.2f,L=%.1f, w=%.1f",cosa,l,w);
  174. std::cout<<description<<std::endl;*/
  175. return false;
  176. }
  177. }
  178. //std::cout<<" default false"<<std::endl;
  179. return false;
  180. }
  181. /**
  182. * @description: 3d wheel detect core func
  183. * @param cloud input cloud for measure
  184. * @param thresh_z z value to cut wheel
  185. * @param result detect result
  186. * @return wether successfully detected
  187. */
  188. bool Ground_region::classify_ceres_detect(pcl::PointCloud<pcl::PointXYZ>::Ptr cloud, double thresh_z,
  189. detect_wheel_ceres3d::Detect_result &result)
  190. {
  191. if (m_detector == nullptr)
  192. return false;
  193. pcl::PointCloud<pcl::PointXYZ>::Ptr cloud_filtered(new pcl::PointCloud<pcl::PointXYZ>);
  194. for (int i = 0; i < cloud->size(); ++i)
  195. {
  196. pcl::PointXYZ pt = cloud->points[i];
  197. if (pt.z < thresh_z)
  198. {
  199. cloud_filtered->push_back(pt);
  200. }
  201. }
  202. //下采样
  203. pcl::VoxelGrid<pcl::PointXYZ> vox; //创建滤波对象
  204. vox.setInputCloud(cloud_filtered); //设置需要过滤的点云给滤波对象
  205. vox.setLeafSize(0.01f, 0.01f, 0.01f); //设置滤波时创建的体素体积为1cm的立方体
  206. vox.filter(*cloud_filtered); //执行滤波处理,存储输出
  207. if (cloud_filtered->size() == 0)
  208. {
  209. return false;
  210. }
  211. pcl::StatisticalOutlierRemoval<pcl::PointXYZ> sor; //创建滤波器对象
  212. sor.setInputCloud(cloud_filtered); //设置待滤波的点云
  213. sor.setMeanK(5); //设置在进行统计时考虑的临近点个数
  214. sor.setStddevMulThresh(3.0); //设置判断是否为离群点的阀值,用来倍乘标准差,也就是上面的std_mul
  215. sor.filter(*cloud_filtered); //滤波结果存储到cloud_filtered
  216. if (cloud_filtered->size() == 0)
  217. {
  218. return false;
  219. }
  220. std::vector<pcl::PointCloud<pcl::PointXYZ>::Ptr> seg_clouds;
  221. seg_clouds = segmentation(cloud_filtered);
  222. if (!(seg_clouds.size() == 4 || seg_clouds.size() == 3))
  223. {
  224. return false;
  225. }
  226. std::vector<cv::Point2f> centers;
  227. cv::Point2f temp_centers(0,0);
  228. for (int i = 0; i < seg_clouds.size(); ++i)
  229. {
  230. Eigen::Vector4f centroid;
  231. pcl::compute3DCentroid(*seg_clouds[i], centroid);
  232. centers.push_back(cv::Point2f(centroid[0], centroid[1]));
  233. temp_centers += cv::Point2f(centroid[0], centroid[1]);
  234. }
  235. temp_centers /= 4.0f;
  236. bool ret = isRect(centers);
  237. if (ret)
  238. {
  239. std::string error_str;
  240. // LOG(WARNING) << "region id: "<< m_region.region_id();
  241. if (m_detector->detect(seg_clouds, result, error_str))
  242. {
  243. return true;
  244. }
  245. else
  246. {
  247. // LOG(WARNING) << error_str;
  248. return false;
  249. }
  250. }
  251. return ret;
  252. }
  253. // constructor
  254. Ground_region::Ground_region()
  255. {
  256. m_region_status = E_UNKNOWN;
  257. m_detector = nullptr;
  258. m_measure_thread = nullptr;
  259. }
  260. // deconstructor
  261. Ground_region::~Ground_region()
  262. {
  263. // LOG(WARNING) << "start deconstruct ground region";
  264. if (m_measure_thread)
  265. {
  266. m_measure_condition.kill_all();
  267. // Close Capturte Thread
  268. if (m_measure_thread->joinable())
  269. {
  270. m_measure_thread->join();
  271. delete m_measure_thread;
  272. m_measure_thread = nullptr;
  273. }
  274. }
  275. // LOG(WARNING) << "thread released";
  276. // 将创建的检测器析构
  277. if(m_detector)
  278. {
  279. delete m_detector;
  280. m_detector = nullptr;
  281. }
  282. // LOG(WARNING) << "detector released";
  283. }
  284. Error_manager Ground_region::init(velodyne::Region region, pcl::PointCloud<pcl::PointXYZ>::Ptr left_model, pcl::PointCloud<pcl::PointXYZ>::Ptr right_model)
  285. {
  286. m_region = region;
  287. m_detector = new detect_wheel_ceres3d(left_model,right_model);
  288. mp_cloud_collection = pcl::PointCloud<pcl::PointXYZ>::Ptr(new pcl::PointCloud<pcl::PointXYZ>);
  289. mp_cloud_filtered = pcl::PointCloud<pcl::PointXYZ>::Ptr(new pcl::PointCloud<pcl::PointXYZ>);
  290. mp_cloud_detect_z = pcl::PointCloud<pcl::PointXYZ>::Ptr(new pcl::PointCloud<pcl::PointXYZ>);
  291. m_measure_thread = new std::thread(&Ground_region::thread_measure_func, this);
  292. m_measure_condition.reset();
  293. m_region_status = E_READY;
  294. return SUCCESS;
  295. }
  296. // 计算均方误差
  297. bool computer_var(std::vector<double> data, double &var)
  298. {
  299. if (data.size() == 0)
  300. return false;
  301. Eigen::VectorXd dis_vec(data.size());
  302. for (int i = 0; i < data.size(); ++i)
  303. {
  304. dis_vec[i] = data[i];
  305. }
  306. double mean = dis_vec.mean();
  307. Eigen::VectorXd mean_vec(data.size());
  308. Eigen::VectorXd mat = dis_vec - (mean_vec.setOnes() * mean);
  309. Eigen::MatrixXd result = (mat.transpose()) * mat;
  310. var = sqrt(result(0) / double(data.size()));
  311. return true;
  312. }
  313. Error_manager Ground_region::detect(pcl::PointCloud<pcl::PointXYZ>::Ptr cloud, detect_wheel_ceres3d::Detect_result &last_result)
  314. {
  315. // 1.*********点云合法性检验*********
  316. if (cloud->size() == 0){
  317. // 更新过滤点
  318. m_filtered_cloud_mutex.lock();
  319. mp_cloud_filtered->clear();
  320. m_filtered_cloud_mutex.unlock();
  321. return Error_manager(VELODYNE_REGION_EMPTY_CLOUD, NEGLIGIBLE_ERROR, "no input point");
  322. }
  323. // 2.*********点云预处理*********
  324. std::chrono::steady_clock::time_point t1 = std::chrono::steady_clock::now();
  325. pcl::PointCloud<pcl::PointXYZ>::Ptr cloud_cut(new pcl::PointCloud<pcl::PointXYZ>);
  326. pcl::PointCloud<pcl::PointXYZ>::Ptr cloud_clean(new pcl::PointCloud<pcl::PointXYZ>);
  327. pcl::PointCloud<pcl::PointXYZ>::Ptr cloud_filtered(new pcl::PointCloud<pcl::PointXYZ>);
  328. for (int i = 0; i < cloud->size(); ++i)
  329. {
  330. pcl::PointXYZ pt = cloud->points[i];
  331. if (pt.x > m_region.minx() && pt.x < m_region.maxx() && pt.y > m_region.miny() && pt.y < m_region.maxy() && pt.z > m_region.minz() && pt.z < m_region.maxz())
  332. {
  333. cloud_cut->push_back(pt);
  334. }
  335. }
  336. if(cloud_cut->size() <= 0)
  337. {
  338. // 更新过滤点
  339. m_filtered_cloud_mutex.lock();
  340. mp_cloud_filtered->clear();
  341. m_filtered_cloud_mutex.unlock();
  342. return Error_manager(VELODYNE_REGION_EMPTY_CLOUD, NEGLIGIBLE_ERROR, "cut no point");
  343. }
  344. //下采样
  345. pcl::VoxelGrid<pcl::PointXYZ> vox; //创建滤波对象
  346. vox.setInputCloud(cloud_cut); //设置需要过滤的点云给滤波对象
  347. vox.setLeafSize(0.02f, 0.02f, 0.02f); //设置滤波时创建的体素体积为1cm的立方体
  348. vox.filter(*cloud_clean); //执行滤波处理,存储输出
  349. // cloud_filtered = cloud_clean;
  350. if (cloud_clean->size() == 0){
  351. // 更新过滤点
  352. m_filtered_cloud_mutex.lock();
  353. mp_cloud_filtered->clear();
  354. m_filtered_cloud_mutex.unlock();
  355. return Error_manager(VELODYNE_REGION_EMPTY_CLOUD, NEGLIGIBLE_ERROR, "ApproximateVoxelGrid no point");
  356. }
  357. //离群点过滤
  358. pcl::StatisticalOutlierRemoval<pcl::PointXYZ> sor;
  359. sor.setInputCloud(cloud_clean);
  360. sor.setMeanK(5); //K近邻搜索点个数
  361. sor.setStddevMulThresh(3.0); //标准差倍数
  362. sor.setNegative(false); //保留未滤波点(内点)
  363. sor.filter(*cloud_filtered); //保存滤波结果到cloud_filter
  364. if (cloud_filtered->size() == 0){
  365. // 更新过滤点
  366. m_filtered_cloud_mutex.lock();
  367. mp_cloud_filtered->clear();
  368. m_filtered_cloud_mutex.unlock();
  369. return Error_manager(VELODYNE_REGION_EMPTY_CLOUD, NEGLIGIBLE_ERROR, "StatisticalOutlierRemoval no point");
  370. }
  371. // 更新过滤点
  372. m_filtered_cloud_mutex.lock();
  373. mp_cloud_filtered->clear();
  374. mp_cloud_filtered->operator+=(*cloud_filtered);
  375. m_filtered_cloud_mutex.unlock();
  376. // 3.*********位姿优化,获取中心xy与角度*********
  377. pcl::PointCloud<pcl::PointXYZ>::Ptr cloud_detect_z(new pcl::PointCloud<pcl::PointXYZ>);
  378. double car_pose_x, car_pose_y, car_pose_theta, car_pose_width, z_value=0.2;
  379. // if(!Car_pose_detector::get_instance_references().detect_pose(cloud_filtered, cloud_detect_z, car_pose_x, car_pose_y, car_pose_theta, car_pose_width, z_value, false))
  380. if(!Car_pose_detector::get_instance_references().detect_pose_mat(cloud_filtered, cloud_detect_z, car_pose_x, car_pose_y, car_pose_theta, false))
  381. {
  382. return Error_manager(VELODYNE_REGION_CERES_SOLVE_ERROR, NEGLIGIBLE_ERROR, "find general car pose value failed.");
  383. }
  384. // LOG_IF(INFO, m_region.region_id() == 0) << "car pose :::: cx: " << car_pose_x+m_region.plc_offsetx() << ", cy: " << car_pose_y+m_region.plc_offsety() << ", theta: " << car_pose_theta*180.0/M_PI << std::endl;
  385. std::chrono::steady_clock::time_point t2 = std::chrono::steady_clock::now();
  386. std::chrono::duration<double> time_used_bowl = std::chrono::duration_cast<std::chrono::duration<double>>(t2 - t1);
  387. // 4.*********xoz优化获取底盘高度*********
  388. // 重新取包含地面点的点云,用于底盘优化
  389. double z_solver_x = car_pose_x;
  390. double z_solver_y = car_pose_y;
  391. double z_solver_theta = car_pose_theta;
  392. double z_solver_width = 1.0;
  393. chassis_ceres_solver t_solver;
  394. // !!!重新筛选地面点并滤波,融入第一步位姿使用的滤波后点云,传入第二步获取底盘高度
  395. pcl::PointCloud<pcl::PointXYZ>::Ptr cloud_z_solver(new pcl::PointCloud<pcl::PointXYZ>);
  396. for (int i = 0; i < cloud->size(); ++i)
  397. {
  398. pcl::PointXYZ pt = cloud->points[i];
  399. if (pt.x > m_region.minx() && pt.x < m_region.maxx() && pt.y > m_region.miny() && pt.y < m_region.maxy() && pt.z <= m_region.minz())
  400. {
  401. cloud_z_solver->push_back(pt);
  402. }
  403. }
  404. //离群点过滤
  405. sor.setInputCloud(cloud_z_solver);
  406. sor.setMeanK(5); //K近邻搜索点个数
  407. sor.setStddevMulThresh(3.0); //标准差倍数
  408. sor.setNegative(false); //保留未滤波点(内点)
  409. sor.filter(*cloud_z_solver); //保存滤波结果到cloud_filter
  410. // 补上车身与车轮点
  411. cloud_z_solver->operator+=(*mp_cloud_filtered);
  412. // 去中心,角度调正
  413. Car_pose_detector::get_instance_references().inv_trans_cloud(cloud_z_solver, z_solver_x, z_solver_y, z_solver_theta);
  414. if (cloud_z_solver->size() == 0)
  415. return Error_manager(VELODYNE_REGION_EMPTY_CLOUD, NORMAL, "z solver no point");
  416. // 给予底盘z中心与高度初值
  417. double mid_z = 0.05, height = 0.08;
  418. z_solver_x = 0.0;
  419. // Error_manager ec = t_solver.solve(cloud_z_solver, z_solver_x, mid_z, z_solver_width, height);
  420. Error_manager ec = t_solver.solve_mat(cloud_z_solver, z_solver_x, mid_z, z_solver_width, height, false);
  421. // 切除大于height高度以外点,并显示width直线
  422. // 根据z值切原始点云
  423. pcl::PassThrough<pcl::PointXYZ> pass;
  424. pass.setInputCloud(cloud_z_solver);
  425. pass.setFilterFieldName("z");
  426. pass.setFilterLimits(m_region.minz(), mid_z + height / 2.0);
  427. pass.setFilterLimitsNegative(false);
  428. pass.filter(*cloud_z_solver);
  429. // // 车宽方向画线
  430. // for (double i = -3.0; i < 3.0; i+=0.02)
  431. // {
  432. // cloud_z_solver->push_back(pcl::PointXYZ(-width/2.0, i, 0));
  433. // cloud_z_solver->push_back(pcl::PointXYZ(width/2.0, i, 0));
  434. // }
  435. // std::cout << "\n------------------------------------ chassis z1: " << mid_z + height / 2.0 << ", mid z: "<< mid_z << std::endl;
  436. if(ec != SUCCESS)
  437. return Error_manager(VELODYNE_REGION_CERES_SOLVE_ERROR, NORMAL, "failed to find chassis z value.");
  438. std::chrono::steady_clock::time_point t3 = std::chrono::steady_clock::now();
  439. std::chrono::duration<double> time_used_block = std::chrono::duration_cast<std::chrono::duration<double>>(t3 - t2);
  440. // 更新z中间点
  441. m_detect_z_cloud_mutex.lock();
  442. mp_cloud_detect_z->clear();
  443. mp_cloud_detect_z->operator+=(*cloud_z_solver);
  444. m_detect_z_cloud_mutex.unlock();
  445. // 二分法存在错误弃用!!!直接使用底盘z值
  446. std::vector<detect_wheel_ceres3d::Detect_result> results;
  447. detect_wheel_ceres3d::Detect_result result;
  448. double chassis_z = mid_z + height / 2.0; // + 0.02;
  449. if(chassis_z > m_region.maxz() || chassis_z < m_region.minz())
  450. {
  451. return Error_manager(VELODYNE_REGION_CERES_SOLVE_ERROR, NEGLIGIBLE_ERROR, (std::string("optimized chassis z value out of range: ")+std::to_string(chassis_z)).c_str());
  452. }
  453. bool ret = false;
  454. while (chassis_z > (mid_z - height / 2.0))
  455. {
  456. // LOG_IF(WARNING, m_region.region_id() == 0) << "chassis z: " << chassis_z << ", midz: " << mid_z - height / 2.0;
  457. // 初值中x使用第一步优化的值
  458. result.cx = car_pose_x;
  459. ret = classify_ceres_detect(cloud_filtered, chassis_z, result);
  460. // changed by yct, 根据底盘z识别,失败则向下移动直至成功或超过底盘内空中点
  461. if(ret)
  462. {
  463. results.push_back(result);
  464. break;
  465. }else{
  466. chassis_z -= 0.01;
  467. }
  468. }
  469. // } while (fabs(center_z - last_center_z) > 0.01);
  470. std::chrono::steady_clock::time_point t4 = std::chrono::steady_clock::now();
  471. std::chrono::duration<double> time_used_div = std::chrono::duration_cast<std::chrono::duration<double>>(t4 - t3);
  472. // std::cout << "\n------------------------------------------------------------ " << std::to_string(time_used_bowl.count()) << " " << std::to_string(time_used_block.count()) << " " << std::to_string(time_used_div.count())<<" || "<< std::to_string(time_used_bowl.count()+time_used_block.count()+time_used_div.count())<< std::endl;
  473. if (results.size() == 0)
  474. {
  475. // std::cout << "\n-------- no result: " << std::endl;
  476. //LOG_IF(INFO, m_region.region_id() == 4) << "failed with midz, currz: " << mid_z << ", " << chassis_z;
  477. return Error_manager(VELODYNE_REGION_CERES_SOLVE_ERROR, NEGLIGIBLE_ERROR, "3d wheel detect failed.");
  478. }
  479. else
  480. {
  481. // // 20201010, may lead to problem in chutian, uncomment in debug only
  482. // // changed by yct, save 3d wheel detect result.
  483. // static int save_debug = 0;
  484. // if (m_region.region_id() == 0 && save_debug++ == 5)
  485. // m_detector->save_debug_data("/home/zx/yct/chutian_measure_2021/build");
  486. // LOG_IF(INFO, m_region.region_id() == 4) << "detected with suitable z value: " << chassis_z;
  487. }
  488. /// to be
  489. float min_mean_loss = 1.0;
  490. for (int i = 0; i < results.size(); ++i)
  491. {
  492. detect_wheel_ceres3d::Detect_result result = results[i];
  493. std::vector<double> loss;
  494. loss.push_back(result.loss.lf_loss);
  495. loss.push_back(result.loss.rf_loss);
  496. loss.push_back(result.loss.lb_loss);
  497. loss.push_back(result.loss.rb_loss);
  498. double mean = (result.loss.lf_loss + result.loss.rf_loss + result.loss.lb_loss + result.loss.rb_loss) / 4.0;
  499. double var = -1.;
  500. computer_var(loss, var);
  501. if (mean < min_mean_loss)
  502. {
  503. last_result = result;
  504. min_mean_loss = mean;
  505. }
  506. }
  507. // printf("z : %.3f angle : %.3f front : %.3f wheel_base:%.3f,width:%.3f, mean:%.5f\n",
  508. // center_z, last_result.theta, last_result.front_theta, last_result.wheel_base, last_result.width,
  509. // min_mean_loss);
  510. // std::cout << "\n-------- final z: " << chassis_z << std::endl;
  511. // std::cout << "cx: " << last_result.cx << ", cy: " << last_result.cy << ", theta: " << last_result.theta
  512. // << ", front: " << last_result.front_theta << ", wheelbase: " << last_result.wheel_base << ", width: " << last_result.width << std::endl << std::endl;
  513. // last_result.cx -= x;
  514. // last_result.cy -= y;
  515. // last_result.theta -= theta;
  516. // 角度校验,理论上car pose检测使用车身点云,应与车轮点云识别得到的车身 x偏移(0.025)与角度(<1°)一致
  517. double car_pose_theta_deg = 90 - car_pose_theta * 180.0 / M_PI;
  518. if (fabs(car_pose_x - last_result.cx) > 0.025 || fabs(car_pose_theta_deg - last_result.theta) > 1)
  519. {
  520. char valid_info[255];
  521. sprintf(valid_info, "validation failed for x and theta, carpose: (%.3f, %.3f), result: (%.3f, %.3f)", car_pose_x, car_pose_theta_deg, last_result.cx, last_result.theta);
  522. return Error_manager(VELODYNE_REGION_CERES_SOLVE_ERROR, NEGLIGIBLE_ERROR, valid_info);
  523. }
  524. // LOG_IF(INFO, m_region.region_id() == 4) << last_result.to_string();
  525. return SUCCESS;
  526. }
  527. int Ground_region::outOfRangeDetection(pcl::PointCloud<pcl::PointXYZ>::Ptr cloud, double plate_cx, double plate_cy, double theta)
  528. {
  529. if(cloud->size() <= 0)
  530. return Range_status::Range_correct;
  531. int res = Range_status::Range_correct;
  532. // 计算转盘旋转后点云坐标
  533. double theta_rad = theta * M_PI / 180.0;
  534. pcl::PointCloud<pcl::PointXYZ> t_cloud;
  535. for (size_t i = 0; i < cloud->size(); i++)
  536. {
  537. Eigen::Vector2d t_point(cloud->points[i].x - plate_cx, cloud->points[i].y - plate_cy);
  538. pcl::PointXYZ t_pcl_point;
  539. t_pcl_point.x = (t_point.x() * cos(theta_rad) - t_point.y() * sin(theta_rad)) + plate_cx;
  540. t_pcl_point.y = (t_point.x() * sin(theta_rad) + t_point.y() * cos(theta_rad)) + plate_cy;
  541. t_pcl_point.z = cloud->points[i].z;
  542. t_cloud.push_back(t_pcl_point);
  543. }
  544. pcl::PointXYZ min_p, max_p;
  545. pcl::getMinMax3D(t_cloud, min_p, max_p);
  546. // 判断左右超界情况
  547. if(min_p.x < m_region.border_minx())
  548. res |= Range_status::Range_left;
  549. if(max_p.x > m_region.border_maxx())
  550. res |= Range_status::Range_right;
  551. // LOG_IF(WARNING, m_region.region_id() == 4)<< "border minmax x: "<< min_p.x<<", "<<max_p.x<<", res: "<<res;
  552. return res;
  553. }
  554. int Ground_region::outOfRangeDetection(pcl::PointCloud<pcl::PointXYZ>::Ptr cloud, Common_data::Car_wheel_information measure_result, double plate_cx, double plate_cy, double theta)
  555. {
  556. int res = Range_status::Range_correct;
  557. if(cloud->size() <= 0)
  558. return res;
  559. // 计算转盘旋转后车辆中心点坐标
  560. Eigen::Vector2d car_center(measure_result.car_center_x, measure_result.car_center_y);
  561. Eigen::Vector2d car_uniform_center;
  562. double theta_rad = theta * M_PI / 180.0;
  563. car_uniform_center.x() = car_center.x() * cos(theta_rad) - car_center.y() * sin(theta_rad);
  564. car_uniform_center.y() = car_center.x() * sin(theta_rad) + car_center.y() * cos(theta_rad);
  565. // 车位位于y负方向,判断后轮超界情况
  566. double rear_wheel_y = car_uniform_center.y() + m_region.plc_offsety() - 0.5 * measure_result.car_wheel_base;
  567. if(rear_wheel_y < m_region.plc_border_miny())
  568. {
  569. res |= Range_status::Range_back;
  570. }
  571. // 判断车辆宽度超界情况
  572. if (measure_result.car_wheel_width < m_region.car_min_width() || measure_result.car_wheel_width > m_region.car_max_width())
  573. {
  574. res |= Range_status::Range_car_width;
  575. }
  576. // 判断车辆轴距超界情况
  577. if (measure_result.car_wheel_base < m_region.car_min_wheelbase() || measure_result.car_wheel_base > m_region.car_max_wheelbase())
  578. {
  579. res |= Range_status::Range_car_wheelbase;
  580. }
  581. // 判断车辆旋转角超界情况
  582. double dtheta = 90-measure_result.car_angle;
  583. if (dtheta > m_region.turnplate_angle_limit_anti_clockwise())
  584. {
  585. res |= Range_status::Range_angle_clock;
  586. }
  587. if (dtheta < -m_region.turnplate_angle_limit_clockwise())
  588. {
  589. res |= Range_status::Range_angle_anti_clock;
  590. }
  591. // // 判断车辆前轮角回正情况
  592. // if (fabs(measure_result.car_front_theta) > 8.0)
  593. // {
  594. // res |= Range_status::Range_steering_wheel_nozero;
  595. // }
  596. res |= outOfRangeDetection(cloud, plate_cx, plate_cy, theta);
  597. return res;
  598. }
  599. //外部调用获取当前车轮定位信息, 获取指令时间之后的车轮定位信息, 如果没有就会报错, 不会等待
  600. Error_manager Ground_region::get_current_wheel_information(Common_data::Car_wheel_information *p_car_wheel_information, std::chrono::system_clock::time_point command_time)
  601. {
  602. if ( p_car_wheel_information == NULL )
  603. {
  604. return Error_manager(Error_code::POINTER_IS_NULL, Error_level::MINOR_ERROR,
  605. " POINTER IS NULL ");
  606. }
  607. //获取指令时间之后的信息, 如果没有就会报错, 不会等待
  608. if( m_detect_update_time > command_time )
  609. {
  610. std::lock_guard<std::mutex> lck(m_car_result_mutex);
  611. *p_car_wheel_information = m_car_wheel_information;
  612. if(m_car_wheel_information.correctness)
  613. return Error_code::SUCCESS;
  614. else
  615. return Error_manager(Error_code::VELODYNE_REGION_CERES_SOLVE_ERROR, Error_level::MINOR_ERROR, " Ground_region detect error");
  616. }
  617. else
  618. {
  619. return Error_manager(Error_code::VELODYNE_REGION_EMPTY_NO_WHEEL_INFORMATION, Error_level::MINOR_ERROR,
  620. " Ground_region::get_current_wheel_information error ");
  621. }
  622. }
  623. //外部调用获取最新的车轮定位信息, 获取指令时间往前一个周期内的车轮定位信息, 如果没有就会报错, 不会等待
  624. Error_manager Ground_region::get_last_wheel_information(Common_data::Car_wheel_information *p_car_wheel_information, std::chrono::system_clock::time_point command_time)
  625. {
  626. if ( p_car_wheel_information == NULL )
  627. {
  628. return Error_manager(Error_code::POINTER_IS_NULL, Error_level::MINOR_ERROR,
  629. " POINTER IS NULL ");
  630. }
  631. //获取指令时间之后的信息, 如果没有就会报错, 不会等待
  632. // LOG(WARNING) << std::chrono::duration_cast<std::chrono::milliseconds>(command_time-m_detect_update_time).count()/1000.0 <<", "
  633. // <<std::chrono::duration_cast<std::chrono::milliseconds>(command_time-m_cloud_collection_time).count()/1000.0;
  634. if( m_detect_update_time > command_time - std::chrono::milliseconds(GROUND_REGION_DETECT_CYCLE_MS))
  635. {
  636. std::lock_guard<std::mutex> lck(m_car_result_mutex);
  637. *p_car_wheel_information = m_car_wheel_information;
  638. if(m_car_wheel_information.correctness)
  639. return Error_code::SUCCESS;
  640. else
  641. return Error_manager(Error_code::VELODYNE_REGION_CERES_SOLVE_ERROR, Error_level::MINOR_ERROR, " Ground_region detect error");
  642. }
  643. else
  644. {
  645. return Error_manager(Error_code::VELODYNE_REGION_EMPTY_NO_WHEEL_INFORMATION, Error_level::MINOR_ERROR,
  646. " Ground_region::get_current_wheel_information error ");
  647. }
  648. }
  649. Error_manager Ground_region::update_cloud(pcl::PointCloud<pcl::PointXYZ>::Ptr cloud)
  650. {
  651. // // 点云z转90度,调试用
  652. //Eigen::AngleAxisd rot_z = Eigen::AngleAxisd(M_PI_2, Eigen::Vector3d::UnitZ());
  653. //for (size_t i = 0; i < cloud->size(); i++)
  654. //{
  655. // Eigen::Vector3d t_point(cloud->points[i].x, cloud->points[i].y, cloud->points[i].z);
  656. // t_point = rot_z.toRotationMatrix() * t_point;
  657. // cloud->points[i].x = t_point.x();
  658. // cloud->points[i].y = t_point.y();
  659. // cloud->points[i].z = t_point.z();
  660. //}
  661. // 限定锁区域,解决因条件变量在析构时无法获得内部mutex导致此互斥锁卡住,造成死锁无法结束程序的问题。
  662. {
  663. std::lock_guard<std::mutex> lck(m_cloud_collection_mutex);
  664. mp_cloud_collection = cloud;
  665. // LOG(WARNING) << "update region cloud size: " << mp_cloud_collection->size() << ",,, input size: " << cloud->size();
  666. m_cloud_collection_time = std::chrono::system_clock::now();
  667. }
  668. m_measure_condition.notify_one(false, true);
  669. // std::chrono::steady_clock::time_point t1 = std::chrono::steady_clock::now();
  670. // std::chrono::duration<double> time_used_update = std::chrono::duration_cast<std::chrono::duration<double>>(t1 - t0);
  671. // std::cout << "update cloud time: " << time_used_update.count() << std::endl;
  672. return SUCCESS;
  673. }
  674. void Ground_region::thread_measure_func()
  675. {
  676. LOG(INFO) << " Ground_region::thread_measure_func() start " << this;
  677. while (m_measure_condition.is_alive())
  678. {
  679. m_measure_condition.wait();
  680. if (m_measure_condition.is_alive())
  681. {
  682. m_region_status = E_BUSY;
  683. detect_wheel_ceres3d::Detect_result t_result;
  684. Error_manager ec = detect(mp_cloud_collection, t_result);
  685. std::lock_guard<std::mutex> lck(m_car_result_mutex);
  686. // 增加滤波轴距
  687. Common_data::Car_wheel_information_stamped t_wheel_info_stamped;
  688. if (ec == SUCCESS)
  689. {
  690. m_car_wheel_information.correctness = true;
  691. m_car_wheel_information.car_center_x = t_result.cx;
  692. m_car_wheel_information.car_center_y = t_result.cy;
  693. m_car_wheel_information.car_angle = t_result.theta;
  694. m_car_wheel_information.car_wheel_base = t_result.wheel_base;
  695. m_car_wheel_information.car_wheel_width = t_result.width;
  696. m_car_wheel_information.car_front_theta = t_result.front_theta;
  697. m_car_wheel_information.theta_uniform(m_region.turnplate_cx(), m_region.turnplate_cy());
  698. // 超界校验
  699. {
  700. std::lock_guard<std::mutex> lck(m_filtered_cloud_mutex);
  701. int res = outOfRangeDetection(mp_cloud_filtered, m_car_wheel_information, m_region.turnplate_cx(), m_region.turnplate_cy(), 90.0 - t_result.theta);
  702. m_car_wheel_information.range_status = res;
  703. }
  704. // 添加plc偏移
  705. m_car_wheel_information.car_center_x += m_region.plc_offsetx();
  706. m_car_wheel_information.car_center_y += m_region.plc_offsety();
  707. m_car_wheel_information.uniform_car_x += m_region.plc_offsetx();
  708. m_car_wheel_information.uniform_car_y += m_region.plc_offsety();
  709. m_car_wheel_information.car_angle += m_region.plc_offset_degree();
  710. t_wheel_info_stamped.wheel_data = m_car_wheel_information;
  711. t_wheel_info_stamped.measure_time = m_detect_update_time;
  712. Measure_filter::get_instance_references().update_data(m_region.region_id(), t_wheel_info_stamped);
  713. ec = Measure_filter::get_instance_references().get_filtered_wheel_information(m_region.region_id(), t_wheel_info_stamped.wheel_data);
  714. if (ec == SUCCESS)
  715. {
  716. m_car_wheel_information.car_wheel_base = t_wheel_info_stamped.wheel_data.car_wheel_base;
  717. m_car_wheel_information.car_front_theta = t_wheel_info_stamped.wheel_data.car_front_theta;
  718. // 临时添加,滤波后前轮超界
  719. if(fabs(m_car_wheel_information.car_front_theta)>8.0)
  720. {
  721. m_car_wheel_information.range_status |= Range_status::Range_steering_wheel_nozero;
  722. }
  723. }
  724. // else{
  725. // std::cout<<ec.to_string()<<std::endl;
  726. // }
  727. //LOG_IF(INFO, m_region.region_id() == 4) << m_car_wheel_information.to_string();
  728. }
  729. else
  730. {
  731. m_car_wheel_information.correctness = false;
  732. // LOG_IF(ERROR, m_region.region_id() == 0) << ec.to_string();
  733. }
  734. m_detect_update_time = std::chrono::system_clock::now();
  735. }
  736. m_region_status = E_READY;
  737. }
  738. LOG(INFO) << " Ground_region::thread_measure_func() end " << this;
  739. }