2017-03-21 20:43:42 +00:00
|
|
|
#include "config.h"
|
|
|
|
|
2018-03-28 15:30:59 +00:00
|
|
|
#include "test_suite/dataStore.hxx"
|
|
|
|
|
2019-09-24 10:32:03 +00:00
|
|
|
#include "testGlobals.hxx"
|
2017-03-21 20:43:42 +00:00
|
|
|
|
2019-04-19 11:26:07 +00:00
|
|
|
#include <simgear/io/iostreams/sgstream.hxx>
|
|
|
|
|
2018-06-11 09:35:09 +00:00
|
|
|
#if defined(HAVE_QT)
|
2018-03-28 22:39:38 +00:00
|
|
|
#include <GUI/QtLauncher.hxx>
|
|
|
|
#endif
|
|
|
|
|
2017-03-21 20:43:42 +00:00
|
|
|
#include <Main/globals.hxx>
|
2017-03-27 14:37:54 +00:00
|
|
|
#include <Main/options.hxx>
|
2019-04-19 11:26:07 +00:00
|
|
|
#include <Main/util.hxx>
|
|
|
|
#include <Main/FGInterpolator.hxx>
|
2017-03-27 14:37:54 +00:00
|
|
|
|
2017-03-21 20:43:42 +00:00
|
|
|
#include <Time/TimeManager.hxx>
|
|
|
|
|
2018-06-12 12:28:23 +00:00
|
|
|
#include <simgear/structure/event_mgr.hxx>
|
2017-03-21 20:43:42 +00:00
|
|
|
#include <simgear/timing/timestamp.hxx>
|
2018-10-10 08:26:06 +00:00
|
|
|
#include <simgear/math/sg_geodesy.hxx>
|
2017-03-27 14:37:54 +00:00
|
|
|
|
2019-04-23 17:10:39 +00:00
|
|
|
#include <Airports/airport.hxx>
|
|
|
|
#include <Navaids/FlightPlan.hxx>
|
|
|
|
#include <Navaids/waypoint.hxx>
|
2019-09-19 15:05:26 +00:00
|
|
|
#include <Navaids/routePath.hxx>
|
2019-04-23 17:10:39 +00:00
|
|
|
|
2019-04-19 11:26:07 +00:00
|
|
|
#include <Scripting/NasalSys.hxx>
|
|
|
|
|
2019-04-23 17:10:39 +00:00
|
|
|
using namespace flightgear;
|
|
|
|
|
2018-07-16 13:59:21 +00:00
|
|
|
namespace FGTestApi {
|
|
|
|
|
2019-04-19 11:26:07 +00:00
|
|
|
bool global_loggingToKML = false;
|
|
|
|
sg_ofstream global_kmlStream;
|
|
|
|
bool global_lineStringOpen = false;
|
|
|
|
|
2018-07-16 13:59:21 +00:00
|
|
|
namespace setUp {
|
2017-03-21 20:43:42 +00:00
|
|
|
|
2018-06-10 13:43:20 +00:00
|
|
|
void initTestGlobals(const std::string& testName)
|
|
|
|
{
|
2019-04-19 11:26:07 +00:00
|
|
|
assert(globals == nullptr);
|
2017-03-21 20:43:42 +00:00
|
|
|
globals = new FGGlobals;
|
|
|
|
|
2018-03-28 15:30:59 +00:00
|
|
|
DataStore &data = DataStore::get();
|
2018-06-05 12:31:06 +00:00
|
|
|
if (!data.getFGRoot().exists()) {
|
|
|
|
data.findFGRoot("");
|
|
|
|
}
|
2018-03-28 15:30:59 +00:00
|
|
|
globals->set_fg_root(data.getFGRoot());
|
2017-03-21 20:43:42 +00:00
|
|
|
|
2018-06-10 13:43:20 +00:00
|
|
|
// current dir
|
|
|
|
SGPath homePath = SGPath::fromUtf8(FGBUILDDIR) / "test_home";
|
|
|
|
if (!homePath.exists()) {
|
|
|
|
(homePath / "dummyFile").create_dir(0755);
|
|
|
|
}
|
2017-03-27 14:37:54 +00:00
|
|
|
|
2018-06-10 13:43:20 +00:00
|
|
|
globals->set_fg_home(homePath);
|
2019-04-19 11:26:07 +00:00
|
|
|
auto props = globals->get_props();
|
|
|
|
props->setStringValue("sim/fg-home", homePath.utf8Str());
|
|
|
|
|
2018-06-11 06:50:36 +00:00
|
|
|
// Activate headless mode.
|
|
|
|
globals->set_headless(true);
|
|
|
|
|
2018-06-10 13:43:20 +00:00
|
|
|
fgSetDefaults();
|
2017-03-21 20:43:42 +00:00
|
|
|
|
2018-06-10 13:43:20 +00:00
|
|
|
std::unique_ptr<TimeManager> t;
|
|
|
|
t.reset(new TimeManager);
|
|
|
|
t->init(); // establish mag-var data
|
2018-06-12 12:28:23 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* 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);
|
2019-04-19 11:26:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool logPositionToKML(const std::string& testName)
|
|
|
|
{
|
2019-09-18 22:54:32 +00:00
|
|
|
// clear any previous state
|
|
|
|
if (global_loggingToKML) {
|
|
|
|
global_kmlStream.close();
|
|
|
|
global_lineStringOpen = false;
|
|
|
|
}
|
|
|
|
|
2019-04-19 11:26:07 +00:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2020-03-11 17:14:38 +00:00
|
|
|
void initStandardNasal(bool withCanvas)
|
2019-04-19 11:26:07 +00:00
|
|
|
{
|
|
|
|
fgInitAllowedPaths();
|
|
|
|
|
|
|
|
auto nasalNode = globals->get_props()->getNode("nasal", true);
|
|
|
|
|
2019-09-24 10:39:56 +00:00
|
|
|
// 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);
|
|
|
|
|
2019-04-19 11:26:07 +00:00
|
|
|
// disable various larger modules
|
2020-03-11 17:14:38 +00:00
|
|
|
nasalNode->setBoolValue("canvas/enabled", withCanvas);
|
2019-04-19 11:26:07 +00:00
|
|
|
nasalNode->setBoolValue("jetways/enabled", false);
|
|
|
|
nasalNode->setBoolValue("jetways_edit/enabled", false);
|
|
|
|
nasalNode->setBoolValue("local_weather/enabled", false);
|
|
|
|
|
2019-09-24 10:39:56 +00:00
|
|
|
// Nasal needs the interpolator running
|
|
|
|
globals->add_subsystem("prop-interpolator", new FGInterpolator, SGSubsystemMgr::INIT);
|
|
|
|
|
2019-04-19 11:26:07 +00:00
|
|
|
// will be inited, since we already did that
|
|
|
|
globals->add_new_subsystem<FGNasalSys>(SGSubsystemMgr::INIT);
|
2018-06-10 13:43:20 +00:00
|
|
|
}
|
2017-03-21 20:43:42 +00:00
|
|
|
|
2019-04-19 11:26:07 +00:00
|
|
|
void populateFPWithoutNasal(flightgear::FlightPlanRef f,
|
2019-04-23 17:10:39 +00:00
|
|
|
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
|
|
|
|
|
2019-09-25 12:25:53 +00:00
|
|
|
auto depRwy = new RunwayWaypt(f->departureRunway(), f);
|
|
|
|
depRwy->setFlag(WPT_DEPARTURE);
|
|
|
|
f->insertWayptAtIndex(depRwy, -1);
|
2019-04-23 17:10:39 +00:00
|
|
|
|
|
|
|
for (auto ws : simgear::strutils::split(waypoints)) {
|
|
|
|
WayptRef wpt = f->waypointFromString(ws);
|
2020-04-15 15:11:33 +00:00
|
|
|
if (!wpt) {
|
|
|
|
SG_LOG(SG_NAVAID, SG_ALERT, "No waypoint created for:" << ws);
|
|
|
|
continue;
|
|
|
|
}
|
2019-04-23 17:10:39 +00:00
|
|
|
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);
|
|
|
|
}
|
2019-04-19 11:26:07 +00:00
|
|
|
|
|
|
|
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++);
|
|
|
|
}
|
|
|
|
}
|
2019-04-23 17:10:39 +00:00
|
|
|
|
|
|
|
|
2018-07-16 13:59:21 +00:00
|
|
|
} // End of namespace setUp.
|
2019-04-19 11:26:07 +00:00
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
2018-07-16 13:59:21 +00:00
|
|
|
|
2019-04-19 11:26:07 +00:00
|
|
|
void endCurrentLineString()
|
|
|
|
{
|
|
|
|
global_lineStringOpen = false;
|
|
|
|
global_kmlStream <<
|
|
|
|
"</coordinates>\n"
|
|
|
|
"</LineString>\n"
|
|
|
|
"</Placemark>\n" << endl;
|
|
|
|
}
|
|
|
|
|
2018-10-10 08:26:06 +00:00
|
|
|
void setPosition(const SGGeod& g)
|
|
|
|
{
|
2019-04-19 11:26:07 +00:00
|
|
|
if (global_loggingToKML) {
|
|
|
|
if (global_lineStringOpen) {
|
|
|
|
endCurrentLineString();
|
|
|
|
}
|
|
|
|
|
|
|
|
logCoordinate(g);
|
|
|
|
}
|
|
|
|
|
2018-10-10 08:26:06 +00:00
|
|
|
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());
|
|
|
|
}
|
2019-09-24 10:29:05 +00:00
|
|
|
|
|
|
|
void setPositionAndStabilise(const SGGeod& g)
|
|
|
|
{
|
|
|
|
setPosition(g);
|
|
|
|
for (int i=0; i<60; ++i) {
|
|
|
|
globals->get_subsystem_mgr()->update(0.015);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-16 13:59:21 +00:00
|
|
|
|
2019-04-22 10:57:55 +00:00
|
|
|
void runForTime(double t)
|
|
|
|
{
|
|
|
|
int ticks = t * 120.0;
|
|
|
|
assert(ticks > 0);
|
2019-09-25 12:25:53 +00:00
|
|
|
const double dt = 1 / 120.0;
|
|
|
|
|
2019-04-22 10:57:55 +00:00
|
|
|
for (int t = 0; t < ticks; ++t) {
|
2019-09-25 12:25:53 +00:00
|
|
|
globals->inc_sim_time_sec(dt);
|
|
|
|
globals->get_subsystem_mgr()->update(dt);
|
2019-04-19 11:26:07 +00:00
|
|
|
if (global_loggingToKML) {
|
|
|
|
logCoordinate(globals->get_aircraft_position());
|
|
|
|
}
|
2019-04-22 10:57:55 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-04-19 11:26:07 +00:00
|
|
|
bool runForTimeWithCheck(double t, RunCheck check)
|
|
|
|
{
|
|
|
|
const int tickHz = 30;
|
|
|
|
const double tickDuration = 1.0 / tickHz;
|
2019-09-25 12:25:53 +00:00
|
|
|
|
2019-04-19 11:26:07 +00:00
|
|
|
int ticks = static_cast<int>(t * tickHz);
|
|
|
|
assert(ticks > 0);
|
|
|
|
const int logInterval = 2 * tickHz; // every two seconds
|
|
|
|
int nextLog = 0;
|
|
|
|
|
|
|
|
for (int t = 0; t < ticks; ++t) {
|
2019-09-25 12:25:53 +00:00
|
|
|
globals->inc_sim_time_sec(tickDuration);
|
2019-04-19 11:26:07 +00:00
|
|
|
globals->get_subsystem_mgr()->update(tickDuration);
|
|
|
|
|
|
|
|
if (global_loggingToKML) {
|
|
|
|
if (nextLog == 0) {
|
|
|
|
logCoordinate(globals->get_aircraft_position());
|
|
|
|
nextLog = logInterval;
|
|
|
|
} else {
|
|
|
|
nextLog--;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bool done = check();
|
|
|
|
if (done) {
|
|
|
|
if (global_loggingToKML) {
|
|
|
|
endCurrentLineString();
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
2019-09-19 15:05:26 +00:00
|
|
|
|
|
|
|
void writeFlightPlanToKML(flightgear::FlightPlanRef fp)
|
|
|
|
{
|
2019-09-24 10:39:56 +00:00
|
|
|
if (!global_loggingToKML)
|
|
|
|
return;
|
|
|
|
|
2019-09-19 15:05:26 +00:00
|
|
|
RoutePath rpath(fp);
|
|
|
|
|
|
|
|
SGGeodVec fullPath;
|
|
|
|
for (int i=0; i<fp->numLegs(); ++i) {
|
|
|
|
SGGeodVec legPath = rpath.pathForIndex(i);
|
|
|
|
fullPath.insert(fullPath.end(), legPath.begin(), legPath.end());
|
|
|
|
}
|
|
|
|
|
|
|
|
writeGeodsToKML("FlightPlan", fullPath);
|
|
|
|
}
|
|
|
|
|
2019-09-23 22:14:32 +00:00
|
|
|
void writeGeodsToKML(const std::string &label, const flightgear::SGGeodVec& geods)
|
2019-09-19 15:05:26 +00:00
|
|
|
{
|
|
|
|
if (global_lineStringOpen) {
|
|
|
|
endCurrentLineString();
|
|
|
|
}
|
|
|
|
|
|
|
|
beginLineString(label);
|
|
|
|
|
|
|
|
for (const auto& g : geods) {
|
|
|
|
logCoordinate(g);
|
|
|
|
}
|
|
|
|
|
|
|
|
endCurrentLineString();
|
|
|
|
}
|
|
|
|
|
2019-09-24 10:29:05 +00:00
|
|
|
bool executeNasal(const std::string& code)
|
|
|
|
{
|
|
|
|
auto nasal = globals->get_subsystem<FGNasalSys>();
|
|
|
|
if (!nasal) {
|
|
|
|
throw sg_exception("Nasal not available");
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string output, errors;
|
|
|
|
bool ok = nasal->parseAndRunWithOutput(code, output, errors);
|
|
|
|
if (!errors.empty()) {
|
|
|
|
SG_LOG(SG_NASAL, SG_ALERT, "Errors running Nasal:" << errors);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return ok;
|
|
|
|
}
|
2019-04-19 11:26:07 +00:00
|
|
|
|
2018-07-16 13:59:21 +00:00
|
|
|
namespace tearDown {
|
|
|
|
|
2018-06-10 13:43:20 +00:00
|
|
|
void shutdownTestGlobals()
|
|
|
|
{
|
2018-03-28 22:39:38 +00:00
|
|
|
// The QApplication instance must be destroyed before exit() begins, see
|
|
|
|
// <https://bugreports.qt.io/browse/QTBUG-48709> (otherwise, segfault).
|
2018-06-11 09:35:09 +00:00
|
|
|
#if defined(HAVE_QT)
|
2018-03-28 22:39:38 +00:00
|
|
|
flightgear::shutdownQtApp();
|
|
|
|
#endif
|
|
|
|
|
2017-03-21 20:43:42 +00:00
|
|
|
delete globals;
|
2019-04-19 11:26:07 +00:00
|
|
|
globals = nullptr;
|
|
|
|
|
|
|
|
if (global_loggingToKML) {
|
|
|
|
if (global_lineStringOpen) {
|
|
|
|
endCurrentLineString();
|
|
|
|
}
|
|
|
|
// post-amble
|
|
|
|
global_kmlStream << "</Document>\n"
|
|
|
|
"</kml>" << endl;
|
|
|
|
global_kmlStream.close();
|
2019-09-18 22:54:32 +00:00
|
|
|
global_loggingToKML = false;
|
2019-04-19 11:26:07 +00:00
|
|
|
}
|
2018-06-10 13:43:20 +00:00
|
|
|
}
|
2018-07-16 13:59:21 +00:00
|
|
|
|
|
|
|
} // End of namespace tearDown.
|
|
|
|
|
|
|
|
} // End of namespace FGTestApi.
|