-
Notifications
You must be signed in to change notification settings - Fork 56
/
Copy pathDU-ECU.conf
175 lines (169 loc) · 8.25 KB
/
DU-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
# This is a modified ECU file based on the standard.
name: DU-ECU-1.0
slots:
core:
class: CoreUnit
antigrav:
class: AntiGravityGeneratorUnit
container:
class: FuelContainer
select: all
gyro:
class: GyroUnit
vBooster:
class: VerticalBooster
hover:
class: Hovercraft
handlers:
unit:
start:
lua: |
Nav = Navigator.new(system, core, unit)
planetInfluenceThreshold = 0.6
LandedActivated = false
BrakeLanding = false
BrakeIsOn = false
targetPitch = 0
planetInfluence = Nav.control.getClosestPlanetInfluence()
if planetInfluence > planetInfluenceThreshold
then
if Nav.axisCommandManager:getAxisCommandType(0) == 1
then
Nav.control.cancelCurrentControlMasterMode()
end
BrakeLanding = true
end
if antigrav ~= nil then
antigrav.activate()
antigrav.show()
end
function getPitch(gravityDirection, forward, right)
local horizontalForward = gravityDirection:cross(right):normalize_inplace() -- Cross forward?
local pitch = math.acos(utils.clamp(horizontalForward:dot(-forward), -1, 1)) * constants.rad2deg -- acos?
if horizontalForward:cross(-forward):dot(right) < 0 then
pitch = -pitch
end -- Cross right dot forward?
return pitch
end
function hoverDetectGround()
local vgroundDistance = -1
local hgroundDistance = -1
if vBooster then
vgroundDistance = vBooster.distance()
end
if hover then
hgroundDistance = hover.distance()
end
if vgroundDistance ~= -1 and hgroundDistance ~= -1 then
if vgroundDistance < hgroundDistance then
return vgroundDistance
else
return hgroundDistance
end
elseif vgroundDistance ~= -1 then
return vgroundDistance
elseif hgroundDistance ~= -1 then
return hgroundDistance
else
return -1
end
end
system:
flush:
lua: |
local verticalAutoLandingSpeed = 20 --export: Vertical auto landing speec in km/h
local power = 3
local worldUp = vec3(core.getConstructWorldOrientationUp())
local worldForward = vec3(core.getConstructWorldOrientationForward())
local worldRight = vec3(core.getConstructWorldOrientationRight())
local worldVertical = vec3(core.getWorldVertical())
local constructVelocity = vec3(core.getWorldVelocity())
local constructVelocityDir = vec3(core.getWorldVelocity()):normalize()
-- are we in deep space or are we near a planet ?
if planetInfluence > 0
then
-- stabilize orientation along the gravity
if (rollPID == nil) then
rollPID = pid.new(0.2, 0, 10)
pitchPID = pid.new(8 * 0.01, 0, 8 * 0.1)
end
local yawVelocity = vec3(core.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(core.getWorldAngularVelocity())
angularAcceleration = - power * worldAngularVelocity
end
if planetInfluence < planetInfluenceThreshold
then
-- immobilize ship when not
targetVelocity = vec3()
stabilization = power * (targetVelocity - vec3(core.getWorldVelocity()))
Nav:setEngineCommand('vertical, brake, horizontal', stabilization -vec3(core.getWorldGravity()), vec3(), false)
else
local longitudinalEngineTags = 'thrust analog longitudinal'
local longitudinalAcceleration = Nav.axisCommandManager:composeAxisAccelerationFromThrottle(longitudinalEngineTags, axisCommandId.longitudinal)
Nav:setEngineForceCommand(longitudinalEngineTags, vec3(), 1)
end
Nav:setEngineCommand('torque', vec3(), angularAcceleration)
if BrakeLanding
then
-- Brakes
local brakeAcceleration = -brakeInput * (3 * constructVelocity + 1 * constructVelocityDir)
Nav:setEngineForceCommand('brake', brakeAcceleration)
end
update:
lua: |
local accelThreshold = 0.1
local speedThreshold = 0.1
local landedGroundHeight = 0 -- export: ECU should turn off when landingGroundHeight+3 is reached. Default is 0
-- auto stopping mechanism when immobile and close to planet
local accel = vec3(Nav.core.getWorldAcceleration()):len()
local speed = vec3(Nav.core.getWorldVelocity()):len()
local groundDistance = hoverDetectGround()
local velocity = vec3(core.getWorldVelocity())
local up = -vec3(core.getWorldVertical())
local velMag = velocity:len()
local vSpd = velocity:dot(up)
local constrF = vec3(core.getConstructWorldOrientationForward())
local constrR = vec3(core.getConstructWorldOrientationRight())
local worldV = vec3(core.getWorldVertical())
local pitch = getPitch(worldV, constrF, constrR)
if BrakeIsOn then
brakeInput = 1
else
brakeInput = 0
end
if BrakeLanding then
Nav.axisCommandManager:setThrottleCommand(axisCommandId.longitudinal, 0)
Nav.axisCommandManager:setTargetGroundAltitude(500)
Nav.axisCommandManager:activateGroundEngineAltitudeStabilization(500)
if groundDistance > -1 then
if velMag < 10 then
BrakeLanding = false
Nav.control.extendLandingGears()
Nav.axisCommandManager:setTargetGroundAltitude(landedGroundHeight)
BrakeIsOn = true
else
BrakeIsOn = true
end
elseif (velocity:normalize():dot(-up) < 0.99) then
BrakeIsOn = true
elseif vSpd < -30 then
BrakeIsOn = true
else
BrakeIsOn = false
end
end
if (accel < accelThreshold
and speed < speedThreshold)
or (groundDistance > -1 and groundDistance < landedGroundHeight+3)
then
system.print("ECU Exiting")
unit.exit()
end