2010-02-28 13:10:48 +01:00
|
|
|
/**
|
|
|
|
******************************************************************************
|
2010-08-15 06:08:57 +02:00
|
|
|
* @addtogroup AHRS AHRS Control
|
|
|
|
* @brief The AHRS Modules perform
|
|
|
|
*
|
|
|
|
* @{
|
|
|
|
* @addtogroup AHRS_Main
|
|
|
|
* @brief Main function which does the hardware dependent stuff
|
|
|
|
* @{
|
|
|
|
*
|
2010-02-28 13:10:48 +01:00
|
|
|
*
|
|
|
|
* @file ahrs.c
|
|
|
|
* @author The OpenPilot Team, http://www.openpilot.org Copyright (C) 2010.
|
2010-08-15 06:08:57 +02:00
|
|
|
* @brief INSGPS Test Program
|
2010-02-28 13:10:48 +01:00
|
|
|
* @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
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
|
/* OpenPilot Includes */
|
|
|
|
#include "ahrs.h"
|
2010-09-14 09:03:34 +02:00
|
|
|
#include "ahrs_adc.h"
|
2010-09-14 09:03:36 +02:00
|
|
|
#include "ahrs_timer.h"
|
2010-07-04 04:21:47 +02:00
|
|
|
#include "pios_opahrs_proto.h"
|
|
|
|
#include "ahrs_fsm.h" /* lfsm_state */
|
2010-08-16 18:58:18 +02:00
|
|
|
#include "insgps.h"
|
2010-08-17 07:32:07 +02:00
|
|
|
#include "CoordinateConversions.h"
|
2010-02-28 13:10:48 +01:00
|
|
|
|
2010-09-14 08:42:56 +02:00
|
|
|
volatile enum algorithms ahrs_algorithm;
|
2010-02-28 13:10:48 +01:00
|
|
|
|
2010-08-26 08:25:11 +02:00
|
|
|
// For debugging the raw sensors
|
|
|
|
//#define DUMP_RAW
|
2010-09-14 09:03:34 +02:00
|
|
|
//#define DUMP_FRIENDLY
|
2010-08-26 08:25:11 +02:00
|
|
|
|
2010-08-15 06:08:57 +02:00
|
|
|
/**
|
|
|
|
* @addtogroup AHRS_Definitions
|
|
|
|
* @{
|
|
|
|
*/
|
2010-08-18 07:47:44 +02:00
|
|
|
// Currently analog acquistion hard coded at 480 Hz
|
2010-09-14 09:03:34 +02:00
|
|
|
#define ADC_RATE (4*480)
|
|
|
|
#define EKF_RATE (ADC_RATE / adc_oversampling)
|
2010-08-15 06:08:57 +02:00
|
|
|
#define VDD 3.3 /* supply voltage for ADC */
|
|
|
|
#define FULL_RANGE 4096 /* 12 bit ADC */
|
|
|
|
#define ACCEL_RANGE 2 /* adjustable by FS input */
|
|
|
|
#define ACCEL_GRAVITY 9.81 /* m s^-1 */
|
|
|
|
#define ACCEL_SENSITIVITY ( VDD / 5 )
|
|
|
|
#define ACCEL_SCALE ( (VDD / FULL_RANGE) / ACCEL_SENSITIVITY * 2 / ACCEL_RANGE * ACCEL_GRAVITY )
|
|
|
|
#define ACCEL_OFFSET -2048
|
|
|
|
|
2010-08-23 04:35:39 +02:00
|
|
|
#define GYRO_SENSITIVITY ( 2.0 / 1000 ) /* 2 mV / (deg s^-1) */
|
|
|
|
#define RAD_PER_DEGREE ( M_PI / 180 )
|
2010-08-15 06:08:57 +02:00
|
|
|
#define GYRO_SCALE ( (VDD / FULL_RANGE) / GYRO_SENSITIVITY * RAD_PER_DEGREE )
|
|
|
|
#define GYRO_OFFSET -1675 /* From data sheet, zero accel output is 1.35 v */
|
2010-08-29 08:38:30 +02:00
|
|
|
|
|
|
|
#define MAX_IDLE_COUNT 65e3
|
2010-08-16 18:58:24 +02:00
|
|
|
/**
|
|
|
|
* @}
|
|
|
|
*/
|
2010-08-15 06:08:57 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @addtogroup AHRS_Local Local Variables
|
|
|
|
* @{
|
|
|
|
*/
|
2010-07-04 04:21:47 +02:00
|
|
|
struct mag_sensor {
|
2010-08-31 04:11:09 +02:00
|
|
|
uint8_t id[4];
|
2010-09-16 04:58:56 +02:00
|
|
|
uint8_t updated;
|
2010-08-31 04:11:09 +02:00
|
|
|
struct {
|
|
|
|
int16_t axis[3];
|
|
|
|
} raw;
|
2010-07-04 04:21:47 +02:00
|
|
|
};
|
2010-02-28 13:10:48 +01:00
|
|
|
|
2010-07-04 04:21:47 +02:00
|
|
|
struct accel_sensor {
|
2010-08-31 04:11:09 +02:00
|
|
|
struct {
|
|
|
|
uint16_t x;
|
|
|
|
uint16_t y;
|
|
|
|
uint16_t z;
|
|
|
|
} raw;
|
|
|
|
struct {
|
|
|
|
float x;
|
|
|
|
float y;
|
|
|
|
float z;
|
|
|
|
} filtered;
|
2010-07-04 04:21:47 +02:00
|
|
|
};
|
|
|
|
|
2010-08-15 01:42:14 +02:00
|
|
|
struct gyro_sensor {
|
2010-08-31 04:11:09 +02:00
|
|
|
struct {
|
|
|
|
uint16_t x;
|
|
|
|
uint16_t y;
|
|
|
|
uint16_t z;
|
|
|
|
} raw;
|
|
|
|
struct {
|
|
|
|
float x;
|
|
|
|
float y;
|
|
|
|
float z;
|
|
|
|
} filtered;
|
|
|
|
struct {
|
|
|
|
uint16_t xy;
|
|
|
|
uint16_t z;
|
|
|
|
} temp;
|
2010-08-15 01:42:14 +02:00
|
|
|
};
|
|
|
|
|
2010-07-04 04:21:47 +02:00
|
|
|
struct attitude_solution {
|
2010-08-31 04:11:09 +02:00
|
|
|
struct {
|
|
|
|
float q1;
|
|
|
|
float q2;
|
|
|
|
float q3;
|
|
|
|
float q4;
|
|
|
|
} quaternion;
|
2010-07-04 04:21:47 +02:00
|
|
|
};
|
2010-02-28 13:10:48 +01:00
|
|
|
|
2010-08-15 03:02:13 +02:00
|
|
|
struct altitude_sensor {
|
2010-08-31 04:11:09 +02:00
|
|
|
float altitude;
|
|
|
|
bool updated;
|
2010-08-15 03:02:13 +02:00
|
|
|
};
|
|
|
|
|
2010-08-19 07:18:34 +02:00
|
|
|
struct gps_sensor {
|
2010-08-31 04:11:09 +02:00
|
|
|
float NED[3];
|
|
|
|
float heading;
|
|
|
|
float groundspeed;
|
|
|
|
float quality;
|
|
|
|
bool updated;
|
2010-08-19 07:18:34 +02:00
|
|
|
};
|
|
|
|
|
2010-09-14 08:42:56 +02:00
|
|
|
struct mag_sensor mag_data;
|
|
|
|
volatile struct accel_sensor accel_data;
|
|
|
|
volatile struct gyro_sensor gyro_data;
|
|
|
|
volatile struct altitude_sensor altitude_data;
|
|
|
|
struct gps_sensor gps_data;
|
|
|
|
volatile struct attitude_solution attitude_data;
|
2010-08-19 07:18:34 +02:00
|
|
|
|
2010-08-16 18:58:24 +02:00
|
|
|
/**
|
|
|
|
* @}
|
|
|
|
*/
|
|
|
|
|
2010-07-04 04:21:47 +02:00
|
|
|
|
|
|
|
/* Function Prototypes */
|
|
|
|
void process_spi_request(void);
|
2010-08-16 18:58:22 +02:00
|
|
|
void downsample_data(void);
|
|
|
|
void calibrate_sensors(void);
|
2010-08-21 23:46:02 +02:00
|
|
|
void converge_insgps();
|
2010-09-14 09:03:36 +02:00
|
|
|
|
|
|
|
volatile uint32_t last_counter_idle_start = 0;
|
|
|
|
volatile uint32_t last_counter_idle_end = 0;
|
|
|
|
volatile uint32_t idle_counts;
|
|
|
|
volatile uint32_t running_counts;
|
|
|
|
uint32_t counter_val;
|
2010-08-15 06:08:57 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @addtogroup AHRS_Global_Data AHRS Global Data
|
|
|
|
* @{
|
|
|
|
* Public data. Used by both EKF and the sender
|
|
|
|
*/
|
2010-08-16 18:58:22 +02:00
|
|
|
//! Accelerometer variance after filter from OP or calibrate_sensors
|
2010-08-21 23:46:02 +02:00
|
|
|
float accel_var[3] = {1,1,1};
|
2010-08-16 18:58:22 +02:00
|
|
|
//! Gyro variance after filter from OP or calibrate sensors
|
2010-08-21 23:46:02 +02:00
|
|
|
float gyro_var[3] = {1,1,1};
|
|
|
|
//! Accelerometer scale after calibration
|
|
|
|
float accel_scale[3] = {ACCEL_SCALE, ACCEL_SCALE, ACCEL_SCALE};
|
|
|
|
//! Gyro scale after calibration
|
|
|
|
float gyro_scale[3] = {GYRO_SCALE, GYRO_SCALE, GYRO_SCALE};
|
2010-08-16 18:58:22 +02:00
|
|
|
//! Magnetometer variance from OP or calibrate sensors
|
2010-08-21 23:46:02 +02:00
|
|
|
float mag_var[3] = {1,1,1};
|
2010-08-16 18:58:22 +02:00
|
|
|
//! Accelerometer bias from OP or calibrate sensors
|
2010-08-21 23:46:02 +02:00
|
|
|
int16_t accel_bias[3] = {ACCEL_OFFSET, ACCEL_OFFSET, ACCEL_OFFSET};
|
2010-08-16 18:58:22 +02:00
|
|
|
//! Gyroscope bias term from OP or calibrate sensors
|
2010-08-21 23:46:02 +02:00
|
|
|
int16_t gyro_bias[3] = {0,0,0};
|
2010-08-16 18:58:22 +02:00
|
|
|
//! Magnetometer bias (direction) from OP or calibrate sensors
|
2010-08-21 23:46:02 +02:00
|
|
|
int16_t mag_bias[3] = {0,0,0};
|
2010-08-16 18:58:22 +02:00
|
|
|
//! Filter coefficients used in decimation. Limited order so filter can't run between samples
|
2010-09-14 09:03:34 +02:00
|
|
|
int16_t fir_coeffs[50];
|
2010-08-19 07:18:34 +02:00
|
|
|
//! Home location in ECEF coordinates
|
|
|
|
double BaseECEF[3] = {0, 0, 0};
|
|
|
|
//! Rotation matrix from LLA to Rne
|
|
|
|
float Rne[3][3];
|
2010-08-21 23:46:02 +02:00
|
|
|
//! Indicates the communications are requesting a calibration
|
|
|
|
uint8_t calibration_pending = FALSE;
|
2010-09-14 09:03:34 +02:00
|
|
|
//! The oversampling rate, ekf is 2k / this
|
2010-09-14 09:03:36 +02:00
|
|
|
static uint8_t adc_oversampling = 15;
|
2010-08-15 06:08:57 +02:00
|
|
|
/**
|
|
|
|
* @}
|
|
|
|
*/
|
|
|
|
|
2010-02-28 13:10:48 +01:00
|
|
|
/**
|
2010-08-31 04:11:09 +02:00
|
|
|
* @brief AHRS Main function
|
|
|
|
*/
|
2010-02-28 13:10:48 +01:00
|
|
|
int main()
|
|
|
|
{
|
2010-08-31 04:11:09 +02:00
|
|
|
float gyro[3], accel[3], mag[3];
|
|
|
|
float vel[3] = {0,0,0};
|
2010-09-06 09:54:19 +02:00
|
|
|
gps_data.quality = -1;
|
2010-08-31 04:11:09 +02:00
|
|
|
|
|
|
|
ahrs_algorithm = INSGPS_Algo;
|
|
|
|
|
|
|
|
/* Brings up System using CMSIS functions, enables the LEDs. */
|
|
|
|
PIOS_SYS_Init();
|
|
|
|
|
|
|
|
/* Delay system */
|
|
|
|
PIOS_DELAY_Init();
|
|
|
|
|
|
|
|
/* Communication system */
|
|
|
|
PIOS_COM_Init();
|
|
|
|
|
|
|
|
/* ADC system */
|
2010-09-14 09:03:34 +02:00
|
|
|
AHRS_ADC_Config(adc_oversampling);
|
2010-08-31 04:11:09 +02:00
|
|
|
|
|
|
|
/* Setup the Accelerometer FS (Full-Scale) GPIO */
|
|
|
|
PIOS_GPIO_Enable(0);
|
|
|
|
SET_ACCEL_2G;
|
2010-09-14 08:42:56 +02:00
|
|
|
#if defined(PIOS_INCLUDE_HMC5843) && defined(PIOS_INCLUDE_I2C)
|
|
|
|
/* Magnetic sensor system */
|
|
|
|
PIOS_I2C_Init();
|
|
|
|
PIOS_HMC5843_Init();
|
2010-08-31 04:11:09 +02:00
|
|
|
|
|
|
|
// Get 3 ID bytes
|
|
|
|
strcpy ((char *)mag_data.id, "ZZZ");
|
|
|
|
PIOS_HMC5843_ReadID(mag_data.id);
|
2010-09-14 08:42:56 +02:00
|
|
|
#endif
|
2010-08-31 04:11:09 +02:00
|
|
|
|
|
|
|
/* SPI link to master */
|
|
|
|
PIOS_SPI_Init();
|
|
|
|
|
|
|
|
lfsm_init();
|
|
|
|
|
2010-09-14 09:03:34 +02:00
|
|
|
ahrs_state = AHRS_IDLE;
|
|
|
|
|
2010-08-31 04:11:09 +02:00
|
|
|
/* Use simple averaging filter for now */
|
2010-09-14 09:03:34 +02:00
|
|
|
for (int i = 0; i < adc_oversampling; i++)
|
2010-08-31 04:11:09 +02:00
|
|
|
fir_coeffs[i] = 1;
|
2010-09-14 09:03:34 +02:00
|
|
|
fir_coeffs[adc_oversampling] = adc_oversampling;
|
2010-08-31 04:11:09 +02:00
|
|
|
|
|
|
|
if(ahrs_algorithm == INSGPS_Algo) {
|
|
|
|
// compute a data point and initialize INS
|
|
|
|
downsample_data();
|
|
|
|
converge_insgps();
|
|
|
|
}
|
|
|
|
|
2010-08-26 08:25:11 +02:00
|
|
|
#ifdef DUMP_RAW
|
2010-09-08 08:15:18 +02:00
|
|
|
int previous_conversion;
|
2010-08-26 08:25:11 +02:00
|
|
|
while(1) {
|
|
|
|
int result;
|
2010-09-08 08:15:18 +02:00
|
|
|
uint8_t framing[16] = {0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15};
|
2010-08-26 08:25:11 +02:00
|
|
|
while( ahrs_state != AHRS_DATA_READY );
|
|
|
|
ahrs_state = AHRS_PROCESSING;
|
2010-09-08 08:15:18 +02:00
|
|
|
|
|
|
|
if(total_conversion_blocks != previous_conversion+1)
|
|
|
|
PIOS_LED_On(LED1); // not keeping up
|
|
|
|
else
|
|
|
|
PIOS_LED_Off(LED1);
|
|
|
|
previous_conversion = total_conversion_blocks;
|
|
|
|
|
2010-08-26 08:25:11 +02:00
|
|
|
downsample_data();
|
|
|
|
ahrs_state = AHRS_IDLE;;
|
2010-08-31 04:11:09 +02:00
|
|
|
|
2010-08-26 08:25:11 +02:00
|
|
|
// Dump raw buffer
|
2010-09-07 18:33:10 +02:00
|
|
|
result = PIOS_COM_SendBuffer(PIOS_COM_AUX, &framing[0], 16); // framing header
|
2010-08-26 08:25:11 +02:00
|
|
|
result += PIOS_COM_SendBuffer(PIOS_COM_AUX, (uint8_t *) &total_conversion_blocks, sizeof(total_conversion_blocks)); // dump block number
|
|
|
|
result += PIOS_COM_SendBuffer(PIOS_COM_AUX, (uint8_t *) &valid_data_buffer[0], ADC_OVERSAMPLE * ADC_CONTINUOUS_CHANNELS * sizeof(valid_data_buffer[0]));
|
|
|
|
if(result == 0)
|
|
|
|
PIOS_LED_Off(LED1);
|
|
|
|
else {
|
|
|
|
PIOS_LED_On(LED1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
2010-09-14 09:03:36 +02:00
|
|
|
|
|
|
|
timer_start();
|
2010-08-15 06:08:57 +02:00
|
|
|
|
2010-08-31 04:11:09 +02:00
|
|
|
/******************* Main EKF loop ****************************/
|
|
|
|
while (1) {
|
2010-09-14 09:03:36 +02:00
|
|
|
|
2010-08-31 04:11:09 +02:00
|
|
|
// Alive signal
|
2010-09-14 08:42:56 +02:00
|
|
|
if((total_conversion_blocks % 100) == 0)
|
|
|
|
PIOS_LED_Toggle(LED1);
|
2010-08-31 04:11:09 +02:00
|
|
|
|
|
|
|
if(calibration_pending)
|
|
|
|
{
|
|
|
|
calibrate_sensors();
|
|
|
|
calibration_pending = FALSE;
|
|
|
|
}
|
|
|
|
|
2010-09-14 08:42:56 +02:00
|
|
|
#if defined(PIOS_INCLUDE_HMC5843) && defined(PIOS_INCLUDE_I2C)
|
2010-09-15 16:21:05 +02:00
|
|
|
// Get magnetic readings
|
|
|
|
if (PIOS_HMC5843_NewDataAvailable()) {
|
|
|
|
PIOS_HMC5843_ReadMag(mag_data.raw.axis);
|
2010-09-16 04:58:56 +02:00
|
|
|
mag_data.updated = 1;
|
2010-09-15 16:21:05 +02:00
|
|
|
}
|
2010-09-14 08:42:56 +02:00
|
|
|
#endif
|
2010-08-31 04:11:09 +02:00
|
|
|
// Delay for valid data
|
2010-09-14 09:03:36 +02:00
|
|
|
|
|
|
|
counter_val = timer_count();
|
|
|
|
running_counts = counter_val - last_counter_idle_end;
|
|
|
|
last_counter_idle_start = counter_val;
|
|
|
|
|
|
|
|
while ( ahrs_state != AHRS_DATA_READY );
|
|
|
|
|
|
|
|
counter_val = timer_count();
|
|
|
|
idle_counts = counter_val - last_counter_idle_start;
|
|
|
|
last_counter_idle_end = counter_val;
|
2010-08-31 04:11:09 +02:00
|
|
|
|
|
|
|
ahrs_state = AHRS_PROCESSING;
|
|
|
|
|
|
|
|
downsample_data();
|
|
|
|
|
|
|
|
/***************** SEND BACK SOME RAW DATA ************************/
|
|
|
|
// Hacky - grab one sample from buffer to populate this. Need to send back
|
|
|
|
// all raw data if it's happening
|
|
|
|
accel_data.raw.x = valid_data_buffer[0];
|
|
|
|
accel_data.raw.y = valid_data_buffer[2];
|
|
|
|
accel_data.raw.z = valid_data_buffer[4];
|
|
|
|
|
|
|
|
gyro_data.raw.x = valid_data_buffer[1];
|
|
|
|
gyro_data.raw.y = valid_data_buffer[3];
|
|
|
|
gyro_data.raw.z = valid_data_buffer[5];
|
|
|
|
|
|
|
|
gyro_data.temp.xy = valid_data_buffer[6];
|
|
|
|
gyro_data.temp.z = valid_data_buffer[7];
|
|
|
|
|
|
|
|
if(ahrs_algorithm == INSGPS_Algo)
|
|
|
|
{
|
|
|
|
/******************** INS ALGORITHM **************************/
|
|
|
|
|
|
|
|
// format data for INS algo
|
|
|
|
gyro[0] = gyro_data.filtered.x;
|
|
|
|
gyro[1] = gyro_data.filtered.y;
|
|
|
|
gyro[2] = gyro_data.filtered.z;
|
|
|
|
accel[0] = accel_data.filtered.x,
|
|
|
|
accel[1] = accel_data.filtered.y,
|
|
|
|
accel[2] = accel_data.filtered.z,
|
2010-08-18 07:47:44 +02:00
|
|
|
|
2010-08-31 04:11:09 +02:00
|
|
|
// Note: The magnetometer driver returns registers X,Y,Z from the chip which are
|
|
|
|
// (left, backward, up). Remapping to (forward, right, down).
|
|
|
|
mag[0] = -(mag_data.raw.axis[1] - mag_bias[1]);
|
|
|
|
mag[1] = -(mag_data.raw.axis[0] - mag_bias[0]);
|
|
|
|
mag[2] = -(mag_data.raw.axis[2] - mag_bias[2]);
|
|
|
|
|
|
|
|
INSPrediction(gyro, accel, 1 / (float) EKF_RATE);
|
|
|
|
|
2010-09-06 09:54:19 +02:00
|
|
|
if ( gps_data.updated && gps_data.quality == 1)
|
2010-08-31 04:11:09 +02:00
|
|
|
{
|
2010-09-06 09:54:19 +02:00
|
|
|
// Compute velocity from Heading and groundspeed
|
|
|
|
vel[0] = gps_data.groundspeed * cos(gps_data.heading * M_PI / 180);
|
|
|
|
vel[1] = gps_data.groundspeed * sin(gps_data.heading * M_PI / 180);
|
|
|
|
|
|
|
|
// Completely unprincipled way to make the position variance
|
|
|
|
// increase as data quality decreases but keep it bounded
|
|
|
|
// Variance becomes 40 m^2 and 40 (m/s)^2 when no gps
|
|
|
|
INSSetPosVelVar(0.004);
|
2010-09-16 04:58:56 +02:00
|
|
|
if(gps_data.updated) {
|
|
|
|
//TOOD: add check for altitude updates
|
|
|
|
FullCorrection(mag, gps_data.NED, vel, altitude_data.altitude);
|
|
|
|
gps_data.updated = 0;
|
|
|
|
} else {
|
|
|
|
GpsBaroCorrection(gps_data.NED,vel,altitude_data.altitude);
|
|
|
|
}
|
|
|
|
|
2010-09-06 09:54:19 +02:00
|
|
|
gps_data.updated = false;
|
2010-09-16 04:58:56 +02:00
|
|
|
mag_data.updated = 0;
|
2010-08-31 04:11:09 +02:00
|
|
|
}
|
2010-09-16 04:58:56 +02:00
|
|
|
else if(gps_data.quality != -1 && mag_data.updated == 1) {
|
2010-09-04 04:19:04 +02:00
|
|
|
MagCorrection(mag); // only trust mags if outdoors
|
2010-09-16 04:58:56 +02:00
|
|
|
mag_data.updated = 0;
|
|
|
|
} else {
|
2010-09-06 09:54:19 +02:00
|
|
|
// Indoors, update with zero position and velocity and high covariance
|
|
|
|
INSSetPosVelVar(0.1);
|
|
|
|
vel[0] = 0;
|
|
|
|
vel[1] = 0;
|
|
|
|
vel[2] = 0;
|
2010-09-14 08:42:56 +02:00
|
|
|
|
2010-09-06 09:54:19 +02:00
|
|
|
VelBaroCorrection(vel,altitude_data.altitude);
|
2010-09-13 00:18:18 +02:00
|
|
|
// MagVelBaroCorrection(mag,vel,altitude_data.altitude); // only trust mags if outdoors
|
2010-09-06 09:54:19 +02:00
|
|
|
}
|
2010-08-31 04:11:09 +02:00
|
|
|
|
|
|
|
attitude_data.quaternion.q1 = Nav.q[0];
|
|
|
|
attitude_data.quaternion.q2 = Nav.q[1];
|
|
|
|
attitude_data.quaternion.q3 = Nav.q[2];
|
|
|
|
attitude_data.quaternion.q4 = Nav.q[3];
|
|
|
|
}
|
|
|
|
else if( ahrs_algorithm == SIMPLE_Algo )
|
|
|
|
{
|
|
|
|
float q[4];
|
|
|
|
float rpy[3];
|
|
|
|
/***************** SIMPLE ATTITUDE FROM NORTH AND ACCEL ************/
|
|
|
|
/* Very simple computation of the heading and attitude from accel. */
|
|
|
|
rpy[2] = atan2((mag_data.raw.axis[0]), (-1 * mag_data.raw.axis[1])) * 180 / M_PI;
|
|
|
|
rpy[1] = atan2(accel_data.filtered.x, accel_data.filtered.z) * 180 / M_PI;
|
|
|
|
rpy[0] = atan2(accel_data.filtered.y,accel_data.filtered.z) * 180 / M_PI;
|
|
|
|
|
|
|
|
RPY2Quaternion(rpy,q);
|
|
|
|
attitude_data.quaternion.q1 = q[0];
|
|
|
|
attitude_data.quaternion.q2 = q[1];
|
|
|
|
attitude_data.quaternion.q3 = q[2];
|
|
|
|
attitude_data.quaternion.q4 = q[3];
|
|
|
|
}
|
|
|
|
|
|
|
|
ahrs_state = AHRS_IDLE;
|
2010-08-22 02:21:54 +02:00
|
|
|
|
2010-08-29 08:38:30 +02:00
|
|
|
#ifdef DUMP_FRIENDLY
|
2010-09-08 08:15:21 +02:00
|
|
|
PIOS_COM_SendFormattedStringNonBlocking(PIOS_COM_AUX, "b: %d\r\n", total_conversion_blocks);
|
|
|
|
PIOS_COM_SendFormattedStringNonBlocking(PIOS_COM_AUX, "a: %d %d %d\r\n", (int16_t)(accel_data.filtered.x * 1000), (int16_t)(accel_data.filtered.y * 1000), (int16_t)(accel_data.filtered.z * 1000));
|
|
|
|
PIOS_COM_SendFormattedStringNonBlocking(PIOS_COM_AUX, "g: %d %d %d\r\n", (int16_t)(gyro_data.filtered.x * 1000), (int16_t)(gyro_data.filtered.y * 1000), (int16_t)(gyro_data.filtered.z * 1000));
|
|
|
|
PIOS_COM_SendFormattedStringNonBlocking(PIOS_COM_AUX, "m: %d %d %d\r\n", mag_data.raw.axis[0], mag_data.raw.axis[1], mag_data.raw.axis[2]);
|
|
|
|
PIOS_COM_SendFormattedStringNonBlocking(PIOS_COM_AUX, "q: %d %d %d %d\r\n", (int16_t)(Nav.q[0] * 1000), (int16_t)(Nav.q[1] * 1000), (int16_t)(Nav.q[2] * 1000), (int16_t)(Nav.q[3] * 1000));
|
2010-08-29 08:38:30 +02:00
|
|
|
#endif
|
2010-09-12 14:44:57 +02:00
|
|
|
|
|
|
|
process_spi_request();
|
2010-08-31 04:11:09 +02:00
|
|
|
}
|
2010-07-04 04:21:47 +02:00
|
|
|
|
2010-08-31 04:11:09 +02:00
|
|
|
return 0;
|
2010-07-04 04:21:47 +02:00
|
|
|
}
|
|
|
|
|
2010-08-15 06:08:57 +02:00
|
|
|
/**
|
|
|
|
* @brief Downsample the analog data
|
|
|
|
* @return none
|
|
|
|
*
|
|
|
|
* Tried to make as much of the filtering fixed point when possible. Need to account
|
|
|
|
* for offset for each sample before the multiplication if filter not a boxcar. Could
|
|
|
|
* precompute fixed offset as sum[fir_coeffs[i]] * ACCEL_OFFSET. Puts data into global
|
|
|
|
* data structures @ref accel_data and @ref gyro_data.
|
2010-08-18 07:47:44 +02:00
|
|
|
*
|
|
|
|
* The accel_data values are converted into a coordinate system where X is forwards along
|
|
|
|
* the fuselage, Y is along right the wing, and Z is down.
|
2010-08-15 06:08:57 +02:00
|
|
|
*/
|
|
|
|
void downsample_data()
|
|
|
|
{
|
2010-08-31 04:11:09 +02:00
|
|
|
int32_t accel_raw[3], gyro_raw[3];
|
|
|
|
uint16_t i;
|
|
|
|
|
|
|
|
// Get the Y data. Third byte in. Convert to m/s
|
|
|
|
accel_raw[0] = 0;
|
2010-09-14 09:03:34 +02:00
|
|
|
for( i = 0; i < adc_oversampling; i++ )
|
|
|
|
accel_raw[0] += ( valid_data_buffer[0 + i * PIOS_ADC_NUM_PINS] + accel_bias[1] ) * fir_coeffs[i];
|
|
|
|
accel_data.filtered.y = (float) accel_raw[0] / (float) fir_coeffs[adc_oversampling] * accel_scale[1];
|
2010-08-31 04:11:09 +02:00
|
|
|
|
|
|
|
// Get the X data which projects forward/backwards. Fifth byte in. Convert to m/s
|
|
|
|
accel_raw[1] = 0;
|
2010-09-14 09:03:34 +02:00
|
|
|
for( i = 0; i < adc_oversampling; i++ )
|
|
|
|
accel_raw[1] += ( valid_data_buffer[2 + i * PIOS_ADC_NUM_PINS] + accel_bias[0] ) * fir_coeffs[i];
|
|
|
|
accel_data.filtered.x = (float) accel_raw[1] / (float) fir_coeffs[adc_oversampling] * accel_scale[0];
|
2010-08-31 04:11:09 +02:00
|
|
|
|
|
|
|
// Get the Z data. Third byte in. Convert to m/s
|
|
|
|
accel_raw[2] = 0;
|
2010-09-14 09:03:34 +02:00
|
|
|
for( i = 0; i < adc_oversampling; i++ )
|
|
|
|
accel_raw[2] += ( valid_data_buffer[4 + i * PIOS_ADC_NUM_PINS] + accel_bias[2] ) * fir_coeffs[i];
|
|
|
|
accel_data.filtered.z = -(float) accel_raw[2] / (float) fir_coeffs[adc_oversampling] * accel_scale[2];
|
2010-08-31 04:11:09 +02:00
|
|
|
|
|
|
|
// Get the X gyro data. Seventh byte in. Convert to deg/s.
|
|
|
|
gyro_raw[0] = 0;
|
2010-09-14 09:03:34 +02:00
|
|
|
for( i = 0; i < adc_oversampling; i++ )
|
|
|
|
gyro_raw[0] += ( valid_data_buffer[1 + i * PIOS_ADC_NUM_PINS] + gyro_bias[0] ) * fir_coeffs[i];
|
|
|
|
gyro_data.filtered.x = (float) gyro_raw[0] / (float) fir_coeffs[adc_oversampling] * gyro_scale[0];
|
2010-08-31 04:11:09 +02:00
|
|
|
|
|
|
|
// Get the Y gyro data. Second byte in. Convert to deg/s.
|
|
|
|
gyro_raw[1] = 0;
|
2010-09-14 09:03:34 +02:00
|
|
|
for( i = 0; i < adc_oversampling; i++ )
|
|
|
|
gyro_raw[1] += ( valid_data_buffer[3 + i * PIOS_ADC_NUM_PINS] + gyro_bias[1] ) * fir_coeffs[i];
|
|
|
|
gyro_data.filtered.y = (float) gyro_raw[1] / (float) fir_coeffs[adc_oversampling] * gyro_scale[1];
|
2010-08-31 04:11:09 +02:00
|
|
|
|
|
|
|
// Get the Z gyro data. Fifth byte in. Convert to deg/s.
|
|
|
|
gyro_raw[2] = 0;
|
2010-09-14 09:03:34 +02:00
|
|
|
for( i = 0; i < adc_oversampling; i++ )
|
|
|
|
gyro_raw[2] += ( valid_data_buffer[5 + i * PIOS_ADC_NUM_PINS] + gyro_bias[2] ) * fir_coeffs[i];
|
|
|
|
gyro_data.filtered.z = (float) gyro_raw[2] / (float) fir_coeffs[adc_oversampling] * gyro_scale[2];
|
2010-08-15 06:08:57 +02:00
|
|
|
}
|
|
|
|
|
2010-08-16 18:58:22 +02:00
|
|
|
/**
|
|
|
|
* @brief Assumes board is not moving computes biases and variances of sensors
|
|
|
|
* @returns None
|
|
|
|
*
|
|
|
|
* All data is stored in global structures. This function should be called from OP when
|
|
|
|
* aircraft is in stable state and then the data stored to SD card.
|
|
|
|
*/
|
|
|
|
void calibrate_sensors() {
|
2010-08-31 04:11:09 +02:00
|
|
|
int i;
|
2010-09-14 08:42:56 +02:00
|
|
|
int16_t mag_raw[3] = {0,0,0};
|
2010-08-31 04:11:09 +02:00
|
|
|
// local biases for noise analysis
|
|
|
|
float accel_bias[3], gyro_bias[3], mag_bias[3];
|
|
|
|
|
|
|
|
// run few loops to get mean
|
|
|
|
gyro_bias[0] = gyro_bias[1] = gyro_bias[2] = 0;
|
|
|
|
accel_bias[0] = accel_bias[1] = accel_bias[2] = 0;
|
|
|
|
mag_bias[0] = mag_bias[1] = mag_bias[2] = 0;
|
|
|
|
for(i = 0; i < 50; i++) {
|
|
|
|
while( ahrs_state != AHRS_DATA_READY );
|
|
|
|
ahrs_state = AHRS_PROCESSING;
|
|
|
|
downsample_data();
|
|
|
|
gyro_bias[0] += gyro_data.filtered.x;
|
|
|
|
gyro_bias[1] += gyro_data.filtered.y;
|
|
|
|
gyro_bias[2] += gyro_data.filtered.z;
|
|
|
|
accel_bias[0] += accel_data.filtered.x;
|
|
|
|
accel_bias[1] += accel_data.filtered.y;
|
|
|
|
accel_bias[2] += accel_data.filtered.z;
|
2010-09-14 08:42:56 +02:00
|
|
|
#if defined(PIOS_INCLUDE_HMC5843) && defined(PIOS_INCLUDE_I2C)
|
2010-08-31 04:11:09 +02:00
|
|
|
PIOS_HMC5843_ReadMag(mag_raw);
|
2010-09-14 08:42:56 +02:00
|
|
|
#endif
|
2010-08-31 04:11:09 +02:00
|
|
|
mag_bias[0] += mag_raw[0];
|
|
|
|
mag_bias[1] += mag_raw[1];
|
|
|
|
mag_bias[2] += mag_raw[2];
|
|
|
|
|
|
|
|
ahrs_state = AHRS_IDLE;
|
|
|
|
process_spi_request();
|
|
|
|
}
|
|
|
|
gyro_bias[0] /= i;
|
|
|
|
gyro_bias[1] /= i;
|
|
|
|
gyro_bias[2] /= i;
|
|
|
|
accel_bias[0] /= i;
|
|
|
|
accel_bias[1] /= i;
|
|
|
|
accel_bias[2] /= i;
|
|
|
|
mag_bias[0] /= i;
|
|
|
|
mag_bias[1] /= i;
|
|
|
|
mag_bias[2] /= i;
|
|
|
|
|
|
|
|
// more iterations for variance
|
|
|
|
accel_var[0] = accel_var[1] = accel_var[2] = 0;
|
|
|
|
gyro_var[0] = gyro_var[1] = gyro_var[2] = 0;
|
|
|
|
mag_var[0] = mag_var[1] = mag_var[2] = 0;
|
|
|
|
for(i = 0; i < 500; i++)
|
|
|
|
{
|
|
|
|
while( ahrs_state != AHRS_DATA_READY );
|
|
|
|
ahrs_state = AHRS_PROCESSING;
|
|
|
|
downsample_data();
|
|
|
|
gyro_var[0] += (gyro_data.filtered.x - gyro_bias[0]) * (gyro_data.filtered.x - gyro_bias[0]);
|
|
|
|
gyro_var[1] += (gyro_data.filtered.y - gyro_bias[1]) * (gyro_data.filtered.y - gyro_bias[1]);
|
|
|
|
gyro_var[2] += (gyro_data.filtered.z - gyro_bias[2]) * (gyro_data.filtered.z - gyro_bias[2]);
|
|
|
|
accel_var[0] += (accel_data.filtered.x - accel_bias[0]) * (accel_data.filtered.x - accel_bias[0]);
|
|
|
|
accel_var[1] += (accel_data.filtered.y - accel_bias[1]) * (accel_data.filtered.y - accel_bias[1]);
|
|
|
|
accel_var[2] += (accel_data.filtered.z - accel_bias[2]) * (accel_data.filtered.z - accel_bias[2]);
|
2010-09-14 08:42:56 +02:00
|
|
|
#if defined(PIOS_INCLUDE_HMC5843) && defined(PIOS_INCLUDE_I2C)
|
2010-08-31 04:11:09 +02:00
|
|
|
PIOS_HMC5843_ReadMag(mag_raw);
|
2010-09-14 08:42:56 +02:00
|
|
|
#endif
|
2010-08-31 04:11:09 +02:00
|
|
|
mag_var[0] += (mag_raw[0] - mag_bias[0]) * (mag_raw[0] - mag_bias[0]);
|
|
|
|
mag_var[1] += (mag_raw[1] - mag_bias[1]) * (mag_raw[1] - mag_bias[1]);
|
|
|
|
mag_var[2] += (mag_raw[2] - mag_bias[2]) * (mag_raw[2] - mag_bias[2]);
|
|
|
|
ahrs_state = AHRS_IDLE;
|
|
|
|
process_spi_request();
|
|
|
|
}
|
|
|
|
gyro_var[0] /= i;
|
|
|
|
gyro_var[1] /= i;
|
|
|
|
gyro_var[2] /= i;
|
|
|
|
accel_var[0] /= i;
|
|
|
|
accel_var[1] /= i;
|
|
|
|
accel_var[2] /= i;
|
|
|
|
mag_var[0] /= i;
|
|
|
|
mag_var[1] /= i;
|
|
|
|
mag_var[2] /= i;
|
|
|
|
|
|
|
|
float mag_length2 = mag_bias[0] * mag_bias[0] + mag_bias[1] * mag_bias[1] + mag_bias[2] * mag_bias[2];
|
|
|
|
mag_var[0] = mag_var[0] / mag_length2;
|
|
|
|
mag_var[1] = mag_var[1] / mag_length2;
|
|
|
|
mag_var[2] = mag_var[2] / mag_length2;
|
|
|
|
|
|
|
|
if(ahrs_algorithm == INSGPS_Algo)
|
|
|
|
converge_insgps();
|
2010-08-21 23:46:02 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Quickly initialize INS assuming stationary and gravity is down
|
|
|
|
*
|
|
|
|
* Currently this is done iteratively but I'm sure it can be directly computed
|
|
|
|
* when I sit down and work it out
|
|
|
|
*/
|
|
|
|
void converge_insgps()
|
|
|
|
{
|
2010-08-31 04:11:09 +02:00
|
|
|
float pos[3] = {0,0,0}, vel[3] = {0,0,0}, BaroAlt = 0, mag[3], accel[3], temp_gyro[3] = {0, 0, 0};
|
|
|
|
INSGPSInit();
|
|
|
|
INSSetAccelVar(accel_var);
|
|
|
|
INSSetGyroBias(temp_gyro); // set this to zero - crude bias corrected from downsample_data
|
|
|
|
INSSetGyroVar(gyro_var);
|
|
|
|
INSSetMagVar(mag_var);
|
|
|
|
|
|
|
|
float temp_var[3] = {10, 10, 10};
|
|
|
|
INSSetGyroVar(temp_var); // ignore gyro's
|
|
|
|
|
|
|
|
accel[0] = accel_data.filtered.x;
|
|
|
|
accel[1] = accel_data.filtered.y;
|
|
|
|
accel[2] = accel_data.filtered.z;
|
|
|
|
|
|
|
|
// Iteratively constrain pitch and roll while updating yaw to align magnetic axis.
|
|
|
|
for(int i = 0; i < 50; i++)
|
|
|
|
{
|
|
|
|
// This should be done directly but I'm too dumb.
|
|
|
|
float rpy[3];
|
|
|
|
Quaternion2RPY(Nav.q, rpy);
|
|
|
|
rpy[1] = -atan2(accel_data.filtered.x, accel_data.filtered.z) * 180 / M_PI;
|
|
|
|
rpy[0] = -atan2(accel_data.filtered.y, accel_data.filtered.z) * 180 / M_PI;
|
|
|
|
// Get magnetic readings
|
2010-09-14 08:42:56 +02:00
|
|
|
#if defined(PIOS_INCLUDE_HMC5843) && defined(PIOS_INCLUDE_I2C)
|
2010-08-31 04:11:09 +02:00
|
|
|
PIOS_HMC5843_ReadMag(mag_data.raw.axis);
|
2010-09-14 08:42:56 +02:00
|
|
|
#endif
|
2010-08-31 04:11:09 +02:00
|
|
|
mag[0] = -mag_data.raw.axis[1];
|
|
|
|
mag[1] = -mag_data.raw.axis[0];
|
|
|
|
mag[2] = -mag_data.raw.axis[2];
|
|
|
|
|
|
|
|
RPY2Quaternion(rpy,Nav.q);
|
|
|
|
INSPrediction( temp_gyro, accel, 1 / (float) EKF_RATE );
|
|
|
|
FullCorrection(mag,pos,vel,BaroAlt);
|
|
|
|
process_spi_request(); // again we must keep this hear to prevent SPI connection dropping
|
|
|
|
}
|
|
|
|
|
|
|
|
INSSetGyroVar(gyro_var);
|
|
|
|
|
2010-08-16 18:58:22 +02:00
|
|
|
}
|
2010-08-15 06:08:57 +02:00
|
|
|
|
2010-08-16 18:58:24 +02:00
|
|
|
/**
|
|
|
|
* @addtogroup AHRS_SPI SPI Messaging
|
|
|
|
* @{
|
|
|
|
* @brief SPI protocol handling requests for data from OP mainboard
|
|
|
|
*/
|
|
|
|
|
2010-07-04 04:21:47 +02:00
|
|
|
static struct opahrs_msg_v1 link_tx_v1;
|
|
|
|
static struct opahrs_msg_v1 link_rx_v1;
|
|
|
|
static struct opahrs_msg_v1 user_rx_v1;
|
|
|
|
static struct opahrs_msg_v1 user_tx_v1;
|
|
|
|
|
|
|
|
void process_spi_request(void)
|
|
|
|
{
|
2010-08-31 04:11:09 +02:00
|
|
|
bool msg_to_process = FALSE;
|
|
|
|
|
|
|
|
PIOS_IRQ_Disable();
|
|
|
|
/* Figure out if we're in an interesting stable state */
|
|
|
|
switch (lfsm_get_state()) {
|
|
|
|
case LFSM_STATE_USER_BUSY:
|
|
|
|
msg_to_process = TRUE;
|
|
|
|
break;
|
|
|
|
case LFSM_STATE_INACTIVE:
|
|
|
|
/* Queue up a receive buffer */
|
|
|
|
lfsm_user_set_rx_v1 (&user_rx_v1);
|
|
|
|
lfsm_user_done ();
|
|
|
|
break;
|
|
|
|
case LFSM_STATE_STOPPED:
|
|
|
|
/* Get things going */
|
|
|
|
lfsm_set_link_proto_v1 (&link_tx_v1, &link_rx_v1);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
/* Not a stable state */
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
PIOS_IRQ_Enable();
|
|
|
|
|
|
|
|
if (!msg_to_process) {
|
|
|
|
/* Nothing to do */
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
switch (user_rx_v1.payload.user.t) {
|
|
|
|
case OPAHRS_MSG_V1_REQ_RESET:
|
|
|
|
PIOS_DELAY_WaitmS(user_rx_v1.payload.user.v.req.reset.reset_delay_in_ms);
|
|
|
|
PIOS_SYS_Reset();
|
|
|
|
break;
|
|
|
|
case OPAHRS_MSG_V1_REQ_SERIAL:
|
|
|
|
opahrs_msg_v1_init_user_tx (&user_tx_v1, OPAHRS_MSG_V1_RSP_SERIAL);
|
|
|
|
PIOS_SYS_SerialNumberGet((char *)&(user_tx_v1.payload.user.v.rsp.serial.serial_bcd));
|
|
|
|
lfsm_user_set_tx_v1 (&user_tx_v1);
|
|
|
|
break;
|
|
|
|
case OPAHRS_MSG_V1_REQ_ALGORITHM:
|
|
|
|
opahrs_msg_v1_init_user_tx (&user_tx_v1, OPAHRS_MSG_V1_RSP_ALGORITHM);
|
|
|
|
ahrs_algorithm = user_rx_v1.payload.user.v.req.algorithm.algorithm;
|
|
|
|
lfsm_user_set_tx_v1 (&user_tx_v1);
|
|
|
|
break;
|
|
|
|
case OPAHRS_MSG_V1_REQ_NORTH:
|
|
|
|
opahrs_msg_v1_init_user_tx (&user_tx_v1, OPAHRS_MSG_V1_RSP_NORTH);
|
|
|
|
INSSetMagNorth(user_rx_v1.payload.user.v.req.north.Be);
|
2010-09-04 04:19:00 +02:00
|
|
|
INSGPSInit(); // TODO: Better reinitialization when North is finally established
|
2010-08-31 04:11:09 +02:00
|
|
|
lfsm_user_set_tx_v1 (&user_tx_v1);
|
|
|
|
break;
|
|
|
|
case OPAHRS_MSG_V1_REQ_CALIBRATION:
|
|
|
|
if(user_rx_v1.payload.user.v.req.calibration.measure_var == AHRS_MEASURE) {
|
|
|
|
calibration_pending = TRUE;
|
|
|
|
} else if (user_rx_v1.payload.user.v.req.calibration.measure_var == AHRS_SET) {
|
|
|
|
accel_var[0] = user_rx_v1.payload.user.v.req.calibration.accel_var[0];
|
|
|
|
accel_var[1] = user_rx_v1.payload.user.v.req.calibration.accel_var[1];
|
|
|
|
accel_var[2] = user_rx_v1.payload.user.v.req.calibration.accel_var[2];
|
|
|
|
gyro_bias[0] = user_rx_v1.payload.user.v.req.calibration.gyro_bias[0];
|
|
|
|
gyro_bias[1] = user_rx_v1.payload.user.v.req.calibration.gyro_bias[1];
|
|
|
|
gyro_bias[2] = user_rx_v1.payload.user.v.req.calibration.gyro_bias[2];
|
|
|
|
gyro_var[0] = user_rx_v1.payload.user.v.req.calibration.gyro_var[0];
|
|
|
|
gyro_var[1] = user_rx_v1.payload.user.v.req.calibration.gyro_var[1];
|
|
|
|
gyro_var[2] = user_rx_v1.payload.user.v.req.calibration.gyro_var[2];
|
|
|
|
mag_var[0] = user_rx_v1.payload.user.v.req.calibration.mag_var[0];
|
|
|
|
mag_var[1] = user_rx_v1.payload.user.v.req.calibration.mag_var[1];
|
|
|
|
mag_var[2] = user_rx_v1.payload.user.v.req.calibration.mag_var[2];
|
|
|
|
INSSetAccelVar(accel_var);
|
|
|
|
float gyro_bias_ins[3] = {0,0,0};
|
|
|
|
INSSetGyroBias(gyro_bias_ins); //gyro bias corrects in preprocessing
|
|
|
|
INSSetGyroVar(gyro_var);
|
|
|
|
INSSetMagVar(mag_var);
|
|
|
|
}
|
|
|
|
if(user_rx_v1.payload.user.v.req.calibration.measure_var != AHRS_ECHO) {
|
|
|
|
/* if echoing don't set anything */
|
|
|
|
accel_bias[0] = user_rx_v1.payload.user.v.req.calibration.accel_bias[0];
|
|
|
|
accel_bias[1] = user_rx_v1.payload.user.v.req.calibration.accel_bias[1];
|
|
|
|
accel_bias[2] = user_rx_v1.payload.user.v.req.calibration.accel_bias[2];
|
|
|
|
accel_scale[0] = user_rx_v1.payload.user.v.req.calibration.accel_scale[0];
|
|
|
|
accel_scale[1] = user_rx_v1.payload.user.v.req.calibration.accel_scale[1];
|
|
|
|
accel_scale[2] = user_rx_v1.payload.user.v.req.calibration.accel_scale[2];
|
|
|
|
gyro_scale[0] = user_rx_v1.payload.user.v.req.calibration.gyro_scale[0];
|
|
|
|
gyro_scale[1] = user_rx_v1.payload.user.v.req.calibration.gyro_scale[1];
|
|
|
|
gyro_scale[2] = user_rx_v1.payload.user.v.req.calibration.gyro_scale[2];
|
|
|
|
mag_bias[0] = user_rx_v1.payload.user.v.req.calibration.mag_bias[0];
|
|
|
|
mag_bias[1] = user_rx_v1.payload.user.v.req.calibration.mag_bias[1];
|
|
|
|
mag_bias[2] = user_rx_v1.payload.user.v.req.calibration.mag_bias[2];
|
|
|
|
}
|
|
|
|
// echo back the values used
|
|
|
|
opahrs_msg_v1_init_user_tx (&user_tx_v1, OPAHRS_MSG_V1_RSP_CALIBRATION);
|
|
|
|
user_tx_v1.payload.user.v.rsp.calibration.accel_var[0] = accel_var[0];
|
|
|
|
user_tx_v1.payload.user.v.rsp.calibration.accel_var[1] = accel_var[1];
|
|
|
|
user_tx_v1.payload.user.v.rsp.calibration.accel_var[2] = accel_var[2];
|
|
|
|
user_tx_v1.payload.user.v.rsp.calibration.gyro_bias[0] = gyro_bias[0];
|
|
|
|
user_tx_v1.payload.user.v.rsp.calibration.gyro_bias[1] = gyro_bias[1];
|
|
|
|
user_tx_v1.payload.user.v.rsp.calibration.gyro_bias[2] = gyro_bias[2];
|
|
|
|
user_tx_v1.payload.user.v.rsp.calibration.gyro_var[0] = gyro_var[0];
|
|
|
|
user_tx_v1.payload.user.v.rsp.calibration.gyro_var[1] = gyro_var[1];
|
|
|
|
user_tx_v1.payload.user.v.rsp.calibration.gyro_var[2] = gyro_var[2];
|
|
|
|
user_tx_v1.payload.user.v.rsp.calibration.mag_var[0] = mag_var[0];
|
|
|
|
user_tx_v1.payload.user.v.rsp.calibration.mag_var[1] = mag_var[1];
|
|
|
|
user_tx_v1.payload.user.v.rsp.calibration.mag_var[2] = mag_var[2];
|
|
|
|
|
|
|
|
lfsm_user_set_tx_v1 (&user_tx_v1);
|
|
|
|
break;
|
|
|
|
case OPAHRS_MSG_V1_REQ_ATTITUDERAW:
|
|
|
|
opahrs_msg_v1_init_user_tx (&user_tx_v1, OPAHRS_MSG_V1_RSP_ATTITUDERAW);
|
|
|
|
user_tx_v1.payload.user.v.rsp.attituderaw.mags.x = mag_data.raw.axis[0];
|
|
|
|
user_tx_v1.payload.user.v.rsp.attituderaw.mags.y = mag_data.raw.axis[1];
|
|
|
|
user_tx_v1.payload.user.v.rsp.attituderaw.mags.z = mag_data.raw.axis[2];
|
|
|
|
|
|
|
|
user_tx_v1.payload.user.v.rsp.attituderaw.gyros.x = gyro_data.raw.x;
|
|
|
|
user_tx_v1.payload.user.v.rsp.attituderaw.gyros.y = gyro_data.raw.y;
|
|
|
|
user_tx_v1.payload.user.v.rsp.attituderaw.gyros.z = gyro_data.raw.z;
|
|
|
|
|
|
|
|
user_tx_v1.payload.user.v.rsp.attituderaw.gyros_filtered.x = gyro_data.filtered.x;
|
|
|
|
user_tx_v1.payload.user.v.rsp.attituderaw.gyros_filtered.y = gyro_data.filtered.y;
|
|
|
|
user_tx_v1.payload.user.v.rsp.attituderaw.gyros_filtered.z = gyro_data.filtered.z;
|
|
|
|
|
|
|
|
user_tx_v1.payload.user.v.rsp.attituderaw.gyros.xy_temp = gyro_data.temp.xy;
|
|
|
|
user_tx_v1.payload.user.v.rsp.attituderaw.gyros.z_temp = gyro_data.temp.z;
|
|
|
|
|
|
|
|
user_tx_v1.payload.user.v.rsp.attituderaw.accels.x = accel_data.raw.x;
|
|
|
|
user_tx_v1.payload.user.v.rsp.attituderaw.accels.y = accel_data.raw.y;
|
|
|
|
user_tx_v1.payload.user.v.rsp.attituderaw.accels.z = accel_data.raw.z;
|
|
|
|
|
|
|
|
user_tx_v1.payload.user.v.rsp.attituderaw.accels_filtered.x = accel_data.filtered.x;
|
|
|
|
user_tx_v1.payload.user.v.rsp.attituderaw.accels_filtered.y = accel_data.filtered.y;
|
|
|
|
user_tx_v1.payload.user.v.rsp.attituderaw.accels_filtered.z = accel_data.filtered.z;
|
|
|
|
|
|
|
|
lfsm_user_set_tx_v1 (&user_tx_v1);
|
|
|
|
break;
|
|
|
|
case OPAHRS_MSG_V1_REQ_UPDATE:
|
|
|
|
// process incoming data
|
|
|
|
opahrs_msg_v1_init_user_tx (&user_tx_v1, OPAHRS_MSG_V1_RSP_UPDATE);
|
|
|
|
if(user_rx_v1.payload.user.v.req.update.barometer.updated)
|
|
|
|
{
|
|
|
|
altitude_data.altitude = user_rx_v1.payload.user.v.req.update.barometer.altitude;
|
|
|
|
altitude_data.updated = user_rx_v1.payload.user.v.req.update.barometer.updated;
|
|
|
|
}
|
|
|
|
if(user_rx_v1.payload.user.v.req.update.gps.updated)
|
|
|
|
{
|
|
|
|
gps_data.updated = true;
|
|
|
|
gps_data.NED[0] = user_rx_v1.payload.user.v.req.update.gps.NED[0];
|
|
|
|
gps_data.NED[1] = user_rx_v1.payload.user.v.req.update.gps.NED[1];
|
|
|
|
gps_data.NED[2] = user_rx_v1.payload.user.v.req.update.gps.NED[2];
|
|
|
|
gps_data.heading = user_rx_v1.payload.user.v.req.update.gps.heading;
|
|
|
|
gps_data.groundspeed = user_rx_v1.payload.user.v.req.update.gps.groundspeed;
|
|
|
|
gps_data.quality = user_rx_v1.payload.user.v.req.update.gps.quality;
|
|
|
|
}
|
|
|
|
|
|
|
|
// send out attitude/position estimate
|
|
|
|
user_tx_v1.payload.user.v.rsp.update.quaternion.q1 = attitude_data.quaternion.q1;
|
|
|
|
user_tx_v1.payload.user.v.rsp.update.quaternion.q2 = attitude_data.quaternion.q2;
|
|
|
|
user_tx_v1.payload.user.v.rsp.update.quaternion.q3 = attitude_data.quaternion.q3;
|
|
|
|
user_tx_v1.payload.user.v.rsp.update.quaternion.q4 = attitude_data.quaternion.q4;
|
|
|
|
|
|
|
|
// TODO: separate this from INSGPS
|
|
|
|
user_tx_v1.payload.user.v.rsp.update.NED[0] = Nav.Pos[0];
|
|
|
|
user_tx_v1.payload.user.v.rsp.update.NED[1] = Nav.Pos[1];
|
|
|
|
user_tx_v1.payload.user.v.rsp.update.NED[2] = Nav.Pos[2];
|
|
|
|
user_tx_v1.payload.user.v.rsp.update.Vel[0] = Nav.Vel[0];
|
|
|
|
user_tx_v1.payload.user.v.rsp.update.Vel[1] = Nav.Vel[1];
|
|
|
|
user_tx_v1.payload.user.v.rsp.update.Vel[2] = Nav.Vel[2];
|
|
|
|
|
|
|
|
// compute the idle fraction
|
2010-09-14 09:03:36 +02:00
|
|
|
user_tx_v1.payload.user.v.rsp.update.load = ((float) running_counts / (float) (idle_counts+running_counts)) * 100;
|
2010-09-16 03:54:21 +02:00
|
|
|
user_tx_v1.payload.user.v.rsp.update.idle_time = idle_counts / (TIMER_RATE / 10000);
|
|
|
|
user_tx_v1.payload.user.v.rsp.update.run_time = running_counts / (TIMER_RATE / 10000);
|
2010-08-31 04:11:09 +02:00
|
|
|
|
|
|
|
lfsm_user_set_tx_v1 (&user_tx_v1);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Finished processing the received message, requeue it */
|
2010-07-04 04:21:47 +02:00
|
|
|
lfsm_user_set_rx_v1 (&user_rx_v1);
|
|
|
|
lfsm_user_done ();
|
2010-02-28 13:10:48 +01:00
|
|
|
}
|
2010-08-15 06:08:57 +02:00
|
|
|
/**
|
2010-08-16 18:58:24 +02:00
|
|
|
* @}
|
2010-08-15 06:08:57 +02:00
|
|
|
*/
|
2010-08-16 18:58:24 +02:00
|
|
|
|
2010-08-15 06:08:57 +02:00
|
|
|
|
|
|
|
|