-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlib_lazcalc.ks
83 lines (66 loc) · 3.21 KB
/
lib_lazcalc.ks
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
//This file is distributed under the terms of the MIT license, (c) the KSLib team
//=====LAUNCH AZIMUTH CALCULATOR=====
//~~LIB_LAZcalc.ks~~
//~~Version 2.1~~
//~~Created by space-is-hard~~
//~~Updated by TDW89~~
//To use: RUN LAZcalc.ks. SET data TO LAZcalc_init([desired circular orbit altitude in meters],[desired orbital inclination; negative if launching from descending node, positive otherwise]). Then loop SET myAzimuth TO LAZcalc(data).
@LAZYGLOBAL OFF.
FUNCTION LAZcalc_init {
PARAMETER
desiredAlt, //Altitude of desired target orbit (in *meters*)
desiredInc. //Inclination of desired target orbit
//We'll pull the latitude now so we aren't sampling it multiple times
LOCAL launchLatitude IS SHIP:LATITUDE.
LOCAL data IS LIST(). // A list is used to store information used by LAZcalc
//Orbital altitude can't be less than sea level
IF desiredAlt <= 0 {
PRINT "Target altitude cannot be below sea level".
SET launchAzimuth TO 1/0. //Throws error
}.
//Determines whether we're trying to launch from the ascending or descending node
LOCAL launchNode TO "Ascending".
IF desiredInc < 0 {
SET launchNode TO "Descending".
//We'll make it positive for now and convert to southerly heading later
SET desiredInc TO ABS(desiredInc).
}.
//Orbital inclination can't be less than launch latitude or greater than 180 - launch latitude
IF ABS(launchLatitude) > desiredInc {
SET desiredInc TO ABS(launchLatitude).
HUDTEXT("Inclination impossible from current latitude, setting for lowest possible inclination.", 10, 2, 30, RED, FALSE).
}.
IF 180 - ABS(launchLatitude) < desiredInc {
SET desiredInc TO 180 - ABS(launchLatitude).
HUDTEXT("Inclination impossible from current latitude, setting for highest possible inclination.", 10, 2, 30, RED, FALSE).
}.
//Does all the one time calculations and stores them in a list to help reduce the overhead or continuously updating
LOCAL equatorialVel IS (2 * CONSTANT():Pi * BODY:RADIUS) / BODY:ROTATIONPERIOD.
LOCAL targetOrbVel IS SQRT(BODY:MU/ (BODY:RADIUS + desiredAlt)).
data:ADD(desiredInc). //[0]
data:ADD(launchLatitude). //[1]
data:ADD(equatorialVel). //[2]
data:ADD(targetOrbVel). //[3]
data:ADD(launchNode). //[4]
RETURN data.
}.
FUNCTION LAZcalc {
PARAMETER
data. //pointer to the list created by LAZcalc_init
LOCAL inertialAzimuth IS ARCSIN(MAX(MIN(COS(data[0]) / COS(SHIP:LATITUDE), 1), -1)).
LOCAL VXRot IS data[3] * SIN(inertialAzimuth) - data[2] * COS(data[1]).
LOCAL VYRot IS data[3] * COS(inertialAzimuth).
// This clamps the result to values between 0 and 360.
LOCAL Azimuth IS MOD(ARCTAN2(VXRot, VYRot) + 360, 360).
//Returns northerly azimuth if launching from the ascending node
IF data[4] = "Ascending" {
RETURN Azimuth.
//Returns southerly azimuth if launching from the descending node
} ELSE IF data[4] = "Descending" {
IF Azimuth <= 90 {
RETURN 180 - Azimuth.
} ELSE IF Azimuth >= 270 {
RETURN 540 - Azimuth.
}.
}.
}.