Compare commits
3 Commits
979415cdb0
...
0c95326a94
| Author | SHA1 | Date | |
|---|---|---|---|
| 0c95326a94 | |||
| 378c3c4d5c | |||
| 5dcd9a463f |
@@ -24,6 +24,7 @@ class ManeuverScheduler:
|
||||
# alarm_manager = get_connexion().space_center.alarm_manager
|
||||
alarm_manager = get_connexion().kerbal_alarm_clock
|
||||
node_offsets = 60.
|
||||
default_duration = 5 * 60.
|
||||
|
||||
@classmethod
|
||||
def get_last_alarm(cls):
|
||||
@@ -71,7 +72,7 @@ class ManeuverScheduler:
|
||||
@classmethod
|
||||
def book_timeslot(cls, ut, vessel, duration=None):
|
||||
if duration is None:
|
||||
duration = 5 * 60
|
||||
duration = cls.default_duration
|
||||
|
||||
if not cls.timeslot_is_free(ut, duration):
|
||||
raise
|
||||
@@ -94,26 +95,52 @@ class ManeuverScheduler:
|
||||
|
||||
|
||||
@classmethod
|
||||
def create_reservation(cls, ut_start, duration, maneuver):
|
||||
def book_timeslot_for_soi(cls, vessel, maneuver, duration=None):
|
||||
if duration is None:
|
||||
duration = cls.default_duration
|
||||
|
||||
soi_change = vessel.orbit.time_to_soi_change
|
||||
if math.isnan(soi_change):
|
||||
raise
|
||||
|
||||
ut_start = get_connexion().space_center.ut + soi_change
|
||||
if not cls.timeslot_is_free(ut_start, duration):
|
||||
raise
|
||||
|
||||
arg_dict = {
|
||||
"title": "{}' Maneuver: {}".format(maneuver.vessel.name, maneuver.name),
|
||||
"description": maneuver.dumps_json()
|
||||
note = {
|
||||
'duration': duration,
|
||||
'vessel_name': vessel.name
|
||||
}
|
||||
if maneuver.alarm_type == ManeuverAlarmType.ManeuverNode:
|
||||
cls.alarm_manager.add_maneuver_node_alarm(
|
||||
maneuver.vessel,
|
||||
maneuver.vessel.control.nodes[0],
|
||||
**arg_dict)
|
||||
elif maneuver.alarm_type == ManeuverAlarmType.SOI:
|
||||
cls.alarm_manager.add_soi_alarm(
|
||||
maneuver.vessel,
|
||||
**arg_dict)
|
||||
|
||||
alarm = cls.alarm_manager.create_alarm(
|
||||
cls.alarm_manager.AlarmType.soi_change,
|
||||
"{}' SOI".format(vessel.name),
|
||||
ut_start
|
||||
)
|
||||
alarm.vessel = vessel
|
||||
alarm.margin = cls.node_offsets
|
||||
alarm.notes = json.dumps(note)
|
||||
alarm.action = cls.alarm_manager.AlarmAction.kill_warp_only
|
||||
|
||||
@classmethod
|
||||
def timeslot_is_free(cls, ut_start: int, duration: int) -> bool:
|
||||
ut_end = ut_start + duration
|
||||
for a in cls.get_ordered_alarms():
|
||||
try:
|
||||
note = json.loads(a.note)
|
||||
alarm_start = a.time
|
||||
alarm_end = a.time + note['duration']
|
||||
if alarm_end < ut_start:
|
||||
continue
|
||||
elif alarm_start <= ut_start <= alarm_end:
|
||||
return False
|
||||
elif ut_start <= alarm_end <= ut_end:
|
||||
return False
|
||||
else:
|
||||
return True
|
||||
except json.JSONDecodeError:
|
||||
continue
|
||||
|
||||
return True
|
||||
|
||||
@classmethod
|
||||
@@ -121,9 +148,19 @@ class ManeuverScheduler:
|
||||
if from_ut is None:
|
||||
from_ut = get_connexion().space_center.ut
|
||||
if duration is None:
|
||||
duration = 5 * 60
|
||||
duration = cls.default_duration
|
||||
|
||||
return from_ut + duration
|
||||
if cls.timeslot_is_free(from_ut, duration):
|
||||
return from_ut
|
||||
|
||||
for a in cls.get_ordered_alarms():
|
||||
try:
|
||||
note = json.loads(a.note)
|
||||
alarm_end = a.time + int(note['duration'])
|
||||
if cls.timeslot_is_free(alarm_end, duration):
|
||||
return alarm_end
|
||||
|
||||
raise EOFError('Excepted to find a free timeslot at the end alarm list')
|
||||
|
||||
@classmethod
|
||||
def get_reservation(cls, ut_at) -> Timeslot:
|
||||
@@ -134,5 +171,3 @@ class ManeuverScheduler:
|
||||
reservation = cls.get_reservation(ut_at)
|
||||
if priority <= reservation.priority:
|
||||
raise
|
||||
|
||||
|
||||
|
||||
@@ -11,7 +11,7 @@ class Maneuver:
|
||||
self.vessel = vessel
|
||||
self.conn = conn
|
||||
|
||||
def plan_next_maneuver(self, conn):
|
||||
def plan_next_maneuver(self):
|
||||
pass
|
||||
|
||||
|
||||
|
||||
90
maneuvers/comsat.py
Normal file
90
maneuvers/comsat.py
Normal file
@@ -0,0 +1,90 @@
|
||||
import math
|
||||
|
||||
from maneuver_scheduler import ManeuverScheduler
|
||||
|
||||
from . import MechJebManeuver
|
||||
|
||||
|
||||
class ComsatManeuver(MechJebManeuver):
|
||||
def __init__(self, conn, vessel, target_body):
|
||||
super().__init__(conn, vessel)
|
||||
self.target_body = target_body
|
||||
|
||||
body = self.target_body
|
||||
if body.satellites:
|
||||
lowest_sat = min(body.satellites, key=lambda sat: sat.orbit.periapsis)
|
||||
max_orbit = lowest_sat.orbit.periapsis - lowest_sat.sphere_of_influence
|
||||
else:
|
||||
max_orbit = body.sphere_of_influence
|
||||
|
||||
self.target_altitude = max_orbit - ((5 / 100) * max_orbit)
|
||||
|
||||
def start(self):
|
||||
self.plan_next_maneuver()
|
||||
|
||||
def plan_next_maneuver(self):
|
||||
sc = self.conn.space_center
|
||||
vessel = sc.active_vessel
|
||||
|
||||
if vessel.orbit.body.name != self.target_body.name:
|
||||
raise NotImplementedError
|
||||
|
||||
if not math.isclose(vessel.orbit.apoapsis, self.target_altitude, rel_tol=.01):
|
||||
SetOrbitApoapsis(self.conn, vessel, self.target_altitude).prepare_maneuver()
|
||||
elif not math.isclose(vessel.orbit.eccentricity, 0, rel_tol=.01):
|
||||
CircularizeOrbitAndDeliver(self.conn, vessel, self.target_altitude).prepare_maneuver()
|
||||
elif self.vessel.control.current_stage == 0:
|
||||
return True
|
||||
|
||||
return False
|
||||
|
||||
|
||||
class SetOrbitApoapsis(ComsatManeuver):
|
||||
name = "Set target orbit's apoapsis"
|
||||
|
||||
def prepare_maneuver(self):
|
||||
oa = self.maneuver_planner.operation_apoapsis
|
||||
oa.new_apoapsis = self.target_altitude
|
||||
oa.time_selector.time_reference = self.mech_jeb.TimeReference.periapsis
|
||||
nodes = oa.make_nodes()
|
||||
|
||||
node = nodes[0]
|
||||
|
||||
ManeuverScheduler.book_timeslot_for_node(self.vessel, node, self)
|
||||
|
||||
|
||||
class CircularizeOrbitAndDeliver(ComsatManeuver):
|
||||
name = "Circularize orbit and deliver comsat"
|
||||
|
||||
def prepare_maneuver(self):
|
||||
oc = self.maneuver_planner.operation_circularize
|
||||
oc.time_selector.time_reference = self.mech_jeb.TimeReference.apoapsis
|
||||
nodes = oc.make_nodes()
|
||||
|
||||
node = nodes[0]
|
||||
|
||||
ManeuverScheduler.book_timeslot_for_node(self.vessel, node, self)
|
||||
|
||||
def execute(self) -> bool:
|
||||
sc = self.conn.space_center
|
||||
if sc.active_vessel.name != self.vessel.name:
|
||||
sc.active_vessel = self.vessel
|
||||
|
||||
while self.vessel.control.nodes:
|
||||
self._execute_node()
|
||||
|
||||
if self.vessel.control.current_stage > 0:
|
||||
self.vessel.control.activate_next_stage()
|
||||
|
||||
oro = self.maneuver_planner.operation_resonant_orbit
|
||||
oro.resonance_numerator = 2
|
||||
oro.resonance_denominator = 3
|
||||
oro.time_selector.lead_time = 10
|
||||
oro.time_selector.time_reference = self.mech_jeb.TimeReference.x_from_now
|
||||
oro.make_nodes()
|
||||
|
||||
while self.vessel.control.nodes:
|
||||
self._execute_node()
|
||||
return self.plan_next_maneuver()
|
||||
else:
|
||||
return True
|
||||
Reference in New Issue
Block a user