/* * 射击模组 */ #pragma once #ifdef __cplusplus extern "C" { #endif /* Includes ----------------------------------------------------------------- */ #include #include "component/filter.h" #include "component/pid.h" #include "device/motor_rm.h" #include "device/motor.h" /* Exported constants ------------------------------------------------------- */ #define SHOOT_OK (0) /* 运行正常 */ #define SHOOT_ERR (-1) /* 运行时发现了其他错误 */ #define SHOOT_ERR_NULL (-2) /* 运行时发现NULL指针 */ #define SHOOT_ERR_MODE (-3) /* 运行时配置了错误的CMD_ShootMode_t */ /* Exported macro ----------------------------------------------------------- */ /* Exported types ----------------------------------------------------------- */ typedef enum { SHOOT_MODE_RELAX = 0, /* 放松模式,电机不工作 */ SHOOT_MODE_SAFE = 1, /* 安全模式,电机工作但不允许射击 */ SHOOT_MODE_LOADED = 2 /* 上膛模式,电机工作并允许射击 */ }Shoot_Mode_t; typedef struct { Shoot_Mode_t mode; /*射击模式*/ float bullet_speed; /*弹丸射速*/ uint32_t bullet_num; /*要发射的弹丸数*/ }Shoot_CMD_t; /* 射击参数的结构体,包含所有初始化用的参数,通常是const,存好几组。*/ typedef struct { MOTOR_RM_Param_t fric_motor_param[2]; /* 摩擦轮电机参数 */ MOTOR_RM_Param_t trig_motor_param; /* 拨弹电机参数 */ KPID_Params_t fric_pid_param; /* 摩擦轮电机控制PID的参数 */ KPID_Params_t trig_pid_param; /* 扳机电机控制PID的参数 */ /* 低通滤波器截止频率 */ struct { /* 输入 */ struct { float fric; /* 摩擦轮电机 */ float trig; /* 扳机电机 */ } in; /* 输出 */ struct { float fric; /* 摩擦轮电机 */ float trig; /* 扳机电机 */ } out; } low_pass_cutoff_freq; float num_trig_tooth; /* 拨弹盘中一圈能存储几颗弹丸 */ float fric_radius; /* 摩擦轮半径,单位:米 */ float cover_open_duty; /* 弹舱盖打开时舵机PWM占空比 */ float cover_close_duty; /* 弹舱盖关闭时舵机PWM占空比 */ float bullet_speed; /* 弹丸初速度 */ uint32_t min_shoot_delay; /* 通过设置最小射击间隔来设置最大射频 */ } Shoot_Params_t; typedef struct { uint32_t last_shoot; /* 上次射击时间 单位:ms */ bool last_fire; /* 上次开火状态 */ bool first_fire; /* 第一次收到开火指令 */ uint32_t shooted; /* 已经发射的弹丸 */ uint32_t to_shoot; /* 计划发射的弹丸 */ float bullet_speed; /* 弹丸初速度 */ uint32_t period_ms; /* 弹丸击发延迟 */ } Shoot_FireCtrl_t; /* * 运行的主结构体,所有这个文件里的函数都在操作这个结构体。 * 包含了初始化参数,中间变量,输出变量。 */ typedef struct { uint64_t lask_wakeup; float dt; const Shoot_Params_t *param; /* 射击的参数,用Shoot_Init设定 */ Shoot_FireCtrl_t fire_ctrl; /* 模块通用 */ Shoot_Mode_t mode; /* 当前模式 */ /* 反馈信息 */ struct { MOTOR_Feedback_t fric[2]; /* 摩擦轮电机反馈 */ MOTOR_Feedback_t trig; /* 拨弹电机反馈 */ } feedback; /* PID计算的目标值 */ struct { float fric_rpm[2]; /* 摩擦轮电机转速,单位:RPM */ float trig_angle; /* 拨弹电机角度,单位:弧度 */ } setpoint; /* 反馈控制用的PID */ struct { KPID_t fric[2]; /* 控制摩擦轮 */ KPID_t trig; /* 控制拨弹电机 */ } pid; /* 过滤器 */ struct { /* 反馈值滤波器 */ struct { LowPassFilter2p_t fric[2]; /* 过滤摩擦轮 */ LowPassFilter2p_t trig; /* 过滤拨弹电机 */ } in; /* 输出值滤波器 */ struct { LowPassFilter2p_t fric[2]; /* 过滤摩擦轮 */ LowPassFilter2p_t trig; /* 过滤拨弹电机 */ } out; } filter; } Shoot_t; /* Exported functions prototypes -------------------------------------------- */ /** * \brief 初始化射击 * * \param s 包含射击数据的结构体 * \param param 包含射击参数的结构体指针 * \param target_freq 任务预期的运行频率 * * \return 函数运行结果 */ int8_t Shoot_Init(Shoot_t *s, const Shoot_Params_t *param, float target_freq); /** * \brief 更新射击的反馈信息 * * \param s 包含射击数据的结构体 * \param can CAN设备结构体 * * \return 函数运行结果 */ int8_t Shoot_UpdateFeedback(Shoot_t *s); /** * \brief 运行射击控制逻辑 * * \param s 包含射击数据的结构体 * \param s_cmd 射击控制指令 * \param s_ref 裁判系统数据 * \param dt_sec 两次调用的时间间隔 * * \return 函数运行结果 */ int8_t Shoot_Control(Shoot_t *s,Shoot_CMD_t *s_cmd); /** * \brief 射击输出值 * * \param s 包含射击数据的结构体 * \param out CAN设备射击输出结构体 */ void Shoot_Output(Shoot_t *s); #ifdef __cplusplus } #endif