2013-05-19 17:11:09 +02:00
|
|
|
/**
|
|
|
|
******************************************************************************
|
|
|
|
* @addtogroup OpenPilotModules OpenPilot Modules
|
|
|
|
* @{
|
|
|
|
* @addtogroup State Estimation
|
|
|
|
* @brief Acquires sensor data and computes state estimate
|
|
|
|
* @{
|
|
|
|
*
|
|
|
|
* @file stateestimation.c
|
|
|
|
* @author The OpenPilot Team, http://www.openpilot.org Copyright (C) 2013.
|
|
|
|
* @brief Module to handle all comms to the AHRS on a periodic basis.
|
|
|
|
*
|
|
|
|
* @see The GNU Public License (GPL) Version 3
|
|
|
|
*
|
|
|
|
******************************************************************************/
|
|
|
|
/*
|
|
|
|
* This program is free software; you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation; either version 3 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful, but
|
|
|
|
* WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
|
|
|
|
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
|
|
|
* for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License along
|
|
|
|
* with this program; if not, write to the Free Software Foundation, Inc.,
|
|
|
|
* 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
|
|
|
*/
|
|
|
|
|
2013-05-19 21:44:33 +02:00
|
|
|
#include "inc/stateestimation.h"
|
2013-05-19 20:38:32 +02:00
|
|
|
|
2014-02-02 19:54:17 +01:00
|
|
|
#include <callbackinfo.h>
|
|
|
|
|
2013-05-19 17:11:09 +02:00
|
|
|
#include <gyrosensor.h>
|
|
|
|
#include <accelsensor.h>
|
2013-05-20 10:35:54 +02:00
|
|
|
#include <magsensor.h>
|
2013-05-19 17:11:09 +02:00
|
|
|
#include <barosensor.h>
|
|
|
|
#include <airspeedsensor.h>
|
2013-06-30 18:49:29 +02:00
|
|
|
#include <gpspositionsensor.h>
|
2013-05-22 21:45:06 +02:00
|
|
|
#include <gpsvelocitysensor.h>
|
2014-03-23 12:56:01 +01:00
|
|
|
#include <homelocation.h>
|
2014-07-17 20:41:14 +02:00
|
|
|
#include <auxmagsensor.h>
|
2015-06-04 13:38:06 +02:00
|
|
|
#include <auxmagsettings.h>
|
2013-05-19 17:11:09 +02:00
|
|
|
|
2013-05-19 20:38:32 +02:00
|
|
|
#include <gyrostate.h>
|
|
|
|
#include <accelstate.h>
|
2013-05-20 10:35:54 +02:00
|
|
|
#include <magstate.h>
|
2013-05-19 20:38:32 +02:00
|
|
|
#include <airspeedstate.h>
|
2013-05-20 10:42:23 +02:00
|
|
|
#include <attitudestate.h>
|
2013-05-19 20:38:32 +02:00
|
|
|
#include <positionstate.h>
|
|
|
|
#include <velocitystate.h>
|
|
|
|
|
2013-05-19 17:11:09 +02:00
|
|
|
#include "revosettings.h"
|
2013-05-19 20:38:32 +02:00
|
|
|
#include "flightstatus.h"
|
2013-05-19 17:11:09 +02:00
|
|
|
|
2013-05-20 10:42:23 +02:00
|
|
|
#include "CoordinateConversions.h"
|
2013-05-19 17:11:09 +02:00
|
|
|
|
|
|
|
// Private constants
|
2014-03-25 21:00:10 +01:00
|
|
|
#define STACK_SIZE_BYTES 256
|
|
|
|
#define CALLBACK_PRIORITY CALLBACK_PRIORITY_REGULAR
|
|
|
|
#define TASK_PRIORITY CALLBACK_TASK_FLIGHTCONTROL
|
|
|
|
#define TIMEOUT_MS 10
|
|
|
|
|
|
|
|
// Private filter init const
|
|
|
|
#define FILTER_INIT_FORCE -1
|
|
|
|
#define FILTER_INIT_IF_POSSIBLE -2
|
2013-05-19 17:11:09 +02:00
|
|
|
|
2013-05-23 21:56:47 +02:00
|
|
|
// local macros, ONLY to be used in the middle of StateEstimationCb in section RUNSTATE_LOAD after the update of states updated!
|
|
|
|
#define FETCH_SENSOR_FROM_UAVOBJECT_CHECK_AND_LOAD_TO_STATE_3_DIMENSIONS(sensorname, shortname, a1, a2, a3) \
|
|
|
|
if (IS_SET(states.updated, SENSORUPDATES_##shortname)) { \
|
|
|
|
sensorname##Data s; \
|
|
|
|
sensorname##Get(&s); \
|
|
|
|
if (IS_REAL(s.a1) && IS_REAL(s.a2) && IS_REAL(s.a3)) { \
|
|
|
|
states.shortname[0] = s.a1; \
|
|
|
|
states.shortname[1] = s.a2; \
|
|
|
|
states.shortname[2] = s.a3; \
|
|
|
|
} \
|
|
|
|
else { \
|
|
|
|
UNSET_MASK(states.updated, SENSORUPDATES_##shortname); \
|
|
|
|
} \
|
|
|
|
}
|
2014-03-27 16:19:22 +01:00
|
|
|
|
2013-05-23 21:56:47 +02:00
|
|
|
#define FETCH_SENSOR_FROM_UAVOBJECT_CHECK_AND_LOAD_TO_STATE_1_DIMENSION_WITH_CUSTOM_EXTRA_CHECK(sensorname, shortname, a1, EXTRACHECK) \
|
|
|
|
if (IS_SET(states.updated, SENSORUPDATES_##shortname)) { \
|
|
|
|
sensorname##Data s; \
|
|
|
|
sensorname##Get(&s); \
|
|
|
|
if (IS_REAL(s.a1) && EXTRACHECK) { \
|
|
|
|
states.shortname[0] = s.a1; \
|
|
|
|
} \
|
|
|
|
else { \
|
|
|
|
UNSET_MASK(states.updated, SENSORUPDATES_##shortname); \
|
|
|
|
} \
|
|
|
|
}
|
|
|
|
|
2014-03-26 09:15:56 +01:00
|
|
|
#define FETCH_SENSOR_FROM_UAVOBJECT_CHECK_AND_LOAD_TO_STATE_2_DIMENSION_WITH_CUSTOM_EXTRA_CHECK(sensorname, shortname, a1, a2, EXTRACHECK) \
|
|
|
|
if (IS_SET(states.updated, SENSORUPDATES_##shortname)) { \
|
|
|
|
sensorname##Data s; \
|
|
|
|
sensorname##Get(&s); \
|
|
|
|
if (IS_REAL(s.a1) && IS_REAL(s.a2) && EXTRACHECK) { \
|
|
|
|
states.shortname[0] = s.a1; \
|
|
|
|
states.shortname[1] = s.a2; \
|
|
|
|
} \
|
|
|
|
else { \
|
|
|
|
UNSET_MASK(states.updated, SENSORUPDATES_##shortname); \
|
|
|
|
} \
|
|
|
|
}
|
2013-05-23 21:56:47 +02:00
|
|
|
|
|
|
|
// local macros, ONLY to be used in the middle of StateEstimationCb in section RUNSTATE_SAVE before the check of alarms!
|
|
|
|
#define EXPORT_STATE_TO_UAVOBJECT_IF_UPDATED_3_DIMENSIONS(statename, shortname, a1, a2, a3) \
|
|
|
|
if (IS_SET(states.updated, SENSORUPDATES_##shortname)) { \
|
|
|
|
statename##Data s; \
|
|
|
|
statename##Get(&s); \
|
|
|
|
s.a1 = states.shortname[0]; \
|
|
|
|
s.a2 = states.shortname[1]; \
|
|
|
|
s.a3 = states.shortname[2]; \
|
|
|
|
statename##Set(&s); \
|
|
|
|
}
|
2014-03-27 16:19:22 +01:00
|
|
|
|
2013-05-23 21:56:47 +02:00
|
|
|
#define EXPORT_STATE_TO_UAVOBJECT_IF_UPDATED_2_DIMENSIONS(statename, shortname, a1, a2) \
|
|
|
|
if (IS_SET(states.updated, SENSORUPDATES_##shortname)) { \
|
|
|
|
statename##Data s; \
|
|
|
|
statename##Get(&s); \
|
|
|
|
s.a1 = states.shortname[0]; \
|
|
|
|
s.a2 = states.shortname[1]; \
|
|
|
|
statename##Set(&s); \
|
|
|
|
}
|
|
|
|
|
2013-05-19 17:11:09 +02:00
|
|
|
|
2014-03-27 09:32:17 +01:00
|
|
|
// Private types
|
2013-05-22 19:05:28 +02:00
|
|
|
struct filterPipelineStruct;
|
2013-05-19 21:44:33 +02:00
|
|
|
|
2013-05-22 19:05:28 +02:00
|
|
|
typedef const struct filterPipelineStruct {
|
|
|
|
const stateFilter *filter;
|
|
|
|
const struct filterPipelineStruct *next;
|
|
|
|
} filterPipeline;
|
2013-05-19 21:44:33 +02:00
|
|
|
|
2013-05-19 17:11:09 +02:00
|
|
|
// Private variables
|
|
|
|
static DelayedCallbackInfo *stateEstimationCallback;
|
|
|
|
|
2013-05-19 20:38:32 +02:00
|
|
|
static volatile RevoSettingsData revoSettings;
|
2013-05-19 17:11:09 +02:00
|
|
|
static volatile sensorUpdates updatedSensors;
|
2014-04-10 20:13:23 +02:00
|
|
|
static volatile int32_t fusionAlgorithm = -1;
|
|
|
|
static const filterPipeline *filterChain = NULL;
|
2013-05-19 20:38:32 +02:00
|
|
|
|
|
|
|
// different filters available to state estimation
|
2013-05-19 21:44:33 +02:00
|
|
|
static stateFilter magFilter;
|
|
|
|
static stateFilter baroFilter;
|
2014-04-08 18:31:11 +02:00
|
|
|
static stateFilter baroiFilter;
|
2014-09-19 16:34:20 +02:00
|
|
|
static stateFilter velocityFilter;
|
2013-06-28 17:22:16 +02:00
|
|
|
static stateFilter altitudeFilter;
|
2013-05-19 21:44:33 +02:00
|
|
|
static stateFilter airFilter;
|
|
|
|
static stateFilter stationaryFilter;
|
2013-06-30 18:49:29 +02:00
|
|
|
static stateFilter llaFilter;
|
2013-05-19 21:44:33 +02:00
|
|
|
static stateFilter cfFilter;
|
|
|
|
static stateFilter cfmFilter;
|
|
|
|
static stateFilter ekf13iFilter;
|
|
|
|
static stateFilter ekf13Filter;
|
2013-05-19 20:38:32 +02:00
|
|
|
|
2014-05-01 17:43:23 +02:00
|
|
|
// this is a hack to provide a computational shortcut for faster gyro state progression
|
|
|
|
static float gyroRaw[3];
|
|
|
|
static float gyroDelta[3];
|
|
|
|
|
2013-05-19 20:38:32 +02:00
|
|
|
// preconfigured filter chains selectable via revoSettings.FusionAlgorithm
|
2014-04-10 20:13:23 +02:00
|
|
|
static const filterPipeline *cfQueue = &(filterPipeline) {
|
2014-05-06 18:46:58 +02:00
|
|
|
.filter = &airFilter,
|
2013-05-22 19:05:28 +02:00
|
|
|
.next = &(filterPipeline) {
|
2014-05-06 18:46:58 +02:00
|
|
|
.filter = &baroiFilter,
|
2013-05-22 19:05:28 +02:00
|
|
|
.next = &(filterPipeline) {
|
2014-05-06 18:46:58 +02:00
|
|
|
.filter = &altitudeFilter,
|
2013-05-22 19:05:28 +02:00
|
|
|
.next = &(filterPipeline) {
|
2014-05-06 18:46:58 +02:00
|
|
|
.filter = &cfFilter,
|
|
|
|
.next = NULL,
|
2014-04-08 18:31:11 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
static const filterPipeline *cfmiQueue = &(filterPipeline) {
|
|
|
|
.filter = &magFilter,
|
|
|
|
.next = &(filterPipeline) {
|
|
|
|
.filter = &airFilter,
|
|
|
|
.next = &(filterPipeline) {
|
|
|
|
.filter = &baroiFilter,
|
|
|
|
.next = &(filterPipeline) {
|
|
|
|
.filter = &altitudeFilter,
|
|
|
|
.next = &(filterPipeline) {
|
|
|
|
.filter = &cfmFilter,
|
|
|
|
.next = NULL,
|
2013-06-28 17:22:16 +02:00
|
|
|
}
|
|
|
|
}
|
2013-05-19 20:38:32 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
2013-05-22 19:05:28 +02:00
|
|
|
static const filterPipeline *cfmQueue = &(filterPipeline) {
|
2013-05-19 21:44:33 +02:00
|
|
|
.filter = &magFilter,
|
2013-05-22 19:05:28 +02:00
|
|
|
.next = &(filterPipeline) {
|
2013-05-20 12:36:20 +02:00
|
|
|
.filter = &airFilter,
|
2013-05-22 19:05:28 +02:00
|
|
|
.next = &(filterPipeline) {
|
2013-06-30 18:49:29 +02:00
|
|
|
.filter = &llaFilter,
|
2013-05-22 19:05:28 +02:00
|
|
|
.next = &(filterPipeline) {
|
2013-06-30 18:49:29 +02:00
|
|
|
.filter = &baroFilter,
|
2013-06-28 17:22:16 +02:00
|
|
|
.next = &(filterPipeline) {
|
2013-07-10 14:38:12 +02:00
|
|
|
.filter = &altitudeFilter,
|
|
|
|
.next = &(filterPipeline) {
|
|
|
|
.filter = &cfmFilter,
|
|
|
|
.next = NULL,
|
|
|
|
}
|
2013-06-28 17:22:16 +02:00
|
|
|
}
|
2013-05-19 20:38:32 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
2013-05-22 19:05:28 +02:00
|
|
|
static const filterPipeline *ekf13iQueue = &(filterPipeline) {
|
2013-05-19 21:44:33 +02:00
|
|
|
.filter = &magFilter,
|
2013-05-22 19:05:28 +02:00
|
|
|
.next = &(filterPipeline) {
|
2013-05-20 12:36:20 +02:00
|
|
|
.filter = &airFilter,
|
2013-05-22 19:05:28 +02:00
|
|
|
.next = &(filterPipeline) {
|
2014-04-08 18:31:11 +02:00
|
|
|
.filter = &baroiFilter,
|
2013-05-22 19:05:28 +02:00
|
|
|
.next = &(filterPipeline) {
|
2014-04-08 18:31:11 +02:00
|
|
|
.filter = &stationaryFilter,
|
2013-05-22 19:05:28 +02:00
|
|
|
.next = &(filterPipeline) {
|
2014-04-08 18:31:11 +02:00
|
|
|
.filter = &ekf13iFilter,
|
2014-09-21 19:26:01 +02:00
|
|
|
.next = &(filterPipeline) {
|
|
|
|
.filter = &velocityFilter,
|
|
|
|
.next = NULL,
|
|
|
|
}
|
2013-05-19 20:38:32 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
2014-04-29 22:25:49 +02:00
|
|
|
|
2013-05-22 19:05:28 +02:00
|
|
|
static const filterPipeline *ekf13Queue = &(filterPipeline) {
|
2013-05-19 21:44:33 +02:00
|
|
|
.filter = &magFilter,
|
2013-05-22 19:05:28 +02:00
|
|
|
.next = &(filterPipeline) {
|
2013-05-20 12:36:20 +02:00
|
|
|
.filter = &airFilter,
|
2013-05-22 19:05:28 +02:00
|
|
|
.next = &(filterPipeline) {
|
2013-06-30 18:49:29 +02:00
|
|
|
.filter = &llaFilter,
|
2013-05-22 19:05:28 +02:00
|
|
|
.next = &(filterPipeline) {
|
2013-06-30 18:49:29 +02:00
|
|
|
.filter = &baroFilter,
|
|
|
|
.next = &(filterPipeline) {
|
|
|
|
.filter = &ekf13Filter,
|
2014-09-19 16:34:20 +02:00
|
|
|
.next = &(filterPipeline) {
|
|
|
|
.filter = &velocityFilter,
|
|
|
|
.next = NULL,
|
|
|
|
}
|
2013-06-30 18:49:29 +02:00
|
|
|
}
|
2013-05-19 20:38:32 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
2013-05-19 17:11:09 +02:00
|
|
|
|
|
|
|
// Private functions
|
|
|
|
|
|
|
|
static void settingsUpdatedCb(UAVObjEvent *objEv);
|
|
|
|
static void sensorUpdatedCb(UAVObjEvent *objEv);
|
2015-06-04 13:38:06 +02:00
|
|
|
static void criticalConfigUpdatedCb(UAVObjEvent *objEv);
|
2013-05-19 17:11:09 +02:00
|
|
|
static void StateEstimationCb(void);
|
|
|
|
|
2013-05-22 21:11:48 +02:00
|
|
|
static inline int32_t maxint32_t(int32_t a, int32_t b)
|
2013-05-22 19:05:28 +02:00
|
|
|
{
|
|
|
|
if (a > b) {
|
|
|
|
return a;
|
|
|
|
}
|
|
|
|
return b;
|
|
|
|
}
|
2013-05-19 17:11:09 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Initialise the module. Called before the start function
|
|
|
|
* \returns 0 on success or -1 if initialisation failed
|
|
|
|
*/
|
|
|
|
int32_t StateEstimationInitialize(void)
|
|
|
|
{
|
|
|
|
RevoSettingsInitialize();
|
2013-05-20 20:18:38 +02:00
|
|
|
|
|
|
|
GyroSensorInitialize();
|
|
|
|
MagSensorInitialize();
|
2014-07-17 20:41:14 +02:00
|
|
|
AuxMagSensorInitialize();
|
2013-05-20 20:18:38 +02:00
|
|
|
BaroSensorInitialize();
|
|
|
|
AirspeedSensorInitialize();
|
2013-05-22 21:45:06 +02:00
|
|
|
GPSVelocitySensorInitialize();
|
2013-06-30 18:49:29 +02:00
|
|
|
GPSPositionSensorInitialize();
|
2013-05-20 20:18:38 +02:00
|
|
|
|
2014-03-23 12:56:01 +01:00
|
|
|
HomeLocationInitialize();
|
|
|
|
|
2013-05-20 20:18:38 +02:00
|
|
|
GyroStateInitialize();
|
|
|
|
AccelStateInitialize();
|
|
|
|
MagStateInitialize();
|
|
|
|
AirspeedStateInitialize();
|
|
|
|
PositionStateInitialize();
|
|
|
|
VelocityStateInitialize();
|
2015-06-05 09:28:05 +02:00
|
|
|
AuxMagSettingsInitialize();
|
2013-05-19 17:11:09 +02:00
|
|
|
|
|
|
|
RevoSettingsConnectCallback(&settingsUpdatedCb);
|
|
|
|
|
2015-06-04 13:38:06 +02:00
|
|
|
HomeLocationConnectCallback(&criticalConfigUpdatedCb);
|
|
|
|
AuxMagSettingsConnectCallback(&criticalConfigUpdatedCb);
|
2014-03-23 12:56:01 +01:00
|
|
|
|
2013-05-19 17:11:09 +02:00
|
|
|
GyroSensorConnectCallback(&sensorUpdatedCb);
|
|
|
|
AccelSensorConnectCallback(&sensorUpdatedCb);
|
2013-05-20 10:35:54 +02:00
|
|
|
MagSensorConnectCallback(&sensorUpdatedCb);
|
2013-05-19 17:11:09 +02:00
|
|
|
BaroSensorConnectCallback(&sensorUpdatedCb);
|
|
|
|
AirspeedSensorConnectCallback(&sensorUpdatedCb);
|
2014-07-17 20:52:10 +02:00
|
|
|
AuxMagSensorConnectCallback(&sensorUpdatedCb);
|
2013-05-22 21:45:06 +02:00
|
|
|
GPSVelocitySensorConnectCallback(&sensorUpdatedCb);
|
2013-06-30 18:49:29 +02:00
|
|
|
GPSPositionSensorConnectCallback(&sensorUpdatedCb);
|
2013-05-19 17:11:09 +02:00
|
|
|
|
2013-05-22 19:05:28 +02:00
|
|
|
uint32_t stack_required = STACK_SIZE_BYTES;
|
|
|
|
// Initialize Filters
|
|
|
|
stack_required = maxint32_t(stack_required, filterMagInitialize(&magFilter));
|
2014-04-08 18:31:11 +02:00
|
|
|
stack_required = maxint32_t(stack_required, filterBaroiInitialize(&baroiFilter));
|
2013-05-22 19:05:28 +02:00
|
|
|
stack_required = maxint32_t(stack_required, filterBaroInitialize(&baroFilter));
|
2014-09-19 16:34:20 +02:00
|
|
|
stack_required = maxint32_t(stack_required, filterVelocityInitialize(&velocityFilter));
|
2013-06-28 17:22:16 +02:00
|
|
|
stack_required = maxint32_t(stack_required, filterAltitudeInitialize(&altitudeFilter));
|
2013-05-22 19:05:28 +02:00
|
|
|
stack_required = maxint32_t(stack_required, filterAirInitialize(&airFilter));
|
|
|
|
stack_required = maxint32_t(stack_required, filterStationaryInitialize(&stationaryFilter));
|
2013-06-30 18:49:29 +02:00
|
|
|
stack_required = maxint32_t(stack_required, filterLLAInitialize(&llaFilter));
|
2013-05-22 19:05:28 +02:00
|
|
|
stack_required = maxint32_t(stack_required, filterCFInitialize(&cfFilter));
|
|
|
|
stack_required = maxint32_t(stack_required, filterCFMInitialize(&cfmFilter));
|
|
|
|
stack_required = maxint32_t(stack_required, filterEKF13iInitialize(&ekf13iFilter));
|
|
|
|
stack_required = maxint32_t(stack_required, filterEKF13Initialize(&ekf13Filter));
|
|
|
|
|
2014-02-02 22:08:16 +01:00
|
|
|
stateEstimationCallback = PIOS_CALLBACKSCHEDULER_Create(&StateEstimationCb, CALLBACK_PRIORITY, TASK_PRIORITY, CALLBACKINFO_RUNNING_STATEESTIMATION, stack_required);
|
2013-05-19 17:11:09 +02:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Start the task. Expects all objects to be initialized by this point.
|
|
|
|
* \returns 0 on success or -1 if initialisation failed
|
|
|
|
*/
|
|
|
|
int32_t StateEstimationStart(void)
|
|
|
|
{
|
|
|
|
RevoSettingsConnectCallback(&settingsUpdatedCb);
|
|
|
|
|
|
|
|
// Force settings update to make sure rotation loaded
|
|
|
|
settingsUpdatedCb(NULL);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2013-06-06 19:25:54 +02:00
|
|
|
MODULE_INITCALL(StateEstimationInitialize, StateEstimationStart);
|
2013-05-19 17:11:09 +02:00
|
|
|
|
2013-05-23 21:56:47 +02:00
|
|
|
|
2013-05-19 17:11:09 +02:00
|
|
|
/**
|
|
|
|
* Module callback
|
|
|
|
*/
|
|
|
|
static void StateEstimationCb(void)
|
|
|
|
{
|
2014-04-30 08:49:50 +02:00
|
|
|
static filterResult alarm = FILTERRESULT_OK;
|
2014-04-29 22:25:49 +02:00
|
|
|
static filterResult lastAlarm = FILTERRESULT_UNINITIALISED;
|
2014-04-30 08:49:50 +02:00
|
|
|
static uint16_t alarmcounter = 0;
|
2014-04-10 20:13:23 +02:00
|
|
|
static const filterPipeline *current;
|
2013-05-22 19:05:28 +02:00
|
|
|
static stateEstimation states;
|
2013-05-22 22:58:14 +02:00
|
|
|
static uint32_t last_time;
|
2013-06-09 21:49:04 +02:00
|
|
|
static uint16_t bootDelay = 64;
|
|
|
|
|
|
|
|
// after system startup, first few sensor readings might be messed up, delay until everything has settled
|
|
|
|
if (bootDelay) {
|
|
|
|
bootDelay--;
|
2014-02-02 22:08:16 +01:00
|
|
|
PIOS_CALLBACKSCHEDULER_Schedule(stateEstimationCallback, TIMEOUT_MS, CALLBACK_UPDATEMODE_SOONER);
|
2013-06-09 21:49:04 +02:00
|
|
|
return;
|
|
|
|
}
|
2013-05-19 17:11:09 +02:00
|
|
|
|
2015-02-26 06:58:57 +01:00
|
|
|
alarm = FILTERRESULT_OK;
|
2013-05-19 17:11:09 +02:00
|
|
|
|
2015-02-26 06:58:57 +01:00
|
|
|
// set alarm to warning if called through timeout
|
|
|
|
if (updatedSensors == 0) {
|
|
|
|
if (PIOS_DELAY_DiffuS(last_time) > 1000 * TIMEOUT_MS) {
|
|
|
|
alarm = FILTERRESULT_WARNING;
|
2013-05-22 19:05:28 +02:00
|
|
|
}
|
2015-02-26 06:58:57 +01:00
|
|
|
} else {
|
|
|
|
last_time = PIOS_DELAY_GetRaw();
|
|
|
|
}
|
2013-05-22 19:05:28 +02:00
|
|
|
|
2015-02-26 06:58:57 +01:00
|
|
|
// check if a new filter chain should be initialized
|
|
|
|
if (fusionAlgorithm != revoSettings.FusionAlgorithm) {
|
|
|
|
FlightStatusData fs;
|
|
|
|
FlightStatusGet(&fs);
|
|
|
|
if (fs.Armed == FLIGHTSTATUS_ARMED_DISARMED || fusionAlgorithm == FILTER_INIT_FORCE) {
|
|
|
|
const filterPipeline *newFilterChain;
|
2015-02-27 04:01:37 +01:00
|
|
|
switch ((RevoSettingsFusionAlgorithmOptions)revoSettings.FusionAlgorithm) {
|
2015-02-26 06:58:57 +01:00
|
|
|
case REVOSETTINGS_FUSIONALGORITHM_BASICCOMPLEMENTARY:
|
|
|
|
newFilterChain = cfQueue;
|
|
|
|
break;
|
|
|
|
case REVOSETTINGS_FUSIONALGORITHM_COMPLEMENTARYMAG:
|
|
|
|
newFilterChain = cfmiQueue;
|
|
|
|
break;
|
|
|
|
case REVOSETTINGS_FUSIONALGORITHM_COMPLEMENTARYMAGGPSOUTDOOR:
|
|
|
|
newFilterChain = cfmQueue;
|
|
|
|
break;
|
|
|
|
case REVOSETTINGS_FUSIONALGORITHM_INS13INDOOR:
|
|
|
|
newFilterChain = ekf13iQueue;
|
|
|
|
break;
|
|
|
|
case REVOSETTINGS_FUSIONALGORITHM_GPSNAVIGATIONINS13:
|
|
|
|
newFilterChain = ekf13Queue;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
newFilterChain = NULL;
|
|
|
|
}
|
|
|
|
// initialize filters in chain
|
|
|
|
current = newFilterChain;
|
|
|
|
bool error = 0;
|
|
|
|
while (current != NULL) {
|
|
|
|
int32_t result = current->filter->init((stateFilter *)current->filter);
|
|
|
|
if (result != 0) {
|
|
|
|
error = 1;
|
2013-05-22 19:05:28 +02:00
|
|
|
break;
|
2013-05-19 20:38:32 +02:00
|
|
|
}
|
2015-02-26 06:58:57 +01:00
|
|
|
current = current->next;
|
|
|
|
}
|
|
|
|
if (error) {
|
|
|
|
AlarmsSet(SYSTEMALARMS_ALARM_ATTITUDE, SYSTEMALARMS_ALARM_ERROR);
|
|
|
|
return;
|
|
|
|
} else {
|
|
|
|
// set new fusion algortithm
|
|
|
|
filterChain = newFilterChain;
|
|
|
|
fusionAlgorithm = revoSettings.FusionAlgorithm;
|
2013-05-19 20:38:32 +02:00
|
|
|
}
|
|
|
|
}
|
2015-02-26 06:58:57 +01:00
|
|
|
}
|
2013-05-19 20:38:32 +02:00
|
|
|
|
2015-02-26 06:58:57 +01:00
|
|
|
// read updated sensor UAVObjects and set initial state
|
|
|
|
states.updated = updatedSensors;
|
|
|
|
updatedSensors = 0;
|
2013-05-19 17:11:09 +02:00
|
|
|
|
2015-02-26 06:58:57 +01:00
|
|
|
// fetch sensors, check values, and load into state struct
|
|
|
|
FETCH_SENSOR_FROM_UAVOBJECT_CHECK_AND_LOAD_TO_STATE_3_DIMENSIONS(GyroSensor, gyro, x, y, z);
|
|
|
|
if (IS_SET(states.updated, SENSORUPDATES_gyro)) {
|
|
|
|
gyroRaw[0] = states.gyro[0];
|
|
|
|
gyroRaw[1] = states.gyro[1];
|
|
|
|
gyroRaw[2] = states.gyro[2];
|
|
|
|
}
|
|
|
|
FETCH_SENSOR_FROM_UAVOBJECT_CHECK_AND_LOAD_TO_STATE_3_DIMENSIONS(AccelSensor, accel, x, y, z);
|
|
|
|
FETCH_SENSOR_FROM_UAVOBJECT_CHECK_AND_LOAD_TO_STATE_3_DIMENSIONS(MagSensor, boardMag, x, y, z);
|
|
|
|
FETCH_SENSOR_FROM_UAVOBJECT_CHECK_AND_LOAD_TO_STATE_3_DIMENSIONS(AuxMagSensor, auxMag, x, y, z);
|
|
|
|
FETCH_SENSOR_FROM_UAVOBJECT_CHECK_AND_LOAD_TO_STATE_3_DIMENSIONS(GPSVelocitySensor, vel, North, East, Down);
|
|
|
|
FETCH_SENSOR_FROM_UAVOBJECT_CHECK_AND_LOAD_TO_STATE_1_DIMENSION_WITH_CUSTOM_EXTRA_CHECK(BaroSensor, baro, Altitude, true);
|
|
|
|
FETCH_SENSOR_FROM_UAVOBJECT_CHECK_AND_LOAD_TO_STATE_2_DIMENSION_WITH_CUSTOM_EXTRA_CHECK(AirspeedSensor, airspeed, CalibratedAirspeed, TrueAirspeed, s.SensorConnected == AIRSPEEDSENSOR_SENSORCONNECTED_TRUE);
|
2013-05-22 19:05:28 +02:00
|
|
|
|
2015-02-26 06:58:57 +01:00
|
|
|
// GPS position data (LLA) is not fetched here since it does not contain floats. The filter must do all checks itself
|
2013-05-22 19:05:28 +02:00
|
|
|
|
2015-02-26 06:58:57 +01:00
|
|
|
// at this point sensor state is stored in "states" with some rudimentary filtering applied
|
2013-05-19 20:38:32 +02:00
|
|
|
|
2015-02-26 06:58:57 +01:00
|
|
|
// apply all filters in the current filter chain
|
|
|
|
current = filterChain;
|
2013-05-22 19:05:28 +02:00
|
|
|
|
2015-02-26 06:58:57 +01:00
|
|
|
// we are not done, re-dispatch self execution
|
2013-05-22 19:05:28 +02:00
|
|
|
|
2015-02-26 06:58:57 +01:00
|
|
|
while (current) {
|
|
|
|
filterResult result = current->filter->filter((stateFilter *)current->filter, &states);
|
|
|
|
if (result > alarm) {
|
|
|
|
alarm = result;
|
2014-07-17 20:41:14 +02:00
|
|
|
}
|
2015-02-26 06:58:57 +01:00
|
|
|
current = current->next;
|
|
|
|
}
|
2014-07-17 20:41:14 +02:00
|
|
|
|
2015-02-26 06:58:57 +01:00
|
|
|
// the final output of filters is saved in state variables
|
|
|
|
// EXPORT_STATE_TO_UAVOBJECT_IF_UPDATED_3_DIMENSIONS(GyroState, gyro, x, y, z) // replaced by performance shortcut
|
|
|
|
if (IS_SET(states.updated, SENSORUPDATES_gyro)) {
|
|
|
|
gyroDelta[0] = states.gyro[0] - gyroRaw[0];
|
|
|
|
gyroDelta[1] = states.gyro[1] - gyroRaw[1];
|
|
|
|
gyroDelta[2] = states.gyro[2] - gyroRaw[2];
|
|
|
|
}
|
|
|
|
EXPORT_STATE_TO_UAVOBJECT_IF_UPDATED_3_DIMENSIONS(AccelState, accel, x, y, z);
|
|
|
|
if (IS_SET(states.updated, SENSORUPDATES_mag)) {
|
|
|
|
MagStateData s;
|
|
|
|
|
|
|
|
MagStateGet(&s);
|
|
|
|
s.x = states.mag[0];
|
|
|
|
s.y = states.mag[1];
|
|
|
|
s.z = states.mag[2];
|
|
|
|
switch (states.magStatus) {
|
|
|
|
case MAGSTATUS_OK:
|
|
|
|
s.Source = MAGSTATE_SOURCE_ONBOARD;
|
|
|
|
break;
|
|
|
|
case MAGSTATUS_AUX:
|
|
|
|
s.Source = MAGSTATE_SOURCE_AUX;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
s.Source = MAGSTATE_SOURCE_INVALID;
|
2013-05-22 19:05:28 +02:00
|
|
|
}
|
2015-02-26 06:58:57 +01:00
|
|
|
MagStateSet(&s);
|
|
|
|
}
|
2013-05-19 17:11:09 +02:00
|
|
|
|
2015-02-26 06:58:57 +01:00
|
|
|
EXPORT_STATE_TO_UAVOBJECT_IF_UPDATED_3_DIMENSIONS(PositionState, pos, North, East, Down);
|
|
|
|
EXPORT_STATE_TO_UAVOBJECT_IF_UPDATED_3_DIMENSIONS(VelocityState, vel, North, East, Down);
|
|
|
|
EXPORT_STATE_TO_UAVOBJECT_IF_UPDATED_2_DIMENSIONS(AirspeedState, airspeed, CalibratedAirspeed, TrueAirspeed);
|
|
|
|
// attitude nees manual conversion from quaternion to euler
|
|
|
|
if (IS_SET(states.updated, SENSORUPDATES_attitude)) { \
|
|
|
|
AttitudeStateData s;
|
|
|
|
AttitudeStateGet(&s);
|
|
|
|
s.q1 = states.attitude[0];
|
|
|
|
s.q2 = states.attitude[1];
|
|
|
|
s.q3 = states.attitude[2];
|
|
|
|
s.q4 = states.attitude[3];
|
|
|
|
Quaternion2RPY(&s.q1, &s.Roll);
|
|
|
|
AttitudeStateSet(&s);
|
|
|
|
}
|
|
|
|
// throttle alarms, raise alarm flags immediately
|
|
|
|
// but require system to run for a while before decreasing
|
|
|
|
// to prevent alarm flapping
|
|
|
|
if (alarm >= lastAlarm) {
|
|
|
|
lastAlarm = alarm;
|
|
|
|
alarmcounter = 0;
|
|
|
|
} else {
|
|
|
|
if (alarmcounter < 100) {
|
|
|
|
alarmcounter++;
|
|
|
|
} else {
|
2013-05-21 07:27:31 +02:00
|
|
|
lastAlarm = alarm;
|
|
|
|
alarmcounter = 0;
|
|
|
|
}
|
2015-02-26 06:58:57 +01:00
|
|
|
}
|
2013-05-19 17:11:09 +02:00
|
|
|
|
2015-02-26 06:58:57 +01:00
|
|
|
// clear alarms if everything is alright, then schedule callback execution after timeout
|
|
|
|
if (lastAlarm == FILTERRESULT_WARNING) {
|
|
|
|
AlarmsSet(SYSTEMALARMS_ALARM_ATTITUDE, SYSTEMALARMS_ALARM_WARNING);
|
|
|
|
} else if (lastAlarm == FILTERRESULT_CRITICAL) {
|
|
|
|
AlarmsSet(SYSTEMALARMS_ALARM_ATTITUDE, SYSTEMALARMS_ALARM_CRITICAL);
|
|
|
|
} else if (lastAlarm >= FILTERRESULT_ERROR) {
|
|
|
|
AlarmsSet(SYSTEMALARMS_ALARM_ATTITUDE, SYSTEMALARMS_ALARM_ERROR);
|
|
|
|
} else {
|
|
|
|
AlarmsClear(SYSTEMALARMS_ALARM_ATTITUDE);
|
|
|
|
}
|
2013-05-22 19:05:28 +02:00
|
|
|
|
2015-02-26 06:58:57 +01:00
|
|
|
if (updatedSensors) {
|
|
|
|
PIOS_CALLBACKSCHEDULER_Dispatch(stateEstimationCallback);
|
|
|
|
} else {
|
|
|
|
PIOS_CALLBACKSCHEDULER_Schedule(stateEstimationCallback, TIMEOUT_MS, CALLBACK_UPDATEMODE_SOONER);
|
2013-05-19 17:11:09 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-05-19 21:44:33 +02:00
|
|
|
|
|
|
|
/**
|
2013-05-23 21:21:14 +02:00
|
|
|
* Callback for eventdispatcher when RevoSettings has been updated
|
2013-05-19 21:44:33 +02:00
|
|
|
*/
|
2013-05-20 17:04:52 +02:00
|
|
|
static void settingsUpdatedCb(__attribute__((unused)) UAVObjEvent *ev)
|
2013-05-19 17:11:09 +02:00
|
|
|
{
|
2013-05-19 21:44:33 +02:00
|
|
|
RevoSettingsGet((RevoSettingsData *)&revoSettings);
|
2013-05-19 17:11:09 +02:00
|
|
|
}
|
|
|
|
|
2014-03-23 12:56:01 +01:00
|
|
|
/**
|
2015-06-04 13:38:06 +02:00
|
|
|
* Callback for eventdispatcher when HomeLocation or other critical configs (auxmagsettings, ...) has been updated
|
2014-03-23 12:56:01 +01:00
|
|
|
*/
|
2015-06-04 13:38:06 +02:00
|
|
|
static void criticalConfigUpdatedCb(__attribute__((unused)) UAVObjEvent *ev)
|
2014-03-23 12:56:01 +01:00
|
|
|
{
|
2014-03-25 21:00:10 +01:00
|
|
|
// Ask for a filter init (necessary for LLA filter)
|
|
|
|
// Only possible if disarmed
|
|
|
|
fusionAlgorithm = FILTER_INIT_IF_POSSIBLE;
|
2014-03-23 12:56:01 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-05-19 21:44:33 +02:00
|
|
|
/**
|
|
|
|
* Callback for eventdispatcher when any sensor UAVObject has been updated
|
|
|
|
* updates the list of "recently updated UAVObjects" and dispatches the state estimator callback
|
|
|
|
*/
|
2013-05-19 17:11:09 +02:00
|
|
|
static void sensorUpdatedCb(UAVObjEvent *ev)
|
|
|
|
{
|
|
|
|
if (!ev) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (ev->obj == GyroSensorHandle()) {
|
2013-05-22 21:11:48 +02:00
|
|
|
updatedSensors |= SENSORUPDATES_gyro;
|
2014-05-01 17:43:23 +02:00
|
|
|
// shortcut - update GyroState right away
|
|
|
|
GyroSensorData s;
|
|
|
|
GyroStateData t;
|
|
|
|
GyroSensorGet(&s);
|
|
|
|
t.x = s.x + gyroDelta[0];
|
|
|
|
t.y = s.y + gyroDelta[1];
|
|
|
|
t.z = s.z + gyroDelta[2];
|
|
|
|
GyroStateSet(&t);
|
2013-05-19 17:11:09 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if (ev->obj == AccelSensorHandle()) {
|
2013-05-22 21:11:48 +02:00
|
|
|
updatedSensors |= SENSORUPDATES_accel;
|
2013-05-19 17:11:09 +02:00
|
|
|
}
|
|
|
|
|
2013-05-20 10:35:54 +02:00
|
|
|
if (ev->obj == MagSensorHandle()) {
|
2014-07-17 20:52:10 +02:00
|
|
|
updatedSensors |= SENSORUPDATES_boardMag;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (ev->obj == AuxMagSensorHandle()) {
|
|
|
|
updatedSensors |= SENSORUPDATES_auxMag;
|
2013-05-19 17:11:09 +02:00
|
|
|
}
|
|
|
|
|
2013-06-30 18:49:29 +02:00
|
|
|
if (ev->obj == GPSPositionSensorHandle()) {
|
|
|
|
updatedSensors |= SENSORUPDATES_lla;
|
2013-05-19 17:11:09 +02:00
|
|
|
}
|
|
|
|
|
2013-05-22 21:45:06 +02:00
|
|
|
if (ev->obj == GPSVelocitySensorHandle()) {
|
2013-05-22 21:11:48 +02:00
|
|
|
updatedSensors |= SENSORUPDATES_vel;
|
2013-05-19 17:11:09 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if (ev->obj == BaroSensorHandle()) {
|
2013-05-22 21:11:48 +02:00
|
|
|
updatedSensors |= SENSORUPDATES_baro;
|
2013-05-19 17:11:09 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if (ev->obj == AirspeedSensorHandle()) {
|
2013-05-22 21:11:48 +02:00
|
|
|
updatedSensors |= SENSORUPDATES_airspeed;
|
2013-05-19 17:11:09 +02:00
|
|
|
}
|
|
|
|
|
2014-02-02 22:08:16 +01:00
|
|
|
PIOS_CALLBACKSCHEDULER_Dispatch(stateEstimationCallback);
|
2013-05-19 17:11:09 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @}
|
|
|
|
* @}
|
|
|
|
*/
|