Robotics StackExchange | Archived questions

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

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 stdmsgs/msg.string.String, got ).

Asked by farhan_haroon_ on 2022-10-02 10:31:53 UTC

Comments

Answers

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.

Asked by Mike Scheutzow on 2022-10-03 17:05:33 UTC

Comments

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).

Asked by ljaniec on 2022-10-03 19:03:34 UTC

Comments