Go to the documentation of this file.00001
00009
00010
00011
00012
00013 #include <ros/ros.h>
00014 #include <ros/network.h>
00015 #include <string>
00016 #include <std_msgs/String.h>
00017 #include <sstream>
00018 #include "../include/%(package)s/qnode.hpp"
00019
00020
00021
00022
00023
00024 namespace %(package)s {
00025
00026
00027
00028
00029
00030 QNode::QNode(int argc, char** argv ) :
00031 init_argc(argc),
00032 init_argv(argv)
00033 {}
00034
00035 QNode::~QNode() {
00036 if(ros::isStarted()) {
00037 ros::shutdown();
00038 ros::waitForShutdown();
00039 }
00040 wait();
00041 }
00042
00043 bool QNode::init() {
00044 ros::init(init_argc,init_argv,"%(package)s");
00045 if ( ! ros::master::check() ) {
00046 return false;
00047 }
00048 ros::start();
00049 ros::NodeHandle n;
00050
00051 chatter_publisher = n.advertise<std_msgs::String>("chatter", 1000);
00052 start();
00053 return true;
00054 }
00055
00056 bool QNode::init(const std::string &master_url, const std::string &host_url) {
00057 std::map<std::string,std::string> remappings;
00058 remappings["__master"] = master_url;
00059 remappings["__hostname"] = host_url;
00060 ros::init(remappings,"%(package)s");
00061 if ( ! ros::master::check() ) {
00062 return false;
00063 }
00064 ros::start();
00065 ros::NodeHandle n;
00066
00067 chatter_publisher = n.advertise<std_msgs::String>("chatter", 1000);
00068 start();
00069 return true;
00070 }
00071
00072 void QNode::run() {
00073 ros::Rate loop_rate(1);
00074 int count = 0;
00075 while ( ros::ok() ) {
00076
00077 std_msgs::String msg;
00078 std::stringstream ss;
00079 ss << "hello world " << count;
00080 msg.data = ss.str();
00081 chatter_publisher.publish(msg);
00082 log(Info,std::string("I sent: ")+msg.data);
00083 ros::spinOnce();
00084 loop_rate.sleep();
00085 ++count;
00086 }
00087 std::cout << "Ros shutdown, proceeding to close the gui." << std::endl;
00088 Q_EMIT rosShutdown();
00089 }
00090
00091
00092 void QNode::log( const LogLevel &level, const std::string &msg) {
00093 logging_model.insertRows(logging_model.rowCount(),1);
00094 std::stringstream logging_model_msg;
00095 switch ( level ) {
00096 case(Debug) : {
00097 ROS_DEBUG_STREAM(msg);
00098 logging_model_msg << "[DEBUG] [" << ros::Time::now() << "]: " << msg;
00099 break;
00100 }
00101 case(Info) : {
00102 ROS_INFO_STREAM(msg);
00103 logging_model_msg << "[INFO] [" << ros::Time::now() << "]: " << msg;
00104 break;
00105 }
00106 case(Warn) : {
00107 ROS_WARN_STREAM(msg);
00108 logging_model_msg << "[INFO] [" << ros::Time::now() << "]: " << msg;
00109 break;
00110 }
00111 case(Error) : {
00112 ROS_ERROR_STREAM(msg);
00113 logging_model_msg << "[ERROR] [" << ros::Time::now() << "]: " << msg;
00114 break;
00115 }
00116 case(Fatal) : {
00117 ROS_FATAL_STREAM(msg);
00118 logging_model_msg << "[FATAL] [" << ros::Time::now() << "]: " << msg;
00119 break;
00120 }
00121 }
00122 QVariant new_row(QString(logging_model_msg.str().c_str()));
00123 logging_model.setData(logging_model.index(logging_model.rowCount()-1),new_row);
00124 Q_EMIT loggingUpdated();
00125 }
00126
00127 }