Rust tutorial
First, make a new project and add autd3
and autd3-link-soem
libraries as dependencies.
cargo new --bin autd3-sample
cd autd3-sample
cargo add autd3@29.0.0-rc.16
cargo add autd3-link-soem@29.0.0-rc.16
cargo add tokio --features full
Next, edit src/main.rs
file as follows.
This is the source code for generating a focus with AM modulation.
use autd3::prelude::*;
use autd3_link_soem::{Status, SOEM};
fn main() -> Result<(), Box<dyn std::error::Error>> {
// Make a controller to control AUTD
// Configure the devices
// The argument of AUTD3::new is position
// Here, the device is placed at the origin
let mut autd = Controller::builder([AUTD3::new(Point3::origin())])
// Open controller with SOEM link
// The callback specified by with_err_handler is called when error occurs
.open(SOEM::builder().with_err_handler(|slave, status| {
eprintln!("slave [{}]: {}", slave, status);
if status == Status::Lost {
// You can also wait for the link to recover, without exitting the process
std::process::exit(-1);
}
}))?;
// Check firmware version
// This code assumes that the version is v10.0.1
autd.firmware_version()?.iter().for_each(|firm_info| {
println!("{}", firm_info);
});
// Enable silencer
// Note that this is enabled by default, so it is not actually necessary
// To disable, send Silencer::disable()
autd.send(Silencer::default())?;
// A focus at 150mm directly above the center of the device
let center = autd.center() + Vector3::new(0., 0., 150.0 * mm);
let g = Focus::new(center);
// 150Hz sine wave modulation
let m = Sine::new(150 * Hz);
// Send data
autd.send((m, g))?;
println!("press enter to quit...");
let mut _s = String::new();
std::io::stdin().read_line(&mut _s)?;
// Close controller
autd.close()?;
Ok(())
}
Then, run the program.
cargo run --release
For Linux, macOS users
You may need to run with administrator privileges when using SOEM on Linux or macOS.
cargo build --release && sudo ./target/release/autd3_sample