Robotics StackExchange | Archived questions

C++ function that returns a ROS2 msg type .

Hello ROS community , i am sure this question has been asked numerous times . But unable to track down an answer for the issue i am facing at the moment .

I am trying to make a function return a ROS2 msg type , i know how to create call backs . I wanted a function that will take input as stdmsgs::msg::String::SharedPtr and do something and return the same stdmsgs::msg::String::SharedPtr

I am getting confused with all this const and SharedPtr . Please take a look at it and tell me how this should be done or point out to an example code that i can use .

    #include "minimal_unit_testing/sub_pub.h"

using std::placeholders::_1;

SubscriberPublisher::SubscriberPublisher() : Node("minimal_subscriber") {
    subscription_ = this->create_subscription<std_msgs::msg::String>(
        "input_topic", 10, std::bind(&SubscriberPublisher::topic_callback, this, _1));

    publisher_ = this->create_publisher<std_msgs::msg::String>("output_topic", 10);
    //
};

void SubscriberPublisher::topic_callback(const std_msgs::msg::String::SharedPtr msg) const {
    RCLCPP_INFO(this->get_logger(), "I heard: '%s'", msg->data.c_str());
    auto message_out = std_msgs::msg::String();
    std::string message_out_data;
    message_out = this->function_to_test(msg);
    // publisher_->publish(message_out);
};


std_msgs::msg::String SubscriberPublisher::function_to_test(std_msgs::msg::String& data) {
    std_msgs::msg::String& message_out;
    if (data == "Hello World") {
        message_out->data = "Greetings";
    }

    return message_out;
};

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

and the Headerfile

#pragma once
#include <std_msgs/msg/string.hpp>
#include <string>

#include "rclcpp/rclcpp.hpp"

#define BOOST_BIND_NO_PLACEHOLDERS

#include <chrono>
#include <functional>
#include <memory>
#include <rclcpp/rclcpp.hpp>
#include <string>
#include <vector>

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


using namespace std;


class SubscriberPublisher : public rclcpp::Node {
   public:  // Access specifier
    SubscriberPublisher();

   private:
    rclcpp::Subscription<std_msgs::msg::String>::SharedPtr subscription_;
    rclcpp::Publisher<std_msgs::msg::String>::SharedPtr publisher_;
    void topic_callback(const std_msgs::msg::String::SharedPtr msg) const;
    std_msgs::msg::String function_to_test( const std_msgs::msg::String& data);
};


#endif

FINALLY SOLVED it

#include "minimal_unit_testing/sub_pub.h"

using std::placeholders::_1;

SubscriberPublisher::SubscriberPublisher() : Node("minimal_subscriber") {

    subscription_ = this->create_subscription<std_msgs::msg::String>(
        "input_topic", 10, std::bind(&SubscriberPublisher::topic_callback, this, _1));

    publisher_ = this->create_publisher<std_msgs::msg::String>("output_topic", 10);
    //
};

void SubscriberPublisher::topic_callback(const std_msgs::msg::String::SharedPtr msg) const {
    RCLCPP_INFO(this->get_logger(), "I heard: '%s'", msg->data.c_str());

    auto message_out = std_msgs::msg::String();
    std::string message_out_data;
    this->function_to_test(msg);
    RCLCPP_INFO(this->get_logger(), "I heard back : '%s'", this->function_to_test(msg)->data.c_str());

    publisher_->publish(message_out);
};


std_msgs::msg::String::SharedPtr SubscriberPublisher::function_to_test(std_msgs::msg::String::SharedPtr input_data) const {
    auto message_out = std_msgs::msg::String();
    if (input_data->data == "hello") {
        message_out.data = "Greetings";
    }
    return input_data;
};

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

& Header

#include <std_msgs/msg/string.hpp>
#include <string>

#include "rclcpp/rclcpp.hpp"

#define BOOST_BIND_NO_PLACEHOLDERS

#include <chrono>
#include <functional>
#include <memory>
#include <rclcpp/rclcpp.hpp>
#include <string>
#include <vector>

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

using namespace std;

class SubscriberPublisher : public rclcpp::Node {
   public:  // Access specifier
    SubscriberPublisher();

   private:
    rclcpp::Subscription<std_msgs::msg::String>::SharedPtr subscription_;
    rclcpp::Publisher<std_msgs::msg::String>::SharedPtr publisher_;
    void topic_callback(const std_msgs::msg::String::SharedPtr msg) const;
    std_msgs::msg::String::SharedPtr function_to_test(std_msgs::msg::String::SharedPtr input_data) const;
};

Asked by chrissunny94 on 2023-05-18 23:59:43 UTC

Comments

Solved please find the solution above , hope it helps someone

Asked by chrissunny94 on 2023-05-19 02:42:59 UTC

Please post solutions as answers, and then accept your own answer.

Closed questions look like they have no resolution, while in fact this one does.

Asked by gvdhoorn on 2023-05-19 06:03:05 UTC

Ping @chrissunny94 :)

Asked by 130s on 2023-06-13 12:54:12 UTC

Answers