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
pub mod control;
pub use control::{Controller, MotorControl, QuadMotorControl};
use crate::Sensors;
use nalgebra::Vector3;
pub type QuadCopter<S, E> = Copter<S, QuadMotorControl<E>>;
#[derive(Clone, Debug, Default)]
pub struct Copter<S, M: MotorControl> {
pub moment_of_inertia: Vector3<f32>,
pub controller: Controller,
pub motor_control: M,
pub sensors: S,
}
impl<S, M> Copter<S, M>
where
S: Sensors,
M: MotorControl,
{
pub fn arm(&mut self) {
self.motor_control.arm();
}
pub fn control(&mut self, position: Vector3<f32>, velocity: Vector3<f32>) {
let (torque, acceleration) = self.controller.position_control(
position,
velocity,
self.sensors.position(),
self.sensors.velocity(),
self.sensors.attitude(),
self.sensors.gyro(),
self.moment_of_inertia,
);
self.motor_control
.motor_control(torque, acceleration, &self.moment_of_inertia);
}
}