BLDC Driver
The BldcDriver component wraps around the ESP MCPWM Peripheral to provide full 6 PWM control over a 3 phase brushless dc motor.
API Reference
Header File
Classes
-
class BldcDriver : public espp::BaseComponent
Interface for controlling the 6 PWMs (high/low sides for phases A,B,C) of a brushless dc motor (BLDC motor). Wraps around the ESP32 https://docs.espressif.com/projects/esp-idf/en/latest/esp32/api-reference/peripherals/mcpwm.html peripheral.
Public Functions
-
inline explicit BldcDriver(const Config &config)
Initialize the bldc driver.
Note
Enables the driver.
- Parameters
config – Config used to initialize the driver.
-
inline ~BldcDriver()
Disable the BldcDriver and destroy it.
-
inline void enable()
Enable the driver, set the enable pin high (if we were provided an enable pin), and remove any force level on the pins if there was a force level.
-
inline void disable()
Disable the driver, set the enable pin low (if we were provided an enable pin), add a force level to each pin to force the gates all low.
-
inline bool is_enabled() const
Check if the driver is enabled.
- Returns
True if the driver is enabled, false otherwise.
-
inline bool is_faulted()
Check if the driver is faulted.
Note
If no fault pin was provided, this will always return false.
- Returns
True if the driver is faulted, false otherwise.
-
inline void set_phase_state(int state_a, int state_b, int state_c)
This function does nothing, merely exists for later if we choose to implement it as part of the FOC control algorithm.
Note
The integers provided are -1 (low), 0 (high impedance), and 1 (active) for the 3 possible states.
- Parameters
state_a – Desired state for phase A.
state_b – Desired state for phase B.
state_c – Desired state for phase C.
-
inline void set_voltage(float ua, float ub, float uc)
Set the phase voltages (in volts) to the driver.
Note
Uses the previously configured power supply voltage and limit voltage (if any was provided) to convert to the appropriate pwm.
- Parameters
ua – Desired voltage [0.0f, power_supply_voltage] for phase A.
ub – Desired voltage [0.0f, power_supply_voltage] for phase B.
uc – Desired voltage [0.0f, power_supply_voltage] for phase C.
-
inline void set_pwm(float duty_a, float duty_b, float duty_c)
Directly set the duty cycle in range [0.0, 1.0] for each phase.
- Parameters
duty_a – Duty cycle [0.0, 1.0] for phase A.
duty_b – Duty cycle [0.0, 1.0] for phase B.
duty_c – Duty cycle [0.0, 1.0] for phase C.
-
inline void configure_power(float power_supply_voltage, float voltage_limit = -1.0f)
Update the power supply configuration and optional motor voltage limit.
- Parameters
power_supply_voltage – New max voltage (volts) from the power supply.
voltage_limit – New limit voltage (volts) to allow to the motor.
-
inline float get_voltage_limit() const
Get the current motor voltage limit (if any).
- Returns
Voltage limit the driver will allow to the motor.
-
inline float get_power_supply_limit() const
Get the configured power supply voltage.
- Returns
Maximum voltage the driver can supply to the motor.
-
inline const std::string &get_name() const
Get the name of the component
Note
This is the tag of the logger
- Returns
A const reference to the name of the component
-
inline void set_log_tag(const std::string_view &tag)
Set the tag for the logger
- Parameters
tag – The tag to use for the logger
-
inline espp::Logger::Verbosity get_log_level() const
Get the log level for the logger
See also
See also
- Returns
The verbosity level of the logger
-
inline void set_log_level(espp::Logger::Verbosity level)
Set the log level for the logger
See also
See also
- Parameters
level – The verbosity level to use for the logger
-
inline void set_log_verbosity(espp::Logger::Verbosity level)
Set the log verbosity for the logger
See also
See also
See also
Note
This is a convenience method that calls set_log_level
- Parameters
level – The verbosity level to use for the logger
-
inline espp::Logger::Verbosity get_log_verbosity() const
Get the log verbosity for the logger
See also
See also
See also
Note
This is a convenience method that calls get_log_level
- Returns
The verbosity level of the logger
-
inline void set_log_rate_limit(std::chrono::duration<float> rate_limit)
Set the rate limit for the logger
See also
Note
Only calls to the logger that have _rate_limit suffix will be rate limited
- Parameters
rate_limit – The rate limit to use for the logger
-
struct Config
Configure the driver for 6-channel control of a BLDC.
Public Members
-
int gpio_a_h
Phase A high side gpio.
-
int gpio_a_l
Phase A low side gpio.
-
int gpio_b_h
Phase B high side gpio.
-
int gpio_b_l
Phase B low side gpio.
-
int gpio_c_h
Phase C high side gpio.
-
int gpio_c_l
Phase C low side gpio.
-
int gpio_enable = {-1}
Enable pin for the BLDC driver (if any).
-
int gpio_fault = {-1}
Fault pin for the BLDC driver (if any).
-
float power_supply_voltage
Voltage of the power supply.
-
float limit_voltage = {-1}
What voltage the motor should be limited to. Less than 0 means no limit. Will be clamped to power supply voltage.
-
uint64_t dead_zone_ns{100}
Dead zone in nanoseconds. Will be applied to both sides of the waveform.
-
int gpio_a_h
-
inline explicit BldcDriver(const Config &config)