-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpython_stk_test_auto_create_xingzuo.py
273 lines (235 loc) · 12.5 KB
/
python_stk_test_auto_create_xingzuo.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
import time
from tqdm import tqdm
startTime = time.time()
from comtypes.gen import STKObjects, STKUtil, AgStkGatorLib
from comtypes.client import CreateObject, GetActiveObject, GetEvents, CoGetObject, ShowEvents
from ctypes import *
import comtypes.gen._00020430_0000_0000_C000_000000000046_0_2_0
from comtypes import GUID
from comtypes import helpstring
from comtypes import COMMETHOD
from comtypes import dispid
from ctypes.wintypes import VARIANT_BOOL
from ctypes import HRESULT
from comtypes import BSTR
from comtypes.automation import VARIANT
from comtypes.automation import _midlSAFEARRAY
from comtypes import CoClass
from comtypes import IUnknown
import comtypes.gen._00DD7BD4_53D5_4870_996B_8ADB8AF904FA_0_1_0
import comtypes.gen._8B49F426_4BF0_49F7_A59B_93961D83CB5D_0_1_0
from comtypes.automation import IDispatch
import comtypes.gen._42D2781B_8A06_4DB2_9969_72D6ABF01A72_0_1_0
from comtypes import DISPMETHOD, DISPPROPERTY, helpstring
"""
SET TO TRUE TO USE ENGINE, FALSE TO USE GUI
"""
useStkEngine = False
Read_Scenario = False
############################################################################
# Scenario Setup
############################################################################
if useStkEngine:
# Launch STK Engine
print("Launching STK Engine...")
stkxApp = CreateObject("STKX11.Application")
# Disable graphics. The NoGraphics property must be set to true before the root object is created.
stkxApp.NoGraphics = True
# Create root object
stkRoot = CreateObject('AgStkObjects11.AgStkObjectRoot')
else:
# Launch GUI
print("Launching STK...")
if not Read_Scenario:
uiApp = CreateObject("STK11.Application")
else:
uiApp = GetActiveObject("STK11.Application")
uiApp.Visible = True
uiApp.UserControl = True
# Get root object
stkRoot = uiApp.Personality2
# Set date format
stkRoot.UnitPreferences.SetCurrentUnit("DateFormat", "UTCG")
# Create new scenario
print("Creating scenario...")
if not Read_Scenario:
# stkRoot.NewScenario('Kuiper')
stkRoot.NewScenario('StarLink')
scenario = stkRoot.CurrentScenario
scenario2 = scenario.QueryInterface(STKObjects.IAgScenario)
# scenario2.StartTime = '24 Sep 2020 16:00:00.00'
# scenario2.StopTime = '25 Sep 2020 16:00:00.00'
totalTime = time.time() - startTime
splitTime = time.time()
print("--- Scenario creation: {a:4.3f} sec\t\tTotal time: {b:4.3f} sec ---".format(a=totalTime, b=totalTime))
# 创建卫星星系
def Creat_satellite(numOrbitPlanes=2, numSatsPerPlane=10, hight=550, Inclination=53, name='Sat'):
# Create constellation object
constellation = scenario.Children.New(STKObjects.eConstellation, name)
constellation2 = constellation.QueryInterface(STKObjects.IAgConstellation)
# Insert the constellation of Satellites
for orbitPlaneNum in range(numOrbitPlanes): # RAAN in degrees
for satNum in range(numSatsPerPlane): # trueAnomaly in degrees
# Insert satellite
satellite = scenario.Children.New(STKObjects.eSatellite, f"{name}{orbitPlaneNum}_{satNum}")
satellite2 = satellite.QueryInterface(STKObjects.IAgSatellite)
# Select Propagator
satellite2.SetPropagatorType(STKObjects.ePropagatorTwoBody)
# Set initial state
twoBodyPropagator = satellite2.Propagator.QueryInterface(STKObjects.IAgVePropagatorTwoBody)
keplarian = twoBodyPropagator.InitialState.Representation.ConvertTo(
STKUtil.eOrbitStateClassical).QueryInterface(STKObjects.IAgOrbitStateClassical)
keplarian.SizeShapeType = STKObjects.eSizeShapeSemimajorAxis
keplarian.SizeShape.QueryInterface(
STKObjects.IAgClassicalSizeShapeSemimajorAxis).SemiMajorAxis = hight + 6371 # km
keplarian.SizeShape.QueryInterface(STKObjects.IAgClassicalSizeShapeSemimajorAxis).Eccentricity = 0
keplarian.Orientation.Inclination = int(Inclination) # degrees
keplarian.Orientation.ArgOfPerigee = 0 # degrees
keplarian.Orientation.AscNodeType = STKObjects.eAscNodeRAAN
RAAN = 360 / numOrbitPlanes * orbitPlaneNum
keplarian.Orientation.AscNode.QueryInterface(STKObjects.IAgOrientationAscNodeRAAN).Value = RAAN # degrees
keplarian.LocationType = STKObjects.eLocationTrueAnomaly
trueAnomaly = 360 / numSatsPerPlane * satNum
keplarian.Location.QueryInterface(STKObjects.IAgClassicalLocationTrueAnomaly).Value = trueAnomaly
# Propagate
satellite2.Propagator.QueryInterface(STKObjects.IAgVePropagatorTwoBody).InitialState.Representation.Assign(
keplarian)
satellite2.Propagator.QueryInterface(STKObjects.IAgVePropagatorTwoBody).Propagate()
# Add to constellation object
constellation2.Objects.AddObject(satellite)
# 为每个卫星加上发射机和接收机
def Add_transmitter_receiver(sat_list):
for each in sat_list:
Instance_name = each.InstanceName
# new transmitter and receiver
transmitter = each.Children.New(STKObjects.eTransmitter, "Transmitter_" + Instance_name)
reciver = each.Children.New(STKObjects.eReceiver, "Reciver_" + Instance_name)
# sensor = each.Children.New(STKObjects.eSensor, 'Sensor_' + Instance_name)
# 设置发射机参数
def Set_Transmitter_Parameter(transmitter, frequency=12, EIRP=20, DataRate=14):
transmitter2 = transmitter.QueryInterface(STKObjects.IAgTransmitter) # 建立发射机的映射,以便对其进行设置
transmitter2.SetModel('Simple Transmitter Model')
txModel = transmitter2.Model
txModel = txModel.QueryInterface(STKObjects.IAgTransmitterModelSimple)
txModel.Frequency = frequency # GHz range:10.7-12.7GHz
txModel.EIRP = EIRP # dBW
txModel.DataRate = DataRate # Mb/sec
# 设置接收机参数
def Set_Receiver_Parameter(receiver, GT=20, frequency=12):
receiver2 = receiver.QueryInterface(STKObjects.IAgReceiver) # 建立发射机的映射,以便对其进行设置
receiver2.SetModel('Simple Receiver Model')
recModel = receiver2.Model
recModel = recModel.QueryInterface(STKObjects.IAgReceiverModelSimple)
recModel.AutoTrackFrequency = False
recModel.Frequency = frequency # GHz range:10.7-12.7GHz
recModel.GOverT = GT # dB/K
return receiver2
# 获得接收机示例,并设置其参数
def Get_sat_receiver(sat, GT=20, frequency=12):
receiver = sat.Children.GetElements(STKObjects.eReceiver)[0] # 找到该卫星的接收机
receiver2 = Set_Receiver_Parameter(receiver=receiver, GT=GT, frequency=frequency)
return receiver2
# 计算传输链路
def Compute_access(access):
access.ComputeAccess()
accessDP = access.DataProviders.Item('Link Information')
accessDP2 = accessDP.QueryInterface(STKObjects.IAgDataPrvTimeVar)
Elements = ["Time", 'Link Name', 'EIRP', 'Prop Loss', 'Rcvr Gain', "Xmtr Gain", "Eb/No", "BER"]
results = accessDP2.ExecElements(scenario2.StartTime, scenario2.StopTime, 3600, Elements)
Times = results.DataSets.GetDataSetByName('Time').GetValues() # 时间
EbNo = results.DataSets.GetDataSetByName('Eb/No').GetValues() # 码元能量
BER = results.DataSets.GetDataSetByName('BER').GetValues() # 误码率
Link_Name = results.DataSets.GetDataSetByName('Link Name').GetValues()
Prop_Loss = results.DataSets.GetDataSetByName('Prop Loss').GetValues()
Xmtr_Gain = results.DataSets.GetDataSetByName('Xmtr Gain').GetValues()
EIRP = results.DataSets.GetDataSetByName('EIRP').GetValues()
# Range = results.DataSets.GetDataSetByName('Range').GetValues()
return Times, Link_Name, BER, EbNo, Prop_Loss, Xmtr_Gain, EIRP
def Creating_All_Access():
# 首先清空场景中所有的链接
print('Clearing All Access')
stkRoot.ExecuteCommand('RemoveAllAccess /')
# 计算某个卫星与其通信的四颗卫星的链路质量,并生成报告
for each_sat in tqdm(sat_list):
now_sat_name = each_sat.InstanceName
now_plane_num = int(now_sat_name.split('_')[0][3:])
now_sat_num = int(now_sat_name.split('_')[1])
now_sat_transmitter = each_sat.Children.GetElements(STKObjects.eTransmitter)[0] # 找到该卫星的发射机
Set_Transmitter_Parameter(now_sat_transmitter, EIRP=20)
# 发射机与接收机相连
# 与后面的卫星的接收机相连
access_backward = now_sat_transmitter.GetAccessToObject(
Get_sat_receiver(sat_dic['Sat' + str(now_plane_num) + '_' + str((now_sat_num + 1) % 22)]))
# 与前面的卫星的接收机相连
access_forward = now_sat_transmitter.GetAccessToObject(
Get_sat_receiver(sat_dic['Sat' + str(now_plane_num) + '_' + str((now_sat_num - 1) % 22)]))
# 与左面的卫星的接收机相连
access_left = now_sat_transmitter.GetAccessToObject(
Get_sat_receiver(sat_dic['Sat' + str((now_plane_num - 1) % 72) + '_' + str(now_sat_num)]))
# 与右面的卫星的接收机相连
access_right = now_sat_transmitter.GetAccessToObject(
Get_sat_receiver(sat_dic['Sat' + str((now_plane_num + 1) % 72) + '_' + str(now_sat_num)]))
B_Times, B_Link_Name, B_BER, B_EbNo, B_Prop_Loss, B_Xmtr_Gain, B_EIRP = Compute_access(access_backward)
F_Times, F_Link_Name, F_BER, F_EbNo, F_Prop_Loss, F_Xmtr_Gain, F_EIRP = Compute_access(access_forward)
L_Times, L_Link_Name, L_BER, L_EbNo, L_Prop_Loss, L_Xmtr_Gain, L_EIRP = Compute_access(access_left)
R_Times, R_Link_Name, R_BER, R_EbNo, R_Prop_Loss, R_Xmtr_Gain, R_EIRP = Compute_access(access_right)
print('{0}\r', R_Times, R_Link_Name, R_BER, R_EbNo, R_Prop_Loss, R_Xmtr_Gain, R_EIRP)
# stkRoot.ExecuteCommand('RemoveAllAccess /')
def Change_Sat_color(sat_list):
# 修改卫星及其轨道的颜色
print('Changing Color of Satellite')
for each_sat in tqdm(sat_list):
now_sat_name = each_sat.InstanceName
now_plane_num = int(now_sat_name.split('_')[0][3:])
now_sat_num = int(now_sat_name.split('_')[1])
satellite = each_sat.QueryInterface(STKObjects.IAgSatellite)
graphics = satellite.Graphics
graphics.SetAttributesType(1) # eAttributesBasic
attributes = graphics.Attributes
attributes_color = attributes.QueryInterface(STKObjects.IAgVeGfxAttributesBasic)
attributes_color.Inherit = False
# 16436871 浅蓝色
# 2330219 墨绿色
# 42495 橙色
# 9234160 米黄色
# 65535 黄色
# 255 红色
# 16776960 青色
color_sheet = [16436871, 2330219, 42495, 9234160, 65535, 255, 16776960]
if now_sat_name[2] == 'A':
color = 255
elif now_sat_name[2] == 'B':
color = 42495
elif now_sat_name[2] == 'C':
color = 16436871
attributes_color.Color = color
# 找出轨道对应的属性接口
orbit = attributes.QueryInterface(STKObjects.IAgVeGfxAttributesOrbit)
orbit.IsOrbitVisible = False # 将轨道设置为不可见
# 如果不是读取当前场景,即首次创建场景
if not Read_Scenario:
Creat_satellite(numOrbitPlanes=72, numSatsPerPlane=22, hight=550, Inclination=53) # Starlink
# Kuiper
# Creat_satellite(numOrbitPlanes=34, numSatsPerPlane=34, hight=630, Inclination=51.9, name='KPA') # Phase A
# Creat_satellite(numOrbitPlanes=32, numSatsPerPlane=32, hight=610, Inclination=42, name='KPB') # Phase B
# Creat_satellite(numOrbitPlanes=28, numSatsPerPlane=28, hight=590, Inclination=33, name='KPC') # Phase C
sat_list = stkRoot.CurrentScenario.Children.GetElements(STKObjects.eSatellite)
Add_transmitter_receiver(sat_list)
Creating_All_Access()
# 创建卫星的字典,方便根据名字对卫星进行查找
sat_list = stkRoot.CurrentScenario.Children.GetElements(STKObjects.eSatellite)
sat_dic = {}
print('Creating Satellite Dictionary')
for sat in tqdm(sat_list):
sat_dic[sat.InstanceName] = sat
Plane_num = []
for i in range(0, 22):
Plane_num.append(i)
Sat_num = []
for i in range(0, 72):
Sat_num.append(i)
Creating_All_Access()
access_list = stkRoot.CurrentScenario.Children.GetElements(STKObjects.eAccess)
for access in tqdm(access_list):
Times, Link_Name, BER, EbNo, Prop_Loss, Xmtr_Gain, EIRP, Range = Compute_access(access=access)
print(Times, Link_Name, BER, EbNo, Xmtr_Gain, EIRP, Prop_Loss, Range)