Struct quadrature_decoder::IncrementalDecoder
source · pub struct IncrementalDecoder<Mode, T = i32> { /* private fields */ }Expand description
A robust quadrature decoder with support for multiple step-modes, based on which channel (A vs. B) is leading the other.
┌ ─ ┐ ┌───┐ ┌───┐ ┌───┐ ┌ ─ ─ high
A │ │ │ │ │
─ ┘ └───┘ └───┘ └───┘ └ ─ ┘ low
AB:
┌ ─ ┐ ┌───┐ ┌───┐ ┌───┐ ┌ ─ high
B │ │ │ │ │
─ ─ ┘ └───┘ └───┘ └───┘ └ ─ ┘ low
Time: ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─▶
┌ ─ ┐ ┌───┐ ┌───┐ ┌───┐ ┌ ─ high
A │ │ │ │ │
─ ─ ┘ └───┘ └───┘ └───┘ └ ─ ┘ low
BA:
┌ ─ ┐ ┌───┐ ┌───┐ ┌───┐ ┌ ─ ─ high
B │ │ │ │ │
─ ┘ └───┘ └───┘ └───┘ └ ─ ┘ low
Implementations§
source§impl<Mode, T> IncrementalDecoder<Mode, T>
impl<Mode, T> IncrementalDecoder<Mode, T>
sourcepub fn update(&mut self, a: bool, b: bool) -> Result<Option<Change>, Error>
pub fn update(&mut self, a: bool, b: bool) -> Result<Option<Change>, Error>
Updates the decoder’s state based on the given a and b pulse train (aka channel) readings,
returning the direction if a change was detected, None if no change was detected,
or Err(_) if an invalid input (i.e. a counteral “jump”) was detected.
Depending on whether it matters why the decoder did not detect a change
(e.g. due to actual lack of change or an erroneous read)
you would either call decoder.update(a, b) directly, or via decoder.update(a, b).unwrap_or_default()
to fall back to None in case of Err(_).
Examples found in repository?
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
fn decode<S, I>(decoder: &mut IncrementalDecoder<S>, pulse_trains: I)
where
S: StepMode,
I: Iterator<Item = (bool, bool)>,
{
println!("Decoder is at counter: {:?}.", decoder.counter());
println!();
for (a, b) in pulse_trains {
println!("Reading pulses: (a: {a:?}, b: {b:?})");
match decoder.update(a, b) {
Ok(Some(change)) => println!("Change detected: {:?}.", change),
Ok(None) => println!("No change detected."),
Err(error) => println!("Error detected: {:?}.", error),
}
println!("Decoder is at counter: {:?}.", decoder.counter());
println!();
}
}sourcepub fn reset(&mut self)
pub fn reset(&mut self)
Resets the decoder to its initial state and its counter counter back to 0.
sourcepub fn counter(&self) -> T
pub fn counter(&self) -> T
Returns the decoder’s counter counter relative to its initial counter in number of cycles.
A change of Change::Positive increments the counter counter,
while a change of Change::Negative decrements it.
Examples found in repository?
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
fn decode<S, I>(decoder: &mut IncrementalDecoder<S>, pulse_trains: I)
where
S: StepMode,
I: Iterator<Item = (bool, bool)>,
{
println!("Decoder is at counter: {:?}.", decoder.counter());
println!();
for (a, b) in pulse_trains {
println!("Reading pulses: (a: {a:?}, b: {b:?})");
match decoder.update(a, b) {
Ok(Some(change)) => println!("Change detected: {:?}.", change),
Ok(None) => println!("No change detected."),
Err(error) => println!("Error detected: {:?}.", error),
}
println!("Decoder is at counter: {:?}.", decoder.counter());
println!();
}
}sourcepub fn set_counter(&mut self, counter: T)
pub fn set_counter(&mut self, counter: T)
Sets the decoder’s counter.