diff --git a/rtabmap_sync/include/rtabmap_sync/SyncDiagnostic.h b/rtabmap_sync/include/rtabmap_sync/SyncDiagnostic.h index 31aba53d..d914c239 100644 --- a/rtabmap_sync/include/rtabmap_sync/SyncDiagnostic.h +++ b/rtabmap_sync/include/rtabmap_sync/SyncDiagnostic.h @@ -6,53 +6,74 @@ #include #include +#include "rtabmap/utilite/ULogger.h" + namespace rtabmap_sync { class SyncDiagnostic { public: - SyncDiagnostic(double tolerance = 0.1) : + SyncDiagnostic(double tolerance = 0.1, int windowSize = 5) : frequencyStatus_(diagnostic_updater::FrequencyStatusParam(&targetFrequency_, &targetFrequency_, tolerance)), lastCallbackCalledStamp_(ros::Time::now().toSec()-1), - targetFrequency_(0.0) - {} + targetFrequency_(0.0), + windowSize_(windowSize) + { + UASSERT(windowSize_ >= 1); + } protected: void initDiagnostic( const std::string & topic, const std::string & topicsNotReceivedWarningMsg, - std::vector otherTasks = std::vector()) + std::vector otherTasks = std::vector()) { topicsNotReceivedWarningMsg_ = topicsNotReceivedWarningMsg; - std::list strList = uSplit(topic, '/'); - for(int i=0; i<2 && strList.size()>1; ++i) - { - // Assuming format is /back_camera/left/image, we want "back_camera" - strList.pop_back(); - } - diagnosticUpdater_.add(frequencyStatus_); - for(size_t i=0; i strList = uSplit(topic, '/'); + for(int i=0; i<2 && strList.size()>1; ++i) + { + // Assuming format is /back_camera/left/image, we want "back_camera" + strList.pop_back(); + } + diagnosticUpdater_.add(frequencyStatus_); + for(size_t i=0; i0.0 && targetFrequency == 0 && (targetFrequency_ == 0.0 || period < 1.0/targetFrequency_)) - { - targetFrequency_ = 1.0/period; - } + double singlePeriod = stamp.toSec() - lastCallbackCalledStamp_; + + window_.push_back(singlePeriod); + if(window_.size() > windowSize_) + { + window_.pop_front(); + } + double period = 0.0; + if(window_.size() == windowSize_) + { + for(size_t i=0; i0.0 && targetFrequency == 0 && (targetFrequency_ == 0.0 || period < 1.0/targetFrequency_)) + { + targetFrequency_ = 1.0/period; + } else if(targetFrequency>0) { targetFrequency_ = targetFrequency; } - lastCallbackCalledStamp_ = stamp.toSec(); + lastCallbackCalledStamp_ = stamp.toSec(); } private: @@ -63,7 +84,7 @@ private: if(ros::Time::now().toSec()-lastCallbackCalledStamp_ >= 5 && !topicsNotReceivedWarningMsg_.empty()) { ROS_WARN_THROTTLE(5, topicsNotReceivedWarningMsg_.c_str()); - } + } } private: @@ -73,6 +94,8 @@ private: ros::Timer diagnosticTimer_; double lastCallbackCalledStamp_; double targetFrequency_; + int windowSize_; + std::deque window_; };