forked from allista/ThrottleControlledAvionics
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathEngineWrapper.cs
370 lines (319 loc) · 12.4 KB
/
EngineWrapper.cs
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
369
370
// Author:
// Allis Tauri <[email protected]>
//
// Copyright (c) 2015 Allis Tauri
//
// This work is licensed under the Creative Commons Attribution-ShareAlike 4.0 International License.
// To view a copy of this license, visit http://creativecommons.org/licenses/by-sa/4.0/
// or send a letter to Creative Commons, PO Box 1866, Mountain View, CA 94042, USA.
// Original idea of EngineWrapper came from HoneyFox's EngineIgnitor mod: https://github.com/HoneyFox/EngineIgnitor
using System;
using UnityEngine;
using AT_Utils;
namespace ThrottleControlledAvionics
{
public abstract class ThrusterWrapper
{
public Vector3 specificTorque = Vector3.zero;
public Vector3 currentTorque = Vector3.zero;
public Vector3 thrustDirection = Vector3.zero;
public Vector3 wThrustLever = Vector3.zero;
public float currentTorque_m;
public float torqueRatio;
public float limit, best_limit, limit_tmp;
public float thrustMod;
public bool preset_limit;
protected float zeroIsp;
public abstract Vessel vessel { get; }
public abstract Part part { get; }
public bool Valid(VesselWrapper VSL)
{ return part != null && vessel != null && vessel.id == VSL.vessel.id; }
public abstract bool isOperational { get; }
public abstract float finalThrust { get; }
public abstract float thrustLimit { get; set; }
public abstract Vector3 wThrustPos { get; }
public abstract Vector3 wThrustDir { get; }
public abstract Vector3 Thrust(float throttle);
public abstract Vector3 Torque(float throttle);
public abstract void InitLimits();
public abstract void InitState();
//needed for autopilots that are executed before FixedUpdate
public void PresetLimit(float lim)
{
limit = best_limit = limit_tmp = lim;
preset_limit = true;
}
public virtual void InitTorque(VesselWrapper VSL, float ratio_factor)
{
wThrustLever = wThrustPos-VSL.Physics.wCoM;
thrustDirection = VSL.refT.InverseTransformDirection(wThrustDir);
specificTorque = VSL.refT.InverseTransformDirection(Vector3.Cross(wThrustLever, wThrustDir));
torqueRatio = Mathf.Pow(Mathf.Clamp01(1-Mathf.Abs(Vector3.Dot(wThrustLever.normalized, wThrustDir))), ratio_factor);
}
}
public class RCSWrapper : ThrusterWrapper
{
public readonly ModuleRCS rcs;
Vector3 avg_thrust_dir;
Vector3 avg_thrust_pos;
float current_thrust;
float current_max_thrust;
public RCSWrapper(ModuleRCS rcs)
{
zeroIsp = rcs.atmosphereCurve.Evaluate(0f);
this.rcs = rcs;
}
public override void InitLimits()
{
if(!preset_limit)
limit = best_limit = limit_tmp = 1f;
preset_limit = false;
}
public override void InitState()
{
thrustMod = rcs.atmosphereCurve.Evaluate((float)(rcs.part.staticPressureAtm))/zeroIsp;
var total_sthrust = 0f;
avg_thrust_dir = Vector3.zero;
avg_thrust_pos = Vector3.zero;
for(int i = 0, count = rcs.thrusterTransforms.Count; i < count; i++)
{
var sthrust = rcs.thrustForces[i];
var T = rcs.thrusterTransforms[i];
if(T == null) continue;
avg_thrust_dir += (rcs.useZaxis? T.forward : T.up)*sthrust;
avg_thrust_pos += T.position*sthrust;
total_sthrust += sthrust;
}
var current_sthrust = avg_thrust_dir.magnitude;
if(total_sthrust > 0) avg_thrust_pos /= total_sthrust;
else avg_thrust_pos = rcs.transform.position;
avg_thrust_dir.Normalize();
current_max_thrust = current_sthrust*rcs.thrusterPower*thrustMod;
current_thrust = current_sthrust*rcs.thrustPercentage*0.01f*rcs.thrusterPower*thrustMod;
InitLimits();
}
public override Vessel vessel { get { return rcs.vessel; } }
public override Part part { get { return rcs.part; } }
public override Vector3 wThrustDir { get { return avg_thrust_dir; } }
public override Vector3 wThrustPos { get { return avg_thrust_pos; } }
public float currentMaxThrust { get { return current_max_thrust; } }
public override float finalThrust { get { return current_thrust; } }
public float maxThrust { get { return rcs.thrusterPower*thrustMod; } }
public override Vector3 Thrust (float throttle)
{ return thrustDirection*current_max_thrust*throttle; }
public override Vector3 Torque (float throttle)
{ return specificTorque*current_max_thrust*throttle; }
public override float thrustLimit
{
get { return rcs.thrustPercentage*0.01f; }
set { rcs.thrustPercentage = Mathf.Clamp(Utils.EWA(rcs.thrustPercentage, value*100), 0, 100); }
}
public override bool isOperational
{ get { return rcs.rcsEnabled && rcs.thrusterTransforms.Count > 0 && rcs.thrusterTransforms.Count == rcs.thrustForces.Length; } }
}
public class EngineID
{
uint id;
//FIXME: generates Number overflow on flight scene load
public EngineID(EngineWrapper e)
{
if(e.part == null || e.engine == null) return;
var rT = e.part.localRoot == null? e.part.transform : e.part.localRoot.transform;
var to = rT.InverseTransformPoint(e.part.partTransform.position);
var td = rT.InverseTransformDirection(e.part.partTransform.forward);
var ids = string.Format("{0} {1} {2:F1} {3:F1} {4:F1} {5:F1} {6:F1} {7:F1}",
e.part.partInfo.name, e.engine.engineID, to.x, to.y, to.z, td.x, td.y, td.z);
id = (uint)ids.GetHashCode();
// Utils.Log("{}: {}", ids, id);//debug
}
public static implicit operator uint(EngineID eid) { return eid.id; }
}
public class EngineWrapper : ThrusterWrapper
{
public static readonly PI_Controller ThrustPI = new PI_Controller();
protected PIf_Controller thrustController = new PIf_Controller();
public readonly ModuleEngines engine;
public readonly ModuleGimbal gimbal;
public readonly TCAEngineInfo info;
public string name { get; private set; }
public uint ID { get; private set; }
public uint flightID { get { return part.flightID; } }
public float throttle;
public float realIsp;
public float flowMod;
public float VSF; //vertical speed factor
public bool isVSC; //vertical speed controller
public bool isSteering;
public TCARole Role { get { return info.Role; } }
public int Group { get { return info.Group; } }
public CenterOfThrustQuery thrustInfo;
public EngineWrapper(ModuleEngines engine)
{
//init
thrustController.setMaster(ThrustPI);
zeroIsp = engine.atmosphereCurve.Evaluate(0f);
name = Utils.ParseCamelCase(engine.part.Title());
if(engine.engineID.Length > 0 && engine.engineID != "Engine")
name += " (" + engine.engineID + ")";
//generate engine ID
this.engine = engine;
ID = new EngineID(this);
//get info
info = engine.part.Modules.GetModule<TCAEngineInfo>();
//find gimbal
gimbal = engine.part.Modules.GetModule<ModuleGimbal>();
}
#region methods
public void SetRole(TCARole role) { info.SetRole(role); }
public override void InitLimits()
{
isVSC = isSteering = false;
switch(Role)
{
case TCARole.MAIN:
case TCARole.BALANCE:
case TCARole.UNBALANCE:
if(!preset_limit)
limit = best_limit = 1f;
isSteering = Role == TCARole.MAIN;
isVSC = true;
break;
case TCARole.MANEUVER:
if(!preset_limit)
limit = best_limit = 0f;
isSteering = true;
break;
case TCARole.MANUAL:
limit = best_limit = thrustLimit;
break;
}
preset_limit = false;
}
public void UpdateThrustInfo()
{
thrustInfo = new CenterOfThrustQuery();
engine.OnCenterOfThrustQuery(thrustInfo);
thrustInfo.dir.Normalize();
}
public override void InitState()
{
if(engine == null || part == null || vessel == null) return;
//update thrust info
UpdateThrustInfo();
realIsp = GetIsp((float)(part.staticPressureAtm), (float)(part.atmDensity/1.225), (float)part.machNumber);
flowMod = GetFlowMod((float)(part.atmDensity/1.225), (float)part.machNumber);
thrustMod = realIsp*flowMod/zeroIsp;
//update Role
if(engine.throttleLocked && info.Role != TCARole.MANUAL)
info.SetRole(TCARole.MANUAL);
InitLimits();
// Utils.Log("Engine.InitState: {}\n" +
// "wThrustDir {}\n" +
// "wThrustPos {}\n" +
// "zeroIsp {}, realIsp {}, flowMod {}, thrustMod {}\n" +
// "P {}, Rho {}, Mach {}, multIsp {}, multFlow {}\n" +
// "###############################################################",
// name, wThrustDir, wThrustPos,
// zeroIsp, realIsp, flowMod, thrustMod,
// part.staticPressureAtm, part.atmDensity, part.machNumber,
// engine.multIsp, engine.multFlow);//debug
}
public override Vector3 Thrust(float throttle)
{
return thrustDirection *
(Role != TCARole.MANUAL?
nominalCurrentThrust(throttle) :
engine.finalThrust);
}
public override Vector3 Torque(float throttle)
{
return specificTorque *
(Role != TCARole.MANUAL?
nominalCurrentThrust(throttle) :
engine.finalThrust);
}
float GetIsp(float pressureAtm, float rel_density, float vel_mach)
{
var Isp = engine.atmosphereCurve.Evaluate(pressureAtm) * engine.multIsp;
if(engine.useAtmCurveIsp)
Isp *= engine.atmCurveIsp.Evaluate(rel_density);
if(engine.useVelCurveIsp)
Isp *= engine.velCurveIsp.Evaluate(vel_mach);
return Isp;
}
float GetFlowMod(float rel_density, float vel_mach)
{
var fmod = engine.multFlow;
if(engine.atmChangeFlow)
{
fmod = rel_density;
if(engine.useAtmCurve)
fmod = engine.atmCurve.Evaluate(fmod);
}
if(engine.useVelCurve)
fmod *= engine.velCurve.Evaluate(vel_mach);
if(fmod > engine.flowMultCap)
{
float to_cap = fmod - engine.flowMultCap;
fmod = engine.flowMultCap + to_cap / (engine.flowMultCapSharpness + to_cap / engine.flowMultCap);
}
//the "< 1" check is needed for TCA to work with SolverEngines,
//as it sets the CLAMP to float.MaxValue:
//SolverEngines/SolverEngines/EngineModule.cs:
//https://github.com/KSP-RO/SolverEngines/blob/eba89da74767fb66ea96661a5950fa52233e8822/SolverEngines/EngineModule.cs#L572
if(fmod < engine.CLAMP && engine.CLAMP < 1) fmod = engine.CLAMP;
return fmod;
}
public float ThrustAtAlt(float vel, float alt, out float mFlow)
{
mFlow = 0;
if(!vessel.mainBody.atmosphere) return zeroIsp;
var atm = vessel.mainBody.AtmoParamsAtAltitude(alt);
var rel_density = (float)(atm.Rho/1.225);
var vel_mach = (float)(vel/atm.Mach1);
var Ve = GetIsp((float)(atm.P/1013.25), rel_density, vel_mach) * Utils.G0;
mFlow = engine.maxFuelFlow * GetFlowMod(rel_density, vel_mach);
return mFlow * Ve;
}
public float MaxFuelFlow { get { return engine.maxFuelFlow*flowMod; } }
public float RealFuelFlow { get { return engine.requestedMassFlow*engine.propellantReqMet/100; } }
#endregion
#region Accessors
public override Vessel vessel { get { return engine.vessel; } }
public override Part part { get { return engine.part; } }
public override Vector3 wThrustDir { get { return thrustInfo.dir; } }
public override Vector3 wThrustPos { get { return thrustInfo.pos; } }
public bool useEngineResponseTime { get { return engine.useEngineResponseTime; } }
public float engineAccelerationSpeed { get { return engine.engineAccelerationSpeed; } }
public float engineDecelerationSpeed { get { return engine.engineDecelerationSpeed; } }
public override float finalThrust { get { return engine.finalThrust; } }
public float nominalCurrentThrust(float throttle)
{
return thrustMod * (engine.throttleLocked ?
engine.maxThrust : Mathf.Lerp(engine.minThrust, engine.maxThrust, throttle));
}
public override float thrustLimit
{
get { return engine.thrustPercentage/100f; }
set
{
if(engine.throttleLocked) return;
thrustController.Update(value*100-engine.thrustPercentage);
engine.thrustPercentage = Mathf.Clamp(engine.thrustPercentage+thrustController.Action, 0, 100);
}
}
public void forceThrustPercentage(float value)
{ if(!engine.throttleLocked) engine.thrustPercentage = Mathf.Clamp(value, 0, 100); }
public override bool isOperational { get { return engine.isOperational; } }
#endregion
public override string ToString()
{
return Utils.Format("[EngineWrapper: name={}, ID={}, flightID={}, Stage={}, Role={}, Group={},\n" +
"useEngineResponseTime={}, engineAccelerationSpeed={}, engineDecelerationSpeed={},\n" +
"finalThrust={}, thrustLimit={}, isOperational={}]",
name, ID, flightID, part.inverseStage, Role, Group,
useEngineResponseTime, engineAccelerationSpeed, engineDecelerationSpeed,
finalThrust, thrustLimit, isOperational);
}
}
}