Ask Your Question
0

Unable to print subscribed sensor_msgs with ROS_INFO

asked 2018-02-08 06:44:20 -0500

eirikaso gravatar image

updated 2018-02-08 06:54:06 -0500

Hello and thanks for reading my post. I am trying to subscribe to a topic from an imu using the sensor_msgs::MagneticField. This topic is published by a 3rd party package called mavros. I am trying to edit the subscriber script from the tutorial, but have some issues printing the data with the ROS_INFO command. I am not sure if I am subsribing to the topic incorrectly, or if it's only an issue with my usage of ROS_INFO. I am trying to print the x variable of the message but the output message looks like this:

[ INFO] [1518093660.171431954]: I heard: [H?H0?`? ]

The documentation for the MagneticField message is here

My code looks like this

#include "ros/ros.h"
#include "sensor_msgs/MagneticField.h"


void chatterCallback(const sensor_msgs::MagneticField::ConstPtr& mag)
{
  ROS_INFO("I heard: [%s]", mag->magnetic_field.x);
}

int main(int argc, char **argv)
{
  ros::init(argc, argv, "listener");

  ros::NodeHandle n;


  ros::Subscriber sub = n.subscribe<sensor_msgs::MagneticField>("/mavros/imu/mag", 1000, chatterCallback);


  ros::spin();

  return 0;
}
edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
0

answered 2018-02-08 07:02:38 -0500

eirikaso gravatar image

updated 2018-02-08 07:02:58 -0500

I found the error. I had to change the [%s] with [%f] for float. Silly mistake

edit flag offensive delete link more

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools

1 follower

Stats

Asked: 2018-02-08 06:44:20 -0500

Seen: 192 times

Last updated: Feb 08 '18