Moving the mission and mission control in their own file. Adapting the rescue mission for the new shuttle
This commit is contained in:
@@ -3,7 +3,7 @@ from krpc.services.spacecenter import SASMode
|
||||
import numpy as np
|
||||
from time import time, sleep
|
||||
|
||||
from .utils import magnitude, unitary, kill_relative_velocity, correct_course
|
||||
from .utils import magnitude, unitary, kill_relative_velocity, correct_course, get_safety_radius, point_toward_direction
|
||||
|
||||
from . import Maneuver
|
||||
|
||||
@@ -52,26 +52,6 @@ class ApproachManeuver(Maneuver):
|
||||
return True
|
||||
|
||||
|
||||
def get_safety_radius(vessel):
|
||||
bbox = vessel.bounding_box(vessel.reference_frame)
|
||||
return max(magnitude(bbox[0]), magnitude(bbox[1]))
|
||||
|
||||
|
||||
def point_toward_direction(vessel, direction, reference_frame):
|
||||
ap = vessel.auto_pilot
|
||||
ap.reference_frame = reference_frame
|
||||
ap.target_direction = unitary(direction)
|
||||
ap.target_roll = 0
|
||||
ap.sas = False
|
||||
ap.engage()
|
||||
sleep(1)
|
||||
ap.wait()
|
||||
|
||||
ap.disengage()
|
||||
ap.sas_mode = SASMode.stability_assist
|
||||
ap.sas = True
|
||||
|
||||
|
||||
THROTTLE = .1
|
||||
VELOCITY_TOLERANCE = .1
|
||||
|
||||
|
||||
96
maneuvers/approach_rcs.py
Normal file
96
maneuvers/approach_rcs.py
Normal file
@@ -0,0 +1,96 @@
|
||||
from krpc.services.spacecenter import SASMode
|
||||
|
||||
import numpy as np
|
||||
from time import time, sleep
|
||||
|
||||
from .utils import magnitude, unitary, kill_relative_velocity, kill_rcs_velocity, correct_course, get_safety_radius,\
|
||||
point_toward_direction
|
||||
|
||||
from . import Maneuver
|
||||
|
||||
|
||||
class ApproachRCSManeuver(Maneuver):
|
||||
|
||||
SAFETY_RADIUS_MARGIN = 10
|
||||
|
||||
def __init__(self, conn, mission_control, reference_frame):
|
||||
super().__init__(conn, mission_control)
|
||||
self.reference_frame = reference_frame
|
||||
|
||||
def start(self):
|
||||
sc = self.conn.space_center
|
||||
vessel = sc.active_vessel
|
||||
target = sc.target_vessel
|
||||
|
||||
kill_rcs_velocity(vessel, self.reference_frame)
|
||||
|
||||
self.conn.drawing.add_direction((0, 1, 0), self.reference_frame)
|
||||
|
||||
vessel.control.rcs = False
|
||||
|
||||
pv = vessel.position(self.reference_frame)
|
||||
|
||||
safety_radius = get_safety_radius(vessel) + get_safety_radius(target) + self.SAFETY_RADIUS_MARGIN
|
||||
|
||||
# if under and inside safety cylinder's circle
|
||||
if pv[1] < safety_radius and pow(pv[0], 2) + pow(pv[2], 2) <= pow(safety_radius, 2):
|
||||
print("We're under the target and inside the safety cylinder, getting out")
|
||||
# get out of the cylinder
|
||||
planar_move_vector = unitary((pv[0], pv[2])) * (safety_radius - magnitude((pv[0], pv[2])))
|
||||
spacial_move_vector = np.array((planar_move_vector[0], 0, planar_move_vector[1]))
|
||||
|
||||
pv = vessel.position(self.reference_frame)
|
||||
move_to_waypoint(self.conn, vessel, pv + spacial_move_vector, self.reference_frame)
|
||||
|
||||
print("We're outside of the safety cylinder, setting vertical distance")
|
||||
pv = vessel.position(self.reference_frame)
|
||||
move_to_waypoint(self.conn, vessel, (pv[0], safety_radius, pv[2]), self.reference_frame)
|
||||
|
||||
# should be above and outside => get inside
|
||||
print("We're at the right vertical distance to the target, setting horizontal position")
|
||||
move_to_waypoint(self.conn, vessel, (0, safety_radius, 0), self.reference_frame)
|
||||
|
||||
# point_toward_direction(vessel, - np.array(vessel.position(self.reference_frame)), self.reference_frame)
|
||||
point_toward_direction(vessel, (0, -1, 0), self.reference_frame)
|
||||
|
||||
return True
|
||||
|
||||
|
||||
def move_to_waypoint(conn, vessel, waypoint, reference_frame):
|
||||
mj = conn.mech_jeb
|
||||
|
||||
kill_relative_velocity(conn, vessel, reference_frame)
|
||||
|
||||
conn.drawing.add_line(vessel.position(reference_frame), waypoint, reference_frame)
|
||||
waypoint = np.array(waypoint)
|
||||
|
||||
start_position = np.array(vessel.position(reference_frame))
|
||||
vector = waypoint - start_position
|
||||
distance = magnitude(vector)
|
||||
direction = unitary(vector)
|
||||
acceleration_distance = distance / 4
|
||||
|
||||
point_toward_direction(vessel, direction, reference_frame)
|
||||
|
||||
print("Starting acceleration")
|
||||
remaining_distance = distance
|
||||
vessel.control.rcs = True
|
||||
vessel.control.forward = 1
|
||||
while remaining_distance > 3 * acceleration_distance:
|
||||
sleep(.1)
|
||||
remaining_distance = magnitude(waypoint - vessel.position(reference_frame))
|
||||
vessel.control.forward = 0
|
||||
print("Target velocity achieved")
|
||||
|
||||
while remaining_distance > acceleration_distance:
|
||||
sleep(.1)
|
||||
correct_course(conn, vessel, waypoint, reference_frame)
|
||||
remaining_distance = magnitude(waypoint - vessel.position(reference_frame))
|
||||
print(remaining_distance)
|
||||
|
||||
print("Starting deceleration")
|
||||
kill_rcs_velocity(vessel, reference_frame)
|
||||
print("Ship decelerated")
|
||||
|
||||
print("destination position: {}".format(waypoint))
|
||||
print("end position: {}".format(np.array(vessel.position(reference_frame))))
|
||||
@@ -1,6 +1,6 @@
|
||||
from time import sleep
|
||||
|
||||
from .utils import kill_relative_velocity, correct_course, magnitude
|
||||
from .utils import kill_relative_velocity, kill_rcs_velocity, correct_course, magnitude
|
||||
|
||||
from . import Maneuver
|
||||
|
||||
@@ -83,43 +83,6 @@ def rcs_push(vessel, axis, duration):
|
||||
vessel.control.rcs = False
|
||||
|
||||
|
||||
def kill_rcs_velocity(vessel, reference_frame):
|
||||
print("Killing RCS velocity")
|
||||
velo = vessel.velocity(reference_frame)
|
||||
vessel.control.rcs = True
|
||||
while any(abs(component) > .05 for component in velo) > .05:
|
||||
if abs(velo[0]) > .05:
|
||||
sign = -velo[0] / abs(velo[0])
|
||||
if abs(velo[0]) > .1:
|
||||
vessel.control.up = 1 * sign
|
||||
elif abs(velo[0]) > .05:
|
||||
vessel.control.up = .1 * sign
|
||||
else:
|
||||
vessel.control.up = 0
|
||||
|
||||
if abs(velo[1]) > .05:
|
||||
sign = -velo[1] / abs(velo[1])
|
||||
if abs(velo[1]) > .1:
|
||||
vessel.control.forward = 1 * sign
|
||||
elif abs(velo[1]) > .05:
|
||||
vessel.control.forward = .1 * sign
|
||||
else:
|
||||
vessel.control.forward = 0
|
||||
|
||||
if abs(velo[2]) > .05:
|
||||
sign = velo[2] / abs(velo[2])
|
||||
if abs(velo[2]) > .1:
|
||||
vessel.control.right = 1 * sign
|
||||
elif abs(velo[2]) > .05:
|
||||
vessel.control.right = .1 * sign
|
||||
else:
|
||||
vessel.control.right = 0
|
||||
sleep(.1)
|
||||
velo = vessel.velocity(reference_frame)
|
||||
vessel.control.rcs = False
|
||||
print("RCS velocity killed")
|
||||
|
||||
|
||||
def align_horizontally(conn, vessel, reference_frame):
|
||||
conn.drawing.add_direction((1, 0, 0), vessel.reference_frame)
|
||||
|
||||
|
||||
@@ -63,3 +63,60 @@ def correct_course(conn, vessel, waypoint, reference_frame):
|
||||
vessel.control.up = -.1
|
||||
else:
|
||||
vessel.control.up = 0
|
||||
|
||||
|
||||
def kill_rcs_velocity(vessel, reference_frame):
|
||||
print("Killing RCS velocity")
|
||||
velo = vessel.velocity(reference_frame)
|
||||
vessel.control.rcs = True
|
||||
while any(abs(component) > .05 for component in velo) > .05:
|
||||
if abs(velo[0]) > .05:
|
||||
sign = -velo[0] / abs(velo[0])
|
||||
if abs(velo[0]) > .1:
|
||||
vessel.control.up = 1 * sign
|
||||
elif abs(velo[0]) > .05:
|
||||
vessel.control.up = .1 * sign
|
||||
else:
|
||||
vessel.control.up = 0
|
||||
|
||||
if abs(velo[1]) > .05:
|
||||
sign = -velo[1] / abs(velo[1])
|
||||
if abs(velo[1]) > .1:
|
||||
vessel.control.forward = 1 * sign
|
||||
elif abs(velo[1]) > .05:
|
||||
vessel.control.forward = .1 * sign
|
||||
else:
|
||||
vessel.control.forward = 0
|
||||
|
||||
if abs(velo[2]) > .05:
|
||||
sign = velo[2] / abs(velo[2])
|
||||
if abs(velo[2]) > .1:
|
||||
vessel.control.right = 1 * sign
|
||||
elif abs(velo[2]) > .05:
|
||||
vessel.control.right = .1 * sign
|
||||
else:
|
||||
vessel.control.right = 0
|
||||
sleep(.1)
|
||||
velo = vessel.velocity(reference_frame)
|
||||
vessel.control.rcs = False
|
||||
print("RCS velocity killed")
|
||||
|
||||
|
||||
def get_safety_radius(vessel):
|
||||
bbox = vessel.bounding_box(vessel.reference_frame)
|
||||
return max(magnitude(bbox[0]), magnitude(bbox[1]))
|
||||
|
||||
|
||||
def point_toward_direction(vessel, direction, reference_frame):
|
||||
ap = vessel.auto_pilot
|
||||
ap.reference_frame = reference_frame
|
||||
ap.target_direction = unitary(direction)
|
||||
ap.target_roll = 0
|
||||
ap.sas = False
|
||||
ap.engage()
|
||||
sleep(1)
|
||||
ap.wait()
|
||||
|
||||
ap.disengage()
|
||||
ap.sas_mode = SASMode.stability_assist
|
||||
ap.sas = True
|
||||
Reference in New Issue
Block a user