ROS Resources: Documentation | Support | Discussion Forum | Index | Service Status | ros @ Robotics Stack Exchange
Ask Your Question

How do I publish ROS messages to ROS_INFO?

asked 2017-05-04 14:43:55 -0500

corcorant gravatar image

updated 2017-05-04 15:23:59 -0500

I currently have a node that is producing messages of the type 'sensor_msgs' which I would like to see in my /rosout log. My approach was to add a statement similar to the following:


This produces type conversion errors that I might be able to work around but I feel like there would be an easier solution.

Edit: Here is the full function, with the ROS_INFO statement at the bottom:

void publishData(const Imu::IMUData &data) {
  sensor_msgs::Imu imu;
  sensor_msgs::MagneticField field;
  sensor_msgs::FluidPressure pressure;

  //  assume we have all of these since they were requested
  /// @todo: Replace this with a mode graceful failure...
  assert(data.fields & Imu::IMUData::Accelerometer);
  assert(data.fields & Imu::IMUData::Magnetometer);
  assert(data.fields & Imu::IMUData::Barometer);
  assert(data.fields & Imu::IMUData::Gyroscope);

  //  timestamp identically
  imu.header.stamp = ros::Time::now();
  imu.header.frame_id = frameId;
  field.header.stamp = imu.header.stamp;
  field.header.frame_id = frameId;
  pressure.header.stamp = imu.header.stamp;
  pressure.header.frame_id = frameId;

  imu.orientation_covariance[0] =
      -1; //  orientation data is on a separate topic

  imu.linear_acceleration.x = data.accel[0] * kEarthGravity;
  imu.linear_acceleration.y = data.accel[1] * kEarthGravity;
  imu.linear_acceleration.z = data.accel[2] * kEarthGravity;
  imu.angular_velocity.x = data.gyro[0];
  imu.angular_velocity.y = data.gyro[1];
  imu.angular_velocity.z = data.gyro[2];

  field.magnetic_field.x = data.mag[0];
  field.magnetic_field.y = data.mag[1];
  field.magnetic_field.z = data.mag[2];

  pressure.fluid_pressure = data.pressure;

  //  publish
  ROS_INFO(imu);                                 <-------I want to publish imu data 
  if (imuDiag) {

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted

answered 2017-05-05 01:45:17 -0500

ROS_INFO is used like printf, so you can't just pass a message as a parameter. It should be some kind of string. I would recommend creating the message you need like this:

ROS_INFO("stamp: %d", imu.header.stamp);
ROS_INFO("linear acceleration x: %f", imu.linear_acceleration.x);

I have not compiled/tested the code, but I think it should work. (You might need to cast some values)

Good luck.

edit flag offensive delete link more

Question Tools

1 follower


Asked: 2017-05-04 14:43:55 -0500

Seen: 16,716 times

Last updated: May 05 '17