加一个调试线程

This commit is contained in:
Robofish 2026-02-19 12:38:12 +08:00
parent 53fda34711
commit 957706859a
7 changed files with 115 additions and 2 deletions

View File

@ -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

View File

@ -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

View File

@ -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); /* 运行结束,等待下一次唤醒 */
}

92
User/task/debug.c Normal file
View File

@ -0,0 +1,92 @@
/*
debug Task
*/
/* Includes ----------------------------------------------------------------- */
#include "task/user_task.h"
#include "bsp/pwm.h"
#include "bsp/gpio.h"
#include <stdbool.h>
/* 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); /* 运行结束,等待下一次唤醒 */
}
}

View File

@ -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 */

View File

@ -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,
};

View File

@ -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
}