Skip to content

Commit

Permalink
Add tools for conversion svg logs to mcap
Browse files Browse the repository at this point in the history
  • Loading branch information
okalachev committed Jun 1, 2024
1 parent 63d602d commit 72b2cf4
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 0 deletions.
12 changes: 12 additions & 0 deletions .github/workflows/tools.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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 changes: 46 additions & 0 deletions tools/csv_to_mcap.py
Original file line number Diff line number Diff line change
@@ -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()
1 change: 1 addition & 0 deletions tools/requirements.txt
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
docopt
matplotlib
mcap

0 comments on commit 72b2cf4

Please sign in to comment.