Trying to reach a working mini model

This commit is contained in:
2023-08-30 23:13:34 +02:00
parent 8fdf18f6a4
commit 2665e657ed
8 changed files with 189 additions and 77 deletions

149
main.py
View File

@@ -7,9 +7,16 @@ from maneuver_scheduler import ManeuverScheduler
from maneuvers.rendezvous import RendezvousManeuver
from maneuvers.approach import ApproachManeuver
from maneuvers.docking import DockingManeuver
from maneuvers.comsat import ComsatManeuver
from connector import get_connexion
from lib import get_contract, get_vessel, get_rescuee_vessel
from lib import get_contract, get_vessel, get_rescuee_vessel, get_body
class MissionType(Enum):
transport = 1
rescue = 2
comsat = 3
class MissionStatus(Enum):
@@ -33,12 +40,16 @@ class Mission:
class TransportMission(Mission):
type = MissionType.transport
def __init__(self, kerbal_name, destination):
self.kerbal_name = kerbal_name
self.destination = destination
class RescueMission(Mission):
type = MissionType.rescue
def __init__(self, contract_title):
self.contract_title = contract_title
self.contract = get_contract(contract_title)
@@ -47,59 +58,89 @@ class RescueMission(Mission):
self.rescuee_vessel_name = self.rescuee_vessel.name
self.rescuee_name = self.rescuee_vessel.crew[0].name
def execute(self, vessel):
def execute(self, mission_control):
conn = get_connexion()
sc = conn.space_center
if sc.active_vessel.name != vessel.name:
sc.active_vessel = vessel
if sc.active_vessel.name != mission_control.vessel.name:
sc.active_vessel = mission_control.vessel
if sc.target_vessel is None or sc.target_vessel.name != self.rescuee_vessel.name:
sc.target_vessel = self.rescuee_vessel
rendezvous_done = RendezvousManeuver(conn, vessel).execute()
rendezvous_done = RendezvousManeuver(conn, mission_control).execute()
if rendezvous_done:
reference_frame = sc.ReferenceFrame.create_relative(
self.rescuee_vessel.reference_frame,
rotation=(1., 0., 0., 0.)
)
approach_done = ApproachManeuver(conn, vessel, reference_frame).start()
sc.active_vessel.parts.controlling = mission_control.get_grappling()
approach_done = ApproachManeuver(conn, mission_control, reference_frame).start()
if approach_done:
docking_part = vessel.parts.root.children[0].children[10].children[0].children[0].children[0].children[
0]
docking_done = DockingManeuver(conn, vessel, docking_part, reference_frame).start()
mission_control.toggle_grappling(True)
docking_done = DockingManeuver(conn,
mission_control,
mission_control.get_grappling(),
reference_frame).start()
# Find new creat member name
# sc.transfer_crew(self.rescuee_name, target_part)
# Release Grapple
# Destroy Capsule
MissionReport.mission_status = MissionStatus.Done
MissionReport.new_missions.append(
report = MissionReport()
report.mission_status = MissionStatus.Done
report.new_missions.append(
TransportMission(self.rescuee_name, 'kerbin_orbit')
)
return report
class ShuttleKerbin:
def __init__(self, vessel_name):
self.vessel_name = vessel_name
self.vessel = get_vessel(vessel_name)
class ComSatNetworkMission:
type = MissionType.comsat
self.current_orbit_missions = []
def __init__(self, body_name):
self.body = get_body(body_name)
def execute(self, mission_control):
conn = get_connexion()
sc = conn.space_center
if sc.active_vessel.name != mission_control.vessel.name:
sc.active_vessel = mission_control.vessel
mission_control.toggle_engines(True)
comsat_done = ComsatManeuver(conn, mission_control, self.body).execute()
if comsat_done:
report = MissionReport()
report.mission_status = MissionStatus.Done
return report
class MissionControl:
mission_types = []
def __init__(self):
self.current_mission = None
def pick_missions(self, backlog):
current_body_name = self.vessel.orbit.body.name
for i, mission in enumerate(backlog.missions[current_body_name]):
if mission.type in self.mission_types:
if self.current_mission is None:
self.plan_mission(mission)
else:
self.current_orbit_missions.append(mission)
del (backlog.missions[current_body_name][i])
def plan_mission(self, mission):
self.current_mission = mission
planning_duration = (5*60)
ut = ManeuverScheduler.next_free_timeslot(duration=planning_duration)
ManeuverScheduler.book_timeslot(ut, self.vessel)
def pick_missions(self, backlog):
for i, mission in enumerate(backlog.kerbin_orbit_rescue):
if self.current_mission is None:
self.plan_mission(mission)
else:
self.current_orbit_missions.append(mission)
del(backlog.kerbin_orbit_rescue[i])
planning_duration = 60
ut = ManeuverScheduler.next_free_timeslot(
from_ut=get_connexion().space_center.ut + planning_duration,
duration=planning_duration)
ManeuverScheduler.book_timeslot(ut, self.vessel, duration=planning_duration)
def execute_mission(self, alarm):
if self.current_mission is None:
@@ -107,11 +148,54 @@ class ShuttleKerbin:
self.current_mission = self.current_orbit_missions.pop(0)
else:
return
self.current_mission.execute(self.vessel)
report = self.current_mission.execute(self)
if report and report.mission_status == MissionStatus.Done:
self.current_mission = None
class ShuttleKerbin(MissionControl):
mission_types = [MissionType.rescue, MissionType.transport]
PISTON_MAX_EXTENSION = 2.4
def __init__(self, vessel_name):
super().__init__()
self.vessel_name = vessel_name
self.vessel = get_vessel(vessel_name)
for e in self.vessel.parts.engines:
e.active = True
self.current_orbit_missions = []
def toggle_grappling(self, value=True):
piston = self.vessel.parts.robotic_pistons[0]
piston.target_extension = self.PISTON_MAX_EXTENSION if value else 0
arm = piston.part.children[0]
if arm.modules[1].has_event('Arm') and not value \
or arm.modules[1].has_event('Disarm') and value:
arm.modules[1].set_action_by_id('ToggleAction', True)
def get_grappling(self):
piston = self.vessel.parts.robotic_pistons[0]
return piston.part.children[0]
class ComsatKerbin(MissionControl):
mission_types = [MissionType.comsat]
def __init__(self, vessel_name):
super().__init__()
self.vessel_name = vessel_name
self.vessel = get_vessel(vessel_name)
def toggle_engines(self, value=True):
self.vessel.parts.engines[0].active = value
class Backlog:
kerbin_orbit_rescue = []
missions = {
'Kerbin': [RescueMission('Rescue Rossby from orbit of Kerbin.'), ] # ComSatNetworkMission('Kerbin')]
}
kerbin_to_orbit = []
kerbin_to_ground = []
@@ -125,10 +209,8 @@ if __name__ == '__main__':
signal.signal(signal.SIGINT, signal_handler)
ships = []
m = RescueMission('Rescue Rossby from orbit of Kerbin.')
Backlog.kerbin_orbit_rescue.append(m)
ships.append(ShuttleKerbin('KKS Gagarin'))
ships.append(ComsatKerbin('KKR Shepard'))
conn = get_connexion()
# conn.space_center.GameMode
@@ -161,6 +243,3 @@ if __name__ == '__main__':
current_ship.execute_mission(alarm)
alarm.remove()
print("turn")