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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
use embedded_time::{duration::Microseconds, Clock};
use num_traits::ToPrimitive;
mod error;
pub use error::Error;
mod task;
pub use task::{Event, Task};
pub struct Scheduler<'a, C, T, E = Error> {
pub tasks: &'a mut [Task<T, E>],
pub clock: C,
pub tick: u16,
pub max_task_slowdown: u8,
pub loop_rate_hz: i16,
loop_period_us: u16,
pub task_not_achieved: u32,
pub task_all_achieved: u32,
pub loop_timer_start_us: u32,
pub last_loop_time_s: f32,
pub extra_loop_us: u32,
}
impl<'a, C, T, E> Scheduler<'a, C, T, E>
where
C: Clock,
C::T: ToPrimitive,
E: From<Error>,
{
pub fn new(tasks: &'a mut [Task<T, E>], clock: C, loop_rate_hz: i16) -> Self {
let loop_period_us = (1000000 / loop_rate_hz as i32) as _;
Self {
tasks,
clock,
tick: 0,
loop_rate_hz,
loop_period_us,
max_task_slowdown: 4,
task_not_achieved: 0,
task_all_achieved: 0,
loop_timer_start_us: 0,
last_loop_time_s: 0.,
extra_loop_us: 0,
}
}
pub fn run(&mut self, state: &mut T) -> Result<(), E> {
let sample_time_us = self.micros_since_epoch()?.0;
if self.loop_timer_start_us == 0 {
self.loop_timer_start_us = sample_time_us;
self.last_loop_time_s = 1. / self.loop_rate_hz as f32;
} else {
self.last_loop_time_s = (sample_time_us - self.loop_timer_start_us) as f32 * 1.0e-6;
}
if self.tick == u16::MAX {
self.tick = 0;
for task in self.tasks.iter_mut() {
task.last_run = 0;
}
} else {
self.tick += 1;
}
let loop_us = self.loop_period_us;
let now = self.micros_since_epoch()?;
let mut time_available = 0;
let loop_tick_us = now.0 - sample_time_us;
if loop_tick_us < loop_us as _ {
time_available = loop_us as u32 - loop_tick_us;
}
time_available += self.extra_loop_us;
self.run_with_time_available_inner(state, now, time_available)?;
if self.task_not_achieved > 0 {
self.extra_loop_us = (self.extra_loop_us + 100).min(5000);
self.task_not_achieved = 0;
self.task_all_achieved = 0;
} else if self.extra_loop_us > 0 {
self.task_all_achieved += 1;
if self.task_all_achieved > 50 {
self.task_all_achieved = 0;
self.extra_loop_us = 0.max(self.extra_loop_us - 50);
}
}
self.loop_timer_start_us = sample_time_us;
Ok(())
}
pub fn run_with_time_available(
&mut self,
system: &mut T,
time_available: u32,
) -> Result<(), E> {
let now = self.micros_since_epoch()?;
self.run_with_time_available_inner(system, now, time_available)
}
fn run_with_time_available_inner(
&mut self,
system: &mut T,
now: Microseconds<u32>,
time_available: u32,
) -> Result<(), E> {
for task in self.tasks.iter_mut() {
if !task.is_high_priority {
let ticks = task.ticks(self.loop_rate_hz);
if let Some(dt) = task.ready(self.tick, ticks) {
if dt as i16 >= ticks * self.max_task_slowdown as i16 {
self.task_not_achieved += 1;
}
if task.max_time_micros as u32 > time_available {
continue;
}
} else {
continue;
}
}
let state = Event {
state: system,
now,
available: Microseconds::new(time_available),
};
task.run(state, self.tick)?;
}
Ok(())
}
fn micros_since_epoch(&mut self) -> Result<Microseconds<u32>, Error> {
let instant = self.clock.try_now()?;
let duration = instant.duration_since_epoch();
let ms: Microseconds<C::T> = Microseconds::try_from(duration)?;
Ok(Microseconds::new(ms.0.to_u32().unwrap()))
}
}
#[cfg(test)]
mod tests {
#[test]
fn it_works() {
let result = 2 + 2;
assert_eq!(result, 4);
}
}