Creating a Custom String/Char

I’m working on the topics quiz right now but I guess this problem applies elsewhere too.

As you can see in my cpp code, I’m attempting to create a string called turnStatus so I can see the status of the robot whether it is moving forward or turning for debugging purposes.


I’ve tried defining turnStatus both as std_Msgs::Char and std_msgs::String or just Char and String alone and they all seem to not work. Each time I get an error message like the following:

I know I can get my program to work even without the debugging message but it would be nice if I can have them to show. Could you please advise me the best way to create this kind of strings for debugging purposes. Thank you!

Hey @aw3645 you are mixing concepts here:

  1. One thing are the C++ strings class, used to contain string variables that will contain useful information for the program.
  2. Another thing is the ROS String message, used to create messages that contain a string and can be published in topics so other nodes can read and get the info

In your case, you need to use the 1st case because you are just storing local information in your program for later decision making inside the same program. You are not going to pass that info to another ROS node (publish). Hence you don’t need to use a std_msgs:String but a std::String

So,

  • Include the String heather of C++
    include <string>
  • Then create an instance of a String
    std::string turnStatus
  • You can fill the value as you are doing in your example above
  • Then print using ROS_INFO
    ROS_INFO("%s", turnStatus.c_str());

If you want to go PRO: do not create global variables, use instead C++ classes

1 Like

Thank you @rtellez @duckfrost both very helpful information!