use autd3_driver::{float, FPGA_CLK_FREQ, MAX_CYCLE};
use crate::error::AUTDInternalError;
use super::{Matrix4, Transducer, UnitQuaternion, Vector3, Vector4};
pub struct AdvancedPhaseTransducer {
idx: usize,
pos: Vector3,
rot: UnitQuaternion,
cycle: u16,
mod_delay: u16,
}
impl Transducer for AdvancedPhaseTransducer {
type Gain = autd3_driver::operation::GainAdvancedPhase;
type Sync = autd3_driver::SyncAdvanced;
type GainSTM = autd3_driver::operation::GainSTMAdvanced;
fn new(idx: usize, pos: Vector3, rot: UnitQuaternion) -> Self {
Self {
idx,
pos,
rot,
cycle: 4096,
mod_delay: 0,
}
}
fn affine(&mut self, t: Vector3, r: UnitQuaternion) {
let rot_mat: Matrix4 = From::from(r);
let trans_mat = rot_mat.append_translation(&t);
let homo = Vector4::new(self.pos[0], self.pos[1], self.pos[2], 1.0);
let new_pos = trans_mat * homo;
self.pos = Vector3::new(new_pos[0], new_pos[1], new_pos[2]);
self.rot = r * self.rot;
}
fn position(&self) -> &Vector3 {
&self.pos
}
fn rotation(&self) -> &UnitQuaternion {
&self.rot
}
fn idx(&self) -> usize {
self.idx
}
fn mod_delay(&self) -> u16 {
self.mod_delay
}
fn set_mod_delay(&mut self, delay: u16) {
self.mod_delay = delay;
}
fn frequency(&self) -> float {
FPGA_CLK_FREQ as float / self.cycle as float
}
fn cycle(&self) -> u16 {
self.cycle
}
}
impl AdvancedPhaseTransducer {
pub fn set_cycle(&mut self, cycle: u16) -> Result<(), AUTDInternalError> {
if cycle > MAX_CYCLE {
return Err(AUTDInternalError::CycleOutOfRange(cycle));
}
self.cycle = cycle;
Ok(())
}
pub fn set_frequency(&mut self, freq: float) -> Result<(), AUTDInternalError> {
let cycle = (FPGA_CLK_FREQ as float / freq).round() as u16;
self.set_cycle(cycle)
}
}