forked from RemoteTechnologiesGroup/RemoteTech
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFlightCore.cs
More file actions
368 lines (311 loc) · 14.3 KB
/
Copy pathFlightCore.cs
File metadata and controls
368 lines (311 loc) · 14.3 KB
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
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using UnityEngine;
namespace RemoteTech
{
public static class FlightCore
{
public static void HoldAttitude(FlightCtrlState fs, FlightComputer f, ReferenceFrame frame, FlightAttitude attitude, Quaternion extra)
{
var v = f.Vessel;
var forward = Vector3.zero;
var up = Vector3.zero;
var rotationReference = Quaternion.identity;
switch (frame)
{
case ReferenceFrame.Orbit:
forward = v.GetObtVelocity();
up = (v.mainBody.position - v.CoM);
break;
case ReferenceFrame.Surface:
forward = v.GetSrfVelocity();
up = (v.mainBody.position - v.CoM);
break;
case ReferenceFrame.North:
up = (v.mainBody.position - v.CoM);
forward = Vector3.Exclude(up,
v.mainBody.position + v.mainBody.transform.up * (float)v.mainBody.Radius - v.CoM
);
break;
case ReferenceFrame.Maneuver:
up = v.transform.up;
if (f.DelayedManeuver != null)
{
forward = f.DelayedManeuver.GetBurnVector(v.orbit);
up = (v.mainBody.position - v.CoM);
}
else
{
forward = v.GetObtVelocity();
up = (v.mainBody.position - v.CoM);
}
break;
case ReferenceFrame.TargetVelocity:
if (f.DelayedTarget != null && f.DelayedTarget is Vessel)
{
forward = v.GetObtVelocity() - f.DelayedTarget.GetObtVelocity();
up = (v.mainBody.position - v.CoM);
}
else
{
up = (v.mainBody.position - v.CoM);
forward = v.GetObtVelocity();
}
break;
case ReferenceFrame.TargetParallel:
if (f.DelayedTarget != null && f.DelayedTarget is Vessel)
{
forward = f.DelayedTarget.GetTransform().position - v.CoM;
up = (v.mainBody.position - v.CoM);
}
else
{
up = (v.mainBody.position - v.CoM);
forward = v.GetObtVelocity();
}
break;
}
Vector3.OrthoNormalize(ref forward, ref up);
rotationReference = Quaternion.LookRotation(forward, up);
switch (attitude)
{
case FlightAttitude.Prograde:
break;
case FlightAttitude.Retrograde:
rotationReference = rotationReference * Quaternion.AngleAxis(180, Vector3.up);
break;
case FlightAttitude.NormalPlus:
rotationReference = rotationReference * Quaternion.AngleAxis(90, Vector3.up);
break;
case FlightAttitude.NormalMinus:
rotationReference = rotationReference * Quaternion.AngleAxis(90, Vector3.down);
break;
case FlightAttitude.RadialPlus:
rotationReference = rotationReference * Quaternion.AngleAxis(90, Vector3.right);
break;
case FlightAttitude.RadialMinus:
rotationReference = rotationReference * Quaternion.AngleAxis(90, Vector3.left);
break;
case FlightAttitude.Surface:
rotationReference = rotationReference * extra;
break;
}
HoldOrientation(fs, f, rotationReference);
}
public static void HoldOrientation(FlightCtrlState fs, FlightComputer f, Quaternion target)
{
f.Vessel.ActionGroups.SetGroup(KSPActionGroup.SAS, false);
kOS.SteeringHelper.SteerShipToward(target, fs, f.Vessel);
}
public static double GetTotalThrust(Vessel v)
{
double thrust = 0.0;
// @todo: Is it possible to select ModuleEngines OR ModuleEnginesFX in a single iterator?
foreach (var pm in v.parts.SelectMany(p => p.FindModulesImplementing<ModuleEngines>()))
{
if (!pm.EngineIgnited) continue;
thrust += (double)pm.maxThrust * (pm.thrustPercentage / 100);
}
foreach (var pm in v.parts.SelectMany(p => p.FindModulesImplementing<ModuleEnginesFX>()))
{
if (!pm.EngineIgnited) continue;
thrust += (double)pm.maxThrust * (pm.thrustPercentage / 100);
}
return thrust;
}
}
}
namespace kOS
{
public static class SteeringHelper
{
public static Vector3d prev_err;
public static Vector3d integral;
private static Vector3d[] averagedAct = new Vector3d[5];
public static void KillRotation(FlightCtrlState c, Vessel vessel)
{
var act = vessel.transform.InverseTransformDirection(vessel.rigidbody.angularVelocity).normalized;
c.pitch = act.x;
c.roll = act.y;
c.yaw = act.z;
c.killRot = true;
}
public static void SteerShipToward(Quaternion target, FlightCtrlState c, Vessel vessel)
{
// I take no credit for this, this is a stripped down, rearranged version of MechJeb's attitude control system
var CoM = vessel.findWorldCenterOfMass();
var MoI = vessel.findLocalMOI(CoM);
var mass = vessel.GetTotalMass();
var up = (CoM - vessel.mainBody.position).normalized;
var vesselR = vessel.transform.rotation;
Quaternion delta;
delta = Quaternion.Inverse(Quaternion.Euler(90, 0, 0) * Quaternion.Inverse(vesselR) * target);
Vector3d deltaEuler = ReduceAngles(delta.eulerAngles);
deltaEuler.y *= -1;
Vector3d torque = GetTorque(vessel, c.mainThrottle);
Vector3d inertia = GetEffectiveInertia(vessel, torque);
Vector3d err = deltaEuler * Math.PI / 180.0F;
err += new Vector3d(inertia.x, inertia.z, inertia.y);
//err.Scale(SwapYZ(Vector3d.Scale(MoI, Inverse(torque))));
prev_err = err;
Vector3d act = 120.0f * err;
float precision = Mathf.Clamp((float)torque.x * 20f / MoI.magnitude, 0.5f, 10f);
float drive_limit = Mathf.Clamp01((float)(err.magnitude * 380.0f / precision));
act.x = Mathf.Clamp((float)act.x, -drive_limit, drive_limit);
act.y = Mathf.Clamp((float)act.y, -drive_limit, drive_limit);
act.z = Mathf.Clamp((float)act.z, -drive_limit, drive_limit);
//act = averageVector3d(averagedAct, act, 2);
c.roll = Mathf.Clamp((float)(c.roll + act.z), -drive_limit, drive_limit);
c.pitch = Mathf.Clamp((float)(c.pitch + act.x), -drive_limit, drive_limit);
c.yaw = Mathf.Clamp((float)(c.yaw + act.y), -drive_limit, drive_limit);
/*
// This revised version from 0.6 gave people problems with gravity turns. I've reverted but may try to make it work
var CoM = vessel.findWorldCenterOfMass();
var MoI = vessel.findLocalMOI(CoM);
var mass = vessel.GetTotalMass();
var up = (CoM - vessel.mainBody.position).normalized;
var target = targetDir.Rotation;
var vesselR = vessel.transform.rotation;
Quaternion delta;
delta = Quaternion.Inverse(Quaternion.Euler(90, 0, 0) * Quaternion.Inverse(vesselR) * target);
Vector3d deltaEuler = ReduceAngles(delta.eulerAngles);
deltaEuler.y *= -1;
Vector3d torque = GetTorque(vessel, c.mainThrottle);
Vector3d inertia = GetEffectiveInertia(vessel, torque);
Vector3d err = deltaEuler * Math.PI / 180.0F;
err += SwapYZ(inertia * 8);
err.Scale(SwapYZ(Vector3d.Scale(MoI * 3, Inverse(torque))));
prev_err = err;
Vector3d act = 400.0f * err;
float precision = Mathf.Clamp((float)torque.x * 20f / MoI.magnitude, 0.5f, 10f);
float drive_limit = Mathf.Clamp01((float)(err.magnitude * 450.0f / precision));
act.x = Mathf.Clamp((float)act.x, -drive_limit, drive_limit);
act.y = Mathf.Clamp((float)act.y, -drive_limit, drive_limit);
act.z = Mathf.Clamp((float)act.z, -drive_limit, drive_limit);
//act = averageVector3d(averagedAct, act, 2);
c.roll = Mathf.Clamp((float)(c.roll + act.z), -drive_limit, drive_limit);
c.pitch = Mathf.Clamp((float)(c.pitch + act.x), -drive_limit, drive_limit);
c.yaw = Mathf.Clamp((float)(c.yaw + act.y), -drive_limit, drive_limit);*/
}
public static Vector3d SwapYZ(Vector3d input)
{
return new Vector3d(input.x, input.z, input.y);
}
public static Vector3d Pow(Vector3d v3d, float exponent)
{
return new Vector3d(Math.Pow(v3d.x, exponent), Math.Pow(v3d.y, exponent), Math.Pow(v3d.z, exponent));
}
public static Vector3d GetEffectiveInertia(Vessel vessel, Vector3d torque)
{
var CoM = vessel.findWorldCenterOfMass();
var MoI = vessel.findLocalMOI(CoM);
var angularVelocity = Quaternion.Inverse(vessel.transform.rotation) * vessel.rigidbody.angularVelocity;
var angularMomentum = new Vector3d(angularVelocity.x * MoI.x, angularVelocity.y * MoI.y, angularVelocity.z * MoI.z);
var retVar = Vector3d.Scale
(
Sign(angularMomentum) * 2.0f,
Vector3d.Scale(Pow(angularMomentum, 2), Inverse(Vector3d.Scale(torque, MoI)))
);
retVar.y *= 10;
return retVar;
}
public static Vector3d GetTorque(Vessel vessel, float thrust)
{
var CoM = vessel.findWorldCenterOfMass();
float pitchYaw = 0;
float roll = 0;
foreach (Part part in vessel.parts)
{
var relCoM = part.Rigidbody.worldCenterOfMass - CoM;
if (part is CommandPod)
{
pitchYaw += Math.Abs(((CommandPod)part).rotPower);
roll += Math.Abs(((CommandPod)part).rotPower);
}
if (part is RCSModule)
{
float max = 0;
foreach (float power in ((RCSModule)part).thrusterPowers)
{
max = Mathf.Max(max, power);
}
pitchYaw += max * relCoM.magnitude;
}
foreach (PartModule module in part.Modules)
{
if (module is ModuleReactionWheel)
{
pitchYaw += ((ModuleReactionWheel)module).PitchTorque;
roll += ((ModuleReactionWheel)module).RollTorque;
}
}
pitchYaw += (float)GetThrustTorque(part, vessel) * thrust;
}
return new Vector3d(pitchYaw, roll, pitchYaw);
}
public static double GetThrustTorque(Part p, Vessel vessel)
{
var CoM = vessel.CoM;
if (p.State == PartStates.ACTIVE)
{
if (p is LiquidEngine)
{
if (((LiquidEngine)p).thrustVectoringCapable)
{
return Math.Sin(Math.Abs(((LiquidEngine)p).gimbalRange) * Math.PI / 180) * ((LiquidEngine)p).maxThrust * (p.Rigidbody.worldCenterOfMass - CoM).magnitude;
}
}
else if (p is LiquidFuelEngine)
{
if (((LiquidFuelEngine)p).thrustVectoringCapable)
{
return Math.Sin(Math.Abs(((LiquidFuelEngine)p).gimbalRange) * Math.PI / 180) * ((LiquidFuelEngine)p).maxThrust * (p.Rigidbody.worldCenterOfMass - CoM).magnitude;
}
}
else if (p is AtmosphericEngine)
{
if (((AtmosphericEngine)p).thrustVectoringCapable)
{
return Math.Sin(Math.Abs(((AtmosphericEngine)p).gimbalRange) * Math.PI / 180) * ((AtmosphericEngine)p).maximumEnginePower * ((AtmosphericEngine)p).totalEfficiency * (p.Rigidbody.worldCenterOfMass - CoM).magnitude;
}
}
}
return 0;
}
private static Vector3d ReduceAngles(Vector3d input)
{
return new Vector3d(
(input.x > 180f) ? (input.x - 360f) : input.x,
(input.y > 180f) ? (input.y - 360f) : input.y,
(input.z > 180f) ? (input.z - 360f) : input.z
);
}
public static Vector3d Inverse(Vector3d input)
{
return new Vector3d(1 / input.x, 1 / input.y, 1 / input.z);
}
public static Vector3d Sign(Vector3d vector)
{
return new Vector3d(Math.Sign(vector.x), Math.Sign(vector.y), Math.Sign(vector.z));
}
private static Vector3d averageVector3d(Vector3d[] vectorArray, Vector3d newVector, int n)
{
double x = 0.0, y = 0.0, z = 0.0;
int k = 0;
// Loop through the array to determine average
// Give more weight to newer items and less weight to older items
for (int i = 0; i < n; i++)
{
k += i + 1;
if (i < n - 1) { vectorArray[i] = vectorArray[i + 1]; }
else { vectorArray[i] = newVector; }
x += vectorArray[i].x * (i + 1);
y += vectorArray[i].y * (i + 1);
z += vectorArray[i].z * (i + 1);
}
return new Vector3d(x / k, y / k, z / k);
}
}
}