From dd27f0ba8b058ceb380eb61c9dd54b6772d90a33 Mon Sep 17 00:00:00 2001 From: Robofish <1683502971@qq.com> Date: Sat, 10 Jan 2026 01:39:42 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0music?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- CMakeLists.txt | 4 ++ Core/Inc/main.h | 2 + Core/Src/tim.c | 13 ++++ CtrBoard-H7_ALL.ioc | 109 ++++++++++++++++++--------------- User/bsp/bsp_config.yaml | 4 ++ User/bsp/pwm.c | 1 + User/bsp/pwm.h | 1 + User/device/device_config.yaml | 2 +- User/device/motor_dm.c | 26 +------- User/task/atti_esit.c | 4 +- User/task/blink.c | 57 +++++++++++++++++ User/task/config.yaml | 28 +++++++++ User/task/ctrl_chassis.c | 53 ++++++++++++++++ User/task/ctrl_gimbal.c | 44 +++++++++++++ User/task/init.c | 4 ++ User/task/monitor.c | 44 +++++++++++++ User/task/rc.c | 1 - User/task/user_task.c | 20 ++++++ User/task/user_task.h | 32 ++++++++++ 19 files changed, 373 insertions(+), 76 deletions(-) create mode 100644 User/task/blink.c create mode 100644 User/task/ctrl_chassis.c create mode 100644 User/task/ctrl_gimbal.c create mode 100644 User/task/monitor.c diff --git a/CMakeLists.txt b/CMakeLists.txt index 7d1ad52..9da878a 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -77,7 +77,11 @@ target_sources(${CMAKE_PROJECT_NAME} PRIVATE # User/task sources User/task/atti_esit.c + User/task/blink.c + User/task/ctrl_chassis.c + User/task/ctrl_gimbal.c User/task/init.c + User/task/monitor.c User/task/rc.c User/task/user_task.c ) diff --git a/Core/Inc/main.h b/Core/Inc/main.h index c0becd0..e147d20 100644 --- a/Core/Inc/main.h +++ b/Core/Inc/main.h @@ -67,6 +67,8 @@ void Error_Handler(void); #define ACCL_CS_GPIO_Port GPIOC #define GYRO_CS_Pin GPIO_PIN_3 #define GYRO_CS_GPIO_Port GPIOC +#define WS2812_Pin GPIO_PIN_7 +#define WS2812_GPIO_Port GPIOA #define DCMI_PWDN_Pin GPIO_PIN_5 #define DCMI_PWDN_GPIO_Port GPIOC #define IMU_HEAT_Pin GPIO_PIN_1 diff --git a/Core/Src/tim.c b/Core/Src/tim.c index 506740f..da17fc9 100644 --- a/Core/Src/tim.c +++ b/Core/Src/tim.c @@ -180,6 +180,10 @@ void MX_TIM3_Init(void) sConfigOC.Pulse = 0; sConfigOC.OCPolarity = TIM_OCPOLARITY_HIGH; sConfigOC.OCFastMode = TIM_OCFAST_DISABLE; + if (HAL_TIM_PWM_ConfigChannel(&htim3, &sConfigOC, TIM_CHANNEL_2) != HAL_OK) + { + Error_Handler(); + } if (HAL_TIM_PWM_ConfigChannel(&htim3, &sConfigOC, TIM_CHANNEL_4) != HAL_OK) { Error_Handler(); @@ -336,10 +340,19 @@ void HAL_TIM_MspPostInit(TIM_HandleTypeDef* timHandle) /* USER CODE END TIM3_MspPostInit 0 */ + __HAL_RCC_GPIOA_CLK_ENABLE(); __HAL_RCC_GPIOB_CLK_ENABLE(); /**TIM3 GPIO Configuration + PA7 ------> TIM3_CH2 PB1 ------> TIM3_CH4 */ + GPIO_InitStruct.Pin = WS2812_Pin; + GPIO_InitStruct.Mode = GPIO_MODE_AF_PP; + GPIO_InitStruct.Pull = GPIO_NOPULL; + GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW; + GPIO_InitStruct.Alternate = GPIO_AF2_TIM3; + HAL_GPIO_Init(WS2812_GPIO_Port, &GPIO_InitStruct); + GPIO_InitStruct.Pin = IMU_HEAT_Pin; GPIO_InitStruct.Mode = GPIO_MODE_AF_PP; GPIO_InitStruct.Pull = GPIO_NOPULL; diff --git a/CtrBoard-H7_ALL.ioc b/CtrBoard-H7_ALL.ioc index 8ce2224..acb1683 100644 --- a/CtrBoard-H7_ALL.ioc +++ b/CtrBoard-H7_ALL.ioc @@ -182,63 +182,64 @@ Mcu.Pin12=PA1 Mcu.Pin13=PA2 Mcu.Pin14=PA3 Mcu.Pin15=PA5 -Mcu.Pin16=PC4 -Mcu.Pin17=PC5 -Mcu.Pin18=PB0 -Mcu.Pin19=PB1 +Mcu.Pin16=PA7 +Mcu.Pin17=PC4 +Mcu.Pin18=PC5 +Mcu.Pin19=PB0 Mcu.Pin2=PC13 -Mcu.Pin20=PB2 -Mcu.Pin21=PE7 -Mcu.Pin22=PE8 -Mcu.Pin23=PE9 -Mcu.Pin24=PE10 -Mcu.Pin25=PE11 -Mcu.Pin26=PE12 -Mcu.Pin27=PE13 -Mcu.Pin28=PE15 -Mcu.Pin29=PB10 +Mcu.Pin20=PB1 +Mcu.Pin21=PB2 +Mcu.Pin22=PE7 +Mcu.Pin23=PE8 +Mcu.Pin24=PE9 +Mcu.Pin25=PE10 +Mcu.Pin26=PE11 +Mcu.Pin27=PE12 +Mcu.Pin28=PE13 +Mcu.Pin29=PE15 Mcu.Pin3=PC14-OSC32_IN -Mcu.Pin30=PB11 -Mcu.Pin31=PB12 -Mcu.Pin32=PB13 -Mcu.Pin33=PB14 -Mcu.Pin34=PB15 -Mcu.Pin35=PD8 -Mcu.Pin36=PD9 -Mcu.Pin37=PD10 -Mcu.Pin38=PD11 -Mcu.Pin39=PD12 +Mcu.Pin30=PB10 +Mcu.Pin31=PB11 +Mcu.Pin32=PB12 +Mcu.Pin33=PB13 +Mcu.Pin34=PB14 +Mcu.Pin35=PB15 +Mcu.Pin36=PD8 +Mcu.Pin37=PD9 +Mcu.Pin38=PD10 +Mcu.Pin39=PD11 Mcu.Pin4=PC15-OSC32_OUT -Mcu.Pin40=PD13 -Mcu.Pin41=PA8 -Mcu.Pin42=PA9 -Mcu.Pin43=PA10 -Mcu.Pin44=PA11 -Mcu.Pin45=PA12 -Mcu.Pin46=PA13(JTMS/SWDIO) -Mcu.Pin47=PA14(JTCK/SWCLK) -Mcu.Pin48=PA15(JTDI) -Mcu.Pin49=PC12 +Mcu.Pin40=PD12 +Mcu.Pin41=PD13 +Mcu.Pin42=PA8 +Mcu.Pin43=PA9 +Mcu.Pin44=PA10 +Mcu.Pin45=PA11 +Mcu.Pin46=PA12 +Mcu.Pin47=PA13(JTMS/SWDIO) +Mcu.Pin48=PA14(JTCK/SWCLK) +Mcu.Pin49=PA15(JTDI) Mcu.Pin5=PH0-OSC_IN -Mcu.Pin50=PD0 -Mcu.Pin51=PD1 -Mcu.Pin52=PD2 -Mcu.Pin53=PD4 -Mcu.Pin54=PD5 -Mcu.Pin55=PD6 -Mcu.Pin56=PD7 -Mcu.Pin57=PB3(JTDO/TRACESWO) -Mcu.Pin58=PB5 -Mcu.Pin59=PB6 +Mcu.Pin50=PC12 +Mcu.Pin51=PD0 +Mcu.Pin52=PD1 +Mcu.Pin53=PD2 +Mcu.Pin54=PD4 +Mcu.Pin55=PD5 +Mcu.Pin56=PD6 +Mcu.Pin57=PD7 +Mcu.Pin58=PB3(JTDO/TRACESWO) +Mcu.Pin59=PB5 Mcu.Pin6=PH1-OSC_OUT -Mcu.Pin60=VP_FREERTOS_VS_CMSIS_V2 -Mcu.Pin61=VP_OCTOSPI1_VS_octo -Mcu.Pin62=VP_SYS_VS_tim23 -Mcu.Pin63=VP_MEMORYMAP_VS_MEMORYMAP +Mcu.Pin60=PB6 +Mcu.Pin61=VP_FREERTOS_VS_CMSIS_V2 +Mcu.Pin62=VP_OCTOSPI1_VS_octo +Mcu.Pin63=VP_SYS_VS_tim23 +Mcu.Pin64=VP_MEMORYMAP_VS_MEMORYMAP Mcu.Pin7=PC0 Mcu.Pin8=PC1 Mcu.Pin9=PC2_C -Mcu.PinsNb=64 +Mcu.PinsNb=65 Mcu.ThirdPartyNb=0 Mcu.UserConstants= Mcu.UserName=STM32H723VGTx @@ -306,6 +307,10 @@ PA3.Mode=OCTOSPI1_IOL_Port1L PA3.Signal=OCTOSPIM_P1_IO2 PA5.Locked=true PA5.Signal=ADCx_INP19 +PA7.GPIOParameters=GPIO_Label +PA7.GPIO_Label=WS2812 +PA7.Locked=true +PA7.Signal=S_TIM3_CH2 PA8.Locked=true PA8.Mode=Clock-out-1 PA8.Signal=RCC_MCO_1 @@ -609,6 +614,8 @@ SH.S_TIM2_CH1_ETR.0=TIM2_CH1,PWM Generation1 CH1 SH.S_TIM2_CH1_ETR.ConfNb=1 SH.S_TIM2_CH3.0=TIM2_CH3,PWM Generation3 CH3 SH.S_TIM2_CH3.ConfNb=1 +SH.S_TIM3_CH2.0=TIM3_CH2,PWM Generation2 CH2 +SH.S_TIM3_CH2.ConfNb=1 SH.S_TIM3_CH4.0=TIM3_CH4,PWM Generation4 CH4 SH.S_TIM3_CH4.ConfNb=1 SPI1.BaudRatePrescaler=SPI_BAUDRATEPRESCALER_4 @@ -647,8 +654,9 @@ TIM2.Prescaler=24 TIM2.Pulse-PWM\ Generation1\ CH1=5000 TIM2.Pulse-PWM\ Generation3\ CH3=5000 TIM3.AutoReloadPreload=TIM_AUTORELOAD_PRELOAD_ENABLE +TIM3.Channel-PWM\ Generation2\ CH2=TIM_CHANNEL_2 TIM3.Channel-PWM\ Generation4\ CH4=TIM_CHANNEL_4 -TIM3.IPParameters=Channel-PWM Generation4 CH4,Prescaler,Period,AutoReloadPreload +TIM3.IPParameters=Channel-PWM Generation4 CH4,Prescaler,Period,AutoReloadPreload,Channel-PWM Generation2 CH2 TIM3.Period=10000-1 TIM3.Prescaler=24-1 UART5.BaudRate=100000 @@ -683,3 +691,4 @@ VP_OCTOSPI1_VS_octo.Signal=OCTOSPI1_VS_octo VP_SYS_VS_tim23.Mode=TIM23 VP_SYS_VS_tim23.Signal=SYS_VS_tim23 board=custom +rtos.0.ip=FREERTOS diff --git a/User/bsp/bsp_config.yaml b/User/bsp/bsp_config.yaml index 18f4dd9..8384b4d 100644 --- a/User/bsp/bsp_config.yaml +++ b/User/bsp/bsp_config.yaml @@ -92,6 +92,10 @@ pwm: custom_name: TIM2_CH3 label: TIM2_CH3 timer: TIM2 + - channel: TIM_CHANNEL_2 + custom_name: WS2812 + label: WS2812 + timer: TIM3 - channel: TIM_CHANNEL_4 custom_name: IMU_HEAT label: IMU_HEAT diff --git a/User/bsp/pwm.c b/User/bsp/pwm.c index 9d3c275..65ee483 100644 --- a/User/bsp/pwm.c +++ b/User/bsp/pwm.c @@ -27,6 +27,7 @@ typedef struct { static const BSP_PWM_Config_t PWM_Map[BSP_PWM_NUM] = { {&htim2, TIM_CHANNEL_1}, {&htim2, TIM_CHANNEL_3}, + {&htim3, TIM_CHANNEL_2}, {&htim3, TIM_CHANNEL_4}, {&htim12, TIM_CHANNEL_2}, {&htim1, TIM_CHANNEL_3}, diff --git a/User/bsp/pwm.h b/User/bsp/pwm.h index da2407b..75be9b8 100644 --- a/User/bsp/pwm.h +++ b/User/bsp/pwm.h @@ -25,6 +25,7 @@ extern "C" { typedef enum { BSP_PWM_TIM2_CH1, BSP_PWM_TIM2_CH3, + BSP_PWM_WS2812, BSP_PWM_IMU_HEAT, BSP_PWM_BUZZER, BSP_PWM_TIM1_CH3, diff --git a/User/device/device_config.yaml b/User/device/device_config.yaml index 981bd38..33b643d 100644 --- a/User/device/device_config.yaml +++ b/User/device/device_config.yaml @@ -8,7 +8,7 @@ bmi088: enabled: true buzzer: bsp_config: - BSP_PWM_BUZZER: BSP_PWM_TIM2_CH1 + BSP_PWM_BUZZER: BSP_PWM_BUZZER enabled: true dr16: bsp_config: diff --git a/User/device/motor_dm.c b/User/device/motor_dm.c index 703c936..a77656f 100644 --- a/User/device/motor_dm.c +++ b/User/device/motor_dm.c @@ -111,21 +111,11 @@ static int8_t MOTOR_DM_SendMITCmd(MOTOR_DM_t *motor, MOTOR_MIT_Output_t *output) uint16_t pos_tmp,vel_tmp,kp_tmp,kd_tmp,tor_tmp; uint16_t id = motor->param.can_id; - /* 根据 reverse 参数调整控制值 */ - float angle = output->angle; - float velocity = output->velocity; - float torque = output->torque; - if (motor->param.reverse) { - angle = -angle; - velocity = -velocity; - torque = -torque; - } - - pos_tmp = float_to_uint(angle, DM_P_MIN , DM_P_MAX, 16); - vel_tmp = float_to_uint(velocity, DM_V_MIN , DM_V_MAX, 12); + pos_tmp = float_to_uint(output->angle, DM_P_MIN , DM_P_MAX, 16); + vel_tmp = float_to_uint(output->velocity, DM_V_MIN , DM_V_MAX, 12); kp_tmp = float_to_uint(output->kp, DM_KP_MIN, DM_KP_MAX, 12); kd_tmp = float_to_uint(output->kd, DM_KD_MIN, DM_KD_MAX, 12); - tor_tmp = float_to_uint(torque, DM_T_MIN , DM_T_MAX, 12); + tor_tmp = float_to_uint(output->torque, DM_T_MIN , DM_T_MAX, 12); /* 打包数据 */ data[0] = (pos_tmp >> 8); @@ -161,11 +151,6 @@ static int8_t MOTOR_DM_SendPosVelCmd(MOTOR_DM_t *motor, float pos, float vel) { uint8_t data[8] = {0}; - /* 根据 reverse 参数调整控制值 */ - if (motor->param.reverse) { - pos = -pos; - vel = -vel; - } /* 直接发送浮点数数据 */ memcpy(&data[0], &pos, 4); // 位置,低位在前 @@ -194,11 +179,6 @@ static int8_t MOTOR_DM_SendVelCmd(MOTOR_DM_t *motor, float vel) { uint8_t data[4] = {0}; - /* 根据 reverse 参数调整控制值 */ - if (motor->param.reverse) { - vel = -vel; - } - /* 直接发送浮点数数据 */ memcpy(&data[0], &vel, 4); // 速度,低位在前 diff --git a/User/task/atti_esit.c b/User/task/atti_esit.c index 6e91e67..7e1c6c7 100644 --- a/User/task/atti_esit.c +++ b/User/task/atti_esit.c @@ -1,6 +1,6 @@ /* atti_esit Task - + */ /* Includes ----------------------------------------------------------------- */ @@ -49,6 +49,7 @@ static const KPID_Params_t imu_temp_ctrl_pid_param = { void Task_atti_esit(void *argument) { (void)argument; /* 未使用argument,消除警告 */ + osDelay(ATTI_ESIT_INIT_DELAY); /* 延时一段时间再开启任务 */ /* USER CODE INIT BEGIN */ @@ -86,4 +87,5 @@ void Task_atti_esit(void *argument) { /* USER CODE END */ } + } \ No newline at end of file diff --git a/User/task/blink.c b/User/task/blink.c new file mode 100644 index 0000000..012f2b7 --- /dev/null +++ b/User/task/blink.c @@ -0,0 +1,57 @@ +/* + blink Task + +*/ + +/* Includes ----------------------------------------------------------------- */ +#include "task/user_task.h" +/* USER INCLUDE BEGIN */ +#include "device/buzzer.h" +#include +/* USER INCLUDE END */ + +/* Private typedef ---------------------------------------------------------- */ +/* Private define ----------------------------------------------------------- */ +/* Private macro ------------------------------------------------------------ */ +/* Private variables -------------------------------------------------------- */ +/* USER STRUCT BEGIN */ +BUZZER_t buzzer; +static uint16_t count; +/* USER STRUCT END */ + +/* Private function --------------------------------------------------------- */ +/* Exported functions ------------------------------------------------------- */ +void Task_blink(void *argument) { + (void)argument; /* 未使用argument,消除警告 */ + + + /* 计算任务运行到指定频率需要等待的tick数 */ + const uint32_t delay_tick = osKernelGetTickFreq() / BLINK_FREQ; + + osDelay(BLINK_INIT_DELAY); /* 延时一段时间再开启任务 */ + + uint32_t tick = osKernelGetTickCount(); /* 控制任务运行频率的计时 */ + /* USER CODE INIT BEGIN */ + /* 播放启动音乐 */ + BUZZER_Init(&buzzer, BSP_PWM_BUZZER); + BUZZER_PlayMusic(&buzzer, MUSIC_RM); + /* USER CODE INIT END */ + + while (1) { + tick += delay_tick; /* 计算下一个唤醒时刻 */ + /* USER CODE BEGIN */ + count++; + uint16_t phase = count % 1000; + if (phase == 0) { + /* 每秒开始播放C4音符 */ + BUZZER_Set(&buzzer, 261.63f, 0.5f); // C4音符频率约261.63Hz + BUZZER_Start(&buzzer); + } else if (phase == 50) { + /* 播放100ms后停止 (50/500Hz = 0.1s) */ + BUZZER_Stop(&buzzer); + } + /* USER CODE END */ + osDelayUntil(tick); /* 运行结束,等待下一次唤醒 */ + } + +} \ No newline at end of file diff --git a/User/task/config.yaml b/User/task/config.yaml index f1fa23a..28adab5 100644 --- a/User/task/config.yaml +++ b/User/task/config.yaml @@ -11,3 +11,31 @@ function: Task_atti_esit name: atti_esit stack: 256 +- delay: 0 + description: '' + freq_control: true + frequency: 500.0 + function: Task_ctrl_chassis + name: ctrl_chassis + stack: 256 +- delay: 0 + description: '' + freq_control: true + frequency: 500.0 + function: Task_ctrl_gimbal + name: ctrl_gimbal + stack: 256 +- delay: 0 + description: '' + freq_control: true + frequency: 500.0 + function: Task_monitor + name: monitor + stack: 256 +- delay: 0 + description: '' + freq_control: true + frequency: 500.0 + function: Task_blink + name: blink + stack: 256 diff --git a/User/task/ctrl_chassis.c b/User/task/ctrl_chassis.c new file mode 100644 index 0000000..4726893 --- /dev/null +++ b/User/task/ctrl_chassis.c @@ -0,0 +1,53 @@ +/* + ctrl_chassis Task +*/ + +/* Includes ----------------------------------------------------------------- */ +#include "task/user_task.h" +/* USER INCLUDE BEGIN */ +#include "bsp/can.h" +#include "device/motor.h" +#include "device/motor_lk.h" +#include "device/motor_lz.h" +/* USER INCLUDE END */ + +/* Private typedef ---------------------------------------------------------- */ +/* Private define ----------------------------------------------------------- */ +/* Private macro ------------------------------------------------------------ */ +/* Private variables -------------------------------------------------------- */ +/* USER STRUCT BEGIN */ +MOTOR_LK_Param_t chassis_motor_params[4] = { + {BSP_CAN_1, 0x201, MOTOR_LK_MF9025, false}, + {BSP_CAN_1, 0x202, MOTOR_LK_MF9025, false}, + {BSP_CAN_1, 0x203, MOTOR_LK_MF9025, false}, + {BSP_CAN_1, 0x204, MOTOR_LK_MF9025, false} +}; +/* USER STRUCT END */ + +/* Private function --------------------------------------------------------- */ +/* Exported functions ------------------------------------------------------- */ +void Task_ctrl_chassis(void *argument) { + (void)argument; /* 未使用argument,消除警告 */ + + + /* 计算任务运行到指定频率需要等待的tick数 */ + const uint32_t delay_tick = osKernelGetTickFreq() / CTRL_CHASSIS_FREQ; + + osDelay(CTRL_CHASSIS_INIT_DELAY); /* 延时一段时间再开启任务 */ + + uint32_t tick = osKernelGetTickCount(); /* 控制任务运行频率的计时 */ + /* USER CODE INIT BEGIN */ + BSP_CAN_Init(); + /* USER CODE INIT END */ + + while (1) { + tick += delay_tick; /* 计算下一个唤醒时刻 */ + /* USER CODE BEGIN */ + MOTOR_LK_SetOutput(&chassis_motor_params[0], 0.5f); + MOTOR_LK_Ctrl(&chassis_motor_params[0]); + + /* USER CODE END */ + osDelayUntil(tick); /* 运行结束,等待下一次唤醒 */ + } + +} \ No newline at end of file diff --git a/User/task/ctrl_gimbal.c b/User/task/ctrl_gimbal.c new file mode 100644 index 0000000..0182846 --- /dev/null +++ b/User/task/ctrl_gimbal.c @@ -0,0 +1,44 @@ +/* + ctrl_gimbal Task + +*/ + +/* Includes ----------------------------------------------------------------- */ +#include "task/user_task.h" +/* USER INCLUDE BEGIN */ + +/* USER INCLUDE END */ + +/* Private typedef ---------------------------------------------------------- */ +/* Private define ----------------------------------------------------------- */ +/* Private macro ------------------------------------------------------------ */ +/* Private variables -------------------------------------------------------- */ +/* USER STRUCT BEGIN */ + +/* USER STRUCT END */ + +/* Private function --------------------------------------------------------- */ +/* Exported functions ------------------------------------------------------- */ +void Task_ctrl_gimbal(void *argument) { + (void)argument; /* 未使用argument,消除警告 */ + + + /* 计算任务运行到指定频率需要等待的tick数 */ + const uint32_t delay_tick = osKernelGetTickFreq() / CTRL_GIMBAL_FREQ; + + osDelay(CTRL_GIMBAL_INIT_DELAY); /* 延时一段时间再开启任务 */ + + uint32_t tick = osKernelGetTickCount(); /* 控制任务运行频率的计时 */ + /* USER CODE INIT BEGIN */ + + /* USER CODE INIT END */ + + while (1) { + tick += delay_tick; /* 计算下一个唤醒时刻 */ + /* USER CODE BEGIN */ + + /* USER CODE END */ + osDelayUntil(tick); /* 运行结束,等待下一次唤醒 */ + } + +} \ No newline at end of file diff --git a/User/task/init.c b/User/task/init.c index 7a2a4da..1335718 100644 --- a/User/task/init.c +++ b/User/task/init.c @@ -32,6 +32,10 @@ void Task_Init(void *argument) { /* 创建任务线程 */ task_runtime.thread.rc = osThreadNew(Task_rc, NULL, &attr_rc); task_runtime.thread.atti_esit = osThreadNew(Task_atti_esit, NULL, &attr_atti_esit); + task_runtime.thread.ctrl_chassis = osThreadNew(Task_ctrl_chassis, NULL, &attr_ctrl_chassis); + task_runtime.thread.ctrl_gimbal = osThreadNew(Task_ctrl_gimbal, NULL, &attr_ctrl_gimbal); + task_runtime.thread.monitor = osThreadNew(Task_monitor, NULL, &attr_monitor); + task_runtime.thread.blink = osThreadNew(Task_blink, NULL, &attr_blink); // 创建消息队列 /* USER MESSAGE BEGIN */ diff --git a/User/task/monitor.c b/User/task/monitor.c new file mode 100644 index 0000000..55d5bd3 --- /dev/null +++ b/User/task/monitor.c @@ -0,0 +1,44 @@ +/* + monitor Task + +*/ + +/* Includes ----------------------------------------------------------------- */ +#include "task/user_task.h" +/* USER INCLUDE BEGIN */ + +/* USER INCLUDE END */ + +/* Private typedef ---------------------------------------------------------- */ +/* Private define ----------------------------------------------------------- */ +/* Private macro ------------------------------------------------------------ */ +/* Private variables -------------------------------------------------------- */ +/* USER STRUCT BEGIN */ + +/* USER STRUCT END */ + +/* Private function --------------------------------------------------------- */ +/* Exported functions ------------------------------------------------------- */ +void Task_monitor(void *argument) { + (void)argument; /* 未使用argument,消除警告 */ + + + /* 计算任务运行到指定频率需要等待的tick数 */ + const uint32_t delay_tick = osKernelGetTickFreq() / MONITOR_FREQ; + + osDelay(MONITOR_INIT_DELAY); /* 延时一段时间再开启任务 */ + + uint32_t tick = osKernelGetTickCount(); /* 控制任务运行频率的计时 */ + /* USER CODE INIT BEGIN */ + + /* USER CODE INIT END */ + + while (1) { + tick += delay_tick; /* 计算下一个唤醒时刻 */ + /* USER CODE BEGIN */ + + /* USER CODE END */ + osDelayUntil(tick); /* 运行结束,等待下一次唤醒 */ + } + +} \ No newline at end of file diff --git a/User/task/rc.c b/User/task/rc.c index 4ef9eed..5674578 100644 --- a/User/task/rc.c +++ b/User/task/rc.c @@ -4,7 +4,6 @@ */ /* Includes ----------------------------------------------------------------- */ -#include "device/dr16.h" #include "task/user_task.h" /* USER INCLUDE BEGIN */ #include "device/dr16.h" diff --git a/User/task/user_task.c b/User/task/user_task.c index e61d1b3..d1a428e 100644 --- a/User/task/user_task.c +++ b/User/task/user_task.c @@ -18,4 +18,24 @@ const osThreadAttr_t attr_atti_esit = { .name = "atti_esit", .priority = osPriorityNormal, .stack_size = 256 * 4, +}; +const osThreadAttr_t attr_ctrl_chassis = { + .name = "ctrl_chassis", + .priority = osPriorityNormal, + .stack_size = 256 * 4, +}; +const osThreadAttr_t attr_ctrl_gimbal = { + .name = "ctrl_gimbal", + .priority = osPriorityNormal, + .stack_size = 256 * 4, +}; +const osThreadAttr_t attr_monitor = { + .name = "monitor", + .priority = osPriorityNormal, + .stack_size = 256 * 4, +}; +const osThreadAttr_t attr_blink = { + .name = "blink", + .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 9a9e599..45ea231 100644 --- a/User/task/user_task.h +++ b/User/task/user_task.h @@ -14,11 +14,19 @@ extern "C" { /* Exported constants ------------------------------------------------------- */ /* 任务运行频率 */ #define RC_FREQ (500.0) +#define CTRL_CHASSIS_FREQ (500.0) +#define CTRL_GIMBAL_FREQ (500.0) +#define MONITOR_FREQ (500.0) +#define BLINK_FREQ (500.0) /* 任务初始化延时ms */ #define TASK_INIT_DELAY (100u) #define RC_INIT_DELAY (0) #define ATTI_ESIT_INIT_DELAY (0) +#define CTRL_CHASSIS_INIT_DELAY (0) +#define CTRL_GIMBAL_INIT_DELAY (0) +#define MONITOR_INIT_DELAY (0) +#define BLINK_INIT_DELAY (0) /* Exported defines --------------------------------------------------------- */ /* Exported macro ----------------------------------------------------------- */ @@ -30,6 +38,10 @@ typedef struct { struct { osThreadId_t rc; osThreadId_t atti_esit; + osThreadId_t ctrl_chassis; + osThreadId_t ctrl_gimbal; + osThreadId_t monitor; + osThreadId_t blink; } thread; /* USER MESSAGE BEGIN */ @@ -53,16 +65,28 @@ typedef struct { struct { UBaseType_t rc; UBaseType_t atti_esit; + UBaseType_t ctrl_chassis; + UBaseType_t ctrl_gimbal; + UBaseType_t monitor; + UBaseType_t blink; } stack_water_mark; /* 各任务运行频率 */ struct { float rc; + float ctrl_chassis; + float ctrl_gimbal; + float monitor; + float blink; } freq; /* 任务最近运行时间 */ struct { float rc; + float ctrl_chassis; + float ctrl_gimbal; + float monitor; + float blink; } last_up_time; } Task_Runtime_t; @@ -74,11 +98,19 @@ extern Task_Runtime_t task_runtime; extern const osThreadAttr_t attr_init; extern const osThreadAttr_t attr_rc; extern const osThreadAttr_t attr_atti_esit; +extern const osThreadAttr_t attr_ctrl_chassis; +extern const osThreadAttr_t attr_ctrl_gimbal; +extern const osThreadAttr_t attr_monitor; +extern const osThreadAttr_t attr_blink; /* 任务函数声明 */ void Task_Init(void *argument); void Task_rc(void *argument); void Task_atti_esit(void *argument); +void Task_ctrl_chassis(void *argument); +void Task_ctrl_gimbal(void *argument); +void Task_monitor(void *argument); +void Task_blink(void *argument); #ifdef __cplusplus }