微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

如何在类中传递Pointer成员

如何解决如何在类中传递Pointer成员

我有一个回调函数(适用于PointCloud2 msg类型的ROS订户)。 我希望能够在回调函数中传递一个不同的指针成员,因为我看到两次调用函数时,使用该指针的变量的值存在问题。

这是我的代码的一部分:

class Planner {
public:
  char frame_id[10] = "os1_lidar";
  int num_area = 1;
  
  int i = 0; // for create the circular path
  float right_point_y = 0;
  float left_point_y = 0;
  float y_distance = 0; //the distance between the trees in the Y axis
  float center_point_y = 0; //the center point between the trees in the y axis after average calculation
  float right_center_point_x = 0; //the center point of the right tree in the x axis
  float left_center_point_x = 0; //the center point of the left tree in the x axis
  float max_center_point_y = 1.4; //the max distance in the 7 axis for sending to arduino the steering angle
  
  //setting the two side area of the trees to filter the point cloud
  float minX_r = -5,minY_r = 2,minZ_r = -0.4;
  float maxX_r = -2,maxY_r = 4,maxZ_r = -0.1;
  float minX_l = -5,minY_l = -4,minZ_l = -0.4;
  float maxX_l = -2,maxY_l = -2,maxZ_l = 0.1;
  
  int red,green,blue = 0; //for change the Boxes' color outside the lines
  
  bool publish_points = true;
  bool publish_markers = true;
  
  ros::Publisher point_cloud_pub;
  ros::Publisher center_point_marker_pub;
  ros::Publisher right_point_marker_pub;
  ros::Publisher left_point_marker_pub;
  ros::Publisher marker_area_right_pub;
  ros::Publisher marker_area_left_pub;
  
  
  void Point_Filtering(const pcl::PointCloud<pcl::PointXYZ>::ConstPtr &cloud_msg);
}

void Planner::Point_Filtering(const pcl::PointCloud<pcl::PointXYZ>::ConstPtr &cloud_msg) {
  ROS_INFO_STREAM_ONCE("Getting Data from Lidar");
  ROS_INFO_STREAM_ONCE("size " << cloud_msg->size());
  ROS_INFO_STREAM_ONCE("width " << cloud_msg->width);
  ROS_INFO_STREAM_ONCE("height " << cloud_msg->height);
  // Right Line
  
  pcl::PointCloud<pcl::PointXYZ>::Ptr cloud_filtered_r(new pcl::PointCloud<pcl::PointXYZ>);
  //Left Line
  pcl::PointCloud<pcl::PointXYZ>::Ptr cloud_filtered_l(new pcl::PointCloud<pcl::PointXYZ>);
  
  //Right Trees Box Filter
  pcl::CropBox<pcl::PointXYZ> BoxFilter_r;
  BoxFilter_r.setInputCloud(cloud_msg);
  BoxFilter_r.setMin(Eigen::Vector4f(minX_r,minY_r,minZ_r,1.0));
  BoxFilter_r.setMax(Eigen::Vector4f(maxX_r,maxY_r,maxZ_r,1.0));
  BoxFilter_r.setNegative(false);
  BoxFilter_r.setTranslation(Eigen::Vector3f(0.0,0.0,0.0));
  BoxFilter_r.filter(*cloud_filtered_r);
  
  //Left Trees Box Filter
  pcl::CropBox<pcl::PointXYZ> BoxFilter_l;
  BoxFilter_l.setInputCloud(cloud_msg);
  BoxFilter_l.setMin(Eigen::Vector4f(minX_l,minY_l,minZ_l,1.0));
  BoxFilter_l.setMax(Eigen::Vector4f(maxX_l,maxY_l,maxZ_l,1.0));
  BoxFilter_l.setNegative(false);
  BoxFilter_l.setTranslation(Eigen::Vector3f(0.0,0.0));
  BoxFilter_l.filter(*cloud_filtered_l);
  
  //Right Trees (Y axis)
  Eigen::Vector4f centroid_r_y;
  pcl::compute3DCentroid(*cloud_filtered_r,centroid_r_y);
  right_point_y = centroid_r_y[1];
  
  //Left Trees (Y axis)
  Eigen::Vector4f centroid_l_y;
  pcl::compute3DCentroid(*cloud_filtered_l,centroid_l_y);
  left_point_y = centroid_l_y[1];
  
  y_distance = abs(centroid_l_y[1]) + abs(centroid_r_y[1]);
  center_point_y = (abs(centroid_l_y[1]) - abs(centroid_r_y[1])) / 2;
  
  //Right Trees (X axis)
  Eigen::Vector4f centroid_r_x;
  pcl::compute3DCentroid(*cloud_filtered_r,centroid_r_x);
  right_center_point_x = centroid_r_x[0];
  //    ROS_INFO_STREAM("right_point_x : "<< num_area << " :" << right_center_point_x);
  
  
  //Left Trees (X axis)
  Eigen::Vector4f centroid_l_x;
  pcl::compute3DCentroid(*cloud_filtered_l,centroid_l_x);
  left_center_point_x = centroid_l_x[0];
}

