0
$\begingroup$

Rosanswers logo

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;
}

Originally posted by eirikaso on ROS Answers with karma: 108 on 2018-02-08

Post score: 0

$\endgroup$

1 Answer 1

0
$\begingroup$

Rosanswers logo

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


Originally posted by eirikaso with karma: 108 on 2018-02-08

This answer was ACCEPTED on the original site

Post score: 0

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.