Keyboard shortcuts

Press ← or → to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Connecting your brick

Every program starts by opening a Brick. How you open it depends on the cable, or lack of one.

LinkCodeFeature
USB, first brick foundBrick::usb_first().await?usb (default)
USB, every brickBrick::usb_all().await?usb (default)
Bluetooth (Linux)Brick::bluetooth(address).await?bluetooth
Bluetooth serial port (any OS)Brick::serial("/dev/rfcomm0").await?serial
Whatever is availablenxt_rs::connect_any().await?—
Simulated brickBrick::mock()—
use nxt_rs::prelude::*;

fn main() -> nxt_rs::Result<()> {
let brick = Brick::usb_first().block()?;

let info = brick.info().block()?;
println!("name:      {}", info.name);
println!("bluetooth: {}", info.bluetooth_address);
println!("battery:   {}", brick.battery().block()?);
println!("firmware:  {}", brick.firmware_version().block()?.firmware);
Ok(()) }

USB is the fastest and simplest option. A command round trip takes a few milliseconds over USB and about 60 ms over Bluetooth. Use Bluetooth when the robot has to move around freely.

The Connecting chapters cover each link in detail, including Bluetooth pairing.

One connection, many handles

Brick is cheap to clone. All clones share the same connection, so you can hand them to different tasks or threads. Requests from different clones never mix up their replies.

nxt_rs::doc_example(|brick| async move {
let for_logging = brick.clone();
assert!(brick.same_brick(&for_logging));
Ok(()) });

Next: Your first program.