High level convenience classes for ros_serial
I thought I'd come up with two convenience classes to wrap up ROS communication in my ATmega1280 code. This may not be the most indispensible part of my project, but just for the fun of learning C++ template programming I thought I'd give it a try. My code is as follows:
#include <ros.h>
template <class T>
class ROSLogger {
public:
ROSLogger(ros::NodeHandle* nh, char* topic, short pubFrequency);
virtual ~ROSLogger();
void advertise();
void publish();
T& getMsg();
private:
ros::NodeHandle* nh_;
ros::Publisher publisher_;
T msg_;
const char* topic_;
short pubFrequency_;
};
template <class T>
ROSLogger<T>::ROSLogger(ros::NodeHandle* nh, char* topic, short pubFrequency) :
nh_(nh),
topic_(topic),
pubFrequency_(pubFrequency),
publisher_(topic_, static_cast<ros::Msg*>(&msg_)) {}
template <class T>
ROSLogger<T>::~ROSLogger() {}
template <class T>
void ROSLogger<T>::advertise() {
nh_->advertise(publisher_);
}
template <class T>
T& ROSLogger<T>::getMsg() {
return msg_;
}
template <class T>
void ROSLogger<T>::publish() {
publisher_.publish(&msg_);
}
To use it one would create a ros::NodeHandle
, initialize it, create a ROSLogger, advertise a topic and then publish data, as below.
unsigned char hello[13] = "hello world!";
ros::NodeHandle nh;
ROSLogger<std_msgs::String> rl(&nh, "test", 10);
void setup() {
nh.initNode();
rl.advertise();
}
void loop() {
nh.spinOnce();
rl.getMsg().data = hello;
rl.publish();
}
At the end I'd create a vector of ROSLogger's in a containing class and their publish methods would fire only with the desired frequency automatically. Of course the latter is not yet implemented. Even if the added value is small, I'd still like to make it work. The point is, now it doesn't, and I don't know why. After running serial_node.py only an error appears:
[INFO] [WallTime: 1311526103.141689] ROS Serial Python Node
[INFO] [WallTime: 1311526103.149892] Connected on /dev/ttyUSB0
[ERROR] [WallTime: 1311526118.161632] Lost sync with device, restarting...
/opt/ros/diamondback/stacks/ros_comm/clients/rospy/src/rospy/topics.py:640: UserWarning: '' is not a legal ROS graph resource name. This may cause problems with other ROS tools
super(Publisher, self).__init__(name, data_class, Registration.PUB)
Unable to register with master node [http://localhost:11311]: master may not be running yet. Will keep trying.
Any clues?