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 implementingOutputPinE2- Secondary enable pin type implementingOutputPin(optional)P1- Primary PWM channel type implementingSetDutyCycleP2- Secondary PWM channel type implementingSetDutyCycle(optional)Enc1- Encoder A channel type implementingInputPin(optional)Enc2- Encoder B channel type implementingInputPin(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>
impl<E1, E2, P1, P2> HBridgeMotorDriver<E1, E2, P1, P2, NoEncoder, NoEncoder>
Sourcepub fn builder() -> HBridgeMotorDriverBuilder<E1, E2, P1, P2, NoEncoder, NoEncoder>
pub fn builder() -> HBridgeMotorDriverBuilder<E1, E2, P1, P2, NoEncoder, NoEncoder>
Sourcepub fn single_pwm(enable: E1, pwm: P1, max_duty: u16) -> Self
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 driverpwm- PWM channel for speed controlmax_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);Sourcepub fn dual_pwm(
enable1: E1,
enable2: E2,
pwm1: P1,
pwm2: P2,
max_duty: u16,
) -> Self
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 pinenable2- Secondary enable pinpwm1- 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,
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,
Sourcepub fn builder_with_encoder() -> HBridgeMotorDriverBuilder<E1, E2, P1, P2, Enc1, Enc2>
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();Sourcepub fn dual_pwm_with_encoder(
enable1: E1,
enable2: E2,
pwm1: P1,
pwm2: P2,
encoder1: Enc1,
encoder2: Enc2,
max_duty: u16,
) -> Self
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 pinenable2- Secondary enable pinpwm1- Primary PWM channel (forward direction)pwm2- Secondary PWM channel (reverse direction)encoder1- Encoder A channel pinencoder2- Encoder B channel pinmax_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
);Sourcepub fn read_encoder(&mut self) -> Result<(), MotorDriverError>
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 succeedsErr(MotorDriverError::GpioError)if encoder pin reading failsErr(MotorDriverError::HardwareFault)if encoders are not configured
§Example
// In a timer interrupt or polling loop
motor.read_encoder()?;
let position = motor.get_pulse_count();Sourcepub fn get_pulse_count(&self) -> i32
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 resetSourcepub fn reset_encoder(&mut self)
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