-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathenergy_consumed.cpp
240 lines (206 loc) · 7.76 KB
/
energy_consumed.cpp
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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
//***************************************************************************
// Copyright 2007-2023 Universidade do Porto - Faculdade de Engenharia *
// Laboratório de Sistemas e Tecnologia Subaquática (LSTS) *
//***************************************************************************
// This file is part of DUNE: Unified Navigation Environment. *
// *
// Commercial Licence Usage *
// Licencees holding valid commercial DUNE licences may use this file in *
// accordance with the commercial licence agreement provided with the *
// Software or, alternatively, in accordance with the terms contained in a *
// written agreement between you and Faculdade de Engenharia da *
// Universidade do Porto. For licensing terms, conditions, and further *
// information contact [email protected]. *
// *
// Modified European Union Public Licence - EUPL v.1.1 Usage *
// Alternatively, this file may be used under the terms of the Modified *
// EUPL, Version 1.1 only (the "Licence"), appearing in the file LICENCE.md *
// included in the packaging of this file. You may not use this work *
// except in compliance with the Licence. Unless required by applicable *
// law or agreed to in writing, software distributed under the Licence is *
// distributed on an "AS IS" basis, WITHOUT WARRANTIES OR CONDITIONS OF *
// ANY KIND, either express or implied. See the Licence for the specific *
// language governing permissions and limitations at *
// https://github.com/LSTS/dune/blob/master/LICENCE.md and *
// http://ec.europa.eu/idabc/eupl.html. *
//***************************************************************************
// Author: Pedro Calado *
//***************************************************************************
// Utility to compute energy consumed during in LSF log file. *
//***************************************************************************
// ISO C++ 98 headers.
#include <iostream>
#include <fstream>
#include <cstring>
#include <cstdlib>
#include <map>
// DUNE headers.
#include <DUNE/DUNE.hpp>
// Battery Data
#include <Monitors/FuelLevel/BatteryData.hpp>
// Minimum rpm before starting to assume that the vehicle is moving
const float c_min_rpm = 400.0;
// Entity label string to look for
const std::string c_label = "Batteries";
// Number of moving average samples
const unsigned c_samples = 7;
// Minimum number of samples before starting to count energy
const unsigned c_min_samples = 20;
int
main(int32_t argc, char** argv)
{
if (argc <= 1)
{
std::cerr << "Usage: " << argv[0] << " <path_to_log_1/Data.lsf[.gz]> ... <path_to_log_n/Data.lsf[.gz]>"
<< std::endl;
std::cerr << "Or: " << argv[0] << "-e <Voltage Entity Label> <Current Entity Label> <path_to_log_1/Data.lsf[.gz]> ... <path_to_log_n/Data.lsf[.gz]>"
<< std::endl;
return 1;
}
std::string volt_label;
std::string curr_label;
unsigned start_index = 1;
if (strcmp(argv[1], "-e") == 0)
{
if (argc < 5)
{
std::cerr << "Too few arguments" << std::endl;
return 1;
}
volt_label = argv[2];
curr_label = argv[3];
start_index = 4;
}
else
{
volt_label = c_label;
curr_label = c_label;
}
// Total of energy spent
double total_accum = 0.0;
// Total energy spent while the motor was on
double motor_total_accum = 0.0;
// Moving average window sizes
unsigned wsizes[Monitors::FuelLevel::BatteryData::BM_TOTAL];
for (unsigned k = 0; k < Monitors::FuelLevel::BatteryData::BM_TOTAL; k++)
wsizes[k] = c_samples;
for (int32_t i = start_index; i < argc; ++i)
{
std::istream* is = 0;
DUNE::Compression::Methods method = DUNE::Compression::Factory::detect(argv[i]);
if (method == DUNE::Compression::METHOD_UNKNOWN)
is = new std::ifstream(argv[i], std::ios::binary);
else
is = new DUNE::Compression::FileInput(argv[i], method);
DUNE::IMC::Message* msg = NULL;
bool got_name = false;
std::string log_name = "unknown";
// Energy computation related data
Monitors::FuelLevel::BatteryData bdata(wsizes);
bool volt_entity_set = false;
bool curr_entity_set = false;
bool entities_set = false;
unsigned eids[Monitors::FuelLevel::BatteryData::BM_TOTAL] = {0};
unsigned samples = 0;
double last_timestamp = 0.0;
double accum = 0.0;
// Current rpm value
float rpm = 0.0;
// Ignore some logs
bool ignore = false;
try
{
while ((msg = DUNE::IMC::Packet::deserialize(*is)) != 0)
{
if (msg->getId() == DUNE_IMC_LOGGINGCONTROL)
{
if (!got_name)
{
DUNE::IMC::LoggingControl* ptr = static_cast<DUNE::IMC::LoggingControl*>(msg);
if (ptr->op == DUNE::IMC::LoggingControl::COP_STARTED)
{
log_name = ptr->name;
got_name = true;
}
}
}
else if (msg->getId() == DUNE_IMC_ENTITYINFO)
{
DUNE::IMC::EntityInfo* ptr = static_cast<DUNE::IMC::EntityInfo*>(msg);
if (ptr->label.compare(volt_label) == 0)
{
eids[Monitors::FuelLevel::BatteryData::BM_VOLTAGE] = ptr->id;
volt_entity_set = true;
}
if (ptr->label.compare(curr_label) == 0)
{
eids[Monitors::FuelLevel::BatteryData::BM_CURRENT] = ptr->id;
curr_entity_set = true;
}
if (!entities_set && volt_entity_set && curr_entity_set)
{
bdata.setEntities(eids);
entities_set = true;
}
}
else if (msg->getId() == DUNE_IMC_VOLTAGE)
{
if (entities_set)
{
DUNE::IMC::Voltage* ptr = static_cast<DUNE::IMC::Voltage*>(msg);
bdata.update(ptr);
++samples;
if (samples > c_min_samples)
{
float drop = bdata.getEnergyDrop(msg->getTimeStamp() - last_timestamp);
accum += drop;
if (rpm > c_min_rpm)
motor_total_accum += drop;
}
}
last_timestamp = msg->getTimeStamp();
}
else if (msg->getId() == DUNE_IMC_CURRENT)
{
if (entities_set)
{
DUNE::IMC::Current* ptr = static_cast<DUNE::IMC::Current*>(msg);
bdata.update(ptr);
}
}
else if (msg->getId() == DUNE_IMC_RPM)
{
DUNE::IMC::Rpm* ptr = static_cast<DUNE::IMC::Rpm*>(msg);
rpm = ptr->value;
}
else if (msg->getId() == DUNE_IMC_SIMULATEDSTATE)
{
// since it has simulated state let us ignore this log
ignore = true;
delete msg;
std::cerr << "this is a simulated log";
break;
}
delete msg;
}
}
catch (std::runtime_error& e)
{
std::cerr << "ERROR: " << e.what() << std::endl;
}
delete is;
if (ignore)
{
std::cerr << "... ignoring" << std::endl;
continue;
}
std::cerr << "Consumed " << accum << " in " << log_name << "." << std::endl;
total_accum += accum;
}
std::cerr << "Total energy consumed is " << total_accum << "Wh" << std::endl
<< "The amount of " << motor_total_accum
<< std::fixed << std::setprecision(1)
<< "Wh (" << motor_total_accum / total_accum * 100.0
<< "%) was consumed while the motor was on" << std::endl;
return 0;
}