-
Notifications
You must be signed in to change notification settings - Fork 6
/
ulog_writer.cpp
81 lines (66 loc) · 2.21 KB
/
ulog_writer.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
/****************************************************************************
* Copyright (c) 2023 PX4 Development Team.
* SPDX-License-Identifier: BSD-3-Clause
****************************************************************************/
#include <chrono>
#include <string>
#include <thread>
#include <ulog_cpp/simple_writer.hpp>
using namespace std::chrono_literals;
static uint64_t currentTimeUs()
{
return std::chrono::duration_cast<std::chrono::microseconds>(
std::chrono::steady_clock::now().time_since_epoch())
.count();
}
struct MyData {
uint64_t timestamp;
float debug_array[4];
float cpuload;
float temperature;
int8_t counter;
static std::string messageName() { return "my_data"; }
static std::vector<ulog_cpp::Field> fields()
{
// clang-format off
return {
{"uint64_t", "timestamp"}, // Monotonic timestamp in microseconds (since boot), must always be the first field
{"float", "debug_array", 4},
{"float", "cpuload"},
{"float", "temperature"},
{"int8_t", "counter"},
}; // clang-format on
}
};
int main(int argc, char** argv)
{
if (argc < 2) {
printf("Usage: %s <file.ulg>\n", argv[0]);
return -1;
}
try {
ulog_cpp::SimpleWriter writer(argv[1], currentTimeUs());
// See https://docs.px4.io/main/en/dev_log/ulog_file_format.html#i-information-message for
// well-known keys
writer.writeInfo("sys_name", "ULogExampleWriter");
writer.writeParameter("PARAM_A", 382.23F);
writer.writeParameter("PARAM_B", 8272);
writer.writeMessageFormat(MyData::messageName(), MyData::fields());
writer.headerComplete();
const uint16_t my_data_msg_id = writer.writeAddLoggedMessage(MyData::messageName());
writer.writeTextMessage(ulog_cpp::Logging::Level::Info, "Hello world", currentTimeUs());
float cpuload = 25.423F;
for (int i = 0; i < 100; ++i) {
MyData data{};
data.timestamp = currentTimeUs();
data.cpuload = cpuload;
data.counter = i;
writer.writeData(my_data_msg_id, data);
cpuload -= 0.424F;
std::this_thread::sleep_for(10ms);
}
} catch (const ulog_cpp::ExceptionBase& e) {
printf("ULog exception: %s\n", e.what());
}
return 0;
}