Browse Source

AP_Gripper: add singleton

master
Peter Barker 7 years ago committed by Francisco Ferreira
parent
commit
8a3df17a07
  1. 26
      libraries/AP_Gripper/AP_Gripper.cpp
  2. 10
      libraries/AP_Gripper/AP_Gripper.h

26
libraries/AP_Gripper/AP_Gripper.cpp

@ -70,9 +70,26 @@ const AP_Param::GroupInfo AP_Gripper::var_info[] = { @@ -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) @@ -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();
}
};

10
libraries/AP_Gripper/AP_Gripper.h

@ -23,6 +23,12 @@ class AP_Gripper { @@ -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: @@ -74,3 +80,7 @@ private:
AP_Gripper_Backend *backend;
};
namespace AP {
AP_Gripper *gripper();
};

Loading…
Cancel
Save