Skip to content

Instantly share code, notes, and snippets.

@patrickelectric
Created May 6, 2025 20:59
Show Gist options
  • Save patrickelectric/4c22bb04f2875b3dfaa9ef087f9a2b51 to your computer and use it in GitHub Desktop.
Save patrickelectric/4c22bb04f2875b3dfaa9ef087f9a2b51 to your computer and use it in GitHub Desktop.
Failed example of sending data from Zenoh to ROS2DDS
use byteorder::LittleEndian;
use cdr_encoding::from_bytes;
use serde::{Deserialize, Serialize};
use zenoh;
#[derive(Serialize, Deserialize, Debug, Default)]
struct Quaternion {
x: f64,
y: f64,
z: f64,
w: f64,
}
#[derive(Serialize, Deserialize, Debug, Default)]
struct Vector3 {
x: f64,
y: f64,
z: f64,
}
#[derive(Serialize, Deserialize, Debug, Default)]
struct Time {
sec: u32,
nanosec: u32,
}
#[derive(Serialize, Deserialize, Debug, Default)]
struct Header {
stamp: Time,
frame_id: String,
}
// From: https://docs.ros2.org/foxy/api/sensor_msgs/msg/Imu.html
#[derive(Serialize, Deserialize, Debug, Default)]
struct Imu {
header: Header,
orientation: Quaternion,
orientation_covariance: [f64; 9],
angular_velocity: Vector3,
angular_velocity_covariance: [f64; 9],
linear_acceleration: Vector3,
linear_acceleration_covariance: [f64; 9],
}
#[tokio::main]
async fn main() -> zenoh::Result<()> {
let session = zenoh::open(zenoh::Config::default()).await?;
let lv_ke = format!(
"@/{}/@ros2_lv/MP/patrick§imu§data_raw/sensor_msgs::msg::Imu/:::",
session.zid()
);
let sub = session.declare_subscriber("mavros/imu/data_raw").await?;
while let Ok(sample) = sub.recv_async().await {
let raw = sample.payload().to_bytes();
session.put(&lv_ke, raw.clone()).await?;
// Remove the CDR header, 2 bytes for endianness, 2 bytes for padding / reserved
// Check 10.2: https://www.omg.org/spec/DDSI-RTPS/2.3/PDF
let payload = &raw[4..];
let (msg, _) = from_bytes::<Imu, LittleEndian>(payload).unwrap();
println!("{msg:#?}");
}
Ok(())
}
@JEnoch
Copy link

JEnoch commented May 7, 2025

Several errors to address:

  • L.51: When declaring the equivalent of a ROS Subscriber, the liveliness token should contain /MS/ (for Msg Subscriber), not /MP/

  • L.55: At the same time than you declare the Zenoh Subscriber, you should announce to the zenoh-bridge-ros2dds that it's a ROS Subscriber declaring the liveliness token as such:

    let token = session.liveliness().declare_token(lv_ke).await?;
  • L.58: Calling session.put(&lv_ke, raw.clone()) is useless.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment