2011-11-14 17:49:37 +01:00
|
|
|
/**
|
|
|
|
******************************************************************************
|
|
|
|
* @addtogroup OpenPilotModules OpenPilot Modules
|
|
|
|
* @{
|
|
|
|
* @addtogroup Attitude Copter Control Attitude Estimation
|
|
|
|
* @brief Acquires sensor data and computes attitude estimate
|
2013-05-18 19:36:45 +02:00
|
|
|
* Specifically updates the the @ref AttitudeState "AttitudeState" and @ref AttitudeRaw "AttitudeRaw" settings objects
|
2011-11-14 17:49:37 +01:00
|
|
|
* @{
|
|
|
|
*
|
|
|
|
* @file attitude.c
|
|
|
|
* @author The OpenPilot Team, http://www.openpilot.org Copyright (C) 2010.
|
|
|
|
* @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
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Input objects: None, takes sensor data via pios
|
2013-05-18 19:36:45 +02:00
|
|
|
* Output objects: @ref AttitudeRaw @ref AttitudeState
|
2011-11-14 17:49:37 +01:00
|
|
|
*
|
|
|
|
* This module computes an attitude estimate from the sensor data
|
|
|
|
*
|
|
|
|
* The module executes in its own thread.
|
|
|
|
*
|
|
|
|
* UAVObjects are automatically generated by the UAVObjectGenerator from
|
|
|
|
* the object definition XML file.
|
|
|
|
*
|
|
|
|
* Modules have no API, all communication to other modules is done through UAVObjects.
|
|
|
|
* However modules may use the API exposed by shared libraries.
|
|
|
|
* See the OpenPilot wiki for more details.
|
|
|
|
* http://www.openpilot.org/OpenPilot_Application_Architecture
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
2013-05-02 23:31:14 +02:00
|
|
|
#include <openpilot.h>
|
2013-09-01 12:10:55 +02:00
|
|
|
#include <pios_struct_helper.h>
|
2012-02-29 16:34:11 +01:00
|
|
|
#include "attitude.h"
|
2013-05-18 19:36:45 +02:00
|
|
|
#include "accelsensor.h"
|
|
|
|
#include "accelstate.h"
|
2012-11-08 11:30:58 +01:00
|
|
|
#include "airspeedsensor.h"
|
2013-05-18 19:36:45 +02:00
|
|
|
#include "airspeedstate.h"
|
|
|
|
#include "attitudestate.h"
|
2011-11-14 17:49:37 +01:00
|
|
|
#include "attitudesettings.h"
|
2013-05-18 19:36:45 +02:00
|
|
|
#include "barosensor.h"
|
2011-11-14 17:49:37 +01:00
|
|
|
#include "flightstatus.h"
|
2013-05-22 21:45:06 +02:00
|
|
|
#include "gpspositionsensor.h"
|
|
|
|
#include "gpsvelocitysensor.h"
|
2013-05-18 19:36:45 +02:00
|
|
|
#include "gyrostate.h"
|
|
|
|
#include "gyrosensor.h"
|
2012-02-29 16:34:11 +01:00
|
|
|
#include "homelocation.h"
|
2013-05-20 10:33:02 +02:00
|
|
|
#include "magsensor.h"
|
|
|
|
#include "magstate.h"
|
2013-05-18 19:36:45 +02:00
|
|
|
#include "positionstate.h"
|
2013-05-03 02:17:44 +02:00
|
|
|
#include "ekfconfiguration.h"
|
2013-04-28 19:38:37 +02:00
|
|
|
#include "ekfstatevariance.h"
|
2012-03-01 11:52:09 +01:00
|
|
|
#include "revocalibration.h"
|
2012-02-29 16:34:11 +01:00
|
|
|
#include "revosettings.h"
|
2013-05-18 19:36:45 +02:00
|
|
|
#include "velocitystate.h"
|
2013-05-02 23:31:14 +02:00
|
|
|
#include "taskinfo.h"
|
|
|
|
|
2011-11-14 17:49:37 +01:00
|
|
|
#include "CoordinateConversions.h"
|
2013-05-02 23:31:14 +02:00
|
|
|
|
2011-11-14 17:49:37 +01:00
|
|
|
// Private constants
|
2013-06-15 16:22:24 +02:00
|
|
|
#define STACK_SIZE_BYTES 2048
|
|
|
|
#define TASK_PRIORITY (tskIDLE_PRIORITY + 3)
|
|
|
|
#define FAILSAFE_TIMEOUT_MS 10
|
2011-11-14 17:49:37 +01:00
|
|
|
|
2013-06-15 16:22:24 +02:00
|
|
|
#define CALIBRATION_DELAY 4000
|
|
|
|
#define CALIBRATION_DURATION 6000
|
2012-05-31 18:21:20 +02:00
|
|
|
// low pass filter configuration to calculate offset
|
|
|
|
// of barometric altitude sensor
|
|
|
|
// reasoning: updates at: 10 Hz, tau= 300 s settle time
|
|
|
|
// exp(-(1/f) / tau ) ~=~ 0.9997
|
2013-05-18 14:17:26 +02:00
|
|
|
#define BARO_OFFSET_LOWPASS_ALPHA 0.9997f
|
2012-11-08 11:30:58 +01:00
|
|
|
|
|
|
|
// simple IAS to TAS aproximation - 2% increase per 1000ft
|
|
|
|
// since we do not have flowing air temperature information
|
2013-05-18 14:17:26 +02:00
|
|
|
#define IAS2TAS(alt) (1.0f + (0.02f * (alt) / 304.8f))
|
2012-11-08 11:30:58 +01:00
|
|
|
|
2011-11-14 17:49:37 +01:00
|
|
|
// Private types
|
|
|
|
|
|
|
|
// Private variables
|
2011-12-12 06:29:43 +01:00
|
|
|
static xTaskHandle attitudeTaskHandle;
|
|
|
|
|
|
|
|
static xQueueHandle gyroQueue;
|
|
|
|
static xQueueHandle accelQueue;
|
|
|
|
static xQueueHandle magQueue;
|
2012-11-08 11:30:58 +01:00
|
|
|
static xQueueHandle airspeedQueue;
|
2011-12-14 08:54:06 +01:00
|
|
|
static xQueueHandle baroQueue;
|
2011-12-14 09:07:33 +01:00
|
|
|
static xQueueHandle gpsQueue;
|
2012-04-10 02:03:21 +02:00
|
|
|
static xQueueHandle gpsVelQueue;
|
2012-02-29 16:34:11 +01:00
|
|
|
|
2012-02-29 17:10:22 +01:00
|
|
|
static AttitudeSettingsData attitudeSettings;
|
2012-02-29 19:05:54 +01:00
|
|
|
static HomeLocationData homeLocation;
|
2012-03-01 11:52:09 +01:00
|
|
|
static RevoCalibrationData revoCalibration;
|
2013-05-03 02:17:44 +02:00
|
|
|
static EKFConfigurationData ekfConfiguration;
|
2012-03-01 11:52:09 +01:00
|
|
|
static RevoSettingsData revoSettings;
|
2013-05-01 20:21:46 +02:00
|
|
|
static FlightStatusData flightStatus;
|
2013-05-18 14:17:26 +02:00
|
|
|
const uint32_t SENSOR_QUEUE_SIZE = 10;
|
2011-11-14 17:49:37 +01:00
|
|
|
|
2013-04-28 23:41:43 +02:00
|
|
|
static bool volatile variance_error = true;
|
2013-05-01 20:21:46 +02:00
|
|
|
static bool volatile initialization_required = true;
|
2013-05-18 14:17:26 +02:00
|
|
|
static uint32_t volatile running_algorithm = 0xffffffff; // we start with no algorithm running
|
2013-05-21 22:40:35 +02:00
|
|
|
static float rollPitchBiasRate = 0;
|
2013-05-22 00:55:10 +02:00
|
|
|
|
|
|
|
// Accel filtering
|
|
|
|
static float accel_alpha = 0;
|
|
|
|
static bool accel_filter_enabled = false;
|
|
|
|
static float accels_filtered[3];
|
|
|
|
static float grot_filtered[3];
|
2013-04-28 23:41:43 +02:00
|
|
|
|
2011-11-14 17:49:37 +01:00
|
|
|
// Private functions
|
|
|
|
static void AttitudeTask(void *parameters);
|
|
|
|
|
2012-06-17 09:21:58 +02:00
|
|
|
static int32_t updateAttitudeComplementary(bool first_run);
|
2012-02-29 16:34:11 +01:00
|
|
|
static int32_t updateAttitudeINSGPS(bool first_run, bool outdoor_mode);
|
2013-05-18 14:17:26 +02:00
|
|
|
static void settingsUpdatedCb(UAVObjEvent *objEv);
|
2011-11-14 17:49:37 +01:00
|
|
|
|
2013-05-22 21:45:06 +02:00
|
|
|
static int32_t getNED(GPSPositionSensorData *gpsPosition, float *NED);
|
2012-02-29 19:05:54 +01:00
|
|
|
|
2013-05-20 10:33:02 +02:00
|
|
|
static void magOffsetEstimation(MagSensorData *mag);
|
2013-05-18 21:55:47 +02:00
|
|
|
|
2013-04-28 23:41:43 +02:00
|
|
|
// check for invalid values
|
2013-05-18 14:17:26 +02:00
|
|
|
static inline bool invalid(float data)
|
|
|
|
{
|
|
|
|
if (isnan(data) || isinf(data)) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
2013-04-28 23:41:43 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// check for invalid variance values
|
2013-05-18 14:17:26 +02:00
|
|
|
static inline bool invalid_var(float data)
|
|
|
|
{
|
|
|
|
if (invalid(data)) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
if (data < 1e-15f) { // var should not be close to zero. And not negative either.
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
2013-04-28 23:41:43 +02:00
|
|
|
}
|
|
|
|
|
2011-12-12 08:34:26 +01:00
|
|
|
/**
|
|
|
|
* API for sensor fusion algorithms:
|
|
|
|
* Configure(xQueueHandle gyro, xQueueHandle accel, xQueueHandle mag, xQueueHandle baro)
|
|
|
|
* Stores all the queues the algorithm will pull data from
|
|
|
|
* FinalizeSensors() -- before saving the sensors modifies them based on internal state (gyro bias)
|
|
|
|
* Update() -- queries queues and updates the attitude estiamte
|
|
|
|
*/
|
2011-12-12 06:29:43 +01:00
|
|
|
|
2011-11-14 17:49:37 +01:00
|
|
|
|
|
|
|
/**
|
2011-12-12 21:39:38 +01:00
|
|
|
* Initialise the module. Called before the start function
|
2011-11-14 17:49:37 +01:00
|
|
|
* \returns 0 on success or -1 if initialisation failed
|
|
|
|
*/
|
|
|
|
int32_t AttitudeInitialize(void)
|
|
|
|
{
|
2013-05-18 21:55:47 +02:00
|
|
|
GyroSensorInitialize();
|
|
|
|
GyroStateInitialize();
|
|
|
|
AccelSensorInitialize();
|
|
|
|
AccelStateInitialize();
|
2013-05-20 10:33:02 +02:00
|
|
|
MagSensorInitialize();
|
|
|
|
MagStateInitialize();
|
2013-05-18 14:17:26 +02:00
|
|
|
AirspeedSensorInitialize();
|
2013-05-18 21:55:47 +02:00
|
|
|
AirspeedStateInitialize();
|
|
|
|
BaroSensorInitialize();
|
2013-05-22 21:45:06 +02:00
|
|
|
GPSPositionSensorInitialize();
|
|
|
|
GPSVelocitySensorInitialize();
|
2013-05-18 14:17:26 +02:00
|
|
|
AttitudeSettingsInitialize();
|
2013-05-18 21:55:47 +02:00
|
|
|
AttitudeStateInitialize();
|
2013-05-18 19:36:45 +02:00
|
|
|
PositionStateInitialize();
|
|
|
|
VelocityStateInitialize();
|
2013-05-18 14:17:26 +02:00
|
|
|
RevoSettingsInitialize();
|
|
|
|
RevoCalibrationInitialize();
|
|
|
|
EKFConfigurationInitialize();
|
|
|
|
EKFStateVarianceInitialize();
|
|
|
|
FlightStatusInitialize();
|
|
|
|
|
|
|
|
// Initialize this here while we aren't setting the homelocation in GPS
|
|
|
|
HomeLocationInitialize();
|
|
|
|
|
|
|
|
// Initialize quaternion
|
2013-05-18 19:36:45 +02:00
|
|
|
AttitudeStateData attitude;
|
|
|
|
AttitudeStateGet(&attitude);
|
2013-05-18 14:17:26 +02:00
|
|
|
attitude.q1 = 1.0f;
|
|
|
|
attitude.q2 = 0.0f;
|
|
|
|
attitude.q3 = 0.0f;
|
|
|
|
attitude.q4 = 0.0f;
|
2013-05-18 19:36:45 +02:00
|
|
|
AttitudeStateSet(&attitude);
|
2013-05-18 14:17:26 +02:00
|
|
|
|
|
|
|
AttitudeSettingsConnectCallback(&settingsUpdatedCb);
|
|
|
|
RevoSettingsConnectCallback(&settingsUpdatedCb);
|
|
|
|
RevoCalibrationConnectCallback(&settingsUpdatedCb);
|
|
|
|
HomeLocationConnectCallback(&settingsUpdatedCb);
|
|
|
|
EKFConfigurationConnectCallback(&settingsUpdatedCb);
|
|
|
|
FlightStatusConnectCallback(&settingsUpdatedCb);
|
|
|
|
|
|
|
|
return 0;
|
2011-11-14 17:49:37 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2011-12-12 21:39:38 +01:00
|
|
|
* Start the task. Expects all objects to be initialized by this point.
|
|
|
|
* \returns 0 on success or -1 if initialisation failed
|
2011-11-14 17:49:37 +01:00
|
|
|
*/
|
2011-12-12 21:39:38 +01:00
|
|
|
int32_t AttitudeStart(void)
|
2011-11-14 17:49:37 +01:00
|
|
|
{
|
2013-05-18 14:17:26 +02:00
|
|
|
// Create the queues for the sensors
|
|
|
|
gyroQueue = xQueueCreate(1, sizeof(UAVObjEvent));
|
|
|
|
accelQueue = xQueueCreate(1, sizeof(UAVObjEvent));
|
|
|
|
magQueue = xQueueCreate(1, sizeof(UAVObjEvent));
|
|
|
|
airspeedQueue = xQueueCreate(1, sizeof(UAVObjEvent));
|
|
|
|
baroQueue = xQueueCreate(1, sizeof(UAVObjEvent));
|
|
|
|
gpsQueue = xQueueCreate(1, sizeof(UAVObjEvent));
|
|
|
|
gpsVelQueue = xQueueCreate(1, sizeof(UAVObjEvent));
|
|
|
|
|
|
|
|
// Start main task
|
|
|
|
xTaskCreate(AttitudeTask, (signed char *)"Attitude", STACK_SIZE_BYTES / 4, NULL, TASK_PRIORITY, &attitudeTaskHandle);
|
|
|
|
PIOS_TASK_MONITOR_RegisterTask(TASKINFO_RUNNING_ATTITUDE, attitudeTaskHandle);
|
2013-05-19 21:56:23 +02:00
|
|
|
#ifdef PIOS_INCLUDE_WDG
|
2013-05-18 14:17:26 +02:00
|
|
|
PIOS_WDG_RegisterFlag(PIOS_WDG_ATTITUDE);
|
2013-05-19 21:56:23 +02:00
|
|
|
#endif
|
2013-05-18 14:17:26 +02:00
|
|
|
|
2013-05-18 19:36:45 +02:00
|
|
|
GyroSensorConnectQueue(gyroQueue);
|
|
|
|
AccelSensorConnectQueue(accelQueue);
|
2013-05-20 10:33:02 +02:00
|
|
|
MagSensorConnectQueue(magQueue);
|
2013-05-18 14:17:26 +02:00
|
|
|
AirspeedSensorConnectQueue(airspeedQueue);
|
2013-05-18 19:36:45 +02:00
|
|
|
BaroSensorConnectQueue(baroQueue);
|
2013-05-22 21:45:06 +02:00
|
|
|
GPSPositionSensorConnectQueue(gpsQueue);
|
|
|
|
GPSVelocitySensorConnectQueue(gpsVelQueue);
|
2013-05-18 14:17:26 +02:00
|
|
|
|
|
|
|
return 0;
|
2011-12-12 06:29:43 +01:00
|
|
|
}
|
|
|
|
|
2013-06-04 05:37:40 +02:00
|
|
|
MODULE_INITCALL(AttitudeInitialize, AttitudeStart);
|
2011-12-12 21:39:38 +01:00
|
|
|
|
2011-12-12 06:29:43 +01:00
|
|
|
/**
|
|
|
|
* Module thread, should not return.
|
|
|
|
*/
|
2013-05-05 09:02:24 +02:00
|
|
|
static void AttitudeTask(__attribute__((unused)) void *parameters)
|
2011-12-12 06:29:43 +01:00
|
|
|
{
|
2013-05-18 14:17:26 +02:00
|
|
|
AlarmsClear(SYSTEMALARMS_ALARM_ATTITUDE);
|
|
|
|
|
|
|
|
// Force settings update to make sure rotation loaded
|
|
|
|
settingsUpdatedCb(NULL);
|
|
|
|
|
|
|
|
// Wait for all the sensors be to read
|
|
|
|
vTaskDelay(100);
|
|
|
|
|
|
|
|
// Main task loop - TODO: make it run as delayed callback
|
|
|
|
while (1) {
|
|
|
|
int32_t ret_val = -1;
|
|
|
|
|
|
|
|
bool first_run = false;
|
|
|
|
if (initialization_required) {
|
|
|
|
initialization_required = false;
|
|
|
|
first_run = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
// This function blocks on data queue
|
|
|
|
switch (running_algorithm) {
|
|
|
|
case REVOSETTINGS_FUSIONALGORITHM_COMPLEMENTARY:
|
|
|
|
ret_val = updateAttitudeComplementary(first_run);
|
|
|
|
break;
|
2013-05-20 20:49:34 +02:00
|
|
|
case REVOSETTINGS_FUSIONALGORITHM_INS13OUTDOOR:
|
2013-05-18 14:17:26 +02:00
|
|
|
ret_val = updateAttitudeINSGPS(first_run, true);
|
|
|
|
break;
|
2013-05-20 20:49:34 +02:00
|
|
|
case REVOSETTINGS_FUSIONALGORITHM_INS13INDOOR:
|
2013-05-18 14:17:26 +02:00
|
|
|
ret_val = updateAttitudeINSGPS(first_run, false);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
AlarmsSet(SYSTEMALARMS_ALARM_ATTITUDE, SYSTEMALARMS_ALARM_CRITICAL);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (ret_val != 0) {
|
|
|
|
initialization_required = true;
|
|
|
|
}
|
|
|
|
|
2013-05-19 21:56:23 +02:00
|
|
|
#ifdef PIOS_INCLUDE_WDG
|
2013-05-18 14:17:26 +02:00
|
|
|
PIOS_WDG_UpdateFlag(PIOS_WDG_ATTITUDE);
|
2013-05-19 21:56:23 +02:00
|
|
|
#endif
|
2013-05-18 14:17:26 +02:00
|
|
|
}
|
2011-11-14 17:49:37 +01:00
|
|
|
}
|
|
|
|
|
2013-05-22 00:55:10 +02:00
|
|
|
static inline void apply_accel_filter(const float *raw, float *filtered)
|
|
|
|
{
|
|
|
|
if (accel_filter_enabled) {
|
|
|
|
filtered[0] = filtered[0] * accel_alpha + raw[0] * (1 - accel_alpha);
|
|
|
|
filtered[1] = filtered[1] * accel_alpha + raw[1] * (1 - accel_alpha);
|
|
|
|
filtered[2] = filtered[2] * accel_alpha + raw[2] * (1 - accel_alpha);
|
|
|
|
} else {
|
|
|
|
filtered[0] = raw[0];
|
|
|
|
filtered[1] = raw[1];
|
|
|
|
filtered[2] = raw[2];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-11-16 18:36:04 +01:00
|
|
|
float accel_mag;
|
|
|
|
float qmag;
|
2011-12-15 07:56:50 +01:00
|
|
|
float attitudeDt;
|
|
|
|
float mag_err[3];
|
|
|
|
|
2012-06-17 09:21:58 +02:00
|
|
|
static int32_t updateAttitudeComplementary(bool first_run)
|
2011-11-14 17:49:37 +01:00
|
|
|
{
|
2013-05-18 14:17:26 +02:00
|
|
|
UAVObjEvent ev;
|
2013-05-18 19:36:45 +02:00
|
|
|
GyroSensorData gyroSensorData;
|
2013-05-18 21:55:47 +02:00
|
|
|
GyroStateData gyroStateData;
|
2013-05-18 19:36:45 +02:00
|
|
|
AccelSensorData accelSensorData;
|
2013-05-18 14:17:26 +02:00
|
|
|
static int32_t timeval;
|
|
|
|
float dT;
|
|
|
|
static uint8_t init = 0;
|
2013-05-18 21:55:47 +02:00
|
|
|
static float gyro_bias[3] = { 0, 0, 0 };
|
2013-06-08 13:05:02 +02:00
|
|
|
static bool magCalibrated = true;
|
2013-06-15 16:22:24 +02:00
|
|
|
static uint32_t initStartupTime = 0;
|
2013-05-18 14:17:26 +02:00
|
|
|
|
|
|
|
// Wait until the AttitudeRaw object is updated, if a timeout then go to failsafe
|
|
|
|
if (xQueueReceive(gyroQueue, &ev, FAILSAFE_TIMEOUT_MS / portTICK_RATE_MS) != pdTRUE ||
|
|
|
|
xQueueReceive(accelQueue, &ev, 1 / portTICK_RATE_MS) != pdTRUE) {
|
|
|
|
// When one of these is updated so should the other
|
|
|
|
// Do not set attitude timeout warnings in simulation mode
|
2013-05-18 19:36:45 +02:00
|
|
|
if (!AttitudeStateReadOnly()) {
|
2013-05-18 14:17:26 +02:00
|
|
|
AlarmsSet(SYSTEMALARMS_ALARM_ATTITUDE, SYSTEMALARMS_ALARM_WARNING);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-05-18 19:36:45 +02:00
|
|
|
AccelSensorGet(&accelSensorData);
|
2013-05-18 14:17:26 +02:00
|
|
|
|
2013-05-18 21:55:47 +02:00
|
|
|
// TODO: put in separate filter
|
|
|
|
AccelStateData accelState;
|
|
|
|
accelState.x = accelSensorData.x;
|
|
|
|
accelState.y = accelSensorData.y;
|
|
|
|
accelState.z = accelSensorData.z;
|
|
|
|
AccelStateSet(&accelState);
|
|
|
|
|
2013-05-18 14:17:26 +02:00
|
|
|
// During initialization and
|
|
|
|
if (first_run) {
|
2012-05-12 05:09:04 +02:00
|
|
|
#if defined(PIOS_INCLUDE_HMC5883)
|
2013-05-18 14:17:26 +02:00
|
|
|
// To initialize we need a valid mag reading
|
|
|
|
if (xQueueReceive(magQueue, &ev, 0 / portTICK_RATE_MS) != pdTRUE) {
|
|
|
|
return -1;
|
|
|
|
}
|
2013-05-20 10:33:02 +02:00
|
|
|
MagSensorData magData;
|
|
|
|
MagSensorGet(&magData);
|
2012-05-12 05:09:04 +02:00
|
|
|
#else
|
2013-05-20 10:33:02 +02:00
|
|
|
MagSensorData magData;
|
2013-05-18 14:17:26 +02:00
|
|
|
magData.x = 100.0f;
|
|
|
|
magData.y = 0.0f;
|
|
|
|
magData.z = 0.0f;
|
2012-05-12 05:09:04 +02:00
|
|
|
#endif
|
2013-06-08 13:05:02 +02:00
|
|
|
float magBias[3];
|
2013-09-01 13:23:20 +02:00
|
|
|
RevoCalibrationmag_biasArrayGet(magBias);
|
2013-06-08 13:05:02 +02:00
|
|
|
// don't trust Mag for initial orientation if it has not been calibrated
|
|
|
|
if (magBias[0] < 1e-6f && magBias[1] < 1e-6f && magBias[2] < 1e-6f) {
|
|
|
|
magCalibrated = false;
|
|
|
|
magData.x = 100.0f;
|
|
|
|
magData.y = 0.0f;
|
|
|
|
magData.z = 0.0f;
|
|
|
|
}
|
2013-05-18 19:36:45 +02:00
|
|
|
AttitudeStateData attitudeState;
|
|
|
|
AttitudeStateGet(&attitudeState);
|
2013-05-18 14:17:26 +02:00
|
|
|
init = 0;
|
|
|
|
|
|
|
|
// Set initial attitude. Use accels to determine roll and pitch, rotate magnetic measurement accordingly,
|
|
|
|
// so pseudo "north" vector can be estimated even if the board is not level
|
2013-05-18 19:36:45 +02:00
|
|
|
attitudeState.Roll = atan2f(-accelSensorData.y, -accelSensorData.z);
|
|
|
|
float zn = cosf(attitudeState.Roll) * magData.z + sinf(attitudeState.Roll) * magData.y;
|
|
|
|
float yn = cosf(attitudeState.Roll) * magData.y - sinf(attitudeState.Roll) * magData.z;
|
2013-05-18 14:17:26 +02:00
|
|
|
|
|
|
|
// rotate accels z vector according to roll
|
2013-05-18 19:36:45 +02:00
|
|
|
float azn = cosf(attitudeState.Roll) * accelSensorData.z + sinf(attitudeState.Roll) * accelSensorData.y;
|
|
|
|
attitudeState.Pitch = atan2f(accelSensorData.x, -azn);
|
2013-05-18 14:17:26 +02:00
|
|
|
|
2013-05-18 19:36:45 +02:00
|
|
|
float xn = cosf(attitudeState.Pitch) * magData.x + sinf(attitudeState.Pitch) * zn;
|
2013-05-18 14:17:26 +02:00
|
|
|
|
2013-05-18 19:36:45 +02:00
|
|
|
attitudeState.Yaw = atan2f(-yn, xn);
|
2013-05-18 14:17:26 +02:00
|
|
|
// TODO: This is still a hack
|
|
|
|
// Put this in a proper generic function in CoordinateConversion.c
|
|
|
|
// should take 4 vectors: g (0,0,-9.81), accels, Be (or 1,0,0 if no home loc) and magnetometers (or 1,0,0 if no mags)
|
|
|
|
// should calculate the rotation in 3d space using proper cross product math
|
|
|
|
// SUBTODO: formulate the math required
|
|
|
|
|
2013-05-18 19:36:45 +02:00
|
|
|
attitudeState.Roll = RAD2DEG(attitudeState.Roll);
|
|
|
|
attitudeState.Pitch = RAD2DEG(attitudeState.Pitch);
|
|
|
|
attitudeState.Yaw = RAD2DEG(attitudeState.Yaw);
|
2013-05-18 14:17:26 +02:00
|
|
|
|
2013-05-18 19:36:45 +02:00
|
|
|
RPY2Quaternion(&attitudeState.Roll, &attitudeState.q1);
|
|
|
|
AttitudeStateSet(&attitudeState);
|
2013-05-18 14:17:26 +02:00
|
|
|
|
|
|
|
timeval = PIOS_DELAY_GetRaw();
|
|
|
|
|
2013-06-15 16:22:24 +02:00
|
|
|
// wait calibration_delay only at powerup
|
|
|
|
if (xTaskGetTickCount() < 3000) {
|
|
|
|
initStartupTime = 0;
|
|
|
|
} else {
|
|
|
|
initStartupTime = xTaskGetTickCount() - CALIBRATION_DELAY;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Zero gyro bias
|
|
|
|
// This is really needed after updating calibration settings.
|
2013-06-22 23:07:53 +02:00
|
|
|
gyro_bias[0] = 0.0f;
|
|
|
|
gyro_bias[1] = 0.0f;
|
|
|
|
gyro_bias[2] = 0.0f;
|
2013-05-18 14:17:26 +02:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2013-06-15 16:22:24 +02:00
|
|
|
if ((xTaskGetTickCount() - initStartupTime < CALIBRATION_DURATION + CALIBRATION_DELAY) &&
|
|
|
|
(xTaskGetTickCount() - initStartupTime > CALIBRATION_DELAY)) {
|
2013-06-17 21:01:24 +02:00
|
|
|
// For first CALIBRATION_DURATION seconds after CALIBRATION_DELAY from startup
|
|
|
|
// Zero gyro bias assuming it is steady, smoothing the gyro input value applying rollPitchBiasRate.
|
2013-05-18 14:17:26 +02:00
|
|
|
attitudeSettings.AccelKp = 1.0f;
|
2013-05-21 22:40:35 +02:00
|
|
|
attitudeSettings.AccelKi = 0.0f;
|
2013-05-18 14:17:26 +02:00
|
|
|
attitudeSettings.YawBiasRate = 0.23f;
|
2013-05-23 00:47:38 +02:00
|
|
|
accel_filter_enabled = false;
|
|
|
|
rollPitchBiasRate = 0.01f;
|
2013-06-08 13:05:02 +02:00
|
|
|
attitudeSettings.MagKp = magCalibrated ? 1.0f : 0.0f;
|
|
|
|
init = 0;
|
2013-05-18 14:17:26 +02:00
|
|
|
} else if ((attitudeSettings.ZeroDuringArming == ATTITUDESETTINGS_ZERODURINGARMING_TRUE) && (flightStatus.Armed == FLIGHTSTATUS_ARMED_ARMING)) {
|
|
|
|
attitudeSettings.AccelKp = 1.0f;
|
2013-05-21 22:40:35 +02:00
|
|
|
attitudeSettings.AccelKi = 0.0f;
|
2013-05-18 14:17:26 +02:00
|
|
|
attitudeSettings.YawBiasRate = 0.23f;
|
2013-05-23 00:47:38 +02:00
|
|
|
accel_filter_enabled = false;
|
|
|
|
rollPitchBiasRate = 0.01f;
|
2013-06-08 13:05:02 +02:00
|
|
|
attitudeSettings.MagKp = magCalibrated ? 1.0f : 0.0f;
|
2013-05-18 14:17:26 +02:00
|
|
|
init = 0;
|
|
|
|
} else if (init == 0) {
|
|
|
|
// Reload settings (all the rates)
|
|
|
|
AttitudeSettingsGet(&attitudeSettings);
|
2013-05-21 22:40:35 +02:00
|
|
|
rollPitchBiasRate = 0.0f;
|
2013-05-23 00:47:38 +02:00
|
|
|
if (accel_alpha > 0.0f) {
|
2013-05-22 00:55:10 +02:00
|
|
|
accel_filter_enabled = true;
|
2013-05-23 00:47:38 +02:00
|
|
|
}
|
|
|
|
init = 1;
|
2013-05-18 14:17:26 +02:00
|
|
|
}
|
|
|
|
|
2013-05-18 19:36:45 +02:00
|
|
|
GyroSensorGet(&gyroSensorData);
|
2013-05-18 21:55:47 +02:00
|
|
|
gyroStateData.x = gyroSensorData.x;
|
|
|
|
gyroStateData.y = gyroSensorData.y;
|
|
|
|
gyroStateData.z = gyroSensorData.z;
|
2013-05-18 14:17:26 +02:00
|
|
|
|
|
|
|
// Compute the dT using the cpu clock
|
|
|
|
dT = PIOS_DELAY_DiffuS(timeval) / 1000000.0f;
|
|
|
|
timeval = PIOS_DELAY_GetRaw();
|
|
|
|
|
|
|
|
float q[4];
|
|
|
|
|
2013-05-18 19:36:45 +02:00
|
|
|
AttitudeStateData attitudeState;
|
|
|
|
AttitudeStateGet(&attitudeState);
|
2013-05-18 14:17:26 +02:00
|
|
|
|
|
|
|
float grot[3];
|
|
|
|
float accel_err[3];
|
|
|
|
|
|
|
|
// Get the current attitude estimate
|
2013-05-18 19:36:45 +02:00
|
|
|
quat_copy(&attitudeState.q1, q);
|
2013-05-18 14:17:26 +02:00
|
|
|
|
2013-05-22 00:55:10 +02:00
|
|
|
// Apply smoothing to accel values, to reduce vibration noise before main calculations.
|
2013-06-06 19:25:54 +02:00
|
|
|
apply_accel_filter((const float *)&accelSensorData.x, accels_filtered);
|
2013-05-22 00:55:10 +02:00
|
|
|
|
2013-05-18 14:17:26 +02:00
|
|
|
// Rotate gravity to body frame and cross with accels
|
|
|
|
grot[0] = -(2.0f * (q[1] * q[3] - q[0] * q[2]));
|
|
|
|
grot[1] = -(2.0f * (q[2] * q[3] + q[0] * q[1]));
|
|
|
|
grot[2] = -(q[0] * q[0] - q[1] * q[1] - q[2] * q[2] + q[3] * q[3]);
|
2013-05-22 00:55:10 +02:00
|
|
|
|
|
|
|
apply_accel_filter(grot, grot_filtered);
|
|
|
|
|
|
|
|
CrossProduct((const float *)accels_filtered, (const float *)grot_filtered, accel_err);
|
2013-05-18 14:17:26 +02:00
|
|
|
|
|
|
|
// Account for accel magnitude
|
2013-05-23 00:47:38 +02:00
|
|
|
accel_mag = accels_filtered[0] * accels_filtered[0] + accels_filtered[1] * accels_filtered[1] + accels_filtered[2] * accels_filtered[2];
|
|
|
|
accel_mag = sqrtf(accel_mag);
|
2013-05-22 00:55:10 +02:00
|
|
|
|
|
|
|
float grot_mag;
|
|
|
|
if (accel_filter_enabled) {
|
2013-05-23 00:47:38 +02:00
|
|
|
grot_mag = sqrtf(grot_filtered[0] * grot_filtered[0] + grot_filtered[1] * grot_filtered[1] + grot_filtered[2] * grot_filtered[2]);
|
|
|
|
} else {
|
|
|
|
grot_mag = 1.0f;
|
|
|
|
}
|
2013-05-22 00:55:10 +02:00
|
|
|
|
2013-06-20 14:35:40 +02:00
|
|
|
// TODO! check grot_mag & accel vector magnitude values for correctness.
|
2013-05-22 00:55:10 +02:00
|
|
|
|
2013-05-23 00:47:38 +02:00
|
|
|
accel_err[0] /= (accel_mag * grot_mag);
|
|
|
|
accel_err[1] /= (accel_mag * grot_mag);
|
|
|
|
accel_err[2] /= (accel_mag * grot_mag);
|
2013-05-22 00:55:10 +02:00
|
|
|
|
|
|
|
|
2013-05-18 14:17:26 +02:00
|
|
|
if (xQueueReceive(magQueue, &ev, 0) != pdTRUE) {
|
|
|
|
// Rotate gravity to body frame and cross with accels
|
|
|
|
float brot[3];
|
|
|
|
float Rbe[3][3];
|
2013-05-20 10:33:02 +02:00
|
|
|
MagSensorData mag;
|
2013-05-18 14:17:26 +02:00
|
|
|
|
|
|
|
Quaternion2R(q, Rbe);
|
2013-05-20 10:33:02 +02:00
|
|
|
MagSensorGet(&mag);
|
2013-05-18 14:17:26 +02:00
|
|
|
|
2013-05-18 21:55:47 +02:00
|
|
|
// TODO: separate filter!
|
|
|
|
if (revoCalibration.MagBiasNullingRate > 0) {
|
|
|
|
magOffsetEstimation(&mag);
|
|
|
|
}
|
2013-05-20 10:33:02 +02:00
|
|
|
MagStateData mags;
|
2013-05-18 21:55:47 +02:00
|
|
|
mags.x = mag.x;
|
|
|
|
mags.y = mag.y;
|
|
|
|
mags.z = mag.z;
|
2013-05-20 10:33:02 +02:00
|
|
|
MagStateSet(&mags);
|
2013-05-18 21:55:47 +02:00
|
|
|
|
2013-05-18 14:17:26 +02:00
|
|
|
// If the mag is producing bad data don't use it (normally bad calibration)
|
|
|
|
if (!isnan(mag.x) && !isinf(mag.x) && !isnan(mag.y) && !isinf(mag.y) && !isnan(mag.z) && !isinf(mag.z)) {
|
|
|
|
rot_mult(Rbe, homeLocation.Be, brot);
|
|
|
|
|
|
|
|
float mag_len = sqrtf(mag.x * mag.x + mag.y * mag.y + mag.z * mag.z);
|
|
|
|
mag.x /= mag_len;
|
|
|
|
mag.y /= mag_len;
|
|
|
|
mag.z /= mag_len;
|
|
|
|
|
|
|
|
float bmag = sqrtf(brot[0] * brot[0] + brot[1] * brot[1] + brot[2] * brot[2]);
|
|
|
|
brot[0] /= bmag;
|
|
|
|
brot[1] /= bmag;
|
|
|
|
brot[2] /= bmag;
|
|
|
|
|
|
|
|
// Only compute if neither vector is null
|
|
|
|
if (bmag < 1.0f || mag_len < 1.0f) {
|
|
|
|
mag_err[0] = mag_err[1] = mag_err[2] = 0.0f;
|
|
|
|
} else {
|
|
|
|
CrossProduct((const float *)&mag.x, (const float *)brot, mag_err);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
mag_err[0] = mag_err[1] = mag_err[2] = 0.0f;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Accumulate integral of error. Scale here so that units are (deg/s) but Ki has units of s
|
2013-05-18 21:55:47 +02:00
|
|
|
// Correct rates based on integral coefficient
|
|
|
|
gyroStateData.x -= gyro_bias[0];
|
|
|
|
gyroStateData.y -= gyro_bias[1];
|
|
|
|
gyroStateData.z -= gyro_bias[2];
|
2013-05-18 14:17:26 +02:00
|
|
|
|
2013-06-09 13:49:52 +02:00
|
|
|
gyro_bias[0] -= accel_err[0] * attitudeSettings.AccelKi - (gyroStateData.x) * rollPitchBiasRate;
|
|
|
|
gyro_bias[1] -= accel_err[1] * attitudeSettings.AccelKi - (gyroStateData.y) * rollPitchBiasRate;
|
|
|
|
gyro_bias[2] -= -mag_err[2] * attitudeSettings.MagKi - (gyroStateData.z) * rollPitchBiasRate;
|
2013-05-21 23:44:12 +02:00
|
|
|
|
2013-05-18 21:55:47 +02:00
|
|
|
// save gyroscope state
|
|
|
|
GyroStateSet(&gyroStateData);
|
|
|
|
|
|
|
|
// Correct rates based on proportional coefficient
|
|
|
|
gyroStateData.x += accel_err[0] * attitudeSettings.AccelKp / dT;
|
|
|
|
gyroStateData.y += accel_err[1] * attitudeSettings.AccelKp / dT;
|
2013-06-06 19:25:54 +02:00
|
|
|
gyroStateData.z += accel_err[2] * attitudeSettings.AccelKp / dT + mag_err[2] * attitudeSettings.MagKp / dT;
|
2013-05-18 14:17:26 +02:00
|
|
|
|
|
|
|
// Work out time derivative from INSAlgo writeup
|
|
|
|
// Also accounts for the fact that gyros are in deg/s
|
|
|
|
float qdot[4];
|
2013-05-18 21:55:47 +02:00
|
|
|
qdot[0] = DEG2RAD(-q[1] * gyroStateData.x - q[2] * gyroStateData.y - q[3] * gyroStateData.z) * dT / 2;
|
|
|
|
qdot[1] = DEG2RAD(q[0] * gyroStateData.x - q[3] * gyroStateData.y + q[2] * gyroStateData.z) * dT / 2;
|
|
|
|
qdot[2] = DEG2RAD(q[3] * gyroStateData.x + q[0] * gyroStateData.y - q[1] * gyroStateData.z) * dT / 2;
|
|
|
|
qdot[3] = DEG2RAD(-q[2] * gyroStateData.x + q[1] * gyroStateData.y + q[0] * gyroStateData.z) * dT / 2;
|
2013-05-18 14:17:26 +02:00
|
|
|
|
|
|
|
// Take a time step
|
|
|
|
q[0] = q[0] + qdot[0];
|
|
|
|
q[1] = q[1] + qdot[1];
|
|
|
|
q[2] = q[2] + qdot[2];
|
|
|
|
q[3] = q[3] + qdot[3];
|
|
|
|
|
|
|
|
if (q[0] < 0.0f) {
|
|
|
|
q[0] = -q[0];
|
|
|
|
q[1] = -q[1];
|
|
|
|
q[2] = -q[2];
|
|
|
|
q[3] = -q[3];
|
|
|
|
}
|
|
|
|
|
|
|
|
// Renomalize
|
|
|
|
qmag = sqrtf(q[0] * q[0] + q[1] * q[1] + q[2] * q[2] + q[3] * q[3]);
|
|
|
|
q[0] = q[0] / qmag;
|
|
|
|
q[1] = q[1] / qmag;
|
|
|
|
q[2] = q[2] / qmag;
|
|
|
|
q[3] = q[3] / qmag;
|
|
|
|
|
|
|
|
// If quaternion has become inappropriately short or is nan reinit.
|
|
|
|
// THIS SHOULD NEVER ACTUALLY HAPPEN
|
|
|
|
if ((fabsf(qmag) < 1.0e-3f) || isnan(qmag)) {
|
|
|
|
q[0] = 1.0f;
|
|
|
|
q[1] = 0.0f;
|
|
|
|
q[2] = 0.0f;
|
|
|
|
q[3] = 0.0f;
|
|
|
|
}
|
|
|
|
|
2013-05-18 19:36:45 +02:00
|
|
|
quat_copy(q, &attitudeState.q1);
|
2013-05-18 14:17:26 +02:00
|
|
|
|
|
|
|
// Convert into eueler degrees (makes assumptions about RPY order)
|
2013-05-18 19:36:45 +02:00
|
|
|
Quaternion2RPY(&attitudeState.q1, &attitudeState.Roll);
|
2013-05-18 14:17:26 +02:00
|
|
|
|
2013-05-18 19:36:45 +02:00
|
|
|
AttitudeStateSet(&attitudeState);
|
2013-05-18 14:17:26 +02:00
|
|
|
|
|
|
|
// Flush these queues for avoid errors
|
|
|
|
xQueueReceive(baroQueue, &ev, 0);
|
|
|
|
if (xQueueReceive(gpsQueue, &ev, 0) == pdTRUE && homeLocation.Set == HOMELOCATION_SET_TRUE) {
|
|
|
|
float NED[3];
|
|
|
|
// Transform the GPS position into NED coordinates
|
2013-05-22 21:45:06 +02:00
|
|
|
GPSPositionSensorData gpsPosition;
|
|
|
|
GPSPositionSensorGet(&gpsPosition);
|
2013-05-18 14:17:26 +02:00
|
|
|
getNED(&gpsPosition, NED);
|
|
|
|
|
2013-05-18 19:36:45 +02:00
|
|
|
PositionStateData positionState;
|
|
|
|
PositionStateGet(&positionState);
|
|
|
|
positionState.North = NED[0];
|
|
|
|
positionState.East = NED[1];
|
|
|
|
positionState.Down = NED[2];
|
|
|
|
PositionStateSet(&positionState);
|
2013-05-18 14:17:26 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if (xQueueReceive(gpsVelQueue, &ev, 0) == pdTRUE) {
|
|
|
|
// Transform the GPS position into NED coordinates
|
2013-05-22 21:45:06 +02:00
|
|
|
GPSVelocitySensorData gpsVelocity;
|
|
|
|
GPSVelocitySensorGet(&gpsVelocity);
|
2013-05-18 14:17:26 +02:00
|
|
|
|
2013-05-18 19:36:45 +02:00
|
|
|
VelocityStateData velocityState;
|
|
|
|
VelocityStateGet(&velocityState);
|
|
|
|
velocityState.North = gpsVelocity.North;
|
|
|
|
velocityState.East = gpsVelocity.East;
|
|
|
|
velocityState.Down = gpsVelocity.Down;
|
|
|
|
VelocityStateSet(&velocityState);
|
2013-05-18 14:17:26 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if (xQueueReceive(airspeedQueue, &ev, 0) == pdTRUE) {
|
|
|
|
// Calculate true airspeed from indicated airspeed
|
|
|
|
AirspeedSensorData airspeedSensor;
|
|
|
|
AirspeedSensorGet(&airspeedSensor);
|
|
|
|
|
2013-05-18 19:36:45 +02:00
|
|
|
AirspeedStateData airspeed;
|
|
|
|
AirspeedStateGet(&airspeed);
|
2013-05-18 14:17:26 +02:00
|
|
|
|
2013-05-18 19:36:45 +02:00
|
|
|
PositionStateData positionState;
|
|
|
|
PositionStateGet(&positionState);
|
2013-05-18 14:17:26 +02:00
|
|
|
|
|
|
|
if (airspeedSensor.SensorConnected == AIRSPEEDSENSOR_SENSORCONNECTED_TRUE) {
|
|
|
|
// we have airspeed available
|
|
|
|
airspeed.CalibratedAirspeed = airspeedSensor.CalibratedAirspeed;
|
2014-03-26 09:15:56 +01:00
|
|
|
airspeed.TrueAirspeed = (airspeedSensor.TrueAirspeed < 0.f) ? airspeed.CalibratedAirspeed * IAS2TAS(homeLocation.Altitude - positionState.Down) : airspeedSensor.TrueAirspeed;
|
2013-05-18 19:36:45 +02:00
|
|
|
AirspeedStateSet(&airspeed);
|
2013-05-18 14:17:26 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-06-15 17:28:30 +02:00
|
|
|
if (!init && flightStatus.Armed == FLIGHTSTATUS_ARMED_DISARMED) {
|
2013-06-15 16:22:24 +02:00
|
|
|
AlarmsSet(SYSTEMALARMS_ALARM_ATTITUDE, SYSTEMALARMS_ALARM_ERROR);
|
|
|
|
} else if (variance_error) {
|
2013-05-18 14:17:26 +02:00
|
|
|
AlarmsSet(SYSTEMALARMS_ALARM_ATTITUDE, SYSTEMALARMS_ALARM_CRITICAL);
|
|
|
|
} else {
|
|
|
|
AlarmsClear(SYSTEMALARMS_ALARM_ATTITUDE);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return 0;
|
2011-11-14 17:49:37 +01:00
|
|
|
}
|
|
|
|
|
2011-12-14 08:54:06 +01:00
|
|
|
#include "insgps.h"
|
|
|
|
int32_t ins_failed = 0;
|
|
|
|
extern struct NavStruct Nav;
|
2012-02-28 07:42:18 +01:00
|
|
|
int32_t init_stage = 0;
|
|
|
|
|
2012-02-29 16:34:11 +01:00
|
|
|
/**
|
|
|
|
* @brief Use the INSGPS fusion algorithm in either indoor or outdoor mode (use GPS)
|
|
|
|
* @params[in] first_run This is the first run so trigger reinitialization
|
|
|
|
* @params[in] outdoor_mode If true use the GPS for position, if false weakly pull to (0,0)
|
|
|
|
* @return 0 for success, -1 for failure
|
|
|
|
*/
|
|
|
|
static int32_t updateAttitudeINSGPS(bool first_run, bool outdoor_mode)
|
2011-12-14 08:54:06 +01:00
|
|
|
{
|
2013-05-18 14:17:26 +02:00
|
|
|
UAVObjEvent ev;
|
2013-05-18 19:36:45 +02:00
|
|
|
GyroSensorData gyroSensorData;
|
|
|
|
AccelSensorData accelSensorData;
|
2013-05-20 10:33:02 +02:00
|
|
|
MagStateData magData;
|
2013-05-18 14:17:26 +02:00
|
|
|
AirspeedSensorData airspeedData;
|
2013-05-18 19:36:45 +02:00
|
|
|
BaroSensorData baroData;
|
2013-05-22 21:45:06 +02:00
|
|
|
GPSPositionSensorData gpsData;
|
|
|
|
GPSVelocitySensorData gpsVelData;
|
2013-05-18 14:17:26 +02:00
|
|
|
|
|
|
|
static bool mag_updated = false;
|
|
|
|
static bool baro_updated;
|
|
|
|
static bool airspeed_updated;
|
|
|
|
static bool gps_updated;
|
|
|
|
static bool gps_vel_updated;
|
|
|
|
|
|
|
|
static bool value_error = false;
|
|
|
|
|
|
|
|
static float baroOffset = 0.0f;
|
|
|
|
|
|
|
|
static uint32_t ins_last_time = 0;
|
|
|
|
static bool inited;
|
|
|
|
|
|
|
|
float NED[3] = { 0.0f, 0.0f, 0.0f };
|
|
|
|
float vel[3] = { 0.0f, 0.0f, 0.0f };
|
|
|
|
float zeros[3] = { 0.0f, 0.0f, 0.0f };
|
|
|
|
|
|
|
|
// Perform the update
|
|
|
|
uint16_t sensors = 0;
|
|
|
|
float dT;
|
|
|
|
|
|
|
|
// Wait until the gyro and accel object is updated, if a timeout then go to failsafe
|
|
|
|
if ((xQueueReceive(gyroQueue, &ev, FAILSAFE_TIMEOUT_MS / portTICK_RATE_MS) != pdTRUE) ||
|
|
|
|
(xQueueReceive(accelQueue, &ev, 1 / portTICK_RATE_MS) != pdTRUE)) {
|
|
|
|
// Do not set attitude timeout warnings in simulation mode
|
2013-05-18 19:36:45 +02:00
|
|
|
if (!AttitudeStateReadOnly()) {
|
2013-05-18 14:17:26 +02:00
|
|
|
AlarmsSet(SYSTEMALARMS_ALARM_ATTITUDE, SYSTEMALARMS_ALARM_WARNING);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (inited) {
|
|
|
|
mag_updated = 0;
|
|
|
|
baro_updated = 0;
|
|
|
|
airspeed_updated = 0;
|
|
|
|
gps_updated = 0;
|
|
|
|
gps_vel_updated = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (first_run) {
|
|
|
|
inited = false;
|
|
|
|
init_stage = 0;
|
|
|
|
|
|
|
|
mag_updated = 0;
|
|
|
|
baro_updated = 0;
|
|
|
|
airspeed_updated = 0;
|
|
|
|
gps_updated = 0;
|
|
|
|
gps_vel_updated = 0;
|
|
|
|
|
|
|
|
ins_last_time = PIOS_DELAY_GetRaw();
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
mag_updated |= (xQueueReceive(magQueue, &ev, 0 / portTICK_RATE_MS) == pdTRUE);
|
|
|
|
baro_updated |= xQueueReceive(baroQueue, &ev, 0 / portTICK_RATE_MS) == pdTRUE;
|
|
|
|
airspeed_updated |= xQueueReceive(airspeedQueue, &ev, 0 / portTICK_RATE_MS) == pdTRUE;
|
|
|
|
|
|
|
|
// Check if we are running simulation
|
2013-05-22 21:45:06 +02:00
|
|
|
if (!GPSPositionSensorReadOnly()) {
|
2013-05-18 14:17:26 +02:00
|
|
|
gps_updated |= (xQueueReceive(gpsQueue, &ev, 0 / portTICK_RATE_MS) == pdTRUE) && outdoor_mode;
|
|
|
|
} else {
|
|
|
|
gps_updated |= pdTRUE && outdoor_mode;
|
|
|
|
}
|
2013-04-27 15:31:37 +02:00
|
|
|
|
2013-05-22 21:45:06 +02:00
|
|
|
if (!GPSVelocitySensorReadOnly()) {
|
2013-04-27 15:31:37 +02:00
|
|
|
gps_vel_updated |= (xQueueReceive(gpsVelQueue, &ev, 0 / portTICK_RATE_MS) == pdTRUE) && outdoor_mode;
|
|
|
|
} else {
|
|
|
|
gps_vel_updated |= pdTRUE && outdoor_mode;
|
|
|
|
}
|
2012-08-12 23:11:04 +02:00
|
|
|
|
2013-05-18 14:17:26 +02:00
|
|
|
// Get most recent data
|
2013-05-18 19:36:45 +02:00
|
|
|
GyroSensorGet(&gyroSensorData);
|
|
|
|
AccelSensorGet(&accelSensorData);
|
2013-05-18 21:55:47 +02:00
|
|
|
// TODO: separate filter!
|
|
|
|
if (mag_updated) {
|
2013-05-20 10:33:02 +02:00
|
|
|
MagSensorData mags;
|
|
|
|
MagSensorGet(&mags);
|
2013-05-18 21:55:47 +02:00
|
|
|
if (revoCalibration.MagBiasNullingRate > 0) {
|
|
|
|
magOffsetEstimation(&mags);
|
|
|
|
}
|
|
|
|
magData.x = mags.x;
|
|
|
|
magData.y = mags.y;
|
|
|
|
magData.z = mags.z;
|
2013-05-20 10:33:02 +02:00
|
|
|
MagStateSet(&magData);
|
2013-05-29 22:18:08 +02:00
|
|
|
} else {
|
|
|
|
MagStateGet(&magData);
|
2013-05-18 21:55:47 +02:00
|
|
|
}
|
|
|
|
|
2013-05-18 19:36:45 +02:00
|
|
|
BaroSensorGet(&baroData);
|
2013-05-18 14:17:26 +02:00
|
|
|
AirspeedSensorGet(&airspeedData);
|
2013-05-22 21:45:06 +02:00
|
|
|
GPSPositionSensorGet(&gpsData);
|
|
|
|
GPSVelocitySensorGet(&gpsVelData);
|
2013-05-18 14:17:26 +02:00
|
|
|
|
2013-05-18 21:55:47 +02:00
|
|
|
// TODO: put in separate filter
|
|
|
|
AccelStateData accelState;
|
|
|
|
accelState.x = accelSensorData.x;
|
|
|
|
accelState.y = accelSensorData.y;
|
|
|
|
accelState.z = accelSensorData.z;
|
|
|
|
AccelStateSet(&accelState);
|
|
|
|
|
|
|
|
|
2013-05-18 14:17:26 +02:00
|
|
|
value_error = false;
|
|
|
|
// safety checks
|
2013-05-18 19:36:45 +02:00
|
|
|
if (invalid(gyroSensorData.x) ||
|
|
|
|
invalid(gyroSensorData.y) ||
|
|
|
|
invalid(gyroSensorData.z) ||
|
|
|
|
invalid(accelSensorData.x) ||
|
|
|
|
invalid(accelSensorData.y) ||
|
|
|
|
invalid(accelSensorData.z)) {
|
2013-05-18 14:17:26 +02:00
|
|
|
// cannot run process update, raise error!
|
|
|
|
AlarmsSet(SYSTEMALARMS_ALARM_ATTITUDE, SYSTEMALARMS_ALARM_ERROR);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (invalid(magData.x) ||
|
|
|
|
invalid(magData.y) ||
|
|
|
|
invalid(magData.z)) {
|
|
|
|
// magnetometers can be ignored for a while
|
|
|
|
mag_updated = false;
|
|
|
|
value_error = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Don't require HomeLocation.Set to be true but at least require a mag configuration (allows easily
|
|
|
|
// switching between indoor and outdoor mode with Set = false)
|
|
|
|
if ((homeLocation.Be[0] * homeLocation.Be[0] + homeLocation.Be[1] * homeLocation.Be[1] + homeLocation.Be[2] * homeLocation.Be[2] < 1e-5f)) {
|
|
|
|
mag_updated = false;
|
|
|
|
value_error = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (invalid(baroData.Altitude)) {
|
|
|
|
baro_updated = false;
|
|
|
|
value_error = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (invalid(airspeedData.CalibratedAirspeed)) {
|
|
|
|
airspeed_updated = false;
|
|
|
|
value_error = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (invalid(gpsData.Altitude)) {
|
|
|
|
gps_updated = false;
|
|
|
|
value_error = true;
|
|
|
|
}
|
|
|
|
|
2013-09-01 12:10:55 +02:00
|
|
|
if (invalid_var(ekfConfiguration.R.GPSPosNorth) ||
|
|
|
|
invalid_var(ekfConfiguration.R.GPSPosEast) ||
|
|
|
|
invalid_var(ekfConfiguration.R.GPSPosDown) ||
|
|
|
|
invalid_var(ekfConfiguration.R.GPSVelNorth) ||
|
|
|
|
invalid_var(ekfConfiguration.R.GPSVelEast) ||
|
|
|
|
invalid_var(ekfConfiguration.R.GPSVelDown)) {
|
2013-05-18 14:17:26 +02:00
|
|
|
gps_updated = false;
|
|
|
|
value_error = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (invalid(gpsVelData.North) ||
|
|
|
|
invalid(gpsVelData.East) ||
|
|
|
|
invalid(gpsVelData.Down)) {
|
|
|
|
gps_vel_updated = false;
|
|
|
|
value_error = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Discard airspeed if sensor not connected
|
|
|
|
if (airspeedData.SensorConnected != AIRSPEEDSENSOR_SENSORCONNECTED_TRUE) {
|
|
|
|
airspeed_updated = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Have a minimum requirement for gps usage
|
|
|
|
if ((gpsData.Satellites < 7) ||
|
|
|
|
(gpsData.PDOP > 4.0f) ||
|
|
|
|
(gpsData.Latitude == 0 && gpsData.Longitude == 0) ||
|
|
|
|
(homeLocation.Set != HOMELOCATION_SET_TRUE)) {
|
|
|
|
gps_updated = false;
|
|
|
|
gps_vel_updated = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!inited) {
|
|
|
|
AlarmsSet(SYSTEMALARMS_ALARM_ATTITUDE, SYSTEMALARMS_ALARM_ERROR);
|
|
|
|
} else if (value_error) {
|
|
|
|
AlarmsSet(SYSTEMALARMS_ALARM_ATTITUDE, SYSTEMALARMS_ALARM_CRITICAL);
|
|
|
|
} else if (variance_error) {
|
|
|
|
AlarmsSet(SYSTEMALARMS_ALARM_ATTITUDE, SYSTEMALARMS_ALARM_CRITICAL);
|
|
|
|
} else if (outdoor_mode && gpsData.Satellites < 7) {
|
|
|
|
AlarmsSet(SYSTEMALARMS_ALARM_ATTITUDE, SYSTEMALARMS_ALARM_ERROR);
|
|
|
|
} else {
|
|
|
|
AlarmsClear(SYSTEMALARMS_ALARM_ATTITUDE);
|
|
|
|
}
|
|
|
|
|
|
|
|
dT = PIOS_DELAY_DiffuS(ins_last_time) / 1.0e6f;
|
|
|
|
ins_last_time = PIOS_DELAY_GetRaw();
|
|
|
|
|
|
|
|
// This should only happen at start up or at mode switches
|
|
|
|
if (dT > 0.01f) {
|
|
|
|
dT = 0.01f;
|
|
|
|
} else if (dT <= 0.001f) {
|
|
|
|
dT = 0.001f;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!inited && mag_updated && baro_updated && (gps_updated || !outdoor_mode) && !variance_error) {
|
|
|
|
// Don't initialize until all sensors are read
|
|
|
|
if (init_stage == 0) {
|
|
|
|
// Reset the INS algorithm
|
|
|
|
INSGPSInit();
|
2013-09-01 12:10:55 +02:00
|
|
|
INSSetMagVar((float[3]) { ekfConfiguration.R.MagX,
|
|
|
|
ekfConfiguration.R.MagY,
|
|
|
|
ekfConfiguration.R.MagZ }
|
2013-05-18 14:17:26 +02:00
|
|
|
);
|
2013-09-01 12:10:55 +02:00
|
|
|
INSSetAccelVar((float[3]) { ekfConfiguration.Q.AccelX,
|
|
|
|
ekfConfiguration.Q.AccelY,
|
|
|
|
ekfConfiguration.Q.AccelZ }
|
2013-05-18 14:17:26 +02:00
|
|
|
);
|
2013-09-01 12:10:55 +02:00
|
|
|
INSSetGyroVar((float[3]) { ekfConfiguration.Q.GyroX,
|
|
|
|
ekfConfiguration.Q.GyroY,
|
|
|
|
ekfConfiguration.Q.GyroZ }
|
2013-05-18 14:17:26 +02:00
|
|
|
);
|
2013-09-01 12:10:55 +02:00
|
|
|
INSSetGyroBiasVar((float[3]) { ekfConfiguration.Q.GyroDriftX,
|
|
|
|
ekfConfiguration.Q.GyroDriftY,
|
|
|
|
ekfConfiguration.Q.GyroDriftZ }
|
2013-05-18 14:17:26 +02:00
|
|
|
);
|
2013-09-01 12:10:55 +02:00
|
|
|
INSSetBaroVar(ekfConfiguration.R.BaroZ);
|
2013-05-18 14:17:26 +02:00
|
|
|
|
|
|
|
// Initialize the gyro bias
|
|
|
|
float gyro_bias[3] = { 0.0f, 0.0f, 0.0f };
|
|
|
|
INSSetGyroBias(gyro_bias);
|
|
|
|
|
|
|
|
float pos[3] = { 0.0f, 0.0f, 0.0f };
|
|
|
|
|
|
|
|
if (outdoor_mode) {
|
2013-05-22 21:45:06 +02:00
|
|
|
GPSPositionSensorData gpsPosition;
|
|
|
|
GPSPositionSensorGet(&gpsPosition);
|
2013-05-18 14:17:26 +02:00
|
|
|
|
|
|
|
// Transform the GPS position into NED coordinates
|
|
|
|
getNED(&gpsPosition, pos);
|
|
|
|
|
|
|
|
// Initialize barometric offset to current GPS NED coordinate
|
|
|
|
baroOffset = -pos[2] - baroData.Altitude;
|
|
|
|
} else {
|
|
|
|
// Initialize barometric offset to homelocation altitude
|
|
|
|
baroOffset = -baroData.Altitude;
|
|
|
|
pos[2] = -(baroData.Altitude + baroOffset);
|
|
|
|
}
|
|
|
|
|
2013-05-18 21:55:47 +02:00
|
|
|
// xQueueReceive(magQueue, &ev, 100 / portTICK_RATE_MS);
|
2013-05-20 10:33:02 +02:00
|
|
|
// MagSensorGet(&magData);
|
2013-05-18 14:17:26 +02:00
|
|
|
|
2013-05-18 19:36:45 +02:00
|
|
|
AttitudeStateData attitudeState;
|
|
|
|
AttitudeStateGet(&attitudeState);
|
2013-05-18 14:17:26 +02:00
|
|
|
|
|
|
|
// Set initial attitude. Use accels to determine roll and pitch, rotate magnetic measurement accordingly,
|
|
|
|
// so pseudo "north" vector can be estimated even if the board is not level
|
2013-05-18 19:36:45 +02:00
|
|
|
attitudeState.Roll = atan2f(-accelSensorData.y, -accelSensorData.z);
|
|
|
|
float zn = cosf(attitudeState.Roll) * magData.z + sinf(attitudeState.Roll) * magData.y;
|
|
|
|
float yn = cosf(attitudeState.Roll) * magData.y - sinf(attitudeState.Roll) * magData.z;
|
2013-05-18 14:17:26 +02:00
|
|
|
|
|
|
|
// rotate accels z vector according to roll
|
2013-05-18 19:36:45 +02:00
|
|
|
float azn = cosf(attitudeState.Roll) * accelSensorData.z + sinf(attitudeState.Roll) * accelSensorData.y;
|
|
|
|
attitudeState.Pitch = atan2f(accelSensorData.x, -azn);
|
2013-05-18 14:17:26 +02:00
|
|
|
|
2013-05-18 19:36:45 +02:00
|
|
|
float xn = cosf(attitudeState.Pitch) * magData.x + sinf(attitudeState.Pitch) * zn;
|
2013-05-18 14:17:26 +02:00
|
|
|
|
2013-05-18 19:36:45 +02:00
|
|
|
attitudeState.Yaw = atan2f(-yn, xn);
|
2013-05-18 14:17:26 +02:00
|
|
|
// TODO: This is still a hack
|
|
|
|
// Put this in a proper generic function in CoordinateConversion.c
|
|
|
|
// should take 4 vectors: g (0,0,-9.81), accels, Be (or 1,0,0 if no home loc) and magnetometers (or 1,0,0 if no mags)
|
|
|
|
// should calculate the rotation in 3d space using proper cross product math
|
|
|
|
// SUBTODO: formulate the math required
|
|
|
|
|
2013-05-18 19:36:45 +02:00
|
|
|
attitudeState.Roll = RAD2DEG(attitudeState.Roll);
|
|
|
|
attitudeState.Pitch = RAD2DEG(attitudeState.Pitch);
|
|
|
|
attitudeState.Yaw = RAD2DEG(attitudeState.Yaw);
|
2013-05-18 14:17:26 +02:00
|
|
|
|
2013-05-18 19:36:45 +02:00
|
|
|
RPY2Quaternion(&attitudeState.Roll, &attitudeState.q1);
|
|
|
|
AttitudeStateSet(&attitudeState);
|
2013-05-18 14:17:26 +02:00
|
|
|
|
2013-05-18 19:36:45 +02:00
|
|
|
float q[4] = { attitudeState.q1, attitudeState.q2, attitudeState.q3, attitudeState.q4 };
|
2013-05-18 14:17:26 +02:00
|
|
|
INSSetState(pos, zeros, q, zeros, zeros);
|
|
|
|
|
2013-09-01 12:10:55 +02:00
|
|
|
INSResetP(cast_struct_to_array(ekfConfiguration.P, ekfConfiguration.P.AttitudeQ1));
|
2013-05-18 14:17:26 +02:00
|
|
|
} else {
|
|
|
|
// Run prediction a bit before any corrections
|
|
|
|
|
|
|
|
// Because the sensor module remove the bias we need to add it
|
|
|
|
// back in here so that the INS algorithm can track it correctly
|
2013-05-18 19:36:45 +02:00
|
|
|
float gyros[3] = { DEG2RAD(gyroSensorData.x), DEG2RAD(gyroSensorData.y), DEG2RAD(gyroSensorData.z) };
|
|
|
|
INSStatePrediction(gyros, &accelSensorData.x, dT);
|
|
|
|
|
|
|
|
AttitudeStateData attitude;
|
|
|
|
AttitudeStateGet(&attitude);
|
2013-05-18 14:17:26 +02:00
|
|
|
attitude.q1 = Nav.q[0];
|
|
|
|
attitude.q2 = Nav.q[1];
|
|
|
|
attitude.q3 = Nav.q[2];
|
|
|
|
attitude.q4 = Nav.q[3];
|
|
|
|
Quaternion2RPY(&attitude.q1, &attitude.Roll);
|
2013-05-18 19:36:45 +02:00
|
|
|
AttitudeStateSet(&attitude);
|
2013-05-18 14:17:26 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
init_stage++;
|
|
|
|
if (init_stage > 10) {
|
|
|
|
inited = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!inited) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Because the sensor module remove the bias we need to add it
|
|
|
|
// back in here so that the INS algorithm can track it correctly
|
2013-05-18 19:36:45 +02:00
|
|
|
float gyros[3] = { DEG2RAD(gyroSensorData.x), DEG2RAD(gyroSensorData.y), DEG2RAD(gyroSensorData.z) };
|
2013-05-18 14:17:26 +02:00
|
|
|
|
|
|
|
// Advance the state estimate
|
2013-05-18 19:36:45 +02:00
|
|
|
INSStatePrediction(gyros, &accelSensorData.x, dT);
|
2013-05-18 14:17:26 +02:00
|
|
|
|
|
|
|
// Copy the attitude into the UAVO
|
2013-05-18 19:36:45 +02:00
|
|
|
AttitudeStateData attitude;
|
|
|
|
AttitudeStateGet(&attitude);
|
2013-05-18 14:17:26 +02:00
|
|
|
attitude.q1 = Nav.q[0];
|
|
|
|
attitude.q2 = Nav.q[1];
|
|
|
|
attitude.q3 = Nav.q[2];
|
|
|
|
attitude.q4 = Nav.q[3];
|
|
|
|
Quaternion2RPY(&attitude.q1, &attitude.Roll);
|
2013-05-18 19:36:45 +02:00
|
|
|
AttitudeStateSet(&attitude);
|
2013-05-18 14:17:26 +02:00
|
|
|
|
|
|
|
// Advance the covariance estimate
|
|
|
|
INSCovariancePrediction(dT);
|
|
|
|
|
|
|
|
if (mag_updated) {
|
|
|
|
sensors |= MAG_SENSORS;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (baro_updated) {
|
|
|
|
sensors |= BARO_SENSOR;
|
|
|
|
}
|
|
|
|
|
|
|
|
INSSetMagNorth(homeLocation.Be);
|
|
|
|
|
|
|
|
if (gps_updated && outdoor_mode) {
|
2013-09-01 12:10:55 +02:00
|
|
|
INSSetPosVelVar((float[3]) { ekfConfiguration.R.GPSPosNorth,
|
|
|
|
ekfConfiguration.R.GPSPosEast,
|
|
|
|
ekfConfiguration.R.GPSPosDown },
|
|
|
|
(float[3]) { ekfConfiguration.R.GPSVelNorth,
|
|
|
|
ekfConfiguration.R.GPSVelEast,
|
|
|
|
ekfConfiguration.R.GPSVelDown }
|
2013-05-18 14:17:26 +02:00
|
|
|
);
|
|
|
|
sensors |= POS_SENSORS;
|
|
|
|
|
|
|
|
if (0) { // Old code to take horizontal velocity from GPS Position update
|
|
|
|
sensors |= HORIZ_SENSORS;
|
|
|
|
vel[0] = gpsData.Groundspeed * cosf(DEG2RAD(gpsData.Heading));
|
|
|
|
vel[1] = gpsData.Groundspeed * sinf(DEG2RAD(gpsData.Heading));
|
|
|
|
vel[2] = 0.0f;
|
|
|
|
}
|
|
|
|
// Transform the GPS position into NED coordinates
|
|
|
|
getNED(&gpsData, NED);
|
|
|
|
|
|
|
|
// Track barometric altitude offset with a low pass filter
|
|
|
|
baroOffset = BARO_OFFSET_LOWPASS_ALPHA * baroOffset +
|
|
|
|
(1.0f - BARO_OFFSET_LOWPASS_ALPHA)
|
|
|
|
* (-NED[2] - baroData.Altitude);
|
|
|
|
} else if (!outdoor_mode) {
|
2013-09-01 12:10:55 +02:00
|
|
|
INSSetPosVelVar((float[3]) { ekfConfiguration.FakeR.FakeGPSPosIndoor,
|
|
|
|
ekfConfiguration.FakeR.FakeGPSPosIndoor,
|
|
|
|
ekfConfiguration.FakeR.FakeGPSPosIndoor },
|
|
|
|
(float[3]) { ekfConfiguration.FakeR.FakeGPSVelIndoor,
|
|
|
|
ekfConfiguration.FakeR.FakeGPSVelIndoor,
|
|
|
|
ekfConfiguration.FakeR.FakeGPSVelIndoor }
|
2013-05-18 14:17:26 +02:00
|
|
|
);
|
|
|
|
vel[0] = vel[1] = vel[2] = 0.0f;
|
|
|
|
NED[0] = NED[1] = 0.0f;
|
|
|
|
NED[2] = -(baroData.Altitude + baroOffset);
|
|
|
|
sensors |= HORIZ_SENSORS | HORIZ_POS_SENSORS;
|
|
|
|
sensors |= POS_SENSORS | VERT_SENSORS;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (gps_vel_updated && outdoor_mode) {
|
|
|
|
sensors |= HORIZ_SENSORS | VERT_SENSORS;
|
|
|
|
vel[0] = gpsVelData.North;
|
|
|
|
vel[1] = gpsVelData.East;
|
|
|
|
vel[2] = gpsVelData.Down;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (airspeed_updated) {
|
|
|
|
// we have airspeed available
|
2013-05-18 19:36:45 +02:00
|
|
|
AirspeedStateData airspeed;
|
|
|
|
AirspeedStateGet(&airspeed);
|
2013-05-18 14:17:26 +02:00
|
|
|
|
|
|
|
airspeed.CalibratedAirspeed = airspeedData.CalibratedAirspeed;
|
2014-03-26 09:15:56 +01:00
|
|
|
airspeed.TrueAirspeed = (airspeedSensor.TrueAirspeed < 0.f) ? airspeed.CalibratedAirspeed * IAS2TAS(homeLocation.Altitude - positionState.Down) : airspeedSensor.TrueAirspeed;
|
|
|
|
|
2013-05-18 19:36:45 +02:00
|
|
|
AirspeedStateSet(&airspeed);
|
2013-05-18 14:17:26 +02:00
|
|
|
|
|
|
|
if (!gps_vel_updated && !gps_updated) {
|
|
|
|
// feed airspeed into EKF, treat wind as 1e2 variance
|
|
|
|
sensors |= HORIZ_SENSORS | VERT_SENSORS;
|
2013-09-01 12:10:55 +02:00
|
|
|
INSSetPosVelVar((float[3]) { ekfConfiguration.FakeR.FakeGPSPosIndoor,
|
|
|
|
ekfConfiguration.FakeR.FakeGPSPosIndoor,
|
|
|
|
ekfConfiguration.FakeR.FakeGPSPosIndoor },
|
|
|
|
(float[3]) { ekfConfiguration.FakeR.FakeGPSVelAirspeed,
|
|
|
|
ekfConfiguration.FakeR.FakeGPSVelAirspeed,
|
|
|
|
ekfConfiguration.FakeR.FakeGPSVelAirspeed }
|
2013-05-18 14:17:26 +02:00
|
|
|
);
|
|
|
|
// rotate airspeed vector into NED frame - airspeed is measured in X axis only
|
|
|
|
float R[3][3];
|
|
|
|
Quaternion2R(Nav.q, R);
|
|
|
|
float vtas[3] = { airspeed.TrueAirspeed, 0.0f, 0.0f };
|
|
|
|
rot_mult(R, vtas, vel);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* TODO: Need to add a general sanity check for all the inputs to make sure their kosher
|
|
|
|
* although probably should occur within INS itself
|
|
|
|
*/
|
|
|
|
if (sensors) {
|
|
|
|
INSCorrection(&magData.x, NED, vel, (baroData.Altitude + baroOffset), sensors);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Copy the position and velocity into the UAVO
|
2013-05-18 19:36:45 +02:00
|
|
|
PositionStateData positionState;
|
|
|
|
PositionStateGet(&positionState);
|
|
|
|
positionState.North = Nav.Pos[0];
|
|
|
|
positionState.East = Nav.Pos[1];
|
|
|
|
positionState.Down = Nav.Pos[2];
|
|
|
|
PositionStateSet(&positionState);
|
|
|
|
|
|
|
|
VelocityStateData velocityState;
|
|
|
|
VelocityStateGet(&velocityState);
|
|
|
|
velocityState.North = Nav.Vel[0];
|
|
|
|
velocityState.East = Nav.Vel[1];
|
|
|
|
velocityState.Down = Nav.Vel[2];
|
|
|
|
VelocityStateSet(&velocityState);
|
|
|
|
|
2013-05-18 21:55:47 +02:00
|
|
|
GyroStateData gyroState;
|
2013-05-29 22:18:08 +02:00
|
|
|
gyroState.x = RAD2DEG(gyros[0] - RAD2DEG(Nav.gyro_bias[0]));
|
|
|
|
gyroState.y = RAD2DEG(gyros[1] - RAD2DEG(Nav.gyro_bias[1]));
|
|
|
|
gyroState.z = RAD2DEG(gyros[2] - RAD2DEG(Nav.gyro_bias[2]));
|
2013-05-18 21:55:47 +02:00
|
|
|
GyroStateSet(&gyroState);
|
2013-05-18 14:17:26 +02:00
|
|
|
|
|
|
|
EKFStateVarianceData vardata;
|
|
|
|
EKFStateVarianceGet(&vardata);
|
2013-09-01 12:10:55 +02:00
|
|
|
INSGetP(cast_struct_to_array(vardata.P, vardata.P.AttitudeQ1));
|
2013-05-18 14:17:26 +02:00
|
|
|
EKFStateVarianceSet(&vardata);
|
|
|
|
|
|
|
|
return 0;
|
2011-12-14 08:54:06 +01:00
|
|
|
}
|
|
|
|
|
2012-02-29 19:05:54 +01:00
|
|
|
/**
|
|
|
|
* @brief Convert the GPS LLA position into NED coordinates
|
|
|
|
* @note this method uses a taylor expansion around the home coordinates
|
|
|
|
* to convert to NED which allows it to be done with all floating
|
|
|
|
* calculations
|
|
|
|
* @param[in] Current GPS coordinates
|
|
|
|
* @param[out] NED frame coordinates
|
|
|
|
* @returns 0 for success, -1 for failure
|
|
|
|
*/
|
|
|
|
float T[3];
|
2013-05-22 21:45:06 +02:00
|
|
|
static int32_t getNED(GPSPositionSensorData *gpsPosition, float *NED)
|
2012-02-29 19:05:54 +01:00
|
|
|
{
|
2013-05-18 14:17:26 +02:00
|
|
|
float dL[3] = { DEG2RAD((gpsPosition->Latitude - homeLocation.Latitude) / 10.0e6f),
|
|
|
|
DEG2RAD((gpsPosition->Longitude - homeLocation.Longitude) / 10.0e6f),
|
|
|
|
(gpsPosition->Altitude + gpsPosition->GeoidSeparation - homeLocation.Altitude) };
|
2012-02-29 19:05:54 +01:00
|
|
|
|
2013-05-18 14:17:26 +02:00
|
|
|
NED[0] = T[0] * dL[0];
|
|
|
|
NED[1] = T[1] * dL[1];
|
|
|
|
NED[2] = T[2] * dL[2];
|
2012-03-03 01:27:33 +01:00
|
|
|
|
2013-05-18 14:17:26 +02:00
|
|
|
return 0;
|
2012-02-29 19:05:54 +01:00
|
|
|
}
|
|
|
|
|
2013-05-18 14:17:26 +02:00
|
|
|
static void settingsUpdatedCb(UAVObjEvent *ev)
|
2011-12-14 08:54:06 +01:00
|
|
|
{
|
2013-05-18 14:17:26 +02:00
|
|
|
if (ev == NULL || ev->obj == FlightStatusHandle()) {
|
|
|
|
FlightStatusGet(&flightStatus);
|
|
|
|
}
|
|
|
|
if (ev == NULL || ev->obj == RevoCalibrationHandle()) {
|
|
|
|
RevoCalibrationGet(&revoCalibration);
|
|
|
|
}
|
|
|
|
// change of these settings require reinitialization of the EKF
|
|
|
|
// when an error flag has been risen, we also listen to flightStatus updates,
|
|
|
|
// since we are waiting for the system to get disarmed so we can reinitialize safely.
|
|
|
|
if (ev == NULL ||
|
|
|
|
ev->obj == EKFConfigurationHandle() ||
|
|
|
|
ev->obj == RevoSettingsHandle() ||
|
|
|
|
(variance_error == true && ev->obj == FlightStatusHandle())
|
|
|
|
) {
|
|
|
|
bool error = false;
|
|
|
|
|
|
|
|
EKFConfigurationGet(&ekfConfiguration);
|
|
|
|
int t;
|
|
|
|
for (t = 0; t < EKFCONFIGURATION_P_NUMELEM; t++) {
|
2013-09-01 12:10:55 +02:00
|
|
|
if (invalid_var(cast_struct_to_array(ekfConfiguration.P, ekfConfiguration.P.AttitudeQ1)[t])) {
|
2013-05-18 14:17:26 +02:00
|
|
|
error = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
for (t = 0; t < EKFCONFIGURATION_Q_NUMELEM; t++) {
|
2013-09-01 12:10:55 +02:00
|
|
|
if (invalid_var(cast_struct_to_array(ekfConfiguration.Q, ekfConfiguration.Q.AccelX)[t])) {
|
2013-05-18 14:17:26 +02:00
|
|
|
error = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
for (t = 0; t < EKFCONFIGURATION_R_NUMELEM; t++) {
|
2013-09-01 12:10:55 +02:00
|
|
|
if (invalid_var(cast_struct_to_array(ekfConfiguration.R, ekfConfiguration.R.BaroZ)[t])) {
|
2013-05-18 14:17:26 +02:00
|
|
|
error = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
RevoSettingsGet(&revoSettings);
|
|
|
|
|
|
|
|
// Reinitialization of the EKF is not desired during flight.
|
|
|
|
// It will be delayed until the board is disarmed by raising the error flag.
|
|
|
|
// We will not prevent the initial initialization though, since the board could be in always armed mode.
|
|
|
|
if (flightStatus.Armed == FLIGHTSTATUS_ARMED_ARMED && !initialization_required) {
|
|
|
|
error = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (error) {
|
|
|
|
variance_error = true;
|
|
|
|
} else {
|
|
|
|
// trigger reinitialization - possibly with new algorithm
|
|
|
|
running_algorithm = revoSettings.FusionAlgorithm;
|
|
|
|
variance_error = false;
|
|
|
|
initialization_required = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (ev == NULL || ev->obj == HomeLocationHandle()) {
|
|
|
|
HomeLocationGet(&homeLocation);
|
|
|
|
// Compute matrix to convert deltaLLA to NED
|
|
|
|
float lat, alt;
|
|
|
|
lat = DEG2RAD(homeLocation.Latitude / 10.0e6f);
|
|
|
|
alt = homeLocation.Altitude;
|
|
|
|
|
|
|
|
T[0] = alt + 6.378137E6f;
|
|
|
|
T[1] = cosf(lat) * (alt + 6.378137E6f);
|
|
|
|
T[2] = -1.0f;
|
|
|
|
|
2013-05-18 19:36:45 +02:00
|
|
|
// TODO: convert positionState to new reference frame and gracefully update EKF state!
|
2013-05-18 14:17:26 +02:00
|
|
|
// needed for long range flights where the reference coordinate is adjusted in flight
|
|
|
|
}
|
|
|
|
if (ev == NULL || ev->obj == AttitudeSettingsHandle()) {
|
|
|
|
AttitudeSettingsGet(&attitudeSettings);
|
2013-05-22 00:55:10 +02:00
|
|
|
|
|
|
|
// Calculate accel filter alpha, in the same way as for gyro data in stabilization module.
|
2013-05-27 21:05:39 +02:00
|
|
|
const float fakeDt = 0.0015f;
|
2013-05-22 00:55:10 +02:00
|
|
|
if (attitudeSettings.AccelTau < 0.0001f) {
|
|
|
|
accel_alpha = 0; // not trusting this to resolve to 0
|
|
|
|
accel_filter_enabled = false;
|
|
|
|
} else {
|
|
|
|
accel_alpha = expf(-fakeDt / attitudeSettings.AccelTau);
|
|
|
|
accel_filter_enabled = true;
|
|
|
|
}
|
2013-05-18 14:17:26 +02:00
|
|
|
}
|
2011-11-14 17:49:37 +01:00
|
|
|
}
|
2013-05-18 21:55:47 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Perform an update of the @ref MagBias based on
|
2013-05-20 10:33:02 +02:00
|
|
|
* Magmeter Offset Cancellation: Theory and Implementation,
|
2013-05-18 21:55:47 +02:00
|
|
|
* revisited William Premerlani, October 14, 2011
|
|
|
|
*/
|
2013-05-20 10:33:02 +02:00
|
|
|
static void magOffsetEstimation(MagSensorData *mag)
|
2013-05-18 21:55:47 +02:00
|
|
|
{
|
|
|
|
#if 0
|
|
|
|
// Constants, to possibly go into a UAVO
|
|
|
|
static const float MIN_NORM_DIFFERENCE = 50;
|
|
|
|
|
|
|
|
static float B2[3] = { 0, 0, 0 };
|
|
|
|
|
|
|
|
MagBiasData magBias;
|
|
|
|
MagBiasGet(&magBias);
|
|
|
|
|
|
|
|
// Remove the current estimate of the bias
|
|
|
|
mag->x -= magBias.x;
|
|
|
|
mag->y -= magBias.y;
|
|
|
|
mag->z -= magBias.z;
|
|
|
|
|
|
|
|
// First call
|
|
|
|
if (B2[0] == 0 && B2[1] == 0 && B2[2] == 0) {
|
|
|
|
B2[0] = mag->x;
|
|
|
|
B2[1] = mag->y;
|
|
|
|
B2[2] = mag->z;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
float B1[3] = { mag->x, mag->y, mag->z };
|
|
|
|
float norm_diff = sqrtf(powf(B2[0] - B1[0], 2) + powf(B2[1] - B1[1], 2) + powf(B2[2] - B1[2], 2));
|
|
|
|
if (norm_diff > MIN_NORM_DIFFERENCE) {
|
|
|
|
float norm_b1 = sqrtf(B1[0] * B1[0] + B1[1] * B1[1] + B1[2] * B1[2]);
|
|
|
|
float norm_b2 = sqrtf(B2[0] * B2[0] + B2[1] * B2[1] + B2[2] * B2[2]);
|
|
|
|
float scale = cal.MagBiasNullingRate * (norm_b2 - norm_b1) / norm_diff;
|
|
|
|
float b_error[3] = { (B2[0] - B1[0]) * scale, (B2[1] - B1[1]) * scale, (B2[2] - B1[2]) * scale };
|
|
|
|
|
|
|
|
magBias.x += b_error[0];
|
|
|
|
magBias.y += b_error[1];
|
|
|
|
magBias.z += b_error[2];
|
|
|
|
|
|
|
|
MagBiasSet(&magBias);
|
|
|
|
|
|
|
|
// Store this value to compare against next update
|
|
|
|
B2[0] = B1[0]; B2[1] = B1[1]; B2[2] = B1[2];
|
|
|
|
}
|
|
|
|
#else // if 0
|
|
|
|
static float magBias[3] = { 0 };
|
|
|
|
|
|
|
|
// Remove the current estimate of the bias
|
|
|
|
mag->x -= magBias[0];
|
|
|
|
mag->y -= magBias[1];
|
|
|
|
mag->z -= magBias[2];
|
|
|
|
|
|
|
|
AttitudeStateData attitude;
|
|
|
|
AttitudeStateGet(&attitude);
|
|
|
|
|
|
|
|
const float Rxy = sqrtf(homeLocation.Be[0] * homeLocation.Be[0] + homeLocation.Be[1] * homeLocation.Be[1]);
|
|
|
|
const float Rz = homeLocation.Be[2];
|
|
|
|
|
|
|
|
const float rate = revoCalibration.MagBiasNullingRate;
|
|
|
|
float Rot[3][3];
|
|
|
|
float B_e[3];
|
|
|
|
float xy[2];
|
|
|
|
float delta[3];
|
|
|
|
|
|
|
|
// Get the rotation matrix
|
|
|
|
Quaternion2R(&attitude.q1, Rot);
|
|
|
|
|
|
|
|
// Rotate the mag into the NED frame
|
|
|
|
B_e[0] = Rot[0][0] * mag->x + Rot[1][0] * mag->y + Rot[2][0] * mag->z;
|
|
|
|
B_e[1] = Rot[0][1] * mag->x + Rot[1][1] * mag->y + Rot[2][1] * mag->z;
|
|
|
|
B_e[2] = Rot[0][2] * mag->x + Rot[1][2] * mag->y + Rot[2][2] * mag->z;
|
|
|
|
|
|
|
|
float cy = cosf(DEG2RAD(attitude.Yaw));
|
|
|
|
float sy = sinf(DEG2RAD(attitude.Yaw));
|
|
|
|
|
|
|
|
xy[0] = cy * B_e[0] + sy * B_e[1];
|
|
|
|
xy[1] = -sy * B_e[0] + cy * B_e[1];
|
|
|
|
|
|
|
|
float xy_norm = sqrtf(xy[0] * xy[0] + xy[1] * xy[1]);
|
|
|
|
|
|
|
|
delta[0] = -rate * (xy[0] / xy_norm * Rxy - xy[0]);
|
|
|
|
delta[1] = -rate * (xy[1] / xy_norm * Rxy - xy[1]);
|
|
|
|
delta[2] = -rate * (Rz - B_e[2]);
|
|
|
|
|
|
|
|
if (!isnan(delta[0]) && !isinf(delta[0]) &&
|
|
|
|
!isnan(delta[1]) && !isinf(delta[1]) &&
|
|
|
|
!isnan(delta[2]) && !isinf(delta[2])) {
|
|
|
|
magBias[0] += delta[0];
|
|
|
|
magBias[1] += delta[1];
|
|
|
|
magBias[2] += delta[2];
|
|
|
|
}
|
|
|
|
#endif // if 0
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-11-14 17:49:37 +01:00
|
|
|
/**
|
2012-02-28 07:42:18 +01:00
|
|
|
* @}
|
|
|
|
* @}
|
|
|
|
*/
|