qnode.cpp
Go to the documentation of this file.
1 
9 /*****************************************************************************
10 ** Includes
11 *****************************************************************************/
12 
13 #include <ros/ros.h>
14 #include <ros/network.h>
15 #include <string>
16 #include <std_msgs/String.h>
17 #include <sstream>
18 #include "../include/%(package)s/qnode.hpp"
19 
20 /*****************************************************************************
21 ** Namespaces
22 *****************************************************************************/
23 
24 namespace %(package)s {
25 
26 /*****************************************************************************
27 ** Implementation
28 *****************************************************************************/
29 
30 QNode::QNode(int argc, char** argv ) :
31  init_argc(argc),
32  init_argv(argv)
33  {}
34 
36  if(ros::isStarted()) {
37  ros::shutdown(); // explicitly needed since we use ros::start();
38  ros::waitForShutdown();
39  }
40  wait();
41 }
42 
43 bool QNode::init() {
44  ros::init(init_argc,init_argv,"%(package)s");
45  if ( ! ros::master::check() ) {
46  return false;
47  }
48  ros::start(); // explicitly needed since our nodehandle is going out of scope.
49  ros::NodeHandle n;
50  // Add your ros communications here.
51  chatter_publisher = n.advertise<std_msgs::String>("chatter", 1000);
52  start();
53  return true;
54 }
55 
56 bool QNode::init(const std::string &master_url, const std::string &host_url) {
57  std::map<std::string,std::string> remappings;
58  remappings["__master"] = master_url;
59  remappings["__hostname"] = host_url;
60  ros::init(remappings,"%(package)s");
61  if ( ! ros::master::check() ) {
62  return false;
63  }
64  ros::start(); // explicitly needed since our nodehandle is going out of scope.
65  ros::NodeHandle n;
66  // Add your ros communications here.
67  chatter_publisher = n.advertise<std_msgs::String>("chatter", 1000);
68  start();
69  return true;
70 }
71 
72 void QNode::run() {
73  ros::Rate loop_rate(1);
74  int count = 0;
75  while ( ros::ok() ) {
76 
77  std_msgs::String msg;
78  std::stringstream ss;
79  ss << "hello world " << count;
80  msg.data = ss.str();
81  chatter_publisher.publish(msg);
82  log(Info,std::string("I sent: ")+msg.data);
83  ros::spinOnce();
84  loop_rate.sleep();
85  ++count;
86  }
87  std::cout << "Ros shutdown, proceeding to close the gui." << std::endl;
88  Q_EMIT rosShutdown(); // used to signal the gui for a shutdown (useful to roslaunch)
89 }
90 
91 
92 void QNode::log( const LogLevel &level, const std::string &msg) {
93  logging_model.insertRows(logging_model.rowCount(),1);
94  std::stringstream logging_model_msg;
95  switch ( level ) {
96  case(Debug) : {
97  ROS_DEBUG_STREAM(msg);
98  logging_model_msg << "[DEBUG] [" << ros::Time::now() << "]: " << msg;
99  break;
100  }
101  case(Info) : {
102  ROS_INFO_STREAM(msg);
103  logging_model_msg << "[INFO] [" << ros::Time::now() << "]: " << msg;
104  break;
105  }
106  case(Warn) : {
107  ROS_WARN_STREAM(msg);
108  logging_model_msg << "[INFO] [" << ros::Time::now() << "]: " << msg;
109  break;
110  }
111  case(Error) : {
112  ROS_ERROR_STREAM(msg);
113  logging_model_msg << "[ERROR] [" << ros::Time::now() << "]: " << msg;
114  break;
115  }
116  case(Fatal) : {
117  ROS_FATAL_STREAM(msg);
118  logging_model_msg << "[FATAL] [" << ros::Time::now() << "]: " << msg;
119  break;
120  }
121  }
122  QVariant new_row(QString(logging_model_msg.str().c_str()));
123  logging_model.setData(logging_model.index(logging_model.rowCount()-1),new_row);
124  Q_EMIT loggingUpdated(); // used to readjust the scrollbar
125 }
126 
127 } // namespace %(package)s
void log(const LogLevel &level, const std::string &msg)
Definition: qnode.cpp:92
void run()
Definition: qnode.cpp:72
virtual ~QNode()
Definition: qnode.cpp:35
char ** init_argv
Definition: qnode.hpp:68
QStringListModel logging_model
Definition: qnode.hpp:70
#define package
Definition: main_window.hpp:9
int init_argc
Definition: qnode.hpp:67
void loggingUpdated()
ros::Publisher chatter_publisher
Definition: qnode.hpp:69
bool init()
Definition: qnode.cpp:43
LogLevel
Definition: qnode.hpp:51
void rosShutdown()


qt_create
Author(s): Daniel Stonier
autogenerated on Wed Mar 11 2020 03:12:19