Skip to content

Commit 72a7a43

Browse files
authored
Merge pull request #60 from esp-cpp/feature/bldc_haptics
Feature/bldc haptics
2 parents 81bfda0 + e3beb88 commit 72a7a43

File tree

98 files changed

+2422
-986
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

98 files changed

+2422
-986
lines changed

.github/workflows/build.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@ jobs:
1919
target: esp32s3
2020
- path: 'components/aw9523/example'
2121
target: esp32
22+
- path: 'components/bldc_haptics/example'
23+
target: esp32s3
2224
- path: 'components/bldc_motor/example'
2325
target: esp32s3
2426
- path: 'components/controller/example'

components/bldc_driver/include/bldc_driver.hpp

Lines changed: 45 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ class BldcDriver {
3333
int gpio_c_h; /**< Phase C high side gpio. */
3434
int gpio_c_l; /**< Phase C low side gpio. */
3535
int gpio_enable{-1}; /**< Enable pin for the BLDC driver (if any). */
36+
int gpio_fault{-1}; /**< Fault pin for the BLDC driver (if any). */
3637
float power_supply_voltage; /**< Voltage of the power supply. */
3738
float limit_voltage{-1}; /**< What voltage the motor should be limited to. Less than 0 means no
3839
limit. Will be clamped to power supply voltage. */
@@ -50,7 +51,7 @@ class BldcDriver {
5051
: gpio_ah_((gpio_num_t)config.gpio_a_h), gpio_al_((gpio_num_t)config.gpio_a_l),
5152
gpio_bh_((gpio_num_t)config.gpio_b_h), gpio_bl_((gpio_num_t)config.gpio_b_l),
5253
gpio_ch_((gpio_num_t)config.gpio_c_h), gpio_cl_((gpio_num_t)config.gpio_c_l),
53-
gpio_en_(config.gpio_enable), dead_zone_(config.dead_zone),
54+
gpio_en_(config.gpio_enable), gpio_fault_(config.gpio_fault), dead_zone_(config.dead_zone),
5455
logger_({.tag = "BLDC Driver", .level = config.log_level}) {
5556
configure_power(config.power_supply_voltage, config.limit_voltage);
5657
init(config);
@@ -87,8 +88,30 @@ class BldcDriver {
8788
// set force level to 0 (gate off), and hold it
8889
mcpwm_generator_set_force_level(g, 0, true);
8990
}
90-
gpio_set_level((gpio_num_t)gpio_en_, 0);
9191
enabled_ = false;
92+
std::lock_guard<std::mutex> lock(en_mutex_);
93+
if (gpio_en_ >= 0) {
94+
gpio_set_level((gpio_num_t)gpio_en_, 0);
95+
}
96+
}
97+
98+
/**
99+
* @brief Check if the driver is enabled.
100+
* @return True if the driver is enabled, false otherwise.
101+
*/
102+
bool is_enabled() const { return enabled_; }
103+
104+
/**
105+
* @brief Check if the driver is faulted.
106+
* @note If no fault pin was provided, this will always return false.
107+
* @return True if the driver is faulted, false otherwise.
108+
*/
109+
bool is_faulted() {
110+
std::lock_guard<std::mutex> lock(fault_mutex_);
111+
if (gpio_fault_ < 0) {
112+
return false;
113+
}
114+
return gpio_get_level((gpio_num_t)gpio_fault_) == 1;
92115
}
93116

94117
/**
@@ -195,6 +218,7 @@ class BldcDriver {
195218
protected:
196219
void init(const Config &config) {
197220
configure_enable_gpio();
221+
configure_fault_gpio();
198222
configure_timer();
199223
configure_operators();
200224
configure_comparators();
@@ -208,6 +232,7 @@ class BldcDriver {
208232
}
209233

210234
void configure_enable_gpio() {
235+
std::lock_guard<std::mutex> lock(en_mutex_);
211236
if (gpio_en_ < 0) {
212237
return;
213238
}
@@ -220,6 +245,21 @@ class BldcDriver {
220245
gpio_set_level((gpio_num_t)gpio_en_, 0);
221246
}
222247

248+
void configure_fault_gpio() {
249+
std::lock_guard<std::mutex> lock(fault_mutex_);
250+
if (gpio_fault_ < 0) {
251+
return;
252+
}
253+
logger_.info("Configure fault pin");
254+
gpio_config_t drv_fault_config;
255+
memset(&drv_fault_config, 0, sizeof(drv_fault_config));
256+
drv_fault_config.pin_bit_mask = 1ULL << gpio_fault_;
257+
drv_fault_config.mode = GPIO_MODE_INPUT;
258+
drv_fault_config.pull_up_en = GPIO_PULLUP_DISABLE;
259+
drv_fault_config.pull_down_en = GPIO_PULLDOWN_ENABLE;
260+
ESP_ERROR_CHECK(gpio_config(&drv_fault_config));
261+
}
262+
223263
void configure_timer() {
224264
logger_.info("Create MCPWM timer");
225265
mcpwm_timer_config_t timer_config;
@@ -307,7 +347,10 @@ class BldcDriver {
307347
gpio_num_t gpio_bl_;
308348
gpio_num_t gpio_ch_;
309349
gpio_num_t gpio_cl_;
350+
std::mutex en_mutex_;
310351
int gpio_en_;
352+
std::mutex fault_mutex_;
353+
int gpio_fault_;
311354
std::atomic<float> power_supply_voltage_;
312355
std::atomic<float> limit_voltage_;
313356
float dead_zone_;
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
idf_component_register(
2+
INCLUDE_DIRS "include"
3+
REQUIRES logger math pid task bldc_motor
4+
)
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
# The following lines of boilerplate have to be in your project's CMakeLists
2+
# in this exact order for cmake to work correctly
3+
cmake_minimum_required(VERSION 3.5)
4+
5+
include($ENV{IDF_PATH}/tools/cmake/project.cmake)
6+
7+
# add the component directories that we want to use
8+
set(EXTRA_COMPONENT_DIRS
9+
"../../../components/"
10+
)
11+
12+
set(
13+
COMPONENTS
14+
"main esptool_py filters task monitor mt6701 bldc_motor bldc_driver bldc_haptics"
15+
CACHE STRING
16+
"List of components to include"
17+
)
18+
19+
project(bldc_motor_example)
20+
21+
set(CMAKE_CXX_STANDARD 20)
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
_Note that this is a template for an ESP-IDF example README.md file. When using this template, replace all these emphasised placeholders with example-specific content._
2+
3+
| Supported Targets | _Supported target, e.g. ESP32_ | _Another supported target, e.g. ESP32-S3_ |
4+
| ----------------- | ------------------------------ | ----------------------------------------- |
5+
6+
_If the example supports all targets supported by ESP-IDF then the table can be omitted_
7+
# _Example Title_
8+
9+
(See the README.md file in the upper level 'examples' directory for more information about examples.)
10+
11+
_What is this example? What does it do?_
12+
13+
_What features of ESP-IDF does it use?_
14+
15+
_What could someone create based on this example? ie applications/use cases/etc_
16+
17+
_If there are any acronyms or Espressif-only words used here, explain them or mention where in the datasheet/TRM this information can be found._
18+
19+
## How to use example
20+
21+
### Hardware Required
22+
23+
_If possible, example should be able to run on any commonly available ESP32 development board. Otherwise, describe what specific hardware should be used._
24+
25+
_If any other items (server, BLE device, app, second chip, whatever) are needed, mention them here. Include links if applicable. Explain how to set them up._
26+
27+
### Configure the project
28+
29+
```
30+
idf.py menuconfig
31+
```
32+
33+
* _If there is any project configuration that the user must set for this example, mention this here._
34+
35+
### Build and Flash
36+
37+
Build the project and flash it to the board, then run monitor tool to view serial output:
38+
39+
```
40+
idf.py -p PORT flash monitor
41+
```
42+
43+
(Replace PORT with the name of the serial port to use.)
44+
45+
(To exit the serial monitor, type ``Ctrl-]``.)
46+
47+
See the Getting Started Guide for full steps to configure and use ESP-IDF to build projects.
48+
49+
## Example Output
50+
51+
_Include an example of the console output from the running example, here:_
52+
53+
```
54+
Use this style for pasting the log.
55+
```
56+
57+
_If the user is supposed to interact with the example at this point (read/write GATT attribute, send HTTP request, press button, etc. then mention it here)_
58+
59+
_For examples where ESP32 is connected with some other hardware, include a table or schematics with connection details._
60+
61+
## Troubleshooting
62+
63+
_If there are any likely problems or errors which many users might encounter, mention them here. Remove this section for very simple examples where nothing is likely to go wrong._
64+
65+
## Example Breakdown
66+
67+
_If the example source code is lengthy, complex, or cannot be easily understood, use this section to break down and explain the source code. This can be done by breaking down the execution path step by step, or explaining what each major function/task/source file does. Add sub titles if necessary. Remove this section for very simple examples where the source code is self explanatory._
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
idf_component_register(SRC_DIRS "."
2+
INCLUDE_DIRS ".")

0 commit comments

Comments
 (0)