Asynchronous API

The Rust library supports asynchronous processing.

NOTE: This feature is not available in the C++, C#, and Python libraries.

Setup

To perform asynchronous processing, you need to enable the async feature of the autd3 crate.

cargo add autd3 --features "async"

Also, you may need to enable the async feature for each Link.

  • SOEM

    cargo add autd3-link-soem --features "async"
    
  • TwinCAT

    cargo add autd3-link-twincat --features "async"
    
  • RemoteTwinCAT

    cargo add autd3-link-twincat --features "remote async"
    
  • RemoteSOEM, Simulator

    • Enabled by default

To use the asynchronous API, use autd3::r#async::Controller instead of the Controller.

use autd3::prelude::*;
use autd3_link_soem::{SOEM, SOEMOption, Status};

#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
    let mut autd = autd3::r#async::Controller::open(
        [AUTD3 {
            pos: Point3::origin(),
            rot: UnitQuaternion::identity(),
        }],
        SOEM::new(
            |slave, status| {
                eprintln!("slave[{}]: {}", slave, status);
                if status == Status::Lost {
                    std::process::exit(-1);
                }
            },
            SOEMOption::default(),
        ),
    )
    .await?;

    autd.firmware_version().await?.iter().for_each(|firm_info| {
        println!("{}", firm_info);
    });

    autd.send(Silencer::default()).await?;

    let g = Focus {
        pos: autd.center() + Vector3::new(0., 0., 150.0 * mm),
        option: FocusOption::default(),
    };

    let m = Sine {
        freq: 150 * Hz,
        option: SineOption::default(),
    };

    autd.send((m, g)).await?;

    println!("press enter to quit...");
    let mut _s = String::new();
    std::io::stdin().read_line(&mut _s)?;

    autd.close().await?;

    Ok(())
}