2011-01-16 03:45:27 +01:00
|
|
|
/**
|
|
|
|
******************************************************************************
|
|
|
|
* @addtogroup OpenPilotModules OpenPilot Modules
|
|
|
|
* @{
|
2011-02-01 03:17:40 +01:00
|
|
|
* @addtogroup Attitude Copter Control Attitude Estimation
|
|
|
|
* @brief Acquires sensor data and computes attitude estimate
|
2011-01-16 03:45:27 +01:00
|
|
|
* Specifically updates the the @ref AttitudeActual "AttitudeActual" and @ref AttitudeRaw "AttitudeRaw" settings objects
|
|
|
|
* @{
|
|
|
|
*
|
2011-02-01 03:17:40 +01:00
|
|
|
* @file attitude.c
|
2011-01-16 03:45:27 +01:00
|
|
|
* @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"
|
2011-02-01 03:17:40 +01:00
|
|
|
#include "attitude.h"
|
2011-01-16 03:45:27 +01:00
|
|
|
#include "attituderaw.h"
|
2011-01-17 09:45:43 +01:00
|
|
|
#include "attitudeactual.h"
|
2011-01-24 08:51:22 +01:00
|
|
|
#include "attitudedesired.h"
|
2011-02-01 03:18:26 +01:00
|
|
|
#include "attitudesettings.h"
|
2011-01-24 08:51:22 +01:00
|
|
|
#include "manualcontrolcommand.h"
|
2011-01-17 09:45:43 +01:00
|
|
|
#include "CoordinateConversions.h"
|
2011-01-24 08:52:14 +01:00
|
|
|
#include "pios_flash_w25x.h"
|
2011-01-16 03:45:27 +01:00
|
|
|
|
|
|
|
// Private constants
|
2011-02-02 09:57:38 +01:00
|
|
|
#define STACK_SIZE_BYTES 540
|
2011-02-01 03:18:19 +01:00
|
|
|
#define TASK_PRIORITY (tskIDLE_PRIORITY+3)
|
2011-01-16 03:45:27 +01:00
|
|
|
|
2011-02-08 17:34:18 +01:00
|
|
|
#define UPDATE_RATE 2.0f
|
2011-01-17 09:45:40 +01:00
|
|
|
#define GYRO_NEUTRAL 1665
|
2011-01-17 09:45:43 +01:00
|
|
|
|
|
|
|
#define PI_MOD(x) (fmod(x + M_PI, M_PI * 2) - M_PI)
|
2011-01-16 03:45:27 +01:00
|
|
|
// Private types
|
|
|
|
|
|
|
|
// Private variables
|
|
|
|
static xTaskHandle taskHandle;
|
|
|
|
|
|
|
|
// Private functions
|
2011-02-01 03:17:40 +01:00
|
|
|
static void AttitudeTask(void *parameters);
|
2011-01-24 08:51:22 +01:00
|
|
|
|
2011-02-02 09:57:38 +01:00
|
|
|
static float gyro_correct_int[3] = {0,0,0};
|
2011-02-03 03:42:33 +01:00
|
|
|
static xQueueHandle gyro_queue;
|
2011-02-02 09:57:38 +01:00
|
|
|
|
2011-02-08 17:34:18 +01:00
|
|
|
static void updateSensors(AttitudeRawData *);
|
|
|
|
static void updateAttitude(AttitudeRawData *);
|
2011-02-08 17:34:28 +01:00
|
|
|
static void settingsUpdatedCb(UAVObjEvent * objEv);
|
|
|
|
|
|
|
|
static float accelKi = 0;
|
|
|
|
static float accelKp = 0;
|
|
|
|
static float gyroGain = 0.42;
|
2011-01-16 03:45:27 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Initialise the module, called on startup
|
|
|
|
* \returns 0 on success or -1 if initialisation failed
|
|
|
|
*/
|
2011-02-01 03:17:40 +01:00
|
|
|
int32_t AttitudeInitialize(void)
|
2011-01-16 03:45:27 +01:00
|
|
|
{
|
2011-02-02 09:57:38 +01:00
|
|
|
// Initialize quaternion
|
|
|
|
AttitudeActualData attitude;
|
|
|
|
AttitudeActualGet(&attitude);
|
|
|
|
attitude.q1 = 1;
|
|
|
|
attitude.q2 = 0;
|
|
|
|
attitude.q3 = 0;
|
|
|
|
attitude.q4 = 0;
|
|
|
|
AttitudeActualSet(&attitude);
|
|
|
|
|
2011-02-03 03:42:33 +01:00
|
|
|
// Create queue for passing gyro data, allow 2 back samples in case
|
2011-02-03 03:42:49 +01:00
|
|
|
gyro_queue = xQueueCreate(1, sizeof(float) * 4);
|
2011-02-03 03:42:33 +01:00
|
|
|
if(gyro_queue == NULL)
|
|
|
|
return -1;
|
2011-02-03 03:42:43 +01:00
|
|
|
PIOS_ADC_SetQueue(gyro_queue);
|
|
|
|
|
2011-02-08 17:34:28 +01:00
|
|
|
AttitudeSettingsConnectCallback(&settingsUpdatedCb);
|
|
|
|
|
2011-01-16 03:45:27 +01:00
|
|
|
// Start main task
|
2011-02-01 03:17:40 +01:00
|
|
|
xTaskCreate(AttitudeTask, (signed char *)"Attitude", STACK_SIZE_BYTES/4, NULL, TASK_PRIORITY, &taskHandle);
|
2011-02-01 03:17:36 +01:00
|
|
|
TaskMonitorAdd(TASKINFO_RUNNING_ATTITUDE, taskHandle);
|
2011-02-01 03:17:32 +01:00
|
|
|
PIOS_WDG_RegisterFlag(PIOS_WDG_ATTITUDE);
|
2011-01-16 03:45:27 +01:00
|
|
|
return 0;
|
|
|
|
}
|
2011-02-03 03:42:33 +01:00
|
|
|
|
2011-01-16 03:45:27 +01:00
|
|
|
/**
|
|
|
|
* Module thread, should not return.
|
|
|
|
*/
|
2011-02-01 03:17:40 +01:00
|
|
|
static void AttitudeTask(void *parameters)
|
2011-01-16 03:45:27 +01:00
|
|
|
{
|
|
|
|
|
2011-02-08 17:34:28 +01:00
|
|
|
uint8_t init = 0;
|
2011-02-01 03:17:36 +01:00
|
|
|
AlarmsClear(SYSTEMALARMS_ALARM_ATTITUDE);
|
2011-01-16 03:45:27 +01:00
|
|
|
|
2011-02-08 17:34:18 +01:00
|
|
|
PIOS_ADC_Config((PIOS_ADC_RATE / 1000.0f) * UPDATE_RATE);
|
2011-02-01 03:17:27 +01:00
|
|
|
|
2011-01-17 09:45:34 +01:00
|
|
|
// Keep flash CS pin high while talking accel
|
2011-02-01 03:17:27 +01:00
|
|
|
PIOS_FLASH_DISABLE;
|
2011-01-17 09:45:40 +01:00
|
|
|
PIOS_ADXL345_Init();
|
2011-02-08 17:34:28 +01:00
|
|
|
|
2011-01-16 03:45:27 +01:00
|
|
|
// Main task loop
|
|
|
|
while (1) {
|
2011-02-08 17:34:28 +01:00
|
|
|
|
2011-02-08 17:34:31 +01:00
|
|
|
if(xTaskGetTickCount() < 10000) {
|
2011-02-08 17:34:28 +01:00
|
|
|
// For first 5 seconds use accels to get gyro bias
|
2011-02-08 17:34:35 +01:00
|
|
|
accelKp = 1;
|
2011-02-08 17:34:31 +01:00
|
|
|
// Decrease the rate of gyro learning during init
|
2011-02-08 17:34:55 +01:00
|
|
|
accelKi = .5 / (1 + xTaskGetTickCount() / 5000);
|
2011-02-08 17:34:28 +01:00
|
|
|
} else if (init == 0) {
|
|
|
|
settingsUpdatedCb(AttitudeSettingsHandle());
|
|
|
|
init = 1;
|
|
|
|
}
|
|
|
|
|
2011-02-01 03:17:32 +01:00
|
|
|
PIOS_WDG_UpdateFlag(PIOS_WDG_ATTITUDE);
|
2011-01-17 09:45:34 +01:00
|
|
|
|
2011-02-08 17:34:18 +01:00
|
|
|
AttitudeRawData attitudeRaw;
|
|
|
|
AttitudeRawGet(&attitudeRaw);
|
|
|
|
updateSensors(&attitudeRaw);
|
|
|
|
updateAttitude(&attitudeRaw);
|
|
|
|
AttitudeRawSet(&attitudeRaw);
|
|
|
|
|
2011-01-16 03:45:27 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-02-08 17:34:18 +01:00
|
|
|
static void updateSensors(AttitudeRawData * attitudeRaw)
|
|
|
|
{
|
2011-01-17 09:45:43 +01:00
|
|
|
struct pios_adxl345_data accel_data;
|
2011-02-03 03:42:43 +01:00
|
|
|
float gyro[4];
|
2011-02-03 03:42:33 +01:00
|
|
|
|
|
|
|
// Only wait the time for two nominal updates before setting an alarm
|
|
|
|
if(xQueueReceive(gyro_queue, (void * const) gyro, UPDATE_RATE * 2) == errQUEUE_EMPTY) {
|
|
|
|
AlarmsSet(SYSTEMALARMS_ALARM_ATTITUDE, SYSTEMALARMS_ALARM_ERROR);
|
|
|
|
return;
|
|
|
|
}
|
2011-01-17 09:45:43 +01:00
|
|
|
|
2011-02-03 03:42:43 +01:00
|
|
|
|
|
|
|
// First sample is temperature
|
2011-02-08 17:34:28 +01:00
|
|
|
attitudeRaw->gyros[ATTITUDERAW_GYROS_X] = -(gyro[1] - GYRO_NEUTRAL) * gyroGain;
|
|
|
|
attitudeRaw->gyros[ATTITUDERAW_GYROS_Y] = (gyro[2] - GYRO_NEUTRAL) * gyroGain;
|
|
|
|
attitudeRaw->gyros[ATTITUDERAW_GYROS_Z] = -(gyro[3] - GYRO_NEUTRAL) * gyroGain;
|
2011-01-17 09:45:43 +01:00
|
|
|
|
2011-02-02 09:57:38 +01:00
|
|
|
// Applying integral component here so it can be seen on the gyros and correct bias
|
2011-02-08 17:34:18 +01:00
|
|
|
attitudeRaw->gyros[ATTITUDERAW_GYROS_X] += gyro_correct_int[0];
|
|
|
|
attitudeRaw->gyros[ATTITUDERAW_GYROS_Y] += gyro_correct_int[1];
|
2011-02-02 09:57:38 +01:00
|
|
|
|
|
|
|
// Because most crafts wont get enough information from gravity to zero yaw gyro
|
2011-02-08 17:34:18 +01:00
|
|
|
attitudeRaw->gyros[ATTITUDERAW_GYROS_Z] += gyro_correct_int[2];
|
|
|
|
gyro_correct_int[2] += - attitudeRaw->gyros[ATTITUDERAW_GYROS_Z] *
|
2011-02-08 17:34:35 +01:00
|
|
|
accelKi;
|
2011-01-24 08:51:56 +01:00
|
|
|
|
|
|
|
|
2011-01-24 08:52:08 +01:00
|
|
|
// Get the accel data
|
2011-02-19 10:33:03 +01:00
|
|
|
/* uint8_t i = 0;
|
2011-02-08 17:34:18 +01:00
|
|
|
attitudeRaw->accels[ATTITUDERAW_ACCELS_X] = 0;
|
|
|
|
attitudeRaw->accels[ATTITUDERAW_ACCELS_Y] = 0;
|
|
|
|
attitudeRaw->accels[ATTITUDERAW_ACCELS_Z] = 0;
|
2011-01-24 08:52:08 +01:00
|
|
|
|
|
|
|
do {
|
|
|
|
i++;
|
2011-02-08 17:34:18 +01:00
|
|
|
attitudeRaw->gyrotemp[0] = PIOS_ADXL345_Read(&accel_data);
|
2011-01-24 08:52:08 +01:00
|
|
|
|
2011-02-08 17:34:18 +01:00
|
|
|
attitudeRaw->accels[ATTITUDERAW_ACCELS_X] += (float) accel_data.x * 0.004f * 9.81;
|
|
|
|
attitudeRaw->accels[ATTITUDERAW_ACCELS_Y] += -(float) accel_data.y * 0.004f * 9.81;
|
|
|
|
attitudeRaw->accels[ATTITUDERAW_ACCELS_Z] += -(float) accel_data.z * 0.004f * 9.81;
|
|
|
|
} while ( (i < 32) && (attitudeRaw->gyrotemp[0] > 0) );
|
|
|
|
attitudeRaw->gyrotemp[1] = i;
|
2011-01-24 08:52:08 +01:00
|
|
|
|
2011-02-08 17:34:18 +01:00
|
|
|
attitudeRaw->accels[ATTITUDERAW_ACCELS_X] /= i;
|
|
|
|
attitudeRaw->accels[ATTITUDERAW_ACCELS_Y] /= i;
|
|
|
|
attitudeRaw->accels[ATTITUDERAW_ACCELS_Z] /= i;
|
2011-02-19 10:33:03 +01:00
|
|
|
*/
|
|
|
|
int32_t x = 0;
|
|
|
|
int32_t y = 0;
|
|
|
|
int32_t z = 0;
|
|
|
|
uint8_t i = 0;
|
|
|
|
uint8_t samples_remaining;
|
|
|
|
do {
|
|
|
|
i++;
|
|
|
|
samples_remaining = PIOS_ADXL345_Read(&accel_data);
|
|
|
|
x += accel_data.x;
|
|
|
|
y += -accel_data.y;
|
|
|
|
z += -accel_data.z;
|
|
|
|
} while ( (i < 32) && (samples_remaining > 0) );
|
|
|
|
attitudeRaw->gyrotemp[0] = samples_remaining;
|
|
|
|
attitudeRaw->gyrotemp[1] = i;
|
|
|
|
attitudeRaw->accels[ATTITUDERAW_ACCELS_X] = ((float)x * 0.004f * 9.81f) / i;
|
|
|
|
attitudeRaw->accels[ATTITUDERAW_ACCELS_Y] = ((float)y * 0.004f * 9.81f) / i;
|
|
|
|
attitudeRaw->accels[ATTITUDERAW_ACCELS_Z] = ((float)z * 0.004f * 9.81f) / i;
|
2011-01-17 09:45:43 +01:00
|
|
|
}
|
|
|
|
|
2011-02-08 17:34:18 +01:00
|
|
|
static void updateAttitude(AttitudeRawData * attitudeRaw)
|
2011-01-17 09:45:43 +01:00
|
|
|
{
|
|
|
|
AttitudeActualData attitudeActual;
|
|
|
|
AttitudeActualGet(&attitudeActual);
|
2011-02-08 17:34:18 +01:00
|
|
|
|
2011-01-17 09:45:43 +01:00
|
|
|
static portTickType lastSysTime = 0;
|
|
|
|
static portTickType thisSysTime;
|
|
|
|
|
2011-01-24 08:51:22 +01:00
|
|
|
static float dT = 0;
|
2011-01-17 09:45:43 +01:00
|
|
|
|
|
|
|
thisSysTime = xTaskGetTickCount();
|
|
|
|
if(thisSysTime > lastSysTime) // reuse dt in case of wraparound
|
|
|
|
dT = (thisSysTime - lastSysTime) / portTICK_RATE_MS / 1000.0f;
|
|
|
|
lastSysTime = thisSysTime;
|
|
|
|
|
2011-02-02 09:57:38 +01:00
|
|
|
// Bad practice to assume structure order, but saves memory
|
|
|
|
float * q = &attitudeActual.q1;
|
2011-02-08 17:34:31 +01:00
|
|
|
float gyro[3];
|
|
|
|
gyro[0] = attitudeRaw->gyros[0];
|
|
|
|
gyro[1] = attitudeRaw->gyros[1];
|
|
|
|
gyro[2] = attitudeRaw->gyros[2];
|
|
|
|
|
2011-02-02 09:57:38 +01:00
|
|
|
{
|
2011-02-08 17:34:18 +01:00
|
|
|
float * accels = attitudeRaw->accels;
|
2011-02-02 09:57:38 +01:00
|
|
|
float grot[3];
|
|
|
|
float accel_err[3];
|
|
|
|
|
|
|
|
// Rotate gravity to body frame and cross with accels
|
2011-02-08 17:34:31 +01:00
|
|
|
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-02-02 09:57:38 +01:00
|
|
|
CrossProduct((const float *) accels, (const float *) grot, accel_err);
|
|
|
|
|
2011-02-08 17:34:31 +01:00
|
|
|
// Account for accel magnitude
|
|
|
|
float accel_mag = sqrt(accels[0]*accels[0] + accels[1]*accels[1] + accels[2]*accels[2]);
|
|
|
|
accel_err[0] /= accel_mag;
|
|
|
|
accel_err[1] /= accel_mag;
|
|
|
|
accel_err[2] /= accel_mag;
|
|
|
|
|
2011-02-08 17:34:35 +01:00
|
|
|
// Accumulate integral of error. Scale here so that units are (rad/s) but Ki has units of s
|
|
|
|
gyro_correct_int[0] += accel_err[0] * accelKi;
|
|
|
|
gyro_correct_int[1] += accel_err[1] * accelKi;
|
2011-02-02 09:57:38 +01:00
|
|
|
//gyro_correct_int[2] += accel_err[2] * settings.AccelKI * dT;
|
|
|
|
|
|
|
|
// Correct rates based on error, integral component dealt with in updateSensors
|
2011-02-08 17:34:35 +01:00
|
|
|
gyro[0] += accel_err[0] * accelKp / dT;
|
|
|
|
gyro[1] += accel_err[1] * accelKp / dT;
|
|
|
|
gyro[2] += accel_err[2] * accelKp / dT;
|
2011-02-02 09:57:38 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
{ // scoping variables to save memory
|
|
|
|
// Work out time derivative from INSAlgo writeup
|
|
|
|
// Also accounts for the fact that gyros are in deg/s
|
|
|
|
float qdot[4];
|
|
|
|
qdot[0] = (-q[1] * gyro[0] - q[2] * gyro[1] - q[3] * gyro[2]) * dT * M_PI / 180 / 2;
|
|
|
|
qdot[1] = (q[0] * gyro[0] - q[3] * gyro[1] + q[2] * gyro[2]) * dT * M_PI / 180 / 2;
|
|
|
|
qdot[2] = (q[3] * gyro[0] + q[0] * gyro[1] - q[1] * gyro[2]) * dT * M_PI / 180 / 2;
|
|
|
|
qdot[3] = (-q[2] * gyro[0] + q[1] * gyro[1] + q[0] * gyro[2]) * dT * M_PI / 180 / 2;
|
|
|
|
|
|
|
|
// 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];
|
|
|
|
}
|
|
|
|
|
|
|
|
// Renomalize
|
|
|
|
float qmag = sqrt(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;
|
|
|
|
|
|
|
|
attitudeActual.q1 = q[0];
|
|
|
|
attitudeActual.q2 = q[1];
|
|
|
|
attitudeActual.q3 = q[2];
|
|
|
|
attitudeActual.q4 = q[3];
|
|
|
|
|
|
|
|
// Convert into eueler degrees (makes assumptions about RPY order)
|
|
|
|
Quaternion2RPY(q,&attitudeActual.Roll);
|
|
|
|
|
2011-01-17 09:45:43 +01:00
|
|
|
AttitudeActualSet(&attitudeActual);
|
2011-01-21 04:40:21 +01:00
|
|
|
}
|
2011-02-08 17:34:28 +01:00
|
|
|
|
|
|
|
static void settingsUpdatedCb(UAVObjEvent * objEv) {
|
|
|
|
AttitudeSettingsData attitudeSettings;
|
|
|
|
AttitudeSettingsGet(&attitudeSettings);
|
|
|
|
|
|
|
|
accelKp = attitudeSettings.AccelKp;
|
2011-02-08 17:34:31 +01:00
|
|
|
accelKi = attitudeSettings.AccelKi;
|
2011-02-08 17:34:28 +01:00
|
|
|
gyroGain = attitudeSettings.GyroGain;
|
|
|
|
}
|
2011-01-16 03:45:27 +01:00
|
|
|
/**
|
|
|
|
* @}
|
|
|
|
* @}
|
|
|
|
*/
|