From 0863d8797206f28b2fb5b76ad5002acc24179ff7 Mon Sep 17 00:00:00 2001 From: Ben Hall Date: Sun, 17 Mar 2024 20:37:58 -0400 Subject: [PATCH] adding mcap to mat script for temp usage --- .gitignore | 1 + default.nix | 1 + flake.nix | 1 + mcap_to_mat.py | 37 +++++++++++++++++++ py_data_acq/py_data_acq/mcap_writer/writer.py | 2 - test_mcap.py | 2 +- 6 files changed, 41 insertions(+), 3 deletions(-) create mode 100644 mcap_to_mat.py diff --git a/.gitignore b/.gitignore index a770904..ebe6bba 100644 --- a/.gitignore +++ b/.gitignore @@ -5,3 +5,4 @@ result result/ .env .idea/ +*.mat \ No newline at end of file diff --git a/default.nix b/default.nix index 0dd5e4d..d736b11 100644 --- a/default.nix +++ b/default.nix @@ -15,6 +15,7 @@ python311Packages.buildPythonApplication { propagatedBuildInputs = [ python311Packages.cantools python311Packages.systemd + python311Packages.websockets python311Packages.pprintpp python311Packages.can diff --git a/flake.nix b/flake.nix index a82cb94..8a222b9 100644 --- a/flake.nix +++ b/flake.nix @@ -93,6 +93,7 @@ ht_can_pkg cmake can-utils + python311Packages.scipy ]; # Setting up the environment variables you need during # development. diff --git a/mcap_to_mat.py b/mcap_to_mat.py new file mode 100644 index 0000000..d8db78e --- /dev/null +++ b/mcap_to_mat.py @@ -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() \ No newline at end of file diff --git a/py_data_acq/py_data_acq/mcap_writer/writer.py b/py_data_acq/py_data_acq/mcap_writer/writer.py index 7842da2..b76c886 100644 --- a/py_data_acq/py_data_acq/mcap_writer/writer.py +++ b/py_data_acq/py_data_acq/mcap_writer/writer.py @@ -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 diff --git a/test_mcap.py b/test_mcap.py index 4ac5f66..200b578 100644 --- a/test_mcap.py +++ b/test_mcap.py @@ -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__":