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

asked 2016-09-24 07:15:07 -0600

Masoud gravatar image

updated 2016-09-25 05:38:16 -0600

Hi everybody

I'm trying to write a subscriber following this tutorial to listen to a topic which is published by a contact sensor ( 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);

  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? 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.


edit retag flag offensive reopen merge delete

Closed for the following reason the question is answered, right answer was accepted by Masoud
close date 2016-09-25 05:38:52.975688


Masoud gravatar imageMasoud ( 2016-09-25 05:40:06 -0600 )edit