Skip to content

Commit d68bb19

Browse files
committed
remove DataLogger -- use Csv instead
1 parent 41d58eb commit d68bb19

File tree

10 files changed

+36
-468
lines changed

10 files changed

+36
-468
lines changed

cmake/MELSources.cmake

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,6 @@ list(APPEND MEL_DEVICES_HEADERS
9696
set(MEL_LOGGING_HEADERS_DIR "${MEL_HEADERS_DIR}/Logging")
9797
list(APPEND MEL_LOGGING_HEADERS
9898
"${MEL_LOGGING_HEADERS_DIR}/Csv.hpp"
99-
"${MEL_LOGGING_HEADERS_DIR}/DataLogger.hpp"
10099
"${MEL_LOGGING_HEADERS_DIR}/File.hpp"
101100
"${MEL_LOGGING_HEADERS_DIR}/Log.hpp"
102101
"${MEL_LOGGING_HEADERS_DIR}/LogRecord.hpp"
@@ -289,7 +288,6 @@ list(APPEND MEL_DEVICES_SRC
289288
set(MEL_LOGGING_SRC_DIR "${MEL_SRC_DIR}/Logging")
290289
list(APPEND MEL_LOGGING_SRC
291290
"${MEL_LOGGING_SRC_DIR}/Csv.cpp"
292-
"${MEL_LOGGING_SRC_DIR}/DataLogger.cpp"
293291
"${MEL_LOGGING_SRC_DIR}/File.cpp"
294292
"${MEL_LOGGING_SRC_DIR}/Log.cpp"
295293
"${MEL_LOGGING_SRC_DIR}/LogRecord.cpp"

examples/ex_csv.cpp

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -69,15 +69,17 @@ int main() {
6969
if (csv.is_open()) {
7070
// make a header
7171
csv.write_row("Time", "double", "string", "int", "vector[0]", "vector[1]", "vector[2]");
72+
// set precision of floating poitn numbers
73+
csv.set_precision(2);
7274
// simulate a loop
73-
print("Starting 60 second loop ...");
75+
print("Starting 10 second loop ...");
7476
Timer timer(hertz(1000));
7577
Time t;
76-
while (t < seconds(60)) {
78+
while (t < seconds(10)) {
7779
// make some data
7880
double a_double = random(0.0, 100.0);
7981
string a_string = "string" + std::to_string(timer.get_elapsed_ticks());
80-
int a_int = random(0, 100);
82+
int a_int = random(0, 1000);
8183
vector<double> a_vector = {random(0.0,1.0), random(1.0,2.0), random(2.0,3.0)};
8284
// write a row (note a_vector will count as 3 fields)
8385
csv.write_row(t, a_double, a_string, a_int, a_vector);

examples/ex_filter.cpp

Lines changed: 10 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
// Author(s): Craig McDonald ([email protected])
1717

1818
#include <MEL/Core/Timer.hpp>
19-
#include <MEL/Logging/DataLogger.hpp>
19+
#include <MEL/Logging/Csv.hpp>
2020
#include <MEL/Math/Butterworth.hpp>
2121
#include <MEL/Math/Filter.hpp>
2222
#include <MEL/Math/Functions.hpp>
@@ -46,31 +46,26 @@ int main() {
4646
double x = 0.5 + o;
4747

4848
// initialize data logger to write immediately to file
49-
DataLogger data_logger(WriterType::Buffered, false);
50-
std::vector<std::string> header = {"Input", "LPF Output", "HPF Output",
51-
"LPF Seeded Output",
52-
"HPF Seeded Output"};
53-
data_logger.set_header(header);
49+
Csv csv("filter.csv");
50+
csv.write_row("Input", "LPF Output", "HPF Output", "LPF Seeded Output", "HPF Seeded Output");
5451

5552
// data storage container
56-
std::vector<double> data_record(header.size());
53+
std::vector<double> data(5);
5754

5855
// begin filtering and logging data
5956
for (int i = 0; i < samples; ++i) {
6057
// generate input signal
6158
x = r * (x - o) * (1 - (x - o)) + o;
6259

6360
// filter input signal and store data
64-
data_record[0] = x;
65-
data_record[1] = lp_filter.update(x);
66-
data_record[2] = hp_filter.update(x);
67-
data_record[3] = lps_filter.update(x);
68-
data_record[4] = hps_filter.update(x);
61+
data[0] = x;
62+
data[1] = lp_filter.update(x);
63+
data[2] = hp_filter.update(x);
64+
data[3] = lps_filter.update(x);
65+
data[4] = hps_filter.update(x);
6966

7067
// write to data log buffer
71-
data_logger.buffer(data_record);
68+
csv.write_row(data);
7269
}
7370

74-
data_logger.save_data("example_filter_data", ".", false);
75-
7671
}

examples/ex_math.cpp

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
#include <MEL/Math/Integrator.hpp>
2222
#include <MEL/Core/Console.hpp>
2323
#include <MEL/Core/Timer.hpp>
24-
#include <MEL/Logging/DataLogger.hpp>
24+
#include <MEL/Logging/Csv.hpp>
2525

2626
using namespace mel;
2727

@@ -35,9 +35,8 @@ int main() {
3535
Integrator integrator = Integrator(5, Integrator::Technique::Simpsons);
3636

3737
// create data logger
38-
DataLogger data_logger(WriterType::Buffered, false);
39-
std::vector<std::string> header = { "t", "x(t)", "dx/dt", "dx/dt", "integral(x)", "integral(x)" };
40-
data_logger.set_header(header);
38+
Csv csv("math.csv");
39+
csv.write_row("t", "x(t)", "dx/dt", "dx/dt", "integral(x)", "integral(x)");
4140
std::vector<double> data(6);
4241

4342
// loop
@@ -55,14 +54,11 @@ int main() {
5554
data[4] = mel::sin(4 * t) * mel::cos(3 * t) + 5;
5655
// integral(x) ~ integrate(dx/dt)
5756
data[5] = integrator.update(data[1], timer.get_elapsed_time());
58-
// write to data log buffer
59-
data_logger.buffer(data);
57+
// write data to csv
58+
csv.write_row(data);
6059
// wait timer
6160
timer.wait();
6261
}
6362

64-
// save data
65-
data_logger.save_data("example_math_data");
66-
6763
return 0;
6864
}

include/MEL/Logging/Csv.hpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
#include <sstream>
2525
#include <fstream>
2626
#include <iomanip>
27+
#include <ios>
2728

2829
namespace mel {
2930

@@ -41,13 +42,19 @@ class Csv : public File {
4142
template <typename Arg, typename... Args>
4243
void write_row(Arg&& arg, Args&&... args);
4344

45+
/// Sets the precision of floating point values (default 6)
46+
void set_precision(std::size_t precision);
47+
4448
private:
4549

4650
// hide functions inherited from File
4751
using File::unlink;
4852
using File::rename;
4953
using File::write;
5054

55+
private:
56+
57+
std::size_t precision_; ///< precision of floating point values
5158
};
5259

5360
// The following free functions are provided for convenience and are not

include/MEL/Logging/DataLogger.hpp

Lines changed: 0 additions & 153 deletions
This file was deleted.

include/MEL/Logging/Detail/Csv.inl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ namespace mel {
33
template <typename Arg, typename... Args>
44
void Csv::write_row(Arg&& arg, Args&&... args) {
55
std::ostringstream ss;
6+
ss << std::setprecision(precision_);
67
ss << std::forward<Arg>(arg);
78
using expander = int[];
89
(void)expander{0, (void(ss << ',' << std::forward<Args>(args)), 0)...};

src/MEL/Logging/Csv.cpp

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,17 @@
55
namespace mel
66
{
77

8-
Csv::Csv() : File() {}
8+
Csv::Csv() : File(), precision_(6) {}
99

1010
Csv::Csv(const std::string &filepath, WriteMode w_mode, OpenMode o_mode) :
11-
File(filepath, w_mode, o_mode)
11+
File(filepath, w_mode, o_mode),
12+
precision_(6)
1213
{
1314

1415
}
1516

17+
void Csv::set_precision(std::size_t precision) {
18+
precision_ = precision;
19+
}
20+
1621
} // mel

0 commit comments

Comments
 (0)