Skip to content

Commit

Permalink
add version 5 rosbag2 writer
Browse files Browse the repository at this point in the history
  • Loading branch information
NerdToMars committed Dec 23, 2023
0 parents commit 4cef211
Show file tree
Hide file tree
Showing 11 changed files with 667 additions and 0 deletions.
84 changes: 84 additions & 0 deletions .github/workflows/CI.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
name: Rosbag2-rs CI

on:
push:
branches: [main]
pull_request:
branches: [main]

jobs:
run-all:
name: run all
runs-on: ubuntu-latest
needs:
- basics
- fmt
- clippy
- docs
steps:
- run: exit 0

# Basic actions that must pass before we kick off more expensive tests.
basics:
name: basic checks
runs-on: ubuntu-latest
needs:
- clippy
- fmt
- docs
steps:
- run: exit 0

clippy:
name: clippy
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- name: Install Rust ${{ env.rust_clippy }}
uses: dtolnay/rust-toolchain@stable
with:
toolchain: ${{ env.rust_clippy }}
components: clippy
- uses: Swatinem/rust-cache@v2
- name: cargo fmt
run: cargo fmt
- name: "clippy --all"
run: cargo clippy --all --tests --all-features --no-deps

docs:
name: docs
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- name: Install Rust ${{ env.rust_nightly }}
uses: dtolnay/rust-toolchain@stable
with:
toolchain: ${{ env.rust_nightly }}
- uses: Swatinem/rust-cache@v2
- name: "doc --lib --all-features"
run: |
cargo doc --lib --no-deps --all-features --document-private-items
test-full:
needs: basics
name: all tests
runs-on: ${{ matrix.os }}
strategy:
matrix:
os:
- windows-latest
- ubuntu-latest
- macos-latest
steps:
- uses: actions/checkout@v3
- name: Install Rust ${{ env.rust_stable }}
uses: dtolnay/rust-toolchain@stable
with:
toolchain: ${{ env.rust_stable }}

- uses: Swatinem/rust-cache@v2

- name: test
run: |
set -euxo pipefail
cargo test
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
/target
/Cargo.lock
6 changes: 6 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"rust-analyzer.cargo.extraArgs": [
"--profile",
"rust-analyzer"
]
}
21 changes: 21 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
[package]
name = "rosbag2-rs"
version = "0.1.0"
edition = "2021"
description = "Rosbag2 writer and more..."
authors = ["CT <ctian.huang@gmail.com>"]
license = "MIT AND Apache-2.0"
readme = "README.md"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
rusqlite = "0.30.0"
serde = { version = "1.0", features = ["derive"] }
serde_yaml = "0.9.25"
anyhow = "1.0.40"

[dev-dependencies]
tempfile = "3.8.1"

[profile.rust-analyzer]
inherits = "dev"
25 changes: 25 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# Rosbag2 Rust

## Introduction

Rosbag2 Rust is a Rust crate designed to provide functionalities for handling ROS2 bag files. This crate aims to enable efficient reading, writing, and manipulation of ROS bag files, making it easier for developers working with ROS to manage and analyze their data.

## Features

### Current Features

- [ ] Write ROS Bag Files (in progress)

### Planned Features

- Read ROS Bag Files
- Advanced message manipulation tools
- ...

## Contributing

We welcome contributions to Rosbags Rust! Whether it's reporting a bug, proposing a feature, or submitting a pull request, all contributions are appreciated.

## License

Rosbags Rust is open-source and is licensed under the MIT License.
18 changes: 18 additions & 0 deletions docker/Dockerfile.no_ros
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
FROM ubuntu:latest

# Update default packages
RUN apt-get update

# Get Ubuntu packages
RUN apt-get install -y \
build-essential \
curl \
libclang-dev

# Get Rust
RUN curl --proto '=https' --tlsv1.2 https://sh.rustup.rs -sSf | bash -s -- -y
RUN echo 'source $HOME/.cargo/env' >> $HOME/.bashrc

