0
$\begingroup$

Rosanswers logo

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


Originally posted by mateo_7_7 on ROS Answers with karma: 90 on 2013-06-10

Post score: 0

$\endgroup$

2 Answers 2

0
$\begingroup$

Rosanswers logo

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.


Originally posted by t.pimentel with karma: 385 on 2013-06-18

This answer was ACCEPTED on the original site

Post score: 2


Original comments

Comment by eliasm on 2014-10-07:
This solved the problem for me as well. Thanks!

$\endgroup$
0
$\begingroup$

Rosanswers logo

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).


Originally posted by Philip with karma: 990 on 2013-06-10

This answer was NOT ACCEPTED on the original site

Post score: 1


Original comments

Comment by mateo_7_7 on 2013-06-11:
you are right! and furthermore: using push_back() i don't need the index, as even the boost::bind function anymore...isn't it?

Comment by Philip on 2013-06-11:
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?

Comment by mateo_7_7 on 2013-06-11:
...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

Comment by Philip on 2013-06-11:
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.

Comment by mateo_7_7 on 2013-06-11:
Invalid arguments ' Candidates are: ros::Subscriber subscribe(const std::basic_string<char,std::char_traits,std::allocator> &, unsigned int, void (#1::*)(#0), #1 *, const ros::TransportHints &) ros::Subscriber subscribe(ros::SubscribeOptions &) ros::Subscriber subscribe(const std::basic_

Comment by mateo_7_7 on 2013-06-11:
string<char,std::char_traits,std::allocator> &, unsigned int, void (#1::*)(#0)const, #1 , const ros::TransportHints &) ros::Subscriber subscribe(const std::basic_string<char,std::char_traits,std::allocator> &, unsigned int, void (#1::)(const boost::shared_ptr<const #0> &),#

Comment by mateo_7_7 on 2013-06-11:
1 , const ros::TransportHints &)ros::Subscriber subscribe(const std::basic_string<char,std::char_traits,std::allocator> &, unsigned int, void (#1::)(const boost::shared_ptr<const #0> &)const, #1 *, const ros::TransportHints

Comment by mateo_7_7 on 2013-06-11:
the last 3 posts are about the error i receive trying to compile the code you suggested me...??

Comment by mateo_7_7 on 2013-06-11:
the method to create the string is definitely right...it works for others topics.

$\endgroup$

Your Answer

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