|
1 | 1 | --- |
2 | 2 | layout: default |
3 | | -title: BLDC Motor |
| 3 | +title: Motor & Driver |
4 | 4 | nav_order: 2 |
5 | 5 | parent: Using the Code |
6 | 6 | permalink: /motor_initialization |
7 | 7 | grand_parent: Arduino <span class="simple">Simple<span class="foc">FOC</span>library</span> |
8 | 8 | --- |
9 | 9 |
|
10 | | -# Motor setup |
| 10 | +# Motor & Driver configuration |
11 | 11 |
|
12 | 12 | <div class="width60"> |
13 | | -<img src="extras/Images/mot2.jpg" style="width:24%;display:inline"><img src="extras/Images/bigger.jpg" style="width:24%;display:inline"><img src="extras/Images/big.jpg" style="width:24%;display:inline"><img src="extras/Images/mot.jpg" style="width:24%;display:inline"> |
| 13 | +<img src="extras/Images/mot2.jpg" style="width:20%;display:inline"><img src="extras/Images/bigger.jpg" style="width:20%;display:inline"><img src="extras/Images/mot.jpg" style="width:20%;display:inline"><img src="extras/Images/nema17_2.jpg" style="width:20%;display:inline"><img src="extras/Images/nema17_1.jpg" style="width:20%;display:inline"> |
14 | 14 | </div> |
15 | 15 |
|
16 | | -All BLDC motors are handled with the `BLDCMotor` class. This class is the heart ❤️ of the Arduino <span class="simple">Simple<span class="foc">FOC</span>library</span>. <br> |
17 | | -It implements: |
18 | | -- BLDC motor & BLDC driver hardware support |
| 16 | +All BLDC motors are handled with the `BLDCMotor` class and stepper motor are handled by `StepperMotor` class. These classes are the heart ❤️ of the Arduino <span class="simple">Simple<span class="foc">FOC</span>library</span>. <br> |
| 17 | +They implement: |
| 18 | +- BLDC/Stepper motor configuration |
| 19 | +- BLDC/Stepper driver hardware support |
19 | 20 | - FOC algorithm |
20 | 21 | - Motion control loops |
21 | 22 | - Monitoring |
22 | 23 | - User communication interface |
23 | 24 |
|
24 | 25 | ## Step 1. Hardware setup |
25 | | -To initialize the hardware of the BLDC motor and BLDC driver you need to specify the `pwm` pin numbers, number of `pole pairs` of the motor and optionally `enable` pin. |
| 26 | +To configure the BLDC motor and define the interface to the BLDC driver you need to specify the 3 `pwm` pin numbers for each motor phase, number of `pole pairs` of the motor and optionally `enable` pin. |
26 | 27 | ```cpp |
27 | 28 | // BLDCMotor( int phA, int phB, int phC, int pp, int en) |
28 | 29 | // - phA, phB, phC - motor A,B,C phase pwm pins |
29 | 30 | // - pp - pole pair number |
30 | 31 | // - enable pin - (optional input) |
31 | 32 | BLDCMotor motor = BLDCMotor(9, 10, 11, 11, 8); |
32 | 33 | ``` |
| 34 | + |
| 35 | +For the stepper motor and stepper driver you need to specify the 4 `pwm` pin numbers (2 for each phase), number of `pole pairs` of the motor and optionally `enable1` and `enable2` pin numbers. |
| 36 | +```cpp |
| 37 | +// StepperMotor( int phA, int phB, int phC, int pp, int cpr, int en) |
| 38 | +// - ph1A, ph1B - motor phase 1 pwm pins |
| 39 | +// - ph2A, ph2B - motor phase 2 pwm pins |
| 40 | +// - pp - pole pair number |
| 41 | +// - enable1 pin - (optional input) |
| 42 | +// - enable2 pin - (optional input) |
| 43 | +StepperMotor motor = StepperMotor(5,6, 9,10, 50, 7, 8); |
| 44 | +``` |
33 | 45 | <blockquote class="info"><p class="heading">Pole pair number </p> |
34 | | -If you are not sure what your `pole_paris` number is I included an example code to estimate your <code class="highlighter-rouge">pole_paris</code> number in the examples <code class="highlighter-rouge">find_pole_pairs_number.ino</code>. |
| 46 | +If you are not sure what your <code class="highlighter-rouge">pole_paris</code> number is I included an example code to estimate your <code class="highlighter-rouge">pole_paris</code> number in the examples <code class="highlighter-rouge">find_pole_pairs_number.ino</code>. |
35 | 47 | </blockquote> |
36 | 48 |
|
37 | | -When you have your `motor` defined you can start the configuration. |
| 49 | +The important thing to note is that the rest of the configuration explained in the following text does not depend which motor you are using. Therefore, once when you have your `motor` defined you can start the configuration. |
38 | 50 |
|
39 | 51 | ## Step 2. Linking the sensor |
40 | 52 | Once when you have the `motor` defined and the sensor initialized you need to link the `motor` and the `sensor` by executing: |
@@ -138,6 +150,29 @@ Finally the configuration is terminated by running `init()` function which prepa |
138 | 150 | motor.init(); |
139 | 151 | ``` |
140 | 152 |
|
| 153 | +### Step 3.3 PWM frequency configuration (optional) |
| 154 | +If you wish to change the PWM frequency provide the value in hertz to the `init()` function. For example, to set the pwm frequency of 30khz the code will look like: |
| 155 | +```cpp |
| 156 | +// Motor hardware init function |
| 157 | +motor.init(30000); |
| 158 | +``` |
| 159 | +<blockquote class="warning"> |
| 160 | +⚠️ Arduino devices based on ATMega328 and ATMega2560 chips have fixed pwm frequency of 32kHz. |
| 161 | +</blockquote> |
| 162 | + |
| 163 | +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>. |
| 164 | + |
| 165 | +MCU | default frequency | MAX frequency | PWM resolution | configurable |
| 166 | +--- | --- | --- | --- | --- |
| 167 | +Arduino UNO(Atmega328) | 32 kHz | 32 kHz | 8bit | no |
| 168 | +Arduino MEGA | 32kHz | 32kHz | 8bit | no |
| 169 | +STM32 | 50kHz | 50kHz | 14bit | yes |
| 170 | +ESP32 | 20kHz | 30kHz | 10bit | yes |
| 171 | +Teensy | 50kHz | 50kHz | 8bit | yes |
| 172 | + |
| 173 | +All of these settings are defined in the `hardware_utils.cpp/h` of the library source. |
| 174 | + |
| 175 | + |
141 | 176 | ## Step 4. Field Oriented Control initialization |
142 | 177 |
|
143 | 178 | After the motor and sensor are initialized and configured, and before we can start the motion control we need to initialize the FOC algorithm. |
@@ -186,7 +221,7 @@ The faster you can run this function the better! |
186 | 221 | Finally, when we can set the phase voltages to the motor using the FOC algorithm we can proceed to the motion control. And this is done with `motor.move()` function. |
187 | 222 |
|
188 | 223 | ```cpp |
189 | | -// Function executing the control loops configured by the motor.controller parameter of the BLDCMotor. |
| 224 | +// Function executing the control loops configured by the motor.controller parameter of the motor. |
190 | 225 | // - This function doesn't need to be run upon each loop execution - depends of the use case |
191 | 226 | // |
192 | 227 | // target Either voltage, angle or velocity based on the motor.controller |
|
0 commit comments