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
226
227
use core::task::Poll;
use embedded_hal::digital::ErrorType;
use fugit::{
NanosDurationU32 as Nanoseconds, TimerDurationU32 as TimerDuration,
};
use fugit_timer::Timer as TimerTrait;
use ramp_maker::MotionProfile;
use crate::{
traits::{SetDirection, Step},
Direction, SetDirectionFuture, StepFuture,
};
use super::{
error::{Error, TimeConversionError},
DelayToTicks,
};
pub enum State<Driver, Timer, Profile: MotionProfile, const TIMER_HZ: u32> {
Idle {
driver: Driver,
timer: Timer,
},
SetDirection(SetDirectionFuture<Driver, Timer, TIMER_HZ>),
Step {
future: StepFuture<Driver, Timer, TIMER_HZ>,
delay: Profile::Delay,
},
StepDelay {
driver: Driver,
timer: Timer,
},
Invalid,
}
pub fn update<Driver, Timer, Profile, Convert, const TIMER_HZ: u32>(
mut state: State<Driver, Timer, Profile, TIMER_HZ>,
new_motion: &mut Option<Direction>,
profile: &mut Profile,
current_step: &mut i32,
current_direction: &mut Direction,
convert: &Convert,
) -> (
Result<
bool,
Error<
<Driver as SetDirection>::Error,
<<Driver as SetDirection>::Dir as ErrorType>::Error,
<Driver as Step>::Error,
<<Driver as Step>::Step as ErrorType>::Error,
Timer::Error,
Convert::Error,
>,
>,
State<Driver, Timer, Profile, TIMER_HZ>,
)
where
Driver: SetDirection + Step,
Timer: TimerTrait<TIMER_HZ>,
Profile: MotionProfile,
Convert: DelayToTicks<Profile::Delay, TIMER_HZ>,
{
loop {
match state {
State::Idle { driver, timer } => {
// Being idle can mean that there's actually nothing to do, or
// it might just be a short breather before more work comes in.
if let Some(direction) = new_motion.take() {
// A new motion has been started. This might override an
// ongoing one, but it makes no difference here.
//
// Let's update the state, but don't return just yet. We
// have more stuff to do (polling the future).
state = State::SetDirection(SetDirectionFuture::new(
direction, driver, timer,
));
*current_direction = direction;
continue;
}
// No new motion has been started, but we might still have an
// ongoing one. Let's ask the motion profile.
if let Some(delay) = profile.next_delay() {
// There's a motion ongoing. Let's start the next step, but
// again, don't return yet. The future needs to be polled.
state = State::Step {
future: StepFuture::new(driver, timer),
delay,
};
continue;
}
// Now we know that there's truly nothing to do. Return to the
// caller and stay idle.
return (Ok(false), State::Idle { driver, timer });
}
State::SetDirection(mut future) => {
match future.poll() {
Poll::Ready(Ok(())) => {
// Direction has been set. Set state back to idle, so we
// can figure out what to do next in the next loop
// iteration.
let (driver, timer) = future.release();
state = State::Idle { driver, timer };
continue;
}
Poll::Ready(Err(err)) => {
// Error happened while setting direction. We need to
// let the caller know.
//
// The state stays as it is. For all we know, the error
// can be recovered from.
return (
Err(Error::SetDirection(err)),
State::SetDirection(future),
);
}
Poll::Pending => {
// Still busy setting direction. Let caller know.
return (Ok(true), State::SetDirection(future));
}
}
}
State::Step { mut future, delay } => {
match future.poll() {
Poll::Ready(Ok(())) => {
// A step was made. Now we need to wait out the rest of
// the step delay before we can do something else.
*current_step += *current_direction as i32;
let (driver, mut timer) = future.release();
let delay_left: TimerDuration<TIMER_HZ> =
match delay_left(
delay,
Driver::PULSE_LENGTH,
convert,
) {
Ok(delay_left) => delay_left,
Err(err) => {
return (
Err(Error::TimeConversion(err)),
State::Idle { driver, timer },
)
}
};
if let Err(err) = timer.start(delay_left) {
return (
Err(Error::StepDelay(err)),
State::Idle { driver, timer },
);
}
state = State::StepDelay { driver, timer };
continue;
}
Poll::Ready(Err(err)) => {
// Error happened while stepping. Need to
// let the caller know.
//
// State stays as it is. For all we know,
// the error can be recovered from.
return (
Err(Error::Step(err)),
State::Step { future, delay },
);
}
Poll::Pending => {
// Still stepping. Let caller know.
return (Ok(true), State::Step { future, delay });
}
}
}
State::StepDelay { driver, mut timer } => {
match timer.wait() {
Ok(()) => {
// We've waited out the step delay. Return to idle
// state, to figure out what's next.
state = State::Idle { driver, timer };
continue;
}
Err(nb::Error::WouldBlock) => {
// The timer is still running. Let the user know.
return (Ok(true), State::StepDelay { driver, timer });
}
Err(nb::Error::Other(err)) => {
// Error while trying to wait. Need to tell the caller.
return (
Err(Error::StepDelay(err)),
State::StepDelay { driver, timer },
);
}
}
}
State::Invalid => {
// This can only happen if this closure panics, the
// user catches the panic, then attempts to
// continue.
//
// A panic in this closure is always going to be a
// bug, and once that happened, we're in an invalid
// state. Not a lot we can do about it.
panic!("Invalid internal state, caused by a previous panic.")
}
}
}
}
fn delay_left<Delay, Convert, const TIMER_HZ: u32>(
delay: Delay,
pulse_length: Nanoseconds,
convert: &Convert,
) -> Result<TimerDuration<TIMER_HZ>, TimeConversionError<Convert::Error>>
where
Convert: DelayToTicks<Delay, TIMER_HZ>,
{
let delay: TimerDuration<TIMER_HZ> = convert
.delay_to_ticks(delay)
.map_err(|err| TimeConversionError::DelayToTicks(err))?;
let pulse_length: TimerDuration<TIMER_HZ> = pulse_length.convert();
let delay_left = delay - pulse_length;
Ok(delay_left)
}