first commit
This commit is contained in:
@@ -0,0 +1,24 @@
|
||||
set(TESTSUITE_SOURCES
|
||||
${TESTSUITE_SOURCES}
|
||||
${CMAKE_CURRENT_SOURCE_DIR}/testGlobals.cxx
|
||||
${CMAKE_CURRENT_SOURCE_DIR}/NavDataCache.cxx
|
||||
${CMAKE_CURRENT_SOURCE_DIR}/PrivateAccessorFDM.cxx
|
||||
${CMAKE_CURRENT_SOURCE_DIR}/scene_graph.cxx
|
||||
${CMAKE_CURRENT_SOURCE_DIR}/TestPilot.cxx
|
||||
${CMAKE_CURRENT_SOURCE_DIR}/NasalUnitTesting_TestSuite.cxx
|
||||
${CMAKE_CURRENT_SOURCE_DIR}/TestDataLogger.cxx
|
||||
${CMAKE_CURRENT_SOURCE_DIR}/testApis.cxx
|
||||
PARENT_SCOPE
|
||||
)
|
||||
|
||||
|
||||
set(TESTSUITE_HEADERS
|
||||
${TESTSUITE_HEADERS}
|
||||
${CMAKE_CURRENT_SOURCE_DIR}/testGlobals.hxx
|
||||
${CMAKE_CURRENT_SOURCE_DIR}/NavDataCache.hxx
|
||||
${CMAKE_CURRENT_SOURCE_DIR}/PrivateAccessorFDM.hxx
|
||||
${CMAKE_CURRENT_SOURCE_DIR}/scene_graph.hxx
|
||||
${CMAKE_CURRENT_SOURCE_DIR}/TestPilot.hxx
|
||||
${CMAKE_CURRENT_SOURCE_DIR}/TestDataLogger.hxx
|
||||
PARENT_SCOPE
|
||||
)
|
||||
@@ -0,0 +1,131 @@
|
||||
// Unit-test API for nasal
|
||||
//
|
||||
// There are two versions of this module, and we load one or the other
|
||||
// depending on if we're running the test_suite (using CppUnit) or
|
||||
// the normal simulator. The logic is that aircraft-developers and
|
||||
// people hacking Nasal likely don't have a way to run the test-suite,
|
||||
// whereas core-developers and Jenksin want a way to run all tests
|
||||
// through the standard CppUnit mechanim. So we have a consistent
|
||||
// Nasal API, but different implement in fgfs_test_suite vs
|
||||
// normal fgfs executable.
|
||||
//
|
||||
// Copyright (C) 2020 James Turner
|
||||
//
|
||||
// This program is free software; you can redistribute it and/or
|
||||
// modify it under the terms of the GNU General Public License as
|
||||
// published by the Free Software Foundation; either version 2 of the
|
||||
// License, or (at your option) any later version.
|
||||
//
|
||||
// This program is distributed in the hope that it will be useful, but
|
||||
// WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||
// General Public License for more details.
|
||||
//
|
||||
// You should have received a copy of the GNU General Public License
|
||||
// along with this program; if not, write to the Free Software
|
||||
// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
||||
|
||||
#include "config.h"
|
||||
|
||||
#include <Main/globals.hxx>
|
||||
#include <Main/util.hxx>
|
||||
|
||||
#include <Scripting/NasalSys.hxx>
|
||||
#include <Scripting/NasalSys_private.hxx>
|
||||
|
||||
#include <cppunit/TestAssert.h>
|
||||
|
||||
#include <simgear/nasal/cppbind/from_nasal.hxx>
|
||||
#include <simgear/nasal/cppbind/to_nasal.hxx>
|
||||
#include <simgear/nasal/cppbind/NasalHash.hxx>
|
||||
#include <simgear/nasal/cppbind/Ghost.hxx>
|
||||
|
||||
static CppUnit::SourceLine nasalSourceLine(const nasal::CallContext& ctx)
|
||||
{
|
||||
const string fileName = ctx.from_nasal<string>(naGetSourceFile(ctx.c_ctx(), 0));
|
||||
const int lineNumber = naGetLine(ctx.c_ctx(), 0);
|
||||
return CppUnit::SourceLine(fileName, lineNumber);
|
||||
}
|
||||
|
||||
static naRef f_assert(const nasal::CallContext& ctx )
|
||||
{
|
||||
bool pass = ctx.requireArg<bool>(0);
|
||||
auto msg = ctx.getArg<string>(1, "assert failed:");
|
||||
|
||||
CppUnit::Asserter::failIf(!pass, "assertion failed:" + msg, nasalSourceLine(ctx));
|
||||
return naNil();
|
||||
}
|
||||
|
||||
static naRef f_fail(const nasal::CallContext& ctx )
|
||||
{
|
||||
auto msg = ctx.getArg<string>(0);
|
||||
|
||||
CppUnit::Asserter::fail("assertion failed:" + msg,
|
||||
nasalSourceLine(ctx));
|
||||
return naNil();
|
||||
}
|
||||
|
||||
static naRef f_assert_equal(const nasal::CallContext& ctx )
|
||||
{
|
||||
naRef argA = ctx.requireArg<naRef>(0);
|
||||
naRef argB = ctx.requireArg<naRef>(1);
|
||||
auto msg = ctx.getArg<string>(2, "assert_equal failed");
|
||||
|
||||
bool same = nasalStructEqual(ctx.c_ctx(), argA, argB);
|
||||
if (!same) {
|
||||
|
||||
string aStr = ctx.from_nasal<string>(argA);
|
||||
string bStr = ctx.from_nasal<string>(argB);
|
||||
msg += "; expected:" + aStr + ", actual:" + bStr;
|
||||
|
||||
CppUnit::Asserter::fail(msg, nasalSourceLine(ctx));
|
||||
}
|
||||
|
||||
return naNil();
|
||||
}
|
||||
|
||||
static naRef f_equal(const nasal::CallContext& ctx)
|
||||
{
|
||||
naRef argA = ctx.requireArg<naRef>(0);
|
||||
naRef argB = ctx.requireArg<naRef>(1);
|
||||
|
||||
bool same = nasalStructEqual(ctx.c_ctx(), argA, argB);
|
||||
return naNum(same);
|
||||
}
|
||||
|
||||
static naRef f_assert_doubles_equal(const nasal::CallContext& ctx )
|
||||
{
|
||||
double argA = ctx.requireArg<double>(0);
|
||||
double argB = ctx.requireArg<double>(1);
|
||||
double tolerance = ctx.requireArg<double>(2);
|
||||
|
||||
auto msg = ctx.getArg<string>(3, "assert_doubles_equal failed");
|
||||
|
||||
const bool same = fabs(argA - argB) < tolerance;
|
||||
if (!same) {
|
||||
msg += "; expected:" + std::to_string(argA) + ", actual:" + std::to_string(argB);
|
||||
CppUnit::Asserter::fail(msg, nasalSourceLine(ctx));
|
||||
}
|
||||
|
||||
return naNil();
|
||||
}
|
||||
|
||||
//------------------------------------------------------------------------------
|
||||
naRef initNasalUnitTestCppUnit(naRef nasalGlobals, naContext c)
|
||||
{
|
||||
nasal::Hash globals_module(nasalGlobals, c),
|
||||
unitTest = globals_module.createHash("unitTest");
|
||||
|
||||
unitTest.set("assert", f_assert);
|
||||
unitTest.set("fail", f_fail);
|
||||
unitTest.set("assert_equal", f_assert_equal);
|
||||
unitTest.set("equal", f_equal);
|
||||
unitTest.set("assert_doubles_equal", f_assert_doubles_equal);
|
||||
|
||||
return naNil();
|
||||
}
|
||||
|
||||
void shutdownNasalUnitTestInSim()
|
||||
{
|
||||
// stub
|
||||
}
|
||||
@@ -0,0 +1,48 @@
|
||||
/*
|
||||
* Copyright (C) 2018 Edward d'Auvergne
|
||||
*
|
||||
* This file is part of the program FlightGear.
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 2 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
|
||||
#include "NavDataCache.hxx"
|
||||
|
||||
#include <Navaids/NavDataCache.hxx>
|
||||
|
||||
|
||||
namespace FGTestApi {
|
||||
|
||||
namespace setUp {
|
||||
|
||||
void initNavDataCache()
|
||||
{
|
||||
if (flightgear::NavDataCache::instance())
|
||||
return;
|
||||
|
||||
flightgear::NavDataCache* cache = flightgear::NavDataCache::createInstance();
|
||||
if (cache->isRebuildRequired()) {
|
||||
std::cerr << "Navcache rebuild for testing" << std::flush;
|
||||
|
||||
while (cache->rebuild() != flightgear::NavDataCache::REBUILD_DONE) {
|
||||
SGTimeStamp::sleepForMSec(1000);
|
||||
std::cerr << "." << std::flush;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
} // End of namespace setUp.
|
||||
|
||||
} // End of namespace FGTestApi.
|
||||
@@ -0,0 +1,35 @@
|
||||
/*
|
||||
* Copyright (C) 2018 Edward d'Auvergne
|
||||
*
|
||||
* This file is part of the program FlightGear.
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 2 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
|
||||
#ifndef FG_NAV_DATA_CACHE_TEST_HELPERS_HXX
|
||||
#define FG_NAV_DATA_CACHE_TEST_HELPERS_HXX
|
||||
|
||||
namespace FGTestApi {
|
||||
|
||||
namespace setUp {
|
||||
|
||||
|
||||
void initNavDataCache();
|
||||
|
||||
} // End of namespace setUp.
|
||||
|
||||
} // End of namespace FGTestApi.
|
||||
|
||||
#endif // of FG_NAV_DATA_CACHE_TEST_HELPERS_HXX
|
||||
@@ -0,0 +1,63 @@
|
||||
#include "PrivateAccessorFDM.hxx"
|
||||
|
||||
#include <FDM/AIWake/AIWakeGroup.hxx>
|
||||
#include <FDM/AIWake/AircraftMesh.hxx>
|
||||
#include <FDM/AIWake/WakeMesh.hxx>
|
||||
#include <FDM/YASim/Atmosphere.hpp>
|
||||
|
||||
|
||||
|
||||
// Access variables from src/FDM/AIWake/AIWakeGroup.hxx.
|
||||
WakeMesh*
|
||||
FGTestApi::PrivateAccessor::FDM::Accessor::read_FDM_AIWake_AIWakeGroup_aiWakeData(AIWakeGroup* instance, int i)
|
||||
{
|
||||
return instance->_aiWakeData[i].mesh;
|
||||
}
|
||||
|
||||
|
||||
// Access variables from src/FDM/AIWake/AircraftMesh.hxx.
|
||||
const std::vector<SGVec3d>
|
||||
FGTestApi::PrivateAccessor::FDM::Accessor::read_FDM_AIWake_AircraftMesh_collPt(AircraftMesh* instance) const
|
||||
{
|
||||
return instance->collPt;
|
||||
}
|
||||
|
||||
const std::vector<SGVec3d>
|
||||
FGTestApi::PrivateAccessor::FDM::Accessor::read_FDM_AIWake_AircraftMesh_midPt(AircraftMesh* instance) const
|
||||
{
|
||||
return instance->midPt;
|
||||
}
|
||||
|
||||
|
||||
// Access variables from src/FDM/AIWake/WakeMesh.hxx.
|
||||
const std::vector<AeroElement_ptr>
|
||||
FGTestApi::PrivateAccessor::FDM::Accessor::read_FDM_AIWake_WakeMesh_elements(WakeMesh* instance) const
|
||||
{
|
||||
return instance->elements;
|
||||
}
|
||||
|
||||
int
|
||||
FGTestApi::PrivateAccessor::FDM::Accessor::read_FDM_AIWake_WakeMesh_nelm(WakeMesh* instance) const
|
||||
{
|
||||
return instance->nelm;
|
||||
}
|
||||
|
||||
double **
|
||||
FGTestApi::PrivateAccessor::FDM::Accessor::read_FDM_AIWake_WakeMesh_Gamma(WakeMesh* instance) const
|
||||
{
|
||||
return instance->Gamma;
|
||||
}
|
||||
|
||||
|
||||
// Access variables from src/FDM/YASim/Atmosphere.hxx.
|
||||
float
|
||||
FGTestApi::PrivateAccessor::FDM::Accessor::read_FDM_YASim_Atmosphere_numColumns(std::unique_ptr<yasim::Atmosphere> &instance) const
|
||||
{
|
||||
return instance->numColumns;
|
||||
}
|
||||
|
||||
float
|
||||
FGTestApi::PrivateAccessor::FDM::Accessor::read_FDM_YASim_Atmosphere_data(std::unique_ptr<yasim::Atmosphere> &instance, int i, int j) const
|
||||
{
|
||||
return instance->data[i][j];
|
||||
}
|
||||
@@ -0,0 +1,53 @@
|
||||
#ifndef _FG_PRIVATE_ACCESSOR_FDM_HXX
|
||||
#define _FG_PRIVATE_ACCESSOR_FDM_HXX
|
||||
|
||||
#include <map>
|
||||
#include <memory>
|
||||
#include <vector>
|
||||
|
||||
#include <simgear/math/SGMath.hxx>
|
||||
#include <simgear/structure/SGSharedPtr.hxx>
|
||||
|
||||
// Forward declarations for src/FDM/AIWake.
|
||||
class AIWakeData;
|
||||
class AIWakeGroup;
|
||||
class AircraftMesh;
|
||||
class WakeMesh;
|
||||
class AeroElement;
|
||||
typedef SGSharedPtr<AeroElement> AeroElement_ptr;
|
||||
|
||||
// Forward declaration for: src/FDM/YASim.
|
||||
namespace yasim {
|
||||
class Atmosphere;
|
||||
}
|
||||
|
||||
|
||||
namespace FGTestApi {
|
||||
namespace PrivateAccessor {
|
||||
namespace FDM {
|
||||
|
||||
class Accessor
|
||||
{
|
||||
public:
|
||||
// Access variables from src/FDM/AIWake/AIWakeGroup.hxx.
|
||||
WakeMesh* read_FDM_AIWake_AIWakeGroup_aiWakeData(AIWakeGroup* instance, int i);
|
||||
|
||||
// Access variables from src/FDM/AIWake/AircraftMesh.hxx.
|
||||
const std::vector<SGVec3d> read_FDM_AIWake_AircraftMesh_collPt(AircraftMesh* instance) const;
|
||||
const std::vector<SGVec3d> read_FDM_AIWake_AircraftMesh_midPt(AircraftMesh* instance) const;
|
||||
|
||||
// Access variables from src/FDM/AIWake/WakeMesh.hxx.
|
||||
const std::vector<AeroElement_ptr> read_FDM_AIWake_WakeMesh_elements(WakeMesh* instance) const;
|
||||
int read_FDM_AIWake_WakeMesh_nelm(WakeMesh* instance) const;
|
||||
double **read_FDM_AIWake_WakeMesh_Gamma(WakeMesh* instance) const;
|
||||
|
||||
// Access variables from src/FDM/YASim/Atmosphere.hxx.
|
||||
float read_FDM_YASim_Atmosphere_numColumns(std::unique_ptr<yasim::Atmosphere> &instance) const;
|
||||
float read_FDM_YASim_Atmosphere_data(std::unique_ptr<yasim::Atmosphere> &instance, int i, int j) const;
|
||||
};
|
||||
|
||||
} // End of namespace FDM.
|
||||
} // End of namespace PrivateAccessor.
|
||||
} // End of namespace FGTestApi.
|
||||
|
||||
#endif // _FG_PRIVATE_ACCESSOR_FDM_HXX
|
||||
@@ -0,0 +1,170 @@
|
||||
/*
|
||||
* Copyright (C) 2020 James Turner
|
||||
*
|
||||
* This file is part of the program FlightGear.
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 2 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
#include "TestDataLogger.hxx"
|
||||
|
||||
#include <algorithm>
|
||||
#include <cmath>
|
||||
#include <limits>
|
||||
#include <vector>
|
||||
|
||||
#include <simgear/io/iostreams/sgstream.hxx>
|
||||
|
||||
#include "Main/globals.hxx"
|
||||
|
||||
namespace FGTestApi {
|
||||
|
||||
using DoubleVec = std::vector<double>;
|
||||
|
||||
static std::unique_ptr<DataLogger> static_instance;
|
||||
|
||||
class DataLogger::DataLoggerPrivate
|
||||
{
|
||||
public:
|
||||
sg_ofstream _stream;
|
||||
|
||||
struct SampleInfo {
|
||||
int column;
|
||||
std::string name;
|
||||
// range / units info, later
|
||||
SGPropertyNode_ptr property;
|
||||
};
|
||||
|
||||
double _currentTimeBase;
|
||||
std::vector<SampleInfo> _samples;
|
||||
DoubleVec _openRow;
|
||||
bool _didHeader = false;
|
||||
|
||||
void writeCurrentRow()
|
||||
{
|
||||
if (!_didHeader) {
|
||||
writeHeader();
|
||||
_didHeader = true;
|
||||
}
|
||||
|
||||
// capture property values into the open row data
|
||||
std::for_each(_samples.begin(), _samples.end(), [this](const SampleInfo& info) {
|
||||
if (info.property) {
|
||||
_openRow[info.column] = info.property->getDoubleValue();
|
||||
}
|
||||
});
|
||||
|
||||
// write time base
|
||||
_stream << globals->get_sim_time_sec() << ",";
|
||||
|
||||
for (const auto v : _openRow) {
|
||||
if (std::isnan(v)) {
|
||||
_stream << ","; // skip this data point
|
||||
} else {
|
||||
_stream << v << ",";
|
||||
}
|
||||
}
|
||||
|
||||
_stream << "\n";
|
||||
|
||||
std::fill(_openRow.begin(), _openRow.end(), std::numeric_limits<double>::quiet_NaN());
|
||||
}
|
||||
|
||||
void writeHeader()
|
||||
{
|
||||
_stream << "sim-time, ";
|
||||
std::for_each(_samples.begin(), _samples.end(), [this](const SampleInfo& info) {
|
||||
_stream << info.name << ", ";
|
||||
});
|
||||
|
||||
_stream << "\n";
|
||||
}
|
||||
};
|
||||
|
||||
DataLogger::DataLogger()
|
||||
{
|
||||
d.reset(new DataLoggerPrivate);
|
||||
}
|
||||
|
||||
DataLogger::~DataLogger()
|
||||
{
|
||||
d->_stream.close();
|
||||
}
|
||||
|
||||
bool DataLogger::isActive()
|
||||
{
|
||||
return static_instance != nullptr;
|
||||
}
|
||||
|
||||
DataLogger* DataLogger::instance()
|
||||
{
|
||||
if (!static_instance) {
|
||||
static_instance.reset(new DataLogger);
|
||||
}
|
||||
|
||||
return static_instance.get();
|
||||
}
|
||||
|
||||
void DataLogger::initTest(const std::string& testName)
|
||||
{
|
||||
d->_stream = sg_ofstream(testName + "_trace.csv");
|
||||
}
|
||||
|
||||
void DataLogger::tearDown()
|
||||
{
|
||||
if (static_instance) {
|
||||
static_instance.reset();
|
||||
}
|
||||
}
|
||||
|
||||
void DataLogger::writeRecord()
|
||||
{
|
||||
d->writeCurrentRow();
|
||||
}
|
||||
|
||||
void DataLogger::recordProperty(const std::string& name, SGPropertyNode_ptr prop)
|
||||
{
|
||||
int index = static_cast<int>(d->_samples.size());
|
||||
DataLoggerPrivate::SampleInfo info{index, name, prop};
|
||||
d->_samples.push_back(info);
|
||||
|
||||
if (d->_openRow.size() <= index) {
|
||||
d->_openRow.resize(index + 1, std::numeric_limits<double>::quiet_NaN());
|
||||
}
|
||||
}
|
||||
|
||||
void DataLogger::recordSamplePoint(const std::string& name, double value)
|
||||
{
|
||||
auto it = std::find_if(d->_samples.begin(), d->_samples.end(), [&name](const DataLoggerPrivate::SampleInfo& sample) {
|
||||
return name == sample.name;
|
||||
});
|
||||
|
||||
int index = 0;
|
||||
if (it == d->_samples.end()) {
|
||||
index = static_cast<int>(d->_samples.size());
|
||||
DataLoggerPrivate::SampleInfo info{index, name};
|
||||
d->_samples.push_back(info);
|
||||
} else {
|
||||
index = it->column;
|
||||
}
|
||||
|
||||
// grow _openRow as required
|
||||
if (d->_openRow.size() <= index) {
|
||||
d->_openRow.resize(index + 1, std::numeric_limits<double>::quiet_NaN());
|
||||
}
|
||||
|
||||
d->_openRow[index] = value;
|
||||
}
|
||||
|
||||
} // namespace FGTestApi
|
||||
@@ -0,0 +1,57 @@
|
||||
/*
|
||||
* Copyright (C) 2020 James Turner
|
||||
*
|
||||
* This file is part of the program FlightGear.
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 2 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <memory>
|
||||
#include <string>
|
||||
|
||||
#include <simgear/props/props.hxx>
|
||||
|
||||
namespace FGTestApi {
|
||||
|
||||
class DataLogger
|
||||
{
|
||||
public:
|
||||
~DataLogger();
|
||||
|
||||
static DataLogger* instance();
|
||||
static bool isActive();
|
||||
|
||||
void initTest(const std::string& testName);
|
||||
|
||||
void recordProperty(const std::string& name, SGPropertyNode_ptr prop);
|
||||
|
||||
void setUp();
|
||||
|
||||
void tearDown();
|
||||
|
||||
void recordSamplePoint(const std::string& name, double value);
|
||||
|
||||
void writeRecord();
|
||||
|
||||
private:
|
||||
DataLogger();
|
||||
|
||||
class DataLoggerPrivate;
|
||||
std::unique_ptr<DataLoggerPrivate> d;
|
||||
};
|
||||
|
||||
|
||||
} // namespace FGTestApi
|
||||
@@ -0,0 +1,282 @@
|
||||
/*
|
||||
* Copyright (C) 2019 James Turner
|
||||
*
|
||||
* This file is part of the program FlightGear.
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 2 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
#include "TestPilot.hxx"
|
||||
|
||||
#include <algorithm>
|
||||
|
||||
#include <simgear/math/SGGeodesy.hxx>
|
||||
#include <simgear/props/props.hxx>
|
||||
#include <simgear/math/SGGeod.hxx>
|
||||
|
||||
#include <Aircraft/AircraftPerformance.hxx> // for formulae
|
||||
#include <Main/globals.hxx>
|
||||
|
||||
#include "TestDataLogger.hxx"
|
||||
|
||||
namespace FGTestApi {
|
||||
|
||||
TestPilot::TestPilot(SGPropertyNode_ptr props) :
|
||||
_propRoot(props)
|
||||
{
|
||||
if (!_propRoot) {
|
||||
// use default properties
|
||||
_propRoot = globals->get_props();
|
||||
}
|
||||
|
||||
_latProp = _propRoot->getNode("position/latitude-deg", true);
|
||||
_lonProp = _propRoot->getNode("position/longitude-deg", true);
|
||||
_altitudeProp = _propRoot->getNode("position/altitude-ft", true);
|
||||
_headingProp = _propRoot->getNode("orientation/heading-deg", true);
|
||||
_speedKnotsProp = _propRoot->getNode("velocities/airspeed-kt", true);
|
||||
_speedMachProp = _propRoot->getNode("velocities/mach", true);
|
||||
_groundspeedKnotsProp = _propRoot->getNode("velocities/groundspeed-kt", true);
|
||||
_verticalFPMProp = _propRoot->getNode("velocities/vertical-fpm", true);
|
||||
|
||||
globals->add_subsystem("flight", this, SGSubsystemMgr::FDM);
|
||||
}
|
||||
|
||||
TestPilot::~TestPilot()
|
||||
{
|
||||
}
|
||||
|
||||
void TestPilot::resetAtPosition(const SGGeod& pos)
|
||||
{
|
||||
_turnActive = false;
|
||||
setPosition(pos);
|
||||
}
|
||||
|
||||
void TestPilot::init()
|
||||
{
|
||||
_vspeedFPM = 1200;
|
||||
}
|
||||
|
||||
void TestPilot::update(double dt)
|
||||
{
|
||||
updateValues(dt);
|
||||
}
|
||||
|
||||
void TestPilot::setSpeedKts(double knots)
|
||||
{
|
||||
_speedKnots = knots;
|
||||
}
|
||||
|
||||
void TestPilot::setVerticalFPM(double fpm)
|
||||
{
|
||||
_vspeedFPM = fpm;
|
||||
}
|
||||
|
||||
void TestPilot::setCourseTrue(double deg)
|
||||
{
|
||||
_trueCourseDeg = deg;
|
||||
}
|
||||
|
||||
void TestPilot::turnToCourse(double deg)
|
||||
{
|
||||
_turnActive = true;
|
||||
_targetCourseDeg = deg;
|
||||
}
|
||||
|
||||
void TestPilot::flyHeading(double hdg)
|
||||
{
|
||||
_lateralMode = LateralMode::Heading;
|
||||
_turnActive = true;
|
||||
_targetCourseDeg = hdg;
|
||||
}
|
||||
|
||||
void TestPilot::flyGPSCourse(GPS *gps)
|
||||
{
|
||||
_gps = gps;
|
||||
_gpsNode = globals->get_props()->getNode("instrumentation/gps");
|
||||
_gpsLegCourse = _gpsNode->getNode("wp/leg-true-course-deg", true);
|
||||
_courseErrorNm = _gpsNode->getNode("wp/wp[1]/course-error-nm", true);
|
||||
|
||||
_lateralMode = LateralMode::GPSCourse;
|
||||
_turnActive = false;
|
||||
}
|
||||
|
||||
void TestPilot::flyGPSCourseOffset(GPS *gps, double offsetNm)
|
||||
{
|
||||
_gps = gps;
|
||||
_gpsNode = globals->get_props()->getNode("instrumentation/gps");
|
||||
_gpsLegCourse = _gpsNode->getNode("wp/leg-true-course-deg", true);
|
||||
_courseErrorNm = _gpsNode->getNode("wp/wp[1]/course-error-nm", true);
|
||||
|
||||
_lateralMode = LateralMode::GPSOffset;
|
||||
_courseOffsetNm = offsetNm;
|
||||
_turnActive = false;
|
||||
}
|
||||
|
||||
void TestPilot::flyDirectTo(const SGGeod& target)
|
||||
{
|
||||
_lateralMode = LateralMode::Direct;
|
||||
_targetPos = target;
|
||||
}
|
||||
|
||||
void TestPilot::updateValues(double dt)
|
||||
{
|
||||
auto dl = DataLogger::instance();
|
||||
|
||||
if (_gps && (_lateralMode == LateralMode::GPSCourse)) {
|
||||
_targetCourseDeg = _gpsLegCourse->getDoubleValue();
|
||||
|
||||
// set how aggressively we try to correct our course
|
||||
double courseCorrectionFactor = 64.0;
|
||||
double crossTrack = _courseErrorNm->getDoubleValue();
|
||||
|
||||
dl->recordSamplePoint("TP-error-nm", crossTrack);
|
||||
|
||||
SG_CLAMP_RANGE(crossTrack, -2.0, 2.0); // clamp to 2nm deviation
|
||||
double correction = courseCorrectionFactor * crossTrack;
|
||||
const double maxCorrectionAngle = 45;
|
||||
|
||||
dl->recordSamplePoint("TP-base-correction-deg", correction);
|
||||
|
||||
// within 1nm of the desired course, start to bias
|
||||
// based on heading error. This is to reduce overshooting
|
||||
// while still keeping the responsiveness high
|
||||
if (fabs(crossTrack) < 1.0) {
|
||||
// compensate for heading
|
||||
double headingError = _targetCourseDeg - _trueCourseDeg;
|
||||
SG_NORMALIZE_RANGE(headingError, -180.0, 180.0);
|
||||
if (fabs(headingError) > 90.0) {
|
||||
// we're pointing the wrong way, don't compensate
|
||||
// otherwise we get into knots trying to make the
|
||||
// turn back the right way
|
||||
} else {
|
||||
const double p = 1.0 - fabs(crossTrack);
|
||||
const double headingErrorFactor = 0.6;
|
||||
correction += p * headingError * headingErrorFactor;
|
||||
}
|
||||
}
|
||||
|
||||
dl->recordSamplePoint("TP-correction-deg", correction);
|
||||
|
||||
SG_CLAMP_RANGE(correction, -maxCorrectionAngle, maxCorrectionAngle);
|
||||
_targetCourseDeg += correction;
|
||||
|
||||
dl->recordSamplePoint("TP-target-deg", _targetCourseDeg);
|
||||
|
||||
SG_NORMALIZE_RANGE(_targetCourseDeg, 0.0, 360.0);
|
||||
if (!_turnActive &&(fabs(_trueCourseDeg - _targetCourseDeg) > 0.5)) {
|
||||
_turnActive = true;
|
||||
}
|
||||
}
|
||||
|
||||
if (_gps && (_lateralMode == LateralMode::GPSOffset)) {
|
||||
_targetCourseDeg = _gpsLegCourse->getDoubleValue();
|
||||
|
||||
double crossTrack = _courseErrorNm->getDoubleValue();
|
||||
double offsetError = crossTrack - _courseOffsetNm;
|
||||
|
||||
const double offsetCorrectionFactor = 25.0;
|
||||
const double correction = offsetError * offsetCorrectionFactor;
|
||||
_targetCourseDeg += correction;
|
||||
|
||||
SG_NORMALIZE_RANGE(_targetCourseDeg, 0.0, 360.0);
|
||||
if (!_turnActive &&(fabs(_trueCourseDeg - _targetCourseDeg) > 0.5)) {
|
||||
_turnActive = true;
|
||||
}
|
||||
}
|
||||
|
||||
if (_lateralMode == LateralMode::Direct) {
|
||||
_targetCourseDeg = SGGeodesy::courseDeg(globals->get_aircraft_position(), _targetPos);
|
||||
SG_NORMALIZE_RANGE(_targetCourseDeg, 0.0, 360.0);
|
||||
if (!_turnActive && (fabs(_trueCourseDeg - _targetCourseDeg) > 0.5)) {
|
||||
_turnActive = true;
|
||||
}
|
||||
}
|
||||
|
||||
if (_turnActive) {
|
||||
if (fabs(_targetCourseDeg - _trueCourseDeg) < 0.1) {
|
||||
_trueCourseDeg = _targetCourseDeg;
|
||||
_turnActive = false;
|
||||
} else {
|
||||
// standard 2-minute turn, 180-deg min, thus 3-degrees per second
|
||||
|
||||
double turnDeg = 5.0 * dt;
|
||||
double errorDeg = _targetCourseDeg - _trueCourseDeg;
|
||||
SG_NORMALIZE_RANGE(errorDeg, -180.0, 180.0);
|
||||
|
||||
// clamp turn to error value
|
||||
turnDeg = std::min(turnDeg, fabs(errorDeg));
|
||||
|
||||
// and now ensure we follow the correct sign
|
||||
turnDeg = copysign(turnDeg, errorDeg);
|
||||
|
||||
// simple integral
|
||||
_trueCourseDeg += turnDeg;
|
||||
SG_NORMALIZE_RANGE(_trueCourseDeg, 0.0, 360.0);
|
||||
}
|
||||
}
|
||||
|
||||
SGGeod currentPos = globals->get_aircraft_position();
|
||||
|
||||
const double M = flightgear::AircraftPerformance::machForCAS(currentPos.getElevationFt(), _speedKnots);
|
||||
_speedMachProp->setDoubleValue(M);
|
||||
const double gs = flightgear::AircraftPerformance::groundSpeedForMach(currentPos.getElevationFt(), M);
|
||||
_groundspeedKnotsProp->setDoubleValue(gs);
|
||||
double d = gs * SG_KT_TO_MPS * dt;
|
||||
SGGeod newPos = SGGeodesy::direct(currentPos, _trueCourseDeg, d);
|
||||
|
||||
if (_altActive) {
|
||||
if (fabs(_targetAltitudeFt - currentPos.getElevationFt()) < 1) {
|
||||
_altActive = false;
|
||||
newPos.setElevationFt(_targetAltitudeFt);
|
||||
} else {
|
||||
double errorFt = _targetAltitudeFt - currentPos.getElevationFt();
|
||||
double vspeed = std::min(fabs(errorFt),_vspeedFPM * dt / 60.0);
|
||||
double dv = copysign(vspeed, errorFt);
|
||||
newPos.setElevationFt(currentPos.getElevationFt() + dv);
|
||||
}
|
||||
}
|
||||
|
||||
setPosition(newPos);
|
||||
}
|
||||
|
||||
void TestPilot::setPosition(const SGGeod& pos)
|
||||
{
|
||||
_latProp->setDoubleValue(pos.getLatitudeDeg());
|
||||
_lonProp->setDoubleValue(pos.getLongitudeDeg());
|
||||
_altitudeProp->setDoubleValue(pos.getElevationFt());
|
||||
|
||||
_headingProp->setDoubleValue(_trueCourseDeg);
|
||||
_speedKnotsProp->setDoubleValue(_speedKnots);
|
||||
_verticalFPMProp->setDoubleValue(_vspeedFPM);
|
||||
}
|
||||
|
||||
void TestPilot::setTargetAltitudeFtMSL(double altFt)
|
||||
{
|
||||
_targetAltitudeFt = altFt;
|
||||
_altActive = true;
|
||||
}
|
||||
|
||||
bool TestPilot::isOnHeading(double heading) const
|
||||
{
|
||||
const double hdgDelta = (_trueCourseDeg - heading);
|
||||
return fabs(hdgDelta) < 0.5;
|
||||
}
|
||||
|
||||
double TestPilot::trueCourseDeg() const
|
||||
{
|
||||
return _trueCourseDeg;
|
||||
}
|
||||
|
||||
|
||||
} // of namespace
|
||||
@@ -0,0 +1,112 @@
|
||||
/*
|
||||
* Copyright (C) 2019 James Turner
|
||||
*
|
||||
* This file is part of the program FlightGear.
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 2 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
#ifndef _FGTEST_API_TEST_PILOT_HXX
|
||||
#define _FGTEST_API_TEST_PILOT_HXX
|
||||
|
||||
#include <simgear/props/propsfwd.hxx>
|
||||
#include <simgear/math/SGMathFwd.hxx>
|
||||
#include <simgear/structure/subsystem_mgr.hxx>
|
||||
#include <simgear/math/SGGeod.hxx>
|
||||
|
||||
class GPS;
|
||||
|
||||
namespace FGTestApi {
|
||||
|
||||
/**
|
||||
* @brief simulation of the user (pilot) fying in a particular way
|
||||
* around the world. Standard property tree values are updated
|
||||
* (position / orientation / velocity)
|
||||
*/
|
||||
class TestPilot : public SGSubsystem
|
||||
{
|
||||
public:
|
||||
TestPilot(SGPropertyNode_ptr props = {});
|
||||
~TestPilot();
|
||||
|
||||
void resetAtPosition(const SGGeod& pos);
|
||||
|
||||
void setSpeedKts(double knots);
|
||||
|
||||
void setCourseTrue(double deg);
|
||||
void turnToCourse(double deg);
|
||||
|
||||
void setVerticalFPM(double fpm);
|
||||
void setTargetAltitudeFtMSL(double altFt);
|
||||
|
||||
// void setTurnRateDegSec();
|
||||
|
||||
void init() override;
|
||||
void update(double dT) override;
|
||||
|
||||
void flyHeading(double hdg);
|
||||
void flyDirectTo(const SGGeod& target);
|
||||
void flyGPSCourse(GPS *gps);
|
||||
|
||||
void flyGPSCourseOffset(GPS *gps, double offsetNm);
|
||||
|
||||
bool isOnHeading(double heading) const;
|
||||
|
||||
double trueCourseDeg() const;
|
||||
private:
|
||||
enum class LateralMode
|
||||
{
|
||||
Heading,
|
||||
Direct,
|
||||
GPSCourse,
|
||||
GPSOffset
|
||||
};
|
||||
|
||||
void updateValues(double dt);
|
||||
void setPosition(const SGGeod& pos);
|
||||
|
||||
SGPropertyNode_ptr _propRoot;
|
||||
|
||||
double _trueCourseDeg = 0.0;
|
||||
double _speedKnots = 0.0; // IAS
|
||||
double _vspeedFPM = 0.0;
|
||||
|
||||
bool _turnActive = false;
|
||||
bool _altActive = false;
|
||||
double _targetCourseDeg = 0.0;
|
||||
double _targetAltitudeFt = 0.0;
|
||||
|
||||
LateralMode _lateralMode = LateralMode::Heading;
|
||||
SGGeod _targetPos;
|
||||
GPS* _gps = nullptr;
|
||||
double _courseOffsetNm =0.0;
|
||||
|
||||
SGPropertyNode_ptr _latProp;
|
||||
SGPropertyNode_ptr _lonProp;
|
||||
SGPropertyNode_ptr _altitudeProp;
|
||||
SGPropertyNode_ptr _headingProp;
|
||||
SGPropertyNode_ptr _speedKnotsProp;
|
||||
SGPropertyNode_ptr _verticalFPMProp;
|
||||
SGPropertyNode_ptr _groundspeedKnotsProp;
|
||||
SGPropertyNode_ptr _speedMachProp;
|
||||
|
||||
SGPropertyNode_ptr _gpsNode;
|
||||
SGPropertyNode_ptr _gpsLegCourse;
|
||||
SGPropertyNode_ptr _courseErrorNm;
|
||||
};
|
||||
|
||||
} // of namespace FGTestApi
|
||||
|
||||
#endif // of _FGTEST_API_TEST_PILOT_HXX
|
||||
|
||||
@@ -0,0 +1,57 @@
|
||||
/*
|
||||
* Copyright (C) 2018 Edward d'Auvergne
|
||||
*
|
||||
* This file is part of the program FlightGear.
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 2 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
#include "scene_graph.hxx"
|
||||
|
||||
#include <Main/fg_props.hxx>
|
||||
#include <Main/globals.hxx>
|
||||
#include <Main/locale.hxx>
|
||||
#include <Scenery/scenery.hxx>
|
||||
#include <Viewer/renderer.hxx>
|
||||
#include <Viewer/FGEventHandler.hxx>
|
||||
|
||||
|
||||
namespace FGTestApi {
|
||||
namespace setUp {
|
||||
|
||||
void initScenery()
|
||||
{
|
||||
// Read the global defaults from $FG_ROOT/defaults.xml (needed by the renderer).
|
||||
SGPath defaultsXML = globals->get_fg_root() / "defaults.xml";
|
||||
if (!defaultsXML.exists())
|
||||
SG_LOG(SG_GENERAL, SG_ALERT, "Cannot read the global defaults from \"" << defaultsXML.utf8Str() << "\".");
|
||||
fgLoadProps("defaults.xml", globals->get_props());
|
||||
|
||||
// otherwise fgSplashProgress will assert
|
||||
globals->get_locale()->selectLanguage({});
|
||||
|
||||
// Set up the renderer.
|
||||
osg::ref_ptr<osgViewer::Viewer> viewer = new osgViewer::Viewer;
|
||||
FGRenderer* render = globals->get_renderer();
|
||||
render->init();
|
||||
render->setView(viewer.get());
|
||||
|
||||
// Start up the scenery subsystem.
|
||||
globals->add_new_subsystem<FGScenery>(SGSubsystemMgr::DISPLAY);
|
||||
globals->get_scenery()->init();
|
||||
globals->get_scenery()->bind();
|
||||
}
|
||||
|
||||
} // End of namespace setUp.
|
||||
} // End of namespace FGTestApi.
|
||||
@@ -0,0 +1,31 @@
|
||||
/*
|
||||
* Copyright (C) 2018 Edward d'Auvergne
|
||||
*
|
||||
* This file is part of the program FlightGear.
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 2 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
#ifndef FG_TEST_SCENE_GRAPH_HXX
|
||||
#define FG_TEST_SCENE_GRAPH_HXX
|
||||
|
||||
namespace FGTestApi {
|
||||
namespace setUp {
|
||||
|
||||
void initScenery();
|
||||
|
||||
} // End of namespace setUp.
|
||||
} // End of namespace FGTestApi.
|
||||
|
||||
#endif // FG_TEST_SCENE_GRAPH_HXX
|
||||
@@ -0,0 +1,30 @@
|
||||
|
||||
#include "config.h"
|
||||
|
||||
// implement various test-only methods on classes
|
||||
|
||||
#include <Airports/groundnetwork.hxx>
|
||||
#include <Airports/airport.hxx>
|
||||
#include <Airports/xmlloader.hxx>
|
||||
#include <Navaids/procedure.hxx>
|
||||
|
||||
void FGAirport::testSuiteInjectGroundnetXML(const SGPath& path)
|
||||
{
|
||||
_groundNetwork.reset(new FGGroundNetwork(const_cast<FGAirport*>(this)));
|
||||
XMLLoader::loadFromPath(_groundNetwork.get(), path);
|
||||
_groundNetwork->init();
|
||||
}
|
||||
|
||||
void FGAirport::testSuiteInjectProceduresXML(const SGPath& path)
|
||||
{
|
||||
if (mProceduresLoaded) {
|
||||
SG_LOG(SG_GENERAL, SG_ALERT, "Procedures already loaded for" << ident());
|
||||
mSIDs.clear();
|
||||
mSTARs.clear();
|
||||
mApproaches.clear();
|
||||
}
|
||||
|
||||
mProceduresLoaded = true;
|
||||
flightgear::RouteBase::loadAirportProcedures(path, const_cast<FGAirport*>(this));
|
||||
}
|
||||
|
||||
@@ -0,0 +1,476 @@
|
||||
#include "config.h"
|
||||
|
||||
#include "test_suite/dataStore.hxx"
|
||||
#include <ctime>
|
||||
|
||||
#include "TestDataLogger.hxx"
|
||||
#include "testGlobals.hxx"
|
||||
|
||||
#include <simgear/io/iostreams/sgstream.hxx>
|
||||
|
||||
#if defined(HAVE_QT)
|
||||
#include <GUI/QtLauncher.hxx>
|
||||
#endif
|
||||
|
||||
#include <Main/globals.hxx>
|
||||
#include <Main/options.hxx>
|
||||
#include <Main/util.hxx>
|
||||
#include <Main/FGInterpolator.hxx>
|
||||
#include <Main/locale.hxx>
|
||||
|
||||
#include <Time/TimeManager.hxx>
|
||||
|
||||
#include <simgear/structure/event_mgr.hxx>
|
||||
#include <simgear/timing/timestamp.hxx>
|
||||
#include <simgear/timing/sg_time.hxx>
|
||||
#include <simgear/math/sg_geodesy.hxx>
|
||||
#include <simgear/props/props_io.hxx>
|
||||
|
||||
#include <Airports/airport.hxx>
|
||||
#include <Navaids/FlightPlan.hxx>
|
||||
#include <Navaids/waypoint.hxx>
|
||||
#include <Navaids/routePath.hxx>
|
||||
|
||||
#include <Scripting/NasalSys.hxx>
|
||||
|
||||
using namespace flightgear;
|
||||
|
||||
namespace FGTestApi {
|
||||
|
||||
bool global_loggingToKML = false;
|
||||
sg_ofstream global_kmlStream;
|
||||
bool global_lineStringOpen = false;
|
||||
|
||||
namespace setUp {
|
||||
|
||||
void initTestGlobals(const std::string& testName)
|
||||
{
|
||||
assert(globals == nullptr);
|
||||
globals = new FGGlobals;
|
||||
|
||||
DataStore &data = DataStore::get();
|
||||
if (!data.getFGRoot().exists()) {
|
||||
data.findFGRoot("");
|
||||
}
|
||||
globals->set_fg_root(data.getFGRoot());
|
||||
|
||||
// current dir
|
||||
SGPath homePath = SGPath::fromUtf8(FGBUILDDIR) / "test_home";
|
||||
if (!homePath.exists()) {
|
||||
(homePath / "dummyFile").create_dir(0755);
|
||||
}
|
||||
|
||||
globals->set_fg_home(homePath);
|
||||
auto props = globals->get_props();
|
||||
props->setStringValue("sim/fg-home", homePath.utf8Str());
|
||||
|
||||
// Activate headless mode.
|
||||
globals->set_headless(true);
|
||||
|
||||
fgSetDefaults();
|
||||
|
||||
auto t = globals->add_new_subsystem<TimeManager>(SGSubsystemMgr::INIT);
|
||||
t->bind();
|
||||
t->init(); // establish mag-var data
|
||||
|
||||
/**
|
||||
* Both the event manager and subsystem manager are initialised by the
|
||||
* FGGlobals ctor, but only the subsystem manager is destroyed by the dtor.
|
||||
* Here the event manager is added to the subsystem manager so it can be
|
||||
* destroyed via the subsystem manager.
|
||||
*/
|
||||
globals->add_subsystem("events", globals->get_event_mgr(), SGSubsystemMgr::DISPLAY);
|
||||
|
||||
// necessary to avoid asserts: mark FGLocale as initialized
|
||||
globals->get_locale()->selectLanguage({});
|
||||
}
|
||||
|
||||
bool logPositionToKML(const std::string& testName)
|
||||
{
|
||||
// clear any previous state
|
||||
if (global_loggingToKML) {
|
||||
global_kmlStream.close();
|
||||
global_lineStringOpen = false;
|
||||
}
|
||||
|
||||
SGPath p = SGPath::desktop() / (testName + ".kml");
|
||||
global_kmlStream.open(p);
|
||||
if (!global_kmlStream.is_open()) {
|
||||
SG_LOG(SG_GENERAL, SG_WARN, "unable to open:" << p);
|
||||
return false;
|
||||
}
|
||||
|
||||
// pre-amble
|
||||
global_kmlStream << "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
|
||||
"<kml xmlns=\"http://www.opengis.net/kml/2.2\">\n"
|
||||
"<Document>\n";
|
||||
// need more precision for doubles when specifying lat/lon, see
|
||||
// https://xkcd.com/2170/ :)
|
||||
global_kmlStream.precision(12);
|
||||
|
||||
global_loggingToKML = true;
|
||||
return true;
|
||||
}
|
||||
|
||||
bool logLinestringsToKML(const std::string& testName)
|
||||
{
|
||||
// clear any previous state
|
||||
if (global_loggingToKML) {
|
||||
global_kmlStream.close();
|
||||
global_lineStringOpen = false;
|
||||
}
|
||||
|
||||
SGPath p = SGPath::desktop() / (testName + ".kml");
|
||||
global_kmlStream.open(p);
|
||||
if (!global_kmlStream.is_open()) {
|
||||
SG_LOG(SG_GENERAL, SG_WARN, "unable to open:" << p);
|
||||
return false;
|
||||
}
|
||||
|
||||
// pre-amble
|
||||
global_kmlStream << "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
|
||||
"<kml xmlns=\"http://www.opengis.net/kml/2.2\">\n"
|
||||
"<Document>\n";
|
||||
// need more precision for doubles when specifying lat/lon, see
|
||||
// https://xkcd.com/2170/ :)
|
||||
global_kmlStream.precision(12);
|
||||
|
||||
global_loggingToKML = false;
|
||||
return true;
|
||||
}
|
||||
|
||||
void initStandardNasal(bool withCanvas)
|
||||
{
|
||||
fgInitAllowedPaths();
|
||||
|
||||
auto nasalNode = globals->get_props()->getNode("nasal", true);
|
||||
|
||||
// load loadpriority.xml, for default modules load order
|
||||
|
||||
auto nasalLoadPriority = globals->get_props()->getNode("/sim/nasal-load-priority",true);
|
||||
readProperties(globals->get_fg_root() / "Nasal/loadpriority.xml", nasalLoadPriority);
|
||||
|
||||
// set various props to reduce Nasal errors
|
||||
auto props = globals->get_props();
|
||||
props->setStringValue("sim/flight-model", "null");
|
||||
props->setStringValue("sim/aircraft", "test-suite-aircraft");
|
||||
|
||||
props->setDoubleValue("sim/current-view/config/default-field-of-view-deg", 90.0);
|
||||
// ensure /sim/view/config exists
|
||||
props->setBoolValue("sim/view/config/foo", false);
|
||||
|
||||
props->setBoolValue("sim/rendering/precipitation-gui-enable", false);
|
||||
props->setBoolValue("sim/rendering/precipitation-aircraft-enable", false);
|
||||
|
||||
// disable various larger modules
|
||||
nasalNode->setBoolValue("canvas/enabled", withCanvas);
|
||||
nasalNode->setBoolValue("jetways/enabled", false);
|
||||
nasalNode->setBoolValue("jetways_edit/enabled", false);
|
||||
nasalNode->setBoolValue("local_weather/enabled", false);
|
||||
|
||||
// Nasal needs the interpolator running
|
||||
globals->add_subsystem("prop-interpolator", new FGInterpolator, SGSubsystemMgr::INIT);
|
||||
|
||||
// will be inited, since we already did that
|
||||
globals->add_new_subsystem<FGNasalSys>(SGSubsystemMgr::INIT);
|
||||
}
|
||||
|
||||
void populateFPWithoutNasal(flightgear::FlightPlanRef f,
|
||||
const std::string& depICAO, const std::string& depRunway,
|
||||
const std::string& destICAO, const std::string& destRunway,
|
||||
const std::string& waypoints)
|
||||
{
|
||||
FGAirportRef depApt = FGAirport::getByIdent(depICAO);
|
||||
f->setDeparture(depApt->getRunwayByIdent(depRunway));
|
||||
|
||||
|
||||
FGAirportRef destApt = FGAirport::getByIdent(destICAO);
|
||||
f->setDestination(destApt->getRunwayByIdent(destRunway));
|
||||
|
||||
// since we don't have the Nasal route-manager delegate, insert the
|
||||
// runway waypoints manually
|
||||
|
||||
auto depRwy = new RunwayWaypt(f->departureRunway(), f);
|
||||
depRwy->setFlag(WPT_DEPARTURE);
|
||||
f->insertWayptAtIndex(depRwy, -1);
|
||||
|
||||
for (auto ws : simgear::strutils::split(waypoints)) {
|
||||
WayptRef wpt = f->waypointFromString(ws);
|
||||
if (!wpt) {
|
||||
SG_LOG(SG_NAVAID, SG_ALERT, "No waypoint created for:" << ws);
|
||||
continue;
|
||||
}
|
||||
f->insertWayptAtIndex(wpt, -1);
|
||||
}
|
||||
|
||||
|
||||
auto destRwy = f->destinationRunway();
|
||||
f->insertWayptAtIndex(new BasicWaypt(destRwy->pointOnCenterline(-8 * SG_NM_TO_METER),
|
||||
destRwy->ident() + "-8", f), -1);
|
||||
f->insertWayptAtIndex(new RunwayWaypt(destRwy, f), -1);
|
||||
}
|
||||
|
||||
void populateFPWithNasal(flightgear::FlightPlanRef f,
|
||||
const std::string& depICAO, const std::string& depRunway,
|
||||
const std::string& destICAO, const std::string& destRunway,
|
||||
const std::string& waypoints)
|
||||
{
|
||||
FGAirportRef depApt = FGAirport::getByIdent(depICAO);
|
||||
f->setDeparture(depApt->getRunwayByIdent(depRunway));
|
||||
|
||||
FGAirportRef destApt = FGAirport::getByIdent(destICAO);
|
||||
f->setDestination(destApt->getRunwayByIdent(destRunway));
|
||||
|
||||
// insert after the last departure waypoint
|
||||
int insertIndex = 1;
|
||||
|
||||
for (auto ws : simgear::strutils::split(waypoints)) {
|
||||
WayptRef wpt = f->waypointFromString(ws);
|
||||
f->insertWayptAtIndex(wpt, insertIndex++);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
} // End of namespace setUp.
|
||||
|
||||
void beginLineString(const std::string& ident)
|
||||
{
|
||||
global_lineStringOpen = true;
|
||||
global_kmlStream << "<Placemark>\n";
|
||||
if (!ident.empty()) {
|
||||
global_kmlStream << "<name>" << ident << "</name>\n";
|
||||
}
|
||||
global_kmlStream << "<LineString>\n";
|
||||
global_kmlStream << "<tessellate>1</tessellate>\n";
|
||||
global_kmlStream << "<coordinates>\n";
|
||||
}
|
||||
|
||||
void logCoordinate(const SGGeod& pos)
|
||||
{
|
||||
if (!global_lineStringOpen) {
|
||||
beginLineString({});
|
||||
}
|
||||
|
||||
global_kmlStream << pos.getLongitudeDeg() << "," << pos.getLatitudeDeg() << " " << endl;
|
||||
}
|
||||
|
||||
void rawLogCoordinate(const SGGeod& pos)
|
||||
{
|
||||
global_kmlStream << pos.getLongitudeDeg() << "," << pos.getLatitudeDeg() << " " << endl;
|
||||
}
|
||||
|
||||
void endCurrentLineString()
|
||||
{
|
||||
global_lineStringOpen = false;
|
||||
global_kmlStream <<
|
||||
"</coordinates>\n"
|
||||
"</LineString>\n"
|
||||
"</Placemark>\n" << endl;
|
||||
}
|
||||
|
||||
void setPosition(const SGGeod& g)
|
||||
{
|
||||
if (global_loggingToKML) {
|
||||
if (global_lineStringOpen) {
|
||||
endCurrentLineString();
|
||||
}
|
||||
|
||||
logCoordinate(g);
|
||||
}
|
||||
|
||||
globals->get_props()->setDoubleValue("position/latitude-deg", g.getLatitudeDeg());
|
||||
globals->get_props()->setDoubleValue("position/longitude-deg", g.getLongitudeDeg());
|
||||
globals->get_props()->setDoubleValue("position/altitude-ft", g.getElevationFt());
|
||||
}
|
||||
|
||||
const SGGeod getPosition()
|
||||
{
|
||||
return SGGeod::fromDegFt(
|
||||
globals->get_props()->getDoubleValue("position/latitude-deg"),
|
||||
globals->get_props()->getDoubleValue("position/longitude-deg"),
|
||||
globals->get_props()->getDoubleValue("position/altitude-ft"));
|
||||
}
|
||||
|
||||
|
||||
|
||||
void setPositionAndStabilise(const SGGeod& g)
|
||||
{
|
||||
setPosition(g);
|
||||
for (int i=0; i<60; ++i) {
|
||||
globals->get_subsystem_mgr()->update(0.015);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
void runForTime(double t)
|
||||
{
|
||||
const int tickHz = 30;
|
||||
const double tickDuration = 1.0 / tickHz;
|
||||
|
||||
int ticks = static_cast<int>(t * tickHz);
|
||||
assert(ticks > 0);
|
||||
|
||||
const int logInterval = 0.5 * tickHz;
|
||||
int nextLog = 0;
|
||||
|
||||
long startTime = globals->get_time_params()->get_cur_time();
|
||||
|
||||
for (int t = 0; t < ticks; ++t) {
|
||||
globals->inc_sim_time_sec(tickDuration);
|
||||
globals->get_time_params()->update(globals->get_view_position(), startTime, t * tickDuration);
|
||||
globals->get_subsystem_mgr()->update(tickDuration);
|
||||
|
||||
if (nextLog == 0) {
|
||||
if (global_loggingToKML) {
|
||||
logCoordinate(globals->get_aircraft_position());
|
||||
}
|
||||
|
||||
if (DataLogger::isActive()) {
|
||||
DataLogger::instance()->writeRecord();
|
||||
}
|
||||
nextLog = logInterval;
|
||||
} else {
|
||||
nextLog--;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
bool runForTimeWithCheck(double t, RunCheck check)
|
||||
{
|
||||
const int tickHz = 30;
|
||||
const double tickDuration = 1.0 / tickHz;
|
||||
|
||||
int ticks = static_cast<int>(t * tickHz);
|
||||
assert(ticks > 0);
|
||||
const int logInterval = 0.5 * tickHz;
|
||||
int nextLog = 0;
|
||||
|
||||
for (int t = 0; t < ticks; ++t) {
|
||||
globals->inc_sim_time_sec(tickDuration);
|
||||
globals->get_subsystem_mgr()->update(tickDuration);
|
||||
|
||||
if (nextLog == 0) {
|
||||
if (global_loggingToKML) {
|
||||
logCoordinate(globals->get_aircraft_position());
|
||||
}
|
||||
|
||||
if (DataLogger::isActive()) {
|
||||
DataLogger::instance()->writeRecord();
|
||||
}
|
||||
nextLog = logInterval;
|
||||
} else {
|
||||
nextLog--;
|
||||
}
|
||||
|
||||
bool done = check();
|
||||
if (done) {
|
||||
if (global_loggingToKML) {
|
||||
endCurrentLineString();
|
||||
}
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
void adjustSimulationWorldTime(time_t desiredUnixTime)
|
||||
{
|
||||
int timeOffset = desiredUnixTime - time(nullptr);
|
||||
globals->get_props()->setIntValue("/sim/time/cur-time-override", 0);
|
||||
globals->get_props()->setIntValue("/sim/time/warp", timeOffset);
|
||||
|
||||
globals->get_subsystem<TimeManager>()->update(0.0);
|
||||
}
|
||||
|
||||
void writeFlightPlanToKML(flightgear::FlightPlanRef fp)
|
||||
{
|
||||
if (!global_loggingToKML)
|
||||
return;
|
||||
|
||||
RoutePath rpath(fp);
|
||||
|
||||
for (int i=0; i<fp->numLegs(); ++i) {
|
||||
SGGeodVec legPath = rpath.pathForIndex(i);
|
||||
auto wp = fp->legAtIndex(i)->waypoint();
|
||||
|
||||
writeGeodsToKML("FP-leg-" + wp->ident(), legPath);
|
||||
|
||||
SGGeod legWPPosition = wp->position();
|
||||
writePointToKML("WP " + wp->ident(), legWPPosition);
|
||||
}
|
||||
}
|
||||
|
||||
void writeGeodsToKML(const std::string &label, const flightgear::SGGeodVec& geods)
|
||||
{
|
||||
if (global_lineStringOpen) {
|
||||
endCurrentLineString();
|
||||
}
|
||||
|
||||
beginLineString(label);
|
||||
|
||||
for (const auto& g : geods) {
|
||||
logCoordinate(g);
|
||||
}
|
||||
|
||||
endCurrentLineString();
|
||||
}
|
||||
|
||||
void writePointToKML(const std::string& ident, const SGGeod& pos)
|
||||
{
|
||||
global_kmlStream << "<Placemark>\n";
|
||||
global_kmlStream << "<name>" << ident << "</name>\n";
|
||||
global_kmlStream << "<Point>\n";
|
||||
global_kmlStream << "<coordinates>\n";
|
||||
rawLogCoordinate(pos);
|
||||
global_kmlStream << "</coordinates>\n";
|
||||
global_kmlStream << "</Point>\n";
|
||||
global_kmlStream << "</Placemark>\n";
|
||||
}
|
||||
|
||||
bool executeNasal(const std::string& code)
|
||||
{
|
||||
auto nasal = globals->get_subsystem<FGNasalSys>();
|
||||
if (!nasal) {
|
||||
throw sg_exception("Nasal not available");
|
||||
}
|
||||
|
||||
nasal->getAndClearErrorList();
|
||||
std::string output, parseErrors;
|
||||
bool ok = nasal->parseAndRunWithOutput(code, output, parseErrors);
|
||||
if (!parseErrors.empty()) {
|
||||
SG_LOG(SG_NASAL, SG_ALERT, "Errors running Nasal:" << parseErrors);
|
||||
return false;
|
||||
}
|
||||
|
||||
return ok;
|
||||
}
|
||||
|
||||
namespace tearDown {
|
||||
|
||||
void shutdownTestGlobals()
|
||||
{
|
||||
// The QApplication instance must be destroyed before exit() begins, see
|
||||
// <https://bugreports.qt.io/browse/QTBUG-48709> (otherwise, segfault).
|
||||
#if defined(HAVE_QT)
|
||||
flightgear::shutdownQtApp();
|
||||
#endif
|
||||
|
||||
delete globals;
|
||||
globals = nullptr;
|
||||
|
||||
if (global_kmlStream) {
|
||||
if (global_lineStringOpen) {
|
||||
endCurrentLineString();
|
||||
}
|
||||
// post-amble
|
||||
global_kmlStream << "</Document>\n"
|
||||
"</kml>" << endl;
|
||||
global_kmlStream.close();
|
||||
global_loggingToKML = false;
|
||||
}
|
||||
}
|
||||
|
||||
} // End of namespace tearDown.
|
||||
|
||||
} // End of namespace FGTestApi.
|
||||
@@ -0,0 +1,75 @@
|
||||
#ifndef FG_TEST_GLOBALS_HELPERS_HXX
|
||||
#define FG_TEST_GLOBALS_HELPERS_HXX
|
||||
|
||||
#include <string>
|
||||
#include <functional>
|
||||
#include <vector>
|
||||
|
||||
#include <simgear/math/SGGeod.hxx>
|
||||
#include <simgear/structure/SGSharedPtr.hxx>
|
||||
|
||||
namespace flightgear
|
||||
{
|
||||
class FlightPlan;
|
||||
typedef SGSharedPtr<FlightPlan> FlightPlanRef;
|
||||
|
||||
typedef std::vector<SGGeod> SGGeodVec;
|
||||
}
|
||||
|
||||
namespace FGTestApi {
|
||||
|
||||
namespace setUp {
|
||||
|
||||
void initTestGlobals(const std::string& testName);
|
||||
|
||||
bool logPositionToKML(const std::string& testName);
|
||||
/**Don't log aircraft positions*/
|
||||
bool logLinestringsToKML(const std::string& testName);
|
||||
|
||||
void initStandardNasal(bool withCanvas = false);
|
||||
|
||||
void populateFPWithoutNasal(flightgear::FlightPlanRef f,
|
||||
const std::string& depICAO, const std::string& depRunway,
|
||||
const std::string& destICAO, const std::string& destRunway,
|
||||
const std::string& waypoints);
|
||||
|
||||
void populateFPWithNasal(flightgear::FlightPlanRef f,
|
||||
const std::string& depICAO, const std::string& depRunway,
|
||||
const std::string& destICAO, const std::string& destRunway,
|
||||
const std::string& waypoints);
|
||||
|
||||
} // End of namespace setUp.
|
||||
|
||||
// helpers during tests
|
||||
|
||||
const SGGeod getPosition();
|
||||
void setPosition(const SGGeod& g);
|
||||
void setPositionAndStabilise(const SGGeod& g);
|
||||
|
||||
void runForTime(double t);
|
||||
|
||||
/**
|
||||
@brief set the simulation date/time clock to 'time'
|
||||
*/
|
||||
void adjustSimulationWorldTime(time_t time);
|
||||
|
||||
using RunCheck = std::function<bool(void)>;
|
||||
|
||||
bool runForTimeWithCheck(double t, RunCheck check);
|
||||
|
||||
void writeFlightPlanToKML(flightgear::FlightPlanRef fp);
|
||||
|
||||
void writeGeodsToKML(const std::string &label, const flightgear::SGGeodVec& geods);
|
||||
void writePointToKML(const std::string& ident, const SGGeod& pos);
|
||||
|
||||
bool executeNasal(const std::string& code);
|
||||
|
||||
namespace tearDown {
|
||||
|
||||
void shutdownTestGlobals();
|
||||
|
||||
} // End of namespace tearDown.
|
||||
|
||||
} // End of namespace FGTestApi.
|
||||
|
||||
#endif // of FG_TEST_GLOBALS_HELPERS_HXX
|
||||
Reference in New Issue
Block a user