用python 在krpc上 设计一个火箭自动入轨的程序
以下是一个基本的火箭自动入轨程序,使用Python和KRPC库编写。
首先,我们需要导入所需的库和模块:
import time
import math
import krpc
然后,我们需要连接到Kerbal Space Program,并获取所有必需的对象:
conn = krpc.connect(name='Automatic Orbit Program')
vessel = conn.space_center.active_vessel
body = vessel.orbit.body
reference_frame = vessel.orbit.body.reference_frame
flight_info = vessel.flight(reference_frame)
orbit_info = vessel.orbit
接下来,我们需要定义一些常量和变量,以及一些辅助函数:
# Constant values
MIN_THROTTLE = 0.5
MAX_THROTTLE = 1.0
MIN_ALTITUDE = 80000
MAX_ALTITUDE = 100000
ANGULAR_TOLERANCE = 0.1
TIME_TO_APOAPSIS_TOLERANCE = 1
TIME_TO_PERIAPSIS_TOLERANCE = 1
# Variables
target_apoapsis = MAX_ALTITUDE
target_periapsis = MIN_ALTITUDE
target_inclination = math.radians(90)
target_period = 2 * math.pi * math.sqrt(body.gravitational_parameter / (target_altitude + body.equatorial_radius) ** 3)
target_speed = math.sqrt(body.gravitational_parameter / (target_altitude + body.equatorial_radius))
# Helper functions
def set_throttle(throttle):
vessel.control.throttle = max(MIN_THROTTLE, min(MAX_THROTTLE, throttle))
def get_time_to_apoapsis():
return orbit_info.time_to_apoapsis
def get_time_to_periapsis():
return orbit_info.time_to_periapsis
def get_apoapsis_altitude():
return orbit_info.apoapsis_altitude
def get_periapsis_altitude():
return orbit_info.periapsis_altitude
def get_inclination():
return orbit_info.inclination
def get_speed():
return flight_info.speed
def get_pitch():
return flight_info.pitch
def get_heading():
return flight_info.heading
def get_roll():
return flight_info.roll
def get_angular_difference(a, b):
return (a - b + 180) % 360 - 180
def set_pitch_and_heading(pitch, heading):
vessel.auto_pilot.target_pitch_and_heading(pitch, heading)
def set_roll(roll):
vessel.auto_pilot.target_roll(roll)
def set_sas_mode(mode):
vessel.auto_pilot.sas_mode = mode
def set_rcs(state):
vessel.control.rcs = state
def set_sas(state):
vessel.control.sas = state
def get_apoapsis_velocity():
return orbit_info.orbital_speed_at(get_apoapsis_altitude())
def get_periapsis_velocity():
return orbit_info.orbital_speed_at(get_periapsis_altitude())
def get_target_velocity():
return target_speed
def get_heading_difference():
return get_angular_difference(get_heading(), math.degrees(target_heading))
def get_pitch_difference():
return get_angular_difference(get_pitch(), math.degrees(target_pitch))
def get_roll_difference():
return get_angular_difference(get_roll(), math.degrees(target_roll))
def get_angular_tolerance():
return math.radians(ANGULAR_TOLERANCE)
def get_time_to_apoapsis_tolerance():
return TIME_TO_APOAPSIS_TOLERANCE
def get_time_to_periapsis_tolerance():
return TIME_TO_PERIAPSIS_TOLERANCE
def get_target_apoapsis():
return target_apoapsis
def get_target_periapsis():
return target_periapsis
def get_target_inclination():
return target_inclination
def get_target_period():
return target_period
def get_target_speed():
return target_speed
def set_target_apoapsis(altitude):
global target_apoapsis
target_apoapsis = altitude
def set_target_periapsis(altitude):
global target_periapsis
target_periapsis = altitude
def set_target_inclination(inclination):
global target_inclination
target_inclination = inclination
def set_target_period(period):
global target_period
target_period = period
def set_target_speed(speed):
global target_speed
target_speed = speed
现在,我们可以开始设置自动入轨程序的主要功能。首先,我们将打开RCS和SAS:
set_rcs(True)
set_sas(True)
然后,我们将把火箭的姿态调整为垂直于地面,并等待火箭升空:
set_pitch_and_heading(-90, 90)
while get_altitude() < 100:
time.sleep(0.1)
接下来,我们将把火箭的俯仰角和偏航角调整为目标轨道的倾角和经过赤道的方向角:
target_heading = 0
target_pitch = math.degrees(target_inclination) - 90
set_pitch_and_heading(target_pitch, target_heading)
while abs(get_pitch_difference()) > get_angular_tolerance() or \
abs(get_heading_difference()) > get_angular_tolerance():
time.sleep(0.1)
然后,我们将等待火箭达到目标轨道的高度和速度:
while get_altitude() < target_apoapsis or get_speed() < target_speed:
set_throttle(MAX_THROTTLE)
time.sleep(0.1)
set_throttle(0)
接下来,我们将等待火箭到达目标轨道的近地点:
while get_time_to_periapsis() > get_time_to_periapsis_tolerance():
set_throttle(MAX_THROTTLE)
time.sleep(0.1)
set_throttle(0)
然后,我们将把火箭的姿态调整为与目标轨道相同的方向,然后等待火箭到达目标轨道的远地点:
target_heading = math.degrees(orbit_info.longitude_of_ascending_node)
target_pitch = 0
set_pitch_and_heading(target_pitch, target_heading)
while abs(get_heading_difference()) > get_angular_tolerance():
time.sleep(0.1)
while get_time_to_apoapsis() > get_time_to_apoapsis_tolerance():
set_throttle(MAX_THROTTLE)
time.sleep(0.1)
set_throttle(0)
最后,我们将关闭RCS和SAS,并完成自动入轨程序:
set_rcs(False)
set_sas(False)
print('Orbit achieved!')
这是一个非常基本的自动入轨程序,可以根据需要进行修改和改进
原文地址: https://www.cveoy.top/t/topic/hefv 著作权归作者所有。请勿转载和采集!