Skip to content

armgit / flix

Public Imported from GitHub
0

Making an ESP32-based quadcopter from scratch

Add tools for conversion svg logs to mcap

Oleg Kalachev72b2cf49d552
diff --git a/.github/workflows/tools.yml b/.github/workflows/tools.yml
index 01d3035..62a3470 100644
--- a/.github/workflows/tools.yml
+++ b/.github/workflows/tools.yml
@@ -19,3 +19,15 @@ jobs:
echo -e "t,x,y,z\n0,1,2,3\n1,4,5,6" > log.csv
./csv_to_ulog log.csv
test $(stat -c %s log.ulg) -eq 196
+ python_tools:
+ runs-on: ubuntu-latest
+ steps:
+ - uses: actions/checkout@v3
+ - name: Install Python dependencies
+ run: pip install -r tools/requirements.txt
+ - name: Test csv_to_mcap tool
+ run: |
+ cd tools
+ echo -e "t,x,y,z\n0,1,2,3\n1,4,5,6" > log.csv
+ ./csv_to_mcap.py log.csv
+ test $(stat -c %s log.mcap) -eq 883
diff --git a/tools/csv_to_mcap.py b/tools/csv_to_mcap.py
new file mode 100755
index 0000000..f881565
--- /dev/null
+++ b/tools/csv_to_mcap.py
@@ -0,0 +1,46 @@
+#!/usr/bin/env python3
+
+"""Convert CSV log file to MCAP file.
+
+Usage:
+ csv_to_mcap.py <csv_file> [<mcap_file>]
+"""
+
+import csv
+import json
+import docopt
+from mcap.writer import Writer
+
+args = docopt.docopt(__doc__)
+input_file = args['<csv_file>']
+output_file = args['<mcap_file>'] or input_file.replace('.csv', '.mcap')
+if input_file == output_file:
+ raise ValueError('Input and output files are the same')
+
+csv_file = open(input_file, 'r')
+csv_reader = csv.reader(csv_file, delimiter=',')
+header = next(csv_reader)
+
+mcap_file = open(output_file, 'wb')
+writer = Writer(mcap_file)
+writer.start()
+
+properties = {key: {'type': 'number'} for key in header}
+schema_id = writer.register_schema(
+ name="state",
+ encoding="jsonschema",
+ data=json.dumps({"type": "object", "properties": properties}).encode(),
+)
+
+channel_id = writer.register_channel(
+ schema_id=schema_id,
+ topic="state",
+ message_encoding="json",
+)
+
+for row in csv_reader:
+ data = {key: float(value) for key, value in zip(header, row)}
+ timestamp = round(float(row[0]) * 1e9)
+ writer.add_message(channel_id=channel_id, log_time=timestamp, data=json.dumps(data).encode(), publish_time=timestamp,)
+
+writer.finish()
diff --git a/tools/requirements.txt b/tools/requirements.txt
index 7b4771a..6b3338b 100644
--- a/tools/requirements.txt
+++ b/tools/requirements.txt
@@ -1,2 +1,3 @@
docopt
matplotlib
+mcap