C++ function that returns a ROS2 msg type . [closed]

asked 2023-05-18 23:59:43 -0500

updated 2023-05-19 02:42:00 -0500

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 std_msgs::msg::String::SharedPtr and do something and return the same std_msgs::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;
};
edit retag flag offensive reopen merge delete

Closed for the following reason the question is answered, right answer was accepted by chrissunny94
close date 2023-07-20 00:50:53.500765

Comments

Solved please find the solution above , hope it helps someone

chrissunny94 gravatar image chrissunny94  ( 2023-05-19 02:42:59 -0500 )edit
2

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.

gvdhoorn gravatar image gvdhoorn  ( 2023-05-19 06:03:05 -0500 )edit
130s gravatar image 130s  ( 2023-06-13 12:54:12 -0500 )edit