-
Notifications
You must be signed in to change notification settings - Fork 362
[vibed experiment] Add Rust language interop example #1622
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
leshy
wants to merge
8
commits into
dev
Choose a base branch
from
feat/rust-interop-example
base: dev
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
a2e012f
feat(examples): add Rust language interop example
leshy 43e2019
feat(examples): add .gitignore for Rust build artifacts
leshy ebfc8a8
refactor(examples): use dimos-lcm crate instead of inline transport
leshy 1f2d26e
fix(rust-example): pin git deps to specific rev and fix busy-poll sle…
3124367
test(interop): add non-interactive language interop tests
285739e
CI code cleanup
leshy 5ccfb50
test(interop): add Lua language interop test
20071cb
CI code cleanup
leshy File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| /target/ |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,10 @@ | ||
| [package] | ||
| name = "robot-control" | ||
| version = "0.1.0" | ||
| edition = "2021" | ||
|
|
||
| [dependencies] | ||
| # TODO: switch to version once dimos-lcm#20 merges | ||
| dimos-lcm = { git = "https://github.com/dimensionalOS/dimos-lcm.git", rev = "474b25d0f9f88b8430753df2453fd2c988a514d1" } | ||
| # TODO: switch to version once dimos-lcm#20 merges | ||
| lcm-msgs = { git = "https://github.com/dimensionalOS/dimos-lcm.git", rev = "474b25d0f9f88b8430753df2453fd2c988a514d1" } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,14 @@ | ||
| # Rust Robot Control Example | ||
|
|
||
| Subscribes to `/odom` and publishes velocity commands to `/cmd_vel` via LCM UDP multicast. | ||
|
|
||
| ## Build & Run | ||
|
|
||
| ```bash | ||
| cargo run | ||
| ``` | ||
|
|
||
| ## Dependencies | ||
|
|
||
| - [Rust toolchain](https://rustup.rs/) | ||
| - Message types fetched automatically from [dimos-lcm](https://github.com/dimensionalOS/dimos-lcm) (`rust-codegen` branch) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,78 @@ | ||
| // Rust robot control example | ||
| // Subscribes to robot pose and publishes twist commands via LCM | ||
|
|
||
| use dimos_lcm::Lcm; | ||
| use lcm_msgs::geometry_msgs::{PoseStamped, Twist, Vector3}; | ||
| use std::thread; | ||
| use std::time::{Duration, Instant}; | ||
|
|
||
| const ODOM_CHANNEL: &str = "/odom#geometry_msgs.PoseStamped"; | ||
| const CMD_VEL_CHANNEL: &str = "/cmd_vel#geometry_msgs.Twist"; | ||
| const PUBLISH_INTERVAL: Duration = Duration::from_millis(100); // 10 Hz | ||
|
|
||
| fn main() { | ||
| let lcm = Lcm::new().expect("Failed to create LCM transport"); | ||
|
|
||
| println!("Robot control started"); | ||
| println!("Subscribing to /odom, publishing to /cmd_vel"); | ||
| println!("Press Ctrl+C to stop.\n"); | ||
|
|
||
| let mut t: f64 = 0.0; | ||
| let mut next_publish = Instant::now(); | ||
|
|
||
| loop { | ||
| // Poll for incoming messages | ||
| match lcm.try_recv() { | ||
| Ok(Some(msg)) if msg.channel == ODOM_CHANNEL => { | ||
| match PoseStamped::decode(&msg.data) { | ||
| Ok(pose) => { | ||
| let pos = &pose.pose.position; | ||
| let ori = &pose.pose.orientation; | ||
| println!( | ||
| "[pose] x={:.2} y={:.2} z={:.2} | qw={:.2}", | ||
| pos.x, pos.y, pos.z, ori.w | ||
| ); | ||
| } | ||
| Err(e) => eprintln!("[pose] decode error: {e}"), | ||
| } | ||
| } | ||
| Ok(Some(_)) => {} // ignore other channels | ||
| Ok(None) => {} | ||
| Err(e) => eprintln!("recv error: {e}"), | ||
| } | ||
|
|
||
| // Publish twist at 10 Hz | ||
| let now = Instant::now(); | ||
| if now >= next_publish { | ||
| let twist = Twist { | ||
| linear: Vector3 { | ||
| x: 0.5, | ||
| y: 0.0, | ||
| z: 0.0, | ||
| }, | ||
| angular: Vector3 { | ||
| x: 0.0, | ||
| y: 0.0, | ||
| z: t.sin() * 0.3, | ||
| }, | ||
| }; | ||
|
|
||
| let data = twist.encode(); | ||
| if let Err(e) = lcm.publish(CMD_VEL_CHANNEL, &data) { | ||
| eprintln!("[twist] publish error: {e}"); | ||
| } else { | ||
| println!( | ||
| "[twist] linear={:.2} angular={:.2}", | ||
| twist.linear.x, twist.angular.z | ||
| ); | ||
| } | ||
|
|
||
| t += 0.1; | ||
| next_publish = now + PUBLISH_INTERVAL; | ||
| } | ||
|
|
||
| // Sleep until next publish deadline (capped at 10ms) to avoid busy-spinning | ||
| let sleep_dur = next_publish.saturating_duration_since(Instant::now()).min(Duration::from_millis(10)); | ||
| thread::sleep(sleep_dur); | ||
| } | ||
| } | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please delete. |
Empty file.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,146 @@ | ||
| # Copyright 2026 Dimensional Inc. | ||
| # | ||
| # Licensed under the Apache License, Version 2.0 (the "License"); | ||
| # you may not use this file except in compliance with the License. | ||
| # You may obtain a copy of the License at | ||
| # | ||
| # http://www.apache.org/licenses/LICENSE-2.0 | ||
| # | ||
| # Unless required by applicable law or agreed to in writing, software | ||
| # distributed under the License is distributed on an "AS IS" BASIS, | ||
| # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| # See the License for the specific language governing permissions and | ||
| # limitations under the License. | ||
|
|
||
| """Fixtures for language-interop integration tests.""" | ||
|
|
||
| from __future__ import annotations | ||
|
|
||
| from collections.abc import Generator | ||
| import os | ||
| from pathlib import Path | ||
| import signal | ||
| import subprocess | ||
| import sys | ||
| import time | ||
|
|
||
| import pytest | ||
|
|
||
| EXAMPLES_DIR = Path(__file__).resolve().parent.parent # language-interop/ | ||
| SIMPLEROBOT_DIR = EXAMPLES_DIR.parent / "simplerobot" | ||
| RUST_DIR = EXAMPLES_DIR / "rust" | ||
| TS_DIR = EXAMPLES_DIR / "ts" | ||
| CPP_DIR = EXAMPLES_DIR / "cpp" | ||
| LUA_DIR = EXAMPLES_DIR / "lua" | ||
|
|
||
|
|
||
| def pytest_configure(config: pytest.Config) -> None: | ||
| config.addinivalue_line("markers", "interop: cross-language interop integration test") | ||
|
|
||
|
|
||
| @pytest.fixture(scope="module") | ||
| def simplerobot() -> Generator[subprocess.Popen[str], None, None]: | ||
| """Start simplerobot.py --headless as a subprocess, tear down after tests.""" | ||
| proc = subprocess.Popen( | ||
| [sys.executable, str(SIMPLEROBOT_DIR / "simplerobot.py"), "--headless"], | ||
| stdout=subprocess.DEVNULL, | ||
| stderr=subprocess.DEVNULL, | ||
| text=True, | ||
| cwd=str(SIMPLEROBOT_DIR), | ||
| ) | ||
| # Give it time to start publishing | ||
| time.sleep(2) | ||
| yield proc | ||
| proc.send_signal(signal.SIGTERM) | ||
| try: | ||
| proc.wait(timeout=5) | ||
| except subprocess.TimeoutExpired: | ||
| proc.kill() | ||
| proc.wait(timeout=5) | ||
|
|
||
|
|
||
| @pytest.fixture(scope="module") | ||
| def rust_binary() -> Path: | ||
| """Build the Rust interop binary and return its path.""" | ||
| cargo_toml = RUST_DIR / "Cargo.toml" | ||
| if not cargo_toml.exists(): | ||
| pytest.skip("Rust example not found") | ||
|
|
||
| env = os.environ.copy() | ||
| cargo_home = Path.home() / ".cargo" / "bin" | ||
| if cargo_home.is_dir(): | ||
| env["PATH"] = str(cargo_home) + os.pathsep + env.get("PATH", "") | ||
|
|
||
| result = subprocess.run( | ||
| ["cargo", "build", "--release"], | ||
| cwd=str(RUST_DIR), | ||
| capture_output=True, | ||
| text=True, | ||
| timeout=120, | ||
| env=env, | ||
| ) | ||
| if result.returncode != 0: | ||
| pytest.skip(f"cargo build failed: {result.stderr}") | ||
|
|
||
| # Binary name from Cargo.toml [package] name = "robot-control" | ||
| binary = RUST_DIR / "target" / "release" / "robot-control" | ||
| if not binary.exists(): | ||
| # Try to find any binary in release | ||
| release_dir = RUST_DIR / "target" / "release" | ||
| found = next( | ||
| ( | ||
| f | ||
| for f in release_dir.iterdir() | ||
| if f.is_file() and os.access(f, os.X_OK) and not f.suffix and ".so" not in f.name | ||
| ), | ||
| None, | ||
| ) | ||
| if found is None: | ||
| pytest.skip("No binary found after cargo build") | ||
| binary = found | ||
|
|
||
| return binary | ||
|
|
||
|
|
||
| @pytest.fixture(scope="module") | ||
| def cpp_binary() -> Path: | ||
| """Build the C++ interop binary and return its path.""" | ||
| cmakelists = CPP_DIR / "CMakeLists.txt" | ||
| if not cmakelists.exists(): | ||
| pytest.skip("C++ example not found") | ||
|
|
||
| build_dir = CPP_DIR / "build" | ||
| build_dir.mkdir(exist_ok=True) | ||
|
|
||
| cmake_result = subprocess.run( | ||
| ["cmake", ".."], | ||
| cwd=str(build_dir), | ||
| capture_output=True, | ||
| text=True, | ||
| timeout=30, | ||
| ) | ||
| if cmake_result.returncode != 0: | ||
| pytest.skip(f"cmake failed: {cmake_result.stderr}") | ||
|
|
||
| make_result = subprocess.run( | ||
| ["make", "-j4"], | ||
| cwd=str(build_dir), | ||
| capture_output=True, | ||
| text=True, | ||
| timeout=60, | ||
| ) | ||
| if make_result.returncode != 0: | ||
| pytest.skip(f"make failed: {make_result.stderr}") | ||
|
|
||
| # Find the built binary | ||
| binary = next( | ||
| ( | ||
| f | ||
| for f in build_dir.iterdir() | ||
| if f.is_file() and os.access(f, os.X_OK) and not f.suffix and ".so" not in f.name | ||
| ), | ||
| None, | ||
| ) | ||
| if binary is None: | ||
| pytest.skip("No C++ binary found after build") | ||
| return binary |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.