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#
Macros#
-
ESP_IDF_VERSION_VAL(major, minor, patch)
-
ESP_IDF_VERSION
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 bool register_pwm_sample_callback(std::function<void()> callback)#
Register a callback fired once per PWM period at the timer “empty” (TEZ) event, i.e. when the counter reaches zero.
For this driver’s center-aligned (up/down) configuration the low-side FETs are conducting around the TEZ event, which is the correct instant to sample low-side shunt currents for FOC current sensing. Wire this to start / read your current-sense ADC so the samples are synchronized to the PWM.
Note
The callback runs in interrupt context. Keep it short (e.g. kick off an ADC conversion or read a pre-triggered result) and non-blocking. If CONFIG_MCPWM_ISR_IRAM_SAFE is enabled, the callback and everything it touches must be in IRAM.
Note
Registering the callback briefly disables and re-enables the MCPWM timer (the ESP-IDF API requires the timer to be in the init state to attach event callbacks), so register it during setup before the motor is spinning.
- Parameters:
callback – Function to call at each TEZ event. Pass nullptr to clear a previously registered callback.
- Returns:
True if the callback was registered (or cleared) successfully.
-
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 void set_power_supply_voltage(float voltage)#
Set the power supply voltage.
Note
Will update the voltage limit to the minimum of the current limit and the new power supply voltage.
- Parameters:
voltage – New power supply voltage.
-
inline void set_voltage_limit(float voltage)#
Set the voltage limit.
Note
Will be clamped to the power supply voltage.
- Parameters:
voltage – New voltage limit.
-
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
-
template<typename T = void>
inline void set_log_tag(const std::string_view&)# Not available: the tag is fixed at construction (pass it to the constructor). Enable CONFIG_ESPP_LOGGER_MUTABLE_TAG (menuconfig: ESPP Logger -> Mutable tag) to change it afterwards.
-
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 intr_priority = {0}#
Interrupt priority for the MCPWM timer.
0 (the default) lets the driver allocate a low-priority interrupt and preserves the previous behavior. Set to 1, 2, or 3 to request a specific level. Only levels 1-3 may be used: levels 4-7 are high-priority interrupts whose handlers must be written in assembly and cannot call C/C++ code (and some are reserved by the system), so they cannot be used here. Values outside the range [0, 3] are clamped (with a warning). Requires ESP-IDF >= 5.1; ignored on older versions.
-
int gpio_a_h#
-
inline explicit BldcDriver(const Config &config)#