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

How can I take a String input and publish it to a topic as a String?

asked 2022-10-02 10:31:53 -0600

farhan_haroon_ gravatar image

I am writing a node that takes String input from user and checks that string using simple if-else and publishes another String to a topic accordingly, but I'm getting a compile time error of different package ( expected std_msgs/msg_.string.String, got <str> ).

edit retag flag offensive close merge delete

2 Answers

Sort by ยป oldest newest most voted
0

answered 2022-10-03 17:05:33 -0600

Mike Scheutzow gravatar image

The online documentation is here: https://docs.ros.org/en/humble/

To see some example code on how to publish a message, click on the "Tutorials" link, then the "Writing a simple publisher and subscriber (C++)" link.

edit flag offensive delete link more
0

answered 2022-10-03 19:03:34 -0600

ljaniec gravatar image

ROS String.msg isn't the same as user input string - you have to use it like here, the part with the callback:

void timer_callback()
{
  auto message = std_msgs::msg::String();
  message.data = "Hello, world! " + std::to_string(count_++);
  RCLCPP_INFO(this->get_logger(), "Publishing: '%s'", message.data.c_str());
  publisher_->publish(message);
}

to use string from the user (so std_msgs::msg::String(), message data field is your C++ string).

edit flag offensive delete link more

Question Tools

Stats

Asked: 2022-10-02 10:31:53 -0600

Seen: 379 times

Last updated: Oct 03 '22