0
$\begingroup$

Rosanswers logo

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?


Originally posted by max11gen on ROS Answers with karma: 164 on 2018-11-15

Post score: 0


Original comments

Comment by gvdhoorn on 2018-11-15:
Why did you delete your question?

Comment by max11gen on 2018-11-15:
@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.

Comment by max11gen on 2018-11-15:
@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.

Comment by gvdhoorn on 2018-11-15:
I would suggest to re-open the question, and answer your own question. Then accept that answer.

$\endgroup$

1 Answer 1

0
$\begingroup$

Rosanswers logo

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.


Originally posted by gvdhoorn with karma: 86574 on 2018-11-15

This answer was ACCEPTED on the original site

Post score: 1


Original comments

Comment by max11gen on 2018-11-15:
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++... ;))

Comment by gvdhoorn on 2018-11-15:
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.

Comment by max11gen on 2018-11-19:
You were right in the end, gvdhoorn. Using the + operator instead of printf worked just fine. Thanks for your help.

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.