Skip to content

Commit

Permalink
Merge branch 'fix/esp_bldc_control_drag' into 'master'
Browse files Browse the repository at this point in the history
fix: Modify drag time type and filter cap

See merge request ae_group/esp-iot-solution!1026
  • Loading branch information
YanKE01 committed Jul 3, 2024
2 parents 5460c83 + c358714 commit f8b0f21
Show file tree
Hide file tree
Showing 23 changed files with 547 additions and 102 deletions.
4 changes: 2 additions & 2 deletions components/.build-rules.yml
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,8 @@ components/motor/esp_sensorless_bldc_control/test_apps/bldc_adc:
- if: SOC_MCPWM_SUPPORTED == 1

components/motor/esp_sensorless_bldc_control/test_apps/bldc_comparer:
disable:
- if: IDF_TARGET in ["esp32c2"]
enable:
- if: SOC_MCPWM_SUPPORTED == 1

components/led/lightbulb_driver/test_apps:
enable:
Expand Down
8 changes: 8 additions & 0 deletions components/motor/esp_sensorless_bldc_control/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,13 @@
# ChangeLog


## v0.3.0 - 2024-6-20

### Bug fix

* Fix drag time and modify the filter cap to type uint32_t
* Fix Comparator Acquisition Method for MCPWM Trigger

## v0.2.0 - 2023-11-14

