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

The message variable should be a reference variable.

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

void chatterCallback(const gazebo_msgs::ContactStateConstPtr& 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;
}

The message variable should be a reference variable.

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

void chatterCallback(const gazebo_msgs::ContactStateConstPtr& cs)
{
  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; //here is for you question 1).
}

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();
ros::Publisher pub = n.advertise<std_msgs::String>("mymsg",1000); //here is for you question 2).
  std_msgs::String msg = "Hello"; //here is for you question 2).
   ros::Rate loop_rate(10);

  while (ros::ok())
  {
      pub.publish(msg); //here is for you question 2).

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

The message variable should be a reference variable.variable. You can refer to the code below.

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

void chatterCallback(const gazebo_msgs::ContactStateConstPtr& cs)
{
  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; //here is for you question 1).
}

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 = "Hello"; //here is for you question 2).

  ros::Rate loop_rate(10);

  while (ros::ok())
  {
      pub.publish(msg); //here is for you question 2).

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