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

According to the documentation (as suggested by @gvdhoorn) the code should look something like this:

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

    ros::init(argc, argv, "arm_to_fpga");
    ros::NodeHandle n_img, n_laser, n_twist;

    ros::Subscriber sub_img = n_img.subscribe("/FPGA_ROS_Scheduler/pc_pub_img", 1000, pc_pub_img_callback);
    ros::Subscriber sub_laser = n_laser.subscribe("/FPGA_ROS_Scheduler/pc_pub_laser", 1000, pc_pub_laser_callback);
    ros::Subscriber sub_twist = n_twist.subscribe("/FPGA_ROS_Scheduler/pc_pub_twist", 1000, pc_pub_twist_callback);

    ros::CallbackQueue Queue_img, Queue_laser, Queue_twist;

    n_img.setCallbackQueue(&Queue_img);
    n_laser.setCallbackQueue(&Queue_laser);
    n_twist.setCallbackQueue(&Queue_twist);

    bool s_img=0, s_laser=0, s_twist=0;

    while(ros::ok())
    {

      if(!Queue_img.isEmpty())
      {
        s_img = true;
        ROS_INFO("img %d - laser %d - twist %d\n", s_img, s_laser, s_twist);
        Queue_img.callAvailable();
      }
      else
        s_img = false;

      if(!Queue_laser.isEmpty())
      {
        s_laser = true;
        ROS_INFO("img %d - laser %d - twist %d\n", s_img, s_laser, s_twist);
        Queue_laser.callAvailable();
      }
      else
        s_laser = false;

      if(!Queue_twist.isEmpty())
      {
        s_twist = true;
        ROS_INFO("img %d - laser %d - twist %d\n", s_img, s_laser, s_twist);
        Queue_twist.callAvailable();
      }
      else
        s_twist = false;

      ros::spinOnce();
    }

    return 0;
}

However, the callbacks are invoked even if I comment the lines with the callAvailable() method.

According to the documentation (as suggested by @gvdhoorn) the code should look something like this:

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

    ros::init(argc, argv, "arm_to_fpga");
    ros::NodeHandle n_img, n_laser, n_twist;

    ros::Subscriber sub_img = n_img.subscribe("/FPGA_ROS_Scheduler/pc_pub_img", 1000, pc_pub_img_callback);
    ros::Subscriber sub_laser = n_laser.subscribe("/FPGA_ROS_Scheduler/pc_pub_laser", 1000, pc_pub_laser_callback);
    ros::Subscriber sub_twist = n_twist.subscribe("/FPGA_ROS_Scheduler/pc_pub_twist", 1000, pc_pub_twist_callback);

    ros::CallbackQueue Queue_img, Queue_laser, Queue_twist;

    n_img.setCallbackQueue(&Queue_img);
    n_laser.setCallbackQueue(&Queue_laser);
    n_twist.setCallbackQueue(&Queue_twist);

    bool s_img=0, s_laser=0, s_twist=0;

    while(ros::ok())
    {

      if(!Queue_img.isEmpty())
      {
        s_img = true;
        ROS_INFO("img %d - laser %d - twist %d\n", s_img, s_laser, s_twist);
        Queue_img.callAvailable();
Queue_img.callOne();
      }
      else
        s_img = false;

      if(!Queue_laser.isEmpty())
      {
        s_laser = true;
        ROS_INFO("img %d - laser %d - twist %d\n", s_img, s_laser, s_twist);
        Queue_laser.callAvailable();
Queue_laser.callOne();
      }
      else
        s_laser = false;

      if(!Queue_twist.isEmpty())
      {
        s_twist = true;
        ROS_INFO("img %d - laser %d - twist %d\n", s_img, s_laser, s_twist);
        Queue_twist.callAvailable();
Queue_twist.callOne();
      }
      else
        s_twist = false;

      ros::spinOnce();
    }

    return 0;
}

However, the callbacks are invoked even if I comment the lines with the callAvailable() method. method.

According to the documentation (as suggested by @gvdhoorn) the code should look something like this:

#include "ros/ros.h"
#include "ros/callback_queue.h"
#include "sensor_msgs/Image.h"
#include "sensor_msgs/LaserScan.h"
#include "geometry_msgs/TwistStamped.h"

// Callbacks
void pc_pub_img_callback(const sensor_msgs::Image::ConstPtr& msg){
      ROS_INFO("Img:Callback");
}

void pc_pub_laser_callback(const sensor_msgs::LaserScan::ConstPtr& msg){
      ROS_INFO("Laser:Callback");
}

void pc_pub_twist_callback(const geometry_msgs::TwistStamped::ConstPtr& msg){
      ROS_INFO("Twist:Callback");
}