### Enhancements
Expand Down
30 changes: 15 additions & 15 deletions components/motor/esp_sensorless_bldc_control/README.md
Original file line number Diff line number Diff line change
@@ -1,39 +1,39 @@
[![Component Registry](https://components.espressif.com/components/espressif/esp_sensorless_bldc_control/badge.svg)](https://components.espressif.com/components/espressif/esp_sensorless_bldc_control)

## ESP Sensorless bldc control 组件介绍
## ESP Sensorless BLDC Control Component Introduction

``esp_sensorless_bldc_control`` 是基于 ESP32 系列芯片的 BLDC 无感方波控制库,支持以下功能:
``esp_sensorless_bldc_control`` is a sensorless BLDC control library based on ESP32 series chips, supporting the following features:

* 支持基于 ADC 采样检测过零点
* 支持基于比较器检测过零点
* 支持基于脉冲法实现转子初始相位检测
* 支持堵转保护
* 支持过流,过欠压保护[feature]
* 支持缺相保护[feature]
* Supports zero-crossing detection based on ADC sampling
* Supports zero-crossing detection based on comparators
* Supports initial rotor position detection using pulse method
* Supports stall protection
* Supports overcurrent and undervoltage protection [feature]
* Supports phase loss protection [feature]

### ESP Sensorless bldc control 用户指南
### ESP Sensorless BLDC Control User Guide

请参考https://docs.espressif.com/projects/esp-iot-solution/zh_CN/latest/motor/index.html
Please refer tohttps://docs.espressif.com/projects/esp-iot-solution/zh_CN/latest/motor/index.html

### 添加组件到工程
### Adding the Component to Your Project

1. 请使用组件管理器指令 `add-dependency` `esp_sensorless_bldc_control` 添加到项目的依赖项, 在 `CMake` 执行期间该组件将被自动下载到工程目录。
1. Use the component manager command `add-dependency` to add `esp_sensorless_bldc_control` to your project's dependencies. The component will be automatically downloaded to the project directory during `CMake` execution.

```
idf.py add-dependency "espressif/esp_sensorless_bldc_control=*"
```
2. `esp_sensorless_bldc_control/user_cfg/bldc_user_cfg.h.tpl` 文件复制到工程目录下,并更名为 `bldc_user_cfg.h`。并在 `main/CMakeLists.txt` 文件中加入:
2. Copy the `esp_sensorless_bldc_control/user_cfg/bldc_user_cfg.h` file to your project directory and rename it to `bldc_user_cfg.h`. Add the following to your `main/CMakeLists.txt` file:
```
idf_component_get_property(bldc_lib espressif__esp_sensorless_bldc_control COMPONENT_LIB)
cmake_policy(SET CMP0079 NEW)
target_link_libraries(${bldc_lib} PUBLIC ${COMPONENT_LIB})
```
Note: 该文件用于设置电机控制相关参数,一定要包含在工程中。
Note: This file is used to set motor control related parameters and must be included in the project.
## 使用示例
## Example Usage
```C
esp_event_loop_create_default();
Expand Down
15 changes: 12 additions & 3 deletions components/motor/esp_sensorless_bldc_control/bldc_control.c
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* SPDX-FileCopyrightText: 2023 Espressif Systems (Shanghai) CO LTD
* SPDX-FileCopyrightText: 2023-2024 Espressif Systems (Shanghai) CO LTD
*
* SPDX-License-Identifier: Apache-2.0
*/
Expand Down Expand Up @@ -103,13 +103,19 @@ esp_err_t bldc_control_init(bldc_control_handle_t *handle, bldc_control_config_t
xTaskCreate(bldc_control_task, "bldc_control_task", 1024 * 4, control, 10, NULL);

switch (config->alignment_mode) {
case ALIGNMENT_COMPARER:
case ALIGNMENT_COMPARER: {
ret = bldc_zero_cross_comparer_init(&control->zero_cross_handle, &config->zero_cross_comparer_config, &control->control_param);
BLDC_CHECK_GOTO(ret == ESP_OK, "bldc_zero_cross_comparer_init failed", deinit);
control->zero_cross = &bldc_zero_cross_comparer_operation;
mcpwm_timer_event_callbacks_t cbs = {
.on_full = &read_comparer_on_full,
};
config->six_step_config.upper_switch_config.bldc_mcpwm.cbs = &cbs;
config->six_step_config.upper_switch_config.bldc_mcpwm.timer_cb_user_data = (void *)control->zero_cross_handle;
break;
}
#if CONFIG_SOC_MCPWM_SUPPORTED
case ALIGNMENT_ADC:
case ALIGNMENT_ADC: {
ret = bldc_zero_cross_adc_init(&control->zero_cross_handle, &config->zero_cross_adc_config, &control->control_param);
BLDC_CHECK_GOTO(ret == ESP_OK, "bldc_zero_cross_comparer_init failed", deinit);
control->zero_cross = &bldc_zero_cross_adc_operation;
Expand All @@ -123,6 +129,7 @@ esp_err_t bldc_control_init(bldc_control_handle_t *handle, bldc_control_config_t
config->six_step_config.upper_switch_config.bldc_mcpwm.cbs = &cbs;
config->six_step_config.upper_switch_config.bldc_mcpwm.timer_cb_user_data = (void *)control->zero_cross_handle;
break;
}
#endif
default:
ESP_LOGE(TAG, "control_mode error");
Expand Down Expand Up @@ -276,6 +283,8 @@ esp_err_t bldc_control_stop(bldc_control_handle_t *handle)
default:
break;
}

bldc_control_dispatch_event(BLDC_CONTROL_STOP, NULL, 0);
return ESP_OK;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* SPDX-FileCopyrightText: 2023 Espressif Systems (Shanghai) CO LTD
* SPDX-FileCopyrightText: 2023-2024 Espressif Systems (Shanghai) CO LTD
*
* SPDX-License-Identifier: Apache-2.0
*/
Expand All @@ -15,7 +15,7 @@ typedef struct {
control_param_t *control_param;
int alignment_pin[PHASE_MAX];
int zero_stable_flag; /*!< Over-zero stabilization flag bit */
uint32_t alignment_queue_value[PHASE_MAX];
uint64_t alignment_queue_value[PHASE_MAX];
uint16_t queue_filter_state[PHASE_MAX]; /*!< State after three-phase filtering */
} bldc_zero_cross_comparer_t;

Expand Down Expand Up @@ -48,6 +48,13 @@ static uint8_t bldc_umef_edge(uint8_t val)
return 2;
}

bool read_comparer_on_full(mcpwm_timer_handle_t timer, const mcpwm_timer_event_data_t *edata, void *user_data)
{
bldc_zero_cross_comparer_t *zero_cross = (bldc_zero_cross_comparer_t *)user_data;
alignment_comparer_get_value(zero_cross);
return false;
}

esp_err_t bldc_zero_cross_comparer_init(bldc_zero_cross_comparer_handle_t *handle, bldc_zero_cross_comparer_config_t *config, control_param_t *control_param)
{
BLDC_CHECK(config != NULL, "bldc zero cross config is NULL", ESP_ERR_INVALID_ARG);
Expand Down Expand Up @@ -92,7 +99,6 @@ uint8_t bldc_zero_cross_comparer_operation(void *handle)

bldc_zero_cross_comparer_t *zero_cross = (bldc_zero_cross_comparer_t *)handle;
uint16_t filterEdge; /*!< Edge detection after filtering */
alignment_comparer_get_value(zero_cross);
zero_cross->control_param->speed_count++;

for (int i = 0; i < PHASE_MAX; i++) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ typedef struct {
uint8_t phase_change_done; /*!< Motor phase change successful */
uint16_t duty; /*!< duty cycle */
uint16_t duty_max; /*!< duty cycle max */
uint16_t drag_time; /*!< drag time */
int16_t drag_time; /*!< drag time */
alignment_mode_t alignment_mode; /*!< alignment mode */
/* speed */
uint32_t expect_speed_rpm; /*!< Expected speed */
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,16 @@ typedef struct {
*/
typedef void *bldc_zero_cross_comparer_handle_t;

/**
* @brief Used to detect the value of gpio when the top tube and bottom tube are open
*
* @param timer MCPWM timer handle
* @param edata MCPWM timer event data, fed by driver
* @param user_data User data, set in `mcpwm_timer_register_event_callbacks()`
* @return Whether a high priority task has been waken up by this function
*/
bool read_comparer_on_full(mcpwm_timer_handle_t timer, const mcpwm_timer_event_data_t *edata, void *user_data);

/**
* @brief Initialize zero cross comparer
*
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,10 @@
version: 0.2.0
version: 0.3.0
targets:
- esp32
- esp32s3
- esp32c6
- esp32h2
- esp32p4
description: ESP32 Sensorless BLDC Control Component
url: https://github.com/espressif/esp-iot-solution/tree/master/components/motor/esp_sensorless_bldc_control
repository: https://github.com/espressif/esp-iot-solution.git
Expand Down
34 changes: 18 additions & 16 deletions examples/motor/bldc_fan_rainmaker/README.md
Original file line number Diff line number Diff line change
@@ -1,22 +1,24 @@
# bldc_fan_rainmaker

`bldc_fan_rainmaker` 例程将无刷电机驱动的电风扇接入 ESP Rainmaker 云,实现了以下功能
The `bldc_fan_rainmaker` example connects a brushless motor-driven fan to the ESP Rainmaker cloud, achieving the following functionalities:

* 风扇无极调速
* 摇头
* 自然风
* 远程启动关闭
* OTA
* BLE 配网
* Stepless fan speed control
* Oscillation
* Natural wind mode
* Remote start and stop
* OTA updates
* BLE provisioning

## 组件介绍
![rainmaker_fan](https://dl.espressif.com/AE/esp-iot-solution/esp_bldc_rainmaker.gif)

* [esp_sensorless_bldc_control](https://components.espressif.com/components/espressif/esp_sensorless_bldc_control) 是基于 ESP32 系列芯片的 BLDC 无感方波控制库。支持以下功能:
* 支持基于 ADC 采样检测过零点
* 支持基于比较器检测过零点
* 支持基于脉冲法实现转子初始相位检测
* 支持堵转保护
* 支持过流,过欠压保护[feature]
* 支持缺相保护[feature]
## Component Overview

* [esp_rainmaker](https://components.espressif.com/components/espressif/esp_rainmaker) 是一个完整且轻量级的 AIoT 解决方案,能够以简单、经济高效且高效的方式为您的企业实现私有云部署。
* [esp_sensorless_bldc_control](https://components.espressif.com/components/espressif/esp_sensorless_bldc_control) is a sensorless BLDC square wave control library based on the ESP32 series chips. It supports the following features:
* Zero-crossing detection based on ADC sampling
* Zero-crossing detection based on a comparator
* Initial rotor position detection using pulse method
* Stall protection
* Overcurrent, over/under-voltage protection [feature]
* Phase loss protection [feature]

* [esp_rainmaker](https://components.espressif.com/components/espressif/esp_rainmaker) is a complete and lightweight AIoT solution that enables private cloud deployment for your business in a simple, cost-effective, and efficient manner.
15 changes: 12 additions & 3 deletions examples/motor/bldc_fan_rainmaker/main/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,15 @@ set(INC_DIRS "app/include/" "hal/include/")
idf_component_register(SRCS ${SRC_FILES} ${SOURCES}
INCLUDE_DIRS "." ${INC_DIRS}
)
idf_component_get_property(bldc_lib esp_sensorless_bldc_control COMPONENT_LIB)
cmake_policy(SET CMP0079 NEW)
target_link_libraries(${bldc_lib} PUBLIC ${COMPONENT_LIB})

idf_build_get_property(build_components BUILD_COMPONENTS)
if("esp_sensorless_bldc_control" IN_LIST build_components)
idf_component_get_property(bldc_lib esp_sensorless_bldc_control COMPONENT_LIB)
cmake_policy(SET CMP0079 NEW)
target_link_libraries(${bldc_lib} PUBLIC ${COMPONENT_LIB})
endif()
if("espressif__esp_sensorless_bldc_control" IN_LIST build_components)
idf_component_get_property(bldc_lib espressif__esp_sensorless_bldc_control COMPONENT_LIB)
cmake_policy(SET CMP0079 NEW)
target_link_libraries(${bldc_lib} PUBLIC ${COMPONENT_LIB})
endif()
61 changes: 43 additions & 18 deletions examples/motor/bldc_fan_rainmaker/main/app/app_rainmaker.c
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
/*
* SPDX-FileCopyrightText: 2023 Espressif Systems (Shanghai) CO LTD
* SPDX-FileCopyrightText: 2023-2024 Espressif Systems (Shanghai) CO LTD
*
* SPDX-License-Identifier: Apache-2.0
*/

#include "app_rainmaker.h"
#include "esp_rmaker_core.h"
#include "esp_rmaker_standard_types.h"
#include "esp_rmaker_standard_params.h"
#include "esp_rmaker_standard_devices.h"
Expand All @@ -23,13 +22,14 @@
#include "app_variable.h"
#include "hal_bldc.h"
#include "hal_stepper_motor.h"
#include "bldc_fan_config.h"

static const char *TAG = "APP_RMKER";
esp_rmaker_device_t *fan_device;
esp_rmaker_param_t *mode_param;
esp_rmaker_param_t *speed_param;
esp_rmaker_param_t *power_param;
esp_rmaker_param_t *shake_param;
const static char *TAG = "app_rainmaker";
static esp_rmaker_device_t *fan_device;
static esp_rmaker_param_t *mode_param;
static esp_rmaker_param_t *speed_param;
static esp_rmaker_param_t *power_param;
static esp_rmaker_param_t *shake_param;

static esp_err_t app_rainmaker_write_cb(const esp_rmaker_device_t *device, const esp_rmaker_param_t *param, esp_rmaker_param_val_t val, void *priv_data, esp_rmaker_write_ctx_t *ctx)
{
Expand All @@ -43,6 +43,10 @@ static esp_err_t app_rainmaker_write_cb(const esp_rmaker_device_t *device, const
val.val.b ? "true" : "false", esp_rmaker_device_get_name(device),
esp_rmaker_param_get_name(param));
motor_parameter.is_start = val.val.b ? 1 : 0;
if (motor_parameter.is_start == 0) {
motor_parameter.start_count = 0;
motor_parameter.target_speed = BLDC_MIN_SPEED;
}
hal_bldc_start_stop(motor_parameter.is_start);
} else if (strcmp(esp_rmaker_param_get_name(param), ESP_RMAKER_DEF_SPEED_NAME) == 0) {
//*!< get target speed value */
Expand Down Expand Up @@ -156,15 +160,8 @@ static void app_rainmaker_event_handler(void *arg, esp_event_base_t event_base,
}
}

esp_err_t app_rainmaker_init()
static void rainmaker_task(void *arg)
{
esp_err_t err = nvs_flash_init(); //*!< init nvs */
if (err == ESP_ERR_NVS_NO_FREE_PAGES || err == ESP_ERR_NVS_NEW_VERSION_FOUND) {
ESP_ERROR_CHECK(nvs_flash_erase());
err = nvs_flash_init();
}
ESP_ERROR_CHECK(err);

esp_rmaker_console_init(); //*!< init rainmaker console */
app_wifi_init(); //*!< init wifi, choose wifi or ble */

Expand Down Expand Up @@ -215,13 +212,41 @@ esp_err_t app_rainmaker_init()
};
esp_rmaker_system_service_enable(&system_serv_config); //*!< enable system service */

esp_rmaker_schedule_enable(); //*!< enable schedule

esp_rmaker_start(); //*!< start rainmaker */
err = app_wifi_start(POP_TYPE_NONE);
esp_err_t err = app_wifi_start(POP_TYPE_NONE);
if (err != ESP_OK) {
ESP_LOGE(TAG, "Could not start Wifi. Aborting!!!");
vTaskDelay(5000 / portTICK_PERIOD_MS);
abort();
}

return ESP_OK;
vTaskDelete(NULL);
}

esp_err_t app_rainmaker_init()
{
esp_err_t err = nvs_flash_init(); //*!< init nvs */
if (err == ESP_ERR_NVS_NO_FREE_PAGES || err == ESP_ERR_NVS_NEW_VERSION_FOUND) {
ESP_ERROR_CHECK(nvs_flash_erase());
err = nvs_flash_init();
}
ESP_ERROR_CHECK(err);
xTaskCreate(rainmaker_task, "rainmaker_task", 4096, NULL, 5, NULL);
return ESP_OK; //*!< create stepper motor task */
}

esp_rmaker_param_t *app_rainmaker_get_param(const char *name)
{
if (!strcmp(name, "Power")) {
return power_param;
} else if (!strcmp(name, "Speed")) {
return speed_param;
} else if (!strcmp(name, "Natural")) {
return mode_param;
} else if (!strcmp(name, "Shake")) {
return shake_param;
}
return NULL;
}
9 changes: 5 additions & 4 deletions examples/motor/bldc_fan_rainmaker/main/app/app_variable.c
Original file line number Diff line number Diff line change
@@ -1,18 +1,19 @@
/*
* SPDX-FileCopyrightText: 2023 Espressif Systems (Shanghai) CO LTD
* SPDX-FileCopyrightText: 2023-2024 Espressif Systems (Shanghai) CO LTD
*
* SPDX-License-Identifier: Apache-2.0
*/

#include "app_variable.h"
#include "string.h"
#include "bldc_fan_config.h"

motor_parameter_t motor_parameter;

void app_variable_init()
{
memset(&motor_parameter, 0x00, sizeof(motor_parameter));
motor_parameter.target_speed = 300; /*!< motor target speed */
motor_parameter.min_speed = 300; /*!< motor min speed */
motor_parameter.max_speed = 1000; /*!< motor max speed */
motor_parameter.target_speed = BLDC_MIN_SPEED; /*!< motor target speed */
motor_parameter.min_speed = BLDC_MIN_SPEED; /*!< motor min speed */
motor_parameter.max_speed = BLDC_MAX_SPEED; /*!< motor max speed */
}
Loading

0 comments on commit f8b0f21

Please sign in to comment.