Skip to content

Commit

Permalink
refining lg2 environment
Browse files Browse the repository at this point in the history
  • Loading branch information
Ross Allen committed Dec 12, 2023
1 parent a5d3fcc commit 95dfba0
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 10 deletions.
14 changes: 14 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,20 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## [UNRELEASE]

### Added

- New Lady-Bandit-Guard scenarios with active Lady spacecraft (LG2)

### Fixed

### Changed

- Updating print statements to logger statements in pe1 e3_envs

### Removed

## [v0.4.4] - 2023-12-11

### Added
Expand Down
37 changes: 27 additions & 10 deletions src/kspdg/lbg1/lg2_envs.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,31 @@
# the bandit

import time
import numpy as np

from kspdg.lbg1.lg1_envs import LBG1_LG1_ParentEnv

class LBG1_LG2_ParentEnv(LBG1_LG1_ParentEnv):
EVASION_THROTTLE = 1.0 # constant throttle Lady applies during evasive maneuver
EVASION_BURN_DURATION = 0.5 # [s] duration of evasive maneuver
EVASION_DIST_THRESHOLD = 500.0 # [m] L-B distance at which evasion executes
EVASION_BURN_DELAY = 10 # [s]

PROGRADE_EVASION_THROTTLE = 0.2 # constant throttle Lady applies
class LBG1_LG2_ParentEnv(LBG1_LG1_ParentEnv):

def __init__(self, loadfile: str, **kwargs):
super().__init__(loadfile=loadfile, **kwargs)

def lady_guard_policy(self):
"""Lady const thrust, Guard heuristic pursuit of Bandit"""

# Do nothing until, and unless, bandit gets too close
while True:
bandit_dist = min(self.get_lb_relative_distance(), self.get_bg_relative_distance())
if bandit_dist < EVASION_DIST_THRESHOLD:
self.logger.info("\n~~~BANDIT DETECTED!\nExecuting evasive maneuvers in {} sec~~~\n".format(EVASION_BURN_DELAY))
break
else:
time.sleep(0.1)

# Set and engage lady auto-pilot reference frame so
# that it points in it's own prograde direction
Expand All @@ -27,21 +40,25 @@ def lady_guard_policy(self):
self.vesLady.auto_pilot.target_heading = 0.0
self.vesLady.auto_pilot.target_roll = 0.0
self.vesLady.auto_pilot.engage()
time.sleep(1.0)

# randomize which direction to burn
self.vesLady.auto_pilot.target_heading = np.random.choice([90.0, 270.0])

# delay to give time for evader to re-orient
time.sleep(0.5)
# delay to give time for lady to re-orient
time.sleep(EVASION_BURN_DELAY-1.0)

# turn on low-thrust maneuver
self.vesLady.control.rcs = True
self.vesLady.control.forward = LBG1_LG2_ParentEnv.PROGRADE_EVASION_THROTTLE
# execute an impulsive maneuver
self.logger.info("\n~~~Executing evasive maneuvers now!~~~\n")
self.vesLady.parts.engines[0].active = True
self.vesLady.control.throttle = EVASION_THROTTLE
time.sleep(EVASION_BURN_DURATION)
self.vesLady.control.throttle = 0.0

# run loop for heuristic for guard
super().lady_guard_policy()

# throttle down lady at end
self.vesLady.control.forward = 0.0
self.vesLady.control.right = 0.0
self.vesLady.control.up = 0.0
self.vesLady.auto_pilot.disengage()


Expand Down

0 comments on commit 95dfba0

Please sign in to comment.