int main(int argc,char **argv) {
  ROS_INFO_STREAM_ONCE("Running");
  // Initialize ROS
  ros::init(argc,argv,"pcl_filter_node");
  ros::NodeHandle nh;
  //region 1st
  Planner planner;
  ros::Subscriber sub = nh.subscribe<pcl::PointCloud<pcl::PointXYZ> >("/fused_points",1,&Planner::Point_Filtering,&planner);
  planner.num_area = 1;
  planner.minX_r = -3;
  planner.minX_l = -3;
  planner.maxX_r = 0;
  planner.maxX_l = 0;
  planner.point_cloud_pub = nh.advertise<pcl::PointCloud<pcl::PointXYZ> >("pcl_filtered",1);
  planner.center_point_marker_pub = nh.advertise<visualization_msgs::Marker>("center_marker",1);
  planner.right_point_marker_pub = nh.advertise<visualization_msgs::Marker>("right_marker",1);
  planner.left_point_marker_pub = nh.advertise<visualization_msgs::Marker>("left_x_marker",1);
  planner.marker_area_right_pub = nh.advertise<visualization_msgs::Marker>("right_Box_marker",1);
  planner.marker_area_left_pub = nh.advertise<visualization_msgs::Marker>("left_Box_marker",1);
  //endregion
  
  //region 2nd
  Planner planner_2;
  planner_2.num_area = 2;
  planner_2.minX_r = -8 ;
  planner_2.minX_l = -8;
  planner_2.maxX_r = -5;
  planner_2.maxX_l = -5;
  planner_2.publish_points = false;
  planner_2.point_cloud_pub = nh.advertise<pcl::PointCloud<pcl::PointXYZ> >("pcl_filtered_2",1);
  planner_2.center_point_marker_pub = nh.advertise<visualization_msgs::Marker> 
    ("center_marker_2",1);
  planner_2.right_point_marker_pub = nh.advertise<visualization_msgs::Marker> 
    ("right_marker_2",1);
  planner_2.left_point_marker_pub = nh.advertise<visualization_msgs::Marker> 
    ("left_x_marker_2",1);
  planner_2.marker_area_right_pub = nh.advertise<visualization_msgs::Marker> 
    ("right_Box_marker_2",1);
  planner_2.marker_area_left_pub = nh.advertise<visualization_msgs::Marker> 
    ("left_Box_marker_2",1);
  ros::Subscriber sub2 = nh.subscribe<pcl::PointCloud<pcl::PointXYZ> >("/fused_points",&planner_2);
    
  //endregion
  ros::Rate r(15);
  ros::Time test_time;
  while (nh.ok()) {
    ros::spinOnce();
    r.sleep();
  }
}

代码获取原始点云,将其过滤到相关区域,然后在这些过滤后的点云中计算质心。

我要走这行:

pcl :: PointCloudpcl :: PointXYZ :: Ptr cloud_filtered_r(新 pcl :: PointCloudpcl :: PointXYZ);

并将其放在课程的公共部分,这样我将能够更改过滤点的名称。 当我两次订阅同一消息时,第二个区域的质心等于第一个区域的质心,这对我来说是错误的。

感谢您的帮助。

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。