libsensors_monitor.cpp
Go to the documentation of this file.
1 /*
2  * Software License Agreement (BSD License)
3  *
4  * Copyright (c) 2014, Open Source Robotics Foundation, Inc
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  *
11  * * Redistributions of source code must retain the above copyright
12  * notice, this list of conditions and the following disclaimer.
13  * * Redistributions in binary form must reproduce the above
14  * copyright notice, this list of conditions and the following
15  * disclaimer in the documentation and/or other materials provided
16  * with the distribution.
17  * * Neither the name of the Open Source Robotics Foundation nor the
18  * names of its contributors may be used to endorse or promote
19  * products derived from this software without specific prior
20  * written permission.
21  *
22  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
23  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
24  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
25  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
26  * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
27  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
28  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
29  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
30  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
32  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
33  * POSSIBILITY OF SUCH DAMAGE.
34  */
35 
36 /*
37  * Author: Mitchell Wills
38  */
39 
40 #include <sensors/sensors.h>
41 #include <string>
42 #include <vector>
43 #include <boost/foreach.hpp>
44 #include <boost/algorithm/string/replace.hpp>
45 
46 #include <ros/ros.h>
49 
50 // All of the enumerated sensor chips
51 std::vector<SensorChipPtr> sensor_chips_;
52 // Enumerate all of the sensor chips that exist on the system
53 void enumerate_sensors(const std::vector<std::string> &ignore){
54  sensor_chips_.clear();
55 
56  sensors_chip_name const *chip_name;
57  int number = 0;
58  while ((chip_name = sensors_get_detected_chips(NULL, &number)) != NULL){
59  SensorChipPtr sensor(new SensorChip(chip_name, ignore));
60  sensor_chips_.push_back(sensor);
61  }
62 }
63 
64 int main(int argc, char** argv){
65  // Get the hostname of the computer
66  char hostname_buf[1024];
67  int gethostname_result = gethostname(hostname_buf, sizeof(hostname_buf));
68  std::string hostname;
69  if(gethostname_result == 0) {
70  hostname = hostname_buf;
71  ROS_INFO_STREAM("Got system hostname: " << hostname);
72  }
73  else {
74  ROS_WARN("Could not get system hostname: %s", strerror(errno));
75  }
76 
77  if(!hostname.empty()) {
78  std::string hostname_clean = boost::replace_all_copy(hostname, "-", "_");
79  ros::init(argc, argv, "sensor_monitor_"+hostname_clean);
80  }
81  else {
82  ros::init(argc, argv, "sensor_monitor");
83  }
84 
85 
86  ros::NodeHandle nh;
87  ros::NodeHandle pnh("~");
89  if(!hostname.empty()) {
90  updater.setHardwareID(hostname);
91  }
92  else {
93  updater.setHardwareID("none");
94  }
95 
96  // Reset the libsensors library
97  sensors_cleanup();
98  if( sensors_init(NULL) != 0 ) {
99  ROS_FATAL("Failed to initialize sensors library");
100  return 1;
101  }
102 
103  std::vector<std::string> ignore_sensors;
104  pnh.getParam("ignore_sensors", ignore_sensors);
105  enumerate_sensors(ignore_sensors);
106 
107  if(sensor_chips_.size() <= 0) {
108  ROS_ERROR("No sensors detected");
109  }
110 
111  // Add each sensor to the diagnostic updater
112  BOOST_FOREACH(SensorChipPtr sensor_chip, sensor_chips_){
113  BOOST_FOREACH(SensorChipFeaturePtr sensor, sensor_chip->features_){
114  updater.add(sensor->getFullLabel(), boost::bind(&SensorChipFeature::buildStatus, sensor, _1));
115  }
116  }
117 
118 
119  while(ros::ok()){
120  updater.update();
121  ros::spinOnce();
122  ros::Duration(0.1).sleep();
123  }
124 
125  sensors_cleanup();
126 
127  return 0;
128 }
129 
#define ROS_FATAL(...)
void setHardwareID(const std::string &hwid)
bool sleep() const
ROSCPP_DECL void init(int &argc, char **argv, const std::string &name, uint32_t options=0)
void add(const std::string &name, TaskFunction f)
virtual void buildStatus(diagnostic_updater::DiagnosticStatusWrapper &stat)=0
updater
#define ROS_WARN(...)
ROSCPP_DECL bool ok()
int main(int argc, char **argv)
#define ROS_INFO_STREAM(args)
bool getParam(const std::string &key, std::string &s) const
void enumerate_sensors(const std::vector< std::string > &ignore)
std::vector< SensorChipPtr > sensor_chips_
ROSCPP_DECL void spinOnce()
#define ROS_ERROR(...)


libsensors_monitor
Author(s): Mitchell Wills
autogenerated on Sun Nov 17 2019 03:20:10