diff --git a/libraries/AP_Gripper/AP_Gripper.cpp b/libraries/AP_Gripper/AP_Gripper.cpp index 929ff4d7c3..568302c4a8 100644 --- a/libraries/AP_Gripper/AP_Gripper.cpp +++ b/libraries/AP_Gripper/AP_Gripper.cpp @@ -70,9 +70,26 @@ const AP_Param::GroupInfo AP_Gripper::var_info[] = { AP_Gripper::AP_Gripper() { + if (_s_instance) { +#if CONFIG_HAL_BOARD == HAL_BOARD_SITL + AP_HAL::panic("Too many grippers"); +#endif + return; + } + _s_instance = this; + AP_Param::setup_object_defaults(this, var_info); } +/* + * Get the AP_Gripper singleton + */ +AP_Gripper *AP_Gripper::_s_instance = nullptr; +AP_Gripper *AP_Gripper::get_instance() +{ + return _s_instance; +} + void AP_Gripper::init() { // return immediately if not enabled @@ -133,3 +150,12 @@ PASS_TO_BACKEND(released) PASS_TO_BACKEND(grabbed) #undef PASS_TO_BACKEND + +namespace AP { + +AP_Gripper *gripper() +{ + return AP_Gripper::get_instance(); +} + +}; diff --git a/libraries/AP_Gripper/AP_Gripper.h b/libraries/AP_Gripper/AP_Gripper.h index fde973abf7..8db9a521ba 100644 --- a/libraries/AP_Gripper/AP_Gripper.h +++ b/libraries/AP_Gripper/AP_Gripper.h @@ -23,6 +23,12 @@ class AP_Gripper { public: AP_Gripper(); + AP_Gripper(const AP_Gripper &other) = delete; + AP_Gripper &operator=(const AP_Gripper&) = delete; + + static AP_Gripper *get_instance(); + static AP_Gripper *_s_instance; + // indicate whether this module is enabled or not bool enabled() const { return _enabled; } @@ -74,3 +80,7 @@ private: AP_Gripper_Backend *backend; }; + +namespace AP { + AP_Gripper *gripper(); +};