-
Notifications
You must be signed in to change notification settings - Fork 23
/
Copy pathBattery.cc
170 lines (155 loc) · 4.26 KB
/
Battery.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with this program. If not, see http://www.gnu.org/licenses/.
//
#include "Battery.h"
#include <float.h>
/**
* Create an infinite energy storage.
* It will always be fully charged and never empty.
* This battery should be used if energy consumption and recharging should be ignored.
*/
Battery::Battery() :
capacity(0), remaining(0), infinite(true), overdraw(0)
{
}
/**
* Create a limited energy storage.
* The battery will be fully charged in the beginning.
* If capacity is initialized with 0, an infinite battery is created.
*
* @param capacity the size of the battery in mAh (e.g. 2200)
*/
Battery::Battery(float capacity) :
capacity(capacity), remaining(capacity), overdraw(0)
{
this->infinite = (capacity == 0) ? true : false;
}
/**
* Create a limited energy storage.
*
* @param capacity the size of the battery in mAh (e.g. 2200)
* @param remaining the remaining energy in the battery at creation (e.g. 1500)
*/
Battery::Battery(float capacity, float remaining) :
capacity(capacity), remaining(remaining), overdraw(0)
{
this->infinite = (capacity == 0) ? true : false;
if (remaining > capacity) EV_WARN << "Battery initialized with remaining > capacity" << endl;
if (remaining < 0) throw cRuntimeError("Remaining less than 0 is not possible.");
}
Battery::~Battery()
{
}
/**
* Add stored energy.
*
* @param amount quantity of energy to add to the storage, in [mAh]
* @return 'false' if maximum capacity was reached
*/
bool Battery::charge(float amount)
{
if (infinite) return true;
remaining += amount;
if (remaining > capacity) {
remaining = capacity;
return false;
}
return true;
}
/**
* Consume stored energy.
*
* @param amount quantity of energy consumed, in [mAh]
* @return 'false' if more energy was consumed than available
*/
bool Battery::discharge(float amount)
{
if (not std::isfinite(amount)) {
std::string error_msg = std::string("Amount is not finite: ") + std::to_string(amount);
EV_ERROR << error_msg << endl;
amount = 0;
}
if (infinite) return true;
remaining -= amount;
if (not std::isfinite(remaining)) {
std::string error_msg = std::string("Remaining is not finite: ") + std::to_string(remaining);
EV_ERROR << error_msg << endl;
remaining = 0;
}
if (remaining < 0) {
overdraw += remaining * (-1);
remaining = 0;
return false;
}
return true;
}
/**
* @return the absolute capacity of the battery, in [mAh]
*/
float Battery::getCapacity() const
{
return capacity;
}
/**
* @return difference between maximum capacity and remaining energy, in [mAh]
*/
float Battery::getMissing() const
{
if (infinite) return 0;
return capacity - remaining;
}
/**
* @return remaining energy, in [mAh]
*/
float Battery::getRemaining() const
{
if (infinite) return FLT_MAX;
return remaining;
}
/**
* @return remaining energy, in percentage
*/
int Battery::getRemainingPercentage() const
{
if (infinite) return 100;
return 100 * remaining / capacity;
}
/**
* @return 'true' if energy storage is empty
*/
bool Battery::isEmpty() const
{
if (infinite) return false;
return (remaining <= 0);
}
/**
* @return 'true' if energy storage is completely full
*/
bool Battery::isFull() const
{
if (infinite) return true;
return (remaining >= capacity);
}
/**
* @return over-charged energy and set counter back to zero, in [mAh]
*/
float Battery::getAndResetOverdraw()
{
if (overdraw > 0) {
EV_WARN << "Battery storage was over-exhausted during the last flight by " << overdraw << "mAh" << endl;
}
float amount = overdraw;
overdraw = 0;
return amount;
}