diff --git a/libraries/AP_Mount/AP_Mount.cpp b/libraries/AP_Mount/AP_Mount.cpp index 10224c3e4a..2c2f67cce5 100644 --- a/libraries/AP_Mount/AP_Mount.cpp +++ b/libraries/AP_Mount/AP_Mount.cpp @@ -397,6 +397,14 @@ AP_Mount::AP_Mount(const AP_AHRS_TYPE &ahrs, const struct Location ¤t_loc) _ahrs(ahrs), _current_loc(current_loc) { + if (_singleton != nullptr) { +#if CONFIG_HAL_BOARD == HAL_BOARD_SITL + AP_HAL::panic("Mount must be singleton"); +#endif + return; + } + _singleton = this; + AP_Param::setup_object_defaults(this, var_info); } @@ -635,3 +643,16 @@ void AP_Mount::send_gimbal_report(mavlink_channel_t chan) } } } + + +// singleton instance +AP_Mount *AP_Mount::_singleton; + +namespace AP { + +AP_Mount *mount() +{ + return AP_Mount::get_singleton(); +} + +}; diff --git a/libraries/AP_Mount/AP_Mount.h b/libraries/AP_Mount/AP_Mount.h index 27697cd976..96bc2931bd 100644 --- a/libraries/AP_Mount/AP_Mount.h +++ b/libraries/AP_Mount/AP_Mount.h @@ -61,6 +61,10 @@ public: AP_Mount(const AP_Mount &other) = delete; AP_Mount &operator=(const AP_Mount&) = delete; + // get singleton instance + static AP_Mount *get_singleton() { + return _singleton; + } // Enums enum MountType { @@ -139,6 +143,9 @@ public: static const struct AP_Param::GroupInfo var_info[]; protected: + + static AP_Mount *_singleton; + // private members const AP_AHRS_TYPE &_ahrs; const struct Location &_current_loc; // reference to the vehicle's current location @@ -183,3 +190,7 @@ protected: struct Location _roi_target; // roi target location } state[AP_MOUNT_MAX_INSTANCES]; }; + +namespace AP { + AP_Mount *mount(); +};