1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
use anyhow::{Ok, Result};
use autd3_driver::{FPGA_CLK_FREQ, MAX_CYCLE};
use crate::error::AUTDInternalError;
use super::{Transducer, UnitQuaternion, Vector3};
pub struct NormalTransducer {
id: usize,
pos: Vector3,
rot: UnitQuaternion,
cycle: u16,
sound_speed: f64,
attenuation: f64,
mod_delay: u16,
}
impl Transducer for NormalTransducer {
fn new(id: usize, pos: Vector3, rot: UnitQuaternion) -> Self {
Self {
id,
pos,
rot,
cycle: 4096,
sound_speed: 340e3,
attenuation: 0.,
mod_delay: 0,
}
}
fn position(&self) -> &Vector3 {
&self.pos
}
fn rotation(&self) -> &UnitQuaternion {
&self.rot
}
fn id(&self) -> usize {
self.id
}
fn cycle(&self) -> u16 {
self.cycle
}
fn frequency(&self) -> f64 {
FPGA_CLK_FREQ as f64 / self.cycle as f64
}
fn mod_delay(&self) -> u16 {
self.mod_delay
}
fn set_mod_delay(&mut self, delay: u16) {
self.mod_delay = delay;
}
fn sound_speed(&self) -> f64 {
self.sound_speed
}
fn set_sound_speed(&mut self, value: f64) {
self.sound_speed = value;
}
fn attenuation(&self) -> f64 {
self.attenuation
}
fn set_attenuation(&mut self, value: f64) {
self.attenuation = value;
}
}
impl NormalTransducer {
pub fn set_cycle(&mut self, cycle: u16) -> Result<()> {
if cycle > MAX_CYCLE {
return Err(AUTDInternalError::CycleOutOfRange(cycle).into());
}
self.cycle = cycle;
Ok(())
}
pub fn set_frequency(&mut self, freq: f64) -> Result<()> {
let cycle = (FPGA_CLK_FREQ as f64 / freq).round() as u16;
self.set_cycle(cycle)
}
}