-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathDEFAULT--ECU.conf
247 lines (228 loc) · 11.7 KB
/
DEFAULT--ECU.conf
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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
# Custom ECU by TheGreatSardini
name: (ECU) DEFAULT-- v1.1
slots:
core:
class: CoreUnit
antigrav:
class: AntiGravityGeneratorUnit
gyro:
class: GyroUnit
vBooster:
class: VerticalBooster
hover:
class: Hovercraft
telemeter:
class: TelemeterUnit
handlers:
unit:
onStart:
lua: |
Nav = Navigator.new(system, core, unit)
planetInfluenceThreshold = 0.6
if antigrav ~= nil then
antigrav.activate()
antigrav.showWidget()
end
if unit then unit.hideWidget() end
buildHud()
aggData = {}
local lastvgd = -1
local lasthgd = -1
function AboveGroundLevel()
local function hoverDetectGround()
local vGndDist,vGndMax,hGndDist,hGndMax = -1,-1,-1,-1
if vBooster then
vGndDist = vBooster.getDistance()
vGndMax = vBooster.getMaxDistance()
if vGndDist > -1 and vGndDist < 0.01 then vGndDist = 0 else lastvgd = vGndDist end --account for underwater
end
if hover then
hGndDist = hover.getDistance()
hGndMax = hover.getMaxDistance()
if hGndDist > -1 and hGndDist < 0.01 then hGndDist = 0 else lasthgd = hGndDist end
end
if vGndDist ~= -1 and hGndDist ~= -1 then
if vGndDist < hGndDist then
return vGndDist, math.max(vGndMax,hGndMax)
else
return hGndDist, math.max(vGndMax,hGndMax)
end
elseif vGndDist ~= -1 then
return vGndDist, vGndMax
elseif hGndDist ~= -1 then
return hGndDist, hGndMax
else
return -1, math.max(vGndMax,hGndMax)
end
end
local hovGndDet, hovGndMax = hoverDetectGround()
--system.print("dist="..hovGndDet)
local telGndDet = -1
local cAV = construct.getVelocity()
--local velMag = vectorLen(cAV[1], cAV[2], cAV[3])
if telemeter then
telGndDet = telemeter.raycast().distance
if telGndDet > 0 then return telGndDet, hovGndMax end
end
return hovGndDet, hovGndMax
end
groundDistance = -1
StartTime = system.getArkTime()
function decodeJSON(json)
local decodedInput = string.gsub(json, "%\\u(%x%x%x%x)", function(a) return utf8.char(tonumber(a,16))end)
return load('return ' .. decodedInput:gsub('[[]', '{'):gsub('[]]', '}'):gsub('"(%w+)":', '["%1"]='))()
end
system:
onFlush:
lua: |
Auto_Land = true --export: Should ship Auto_Land if ECU activates. False means continue level flight.
Hold = false --export: Stop moving, hold position and altitude
local Auto_Land_Speed = 50 --export: Vertical auto landing speed in km/h
Hold_And_Land_Height = 10 --export: when both Hold and Auto_Land are on, distance from ground to Auto_Land
local power = 3
local worldUp = vec3(construct.getWorldOrientationUp())
local worldForward = vec3(construct.getWorldOrientationForward())
local worldRight = vec3(construct.getWorldOrientationRight())
local worldVertical = vec3(core.getWorldVertical())
-- Maintain current thrust?
if Hold == false and Auto_Land == false then
--do nothing
elseif antigrav and aggData.State == "ON" and aggData.Power > 10 then
--do nothing AGG is holding in place
else
-- are we in deep space or are we near a planet ?
local planetInfluence = unit.getClosestPlanetInfluence()
if planetInfluence > 0
then
-- stabilize orientation along the gravity
if (rollPID == nil) then
rollPID = pid.new(0.2, 0, 10)
pitchPID = pid.new(0.2, 0, 10)
end
local yawVelocity = vec3(construct.getWorldAngularVelocity()):dot(worldUp)
local currentRoll = getRoll(worldVertical, worldForward, worldRight)
local currentPitch = -math.asin(worldForward:dot(worldVertical)) * constants.rad2deg
rollPID:inject(-currentRoll)
pitchPID:inject(-currentPitch)
local yawAcceleration = - power * yawVelocity
angularAcceleration = rollPID:get() * worldForward + pitchPID:get() * worldRight + yawAcceleration * worldUp
else
-- cancel rotation
local worldAngularVelocity = vec3(construct.getWorldAngularVelocity())
angularAcceleration = - power * worldAngularVelocity
end
Nav:setEngineCommand('torque', vec3(), angularAcceleration)
groundDistance, _ = AboveGroundLevel()
if planetInfluence > planetInfluenceThreshold
and (
Hold == false
or
(Hold == true and Auto_Land == true and groundDistance > -1 and groundDistance < Hold_And_Land_Height)
)
then
local slowLand = Auto_Land_Speed / 10 -- figure out what the deal is with this under water?
local factor = groundDistance >= 0 and 0.2+0.8/(80/groundDistance) or 1
--system.print("factor="..factor.." grnd="..groundDistance)
-- go down at verticalTargetSpeed when very close to a planet
targetVelocity = (Auto_Land_Speed / 3.6) * factor * worldVertical
else
-- immobilize ship when not
targetVelocity = vec3()
--system.print("immobilize")
end
stabilization = (power * (targetVelocity - vec3(construct.getWorldVelocity()))) -vec3(core.getWorldGravity())
Nav:setEngineCommand('airfoil,brake,not_ground thrust,ground', stabilization, vec3(), false, 1, 'airfoil,brake', 'hover_engine', 'eco', 0)
end
onUpdate:
lua: |
local accelThreshold = 0.1
local speedThreshold = 0.1
local time = system.getArkTime()
if antigrav then
local decodeAGG = decodeJSON(antigrav.getWidgetData())
aggData.Power = decodeAGG.antiGPower ~= nil and math.floor(decodeAGG["antiGPower"]*100) or 0
aggData.Field = decodeAGG.antiGravityField ~= nil and math.floor(decodeAGG["antiGravityField"]*100).."Es" or "LOADING"
aggData.Compensation = math.floor(antigrav.getCompensationRate()*1000)/10 .."%" or "N/A"
aggData.Altitude = decodeAGG.baseAltitude ~= nil and decodeAGG.baseAltitude or 0
aggData.State = antigrav.isActive() == true and "ON" or "OFF"
aggData.Target = antigrav.getTargetAltitude()
end
if Hold_And_Land_Height == nil then local Hold_And_Land_Height = 0 end --for when it bugs and doesn't update before onupdate
-- auto stopping mechanism when immobile and close to planet and not holding
local accel = vec3(construct.getWorldAcceleration()):len()
local speed = vec3(construct.getWorldVelocity()):len()
if accel < accelThreshold
and speed < speedThreshold
and unit.getClosestPlanetInfluence() > planetInfluenceThreshold
and Auto_Land == true --there's a bug where it shows nil on first iteration
and (Hold == true and groundDistance < Hold_And_Land_Height and groundDistance > -1 or Hold == false)
and time - StartTime > 2
then
--system.print("Auto Land Complete: Exiting ECU")
unit.exit()
end
library:
onStart:
lua: |
function buildHud()
WLP = {}
WLP.css = [[
BODY {
background:rgba(0, 0, 0, 0);
color:#000000;
width:100vw;
height:100vh;
}
DIV.WinLib_window {
position:absolute;
background:rgba(255, 0, 0, 0);
font-family:"Arial", Sans-Serif;
box-shadow:0px 0px rgba(0, 0, 0, 0);
}
DIV.WinLib_window>.WinLib_content {
padding:4px;
}
#main {
width:{wlib_width}px;
height:{wlib_height}px;
top:{wlib_posY}px;
left:{wlib_posX}px;
z-index:{wlib_zIndex};
}
]]
WLP.html = [[
<style type="text/css">
{wlib_css}
</style>
<DIV class="WinLib_window" id="main">
<DIV class="WinLib_content">
{wlib_html}
</DIV>
</DIV>
]]
local window = {}
window.width = system.getScreenWidth()
window.height = system.getScreenHeight()
window.SVGSize = {x=300,y=104}
window.scale = 1
window.w_pos = {x=window.width/2 -(window.SVGSize.x*window.scale)/2, y=window.height*0.9 -(window.SVGSize.y*window.scale)}
window.zIndex = 1
local SVG = [[
<g text-anchor="middle" font-family="Play" alignment-baseline="bottom" stroke-width="0" style="fill: white" >
<text x="150" y="40" font-size="40" >ECU ACTIVE</text>
<text x="150" y="65" font-size="22">CTRL + Backspace to Exit</text>
</g>
]]
local SVGfinal = '<div><svg viewBox="0 0 '.. window.SVGSize.x ..' '.. window.SVGSize.y ..'" style="fill: white; filter: drop-shadow(0px 0px 4px #800);">'..SVG..'</svg></div>'
local css = WLP.css
:gsub("{wlib_width}",window.SVGSize.x*window.scale)
:gsub("{wlib_height}",window.SVGSize.y*window.scale)
:gsub("{wlib_posX}",window.w_pos.x)
:gsub("{wlib_posY}",window.w_pos.y)
:gsub("{wlib_zIndex}",window.zIndex)
local generated = WLP.html
:gsub("{wlib_css}",css)
:gsub("{wlib_html}",SVGfinal)
system.setScreen(generated)
system.showScreen(1)
end