带有成员类方法的ROS 2订阅者回调

问题描述

我想在回调函数中使用成员类方法。 以下C ++代码是使用成员类方法hello()进行回调的简单订阅者。

#include <functional>
#include <memory>

#include "rclcpp/rclcpp.hpp"
#include "std_msgs/msg/string.hpp"

using std::placeholders::_1;

class Subclass
{
private:
public:
    Subclass(/* args */);
    ~Subclass();
    std::string hello(std::string &s)
    {
        return "Hello " + s;
    }
};

class MinimalSubscriber : public rclcpp::Node
{
public:
    MinimalSubscriber()
        : Node("minimal_subscriber")
    {
        subscription_ = this->create_subscription<std_msgs::msg::String>(
            "topic",10,std::bind(&MinimalSubscriber::topic_callback,this,_1));
    }

private:
    Subclass subclass_;
    void topic_callback(const std_msgs::msg::String::SharedPtr msg) const
    {
        std::string s = subclass_.hello(msg->data);
        RCLCPP_INFO(this->get_logger(),"I heard: '%s'",s.c_str());
    }
    rclcpp::Subscription<std_msgs::msg::String>::SharedPtr subscription_;
};

int main(int argc,char *argv[])
{
    rclcpp::init(argc,argv);
    rclcpp::spin(std::make_shared<MinimalSubscriber>());
    rclcpp::shutdown();
    return 0;
}

我构建了C ++代码,并捕获了以下消息。我该如何修复代码? 在Ubuntu LTS 20.04中使用ROS2 Foxy进行构建。

/home/ubuntu/dev_ws/src/ros2_pigpio/src/subclass.cpp: In member function ‘void MinimalSubscriber::topic_callback(std_msgs::msg::String_<std::allocator<void> >::SharedPtr) const’:
/home/ubuntu/dev_ws/src/ros2_pigpio/src/subclass.cpp:35:50: error: passing ‘const Subclass’ as ‘this’ argument discards qualifiers [-fpermissive]
   35 |         std::string s = subclass_.hello(msg->data);
      |                                                  ^
/home/ubuntu/dev_ws/src/ros2_pigpio/src/subclass.cpp:15:17: note:   in call to std::string Subclass::hello(std::string&)’
   15 |     std::string hello(std::string &s)
      |                 ^~~~~
make[2]: *** [CMakeFiles/subclass.dir/build.make:63: CMakeFiles/subclass.dir/src/subclass.cpp.o] Error 1
make[1]: *** [CMakeFiles/Makefile2:84: CMakeFiles/subclass.dir/all] Error 2
make: *** [Makefile:141: all] Error 2

解决方法

首先,如果您将msg->data作为字符串传递,请使用msg->data.c_str()。因此,我相信您的std::string hello(std::string &s)方法在这种情况下必须是静态的,否则您必须使用构造函数创建子类的实例。

,

我可以建造一个。

#include <functional>
#include <memory>

#include "rclcpp/rclcpp.hpp"
#include "std_msgs/msg/string.hpp"

using std::placeholders::_1;

class Subclass
{
private:
public:
    Subclass(/* args */){};
    ~Subclass(){};
    std::string hello(std::string &s) const
    {
        return "Hello " + s;
    }
};

class MinimalSubscriber : public rclcpp::Node
{
public:
    MinimalSubscriber()
        : Node("minimal_subscriber")
    {
        subscription_ = this->create_subscription<std_msgs::msg::String>(
            "topic",10,std::bind(&MinimalSubscriber::topic_callback,this,_1));
    }

private:
    Subclass subclass_;
    void topic_callback(const std_msgs::msg::String::SharedPtr msg) const
    {
        std::string s = subclass_.hello(msg->data);
        RCLCPP_INFO(this->get_logger(),"I heard: '%s'",s.c_str());
    }
    rclcpp::Subscription<std_msgs::msg::String>::SharedPtr subscription_;
};

int main(int argc,char *argv[])
{
    rclcpp::init(argc,argv);
    rclcpp::spin(std::make_shared<MinimalSubscriber>());
    rclcpp::shutdown();
    return 0;
}

相关问答

错误1:Request method ‘DELETE‘ not supported 错误还原:...
错误1:启动docker镜像时报错:Error response from daemon:...
错误1:private field ‘xxx‘ is never assigned 按Alt...
报错如下,通过源不能下载,最后警告pip需升级版本 Requirem...