AP_Gripper: add singleton

This commit is contained in:
Peter Barker 2018-06-08 13:36:31 +10:00 committed by Francisco Ferreira
parent 289b93c3d0
commit 8a3df17a07
2 changed files with 36 additions and 0 deletions

View File

@ -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();
}
};

View File

@ -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();
};