Skip to content

Commit 329415b

Browse files
committed
re_ros_msg
1 parent 65cab24 commit 329415b

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
@@ -233,6 +233,7 @@ Update instructions:
233233
| re_mcap | Convert MCAP into Rerun-compatible data. |
234234
| re_memory | Run-time memory tracking and profiling. |
235235
| re_perf_telemetry | In and out of process performance profiling utilities for Rerun & Redap |
236+
| re_ros_msg | Parsing and deserializing ROS messages |
236237
| re_smart_channel | A channel that keeps track of latency and queue length. |
237238
| re_span | An integer range that always has a non-negative length |
238239
| re_string_interner | Yet another string interning library |

Cargo.lock

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8988,6 +8988,7 @@ dependencies = [
89888988
"re_chunk",
89898989
"re_log",
89908990
"re_log_types",
8991+
"re_ros_msg",
89918992
"re_tracing",
89928993
"re_types",
89938994
"serde",
@@ -9300,6 +9301,23 @@ dependencies = [
93009301
"winit",
93019302
]
93029303

9304+
[[package]]
9305+
name = "re_ros_msg"
9306+
version = "0.26.0-alpha.1+dev"
9307+
dependencies = [
9308+
"ahash",
9309+
"anyhow",
9310+
"byteorder",
9311+
"cdr-encoding",
9312+
"insta",
9313+
"re_format_arrow",
9314+
"re_log",
9315+
"re_tracing",
9316+
"serde",
9317+
"serde_bytes",
9318+
"thiserror 1.0.69",
9319+
]
9320+
93039321
[[package]]
93049322
name = "re_sdk"
93059323
version = "0.26.0-alpha.1+dev"

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,7 @@ re_log = { path = "crates/utils/re_log", version = "=0.26.0-alpha.1", default-fe
106106
re_mcap = { path = "crates/utils/re_mcap", version = "=0.26.0-alpha.1", default-features = false }
107107
re_memory = { path = "crates/utils/re_memory", version = "=0.26.0-alpha.1", default-features = false }
108108
re_perf_telemetry = { path = "crates/utils/re_perf_telemetry", version = "=0.26.0-alpha.1", default-features = false }
109+
re_ros_msg = { path = "crates/utils/re_ros_msg", version = "=0.26.0-alpha.1", default-features = false }
109110
re_smart_channel = { path = "crates/utils/re_smart_channel", version = "=0.26.0-alpha.1", default-features = false }
110111
re_span = { path = "crates/utils/re_span", version = "=0.26.0-alpha.1", default-features = false }
111112
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)