|
| 1 | +--- |
| 2 | +layout: default |
| 3 | +title: BLDCDriver 3PWM |
| 4 | +nav_order: 1 |
| 5 | +permalink: /bldcdriver3pwm |
| 6 | +parent: BLDCDriver |
| 7 | +grand_parent: Driver code |
| 8 | +grand_grand_parent: Writing the Code |
| 9 | +grand_grand_grand_parent: Arduino <span class="simple">Simple<span class="foc">FOC</span>library</span> |
| 10 | +--- |
| 11 | + |
| 12 | +# BLDC driver 3PWM - `BLDCDriver3PWM` |
| 13 | + |
| 14 | +This is the class which provides an abstraction layer of most of the common 3PWM bldc drivers out there. Basically any BLDC driver board that can be run using 3PWM signals can be represented with this class. |
| 15 | +Examples: |
| 16 | +- Arduino <span class="simple">Simple<span class="foc">FOC</span>Shield</span> |
| 17 | +- L6234 breakout board |
| 18 | +- HMBGC v2.2 |
| 19 | +- DRV830x ( can be run in 3pwm or 6pwm mode ) |
| 20 | +- X-NUCLEO-IHM07M1 |
| 21 | +- etc. |
| 22 | + |
| 23 | + |
| 24 | +<img src="extras/Images/3pwm_driver.png" class="width40"> |
| 25 | + |
| 26 | +## Step 1. Hardware setup |
| 27 | +To create the interface to the BLDC driver you need to specify the 3 `pwm` pin numbers for each motor phase and optionally `enable` pin. |
| 28 | +```cpp |
| 29 | +// BLDCDriver3PWM( int phA, int phB, int phC, int en) |
| 30 | +// - phA, phB, phC - A,B,C phase pwm pins |
| 31 | +// - enable pin - (optional input) |
| 32 | +BLDCDriver3PWM motor = BLDCDriver3PWM(9, 10, 11, 8); |
| 33 | +``` |
| 34 | + |
| 35 | +## Step 2.1 PWM Configuration |
| 36 | +```cpp |
| 37 | +// pwm frequency to be used [Hz] |
| 38 | +// for atmega328 fixed to 32kHz |
| 39 | +// esp32/stm32/teensy configurable |
| 40 | +driver.pwm_frequency = 50000; |
| 41 | +``` |
| 42 | +<blockquote class="warning"> |
| 43 | +⚠️ Arduino devices based on ATMega328 chips have fixed pwm frequency of 32kHz. |
| 44 | +</blockquote> |
| 45 | + |
| 46 | +Here is a list of different microcontrollers and their PWM frequency and resolution used with the Arduino <span class="simple">Simple<span class="foc">FOC</span>library</span>. |
| 47 | + |
| 48 | +MCU | default frequency | MAX frequency | PWM resolution | Center-aligned | Configurable freq |
| 49 | +--- | --- | --- | --- | --- |
| 50 | +Arduino UNO(Atmega328) | 32 kHz | 32 kHz | 8bit | yes | no |
| 51 | +STM32 | 50kHz | 100kHz | 14bit | yes | yes |
| 52 | +ESP32 | 40kHz | 100kHz | 10bit | yes | yes |
| 53 | +Teensy | 50kHz | 100kHz | 8bit | yes | yes |
| 54 | + |
| 55 | +All of these settings are defined in the `drivers/hardware_specific/x_mcu.cpp/h` of the library source. |
| 56 | + |
| 57 | + |
| 58 | +## Step 2.2 Voltages |
| 59 | +Driver class is the one that handles setting the pwm duty cycles to the driver output pins and it is needs to know the DC power supply voltage it is plugged to. |
| 60 | +Additionally driver class enables the user to set the absolute DC voltage limit the driver will be set to the output pins. |
| 61 | +```cpp |
| 62 | +// power supply voltage [V] |
| 63 | +driver.voltage_power_supply = 12; |
| 64 | +// Max DC voltage allowed - default voltage_power_supply |
| 65 | +driver.voltage_limit = 12; |
| 66 | +``` |
| 67 | + |
| 68 | +<img src="extras/Images/limits.png" class="width60"> |
| 69 | + |
| 70 | +This parameter is used by the `BLDCMotor` class as well. As shown on the figure above the once the voltage limit `driver.voltage_limit` is set, it will be communicated to the FOC algorithm in `BLDCMotor` class and the phase voltages will be centered around the `driver.voltage_limit/2`. |
| 71 | + |
| 72 | +Therefore this parameter is very important if there is concern of too high currents generated by the motor. In those cases this parameter can be used as a security feature. |
| 73 | + |
| 74 | +## Step 2.3 Initialisation |
| 75 | +Once when all the necessary configuration parameters are set the driver function `init()` is called. This function uses the configuration parameters and configures all the necessary hardware and software for driver code execution. |
| 76 | +```cpp |
| 77 | +// driver init |
| 78 | +driver.init(); |
| 79 | +``` |
| 80 | + |
| 81 | +## Step 3. Using encoder in real-time |
| 82 | + |
| 83 | +BLDC driver class was developed to be used with the <span class="simple">Simple<span class="foc">FOC</span>library</span> and to provide the abstraction layer for FOC algorithm implemented in the `BLDCMotor` class. But the `BLDCDriver3PWM` class can used as a standalone class as well and once can choose to implement any other type of control algorithm using the bldc driver. |
| 84 | + |
| 85 | +## FOC algorithm support |
| 86 | +In the context of the FOC control all the driver usage is done internally by the motion control algorithm and all that is needed to enable is is just link the driver to the `BLDCMotor` class. |
| 87 | +```cpp |
| 88 | +// linking the driver to the motor |
| 89 | +motor.linkDriver(&driver) |
| 90 | +``` |
| 91 | + |
| 92 | +## Standalone driver |
| 93 | +If you wish to use the bldc driver as a standalone device and implement your-own logic around it this can be easily done. Here is an example code of a very simple standalone application. |
| 94 | +```cpp |
| 95 | +// BLDC driver standalone example |
| 96 | +#include <SimpleFOC.h> |
| 97 | + |
| 98 | +// BLDC driver instance |
| 99 | +BLDCDriver3PWM driver = BLDCDriver3PWM(9, 5, 6, 8); |
| 100 | + |
| 101 | +void setup() { |
| 102 | + |
| 103 | + // pwm frequency to be used [Hz] |
| 104 | + driver.pwm_frequency = 50000; |
| 105 | + // power supply voltage [V] |
| 106 | + driver.voltage_power_supply = 12; |
| 107 | + // Max DC voltage allowed - default voltage_power_supply |
| 108 | + driver.voltage_limit = 12; |
| 109 | + |
| 110 | + // driver init |
| 111 | + driver.init(); |
| 112 | + |
| 113 | + // enable driver |
| 114 | + driver.enable(); |
| 115 | + |
| 116 | + _delay(1000); |
| 117 | +} |
| 118 | + |
| 119 | +void loop() { |
| 120 | + // setting pwm |
| 121 | + // phase A: 3V, phase B: 6V, phase C: 5V |
| 122 | + driver.setPwm(3,6,5); |
| 123 | +} |
| 124 | +``` |
0 commit comments