使用STK的Python API和Astrogator模块来创建一个简单的霍曼转移轨道场景(从近地轨道转移到地球同步轨道):
1、创建一个新的场景并添加一个卫星。
2、定义卫星的初始状态(近地轨道的参数)。
3、传播近地轨道。
4、使用目标序列和DV1机动将卫星转移到转移椭圆轨道。
5、传播转移椭圆轨道到远地点。
6、使用目标序列和DV2机动将卫星转移到外部轨道(地球同步轨道)。
7、传播外部轨道。
8、运行任务控制序列(MCS)并应用轨道机动。
9、打印结果并关闭场景。
import os # 导入os模块,用于操作系统相关的功能
# 尝试导入STK相关模块,如果失败则打印错误信息
try:
if os.name == "nt": # 判断是否在Windows系统上运行
from agi.stk12.stkdesktop import STKDesktop # 导入STK Desktop模块
else:
from agi.stk12.stkengine import STKEngine # 导入STK Engine模块
from agi.stk12.stkobjects import * # 导入STK对象模块
from agi.stk12.stkobjects.astrogator import * # 导入Astrogator模块
from agi.stk12.utilities.colors import * # 导入颜色工具模块
except:
print("Failed to import stk modules. Make sure you have installed the STK Python API wheel (agi.stk<..ver..>-py3-none-any.whl) from the STK Install bin directory")
# 启动STK应用程序并获取根对象
if os.name == "nt":
app = STKDesktop.StartApplication(visible=True, userControl=True) # 在Windows上启动STK Desktop应用程序
stkRoot = app.Root # 获取STK的根对象
else:
app = STKEngine.StartApplication(noGraphics=False) # 在非Windows系统上启动STK Engine应用程序
stkRoot = app.NewObjectRoot() # 获取STK的根对象
# 检查是否有使用Astrogator模块的许可
if not stkRoot.AvailableFeatures.IsPropagatorTypeAvailable(AgEVePropagatorType.ePropagatorAstrogator):
print("You do not have the required license.")
"""1-创建一个新的场景和卫星对象"""
print("New Scenario...")
stkRoot.NewScenario("HohmannTransfer") # 创建名为"HohmannTransfer"的新场景
sat1 = stkRoot.CurrentScenario.Children.New(AgESTKObjectType.eSatellite, "Satellite1") # 在场景中创建名为"Satellite1"的卫星对象
sat1.SetPropagatorType(AgEVePropagatorType.ePropagatorAstrogator) # 设置卫星的轨道传播器类型为Astrogator
_driver = sat1.Propagator # 获取卫星的轨道传播器对象
_driver.MainSequence.RemoveAll() # 清除轨道传播器的主序列中的所有元素
_driver.Options.DrawTrajectoryIn3D = True # 设置在3D视图中绘制轨迹
input("Press enter to continue...")
"""2-定义卫星的初始状态"""
print("Define the Initial State...")
# 在主序列中插入初始状态段,命名为"Inner Orbit"
initState = _driver.MainSequence.Insert(AgEVASegmentType.eVASegmentTypeInitialState, "Inner Orbit", "-")
initState.SetElementType(AgEVAElementType.eVAElementTypeKeplerian) # 设置初始状态的元素类型为开普勒元素
modKep = initState.Element # 获取开普勒元素对象
modKep.PeriapsisRadiusSize = 6700 # 设置近拱点半径为6700公里
modKep.ArgOfPeriapsis = 0 # 设置近拱点幅角为0度
modKep.Eccentricity = 0 # 设置偏心率为0(圆轨道)
modKep.Inclination = 0 # 设置轨道倾角为0度
modKep.RAAN = 0 # 设置升交点赤经为0度
modKep.TrueAnomaly = 0 # 设置真近点角为0度
input("Press enter to continue...")
"""3-传播近地轨道"""
print("Propagate the Parking Orbit...")
propagate = _driver.MainSequence.Insert(AgEVASegmentType.eVASegmentTypePropagate, "Propagate", "-") # 在主序列中插入传播段,命名为"Propagate"
propagate.PropagatorName = "Earth Point Mass" # 设置传播器为地球点质量模型
propagate.Properties.Color = Colors.Blue # 设置轨迹颜色为蓝色
propagate.StoppingConditions["Duration"].Properties.Trip = 7200 # 设置停止条件为持续时间7200秒(2小时)
input("Press enter to continue...")
"""4-使用目标序列和 DV1 机动将卫星转移到转移椭圆轨道"""
print("Target Sequence containing Maneuver into the Transfer Ellipse...")
ts = _driver.MainSequence.Insert(AgEVASegmentType.eVASegmentTypeTargetSequence, "Start Transfer", "-") # 在主序列中插入目标序列段,命名为"Start Transfer"
dv1 = ts.Segments.Insert(AgEVASegmentType.eVASegmentTypeManeuver, "DV1", "-") # 在目标序列中插入机动段,命名为"DV1"
dv1.SetManeuverType(AgEVAManeuverType.eVAManeuverTypeImpulsive) # 设置机动类型为冲动机动
impulsive = dv1.Maneuver # 获取冲动机动对象
impulsive.SetAttitudeControlType(AgEVAAttitudeControl.eVAAttitudeControlThrustVector) # 设置姿态控制类型为推力矢量控制
thrustVector = impulsive.AttitudeControl # 获取推力矢量控制对象
thrustVector.ThrustAxesName = "Satellite/Satellite1 VNC(Earth)" # 设置推力轴名称为卫星的VNC(地球)坐标系
dv1.EnableControlParameter(AgEVAControlManeuver.eVAControlManeuverImpulsiveCartesianX) # 启用X方向的控制参数
dv1.Results.Add("Keplerian Elems/Radius of Apoapsis") # 添加结果参数:远地点半径
dc = ts.Profiles["Differential Corrector"] # 获取差分校正器对象
xControlParam = dc.ControlParameters.GetControlByPaths("DV1", "ImpulsiveMnvr.Cartesian.X") # 获取X方向控制参数对象
xControlParam.Enable = True # 启用X方向控制参数
xControlParam.MaxStep = 0.3 # 设置X方向控制参数的最大步长为0.3公里/秒
roaResult = dc.Results.GetResultByPaths("DV1", "Radius Of Apoapsis") # 获取远地点半径结果对象
roaResult.Enable = True # 启用远地点半径结果
roaResult.DesiredValue = 42238 # 设置远地点半径的期望值为42238公里(地球同步轨道的半径)
roaResult.Tolerance = 0.1 # 设置远地点半径的容差为0.1公里
dc.MaxIterations = 50 # 设置差分校正器的最大迭代次数为50
dc.EnableDisplayStatus = True # 启用差分校正器的状态显示
dc.Mode = AgEVAProfileMode.eVAProfileModeIterate # 设置差分校正器的模式为迭代模式
ts.Action = AgEVATargetSeqAction.eVATargetSeqActionRunActiveProfiles # 设置目标序列的动作为运行激活的轮廓
input("Press enter to continue...")
"""5-传播转移椭圆轨道到远地点"""
print("Propagate the Transfer Orbit to Apogee...")
# 在主序列中插入传播段,命名为"Transfer Ellipse"
transferEllipse = _driver.MainSequence.Insert(AgEVASegmentType.eVASegmentTypePropagate, "Transfer Ellipse", "-")
transferEllipse.Properties.Color = Colors.Red # 设置轨迹颜色为红色
transferEllipse.PropagatorName = "Earth Point Mass" # 设置传播器为地球点质量模型
transferEllipse.StoppingConditions.Add("Apoapsis") # 设置停止条件为到达远地点
transferEllipse.StoppingConditions.Remove("Duration") # 移除持续时间作为停止条件
input("Press enter to continue...")
"""6-使用目标序列和 DV2 机动转移到外部轨道(同步轨道)"""
print("Target Sequence containing Maneuver into the Outer Orbit...")
ts = _driver.MainSequence.Insert(AgEVASegmentType.eVASegmentTypeTargetSequence, "Finish Transfer", "-") # 在主序列中插入目标序列段,命名为"Finish Transfer"
dv2 = ts.Segments.Insert(AgEVASegmentType.eVASegmentTypeManeuver, "DV2", "-") # 在目标序列中插入机动段,命名为"DV2"
dv2.SetManeuverType(AgEVAManeuverType.eVAManeuverTypeImpulsive) # 设置机动类型为冲动机动
impulsive = dv2.Maneuver # 获取冲动机动对象
impulsive.SetAttitudeControlType(AgEVAAttitudeControl.eVAAttitudeControlThrustVector) # 设置姿态控制类型为推力矢量控制
thrustVector = impulsive.AttitudeControl # 获取推力矢量控制对象
thrustVector.ThrustAxesName = "Satellite/Satellite1 VNC(Earth)" # 设置推力轴名称为卫星的VNC(地球)坐标系
dv2.EnableControlParameter(AgEVAControlManeuver.eVAControlManeuverImpulsiveCartesianX) # 启用X方向的控制参数
dv2.Results.Add("Keplerian Elems/Eccentricity") # 添加结果参数:偏心率
dc = ts.Profiles["Differential Corrector"] # 获取差分校正器对象
xControlParam2 = dc.ControlParameters.GetControlByPaths("DV2", "ImpulsiveMnvr.Cartesian.X") # 获取X方向控制参数对象
xControlParam2.Enable = True # 启用X方向控制参数
xControlParam2.MaxStep = 0.3 # 设置X方向控制参数的最大步长为0.3公里/秒
eccResult = dc.Results.GetResultByPaths("DV2", "Eccentricity") # 获取偏心率结果对象
eccResult.Enable = True # 启用偏心率结果
eccResult.DesiredValue = 0 # 设置偏心率的期望值为0(圆轨道)
dc.EnableDisplayStatus = True # 启用差分校正器的状态显示
dc.Mode = AgEVAProfileMode.eVAProfileModeIterate # 设置差分校正器的模式为迭代模式
ts.Action = AgEVATargetSeqAction.eVATargetSeqActionRunActiveProfiles # 设置目标序列的动作为运行激活的轮廓
input("Press enter to continue...")
"""7-传播外部轨道"""
print("Propagate the Outer Orbit...")
outerOrbit = _driver.MainSequence.Insert(AgEVASegmentType.eVASegmentTypePropagate, "Outer Orbit", "-") # 在主序列中插入传播段,命名为"Outer Orbit"
outerOrbit.Properties.Color = Colors.Green
"""8-运行任务控制序列(MCS)并应用轨道机动"""
print("Run MCS...")
_driver.RunMCS() # 运行任务控制序列
startTransfer = _driver.MainSequence["Start Transfer"] # 获取"Start Transfer"目标序列对象
finishTransfer = _driver.MainSequence["Finish Transfer"] # 获取"Finish Transfer"目标序列对象
startDC = startTransfer.Profiles["Differential Corrector"] # 获取"Start Transfer"差分校正器对象
finishDC = finishTransfer.Profiles["Differential Corrector"] # 获取"Finish Transfer"差分校正器对象
dv1 = startTransfer.Segments["DV1"] # 获取"DV1"机动段对象
dv1Impulsive = dv1.Maneuver # 获取"DV1"冲动机动对象
dv1ThrustVector = dv1Impulsive.AttitudeControl # 获取"DV1"推力矢量控制对象
startTransfer.ApplyProfiles() # 应用"Start Transfer"目标序列的轮廓
dv2 = finishTransfer.Segments["DV2"] # 获取"DV2"机动段对象
dv2Impulsive = dv2.Maneuver # 获取"DV2"冲动机动对象
dv2ThrustVector = dv2Impulsive.AttitudeControl # 获取"DV2"推力矢量控制对象
finishTransfer.ApplyProfiles() # 应用"Finish Transfer"目标序列的轮廓
input("Press enter to continue...")
"""9-打印结果并关闭界面"""
print("Print Results...")
reportData = ""
outerOrbit = _driver.MainSequence["Outer Orbit"] # 获取"Outer Orbit"传播段对象
result = outerOrbit.ExecSummary # 获取"Outer Orbit"执行摘要结果
print(result.Category) # 打印结果类别
intervals = result.Value # 获取结果区间集合
for i in range(0, intervals.Count): # 遍历每个区间
interval = intervals.Item(i) # 获取当前区间
datasets = interval.DataSets # 获取当前区间的数据集合
for j in range(0, datasets.Count): # 遍历每个数据集
dataset = datasets.Item(j) # 获取当前数据集
print("Title - " + dataset.ElementName) # 打印数据集标题
elements = dataset.GetValues() # 获取数据集的值
for o in elements: # 遍历每个值
reportData += o + "\r\n" # 将值添加到报告数据字符串中
print(reportData) # 打印报告数据
print("Finished...") # 打印完成信息
input("Press enter to close the application...") # 等待用户输入以关闭应用程序
# 关闭场景并关闭应用程序
stkRoot.CloseScenario() # 关闭场景
app.ShutDown() # 关闭应用程序