2011-09-13 07:25:03 +02:00
|
|
|
/**
|
|
|
|
******************************************************************************
|
|
|
|
*
|
|
|
|
* @file guidance.c
|
|
|
|
* @author The OpenPilot Team, http://www.openpilot.org Copyright (C) 2010.
|
2013-05-18 14:17:26 +02:00
|
|
|
* @brief This module compared @ref PositionActuatl to @ref ActiveWaypoint
|
2011-09-13 07:25:03 +02:00
|
|
|
* and sets @ref AttitudeDesired. It only does this when the FlightMode field
|
|
|
|
* of @ref ManualControlCommand is Auto.
|
|
|
|
*
|
|
|
|
* @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 object: ActiveWaypoint
|
2013-05-18 19:36:45 +02:00
|
|
|
* Input object: PositionState
|
2011-09-13 07:25:03 +02:00
|
|
|
* Input object: ManualControlCommand
|
|
|
|
* Output object: AttitudeDesired
|
|
|
|
*
|
|
|
|
* This module will periodically update the value of the AttitudeDesired object.
|
|
|
|
*
|
|
|
|
* The module executes in its own thread in this example.
|
|
|
|
*
|
|
|
|
* 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>
|
|
|
|
|
2012-02-09 06:02:29 +01:00
|
|
|
#include <math.h>
|
2013-05-03 01:15:13 +02:00
|
|
|
#include <CoordinateConversions.h>
|
|
|
|
#include <altholdsmoothed.h>
|
2013-05-18 19:36:45 +02:00
|
|
|
#include <attitudestate.h>
|
2013-05-03 01:15:13 +02:00
|
|
|
#include <altitudeholdsettings.h>
|
2013-05-18 14:17:26 +02:00
|
|
|
#include <altitudeholddesired.h> // object that will be updated by the module
|
2013-05-18 19:36:45 +02:00
|
|
|
#include <barosensor.h>
|
|
|
|
#include <positionstate.h>
|
2013-05-03 01:15:13 +02:00
|
|
|
#include <flightstatus.h>
|
|
|
|
#include <stabilizationdesired.h>
|
2013-05-18 19:36:45 +02:00
|
|
|
#include <accelstate.h>
|
2013-05-13 23:30:46 +02:00
|
|
|
#include <taskinfo.h>
|
2013-05-03 01:15:13 +02:00
|
|
|
#include <pios_constants.h>
|
2011-09-13 07:25:03 +02:00
|
|
|
// Private constants
|
2013-05-18 14:17:26 +02:00
|
|
|
#define MAX_QUEUE_SIZE 2
|
2011-09-13 07:25:03 +02:00
|
|
|
#define STACK_SIZE_BYTES 1024
|
2013-05-18 14:17:26 +02:00
|
|
|
#define TASK_PRIORITY (tskIDLE_PRIORITY + 1)
|
2012-04-06 20:58:46 +02:00
|
|
|
#define ACCEL_DOWNSAMPLE 4
|
2013-06-10 23:48:08 +02:00
|
|
|
#define TIMEOUT_TRESHOLD 200000
|
2011-09-13 07:25:03 +02:00
|
|
|
// Private types
|
|
|
|
|
|
|
|
// Private variables
|
|
|
|
static xTaskHandle altitudeHoldTaskHandle;
|
|
|
|
static xQueueHandle queue;
|
2012-02-08 16:42:10 +01:00
|
|
|
static AltitudeHoldSettingsData altitudeHoldSettings;
|
2011-09-13 07:25:03 +02:00
|
|
|
|
|
|
|
// Private functions
|
|
|
|
static void altitudeHoldTask(void *parameters);
|
2013-05-18 14:17:26 +02:00
|
|
|
static void SettingsUpdatedCb(UAVObjEvent *ev);
|
2011-09-13 07:25:03 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Initialise the module, called on startup
|
|
|
|
* \returns 0 on success or -1 if initialisation failed
|
|
|
|
*/
|
|
|
|
int32_t AltitudeHoldStart()
|
|
|
|
{
|
2013-05-18 14:17:26 +02:00
|
|
|
// Start main task
|
|
|
|
xTaskCreate(altitudeHoldTask, (signed char *)"AltitudeHold", STACK_SIZE_BYTES / 4, NULL, TASK_PRIORITY, &altitudeHoldTaskHandle);
|
|
|
|
PIOS_TASK_MONITOR_RegisterTask(TASKINFO_RUNNING_ALTITUDEHOLD, altitudeHoldTaskHandle);
|
2011-09-13 07:25:03 +02:00
|
|
|
|
2013-05-18 14:17:26 +02:00
|
|
|
return 0;
|
2011-09-13 07:25:03 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Initialise the module, called on startup
|
|
|
|
* \returns 0 on success or -1 if initialisation failed
|
|
|
|
*/
|
|
|
|
int32_t AltitudeHoldInitialize()
|
|
|
|
{
|
2013-05-18 14:17:26 +02:00
|
|
|
AltitudeHoldSettingsInitialize();
|
|
|
|
AltitudeHoldDesiredInitialize();
|
|
|
|
AltHoldSmoothedInitialize();
|
2011-09-13 07:25:03 +02:00
|
|
|
|
2013-05-18 14:17:26 +02:00
|
|
|
// Create object queue
|
|
|
|
queue = xQueueCreate(MAX_QUEUE_SIZE, sizeof(UAVObjEvent));
|
2012-02-08 16:42:10 +01:00
|
|
|
|
2013-05-18 14:17:26 +02:00
|
|
|
AltitudeHoldSettingsConnectCallback(&SettingsUpdatedCb);
|
2012-02-08 16:42:10 +01:00
|
|
|
|
2013-05-18 14:17:26 +02:00
|
|
|
return 0;
|
2011-09-13 07:25:03 +02:00
|
|
|
}
|
2013-06-04 05:37:40 +02:00
|
|
|
MODULE_INITCALL(AltitudeHoldInitialize, AltitudeHoldStart);
|
2011-09-13 07:25:03 +02:00
|
|
|
|
2012-02-08 16:42:10 +01:00
|
|
|
float tau;
|
|
|
|
float throttleIntegral;
|
2012-02-12 07:35:19 +01:00
|
|
|
float velocity;
|
2012-02-08 16:42:10 +01:00
|
|
|
float decay;
|
2012-02-08 17:56:39 +01:00
|
|
|
float velocity_decay;
|
2012-02-08 16:42:10 +01:00
|
|
|
bool running = false;
|
2012-02-08 17:38:04 +01:00
|
|
|
float error;
|
2012-02-09 03:47:10 +01:00
|
|
|
float switchThrottle;
|
|
|
|
float smoothed_altitude;
|
2012-02-09 06:02:29 +01:00
|
|
|
float starting_altitude;
|
2012-02-12 07:35:19 +01:00
|
|
|
|
2011-09-13 07:25:03 +02:00
|
|
|
/**
|
|
|
|
* Module thread, should not return.
|
|
|
|
*/
|
2013-05-05 09:02:24 +02:00
|
|
|
static void altitudeHoldTask(__attribute__((unused)) void *parameters)
|
2011-09-13 07:25:03 +02:00
|
|
|
{
|
2013-05-18 14:17:26 +02:00
|
|
|
AltitudeHoldDesiredData altitudeHoldDesired;
|
|
|
|
StabilizationDesiredData stabilizationDesired;
|
2013-06-29 12:45:40 +02:00
|
|
|
float q[4], Rbe[3][3];
|
2013-05-18 14:17:26 +02:00
|
|
|
|
|
|
|
portTickType thisTime, lastUpdateTime;
|
|
|
|
UAVObjEvent ev;
|
|
|
|
|
|
|
|
// Force update of the settings
|
|
|
|
SettingsUpdatedCb(&ev);
|
2013-06-10 23:48:08 +02:00
|
|
|
// Failsafe handling
|
|
|
|
uint32_t lastAltitudeHoldDesiredUpdate = 0;
|
|
|
|
bool enterFailSafe = false;
|
2013-05-18 14:17:26 +02:00
|
|
|
// Listen for updates.
|
|
|
|
AltitudeHoldDesiredConnectQueue(queue);
|
2013-05-18 19:36:45 +02:00
|
|
|
BaroSensorConnectQueue(queue);
|
2013-05-18 14:17:26 +02:00
|
|
|
FlightStatusConnectQueue(queue);
|
2013-05-18 19:36:45 +02:00
|
|
|
AccelStateConnectQueue(queue);
|
2013-06-20 10:04:46 +02:00
|
|
|
bool altitudeHoldFlightMode = false;
|
2013-05-18 19:36:45 +02:00
|
|
|
BaroSensorAltitudeGet(&smoothed_altitude);
|
2013-05-18 14:17:26 +02:00
|
|
|
running = false;
|
|
|
|
enum init_state { WAITING_BARO, WAITIING_INIT, INITED } init = WAITING_BARO;
|
|
|
|
|
2013-06-20 10:04:46 +02:00
|
|
|
uint8_t flightMode;
|
|
|
|
FlightStatusFlightModeGet(&flightMode);
|
|
|
|
// initialize enable flag
|
|
|
|
altitudeHoldFlightMode = flightMode == FLIGHTSTATUS_FLIGHTMODE_ALTITUDEHOLD || flightMode == FLIGHTSTATUS_FLIGHTMODE_ALTITUDEVARIO;
|
2013-05-18 14:17:26 +02:00
|
|
|
// Main task loop
|
|
|
|
bool baro_updated = false;
|
|
|
|
while (1) {
|
2013-06-10 23:48:08 +02:00
|
|
|
enterFailSafe = PIOS_DELAY_DiffuS(lastAltitudeHoldDesiredUpdate) > TIMEOUT_TRESHOLD;
|
2013-05-18 14:17:26 +02:00
|
|
|
// Wait until the AttitudeRaw object is updated, if a timeout then go to failsafe
|
|
|
|
if (xQueueReceive(queue, &ev, 100 / portTICK_RATE_MS) != pdTRUE) {
|
|
|
|
if (!running) {
|
|
|
|
throttleIntegral = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Todo: Add alarm if it should be running
|
|
|
|
continue;
|
2013-05-18 19:36:45 +02:00
|
|
|
} else if (ev.obj == BaroSensorHandle()) {
|
2013-05-18 14:17:26 +02:00
|
|
|
baro_updated = true;
|
|
|
|
|
|
|
|
init = (init == WAITING_BARO) ? WAITIING_INIT : init;
|
|
|
|
} else if (ev.obj == FlightStatusHandle()) {
|
2013-06-20 10:04:46 +02:00
|
|
|
FlightStatusFlightModeGet(&flightMode);
|
|
|
|
altitudeHoldFlightMode = flightMode == FLIGHTSTATUS_FLIGHTMODE_ALTITUDEHOLD || flightMode == FLIGHTSTATUS_FLIGHTMODE_ALTITUDEVARIO;
|
|
|
|
if (altitudeHoldFlightMode && !running) {
|
2013-05-18 14:17:26 +02:00
|
|
|
// Copy the current throttle as a starting point for integral
|
|
|
|
StabilizationDesiredThrottleGet(&throttleIntegral);
|
|
|
|
switchThrottle = throttleIntegral;
|
|
|
|
error = 0;
|
|
|
|
velocity = 0;
|
|
|
|
running = true;
|
2013-06-20 10:04:46 +02:00
|
|
|
} else if (!altitudeHoldFlightMode) {
|
2013-05-18 14:17:26 +02:00
|
|
|
running = false;
|
2013-06-10 23:48:08 +02:00
|
|
|
lastAltitudeHoldDesiredUpdate = PIOS_DELAY_GetRaw();
|
2013-05-18 14:17:26 +02:00
|
|
|
}
|
2013-05-18 19:36:45 +02:00
|
|
|
} else if (ev.obj == AccelStateHandle()) {
|
2013-05-18 14:17:26 +02:00
|
|
|
static uint32_t timeval;
|
|
|
|
|
|
|
|
static float z[4] = { 0, 0, 0, 0 };
|
|
|
|
float z_new[4];
|
|
|
|
float P[4][4], K[4][2], x[2];
|
|
|
|
float G[4] = { 1.0e-15f, 1.0e-15f, 1.0e-3f, 1.0e-7f };
|
|
|
|
static float V[4][4] = {
|
|
|
|
{ 10.0f, 0.0f, 0.0f, 0.0f },
|
|
|
|
{ 0.0f, 100.0f, 0.0f, 0.0f },
|
|
|
|
{ 0.0f, 0.0f, 100.0f, 0.0f },
|
|
|
|
{ 0.0f, 0.0f, 0.0f, 1000.0f }
|
|
|
|
};
|
|
|
|
static uint32_t accel_downsample_count = 0;
|
|
|
|
static float accels_accum[3] = { 0.0f, 0.0f, 0.0f };
|
|
|
|
float dT;
|
|
|
|
static float S[2] = { 1.0f, 10.0f };
|
|
|
|
|
|
|
|
/* Somehow this always assigns to zero. Compiler bug? Race condition? */
|
|
|
|
S[0] = altitudeHoldSettings.PressureNoise;
|
|
|
|
S[1] = altitudeHoldSettings.AccelNoise;
|
|
|
|
G[2] = altitudeHoldSettings.AccelDrift;
|
|
|
|
|
2013-05-18 19:36:45 +02:00
|
|
|
AccelStateData accelState;
|
|
|
|
AccelStateGet(&accelState);
|
|
|
|
AttitudeStateData attitudeState;
|
|
|
|
AttitudeStateGet(&attitudeState);
|
|
|
|
BaroSensorData baro;
|
|
|
|
BaroSensorGet(&baro);
|
2013-05-18 14:17:26 +02:00
|
|
|
|
|
|
|
/* Downsample accels to stop this calculation consuming too much CPU */
|
2013-05-18 19:36:45 +02:00
|
|
|
accels_accum[0] += accelState.x;
|
|
|
|
accels_accum[1] += accelState.y;
|
|
|
|
accels_accum[2] += accelState.z;
|
2013-05-18 14:17:26 +02:00
|
|
|
accel_downsample_count++;
|
|
|
|
|
|
|
|
if (accel_downsample_count < ACCEL_DOWNSAMPLE) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
accel_downsample_count = 0;
|
2013-05-18 19:36:45 +02:00
|
|
|
accelState.x = accels_accum[0] / ACCEL_DOWNSAMPLE;
|
|
|
|
accelState.y = accels_accum[1] / ACCEL_DOWNSAMPLE;
|
|
|
|
accelState.z = accels_accum[2] / ACCEL_DOWNSAMPLE;
|
2013-05-18 14:17:26 +02:00
|
|
|
accels_accum[0] = accels_accum[1] = accels_accum[2] = 0;
|
|
|
|
|
|
|
|
thisTime = xTaskGetTickCount();
|
|
|
|
|
|
|
|
if (init == WAITIING_INIT) {
|
|
|
|
z[0] = baro.Altitude;
|
|
|
|
z[1] = 0;
|
2013-05-18 19:36:45 +02:00
|
|
|
z[2] = accelState.z;
|
2013-05-18 14:17:26 +02:00
|
|
|
z[3] = 0;
|
|
|
|
init = INITED;
|
|
|
|
} else if (init == WAITING_BARO) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
x[0] = baro.Altitude;
|
|
|
|
// rotate avg accels into earth frame and store it
|
2013-06-29 12:26:28 +02:00
|
|
|
q[0] = attitudeState.q1;
|
|
|
|
q[1] = attitudeState.q2;
|
|
|
|
q[2] = attitudeState.q3;
|
|
|
|
q[3] = attitudeState.q4;
|
|
|
|
Quaternion2R(q, Rbe);
|
|
|
|
x[1] = -(Rbe[0][2] * accelState.x + Rbe[1][2] * accelState.y + Rbe[2][2] * accelState.z + 9.81f);
|
2013-05-18 14:17:26 +02:00
|
|
|
|
|
|
|
dT = PIOS_DELAY_DiffuS(timeval) / 1.0e6f;
|
|
|
|
timeval = PIOS_DELAY_GetRaw();
|
|
|
|
|
|
|
|
P[0][0] = dT * (V[0][1] + dT * V[1][1]) + V[0][0] + G[0] + dT * V[1][0];
|
|
|
|
P[0][1] = dT * (V[0][2] + dT * V[1][2]) + V[0][1] + dT * V[1][1];
|
|
|
|
P[0][2] = V[0][2] + dT * V[1][2];
|
|
|
|
P[0][3] = V[0][3] + dT * V[1][3];
|
|
|
|
P[1][0] = dT * (V[1][1] + dT * V[2][1]) + V[1][0] + dT * V[2][0];
|
|
|
|
P[1][1] = dT * (V[1][2] + dT * V[2][2]) + V[1][1] + G[1] + dT * V[2][1];
|
|
|
|
P[1][2] = V[1][2] + dT * V[2][2];
|
|
|
|
P[1][3] = V[1][3] + dT * V[2][3];
|
|
|
|
P[2][0] = V[2][0] + dT * V[2][1];
|
|
|
|
P[2][1] = V[2][1] + dT * V[2][2];
|
|
|
|
P[2][2] = V[2][2] + G[2];
|
|
|
|
P[2][3] = V[2][3];
|
|
|
|
P[3][0] = V[3][0] + dT * V[3][1];
|
|
|
|
P[3][1] = V[3][1] + dT * V[3][2];
|
|
|
|
P[3][2] = V[3][2];
|
|
|
|
P[3][3] = V[3][3] + G[3];
|
|
|
|
|
|
|
|
if (baro_updated) {
|
|
|
|
K[0][0] = -(V[2][2] * S[0] + V[2][3] * S[0] + V[3][2] * S[0] + V[3][3] * S[0] + G[2] * S[0] + G[3] * S[0] + S[0] * S[1]) / (V[0][0] * G[2] + V[0][0] * G[3] + V[2][2] * G[0] + V[2][3] * G[0] + V[3][2] * G[0] + V[3][3] * G[0] + V[0][0] * S[1] + V[2][2] * S[0] + V[2][3] * S[0] + V[3][2] * S[0] + V[3][3] * S[0] + V[0][0] * V[2][2] - V[0][2] * V[2][0] + V[0][0] * V[2][3] + V[0][0] * V[3][2] - V[0][2] * V[3][0] - V[2][0] * V[0][3] + V[0][0] * V[3][3] - V[0][3] * V[3][0] + G[0] * G[2] + G[0] * G[3] + G[0] * S[1] + G[2] * S[0] + G[3] * S[0] + S[0] * S[1] + (dT * dT) * V[1][1] * V[2][2] - (dT * dT) * V[1][2] * V[2][1] + (dT * dT) * V[1][1] * V[2][3] + (dT * dT) * V[1][1] * V[3][2] - (dT * dT) * V[1][2] * V[3][1] - (dT * dT) * V[2][1] * V[1][3] + (dT * dT) * V[1][1] * V[3][3] - (dT * dT) * V[1][3] * V[3][1] + dT * V[0][1] * G[2] + dT * V[1][0] * G[2] + dT * V[0][1] * G[3] + dT * V[1][0] * G[3] + dT * V[0][1] * S[1] + dT * V[1][0] * S[1] + (dT * dT) * V[1][1] * G[2] + (dT * dT) * V[1][1] * G[3] + (dT * dT) * V[1][1] * S[1] + dT * V[0][1] * V[2][2] + dT * V[1][0] * V[2][2] - dT * V[0][2] * V[2][1] - dT * V[2][0] * V[1][2] + dT * V[0][1] * V[2][3] + dT * V[0][1] * V[3][2] + dT * V[1][0] * V[2][3] + dT * V[1][0] * V[3][2] - dT * V[0][2] * V[3][1] - dT * V[2][0] * V[1][3] - dT * V[0][3] * V[2][1] - dT * V[1][2] * V[3][0] + dT * V[0][1] * V[3][3] + dT * V[1][0] * V[3][3] - dT * V[0][3] * V[3][1] - dT * V[3][0] * V[1][3]) + 1.0f;
|
|
|
|
K[0][1] = ((V[0][2] + V[0][3]) * S[0] + dT * (V[1][2] + V[1][3]) * S[0]) / (V[0][0] * G[2] + V[0][0] * G[3] + V[2][2] * G[0] + V[2][3] * G[0] + V[3][2] * G[0] + V[3][3] * G[0] + V[0][0] * S[1] + V[2][2] * S[0] + V[2][3] * S[0] + V[3][2] * S[0] + V[3][3] * S[0] + V[0][0] * V[2][2] - V[0][2] * V[2][0] + V[0][0] * V[2][3] + V[0][0] * V[3][2] - V[0][2] * V[3][0] - V[2][0] * V[0][3] + V[0][0] * V[3][3] - V[0][3] * V[3][0] + G[0] * G[2] + G[0] * G[3] + G[0] * S[1] + G[2] * S[0] + G[3] * S[0] + S[0] * S[1] + (dT * dT) * V[1][1] * V[2][2] - (dT * dT) * V[1][2] * V[2][1] + (dT * dT) * V[1][1] * V[2][3] + (dT * dT) * V[1][1] * V[3][2] - (dT * dT) * V[1][2] * V[3][1] - (dT * dT) * V[2][1] * V[1][3] + (dT * dT) * V[1][1] * V[3][3] - (dT * dT) * V[1][3] * V[3][1] + dT * V[0][1] * G[2] + dT * V[1][0] * G[2] + dT * V[0][1] * G[3] + dT * V[1][0] * G[3] + dT * V[0][1] * S[1] + dT * V[1][0] * S[1] + (dT * dT) * V[1][1] * G[2] + (dT * dT) * V[1][1] * G[3] + (dT * dT) * V[1][1] * S[1] + dT * V[0][1] * V[2][2] + dT * V[1][0] * V[2][2] - dT * V[0][2] * V[2][1] - dT * V[2][0] * V[1][2] + dT * V[0][1] * V[2][3] + dT * V[0][1] * V[3][2] + dT * V[1][0] * V[2][3] + dT * V[1][0] * V[3][2] - dT * V[0][2] * V[3][1] - dT * V[2][0] * V[1][3] - dT * V[0][3] * V[2][1] - dT * V[1][2] * V[3][0] + dT * V[0][1] * V[3][3] + dT * V[1][0] * V[3][3] - dT * V[0][3] * V[3][1] - dT * V[3][0] * V[1][3]);
|
|
|
|
K[1][0] = (V[1][0] * G[2] + V[1][0] * G[3] + V[1][0] * S[1] + V[1][0] * V[2][2] - V[2][0] * V[1][2] + V[1][0] * V[2][3] + V[1][0] * V[3][2] - V[2][0] * V[1][3] - V[1][2] * V[3][0] + V[1][0] * V[3][3] - V[3][0] * V[1][3] + (dT * dT) * V[2][1] * V[3][2] - (dT * dT) * V[2][2] * V[3][1] + (dT * dT) * V[2][1] * V[3][3] - (dT * dT) * V[3][1] * V[2][3] + dT * V[1][1] * G[2] + dT * V[2][0] * G[2] + dT * V[1][1] * G[3] + dT * V[2][0] * G[3] + dT * V[1][1] * S[1] + dT * V[2][0] * S[1] + (dT * dT) * V[2][1] * G[2] + (dT * dT) * V[2][1] * G[3] + (dT * dT) * V[2][1] * S[1] + dT * V[1][1] * V[2][2] - dT * V[1][2] * V[2][1] + dT * V[1][1] * V[2][3] + dT * V[1][1] * V[3][2] + dT * V[2][0] * V[3][2] - dT * V[1][2] * V[3][1] - dT * V[2][1] * V[1][3] - dT * V[3][0] * V[2][2] + dT * V[1][1] * V[3][3] + dT * V[2][0] * V[3][3] - dT * V[3][0] * V[2][3] - dT * V[1][3] * V[3][1]) / (V[0][0] * G[2] + V[0][0] * G[3] + V[2][2] * G[0] + V[2][3] * G[0] + V[3][2] * G[0] + V[3][3] * G[0] + V[0][0] * S[1] + V[2][2] * S[0] + V[2][3] * S[0] + V[3][2] * S[0] + V[3][3] * S[0] + V[0][0] * V[2][2] - V[0][2] * V[2][0] + V[0][0] * V[2][3] + V[0][0] * V[3][2] - V[0][2] * V[3][0] - V[2][0] * V[0][3] + V[0][0] * V[3][3] - V[0][3] * V[3][0] + G[0] * G[2] + G[0] * G[3] + G[0] * S[1] + G[2] * S[0] + G[3] * S[0] + S[0] * S[1] + (dT * dT) * V[1][1] * V[2][2] - (dT * dT) * V[1][2] * V[2][1] + (dT * dT) * V[1][1] * V[2][3] + (dT * dT) * V[1][1] * V[3][2] - (dT * dT) * V[1][2] * V[3][1] - (dT * dT) * V[2][1] * V[1][3] + (dT * dT) * V[1][1] * V[3][3] - (dT * dT) * V[1][3] * V[3][1] + dT * V[0][1] * G[2] + dT * V[1][0] * G[2] + dT * V[0][1] * G[3] + dT * V[1][0] * G[3] + dT * V[0][1] * S[1] + dT * V[1][0] * S[1] + (dT * dT) * V[1][1] * G[2] + (dT * dT) * V[1][1] * G[3] + (dT * dT) * V[1][1] * S[1] + dT * V[0][1] * V[2][2] + dT * V[1][0] * V[2][2] - dT * V[0][2] * V[2][1] - dT * V[2][0] * V[1][2] + dT * V[0][1] * V[2][3] + dT * V[0][1] * V[3][2] + dT * V[1][0] * V[2][3] + dT * V[1][0] * V[3][2] - dT * V[0][2] * V[3][1] - dT * V[2][0] * V[1][3] - dT * V[0][3] * V[2][1] - dT * V[1][2] * V[3][0] + dT * V[0][1] * V[3][3] + dT * V[1][0] * V[3][3] - dT * V[0][3] * V[3][1] - dT * V[3][0] * V[1][3]);
|
|
|
|
K[1][1] = (V[1][2] * G[0] + V[1][3] * G[0] + V[1][2] * S[0] + V[1][3] * S[0] + V[0][0] * V[1][2] - V[1][0] * V[0][2] + V[0][0] * V[1][3] - V[1][0] * V[0][3] + (dT * dT) * V[0][1] * V[2][2] + (dT * dT) * V[1][0] * V[2][2] - (dT * dT) * V[0][2] * V[2][1] - (dT * dT) * V[2][0] * V[1][2] + (dT * dT) * V[0][1] * V[2][3] + (dT * dT) * V[1][0] * V[2][3] - (dT * dT) * V[2][0] * V[1][3] - (dT * dT) * V[0][3] * V[2][1] + (dT * dT * dT) * V[1][1] * V[2][2] - (dT * dT * dT) * V[1][2] * V[2][1] + (dT * dT * dT) * V[1][1] * V[2][3] - (dT * dT * dT) * V[2][1] * V[1][3] + dT * V[2][2] * G[0] + dT * V[2][3] * G[0] + dT * V[2][2] * S[0] + dT * V[2][3] * S[0] + dT * V[0][0] * V[2][2] + dT * V[0][1] * V[1][2] - dT * V[0][2] * V[1][1] - dT * V[0][2] * V[2][0] + dT * V[0][0] * V[2][3] + dT * V[0][1] * V[1][3] - dT * V[1][1] * V[0][3] - dT * V[2][0] * V[0][3]) / (V[0][0] * G[2] + V[0][0] * G[3] + V[2][2] * G[0] + V[2][3] * G[0] + V[3][2] * G[0] + V[3][3] * G[0] + V[0][0] * S[1] + V[2][2] * S[0] + V[2][3] * S[0] + V[3][2] * S[0] + V[3][3] * S[0] + V[0][0] * V[2][2] - V[0][2] * V[2][0] + V[0][0] * V[2][3] + V[0][0] * V[3][2] - V[0][2] * V[3][0] - V[2][0] * V[0][3] + V[0][0] * V[3][3] - V[0][3] * V[3][0] + G[0] * G[2] + G[0] * G[3] + G[0] * S[1] + G[2] * S[0] + G[3] * S[0] + S[0] * S[1] + (dT * dT) * V[1][1] * V[2][2] - (dT * dT) * V[1][2] * V[2][1] + (dT * dT) * V[1][1] * V[2][3] + (dT * dT) * V[1][1] * V[3][2] - (dT * dT) * V[1][2] * V[3][1] - (dT * dT) * V[2][1] * V[1][3] + (dT * dT) * V[1][1] * V[3][3] - (dT * dT) * V[1][3] * V[3][1] + dT * V[0][1] * G[2] + dT * V[1][0] * G[2] + dT * V[0][1] * G[3] + dT * V[1][0] * G[3] + dT * V[0][1] * S[1] + dT * V[1][0] * S[1] + (dT * dT) * V[1][1] * G[2] + (dT * dT) * V[1][1] * G[3] + (dT * dT) * V[1][1] * S[1] + dT * V[0][1] * V[2][2] + dT * V[1][0] * V[2][2] - dT * V[0][2] * V[2][1] - dT * V[2][0] * V[1][2] + dT * V[0][1] * V[2][3] + dT * V[0][1] * V[3][2] + dT * V[1][0] * V[2][3] + dT * V[1][0] * V[3][2] - dT * V[0][2] * V[3][1] - dT * V[2][0] * V[1][3] - dT * V[0][3] * V[2][1] - dT * V[1][2] * V[3][0] + dT * V[0][1] * V[3][3] + dT * V[1][0] * V[3][3] - dT * V[0][3] * V[3][1] - dT * V[3][0] * V[1][3]);
|
|
|
|
K[2][0] = (V[2][0] * G[3] - V[3][0] * G[2] + V[2][0] * S[1] + V[2][0] * V[3][2] - V[3][0] * V[2][2] + V[2][0] * V[3][3] - V[3][0] * V[2][3] + dT * V[2][1] * G[3] - dT * V[3][1] * G[2] + dT * V[2][1] * S[1] + dT * V[2][1] * V[3][2] - dT * V[2][2] * V[3][1] + dT * V[2][1] * V[3][3] - dT * V[3][1] * V[2][3]) / (V[0][0] * G[2] + V[0][0] * G[3] + V[2][2] * G[0] + V[2][3] * G[0] + V[3][2] * G[0] + V[3][3] * G[0] + V[0][0] * S[1] + V[2][2] * S[0] + V[2][3] * S[0] + V[3][2] * S[0] + V[3][3] * S[0] + V[0][0] * V[2][2] - V[0][2] * V[2][0] + V[0][0] * V[2][3] + V[0][0] * V[3][2] - V[0][2] * V[3][0] - V[2][0] * V[0][3] + V[0][0] * V[3][3] - V[0][3] * V[3][0] + G[0] * G[2] + G[0] * G[3] + G[0] * S[1] + G[2] * S[0] + G[3] * S[0] + S[0] * S[1] + (dT * dT) * V[1][1] * V[2][2] - (dT * dT) * V[1][2] * V[2][1] + (dT * dT) * V[1][1] * V[2][3] + (dT * dT) * V[1][1] * V[3][2] - (dT * dT) * V[1][2] * V[3][1] - (dT * dT) * V[2][1] * V[1][3] + (dT * dT) * V[1][1] * V[3][3] - (dT * dT) * V[1][3] * V[3][1] + dT * V[0][1] * G[2] + dT * V[1][0] * G[2] + dT * V[0][1] * G[3] + dT * V[1][0] * G[3] + dT * V[0][1] * S[1] + dT * V[1][0] * S[1] + (dT * dT) * V[1][1] * G[2] + (dT * dT) * V[1][1] * G[3] + (dT * dT) * V[1][1] * S[1] + dT * V[0][1] * V[2][2] + dT * V[1][0] * V[2][2] - dT * V[0][2] * V[2][1] - dT * V[2][0] * V[1][2] + dT * V[0][1] * V[2][3] + dT * V[0][1] * V[3][2] + dT * V[1][0] * V[2][3] + dT * V[1][0] * V[3][2] - dT * V[0][2] * V[3][1] - dT * V[2][0] * V[1][3] - dT * V[0][3] * V[2][1] - dT * V[1][2] * V[3][0] + dT * V[0][1] * V[3][3] + dT * V[1][0] * V[3][3] - dT * V[0][3] * V[3][1] - dT * V[3][0] * V[1][3]);
|
|
|
|
K[2][1] = (V[0][0] * G[2] + V[2][2] * G[0] + V[2][3] * G[0] + V[2][2] * S[0] + V[2][3] * S[0] + V[0][0] * V[2][2] - V[0][2] * V[2][0] + V[0][0] * V[2][3] - V[2][0] * V[0][3] + G[0] * G[2] + G[2] * S[0] + (dT * dT) * V[1][1] * V[2][2] - (dT * dT) * V[1][2] * V[2][1] + (dT * dT) * V[1][1] * V[2][3] - (dT * dT) * V[2][1] * V[1][3] + dT * V[0][1] * G[2] + dT * V[1][0] * G[2] + (dT * dT) * V[1][1] * G[2] + dT * V[0][1] * V[2][2] + dT * V[1][0] * V[2][2] - dT * V[0][2] * V[2][1] - dT * V[2][0] * V[1][2] + dT * V[0][1] * V[2][3] + dT * V[1][0] * V[2][3] - dT * V[2][0] * V[1][3] - dT * V[0][3] * V[2][1]) / (V[0][0] * G[2] + V[0][0] * G[3] + V[2][2] * G[0] + V[2][3] * G[0] + V[3][2] * G[0] + V[3][3] * G[0] + V[0][0] * S[1] + V[2][2] * S[0] + V[2][3] * S[0] + V[3][2] * S[0] + V[3][3] * S[0] + V[0][0] * V[2][2] - V[0][2] * V[2][0] + V[0][0] * V[2][3] + V[0][0] * V[3][2] - V[0][2] * V[3][0] - V[2][0] * V[0][3] + V[0][0] * V[3][3] - V[0][3] * V[3][0] + G[0] * G[2] + G[0] * G[3] + G[0] * S[1] + G[2] * S[0] + G[3] * S[0] + S[0] * S[1] + (dT * dT) * V[1][1] * V[2][2] - (dT * dT) * V[1][2] * V[2][1] + (dT * dT) * V[1][1] * V[2][3] + (dT * dT) * V[1][1] * V[3][2] - (dT * dT) * V[1][2] * V[3][1] - (dT * dT) * V[2][1] * V[1][3] + (dT * dT) * V[1][1] * V[3][3] - (dT * dT) * V[1][3] * V[3][1] + dT * V[0][1] * G[2] + dT * V[1][0] * G[2] + dT * V[0][1] * G[3] + dT * V[1][0] * G[3] + dT * V[0][1] * S[1] + dT * V[1][0] * S[1] + (dT * dT) * V[1][1] * G[2] + (dT * dT) * V[1][1] * G[3] + (dT * dT) * V[1][1] * S[1] + dT * V[0][1] * V[2][2] + dT * V[1][0] * V[2][2] - dT * V[0][2] * V[2][1] - dT * V[2][0] * V[1][2] + dT * V[0][1] * V[2][3] + dT * V[0][1] * V[3][2] + dT * V[1][0] * V[2][3] + dT * V[1][0] * V[3][2] - dT * V[0][2] * V[3][1] - dT * V[2][0] * V[1][3] - dT * V[0][3] * V[2][1] - dT * V[1][2] * V[3][0] + dT * V[0][1] * V[3][3] + dT * V[1][0] * V[3][3] - dT * V[0][3] * V[3][1] - dT * V[3][0] * V[1][3]);
|
|
|
|
K[3][0] = (-V[2][0] * G[3] + V[3][0] * G[2] + V[3][0] * S[1] - V[2][0] * V[3][2] + V[3][0] * V[2][2] - V[2][0] * V[3][3] + V[3][0] * V[2][3] - dT * V[2][1] * G[3] + dT * V[3][1] * G[2] + dT * V[3][1] * S[1] - dT * V[2][1] * V[3][2] + dT * V[2][2] * V[3][1] - dT * V[2][1] * V[3][3] + dT * V[3][1] * V[2][3]) / (V[0][0] * G[2] + V[0][0] * G[3] + V[2][2] * G[0] + V[2][3] * G[0] + V[3][2] * G[0] + V[3][3] * G[0] + V[0][0] * S[1] + V[2][2] * S[0] + V[2][3] * S[0] + V[3][2] * S[0] + V[3][3] * S[0] + V[0][0] * V[2][2] - V[0][2] * V[2][0] + V[0][0] * V[2][3] + V[0][0] * V[3][2] - V[0][2] * V[3][0] - V[2][0] * V[0][3] + V[0][0] * V[3][3] - V[0][3] * V[3][0] + G[0] * G[2] + G[0] * G[3] + G[0] * S[1] + G[2] * S[0] + G[3] * S[0] + S[0] * S[1] + (dT * dT) * V[1][1] * V[2][2] - (dT * dT) * V[1][2] * V[2][1] + (dT * dT) * V[1][1] * V[2][3] + (dT * dT) * V[1][1] * V[3][2] - (dT * dT) * V[1][2] * V[3][1] - (dT * dT) * V[2][1] * V[1][3] + (dT * dT) * V[1][1] * V[3][3] - (dT * dT) * V[1][3] * V[3][1] + dT * V[0][1] * G[2] + dT * V[1][0] * G[2] + dT * V[0][1] * G[3] + dT * V[1][0] * G[3] + dT * V[0][1] * S[1] + dT * V[1][0] * S[1] + (dT * dT) * V[1][1] * G[2] + (dT * dT) * V[1][1] * G[3] + (dT * dT) * V[1][1] * S[1] + dT * V[0][1] * V[2][2] + dT * V[1][0] * V[2][2] - dT * V[0][2] * V[2][1] - dT * V[2][0] * V[1][2] + dT * V[0][1] * V[2][3] + dT * V[0][1] * V[3][2] + dT * V[1][0] * V[2][3] + dT * V[1][0] * V[3][2] - dT * V[0][2] * V[3][1] - dT * V[2][0] * V[1][3] - dT * V[0][3] * V[2][1] - dT * V[1][2] * V[3][0] + dT * V[0][1] * V[3][3] + dT * V[1][0] * V[3][3] - dT * V[0][3] * V[3][1] - dT * V[3][0] * V[1][3]);
|
|
|
|
K[3][1] = (V[0][0] * G[3] + V[3][2] * G[0] + V[3][3] * G[0] + V[3][2] * S[0] + V[3][3] * S[0] + V[0][0] * V[3][2] - V[0][2] * V[3][0] + V[0][0] * V[3][3] - V[0][3] * V[3][0] + G[0] * G[3] + G[3] * S[0] + (dT * dT) * V[1][1] * V[3][2] - (dT * dT) * V[1][2] * V[3][1] + (dT * dT) * V[1][1] * V[3][3] - (dT * dT) * V[1][3] * V[3][1] + dT * V[0][1] * G[3] + dT * V[1][0] * G[3] + (dT * dT) * V[1][1] * G[3] + dT * V[0][1] * V[3][2] + dT * V[1][0] * V[3][2] - dT * V[0][2] * V[3][1] - dT * V[1][2] * V[3][0] + dT * V[0][1] * V[3][3] + dT * V[1][0] * V[3][3] - dT * V[0][3] * V[3][1] - dT * V[3][0] * V[1][3]) / (V[0][0] * G[2] + V[0][0] * G[3] + V[2][2] * G[0] + V[2][3] * G[0] + V[3][2] * G[0] + V[3][3] * G[0] + V[0][0] * S[1] + V[2][2] * S[0] + V[2][3] * S[0] + V[3][2] * S[0] + V[3][3] * S[0] + V[0][0] * V[2][2] - V[0][2] * V[2][0] + V[0][0] * V[2][3] + V[0][0] * V[3][2] - V[0][2] * V[3][0] - V[2][0] * V[0][3] + V[0][0] * V[3][3] - V[0][3] * V[3][0] + G[0] * G[2] + G[0] * G[3] + G[0] * S[1] + G[2] * S[0] + G[3] * S[0] + S[0] * S[1] + (dT * dT) * V[1][1] * V[2][2] - (dT * dT) * V[1][2] * V[2][1] + (dT * dT) * V[1][1] * V[2][3] + (dT * dT) * V[1][1] * V[3][2] - (dT * dT) * V[1][2] * V[3][1] - (dT * dT) * V[2][1] * V[1][3] + (dT * dT) * V[1][1] * V[3][3] - (dT * dT) * V[1][3] * V[3][1] + dT * V[0][1] * G[2] + dT * V[1][0] * G[2] + dT * V[0][1] * G[3] + dT * V[1][0] * G[3] + dT * V[0][1] * S[1] + dT * V[1][0] * S[1] + (dT * dT) * V[1][1] * G[2] + (dT * dT) * V[1][1] * G[3] + (dT * dT) * V[1][1] * S[1] + dT * V[0][1] * V[2][2] + dT * V[1][0] * V[2][2] - dT * V[0][2] * V[2][1] - dT * V[2][0] * V[1][2] + dT * V[0][1] * V[2][3] + dT * V[0][1] * V[3][2] + dT * V[1][0] * V[2][3] + dT * V[1][0] * V[3][2] - dT * V[0][2] * V[3][1] - dT * V[2][0] * V[1][3] - dT * V[0][3] * V[2][1] - dT * V[1][2] * V[3][0] + dT * V[0][1] * V[3][3] + dT * V[1][0] * V[3][3] - dT * V[0][3] * V[3][1] - dT * V[3][0] * V[1][3]);
|
|
|
|
|
|
|
|
z_new[0] = -K[0][0] * (dT * z[1] - x[0] + z[0]) + dT * z[1] - K[0][1] * (-x[1] + z[2] + z[3]) + z[0];
|
|
|
|
z_new[1] = -K[1][0] * (dT * z[1] - x[0] + z[0]) + dT * z[2] - K[1][1] * (-x[1] + z[2] + z[3]) + z[1];
|
|
|
|
z_new[2] = -K[2][0] * (dT * z[1] - x[0] + z[0]) - K[2][1] * (-x[1] + z[2] + z[3]) + z[2];
|
|
|
|
z_new[3] = -K[3][0] * (dT * z[1] - x[0] + z[0]) - K[3][1] * (-x[1] + z[2] + z[3]) + z[3];
|
|
|
|
|
|
|
|
memcpy(z, z_new, sizeof(z_new));
|
|
|
|
|
|
|
|
V[0][0] = -K[0][1] * P[2][0] - K[0][1] * P[3][0] - P[0][0] * (K[0][0] - 1.0f);
|
|
|
|
V[0][1] = -K[0][1] * P[2][1] - K[0][1] * P[3][2] - P[0][1] * (K[0][0] - 1.0f);
|
|
|
|
V[0][2] = -K[0][1] * P[2][2] - K[0][1] * P[3][2] - P[0][2] * (K[0][0] - 1.0f);
|
|
|
|
V[0][3] = -K[0][1] * P[2][3] - K[0][1] * P[3][3] - P[0][3] * (K[0][0] - 1.0f);
|
|
|
|
V[1][0] = P[1][0] - K[1][0] * P[0][0] - K[1][1] * P[2][0] - K[1][1] * P[3][0];
|
|
|
|
V[1][1] = P[1][1] - K[1][0] * P[0][1] - K[1][1] * P[2][1] - K[1][1] * P[3][2];
|
|
|
|
V[1][2] = P[1][2] - K[1][0] * P[0][2] - K[1][1] * P[2][2] - K[1][1] * P[3][2];
|
|
|
|
V[1][3] = P[1][3] - K[1][0] * P[0][3] - K[1][1] * P[2][3] - K[1][1] * P[3][3];
|
|
|
|
V[2][0] = -K[2][0] * P[0][0] - K[2][1] * P[3][0] - P[2][0] * (K[2][1] - 1.0f);
|
|
|
|
V[2][1] = -K[2][0] * P[0][1] - K[2][1] * P[3][2] - P[2][1] * (K[2][1] - 1.0f);
|
|
|
|
V[2][2] = -K[2][0] * P[0][2] - K[2][1] * P[3][2] - P[2][2] * (K[2][1] - 1.0f);
|
|
|
|
V[2][3] = -K[2][0] * P[0][3] - K[2][1] * P[3][3] - P[2][3] * (K[2][1] - 1.0f);
|
|
|
|
V[3][0] = -K[3][0] * P[0][0] - K[3][1] * P[2][0] - P[3][0] * (K[3][1] - 1.0f);
|
|
|
|
V[3][1] = -K[3][0] * P[0][1] - K[3][1] * P[2][1] - P[3][2] * (K[3][1] - 1.0f);
|
|
|
|
V[3][2] = -K[3][0] * P[0][2] - K[3][1] * P[2][2] - P[3][2] * (K[3][1] - 1.0f);
|
|
|
|
V[3][3] = -K[3][0] * P[0][3] - K[3][1] * P[2][3] - P[3][3] * (K[3][1] - 1.0f);
|
|
|
|
|
|
|
|
|
|
|
|
baro_updated = false;
|
|
|
|
} else {
|
|
|
|
K[0][0] = (V[0][2] + V[0][3] + dT * V[1][2] + dT * V[1][3]) / (V[2][2] + V[2][3] + V[3][2] + V[3][3] + G[2] + G[3] + S[1]);
|
|
|
|
K[1][0] = (V[1][2] + V[1][3] + dT * V[2][2] + dT * V[2][3]) / (V[2][2] + V[2][3] + V[3][2] + V[3][3] + G[2] + G[3] + S[1]);
|
|
|
|
K[2][0] = (V[2][2] + V[2][3] + G[2]) / (V[2][2] + V[2][3] + V[3][2] + V[3][3] + G[2] + G[3] + S[1]);
|
|
|
|
K[3][0] = (V[3][2] + V[3][3] + G[3]) / (V[2][2] + V[2][3] + V[3][2] + V[3][3] + G[2] + G[3] + S[1]);
|
|
|
|
|
|
|
|
|
|
|
|
z_new[0] = dT * z[1] - K[0][0] * (-x[1] + z[2] + z[3]) + z[0];
|
|
|
|
z_new[1] = dT * z[2] - K[1][0] * (-x[1] + z[2] + z[3]) + z[1];
|
|
|
|
z_new[2] = -K[2][0] * (-x[1] + z[2] + z[3]) + z[2];
|
|
|
|
z_new[3] = -K[3][0] * (-x[1] + z[2] + z[3]) + z[3];
|
|
|
|
|
|
|
|
memcpy(z, z_new, sizeof(z_new));
|
|
|
|
|
|
|
|
V[0][0] = P[0][0] - K[0][0] * P[2][0] - K[0][0] * P[3][0];
|
|
|
|
V[0][1] = P[0][1] - K[0][0] * P[2][1] - K[0][0] * P[3][2];
|
|
|
|
V[0][2] = P[0][2] - K[0][0] * P[2][2] - K[0][0] * P[3][2];
|
|
|
|
V[0][3] = P[0][3] - K[0][0] * P[2][3] - K[0][0] * P[3][3];
|
|
|
|
V[1][0] = P[1][0] - K[1][0] * P[2][0] - K[1][0] * P[3][0];
|
|
|
|
V[1][1] = P[1][1] - K[1][0] * P[2][1] - K[1][0] * P[3][2];
|
|
|
|
V[1][2] = P[1][2] - K[1][0] * P[2][2] - K[1][0] * P[3][2];
|
|
|
|
V[1][3] = P[1][3] - K[1][0] * P[2][3] - K[1][0] * P[3][3];
|
|
|
|
V[2][0] = -K[2][0] * P[3][0] - P[2][0] * (K[2][0] - 1.0f);
|
|
|
|
V[2][1] = -K[2][0] * P[3][2] - P[2][1] * (K[2][0] - 1.0f);
|
|
|
|
V[2][2] = -K[2][0] * P[3][2] - P[2][2] * (K[2][0] - 1.0f);
|
|
|
|
V[2][3] = -K[2][0] * P[3][3] - P[2][3] * (K[2][0] - 1.0f);
|
|
|
|
V[3][0] = -K[3][0] * P[2][0] - P[3][0] * (K[3][0] - 1.0f);
|
|
|
|
V[3][1] = -K[3][0] * P[2][1] - P[3][2] * (K[3][0] - 1.0f);
|
|
|
|
V[3][2] = -K[3][0] * P[2][2] - P[3][2] * (K[3][0] - 1.0f);
|
|
|
|
V[3][3] = -K[3][0] * P[2][3] - P[3][3] * (K[3][0] - 1.0f);
|
|
|
|
}
|
|
|
|
|
|
|
|
AltHoldSmoothedData altHold;
|
|
|
|
AltHoldSmoothedGet(&altHold);
|
|
|
|
altHold.Altitude = z[0];
|
|
|
|
altHold.Velocity = z[1];
|
|
|
|
altHold.Accel = z[2];
|
|
|
|
AltHoldSmoothedSet(&altHold);
|
|
|
|
|
|
|
|
AltHoldSmoothedGet(&altHold);
|
|
|
|
|
2013-06-20 10:04:46 +02:00
|
|
|
// Verify that we are in altitude hold mode
|
|
|
|
uint8_t armed;
|
|
|
|
FlightStatusArmedGet(&armed);
|
|
|
|
if (!altitudeHoldFlightMode || armed != FLIGHTSTATUS_ARMED_ARMED) {
|
2013-05-18 14:17:26 +02:00
|
|
|
running = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!running) {
|
2013-06-10 23:48:08 +02:00
|
|
|
lastAltitudeHoldDesiredUpdate = PIOS_DELAY_GetRaw();
|
2013-05-18 14:17:26 +02:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Compute the altitude error
|
2013-06-29 12:18:29 +02:00
|
|
|
error = altitudeHoldDesired.Altitude - altHold.Altitude;
|
2013-05-18 14:17:26 +02:00
|
|
|
|
|
|
|
// Compute integral off altitude error
|
|
|
|
throttleIntegral += error * altitudeHoldSettings.Ki * dT;
|
|
|
|
|
|
|
|
// Only update stabilizationDesired less frequently
|
|
|
|
if ((thisTime - lastUpdateTime) < 20) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
lastUpdateTime = thisTime;
|
|
|
|
|
|
|
|
// Instead of explicit limit on integral you output limit feedback
|
|
|
|
StabilizationDesiredGet(&stabilizationDesired);
|
2013-06-10 23:48:08 +02:00
|
|
|
if (!enterFailSafe) {
|
|
|
|
stabilizationDesired.Throttle = error * altitudeHoldSettings.Kp + throttleIntegral -
|
|
|
|
altHold.Velocity * altitudeHoldSettings.Kd - altHold.Accel * altitudeHoldSettings.Ka;
|
2013-06-29 12:45:40 +02:00
|
|
|
// scale up throttle to compensate for roll/pitch angle but limit this to 60 deg (cos(60) == 0.5) to prevent excessive scaling
|
|
|
|
float throttlescale = Rbe[2][2] < 0.5f ? 0.5f : Rbe[2][2];
|
|
|
|
stabilizationDesired.Throttle /= throttlescale;
|
|
|
|
|
2013-06-10 23:48:08 +02:00
|
|
|
if (stabilizationDesired.Throttle > 1) {
|
|
|
|
throttleIntegral -= (stabilizationDesired.Throttle - 1);
|
|
|
|
stabilizationDesired.Throttle = 1;
|
|
|
|
} else if (stabilizationDesired.Throttle < 0) {
|
|
|
|
throttleIntegral -= stabilizationDesired.Throttle;
|
|
|
|
stabilizationDesired.Throttle = 0;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// shutdown motors
|
|
|
|
stabilizationDesired.Throttle = -1;
|
2013-05-18 14:17:26 +02:00
|
|
|
}
|
|
|
|
stabilizationDesired.StabilizationMode[STABILIZATIONDESIRED_STABILIZATIONMODE_ROLL] = STABILIZATIONDESIRED_STABILIZATIONMODE_ATTITUDE;
|
|
|
|
stabilizationDesired.StabilizationMode[STABILIZATIONDESIRED_STABILIZATIONMODE_PITCH] = STABILIZATIONDESIRED_STABILIZATIONMODE_ATTITUDE;
|
|
|
|
stabilizationDesired.StabilizationMode[STABILIZATIONDESIRED_STABILIZATIONMODE_YAW] = STABILIZATIONDESIRED_STABILIZATIONMODE_AXISLOCK;
|
|
|
|
stabilizationDesired.Roll = altitudeHoldDesired.Roll;
|
|
|
|
stabilizationDesired.Pitch = altitudeHoldDesired.Pitch;
|
|
|
|
stabilizationDesired.Yaw = altitudeHoldDesired.Yaw;
|
2013-06-10 23:48:08 +02:00
|
|
|
|
2013-05-18 14:17:26 +02:00
|
|
|
StabilizationDesiredSet(&stabilizationDesired);
|
|
|
|
} else if (ev.obj == AltitudeHoldDesiredHandle()) {
|
2013-06-10 23:48:08 +02:00
|
|
|
// reset the failsafe timer
|
|
|
|
lastAltitudeHoldDesiredUpdate = PIOS_DELAY_GetRaw();
|
2013-05-18 14:17:26 +02:00
|
|
|
AltitudeHoldDesiredGet(&altitudeHoldDesired);
|
|
|
|
}
|
|
|
|
}
|
2011-09-13 07:25:03 +02:00
|
|
|
}
|
2012-01-03 00:19:14 +01:00
|
|
|
|
2013-05-05 09:02:24 +02:00
|
|
|
static void SettingsUpdatedCb(__attribute__((unused)) UAVObjEvent *ev)
|
2012-01-03 00:19:14 +01:00
|
|
|
{
|
2013-05-18 14:17:26 +02:00
|
|
|
AltitudeHoldSettingsGet(&altitudeHoldSettings);
|
2012-01-03 00:19:14 +01:00
|
|
|
}
|