refactor(peripherals): avoid using esp private feature in examples

This commit is contained in:
morris
2026-06-18 10:34:25 +08:00
parent 3b3b62adee
commit 3b5f3bb672
6 changed files with 51 additions and 12 deletions
@@ -1,3 +1,3 @@
dependencies:
pid_ctrl: "^0.1.1"
pid_ctrl: "^0.3.0"
bdc_motor: "^0.2.0"
@@ -17,7 +17,7 @@
static const char *TAG = "example";
// Enable this config, we will print debug formated string, which in return can be captured and parsed by Serial-Studio
// Enable this config, we will print debug formatted string, which in return can be captured and parsed by Serial-Studio
#define SERIAL_STUDIO_DEBUG CONFIG_SERIAL_STUDIO_DEBUG
#define BDC_MCPWM_TIMER_RESOLUTION_HZ 10000000 // 10MHz, 1 tick = 0.1us
@@ -37,7 +37,7 @@ static const char *TAG = "example";
typedef struct {
bdc_motor_handle_t motor;
pcnt_unit_handle_t pcnt_encoder;
pid_ctrl_block_handle_t pid_ctrl;
pid_ctrl_block_handle_f_t pid_ctrl;
int report_pulses;
} motor_control_context_t;
@@ -46,7 +46,7 @@ static void pid_loop_cb(void *args)
static int last_pulse_count = 0;
motor_control_context_t *ctx = (motor_control_context_t *)args;
pcnt_unit_handle_t pcnt_unit = ctx->pcnt_encoder;
pid_ctrl_block_handle_t pid_ctrl = ctx->pid_ctrl;
pid_ctrl_block_handle_f_t pid_ctrl = ctx->pid_ctrl;
bdc_motor_handle_t motor = ctx->motor;
// get the result from rotary encoder
@@ -121,7 +121,7 @@ void app_main(void)
motor_ctrl_ctx.pcnt_encoder = pcnt_unit;
ESP_LOGI(TAG, "Create PID control block");
pid_ctrl_parameter_t pid_runtime_param = {
pid_ctrl_parameter_f_t pid_runtime_param = {
.kp = 0.6,
.ki = 0.4,
.kd = 0.2,
@@ -131,8 +131,8 @@ void app_main(void)
.max_integral = 1000,
.min_integral = -1000,
};
pid_ctrl_block_handle_t pid_ctrl = NULL;
pid_ctrl_config_t pid_config = {
pid_ctrl_block_handle_f_t pid_ctrl = NULL;
pid_ctrl_config_f_t pid_config = {
.init_param = pid_runtime_param,
};
ESP_ERROR_CHECK(pid_new_control_block(&pid_config, &pid_ctrl));
@@ -7,7 +7,6 @@
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "esp_log.h"
#include "esp_private/esp_clk.h"
#include "driver/mcpwm_cap.h"
#include "driver/gpio.h"
@@ -99,13 +98,16 @@ void app_main(void)
ESP_ERROR_CHECK(mcpwm_capture_timer_enable(cap_timer));
ESP_ERROR_CHECK(mcpwm_capture_timer_start(cap_timer));
uint32_t cap_timer_resolution;
ESP_ERROR_CHECK(mcpwm_capture_timer_get_resolution(cap_timer, &cap_timer_resolution));
uint32_t tof_ticks;
while (1) {
// trigger the sensor to start a new sample
gen_trig_output();
// wait for echo done signal
if (xTaskNotifyWait(0x00, ULONG_MAX, &tof_ticks, pdMS_TO_TICKS(1000)) == pdTRUE) {
float pulse_width_us = tof_ticks * (1000000.0 / esp_clk_apb_freq());
float pulse_width_us = tof_ticks * (1000000.0 / cap_timer_resolution);
if (pulse_width_us > 35000) {
// out of range
continue;