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

write in a vector through the same (ROS) callback using boost::bind (C++)

asked 2013-06-10 22:12:01 -0600

mateo_7_7 gravatar image

updated 2014-01-28 17:16:50 -0600

ngrennan gravatar image

i'm using ROS topics in order to acquire some image streams from a simulator. the idea is the following: i have "n" visual sensors and i'd like to declare n subscriber (through a for cycle) that read n different topics and call the same callback: passing an integer to the c.b. i'll be able to fill a vector of images (each of them received on a different topic).

this is my code:

vector <IplImage*> Images;

void newImageTrigger_trgI(const sensor_msgs::ImageConstPtr& msg, int i)
{
 cv_bridge::CvImagePtr cv_ptr;

 try
 {

cv_ptr = cv_bridge::toCvCopy(msg, enc::BGR8);

Images[i] = cv_ptr->image;

}
catch (cv_bridge::Exception& e)
{
ROS_ERROR("cv_bridge exception: %s", e.what());
}

}

int main( int argc, char** argv )
{

ros::init(argc, argv, "ImageProcessing");
ros::NodeHandle n;

std::vector<ros::Subscriber> sub;
for (int i = 1; i < 6; i++)
{
string str_i=string_converter(i);
 sub[i] = n.subscribe("/vrep/visionSensorData"+str_i, 1,...   //ERROR
   boost::bind(&newImageTrigger_trgI, _1, i));
 }

  .....

Obviously: "invalid arguments" at //ERROR

can someone help me please?...i think it's something concerning boost::bind function thanks

edit retag flag offensive close merge delete

2 Answers

Sort by ยป oldest newest most voted
2

answered 2013-06-18 08:27:47 -0600

t.pimentel gravatar image

updated 2014-08-27 17:20:11 -0600

Good afternoon,

I think I had the same problem you're having and what I did to fix it was substituting this line:

sub[i] = n.subscribe("/vrep/visionSensorData"+str_i, 1, boost::bind(&newImageTrigger_trgI, _1, i));

for this:

sub[i] = n.subscribe <sensor_msgs::ImageConstPtr> ("/vrep/visionSensorData"+str_i, 1, boost::bind(&newImageTrigger_trgI, _1, i));

I think, when you bind the parameter with the boost library you have to specify the type of the callback item to the node handler, but am not sure why.

edit flag offensive delete link more

Comments

This solved the problem for me as well. Thanks!

eliasm gravatar image eliasm  ( 2014-10-07 05:24:04 -0600 )edit
1

answered 2013-06-10 22:36:00 -0600

Philip gravatar image

updated 2013-06-10 22:37:55 -0600

While the ROS specific stuff looks OK on first glance, it is always helpful to post the full error message instead of just saying "invalid arguments" ;-)

I assume that the way you try to create your string is your main problem. Try

for (int i = 1; i < 6; i++)
{
  std::stringstream topicName;
  topicName << "/vrep/visionSensorData" << i;
  sub[i] = n.subscribe(topicName.str().c_str(), 1, boost::bind(&newImageTrigger_trgI, _1, i));
}

to subscribe to the topics /vrep/visionSensorData1 .. /vrep/visionSensorData6

P.S.: Oh, and look up how to write to a vector (either resize before the loop if you already know the final size, or use push_back() in the loop).

edit flag offensive delete link more

Comments

you are right! and furthermore: using push_back() i don't need the index, as even the boost::bind function anymore...isn't it?

mateo_7_7 gravatar image mateo_7_7  ( 2013-06-11 02:43:48 -0600 )edit

Sounds good. You need the boost::bind if you want to know which subscriber is currently calling the callback, in your case to save the image in the correct position of the Images[]-array, Can you post your final code as an edit to the questions, so others can see the final solution?

Philip gravatar image Philip  ( 2013-06-11 03:06:37 -0600 )edit

...but if i use the subscribers in order, and so i read the topics in order, automatically i'll have the images stored in the vector in order..am I right? ps. of course...i'm gonna do that, i have to fix some errors

mateo_7_7 gravatar image mateo_7_7  ( 2013-06-11 03:14:36 -0600 )edit
1

The subscribers aren't necessarily called in any order, at least in none that you should rely on. When the callback function is invoked (because a message was received) and some value has to be saved at a specific "position" in your vector, you have to provide that information to the callback.

Philip gravatar image Philip  ( 2013-06-11 03:26:33 -0600 )edit

Invalid arguments ' Candidates are: ros::Subscriber subscribe(const std::basic_string<char,std::char_traits<char>,std::allocator<char>> &, unsigned int, void (#1::*)(#0), #1 *, const ros::TransportHints &) ros::Subscriber subscribe(ros::SubscribeOptions &) ros::Subscriber subscribe(const std::basic_

mateo_7_7 gravatar image mateo_7_7  ( 2013-06-11 03:44:12 -0600 )edit

string<char,std::char_traits<char>,std::allocator<char>> &, unsigned int, void (#1::)(#0)const, #1 *, const ros::TransportHints &) ros::Subscriber subscribe(const std::basic_string<char,std::char_traits<char>,std::allocator<char>> &, unsigned int, void (#1::)(const boost::shared_ptr<const #0> &),#

mateo_7_7 gravatar image mateo_7_7  ( 2013-06-11 03:46:19 -0600 )edit

1 , const ros::TransportHints &)ros::Subscriber subscribe(const std::basic_string<char,std::char_traits<char>,std::allocator<char>> &, unsigned int, void (#1::)(const boost::shared_ptr<const #0> &)const, #1 *, const ros::TransportHints

mateo_7_7 gravatar image mateo_7_7  ( 2013-06-11 03:46:30 -0600 )edit

the last 3 posts are about the error i receive trying to compile the code you suggested me...??

mateo_7_7 gravatar image mateo_7_7  ( 2013-06-11 03:47:40 -0600 )edit

Question Tools

Stats

Asked: 2013-06-10 22:12:01 -0600

Seen: 1,821 times

Last updated: Aug 27 '14