From f4d16ef0fd96d819f30a639eda7da6ac182b04c3 Mon Sep 17 00:00:00 2001 From: Mathew Sutcliffe Date: Sat, 9 Mar 2013 22:08:54 +0000 Subject: [PATCH] second draft of ISimulator, and put units of measurement on some identifiers --- blacklib/include/blackcore/constants.h | 2 +- blacklib/include/blackcore/sim_callbacks.h | 12 +- blacklib/include/blackcore/simulator.h | 180 ++++++++++++--------- blacklib/include/blackcore/vector_geo.h | 34 ++-- blacklib/src/blackcore/ecef.cpp | 14 +- blacklib/src/blackcore/ned.cpp | 4 +- blacklib/src/blackcore/simulator.cpp | 54 ++----- blacklib/src/blackcore/vector_geo.cpp | 67 ++++---- 8 files changed, 179 insertions(+), 188 deletions(-) diff --git a/blacklib/include/blackcore/constants.h b/blacklib/include/blackcore/constants.h index f442a3963..f26544b4f 100644 --- a/blacklib/include/blackcore/constants.h +++ b/blacklib/include/blackcore/constants.h @@ -34,7 +34,7 @@ namespace BlackCore const double KnotsToMeterPerSecond = 0.5144444444; //! Equatorial radius of WGS84 ellipsoid (6378137 m) - const double EarthRadius = 6378137.0; + const double EarthRadiusMeters = 6378137.0; //! Flattening of WGS84 ellipsoid (1/298.257223563). const double Flattening = 1/298.257223563; diff --git a/blacklib/include/blackcore/sim_callbacks.h b/blacklib/include/blackcore/sim_callbacks.h index cb602bca6..e9e77684d 100644 --- a/blacklib/include/blackcore/sim_callbacks.h +++ b/blacklib/include/blackcore/sim_callbacks.h @@ -3,14 +3,4 @@ //! License, v. 2.0. If a copy of the MPL was not distributed with this //! file, You can obtain one at http://mozilla.org/MPL/2.0/ -#ifndef SIM_CALLBACKS_H -#define SIM_CALLBACKS_H - -typedef void (* cbChangedRadioFreq)(const int radioNumber, const QString &frequency); -typedef void (* cbChangedGearPosition)(const int percentage); -typedef void (* cbChangedLights)(const int map); -typedef void (* cbSimStarted)(const bool status); -typedef void (* cbChangedAircraftType)(const QString &type); -typedef void (* cbChangedFlaps)(const int percentage); - -#endif // CALLBACKS_H +//TODO remove this file diff --git a/blacklib/include/blackcore/simulator.h b/blacklib/include/blackcore/simulator.h index a2e024f16..4885cd9cc 100644 --- a/blacklib/include/blackcore/simulator.h +++ b/blacklib/include/blackcore/simulator.h @@ -8,22 +8,75 @@ #include -#include - +#include "blackcore/sim_callbacks.h" #include "blackcore/vector_geo.h" -class CLibraryContext; #define SHARED_LIBRARY_NAME_FS9 "bb_driver_fs9" #define SHARED_LIBRARY_NAME_FSX "bb_driver_fsx" #define SHARED_LIBRARY_NAME_XPLANE "bb_driver_xplane" +namespace BlackMisc { + + class IContext; + +} + namespace BlackCore { - class CVector3D; - class CVectorGEO; - class IContext; + class CPlaneModel + { + public: + QString name; + QString typeCode; + QString airlineCode; + }; - class ISimulator + class CPhysicalState + { + public: + CPhysicalState() : headingDegrees(0), pitchDegrees(0), bankDegrees(0), groundSpeedKnots(0) + {} + CVectorGeo position; + float headingDegrees; + float pitchDegrees; + float bankDegrees; + float groundSpeedKnots; + }; + + class CAvionicsState + { + CAvionicsState() : squawkCode(0), squawkModeC(false), squawkIdent(false), + com1FreqHz(0), com2FreqHz(0) + {} + qint16 squawkCode; + bool squawkModeC; + bool squawkIdent; + qint32 com1FreqHz; + qint32 com2FreqHz; + }; + + class CAnimationState + { + public: + CAnimationState() : gearPercent(0), flapsPercent(0), landingLights(false), + taxiLights(false), navLights(false), strobeLights(false), beaconLights(false) + {} + qint8 gearPercent; + qint8 flapsPercent; + bool landingLights; + bool taxiLights; + bool navLights; + bool strobeLights; + bool beaconLights; + }; + + typedef std::tr1::function cbSimStarted; + typedef std::tr1::function cbChangedAvionicsState; + typedef std::tr1::function cbChangedAnimationState; + typedef std::tr1::function cbChangedModel; + typedef std::tr1::function cbSendTextMessage; + + class ISimulator { public: @@ -31,103 +84,82 @@ namespace BlackCore { static const quint32 InterfaceVersionMajor; static const quint32 InterfaceVersionMinor; + enum ESimulator { + FS9 = 0, + FSX, + XPLANE, + }; - enum ESimulator { FS9 = 0, - FSX, - XPLANE}; - - ISimulator(void) {} + ISimulator() {} virtual ~ISimulator() {} - virtual void setLibraryContext(IContext *context) = 0; + virtual void setLibraryContext(BlackMisc::IContext *context); + static ISimulator *createDriver(ESimulator sim); + //////////////////////////////// // Global section //////////////////////////////// virtual int init() = 0; - virtual int connectTo() = 0; + virtual int connect() = 0; // Callback when the Simulation starts - virtual void setcbSimStarted(cbSimStarted func, void *context); + virtual void setcbSimStarted(const cbSimStarted &func); - virtual bool isRunning() = 0; + virtual bool isConnected() = 0; virtual QString getLastErrorMessage() = 0; - virtual qint32 sendTextMessage(const QString &text) = 0; - - //////////////////////////////// - // Remote plane section - //////////////////////////////// - - virtual qint32 addPlane(const QString& callsign, const QString &type, const CVectorGEO * const pos, const double groundSpeed) = 0; - - virtual bool removePlane(const qint32 id) = 0; - - virtual bool updatePositionAndSpeed(const qint32 id, const CVectorGEO * const pos, const double groundSpeed) = 0; - - virtual bool setGear(const qint32 id, const qint32 percentage) = 0; - - virtual bool setFlaps(const qint32 id, const qint32 percentage) = 0; - - virtual bool setLights(const qint32 id, const qint32 map) = 0; - //////////////////////////////// // User plane section //////////////////////////////// - // Callback frequency tuner - virtual void setChangedRadioFreq(cbChangedRadioFreq func, void * context); + // Callback avionics state + virtual void setcbChangedAvionicsState(const cbChangedAvionicsState &func); - // Callback when the gear is moving - virtual void setcbChangedGearPosition(cbChangedGearPosition func, void *context); - - // Callback if the user switched on/off a light - virtual void setcbChangedLights(cbChangedLights func, void *context); + // Callback animation state + virtual void setcbChangedAnimationState(const cbChangedAnimationState &func); // Callback, when the Aircraft is set or gets changed - virtual void setcbChangedAircraftType(cbChangedAircraftType func, void *context); + virtual void setcbChangedModel(const cbChangedModel &func); - // Callback, when the Flaps are moving - virtual void setcbChangedFlaps(cbChangedFlaps func, void *context); + // Not const because it may need to mutate state in order to communicate with the sim + virtual bool isOnGround() = 0; - // This one is called regular and when we actually need it, therefor not a callback - virtual double getUserSpeed() const = 0; + // This might block - use QtConcurrent::run if that is a problem + virtual CPhysicalState getPhysicalState() = 0; - // This one is called regular and when we actually need it, therefor not a callback - virtual CVectorGEO getUserPosition() const = 0; - - // This one is called regular and when we actually need it, therefor not a callback - virtual qint32 getUserPitch() const = 0; - - // This one is called regular and when we actually need it, therefor not a callback - virtual qint32 getUserBank() const = 0; - - // This one is called regular and when we actually need it, therefor not a callback - virtual qint32 getUserHeading() const = 0; - - // This one is called regular and when we actually need it, therefor not a callback - virtual qint32 getUserPitchBankHeading() const = 0; + //////////////////////////////// + // Remote plane section + //////////////////////////////// - virtual bool isOnGround() const = 0; + // This might block - use QtConcurrent::run if that is a problem + virtual qint32 addPlane(const QString &callsign) = 0; + + virtual bool removePlane(const qint32 planeID) = 0; + + virtual void setModel(const qint32 planeID, const CPlaneModel &model) = 0; + + virtual bool setPhysicalState(const qint32 planeID, const CPhysicalState &state) = 0; + + virtual bool setAnimationState(const qint32 planeID, const CAnimationState &state) = 0; + + // Calls the supplied visitor function once for every model available in the simulator. + virtual void visitAllModels(const std::tr1::function &visitor) = 0; + + // Fills container with all models. Works for any container that supports push_back. + template + void getAllModels(T &container) { visitAllModels(std::tr1::bind(T::push_back, container)); } - static ISimulator *createDriver(ESimulator sim); - protected: - - CLibraryContext * mLibraryContext; - - cbSimStarted mSimStarted; - cbChangedRadioFreq mChangedRadioFreq; - cbChangedGearPosition mChangedGearPosition; - cbChangedLights mChangedLights; - cbChangedAircraftType mChangedAircraftType; - cbChangedFlaps mChangedFlaps; - - void *m_CallbackContext; + BlackMisc::IContext *m_libraryContext; + cbSimStarted m_cbSimStarted; + cbChangedAvionicsState m_cbChangedAvionicsState; + cbChangedAnimationState m_cbChangedAnimationState; + cbChangedModel m_cbChangedModel; }; } //! namespace BlackCore diff --git a/blacklib/include/blackcore/vector_geo.h b/blacklib/include/blackcore/vector_geo.h index 293443159..21f328982 100644 --- a/blacklib/include/blackcore/vector_geo.h +++ b/blacklib/include/blackcore/vector_geo.h @@ -6,6 +6,8 @@ #ifndef VECTOR_GEO_H #define VECTOR_GEO_H +#include + namespace BlackCore { @@ -15,40 +17,40 @@ class CVectorGeo { public: CVectorGeo(); - CVectorGeo(double lat, double lon, double alt); - CVectorGeo( const CVectorGeo &other ); + CVectorGeo(double latitudeDegrees, double longintudeDegrees, double altitudeMeters); + CVectorGeo(const CVectorGeo &other); - void setLatitude( double latitude) + void setLatitudeDegrees(double latitudeDegrees) { - m_latitude = latitude; + m_latitudeDegrees = latitudeDegrees; } - void setLongitude( double longitude) + void setLongitudeDegrees(double longitudeDegrees) { - m_longitude = longitude; + m_longitudeDegrees = longitudeDegrees; } - void setAltitude( double altitude) + void setAltitude(double altitudeMeters) { - m_altitude = altitude; + m_altitudeMeters = altitudeMeters; } - double latitude() const {return m_latitude;} - double longitude() const {return m_longitude;} - double altitude() const {return m_altitude;} + double latitudeDegrees() const { return m_latitudeDegrees; } + double longitudeDegrees() const { return m_longitudeDegrees; } + double altitudeMeters() const { return m_altitudeMeters; } CEcef toCartesian(); void zeros(); - void print(); + void print(std::ostream &stream = std::cout); - CVectorGeo & operator = (const CVectorGeo &rhs); + CVectorGeo &operator=(const CVectorGeo &rhs); private: - double m_latitude; - double m_longitude; - double m_altitude; + double m_latitudeDegrees; + double m_longitudeDegrees; + double m_altitudeMeters; }; } // namespace BlackCore diff --git a/blacklib/src/blackcore/ecef.cpp b/blacklib/src/blackcore/ecef.cpp index 6bd1ff5ae..b36311e06 100644 --- a/blacklib/src/blackcore/ecef.cpp +++ b/blacklib/src/blackcore/ecef.cpp @@ -32,7 +32,7 @@ CNed CEcef::toNED(const CVectorGeo &pos) { CNed result; - double angle = - ( pos.latitude() * Constants::DegToRad ) - Constants::PI/2; + double angle = - ( pos.latitudeDegrees() * Constants::DegToRad ) - Constants::PI/2; CMatrix3D dcm1; CMatrix3D dcm2; @@ -55,7 +55,7 @@ CNed CEcef::toNED(const CVectorGeo &pos) dcm2(2,0) = sin( angle ); dcm2(2,2) = cos( angle ); - angle = pos.longitude() * Constants::DegToRad; + angle = pos.longitudeDegrees() * Constants::DegToRad; dcm3(0,0) = cos(angle ); dcm3(0,1) = sin(angle ); @@ -103,8 +103,8 @@ CVectorGeo CEcef::toGeodetic() double sphi = 0; double cphi = 0; - double p = CMath::square(R / Constants::EarthRadius); - double q = Constants::e2m * CMath::square(v[2] / Constants::EarthRadius); + double p = CMath::square(R / Constants::EarthRadiusMeters); + double q = Constants::e2m * CMath::square(v[2] / Constants::EarthRadiusMeters); double r = (p + q - Constants::e4) / 6.0; if ( !(Constants::e4 * q == 0 && r <= 0) ) @@ -188,7 +188,7 @@ CVectorGeo CEcef::toGeodetic() sphi = zz / H; cphi = xx / H; if (v[2] < 0) sphi = -sphi; // for tiny negative Z (not for prolate) - h = - Constants::EarthRadius * (Constants::e2m) * H / Constants::e2absolute; + h = - Constants::EarthRadiusMeters * (Constants::e2m) * H / Constants::e2absolute; } double lat = atan2(sphi, cphi) * Constants::RadToDeg; @@ -196,8 +196,8 @@ CVectorGeo CEcef::toGeodetic() //! Negative signs return lon in [-180, 180). double lon = -atan2(-slam, clam) * Constants::RadToDeg; - result.setLongitude(lon); - result.setLatitude(lat); + result.setLongitudeDegrees(lon); + result.setLatitudeDegrees(lat); result.setAltitude(h); return result; diff --git a/blacklib/src/blackcore/ned.cpp b/blacklib/src/blackcore/ned.cpp index 521c87ddf..253b9d73c 100644 --- a/blacklib/src/blackcore/ned.cpp +++ b/blacklib/src/blackcore/ned.cpp @@ -24,7 +24,7 @@ namespace BlackCore CEcef CNed::toECEF() { - double angle = - ( m_position.latitude() * Constants::DegToRad ) - Constants::PI/2; + double angle = - ( m_position.latitudeDegrees() * Constants::DegToRad ) - Constants::PI/2; CMatrix3D dcm1; CMatrix3D dcm2; @@ -47,7 +47,7 @@ namespace BlackCore dcm2(2,0) = sin( angle ); dcm2(2,2) = cos( angle ); - angle = m_position.longitude() * Constants::DegToRad; + angle = m_position.longitudeDegrees() * Constants::DegToRad; dcm3(0,0) = cos(angle ); dcm3(0,1) = sin(angle ); diff --git a/blacklib/src/blackcore/simulator.cpp b/blacklib/src/blackcore/simulator.cpp index 62c284f02..8a8933767 100644 --- a/blacklib/src/blackcore/simulator.cpp +++ b/blacklib/src/blackcore/simulator.cpp @@ -32,6 +32,11 @@ namespace BlackCore { typedef quint32 (*getDriverVersionMinor)(void); const char *DRIVER_VERSION_MINOR = "BB_InterfaceVersionMinor"; + void ISimulator::setLibraryContext(BlackMisc::IContext *context) + { + m_libraryContext = context; + } + ISimulator *ISimulator::createDriver(ESimulator sim) { QLibrary myLib; @@ -108,55 +113,24 @@ namespace BlackCore { return result; } - //////////////////////////////// - // Global section - //////////////////////////////// - - // Callback when the Simulation starts - void ISimulator::setcbSimStarted(cbSimStarted func, void *context) + void ISimulator::setcbSimStarted(const cbSimStarted &func) { - mSimStarted = func; + m_cbSimStarted = func; } - //////////////////////////////// - // User plane section - //////////////////////////////// - // Callback frequency tuner - void ISimulator::setChangedRadioFreq(cbChangedRadioFreq func, void * context) + void ISimulator::setcbChangedAvionicsState(const cbChangedAvionicsState &func) { - mChangedRadioFreq = func; - m_CallbackContext = context; + m_cbChangedAvionicsState = func; } - // Callback when the gear is moving - void ISimulator::setcbChangedGearPosition(cbChangedGearPosition func, void *context) + void ISimulator::setcbChangedAnimationState(const cbChangedAnimationState &func) { - mChangedGearPosition = func; - m_CallbackContext = context; + m_cbChangedAnimationState = func; } - // Callback if the user switched on/off a light - void ISimulator::setcbChangedLights(cbChangedLights func, void *context) + void ISimulator::setcbChangedModel(const cbChangedModel &func) { - mChangedLights = func; - m_CallbackContext = context; + m_cbChangedModel = func; } - // Callback, when the Aircraft is set or gets changed - void ISimulator::setcbChangedAircraftType(cbChangedAircraftType func, void *context) - { - mChangedAircraftType = func; - m_CallbackContext = context; - } - - // Callback, when the Flaps are moving - void ISimulator::setcbChangedFlaps(cbChangedFlaps func, void *context) - { - mChangedFlaps = func; - m_CallbackContext = context; - } - -} //! namespace BlackCore { - - - +} //! namespace BlackCore diff --git a/blacklib/src/blackcore/vector_geo.cpp b/blacklib/src/blackcore/vector_geo.cpp index 967edd6a5..fb4f6c93d 100644 --- a/blacklib/src/blackcore/vector_geo.cpp +++ b/blacklib/src/blackcore/vector_geo.cpp @@ -9,48 +9,41 @@ namespace BlackCore { CVectorGeo::CVectorGeo() - { - zeros(); - } + : m_latitudeDegrees(0), m_longitudeDegrees(0), m_altitudeMeters(0) + {} - CVectorGeo::CVectorGeo(double lat, double lon, double alt) - { - zeros(); - m_latitude = lat; - m_longitude = lon; - m_altitude = alt; - } + CVectorGeo::CVectorGeo(double latitudeDegrees, double longitudeDegrees, double altitudeMeters) + : m_latitudeDegrees(latitudeDegrees), m_longitudeDegrees(longitudeDegrees), + m_altitudeMeters(altitudeMeters) + {} CVectorGeo::CVectorGeo(const CVectorGeo &other) - { - zeros(); - m_latitude = other.latitude(); - m_longitude = other.longitude(); - m_altitude = other.altitude(); - } + : m_latitudeDegrees(other.m_latitudeDegrees), m_longitudeDegrees(other.m_longitudeDegrees), + m_altitudeMeters(other.m_altitudeMeters) + {} CEcef CVectorGeo::toCartesian() { CEcef result; - double phi = m_latitude * Constants::DegToRad; - double lambda = m_longitude * Constants::DegToRad; + double phi = m_latitudeDegrees * Constants::DegToRad; + double lambda = m_longitudeDegrees * Constants::DegToRad; double sphi = sin(phi); double cphi = 0; - if (std::abs(m_latitude) != 90) + if (std::abs(m_latitudeDegrees) != 90) cphi = cos(phi); - double n = Constants::EarthRadius/sqrt(1-Constants::e2 * CMath::square(sphi)); + double n = Constants::EarthRadiusMeters/sqrt(1-Constants::e2 * CMath::square(sphi)); double slambda = 0; - if (m_longitude != -180) + if (m_longitudeDegrees != -180) slambda = sin(lambda); double clambda = 0; - if (std::abs(m_longitude) != 90) + if (std::abs(m_longitudeDegrees) != 90) clambda = cos(lambda); - double h = m_altitude * Constants::FeetToMeter; + double h = m_altitudeMeters; double X = (n + h) * cphi; double Y = X * slambda; @@ -65,27 +58,27 @@ namespace BlackCore void CVectorGeo::zeros() { - m_latitude = 0; - m_longitude = 0; - m_altitude = 0; + m_latitudeDegrees = 0; + m_longitudeDegrees = 0; + m_altitudeMeters = 0; } - void CVectorGeo::print() -{ - std::cout << "v = " << std::endl; - std::cout << std::fixed; - std::cout << "[" << m_latitude << "]" << std::endl; - std::cout << "[" << m_longitude << "]" << std::endl; - std::cout << "[" << m_altitude << "]" << std::endl; -} + void CVectorGeo::print(std::ostream &stream) + { + stream << "v = " << std::endl; + stream << std::fixed; + stream << "[" << m_latitudeDegrees << "]" << std::endl; + stream << "[" << m_longitudeDegrees << "]" << std::endl; + stream << "[" << m_altitudeMeters << "]" << std::endl; + } CVectorGeo &CVectorGeo::operator =(const CVectorGeo &rhs) { if (this != &rhs) { - m_latitude = rhs.latitude(); - m_longitude = rhs.longitude(); - m_altitude = rhs.altitude(); + m_latitudeDegrees = rhs.latitudeDegrees(); + m_longitudeDegrees = rhs.longitudeDegrees(); + m_altitudeMeters = rhs.altitudeMeters(); } return *this; }