Skip to content

Commit

Permalink
adding mcap to mat script for temp usage
Browse files Browse the repository at this point in the history
  • Loading branch information
RCMast3r committed Mar 18, 2024
1 parent 2b2a320 commit 0863d87
Show file tree
Hide file tree
Showing 6 changed files with 41 additions and 3 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,4 @@ result
result/
.env
.idea/
*.mat
1 change: 1 addition & 0 deletions default.nix
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ python311Packages.buildPythonApplication {
propagatedBuildInputs = [
python311Packages.cantools
python311Packages.systemd

python311Packages.websockets
python311Packages.pprintpp
python311Packages.can
Expand Down
1 change: 1 addition & 0 deletions flake.nix
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@
ht_can_pkg
cmake
can-utils
python311Packages.scipy
];
# Setting up the environment variables you need during
# development.
Expand Down
37 changes: 37 additions & 0 deletions mcap_to_mat.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import sys
from pathlib import Path
from mcap.reader import make_reader
from mcap_protobuf.reader import read_protobuf_messages
from mcap_protobuf.decoder import DecoderFactory
from scipy.io import savemat

def main():
with open(sys.argv[1], "rb") as f:
reader = make_reader(f, decoder_factories=[DecoderFactory()])
topics = []
for channel in reader.get_summary().channels:
topics.append(reader.get_summary().channels[channel].topic)

msgs_lists = []
for topic in topics:
msgs = []
for schema, channel, message, proto_msg in reader.iter_decoded_messages(topics=[topic]):


res = [f.name for f in proto_msg.DESCRIPTOR.fields]
msg = {}
msg["recvd_time"] = message.log_time
for name in res:
msg[name] = getattr(proto_msg, name)
msgs.append(msg)
msgs_lists.append(msgs)
mdict= {}
for index, topic in enumerate(topics):
mdict[topic] = msgs_lists[index]
out_name = Path(sys.argv[1]).stem
savemat(out_name+".mat", mdict, long_field_names=True)



if __name__ == "__main__":
main()
2 changes: 0 additions & 2 deletions py_data_acq/py_data_acq/mcap_writer/writer.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,7 @@ def __aenter__(self):
async def __aexit__(self, exc_type: Any, exc_val: Any, traceback: Any):
super().finish()
self.writing_file.close()

async def write_msg(self, msg):

super().write_message(topic=msg.DESCRIPTOR.name+"_data", message=msg, log_time=int(time.time_ns()), publish_time=int(time.time_ns()))
self.writing_file.flush()
return True
Expand Down
2 changes: 1 addition & 1 deletion test_mcap.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
from mcap_protobuf.reader import read_protobuf_messages

def main():
for msg in read_protobuf_messages("/home/ben/hytech_mcaps/recordings/03_17_2024_01_34_00_fixed.mcap", log_time_order=False):
for msg in read_protobuf_messages(sys.argv[1], log_time_order=False):
print(f"{msg.topic}: {msg.proto_msg}")

if __name__ == "__main__":
Expand Down

0 comments on commit 0863d87

Please sign in to comment.