0
$\begingroup$

Rosanswers logo

Hello!,

I'm working with arduino and ros and i'm reading a value of a sensor and, that value is publish in a topic, i'm trying to create a Subscriber who heard the message that the topic is sending, actully i wrote a .cpp file:

#include "ros/ros.h"
#include "std_msgs/String.h"

void chatterCallback(const std_msgs::String::ConstPtr& msg)
{
  ROS_INFO("I heard: [%s]", msg->data.c_str());
}

int main(int argc, char **argv)
{
  ros::init(argc, argv, "listener");
  ros::NodeHandle n;
  ros::Subscriber sub = n.subscribe("chatter", 500, chatterCallback);
  ros::spin();

  return 0;
}

but the problem is that the value that i'm reading from the topic of arduino is a float32 value and my listener reads only string values, i try to change the code but when i do make it gives me an error.... If i change the value that the topic gives to me to an string value the listener works ok.

someone could help me???


Originally posted by joseescobar60 on ROS Answers with karma: 172 on 2012-11-13

Post score: 0


Original comments

Comment by Lorenz on 2012-11-13:
Please make sure you read http://ros.org/wiki/Support If you are getting errors, please put the corresponding code and complete error output in your question.

$\endgroup$

1 Answer 1

0
$\begingroup$

Rosanswers logo

Try:

#include <ros/ros.h>
#include <std_msgs/Float32.h>

void chatterCallback(const std_msgs::Float32::ConstPtr& msg)
{
  ROS_INFO("I heard: [%f]", msg->data);
}

int main(int argc, char **argv)
{
  ros::init(argc, argv, "listener");
  ros::NodeHandle n;
  ros::Subscriber sub = n.subscribe("chatter", 500, chatterCallback);
  ros::spin();

  return 0;
}

Originally posted by Lorenz with karma: 22731 on 2012-11-13

This answer was ACCEPTED on the original site

Post score: 1


Original comments

Comment by joseescobar60 on 2012-11-13:
thank you!, i already change the String for Float32 but it don't works, but the ROS_INFO("I heard: [%f]", msg->data); and now i changed it and it works, thank you

$\endgroup$

Your Answer

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