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
|
|
|
|
* Specifically updates the the @ref AttitudeActual "AttitudeActual" and @ref AttitudeRaw "AttitudeRaw" settings objects
|
|
|
|
* @{
|
|
|
|
*
|
|
|
|
* @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
|
|
|
|
* Output objects: @ref AttitudeRaw @ref AttitudeActual
|
|
|
|
*
|
|
|
|
* 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
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "pios.h"
|
2012-03-12 00:34:19 +01:00
|
|
|
|
2012-02-29 16:34:11 +01:00
|
|
|
#include "attitude.h"
|
2012-03-12 00:34:19 +01:00
|
|
|
|
|
|
|
#include "accels.h"
|
2012-11-08 11:30:58 +01:00
|
|
|
#include "airspeedsensor.h"
|
|
|
|
#include "airspeedactual.h"
|
2011-11-14 17:49:37 +01:00
|
|
|
#include "attitudeactual.h"
|
|
|
|
#include "attitudesettings.h"
|
2011-12-14 08:54:06 +01:00
|
|
|
#include "baroaltitude.h"
|
2011-11-14 17:49:37 +01:00
|
|
|
#include "flightstatus.h"
|
2012-02-28 07:42:18 +01:00
|
|
|
#include "gpsposition.h"
|
2012-04-10 02:03:21 +02:00
|
|
|
#include "gpsvelocity.h"
|
2012-02-29 16:34:11 +01:00
|
|
|
#include "gyros.h"
|
|
|
|
#include "gyrosbias.h"
|
|
|
|
#include "homelocation.h"
|
|
|
|
#include "magnetometer.h"
|
|
|
|
#include "positionactual.h"
|
2012-03-01 11:52:09 +01:00
|
|
|
#include "revocalibration.h"
|
2012-02-29 16:34:11 +01:00
|
|
|
#include "revosettings.h"
|
|
|
|
#include "velocityactual.h"
|
2011-11-14 17:49:37 +01:00
|
|
|
#include "CoordinateConversions.h"
|
2013-04-09 01:07:59 +02:00
|
|
|
#include <pios_math.h>
|
2011-11-14 17:49:37 +01:00
|
|
|
// Private constants
|
2012-05-05 18:33:47 +02:00
|
|
|
#define STACK_SIZE_BYTES 2048
|
2011-11-14 17:49:37 +01:00
|
|
|
#define TASK_PRIORITY (tskIDLE_PRIORITY+3)
|
2011-12-12 21:39:38 +01:00
|
|
|
#define FAILSAFE_TIMEOUT_MS 10
|
2011-11-14 17:49:37 +01:00
|
|
|
|
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
|
|
|
|
#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
|
|
|
|
#define IAS2TAS(alt) (1.0f + (0.02f*(alt)/304.8f))
|
|
|
|
|
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;
|
|
|
|
static RevoSettingsData revoSettings;
|
2011-12-12 06:29:43 +01:00
|
|
|
const uint32_t SENSOR_QUEUE_SIZE = 10;
|
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);
|
2011-11-14 17:49:37 +01:00
|
|
|
static void settingsUpdatedCb(UAVObjEvent * objEv);
|
|
|
|
|
2012-02-29 19:05:54 +01:00
|
|
|
static int32_t getNED(GPSPositionData * gpsPosition, float * NED);
|
|
|
|
|
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)
|
|
|
|
{
|
|
|
|
AttitudeActualInitialize();
|
2012-11-08 11:30:58 +01:00
|
|
|
AirspeedActualInitialize();
|
|
|
|
AirspeedSensorInitialize();
|
2011-11-14 17:49:37 +01:00
|
|
|
AttitudeSettingsInitialize();
|
2011-12-14 09:07:33 +01:00
|
|
|
PositionActualInitialize();
|
|
|
|
VelocityActualInitialize();
|
2012-02-29 23:13:15 +01:00
|
|
|
RevoSettingsInitialize();
|
2012-03-15 00:25:36 +01:00
|
|
|
RevoCalibrationInitialize();
|
2011-11-14 17:49:37 +01:00
|
|
|
|
2011-12-14 21:08:03 +01:00
|
|
|
// Initialize this here while we aren't setting the homelocation in GPS
|
|
|
|
HomeLocationInitialize();
|
2012-03-11 20:39:31 +01:00
|
|
|
|
2011-11-14 17:49:37 +01:00
|
|
|
// Initialize quaternion
|
|
|
|
AttitudeActualData attitude;
|
|
|
|
AttitudeActualGet(&attitude);
|
|
|
|
attitude.q1 = 1;
|
|
|
|
attitude.q2 = 0;
|
|
|
|
attitude.q3 = 0;
|
|
|
|
attitude.q4 = 0;
|
|
|
|
AttitudeActualSet(&attitude);
|
2011-12-12 21:39:38 +01:00
|
|
|
|
2011-11-14 17:49:37 +01:00
|
|
|
// Cannot trust the values to init right above if BL runs
|
2011-12-12 21:39:38 +01:00
|
|
|
GyrosBiasData gyrosBias;
|
|
|
|
GyrosBiasGet(&gyrosBias);
|
|
|
|
gyrosBias.x = 0;
|
|
|
|
gyrosBias.y = 0;
|
|
|
|
gyrosBias.z = 0;
|
|
|
|
GyrosBiasSet(&gyrosBias);
|
|
|
|
|
2011-11-14 17:49:37 +01:00
|
|
|
AttitudeSettingsConnectCallback(&settingsUpdatedCb);
|
2012-02-29 16:34:11 +01:00
|
|
|
RevoSettingsConnectCallback(&settingsUpdatedCb);
|
2012-03-15 00:25:36 +01:00
|
|
|
RevoCalibrationConnectCallback(&settingsUpdatedCb);
|
2012-02-29 19:05:54 +01:00
|
|
|
HomeLocationConnectCallback(&settingsUpdatedCb);
|
|
|
|
|
2011-11-14 17:49:37 +01:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
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
|
|
|
{
|
2011-12-12 21:39:38 +01:00
|
|
|
// Create the queues for the sensors
|
|
|
|
gyroQueue = xQueueCreate(1, sizeof(UAVObjEvent));
|
2011-12-14 08:54:06 +01:00
|
|
|
accelQueue = xQueueCreate(1, sizeof(UAVObjEvent));
|
|
|
|
magQueue = xQueueCreate(1, sizeof(UAVObjEvent));
|
2012-11-08 11:30:58 +01:00
|
|
|
airspeedQueue = xQueueCreate(1, sizeof(UAVObjEvent));
|
2011-12-14 08:54:06 +01:00
|
|
|
baroQueue = xQueueCreate(1, sizeof(UAVObjEvent));
|
2011-12-14 09:07:33 +01:00
|
|
|
gpsQueue = xQueueCreate(1, sizeof(UAVObjEvent));
|
2012-04-10 02:03:21 +02:00
|
|
|
gpsVelQueue = xQueueCreate(1, sizeof(UAVObjEvent));
|
2012-02-28 07:42:18 +01:00
|
|
|
|
2011-12-12 21:39:38 +01:00
|
|
|
// Start main task
|
|
|
|
xTaskCreate(AttitudeTask, (signed char *)"Attitude", STACK_SIZE_BYTES/4, NULL, TASK_PRIORITY, &attitudeTaskHandle);
|
|
|
|
TaskMonitorAdd(TASKINFO_RUNNING_ATTITUDE, attitudeTaskHandle);
|
|
|
|
PIOS_WDG_RegisterFlag(PIOS_WDG_ATTITUDE);
|
2011-12-12 06:29:43 +01:00
|
|
|
|
2011-12-12 21:39:38 +01:00
|
|
|
GyrosConnectQueue(gyroQueue);
|
2011-12-14 08:54:06 +01:00
|
|
|
AccelsConnectQueue(accelQueue);
|
|
|
|
MagnetometerConnectQueue(magQueue);
|
2012-11-08 11:30:58 +01:00
|
|
|
AirspeedSensorConnectQueue(airspeedQueue);
|
2011-12-14 08:54:06 +01:00
|
|
|
BaroAltitudeConnectQueue(baroQueue);
|
2011-12-14 09:07:33 +01:00
|
|
|
GPSPositionConnectQueue(gpsQueue);
|
2012-04-10 02:03:21 +02:00
|
|
|
GPSVelocityConnectQueue(gpsVelQueue);
|
|
|
|
|
2011-12-12 21:39:38 +01:00
|
|
|
return 0;
|
2011-12-12 06:29:43 +01:00
|
|
|
}
|
|
|
|
|
2011-12-12 21:39:38 +01:00
|
|
|
MODULE_INITCALL(AttitudeInitialize, AttitudeStart)
|
|
|
|
|
2011-12-12 06:29:43 +01:00
|
|
|
/**
|
|
|
|
* Module thread, should not return.
|
|
|
|
*/
|
|
|
|
static void AttitudeTask(void *parameters)
|
|
|
|
{
|
2012-04-15 18:50:56 +02:00
|
|
|
bool first_run = true;
|
2012-02-29 17:10:22 +01:00
|
|
|
uint32_t last_algorithm;
|
2011-12-12 06:29:43 +01:00
|
|
|
AlarmsClear(SYSTEMALARMS_ALARM_ATTITUDE);
|
|
|
|
|
|
|
|
// Force settings update to make sure rotation loaded
|
2012-07-21 21:31:14 +02:00
|
|
|
settingsUpdatedCb(NULL);
|
2012-02-29 16:34:11 +01:00
|
|
|
|
2011-12-14 08:54:06 +01:00
|
|
|
// Wait for all the sensors be to read
|
|
|
|
vTaskDelay(100);
|
2012-02-29 16:34:11 +01:00
|
|
|
|
|
|
|
// Invalidate previous algorithm to trigger a first run
|
|
|
|
last_algorithm = 0xfffffff;
|
|
|
|
|
2011-12-12 06:29:43 +01:00
|
|
|
// Main task loop
|
|
|
|
while (1) {
|
2012-02-28 07:42:18 +01:00
|
|
|
|
2012-04-07 07:37:42 +02:00
|
|
|
int32_t ret_val = -1;
|
|
|
|
|
2012-02-29 16:34:11 +01:00
|
|
|
if (last_algorithm != revoSettings.FusionAlgorithm) {
|
|
|
|
last_algorithm = revoSettings.FusionAlgorithm;
|
|
|
|
first_run = true;
|
|
|
|
}
|
|
|
|
|
2011-12-12 06:29:43 +01:00
|
|
|
// This function blocks on data queue
|
2012-02-29 16:34:11 +01:00
|
|
|
switch (revoSettings.FusionAlgorithm ) {
|
2012-06-17 09:21:58 +02:00
|
|
|
case REVOSETTINGS_FUSIONALGORITHM_COMPLEMENTARY:
|
|
|
|
ret_val = updateAttitudeComplementary(first_run);
|
2012-02-29 16:34:11 +01:00
|
|
|
break;
|
|
|
|
case REVOSETTINGS_FUSIONALGORITHM_INSOUTDOOR:
|
2012-04-07 07:37:42 +02:00
|
|
|
ret_val = updateAttitudeINSGPS(first_run, true);
|
2012-02-29 16:34:11 +01:00
|
|
|
break;
|
|
|
|
case REVOSETTINGS_FUSIONALGORITHM_INSINDOOR:
|
2012-04-07 07:37:42 +02:00
|
|
|
ret_val = updateAttitudeINSGPS(first_run, false);
|
2012-02-29 16:34:11 +01:00
|
|
|
break;
|
|
|
|
default:
|
2012-07-21 21:00:52 +02:00
|
|
|
AlarmsSet(SYSTEMALARMS_ALARM_ATTITUDE,SYSTEMALARMS_ALARM_CRITICAL);
|
2012-02-29 16:34:11 +01:00
|
|
|
break;
|
|
|
|
}
|
2012-02-28 07:42:18 +01:00
|
|
|
|
2012-04-07 07:37:42 +02:00
|
|
|
if(ret_val == 0)
|
|
|
|
first_run = false;
|
2012-02-28 07:42:18 +01:00
|
|
|
|
2011-12-12 21:39:38 +01:00
|
|
|
PIOS_WDG_UpdateFlag(PIOS_WDG_ATTITUDE);
|
2011-11-14 17:49:37 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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];
|
|
|
|
float magKi = 0.000001f;
|
2012-04-23 02:01:41 +02:00
|
|
|
float magKp = 0.01f;
|
2011-12-15 07:56:50 +01:00
|
|
|
|
2012-06-17 09:21:58 +02:00
|
|
|
static int32_t updateAttitudeComplementary(bool first_run)
|
2011-11-14 17:49:37 +01:00
|
|
|
{
|
2011-12-12 21:39:38 +01:00
|
|
|
UAVObjEvent ev;
|
2011-12-12 20:28:04 +01:00
|
|
|
GyrosData gyrosData;
|
|
|
|
AccelsData accelsData;
|
2011-12-12 21:39:38 +01:00
|
|
|
static int32_t timeval;
|
|
|
|
float dT;
|
|
|
|
static uint8_t init = 0;
|
|
|
|
|
|
|
|
// Wait until the AttitudeRaw object is updated, if a timeout then go to failsafe
|
2012-04-06 20:59:12 +02:00
|
|
|
if ( xQueueReceive(gyroQueue, &ev, FAILSAFE_TIMEOUT_MS / portTICK_RATE_MS) != pdTRUE ||
|
|
|
|
xQueueReceive(accelQueue, &ev, 1 / portTICK_RATE_MS) != pdTRUE )
|
2012-02-07 15:46:34 +01:00
|
|
|
{
|
|
|
|
// When one of these is updated so should the other
|
2012-07-23 16:09:47 +02:00
|
|
|
// Do not set attitude timeout warnings in simulation mode
|
|
|
|
if (!AttitudeActualReadOnly()){
|
|
|
|
AlarmsSet(SYSTEMALARMS_ALARM_ATTITUDE,SYSTEMALARMS_ALARM_WARNING);
|
|
|
|
return -1;
|
|
|
|
}
|
2012-02-07 15:46:34 +01:00
|
|
|
}
|
2012-03-15 00:25:36 +01:00
|
|
|
|
|
|
|
AccelsGet(&accelsData);
|
|
|
|
|
2011-12-12 21:39:38 +01:00
|
|
|
// During initialization and
|
|
|
|
FlightStatusData flightStatus;
|
|
|
|
FlightStatusGet(&flightStatus);
|
2012-03-10 23:17:58 +01:00
|
|
|
if(first_run) {
|
2012-05-12 05:09:04 +02:00
|
|
|
#if defined(PIOS_INCLUDE_HMC5883)
|
2012-05-10 07:29:23 +02:00
|
|
|
// To initialize we need a valid mag reading
|
|
|
|
if ( xQueueReceive(magQueue, &ev, 0 / portTICK_RATE_MS) != pdTRUE )
|
|
|
|
return -1;
|
2012-03-10 23:17:58 +01:00
|
|
|
MagnetometerData magData;
|
|
|
|
MagnetometerGet(&magData);
|
2012-05-12 05:09:04 +02:00
|
|
|
#else
|
|
|
|
MagnetometerData magData;
|
|
|
|
magData.x = 100;
|
|
|
|
magData.y = 0;
|
|
|
|
magData.z = 0;
|
|
|
|
#endif
|
|
|
|
AttitudeActualData attitudeActual;
|
|
|
|
AttitudeActualGet(&attitudeActual);
|
2011-12-12 21:39:38 +01:00
|
|
|
init = 0;
|
2013-04-09 01:07:59 +02:00
|
|
|
attitudeActual.Roll = atan2f(-accelsData.y, -accelsData.z) * 180.0f / M_PI_F;
|
|
|
|
attitudeActual.Pitch = atan2f(accelsData.x, -accelsData.z) * 180.0f / M_PI_F;
|
|
|
|
attitudeActual.Yaw = atan2f(-magData.y, magData.x) * 180.0f / M_PI_F;
|
2012-05-10 07:29:23 +02:00
|
|
|
|
|
|
|
RPY2Quaternion(&attitudeActual.Roll,&attitudeActual.q1);
|
2012-03-10 23:17:58 +01:00
|
|
|
AttitudeActualSet(&attitudeActual);
|
|
|
|
|
2012-05-10 07:29:23 +02:00
|
|
|
timeval = PIOS_DELAY_GetRaw();
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
2012-03-10 23:17:58 +01:00
|
|
|
}
|
2011-12-12 21:39:38 +01:00
|
|
|
|
|
|
|
if((init == 0 && xTaskGetTickCount() < 7000) && (xTaskGetTickCount() > 1000)) {
|
|
|
|
// For first 7 seconds use accels to get gyro bias
|
2012-02-29 17:10:22 +01:00
|
|
|
attitudeSettings.AccelKp = 1;
|
|
|
|
attitudeSettings.AccelKi = 0.9;
|
|
|
|
attitudeSettings.YawBiasRate = 0.23;
|
2012-04-23 02:01:41 +02:00
|
|
|
magKp = 1;
|
2012-02-29 17:10:22 +01:00
|
|
|
} else if ((attitudeSettings.ZeroDuringArming == ATTITUDESETTINGS_ZERODURINGARMING_TRUE) && (flightStatus.Armed == FLIGHTSTATUS_ARMED_ARMING)) {
|
|
|
|
attitudeSettings.AccelKp = 1;
|
|
|
|
attitudeSettings.AccelKi = 0.9;
|
|
|
|
attitudeSettings.YawBiasRate = 0.23;
|
2012-04-23 02:01:41 +02:00
|
|
|
magKp = 1;
|
2011-12-12 21:39:38 +01:00
|
|
|
init = 0;
|
|
|
|
} else if (init == 0) {
|
|
|
|
// Reload settings (all the rates)
|
2012-02-29 17:10:22 +01:00
|
|
|
AttitudeSettingsGet(&attitudeSettings);
|
2012-04-23 02:01:41 +02:00
|
|
|
magKp = 0.01f;
|
2011-12-12 21:39:38 +01:00
|
|
|
init = 1;
|
2012-05-10 07:29:23 +02:00
|
|
|
}
|
2012-02-28 07:42:18 +01:00
|
|
|
|
2011-12-12 21:39:38 +01:00
|
|
|
GyrosGet(&gyrosData);
|
2011-12-12 06:29:43 +01:00
|
|
|
|
2011-12-12 21:39:38 +01:00
|
|
|
// Compute the dT using the cpu clock
|
|
|
|
dT = PIOS_DELAY_DiffuS(timeval) / 1000000.0f;
|
2011-11-27 08:22:37 +01:00
|
|
|
timeval = PIOS_DELAY_GetRaw();
|
2012-02-28 07:42:18 +01:00
|
|
|
|
2011-11-16 18:36:04 +01:00
|
|
|
float q[4];
|
2012-02-28 07:42:18 +01:00
|
|
|
|
2011-11-16 18:36:04 +01:00
|
|
|
AttitudeActualData attitudeActual;
|
|
|
|
AttitudeActualGet(&attitudeActual);
|
2011-11-14 17:49:37 +01:00
|
|
|
|
2011-11-16 18:36:04 +01:00
|
|
|
float grot[3];
|
|
|
|
float accel_err[3];
|
2012-02-28 07:42:18 +01:00
|
|
|
|
2011-11-16 18:36:04 +01:00
|
|
|
// Get the current attitude estimate
|
|
|
|
quat_copy(&attitudeActual.q1, q);
|
2012-02-28 07:42:18 +01:00
|
|
|
|
2011-11-16 18:36:04 +01:00
|
|
|
// Rotate gravity to body frame and cross with accels
|
|
|
|
grot[0] = -(2 * (q[1] * q[3] - q[0] * q[2]));
|
|
|
|
grot[1] = -(2 * (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]);
|
2011-12-12 20:28:04 +01:00
|
|
|
CrossProduct((const float *) &accelsData.x, (const float *) grot, accel_err);
|
2011-11-16 18:36:04 +01:00
|
|
|
|
|
|
|
// Account for accel magnitude
|
2011-12-12 20:28:04 +01:00
|
|
|
accel_mag = accelsData.x*accelsData.x + accelsData.y*accelsData.y + accelsData.z*accelsData.z;
|
2011-11-16 18:36:04 +01:00
|
|
|
accel_mag = sqrtf(accel_mag);
|
|
|
|
accel_err[0] /= accel_mag;
|
|
|
|
accel_err[1] /= accel_mag;
|
|
|
|
accel_err[2] /= accel_mag;
|
2012-02-28 07:42:18 +01:00
|
|
|
|
2012-02-07 15:46:34 +01:00
|
|
|
if ( xQueueReceive(magQueue, &ev, 0) != pdTRUE )
|
|
|
|
{
|
2011-12-15 07:56:50 +01:00
|
|
|
// Rotate gravity to body frame and cross with accels
|
|
|
|
float brot[3];
|
|
|
|
float Rbe[3][3];
|
|
|
|
MagnetometerData mag;
|
2012-07-21 18:43:31 +02:00
|
|
|
|
2011-12-15 07:56:50 +01:00
|
|
|
Quaternion2R(q, Rbe);
|
|
|
|
MagnetometerGet(&mag);
|
2012-07-21 18:43:31 +02:00
|
|
|
|
|
|
|
// If the mag is producing bad data don't use it (normally bad calibration)
|
|
|
|
if (mag.x == mag.x && mag.y == mag.y && mag.z == mag.z) {
|
2012-07-21 20:51:20 +02:00
|
|
|
rot_mult(Rbe, homeLocation.Be, brot);
|
2012-07-21 18:43:31 +02:00
|
|
|
|
|
|
|
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 || mag_len < 1)
|
|
|
|
mag_err[0] = mag_err[1] = mag_err[2] = 0;
|
|
|
|
else
|
|
|
|
CrossProduct((const float *) &mag.x, (const float *) brot, mag_err);
|
|
|
|
}
|
2011-12-15 07:56:50 +01:00
|
|
|
} else {
|
|
|
|
mag_err[0] = mag_err[1] = mag_err[2] = 0;
|
|
|
|
}
|
2012-02-28 07:42:18 +01:00
|
|
|
|
2011-11-16 18:36:04 +01:00
|
|
|
// Accumulate integral of error. Scale here so that units are (deg/s) but Ki has units of s
|
2011-12-12 21:39:38 +01:00
|
|
|
GyrosBiasData gyrosBias;
|
|
|
|
GyrosBiasGet(&gyrosBias);
|
2012-07-21 22:14:45 +02:00
|
|
|
gyrosBias.x -= accel_err[0] * attitudeSettings.AccelKi;
|
|
|
|
gyrosBias.y -= accel_err[1] * attitudeSettings.AccelKi;
|
|
|
|
gyrosBias.z -= mag_err[2] * magKi;
|
2011-12-12 21:39:38 +01:00
|
|
|
GyrosBiasSet(&gyrosBias);
|
|
|
|
|
2013-04-28 12:25:48 +02:00
|
|
|
if (revoCalibration.BiasCorrectedRaw != REVOCALIBRATION_BIASCORRECTEDRAW_TRUE) {
|
|
|
|
// if the raw values are not adjusted, we need to adjust here.
|
|
|
|
gyrosData.x -= gyrosBias.x;
|
|
|
|
gyrosData.y -= gyrosBias.y;
|
|
|
|
gyrosData.z -= gyrosBias.z;
|
|
|
|
}
|
|
|
|
|
2011-11-16 18:36:04 +01:00
|
|
|
// Correct rates based on error, integral component dealt with in updateSensors
|
2012-02-29 17:10:22 +01:00
|
|
|
gyrosData.x += accel_err[0] * attitudeSettings.AccelKp / dT;
|
|
|
|
gyrosData.y += accel_err[1] * attitudeSettings.AccelKp / dT;
|
|
|
|
gyrosData.z += accel_err[2] * attitudeSettings.AccelKp / dT + mag_err[2] * magKp / dT;
|
2011-12-12 06:29:43 +01:00
|
|
|
|
2011-11-16 18:36:04 +01:00
|
|
|
// Work out time derivative from INSAlgo writeup
|
|
|
|
// Also accounts for the fact that gyros are in deg/s
|
|
|
|
float qdot[4];
|
2013-04-09 01:07:59 +02:00
|
|
|
qdot[0] = (-q[1] * gyrosData.x - q[2] * gyrosData.y - q[3] * gyrosData.z) * dT * M_PI_F / 180 / 2;
|
|
|
|
qdot[1] = (q[0] * gyrosData.x - q[3] * gyrosData.y + q[2] * gyrosData.z) * dT * M_PI_F / 180 / 2;
|
|
|
|
qdot[2] = (q[3] * gyrosData.x + q[0] * gyrosData.y - q[1] * gyrosData.z) * dT * M_PI_F / 180 / 2;
|
|
|
|
qdot[3] = (-q[2] * gyrosData.x + q[1] * gyrosData.y + q[0] * gyrosData.z) * dT * M_PI_F / 180 / 2;
|
2012-02-28 07:42:18 +01:00
|
|
|
|
2011-11-16 18:36:04 +01: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];
|
2012-02-28 07:42:18 +01:00
|
|
|
|
2011-11-16 18:36:04 +01:00
|
|
|
if(q[0] < 0) {
|
|
|
|
q[0] = -q[0];
|
|
|
|
q[1] = -q[1];
|
|
|
|
q[2] = -q[2];
|
|
|
|
q[3] = -q[3];
|
2011-11-14 17:49:37 +01:00
|
|
|
}
|
2012-02-28 07:42:18 +01:00
|
|
|
|
2011-11-14 17:49:37 +01:00
|
|
|
// Renomalize
|
2011-11-16 18:36:04 +01:00
|
|
|
qmag = sqrtf(q[0]*q[0] + q[1]*q[1] + q[2]*q[2] + q[3]*q[3]);
|
2011-11-14 17:49:37 +01:00
|
|
|
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
|
2011-11-16 18:36:04 +01:00
|
|
|
if((fabs(qmag) < 1.0e-3f) || (qmag != qmag)) {
|
2011-11-14 17:49:37 +01:00
|
|
|
q[0] = 1;
|
|
|
|
q[1] = 0;
|
|
|
|
q[2] = 0;
|
|
|
|
q[3] = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
quat_copy(q, &attitudeActual.q1);
|
|
|
|
|
|
|
|
// Convert into eueler degrees (makes assumptions about RPY order)
|
|
|
|
Quaternion2RPY(&attitudeActual.q1,&attitudeActual.Roll);
|
|
|
|
|
|
|
|
AttitudeActualSet(&attitudeActual);
|
2012-02-28 07:42:18 +01:00
|
|
|
|
2012-02-07 15:46:34 +01:00
|
|
|
// Flush these queues for avoid errors
|
2012-03-03 01:37:43 +01:00
|
|
|
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
|
|
|
|
GPSPositionData gpsPosition;
|
|
|
|
GPSPositionGet(&gpsPosition);
|
|
|
|
getNED(&gpsPosition, NED);
|
2012-04-15 23:23:34 +02:00
|
|
|
|
2012-03-03 01:37:43 +01:00
|
|
|
PositionActualData positionActual;
|
|
|
|
PositionActualGet(&positionActual);
|
|
|
|
positionActual.North = NED[0];
|
|
|
|
positionActual.East = NED[1];
|
|
|
|
positionActual.Down = NED[2];
|
|
|
|
PositionActualSet(&positionActual);
|
2012-04-23 01:51:23 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if ( xQueueReceive(gpsVelQueue, &ev, 0) == pdTRUE ) {
|
|
|
|
// Transform the GPS position into NED coordinates
|
|
|
|
GPSVelocityData gpsVelocity;
|
|
|
|
GPSVelocityGet(&gpsVelocity);
|
2012-03-03 01:37:43 +01:00
|
|
|
|
|
|
|
VelocityActualData velocityActual;
|
|
|
|
VelocityActualGet(&velocityActual);
|
2012-04-23 01:51:23 +02:00
|
|
|
velocityActual.North = gpsVelocity.North;
|
|
|
|
velocityActual.East = gpsVelocity.East;
|
|
|
|
velocityActual.Down = gpsVelocity.Down;
|
2012-03-03 01:37:43 +01:00
|
|
|
VelocityActualSet(&velocityActual);
|
2012-02-07 15:46:34 +01:00
|
|
|
}
|
2012-11-08 11:30:58 +01:00
|
|
|
|
|
|
|
if ( xQueueReceive(airspeedQueue, &ev, 0) == pdTRUE ) {
|
|
|
|
// Calculate true airspeed from indicated airspeed
|
|
|
|
AirspeedSensorData airspeedSensor;
|
|
|
|
AirspeedSensorGet(&airspeedSensor);
|
|
|
|
|
|
|
|
AirspeedActualData airspeed;
|
|
|
|
AirspeedActualGet(&airspeed);
|
|
|
|
|
|
|
|
PositionActualData positionActual;
|
|
|
|
PositionActualGet(&positionActual);
|
|
|
|
|
|
|
|
if (airspeedSensor.SensorConnected==AIRSPEEDSENSOR_SENSORCONNECTED_TRUE) {
|
|
|
|
// we have airspeed available
|
|
|
|
airspeed.CalibratedAirspeed = airspeedSensor.CalibratedAirspeed;
|
|
|
|
airspeed.TrueAirspeed = airspeed.CalibratedAirspeed * IAS2TAS( homeLocation.Altitude - positionActual.Down );
|
|
|
|
AirspeedActualSet(&airspeed);
|
|
|
|
}
|
|
|
|
}
|
2012-02-07 15:46:34 +01:00
|
|
|
|
2012-04-23 01:51:23 +02:00
|
|
|
|
2011-12-12 06:29:43 +01:00
|
|
|
AlarmsClear(SYSTEMALARMS_ALARM_ATTITUDE);
|
|
|
|
|
2011-12-12 20:28:04 +01:00
|
|
|
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
|
|
|
{
|
|
|
|
UAVObjEvent ev;
|
|
|
|
GyrosData gyrosData;
|
|
|
|
AccelsData accelsData;
|
|
|
|
MagnetometerData magData;
|
2012-11-08 11:30:58 +01:00
|
|
|
AirspeedSensorData airspeedData;
|
2011-12-14 08:54:06 +01:00
|
|
|
BaroAltitudeData baroData;
|
2012-02-28 07:42:18 +01:00
|
|
|
GPSPositionData gpsData;
|
2012-04-10 02:03:21 +02:00
|
|
|
GPSVelocityData gpsVelData;
|
2012-02-28 07:42:18 +01:00
|
|
|
GyrosBiasData gyrosBias;
|
2011-12-14 08:54:06 +01:00
|
|
|
|
2012-05-10 08:12:03 +02:00
|
|
|
static bool mag_updated = false;
|
2012-02-28 07:42:18 +01:00
|
|
|
static bool baro_updated;
|
2012-11-08 11:30:58 +01:00
|
|
|
static bool airspeed_updated;
|
2012-02-28 07:42:18 +01:00
|
|
|
static bool gps_updated;
|
2012-04-10 02:03:21 +02:00
|
|
|
static bool gps_vel_updated;
|
2012-02-28 07:42:18 +01:00
|
|
|
|
2012-05-31 16:41:03 +02:00
|
|
|
static float baroOffset = 0;
|
|
|
|
|
2012-02-28 07:42:18 +01:00
|
|
|
static uint32_t ins_last_time = 0;
|
2011-12-14 08:54:06 +01:00
|
|
|
static bool inited;
|
2012-02-28 07:42:18 +01:00
|
|
|
|
|
|
|
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;
|
|
|
|
|
2011-12-14 08:54:06 +01:00
|
|
|
// Wait until the gyro and accel object is updated, if a timeout then go to failsafe
|
2012-04-06 20:59:12 +02:00
|
|
|
if ( (xQueueReceive(gyroQueue, &ev, FAILSAFE_TIMEOUT_MS / portTICK_RATE_MS) != pdTRUE) ||
|
2012-07-23 16:09:47 +02:00
|
|
|
(xQueueReceive(accelQueue, &ev, 1 / portTICK_RATE_MS) != pdTRUE) )
|
2011-12-14 08:54:06 +01:00
|
|
|
{
|
2012-07-23 16:09:47 +02:00
|
|
|
// Do not set attitude timeout warnings in simulation mode
|
|
|
|
if (!AttitudeActualReadOnly()){
|
|
|
|
AlarmsSet(SYSTEMALARMS_ALARM_ATTITUDE,SYSTEMALARMS_ALARM_WARNING);
|
|
|
|
return -1;
|
|
|
|
}
|
2011-12-14 08:54:06 +01:00
|
|
|
}
|
2012-02-28 07:42:18 +01:00
|
|
|
|
2011-12-14 08:54:06 +01:00
|
|
|
if (inited) {
|
|
|
|
mag_updated = 0;
|
|
|
|
baro_updated = 0;
|
2012-11-08 11:30:58 +01:00
|
|
|
airspeed_updated = 0;
|
2012-02-28 07:42:18 +01:00
|
|
|
gps_updated = 0;
|
2012-04-15 18:50:56 +02:00
|
|
|
gps_vel_updated = 0;
|
2011-12-14 08:54:06 +01:00
|
|
|
}
|
2012-02-28 07:42:18 +01:00
|
|
|
|
2012-05-10 08:12:03 +02:00
|
|
|
if (first_run) {
|
|
|
|
inited = false;
|
|
|
|
init_stage = 0;
|
|
|
|
|
|
|
|
mag_updated = 0;
|
|
|
|
baro_updated = 0;
|
2012-11-08 11:30:58 +01:00
|
|
|
airspeed_updated = 0;
|
2012-05-10 08:12:03 +02:00
|
|
|
gps_updated = 0;
|
|
|
|
gps_vel_updated = 0;
|
|
|
|
|
|
|
|
ins_last_time = PIOS_DELAY_GetRaw();
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
2012-08-12 23:11:04 +02:00
|
|
|
|
|
|
|
mag_updated |= (xQueueReceive(magQueue, &ev, 0 / portTICK_RATE_MS) == pdTRUE);
|
|
|
|
baro_updated |= xQueueReceive(baroQueue, &ev, 0 / portTICK_RATE_MS) == pdTRUE;
|
2012-11-08 11:30:58 +01:00
|
|
|
airspeed_updated |= xQueueReceive(airspeedQueue, &ev, 0 / portTICK_RATE_MS) == pdTRUE;
|
2012-08-12 23:11:04 +02:00
|
|
|
gps_updated |= (xQueueReceive(gpsQueue, &ev, 0 / portTICK_RATE_MS) == pdTRUE) && outdoor_mode;
|
|
|
|
gps_vel_updated |= (xQueueReceive(gpsVelQueue, &ev, 0 / portTICK_RATE_MS) == pdTRUE) && outdoor_mode;
|
|
|
|
|
2012-02-28 07:42:18 +01:00
|
|
|
// Get most recent data
|
|
|
|
GyrosGet(&gyrosData);
|
|
|
|
AccelsGet(&accelsData);
|
|
|
|
MagnetometerGet(&magData);
|
|
|
|
BaroAltitudeGet(&baroData);
|
2012-11-08 11:30:58 +01:00
|
|
|
AirspeedSensorGet(&airspeedData);
|
2012-02-28 07:42:18 +01:00
|
|
|
GPSPositionGet(&gpsData);
|
2012-04-10 02:03:21 +02:00
|
|
|
GPSVelocityGet(&gpsVelData);
|
2012-07-21 20:51:20 +02:00
|
|
|
GyrosBiasGet(&gyrosBias);
|
2012-02-28 07:42:18 +01:00
|
|
|
|
2012-07-21 18:43:31 +02:00
|
|
|
// Discard mag if it has NAN (normally from bad calibration)
|
|
|
|
mag_updated &= (magData.x == magData.x && magData.y == magData.y && magData.z == magData.z);
|
2012-08-12 23:11:04 +02:00
|
|
|
// 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)
|
|
|
|
mag_updated &= (homeLocation.Be[0] != 0 || homeLocation.Be[1] != 0 || homeLocation.Be[2]);
|
2012-02-28 07:42:18 +01:00
|
|
|
|
2012-11-08 11:30:58 +01:00
|
|
|
// Discard airspeed if sensor not connected
|
|
|
|
airspeed_updated &= ( airspeedData.SensorConnected == AIRSPEEDSENSOR_SENSORCONNECTED_TRUE );
|
|
|
|
|
2012-02-28 07:42:18 +01:00
|
|
|
// Have a minimum requirement for gps usage
|
2012-03-03 01:32:23 +01:00
|
|
|
gps_updated &= (gpsData.Satellites >= 7) && (gpsData.PDOP <= 4.0f) && (homeLocation.Set == HOMELOCATION_SET_TRUE);
|
2012-02-28 07:42:18 +01:00
|
|
|
|
|
|
|
if (!inited)
|
|
|
|
AlarmsSet(SYSTEMALARMS_ALARM_ATTITUDE,SYSTEMALARMS_ALARM_ERROR);
|
|
|
|
else if (outdoor_mode && gpsData.Satellites < 7)
|
|
|
|
AlarmsSet(SYSTEMALARMS_ALARM_ATTITUDE,SYSTEMALARMS_ALARM_ERROR);
|
|
|
|
else
|
|
|
|
AlarmsClear(SYSTEMALARMS_ALARM_ATTITUDE);
|
2012-03-01 11:52:09 +01:00
|
|
|
|
2012-02-28 07:42:18 +01:00
|
|
|
if (!inited && mag_updated && baro_updated && (gps_updated || !outdoor_mode)) {
|
2011-12-14 08:54:06 +01:00
|
|
|
// Don't initialize until all sensors are read
|
2012-02-28 07:42:18 +01:00
|
|
|
if (init_stage == 0 && !outdoor_mode) {
|
|
|
|
float Pdiag[16]={25.0f,25.0f,25.0f,5.0f,5.0f,5.0f,1e-5f,1e-5f,1e-5f,1e-5f,1e-5f,1e-5f,1e-5f,1e-4f,1e-4f,1e-4f};
|
|
|
|
float q[4];
|
|
|
|
float pos[3] = {0.0f, 0.0f, 0.0f};
|
2012-05-31 16:41:03 +02:00
|
|
|
|
|
|
|
// Initialize barometric offset to homelocation altitude
|
2012-05-31 18:21:20 +02:00
|
|
|
baroOffset = -baroData.Altitude;
|
2012-05-31 16:41:03 +02:00
|
|
|
pos[2] = -(baroData.Altitude + baroOffset);
|
2012-02-28 07:42:18 +01:00
|
|
|
|
|
|
|
// Reset the INS algorithm
|
|
|
|
INSGPSInit();
|
2012-03-01 11:52:09 +01:00
|
|
|
INSSetMagVar(revoCalibration.mag_var);
|
|
|
|
INSSetAccelVar(revoCalibration.accel_var);
|
|
|
|
INSSetGyroVar(revoCalibration.gyro_var);
|
2012-04-07 07:37:42 +02:00
|
|
|
INSSetBaroVar(revoCalibration.baro_var);
|
2012-02-28 07:42:18 +01:00
|
|
|
|
2013-04-28 12:25:48 +02:00
|
|
|
// Initialize the gyro bias
|
|
|
|
float gyro_bias[3] = {0,0,0};
|
2012-07-21 22:11:04 +02:00
|
|
|
INSSetGyroBias(gyro_bias);
|
2012-07-21 20:51:20 +02:00
|
|
|
|
2012-05-10 08:12:03 +02:00
|
|
|
xQueueReceive(magQueue, &ev, 100 / portTICK_RATE_MS);
|
|
|
|
MagnetometerGet(&magData);
|
2012-02-28 07:42:18 +01:00
|
|
|
|
2012-05-10 08:12:03 +02:00
|
|
|
// Set initial attitude
|
|
|
|
AttitudeActualData attitudeActual;
|
2013-04-09 01:07:59 +02:00
|
|
|
attitudeActual.Roll = atan2f(-accelsData.y, -accelsData.z) * 180.0f / M_PI_F;
|
|
|
|
attitudeActual.Pitch = atan2f(accelsData.x, -accelsData.z) * 180.0f / M_PI_F;
|
|
|
|
attitudeActual.Yaw = atan2f(-magData.y, magData.x) * 180.0f / M_PI_F;
|
2012-05-10 08:12:03 +02:00
|
|
|
RPY2Quaternion(&attitudeActual.Roll,&attitudeActual.q1);
|
|
|
|
AttitudeActualSet(&attitudeActual);
|
|
|
|
|
|
|
|
q[0] = attitudeActual.q1;
|
|
|
|
q[1] = attitudeActual.q2;
|
|
|
|
q[2] = attitudeActual.q3;
|
|
|
|
q[3] = attitudeActual.q4;
|
2012-02-28 07:42:18 +01:00
|
|
|
INSSetState(pos, zeros, q, zeros, zeros);
|
|
|
|
INSResetP(Pdiag);
|
|
|
|
} else if (init_stage == 0 && outdoor_mode) {
|
|
|
|
float Pdiag[16]={25.0f,25.0f,25.0f,5.0f,5.0f,5.0f,1e-5f,1e-5f,1e-5f,1e-5f,1e-5f,1e-5f,1e-5f,1e-4f,1e-4f,1e-4f};
|
2012-03-03 01:27:33 +01:00
|
|
|
float q[4];
|
2012-02-28 07:42:18 +01:00
|
|
|
float NED[3];
|
|
|
|
|
|
|
|
// Reset the INS algorithm
|
|
|
|
INSGPSInit();
|
2012-03-01 11:52:09 +01:00
|
|
|
INSSetMagVar(revoCalibration.mag_var);
|
|
|
|
INSSetAccelVar(revoCalibration.accel_var);
|
|
|
|
INSSetGyroVar(revoCalibration.gyro_var);
|
2012-04-07 07:37:42 +02:00
|
|
|
INSSetBaroVar(revoCalibration.baro_var);
|
2012-02-28 07:42:18 +01:00
|
|
|
|
2012-07-21 20:57:25 +02:00
|
|
|
INSSetMagNorth(homeLocation.Be);
|
2012-02-28 07:42:18 +01:00
|
|
|
|
2012-07-21 20:51:20 +02:00
|
|
|
// Initialize the gyro bias from the settings
|
2013-04-28 12:25:48 +02:00
|
|
|
float gyro_bias[3] = {0,0,0};
|
2012-07-21 22:11:04 +02:00
|
|
|
INSSetGyroBias(gyro_bias);
|
2012-02-28 07:42:18 +01:00
|
|
|
|
|
|
|
GPSPositionData gpsPosition;
|
|
|
|
GPSPositionGet(&gpsPosition);
|
|
|
|
|
2012-03-03 01:25:33 +01:00
|
|
|
// Transform the GPS position into NED coordinates
|
|
|
|
getNED(&gpsPosition, NED);
|
2012-05-31 16:41:03 +02:00
|
|
|
|
|
|
|
// Initialize barometric offset to cirrent GPS NED coordinate
|
|
|
|
baroOffset = -NED[2] - baroData.Altitude;
|
2012-02-28 07:42:18 +01:00
|
|
|
|
2012-05-10 08:12:03 +02:00
|
|
|
xQueueReceive(magQueue, &ev, 100 / portTICK_RATE_MS);
|
|
|
|
MagnetometerGet(&magData);
|
|
|
|
|
2012-02-28 07:42:18 +01:00
|
|
|
// Set initial attitude
|
2012-05-10 08:12:03 +02:00
|
|
|
AttitudeActualData attitudeActual;
|
2013-04-09 01:07:59 +02:00
|
|
|
attitudeActual.Roll = atan2f(-accelsData.y, -accelsData.z) * 180.0f / M_PI_F;
|
|
|
|
attitudeActual.Pitch = atan2f(accelsData.x, -accelsData.z) * 180.0f / M_PI_F;
|
|
|
|
attitudeActual.Yaw = atan2f(-magData.y, magData.x) * 180.0f / M_PI_F;
|
2012-05-10 08:12:03 +02:00
|
|
|
RPY2Quaternion(&attitudeActual.Roll,&attitudeActual.q1);
|
|
|
|
AttitudeActualSet(&attitudeActual);
|
|
|
|
|
|
|
|
q[0] = attitudeActual.q1;
|
|
|
|
q[1] = attitudeActual.q2;
|
|
|
|
q[2] = attitudeActual.q3;
|
|
|
|
q[3] = attitudeActual.q4;
|
2012-02-28 07:42:18 +01:00
|
|
|
|
|
|
|
INSSetState(NED, zeros, q, zeros, zeros);
|
|
|
|
INSResetP(Pdiag);
|
|
|
|
} else if (init_stage > 0) {
|
|
|
|
// Run prediction a bit before any corrections
|
2012-05-10 08:12:03 +02:00
|
|
|
dT = PIOS_DELAY_DiffuS(ins_last_time) / 1.0e6f;
|
|
|
|
|
2013-04-28 12:25:48 +02:00
|
|
|
// Because the sensor module remove the bias we need to add it
|
|
|
|
// back in here so that the INS algorithm can track it correctly
|
|
|
|
float gyros[3] = {gyrosData.x * M_PI_F / 180.0f, gyrosData.y * M_PI_F / 180.0f, gyrosData.z * M_PI_F / 180.0f};
|
|
|
|
if (revoCalibration.BiasCorrectedRaw == REVOCALIBRATION_BIASCORRECTEDRAW_TRUE) {
|
|
|
|
gyros[0] += gyrosBias.x * M_PI_F / 180.0f;
|
|
|
|
gyros[1] += gyrosBias.y * M_PI_F / 180.0f;
|
|
|
|
gyros[2] += gyrosBias.z * M_PI_F / 180.0f;
|
|
|
|
}
|
2012-05-10 08:12:03 +02:00
|
|
|
INSStatePrediction(gyros, &accelsData.x, dT);
|
|
|
|
|
|
|
|
AttitudeActualData attitude;
|
|
|
|
AttitudeActualGet(&attitude);
|
|
|
|
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);
|
|
|
|
AttitudeActualSet(&attitude);
|
2012-02-28 07:42:18 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
init_stage++;
|
2012-02-28 09:37:59 +01:00
|
|
|
if(init_stage > 10)
|
2012-02-28 07:42:18 +01:00
|
|
|
inited = true;
|
2011-12-14 09:07:33 +01:00
|
|
|
|
2011-12-14 08:54:06 +01:00
|
|
|
ins_last_time = PIOS_DELAY_GetRaw();
|
2012-02-28 07:42:18 +01:00
|
|
|
|
2012-04-07 07:37:42 +02:00
|
|
|
return 0;
|
2011-12-14 08:54:06 +01:00
|
|
|
}
|
2012-02-28 07:42:18 +01:00
|
|
|
|
|
|
|
if (!inited)
|
2012-05-10 08:12:03 +02:00
|
|
|
return 0;
|
2012-02-28 07:42:18 +01:00
|
|
|
|
2011-12-14 08:54:06 +01:00
|
|
|
dT = PIOS_DELAY_DiffuS(ins_last_time) / 1.0e6f;
|
|
|
|
ins_last_time = PIOS_DELAY_GetRaw();
|
2012-02-28 07:42:18 +01:00
|
|
|
|
2011-12-14 08:54:06 +01:00
|
|
|
// 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;
|
2011-12-14 09:07:33 +01:00
|
|
|
|
2012-02-28 07:42:18 +01:00
|
|
|
// Because the sensor module remove the bias we need to add it
|
2012-07-21 20:51:20 +02:00
|
|
|
// back in here so that the INS algorithm can track it correctly
|
2013-04-09 01:07:59 +02:00
|
|
|
float gyros[3] = {gyrosData.x * M_PI_F / 180.0f, gyrosData.y * M_PI_F / 180.0f, gyrosData.z * M_PI_F / 180.0f};
|
2012-07-21 22:44:23 +02:00
|
|
|
if (revoCalibration.BiasCorrectedRaw == REVOCALIBRATION_BIASCORRECTEDRAW_TRUE) {
|
2013-04-09 01:07:59 +02:00
|
|
|
gyros[0] += gyrosBias.x * M_PI_F / 180.0f;
|
|
|
|
gyros[1] += gyrosBias.y * M_PI_F / 180.0f;
|
|
|
|
gyros[2] += gyrosBias.z * M_PI_F / 180.0f;
|
2012-07-21 20:51:20 +02:00
|
|
|
}
|
2012-02-28 07:42:18 +01:00
|
|
|
|
2011-12-14 08:54:06 +01:00
|
|
|
// Advance the state estimate
|
|
|
|
INSStatePrediction(gyros, &accelsData.x, dT);
|
2012-02-28 07:42:18 +01:00
|
|
|
|
2011-12-14 09:07:33 +01:00
|
|
|
// Copy the attitude into the UAVO
|
2011-12-14 08:54:06 +01:00
|
|
|
AttitudeActualData attitude;
|
|
|
|
AttitudeActualGet(&attitude);
|
|
|
|
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);
|
|
|
|
AttitudeActualSet(&attitude);
|
2012-02-28 07:42:18 +01:00
|
|
|
|
2011-12-14 08:54:06 +01:00
|
|
|
// Advance the covariance estimate
|
|
|
|
INSCovariancePrediction(dT);
|
2012-02-28 07:42:18 +01:00
|
|
|
|
2012-02-28 09:37:59 +01:00
|
|
|
if(mag_updated)
|
|
|
|
sensors |= MAG_SENSORS;
|
2012-02-28 07:42:18 +01:00
|
|
|
|
2011-12-14 08:54:06 +01:00
|
|
|
if(baro_updated)
|
|
|
|
sensors |= BARO_SENSOR;
|
|
|
|
|
2012-07-21 20:57:25 +02:00
|
|
|
INSSetMagNorth(homeLocation.Be);
|
2012-07-21 20:51:20 +02:00
|
|
|
|
2012-04-15 18:50:56 +02:00
|
|
|
if (gps_updated && outdoor_mode)
|
2011-12-14 09:07:33 +01:00
|
|
|
{
|
2012-04-06 19:59:49 +02:00
|
|
|
INSSetPosVelVar(revoCalibration.gps_var[REVOCALIBRATION_GPS_VAR_POS], revoCalibration.gps_var[REVOCALIBRATION_GPS_VAR_VEL]);
|
2012-04-15 18:50:56 +02:00
|
|
|
sensors |= POS_SENSORS;
|
2012-02-28 07:42:18 +01:00
|
|
|
|
2012-04-15 18:50:56 +02:00
|
|
|
if (0) { // Old code to take horizontal velocity from GPS Position update
|
|
|
|
sensors |= HORIZ_SENSORS;
|
2013-04-09 01:07:59 +02:00
|
|
|
vel[0] = gpsData.Groundspeed * cosf(gpsData.Heading * M_PI_F / 180.0f);
|
|
|
|
vel[1] = gpsData.Groundspeed * sinf(gpsData.Heading * M_PI_F / 180.0f);
|
2012-04-10 02:03:21 +02:00
|
|
|
vel[2] = 0;
|
|
|
|
}
|
2012-02-29 19:05:54 +01:00
|
|
|
// Transform the GPS position into NED coordinates
|
2012-04-15 18:50:56 +02:00
|
|
|
getNED(&gpsData, NED);
|
2012-02-29 05:49:17 +01:00
|
|
|
|
2012-05-31 16:41:03 +02:00
|
|
|
// Track barometric altitude offset with a low pass filter
|
2012-05-31 18:21:20 +02:00
|
|
|
baroOffset = BARO_OFFSET_LOWPASS_ALPHA * baroOffset +
|
|
|
|
(1.0f - BARO_OFFSET_LOWPASS_ALPHA )
|
|
|
|
* ( -NED[2] - baroData.Altitude );
|
2012-05-31 16:41:03 +02:00
|
|
|
|
2012-02-28 07:42:18 +01:00
|
|
|
} else if (!outdoor_mode) {
|
2012-11-08 11:30:58 +01:00
|
|
|
INSSetPosVelVar(1e6f, 1e5f);
|
2012-02-28 07:42:18 +01:00
|
|
|
vel[0] = vel[1] = vel[2] = 0;
|
|
|
|
NED[0] = NED[1] = 0;
|
2012-05-31 16:41:03 +02:00
|
|
|
NED[2] = -(baroData.Altitude + baroOffset);
|
2012-02-28 09:37:59 +01:00
|
|
|
sensors |= HORIZ_SENSORS | HORIZ_POS_SENSORS;
|
2012-04-07 07:58:19 +02:00
|
|
|
sensors |= POS_SENSORS |VERT_SENSORS;
|
2011-12-14 09:07:33 +01:00
|
|
|
}
|
2012-02-28 07:42:18 +01:00
|
|
|
|
2012-04-15 18:50:56 +02:00
|
|
|
if (gps_vel_updated && outdoor_mode) {
|
|
|
|
sensors |= HORIZ_SENSORS | VERT_SENSORS;
|
|
|
|
vel[0] = gpsVelData.North;
|
|
|
|
vel[1] = gpsVelData.East;
|
|
|
|
vel[2] = gpsVelData.Down;
|
|
|
|
}
|
2012-11-08 11:30:58 +01:00
|
|
|
|
|
|
|
if (airspeed_updated) {
|
|
|
|
// we have airspeed available
|
|
|
|
AirspeedActualData airspeed;
|
|
|
|
AirspeedActualGet(&airspeed);
|
|
|
|
|
|
|
|
airspeed.CalibratedAirspeed = airspeedData.CalibratedAirspeed;
|
|
|
|
airspeed.TrueAirspeed = airspeed.CalibratedAirspeed * IAS2TAS( homeLocation.Altitude - Nav.Pos[2] );
|
|
|
|
AirspeedActualSet(&airspeed);
|
|
|
|
|
|
|
|
if ( !gps_vel_updated && !gps_updated) {
|
|
|
|
// feed airspeed into EKF, treat wind as 1e2 variance
|
|
|
|
sensors |= HORIZ_SENSORS | VERT_SENSORS;
|
|
|
|
INSSetPosVelVar(1e6f, 1e2f);
|
|
|
|
// 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,0};
|
|
|
|
rot_mult(R,vtas,vel);
|
|
|
|
}
|
|
|
|
}
|
2012-04-15 18:50:56 +02:00
|
|
|
|
2011-12-14 08:54:06 +01:00
|
|
|
/*
|
|
|
|
* TODO: Need to add a general sanity check for all the inputs to make sure their kosher
|
|
|
|
* although probably should occur within INS itself
|
|
|
|
*/
|
2012-02-28 07:42:18 +01:00
|
|
|
if (sensors)
|
2012-05-31 16:41:03 +02:00
|
|
|
INSCorrection(&magData.x, NED, vel, ( baroData.Altitude + baroOffset ), sensors);
|
2012-02-29 16:34:11 +01:00
|
|
|
|
2011-12-14 09:07:33 +01:00
|
|
|
// Copy the position and velocity into the UAVO
|
|
|
|
PositionActualData positionActual;
|
|
|
|
PositionActualGet(&positionActual);
|
|
|
|
positionActual.North = Nav.Pos[0];
|
|
|
|
positionActual.East = Nav.Pos[1];
|
|
|
|
positionActual.Down = Nav.Pos[2];
|
|
|
|
PositionActualSet(&positionActual);
|
|
|
|
|
|
|
|
VelocityActualData velocityActual;
|
|
|
|
VelocityActualGet(&velocityActual);
|
|
|
|
velocityActual.North = Nav.Vel[0];
|
|
|
|
velocityActual.East = Nav.Vel[1];
|
|
|
|
velocityActual.Down = Nav.Vel[2];
|
|
|
|
VelocityActualSet(&velocityActual);
|
2012-07-21 22:11:04 +02:00
|
|
|
|
2013-04-28 12:25:48 +02:00
|
|
|
gyrosBias.x = Nav.gyro_bias[0] * 180.0f / M_PI_F;
|
|
|
|
gyrosBias.y = Nav.gyro_bias[1] * 180.0f / M_PI_F;
|
|
|
|
gyrosBias.z = Nav.gyro_bias[2] * 180.0f / M_PI_F;
|
|
|
|
GyrosBiasSet(&gyrosBias);
|
2011-12-14 08:54:06 +01:00
|
|
|
|
2012-03-11 20:39:31 +01:00
|
|
|
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];
|
|
|
|
const float DEG2RAD = 3.141592653589793f / 180.0f;
|
|
|
|
static int32_t getNED(GPSPositionData * gpsPosition, float * NED)
|
|
|
|
{
|
2012-03-01 11:05:52 +01:00
|
|
|
float dL[3] = {(gpsPosition->Latitude - homeLocation.Latitude) / 10.0e6f * DEG2RAD,
|
|
|
|
(gpsPosition->Longitude - homeLocation.Longitude) / 10.0e6f * DEG2RAD,
|
2012-03-03 01:27:33 +01:00
|
|
|
(gpsPosition->Altitude + gpsPosition->GeoidSeparation - homeLocation.Altitude)};
|
2012-02-29 19:05:54 +01: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
|
|
|
|
|
|
|
return 0;
|
2012-02-29 19:05:54 +01:00
|
|
|
}
|
|
|
|
|
2012-07-21 20:50:16 +02:00
|
|
|
static void settingsUpdatedCb(UAVObjEvent * ev)
|
2011-12-14 08:54:06 +01:00
|
|
|
{
|
2012-07-21 21:31:14 +02:00
|
|
|
if (ev == NULL || ev->obj == RevoCalibrationHandle()) {
|
2012-07-21 20:50:16 +02:00
|
|
|
RevoCalibrationGet(&revoCalibration);
|
2012-07-21 21:31:14 +02:00
|
|
|
|
2012-07-21 20:50:16 +02:00
|
|
|
// In case INS currently running
|
|
|
|
INSSetMagVar(revoCalibration.mag_var);
|
|
|
|
INSSetAccelVar(revoCalibration.accel_var);
|
|
|
|
INSSetGyroVar(revoCalibration.gyro_var);
|
|
|
|
INSSetBaroVar(revoCalibration.baro_var);
|
2012-07-21 21:31:14 +02:00
|
|
|
}
|
|
|
|
if(ev == NULL || ev->obj == HomeLocationHandle()) {
|
2012-07-21 20:50:16 +02:00
|
|
|
HomeLocationGet(&homeLocation);
|
|
|
|
// Compute matrix to convert deltaLLA to NED
|
|
|
|
float lat, alt;
|
|
|
|
lat = homeLocation.Latitude / 10.0e6f * DEG2RAD;
|
|
|
|
alt = homeLocation.Altitude;
|
|
|
|
|
|
|
|
T[0] = alt+6.378137E6f;
|
|
|
|
T[1] = cosf(lat)*(alt+6.378137E6f);
|
|
|
|
T[2] = -1.0f;
|
2012-07-21 21:31:14 +02:00
|
|
|
}
|
|
|
|
if (ev == NULL || ev->obj == AttitudeSettingsHandle())
|
2012-07-21 20:50:16 +02:00
|
|
|
AttitudeSettingsGet(&attitudeSettings);
|
2012-07-21 21:31:14 +02:00
|
|
|
if (ev == NULL || ev->obj == RevoSettingsHandle())
|
2012-07-21 20:50:16 +02:00
|
|
|
RevoSettingsGet(&revoSettings);
|
2011-11-14 17:49:37 +01:00
|
|
|
}
|
|
|
|
/**
|
2012-02-28 07:42:18 +01:00
|
|
|
* @}
|
|
|
|
* @}
|
|
|
|
*/
|