What to put ROS_INFO parameter for float data type in a subscriber? [closed]
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.
Did you add
std_msgs
to the dependencies of your package in the CMakeLists.txt and package.xml?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?
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)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.It's actually a pointer to a
std_msgs::Float32
, being passed by reference. You access its fields using the pointer operator->
.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!