clamp_safety_detect.cpp 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329
  1. #include "clamp_safety_detect.h"
  2. bool clamp_safety_detect::fit_line(pcl::PointCloud<pcl::PointXYZ>::Ptr cloud, Line &line) {
  3. if (cloud->size() < 20)
  4. return false;
  5. Eigen::MatrixXd A, B;
  6. A.resize(cloud->size(), 2);
  7. B.resize(cloud->size(), 1);
  8. for (int i = 0; i < cloud->size(); ++i) {
  9. A(i, 0) = cloud->points[i].x;
  10. A(i, 1) = 1.0;
  11. B(i, 0) = cloud->points[i].y;
  12. }
  13. Eigen::MatrixXd P = (A.transpose() * A).inverse() * (A.transpose()) * B;
  14. float a = P(0, 0);
  15. float b = -1.0;
  16. float c = P(1, 0);
  17. float sum = 0;
  18. float sumx = 0, sumy = 0, w = 0, sumw = 0;
  19. for (int i = 0; i < cloud->size(); ++i) {
  20. pcl::PointXYZ pt = cloud->points[i];
  21. w = float(1 / pow(cos(atan(fabs(pt.x / pt.y))), 2));
  22. sumw += w;
  23. sumx += w * pt.x;
  24. sumy += pt.y;
  25. sum += std::pow(a * pt.x + b * pt.y + c, 2) / (a * a + b * b);
  26. }
  27. line.cov = sqrt(sum) / float(cloud->size());
  28. line.cx = sumx / sumw;
  29. line.cy = sumy / float(cloud->size());
  30. line.a = a;
  31. line.b = b;
  32. line.c = c;
  33. line.theta_by_x = acos(abs(b / sqrt(a * a + b * b))) * 180.0 / M_PI;
  34. line.cloud = cloud;
  35. //printf(" abc(%f,%f,%f) line.vonv cov:%f,%f,%f,%f,%f\n",a,b,c,line.theta_by_x,line.cov);
  36. return true;
  37. }
  38. bool compare_r(const Line &line1, const Line &line2) {
  39. return line1.theta_by_x < line2.theta_by_x;
  40. }
  41. bool pca(pcl::PointCloud<pcl::PointXY>::Ptr neighber, float &theta) {
  42. if (neighber->size() < 10) {
  43. return false;
  44. }
  45. float sumx = 0, sumy = 0;
  46. for (int i = 0; i < neighber->size(); ++i) {
  47. sumx += neighber->points[i].x;
  48. sumy += neighber->points[i].y;
  49. }
  50. float mean_x = sumx / float(neighber->size());
  51. float mean_y = sumy / float(neighber->size());
  52. Eigen::MatrixXd A(2, neighber->size());
  53. for (int i = 0; i < neighber->size(); ++i) {
  54. pcl::PointXY pt = neighber->points[i];
  55. A(0, i) = pt.x - mean_x;
  56. A(1, i) = pt.y - mean_y;
  57. }
  58. Eigen::MatrixXd cov = A * A.transpose();
  59. Eigen::EigenSolver<Eigen::MatrixXd> es(cov);
  60. Eigen::VectorXd values = es.eigenvalues().real().normalized();
  61. Eigen::MatrixXd vectors = es.eigenvectors().real();
  62. //printf("pca values: %.5f %.5f ",values[0],values[1]);
  63. float max_value = values[0];
  64. Eigen::Vector2d normal = vectors.col(0);
  65. if (values[1] > values[0]) {
  66. max_value = values[1];
  67. normal = vectors.col(1);
  68. }
  69. if (isnanf(max_value)) {
  70. return false;
  71. }
  72. if (max_value < 0.90) {
  73. return false;
  74. }
  75. float r = 90 * 0.005 / (2. * M_PI);
  76. theta = acos(abs(normal[0])) * r;
  77. return true;
  78. }
  79. void pca_cloud(pcl::PointCloud<pcl::PointXYZ>::Ptr cloud, pcl::PointCloud<pcl::PointXYZ>::Ptr features, int length) {
  80. if (cloud->size() < length) {
  81. printf("ERROR pca points size < %d\n", length);
  82. return;
  83. }
  84. features->clear();
  85. int fail_pca = 0;
  86. for (int i = length / 2; i < cloud->size() - length / 2; ++i) {
  87. pcl::PointCloud<pcl::PointXY>::Ptr neighber(new pcl::PointCloud<pcl::PointXY>);
  88. for (int j = i - length / 2; j < i + length / 2; ++j) {
  89. pcl::PointXYZ pt = cloud->points[j];
  90. if (pt.y < 0.05 || pt.y > 0.5 || fabs(pt.x) > 0.5)
  91. continue;
  92. pcl::PointXY p_xy;
  93. p_xy.x = cloud->points[j].x;
  94. p_xy.y = cloud->points[j].y;
  95. if (isnan(p_xy.x) || isnan(p_xy.y))
  96. continue;
  97. neighber->push_back(p_xy);
  98. }
  99. float theta = 0;
  100. if (pca(neighber, theta)) {
  101. pcl::PointXYZ pt = cloud->points[i];
  102. pt.z = theta;
  103. features->push_back(pt);
  104. } else {
  105. fail_pca++;
  106. }
  107. }
  108. }
  109. void pca_cloud(pcl::PointCloud<pcl::PointXYZ>::Ptr cloud, pcl::PointCloud<pcl::PointXYZ>::Ptr features, float radius) {
  110. if (cloud->size() < 200)
  111. return;
  112. pcl::search::KdTree<pcl::PointXYZ>::Ptr tree(new pcl::search::KdTree<pcl::PointXYZ>);
  113. //pcl::KdTreeFLANN<pcl::PointXYZ> tree;
  114. tree->setInputCloud(cloud);
  115. features->clear();
  116. for (int i = 0; i < cloud->size(); ++i) {
  117. std::vector<float> distances; //distance Vector
  118. std::vector<int> neighber_ids;
  119. tree->radiusSearch(cloud->points[i], radius, neighber_ids, distances); //KD tree
  120. pcl::PointCloud<pcl::PointXY>::Ptr neighber(new pcl::PointCloud<pcl::PointXY>);
  121. for (int neighber_id : neighber_ids) {
  122. pcl::PointXY p_xy{};
  123. p_xy.x = cloud->points[neighber_id].x;
  124. p_xy.y = cloud->points[neighber_id].y;
  125. neighber->push_back(p_xy);
  126. }
  127. float theta = 0;
  128. if (pca(neighber, theta)) {
  129. pcl::PointXYZ pt = cloud->points[i];
  130. pt.z = theta;
  131. features->push_back(pt);
  132. }
  133. }
  134. }
  135. clamp_safety_detect::clamp_safety_detect() = default;
  136. clamp_safety_detect::~clamp_safety_detect() = default;
  137. #if __VIEW__PCL
  138. void clamp_safety_detect::set_viewer(pcl::visualization::PCLVisualizer* viewer,int port)
  139. {
  140. m_viewer=viewer;
  141. m_port=port;
  142. }
  143. void clamp_safety_detect::view_cloud(pcl::PointCloud<pcl::PointXYZ>::Ptr cloud,std::string name,int r,int g,int b)
  144. {
  145. pcl::visualization::PointCloudColorHandlerCustom<pcl::PointXYZ> single_color2(cloud,r,g,b);
  146. m_viewer->addPointCloud(cloud, single_color2, name,m_port);
  147. m_viewer->setPointCloudRenderingProperties(pcl::visualization::PCL_VISUALIZER_POINT_SIZE, 1, name,m_port);
  148. }
  149. #endif
  150. std::vector<pcl::PointCloud<pcl::PointXYZ>::Ptr> clamp_safety_detect::classify_cloud(
  151. pcl::PointCloud<pcl::PointXYZ>::Ptr cloud) {
  152. std::vector<pcl::PointCloud<pcl::PointXYZ>::Ptr> cluster_cloud;
  153. //printf(" cloud size():%d\n",cloud->size());
  154. pcl::PointCloud<pcl::PointXYZ>::Ptr cloud_r(new pcl::PointCloud<pcl::PointXYZ>);
  155. pca_cloud(cloud, cloud_r, 20);
  156. if (cloud_r->size() == 0) {
  157. return cluster_cloud;
  158. }
  159. pcl::search::KdTree<pcl::PointXYZ>::Ptr tree(new pcl::search::KdTree<pcl::PointXYZ>);
  160. tree->setInputCloud(cloud_r);
  161. std::vector<pcl::PointIndices> cluster_indices;
  162. pcl::EuclideanClusterExtraction<pcl::PointXYZ> ec;
  163. ec.setClusterTolerance(0.1);
  164. ec.setMinClusterSize(100);
  165. ec.setMaxClusterSize(1000);
  166. ec.setSearchMethod(tree);
  167. ec.setInputCloud(cloud_r);
  168. ec.extract(cluster_indices);
  169. cluster_cloud.resize(cluster_indices.size());
  170. for (int i = 0; i < cluster_indices.size(); ++i) {
  171. pcl::PointCloud<pcl::PointXYZ>::Ptr clust(new pcl::PointCloud<pcl::PointXYZ>);
  172. for (int j = 0; j < cluster_indices[i].indices.size(); ++j) {
  173. pcl::PointXYZ pt;
  174. pcl::PointXYZ ptr = cloud_r->points[cluster_indices[i].indices[j]];
  175. pt.x = ptr.x;
  176. pt.y = ptr.y;
  177. pt.z = ptr.z; //z表示角度
  178. clust->push_back(pt);
  179. }
  180. cluster_cloud[i] = clust;
  181. }
  182. return cluster_cloud;
  183. }
  184. bool clamp_safety_detect::check_wheel_line(const Line &line, Safety_status &safety) {
  185. if (line.cloud->size() < 20) {
  186. printf("line cloud size <20\n");
  187. return false;
  188. }
  189. ///判断轮胎
  190. pcl::PointXYZ minp, maxp;
  191. pcl::getMinMax3D(*line.cloud, minp, maxp);
  192. float length = maxp.x - minp.x;
  193. if (line.cov < 0.03 && fabs(line.theta_by_x) < 10.
  194. && (length > 0.1 && length < 0.5)) {
  195. //前方直线是轮胎,判断中心是否居中、直线长度是否在范围内
  196. safety.cx = line.cx;
  197. // printf("safety.cx = %.3f\n", safety.cx);
  198. //雷达距离轮胎的距离 间隙
  199. safety.gap = fabs(line.cy);
  200. //中心偏差5cm内,可夹,plc决定
  201. safety.wheel_exist = true;
  202. }
  203. #if __VIEW__PCL
  204. char name[64]={0};
  205. //显示直线,正确为绿,偏移为黄,错误为红,
  206. char buf[64]={0};
  207. sprintf(buf,"cx:%.4f,gap:%.4f,Θ:%.4f cov:%.4f",safety.cx,fabs(line.cy),line.theta_by_x,line.cov);
  208. sprintf(name,"wheel_%d",m_port);
  209. m_viewer->addText3D(buf,pcl::PointXYZ(line.cx-0.2,line.cy,0),0.01,0,1,0,"wheel",m_port);
  210. float x1=line.cx-0.2;
  211. float x2=line.cx+0.2;
  212. float y1=-(x1*line.a/line.b+line.c/line.b);
  213. float y2=-(x2*line.a/line.b+line.c/line.b);
  214. sprintf(name,"line_%d",m_port);
  215. if (safety.wheel_exist)
  216. {
  217. m_viewer->addLine(pcl::PointXYZ(x1,y1,0), pcl::PointXYZ(x2,y2,0), 0, 255, 0, name,m_port);
  218. }
  219. else if (fabs(safety.cx) < 0.5)
  220. {
  221. m_viewer->addLine(pcl::PointXYZ(x1,y1,0), pcl::PointXYZ(x2,y2,0), 255, 255, 0, name,m_port);
  222. }
  223. else
  224. {
  225. m_viewer->addLine(pcl::PointXYZ(x1,y1,0), pcl::PointXYZ(x2,y2,0), 255, 0, 0, name,m_port);
  226. }
  227. #endif
  228. return safety.wheel_exist;
  229. }
  230. bool clamp_safety_detect::check_clamp_lines(const Line &line1, const Line &line2, Safety_status &safety) {
  231. if (fabs(line1.theta_by_x - line2.theta_by_x) > 10 || line1.cov > 0.03 || line2.cov > 0.03) {
  232. }
  233. if (fabs(line1.theta_by_x - 90.) < 15. && fabs(line2.theta_by_x - 90.) < 15.)
  234. safety.clamp_completed = true;
  235. #if __VIEW__PCL
  236. char name[64]={0};
  237. float x1=line1.cx-0.2;
  238. float x2=line1.cx+0.2;
  239. float y1=-(x1*line1.a/line1.b+line1.c/line1.b);
  240. float y2=-(x2*line1.a/line1.b+line1.c/line1.b);
  241. sprintf(name,"line1_%d",m_port);
  242. m_viewer->addLine(pcl::PointXYZ(x1,y1,0), pcl::PointXYZ(x2,y2,0), 255, 0, 0, name,m_port);
  243. char buf[32]={0};
  244. sprintf(buf,"cx:%.3f,Θ:%.1f cov:%.3f",line1.cx,line1.theta_by_x,line1.cov);
  245. sprintf(name,"line1_txt_%d",m_port);
  246. m_viewer->addText3D(buf,pcl::PointXYZ(line1.cx-0.15,line1.cy,0),0.01,0,1,0,name,m_port);
  247. x1=line2.cx-0.2;
  248. x2=line2.cx+0.2;
  249. y1=-(x1*line2.a/line2.b+line2.c/line2.b);
  250. y2=-(x2*line2.a/line2.b+line2.c/line2.b);
  251. sprintf(name,"line2_%d",m_port);
  252. m_viewer->addLine(pcl::PointXYZ(x1,y1,0), pcl::PointXYZ(x2,y2,0), 255, 0, 0, name,m_port);
  253. memset(buf,0,32);
  254. sprintf(buf,"cx:%.3f,Θ:%.3f cov:%.3f",line2.cx,line2.theta_by_x,line2.cov);
  255. sprintf(name,"line2_txt%d",m_port);
  256. m_viewer->addText3D(buf,pcl::PointXYZ(line2.cx-0.15,line2.cy,0),0.01,0,1,0,name,m_port);
  257. #endif
  258. return true;
  259. }
  260. bool clamp_safety_detect::detect(pcl::PointCloud<pcl::PointXYZ>::Ptr cloud, Safety_status &safety) {
  261. #if __VIEW__PCL
  262. m_viewer->removeAllPointClouds(m_port);
  263. m_viewer->removeAllShapes(m_port);
  264. char buf[32]={0};
  265. sprintf(buf,"cloud_%d",m_port);
  266. view_cloud(cloud,buf,255,255,255);
  267. #endif
  268. //找直线,记录内点率,直线与x轴夹角,斜率b/a
  269. Line line;
  270. if (fit_line(cloud, line) && check_wheel_line(line, safety)) {
  271. return true;
  272. }
  273. return false;
  274. }