safety_excutor.cpp 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  1. //
  2. // Created by zx on 2021/12/16.
  3. //
  4. #include "safety_excutor.h"
  5. #include <pcl/filters/passthrough.h>
  6. #include <pcl/common/common.h>
  7. safety_excutor::safety_excutor()
  8. {
  9. mp_lidars=nullptr;
  10. }
  11. safety_excutor::~safety_excutor()
  12. {
  13. if(mp_lidars!= nullptr)
  14. {
  15. delete[] mp_lidars;
  16. mp_lidars= nullptr;
  17. }
  18. }
  19. bool safety_excutor::init(const shutter::shutter_param& parameter)
  20. {
  21. if(parameter.lidars_size()==0)
  22. return false;
  23. mp_lidars=new livox::LivoxMid70[parameter.lidars_size()];
  24. const float DEGREE=M_PI/180.0;
  25. for(int i=0;i<parameter.lidars_size();++i)
  26. {
  27. shutter::lidar_parameter ld_p=parameter.lidars(i);
  28. mp_lidars[i].AddBroadcastCode(parameter.lidars(i).sncode());
  29. mp_lidars[i].SetFPS(parameter.lidars(i).fps());
  30. mp_lidars[i].SetTransformParam(Eigen::Vector3f(ld_p.r()*DEGREE,ld_p.p()*DEGREE,ld_p.y()*DEGREE),
  31. Eigen::Vector3f(ld_p.tx(),ld_p.ty(),ld_p.tz()));
  32. }
  33. m_parameter=parameter;
  34. return true;
  35. }
  36. safety_excutor* safety_excutor::CreateExcutor(const shutter::shutter_param& parameter)
  37. {
  38. safety_excutor* excutor=new safety_excutor();
  39. if(excutor->init(parameter)==false)
  40. return nullptr;
  41. return excutor;
  42. }
  43. #if VIEW
  44. #include <pcl/visualization/pcl_visualizer.h>
  45. pcl::visualization::PCLVisualizer viewer("Cloud");
  46. #endif
  47. pcl::PointCloud<pcl::PointXYZ>::Ptr safety_excutor::PassThroughCloud(pcl::PointCloud<pcl::PointXYZ>::Ptr cloud,
  48. const shutter::box_param& box)
  49. {
  50. pcl::PointCloud<pcl::PointXYZ>::Ptr cloud_inner(new pcl::PointCloud<pcl::PointXYZ>);
  51. //限制 x y z
  52. pcl::PassThrough<pcl::PointXYZ> passX;
  53. pcl::PassThrough<pcl::PointXYZ> passY;
  54. pcl::PassThrough<pcl::PointXYZ> passZ;
  55. passX.setInputCloud(cloud);
  56. passX.setFilterFieldName("x");
  57. passX.setFilterLimits(box.minx(), box.maxx());
  58. passX.filter(*cloud_inner);
  59. passY.setInputCloud(cloud_inner);
  60. passY.setFilterFieldName("y");
  61. passY.setFilterLimits(box.miny(), box.maxy());
  62. passY.filter(*cloud_inner);
  63. passY.setInputCloud(cloud_inner);
  64. passY.setFilterFieldName("z");
  65. passY.setFilterLimits(box.minz(), box.maxz());
  66. passY.filter(*cloud_inner);
  67. return cloud_inner;
  68. }
  69. bool safety_excutor::verify(tagMeasureData& data)
  70. {
  71. pcl::PointCloud<pcl::PointXYZ>::Ptr cloud(new pcl::PointCloud<pcl::PointXYZ>);
  72. bool connected=true;
  73. for(int i=0;i<m_parameter.lidars_size();++i)
  74. {
  75. if(mp_lidars[i].is_connected()==false)
  76. connected=false;
  77. pcl::PointCloud<pcl::PointXYZ>::Ptr cloud_t=mp_lidars[i].GetCloud();
  78. (*cloud)+=(*cloud_t);
  79. }
  80. shutter::box_param box=m_parameter.box();
  81. pcl::PointCloud<pcl::PointXYZ>::Ptr cloud_inner=PassThroughCloud(cloud,box);
  82. shutter::box_param box1=m_parameter.verify_box1();
  83. shutter::box_param box2=m_parameter.verify_box2();
  84. pcl::PointCloud<pcl::PointXYZ>::Ptr cloud_warnning1=PassThroughCloud(cloud_inner,box1);
  85. pcl::PointCloud<pcl::PointXYZ>::Ptr cloud_warnning2=PassThroughCloud(cloud_inner,box2);
  86. bool safe1=(cloud_warnning1->size()<=20),safe2=((cloud_warnning2->size()<=20));
  87. //std::cout<<" v1:"<<cloud_warnning1->size()<<" v2:"<<cloud_warnning2->size()<<std::endl;
  88. data.wheel_path_safety=(unsigned short)(safe1&safe2&connected);
  89. shutter::box_param box_parkspace,box_left,box_right;
  90. if(box1.maxx()<box2.minx())
  91. {
  92. box_left=box1;
  93. box_right=box2;
  94. }
  95. else
  96. {
  97. box_left=box2;
  98. box_right=box1;
  99. }
  100. box_parkspace=box1;
  101. box_parkspace.set_minx(box_left.maxx());
  102. box_parkspace.set_maxx(box_right.minx());
  103. pcl::PointCloud<pcl::PointXYZ>::Ptr cloud_parkspace=PassThroughCloud(cloud_inner,box_parkspace);
  104. data.parkspcae_safety=(cloud_parkspace->size()<=5)?(connected&true):(connected&false);
  105. //计算缝隙与角度
  106. if(cloud_inner->size()!=0)
  107. {
  108. pcl::PointXYZ minp, maxp;
  109. pcl::getMinMax3D(*cloud_inner, minp, maxp);
  110. float l = minp.x - box_left.maxx();
  111. float r = box_right.minx() - maxp.x;
  112. data.left_space = (l >= 0) ? l : 0;
  113. data.right_space = (r >= 0) ? r : 0;
  114. //优化求解角度与偏移
  115. data.theta = 0;
  116. data.offset_x = 0;
  117. }
  118. else
  119. {
  120. data.left_space = 2.0;
  121. data.right_space = 2.0;
  122. //优化求解角度与偏移
  123. data.theta = 0;
  124. data.offset_x = 0;
  125. }
  126. #if VIEW
  127. viewer.removePointCloud(m_parameter.ShortDebugString()+"cloud");
  128. viewer.removeShape(m_parameter.ShortDebugString()+"cube1");
  129. viewer.removeShape(m_parameter.ShortDebugString()+"cube2");
  130. //绘制矩形
  131. Eigen::AngleAxisf rotation = Eigen::AngleAxisf(0, Eigen::Vector3f::UnitZ());
  132. viewer.addCube(Eigen::Vector3f((box1.minx()+box1.maxx())/2.0,(box1.miny()+box1.maxy())/2.0,(box1.minz()+box1.maxz())/2.0),
  133. Eigen::Quaternionf(rotation), box1.maxx()-box1.minx(), box1.maxy()-box1.miny(),box1.maxz()-box1.minz(),
  134. m_parameter.ShortDebugString()+"cube1");
  135. viewer.addCube(Eigen::Vector3f((box2.minx()+box2.maxx())/2.0,(box2.miny()+box2.maxy())/2.0,(box2.minz()+box2.maxz())/2.0),
  136. Eigen::Quaternionf(rotation), box2.maxx()-box2.minx(), box2.maxy()-box2.miny(),box2.maxz()-box2.minz(),
  137. m_parameter.ShortDebugString()+"cube2");
  138. viewer.setShapeRenderingProperties(pcl::visualization::PCL_VISUALIZER_COLOR, 1, 0.5, 0, m_parameter.ShortDebugString()+"cube1");
  139. viewer.setShapeRenderingProperties(pcl::visualization::PCL_VISUALIZER_OPACITY, 0.3, m_parameter.ShortDebugString()+"cube1");
  140. viewer.setShapeRenderingProperties(pcl::visualization::PCL_VISUALIZER_COLOR, 0, 0.5, 1, m_parameter.ShortDebugString()+"cube2");
  141. viewer.setShapeRenderingProperties(pcl::visualization::PCL_VISUALIZER_OPACITY, 0.3, m_parameter.ShortDebugString()+"cube2");
  142. pcl::visualization::PointCloudColorHandlerCustom<pcl::PointXYZ> single_color2(cloud,
  143. 255*int(!data.wheel_path_safety),255*int(data.wheel_path_safety),255*int(!data.parkspcae_safety));
  144. viewer.addPointCloud(cloud, single_color2, m_parameter.ShortDebugString()+"cloud");
  145. viewer.setPointCloudRenderingProperties(pcl::visualization::PCL_VISUALIZER_POINT_SIZE, 1, m_parameter.ShortDebugString()+"cloud");
  146. viewer.spinOnce();
  147. #endif
  148. printf(" shutter recieve points %d l_space:%.3f r_space:%.3f wheel_safety: %d parkspace_safety: %d\n",
  149. cloud->size(),data.left_space,data.right_space,data.wheel_path_safety,data.parkspcae_safety);
  150. //保存一帧
  151. /*static bool saved=false;
  152. if(saved==false)
  153. {
  154. FILE* p=fopen("./cloud.txt","w");
  155. for(int i=0;i<cloud->size();++i)
  156. {
  157. fprintf(p,"%f %f %f\n",cloud->points[i].x,cloud->points[i].y,cloud->points[i].z);
  158. }
  159. fclose(p);
  160. saved=true;
  161. }*/
  162. return data.wheel_path_safety;
  163. }
  164. bool safety_excutor::pointInRectangle(pcl::PointXYZ point,const shutter::box_param& box)
  165. {
  166. float x=point.x;
  167. float y=point.y;
  168. float z=point.z;
  169. if(x>box.minx()&&x<box.maxx()&&y>box.miny()&&y<box.maxy()&&z>box.minz()&&z<box.maxz())
  170. return true;
  171. return false;
  172. }