ROS Resources: Documentation | Support | Discussion Forum | Index | Service Status | ros @ Robotics Stack Exchange |
1 | initial version |
Alright so I figured it out, at least it works for me. This is assuming you have your class in separate .h and .cpp files but if you don't it still works but it's slightly different.
testPublish.h:
#ifndef TESTPUBLISH_H
#define TESTPUBLISH_H
#include <ros.h>
#include <std_msgs/Float32.h>
class testPublish {
public:
testPublish();
std_msgs::Float32 test;
ros::Publisher topicTestOut;
void publishFunction();
};
#endif
testPublish.cpp
#include "testPublish.h"
testPublish::testPublish() : topicTestOut("au_testOut", &test) {
test.data = 0;
}
void testPublish::publishFunction() {
test.data += 1;
topicTestOut.publish(&test);
}
Then in your sketch file you have to create an object with the class:
testPublish testClassPublish;
and advertise it from your nodeHandler:
nh.advertise(testClassPublish.topicTestOut);
Then just to get test data from my loop I called publishFunction:
testClassPublish.publishFunction();