Browse Source

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.
mission-4.1.18
Lucas De Marchi 10 years ago committed by Andrew Tridgell
parent
commit
c66800dfec
  1. 15
      libraries/AP_InertialSensor/AP_InertialSensor.cpp
  2. 3
      libraries/AP_InertialSensor/AP_InertialSensor.h

15
libraries/AP_InertialSensor/AP_InertialSensor.cpp

@ -311,6 +311,8 @@ const AP_Param::GroupInfo AP_InertialSensor::var_info[] PROGMEM = { @@ -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() : @@ -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() : @@ -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

3
libraries/AP_InertialSensor/AP_InertialSensor.h

@ -54,6 +54,7 @@ class AP_InertialSensor @@ -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: @@ -378,6 +379,8 @@ private:
} _hil {};
DataFlash_Class *_dataflash;
static AP_InertialSensor *_s_instance;
};
#include "AP_InertialSensor_Backend.h"

Loading…
Cancel
Save