Skip to content

Commit 363c187

Browse files
committed
re_ros_msg
1 parent 8f51633 commit 363c187

File tree

12 files changed

+272
-196
lines changed

12 files changed

+272
-196
lines changed

ARCHITECTURE.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -232,6 +232,7 @@ Update instructions:
232232
| re_mcap | Convert MCAP into Rerun-compatible data. |
233233
| re_memory | Run-time memory tracking and profiling. |
234234
| re_perf_telemetry | In and out of process performance profiling utilities for Rerun & Redap |
235+
| re_ros_msg | Parsing and deserializing ROS messages |
235236
| re_smart_channel | A channel that keeps track of latency and queue length. |
236237
| re_span | An integer range that always has a non-negative length |
237238
| re_string_interner | Yet another string interning library |

Cargo.lock

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8141,6 +8141,7 @@ dependencies = [
81418141
"re_format_arrow",
81428142
"re_log",
81438143
"re_log_types",
8144+
"re_ros_msg",
81448145
"re_tracing",
81458146
"re_types",
81468147
"serde",
@@ -8451,6 +8452,23 @@ dependencies = [
84518452
"winit",
84528453
]
84538454

8455+
[[package]]
8456+
name = "re_ros_msg"
8457+
version = "0.26.0-alpha.1+dev"
8458+
dependencies = [
8459+
"ahash",
8460+
"anyhow",
8461+
"byteorder",
8462+
"cdr-encoding",
8463+
"insta",
8464+
"re_format_arrow",
8465+
"re_log",
8466+
"re_tracing",
8467+
"serde",
8468+
"serde_bytes",
8469+
"thiserror 1.0.69",
8470+
]
8471+
84548472
[[package]]
84558473
name = "re_sdk"
84568474
version = "0.26.0-alpha.1+dev"

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,7 @@ re_log = { path = "crates/utils/re_log", version = "=0.26.0-alpha.1", default-fe
104104
re_mcap = { path = "crates/utils/re_mcap", version = "=0.26.0-alpha.1", default-features = false }
105105
re_memory = { path = "crates/utils/re_memory", version = "=0.26.0-alpha.1", default-features = false }
106106
re_perf_telemetry = { path = "crates/utils/re_perf_telemetry", version = "=0.26.0-alpha.1", default-features = false }
107+
re_ros_msg = { path = "crates/utils/re_ros_msg", version = "=0.26.0-alpha.1", default-features = false }
107108
re_smart_channel = { path = "crates/utils/re_smart_channel", version = "=0.26.0-alpha.1", default-features = false }
108109
re_span = { path = "crates/utils/re_span", version = "=0.26.0-alpha.1", default-features = false }
109110
re_string_interner = { path = "crates/utils/re_string_interner", version = "=0.26.0-alpha.1", default-features = false }

crates/utils/re_mcap/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ workspace = true
1818
re_chunk.workspace = true
1919
re_log.workspace = true
2020
re_log_types.workspace = true
21+
re_ros_msg.workspace = true
2122
re_tracing.workspace = true
2223
re_types = { workspace = true, features = ["ecolor", "glam", "image", "video"] }
2324

crates/utils/re_mcap/src/layers/ros2_reflection.rs

Lines changed: 33 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
use anyhow::Context as _;
12
use arrow::{
23
array::{
34
ArrayBuilder, ArrowPrimitiveType, BinaryBuilder, BooleanBuilder, FixedSizeListBuilder,
@@ -10,18 +11,43 @@ use arrow::{
1011
Int64Type, UInt8Type, UInt16Type, UInt32Type, UInt64Type,
1112
},
1213
};
14+
use cdr_encoding::CdrDeserializer;
1315
use re_chunk::{Chunk, ChunkId};
14-
use re_types::{ComponentDescriptor, reflection::ComponentDescriptorExt as _};
15-
16-
use crate::parsers::ros2msg::reflection::{
16+
use re_ros_msg::{
1717
MessageSchema,
18-
deserialize::primitive_array::PrimitiveArray,
19-
deserialize::{Value, decode_bytes},
18+
deserialize::{MapResolver, MessageSeed, Value, primitive_array::PrimitiveArray},
2019
message_spec::{ArraySize, BuiltInType, ComplexType, MessageSpecification, Type},
2120
};
22-
use crate::parsers::{MessageParser, ParserContext};
21+
use re_types::{ComponentDescriptor, reflection::ComponentDescriptorExt as _};
22+
use serde::de::DeserializeSeed as _;
23+
24+
use crate::parsers::{MessageParser, ParserContext, dds};
2325
use crate::{Error, LayerIdentifier, MessageLayer};
2426

27+
pub fn decode_bytes(top: &MessageSchema, buf: &[u8]) -> anyhow::Result<Value> {
28+
// 4-byte encapsulation header
29+
if buf.len() < 4 {
30+
anyhow::bail!("short encapsulation");
31+
}
32+
33+
let representation_identifier = dds::RepresentationIdentifier::from_bytes([buf[0], buf[1]])
34+
.with_context(|| "failed to parse CDR representation identifier")?;
35+
36+
let resolver = MapResolver::new(top.dependencies.iter().map(|dep| (dep.name.clone(), dep)));
37+
38+
let seed = MessageSeed::new(&top.spec, &resolver);
39+
40+
if representation_identifier.is_big_endian() {
41+
let mut de = CdrDeserializer::<byteorder::BigEndian>::new(&buf[4..]);
42+
seed.deserialize(&mut de)
43+
.with_context(|| "failed to deserialize CDR message")
44+
} else {
45+
let mut de = CdrDeserializer::<byteorder::LittleEndian>::new(&buf[4..]);
46+
seed.deserialize(&mut de)
47+
.with_context(|| "failed to deserialize CDR message")
48+
}
49+
}
50+
2551
struct Ros2ReflectionMessageParser {
2652
num_rows: usize,
2753
message_schema: MessageSchema,
@@ -260,7 +286,7 @@ fn append_value(
260286

261287
struct_builder.append(true);
262288
}
263-
Value::Array(vec) | Value::Seq(vec) => {
289+
Value::Array(vec) | Value::Sequence(vec) => {
264290
let list_builder = downcast_err::<ListBuilder<Box<dyn ArrayBuilder>>>(builder)?;
265291

266292
for val in vec {

crates/utils/re_mcap/src/parsers/ros2msg/mod.rs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,6 @@ pub mod scalar_parser;
77
pub mod sensor_msgs;
88
pub mod std_msgs;
99

10-
pub mod reflection;
11-
1210
/// Trait for ROS2 message parsers that can be constructed with just a row count.
1311
pub trait Ros2MessageParser: MessageParser {
1412
/// Create a new parser instance.

crates/utils/re_ros_msg/Cargo.toml

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
[package]
2+
name = "re_ros_msg"
3+
description = "Parse and decode ROS messages"
4+
authors.workspace = true
5+
edition.workspace = true
6+
homepage.workspace = true
7+
include.workspace = true
8+
license.workspace = true
9+
publish = true
10+
repository.workspace = true
11+
rust-version.workspace = true
12+
version.workspace = true
13+
14+
[lints]
15+
workspace = true
16+
17+
[dependencies]
18+
re_log.workspace = true
19+
re_tracing.workspace = true
20+
21+
ahash.workspace = true
22+
anyhow.workspace = true
23+
byteorder.workspace = true
24+
cdr-encoding.workspace = true
25+
serde.workspace = true
26+
serde_bytes.workspace = true
27+
thiserror.workspace = true
28+
29+
[dev-dependencies]
30+
re_format_arrow.workspace = true
31+
32+
insta = { workspace = true, features = ["filters", "redactions"] }

0 commit comments

Comments
 (0)