diagnostic_nodelet.cpp
Go to the documentation of this file.
00001 // -*- mode: c++ -*-
00002 /*********************************************************************
00003  * Software License Agreement (BSD License)
00004  *
00005  *  Copyright (c) 2014, JSK Lab
00006  *  All rights reserved.
00007  *
00008  *  Redistribution and use in source and binary forms, with or without
00009  *  modification, are permitted provided that the following conditions
00010  *  are met:
00011  *
00012  *   * Redistributions of source code must retain the above copyright
00013  *     notice, this list of conditions and the following disclaimer.
00014  *   * Redistributions in binary form must reproduce the above
00015  *     copyright notice, this list of conditions and the following
00016  *     disclaimer in the documentation and/o2r other materials provided
00017  *     with the distribution.
00018  *   * Neither the name of the JSK Lab nor the names of its
00019  *     contributors may be used to endorse or promote products derived
00020  *     from this software without specific prior written permission.
00021  *
00022  *  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
00023  *  "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
00024  *  LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
00025  *  FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
00026  *  COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
00027  *  INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
00028  *  BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
00029  *  LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
00030  *  CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
00031  *  LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
00032  *  ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
00033  *  POSSIBILITY OF SUCH DAMAGE.
00034  *********************************************************************/
00035 
00036 #include "jsk_topic_tools/diagnostic_nodelet.h"
00037 #include <sstream>
00038 namespace jsk_topic_tools
00039 {
00040   DiagnosticNodelet::DiagnosticNodelet(const std::string& name):
00041     name_(name)
00042   {
00043 
00044   }
00045   
00046   void DiagnosticNodelet::onInit()
00047   {
00048     ConnectionBasedNodelet::onInit();
00049     diagnostic_updater_.reset(
00050       new TimeredDiagnosticUpdater(*pnh_, ros::Duration(1.0)));
00051     diagnostic_updater_->setHardwareID(getName());
00052     diagnostic_updater_->add(
00053       getName() + "::" + name_,
00054       boost::bind(
00055         &DiagnosticNodelet::updateDiagnostic,
00056         this,
00057         _1));
00058     double vital_rate;
00059     pnh_->param("vital_rate", vital_rate, 1.0);
00060     vital_checker_.reset(
00061       new jsk_topic_tools::VitalChecker(1 / vital_rate));
00062     diagnostic_updater_->start();
00063   }
00064 
00065   void DiagnosticNodelet::updateDiagnostic(
00066     diagnostic_updater::DiagnosticStatusWrapper &stat)
00067   {
00068     if (connection_status_ == SUBSCRIBED) {
00069       if (vital_checker_->isAlive()) {
00070         stat.summary(diagnostic_msgs::DiagnosticStatus::OK,
00071                      getName() + " running");
00072       }
00073       else {
00074         jsk_topic_tools::addDiagnosticErrorSummary(
00075           name_, vital_checker_, stat);
00076       }
00077     }
00078     else {
00079       stat.summary(diagnostic_msgs::DiagnosticStatus::OK,
00080                    getName() + " is not subscribed");
00081     }
00082     std::stringstream topic_names;
00083     for (size_t i = 0; i < publishers_.size(); i++) {
00084       if (i == publishers_.size() - 1) {
00085         topic_names << publishers_[i].getTopic();
00086       }
00087       else {
00088         topic_names << publishers_[i].getTopic() << ", ";
00089       }
00090     }
00091     stat.add("watched topics", topic_names.str());
00092     for (size_t i = 0; i < publishers_.size(); i++) {
00093       stat.add(publishers_[i].getTopic(),
00094                (boost::format("%d subscribers") %
00095                 publishers_[i].getNumSubscribers()).str());
00096     }
00097   }
00098 }


jsk_topic_tools
Author(s): Kei Okada , Yusuke Furuta
autogenerated on Fri Sep 8 2017 03:38:56