小自瞄

This commit is contained in:
Robofish 2025-01-17 00:22:21 +08:00
parent 9e763bae27
commit 6c7e02f5c7
14 changed files with 9416 additions and 9291 deletions

5
.vscode/settings.json vendored Normal file
View File

@ -0,0 +1,5 @@
{
"files.associations": {
"user_math.h": "c"
}
}

View File

@ -74,7 +74,7 @@ void MX_FREERTOS_Init(void);
* @retval int
*/
int main(void)
{
{
/* USER CODE BEGIN 1 */
/* USER CODE END 1 */

View File

@ -407,6 +407,11 @@
<WinNumber>1</WinNumber>
<ItemText>rc</ItemText>
</Ww>
<Ww>
<count>10</count>
<WinNumber>1</WinNumber>
<ItemText>cmd_host</ItemText>
</Ww>
</WatchWindow1>
<Tracepoint>
<THDelay>0</THDelay>
@ -658,7 +663,7 @@
<Group>
<GroupName>Application/User/USB_DEVICE/App</GroupName>
<tvExp>1</tvExp>
<tvExp>0</tvExp>
<tvExpOptDlg>0</tvExpOptDlg>
<cbSel>0</cbSel>
<RteFlg>0</RteFlg>
@ -1214,7 +1219,7 @@
<Group>
<GroupName>Middlewares/USB_Device_Library</GroupName>
<tvExp>1</tvExp>
<tvExp>0</tvExp>
<tvExpOptDlg>0</tvExpOptDlg>
<cbSel>0</cbSel>
<RteFlg>0</RteFlg>
@ -1494,7 +1499,7 @@
<Group>
<GroupName>User/component</GroupName>
<tvExp>1</tvExp>
<tvExp>0</tvExp>
<tvExpOptDlg>0</tvExpOptDlg>
<cbSel>0</cbSel>
<RteFlg>0</RteFlg>
@ -1670,7 +1675,7 @@
<Group>
<GroupName>User/device</GroupName>
<tvExp>1</tvExp>
<tvExp>0</tvExp>
<tvExpOptDlg>0</tvExpOptDlg>
<cbSel>0</cbSel>
<RteFlg>0</RteFlg>
@ -1750,7 +1755,7 @@
<Group>
<GroupName>User/module</GroupName>
<tvExp>1</tvExp>
<tvExp>0</tvExp>
<tvExpOptDlg>0</tvExpOptDlg>
<cbSel>0</cbSel>
<RteFlg>0</RteFlg>

Binary file not shown.

File diff suppressed because it is too large Load Diff

View File

@ -14,6 +14,7 @@ extern "C" {
#define AI_NOTICE_HITBUFF (1 << 1)
#define AI_NOTICE_AUTOMATIC (1 << 2)
#define AI_NOTICE_FIRE (1 << 3)
#define AI_NOTICE_TRACKING (1 << 4)
#define AI_ID_MCU (0xC4)
#define AI_ID_REF (0xA8)
@ -53,13 +54,14 @@ typedef struct __attribute__((packed)) {
/* 视觉 -> 电控 数据结构体*/
typedef struct __attribute__((packed)) {
struct __attribute__((packed)) {
float yaw; /* 偏航角Yaw angle */
float pit; /* 俯仰角Pitch angle */
float rol; /* 翻滚角Roll angle */
} gimbal; /* 欧拉角 */
float distance; /* 距离 */
float distance; /*目标距离*/
uint8_t notice; /* 控制命令 */
struct __attribute__((packed)) {

View File

@ -10,3 +10,4 @@ void Ballistics_Apply(Ballistics_t *b, float bullet_speed) {
(void)bullet_speed;
}
void Ballistics_Reset(Ballistics_t *b) { (void)b; }

View File

@ -192,12 +192,14 @@ static void CMD_RcLogic(const CMD_RC_t *rc, CMD_t *cmd, float dt_sec) {
cmd->chassis.mode = CHASSIS_MODE_BREAK;
cmd->host_overwrite = false;
cmd->shoot.ai_fire = false;
cmd->ai_status = AI_STATUS_STOP;
break;
case CMD_SW_MID:
cmd->chassis.mode = CHASSIS_MODE_FOLLOW_GIMBAL;
cmd->host_overwrite = true;
cmd->shoot.ai_fire = false;
cmd->ai_status = AI_STATUS_AUTOMATIC;
break;
case CMD_SW_DOWN:
@ -205,12 +207,14 @@ static void CMD_RcLogic(const CMD_RC_t *rc, CMD_t *cmd, float dt_sec) {
cmd->chassis.mode_rotor = ROTOR_MODE_CW;
cmd->host_overwrite = true;
cmd->shoot.ai_fire = true;
cmd->ai_status = AI_STATUS_AUTOMATIC;
break;
case CMD_SW_ERR:
cmd->chassis.mode = CHASSIS_MODE_RELAX;
cmd->host_overwrite = false;
cmd->shoot.ai_fire = false;
cmd->ai_status = AI_STATUS_STOP;
break;
}
switch (rc->sw_r) {
@ -221,7 +225,7 @@ static void CMD_RcLogic(const CMD_RC_t *rc, CMD_t *cmd, float dt_sec) {
break;
case CMD_SW_MID:
cmd->gimbal.mode = GIMBAL_MODE_SEARCH;
cmd->gimbal.mode = GIMBAL_MODE_ABSOLUTE;
cmd->shoot.fire = false;
cmd->shoot.mode = SHOOT_MODE_LOADED;
break;
@ -360,7 +364,7 @@ int8_t CMD_ParseHost(const CMD_Host_t *host, CMD_t *cmd, float dt_sec) {
if (host->fire && cmd->shoot.ai_fire) {
cmd->shoot.fire = true;
} else {
cmd->shoot.fire = false;
cmd->shoot.fire = true;
}
return 0;
}

View File

@ -112,3 +112,17 @@ inline float CalculateRpm(float bullet_speed, float fric_radius, bool is17mm) {
/* 不为裁判系统设定值时,计算转速 */
return 60.0f * (float)bullet_speed / (M_2PI * fric_radius);
}
/**
* @brief
*
* @param file
* @param line
*/
void VerifyFailed(const char *file, uint32_t line) {
UNUSED(file);
UNUSED(line);
while (1) {
__NOP();
}
}

View File

@ -104,3 +104,53 @@ float CalculateRpm(float bullet_speed, float fric_radius, bool is17mm);
#ifdef __cplusplus
}
#endif
#ifdef DEBUG
/**
* @brief
*
*/
#define ASSERT(expr) \
do { \
if (!(expr)) { \
VerifyFailed(__FILE__, __LINE__); \
} \
} while (0)
#else
/**
* @brief DEBUG
*
*/
#define ASSERT(expr) ((void)(0))
#endif
#ifdef DEBUG
/**
* @brief
*
*/
#define VERIFY(expr) \
do { \
if (!(expr)) { \
VerifyFailed(__FILE__, __LINE__); \
} \
} while (0)
#else
/**
* @brief
*
*/
#define VERIFY(expr) ((void)(expr))
#endif
/**
* @brief
*
* @param file
* @param line
*/
void VerifyFailed(const char *file, uint32_t line);

View File

@ -12,34 +12,51 @@
#include "component\crc16.h"
#include "component\crc8.h"
#include "component\user_math.h"
#include "component\filter.h"
/* Private define ----------------------------------------------------------- */
#define AI_HOST_MAX_CONTROL_VALUE (0.001f)
#define AI_LEN_RX_BUFF (sizeof(Protocol_DownPackage_t))
/* Private macro ------------------------------------------------------------ */
/* Private typedef ---------------------------------------------------------- */
/* Private variables -------------------------------------------------------- */
static volatile uint32_t drop_message = 0;
static uint8_t rxbuf[AI_LEN_RX_BUFF];
static osThreadId_t thread_alert;
static bool inited = false;
static LowPassFilter2p_t low_pass_filter;
/* Private function -------------------------------------------------------- */
static void Ai_RxCpltCallback(void) {
osThreadFlagsSet(thread_alert, SIGNAL_AI_RAW_REDY);
}
static void Ai_IdleLineCallback(void) {
osThreadFlagsSet(thread_alert, SIGNAL_AI_RAW_REDY);
}
/* Exported functions ------------------------------------------------------- */
int8_t AI_Init(AI_t *ai) {
if (ai == NULL) return DEVICE_ERR_NULL;
UNUSED(ai);
ASSERT(ai);
if (inited) return DEVICE_ERR_INITED;
if ((thread_alert = osThreadGetId()) == NULL) return DEVICE_ERR_NULL;
VERIFY((thread_alert = osThreadGetId()) != NULL);
BSP_UART_RegisterCallback(BSP_UART_AI, BSP_UART_RX_CPLT_CB,
Ai_RxCpltCallback);
inited = true;
BSP_UART_RegisterCallback(BSP_UART_AI, BSP_UART_IDLE_LINE_CB,
Ai_IdleLineCallback);
LowPassFilter2p_Init(&low_pass_filter, 1000.0f, 5.0f); // 示例参数
ai->ai_online = false;
inited = true;
return 0;
}
@ -50,9 +67,9 @@ int8_t AI_Restart(void) {
}
int8_t AI_StartReceiving(AI_t *ai) {
if (HAL_UART_Receive_DMA(BSP_UART_GetHandle(BSP_UART_AI),
ai->form_host.rx_buffer,
sizeof(ai->form_host.rx_buffer)) == HAL_OK)
UNUSED(ai);
if (HAL_UART_Receive_DMA(BSP_UART_GetHandle(BSP_UART_AI), rxbuf,
AI_LEN_RX_BUFF) == HAL_OK)
return DEVICE_OK;
return DEVICE_ERR;
}
@ -62,31 +79,19 @@ bool AI_WaitDmaCplt(void) {
SIGNAL_AI_RAW_REDY);
}
int8_t AI_ParseHost(AI_t *ai, CMD_Host_t *cmd_host) {
(void)cmd_host;
ai->ai_online = true;
if (!CRC16_Verify(ai->form_host.rx_buffer, sizeof(ai->form_host.rx_buffer)))
int8_t AI_ParseHost(AI_t *ai) {
if (!CRC16_Verify((const uint8_t *)&(rxbuf), sizeof(ai->form_host)))
goto error;
memcpy(&(ai->form_host.DownPackage), ai->form_host.rx_buffer, sizeof(ai->form_host.DownPackage));
// cmd_host->gimbal_delta.pit = ai->form_host.data.gimbal.pit;
// cmd_host->gimbal_delta.yaw = ai->form_host.data.gimbal.yaw;
// cmd_host->gimbal_delta.rol = ai->form_host.data.gimbal.rol;
// cmd_host->fire = (ai->form_host.data.notice & AI_NOTICE_FIRE);
// cmd_host->chassis_move_vec.vx = ai->form_host.data.chassis_move_vec.vx;
// cmd_host->chassis_move_vec.vy = ai->form_host.data.chassis_move_vec.vy;
// cmd_host->chassis_move_vec.wz = ai->form_host.data.chassis_move_vec.wz;
// float yaw_out = PID_Calc(&ai->pid[0], 0.0f, ai->form_host.DownPackage.data.gimbal.yaw, 0.0f , 1.0/10.0f);
// float pit_out = PID_Calc(&ai->pid[1], 0.0f, ai->form_host.DownPackage.data.gimbal.pit, 0.0f , 1.0/10.0f);
cmd_host->gimbal_delta.pit = ai->form_host.DownPackage.data.gimbal.pit;
cmd_host->gimbal_delta.yaw = ai->form_host.DownPackage.data.gimbal.yaw;
// cmd_host->gimbal_delta.pit = pit_out;
// cmd_host->gimbal_delta.yaw = yaw_out;
cmd_host->gimbal_delta.rol = ai->form_host.DownPackage.data.gimbal.rol;
cmd_host->fire = (ai->form_host.DownPackage.data.notice & AI_NOTICE_FIRE);
cmd_host->chassis_move_vec.vx = ai->form_host.DownPackage.data.chassis_move_vec.vx;
cmd_host->chassis_move_vec.vy = ai->form_host.DownPackage.data.chassis_move_vec.vy;
cmd_host->chassis_move_vec.wz = ai->form_host.DownPackage.data.chassis_move_vec.wz;
ai->ai_online = true;
// 对接收到的数据进行滤波处理
for (size_t i = 0; i < sizeof(ai->form_host.data.gimbal); ++i) {
((float*)&ai->form_host.data.gimbal)[i] =
LowPassFilter2p_Apply(&low_pass_filter, ((float*)&ai->form_host.data.gimbal)[i]);
}
memcpy(&(ai->form_host), rxbuf, sizeof(ai->form_host));
memset(rxbuf, 0, AI_LEN_RX_BUFF);
return DEVICE_OK;
error:
@ -94,12 +99,27 @@ error:
return DEVICE_ERR;
}
void AI_PackCmd(AI_t *ai, CMD_Host_t *cmd_host) {
cmd_host->gimbal_delta.yaw = -ai->form_host.data.gimbal.yaw;
// cmd_host->gimbal_delta.yaw = cmd_host->gimbal_delta.yaw - 0.00015;
cmd_host->gimbal_delta.pit = -ai->form_host.data.gimbal.pit;
cmd_host->gimbal_delta.pit = cmd_host->gimbal_delta.pit + 0.0002;
Clip(&(cmd_host->gimbal_delta.yaw), -AI_HOST_MAX_CONTROL_VALUE,
AI_HOST_MAX_CONTROL_VALUE);
Clip(&(cmd_host->gimbal_delta.pit), -AI_HOST_MAX_CONTROL_VALUE,
AI_HOST_MAX_CONTROL_VALUE);
cmd_host->fire = (ai->form_host.data.notice & AI_NOTICE_FIRE);
cmd_host->chassis_move_vec.vx = ai->form_host.data.chassis_move_vec.vx;
cmd_host->chassis_move_vec.vy = ai->form_host.data.chassis_move_vec.vy;
cmd_host->chassis_move_vec.wz = ai->form_host.data.chassis_move_vec.wz;
}
int8_t AI_HandleOffline(AI_t *ai, CMD_Host_t *cmd_host) {
if (ai == NULL) return DEVICE_ERR_NULL;
if (cmd_host == NULL) return DEVICE_ERR_NULL;
ai->ai_online = false;
memset(ai->form_host.rx_buffer, 0, sizeof(ai->form_host.rx_buffer));
memset(&(ai->form_host.DownPackage), 0, sizeof(ai->form_host.DownPackage));
memset(&(ai->form_host), 0, sizeof(ai->form_host));
memset(cmd_host, 0, sizeof(*cmd_host));
return 0;
}

View File

@ -16,6 +16,7 @@ extern "C" {
#include "component\ahrs.h"
#include "component\cmd.h"
#include "component\user_math.h"
#include "component\filter.h"
#include "device\device.h"
#include "device\referee.h"
#include "protocol.h"
@ -36,10 +37,8 @@ typedef struct __packed {
typedef struct __packed {
osThreadId_t thread_alert;
struct {
uint8_t rx_buffer[sizeof(Protocol_DownPackage_t)];
Protocol_DownPackage_t DownPackage;
}form_host;
Protocol_DownPackage_t form_host;
struct {
AI_UpPackageReferee_t ref;
@ -47,7 +46,6 @@ typedef struct __packed {
} to_host;
CMD_AI_Status_t status;
bool ai_online;
} AI_t;
@ -57,11 +55,12 @@ int8_t AI_Restart(void);
int8_t AI_StartReceiving(AI_t *ai);
bool AI_WaitDmaCplt(void);
int8_t AI_ParseHost(AI_t *ai, CMD_Host_t *cmd_host);
int8_t AI_ParseHost(AI_t *ai);
int8_t AI_HandleOffline(AI_t *ai, CMD_Host_t *cmd_host);
int8_t AI_PackMCU(AI_t *ai, const AHRS_Quaternion_t *quat);
int8_t AI_PackRef(AI_t *ai, const Referee_ForAI_t *ref);
int8_t AI_StartSend(AI_t *ai, bool option);
void AI_PackCmd(AI_t *ai, CMD_Host_t *cmd_host);
#ifdef __cplusplus
}
#endif

View File

@ -191,7 +191,7 @@ static const Config_RobotParam_t param_default = {
.cover_close_duty = 0.050f,
.model = SHOOT_MODEL_17MM,
.bullet_speed = 30.f,
.min_shoot_delay = (uint32_t)(1000.0f / 10.0f),
.min_shoot_delay = (uint32_t)(1000.0f / 5.0f),
}, /* shoot */
.can = {
@ -718,7 +718,7 @@ static const Config_RobotParam_t param_sentry_gimbal = {
.cover_close_duty = 0.050f,
.model = SHOOT_MODEL_17MM,
.bullet_speed = 25.f,
.min_shoot_delay = (uint32_t)(1000.0f / 20.0f),
.min_shoot_delay = (uint32_t)(1000.0f / 1.0f),
}, /* shoot */
.can = {

View File

@ -42,9 +42,7 @@ void Task_Ai(void *argument) {
AI_Init(&ai);
uint32_t tick = osKernelGetTickCount();
uint32_t last_online_tick = tick;
while (1) {
#ifdef DEBUG
task_runtime.stack_water_mark.ai = osThreadGetStackSpace(osThreadGetId());
@ -54,18 +52,23 @@ void Task_Ai(void *argument) {
AI_StartReceiving(&ai);
if (AI_WaitDmaCplt()) {
AI_ParseHost(&ai, &cmd_host);
AI_ParseHost(&ai);
last_online_tick = tick;
} else {
if (tick - last_online_tick > 300) AI_HandleOffline(&ai, &cmd_host);
if (tick - last_online_tick > 300) AI_HandleOffline(&ai,&cmd_host);
}
if (ai.status != AI_STATUS_STOP && ai.ai_online){
AI_PackCmd(&ai, &cmd_host);
osMessageQueueReset(task_runtime.msgq.cmd.raw.host);
osMessageQueuePut(task_runtime.msgq.cmd.raw.host, &(cmd_host), 0, 0);
}
osMessageQueueGet(task_runtime.msgq.ai.quat, &(quat), NULL, 0);
osMessageQueueGet(task_runtime.msgq.cmd.ai, &(ai.status), NULL, 0);
bool ref_update = (osMessageQueueGet(task_runtime.msgq.referee.ai,
&(referee_ai), NULL, 0) == osOK);
AI_PackMCU(&ai, &quat);
if (ref_update) AI_PackRef(&ai, &(referee_ai));