添加了回调任务的支持,允许bsp将回调函数设置为在中断中唤醒任务而不是直接执行,有助于提高实时性
This commit is contained in:
parent
e938075e11
commit
393923cc0b
1
Makefile
1
Makefile
|
@ -117,6 +117,7 @@ bsp/usart/bsp_usart.c \
|
||||||
bsp/usb/bsp_usb.c \
|
bsp/usb/bsp_usb.c \
|
||||||
bsp/log/bsp_log.c \
|
bsp/log/bsp_log.c \
|
||||||
bsp/flash/bsp_flash.c \
|
bsp/flash/bsp_flash.c \
|
||||||
|
bsp/bsp_tools.c \
|
||||||
modules/algorithm/controller.c \
|
modules/algorithm/controller.c \
|
||||||
modules/algorithm/kalman_filter.c \
|
modules/algorithm/kalman_filter.c \
|
||||||
modules/algorithm/QuaternionEKF.c \
|
modules/algorithm/QuaternionEKF.c \
|
||||||
|
|
|
@ -58,7 +58,7 @@ void StartINSTASK(void const *argument)
|
||||||
static float ins_start, ins_dt;
|
static float ins_start, ins_dt;
|
||||||
INS_Init(); // 确保BMI088被正确初始化.
|
INS_Init(); // 确保BMI088被正确初始化.
|
||||||
LOGINFO("[freeRTOS] INS Task Start");
|
LOGINFO("[freeRTOS] INS Task Start");
|
||||||
while (1)
|
for (;;)
|
||||||
{
|
{
|
||||||
// 1kHz
|
// 1kHz
|
||||||
ins_start = DWT_GetTimeline_ms();
|
ins_start = DWT_GetTimeline_ms();
|
||||||
|
@ -75,7 +75,7 @@ void StartMOTORTASK(void const *argument)
|
||||||
{
|
{
|
||||||
static float motor_dt, motor_start;
|
static float motor_dt, motor_start;
|
||||||
LOGINFO("[freeRTOS] MOTOR Task Start");
|
LOGINFO("[freeRTOS] MOTOR Task Start");
|
||||||
while (1)
|
for (;;)
|
||||||
{
|
{
|
||||||
motor_start = DWT_GetTimeline_ms();
|
motor_start = DWT_GetTimeline_ms();
|
||||||
MotorControlTask();
|
MotorControlTask();
|
||||||
|
@ -91,7 +91,7 @@ void StartDAEMONTASK(void const *argument)
|
||||||
static float daemon_dt, daemon_start;
|
static float daemon_dt, daemon_start;
|
||||||
BuzzerInit();
|
BuzzerInit();
|
||||||
LOGINFO("[freeRTOS] Daemon Task Start");
|
LOGINFO("[freeRTOS] Daemon Task Start");
|
||||||
while (1)
|
for (;;)
|
||||||
{
|
{
|
||||||
// 100Hz
|
// 100Hz
|
||||||
daemon_start = DWT_GetTimeline_ms();
|
daemon_start = DWT_GetTimeline_ms();
|
||||||
|
@ -109,7 +109,7 @@ void StartROBOTTASK(void const *argument)
|
||||||
static float robot_dt, robot_start;
|
static float robot_dt, robot_start;
|
||||||
LOGINFO("[freeRTOS] ROBOT core Task Start");
|
LOGINFO("[freeRTOS] ROBOT core Task Start");
|
||||||
// 200Hz-500Hz,若有额外的控制任务如平衡步兵可能需要提升至1kHz
|
// 200Hz-500Hz,若有额外的控制任务如平衡步兵可能需要提升至1kHz
|
||||||
while (1)
|
for (;;)
|
||||||
{
|
{
|
||||||
robot_start = DWT_GetTimeline_ms();
|
robot_start = DWT_GetTimeline_ms();
|
||||||
RobotTask();
|
RobotTask();
|
||||||
|
@ -125,7 +125,7 @@ void StartUITASK(void const *argument)
|
||||||
LOGINFO("[freeRTOS] UI Task Start");
|
LOGINFO("[freeRTOS] UI Task Start");
|
||||||
MyUIInit();
|
MyUIInit();
|
||||||
LOGINFO("[freeRTOS] UI Init Done, communication with ref has established");
|
LOGINFO("[freeRTOS] UI Init Done, communication with ref has established");
|
||||||
while (1)
|
for (;;)
|
||||||
{
|
{
|
||||||
// 每给裁判系统发送一包数据会挂起一次,详见UITask函数的refereeSend()
|
// 每给裁判系统发送一包数据会挂起一次,详见UITask函数的refereeSend()
|
||||||
UITask();
|
UITask();
|
||||||
|
|
|
@ -11,7 +11,7 @@
|
||||||
* 需在实时系统启动前调用,目前由RobotoInit()调用
|
* 需在实时系统启动前调用,目前由RobotoInit()调用
|
||||||
*
|
*
|
||||||
* @note 其他实例型的外设如CAN和串口会在注册实例的时候自动初始化,不注册不初始化
|
* @note 其他实例型的外设如CAN和串口会在注册实例的时候自动初始化,不注册不初始化
|
||||||
*/
|
*/
|
||||||
//
|
//
|
||||||
void BSPInit()
|
void BSPInit()
|
||||||
{
|
{
|
||||||
|
|
|
@ -0,0 +1,56 @@
|
||||||
|
#include <stdarg.h>
|
||||||
|
|
||||||
|
#include "cmsis_os.h"
|
||||||
|
#include "bsp_log.h"
|
||||||
|
#include "bsp_tools.h"
|
||||||
|
|
||||||
|
#define MX_SIG_LIST_SIZE 32 // 不可修改,最大信号量数量
|
||||||
|
|
||||||
|
typedef struct
|
||||||
|
{
|
||||||
|
void *callback;
|
||||||
|
uint32_t sig;
|
||||||
|
void *ins;
|
||||||
|
} CallbackTask_t;
|
||||||
|
|
||||||
|
// 递增记录全局信号量的数组,最大32个信号量
|
||||||
|
static uint8_t sig_idx = 0;
|
||||||
|
static uint32_t tmp_sig = 1; // tmp_sing << sig_idx从而生成对应信号量
|
||||||
|
|
||||||
|
static osThreadId cbkid_list[MX_SIG_LIST_SIZE];
|
||||||
|
static CallbackTask_t cbkinfo_list[MX_SIG_LIST_SIZE];
|
||||||
|
|
||||||
|
// 死循环任务,执行cbk函数指针,每次执行完毕后等待sig信号
|
||||||
|
static void CallbackTaskBase(const void *cbk)
|
||||||
|
{
|
||||||
|
void (*cbk_func)(void *) = (void (*)(void *))((CallbackTask_t *)cbk)->callback;
|
||||||
|
void *ins = ((CallbackTask_t *)cbk)->ins;
|
||||||
|
uint32_t sig = ((CallbackTask_t *)cbk)->sig;
|
||||||
|
|
||||||
|
for (;;)
|
||||||
|
{
|
||||||
|
cbk_func(ins);
|
||||||
|
osSignalWait(sig, osWaitForever);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
uint32_t CreateCallbackTask(char *name, void *cbk, void *ins, osPriority priority)
|
||||||
|
{
|
||||||
|
if (sig_idx >= MX_SIG_LIST_SIZE)
|
||||||
|
while (1)
|
||||||
|
LOGERROR("[rtos:cbk_register] CreateCallbackTask: sig_idx >= MX_SIG_LIST_SIZE");
|
||||||
|
|
||||||
|
cbkinfo_list[sig_idx].callback = cbk;
|
||||||
|
cbkinfo_list[sig_idx].sig = tmp_sig << sig_idx;
|
||||||
|
cbkinfo_list[sig_idx].ins = ins;
|
||||||
|
|
||||||
|
osThreadDef_t threadDef;
|
||||||
|
threadDef.name = name;
|
||||||
|
threadDef.pthread = CallbackTaskBase;
|
||||||
|
threadDef.tpriority = priority;
|
||||||
|
threadDef.instances = 0;
|
||||||
|
threadDef.stacksize = 128;
|
||||||
|
cbkid_list[sig_idx] = osThreadCreate(&threadDef, (void *)&cbkinfo_list[sig_idx]);
|
||||||
|
|
||||||
|
return cbkinfo_list[sig_idx++].sig; // 返回信号量,同时增加索引
|
||||||
|
}
|
|
@ -0,0 +1,13 @@
|
||||||
|
#include "cmsis_os.h"
|
||||||
|
#include "bsp_log.h"
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @brief 创建一个新的任务,该任务收到osSignalSet信号时会被唤醒,否则保持挂起状态
|
||||||
|
*
|
||||||
|
* @param name 任务名称,注意以'\0'结尾
|
||||||
|
* @param cbk 回调函数指针
|
||||||
|
* @param ins 回调函数的参数,bsp为实例指针
|
||||||
|
* @param priority 任务优先级,决定了在中断结束后能否保证立刻执行
|
||||||
|
* @return uint32_t 信号量,用于唤醒该任务
|
||||||
|
*/
|
||||||
|
uint32_t CreateCallbackTask(char *name, void *cbk, void *ins, osPriority priority);
|
|
@ -72,7 +72,7 @@ static void MotorSenderGrouping(DJIMotorInstance *motor, CAN_Init_Config_s *conf
|
||||||
{
|
{
|
||||||
LOGERROR("[dji_motor] ID crash. Check in debug mode, add dji_motor_instance to watch to get more information.");
|
LOGERROR("[dji_motor] ID crash. Check in debug mode, add dji_motor_instance to watch to get more information.");
|
||||||
uint16_t can_bus = config->can_handle == &hcan1 ? 1 : 2;
|
uint16_t can_bus = config->can_handle == &hcan1 ? 1 : 2;
|
||||||
while (1) // 后续可以把id和CAN打印出来 // 6020的id 1-4和2006/3508的id 5-8会发生冲突(若有注册,即1!5,2!6,3!7,4!8) (1!5!,LTC! (((不是)
|
while (1) // 6020的id 1-4和2006/3508的id 5-8会发生冲突(若有注册,即1!5,2!6,3!7,4!8) (1!5!,LTC! (((不是)
|
||||||
LOGERROR("[dji_motor] id [%d], can_bus [%d]", config->rx_id, can_bus);
|
LOGERROR("[dji_motor] id [%d], can_bus [%d]", config->rx_id, can_bus);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -101,7 +101,7 @@ static void MotorSenderGrouping(DJIMotorInstance *motor, CAN_Init_Config_s *conf
|
||||||
{
|
{
|
||||||
LOGERROR("[dji_motor] ID crash. Check in debug mode, add dji_motor_instance to watch to get more information.");
|
LOGERROR("[dji_motor] ID crash. Check in debug mode, add dji_motor_instance to watch to get more information.");
|
||||||
uint16_t can_bus = config->can_handle == &hcan1 ? 1 : 2;
|
uint16_t can_bus = config->can_handle == &hcan1 ? 1 : 2;
|
||||||
while (1) // 后续可以把id和CAN打印出来 // 6020的id 1-4和2006/3508的id 5-8会发生冲突(若有注册,即1!5,2!6,3!7,4!8) (1!5!,LTC! (((不是)
|
while (1) // 6020的id 1-4和2006/3508的id 5-8会发生冲突(若有注册,即1!5,2!6,3!7,4!8) (1!5!,LTC! (((不是)
|
||||||
LOGERROR("[dji_motor] id [%d], can_bus [%d]", config->rx_id, can_bus);
|
LOGERROR("[dji_motor] id [%d], can_bus [%d]", config->rx_id, can_bus);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue