Skip to content

Single Device

This section explains how to drive a single device.

The following is an example that generates a focal point 150 mm directly above the center of the array with a single AUTD3 device and applies a 200 Hz sine-wave AM.

First, create an appropriate project and add the autd3-rs library as a dependency. For focus computation and AM modulation, also add autd3-rs-pattern and autd3-rs-modulation. autd3-rs runs on the tokio runtime, so also add tokio.

cargo new --bin autd3-sample && cd autd3-sample
cargo add autd3-rs
cargo add autd3-rs-pattern
cargo add autd3-rs-modulation
cargo add tokio --features full

Next, edit the src/main.rs file to look like the following.

use autd3_rs::commands::{Clear, Modulation, Pattern, SetSilencer, Synchronize};
use autd3_rs::geometry::{Autd3, Geometry, offset};
use autd3_rs::units::{Hz, m, mm, s};
use autd3_rs::value::{Intensity, SamplingConfig};
use autd3_rs::Client;
use autd3_rs::TransportOption;
#[tokio::main(flavor = "multi_thread")]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
// Define a geometry consisting of a single AUTD3 device.
let geometry = Geometry::new(vec![Autd3::default()]);
// Open the client.
let mut client = Client::open(&geometry, &TransportOption::default()).await?;
client.send(Clear).await?;
client.send(Synchronize).await?;
client.send(SetSilencer::default()).await?;
// Generate a focus 150 mm above the array center.
let target = geometry.center() + offset(0.0 * mm, 0.0 * mm, 150.0 * mm);
let wavelength = autd3_rs_pattern::wavelength(340.0 * m / s);
let mut phases = geometry.phase_buffer();
autd3_rs_pattern::focus(
&geometry,
target,
wavelength,
&mut phases,
);
client.send(Pattern::new(&phases, Intensity::MAX)).await?;
// Apply a 200 Hz sine-wave AM.
let sampling_config = SamplingConfig::FREQ_4K;
let mut modulation = autd3_rs_modulation::modulation_buffer();
autd3_rs_modulation::sine(
200 * Hz,
&autd3_rs_modulation::SineOption::default(),
sampling_config,
&mut modulation,
)?;
client.send(Modulation::new(sampling_config, &modulation)).await?;
tokio::signal::ctrl_c().await?;
client.send(Pattern::new(&geometry.phase_buffer(), Intensity::MIN)).await?;
Ok(())
}

Then, run it.

cargo run --release

Press Ctrl+C to exit.