topic_status_monitor.cpp
Go to the documentation of this file.
00001 /*
00002  * Copyright 2017 Fraunhofer Institute for Manufacturing Engineering and Automation (IPA)
00003  *
00004  * Licensed under the Apache License, Version 2.0 (the "License");
00005  * you may not use this file except in compliance with the License.
00006  * You may obtain a copy of the License at
00007  *
00008  *   http://www.apache.org/licenses/LICENSE-2.0
00009 
00010  * Unless required by applicable law or agreed to in writing, software
00011  * distributed under the License is distributed on an "AS IS" BASIS,
00012  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00013  * See the License for the specific language governing permissions and
00014  * limitations under the License.
00015  */
00016 
00017 
00018 #include <ros/ros.h>
00019 #include <topic_tools/shape_shifter.h>
00020 #include <diagnostic_updater/diagnostic_updater.h>
00021 #include <diagnostic_updater/update_functions.h>
00022 #include <diagnostic_updater/publisher.h>
00023 #include <boost/thread/mutex.hpp>
00024 #include <cmath>
00025 
00026 
00027 class GenericTopicDiagnostic
00028 {
00029 public:
00030   GenericTopicDiagnostic(std::string& topic_name, diagnostic_updater::Updater& diagnostic_updater)
00031   {
00032     ros::NodeHandle nh;
00033     double hz, hzerror;
00034     int window_size;
00035     ros::param::get("~hz", hz);
00036     ros::param::get("~hzerror", hzerror);
00037     ros::param::param<int>("~window_size", window_size, std::ceil(hz));
00038 
00039     min_freq_ = hz-hzerror;
00040     max_freq_ = hz+hzerror;
00041 
00042     diagnostic_updater::FrequencyStatusParam freq_param(&min_freq_, &max_freq_, 0.0, window_size); //min_freq, max_freq, tolerance (default: 0.1), window_size (default: 5)
00043     diagnostic_updater::TimeStampStatusParam stamp_param(-1, 1); //min_acceptable (default: -1), max_acceptable (default: 5)
00044 
00045     topic_diagnostic_task_.reset(new diagnostic_updater::TopicDiagnostic(topic_name, diagnostic_updater, freq_param, stamp_param));
00046 
00047     generic_sub_ = nh.subscribe<topic_tools::ShapeShifter>(topic_name, 1, &GenericTopicDiagnostic::topicCallback, this);
00048   }
00049 
00050   void topicCallback(const topic_tools::ShapeShifter::ConstPtr& msg)
00051   {
00052     topic_diagnostic_task_->tick(ros::Time::now());
00053   }
00054 
00055   double min_freq_, max_freq_;
00056   ros::Subscriber generic_sub_;
00057   boost::shared_ptr< diagnostic_updater::TopicDiagnostic > topic_diagnostic_task_;
00058 };
00059 
00060 
00061 class TopicStatusMonitor
00062 {
00063 public:
00064   TopicStatusMonitor()
00065   {
00066     ros::NodeHandle nh;
00067     ros::param::get("~topics", topics_);
00068     ros::param::get("~diagnostics_name", hardware_id_);
00069     diagnostic_updater_.setHardwareID(hardware_id_);
00070     
00071     for(size_t i=0; i<topics_.size(); i++)
00072     {
00073       tasks_.push_back(new GenericTopicDiagnostic (topics_[i], diagnostic_updater_));
00074     }
00075 
00076     diagnostic_timer_ = nh.createTimer(ros::Duration(1.0), &TopicStatusMonitor::updateDiagnostics, this);
00077     diagnostic_timer_.start();
00078   }
00079 
00080   ~TopicStatusMonitor()
00081   {}
00082   
00083 
00084   void updateDiagnostics(const ros::TimerEvent& event)
00085   {
00086     diagnostic_updater_.update();
00087   }
00088 
00089 private:
00090   std::string hardware_id_;
00091   std::vector<std::string> topics_;
00092   diagnostic_updater::Updater diagnostic_updater_;
00093 
00094   std::vector< GenericTopicDiagnostic* > tasks_;
00095   ros::Timer diagnostic_timer_;
00096 };
00097 
00098 
00099 
00100 int main(int argc, char **argv)
00101 {
00102   ros::init(argc, argv, "topic_status_monitor");
00103   TopicStatusMonitor tsm;
00104 
00105   ros::spin();
00106 
00107   return 0;
00108 }


cob_monitoring
Author(s): Florian Weisshardt , Felix Messmer
autogenerated on Sun Jun 9 2019 20:20:19