COPY . /r2r
RUN chmod +x /r2r/tests/test.bash
ENTRYPOINT [ "/r2r/tests/test.bash" ]
48 changes: 48 additions & 0 deletions examples/create_rosbag.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
use anyhow::Result;
use rosbag2_rs::Writer;
use std::path::Path;

fn main() -> Result<()> {
// Specify the path for the new ROS2 bag
let bag_path = Path::new("example_rosbag_by_rust");

// Initialize the Writer
let mut writer = Writer::new(bag_path);
writer.open()?;

// Define a standard ROS2 message type (adjust this according to actual ROS2 message types)
let topic = "example_topic";
let msgtype = "std_msgs/msg/Int32";

// Add a connection for this message type
// adjust qos profile according to actual ROS2 message types (https://docs.ros2.org/latest/api/rmw/structrmw__qos__profile__t.html)
const LATCH: &str = r#"- history: 3
depth: 0
reliability: 1
durability: 1
deadline:
sec: 2147483647
nsec: 4294967295
lifespan:
sec: 2147483647
nsec: 4294967295
liveliness: 1
liveliness_lease_duration:
sec: 2147483647
nsec: 4294967295
avoid_ros_namespace_conventions: false
"#;

let connection = writer.add_connection(topic, msgtype, "cdr", LATCH)?;

// Write some dummy messages
for i in 0..50 {
let dummy_data = [0, 1, 0, 1, 43, 42, 0, 0]; // dummy data 0x2a2b = (int32)10795
writer.write(&connection, 1_000_000_000 * i, &dummy_data)?;
}

writer.close()?;

println!("ROS2 bag created at {:?}", bag_path);
Ok(())
}
38 changes: 38 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
pub mod metadata;
pub use metadata::*;

pub mod writer;
pub use writer::*;

#[derive(Clone, Debug, PartialEq)]
pub struct TopicConnection {
pub id: i32,
pub topic: String,
pub msgtype: String,
// pub msgdef: String,
// pub digest: String,
pub msgcount: i32,
pub ext: ConnectionExt,
}

#[derive(Clone, Debug, PartialEq)]
pub struct ConnectionExt {
pub serialization_format: String,
pub offered_qos_profiles: String,
// Add other fields specific to ROS bag version 2
}

pub fn add(left: usize, right: usize) -> usize {
left + right
}

#[cfg(test)]
mod tests {
use super::*;

#[test]
fn it_works() {
let result = add(2, 2);
assert_eq!(result, 4);
}
}
58 changes: 58 additions & 0 deletions src/metadata.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
use serde::{Deserialize, Serialize};
use std::collections::HashMap;

#[derive(Serialize, Deserialize)]
pub struct StartingTime {
pub nanoseconds_since_epoch: i64,
}

#[derive(Serialize, Deserialize)]
pub struct BagDuration {
pub nanoseconds: i64,
}

#[derive(Serialize, Deserialize)]
pub struct TopicMetadata {
pub name: String,

#[serde(rename = "type")]
pub type_: String, // `type` is a reserved keyword in Rust
pub serialization_format: String,
pub offered_qos_profiles: String,
// pub type_description_hash: String // TODO: humble rosbag2 does not need this field
}

#[derive(Serialize, Deserialize)]
pub struct TopicWithMessageCount {
pub message_count: i32,
pub topic_metadata: TopicMetadata,
}

#[derive(Serialize, Deserialize)]
pub struct FileInformation {
pub path: String,
pub starting_time: StartingTime,
pub duration: BagDuration,
pub message_count: i32,
}

#[derive(Serialize, Deserialize)]
pub struct Metadata {
pub version: i32,
pub storage_identifier: String,
pub relative_file_paths: Vec<String>,
pub starting_time: StartingTime,
pub duration: BagDuration,
pub message_count: i32,
pub compression_format: String,
pub compression_mode: String,
pub topics_with_message_count: Vec<TopicWithMessageCount>,
pub files: Vec<FileInformation>,
pub custom_data: HashMap<String, String>,
pub ros_distro: String,
}

#[derive(Serialize, Deserialize)]
pub struct BagFileInfo {
pub rosbag2_bagfile_information: Metadata,
}
Loading

0 comments on commit 4cef211

Please sign in to comment.