What to put ROS_INFO parameter for float data type in a subscriber? [closed]

asked 2018-08-31 02:29:10 -0600

J. J. gravatar image

Hi I'm trying to make a simple subscriber class. However I'm having an error :

error: ‘Float32’ in namespace ‘std_msgs’ does not name a type

void chatterCallback(const std_msgs::Float32::ConstPtr& msg)

Here is my code:


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

class FloatSub
{
private:

    ros::Publisher sub;
    std_msgs::Float32 abc;

public:
    void Callback(const std_msgs::Float32::ConstPtr& msg);
};

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

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

  ros::NodeHandle nh;

  FloatSub floatsub;
  ros::Subscriber sub = nh.subscribe("chatter", 1000, &FloatSub::Callback, &floatsub);
  ros::spin();
  return 0;
}

I think my problem comes from the line :

ROS_INFO("I heard: [%f]", msg->data);

From my understanding, since the topic is publishing a float, I also need to take in a float however I do not know how the 'msg->data' works and how it can be modified to suite different data types. If anyone could link me or any help would be welcome. Thanks.

edit retag flag offensive reopen merge delete

Closed for the following reason the question is answered, right answer was accepted by J. J.
close date 2018-09-10 00:27:45.121325

Comments

Did you add std_msgs to the dependencies of your package in the CMakeLists.txt and package.xml?

Chaos gravatar image Chaos  ( 2018-08-31 02:46:01 -0600 )edit

I just fixed! apparently I was subscribing to the wrong topic But I still do not know how the 'msg-> data' works. I just copied it from another code. I mean what would I put in the msg -> X if I have a float64 or a sensor_msg::Range data type?

J. J. gravatar image J. J.  ( 2018-08-31 02:55:23 -0600 )edit

In your case, msg is a pointer to the class std_msgs::Float32ConstPtr, that is a ROS's standard message type that has only one field: data. For other types' reference you can search on the ROS wiki (i.e.: sensor_msgs::Range)

Chaos gravatar image Chaos  ( 2018-08-31 03:00:10 -0600 )edit

As I am using rosmsg show std_msgs/Float32 and it says float32 data I am assuming the 'data' from msg->'data' comes from this? Sorry if this is a noob question.

J. J. gravatar image J. J.  ( 2018-08-31 03:12:05 -0600 )edit

It's actually a pointer to a std_msgs::Float32, being passed by reference. You access its fields using the pointer operator ->.

pcoenen gravatar image pcoenen  ( 2018-08-31 03:15:02 -0600 )edit

Yes, you're right. Every message in ROS is to be considered as a class (or as a struct). By using rosmsg show you can see how the message is defined. Don't worry: everybody was a noob!

Chaos gravatar image Chaos  ( 2018-08-31 03:16:02 -0600 )edit