Add aircraft automation code, including flap control and stable autopilot flight.

This commit is contained in:
2021-08-24 06:10:21 -04:00
parent a99912e3ae
commit 376629354b
9 changed files with 204 additions and 19 deletions

View File

@ -78,7 +78,9 @@ function addLibs {
if line:Contains("RunOncePath") {
local start is line:Find(char(34)).
local end is line:FindLast(char(34)).
libs:Add(line:Substring(start + 1, end - start - 1)).
local libFile is line:Substring(start + 1, end - start - 1).
libs:Add(libFile).
addLibs(libs, libFile).
}
}
}

32
lib/flap_control.ks Normal file
View File

@ -0,0 +1,32 @@
function SetFlapAngle {
parameter angle.
local flaps is getFlaps().
if flaps:LENGTH = 0 { return. }
for flap in flaps {
flap:GetModule("ModuleControlSurface"):SetField("deploy angle", angle).
print("DEBUG: Set flap angle to " + angle).
}
}
function SetFlaps {
parameter extend is true.
local flaps is getFlaps().
if flaps:LENGTH = 0 { return. }
for flap in flaps {
flap:GetModule("ModuleControlSurface"):SetField("deploy", extend).
print("DEBUG: Set flap deployment to " + extend).
}
}
function getFlaps {
local flaps is SHIP:PartsTagged("flap").
for flap in flaps {
if flap:MODULES:Find("ModuleControlSurface") = -1 {
print "WARNING: Flap is not a control surface. Aborting operation.".
return List().
}
}
return flaps.
}

View File

@ -1,5 +1,6 @@
RunOncePath("lib/throttle").
RunOncePath("lib/navigation").
RunOncePath("lib/navball").
// Calculate the direction to lock during ascent.
function getClampedDir {
@ -8,18 +9,12 @@ function getClampedDir {
// face just beneath prograde, but hold a solid eastern heading and don't
// rotate the ship
local newHeading is lookdirup(SHIP:SRFPROGRADE:FOREVECTOR, heading(90, 0, 270):TOPVECTOR).
if getPitch(newHeading:FOREVECTOR) < minPitch {
if GetPitch(newHeading:FOREVECTOR) < minPitch {
set newHeading to heading(90, minPitch, 270).
}
return newHeading.
}
// Returns the navball pitch of a given vector.
function getPitch {
parameter v is SHIP:FACING:FOREVECTOR.
return 90 - vectorangle(SHIP:UP:FOREVECTOR, v).
}
// Given a target (end) pitch, a target duration, and a start time,
// returns the correct current heading.
function pitchProgram {

9
lib/navball.ks Normal file
View File

@ -0,0 +1,9 @@
// Returns the navball pitch of a given vector.
function GetPitch {
parameter v is SHIP:FACING:FOREVECTOR.
return 90 - vectorangle(SHIP:UP:FOREVECTOR, v).
}
function GetHeading {
return mod(360 - LatLng(90,0):BEARING, 360).
}

34
lib/stabilize_aircraft.ks Normal file
View File

@ -0,0 +1,34 @@
RunOncePath("lib/navball").
// Fly level toward the current compass heading.
function HoldHorizon {
parameter Kp is 0.02.
parameter Ki is 0.04.
parameter Kd is 0.0066.
SAS off.
local pitchPID is PidLoop(Kp, Ki, Kd).
// local rollPID is PidLoop(Kp, Ki, Kd).
// lock rollVec to VXCL(SHIP:FACING:FOREVECTOR, SHIP:FACING:TOPVECTOR).
// lock upVec to VXCL(SHIP:FACING:FOREVECTOR, SHIP:UP:FOREVECTOR).
local h is GetHeading().
local p is 0.
lock p to pitchPID:Update(TIME:SECONDS, SHIP:VERTICALSPEED).
lock STEERING to Heading(h, p, 0).
// until done {
// set SHIP:CONTROL:PITCH to pitchPID:Update(TIME:SECONDS, GetPitch(SHIP:SRFPROGRADE:FOREVECTOR)).
// set SHIP:CONTROL:ROLL to rollPID:Update(TIME:SECONDS, VAng(rollVec, upVec)).
// }
wait until done.
set done to false.
unlock STEERING.
SAS on.
// set SHIP:CONTROL:PITCH to 0.
// set SHIP:CONTROL:ROLL to 0.
}

View File

@ -53,3 +53,25 @@ function MakeMenu {
set top:AddButton(execLabel):onClick to callback:Bind(optionList).
}
}
function AddStockButtons {
parameter row.
parameter bootname.
local btn is row:AddButton("Term").
set btn:TOGGLE to true.
set btn:ONTOGGLE to {
parameter d.
if d {
CORE:PART:GETMODULE("kOSProcessor"):DOEVENT("Open Terminal").
} else {
CORE:PART:GETMODULE("kOSProcessor"):DOEVENT("Close Terminal").
}
}.
MakeButton(row, "Update", {
switch to 0.
run reinstall(bootname).
}).
}