Skip to content

Commit

Permalink
Issue #60: Cache eclipse times
Browse files Browse the repository at this point in the history
  • Loading branch information
Mark2000 committed Sep 14, 2023
1 parent 65755e8 commit 5db6937
Showing 1 changed file with 28 additions and 18 deletions.
46 changes: 28 additions & 18 deletions bsk_rl/envs/general_satellite_tasking/utils/orbital.py
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,10 @@ def __init__(
)
)

self._eclipse_starts: list[float] = []
self._eclipse_ends: list[float] = []
self._eclipse_search_time = 0.0

self.init_simulator()

def init_simulator(self) -> None:
Expand Down Expand Up @@ -233,6 +237,19 @@ def extend_to(self, t: float) -> None:
self.ConfigureStopTime(macros.sec2nano(t))
self.ExecuteSimulation()

def generate_eclipses(self, t: float) -> None:
self.extend_to(t + self.dt)
upcoming_times = self.times[self.times > self._eclipse_search_time]
upcoming_eclipse = (
self.eclipse_log.shadowFactor[self.times > self._eclipse_search_time] > 0
).astype(float)
for i in np.where(np.diff(upcoming_eclipse) == -1)[0]:
self._eclipse_starts.append(upcoming_times[i])
for i in np.where(np.diff(upcoming_eclipse) == 1)[0]:
self._eclipse_ends.append(upcoming_times[i])

self._eclipse_search_time = t

def next_eclipse(self, t: float, max_tries: int = 100) -> tuple[float, float]:
"""Find the soonest eclipse transitions. The returned values are not necessarily
from the same eclipse event, such as when the search start time is in eclipse.
Expand All @@ -244,26 +261,19 @@ def next_eclipse(self, t: float, max_tries: int = 100) -> tuple[float, float]:
eclipse_start: Nearest upcoming eclipse beginning
eclipse_end: Nearest upcoming eclipse end
"""
self.extend_to(t + self.dt)
for _ in range(max_tries):
upcoming_times = self.times[self.times > t]
upcoming_eclipse = self.eclipse_log.shadowFactor[self.times > t] > 0
if sum(np.diff(upcoming_eclipse)) >= 2:
break
self.extend_to(self.sim_time + self.dt * 10)

current_state = upcoming_eclipse[0]
transition_times = upcoming_times[np.where(np.diff(upcoming_eclipse))[0][0:2]]

if len(transition_times) != 2:
return 1.0, 1.0
for i in range(max_tries):
if any([t_start > t for t_start in self._eclipse_starts]) and any(
[t_end > t for t_end in self._eclipse_ends]
):
eclipse_start = min(
[t_start for t_start in self._eclipse_starts if t_start > t]
)
eclipse_end = min([t_end for t_end in self._eclipse_ends if t_end > t])
return eclipse_start, eclipse_end

if current_state:
eclipse_start, eclipse_end = transition_times
else:
eclipse_end, eclipse_start = transition_times
self.generate_eclipses(t + i * self.dt * 10)

return eclipse_start, eclipse_end
return 1.0, 1.0

@property
def r_BN_N(self) -> interp1d:
Expand Down

0 comments on commit 5db6937

Please sign in to comment.