subscriber_test.cpp
Go to the documentation of this file.
1 #include <boost/bind.hpp>
2 #include <ros/ros.h>
4 #include <boost/bind.hpp>
5 
7 
8 #include <std_msgs/Float32.h>
9 #include <nav_msgs/Odometry.h>
10 
11 namespace du = diagnostic_updater;
12 
13 // Alias type for easier access to DiagnosticStatus enumerations.
14 typedef diagnostic_msgs::DiagnosticStatus DS;
15 
17 {
21 
23 
25 
26  public:
28  {
29  // Setup a one-shot timer to initialize the node after a brief
30  // delay so that /rosout is always fully initialized.
31  ROS_INFO("Starting initialization timer...");
34  this,
35  true);
36  }
37 
38  void initialize(const ros::WallTimerEvent &ignored)
39  {
40  // Example to subscribe to a class method.
42 
43  // Example to subscribe using a boost function. I am unable to
44  // figure out how to get the compiler to infer the type, so I have
45  // to declare the function first and then pass that.
46  boost::function<void(const boost::shared_ptr< nav_msgs::Odometry const> &)> callback = boost::bind(&SubscriberTest::handleMessage, this, _1);
47  sub_ = swri::Subscriber(nh_, "odom", 100, callback);
48 
50 
53  "swri::Subscriber test (manual diagnostics)", this,
55 
57  "swri::Subscriber test (auto diagnostics)", this,
59 
62  this);
63  }
64 
65  void handleMessage(const nav_msgs::OdometryConstPtr &msg)
66  {
67  ROS_INFO_THROTTLE(1.0, "Receiving messages.");
68  }
69 
71  {
73  }
74 
76  {
77  status.summary(DS::OK, "No errors reported.");
78 
79  // The swri::Subscriber provides methods to access all of the
80  // statistics and status information. These are suitable for
81  // making control decisions or report diagnostics. In this
82  // example, we access the data directly to update a diagnostic
83  // status on our own. This is useful if you want to apply custom
84  // summary semantics or change how the key/values are included.
85 
86  if (sub_.messageCount() == 0) {
87  status.mergeSummary(DS::WARN, "No messages received.");
88  } else if (sub_.inTimeout()) {
89  status.mergeSummary(DS::ERROR, "Topic has timed out.");
90  } else if (sub_.timeoutCount() > 0) {
91  status.mergeSummary(DS::WARN, "Timeouts have occurred.");
92  }
93 
94  if (sub_.mappedTopic() == sub_.unmappedTopic()) {
95  status.addf("Topic Name", "%s", sub_.mappedTopic().c_str());
96  } else {
97  status.addf("Topic Name", "%s -> %s",
98  sub_.unmappedTopic().c_str(),
99  sub_.mappedTopic().c_str());
100  }
101  status.addf("Number of publishers", "%d", sub_.numPublishers());
102 
103  status.addf("Mean Latency [us]", "%f", sub_.meanLatencyMicroseconds());
104  status.addf("Min Latency [us]", "%f", sub_.minLatencyMicroseconds());
105  status.addf("Max Latency [us]", "%f", sub_.maxLatencyMicroseconds());
106 
107  status.addf("Mean Frequency [Hz]", "%f", sub_.meanFrequencyHz());
108  status.addf("Mean Period [ms]", "%f", sub_.meanPeriodMilliseconds());
109  status.addf("Min Period [ms]", "%f", sub_.minPeriodMilliseconds());
110  status.addf("Max Period [ms]", "%f", sub_.maxPeriodMilliseconds());
111 
112  if (sub_.timeoutEnabled()) {
113  status.addf("Timed Out Count", "%d [> %f ms]",
114  sub_.timeoutCount(),
116  } else {
117  status.add("Timed Out Count", "N/A");
118  }
119  }
120 
122  {
123  status.summary(DS::OK, "No errors reported.");
124 
125  // In this example, we use swri::Subscriber's appendDiagnostics
126  // method to include diagnostics using a common format and common
127  // summary semantics. If we didn't care about specific bits of
128  // information, we can leave them out by removing specific flags.
129  sub_.appendDiagnostics(status, "Odometry",
135  }
136 }; // class SubscriberTest
137 
138 int main(int argc, char **argv)
139 {
140  ros::init(argc, argv, "subscriber_test");
141 
142  SubscriberTest node;
143  ros::spin();
144 
145  return 0;
146 }
swri::Subscriber::maxPeriodMilliseconds
double maxPeriodMilliseconds() const
Definition: subscriber.h:406
swri::Subscriber
Definition: subscriber.h:61
SubscriberTest::diag_timer_
ros::Timer diag_timer_
Definition: subscriber_test.cpp:20
swri::Subscriber::meanPeriodMilliseconds
double meanPeriodMilliseconds() const
Definition: subscriber.h:394
boost::shared_ptr
diagnostic_updater::DiagnosticStatusWrapper::add
void add(const std::string &key, const bool &b)
swri::Subscriber::meanFrequencyHz
double meanFrequencyHz() const
Definition: subscriber.h:376
ros::init
ROSCPP_DECL void init(const M_string &remappings, const std::string &name, uint32_t options=0)
SubscriberTest::handleMessage
void handleMessage(const nav_msgs::OdometryConstPtr &msg)
Definition: subscriber_test.cpp:65
ros.h
SubscriberTest::diagnostic_updater_
du::Updater diagnostic_updater_
Definition: subscriber_test.cpp:22
SubscriberTest::init_timer_
ros::WallTimer init_timer_
Definition: subscriber_test.cpp:19
swri::Subscriber::mappedTopic
const std::string & mappedTopic() const
Definition: subscriber.h:288
main
int main(int argc, char **argv)
Definition: subscriber_test.cpp:138
ros::WallTimer
diagnostic_updater::Updater
subscriber.h
diagnostic_updater.h
diagnostic_updater::DiagnosticStatusWrapper::summary
void summary(const diagnostic_msgs::DiagnosticStatus &src)
SubscriberTest::SubscriberTest
SubscriberTest()
Definition: subscriber_test.cpp:27
swri::Subscriber::timeoutCount
int timeoutCount()
Definition: subscriber.h:471
diagnostic_updater
swri::Subscriber::appendDiagnostics
void appendDiagnostics(diagnostic_updater::DiagnosticStatusWrapper &status, const std::string &name, const int flags)
Definition: subscriber.h:477
swri::Subscriber::timeoutMilliseconds
double timeoutMilliseconds() const
Definition: subscriber.h:459
swri::Subscriber::unmappedTopic
const std::string & unmappedTopic() const
Definition: subscriber.h:282
swri::Subscriber::maxLatencyMicroseconds
double maxLatencyMicroseconds() const
Definition: subscriber.h:364
DS
diagnostic_msgs::DiagnosticStatus DS
Definition: subscriber_test.cpp:14
swri::Subscriber::DIAG_CONNECTION
@ DIAG_CONNECTION
Definition: subscriber.h:182
swri::Subscriber::DIAG_MSG_COUNT
@ DIAG_MSG_COUNT
Definition: subscriber.h:183
SubscriberTest::handleDiagnosticsTimer
void handleDiagnosticsTimer(const ros::TimerEvent &ignored)
Definition: subscriber_test.cpp:70
ros::TimerEvent
swri::Subscriber::messageCount
int messageCount() const
Definition: subscriber.h:306
diagnostic_updater::DiagnosticStatusWrapper::mergeSummary
void mergeSummary(const diagnostic_msgs::DiagnosticStatus &src)
diagnostic_updater::Updater::setHardwareID
void setHardwareID(const std::string &hwid)
swri::Subscriber::inTimeout
bool inTimeout()
Definition: subscriber.h:465
diagnostic_updater::Updater::update
void update()
ros::WallTimerEvent
swri::Subscriber::DIAG_RATE
@ DIAG_RATE
Definition: subscriber.h:186
SubscriberTest::nh_
ros::NodeHandle nh_
Definition: subscriber_test.cpp:18
SubscriberTest::manualDiagnostics
void manualDiagnostics(du::DiagnosticStatusWrapper &status)
Definition: subscriber_test.cpp:75
swri::Subscriber::numPublishers
int numPublishers() const
Definition: subscriber.h:294
swri::Subscriber::meanLatencyMicroseconds
double meanLatencyMicroseconds() const
Definition: subscriber.h:352
swri::Subscriber::DIAG_LATENCY
@ DIAG_LATENCY
Definition: subscriber.h:185
swri::Subscriber::setTimeout
void setTimeout(const ros::Duration &time_out)
Definition: subscriber.h:412
SubscriberTest::autoDiagnostics
void autoDiagnostics(du::DiagnosticStatusWrapper &status)
Definition: subscriber_test.cpp:121
diagnostic_updater::DiagnosticStatusWrapper::addf
void addf(const std::string &key, const char *format,...)
diagnostic_updater::DiagnosticStatusWrapper
ros::spin
ROSCPP_DECL void spin()
swri::Subscriber::minPeriodMilliseconds
double minPeriodMilliseconds() const
Definition: subscriber.h:400
ros::WallDuration
swri::Subscriber::timeoutEnabled
bool timeoutEnabled() const
Definition: subscriber.h:453
ROS_INFO
#define ROS_INFO(...)
diagnostic_updater::DiagnosticTaskVector::add
void add(const std::string &name, TaskFunction f)
swri::Subscriber::minLatencyMicroseconds
double minLatencyMicroseconds() const
Definition: subscriber.h:358
ros::NodeHandle::createTimer
Timer createTimer(Duration period, const TimerCallback &callback, bool oneshot=false, bool autostart=true) const
ros::Duration
SubscriberTest::initialize
void initialize(const ros::WallTimerEvent &ignored)
Definition: subscriber_test.cpp:38
ros::NodeHandle::createWallTimer
WallTimer createWallTimer(WallDuration period, const WallTimerCallback &callback, bool oneshot=false, bool autostart=true) const
ros::Timer
SubscriberTest::sub_
swri::Subscriber sub_
Definition: subscriber_test.cpp:24
swri::Subscriber::DIAG_TIMEOUT
@ DIAG_TIMEOUT
Definition: subscriber.h:184
ros::NodeHandle
ROS_INFO_THROTTLE
#define ROS_INFO_THROTTLE(period,...)
SubscriberTest
Definition: subscriber_test.cpp:16


swri_roscpp
Author(s): P. J. Reed
autogenerated on Fri Aug 2 2024 08:39:15