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

Calling ros::NodeHandle::subscribe with a topicname constructed by sprintf

asked 2018-11-15 05:43:08 -0600

max11gen gravatar image

My Topic names are supposed to change, when I use different sensors. Therefor I want my node to subscribe to different topics depending on parameters on the parameter server. I tried to use this in my CPP node:

char left_camera_topic[50]
std::string camera_type;
n.getParam("/neuromorphic_stereo/config/camera_type", camera_type);
sprintf(left_camera_topic,"/%s_left/events",camera_type);
ros::Subscriber leftCam = n.subscribe(left_camera_topic_string, subscriberCallback);

with

void subscriberCallback(const dvs_msgs::EventArray& msg);

But building this gets me the error

error: no matching function for call to 'ros::NodeHandle::subscribe(const char [50], void (&)(const EventArray&))' ros::Subscriber leftCam = n.subscribe(left_camera_topic_string, subscriberCallback);

Is it not possible to construct the topicnames like this, or am I doing something wrong?

edit retag flag offensive close merge delete

Comments

Why did you delete your question?

gvdhoorn gravatar image gvdhoorn  ( 2018-11-15 05:55:11 -0600 )edit

@gvdhoorn I'm sorry I deleted the question before you posted your answer because I just did a stupid mistake: I forgot the buffer-size argument of the subscribe function. adding that argument makes the code work and your answer therefor doesn't solve the problem.

max11gen gravatar image max11gen  ( 2018-11-15 06:12:05 -0600 )edit

@gvdhoorn I could, of course, reopen the question and you adjust your answer to fit the solution. And thanks for the remapping suggestion, I'll have a look at that.

max11gen gravatar image max11gen  ( 2018-11-15 06:17:27 -0600 )edit

I would suggest to re-open the question, and answer your own question. Then accept that answer.

gvdhoorn gravatar image gvdhoorn  ( 2018-11-15 08:43:09 -0600 )edit

1 Answer

Sort by ยป oldest newest most voted
1

answered 2018-11-15 05:54:28 -0600

gvdhoorn gravatar image

Is it not possible to construct the topicnames like this, or am I doing something wrong?

Raw char arrays are not std::strings, so that's why you get that error.

If you really must use char, you could wrap it in a std::string(..) ctor.

But I don't understand why you are using char in the first place: std::string is a type 'natively' supported by the ros::param API.

And an observation:

My Topic names are supposed to change, when I use different sensors. Therefor I want my node to subscribe to different topics depending on parameters on the parameter server.

Unless you are creating and destroying subscriptions at runtime (so have to change these during the entire runtime of your program, not just in the initialisation phase), don't do this. Don't parameterise topic names like this.

I would suggest to use remapping. See #q303611 for a (very) high-level overview of what that does.

edit flag offensive delete link more

Comments

The reason for me to use char-arrays instead of strings is that strings don't work as buffer in sprintf (at least I couldn't figure out, how. Kind of new to c++... ;))

max11gen gravatar image max11gen  ( 2018-11-15 06:21:47 -0600 )edit

But with std::string, the + operator can be used to concatenate strings, so no need for sprintf(..) it would seem.

But I would still not use parameters for topic names, as I wrote in my answer.

gvdhoorn gravatar image gvdhoorn  ( 2018-11-15 08:42:31 -0600 )edit

You were right in the end, gvdhoorn. Using the + operator instead of printf worked just fine. Thanks for your help.

max11gen gravatar image max11gen  ( 2018-11-19 09:01:10 -0600 )edit

Question Tools

1 follower

Stats

Asked: 2018-11-15 05:43:08 -0600

Seen: 393 times

Last updated: Nov 15 '18