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

subscribing a node to a topic problem

asked 2012-11-13 02:25:29 -0500

joseescobar60 gravatar image

updated 2012-11-15 04:46:53 -0500

mjcarroll gravatar image

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???

edit retag flag offensive close merge delete

Comments

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.

Lorenz gravatar image Lorenz  ( 2012-11-13 03:29:41 -0500 )edit

1 Answer

Sort by ยป oldest newest most voted
1

answered 2012-11-13 03:30:46 -0500

Lorenz gravatar image

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;
}
edit flag offensive delete link more

Comments

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

joseescobar60 gravatar image joseescobar60  ( 2012-11-13 03:34:13 -0500 )edit

Question Tools

1 follower

Stats

Asked: 2012-11-13 02:25:29 -0500

Seen: 941 times

Last updated: Nov 13 '12