Struct IndexedIncrementalDecoder

Source
pub struct IndexedIncrementalDecoder<Mode, T = i32> { /* private fields */ }
Expand description

A robust indexed 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  

                         ┌─┐                         high
           Z             │ │                              
             ─ ─ ─ ──────┘ └────────────────── ─ ─ ─ low  
Time: ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─▶
                 ┌ ─ ┐   ┌───┐   ┌───┐   ┌───┐   ┌ ─ high
           A             │   │   │   │   │                
             ─ ─ ┘   └───┘   └───┘   └───┘   └ ─ ┘   low  
BA:                                                  
               ┌ ─ ┐   ┌───┐   ┌───┐   ┌───┐   ┌ ─ ─ high
           B           │   │   │   │   │                  
             ─ ┘   └───┘   └───┘   └───┘   └ ─ ┘     low  

                         ┌─┐                         high
           Z             │ │                              
             ─ ─ ─ ──────┘ └────────────────── ─ ─ ─ low  

Implementations§

Source§

impl<Mode, T> IndexedIncrementalDecoder<Mode, T>
where Mode: StepMode, T: Copy + Zero + One + SaturatingAdd + From<i8>,

Source

pub fn update( &mut self, a: bool, b: bool, z: 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.

Upon detection of a raising edge on the z pulse train the counter gets reset back to 0.

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?
examples/indexed_incremental.rs (line 13)
3fn decode<S, I>(decoder: &mut IndexedIncrementalDecoder<S>, pulse_trains: I)
4where
5    S: StepMode,
6    I: Iterator<Item = ((bool, bool), bool)>,
7{
8    println!("Decoder is at counter: {:?}.", decoder.counter());
9    println!();
10
11    for ((a, b), z) in pulse_trains {
12        println!("Reading pulses: (a: {a:?}, b: {b:?}, z: {z:?})");
13        match decoder.update(a, b, z) {
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}
Source

pub fn reset(&mut self)

Resets the decoder to its initial state and its counter counter back to 0.

Source

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?
examples/indexed_incremental.rs (line 8)
3fn decode<S, I>(decoder: &mut IndexedIncrementalDecoder<S>, pulse_trains: I)
4where
5    S: StepMode,
6    I: Iterator<Item = ((bool, bool), bool)>,
7{
8    println!("Decoder is at counter: {:?}.", decoder.counter());
9    println!();
10
11    for ((a, b), z) in pulse_trains {
12        println!("Reading pulses: (a: {a:?}, b: {b:?}, z: {z:?})");
13        match decoder.update(a, b, z) {
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}
Source

pub fn set_counter(&mut self, counter: T)

Sets the decoder’s counter.

Trait Implementations§

Source§

impl<Mode: Debug, T: Debug> Debug for IndexedIncrementalDecoder<Mode, T>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl<Mode, T> Default for IndexedIncrementalDecoder<Mode, T>
where Mode: StepMode, IncrementalDecoder<Mode, T>: Default,

Source§

fn default() -> Self

Returns the “default value” for a type. Read more

Auto Trait Implementations§

§

impl<Mode, T> Freeze for IndexedIncrementalDecoder<Mode, T>
where T: Freeze,

§

impl<Mode, T> RefUnwindSafe for IndexedIncrementalDecoder<Mode, T>
where T: RefUnwindSafe, Mode: RefUnwindSafe,

§

impl<Mode, T> Send for IndexedIncrementalDecoder<Mode, T>
where T: Send, Mode: Send,

§

impl<Mode, T> Sync for IndexedIncrementalDecoder<Mode, T>
where T: Sync, Mode: Sync,

§

impl<Mode, T> Unpin for IndexedIncrementalDecoder<Mode, T>
where T: Unpin, Mode: Unpin,

§

impl<Mode, T> UnwindSafe for IndexedIncrementalDecoder<Mode, T>
where T: UnwindSafe, Mode: UnwindSafe,

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.