Skip to content

Commit

Permalink
ACU sun: add tests, cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
mhasself committed Nov 1, 2023
1 parent 11e3f58 commit 28b8bdc
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 33 deletions.
33 changes: 0 additions & 33 deletions socs/agents/acu/avoidance.py
Original file line number Diff line number Diff line change
Expand Up @@ -635,36 +635,3 @@ def get_traj(self, res=0.5):
xx.append(np.linspace(x0, x1, n))
yy.append(np.linspace(y0, y1, n))
return np.hstack(tuple(xx)), np.hstack(tuple(yy))


class RollingMinimum:
def __init__(self, window, fallback=None):
self.window = window
self.subwindow = window / 10
self.fallback = fallback
self.records = []

def append(self, val, t=None):
if t is None:
t = time.time()
# Remove old data
while len(self.records) and (t - self.records[0][0]) > self.window:
self.records.pop(0)
# Add this to existing subwindow?
if len(self.records):
# Consider values up to subwindow ago.
_t, _val = self.records[-1]
if t - _t < self.subwindow:
if val <= _val:
self.records[-1] = (t, val)
return
# Or start a new subwindow.
self.records.append((t, val))

def get(self, lookback=None):
if lookback is None:
lookback = self.window
recs = [v for t, v in self.records if (time.time() - t < lookback)]
if len(recs):
return min(recs)
return self.fallback
36 changes: 36 additions & 0 deletions tests/agents/test_acu_agent.py
Original file line number Diff line number Diff line change
@@ -1 +1,37 @@
from socs.agents.acu import avoidance as av
from socs.agents.acu.agent import ACUAgent # noqa: F401


def test_avoidance():
az0, el0 = 72, 67
t0 = 1698850000

sun = av.SunTracker(fake_now=t0)
pos = sun.get_sun_pos()
az, el = pos['sun_azel']
assert abs(az - az0) < .5 and abs(el - el0) < .5

# Zenith should be about 23 deg away.
assert abs(sun.get_sun_pos(0, 90)['sun_dist'] - 23) < 0.5

# Unsafe positions.
assert sun.check_trajectory([90], [60])['sun_time'] == 0

# Safe positions
assert sun.check_trajectory([90], [20])['sun_time'] > 0
assert sun.check_trajectory([270], [60])['sun_time'] > 0

# No safe moves to Sun position.
paths = sun.analyze_paths(180, 20, az0, el0)
path, analysis = sun.select_move(paths)
assert path is None

# Escape paths.
path = sun.find_escape_paths(az0, el0 - 5)
assert path is not None
path = sun.find_escape_paths(az0, el0 + 5)
assert path is not None
path = sun.find_escape_paths(az0 - 10, el0)
assert path is not None
path = sun.find_escape_paths(az0 + 10, el0)
assert path is not None

0 comments on commit 28b8bdc

Please sign in to comment.