350 lines
15 KiB
C
350 lines
15 KiB
C
/* =============================================================================
|
||
* shoot.c - 发射机构:摩擦轮 2xM3508(0x200 CAN1 槽1/2)+ 拨弹 M2006(0x200 CAN2 槽1)
|
||
* 逻辑(与 legacy 一致):
|
||
* 摩擦轮开关受发射机构上电状态门控(可由 DEBUG_SHOOT_WITHOUT_REFEREE 绕过),
|
||
* 开启时联动激光(PC8=TIM3_CH3 PWM,占空比 0/满 对应关/开)
|
||
* 拨弹仅在摩擦轮开启后允许;fire=-GIMBAL_RAMMER_SPEED,
|
||
* eject 反向限速 1500rpm,累计约 10 圈转子后锁存(机械只能退一颗)
|
||
* 拨弹电流经 shoot_send_cmd() 经 CAN2 0x200 帧直接发送(ID 1)
|
||
* 另含"台架调试工况"分支(见文件末尾,由 GIMBAL_BENCH_RAMMER_TUNE 控制)
|
||
* ============================================================================= */
|
||
#include "shoot.h"
|
||
#include "gimbal.h"
|
||
#include "pid.h"
|
||
#include "tim.h"
|
||
#include "robot_param.h"
|
||
#include "dr16.h"
|
||
#include "main.h"
|
||
#include <math.h>
|
||
|
||
extern TIM_HandleTypeDef htim3;
|
||
|
||
uint8_t shoot_friction_flag = 0;
|
||
|
||
static PID_PositionTypeDef friction_l_pid, friction_r_pid, rammer_pid;
|
||
|
||
/* 退弹行程状态:机械一次只能退一颗弹,累计转子角超限后锁存,正转发射后清零 */
|
||
static uint8_t rammer_eject_active = 0;
|
||
static uint8_t rammer_eject_latched = 0;
|
||
static float rammer_eject_start_deg = 0.0f;
|
||
static float rammer_eject_base_deg = 0.0f;
|
||
static float rammer_eject_accum_deg = 0.0f;
|
||
|
||
/* 激光:PC8 = TIM3_CH3(10kHz PWM,ARR=8399),占空比 0/满 对应关/开 */
|
||
#define LASER_ON() __HAL_TIM_SET_COMPARE(&htim3, TIM_CHANNEL_3, 8399)
|
||
#define LASER_OFF() __HAL_TIM_SET_COMPARE(&htim3, TIM_CHANNEL_3, 0)
|
||
|
||
/* 激光 PWM 初始化:先关激光,再启动 TIM3_CH3。无参数、无返回值。 */
|
||
static void laser_init(void)
|
||
{
|
||
__HAL_TIM_SET_COMPARE(&htim3, TIM_CHANNEL_3, 0);
|
||
HAL_TIM_PWM_Start(&htim3, TIM_CHANNEL_3);
|
||
}
|
||
|
||
/* 初始化摩擦轮两路与拨弹盘的速度环 PID,并启动激光 PWM。can_start 之后调用一次。
|
||
* 无参数、无返回值。 */
|
||
void shoot_init(void)
|
||
{
|
||
PID_PositionStructureInit(&friction_l_pid, 0);
|
||
PID_PositionSetParameter(&friction_l_pid, GIMBAL_FRICTION_WHEEL_KP, GIMBAL_FRICTION_WHEEL_KI, GIMBAL_FRICTION_WHEEL_KD);
|
||
PID_PositionSetEkRange(&friction_l_pid, -5, 5);
|
||
PID_PositionSetOUTRange(&friction_l_pid, -15000, 15000);
|
||
|
||
PID_PositionStructureInit(&friction_r_pid, 0);
|
||
PID_PositionSetParameter(&friction_r_pid, GIMBAL_FRICTION_WHEEL_KP, GIMBAL_FRICTION_WHEEL_KI, GIMBAL_FRICTION_WHEEL_KD);
|
||
PID_PositionSetEkRange(&friction_r_pid, -5, 5);
|
||
PID_PositionSetOUTRange(&friction_r_pid, -15000, 15000);
|
||
|
||
PID_PositionStructureInit(&rammer_pid, GIMBAL_RAMMER_SPEED);
|
||
PID_PositionSetParameter(&rammer_pid, GIMBAL_RAMMER_KP, GIMBAL_RAMMER_KI, GIMBAL_RAMMER_KD);
|
||
PID_PositionSetEkRange(&rammer_pid, -20, 20);
|
||
PID_PositionSetOUTRange(&rammer_pid, -10000, 10000);
|
||
|
||
laser_init();
|
||
}
|
||
|
||
/* 发射机构控制,2 ms 调用一次。只算 PID,不发 CAN(发送见 shoot_send_cmd)。
|
||
* friction_on : 1=开摩擦轮/激光
|
||
* fire : 1=拨弹正转(发射)
|
||
* eject : 1=拨弹反转(退弹)
|
||
* shooter_power_on: 裁判系统给出的发射机构上电状态(0 时禁止开摩擦轮)
|
||
* 无返回值;摩擦轮/拨弹目标转速单位均为转子 RPM,输出单位 id 电流码 */
|
||
void shoot_control(uint8_t friction_on, uint8_t fire, uint8_t eject, uint8_t shooter_power_on)
|
||
{
|
||
/* 摩擦轮(与 legacy 逻辑一致:发射机构上电才允许开启;
|
||
* 台架调试时可经 DEBUG_SHOOT_WITHOUT_REFEREE 绕过裁判系统检查) */
|
||
if (friction_on && (shooter_power_on || DEBUG_SHOOT_WITHOUT_REFEREE))
|
||
{
|
||
friction_l_pid.Need_Value = -GIMBAL_FRICTION_WHEEL_SPEED;
|
||
friction_r_pid.Need_Value = GIMBAL_FRICTION_WHEEL_SPEED;
|
||
LASER_ON();
|
||
shoot_friction_flag = 1;
|
||
}
|
||
else
|
||
{
|
||
friction_l_pid.Need_Value = 0;
|
||
friction_r_pid.Need_Value = 0;
|
||
LASER_OFF();
|
||
shoot_friction_flag = 0;
|
||
}
|
||
|
||
/* 拨弹:仅在摩擦轮开启后允许
|
||
* 发射:定速正转;退弹:限速 1500 rpm,累计转子角到约 10 圈即断给定并锁存,
|
||
* 机械一次只能退一颗,必须重新"正转发射"才恢复下一次退弹行程 */
|
||
if (shoot_friction_flag)
|
||
{
|
||
if (fire)
|
||
{
|
||
rammer_eject_active = 0;
|
||
rammer_eject_latched = 0;
|
||
rammer_eject_accum_deg = 0.0f;
|
||
rammer_pid.Need_Value = -GIMBAL_RAMMER_SPEED;
|
||
}
|
||
else if (eject)
|
||
{
|
||
/* 进入退弹时记录起点,松开再按继续累计(仍属同一颗弹) */
|
||
if (!rammer_eject_active)
|
||
{
|
||
rammer_eject_active = 1;
|
||
rammer_eject_start_deg = motor_rammer.fdb.rotor_angle_deg;
|
||
rammer_eject_base_deg = rammer_eject_accum_deg;
|
||
}
|
||
rammer_eject_accum_deg = rammer_eject_base_deg +
|
||
fabsf(motor_rammer.fdb.rotor_angle_deg - rammer_eject_start_deg);
|
||
|
||
if (rammer_eject_accum_deg >= GIMBAL_RAMMER_EJECT_MAX_ROTOR_DEG)
|
||
rammer_eject_latched = 1;
|
||
|
||
rammer_pid.Need_Value = rammer_eject_latched ? 0.0f : -GIMBAL_RAMMER_EJECT_SPEED;
|
||
}
|
||
else
|
||
{
|
||
rammer_eject_active = 0;
|
||
rammer_pid.Need_Value = 0;
|
||
}
|
||
}
|
||
else
|
||
{
|
||
rammer_eject_active = 0;
|
||
rammer_pid.Need_Value = 0;
|
||
}
|
||
|
||
PID_PositionCalc(&friction_l_pid, motor_friction_l.fdb.rotor_speed);
|
||
PID_PositionCalc(&friction_r_pid, motor_friction_r.fdb.rotor_speed);
|
||
PID_PositionCalc(&rammer_pid, motor_rammer.fdb.rotor_speed);
|
||
}
|
||
|
||
/* 下发摩擦轮(CAN1 0x200 槽1/2)与拨弹盘(CAN2 0x200 槽1)电流。
|
||
* 电流单位:id 电流码,±16384 对应满量程。无参数、无返回值。 */
|
||
void shoot_send_cmd(void)
|
||
{
|
||
/* 摩擦轮 2×M3508:CAN1 0x200 槽1/2 */
|
||
dji_motor_send_current_low(&hcan1, (int16_t)friction_l_pid.OUT, (int16_t)friction_r_pid.OUT, 0, 0);
|
||
/* 拨弹盘 M2006(ID 1):CAN2 0x200 槽1 */
|
||
dji_motor_send_current_low(&hcan2, (int16_t)rammer_pid.OUT, 0, 0, 0);
|
||
}
|
||
|
||
/* 发射机构安全停机:摩擦轮/拨弹电流全部写 0,激光关闭,清摩擦轮状态。
|
||
* 无参数、无返回值。 */
|
||
void shoot_output_zero(void)
|
||
{
|
||
dji_motor_send_current_low(&hcan1, 0, 0, 0, 0);
|
||
dji_motor_send_current_low(&hcan2, 0, 0, 0, 0);
|
||
LASER_OFF();
|
||
shoot_friction_flag = 0;
|
||
rammer_eject_active = 0; /* 断流后重新进入退弹需重取起点 */
|
||
}
|
||
|
||
/* ============================================================================
|
||
* 台架调试工况:拨弹盘单机(仅 GIMBAL_BENCH_RAMMER_TUNE=1 时由 shoot_task 调用)
|
||
*
|
||
* 控制方式与整车遥控器模式完全相同(2026-10-18 按要求改回原映射):
|
||
* S1 上(1) = 摩擦轮开;台架无摩擦轮,仅作为"拨弹使能"门控
|
||
* 拨轮向下(<1000) = 发射 -> 正转,目标转速 GIMBAL_RAMMER_SPEED(-5400)
|
||
* 拨轮向上(>1050) = 退弹 -> 反转,目标转速 -GIMBAL_RAMMER_SPEED(+5400)
|
||
* 拨轮中位(1000~1050) = 停止
|
||
*
|
||
* 退弹保护(机械只有 2 发余量,控制方式不变、只加保护):
|
||
* - 累计退弹转角(bench_eject_accum_deg)达到 GIMBAL_BENCH_EJECT_MAX_DEG -> 断流锁存,
|
||
* 必须重新"正转发射"才会清零余量并允许再次退弹(避免多次小幅退弹累计超限)
|
||
* - 电流软限 GIMBAL_BENCH_EJECT_CUR_MAX_A 连续超 GIMBAL_BENCH_EJECT_OVER_MS -> 中止
|
||
* - 电流硬限 GIMBAL_BENCH_EJECT_CUR_HARD_A 瞬时超过 -> 立即中止
|
||
* - 单次连续退弹超时 GIMBAL_BENCH_EJECT_MAX_MS -> 中止
|
||
* - 中止后立即断流并锁存,拨轮必须回中/回发射方向才能解除
|
||
* - 遥控器掉线 -> 立即断流并复位
|
||
* IDLE/LATCH 状态下电流指令强制 0(绝不顶在机械限位上持续过载)
|
||
* ==========================================================================*/
|
||
typedef enum
|
||
{
|
||
BENCH_IDLE = 0, /* 待机/正转发射 */
|
||
BENCH_EJECT = 1, /* 退弹中 */
|
||
BENCH_LATCH = 2 /* 退弹被保护中止/余量用尽:断流锁存 */
|
||
} bench_state_e;
|
||
|
||
static bench_state_e bench_state = BENCH_IDLE;
|
||
static float bench_eject_start_rotor_deg = 0.0f; /* 本次退弹起点(转子累计角) */
|
||
static float bench_eject_accum_start = 0.0f; /* 本次退弹开始前已累计的转角 */
|
||
static float bench_eject_accum_deg = 0.0f; /* 自上次正转以来的累计退弹角(°) */
|
||
static uint32_t bench_eject_start_tick = 0;
|
||
static uint32_t bench_over_cur_ms = 0;
|
||
static int16_t bench_rammer_out_raw = 0;
|
||
|
||
volatile float bench_rammer_out_cur = 0.0f;
|
||
volatile float bench_eject_travel_deg = 0.0f; /* 累计退弹转角(°) */
|
||
volatile uint8_t bench_state_dbg = 0; /* 0=IDLE 1=EJECT 2=LATCH */
|
||
volatile uint8_t bench_eject_abort = 0; /* 0=无 1=电流软限 2=超时 3=电流硬限 */
|
||
|
||
/* 台架调试:拨弹盘单机状态机,2 ms 调用一次(仅 GIMBAL_BENCH_RAMMER_TUNE=1 时使用)。
|
||
* 读遥控 S1/拨轮给出目标转速(RPM),做退弹余量、电流软/硬限、超时保护,
|
||
* 结果写入 bench_rammer_out_raw(id 电流码)供 shoot_bench_send 下发。
|
||
* 无参数、无返回值。 */
|
||
void shoot_bench_step(void)
|
||
{
|
||
uint8_t gate = (dr16.s1 == 1); /* 摩擦轮开关 -> 台架作拨弹使能 */
|
||
uint16_t wheel = dr16.thumb_wheel; /* 拨轮:向下发射/向上退弹 */
|
||
float cur_a = motor_rammer.fdb.torque_current; /* 实测电流(A) */
|
||
float cmd = 0.0f;
|
||
|
||
/* 遥控器离线:归零复位,避免失控 */
|
||
if (dr16_status == 0 || dr16_offline())
|
||
{
|
||
bench_state = BENCH_IDLE;
|
||
bench_eject_accum_deg = 0.0f;
|
||
bench_rammer_out_raw = 0;
|
||
bench_rammer_out_cur = 0.0f;
|
||
bench_eject_travel_deg = 0.0f;
|
||
bench_eject_abort = 0;
|
||
bench_state_dbg = (uint8_t)bench_state;
|
||
return;
|
||
}
|
||
|
||
switch (bench_state)
|
||
{
|
||
case BENCH_IDLE:
|
||
if (!gate)
|
||
{ /* S1 不在上:使能关闭,断流 */
|
||
cmd = 0.0f;
|
||
break;
|
||
}
|
||
|
||
if (wheel < 1000)
|
||
{ /* 拨轮向下 = 发射(正转) */
|
||
cmd = GIMBAL_RAMMER_SPEED;
|
||
bench_eject_accum_deg = 0.0f; /* 正转恢复退弹余量 */
|
||
bench_eject_travel_deg = 0.0f;
|
||
bench_eject_abort = 0;
|
||
}
|
||
else if (wheel > 1050)
|
||
{ /* 拨轮向上 = 退弹(反转) */
|
||
if (bench_eject_accum_deg >= GIMBAL_BENCH_EJECT_MAX_DEG)
|
||
{
|
||
bench_state = BENCH_LATCH;/* 余量已用尽:锁存,等正转恢复 */
|
||
cmd = 0.0f;
|
||
}
|
||
else
|
||
{
|
||
bench_state = BENCH_EJECT;
|
||
bench_eject_start_rotor_deg = motor_rammer.fdb.rotor_angle_deg;
|
||
bench_eject_accum_start = bench_eject_accum_deg;
|
||
bench_eject_start_tick = HAL_GetTick();
|
||
bench_over_cur_ms = 0;
|
||
bench_eject_abort = 0;
|
||
cmd = -GIMBAL_RAMMER_SPEED;
|
||
}
|
||
}
|
||
break;
|
||
|
||
case BENCH_EJECT:
|
||
{
|
||
uint32_t now = HAL_GetTick();
|
||
float travel;
|
||
|
||
/* 松开拨轮或关使能:中止本次退弹(已走余量保留,不清零) */
|
||
if (!gate || wheel <= 1050)
|
||
{
|
||
bench_state = BENCH_IDLE;
|
||
cmd = 0.0f;
|
||
break;
|
||
}
|
||
|
||
/* 累计退弹转角 = 本次起点 + 本段转子角差/减速比 */
|
||
travel = fabsf(motor_rammer.fdb.rotor_angle_deg -
|
||
bench_eject_start_rotor_deg) / GIMBAL_BENCH_RAMMER_GEAR;
|
||
bench_eject_accum_deg = bench_eject_accum_start + travel;
|
||
bench_eject_travel_deg = bench_eject_accum_deg;
|
||
|
||
/* --- 电流保护(软限计时 + 硬限瞬时) --- */
|
||
if (fabsf(cur_a) > GIMBAL_BENCH_EJECT_CUR_HARD_A)
|
||
bench_eject_abort = 3;
|
||
else if (fabsf(cur_a) > GIMBAL_BENCH_EJECT_CUR_MAX_A)
|
||
{
|
||
if (bench_over_cur_ms < 1000u)
|
||
bench_over_cur_ms += 2u; /* 本任务 2ms 一拍 */
|
||
if (bench_over_cur_ms >= GIMBAL_BENCH_EJECT_OVER_MS)
|
||
bench_eject_abort = 1;
|
||
}
|
||
else
|
||
bench_over_cur_ms = 0;
|
||
|
||
if (bench_eject_abort != 0)
|
||
{
|
||
bench_state = BENCH_LATCH; /* 过载中止:断流锁存 */
|
||
cmd = 0.0f;
|
||
}
|
||
else if (bench_eject_accum_deg >= GIMBAL_BENCH_EJECT_MAX_DEG)
|
||
{
|
||
bench_state = BENCH_LATCH; /* 余量用尽:断流锁存 */
|
||
cmd = 0.0f;
|
||
}
|
||
else if (now - bench_eject_start_tick >= GIMBAL_BENCH_EJECT_MAX_MS)
|
||
{
|
||
bench_eject_abort = 2; /* 超时中止 */
|
||
bench_state = BENCH_LATCH;
|
||
cmd = 0.0f;
|
||
}
|
||
else
|
||
cmd = -GIMBAL_RAMMER_SPEED; /* 与整车同速退弹 */
|
||
break;
|
||
}
|
||
|
||
case BENCH_LATCH:
|
||
cmd = 0.0f;
|
||
/* 拨轮回中或回发射方向才解除;之后靠"正转发射"清零余量 */
|
||
if (wheel <= 1050)
|
||
bench_state = BENCH_IDLE;
|
||
break;
|
||
|
||
default:
|
||
bench_state = BENCH_IDLE;
|
||
break;
|
||
}
|
||
|
||
/* 锁存状态强制断流(保护已触发 / 余量用尽,绝不顶限位);
|
||
* 其余状态照常走 PID(cmd=0 即刹停到 0,与整车逻辑一致) */
|
||
if (bench_state == BENCH_LATCH)
|
||
{
|
||
rammer_pid.Need_Value = 0.0f;
|
||
bench_rammer_out_raw = 0;
|
||
bench_rammer_out_cur = 0.0f;
|
||
}
|
||
else
|
||
{
|
||
rammer_pid.Need_Value = cmd;
|
||
/* 方向修正:反馈与输出都乘 GIMBAL_BENCH_RAMMER_DIR(-1 时两者同时取反,
|
||
* 环内仍是负反馈,所以 PID 参数不用改)。
|
||
* 转角累计用的是原始转子角,与方向无关,退弹余量判断不受影响。 */
|
||
PID_PositionCalc(&rammer_pid, GIMBAL_BENCH_RAMMER_DIR * motor_rammer.fdb.rotor_speed);
|
||
bench_rammer_out_raw = (int16_t)(GIMBAL_BENCH_RAMMER_DIR * rammer_pid.OUT);
|
||
bench_rammer_out_cur = (float)bench_rammer_out_raw; /* 实际下发值(FireWater 第3通道) */
|
||
}
|
||
|
||
bench_state_dbg = (uint8_t)bench_state;
|
||
}
|
||
|
||
/* 台架调试:CAN2 0x200 槽1 下发拨弹电流(id 电流码)。无参数、无返回值。 */
|
||
void shoot_bench_send(void)
|
||
{
|
||
/* 拨弹盘 M2006(ID 1)在 CAN2 0x200 槽 1;其余槽固定 0 */
|
||
dji_motor_send_current_low(&hcan2, bench_rammer_out_raw, 0, 0, 0);
|
||
}
|