Add tools for conversion svg logs to mcap

This commit is contained in:
Oleg Kalachev 2024-06-02 01:45:49 +03:00
parent 63d602dd7a
commit 72b2cf49d5
3 changed files with 59 additions and 0 deletions

View File

@ -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

46
tools/csv_to_mcap.py Executable file
View File

@ -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()

View File

@ -1,2 +1,3 @@
docopt
matplotlib
mcap