ROS Resources: Documentation | Support | Discussion Forum | Index | Service Status | ros @ Robotics Stack Exchange
Ask Your Question
0

[SOLVED] How to subscribe a std_msgs::msg::String and copy in a std::string?

asked 2023-01-31 07:04:12 -0500

iopoi97 gravatar image

updated 2023-02-01 10:05:32 -0500

Hello I'm having some issues in subscribing a topic containing std_msgs::msg::string on ros2 galactic. I have defined a std::string and want to subscribe the topic and do something like `string=msg.data. Here my code:

#include <memory>
#include "rclcpp/rclcpp.hpp"
#include "std_msgs/msg/string.hpp"
#include "kdl_parser/kdl_parser.hpp"
using std::placeholders::_1;

class ROBOT_INVDYN : public rclcpp::Node{
    public:
        ROBOT_INVDYN() : Node("robot_invdyn"){
        subscription_ = this->create_subscription<std_msgs::msg::String> ("robot_description", 10, std::bind(&ROBOT_INVDYN::topic_callback,this, _1));
        }
    private:
        std::string robot;
        void topic_callback(const std_msgs::msg::String &msg)const{
            RCLCPP_INFO(this->get_logger(), "I heard: '%s'", msg.data.c_str());
            robot=msg.data;
        }
        rclcpp::Subscription<std_msgs::msg::String>::SharedPtr subscription_; 
};

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

but I keep on receiving errors on the operator =as:

error: no match for ‘operator=’ (operand types are ‘const string’ {aka ‘const std::__cxx11::basic_string<char>’} and ‘const _data_type’ {aka ‘const std::__cxx11::basic_string<char>’})
   17 |             robot_desc_string = msg.data;
      |                                     ^~~~

Could you help me?

edit retag flag offensive close merge delete

Comments

Please share your complete code

bvbdort gravatar image bvbdort  ( 2023-01-31 07:46:40 -0500 )edit

Yes, sure I will edit.

iopoi97 gravatar image iopoi97  ( 2023-01-31 10:10:15 -0500 )edit

1 Answer

Sort by » oldest newest most voted
1

answered 2023-01-31 12:16:21 -0500

I'm pretty sure its because you marked your method signature as const but are modifying a non-const member variable robot. You should remove the void topic_callback(const std_msgs::msg::String &msg) **const**.

edit flag offensive delete link more

Comments

perfect, it worked!

iopoi97 gravatar image iopoi97  ( 2023-02-01 10:05:03 -0500 )edit

Can you mark this as the right answer with the checkmark so it is removed from the unanswered questions queue?

stevemacenski gravatar image stevemacenski  ( 2023-02-01 15:10:49 -0500 )edit

Sorry for not having done this before!

iopoi97 gravatar image iopoi97  ( 2023-02-02 02:12:27 -0500 )edit

Question Tools

Stats

Asked: 2023-01-31 07:04:12 -0500

Seen: 647 times

Last updated: Feb 01 '23