sentry_chassis_hzz/application/robot_task.h

135 lines
4.1 KiB
C
Raw Normal View History

/* 注意该文件应只用于任务初始化,只能被robot.c包含*/
#pragma once
#include "FreeRTOS.h"
#include "task.h"
#include "main.h"
#include "cmsis_os.h"
#include "ins_task.h"
#include "motor_task.h"
#include "referee_task.h"
#include "master_process.h"
#include "daemon.h"
#include "HT04.h"
2023-07-07 19:27:24 +08:00
#include "buzzer.h"
#include "bsp_log.h"
osThreadId insTaskHandle;
osThreadId robotTaskHandle;
osThreadId motorTaskHandle;
osThreadId daemonTaskHandle;
osThreadId uiTaskHandle;
void StartINSTASK(void const *argument);
void StartMOTORTASK(void const *argument);
void StartDAEMONTASK(void const *argument);
void StartROBOTTASK(void const *argument);
void StartUITASK(void const *argument);
/**
* @brief ,
*
*/
void OSTaskInit()
{
osThreadDef(instask, StartINSTASK, osPriorityAboveNormal, 0, 1024);
insTaskHandle = osThreadCreate(osThread(instask), NULL); // 由于是阻塞读取传感器,为姿态解算设置较高优先级,确保以1khz的频率执行
// 后续修改为读取传感器数据准备好的中断处理,
osThreadDef(motortask, StartMOTORTASK, osPriorityNormal, 0, 256);
motorTaskHandle = osThreadCreate(osThread(motortask), NULL);
osThreadDef(daemontask, StartDAEMONTASK, osPriorityNormal, 0, 128);
daemonTaskHandle = osThreadCreate(osThread(daemontask), NULL);
osThreadDef(robottask, StartROBOTTASK, osPriorityNormal, 0, 1024);
robotTaskHandle = osThreadCreate(osThread(robottask), NULL);
osThreadDef(uitask, StartUITASK, osPriorityNormal, 0, 512);
uiTaskHandle = osThreadCreate(osThread(uitask), NULL);
HTMotorControlInit(); // 没有注册HT电机则不会执行
}
void StartINSTASK(void const *argument)
{
static float ins_start, ins_dt;
INS_Init(); // 确保BMI088被正确初始化.
LOGINFO("[freeRTOS] INS Task Start");
while (1)
{
// 1kHz
ins_start = DWT_GetTimeline_ms();
INS_Task();
ins_dt = DWT_GetTimeline_ms() - ins_start;
if (ins_dt > 1)
LOGERROR("[freeRTOS] INS Task is being DELAY! dt = [%f]", &ins_dt);
VisionSend(); // 解算完成后发送视觉数据,但是当前的实现不太优雅,后续若添加硬件触发需要重新考虑结构的组织
osDelay(1);
}
}
void StartMOTORTASK(void const *argument)
{
static float motor_dt, motor_start;
LOGINFO("[freeRTOS] MOTOR Task Start");
while (1)
{
motor_start = DWT_GetTimeline_ms();
MotorControlTask();
motor_dt = DWT_GetTimeline_ms() - motor_start;
if (motor_dt > 1)
LOGERROR("[freeRTOS] MOTOR Task is being DELAY! dt = [%f]", &motor_dt);
osDelay(1);
}
}
void StartDAEMONTASK(void const *argument)
{
static float daemon_dt, daemon_start;
2023-07-07 19:27:24 +08:00
buzzer_init();
LOGINFO("[freeRTOS] Daemon Task Start");
while (1)
{
// 100Hz
daemon_start = DWT_GetTimeline_ms();
DaemonTask();
2023-07-07 19:27:24 +08:00
BuzzerTask();
daemon_dt = DWT_GetTimeline_ms() - daemon_start;
if (daemon_dt > 10)
LOGERROR("[freeRTOS] Daemon Task is being DELAY! dt = [%f]", &daemon_dt);
osDelay(10);
}
}
void StartROBOTTASK(void const *argument)
{
static float robot_dt, robot_start;
LOGINFO("[freeRTOS] ROBOT core Task Start");
// 200Hz-500Hz,若有额外的控制任务如平衡步兵可能需要提升至1kHz
while (1)
{
robot_start = DWT_GetTimeline_ms();
RobotTask();
robot_dt = DWT_GetTimeline_ms() - robot_start;
if (robot_dt > 5)
LOGERROR("[freeRTOS] ROBOT core Task is being DELAY! dt = [%f]", &robot_dt);
osDelay(5);
}
}
void StartUITASK(void const *argument)
{
LOGINFO("[freeRTOS] UI Task Start");
MyUIInit();
LOGINFO("[freeRTOS] UI Init Done, communication with ref has established");
while (1)
{
// 每给裁判系统发送一包数据会挂起一次,详见UITask函数的refereeSend()
UITask();
osDelay(1); // 即使没有任何UI需要刷新,也挂起一次,防止卡在UITask中无法切换
}
}