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>
where Mode: StepMode, T: Copy + Zero + One + SaturatingAdd + From<i8>,

source

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?
examples/incremental.rs (line 13)
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!();
    }
}
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/incremental.rs (line 8)
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!();
    }
}
source

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

Sets the decoder’s counter.

Trait Implementations§

source§

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

source§

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

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

impl<T> Default for IncrementalDecoder<FullStep, T>
where T: Zero,

source§

fn default() -> Self

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

impl<T> Default for IncrementalDecoder<HalfStep, T>
where T: Zero,

source§

fn default() -> Self

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

impl<T> Default for IncrementalDecoder<QuadStep, T>
where T: Zero,

source§

fn default() -> Self

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

Auto Trait Implementations§

§

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

§

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

§

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

§

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

§

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

§

impl<Mode, T> UnwindSafe for IncrementalDecoder<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>,

§

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>,

§

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.