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
use embedded_flight_control::attitude::MultiCopterAttitudeController;
use embedded_flight_core::InertialSensor;
use embedded_flight_motors::{esc::ESC, MotorMatrix};
use embedded_flight_scheduler::{Error, Scheduler, State, Task};
use embedded_time::Clock;
use nalgebra::{Quaternion, Vector3};
use num_traits::NumCast;
pub struct MultiCopterState<E, const N: usize> {
pub attitude: Quaternion<f32>,
pub gyro: Vector3<f32>,
pub attitude_controller: MultiCopterAttitudeController,
pub motor_matrix: MotorMatrix<E, f32, N>,
}
pub struct MultiCopter<'t, C, I, E, const N: usize> {
pub scheduler: Scheduler<'t, C, MultiCopterState<E, N>>,
pub inertial_sensor: I,
pub state: MultiCopterState<E, N>,
}
impl<'t, C, I, E, const N: usize> MultiCopter<'t, C, I, E, N>
where
C: Clock<T = u32>,
I: InertialSensor,
E: ESC,
E::Output: NumCast
{
pub fn new(
motor_matrix: MotorMatrix<E, f32, N>,
inertial_sensor: I,
tasks: &'t mut [Task<MultiCopterState<E, N>>],
clock: C,
loop_rate_hz: i16,
) -> Self {
Self {
scheduler: Scheduler::new(tasks, clock, loop_rate_hz),
inertial_sensor,
state: MultiCopterState {
attitude: Quaternion::default(),
gyro: Vector3::default(),
attitude_controller: MultiCopterAttitudeController::default(),
motor_matrix,
},
}
}
pub fn run(&mut self) -> Result<(), Error> {
loop {
self.state.attitude = self.inertial_sensor.attitude();
self.state.gyro = self.inertial_sensor.gyro();
self.scheduler.run(&mut self.state)?;
}
}
}
pub fn multi_copter_tasks<E, const N: usize>() -> [Task<MultiCopterState<E, N>>; 1]
where
E: ESC,
E::Output: NumCast
{
[motor_output_task()]
}
pub fn motor_output_task<E, const N: usize>() -> Task<MultiCopterState<E, N>>
where
E: ESC,
E::Output: NumCast
{
Task::high_priority(|state: State<'_, MultiCopterState<E, N>>| {
let motor_output = state
.system
.attitude_controller
.motor_output(state.system.gyro, state.now.0);
state.system.motor_matrix.output(motor_output);
Ok(())
})
}