42 #include <boost/bind.hpp> 43 #include <boost/program_options.hpp> 44 #include <boost/thread/thread.hpp> 45 #include <boost/date_time.hpp> 52 namespace po = boost::program_options;
79 for (
int i = 0; i < os.
server.MAX_BAT_COUNT; ++i)
81 present[i] = os.
server.battery[i].present;
82 inhibited[i] = os.
server.battery[i].inhibited;
83 no_good[i] = os.
server.battery[i].power_no_good;
84 last_update[i] = os.
server.battery[i].last_battery_update;
93 id_(id), device_(dev), timeout_(timeout), os(id, 0), stopRequest(false)
97 present.resize(os.
server.MAX_BAT_COUNT);
98 inhibited.resize(os.
server.MAX_BAT_COUNT);
99 no_good.resize(os.
server.MAX_BAT_COUNT);
100 last_update.resize(os.
server.MAX_BAT_COUNT);
112 boost::posix_time::time_duration timeout = boost::posix_time::seconds(5);
113 runThread_->timed_join(timeout);
121 for (
int i = 0; i < os.
server.MAX_BAT_COUNT; ++i)
123 ok = ok && present[i];
124 stale = ((
ros::Time::now() - last_update[i]).toSec() > timeout_) || stale;
133 ss <<
"Port: " << device_ <<
"\n";
136 for (
int i = 0; i < os.
server.MAX_BAT_COUNT; ++i)
138 ss <<
"\tBattery " << i <<
":\n";
139 ss <<
"\t\tPresent: " << (present[i] ?
"Yes" :
"No") <<
"\n";
140 ss <<
"\t\tInhibited: " << (inhibited[i] ?
"Yes" :
"No") <<
"\n";
141 ss <<
"\t\tNo Good: " << (no_good[i] ?
"Yes" :
"No") <<
"\n";
142 ss <<
"\t\tHas updated: " << (last_update[i] >
ros::Time(1) ?
"Yes" :
"No") <<
"\n";
143 ss <<
"\t\tTime Since Update: " << (
ros::Time::now() - last_update[i]).toSec() <<
"\n";
144 ng = ng || no_good[i];
147 ss <<
"Warning: \"No Good\" flag enabled. This may cause battery problems.\n";
153 int main(
int argc,
char** argv)
155 int duration, min_duration, timeout;
156 po::options_description desc(
"battery_check port1 port2 ... [options]");
158 (
"help,h",
"Print help message and exit")
159 (
"verbose,v",
"Verbose battery output")
160 (
"timeout,t", po::value<int>(&timeout)->
default_value(15),
"Timeout before stale")
161 (
"duration,d", po::value<int>(&duration)->default_value(60),
"Maximum duration of test")
162 (
"min-duration,m", po::value<int>(&min_duration)->default_value(20),
"Minimum duration of test")
163 (
"port", po::value<vector<string> >(),
"Battery ports to check");
165 po::positional_options_description p;
168 po::variables_map vm;
169 po::store(po::command_line_parser(argc, argv).options(desc).positional(p).run(), vm);
172 bool verbose = vm.count(
"verbose");
174 if (vm.count(
"help"))
176 cout << desc <<
"\n";
180 if (!vm.count(
"port"))
182 fprintf(stderr,
"No ports specified. Unable to check batteries.\n");
186 if (min_duration < timeout)
188 fprintf(stderr,
"Timeout must be greater than the minimum duration. Unable check batteries.\n");
192 vector<string> ports(vm[
"port"].as< vector<string> >());
193 vector<string>::iterator it;
197 cout <<
"Checking ports: ";
198 for (it = ports.begin(); it != ports.end(); ++it)
204 cout <<
"Running battery check. Waiting for battery drivers to initialize\n";
208 vector<boost::shared_ptr<BatteryServerChecker> > checkers;
209 for (uint i = 0; i < ports.size(); ++i)
212 checkers[i]->start();
216 cout <<
"Battery monitoring started\n";
236 for (uint i = 0; i < checkers.size(); ++i)
237 now_ok = checkers[i]->batteryOK() && now_ok;
247 cout <<
"Stopping battery monitors\n";
249 for (uint i = 0; i < checkers.size(); ++i)
254 fprintf(stdout,
"Battery status reports:\n");
255 for (uint i = 0; i < checkers.size(); ++i)
256 cout << checkers[i]->getStatus().c_str();
261 fprintf(stdout,
"All batteries OK\n");
265 fprintf(stderr,
"Not all batteries reported OK.\n");
266 cerr <<
"Status: \n";
267 for (uint i = 0; i < checkers.size(); ++i)
268 cerr <<
"\tDevice " << i <<
": " << (checkers[i]->batteryOK() ? string(
"OK") : string(
"Error")) <<
"\n";
volatile bool stopRequest
vector< ros::Time > last_update
boost::shared_ptr< boost::thread > runThread_
pr2_msgs::BatteryServer2 server
void initialize(const std::string &input_dev)
BatteryServerChecker(int id, const string &dev, int timeout)
int main(int argc, char **argv)