From d7a93b88efe610e600d6657f521d453772033419 Mon Sep 17 00:00:00 2001 From: Armor <2654988228@qq.com> Date: Wed, 14 Jan 2026 23:45:49 +0800 Subject: [PATCH] update --- src/KSP_tools/krpc_explore.py | 41 ++++++++++++++++++++++++----------- 1 file changed, 28 insertions(+), 13 deletions(-) diff --git a/src/KSP_tools/krpc_explore.py b/src/KSP_tools/krpc_explore.py index 58c54fa..0c07353 100644 --- a/src/KSP_tools/krpc_explore.py +++ b/src/KSP_tools/krpc_explore.py @@ -102,6 +102,11 @@ def auto_land_predictive(host='localhost', port=50000, target_altitude=0.5, targ # WAIT 阶段按折减推力估算刹车距离(提前点火,补偿点火/推力建立延迟) thrust_discount_wait = 0.9 + # 低空/接地参数(用于“最后几米慢下来”) + touchdown_vspeed_tol = 0.6 # m/s:认为“软着陆”的竖直速度容差 + low_altitude_pid_switch = 10.0 # m:低于该高度切换到竖直速度控制 + vspeed_p_gain = 0.6 # 比例增益:越大越积极(0.3~1.0 之间可调) + def _get_g(local_vessel): try: body = local_vessel.orbit.body @@ -217,23 +222,26 @@ def auto_land_predictive(host='localhost', port=50000, target_altitude=0.5, targ alt, srf_speed, v_speed, mass, avail_thrust, g_local, situation_name = _read_state() remaining_alt = max(0.0, alt - float(target_altitude)) - v = max(0.0, float(srf_speed)) - vf = max(0.0, abs(float(target_vspeed))) + # BURN 阶段以“竖直下降速度”为控制对象:下降为负,统一转成向下为正的标量 v_down + v_down = max(0.0, -float(v_speed)) + vf_down = max(0.0, abs(float(target_vspeed))) # 目标接地竖直速度(向下为正) - # 终止条件:以 vessel.situation.name == 'landed' 为准 - if str(situation_name).strip().lower() == 'landed': + # 终止条件:LANDED 可能会在仍有较大竖直速度时触发(弹跳/碰撞瞬间) + # 因此要求:LANDED 且竖直速度足够小才结束 + is_landed = str(situation_name).strip().lower() == 'landed' + if is_landed and v_down <= (vf_down + float(touchdown_vspeed_tol)): try: vessel.control.throttle = 0.0 except Exception: pass status['result'] = 'landed' - status['logs'].append('检测到 vessel.situation=LANDED,着陆完成') + status['logs'].append('检测到 vessel.situation=LANDED 且竖直速度满足软着陆阈值,着陆完成') if verbose: - print('检测到 vessel.situation=LANDED,着陆完成') + print('检测到 vessel.situation=LANDED 且竖直速度满足软着陆阈值,着陆完成') break # 兜底:极低高度 + 速度足够小(防止 situation 迟迟不切换导致卡死) - if remaining_alt <= max(0.2, float(target_altitude)) and v <= (vf + 0.5): + if remaining_alt <= max(0.2, float(target_altitude)) and v_down <= (vf_down + float(touchdown_vspeed_tol)): try: vessel.control.throttle = 0.0 except Exception: @@ -244,11 +252,16 @@ def auto_land_predictive(host='localhost', port=50000, target_altitude=0.5, targ print('达到高度/速度阈值,但 vessel.situation 仍未变为 LANDED') break - # 计算所需净向上加速度 - if remaining_alt <= 1e-3: - a_req_up = 0.0 + # 低空策略:切换到“竖直速度跟踪”以确保最后几米把速度压到目标值 + if alt <= float(low_altitude_pid_switch): + # 需要的净向上加速度:a_net_up = k * (v_down - vf_down) + a_req_up = max(0.0, float(vspeed_p_gain) * max(0.0, (v_down - vf_down))) else: - a_req_up = max(0.0, (v * v - vf * vf) / (2.0 * remaining_alt)) + # 中高空:用距离反推所需净向上加速度(使用竖直下降速度) + if remaining_alt <= 1e-3: + a_req_up = 0.0 + else: + a_req_up = max(0.0, (v_down * v_down - vf_down * vf_down) / (2.0 * remaining_alt)) # 推力不足时给满油门 if mass <= 0 or avail_thrust <= 1e-6: @@ -259,7 +272,7 @@ def auto_land_predictive(host='localhost', port=50000, target_altitude=0.5, targ throttle = max(0.0, min(1.0, float(throttle))) # 低空兜底:高度<5m 且仍很快 -> 满油门 - if alt < 5.0 and v > 10.0: + if alt < 5.0 and v_down > 10.0: throttle = 1.0 try: @@ -271,7 +284,9 @@ def auto_land_predictive(host='localhost', port=50000, target_altitude=0.5, targ msg = ( f'BURN alt={alt:.1f} rem={remaining_alt:.1f} ' f'srf_speed={srf_speed:.2f} vs={v_speed:.2f} ' - f'a_req_up={a_req_up:.2f} throttle={throttle:.2f}' + f'v_down={v_down:.2f} vf_down={vf_down:.2f} ' + f'a_req_up={a_req_up:.2f} throttle={throttle:.2f} ' + f'landed={int(is_landed)}' ) status['logs'].append(msg) print(msg)