Move RCS logic to separate file.

This commit is contained in:
Casper V. Kristensen 2019-03-01 23:07:36 +01:00
parent 706daf5228
commit 780aee5e7c
Signed by: caspervk
GPG key ID: 289CA03790535054
3 changed files with 84 additions and 111 deletions

80
dock.ks
View file

@ -1,51 +1,10 @@
@LAZYGLOBAL off. @LAZYGLOBAL off.
run once "lib/rendezvous". run once "lib/rcs".
run once "lib/rocket". run once "lib/target".
run once "lib/vectors". run once "lib/vectors".
function translate {
//
// Translate the ship's position by the given vector using RCS thrusters.
//
parameter translation_vector_delegate.
parameter max_speed.
local lock translation_vector to translation_vector_delegate().
// Find available RCS acceleration by Newton's second law: (F=mg). Note that the acceleration is conservatively divided
// by 6 since thrusters might not be aligned with thrust vector (but assumed evenly distributed on the 6 axes).
// Furthermore, acceleration is limited to 0.05m/s^2 since small ships with low mass are uncontrollable if allowed to accelerate wildly.
local acceleration is min(0.05, (rcs_maxthrust() / SHIP:MASS) / 6).
// Time t to travel distance d under constant acceleration a is t = sqrt(2d/a) (https://en.wikipedia.org/wiki/Equations_for_a_falling_body).
// Multiply this by the available acceleration to get speed (s * m/s^2 = m/s) required to decelerate to 0 by the time d=0.
local lock desired_speed to min(max_speed, sqrt(2*translation_vector:mag / acceleration) * acceleration).
local lock desired_velocity to translation_vector:normalized * desired_speed.
// The proportional gain factor is set to the square root of the ship's mass. Why? It was empirically shown to work.
// The idea is that small ships are more sensitive to adjustments, and so small error corrections will do more harm than good.
local pid_x is PIDLOOP(sqrt(SHIP:MASS), 0.01, 0.001, -1, 1). // kp, ki, kd, minoutput, maxoutput
local pid_y is PIDLOOP(sqrt(SHIP:MASS), 0.01, 0.001, -1, 1).
local pid_z is PIDLOOP(sqrt(SHIP:MASS), 0.01, 0.001, -1, 1).
//vdraw(SHIP:controlpart:position, translation_vector@, WHITE).
//vdraw(SHIP:controlpart:position, relative_velocity@, GREEN).
RCS on.
until translation_vector:mag < 0.05 and relative_velocity():mag < 0.01 {
set pid_x:setpoint to desired_velocity:x.
set pid_y:setpoint to desired_velocity:y.
set pid_z:setpoint to desired_velocity:z.
local x is pid_x:update(TIME:seconds, relative_velocity():x).
local y is pid_y:update(TIME:seconds, relative_velocity():y).
local z is pid_z:update(TIME:seconds, relative_velocity():z).
set SHIP:CONTROL:TRANSLATION to ship_raw_to_ship_control(V(x,y,z)).
}
}
function dock { function dock {
// //
// Dock the ship with another vessel. // Dock the ship with another vessel.
@ -77,8 +36,8 @@ function dock {
wait until vang(SHIP:facing:vector, alignment_direction:vector) <= 1. wait until vang(SHIP:facing:vector, alignment_direction:vector) <= 1.
print "==> Translating". print "==> Translating".
// TODO: Translate "around" the target vessel on different axes first so we dont fail if behind the target docking port. // TODO: Translate "around" the target vessel on different axes first so we dont crash if behind the target docking port.
translate({return (target_port + target_port_facing) - (local_port + local_port_facing).}, max_translation_speed). rcs_translate({return (target_port + target_port_facing) - (local_port + local_port_facing).}, max_translation_speed).
print "==> Docking". print "==> Docking".
// TARGET will be unset the moment we dock, causing many of the calculations and local locks to cause errors. // TARGET will be unset the moment we dock, causing many of the calculations and local locks to cause errors.
@ -91,34 +50,3 @@ function dock {
wait until not HASTARGET. wait until not HASTARGET.
print "==> DOCKING COMPLETE". print "==> DOCKING COMPLETE".
} }
dock().
// TODO: Make GUI with dropdown of target:dockingports and SHIP:dockingports so user can't fuck it up.
//if not HASTARGET {
// print "Please select a target".
// return.
//}
//function find_docking_port {
// parameter target. // SHIP or TARGET.
// parameter target_name is target:name.
//
// if target:istype("DockingPort") {
// return target.
// }
//
// local target_ports is target:dockingports.
// if target_ports:empty {
// print "No docking ports on " + target_name.
// return.
// }
// if target_ports:length <> 1 {
// print "Multiple docking ports on " + target_name + ", please select one and try again".
// return.
// }
// return target_ports[0]
//}

80
lib/rcs.ks Normal file
View file

@ -0,0 +1,80 @@
@LAZYGLOBAL off.
run once "lib/target".
run once "lib/vectors".
function rcs_isp_sum {
//
// Return the sum of ISP for RCS thrusters.
//
local sum is 0.
for thruster in SHIP:modulesnamed("ModuleRCSFX") {
set sum to sum + thruster:getfield("rcs isp").
}
return sum.
}
function rcs_maxthrust {
// There's no way to get thrust of RCS dynamically in kOS; gonna have to hard-code it :/
// Source: https://wiki.kerbalspaceprogram.com/wiki/Reaction_Control_System#Thrusters
local rcs_thrusts is LEXICON( // in kN
"RCSBlock", 1.0, // RV-105 RCS Thruster Block
"linearRcs", 2.0, // Place-Anywhere 7 Linear RCS Port
"vernierEngine", 12.0 // Vernor Engine
).
local thrust_sum is 0.
for module in SHIP:modulesnamed("ModuleRCSFX") {
local part_name is module:part:name.
if rcs_thrusts:haskey(part_name) {
set thrust_sum to thrust_sum + rcs_thrusts[part_name].
} else { // if non-stock part
print "WARNING: Unknown RCS Thruster '" + part_name + "'; using default thrust of 1.0 kN".
set thrust_sum to thrust_sum + 1.0.
}
}
return thrust_sum.
}
function rcs_translate {
//
// Translate the ship's position by the given vector using RCS thrusters.
//
parameter translation_vector_delegate.
parameter max_speed.
local lock translation_vector to translation_vector_delegate().
// Find available RCS acceleration by Newton's second law: (F=mg). Note that the acceleration is conservatively divided
// by 6 since thrusters might not be aligned with thrust vector (but assumed evenly distributed on the 6 axes).
// Furthermore, acceleration is limited to 0.05m/s^2 since small ships with low mass are uncontrollable if allowed to accelerate wildly.
local acceleration is min(0.05, (rcs_maxthrust() / SHIP:MASS) / 6).
// Time t to travel distance d under constant acceleration a is t = sqrt(2d/a) (https://en.wikipedia.org/wiki/Equations_for_a_falling_body).
// Multiply this by the available acceleration to get speed (s * m/s^2 = m/s) required to decelerate to 0 by the time d=0.
local lock desired_speed to min(max_speed, sqrt(2*translation_vector:mag / acceleration) * acceleration).
local lock desired_velocity to translation_vector:normalized * desired_speed.
// The proportional gain factor is set to the square root of the ship's mass. Why? It was empirically shown to work.
// The idea is that small ships are more sensitive to adjustments, and so small error corrections will do more harm than good.
local pid_x is PIDLOOP(sqrt(SHIP:MASS), 0.01, 0.001, -1, 1). // kp, ki, kd, minoutput, maxoutput
local pid_y is PIDLOOP(sqrt(SHIP:MASS), 0.01, 0.001, -1, 1).
local pid_z is PIDLOOP(sqrt(SHIP:MASS), 0.01, 0.001, -1, 1).
//vdraw(SHIP:controlpart:position, translation_vector@, WHITE).
//vdraw(SHIP:controlpart:position, relative_velocity@, GREEN).
RCS on.
until translation_vector:mag < 0.05 and relative_velocity():mag < 0.01 {
set pid_x:setpoint to desired_velocity:x.
set pid_y:setpoint to desired_velocity:y.
set pid_z:setpoint to desired_velocity:z.
local x is pid_x:update(TIME:seconds, relative_velocity():x).
local y is pid_y:update(TIME:seconds, relative_velocity():y).
local z is pid_z:update(TIME:seconds, relative_velocity():z).
set SHIP:CONTROL:TRANSLATION to ship_raw_to_ship_control(V(x,y,z)).
}
}

View file

@ -51,41 +51,6 @@ function isp_sum {
} }
function rcs_isp_sum {
//
// Return the sum of ISP for RCS thrusters.
//
local sum is 0.
for thruster in SHIP:modulesnamed("ModuleRCSFX") {
set sum to sum + thruster:getfield("rcs isp").
}
return sum.
}
function rcs_maxthrust {
// There's no way to get thrust of RCS dynamically in kOS; gonna have to hard-code it :/
// Source: https://wiki.kerbalspaceprogram.com/wiki/Reaction_Control_System#Thrusters
local rcs_thrusts is LEXICON( // in kN
"RCSBlock", 1.0, // RV-105 RCS Thruster Block
"linearRcs", 2.0, // Place-Anywhere 7 Linear RCS Port
"vernierEngine", 12.0 // Vernor Engine
).
local thrust_sum is 0.
for module in SHIP:modulesnamed("ModuleRCSFX") {
local part_name is module:part:name.
if rcs_thrusts:haskey(part_name) {
set thrust_sum to thrust_sum + rcs_thrusts[part_name].
} else { // if non-stock part
print "WARNING: Unknown RCS Thruster '" + part_name + "'; using default thrust of 1.0 kN".
set thrust_sum to thrust_sum + 1.0.
}
}
return thrust_sum.
}
function deploy_fairings { function deploy_fairings {
print "Deploying fairings". print "Deploying fairings".
for module in SHIP:modulesnamed("ModuleProceduralFairing") { for module in SHIP:modulesnamed("ModuleProceduralFairing") {