int main(int argc, char **argv)
argn, char* args[])
{

    ros::init(argc, argv,   ros::init(argn, args, "arm_to_fpga");
   ros::NodeHandle n_img, n_laser, n_img;
  ros::NodeHandle n_laser;
  ros::NodeHandle n_twist;

  ros::CallbackQueue Queue_img;
  ros::CallbackQueue Queue_laser;
  ros::CallbackQueue Queue_twist;

  n_img.setCallbackQueue(&Queue_img);
  n_laser.setCallbackQueue(&Queue_laser);
  n_twist.setCallbackQueue(&Queue_twist);

  ros::Subscriber sub_img s_img = n_img.subscribe("/FPGA_ROS_Scheduler/pc_pub_img", 1000, 1, pc_pub_img_callback);
   ros::Subscriber sub_laser s_laser = n_laser.subscribe("/FPGA_ROS_Scheduler/pc_pub_laser", 1000, 1, pc_pub_laser_callback);
   ros::Subscriber sub_twist s_twist = n_twist.subscribe("/FPGA_ROS_Scheduler/pc_pub_twist", 1000, 1, pc_pub_twist_callback);

    ros::CallbackQueue Queue_img, Queue_laser, Queue_twist;

    n_img.setCallbackQueue(&Queue_img);
    n_laser.setCallbackQueue(&Queue_laser);
    n_twist.setCallbackQueue(&Queue_twist);

    bool s_img=0, s_laser=0, s_twist=0;

    while(ros::ok())
   {

      if(!Queue_img.isEmpty())
      if(!Queue_img.isEmpty() && !Queue_laser.isEmpty() && !Queue_twist.isEmpty())
    {
        s_img = true;
        ROS_INFO("img %d - laser %d - twist %d\n", s_img, s_laser, s_twist);
        Queue_img.callOne();
      }
      else
        s_img = false;

      if(!Queue_laser.isEmpty())
      {
        s_laser = true;
        ROS_INFO("img %d - laser %d - twist %d\n", s_img, s_laser, s_twist);
        Queue_laser.callOne();
      }
      else
        s_laser = false;

      if(!Queue_twist.isEmpty())
      {
        s_twist = true;
        ROS_INFO("img %d - laser %d - twist %d\n", s_img, s_laser, s_twist);
        Queue_twist.callOne();
      }
      else
        s_twist = false;

      ros::spinOnce();
    }

  Queue_img.callOne(ros::WallDuration(1.0));
      Queue_laser.callOne(ros::WallDuration(1.0));
      Queue_twist.callOne(ros::WallDuration(1.0));
    }
  }

  ROS_INFO("Hurray!");

  return 0;
}

However, the callbacks are invoked even if I comment the lines with the callAvailable() method.EDIT: Here is the working example. It waits until all 3 queues have at least one message to invoke the callbacks.

According to the documentation (as suggested by @gvdhoorn) the code should look something like this:

#include "ros/ros.h"
#include "ros/callback_queue.h"
#include "sensor_msgs/Image.h"
#include "sensor_msgs/LaserScan.h"
#include "geometry_msgs/TwistStamped.h"

// Callbacks
void pc_pub_img_callback(const sensor_msgs::Image::ConstPtr& msg){
      ROS_INFO("Img:Callback");
}

void pc_pub_laser_callback(const sensor_msgs::LaserScan::ConstPtr& msg){
      ROS_INFO("Laser:Callback");
}

void pc_pub_twist_callback(const geometry_msgs::TwistStamped::ConstPtr& msg){
      ROS_INFO("Twist:Callback");
}

int main(int argn, char* args[])
{
  ros::init(argn, args, "arm_to_fpga");
  ros::NodeHandle n_img;
  ros::NodeHandle n_laser;
  ros::NodeHandle n_twist;

  ros::CallbackQueue Queue_img;
  ros::CallbackQueue Queue_laser;
  ros::CallbackQueue Queue_twist;

  n_img.setCallbackQueue(&Queue_img);
  n_laser.setCallbackQueue(&Queue_laser);
  n_twist.setCallbackQueue(&Queue_twist);

  ros::Subscriber s_img = n_img.subscribe("/FPGA_ROS_Scheduler/pc_pub_img", 1, pc_pub_img_callback);
  ros::Subscriber s_laser = n_laser.subscribe("/FPGA_ROS_Scheduler/pc_pub_laser", 1, pc_pub_laser_callback);
  ros::Subscriber s_twist = n_twist.subscribe("/FPGA_ROS_Scheduler/pc_pub_twist", 1, pc_pub_twist_callback);

  while(ros::ok())
  {
    if(!Queue_img.isEmpty() && !Queue_laser.isEmpty() && !Queue_twist.isEmpty())
    {
      Queue_img.callOne(ros::WallDuration(1.0));
      Queue_laser.callOne(ros::WallDuration(1.0));
      Queue_twist.callOne(ros::WallDuration(1.0));
    }
  }

  ROS_INFO("Hurray!");
  ros::spinOnce();
  }    
  return 0;
}

EDIT: Here is the working example. It waits until all 3 queues have at least one message to invoke the callbacks.