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

Revision history [back]

click to hide/show revision 1
initial version

Ultimately, we implemented an external trigger (arduino based, since it was convenient) for the cameras. Though they spin continuously, the images are always within ~2ms of each other.

Simplified:

Camera Service:

class ImageGrab
{
public:
ImageGrab()
: it_(nh_)
{
  grab_service_ = nh_.advertiseService("/hast/Image_Grab", &ImageGrab::grab, this); 
  image_subr_ = it_.subscribe("/right/camera/image_rect_color", 1, &ImageGrab::imageCbr, this);
  image_subl_ = it_.subscribe("/left/camera/image_rect_color", 1, &ImageGrab::imageCbl, this);
}//close constructor

void imageCbr(const sensor_msgs::ImageConstPtr& msg)
{
  cv_bridge::CvImagePtr cv_ptr;
  try
  {cv_ptr = cv_bridge::toCvCopy(msg, enc::BGR8);}
  catch (cv_bridge::Exception& e)
  {ROS_ERROR("cv_bridge exception: %s", e.what());return;}
  right_image=cv_ptr->image;
} // end void CBr

void imageCbl(const sensor_msgs::ImageConstPtr& msg)
{
  cv_bridge::CvImagePtr cv_ptr;
  try
  {cv_ptr = cv_bridge::toCvCopy(msg, enc::BGR8);}//end try
  catch (cv_bridge::Exception& e)
  {ROS_ERROR("cv_bridge exception: %s", e.what());return;}//end catch
  left_image=cv_ptr->image;
} // end void Cbl

bool grab(hast::targets::Request &req, hast::targets::Response &res)
{//begin grab
  res.rX = rX;
  res.rY = rY;
  res.lX = lX;
  res.lY = lY;
  res.flag = flag;
  return true;
}
}; //end ImageGrab class

int main(int argc, char **argv) 
{
  ros::init(argc, argv, "observe_srv");
  ImageGrab ig;
  ROS_INFO("Watching..");
  ros::spin();
  return 0;
}

Client:

#include "hast/targets.h"
hast::targets srv;
int main(int argc, char **argv)
{
  ros::init(argc, argv, "hast_cli");
  ros::NodeHandle n;
  ros::ServiceClient client = n.serviceClient<hast::targets>("/hast/Image_Grab");

  do
  {
    if (client.call(srv)) // Successful call() returns true, failed returns false
    {
    read_grab();
    } 
    else //Failed client call
    {
    ROS_INFO("Failed to call service 'Grab Images'");
    }//end else

    // -----Run Experiment here------

  } //end do block
  while (ros::ok());
  return 0;
} //end main

Ultimately, we implemented an external trigger (arduino based, since it was convenient) for the cameras. Though they spin continuously, the images are always within ~2ms of each other.other. I did take the advice to save it as a member variable (private).

Simplified:

Camera Service:

class ImageGrab
{
private:
ros::NodeHandle nh_;
image_transport::ImageTransport it_;
image_transport::Subscriber image_subr_;
image_transport::Subscriber image_subl_;
ros::ServiceServer grab_service_;

cv::Mat left_image, right_image;
bool flag;

public:
ImageGrab()
: it_(nh_)
{
  grab_service_ = nh_.advertiseService("/hast/Image_Grab", &ImageGrab::grab, this); 
  image_subr_ = it_.subscribe("/right/camera/image_rect_color", 1, &ImageGrab::imageCbr, this);
  image_subl_ = it_.subscribe("/left/camera/image_rect_color", 1, &ImageGrab::imageCbl, this);
}//close constructor

void imageCbr(const sensor_msgs::ImageConstPtr& msg)
{
  cv_bridge::CvImagePtr cv_ptr;
  try
  {cv_ptr = cv_bridge::toCvCopy(msg, enc::BGR8);}
  catch (cv_bridge::Exception& e)
  {ROS_ERROR("cv_bridge exception: %s", e.what());return;}
  right_image=cv_ptr->image;
} // end void CBr

void imageCbl(const sensor_msgs::ImageConstPtr& msg)
{
  cv_bridge::CvImagePtr cv_ptr;
  try
  {cv_ptr = cv_bridge::toCvCopy(msg, enc::BGR8);}//end try
  catch (cv_bridge::Exception& e)
  {ROS_ERROR("cv_bridge exception: %s", e.what());return;}//end catch
  left_image=cv_ptr->image;
} // end void Cbl

bool grab(hast::targets::Request &req, hast::targets::Response &res)
{//begin grab
  res.rX = rX;
  res.rY = rY;
  res.lX = lX;
  res.lY = lY;
  res.flag = flag;
  return true;
}
}; //end ImageGrab class

int main(int argc, char **argv) 
{
  ros::init(argc, argv, "observe_srv");
  ImageGrab ig;
  ROS_INFO("Watching..");
  ros::spin();
  return 0;
}

Client:

#include "hast/targets.h"
hast::targets srv;
int main(int argc, char **argv)
{
  ros::init(argc, argv, "hast_cli");
  ros::NodeHandle n;
  ros::ServiceClient client = n.serviceClient<hast::targets>("/hast/Image_Grab");

  do
  {
    if (client.call(srv)) // Successful call() returns true, failed returns false
    {
    read_grab();
    } 
    else //Failed client call
    {
    ROS_INFO("Failed to call service 'Grab Images'");
    }//end else

    // -----Run Experiment here------

  } //end do block
  while (ros::ok());
  return 0;
} //end main