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?
3fn decode<S, I>(decoder: &mut IncrementalDecoder<S>, pulse_trains: I)
4where
5 S: StepMode,
6 I: Iterator<Item = (bool, bool)>,
7{
8 println!("Decoder is at counter: {:?}.", decoder.counter());
9 println!();
10
11 for (a, b) in pulse_trains {
12 println!("Reading pulses: (a: {a:?}, b: {b:?})");
13 match decoder.update(a, b) {
14 Ok(Some(change)) => println!("Change detected: {:?}.", change),
15 Ok(None) => println!("No change detected."),
16 Err(error) => println!("Error detected: {:?}.", error),
17 }
18 println!("Decoder is at counter: {:?}.", decoder.counter());
19 println!();
20 }
21}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?
3fn decode<S, I>(decoder: &mut IncrementalDecoder<S>, pulse_trains: I)
4where
5 S: StepMode,
6 I: Iterator<Item = (bool, bool)>,
7{
8 println!("Decoder is at counter: {:?}.", decoder.counter());
9 println!();
10
11 for (a, b) in pulse_trains {
12 println!("Reading pulses: (a: {a:?}, b: {b:?})");
13 match decoder.update(a, b) {
14 Ok(Some(change)) => println!("Change detected: {:?}.", change),
15 Ok(None) => println!("No change detected."),
16 Err(error) => println!("Error detected: {:?}.", error),
17 }
18 println!("Decoder is at counter: {:?}.", decoder.counter());
19 println!();
20 }
21}Sourcepub fn set_counter(&mut self, counter: T)
pub fn set_counter(&mut self, counter: T)
Sets the decoder’s counter.