Skip to content

Commit

Permalink
Add tool for conversion csv to ulog
Browse files Browse the repository at this point in the history
  • Loading branch information
okalachev committed May 2, 2024
1 parent a383c83 commit f3c7124
Show file tree
Hide file tree
Showing 4 changed files with 107 additions and 1 deletion.
21 changes: 21 additions & 0 deletions .github/workflows/tools.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
name: Build tools

on:
push:
branches: [ '*' ]
pull_request:
branches: [ master ]

jobs:
csv_to_ulog:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- name: Build csv_to_ulog
run: cd tools/csv_to_ulog && mkdir build && cd build && cmake .. && make
- name: Test csv_to_ulog
run: |
cd tools/csv_to_ulog/build
echo "t,x,y,z\n0,1,2,3\n1,4,5,6" > log.csv
./csv_to_ulog log.csv log.ulg
test $(stat -c %s log.ulg) -eq 1634072
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
*.hex
*.elf
gazebo/build/
build/
tools/log/
.dependencies
23 changes: 23 additions & 0 deletions tools/csv_to_ulog/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
cmake_minimum_required(VERSION 3.15)
project(csv_to_ulog)
include(FetchContent)
set(CMAKE_CXX_STANDARD 17)

FetchContent_Declare(
ulog_cpp
GIT_REPOSITORY https://github.com/PX4/ulog_cpp.git
GIT_TAG cf24ec6
)

FetchContent_Declare(
rapidcsv
GIT_REPOSITORY https://github.com/d99kris/rapidcsv.git
GIT_TAG v8.82
)

FetchContent_MakeAvailable(ulog_cpp)
FetchContent_MakeAvailable(rapidcsv)

add_executable(csv_to_ulog csv_to_ulog.cpp)
target_link_libraries(csv_to_ulog PUBLIC ulog_cpp::ulog_cpp)
target_include_directories(csv_to_ulog PUBLIC ${rapidcsv_SOURCE_DIR}/src)
62 changes: 62 additions & 0 deletions tools/csv_to_ulog/csv_to_ulog.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
// Copyright (c) 2023 Oleg Kalachev <[email protected]>
// Repository: https://github.com/okalachev/flix

// Tool for conversion CSV log file to ULog format

#include <ulog_cpp/simple_writer.hpp>
#include <vector>
#include <filesystem>
#include "rapidcsv.h"

using std::vector;

struct Data {
uint64_t timestamp;
float values[30];
};

int main(int argc, char** argv)
{
if (argc < 3) {
printf("Usage: %s <file.csv> <file.ulg>\n", argv[0]);
return -1;
}

// check input file exists
if (!std::filesystem::exists(argv[1])) {
printf("Input file \"%s\" does not exist\n", argv[1]);
return -1;
}

// open csv file
rapidcsv::Document csv(argv[1]);
auto columns = csv.GetColumnNames();

const char* msg_name = "state";

ulog_cpp::SimpleWriter writer(argv[2], 0);
writer.writeInfo("sys_name", "flix");

vector<ulog_cpp::Field> fields;
fields.push_back(ulog_cpp::Field("uint64_t", "timestamp"));
columns.erase(columns.begin()); // remove timestamp column
for (auto& column : columns) {
std::replace(column.begin(), column.end(), '.', '_'); // replace dots with underscores
std::transform(column.begin(), column.end(), column.begin(), [](unsigned char c) { return std::tolower(c); }); // lowercase column name
fields.push_back(ulog_cpp::Field("float", column));
}

writer.writeMessageFormat(msg_name, fields);
writer.headerComplete();

const uint16_t msg_id = writer.writeAddLoggedMessage(msg_name);

for (size_t i = 0; i < csv.GetRowCount(); i++) {
Data data;
data.timestamp = csv.GetCell<float>(0, i) * 1000000.0;
for (size_t j = 1; j < columns.size(); j++) {
data.values[j] = csv.GetCell<float>(j, i);
}
writer.writeData(msg_id, data);
}
}

0 comments on commit f3c7124

Please sign in to comment.