/* *文件名称:hlj_pc.c *功能描述:HLJ_PC底盘功率控制系统(功率预算+运动学速度规划+四轮功率闭环分配) *备注:复用module_test已验证的M3508功率估计模型,适配90W裁判上限/缓冲能量/超级电容 */ #include "hlj_pc.h" #include "pid.h" #include "robot_param.h" #include #include #include #define HLJ_PC_CMD_LIMIT_CNT 16000.0f//单电机电流软限幅(counts) #define HLJ_PC_BUFFER_TARGET_J 30.0f //缓冲能量PID目标值(J) #define HLJ_PC_BUFFER_KP 1.0f //缓冲能量PID Kp #define HLJ_PC_BUFFER_KI 0.0f //缓冲能量PID Ki #define HLJ_PC_BUFFER_KD 0.0f //缓冲能量PID Kd #define HLJ_PC_BUFFER_OUT_LIM 20.0f //缓冲能量对基准功率最大修正量(W) hlj_pc_model_param_t g_hlj_pc_param = { .k_torque = 1.99688994e-6f, .k_a = 1.63e-07f, .k_speed_sq = 1.06e-07f, .k_constant = 2.0f, }; hlj_pc_info_t g_hlj_pc_info;//全局诊断状态 static PID_PositionTypeDef hlj_buffer_pid;//缓冲能量闭环PID static uint8_t hlj_pc_inited = 0U; //初始化标志 /* *函数简介:将浮点值限幅至[lo, hi]区间 *参数说明:val:输入值,lo:下限,hi:上限 *返回类型:float:限幅结果 *备注:内部辅助函数 */ static float hlj_clampf(float val, float lo, float hi) { if (val < lo) return lo; if (val > hi) return hi; return val; } /* *函数简介:初始化HLJ_PC功率控制系统(缓冲能量PID与默认预算) *参数说明:无 *返回类型:无 *备注:在CAN_Task初始化阶段调用一次 */ void hlj_pc_init(void) { memset(&g_hlj_pc_info, 0, sizeof(g_hlj_pc_info)); g_hlj_pc_info.ref_limit_w = HLJ_PC_DEFAULT_POWER_W; g_hlj_pc_info.total_budget_w = HLJ_PC_DEFAULT_POWER_W; g_hlj_pc_info.kv_scale = 1.0f; g_hlj_pc_info.kw_scale = 1.0f; g_hlj_pc_info.final_power_scale = 1.0f; PID_PositionStructureInit(&hlj_buffer_pid, HLJ_PC_BUFFER_TARGET_J); PID_PositionSetParameter(&hlj_buffer_pid, HLJ_PC_BUFFER_KP, HLJ_PC_BUFFER_KI, HLJ_PC_BUFFER_KD); PID_PositionSetOUTRange(&hlj_buffer_pid, -HLJ_PC_BUFFER_OUT_LIM, HLJ_PC_BUFFER_OUT_LIM); hlj_pc_inited = 1U; } /* *函数简介:计算单颗M3508电机的估计输入功率(W) *参数说明:speed_rpm:转子转速(RPM) *参数说明:cmd_current:电流控制指令(counts) *参数说明:p_mech:输出机械功率指针(可为NULL) *参数说明:p_loss:输出损耗功率指针(可为NULL) *返回类型:float:估计输入总功率(W,带符号) *备注:无 */ float hlj_pc_calc_single_power(float speed_rpm, float cmd_current, float * p_mech, float * p_loss) { const float mech = g_hlj_pc_param.k_torque * speed_rpm * cmd_current; const float loss = g_hlj_pc_param.k_a * cmd_current * cmd_current + g_hlj_pc_param.k_speed_sq * speed_rpm * speed_rpm + g_hlj_pc_param.k_constant; if (p_mech != NULL) *p_mech = mech; if (p_loss != NULL) *p_loss = loss; return mech + loss; } /* *函数简介:根据目标功率逆解单颗电机允许的最大电流指令(counts) *参数说明:speed_rpm:转子转速(RPM) *参数说明:raw_cmd:原始期望电流指令(counts) *参数说明:target_power:目标消耗功率(W) *返回类型:int16_t:限幅后的电流指令(±16000以内) *备注:不可达时取二次曲线最低功耗点(-b/(2a))并限幅 */ int16_t hlj_pc_solve_single_cmd(float speed_rpm, int16_t raw_cmd, float target_power) { const float a = g_hlj_pc_param.k_a; const float b = g_hlj_pc_param.k_torque * speed_rpm; const float c = g_hlj_pc_param.k_speed_sq * speed_rpm * speed_rpm + g_hlj_pc_param.k_constant - target_power; float root = 0.0f; if (a <= 0.0f || target_power < 0.0f) return raw_cmd; const float inside = b * b - 4.0f * a * c; if (inside < 0.0f) { /* 高速工况下目标功率不可达,回退至最小功耗电流点 */ root = -b / (2.0f * a); } else if (raw_cmd >= 0) { root = (-b + sqrtf(inside)) / (2.0f * a); } else { root = (-b - sqrtf(inside)) / (2.0f * a); } root = hlj_clampf(root, -HLJ_PC_CMD_LIMIT_CNT, HLJ_PC_CMD_LIMIT_CNT); return (int16_t)root; } /* *函数简介:更新本周期底盘总功率预算(裁判上限+缓冲能量+超电剩余) *参数说明:in:输入状态结构体指针 *返回类型:float:本周期底盘允许消耗的总功率预算(W) *备注:缓冲能量不存在或超电离线时自动退回90W基础上限 */ float hlj_pc_update_budget(const hlj_pc_input_t * in) { if (!hlj_pc_inited) hlj_pc_init(); float ref_limit = HLJ_PC_DEFAULT_POWER_W; float buf_delta = 0.0f; float cap_extra = 0.0f; if (in != NULL) { if (in->ref_online && in->ref_power_limit > 0.0f) ref_limit = in->ref_power_limit; if (in->ref_online && in->buffer_energy > 0.0f) { PID_PositionCalc(&hlj_buffer_pid, in->buffer_energy); buf_delta = -hlj_buffer_pid.OUT; } if (in->cap_online && in->cap_ready && in->cap_enable && in->cap_energy > HLJ_PC_CAP_MIN_ENERGY) { float ratio = ((float)in->cap_energy - (float)HLJ_PC_CAP_MIN_ENERGY) / (float)(HLJ_PC_CAP_FULL_ENERGY - HLJ_PC_CAP_MIN_ENERGY); ratio = hlj_clampf(ratio, 0.0f, 1.0f); cap_extra = HLJ_PC_CAP_EXTRA_MAX_W * ratio; } } float budget = ref_limit + buf_delta + cap_extra; budget = hlj_clampf(budget, HLJ_PC_MIN_POWER_W, ref_limit + HLJ_PC_CAP_EXTRA_MAX_W); g_hlj_pc_info.ref_limit_w = ref_limit; g_hlj_pc_info.buf_delta_w = buf_delta; g_hlj_pc_info.cap_extra_w = cap_extra; g_hlj_pc_info.total_budget_w = budget; return budget; } /* *函数简介:依据麦轮逆解与轮速环比例估算给定(vx,vy,w)对应的四轮预计总正功率 *参数说明:vx:底盘前后速度(m/s),vy:底盘左右速度(m/s),w:角速度(rad/s) *参数说明:wheel_rpm:四轮实测转速(RPM) *返回类型:float:四轮预计正向总功率(W) *备注:内部函数,用于速度规划层快速评估 */ static float hlj_pc_estimate_motion_power(float vx, float vy, float w, const float wheel_rpm[4]) { const float rear = w * (MECANUM_RX + MECANUM_RY) / 100.0f; const float factor = MECANUM_SPEED_FACTOR / MECANUM_WHEEL_RADIUS; const float target_rpm[4] = { (vx - vy - rear) * factor, (vx + vy - rear) * factor, (-vx - vy - rear) * factor, (-vx + vy - rear) * factor, }; float total = 0.0f; for (uint8_t i = 0U; i < 4U; i++) { float cmd_est = MECANUM_WHEEL_SPEED_KP * (target_rpm[i] - wheel_rpm[i]); cmd_est = hlj_clampf(cmd_est, -MECANUM_WHEEL_SPEED_OUT_LIMIT, MECANUM_WHEEL_SPEED_OUT_LIMIT); float p = hlj_pc_calc_single_power(wheel_rpm[i], cmd_est, NULL, NULL); if (p > 0.0f) total += p; } return total; } /* *函数简介:固定某一轴缩放比例,二分求解另一轴满足功率预算的最大缩放因子 *参数说明:vx:期望vx,vy:期望vy,w:期望w,wheel_rpm:四轮转速 *参数说明:scale_v_mode:1=固定kw二分kv,0=固定kv二分kw *参数说明:fixed_scale:固定轴缩放比例 *参数说明:lo:二分下界,hi:二分上界,budget:目标功率预算(W) *返回类型:float:最优缩放因子 *备注:6次二分迭代精度约1.5%,耗时极低 */ static float hlj_pc_bisect_scale(float vx, float vy, float w, const float wheel_rpm[4], uint8_t scale_v_mode, float fixed_scale, float lo, float hi, float budget) { for (uint8_t iter = 0U; iter < 6U; iter++) { float mid = 0.5f * (lo + hi); float kv = scale_v_mode ? mid : fixed_scale; float kw = scale_v_mode ? fixed_scale : mid; float p = hlj_pc_estimate_motion_power(vx * kv, vy * kv, w * kw, wheel_rpm); if (p <= budget) lo = mid; else hi = mid; } return lo; } /* *函数简介:在当前功率预算下对底盘平移与小陀螺速度进行最优规划 *参数说明:vx_in:期望底盘系前后速度(m/s) *参数说明:vy_in:期望底盘系左右速度(m/s) *参数说明:w_in:期望自旋角速度(rad/s) *参数说明:is_spin:1=小陀螺模式,0=非小陀螺模式 *参数说明:wheel_rpm:四轮当前实测转子转速(RPM) *参数说明:vx_out:规划后前后速度输出指针 *参数说明:vy_out:规划后左右速度输出指针 *参数说明:w_out:规划后自旋角速度输出指针 *返回类型:无 *备注:边小陀螺边前进时先在[HLJ_PC_MIN_SPIN_RATIO,1]降陀螺保平移,再等比降平移保方向 */ void hlj_pc_plan_velocity(float vx_in, float vy_in, float w_in, uint8_t is_spin, const float wheel_rpm[4], float * vx_out, float * vy_out, float * w_out) { if (vx_out == NULL || vy_out == NULL || w_out == NULL) return; if (wheel_rpm == NULL) { *vx_out = vx_in; *vy_out = vy_in; *w_out = w_in; return; } const float budget = g_hlj_pc_info.total_budget_w; const float p_full = hlj_pc_estimate_motion_power(vx_in, vy_in, w_in, wheel_rpm); float kv = 1.0f; float kw = 1.0f; uint8_t limited = 0U; if (p_full > budget) { limited = 1U; const uint8_t has_trans = (uint8_t)(fabsf(vx_in) > 1e-4f || fabsf(vy_in) > 1e-4f); if (is_spin && has_trans) { /* 边小陀螺边前进:先尝试在保留平移的前提下降低小陀螺转速 */ float p_spin_min = hlj_pc_estimate_motion_power(vx_in, vy_in, w_in * HLJ_PC_MIN_SPIN_RATIO, wheel_rpm); if (p_spin_min <= budget) { kw = hlj_pc_bisect_scale(vx_in, vy_in, w_in, wheel_rpm, 0U, 1.0f, HLJ_PC_MIN_SPIN_RATIO, 1.0f, budget); } else { kw = HLJ_PC_MIN_SPIN_RATIO; kv = hlj_pc_bisect_scale(vx_in, vy_in, w_in, wheel_rpm, 1U, kw, 0.0f, 1.0f, budget); } } else if (is_spin) { /* 纯小陀螺(含变速小陀螺):直接缩放角速度避免无效铜损 */ kw = hlj_pc_bisect_scale(vx_in, vy_in, w_in, wheel_rpm, 0U, 1.0f, 0.25f, 1.0f, budget); } else { /* 非小陀螺模式:等比缩放平移速度保持方向不变 */ kv = hlj_pc_bisect_scale(vx_in, vy_in, w_in, wheel_rpm, 1U, 1.0f, 0.0f, 1.0f, budget); } } *vx_out = vx_in * kv; *vy_out = vy_in * kv; *w_out = w_in * kw; g_hlj_pc_info.kv_scale = kv; g_hlj_pc_info.kw_scale = kw; g_hlj_pc_info.velocity_limited = limited; g_hlj_pc_info.planned_est_power_w = limited ? hlj_pc_estimate_motion_power(*vx_out, *vy_out, *w_out, wheel_rpm) : p_full; } /* *函数简介:四轮最终电流指令功率闭环限幅(兜底分配) *参数说明:wheel_rpm:四轮当前实测转子转速(RPM) *参数说明:motor_cmds:四轮PID原始指令数组(原地写回限幅后指令) *返回类型:无 *备注:保证四轮正功率之和不超过total_budget_w */ void hlj_pc_limit_motors(const float wheel_rpm[4], int16_t motor_cmds[4]) { if (wheel_rpm == NULL || motor_cmds == NULL) return; if (!hlj_pc_inited) hlj_pc_init(); const float budget = g_hlj_pc_info.total_budget_w; float initial_p[4] = {0.0f}; float total_p = 0.0f; for (uint8_t i = 0U; i < 4U; i++) { initial_p[i] = hlj_pc_calc_single_power(wheel_rpm[i], (float)motor_cmds[i], NULL, NULL); if (initial_p[i] > 0.0f) total_p += initial_p[i]; } g_hlj_pc_info.initial_total_power = total_p; g_hlj_pc_info.final_power_scale = 1.0f; g_hlj_pc_info.current_limited = 0U; if (total_p > budget && total_p > 0.0f) { const float scale = (budget > 0.0f) ? (budget / total_p) : 0.0f; g_hlj_pc_info.final_power_scale = scale; g_hlj_pc_info.current_limited = 1U; for (uint8_t i = 0U; i < 4U; i++) { if (initial_p[i] <= 0.0f) continue; motor_cmds[i] = hlj_pc_solve_single_cmd(wheel_rpm[i], motor_cmds[i], initial_p[i] * scale); } } }