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

Ros2 service String providing unexpected Result �2���

asked 2021-05-05 05:57:04 -0500

anni gravatar image

updated 2021-05-05 12:17:54 -0500

gvdhoorn gravatar image

Hi, I am using : kernel Linux 5.8.0-50generic(x86_64) Distribution Ubuntu 20.04.2LTS ROS2-Foxy C++ std 14 I am trying to create a service In which i passing two string and expected to get a concatinated string but i am gettin g unexpected results. Here are the output texts

SERVICE OUTPUT:-

[INFO] [1620193993.198502097] [rclcpp]: Ready to add two string.
[WARN] [1620193993.278931507] [rclcpp]: this is what i am receiving 123
[INFO] [1620193993.279032788] [rclcpp]: Incoming request
a: �.��� b: �2���
[INFO] [1620193993.279054157] [rclcpp]: sending back response: [�2���]
[WARN] [1620195212.142007473] [rclcpp]: this is what i am receiving 123
[INFO] [1620195212.142103931] [rclcpp]: Incoming request
a: �.��� b: �2���
[INFO] [1620195212.142131196] [rclcpp]: sending back response: [�2���]

CLIENT SIDE TEXT:-

(FIRST ATTEMP)
[INFO] [1620195212.073202455] [rclcpp]: this is what we are sending @mG��
[INFO] [1620195212.142851611] [rclcpp]: Result: @mG��

(SECOND ATTEMPT)
[INFO] [1620193990.932057565] [rclcpp]: service not available, waiting again...
[INFO] [1620193991.932632967] [rclcpp]: service not available, waiting again...
[INFO] [1620193992.933087641] [rclcpp]: service not available, waiting again...
[INFO] [1620193993.279721272] [rclcpp]: Result: ��H;�

I am attaching github link for source code:- https://github.com/aniketkalra11/rcl_...

edit retag flag offensive close merge delete

1 Answer

Sort by » oldest newest most voted
5

answered 2021-05-05 09:03:32 -0500

updated 2021-05-05 09:10:30 -0500

When printing an std::string (using the %s specifier), you need to actually print the underlying C string and not the std::string object. Otherwise you will get "garbage" like that. This is a very common error with the printf functions (and RCLCPP_INFOdoes pretty much the same thing underneath). For example: https://stackoverflow.com/questions/7...

You can do this by calling the c_str() method of the std::string object. I actually just tested and you should be getting a compiler warning if you don't do that. If you did get warnings when compiling, don't ignore them!

In your case, this would look like:

RCLCPP_INFO(rclcpp::get_logger("rclcpp"), "sending back response: [%s]", response.get()->concatinated_aa_bb.c_str());

Same thing for the other times you use RCLCPP_INFO with std::string objects. Just add .c_str() after all std::string variables to print them.

edit flag offensive delete link more

Question Tools

1 follower

Stats

Asked: 2021-05-05 03:05:47 -0500

Seen: 1,036 times

Last updated: May 05 '21