@@ -34,13 +34,19 @@ void EngineControl_A32NX::update() {
3434 profilerUpdate.start ();
3535#endif
3636
37- if (!fadecInitialized) {
38- initializeEngineControlData ();
39- fadecInitialized = true ;
37+ // Get ATC ID from sim to be able to load and store fuel levels
38+ // If not yet available, request it from sim and return early
39+ // If available initialize the engine control data
40+ if (atcId.empty ()) {
41+ simData.atcIdDataPtr ->requestUpdateFromSim (msfsHandlerPtr->getTimeStamp (), msfsHandlerPtr->getTickCounter ());
42+ if (simData.atcIdDataPtr ->hasChanged ()) {
43+ atcId = simData.atcIdDataPtr ->data ().atcID ;
44+ LOG_INFO (" Fadec::EngineControl_A32NX::update() - received ATC ID: " + atcId);
45+ initializeEngineControlData ();
46+ }
47+ return ;
4048 }
4149
42- loadFuelConfigIfPossible ();
43-
4450 const double deltaTime = std::max (0.002 , msfsHandlerPtr->getSimulationDeltaTime ());
4551 const double simTime = msfsHandlerPtr->getSimulationTime ();
4652 const double mach = simData.simVarsDataPtr ->data ().airSpeedMach ;
@@ -145,40 +151,6 @@ void EngineControl_A32NX::update() {
145151// PRIVATE
146152// =============================================================================
147153
148- void EngineControl_A32NX::loadFuelConfigIfPossible () {
149- #ifdef PROFILING
150- profilerEnsureFadecIsInitialized.start ();
151- #endif
152- const FLOAT64 simTime = msfsHandlerPtr->getSimulationTime ();
153- const UINT64 tickCounter = msfsHandlerPtr->getTickCounter ();
154-
155- if (!hasLoadedFuelConfig) {
156- bool isSimulationReady = msfsHandlerPtr->getAircraftIsReadyVar ();
157-
158- simData.atcIdDataPtr ->requestUpdateFromSim (msfsHandlerPtr->getTimeStamp (), tickCounter);
159-
160- // we only receive the data one tick later as we request it via simconnect. But it should be enought to only perform the check after
161- // isSimulationReady as this is set by the JS instruments after spawn
162- if (isSimulationReady) {
163- if (simData.atcIdDataPtr ->data ().atcID [0 ] != ' \0 ' ) {
164- atcId = simData.atcIdDataPtr ->data ().atcID ;
165- LOG_INFO (" Fadec::EngineControl_A32NX::ensureFadecIsInitialized() - received ATC ID: " + atcId);
166- initializeFuelTanks (simTime, tickCounter);
167- } else {
168- LOG_INFO (" Fadec::EngineControl_A32NX::ensureFadecIsInitialized() - no ATC ID received, taking default: " + atcId);
169- }
170- // if ATC ID is empty, we take the default and still set hasLoadedFuelConfig to as it won't change anymore
171- hasLoadedFuelConfig = true ;
172- }
173- }
174-
175- #ifdef PROFILING
176- profilerEnsureFadecIsInitialized.stop ();
177- if (msfsHandlerPtr->getTickCounter () % 100 == 0 ) {
178- profilerEnsureFadecIsInitialized.print ();
179- }
180- #endif
181- }
182154/* *
183155 * @brief Initializes the engine control data.
184156 *
@@ -237,26 +209,7 @@ void EngineControl_A32NX::initializeEngineControlData() {
237209 simData.engineTimer [L]->set (0 );
238210 simData.engineTimer [R]->set (0 );
239211
240- initializeFuelTanks (timeStamp, tickCounter);
241-
242- // Initialize Pump State
243- simData.fuelPumpState [L]->set (0 );
244- simData.fuelPumpState [R]->set (0 );
245-
246- // Initialize Thrust Limits
247- simData.thrustLimitIdle ->set (0 );
248- simData.thrustLimitClimb ->set (0 );
249- simData.thrustLimitFlex ->set (0 );
250- simData.thrustLimitMct ->set (0 );
251- simData.thrustLimitToga ->set (0 );
252- }
253-
254- void EngineControl_A32NX::initializeFuelTanks (FLOAT64 timeStamp, UINT64 tickCounter) {
255- LOG_INFO (" Fadec::EngineControl_A32NX::initializeFuelTanks()" );
256-
257- #ifdef PROFILING
258- ScopedTimer timer (" Fadec::EngineControl_A32NX::initializeFuelTanks()" );
259- #endif
212+ // Initialize Fuel Tanks
260213 const double fuelWeightGallon = simData.simVarsDataPtr ->data ().fuelWeightPerGallon ; // weight of gallon of jet A in lbs
261214
262215 const double centerQuantity = simData.simVarsDataPtr ->data ().fuelTankQuantityCenter ; // gal
@@ -265,10 +218,6 @@ void EngineControl_A32NX::initializeFuelTanks(FLOAT64 timeStamp, UINT64 tickCoun
265218 const double leftAuxQuantity = simData.simVarsDataPtr ->data ().fuelTankQuantityLeftAux ; // gal
266219 const double rightAuxQuantity = simData.simVarsDataPtr ->data ().fuelTankQuantityRightAux ; // gal
267220
268- LOG_INFO (" Fadec::EngineControl_A32NX::initializeFuelTanks() - Current Fuel Levels from Sim:\n Center: " + std::to_string (centerQuantity) +
269- " gal\n Left: " + std::to_string (leftQuantity) + " gal\n Right: " + std::to_string (rightQuantity) +
270- " gal\n Left Aux: " + std::to_string (leftAuxQuantity) + " gal\n Right Aux: " + std::to_string (rightAuxQuantity) + " gal" );
271-
272221 // only loads saved fuel quantity on C/D spawn
273222 if (simData.startState ->updateFromSim (timeStamp, tickCounter) == 2 ) {
274223 // Load fuel configuration from file
@@ -298,6 +247,17 @@ void EngineControl_A32NX::initializeFuelTanks(FLOAT64 timeStamp, UINT64 tickCoun
298247 simData.fuelAuxLeftPre ->set (leftAuxQuantity * fuelWeightGallon); // in Pounds
299248 simData.fuelAuxRightPre ->set (rightAuxQuantity * fuelWeightGallon); // in Pounds
300249 }
250+
251+ // Initialize Pump State
252+ simData.fuelPumpState [L]->set (0 );
253+ simData.fuelPumpState [R]->set (0 );
254+
255+ // Initialize Thrust Limits
256+ simData.thrustLimitIdle ->set (0 );
257+ simData.thrustLimitClimb ->set (0 );
258+ simData.thrustLimitFlex ->set (0 );
259+ simData.thrustLimitMct ->set (0 );
260+ simData.thrustLimitToga ->set (0 );
301261}
302262
303263double EngineControl_A32NX::generateEngineImbalance () {
@@ -1013,11 +973,8 @@ void EngineControl_A32NX::updateFuel(double deltaTimeSeconds) {
1013973
1014974 // --------------------------------------------
1015975 // Will save the current fuel quantities at a certain interval
1016- // if the simulation is ready
1017- // and the aircraft is on the ground and the engines are off/shutting down
1018-
1019- if (msfsHandlerPtr->getAircraftIsReadyVar () && msfsHandlerPtr->getSimOnGround () &&
1020- (msfsHandlerPtr->getSimulationTime () - lastFuelSaveTime) > FUEL_SAVE_INTERVAL &&
976+ // if the aircraft is on the ground and the engines are off/shutting down
977+ if (msfsHandlerPtr->getSimOnGround () && (msfsHandlerPtr->getSimulationTime () - lastFuelSaveTime) > FUEL_SAVE_INTERVAL &&
1021978 (engine1State == OFF || engine1State == SHUTTING || engine2State == OFF || engine2State == SHUTTING)) {
1022979 fuelConfiguration.setFuelLeft (simData.fuelLeftPre ->get () / weightLbsPerGallon);
1023980 fuelConfiguration.setFuelRight (simData.fuelRightPre ->get () / weightLbsPerGallon);
0 commit comments