00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029 #ifndef SWRI_ROSCPP_SUBSCRIBER_H_
00030 #define SWRI_ROSCPP_SUBSCRIBER_H_
00031
00032
00033 #include <ros/node_handle.h>
00034 #include <diagnostic_updater/DiagnosticStatusWrapper.h>
00035
00036 #include <swri_roscpp/parameters.h>
00037 #include <swri_roscpp/subscriber_impl.h>
00038
00039 namespace swri
00040 {
00041
00042
00043
00044
00045
00046
00047
00048
00049
00050
00051
00052
00053
00054
00055
00056
00057
00058
00059
00060
00061 class Subscriber
00062 {
00063 private:
00064
00065
00066
00067
00068
00069 boost::shared_ptr<SubscriberImpl> impl_;
00070
00071 public:
00072 Subscriber();
00073
00074
00075 template<class M , class T >
00076 Subscriber(ros::NodeHandle &nh,
00077 const std::string &topic,
00078 uint32_t queue_size,
00079 void(T::*fp)(const boost::shared_ptr< M const > &),
00080 T *obj,
00081 const ros::TransportHints &transport_hints=ros::TransportHints());
00082
00083
00084 template<class M>
00085 Subscriber(ros::NodeHandle &nh,
00086 const std::string &topic,
00087 uint32_t queue_size,
00088 const boost::function<void(const boost::shared_ptr<M const> &)> &callback,
00089 const ros::TransportHints &transport_hints=ros::TransportHints());
00090
00091
00092
00093
00094
00095
00096 template<class M>
00097 Subscriber(ros::NodeHandle &nh,
00098 const std::string &topic,
00099 boost::shared_ptr< M const > *dest,
00100 const ros::TransportHints &transport_hints=ros::TransportHints());
00101
00102 Subscriber& operator=(const Subscriber &other);
00103
00104
00105 void resetStatistics();
00106
00107
00108
00109 const std::string& unmappedTopic() const;
00110
00111
00112 const std::string& mappedTopic() const;
00113
00114
00115 int numPublishers() const;
00116
00117
00118 int messageCount() const;
00119
00120
00121
00122
00123 ros::Duration age(const ros::Time &now=ros::TIME_MIN) const;
00124 double ageSeconds(const ros::Time &now=ros::TIME_MIN) const;
00125 double ageMilliseconds(const ros::Time &now=ros::TIME_MIN) const;
00126
00127
00128
00129
00130 ros::Duration meanLatency() const;
00131 ros::Duration minLatency() const;
00132 ros::Duration maxLatency() const;
00133 double meanLatencyMicroseconds() const;
00134 double minLatencyMicroseconds() const;
00135 double maxLatencyMicroseconds() const;
00136
00137
00138
00139 double meanFrequencyHz() const;
00140 ros::Duration meanPeriod() const;
00141 ros::Duration minPeriod() const;
00142 ros::Duration maxPeriod() const;
00143 double meanPeriodMilliseconds() const;
00144 double minPeriodMilliseconds() const;
00145 double maxPeriodMilliseconds() const;
00146
00147
00148 void setTimeout(const ros::Duration &time_out);
00149 void setTimeout(const double time_out);
00150
00151 void timeoutParam(const ros::NodeHandle &nh,
00152 const std::string ¶meter_name,
00153 const double default_value);
00154
00155
00156
00157
00158 bool blockTimeouts(bool block);
00159
00160
00161
00162 bool timeoutsBlocked() const;
00163
00164
00165 bool timeoutEnabled() const;
00166
00167 ros::Duration timeout() const;
00168 double timeoutMilliseconds() const;
00169
00170
00171 bool inTimeout();
00172
00173 int timeoutCount();
00174
00175
00176
00177 enum DIAGNOSTIC_FLAGS {
00178 DIAG_CONNECTION = 1 << 0,
00179 DIAG_MSG_COUNT = 1 << 1,
00180 DIAG_TIMEOUT = 1 << 2,
00181 DIAG_LATENCY = 1 << 3,
00182 DIAG_RATE = 1 << 4,
00183
00184 DIAG_ALL = ~0,
00185 DIAG_MOST = DIAG_ALL ^ DIAG_CONNECTION
00186
00187 };
00188
00189
00190
00191
00192
00193
00194
00195
00196
00197
00198
00199 void appendDiagnostics(diagnostic_updater::DiagnosticStatusWrapper &status,
00200 const std::string &name,
00201 const int flags);
00202 };
00203
00204 inline
00205 Subscriber::Subscriber()
00206 {
00207
00208
00209 impl_ = boost::make_shared<SubscriberImpl>();
00210 }
00211
00212 template<class M , class T >
00213 inline
00214 Subscriber::Subscriber(ros::NodeHandle &nh,
00215 const std::string &topic,
00216 uint32_t queue_size,
00217 void(T::*fp)(const boost::shared_ptr< M const > &),
00218 T *obj,
00219 const ros::TransportHints &transport_hints)
00220 {
00221 impl_ = boost::shared_ptr<SubscriberImpl>(
00222 new TypedSubscriberImpl<M,T>(
00223 nh, topic, queue_size, fp, obj, transport_hints));
00224 }
00225
00226 template<class M>
00227 inline
00228 Subscriber::Subscriber(ros::NodeHandle &nh,
00229 const std::string &topic,
00230 uint32_t queue_size,
00231 const boost::function<void(const boost::shared_ptr<M const> &)> &callback,
00232 const ros::TransportHints &transport_hints)
00233 {
00234 impl_ = boost::shared_ptr<SubscriberImpl>(
00235 new BindSubscriberImpl<M>(
00236 nh, topic, queue_size, callback, transport_hints));
00237 }
00238
00239 template<class M>
00240 inline
00241 Subscriber::Subscriber(ros::NodeHandle &nh,
00242 const std::string &topic,
00243 boost::shared_ptr< M const > *dest,
00244 const ros::TransportHints &transport_hints)
00245 {
00246 impl_ = boost::shared_ptr<SubscriberImpl>(
00247 new StorageSubscriberImpl<M>(
00248 nh, topic, dest, transport_hints));
00249 }
00250
00251 inline
00252 Subscriber& Subscriber::operator=(const Subscriber &other)
00253 {
00254
00255
00256
00257
00258
00259
00260
00261
00262
00263
00264
00265
00266 ros::Duration new_timeout = other.impl_->timeout();
00267 if (impl_->timeoutEnabled() && !other.impl_->timeoutEnabled()) {
00268 new_timeout = impl_->timeout();
00269 }
00270
00271 impl_ = other.impl_;
00272 impl_->setTimeout(new_timeout);
00273
00274 return *this;
00275 }
00276
00277 inline
00278 const std::string& Subscriber::unmappedTopic() const
00279 {
00280 return impl_->unmappedTopic();
00281 }
00282
00283 inline
00284 const std::string& Subscriber::mappedTopic() const
00285 {
00286 return impl_->mappedTopic();
00287 }
00288
00289 inline
00290 int Subscriber::numPublishers() const
00291 {
00292 return impl_->numPublishers();
00293 }
00294
00295 inline
00296 void Subscriber::resetStatistics()
00297 {
00298 impl_->resetStatistics();
00299 }
00300
00301 inline
00302 int Subscriber::messageCount() const
00303 {
00304 return impl_->messageCount();
00305 }
00306
00307 inline
00308 ros::Duration Subscriber::age(const ros::Time &now) const
00309 {
00310 if (now == ros::TIME_MIN) {
00311 return impl_->age(ros::Time::now());
00312 } else {
00313 return impl_->age(now);
00314 }
00315 }
00316
00317 inline
00318 double Subscriber::ageSeconds(const ros::Time &now) const
00319 {
00320 return age(now).toSec();
00321 }
00322
00323 inline
00324 double Subscriber::ageMilliseconds(const ros::Time &now) const
00325 {
00326 return age(now).toNSec() / 1000000.0;
00327 }
00328
00329 inline
00330 ros::Duration Subscriber::meanLatency() const
00331 {
00332 return impl_->meanLatency();
00333 }
00334
00335 inline
00336 ros::Duration Subscriber::minLatency() const
00337 {
00338 return impl_->minLatency();
00339 }
00340
00341 inline
00342 ros::Duration Subscriber::maxLatency() const
00343 {
00344 return impl_->maxLatency();
00345 }
00346
00347 inline
00348 double Subscriber::meanLatencyMicroseconds() const
00349 {
00350 return meanLatency().toNSec() / 1000.0;
00351 }
00352
00353 inline
00354 double Subscriber::minLatencyMicroseconds() const
00355 {
00356 return minLatency().toNSec() / 1000.0;
00357 }
00358
00359 inline
00360 double Subscriber::maxLatencyMicroseconds() const
00361 {
00362 return maxLatency().toNSec() / 1000.0;
00363 }
00364
00365 inline
00366 ros::Duration Subscriber::meanPeriod() const
00367 {
00368 return impl_->meanPeriod();
00369 }
00370
00371 inline
00372 double Subscriber::meanFrequencyHz() const
00373 {
00374 return impl_->meanFrequencyHz();
00375 }
00376
00377 inline
00378 ros::Duration Subscriber::minPeriod() const
00379 {
00380 return impl_->minPeriod();
00381 }
00382
00383 inline
00384 ros::Duration Subscriber::maxPeriod() const
00385 {
00386 return impl_->maxPeriod();
00387 }
00388
00389 inline
00390 double Subscriber::meanPeriodMilliseconds() const
00391 {
00392 return meanPeriod().toNSec() / 1000000.0;
00393 }
00394
00395 inline
00396 double Subscriber::minPeriodMilliseconds() const
00397 {
00398 return minPeriod().toNSec() / 1000000.0;
00399 }
00400
00401 inline
00402 double Subscriber::maxPeriodMilliseconds() const
00403 {
00404 return maxPeriod().toNSec() / 1000000.0;
00405 }
00406
00407 inline
00408 void Subscriber::setTimeout(const ros::Duration &time_out)
00409 {
00410 impl_->setTimeout(time_out);
00411 }
00412
00413 inline
00414 void Subscriber::setTimeout(const double time_out)
00415 {
00416 setTimeout(ros::Duration(time_out));
00417 }
00418
00419 inline
00420 void Subscriber::timeoutParam(
00421 const ros::NodeHandle &nh,
00422 const std::string ¶meter_name,
00423 const double default_value)
00424 {
00425 double timeout;
00426 swri::param(nh, parameter_name, timeout, default_value);
00427 setTimeout(timeout);
00428 }
00429
00430 inline
00431 bool Subscriber::blockTimeouts(bool block)
00432 {
00433 return impl_->blockTimeouts(block);
00434 }
00435
00436 inline
00437 bool Subscriber::timeoutsBlocked() const
00438 {
00439 return impl_->timeoutsBlocked();
00440 }
00441
00442 inline
00443 ros::Duration Subscriber::timeout() const
00444 {
00445 return impl_->timeout();
00446 }
00447
00448 inline
00449 bool Subscriber::timeoutEnabled() const
00450 {
00451 return impl_->timeoutEnabled();
00452 }
00453
00454 inline
00455 double Subscriber::timeoutMilliseconds() const
00456 {
00457 return impl_->timeout().toNSec() / 1.0e6;
00458 }
00459
00460 inline
00461 bool Subscriber::inTimeout()
00462 {
00463 return impl_->inTimeout();
00464 }
00465
00466 inline
00467 int Subscriber::timeoutCount()
00468 {
00469 return impl_->timeoutCount();
00470 }
00471
00472 inline
00473 void Subscriber::appendDiagnostics(diagnostic_updater::DiagnosticStatusWrapper &status,
00474 const std::string &name,
00475 int flags)
00476 {
00477
00478 typedef diagnostic_msgs::DiagnosticStatus DS;
00479
00480
00481
00482
00483
00484
00485
00486
00487
00488
00489 if (!messageCount()) {
00490 status.mergeSummaryf(DS::WARN, "Waiting for %s messages.", name.c_str());
00491 } else if (inTimeout()) {
00492 status.mergeSummaryf(DS::ERROR, "%s timeout.", name.c_str());
00493 } else if (timeoutCount()) {
00494 status.mergeSummaryf(DS::WARN, "%s timeouts have occurred.", name.c_str());
00495 }
00496
00497 if (flags & DIAG_CONNECTION) {
00498 if (mappedTopic() == unmappedTopic()) {
00499 status.addf(name + "topic name", "%s", mappedTopic().c_str());
00500 } else {
00501 status.addf(name + "topic name", "%s -> %s",
00502 unmappedTopic().c_str(),
00503 mappedTopic().c_str());
00504 }
00505 status.addf(name + " publishers", "%d", numPublishers());
00506 }
00507
00508 if (flags & DIAG_MSG_COUNT) {
00509 status.add(name + " message count", messageCount());
00510 }
00511
00512 if ((flags & DIAG_TIMEOUT) && timeoutEnabled()) {
00513 status.addf(name + " timeouts count",
00514 "%d ( > %.2f ms)",
00515 timeoutCount(),
00516 timeoutMilliseconds());
00517 }
00518
00519 if (flags & DIAG_LATENCY) {
00520 if (messageCount() < 1) {
00521 status.add(name + " latency",
00522 "min: N/A us, mean: N/A us, max: N/A us");
00523 } else {
00524 status.addf(name + " latency",
00525 "min: %.2f us, mean: %.2f us, max: %.2f us",
00526 minLatencyMicroseconds(),
00527 meanLatencyMicroseconds(),
00528 maxLatencyMicroseconds());
00529 }
00530 }
00531
00532 if (flags & DIAG_RATE) {
00533 if (messageCount() < 2) {
00534 status.add(
00535 name + " rates",
00536 "min period: N/A ms, mean frequency: N/A hz, max period: N/A ms");
00537 } else {
00538 status.addf(
00539 name + " rates",
00540 "min period: %.2f ms, mean frequency: %.2f hz, max period: %.2f ms",
00541 minPeriodMilliseconds(),
00542 meanFrequencyHz(),
00543 maxPeriodMilliseconds());
00544 }
00545 }
00546 }
00547 }
00548 #endif // SWRI_ROSCPP_SUBSCRIBER_H_