feat: 优化着陆控制逻辑,加入横向速度纠偏

This commit is contained in:
2026-01-31 12:57:58 +08:00
parent e7c2604e78
commit 4b4736ecce

View File

@@ -27,7 +27,15 @@ class SpaceX_Lander:
self.loop_dt = 0.05 # 控制周期
# PID 控制器
self.speed_pid = PID(kp=0.06, ki=0.05, kd=0.05, integral_limit=10.0, alpha=0.5, output_limit=(0.0, 1.0))
# 垂直速度 -> 节流阀 (不需要变,保持现状)
self.vertical_speed_pid = PID(kp=0.06, ki=0.05, kd=0.05, integral_limit=10.0, alpha=0.5, output_limit=(0.0, 1.0))
# 横向速度 -> 倾斜角度 (Aggressive Tuning)
# kp 增加到 3.0:意指每 1m/s 的横向速度误差,就倾斜 3 度去修正
# ki 增加到 0.02:消除残余稳态误差
# kd 增加到 0.1:提供更好的阻尼,防止震荡
self.horizontal_north_pid = PID(kp=3.0, ki=0.02, kd=0.1, integral_limit=15.0, alpha=0.5, output_limit=(-20.0, 20.0))
self.horizontal_east_pid = PID(kp=3.0, ki=0.02, kd=0.1, integral_limit=15.0, alpha=0.5, output_limit=(-20.0, 20.0))
# 状态
self.state = LandingState.GLIDE
@@ -69,7 +77,11 @@ class SpaceX_Lander:
last_time = now
# 获取实时飞行数据
flight = self.vessel.flight(self.celestial.reference_frame)
hybrid_ref = self.conn.space_center.ReferenceFrame.create_hybrid( # type: ignore
position=self.vessel.orbit.body.reference_frame,
rotation=self.vessel.surface_reference_frame
)
flight = self.vessel.flight(hybrid_ref)
current_altitude = flight.surface_altitude
current_velocity = flight.velocity
current_vertical_speed = flight.vertical_speed
@@ -104,21 +116,44 @@ class SpaceX_Lander:
# 2. 状态执行逻辑
throttle = 0.0
# 计算横向控制输出 (全阶段开启,除非触地瞬间)
# 目标横向速度为 0
north_error = 0 - current_velocity[1]
east_error = 0 - current_velocity[2]
north_output = self.horizontal_north_pid.update(north_error, dt)
east_output = self.horizontal_east_pid.update(east_error, dt)
# 根据不同阶段应用差异化的横向姿态控制
# 动力阶段LAND/FINAL需要倾斜推力来反推气动阶段GLIDE则需要调整攻角利用阻力和升力
dynamic_max_tilt = max(5.0, min(20.0, h_err * 0.4))
if self.state == LandingState.GLIDE:
# 气动阶段:采用反向纠偏策略 (依靠舰体气动效应)
self.set_steering_with_lateral_error(-east_output * 3, -north_output * 3, max_tilt=dynamic_max_tilt)
else:
# 动力阶段:采用正向纠偏策略 (依靠引擎推力矢量)
self.set_steering_with_lateral_error(east_output, north_output, max_tilt=dynamic_max_tilt)
if self.state == LandingState.GLIDE:
throttle = 0.0
print(f"高度: {h_err:.1f}m, 点火距: {transition_distance:.1f}m, 速: {current_velocity[1]:.1f}m/s, 速: {current_velocity[0]:.1f}m/s, 状态: GLIDE", " "*10, end="\r")
print(f"高度: {h_err:.1f}m, 点火距: {transition_distance:.1f}m, 状态: GLIDE, 北速: {current_velocity[1]:.1f}m/s, 速: {current_velocity[2]:.1f}m/s", " "*5, end="\r")
elif self.state == LandingState.LAND:
# 动态减速曲线
a_plan = max(0.1, a_net_max * self.safety_factor)
ideal_speed = -(max(0, self.target_landing_speed**2 + 2 * a_plan * (h_err - self.final_approach_alt))**0.5)
throttle = self.speed_pid.update(ideal_speed - current_vertical_speed, dt)
print(f"高度: {h_err:.1f}m, 理想速: {ideal_speed:.1f}m/s, 当前速: {current_vertical_speed:.1f}m/s, 状态: LAND", " "*10, end="\r")
throttle = self.vertical_speed_pid.update(ideal_speed - current_vertical_speed, dt)
print(f"高度: {h_err:.1f}m, 理想速: {ideal_speed:.1f}m/s, 当前速: {current_vertical_speed:.1f}m/s, 状态: LAND, 偏角: {dynamic_max_tilt:.1f}", " "*5, end="\r")
elif self.state == LandingState.FINAL:
# 匀速下降阶段
throttle = self.speed_pid.update(self.target_landing_speed - current_vertical_speed, dt)
print(f"高度: {h_err:.1f}m, 目标速: {self.target_landing_speed:.1f}m/s, 当前速: {current_vertical_speed:.1f}m/s, 状态: FINAL", " "*10, end="\r")
throttle = self.vertical_speed_pid.update(self.target_landing_speed - current_vertical_speed, dt)
print(f"高度: {h_err:.1f}m, 目标速: {self.target_landing_speed:.1f}m/s, 当前速: {current_vertical_speed:.1f}m/s, 状态: FINAL", " "*5, end="\r")
# 触地检测
if h_err < 0.5 and abs(current_vertical_speed) < 0.5:
@@ -140,7 +175,7 @@ class SpaceX_Lander:
def _switch_state(self, new_state):
print(f"\n[状态切换]: {self.state} -> {new_state}")
self.state = new_state
self.speed_pid.reset()
self.vertical_speed_pid.reset()
if __name__ == "__main__":
lander = SpaceX_Lander()