Skip to main content

HBridgeMotorDriver

Struct HBridgeMotorDriver 

Source
pub struct HBridgeMotorDriver<E1, E2, P1, P2, Enc1, Enc2> { /* private fields */ }
Expand description

H-bridge motor driver implementation with optional encoder support.

This struct provides comprehensive motor control functionality including:

  • Single or dual PWM channel control for H-bridge motor drivers
  • Single or dual enable pin control
  • Optional quadrature encoder support for position feedback
  • Speed and direction control with safety checks

§Type Parameters

  • E1 - Primary enable pin type implementing OutputPin
  • E2 - Secondary enable pin type implementing OutputPin (optional)
  • P1 - Primary PWM channel type implementing SetDutyCycle
  • P2 - Secondary PWM channel type implementing SetDutyCycle (optional)
  • Enc1 - Encoder A channel type implementing InputPin (optional)
  • Enc2 - Encoder B channel type implementing InputPin (optional)

§Example

use motor_driver_hal::HBridgeMotorDriver;
 
// Create a simple single PWM motor driver
let motor = HBridgeMotorDriver::single_pwm(enable_pin, pwm_channel, 1000);
 
// Create a dual PWM motor driver with encoders
let motor = HBridgeMotorDriver::dual_pwm_with_encoder(
    enable1, enable2, pwm1, pwm2, enc_a, enc_b, 1000
);

Implementations§

Source§

impl<E1, E2, P1, P2> HBridgeMotorDriver<E1, E2, P1, P2, NoEncoder, NoEncoder>
where E1: OutputPin, E2: OutputPin, P1: SetDutyCycle, P2: SetDutyCycle,

Source

pub fn builder() -> HBridgeMotorDriverBuilder<E1, E2, P1, P2, NoEncoder, NoEncoder>

Creates a new builder for motor drivers without encoder support.

§Returns

A new builder instance configured for NoEncoder types

§Example
let motor = HBridgeMotorDriver::builder()
    .with_enable(enable_pin)
    .with_pwm(pwm_channel)
    .build();
Source

pub fn single_pwm(enable: E1, pwm: P1, max_duty: u16) -> Self

Creates a motor driver with single PWM channel configuration.

This is a convenience constructor for the most common motor driver configuration using one enable pin and one PWM channel.

§Arguments
  • enable - GPIO pin for enabling/disabling the motor driver
  • pwm - PWM channel for speed control
  • max_duty - Maximum duty cycle value for speed scaling
§Returns

A configured motor driver instance (not yet initialized)

§Example
let motor = HBridgeMotorDriver::single_pwm(enable_pin, pwm_channel, 1000);
Source

pub fn dual_pwm( enable1: E1, enable2: E2, pwm1: P1, pwm2: P2, max_duty: u16, ) -> Self

Creates a motor driver with dual PWM and dual enable configuration.

This configuration provides the most control options with separate PWM channels for each direction and separate enable pins.

§Arguments
  • enable1 - Primary enable pin
  • enable2 - Secondary enable pin
  • pwm1 - Primary PWM channel (forward direction)
  • pwm2 - Secondary PWM channel (reverse direction)
  • max_duty - Maximum duty cycle value for both PWM channels
§Returns

A configured motor driver instance (not yet initialized)

§Example
let motor = HBridgeMotorDriver::dual_pwm(
    enable1, enable2, pwm1, pwm2, 1000
);
Source§

impl<E1, E2, P1, P2, Enc1, Enc2> HBridgeMotorDriver<E1, E2, P1, P2, Enc1, Enc2>
where E1: OutputPin, E2: OutputPin, P1: SetDutyCycle, P2: SetDutyCycle, Enc1: InputPin, Enc2: InputPin,

Source

pub fn builder_with_encoder() -> HBridgeMotorDriverBuilder<E1, E2, P1, P2, Enc1, Enc2>

Creates a new builder for motor drivers with encoder support.

§Returns

A new builder instance configured for encoder types Enc1 and Enc2

§Example
let motor = HBridgeMotorDriver::builder_with_encoder()
    .with_dual_enable(enable1, enable2)
    .with_dual_pwm(pwm1, pwm2)
    .with_encoder(enc_a, enc_b)
    .with_ppr(1024)
    .build();
Source

pub fn dual_pwm_with_encoder( enable1: E1, enable2: E2, pwm1: P1, pwm2: P2, encoder1: Enc1, encoder2: Enc2, max_duty: u16, ) -> Self

Creates a motor driver with dual PWM, dual enable, and encoder support.

This is the most feature-complete configuration providing precise motor control with position feedback.

§Arguments
  • enable1 - Primary enable pin
  • enable2 - Secondary enable pin
  • pwm1 - Primary PWM channel (forward direction)
  • pwm2 - Secondary PWM channel (reverse direction)
  • encoder1 - Encoder A channel pin
  • encoder2 - Encoder B channel pin
  • max_duty - Maximum duty cycle value
§Returns

A configured motor driver instance with encoder support

§Example
let motor = HBridgeMotorDriver::dual_pwm_with_encoder(
    enable1, enable2, pwm1, pwm2, enc_a, enc_b, 1000
);
Source

pub fn read_encoder(&mut self) -> Result<(), MotorDriverError>

Reads the current encoder state and updates pulse count.

This method implements quadrature encoder decoding using a state machine to track motor position. It should be called regularly (typically in a timer interrupt or polling loop) to maintain accurate position tracking.

§Returns
  • Ok(()) if encoder reading succeeds
  • Err(MotorDriverError::GpioError) if encoder pin reading fails
  • Err(MotorDriverError::HardwareFault) if encoders are not configured
