AP_InertialSensor: add singleton interface

In order to allow other libraries to use the InertialSensor we need a
way to let them to get the only instance of InertialSensor. The
conventional way to do a singleton would be to let the constructor
private and force it to be instantiated from the get_instance() method.

Here however we just call panic() on the constructor if there's already
an instance alive. This allows us to let the vehicles as is. Later we
can change it so they call the get_instance() method instead.
This commit is contained in:
Lucas De Marchi 2015-08-12 15:04:29 -03:00 committed by Andrew Tridgell
parent 565c18603d
commit c66800dfec
2 changed files with 18 additions and 0 deletions

View File

@ -311,6 +311,8 @@ const AP_Param::GroupInfo AP_InertialSensor::var_info[] PROGMEM = {
AP_GROUPEND
};
AP_InertialSensor *AP_InertialSensor::_s_instance = nullptr;
AP_InertialSensor::AP_InertialSensor() :
_gyro_count(0),
_accel_count(0),
@ -326,6 +328,10 @@ AP_InertialSensor::AP_InertialSensor() :
_backends_detected(false),
_dataflash(NULL)
{
if (_s_instance) {
hal.scheduler->panic(PSTR("Too many inertial sensors"));
}
_s_instance = this;
AP_Param::setup_object_defaults(this, var_info);
for (uint8_t i=0; i<INS_MAX_BACKENDS; i++) {
_backends[i] = NULL;
@ -349,6 +355,15 @@ AP_InertialSensor::AP_InertialSensor() :
memset(_delta_angle_valid,0,sizeof(_delta_angle_valid));
}
/*
* Get the AP_InertialSensor singleton
*/
AP_InertialSensor *AP_InertialSensor::get_instance()
{
if (!_s_instance)
_s_instance = new AP_InertialSensor();
return _s_instance;
}
/*
register a new gyro instance

View File

@ -54,6 +54,7 @@ class AP_InertialSensor
public:
AP_InertialSensor();
static AP_InertialSensor *get_instance();
enum Start_style {
COLD_START = 0,
@ -378,6 +379,8 @@ private:
} _hil {};
DataFlash_Class *_dataflash;
static AP_InertialSensor *_s_instance;
};
#include "AP_InertialSensor_Backend.h"