【问题标题】:ROS Custom message with sensor_msgs/Image Publisher带有 sensor_msgs/Image Publisher 的 ROS 自定义消息
【发布时间】:2015-04-19 17:53:53
【问题描述】:

我有一个自定义的 .msg 文件 MyImage.msg

sensor_msgs/Image im
float32 age
string name

我已经配置了自定义 .msg 文件,如链接所示:CreatingMsgAndSrv

此外,我正在尝试使用此消息编写一个简单的发布者。

#include <ros/ros.h>

#include <custom_msg/MyImage.h>
#include <image_transport/image_transport.h>


#include <opencv2/highgui/highgui.hpp>
#include <opencv2/opencv.hpp>
#include <cv_bridge/cv_bridge.h>



int main( int argc, char ** argv )
{
    ros::init(argc, argv, "publish_custom");
    ros::NodeHandle nh;

    ros::Publisher pub2 = nh.advertise<custom_msg::MyImage>("custom_image", 2 );

    cv::Mat image = cv::imread( "Lenna.png", CV_LOAD_IMAGE_COLOR );
    sensor_msgs::ImagePtr im_msg = cv_bridge::CvImage(std_msgs::Header(), "bgr8", image).toImageMsg();

    ros::Rate rate( 2 );

    while( ros::ok() )
    {
        ROS_INFO_STREAM_ONCE( "IN main loop");


        custom_msg::MyImage msg2;
        msg2.age=54.3;
        msg2.im = im_msg;
        msg2.name="Gena";

        pub2.publish(msg2);

        rate.sleep();
    }
}

这似乎不能用 catkin_make 编译。错误消息是 -

/home/eeuser/ros_workspaces/HeloRosProject/src/custom_msg/publish.cpp: In function ‘int main(int, char**)’:
/home/eeuser/ros_workspaces/HeloRosProject/src/custom_msg/publish.cpp:40:19: error: no match for ‘operator=’ in ‘msg2.custom_msg::MyImage_<std::allocator<void> >::im = im_msg’
/home/eeuser/ros_workspaces/HeloRosProject/src/custom_msg/publish.cpp:40:19: note: candidate is:
/opt/ros/hydro/include/sensor_msgs/Image.h:56:8: note: sensor_msgs::Image_<std::allocator<void> >& sensor_msgs::Image_<std::allocator<void> >::operator=(const sensor_msgs::Image_<std::allocator<void> >&)
/opt/ros/hydro/include/sensor_msgs/Image.h:56:8: note:   no known conversion for argument 1 from ‘sensor_msgs::ImagePtr {aka boost::shared_ptr<sensor_msgs::Image_<std::allocator<void> > >}’ to ‘const sensor_msgs::Image_<std::allocator<void> >&’
make[2]: *** [custom_msg/CMakeFiles/publish.dir/publish.cpp.o] Error 1
make[1]: *** [custom_msg/CMakeFiles/publish.dir/all] Error 2
make: *** [all] Error 2
Invoking "make" failed

我可以理解msg2.im = im_msg; 不正确。请帮我解决这个问题。

【问题讨论】:

    标签: ros


    【解决方案1】:

    您正在尝试将sensor_msgs::ImagePtr(一个指针)分配给sensor_msgs::Image 字段。简直不能。只需查看错误日志的第五行:

    no known conversion for argument 1 from ‘sensor_msgs::ImagePtr {aka boost::shared_ptr<sensor_msgs::Image_<std::allocator<void> > >}’ to ‘const sensor_msgs::Image_<std::allocator<void> >&’
    

    要解决这个简单的问题,只需将解引用运算符 (*) 添加到该指针即可:

    msg2.im = *im_msg;
    

    我假设代码中没有其他错误。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-02-04
      • 1970-01-01
      • 1970-01-01
      • 2022-07-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多