observation.cpp
Go to the documentation of this file.
1 /*********************************************************************
2  * Software License Agreement (BSD License)
3  *
4  * Copyright (c) 2009, Willow Garage, 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 Willow Garage nor the names of its
18  * contributors may be used to endorse or promote products derived
19  * from this software without specific prior written permission.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
24  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
25  * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
26  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
27  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
28  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
29  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
31  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
32  * POSSIBILITY OF SUCH DAMAGE.
33  *********************************************************************/
34 
35 #include "power_state_estimator.h"
36 
37 #include <stdlib.h>
38 #include <fstream>
39 #include <iostream>
40 
41 #include "ros/ros.h"
42 
43 using namespace std;
44 using namespace power_monitor;
45 
46 // PowerObservation
47 
48 PowerObservation::PowerObservation() { }
49 
50 PowerObservation::PowerObservation(const ros::Time& stamp, int8_t master_state, const vector<BatteryObservation>& batteries) : stamp_(stamp), master_state_(master_state), batteries_(batteries) { }
51 
52 const ros::Time& PowerObservation::getStamp() const { return stamp_; }
53 int8_t PowerObservation::getMasterState() const { return master_state_; }
54 const vector<BatteryObservation>& PowerObservation::getBatteries() const { return batteries_; }
55 
56 unsigned int PowerObservation::getAcCount() const
57 {
58  unsigned int ac_count = 0;
59  for (unsigned int i = 0; i < batteries_.size(); i++)
60  if (batteries_[i].isAcPresent())
61  ac_count++;
62 
63  return ac_count;
64 }
65 
66 float PowerObservation::getTotalPower() const
67 {
68  float total_power = 0.0f;
69  for (unsigned int i = 0; i < batteries_.size(); i++)
70  total_power += batteries_[i].getPower();
71 
72  return total_power;
73 }
74 
78 float PowerObservation::getMinVoltage() const
79 {
80  float min_voltage = 9999.9f;
81  for (unsigned int i = 0; i < batteries_.size(); i++)
82  min_voltage = min(min_voltage, batteries_[i].getVoltage());
83 
84  return min_voltage;
85 }
86 
90 unsigned int PowerObservation::getMinRelativeStateOfCharge() const
91 {
92  unsigned int min_rsc = 999;
93  for (unsigned int i = 0; i < batteries_.size(); i++)
94  min_rsc = min(min_rsc, batteries_[i].getRelativeStateOfCharge());
95 
96  return min_rsc;
97 }
98 
99 float PowerObservation::getTotalRemainingCapacity() const
100 {
101  float rem_cap = 0.0f;
102  for (unsigned int i = 0; i < batteries_.size(); i++)
103  rem_cap += batteries_[i].getRemainingCapacity();
104 
105  return rem_cap;
106 }
107 
111 ros::Duration PowerObservation::getMinTimeToEmpty(const ros::Time& t) const
112 {
113  ros::Duration min_tte(-1, 0);
114 
115  int count = 0;
116 
117  for (unsigned int i = 0; i < batteries_.size(); i++)
118  {
119  const BatteryObservation& b = batteries_[i];
120 
121  if (b.isAcPresent())
122  continue;
123 
124  ros::Duration staleness = t - b.getStamp();
125 
126  ros::Duration tte(0);
127  if (staleness < b.getTimeToEmpty())
128  tte = b.getTimeToEmpty() - staleness;
129 
130  if (count == 0)
131  min_tte = tte;
132  else
133  min_tte = min(min_tte, tte);
134 
135  count++;
136  }
137 
138  return min_tte;
139 }
140 
144 ros::Duration PowerObservation::getMaxTimeToFull(const ros::Time& t) const
145 {
146  ros::Duration max_ttf(-1, 0);
147 
148  int count = 0;
149 
150  for (unsigned int i = 0; i < batteries_.size(); i++)
151  {
152  const BatteryObservation& b = batteries_[i];
153 
154  if (!b.isAcPresent())
155  continue;
156 
157  ros::Duration staleness = t - b.getStamp();
158 
159  ros::Duration ttf(0);
160  if (staleness < b.getTimeToFull())
161  ttf = b.getTimeToFull() - staleness;
162 
163  if (count == 0)
164  max_ttf = ttf;
165  else
166  max_ttf = max(max_ttf, ttf);
167 
168  count++;
169  }
170 
171  return max_ttf;
172 }
173 
174 // BatteryObservation
175 
176 BatteryObservation::BatteryObservation(const ros::Time& stamp, bool ac_present, float voltage, float current,
177  unsigned int relative_state_of_charge, float remaining_capacity,
178  const ros::Duration& time_to_empty, const ros::Duration& time_to_full)
179  : stamp_(stamp), ac_present_(ac_present), voltage_(voltage), current_(current), relative_state_of_charge_(relative_state_of_charge),
180  remaining_capacity_(remaining_capacity), time_to_empty_(time_to_empty), time_to_full_(time_to_full)
181 {
182 }
183 
185 
187 float BatteryObservation::getVoltage() const { return voltage_; }
188 float BatteryObservation::getCurrent() const { return current_; }
193 
195 {
196  return voltage_ * current_;
197 }
const ros::Time & getStamp() const
unsigned int getRelativeStateOfCharge() const
const ros::Duration & getTimeToEmpty() const
const ros::Duration & getTimeToFull() const
int min(int a, int b)


power_monitor
Author(s): Tim Field, Curt Meyers
autogenerated on Fri May 14 2021 02:50:05