From 957706859a3a1deda84192e762da336df9b63cdd Mon Sep 17 00:00:00 2001 From: Robofish <1683502971@qq.com> Date: Thu, 19 Feb 2026 12:38:12 +0800 Subject: [PATCH] =?UTF-8?q?=E5=8A=A0=E4=B8=80=E4=B8=AA=E8=B0=83=E8=AF=95?= =?UTF-8?q?=E7=BA=BF=E7=A8=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- CMakeLists.txt | 1 + User/task/config.yaml | 7 +++ User/task/ctrl_chassis.c | 1 - User/task/debug.c | 92 ++++++++++++++++++++++++++++++++++++++++ User/task/init.c | 1 + User/task/user_task.c | 5 +++ User/task/user_task.h | 10 ++++- 7 files changed, 115 insertions(+), 2 deletions(-) create mode 100644 User/task/debug.c diff --git a/CMakeLists.txt b/CMakeLists.txt index c267878..cef6493 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -97,6 +97,7 @@ target_sources(${CMAKE_PROJECT_NAME} PRIVATE User/task/ctrl_chassis.c User/task/ctrl_gimbal.c User/task/ctrl_shoot.c + User/task/debug.c User/task/init.c User/task/monitor.c User/task/rc.c diff --git a/User/task/config.yaml b/User/task/config.yaml index 787eb63..be01998 100644 --- a/User/task/config.yaml +++ b/User/task/config.yaml @@ -66,3 +66,10 @@ function: Task_cli name: cli stack: 1024 +- delay: 0 + description: '' + freq_control: true + frequency: 10.0 + function: Task_debug + name: debug + stack: 256 diff --git a/User/task/ctrl_chassis.c b/User/task/ctrl_chassis.c index 9859c36..b5d3439 100644 --- a/User/task/ctrl_chassis.c +++ b/User/task/ctrl_chassis.c @@ -100,7 +100,6 @@ void Task_ctrl_chassis(void *argument) { osMessageQueueReset(task_runtime.msgq.chassis.vofa); // 重置消息队列,防止阻塞 osMessageQueuePut(task_runtime.msgq.chassis.vofa, &chassis, 0, 0); /* USER CODE END */ - osDelayUntil(tick); /* 运行结束,等待下一次唤醒 */ } diff --git a/User/task/debug.c b/User/task/debug.c new file mode 100644 index 0000000..5c58566 --- /dev/null +++ b/User/task/debug.c @@ -0,0 +1,92 @@ +/* + debug Task + +*/ + +/* Includes ----------------------------------------------------------------- */ +#include "task/user_task.h" +#include "bsp/pwm.h" +#include "bsp/gpio.h" +#include +/* USER INCLUDE BEGIN */ + +/* USER INCLUDE END */ + +/* Private typedef ---------------------------------------------------------- */ +/* Private define ----------------------------------------------------------- */ +/* Private macro ------------------------------------------------------------ */ +/* Private variables -------------------------------------------------------- */ +/* USER STRUCT BEGIN */ +bool flag = false; +/* USER STRUCT END */ + +/* Private function --------------------------------------------------------- */ +/* USER PRIVATE CODE BEGIN */ +/* 可根据舵机实际行程调整脉宽范围 */ +#define SERVO_MIN_PULSE_MS 0.4f +#define SERVO_MAX_PULSE_MS 2.6f + +static float Servo_AngleToDuty(float angle_deg) { + if (angle_deg < 0.0f) { + angle_deg = 0.0f; + } + if (angle_deg > 180.0f) { + angle_deg = 180.0f; + } + + const float period_ms = 20.0f; /* 50 Hz */ + float pulse_ms = SERVO_MIN_PULSE_MS + + (SERVO_MAX_PULSE_MS - SERVO_MIN_PULSE_MS) * (angle_deg / 180.0f); + + return pulse_ms / period_ms; +} + +/* USER PRIVATE CODE END */ +/* Exported functions ------------------------------------------------------- */ +void Task_debug(void *argument) { + (void)argument; /* 未使用argument,消除警告 */ + + + /* 计算任务运行到指定频率需要等待的tick数 */ + const uint32_t delay_tick = osKernelGetTickFreq() / DEBUG_FREQ; + + osDelay(DEBUG_INIT_DELAY); /* 延时一段时间再开启任务 */ + + uint32_t tick = osKernelGetTickCount(); /* 控制任务运行频率的计时 */ + /* USER CODE INIT BEGIN */ + BSP_GPIO_WritePin(BSP_GPIO_POWER_5V,true); + BSP_PWM_SetFreq(BSP_PWM_TIM2_CH1, 50.0f); + BSP_PWM_Start(BSP_PWM_TIM2_CH1); + + /* USER CODE INIT END */ + + while (1) { + tick += delay_tick; /* 计算下一个唤醒时刻 */ + /* USER CODE BEGIN */ + static float angle_1 = 45.0f; + static float angle_2 = 90.0f; + // static float step = 1.0f; + + // BSP_PWM_SetComp(BSP_PWM_TIM2_CH1, Servo_AngleToDuty(angle)); + + // angle += step; + // if (angle >= 90.0f) { + // angle = 90.0f; + // step = -step; + // } else if (angle <= 45.0f) { + // angle = 45.0f; + // step = -step; + // } + if (flag) { + BSP_PWM_SetComp(BSP_PWM_TIM2_CH1, Servo_AngleToDuty(angle_1)); + flag = false; + } else { + BSP_PWM_SetComp(BSP_PWM_TIM2_CH1, Servo_AngleToDuty(angle_2)); + flag = true; + } + + /* USER CODE END */ + osDelayUntil(tick); /* 运行结束,等待下一次唤醒 */ + } + +} \ No newline at end of file diff --git a/User/task/init.c b/User/task/init.c index 546d0db..92671a5 100644 --- a/User/task/init.c +++ b/User/task/init.c @@ -42,6 +42,7 @@ void Task_Init(void *argument) { task_runtime.thread.ai = osThreadNew(Task_ai, NULL, &attr_ai); // task_runtime.thread.vofa = osThreadNew(Task_vofa, NULL, &attr_vofa); task_runtime.thread.cli = osThreadNew(Task_cli, NULL, &attr_cli); + // task_runtime.thread.debug = osThreadNew(Task_debug, NULL, &attr_debug); // 创建消息队列 /* USER MESSAGE BEGIN */ diff --git a/User/task/user_task.c b/User/task/user_task.c index cb2380f..e292216 100644 --- a/User/task/user_task.c +++ b/User/task/user_task.c @@ -58,4 +58,9 @@ const osThreadAttr_t attr_cli = { .name = "cli", .priority = osPriorityNormal, .stack_size = 1024 * 4, +}; +const osThreadAttr_t attr_debug = { + .name = "debug", + .priority = osPriorityNormal, + .stack_size = 256 * 4, }; \ No newline at end of file diff --git a/User/task/user_task.h b/User/task/user_task.h index 7f7dbdf..627e6af 100644 --- a/User/task/user_task.h +++ b/User/task/user_task.h @@ -21,10 +21,11 @@ extern "C" { #define CTRL_SHOOT_FREQ (500.0) #define AI_FREQ (500.0) #define VOFA_FREQ (500.0) +#define DEBUG_FREQ (10.0) /* 任务初始化延时ms */ #define TASK_INIT_DELAY (100u) -#define RC_INIT_DELAY (500u) +#define RC_INIT_DELAY (0) #define ATTI_ESIT_INIT_DELAY (0) #define CTRL_CHASSIS_INIT_DELAY (0) #define CTRL_GIMBAL_INIT_DELAY (0) @@ -34,6 +35,7 @@ extern "C" { #define AI_INIT_DELAY (0) #define VOFA_INIT_DELAY (0) #define CLI_INIT_DELAY (0) +#define DEBUG_INIT_DELAY (0) /* Exported defines --------------------------------------------------------- */ /* Exported macro ----------------------------------------------------------- */ @@ -53,6 +55,7 @@ typedef struct { osThreadId_t ai; osThreadId_t vofa; osThreadId_t cli; + osThreadId_t debug; } thread; /* USER MESSAGE BEGIN */ @@ -109,6 +112,7 @@ typedef struct { UBaseType_t ai; UBaseType_t vofa; UBaseType_t cli; + UBaseType_t debug; } stack_water_mark; /* 各任务运行频率 */ @@ -121,6 +125,7 @@ typedef struct { float ctrl_shoot; float ai; float vofa; + float debug; } freq; /* 任务最近运行时间 */ @@ -133,6 +138,7 @@ typedef struct { float ctrl_shoot; float ai; float vofa; + float debug; } last_up_time; } Task_Runtime_t; @@ -152,6 +158,7 @@ extern const osThreadAttr_t attr_ctrl_shoot; extern const osThreadAttr_t attr_ai; extern const osThreadAttr_t attr_vofa; extern const osThreadAttr_t attr_cli; +extern const osThreadAttr_t attr_debug; /* 任务函数声明 */ void Task_Init(void *argument); @@ -165,6 +172,7 @@ void Task_ctrl_shoot(void *argument); void Task_ai(void *argument); void Task_vofa(void *argument); void Task_cli(void *argument); +void Task_debug(void *argument); #ifdef __cplusplus }