Skip to main content

Rust

Connect directly to Foxglove or play back local data recorded with the Rust SDK.

Install the SDK

Install foxglove from crates.io: https://crates.io/crates/foxglove

cargo add foxglove

If you want to try Foxglove with a new project, you can use these steps:

cargo new example --bin
cd example
cargo add foxglove

Live data

Log messages from Rust

Add the following to main.rs:

use std::{thread, time};

#[derive(foxglove::Encode)]
struct Message {
elapsed: f64,
}

fn main() {
foxglove::WebSocketServer::new()
.start_blocking()
.expect("Server failed to start");

let start = time::SystemTime::now();
loop {
foxglove::log!(
"/hello",
Message {
elapsed: start
.elapsed()
.expect("clock failed to elapse")
.as_secs_f64()
}
);
thread::sleep(time::Duration::from_millis(30));
}
}

Run the example

From the project directory:

cargo run

Connect

In Foxglove, select "Open connection" from the dashboard or left-hand menu.

Select open connection

Select "Foxglove WebSocket" in the "Open a new connection" dialog, then click "Open" to accept the default connection string:

Foxglove WebSocket dialog

Local data

Recording data to a file

The SDK can also log data to disk. Let's augment the example above to also produce an MCAP file which can later be opened in Foxglove:

use std::{thread, time};

#[derive(foxglove::Encode)]
struct Message {
elapsed: f64,
}

fn main() {
foxglove::WebSocketServer::new()
.start_blocking()
.expect("Server failed to start");

// Keep a reference to the writer. It'll automatically flush and close when it's dropped,
// or we could call `.close()` to close it manually.
let _ = foxglove::McapWriter::new()
.create_new_buffered_file("example.mcap")
.expect("Failed to create writer");

let start = time::SystemTime::now();
loop {
foxglove::log!(
"/hello",
Message {
elapsed: start
.elapsed()
.expect("clock failed to elapse")
.as_secs_f64()
}
);
thread::sleep(time::Duration::from_millis(30));
}
}

Viewing data from a file

Open your recording(s) directly from your computer by:

  • Dragging and dropping them into Foxglove
  • Using Cmd/Ctrl + o
  • Clicking Open local file(s) in the app

Local file dialog

note

When opening multiple files, Foxglove will display the data as a single merged timeline. The files must be of the same format.

Explore your data

Your data is now available for exploring in Foxglove. Add some panels to begin visualizing what your robot is seeing and producing. Not sure where to start? Try adding a Raw Messages panel, Plot panel, or 3D panel.