From 413ef5a3b466e0e0678112041d2db840388b26a2 Mon Sep 17 00:00:00 2001 From: Peter Barker Date: Fri, 26 Oct 2018 13:31:20 +1100 Subject: [PATCH] AC_Avoid: add singleton --- libraries/AC_Avoidance/AC_Avoid.cpp | 14 ++++++++++++++ libraries/AC_Avoidance/AC_Avoid.h | 11 +++++++++++ 2 files changed, 25 insertions(+) diff --git a/libraries/AC_Avoidance/AC_Avoid.cpp b/libraries/AC_Avoidance/AC_Avoid.cpp index 58446411e7..42233a47d8 100644 --- a/libraries/AC_Avoidance/AC_Avoid.cpp +++ b/libraries/AC_Avoidance/AC_Avoid.cpp @@ -57,6 +57,8 @@ AC_Avoid::AC_Avoid(const AP_AHRS& ahrs, const AC_Fence& fence, const AP_Proximit _proximity(proximity), _beacon(beacon) { + _singleton = this; + AP_Param::setup_object_defaults(this, var_info); } @@ -571,3 +573,15 @@ void AC_Avoid::get_proximity_roll_pitch_pct(float &roll_positive, float &roll_ne } } } + +// singleton instance +AC_Avoid *AC_Avoid::_singleton; + +namespace AP { + +AC_Avoid *ac_avoid() +{ + return AC_Avoid::get_singleton(); +} + +} diff --git a/libraries/AC_Avoidance/AC_Avoid.h b/libraries/AC_Avoidance/AC_Avoid.h index 55ec0e3d9d..1469ca67a9 100644 --- a/libraries/AC_Avoidance/AC_Avoid.h +++ b/libraries/AC_Avoidance/AC_Avoid.h @@ -34,6 +34,11 @@ public: AC_Avoid(const AC_Avoid &other) = delete; AC_Avoid &operator=(const AC_Avoid&) = delete; + // get singleton instance + static AC_Avoid *get_singleton() { + return _singleton; + } + /* * Adjusts the desired velocity so that the vehicle can stop * before the fence/object. @@ -140,4 +145,10 @@ private: AP_Int8 _behavior; // avoidance behaviour (slide or stop) bool _proximity_enabled = true; // true if proximity sensor based avoidance is enabled (used to allow pilot to enable/disable) + + static AC_Avoid *_singleton; +}; + +namespace AP { + AC_Avoid *ac_avoid(); };