§Example
// In a timer interrupt or polling loop
motor.read_encoder()?;
let position = motor.get_pulse_count();
Source

pub fn get_pulse_count(&self) -> i32

Gets the current encoder pulse count relative to the last reset.

The pulse count is automatically adjusted by the pulse offset set by reset_encoder() to provide relative position measurements.

§Returns

Current pulse count since last encoder reset

§Example
motor.reset_encoder(); // Reset to zero
// ... motor movement ...
let position = motor.get_pulse_count(); // Position since reset
Source

pub fn reset_encoder(&mut self)

Resets the encoder position counter to zero.

This sets the current position as the new reference point (zero). Subsequent calls to get_pulse_count() will return values relative to this reset point.

§Example
motor.reset_encoder(); // Set current position as zero
Source

pub fn set_target_pulse(&mut self, target: i32)

Sets the target pulse count for position control.

This target is used by check_ppr() to verify that the motor has reached the desired position.

§Arguments
  • target - Target pulse count relative to encoder reset point
§Example
motor.set_target_pulse(1000); // Move 1000 pulses from current position

Trait Implementations§

Source§

impl<E1, E2, P1, P2, Enc1, Enc2> MotorDriver for HBridgeMotorDriver<E1, E2, P1, P2, Enc1, Enc2>
where E1: OutputPin, E2: OutputPin, P1: SetDutyCycle, P2: SetDutyCycle, Enc1: InputPin, Enc2: InputPin,

Source§

type Error = MotorDriverError

The error type returned by this driver’s operations.
Source§

fn initialize(&mut self) -> Result<(), Self::Error>

Initializes the motor driver hardware. Read more
Source§

fn set_speed(&mut self, speed: i16) -> Result<(), Self::Error>

Sets the motor speed and direction. Read more
Source§

fn set_direction(&mut self, forward: bool) -> Result<(), Self::Error>

Sets the motor direction without changing speed magnitude. Read more
Source§

fn stop(&mut self) -> Result<(), Self::Error>

Stops the motor by setting PWM to zero (coast stop). Read more
Source§

fn brake(&mut self) -> Result<(), Self::Error>

Applies active braking to the motor. Read more
Source§

fn enable(&mut self) -> Result<(), Self::Error>

Enables the motor driver. Read more
Source§

fn disable(&mut self) -> Result<(), Self::Error>

Disables the motor driver. Read more
Source§

fn get_speed(&self) -> Result<i16, Self::Error>

Gets the current motor speed setting. Read more
Source§

fn get_direction(&self) -> Result<bool, Self::Error>

Gets the current motor direction. Read more
Source§

fn set_ppr(&mut self, ppr: i16) -> Result<bool, Self::Error>

Sets the pulses per revolution for encoder calculations. Read more
Source§

fn check_ppr(&mut self) -> Result<(), Self::Error>

Checks if the encoder pulse count matches the target position. Read more
Source§

fn get_current(&self) -> Result<f32, Self::Error>

Gets the current motor current consumption. Read more
Source§

fn get_voltage(&self) -> Result<f32, Self::Error>

Gets the current motor supply voltage. Read more
Source§

fn get_temperature(&self) -> Result<f32, Self::Error>

Gets the current motor driver temperature. Read more
Source§

fn get_fault_status(&self) -> Result<u8, Self::Error>

Gets the current fault status of the motor driver. Read more

Auto Trait Implementations§

§

impl<E1, E2, P1, P2, Enc1, Enc2> Freeze for HBridgeMotorDriver<E1, E2, P1, P2, Enc1, Enc2>
where E1: Freeze, P1: Freeze, E2: Freeze, P2: Freeze, Enc1: Freeze, Enc2: Freeze,

§

impl<E1, E2, P1, P2, Enc1, Enc2> RefUnwindSafe for HBridgeMotorDriver<E1, E2, P1, P2, Enc1, Enc2>

§

impl<E1, E2, P1, P2, Enc1, Enc2> Send for HBridgeMotorDriver<E1, E2, P1, P2, Enc1, Enc2>
where E1: Send, P1: Send, E2: Send, P2: Send, Enc1: Send, Enc2: Send,

§

impl<E1, E2, P1, P2, Enc1, Enc2> Sync for HBridgeMotorDriver<E1, E2, P1, P2, Enc1, Enc2>
where E1: Sync, P1: Sync, E2: Sync, P2: Sync, Enc1: Sync, Enc2: Sync,

§

impl<E1, E2, P1, P2, Enc1, Enc2> Unpin for HBridgeMotorDriver<E1, E2, P1, P2, Enc1, Enc2>
where E1: Unpin, P1: Unpin, E2: Unpin, P2: Unpin, Enc1: Unpin, Enc2: Unpin,

§

impl<E1, E2, P1, P2, Enc1, Enc2> UnsafeUnpin for HBridgeMotorDriver<E1, E2, P1, P2, Enc1, Enc2>
where E1: UnsafeUnpin, P1: UnsafeUnpin, E2: UnsafeUnpin, P2: UnsafeUnpin, Enc1: UnsafeUnpin, Enc2: UnsafeUnpin,

§

impl<E1, E2, P1, P2, Enc1, Enc2> UnwindSafe for HBridgeMotorDriver<E1, E2, P1, P2, Enc1, Enc2>
where E1: UnwindSafe, P1: UnwindSafe, E2: UnwindSafe, P2: UnwindSafe, Enc1: UnwindSafe, Enc2: 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.