AirControl  1.3.0
Open Source, Modular, and Extensible Flight Simulator For Deep Learning Research
AC_Airplane_Engine.cs
1 using System.Collections;
2 using System.Collections.Generic;
3 using UnityEngine;
4 using Communicator;
5 using Commons;
6 using JetBrains.Annotations;
7 using System;
8 using UnityEngine.TextCore.LowLevel;
9 
10 
11 namespace AirControl
12 {
16  [RequireComponent(typeof(AC_Airplane_Propeller))]
17  [RequireComponent(typeof(AC_Airplane_Fuel))]
18  [RequireComponent(typeof(AC_Airplane_Audio))]
19  [RequireComponent(typeof(AC_BaseAirplane_Input))]
20  [RequireComponent(typeof(AC_XboxAirplane_Input))]
21  [RequireComponent(typeof(AC_Airplane_Audio))]
22  public class AC_Airplane_Engine : MonoBehaviour
23  {
24  #region Variables
25  [Header("Engine Properties")]
26  public float maxForce = 3000f;
27  public float maxRPM = 3500f;
28 
29  public AnimationCurve powerCurve = AnimationCurve.Linear(0f, 0f, 1f, 1f);
30  public AnimationCurve liftOff = AnimationCurve.Linear(0f, 0f, 1000f, 1000f);
31 
32  [Header("Propellers")]
33  public AC_Airplane_Propeller propeller;
34 
35  private float shutOffSpeed = 2f; // engine slowdone rate on shutoff
36  private bool isShutOff = false;
37  private float lastThrottleValue;
38  private float finalShutoffThrottleValue;
39  private Vector3 rotationDelta;
40  private Vector3 rotationLast;
41  private float propellerSpan;
42 
43 
44  private AC_Airplane_Fuel fuel;
45  #endregion
46 
47  #region Properties
48  // property that listens to the engine shutoff script events
49  public bool ShutEngineOff
50  {
51  set{isShutOff = value;}
52  }
53 
54  private float currentRPM;
55  public float CurrentRPM
56  {
57  get{return currentRPM;}
58  }
59  #endregion
60 
61  #region constants
62  private const float engineEfficiency = 0.6f; // What percentage of propellers actions get converted to actual force
63  private const float airDensity = 0.07967f;
64  #endregion
65 
66  #region BuiltIn Methods
67  void Start()
71  {
72  // maxForce = 7000;// cesna 4500
73  propellerSpan = (float)CommonFunctions.airplanePreset[CommonFunctions.ActiveAirplane+"/propellarSpan"];
74  maxRPM = (float)CommonFunctions.airplanePreset[CommonFunctions.ActiveAirplane+"/maxRPM"];
75  shutOffSpeed = (float)CommonFunctions.airplanePreset[CommonFunctions.ActiveAirplane+"/shutOffSpeed"];
76  if(!fuel)
77  {
78  fuel = GetComponent<AC_Airplane_Fuel>();
79  if(fuel)
80  {
81  fuel.InitFuel();
82  }
83  }
84 
85  }
86 
87  void Update(){
88  // Update max force using equation $F_i = C_t\rho\omega^2_{max}D^4$
89  maxForce = engineEfficiency*airDensity* (float)Math.Pow(propellerSpan,4)* (float)Math.Pow(rotationDelta.magnitude,2);
90  }
91  #endregion
92 
93  #region Custom Methods
94 
95  public void getAngularVelocityPro()
96  {
97  rotationDelta = propeller.transform.eulerAngles - rotationLast;
98  rotationLast = propeller.transform.eulerAngles;
99  // Debug.Log("rotationDelta : "+ rotationDelta.magnitude);
100  }
101 
102 
110  public Vector3 CalculateForce(float throttle)
111  {
112  //Calcualte Power
113  float finalThrottle = Mathf.Clamp01(throttle);
114  // angular velocity
115  getAngularVelocityPro();
116 
117  if(!isShutOff)
118  {
119  finalThrottle = powerCurve.Evaluate(finalThrottle);
120  //keep eye on last throttle value, In case of engine cutoff we can use this value to slowly decrease the engine power
121  lastThrottleValue = finalThrottle;
122  }
123  else
124  {
125  lastThrottleValue -= Time.deltaTime * shutOffSpeed;
126  lastThrottleValue = Mathf.Clamp01(lastThrottleValue);
127  finalThrottle = powerCurve.Evaluate(lastThrottleValue);
128  }
129 
130 
131  //Calculate RPM's
132  currentRPM = finalThrottle * maxRPM;
133  if(propeller)
134  {
135  propeller.HandlePropeller(currentRPM);
136  }
137 
138 
139  // Process the Fuel
140  HandleFuel(finalThrottle);
141 
142 
143  //Create Force
144  float finalPower = finalThrottle * maxForce;
145  Vector3 finalForce = transform.forward * finalPower;
146 
147  #region DBArea
148  //Setting Current engine paramters to DB
149  // DB_Functions.SetEngineVariables(connection, maxForce, finalPower, maxRPM, CurrentRPM);
150  StaticOutputSchema.MaxPower = maxForce; // can be moved to start
151  StaticOutputSchema.CurrentPower = finalPower;
152  StaticOutputSchema.MaxRPM = maxRPM; // can be moved to start
153  StaticOutputSchema.CurrentRPM = currentRPM;
154  #endregion
155 
156  return finalForce;
157  }
158 
163  void HandleFuel(float throttleValue)
164  {
165  //Handle Fuel
166  if(fuel)
167  {
168  fuel.UpdateFuel(throttleValue);
169  if(fuel.CurrentFuel <= 0f)
170  {
171  isShutOff = false;
172  }
173  }
174  }
175  #endregion
176 
177  }
178 }
AirControl.AC_Airplane_Engine
Engine controls
Definition: AC_Airplane_Engine.cs:22
AirControl.AC_Airplane_Fuel.UpdateFuel
void UpdateFuel(float aPrecentage)
Fuel burnout calculations
Definition: AC_Airplane_Fuel.cs:77
AirControl.AC_Airplane_Fuel.InitFuel
void InitFuel()
Initialize full to full capacity
Definition: AC_Airplane_Fuel.cs:45
AirControl.AC_Airplane_Engine.CalculateForce
Vector3 CalculateForce(float throttle)
Calculate the force created by engine Calculate Engine RPM Calculate fuel consumption
Definition: AC_Airplane_Engine.cs:110
AirControl
Definition: AirplaneSelector.cs:8
Communicator
Definition: InputHandle.cs:10
AirControl.AC_Airplane_Propeller
Control propeller rotation
Definition: AC_Airplane_Propeller.cs:10
AirControl.AC_Airplane_Fuel
Control fuel consumption
Definition: AC_Airplane_Fuel.cs:12
Commons
Definition: AirplaneProperties.cs:14