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

A very simple example is shown below.

  1. Defined a class call TestRos, which has a subscriber and a publisher.
  2. In the main function, declared a variable test_ros with the type of TestRos.
  3. Using ros::spin() to handle callback function.
  4. In the TestRos class, we defined a callback function called imageCallback, where we receive input message and do some processing and then publish the output message.

#include <ros/ros.h>
#include <geometry_msgs/PoseStamped.h>
class TestRos
{
  ros::NodeHandle n;
  ros::Subscriber positions_sub; 
  ros::Publisher positions_pub;

  geometry_msgs::PoseStamped output_msg;

  void imageCallback(const geometry_msgs::PoseStamped::ConstPtr& ps4){
    output_msg.pose.position.x = 10 * ps4->pose.position.x;
    output_msg.pose.position.y = 10 * ps4->pose.position.y;
    output_msg.pose.position.z = 10 * ps4->pose.position.z;
    positions_pub.publish(output_msg);  
  } 

public:
  TestRos(){
    positions_sub = n.subscribe<geometry_msgs::PoseStamped>("/visp_auto_tracker/object_position", 1000, &TestRos::imageCallback, this);
    positions_pub = n.advertise<geometry_msgs::PoseStamped>("positions", 1000);  
  }

  ~TestRos(){
  }

};


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

  ros::init(argc, argv, "coordinator");
  TestRos test_ros;  

  ros::spin();
  ros::shutdown();
  return 0;
}

A very simple example is shown below.

  1. Defined a class call TestRos, which has a subscriber and a publisher.
  2. In the main function, declared a variable test_ros with the type of TestRos.
  3. Using ros::spin() to handle callback function.
  4. In the TestRos class, we defined a callback function called imageCallback, where we receive input message and do some processing and then publish the output message.

I removed unnecessary header files in your original code to make it compilable. You can add them back to integrate with opencv libraries. In your original code example, the message queue size is 1000. However, if you want to use the latest message, you may change it to 1.

Please correct me if I am wrong. Thanks.


#include <ros/ros.h>
#include <geometry_msgs/PoseStamped.h>
class TestRos
{
  ros::NodeHandle n;
  ros::Subscriber positions_sub; 
  ros::Publisher positions_pub;

  geometry_msgs::PoseStamped output_msg;

  void imageCallback(const geometry_msgs::PoseStamped::ConstPtr& ps4){
    output_msg.pose.position.x = 10 * ps4->pose.position.x;
    output_msg.pose.position.y = 10 * ps4->pose.position.y;
    output_msg.pose.position.z = 10 * ps4->pose.position.z;
    positions_pub.publish(output_msg);  
  } 

public:
  TestRos(){
    positions_sub = n.subscribe<geometry_msgs::PoseStamped>("/visp_auto_tracker/object_position", 1000, &TestRos::imageCallback, this);
    positions_pub = n.advertise<geometry_msgs::PoseStamped>("positions", 1000);  
  }

  ~TestRos(){
  }

};


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

  ros::init(argc, argv, "coordinator");
  TestRos test_ros;  

  ros::spin();
  ros::shutdown();
  return 0;
}

A simple example is shown below.

  1. Defined a class call TestRos, which has a subscriber and a publisher.
  2. In the main function, declared a variable test_ros with the type of TestRos.
  3. Using ros::spin() to handle callback function.
  4. In the TestRos class, we defined a callback function called imageCallback, where we receive input message and do some processing and then publish the output message.

I removed unnecessary header files in your original code to make it compilable. You can add them back to integrate with opencv libraries. In your original code example, the message queue size is 1000. However, if you want to use the latest message, you may change it to 1.

Please correct me if I am wrong. Thanks.


#include <ros/ros.h>
#include <geometry_msgs/PoseStamped.h>
class TestRos
{
  ros::NodeHandle n;
  ros::Subscriber positions_sub; 
  ros::Publisher positions_pub;

  geometry_msgs::PoseStamped output_msg;

  void imageCallback(const geometry_msgs::PoseStamped::ConstPtr& ps4){
    output_msg.pose.position.x = 10 * ps4->pose.position.x;
    output_msg.pose.position.y = 10 * ps4->pose.position.y;
    output_msg.pose.position.z = 10 * ps4->pose.position.z;
    positions_pub.publish(output_msg);  
  } 

public:
  TestRos(){
    positions_sub = n.subscribe<geometry_msgs::PoseStamped>("/visp_auto_tracker/object_position", 1000, &TestRos::imageCallback, this);
    positions_pub = n.advertise<geometry_msgs::PoseStamped>("positions", 1000);  
  }

  ~TestRos(){
  }

};


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

  ros::init(argc, argv, "coordinator");
  TestRos test_ros;  

  ros::spin();
  ros::shutdown();
  return 0;
}