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 | class SatSimulation:
'''
Runs real time to update satellite positions
'''
# Time slice for simulation
TIME_SLICE = 10
MIN_ALTITUDE = 35
def __init__(self, graph: networkx.Graph):
self.graph = graph
self.ts = load.timescale()
self.satellites: list[Satellite] = []
self.ground_stations: list[GroundStation] = []
self.client: simclient.Client = simclient.Client("http://127.0.0.0:8000")
self.calc_only = False
self.min_altitude = SatSimulation.MIN_ALTITUDE
self.zero_uplink_count = 0
self.uplink_updates = 0
for name in torus_topo.ground_stations(graph):
node = graph.nodes[name]
position = wgs84.latlon(node[torus_topo.LAT], node[torus_topo.LON])
ground_station = GroundStation(name, position)
self.ground_stations.append(ground_station)
for name in torus_topo.satellites(graph):
orbit = graph.nodes[name]["orbit"]
ts = load.timescale()
l1, l2 = orbit.tle_format()
earth_satellite = EarthSatellite(l1, l2, name, ts)
satellite = Satellite(name, earth_satellite)
self.satellites.append(satellite)
def updatePositions(self, future_time: datetime.datetime):
sfield_time = self.ts.from_datetime(future_time)
positions = []
ground_positions = []
# Update satellite positions
for satellite in self.satellites:
satellite.geo = satellite.earth_sat.at(sfield_time)
lat, lon = wgs84.latlon_of(satellite.geo)
satellite.lat = lat
satellite.lon = lon
satellite.height = wgs84.height_of(satellite.geo)
# Create position update
position = simapi.SatellitePosition(
name=satellite.name,
lat=float(satellite.lat.degrees),
lon=float(satellite.lon.degrees),
height=float(satellite.height.km)
)
positions.append(position)
# Add ground station positions
for station in self.ground_stations:
ground_pos = simapi.GroundStationPosition(
name=station.name,
lat=float(station.position.latitude.degrees),
lon=float(station.position.longitude.degrees)
)
ground_positions.append(ground_pos)
# Collect satellite-to-satellite links
satellite_links = []
for node1, node2 in self.graph.edges():
if node1.startswith('R') and node2.startswith('R'): # Satellite nodes start with R
status = self.graph.edges[node1, node2].get("up", True)
satellite_links.append(simapi.Link(
node1_name=node1,
node2_name=node2,
up=status
))
# Collect ground station uplinks
ground_uplinks = []
for station in self.ground_stations:
uplinks_list = []
for uplink in station.uplinks:
uplinks_list.append(simapi.UpLink(
sat_node=uplink.satellite_name,
distance=int(uplink.distance)
))
if uplinks_list:
ground_uplinks.append(simapi.UpLinks(
ground_node=station.name,
uplinks=uplinks_list
))
# Send position updates to API
data = simapi.SatellitePositions(
satellites=positions,
ground_stations=ground_positions,
satellite_links=satellite_links,
ground_uplinks=ground_uplinks
)
self.client.update_positions(data)
#print(f"{satellite.name} Lat: {satellite.lat}, Lon: {satellite.lon}, Hieght: {satellite.height.km}km")
print(f"{station.name} Lat: {station.position.latitude.degrees}, Lon: {station.position.longitude.degrees}")
@staticmethod
def nearby(ground_station: GroundStation, satellite: Satellite) -> bool:
return (satellite.lon.degrees > ground_station.position.longitude.degrees - 20 and
satellite.lon.degrees < ground_station.position.longitude.degrees + 20 and
satellite.lat.degrees > ground_station.position.latitude.degrees - 20 and
satellite.lat.degrees < ground_station.position.latitude.degrees + 20)
def updateUplinkStatus(self, future_time: datetime.datetime):
'''
Update the links between ground stations and satellites
'''
self.uplink_updates += 1
zero_uplinks: bool = False
sfield_time = self.ts.from_datetime(future_time)
for ground_station in self.ground_stations:
ground_station.uplinks = []
for satellite in self.satellites:
# Calculate az for close satellites
if SatSimulation.nearby(ground_station, satellite):
difference = satellite.earth_sat - ground_station.position
topocentric = difference.at(sfield_time)
alt, az, d = topocentric.altaz()
if alt.degrees > self.min_altitude:
uplink = Uplink(satellite.name, ground_station.name, d.km)
ground_station.uplinks.append(uplink)
print(f"{satellite.name} Lat: {satellite.lat}, Lon: {satellite.lon}")
print(f"{ground_station.name} Lat: {ground_station.position.latitude}, Lon: {ground_station.position.longitude}")
print(f"ground {ground_station.name}, sat {satellite.name}: {alt}, {az}, {d.km}")
if len(ground_station.uplinks) == 0:
zero_uplinks = True
if zero_uplinks:
self.zero_uplink_count += 1
def updateInterPlaneStatus(self):
inclination = self.graph.graph["inclination"]
for satellite in self.satellites:
# Track if state changed
satellite.prev_inter_plane_status = satellite.inter_plane_status
if satellite.lat.degrees > (inclination - 2) or satellite.lat.degrees < (
-inclination + 2
):
# Above the threashold for inter plane links to connect
satellite.inter_plane_status = False
else:
satellite.inter_plane_status = True
def send_updates(self):
for satellite in self.satellites:
if satellite.prev_inter_plane_status != satellite.inter_plane_status:
for neighbor in self.graph.adj[satellite.name]:
if self.graph.edges[satellite.name, neighbor]["inter_ring"]:
self.client.set_link_state(satellite.name, neighbor, satellite.inter_plane_status)
for ground_station in self.ground_stations:
links = []
for uplink in ground_station.uplinks:
links.append((uplink.satellite_name, int(uplink.distance)))
self.client.set_uplinks(ground_station.name, links)
def run(self):
current_time = datetime.datetime.now(tz=datetime.timezone.utc)
slice_delta = datetime.timedelta(seconds=SatSimulation.TIME_SLICE)
# Generate positions for current time
print(f"update positions for {current_time}")
self.updatePositions(current_time)
self.updateUplinkStatus(current_time)
self.updateInterPlaneStatus()
self.send_updates()
while True:
# Generate positions for next time step
future_time = current_time + slice_delta
print(f"update positions for {future_time}")
self.updatePositions(future_time)
self.updateUplinkStatus(future_time)
self.updateInterPlaneStatus()
sleep_delta = future_time - datetime.datetime.now(tz=datetime.timezone.utc)
print(f"zero uplink % = {self.zero_uplink_count / self.uplink_updates}")
print("sleep")
if not self.calc_only:
# Wait until next time step thenupdate
time.sleep(sleep_delta.seconds)
self.send_updates()
current_time = future_time
|