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

Dear Shay and other ROS users,

Thanks for your attention. I replaced my code with the one that you provide and tried to compile it, but I got some error during compiling which made me to comment and edit some lines. Now, the final code that has been compiled successfully is as follow:

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

void chatterCallback(const gazebo_msgs::ContactState::ConstPtr& cs) // this line has been edited by adding "::" between ContactState and ConstPtr&
{
  int a;
  a = sizeof(cs);
  std::stringstream ss;
  ss << a;
  ROS_INFO("The size of recieved ContactState message is: %d", a);

  // double z = cs->states[0].total_wrench.force.z; // this line has been commented due to this error : ‘const struct gazebo_msgs::ContactState_<std::allocator<void> >’ has no member named ‘states’
}

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::Publisher pub = n.advertise<std_msgs::String>("mymsg",1000); //here is for you question 2).
  std_msgs::String msg;
  msg.data = "Hello"; // this line has been edited due to this error: conversion from ‘const char [6]’ to non-scalar type ‘std_msgs::String’ requested

  ros::Rate loop_rate(10);

  while (ros::ok())
  {
      pub.publish(msg);

      ros::spinOnce();
      loop_rate.sleep();
  }
  return 0;
}

After executing this new code using rosrun, I realized that my main problem remains unsolved. I mean ROS don't call chatterCallback function as new messages published by "contact_sensor_state" :(

Does anybody knows what is the problem here? Isn't it better to specify type of topic in addition of it's name at ros::Subscriber sub = n.subscribe("contact_sensor_state", 1000, chatterCallback);. How should I edit this line to specify type of topic??

Hope for your help