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
|
2011-06-23 16:35:55 +02:00
|
|
|
* @brief Acquires sensor data and computes attitude estimate
|
2013-05-18 19:36:45 +02:00
|
|
|
* Specifically updates the the @ref AttitudeState "AttitudeState" and @ref AttitudeRaw "AttitudeRaw" settings objects
|
2011-01-16 03:45:27 +01:00
|
|
|
* @{
|
|
|
|
*
|
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
|
2013-05-18 19:36:45 +02:00
|
|
|
* Output objects: @ref AttitudeRaw @ref AttitudeState
|
2011-01-16 03:45:27 +01:00
|
|
|
*
|
|
|
|
* This module computes an attitude estimate from the sensor data
|
|
|
|
*
|
|
|
|
* The module executes in its own thread.
|
|
|
|
*
|
|
|
|
* UAVObjects are automatically generated by the UAVObjectGenerator from
|
|
|
|
* the object definition XML file.
|
|
|
|
*
|
|
|
|
* Modules have no API, all communication to other modules is done through UAVObjects.
|
|
|
|
* However modules may use the API exposed by shared libraries.
|
|
|
|
* See the OpenPilot wiki for more details.
|
|
|
|
* http://www.openpilot.org/OpenPilot_Application_Architecture
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
2013-05-02 23:31:14 +02:00
|
|
|
#include <openpilot.h>
|
|
|
|
|
|
|
|
#include <pios_board_info.h>
|
|
|
|
|
2011-02-01 03:17:40 +01:00
|
|
|
#include "attitude.h"
|
2013-05-18 19:36:45 +02:00
|
|
|
#include "gyrostate.h"
|
|
|
|
#include "accelstate.h"
|
|
|
|
#include "attitudestate.h"
|
2011-02-01 03:18:26 +01:00
|
|
|
#include "attitudesettings.h"
|
2013-12-29 14:08:53 +01:00
|
|
|
#include "accelgyrosettings.h"
|
2011-05-07 22:29:21 +02:00
|
|
|
#include "flightstatus.h"
|
2011-08-22 18:17:51 +02:00
|
|
|
#include "manualcontrolcommand.h"
|
2013-05-02 23:31:14 +02:00
|
|
|
#include "taskinfo.h"
|
|
|
|
|
2011-01-17 09:45:43 +01:00
|
|
|
#include "CoordinateConversions.h"
|
2013-04-18 21:03:25 +02:00
|
|
|
|
2013-05-02 23:31:14 +02:00
|
|
|
|
2011-01-16 03:45:27 +01:00
|
|
|
// Private constants
|
2011-02-02 09:57:38 +01:00
|
|
|
#define STACK_SIZE_BYTES 540
|
2013-05-18 14:17:26 +02:00
|
|
|
#define TASK_PRIORITY (tskIDLE_PRIORITY + 3)
|
2011-01-16 03:45:27 +01:00
|
|
|
|
2013-05-18 14:17:26 +02:00
|
|
|
#define SENSOR_PERIOD 4
|
|
|
|
#define UPDATE_RATE 25.0f
|
2011-01-17 09:45:43 +01:00
|
|
|
|
2014-02-04 19:48:33 +01:00
|
|
|
#define UPDATE_EXPECTED (1.0f / 666.0f)
|
|
|
|
#define UPDATE_MIN 1.0e-6f
|
|
|
|
#define UPDATE_MAX 1.0f
|
|
|
|
#define UPDATE_ALPHA 1.0e-2f
|
|
|
|
|
2011-01-16 03:45:27 +01:00
|
|
|
// Private types
|
|
|
|
|
|
|
|
// Private variables
|
|
|
|
static xTaskHandle taskHandle;
|
2014-02-04 19:48:33 +01:00
|
|
|
static PiOSDeltatimeConfig dtconfig;
|
2011-01-16 03:45:27 +01:00
|
|
|
|
|
|
|
// Private functions
|
2011-02-01 03:17:40 +01:00
|
|
|
static void AttitudeTask(void *parameters);
|
2011-01-24 08:51:22 +01:00
|
|
|
|
2013-05-18 14:17:26 +02: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
|
|
|
|
2013-05-18 19:36:45 +02:00
|
|
|
static int32_t updateSensors(AccelStateData *, GyroStateData *);
|
|
|
|
static int32_t updateSensorsCC3D(AccelStateData *accelStateData, GyroStateData *gyrosData);
|
|
|
|
static void updateAttitude(AccelStateData *, GyroStateData *);
|
2013-05-18 14:17:26 +02:00
|
|
|
static void settingsUpdatedCb(UAVObjEvent *objEv);
|
2011-02-08 17:34:28 +01:00
|
|
|
|
2013-05-18 14:17:26 +02:00
|
|
|
static float accelKi = 0;
|
|
|
|
static float accelKp = 0;
|
2012-10-31 19:05:59 +01:00
|
|
|
static float accel_alpha = 0;
|
2012-11-06 20:17:10 +01:00
|
|
|
static bool accel_filter_enabled = false;
|
|
|
|
static float accels_filtered[3];
|
|
|
|
static float grot_filtered[3];
|
2011-03-28 20:42:48 +02:00
|
|
|
static float yawBiasRate = 0;
|
2013-05-04 01:40:02 +02:00
|
|
|
static float rollPitchBiasRate = 0.0f;
|
2014-02-06 00:57:27 +01:00
|
|
|
static AccelGyroSettingsaccel_biasData accel_bias;
|
2013-05-18 14:17:26 +02:00
|
|
|
static float q[4] = { 1, 0, 0, 0 };
|
2011-04-15 08:37:16 +02:00
|
|
|
static float R[3][3];
|
2011-03-29 04:15:57 +02:00
|
|
|
static int8_t rotate = 0;
|
2011-05-07 22:29:21 +02:00
|
|
|
static bool zero_during_arming = false;
|
2013-05-18 14:17:26 +02:00
|
|
|
static bool bias_correct_gyro = true;
|
2011-01-16 03:45:27 +01:00
|
|
|
|
2013-12-29 18:45:44 +01:00
|
|
|
// static float gyros_passed[3];
|
|
|
|
|
2013-12-29 14:08:53 +01:00
|
|
|
// temp coefficient to calculate gyro bias
|
2014-02-06 00:57:27 +01:00
|
|
|
static bool apply_gyro_temp = false;
|
|
|
|
static bool apply_accel_temp = false;
|
|
|
|
static AccelGyroSettingsgyro_temp_coeffData gyro_temp_coeff;;
|
|
|
|
static AccelGyroSettingsaccel_temp_coeffData accel_temp_coeff;
|
|
|
|
static AccelGyroSettingstemp_calibrated_extentData temp_calibrated_extent;
|
2013-12-29 14:08:53 +01:00
|
|
|
|
|
|
|
// Accel and Gyro scaling (this is the product of sensor scale and adjustement in AccelGyroSettings
|
2014-02-06 00:57:27 +01:00
|
|
|
static AccelGyroSettingsgyro_scaleData gyro_scale;
|
|
|
|
static AccelGyroSettingsaccel_scaleData accel_scale;
|
2013-12-29 14:08:53 +01:00
|
|
|
|
|
|
|
|
2011-08-22 18:17:51 +02:00
|
|
|
// For running trim flights
|
2013-05-18 14:17:26 +02:00
|
|
|
static volatile bool trim_requested = false;
|
2011-08-22 18:17:51 +02:00
|
|
|
static volatile int32_t trim_accels[3];
|
|
|
|
static volatile int32_t trim_samples;
|
|
|
|
int32_t const MAX_TRIM_FLIGHT_SAMPLES = 65535;
|
|
|
|
|
2013-12-29 18:45:44 +01:00
|
|
|
#define GRAV 9.81f
|
|
|
|
#define STD_CC_ACCEL_SCALE (GRAV * 0.004f)
|
2011-08-22 18:17:51 +02:00
|
|
|
/* 0.004f is gravity / LSB */
|
2013-12-29 18:45:44 +01:00
|
|
|
#define STD_CC_ANALOG_GYRO_NEUTRAL 1665
|
|
|
|
#define STD_CC_ANALOG_GYRO_GAIN 0.42f
|
2011-08-22 18:17:51 +02:00
|
|
|
|
2013-12-29 18:45:44 +01:00
|
|
|
// Used to detect CC vs CC3D
|
2013-12-29 14:08:53 +01:00
|
|
|
static const struct pios_board_info *bdinfo = &pios_board_info_blob;
|
2013-12-29 18:45:44 +01:00
|
|
|
#define BOARDISCC3D (bdinfo->board_rev == 0x02)
|
2011-01-16 03:45:27 +01:00
|
|
|
/**
|
|
|
|
* Initialise the module, called on startup
|
|
|
|
* \returns 0 on success or -1 if initialisation failed
|
|
|
|
*/
|
2011-06-20 07:35:40 +02:00
|
|
|
int32_t AttitudeStart(void)
|
|
|
|
{
|
2013-05-18 14:17:26 +02:00
|
|
|
// Start main task
|
|
|
|
xTaskCreate(AttitudeTask, (signed char *)"Attitude", STACK_SIZE_BYTES / 4, NULL, TASK_PRIORITY, &taskHandle);
|
|
|
|
PIOS_TASK_MONITOR_RegisterTask(TASKINFO_RUNNING_ATTITUDE, taskHandle);
|
|
|
|
PIOS_WDG_RegisterFlag(PIOS_WDG_ATTITUDE);
|
|
|
|
|
|
|
|
return 0;
|
2011-06-20 07:35:40 +02:00
|
|
|
}
|
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
|
|
|
{
|
2013-05-18 19:36:45 +02:00
|
|
|
AttitudeStateInitialize();
|
2013-05-18 14:17:26 +02:00
|
|
|
AttitudeSettingsInitialize();
|
2013-12-29 14:08:53 +01:00
|
|
|
AccelGyroSettingsInitialize();
|
2013-05-18 19:36:45 +02:00
|
|
|
AccelStateInitialize();
|
|
|
|
GyroStateInitialize();
|
2013-05-18 14:17:26 +02:00
|
|
|
|
|
|
|
// Initialize quaternion
|
2013-05-18 19:36:45 +02:00
|
|
|
AttitudeStateData attitude;
|
|
|
|
AttitudeStateGet(&attitude);
|
2013-05-18 14:17:26 +02:00
|
|
|
attitude.q1 = 1;
|
|
|
|
attitude.q2 = 0;
|
|
|
|
attitude.q3 = 0;
|
|
|
|
attitude.q4 = 0;
|
2013-05-18 19:36:45 +02:00
|
|
|
AttitudeStateSet(&attitude);
|
2013-05-18 14:17:26 +02:00
|
|
|
|
|
|
|
// Cannot trust the values to init right above if BL runs
|
|
|
|
gyro_correct_int[0] = 0;
|
|
|
|
gyro_correct_int[1] = 0;
|
|
|
|
gyro_correct_int[2] = 0;
|
|
|
|
|
|
|
|
q[0] = 1;
|
|
|
|
q[1] = 0;
|
|
|
|
q[2] = 0;
|
|
|
|
q[3] = 0;
|
|
|
|
for (uint8_t i = 0; i < 3; i++) {
|
|
|
|
for (uint8_t j = 0; j < 3; j++) {
|
|
|
|
R[i][j] = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
trim_requested = false;
|
|
|
|
|
|
|
|
AttitudeSettingsConnectCallback(&settingsUpdatedCb);
|
2013-12-29 14:08:53 +01:00
|
|
|
AccelGyroSettingsConnectCallback(&settingsUpdatedCb);
|
2013-05-18 14:17:26 +02:00
|
|
|
return 0;
|
2011-01-16 03:45:27 +01:00
|
|
|
}
|
2011-02-03 03:42:33 +01:00
|
|
|
|
2013-06-04 05:37:40 +02:00
|
|
|
MODULE_INITCALL(AttitudeInitialize, AttitudeStart);
|
2011-06-20 07:35:40 +02:00
|
|
|
|
2011-01-16 03:45:27 +01:00
|
|
|
/**
|
|
|
|
* Module thread, should not return.
|
|
|
|
*/
|
2013-05-18 14:17:26 +02:00
|
|
|
|
2012-01-24 18:54:23 +01:00
|
|
|
int32_t accel_test;
|
|
|
|
int32_t gyro_test;
|
2013-05-05 09:02:24 +02:00
|
|
|
static void AttitudeTask(__attribute__((unused)) void *parameters)
|
2011-01-16 03:45:27 +01:00
|
|
|
{
|
2013-05-18 14:17:26 +02:00
|
|
|
uint8_t init = 0;
|
|
|
|
|
|
|
|
AlarmsClear(SYSTEMALARMS_ALARM_ATTITUDE);
|
|
|
|
|
|
|
|
// Set critical error and wait until the accel is producing data
|
|
|
|
while (PIOS_ADXL345_FifoElements() == 0) {
|
|
|
|
AlarmsSet(SYSTEMALARMS_ALARM_ATTITUDE, SYSTEMALARMS_ALARM_CRITICAL);
|
|
|
|
PIOS_WDG_UpdateFlag(PIOS_WDG_ATTITUDE);
|
|
|
|
}
|
|
|
|
|
2013-12-29 14:08:53 +01:00
|
|
|
bool cc3d = BOARDISCC3D;
|
2013-05-18 14:17:26 +02:00
|
|
|
|
|
|
|
if (cc3d) {
|
2012-03-26 07:22:59 +02:00
|
|
|
#if defined(PIOS_INCLUDE_MPU6000)
|
2013-05-18 14:17:26 +02:00
|
|
|
gyro_test = PIOS_MPU6000_Test();
|
2012-01-24 18:54:23 +01:00
|
|
|
#endif
|
2013-05-18 14:17:26 +02:00
|
|
|
} else {
|
2012-01-24 18:54:23 +01:00
|
|
|
#if defined(PIOS_INCLUDE_ADXL345)
|
2013-05-18 14:17:26 +02:00
|
|
|
accel_test = PIOS_ADXL345_Test();
|
2012-01-24 18:54:23 +01:00
|
|
|
#endif
|
2012-03-26 07:22:59 +02:00
|
|
|
|
|
|
|
#if defined(PIOS_INCLUDE_ADC)
|
2013-05-18 14:17:26 +02:00
|
|
|
// Create queue for passing gyro data, allow 2 back samples in case
|
|
|
|
gyro_queue = xQueueCreate(1, sizeof(float) * 4);
|
|
|
|
PIOS_Assert(gyro_queue != NULL);
|
|
|
|
PIOS_ADC_SetQueue(gyro_queue);
|
|
|
|
PIOS_ADC_Config((PIOS_ADC_RATE / 1000.0f) * UPDATE_RATE);
|
2012-03-26 07:22:59 +02:00
|
|
|
#endif
|
2013-05-18 14:17:26 +02:00
|
|
|
}
|
|
|
|
// Force settings update to make sure rotation loaded
|
|
|
|
settingsUpdatedCb(AttitudeSettingsHandle());
|
|
|
|
|
2014-02-04 19:48:33 +01:00
|
|
|
PIOS_DELTATIME_Init(&dtconfig, UPDATE_EXPECTED, UPDATE_MIN, UPDATE_MAX, UPDATE_ALPHA);
|
|
|
|
|
2013-05-18 14:17:26 +02:00
|
|
|
// Main task loop
|
|
|
|
while (1) {
|
|
|
|
FlightStatusData flightStatus;
|
|
|
|
FlightStatusGet(&flightStatus);
|
|
|
|
|
|
|
|
if ((xTaskGetTickCount() < 7000) && (xTaskGetTickCount() > 1000)) {
|
|
|
|
// Use accels to initialise attitude and calculate gyro bias
|
|
|
|
accelKp = 1.0f;
|
|
|
|
accelKi = 0.0f;
|
|
|
|
yawBiasRate = 0.01f;
|
|
|
|
rollPitchBiasRate = 0.01f;
|
|
|
|
accel_filter_enabled = false;
|
|
|
|
init = 0;
|
|
|
|
} else if (zero_during_arming && (flightStatus.Armed == FLIGHTSTATUS_ARMED_ARMING)) {
|
|
|
|
accelKp = 1.0f;
|
|
|
|
accelKi = 0.0f;
|
|
|
|
yawBiasRate = 0.01f;
|
|
|
|
rollPitchBiasRate = 0.01f;
|
|
|
|
accel_filter_enabled = false;
|
|
|
|
init = 0;
|
|
|
|
} else if (init == 0) {
|
|
|
|
// Reload settings (all the rates)
|
|
|
|
AttitudeSettingsAccelKiGet(&accelKi);
|
|
|
|
AttitudeSettingsAccelKpGet(&accelKp);
|
|
|
|
AttitudeSettingsYawBiasRateGet(&yawBiasRate);
|
|
|
|
rollPitchBiasRate = 0.0f;
|
|
|
|
if (accel_alpha > 0.0f) {
|
|
|
|
accel_filter_enabled = true;
|
|
|
|
}
|
|
|
|
init = 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
PIOS_WDG_UpdateFlag(PIOS_WDG_ATTITUDE);
|
|
|
|
|
2013-05-18 19:36:45 +02:00
|
|
|
AccelStateData accelState;
|
|
|
|
GyroStateData gyros;
|
2013-05-18 14:17:26 +02:00
|
|
|
int32_t retval = 0;
|
|
|
|
|
|
|
|
if (cc3d) {
|
2013-05-18 19:36:45 +02:00
|
|
|
retval = updateSensorsCC3D(&accelState, &gyros);
|
2013-05-18 14:17:26 +02:00
|
|
|
} else {
|
2013-05-18 19:36:45 +02:00
|
|
|
retval = updateSensors(&accelState, &gyros);
|
2013-05-18 14:17:26 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Only update attitude when sensor data is good
|
|
|
|
if (retval != 0) {
|
|
|
|
AlarmsSet(SYSTEMALARMS_ALARM_ATTITUDE, SYSTEMALARMS_ALARM_ERROR);
|
|
|
|
} else {
|
|
|
|
// Do not update attitude data in simulation mode
|
2013-05-18 19:36:45 +02:00
|
|
|
if (!AttitudeStateReadOnly()) {
|
|
|
|
updateAttitude(&accelState, &gyros);
|
2013-05-18 14:17:26 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
AlarmsClear(SYSTEMALARMS_ALARM_ATTITUDE);
|
|
|
|
}
|
|
|
|
}
|
2011-01-16 03:45:27 +01:00
|
|
|
}
|
|
|
|
|
2011-08-14 01:44:19 +02:00
|
|
|
/**
|
|
|
|
* Get an update from the sensors
|
|
|
|
* @param[in] attitudeRaw Populate the UAVO instead of saving right here
|
|
|
|
* @return 0 if successfull, -1 if not
|
|
|
|
*/
|
2013-05-18 19:36:45 +02:00
|
|
|
static int32_t updateSensors(AccelStateData *accelState, GyroStateData *gyros)
|
2011-06-23 16:35:55 +02:00
|
|
|
{
|
2013-05-18 14:17:26 +02:00
|
|
|
struct pios_adxl345_data accel_data;
|
|
|
|
float gyro[4];
|
|
|
|
|
|
|
|
// 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 -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Do not read raw sensor data in simulation mode
|
2013-05-18 19:36:45 +02:00
|
|
|
if (GyroStateReadOnly() || AccelStateReadOnly()) {
|
2013-05-18 14:17:26 +02:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
// No accel data available
|
|
|
|
if (PIOS_ADXL345_FifoElements() == 0) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
// First sample is temperature
|
2014-02-06 00:57:27 +01:00
|
|
|
gyros->x = -(gyro[1] - STD_CC_ANALOG_GYRO_NEUTRAL) * gyro_scale.X;
|
|
|
|
gyros->y = (gyro[2] - STD_CC_ANALOG_GYRO_NEUTRAL) * gyro_scale.Y;
|
|
|
|
gyros->z = -(gyro[3] - STD_CC_ANALOG_GYRO_NEUTRAL) * gyro_scale.Z;
|
2013-05-18 14:17:26 +02: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));
|
|
|
|
|
2014-02-06 00:57:27 +01:00
|
|
|
float accel[3] = { accel_scale.X * (float)x / i,
|
|
|
|
accel_scale.Y * (float)y / i,
|
|
|
|
accel_scale.Z * (float)z / i };
|
2013-05-18 14:17:26 +02:00
|
|
|
|
|
|
|
if (rotate) {
|
|
|
|
// TODO: rotate sensors too so stabilization is well behaved
|
|
|
|
float vec_out[3];
|
|
|
|
rot_mult(R, accel, vec_out);
|
2013-05-18 19:36:45 +02:00
|
|
|
accelState->x = vec_out[0];
|
|
|
|
accelState->y = vec_out[1];
|
|
|
|
accelState->z = vec_out[2];
|
2013-05-18 14:17:26 +02:00
|
|
|
rot_mult(R, &gyros->x, vec_out);
|
2013-05-18 19:36:45 +02:00
|
|
|
gyros->x = vec_out[0];
|
|
|
|
gyros->y = vec_out[1];
|
|
|
|
gyros->z = vec_out[2];
|
2013-05-18 14:17:26 +02:00
|
|
|
} else {
|
2013-05-18 19:36:45 +02:00
|
|
|
accelState->x = accel[0];
|
|
|
|
accelState->y = accel[1];
|
|
|
|
accelState->z = accel[2];
|
2013-05-18 14:17:26 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if (trim_requested) {
|
|
|
|
if (trim_samples >= MAX_TRIM_FLIGHT_SAMPLES) {
|
|
|
|
trim_requested = false;
|
|
|
|
} else {
|
|
|
|
uint8_t armed;
|
|
|
|
float throttle;
|
|
|
|
FlightStatusArmedGet(&armed);
|
|
|
|
ManualControlCommandThrottleGet(&throttle); // Until flight status indicates airborne
|
|
|
|
if ((armed == FLIGHTSTATUS_ARMED_ARMED) && (throttle > 0.0f)) {
|
|
|
|
trim_samples++;
|
|
|
|
// Store the digitally scaled version since that is what we use for bias
|
2013-05-18 19:36:45 +02:00
|
|
|
trim_accels[0] += accelState->x;
|
|
|
|
trim_accels[1] += accelState->y;
|
|
|
|
trim_accels[2] += accelState->z;
|
2013-05-18 14:17:26 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Scale accels and correct bias
|
2014-02-06 00:57:27 +01:00
|
|
|
accelState->x -= accel_bias.X;
|
|
|
|
accelState->y -= accel_bias.Y;
|
|
|
|
accelState->z -= accel_bias.Z;
|
2013-05-18 14:17:26 +02:00
|
|
|
|
|
|
|
if (bias_correct_gyro) {
|
|
|
|
// Applying integral component here so it can be seen on the gyros and correct bias
|
|
|
|
gyros->x += gyro_correct_int[0];
|
|
|
|
gyros->y += gyro_correct_int[1];
|
|
|
|
gyros->z += gyro_correct_int[2];
|
|
|
|
}
|
|
|
|
|
|
|
|
// Force the roll & pitch gyro rates to average to zero during initialisation
|
|
|
|
gyro_correct_int[0] += -gyros->x * rollPitchBiasRate;
|
|
|
|
gyro_correct_int[1] += -gyros->y * rollPitchBiasRate;
|
|
|
|
|
|
|
|
// Because most crafts wont get enough information from gravity to zero yaw gyro, we try
|
|
|
|
// and make it average zero (weakly)
|
|
|
|
gyro_correct_int[2] += -gyros->z * yawBiasRate;
|
|
|
|
|
2013-05-18 19:36:45 +02:00
|
|
|
GyroStateSet(gyros);
|
|
|
|
AccelStateSet(accelState);
|
2013-05-18 14:17:26 +02:00
|
|
|
|
|
|
|
return 0;
|
2011-01-17 09:45:43 +01:00
|
|
|
}
|
|
|
|
|
2012-01-24 06:58:31 +01:00
|
|
|
/**
|
|
|
|
* Get an update from the sensors
|
|
|
|
* @param[in] attitudeRaw Populate the UAVO instead of saving right here
|
|
|
|
* @return 0 if successfull, -1 if not
|
|
|
|
*/
|
2013-12-29 14:23:46 +01:00
|
|
|
static struct pios_mpu6000_data mpu6000_data;
|
2013-05-18 19:36:45 +02:00
|
|
|
static int32_t updateSensorsCC3D(AccelStateData *accelStateData, GyroStateData *gyrosData)
|
2012-01-24 06:58:31 +01:00
|
|
|
{
|
2013-05-18 14:17:26 +02:00
|
|
|
float accels[3], gyros[3];
|
|
|
|
|
2012-03-26 07:22:59 +02:00
|
|
|
#if defined(PIOS_INCLUDE_MPU6000)
|
2012-01-25 08:10:47 +01:00
|
|
|
|
2013-05-18 14:17:26 +02:00
|
|
|
xQueueHandle queue = PIOS_MPU6000_GetQueue();
|
|
|
|
|
|
|
|
if (xQueueReceive(queue, (void *)&mpu6000_data, SENSOR_PERIOD) == errQUEUE_EMPTY) {
|
|
|
|
return -1; // Error, no data
|
|
|
|
}
|
|
|
|
// Do not read raw sensor data in simulation mode
|
2013-05-18 19:36:45 +02:00
|
|
|
if (GyroStateReadOnly() || AccelStateReadOnly()) {
|
2013-05-18 14:17:26 +02:00
|
|
|
return 0;
|
|
|
|
}
|
2014-02-06 00:57:27 +01:00
|
|
|
gyros[0] = mpu6000_data.gyro_x * gyro_scale.X;
|
|
|
|
gyros[1] = mpu6000_data.gyro_y * gyro_scale.Y;
|
|
|
|
gyros[2] = mpu6000_data.gyro_z * gyro_scale.Z;
|
|
|
|
|
|
|
|
accels[0] = mpu6000_data.accel_x * accel_scale.X;
|
|
|
|
accels[1] = mpu6000_data.accel_y * accel_scale.Y;
|
|
|
|
accels[2] = mpu6000_data.accel_z * accel_scale.Z;
|
2013-05-18 14:17:26 +02:00
|
|
|
|
2014-02-06 00:57:27 +01:00
|
|
|
float ctemp = mpu6000_data.temperature > temp_calibrated_extent.max ? temp_calibrated_extent.max :
|
|
|
|
(mpu6000_data.temperature < temp_calibrated_extent.min ? temp_calibrated_extent.min
|
|
|
|
: mpu6000_data.temperature);
|
2013-05-18 14:17:26 +02:00
|
|
|
|
|
|
|
|
2013-12-29 18:45:44 +01:00
|
|
|
if (apply_gyro_temp) {
|
2014-02-06 00:57:27 +01:00
|
|
|
gyros[0] -= (gyro_temp_coeff.X + gyro_temp_coeff.X2 * ctemp) * ctemp;
|
|
|
|
gyros[1] -= (gyro_temp_coeff.Y + gyro_temp_coeff.Y2 * ctemp) * ctemp;
|
|
|
|
gyros[2] -= (gyro_temp_coeff.Z + gyro_temp_coeff.Z2 * ctemp) * ctemp;
|
2013-12-29 14:23:46 +01:00
|
|
|
}
|
|
|
|
|
2013-12-29 18:45:44 +01:00
|
|
|
if (apply_accel_temp) {
|
2014-02-06 00:57:27 +01:00
|
|
|
accels[0] -= accel_temp_coeff.X * ctemp;
|
|
|
|
accels[1] -= accel_temp_coeff.Y * ctemp;
|
|
|
|
accels[2] -= accel_temp_coeff.Z * ctemp;
|
2013-12-29 14:23:46 +01:00
|
|
|
}
|
2013-05-18 19:36:45 +02:00
|
|
|
// gyrosData->temperature = 35.0f + ((float)mpu6000_data.temperature + 512.0f) / 340.0f;
|
|
|
|
// accelsData->temperature = 35.0f + ((float)mpu6000_data.temperature + 512.0f) / 340.0f;
|
2013-05-18 14:17:26 +02:00
|
|
|
#endif /* if defined(PIOS_INCLUDE_MPU6000) */
|
|
|
|
|
|
|
|
if (rotate) {
|
|
|
|
// TODO: rotate sensors too so stabilization is well behaved
|
|
|
|
float vec_out[3];
|
|
|
|
rot_mult(R, accels, vec_out);
|
|
|
|
accels[0] = vec_out[0];
|
|
|
|
accels[1] = vec_out[1];
|
|
|
|
accels[2] = vec_out[2];
|
|
|
|
rot_mult(R, gyros, vec_out);
|
|
|
|
gyros[0] = vec_out[0];
|
|
|
|
gyros[1] = vec_out[1];
|
|
|
|
gyros[2] = vec_out[2];
|
|
|
|
}
|
|
|
|
|
2014-02-06 00:57:27 +01:00
|
|
|
accelStateData->x = accels[0] - accel_bias.X;
|
|
|
|
accelStateData->y = accels[1] - accel_bias.Y;
|
|
|
|
accelStateData->z = accels[2] - accel_bias.Z;
|
2013-05-18 14:17:26 +02:00
|
|
|
|
2013-05-18 19:36:45 +02:00
|
|
|
gyrosData->x = gyros[0];
|
|
|
|
gyrosData->y = gyros[1];
|
|
|
|
gyrosData->z = gyros[2];
|
2013-05-18 14:17:26 +02:00
|
|
|
|
|
|
|
if (bias_correct_gyro) {
|
|
|
|
// Applying integral component here so it can be seen on the gyros and correct bias
|
|
|
|
gyrosData->x += gyro_correct_int[0];
|
|
|
|
gyrosData->y += gyro_correct_int[1];
|
|
|
|
gyrosData->z += gyro_correct_int[2];
|
|
|
|
}
|
|
|
|
|
|
|
|
// Force the roll & pitch gyro rates to average to zero during initialisation
|
|
|
|
gyro_correct_int[0] += -gyrosData->x * rollPitchBiasRate;
|
|
|
|
gyro_correct_int[1] += -gyrosData->y * rollPitchBiasRate;
|
|
|
|
|
|
|
|
// Because most crafts wont get enough information from gravity to zero yaw gyro, we try
|
|
|
|
// and make it average zero (weakly)
|
|
|
|
gyro_correct_int[2] += -gyrosData->z * yawBiasRate;
|
|
|
|
|
2013-05-18 19:36:45 +02:00
|
|
|
GyroStateSet(gyrosData);
|
|
|
|
AccelStateSet(accelStateData);
|
2013-05-18 14:17:26 +02:00
|
|
|
|
|
|
|
return 0;
|
2012-01-24 06:58:31 +01:00
|
|
|
}
|
|
|
|
|
2012-11-11 21:10:52 +01:00
|
|
|
static inline void apply_accel_filter(const float *raw, float *filtered)
|
2012-11-06 20:17:10 +01:00
|
|
|
{
|
2013-05-18 14:17:26 +02:00
|
|
|
if (accel_filter_enabled) {
|
|
|
|
filtered[0] = filtered[0] * accel_alpha + raw[0] * (1 - accel_alpha);
|
|
|
|
filtered[1] = filtered[1] * accel_alpha + raw[1] * (1 - accel_alpha);
|
|
|
|
filtered[2] = filtered[2] * accel_alpha + raw[2] * (1 - accel_alpha);
|
|
|
|
} else {
|
|
|
|
filtered[0] = raw[0];
|
|
|
|
filtered[1] = raw[1];
|
|
|
|
filtered[2] = raw[2];
|
|
|
|
}
|
2012-11-06 20:17:10 +01:00
|
|
|
}
|
|
|
|
|
2013-05-18 19:36:45 +02:00
|
|
|
static void updateAttitude(AccelStateData *accelStateData, GyroStateData *gyrosData)
|
2011-01-17 09:45:43 +01:00
|
|
|
{
|
2014-02-04 19:48:33 +01:00
|
|
|
float dT = PIOS_DELTATIME_GetAverageSeconds(&dtconfig);
|
2013-05-18 14:17:26 +02:00
|
|
|
|
|
|
|
// Bad practice to assume structure order, but saves memory
|
|
|
|
float *gyros = &gyrosData->x;
|
2013-05-18 19:36:45 +02:00
|
|
|
float *accels = &accelStateData->x;
|
2013-05-18 14:17:26 +02:00
|
|
|
|
|
|
|
float grot[3];
|
|
|
|
float accel_err[3];
|
|
|
|
|
|
|
|
// Apply smoothing to accel values, to reduce vibration noise before main calculations.
|
|
|
|
apply_accel_filter(accels, accels_filtered);
|
|
|
|
|
|
|
|
// Rotate gravity unit vector to body frame, filter 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]);
|
|
|
|
|
|
|
|
apply_accel_filter(grot, grot_filtered);
|
|
|
|
|
|
|
|
CrossProduct((const float *)accels_filtered, (const float *)grot_filtered, accel_err);
|
|
|
|
|
|
|
|
// Account for accel magnitude
|
|
|
|
float accel_mag = sqrtf(accels_filtered[0] * accels_filtered[0] + accels_filtered[1] * accels_filtered[1] + accels_filtered[2] * accels_filtered[2]);
|
|
|
|
if (accel_mag < 1.0e-3f) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Account for filtered gravity vector magnitude
|
|
|
|
float grot_mag;
|
|
|
|
|
|
|
|
if (accel_filter_enabled) {
|
|
|
|
grot_mag = sqrtf(grot_filtered[0] * grot_filtered[0] + grot_filtered[1] * grot_filtered[1] + grot_filtered[2] * grot_filtered[2]);
|
|
|
|
} else {
|
|
|
|
grot_mag = 1.0f;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (grot_mag < 1.0e-3f) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
accel_err[0] /= (accel_mag * grot_mag);
|
|
|
|
accel_err[1] /= (accel_mag * grot_mag);
|
|
|
|
accel_err[2] /= (accel_mag * grot_mag);
|
|
|
|
|
|
|
|
// Accumulate integral of error. Scale here so that units are (deg/s) but Ki has units of s
|
|
|
|
gyro_correct_int[0] += accel_err[0] * accelKi;
|
|
|
|
gyro_correct_int[1] += accel_err[1] * accelKi;
|
|
|
|
|
|
|
|
// gyro_correct_int[2] += accel_err[2] * accelKi;
|
|
|
|
|
|
|
|
// Correct rates based on error, integral component dealt with in updateSensors
|
|
|
|
gyros[0] += accel_err[0] * accelKp / dT;
|
|
|
|
gyros[1] += accel_err[1] * accelKp / dT;
|
|
|
|
gyros[2] += accel_err[2] * accelKp / dT;
|
|
|
|
|
|
|
|
{ // 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] * gyros[0] - q[2] * gyros[1] - q[3] * gyros[2]) * dT * (M_PI_F / 180.0f / 2.0f);
|
|
|
|
qdot[1] = (q[0] * gyros[0] - q[3] * gyros[1] + q[2] * gyros[2]) * dT * (M_PI_F / 180.0f / 2.0f);
|
|
|
|
qdot[2] = (q[3] * gyros[0] + q[0] * gyros[1] - q[1] * gyros[2]) * dT * (M_PI_F / 180.0f / 2.0f);
|
|
|
|
qdot[3] = (-q[2] * gyros[0] + q[1] * gyros[1] + q[0] * gyros[2]) * dT * (M_PI_F / 180.0f / 2.0f);
|
|
|
|
|
|
|
|
// Take a time step
|
|
|
|
q[0] = q[0] + qdot[0];
|
|
|
|
q[1] = q[1] + qdot[1];
|
|
|
|
q[2] = q[2] + qdot[2];
|
|
|
|
q[3] = q[3] + qdot[3];
|
|
|
|
|
|
|
|
if (q[0] < 0) {
|
|
|
|
q[0] = -q[0];
|
|
|
|
q[1] = -q[1];
|
|
|
|
q[2] = -q[2];
|
|
|
|
q[3] = -q[3];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Renomalize
|
|
|
|
float qmag = sqrtf(q[0] * q[0] + q[1] * q[1] + q[2] * q[2] + q[3] * q[3]);
|
|
|
|
|
|
|
|
// If quaternion has become inappropriately short or is nan reinit.
|
|
|
|
// THIS SHOULD NEVER ACTUALLY HAPPEN
|
|
|
|
if ((fabsf(qmag) < 1e-3f) || isnan(qmag)) {
|
|
|
|
q[0] = 1;
|
|
|
|
q[1] = 0;
|
|
|
|
q[2] = 0;
|
|
|
|
q[3] = 0;
|
|
|
|
} else {
|
|
|
|
q[0] = q[0] / qmag;
|
|
|
|
q[1] = q[1] / qmag;
|
|
|
|
q[2] = q[2] / qmag;
|
|
|
|
q[3] = q[3] / qmag;
|
|
|
|
}
|
|
|
|
|
2013-05-18 19:36:45 +02:00
|
|
|
AttitudeStateData attitudeState;
|
|
|
|
AttitudeStateGet(&attitudeState);
|
2013-05-18 14:17:26 +02:00
|
|
|
|
2013-05-18 19:36:45 +02:00
|
|
|
quat_copy(q, &attitudeState.q1);
|
2013-05-18 14:17:26 +02:00
|
|
|
|
|
|
|
// Convert into eueler degrees (makes assumptions about RPY order)
|
2013-05-18 19:36:45 +02:00
|
|
|
Quaternion2RPY(&attitudeState.q1, &attitudeState.Roll);
|
2013-05-18 14:17:26 +02:00
|
|
|
|
2013-05-18 19:36:45 +02:00
|
|
|
AttitudeStateSet(&attitudeState);
|
2011-01-21 04:40:21 +01:00
|
|
|
}
|
2011-02-08 17:34:28 +01:00
|
|
|
|
2013-05-18 14:17:26 +02:00
|
|
|
static void settingsUpdatedCb(__attribute__((unused)) UAVObjEvent *objEv)
|
|
|
|
{
|
|
|
|
AttitudeSettingsData attitudeSettings;
|
2013-12-29 14:08:53 +01:00
|
|
|
AccelGyroSettingsData accelGyroSettings;
|
2013-05-18 14:17:26 +02:00
|
|
|
|
|
|
|
AttitudeSettingsGet(&attitudeSettings);
|
2013-12-29 14:08:53 +01:00
|
|
|
AccelGyroSettingsGet(&accelGyroSettings);
|
2013-05-18 14:17:26 +02:00
|
|
|
|
|
|
|
accelKp = attitudeSettings.AccelKp;
|
|
|
|
accelKi = attitudeSettings.AccelKi;
|
|
|
|
yawBiasRate = attitudeSettings.YawBiasRate;
|
|
|
|
|
|
|
|
// Calculate accel filter alpha, in the same way as for gyro data in stabilization module.
|
|
|
|
const float fakeDt = 0.0025f;
|
|
|
|
if (attitudeSettings.AccelTau < 0.0001f) {
|
|
|
|
accel_alpha = 0; // not trusting this to resolve to 0
|
|
|
|
accel_filter_enabled = false;
|
|
|
|
} else {
|
|
|
|
accel_alpha = expf(-fakeDt / attitudeSettings.AccelTau);
|
|
|
|
accel_filter_enabled = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
zero_during_arming = attitudeSettings.ZeroDuringArming == ATTITUDESETTINGS_ZERODURINGARMING_TRUE;
|
|
|
|
bias_correct_gyro = attitudeSettings.BiasCorrectGyro == ATTITUDESETTINGS_BIASCORRECTGYRO_TRUE;
|
|
|
|
|
2014-02-06 00:57:27 +01:00
|
|
|
memcpy(&gyro_temp_coeff, &accelGyroSettings.gyro_temp_coeff, sizeof(AccelGyroSettingsgyro_temp_coeffData));
|
|
|
|
memcpy(&accel_temp_coeff, &accelGyroSettings.accel_temp_coeff, sizeof(AccelGyroSettingsaccel_temp_coeffData));
|
2013-12-29 14:08:53 +01:00
|
|
|
|
2013-05-18 14:17:26 +02:00
|
|
|
|
2014-02-06 00:57:27 +01:00
|
|
|
apply_gyro_temp = (fabsf(gyro_temp_coeff.X) > 1e-6f ||
|
|
|
|
fabsf(gyro_temp_coeff.Y) > 1e-6f ||
|
|
|
|
fabsf(gyro_temp_coeff.Z) > 1e-6f ||
|
|
|
|
fabsf(gyro_temp_coeff.X2) > 1e-6f ||
|
|
|
|
fabsf(gyro_temp_coeff.Y2) > 1e-6f ||
|
|
|
|
fabsf(gyro_temp_coeff.Z2) > 1e-6f);
|
2013-12-29 14:08:53 +01:00
|
|
|
|
2014-02-06 00:57:27 +01:00
|
|
|
apply_accel_temp = (fabsf(accel_temp_coeff.X) > 1e-6f ||
|
|
|
|
fabsf(accel_temp_coeff.Y) > 1e-6f ||
|
|
|
|
fabsf(accel_temp_coeff.Z) > 1e-6f);
|
2013-12-29 14:08:53 +01:00
|
|
|
|
|
|
|
gyro_correct_int[0] = accelGyroSettings.gyro_bias.X;
|
|
|
|
gyro_correct_int[1] = accelGyroSettings.gyro_bias.Y;
|
|
|
|
gyro_correct_int[2] = accelGyroSettings.gyro_bias.Z;
|
2013-05-18 14:17:26 +02:00
|
|
|
|
2014-02-06 00:57:27 +01:00
|
|
|
temp_calibrated_extent.min = accelGyroSettings.temp_calibrated_extent.min;
|
|
|
|
temp_calibrated_extent.max = accelGyroSettings.temp_calibrated_extent.max;
|
2013-12-29 14:08:53 +01:00
|
|
|
|
2013-12-29 18:45:44 +01:00
|
|
|
if (BOARDISCC3D) {
|
2014-02-06 00:57:27 +01:00
|
|
|
accel_bias.X = accelGyroSettings.accel_bias.X;
|
|
|
|
accel_bias.Y = accelGyroSettings.accel_bias.Y;
|
|
|
|
accel_bias.Z = accelGyroSettings.accel_bias.Z;
|
2013-12-29 14:08:53 +01:00
|
|
|
|
2014-02-06 00:57:27 +01:00
|
|
|
gyro_scale.X = accelGyroSettings.gyro_scale.X * PIOS_MPU6000_GetScale();
|
|
|
|
gyro_scale.Y = accelGyroSettings.gyro_scale.Y * PIOS_MPU6000_GetScale();
|
|
|
|
gyro_scale.Z = accelGyroSettings.gyro_scale.Z * PIOS_MPU6000_GetScale();
|
2013-12-29 14:08:53 +01:00
|
|
|
|
2014-02-06 00:57:27 +01:00
|
|
|
accel_scale.X = accelGyroSettings.accel_scale.X * PIOS_MPU6000_GetAccelScale();
|
|
|
|
accel_scale.Y = accelGyroSettings.accel_scale.Y * PIOS_MPU6000_GetAccelScale();
|
|
|
|
accel_scale.Z = accelGyroSettings.accel_scale.Z * PIOS_MPU6000_GetAccelScale();
|
2013-12-29 14:08:53 +01:00
|
|
|
} else {
|
|
|
|
// Original CC with analog gyros and ADXL accel
|
2014-02-06 00:57:27 +01:00
|
|
|
accel_bias.X = accelGyroSettings.accel_bias.X;
|
|
|
|
accel_bias.Y = accelGyroSettings.accel_bias.Y;
|
|
|
|
accel_bias.Z = accelGyroSettings.accel_bias.Z;
|
2013-12-29 14:08:53 +01:00
|
|
|
|
2014-02-06 00:57:27 +01:00
|
|
|
gyro_scale.X = accelGyroSettings.gyro_scale.X * STD_CC_ANALOG_GYRO_GAIN;
|
|
|
|
gyro_scale.Y = accelGyroSettings.gyro_scale.Y * STD_CC_ANALOG_GYRO_GAIN;
|
|
|
|
gyro_scale.Z = accelGyroSettings.gyro_scale.Z * STD_CC_ANALOG_GYRO_GAIN;
|
2013-12-29 14:08:53 +01:00
|
|
|
|
2014-02-06 00:57:27 +01:00
|
|
|
accel_scale.X = accelGyroSettings.accel_scale.X * STD_CC_ACCEL_SCALE;
|
|
|
|
accel_scale.Y = accelGyroSettings.accel_scale.Y * STD_CC_ACCEL_SCALE;
|
|
|
|
accel_scale.Z = accelGyroSettings.accel_scale.Z * STD_CC_ACCEL_SCALE;
|
2013-12-29 14:08:53 +01:00
|
|
|
}
|
2013-05-18 14:17:26 +02:00
|
|
|
|
|
|
|
// Indicates not to expend cycles on rotation
|
2014-04-10 01:47:37 +02:00
|
|
|
if (fabsf(attitudeSettings.BoardRotation.Pitch) < 0.00001f &&
|
|
|
|
fabsf(attitudeSettings.BoardRotation.Roll) < 0.00001f &&
|
|
|
|
fabsf(attitudeSettings.BoardRotation.Yaw) < 0.00001f) {
|
2013-05-18 14:17:26 +02:00
|
|
|
rotate = 0;
|
|
|
|
|
|
|
|
// Shouldn't be used but to be safe
|
|
|
|
float rotationQuat[4] = { 1, 0, 0, 0 };
|
|
|
|
Quaternion2R(rotationQuat, R);
|
|
|
|
} else {
|
|
|
|
float rotationQuat[4];
|
2013-09-01 12:10:55 +02:00
|
|
|
const float rpy[3] = { attitudeSettings.BoardRotation.Roll,
|
|
|
|
attitudeSettings.BoardRotation.Pitch,
|
|
|
|
attitudeSettings.BoardRotation.Yaw };
|
2013-05-18 14:17:26 +02:00
|
|
|
RPY2Quaternion(rpy, rotationQuat);
|
|
|
|
Quaternion2R(rotationQuat, R);
|
|
|
|
rotate = 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (attitudeSettings.TrimFlight == ATTITUDESETTINGS_TRIMFLIGHT_START) {
|
|
|
|
trim_accels[0] = 0;
|
|
|
|
trim_accels[1] = 0;
|
|
|
|
trim_accels[2] = 0;
|
|
|
|
trim_samples = 0;
|
|
|
|
trim_requested = true;
|
|
|
|
} else if (attitudeSettings.TrimFlight == ATTITUDESETTINGS_TRIMFLIGHT_LOAD) {
|
|
|
|
trim_requested = false;
|
2013-12-29 14:08:53 +01:00
|
|
|
accelGyroSettings.accel_scale.X = trim_accels[0] / trim_samples;
|
|
|
|
accelGyroSettings.accel_scale.Y = trim_accels[1] / trim_samples;
|
2013-05-18 14:17:26 +02:00
|
|
|
// Z should average -grav
|
2013-12-29 14:08:53 +01:00
|
|
|
accelGyroSettings.accel_scale.Z = trim_accels[2] / trim_samples + GRAV;
|
2013-12-29 18:45:44 +01:00
|
|
|
attitudeSettings.TrimFlight = ATTITUDESETTINGS_TRIMFLIGHT_NORMAL;
|
2013-05-18 14:17:26 +02:00
|
|
|
AttitudeSettingsSet(&attitudeSettings);
|
|
|
|
} else {
|
|
|
|
trim_requested = false;
|
|
|
|
}
|
2011-02-08 17:34:28 +01:00
|
|
|
}
|
2011-01-16 03:45:27 +01:00
|
|
|
/**
|
2011-08-22 18:17:51 +02:00
|
|
|
* @}
|
|
|
|
* @}
|
|
|
|
*/
|