ROS Resources: Documentation | Support | Discussion Forum | Index | Service Status | ros @ Robotics Stack Exchange |
1 | initial version |
Wrapping the ros::init() and NodeHandle creation seems to work. In a bigger project where I have passed the ROS nodeHandle around and attached publishers and things to it I can't get it to work yet.
#include "ros/ros.h"
#include "std_msgs/String.h"
#include "rosConnection.H"
#include <sstream>
#include <iostream>
using namespace std;
int main(int argc, char **argv)
{
int count = 0;
bool reconnect = false;
do
{
rosConnectionHandler_t rc (argc, argv);
ros::Publisher chatter_pub = rc.nodeHandle ()->advertise<std_msgs::String>("chatter", 1000);
ros::Rate loop_rate(10);
while (ros::ok() && ros::master::check())
{
std_msgs::String msg;
stringstream ss;
ss << "hello world " << count;
msg.data = ss.str();
cout << msg.data << endl;
chatter_pub.publish(msg);
ros::spinOnce();
loop_rate.sleep();
++count;
}
cout << "ROS OK: " << (ros::ok()?"true":"false") << endl;
cout << "MASTER UP: " << (ros::master::check()?"true":"false") << endl;
reconnect = !ros::master::check() && ros::ok();
if (reconnect)
cout << "Attempting to reconnect" << endl;
} while (reconnect);
return 0;
}
#pragma once
#include <ros/ros.h>
class rosConnectionHandler_t
{
public:
rosConnectionHandler_t (int argc_, char** argv_)
{
ros::init (argc_, argv_, "testnode");
nh = new ros::NodeHandle();
}
~rosConnectionHandler_t ()
{
delete nh;
}
ros::NodeHandle *nodeHandle() { return nh; };
private:
ros::NodeHandle *nh;
};