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

pr2 camera subscriber [closed]

asked 2015-01-06 08:59:48 -0600

Nassim A. gravatar image

updated 2015-01-06 09:27:28 -0600

Hello everyone!

First I am using the groovy version of ROS under ubuntu 12.04.

I have some issues with the ImageTransporter Subscriber. I am trying to save pictures from different points of the environment using a pr2 robot.

I use a lookAt function described here to point to a coordinate in the environment and then subscribe to the "wide_stereo/left/image_mono" topic.

Here is the code I use for saving the picture:

void savePicture(const sensor_msgs::ImageConstPtr& msg){
  std::stringstream s;
      cv::imwrite(s.str().c_str(), cv_bridge::toCvShare(msg, "bgr8")->image);
  catch(std::runtime_error& ex)
      fprintf(stderr, "Exception converting image to PNG format: %s\n", ex.what());

And here is my "main" function:

int main(int argc, char** argv)
  //init the ROS node
  ros::init(argc, argv, "image_listener");
  ros::NodeHandle nh;

  RobotHead head(nh);
  image_transport::ImageTransport it(nh);

  float angle = -M_PI;

  numImage = 0; //Global variable to increment each time a picture is taken
      image_transport::Subscriber sub = it.subscribe("wide_stereo/left/image_mono", 1, savePicture);
      angle = angle + (M_PI/4.0);

The thing is that this code is correctly compiled but doesn't save any image. When I put the ros::spin() in the while loop it only saves the first point given to the lookAt() function so it seems that the saving works.

Any help or advice is welcome and thank you for reading.

edit retag flag offensive reopen merge delete

Closed for the following reason the question is answered, right answer was accepted by Nassim A.
close date 2015-01-14 05:01:16.631352

2 Answers

Sort by ยป oldest newest most voted

answered 2015-01-14 04:59:22 -0600

Nassim A. gravatar image

updated 2015-01-14 04:59:46 -0600

I have finally found out! I only had to chage the ros::spin() to a ros::spinOnce() and put it in the while loop with the subscriber outside the while loop. Thank you very much for your help.

edit flag offensive delete link more

answered 2015-01-06 10:17:01 -0600

Subscribers take a nonzero amount of time to become ready, and only need to be called once, so your call to image_transport::Subscriber sub = it.subscribe(...) needs to happen outside the while loop.

edit flag offensive delete link more


Thank you very much for your answer! When I did that it only saved the last picture. I thought it was due to an overwriting problem with savePicture() function. And when I put the numImage++ in the savePicture() function it worked but saved the whole panorama and I only need some points of it.

Nassim A. gravatar image Nassim A.  ( 2015-01-06 11:01:42 -0600 )edit

@Nassim A.: please don't use answers for posting comments. Use comments for that :).

gvdhoorn gravatar image gvdhoorn  ( 2015-01-06 11:37:33 -0600 )edit

Question Tools


Asked: 2015-01-06 08:59:48 -0600

Seen: 161 times

Last updated: Jan 14 '15