c++ 在无效的发布服务器上调用publish()时出错?

ecbunoof  于 2023-05-02  发布在  其他
关注(0)|答案(1)|浏览(225)

我创建了一个ROS节点,用于使用PCA从激光扫描中进行车道检测。也习惯于可视化车道。于是我创造了一个标记,然后发布了标记。代码可以用catkin_ make编译。但运行时节点是有错误的。这就是错误 *

[FATAL] [1682304485.986580762]: ASSERTION FAILED
    file = /opt/ros/noetic/include/ros/publisher.h
    line = 107
    cond = false
    message = 
[FATAL] [1682304485.989028993]: Call to publish() on an invalid Publisher
[FATAL] [1682304485.989061888]:

这里是节点

#include <iostream>
#include "ros/ros.h"
#include "std_msgs/String.h"
#include <vector> 
#include <sensor_msgs/LaserScan.h>
#include <sensor_msgs/PointCloud2.h>
#include <sensor_msgs/PointCloud.h>
#include <laser_geometry/laser_geometry.h>
#include <tf/transform_listener.h>
#include <Eigen/Core>
#include <pcl_ros/point_cloud.h>
#include <pcl/point_types.h>
#include <pcl_conversions/pcl_conversions.h>
#include "ros/ros.h"
#include <pcl/common/pca.h>
#include <pcl/io/io.h>
#include <pcl/conversions.h> 
#include <pcl/point_cloud.h>
#include <pcl/point_types.h>
#include <visualization_msgs/MarkerArray.h>
#include <pcl/io/pcd_io.h>
#include <pcl/PCLPointCloud2.h>
#include <pcl_conversions/pcl_conversions.h>

//using namespace std;
laser_geometry::LaserProjection projector_;
ros::Publisher pub_marker;

void getOrientation (const sensor_msgs::LaserScan::ConstPtr& scan)

{
    //convert laser scan into point clouds
    sensor_msgs::PointCloud2 cloud;
    projector_.projectLaser(*scan, cloud);

    //Construct a buffer used by the pca analysis
    pcl::PointCloud<pcl::PointXYZ>::Ptr cloud1(new pcl::PointCloud< pcl::PointXYZ>);   
    pcl::fromROSMsg (cloud, *cloud1);
    pcl::PCA<pcl::PointXYZ> pca;
    //pcl::PCA<PointT> pca;

    pca.setInputCloud(cloud1);
    Eigen::Vector3f eigen_values;
    eigen_values=pca.getEigenValues();
    Eigen::Matrix3f eigen_vector;
    eigen_vector=pca.getEigenVectors();

        /* 4. PCA Visualization */
    visualization_msgs::Marker points;
    points.header = cloud.header;
    points.header.frame_id = "/laser_frame"; // odom -> /base_link
    //points.header.frame_id = header.frame_id;
    points.header.stamp = cloud.header.stamp; // ros::Time::now() -> header.stamp
    
    points.ns = "pca"; // namespace + id
    points.id = 0; // pca/0
    points.action = visualization_msgs::Marker::ADD;
    points.type = visualization_msgs::Marker::ARROW;

    points.pose.orientation.w = 1.0;
    points.scale.x = 0.05;
    points.scale.y = 0.05;
    points.scale.z = 0.05;
    points.color.g = 1.0f;
    points.color.r = 0.0f;
    points.color.b = 0.0f;
    points.color.a = 1.0;

    geometry_msgs::Point p_0, p_1;
    p_0.x = 0; p_0.y = 0; p_0.z = 0; // get from tf
    p_1.x = eigen_vector(0,0);
    p_1.y = eigen_vector(0,1); // always negative
    std::cerr << "y = " << eigen_vector(0,1) << std::endl;
    //p_1.z = eigen_vector(0,2);
    points.points.push_back(p_0);
    points.points.push_back(p_1);
    pub_marker.publish(points);

}
 int main(int argc, char **argv)
 {
    
    // Initiate a new ROS node named "talker"
    ros::init(argc, argv, "lane_detection_pca_node");
    ros::NodeHandle nh;
    ros::Subscriber sub = nh.subscribe("/scan", 100, getOrientation);
    ros::Publisher pub_marker = nh.advertise<visualization_msgs::Marker>("points", 100);
    ros::spin();
    return 0;
}

这是可视化标记发布器的问题。有什么帮助吗?

uqjltbpv

uqjltbpv1#

声明全局变量:

//using namespace std;
laser_geometry::LaserProjection projector_;
ros::Publisher pub_marker;

ros::init()还没有被调用。如果删除ros::Publisher pub_marker;行,系统将正常工作

相关问题