Gazebo | Ignition | Community
Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

How to subscribe to a topic whom type is gazebo_msg/ContactState?

Hi everybody

I'm trying to write a subscribe following this tutorial to listen to a topic which is published by a contact sensor (libgazebo_ros_bumper.so). Here is the structure of this topic shown in "topic monitor":

image description

Assuming that we want to write the size of ContactState message published by this topic, I wrote this program and compiled it (using catkin_make command) successfully:

#include "ros/ros.h"
#include "std_msgs/String.h"
#include "gazebo_msgs/ContactState.h"
#include <sstream>

void chatterCallback(const gazebo_msgs::ContactState cs)
{
  int a;
  a = sizeof(cs);
  std::stringstream ss;
  ss << a;
  ROS_INFO("The size of recieved ContactState message is: %d", a);
}

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

  ros::init(argc, argv, "listener");
  ros::NodeHandle n;
  ros::Subscriber sub = n.subscribe("contact_sensor_state", 1000, chatterCallback);
  ros::spin();

  return 0;
}

However, Nothing happen whenever I run this subscriber node using rosrun command. It seems that ROS never call chatterCallback function. How should I edit ros::Subscriber sub = n.subscribe("contact_sensor_state", 1000, chatterCallback); to fix the issue?

I also have 3 more simple question regarding the above question: 1) How can I store the valued published by "contact_sensor_state/states/[0]/total_wrench/force/z" in a double variable in my program?

2) Is it possible to advertise a variable in my program using something similar? I mean is it possible for a node to be both a subscriber and publisher. If yes, how should I edit the code to do this?

I'm using ROS indigo, gazebo 2.2 and ubuntu 14.04.

Thanks

How to subscribe to a topic whom type is gazebo_msg/ContactState?

Hi everybody

I'm trying to write a subscribe following this tutorial to listen to a topic which is published by a contact sensor (libgazebo_ros_bumper.so). Here is the structure of this topic shown in "topic monitor":

image description

Assuming that we want to write the size of ContactState message published by this topic, I wrote this program and compiled it (using catkin_make command) successfully:

#include "ros/ros.h"
#include "std_msgs/String.h"
#include "gazebo_msgs/ContactState.h"
#include <sstream>

void chatterCallback(const gazebo_msgs::ContactState cs)
{
  int a;
  a = sizeof(cs);
  std::stringstream ss;
  ss << a;
  ROS_INFO("The size of recieved ContactState message is: %d", a);
}

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

  ros::init(argc, argv, "listener");
  ros::NodeHandle n;
  ros::Subscriber sub = n.subscribe("contact_sensor_state", 1000, chatterCallback);
  ros::spin();

  return 0;
}

However, Nothing happen whenever I run this subscriber node using rosrun command. It seems that ROS never call chatterCallback function. How should I edit ros::Subscriber sub = n.subscribe("contact_sensor_state", 1000, chatterCallback); to fix the issue?

I also have 3 2 more simple question regarding the above question: 1) How can I store the valued published by "contact_sensor_state/states/[0]/total_wrench/force/z" in a double variable in my program?

2) Is it possible to advertise a variable in my program using something similar? I mean is it possible for a node to be both a subscriber and publisher. If yes, how should I edit the code to do this?

I'm using ROS indigo, gazebo 2.2 and ubuntu 14.04.

Thanks

How to subscribe to a topic whom type is gazebo_msg/ContactState?

Hi everybody

I'm trying to write a subscribe following this tutorial to listen to a topic which is published by a contact sensor (libgazebo_ros_bumper.so). Here is the structure of this topic shown in "topic monitor":

image description

Assuming that we want to write the size of ContactState message published by this topic, I wrote this program and compiled it (using catkin_make command) successfully:

#include "ros/ros.h"
#include "std_msgs/String.h"
#include "gazebo_msgs/ContactState.h"
#include <sstream>

void chatterCallback(const gazebo_msgs::ContactState cs)
{
  int a;
  a = sizeof(cs);
  std::stringstream ss;
  ss << a;
  ROS_INFO("The size of recieved ContactState message is: %d", a);
}

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

  ros::init(argc, argv, "listener");
  ros::NodeHandle n;
  ros::Subscriber sub = n.subscribe("contact_sensor_state", 1000, chatterCallback);
  ros::spin();

  return 0;
}

However, Nothing happen whenever I run this subscriber node using rosrun command. It seems that ROS never call chatterCallback function. How should I edit ros::Subscriber sub = n.subscribe("contact_sensor_state", 1000, chatterCallback); to fix the issue?

I also have 2 more simple question regarding the above question: question:

1) How can I store the valued published by "contact_sensor_state/states/[0]/total_wrench/force/z" in a double variable in my program?

2) Is it possible to advertise a variable in my program using something similar? I mean is it possible for a node to be both a subscriber and publisher. If yes, how should I edit the code to do this?

I'm using ROS indigo, gazebo 2.2 and ubuntu 14.04.

Thanks

How to subscribe to a topic whom type is gazebo_msg/ContactState?

Hi everybody

I'm trying to write a subscribe subscriber following this tutorial to listen to a topic which is published by a contact sensor (libgazebo_ros_bumper.so). Here is the structure of this topic shown in "topic monitor":

image description

Assuming that we want to write the size of ContactState message published by this topic, I wrote this program and compiled it (using catkin_make command) successfully:

#include "ros/ros.h"
#include "std_msgs/String.h"
#include "gazebo_msgs/ContactState.h"
#include <sstream>

void chatterCallback(const gazebo_msgs::ContactState cs)
{
  int a;
  a = sizeof(cs);
  std::stringstream ss;
  ss << a;
  ROS_INFO("The size of recieved ContactState message is: %d", a);
}

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

  ros::init(argc, argv, "listener");
  ros::NodeHandle n;
  ros::Subscriber sub = n.subscribe("contact_sensor_state", 1000, chatterCallback);
  ros::spin();

  return 0;
}

However, Nothing happen whenever I run this subscriber node using rosrun command. It seems that ROS never call chatterCallback function. How should I edit ros::Subscriber sub = n.subscribe("contact_sensor_state", 1000, chatterCallback); to fix the issue?

I also have 2 more simple question regarding the above question:

1) How can I store the valued published by "contact_sensor_state/states/[0]/total_wrench/force/z" in a double variable in my program?

2) Is it possible to advertise a variable in my program using something similar? program? I mean is it possible for a node to be both a subscriber and publisher. If yes, how should I edit the code to do this?

I'm using ROS indigo, gazebo 2.2 and ubuntu 14.04.

Thanks