2011-12-12 21:39:38 +01:00
|
|
|
/**
|
|
|
|
******************************************************************************
|
|
|
|
* @addtogroup OpenPilotModules OpenPilot Modules
|
|
|
|
* @{
|
|
|
|
* @addtogroup Sensors
|
2013-05-18 14:17:26 +02:00
|
|
|
* @brief Acquires sensor data
|
2013-05-20 10:33:02 +02:00
|
|
|
* Specifically updates the the @ref GyroSensor, @ref AccelSensor, and @ref MagSensor objects
|
2011-12-12 21:39:38 +01:00
|
|
|
* @{
|
|
|
|
*
|
|
|
|
* @file sensors.c
|
|
|
|
* @author The OpenPilot Team, http://www.openpilot.org Copyright (C) 2010.
|
|
|
|
* @brief Module to handle all comms to the AHRS on a periodic basis.
|
|
|
|
*
|
|
|
|
* @see The GNU Public License (GPL) Version 3
|
|
|
|
*
|
|
|
|
******************************************************************************/
|
|
|
|
/*
|
|
|
|
* This program is free software; you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation; either version 3 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful, but
|
|
|
|
* WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
|
|
|
|
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
|
|
|
* for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License along
|
|
|
|
* with this program; if not, write to the Free Software Foundation, Inc.,
|
|
|
|
* 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Input objects: None, takes sensor data via pios
|
2013-05-20 10:33:02 +02:00
|
|
|
* Output objects: @ref GyroSensor @ref AccelSensor @ref MagSensor
|
2011-12-12 21:39:38 +01:00
|
|
|
*
|
|
|
|
* 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-04-20 23:02:51 +02:00
|
|
|
#include <openpilot.h>
|
2014-12-23 19:14:31 +01:00
|
|
|
#include <pios_sensors.h>
|
2013-04-20 23:02:51 +02:00
|
|
|
#include <homelocation.h>
|
2014-12-29 12:28:42 +01:00
|
|
|
|
2013-05-20 10:33:02 +02:00
|
|
|
#include <magsensor.h>
|
2013-05-18 19:36:45 +02:00
|
|
|
#include <accelsensor.h>
|
|
|
|
#include <gyrosensor.h>
|
2014-12-29 12:28:42 +01:00
|
|
|
#include <barosensor.h>
|
|
|
|
#include <flightstatus.h>
|
|
|
|
|
2013-04-20 23:02:51 +02:00
|
|
|
#include <attitudesettings.h>
|
|
|
|
#include <revocalibration.h>
|
2013-12-26 16:55:57 +01:00
|
|
|
#include <accelgyrosettings.h>
|
2014-12-29 12:28:42 +01:00
|
|
|
#include <revosettings.h>
|
|
|
|
|
|
|
|
#include <mathmisc.h>
|
2013-05-02 23:31:14 +02:00
|
|
|
#include <taskinfo.h>
|
2014-10-14 05:22:36 +02:00
|
|
|
#include <pios_math.h>
|
2014-12-29 12:28:42 +01:00
|
|
|
#include <pios_constants.h>
|
2013-04-20 23:02:51 +02:00
|
|
|
#include <CoordinateConversions.h>
|
2012-03-21 08:00:32 +01:00
|
|
|
#include <pios_board_info.h>
|
2014-12-23 19:14:31 +01:00
|
|
|
#include <string.h>
|
2014-12-29 13:03:54 +01:00
|
|
|
|
2011-12-12 21:39:38 +01:00
|
|
|
// Private constants
|
2014-12-23 19:14:31 +01:00
|
|
|
#define STACK_SIZE_BYTES 1000
|
|
|
|
#define TASK_PRIORITY (tskIDLE_PRIORITY + 3)
|
|
|
|
|
|
|
|
#define MAX_SENSORS_PER_INSTANCE 2
|
|
|
|
#ifdef PIOS_INCLUDE_WDG
|
|
|
|
#define RELOAD_WDG() PIOS_WDG_UpdateFlag(PIOS_WDG_SENSORS)
|
|
|
|
#define REGISTER_WDG() PIOS_WDG_RegisterFlag(PIOS_WDG_SENSORS)
|
|
|
|
#else
|
|
|
|
#define RELOAD_WDG()
|
|
|
|
#define REGISTER_WDG()
|
|
|
|
#endif
|
2014-10-14 05:22:36 +02:00
|
|
|
|
2014-12-29 12:28:42 +01:00
|
|
|
static const TickType_t sensor_period_ticks = ((uint32_t)1000.0f / PIOS_SENSOR_RATE) / portTICK_RATE_MS;
|
|
|
|
|
|
|
|
// Interval in number of sample to recalculate temp bias
|
|
|
|
#define TEMP_CALIB_INTERVAL 30
|
|
|
|
|
|
|
|
// LPF
|
|
|
|
#define TEMP_DT_GYRO_ACCEL (1.0f / PIOS_SENSOR_RATE)
|
|
|
|
#define TEMP_LPF_FC_GYRO_ACCEL 5.0f
|
|
|
|
static const float temp_alpha_gyro_accel = LPF_ALPHA(TEMP_DT_GYRO_ACCEL, TEMP_LPF_FC_GYRO_ACCEL);
|
2011-12-12 21:39:38 +01:00
|
|
|
|
2014-10-07 11:23:26 +02:00
|
|
|
// Interval in number of sample to recalculate temp bias
|
2014-12-29 12:28:42 +01:00
|
|
|
#define BARO_TEMP_CALIB_INTERVAL 10
|
2014-10-14 05:22:36 +02:00
|
|
|
|
|
|
|
// LPF
|
2014-12-29 12:28:42 +01:00
|
|
|
#define TEMP_DT_BARO (1.0f / 120.0f)
|
|
|
|
#define TEMP_LPF_FC_BARO 5.0f
|
|
|
|
static const float temp_alpha_baro = TEMP_DT_BARO / (TEMP_DT_BARO + 1.0f / (2.0f * M_PI_F * TEMP_LPF_FC_BARO));
|
|
|
|
|
2014-10-07 11:23:26 +02:00
|
|
|
|
2014-12-29 12:28:42 +01:00
|
|
|
#define ZERO_ROT_ANGLE 0.00001f
|
2011-12-12 21:39:38 +01:00
|
|
|
// Private types
|
2014-12-23 19:14:31 +01:00
|
|
|
typedef struct {
|
|
|
|
// used to accumulate all samples in a task iteration
|
|
|
|
Vector3i32 accum[2];
|
|
|
|
int32_t temperature;
|
|
|
|
uint32_t count;
|
|
|
|
} sensor_fetch_context;
|
|
|
|
|
|
|
|
#define MAX_SENSOR_DATA_SIZE (sizeof(PIOS_SENSORS_3Axis_SensorsWithTemp) + MAX_SENSORS_PER_INSTANCE * sizeof(Vector3i16))
|
|
|
|
typedef union {
|
|
|
|
PIOS_SENSORS_3Axis_SensorsWithTemp sensorSample3Axis;
|
|
|
|
PIOS_SENSORS_1Axis_SensorsWithTemp sensorSample1Axis;
|
|
|
|
} sensor_data;
|
|
|
|
|
2014-10-07 11:40:39 +02:00
|
|
|
#define PIOS_INSTRUMENT_MODULE
|
|
|
|
#include <pios_instrumentation_helper.h>
|
2011-12-12 21:39:38 +01:00
|
|
|
|
2014-12-27 18:50:03 +01:00
|
|
|
PERF_DEFINE_COUNTER(counterAccelSamples);
|
|
|
|
PERF_DEFINE_COUNTER(counterAccelPeriod);
|
|
|
|
PERF_DEFINE_COUNTER(counterMagPeriod);
|
|
|
|
PERF_DEFINE_COUNTER(counterBaroPeriod);
|
|
|
|
PERF_DEFINE_COUNTER(counterSensorPeriod);
|
2014-12-29 13:03:54 +01:00
|
|
|
PERF_DEFINE_COUNTER(counterSensorResets);
|
2014-12-27 18:50:03 +01:00
|
|
|
|
2011-12-12 21:39:38 +01:00
|
|
|
// Private functions
|
|
|
|
static void SensorsTask(void *parameters);
|
2013-05-18 14:17:26 +02:00
|
|
|
static void settingsUpdatedCb(UAVObjEvent *objEv);
|
2011-12-12 21:39:38 +01:00
|
|
|
|
2014-12-23 19:14:31 +01:00
|
|
|
static void accumulateSamples(sensor_fetch_context *sensor_context, sensor_data *sample);
|
2014-12-29 12:28:42 +01:00
|
|
|
static void processSamples3d(sensor_fetch_context *sensor_context, const PIOS_SENSORS_Instance *sensor);
|
|
|
|
static void processSamples1d(PIOS_SENSORS_1Axis_SensorsWithTemp *sample, const PIOS_SENSORS_Instance *sensor);
|
|
|
|
|
2014-12-23 19:14:31 +01:00
|
|
|
static void clearContext(sensor_fetch_context *sensor_context);
|
|
|
|
|
|
|
|
static void handleAccel(float *samples, float temperature);
|
|
|
|
static void handleGyro(float *samples, float temperature);
|
|
|
|
static void handleMag(float *samples, float temperature);
|
2014-12-29 12:28:42 +01:00
|
|
|
static void handleBaro(float sample, float temperature);
|
2014-12-23 19:14:31 +01:00
|
|
|
|
|
|
|
static void updateAccelTempBias(float temperature);
|
|
|
|
static void updateGyroTempBias(float temperature);
|
2014-12-29 12:28:42 +01:00
|
|
|
static void updateBaroTempBias(float temperature);
|
2014-12-23 19:14:31 +01:00
|
|
|
|
2012-07-25 18:23:27 +02:00
|
|
|
// Private variables
|
2014-12-23 19:14:31 +01:00
|
|
|
static sensor_data *source_data;
|
2012-07-25 18:23:27 +02:00
|
|
|
static xTaskHandle sensorsTaskHandle;
|
|
|
|
RevoCalibrationData cal;
|
2013-12-26 16:55:57 +01:00
|
|
|
AccelGyroSettingsData agcal;
|
2012-07-25 18:23:27 +02:00
|
|
|
|
2011-12-12 21:39:38 +01:00
|
|
|
// These values are initialized by settings but can be updated by the attitude algorithm
|
|
|
|
|
2013-05-18 14:17:26 +02:00
|
|
|
static float mag_bias[3] = { 0, 0, 0 };
|
2014-05-08 23:18:38 +02:00
|
|
|
static float mag_transform[3][3] = {
|
|
|
|
{ 1, 0, 0 }, { 0, 1, 0 }, { 0, 0, 1 }
|
|
|
|
};
|
2014-12-23 19:14:31 +01:00
|
|
|
|
2014-12-29 12:28:42 +01:00
|
|
|
// Variables used to handle accel/gyro temperature bias
|
2014-02-06 00:57:57 +01:00
|
|
|
static volatile bool gyro_temp_calibrated = false;
|
2014-01-31 21:01:09 +01:00
|
|
|
static volatile bool accel_temp_calibrated = false;
|
2013-07-17 15:28:55 +02:00
|
|
|
|
2014-10-11 23:54:21 +02:00
|
|
|
static float accel_temperature = NAN;
|
|
|
|
static float gyro_temperature = NAN;
|
2014-10-07 11:23:26 +02:00
|
|
|
static float accel_temp_bias[3] = { 0 };
|
|
|
|
static float gyro_temp_bias[3] = { 0 };
|
2014-12-23 19:14:31 +01:00
|
|
|
static uint8_t accel_temp_calibration_count = 0;
|
|
|
|
static uint8_t gyro_temp_calibration_count = 0;
|
2014-10-07 11:23:26 +02:00
|
|
|
|
2013-05-18 14:17:26 +02:00
|
|
|
static float R[3][3] = {
|
|
|
|
{ 0 }
|
|
|
|
};
|
2014-12-29 12:28:42 +01:00
|
|
|
// Variables used to handle baro temperature bias
|
|
|
|
static RevoSettingsBaroTempCorrectionPolynomialData baroCorrection;
|
|
|
|
static RevoSettingsBaroTempCorrectionExtentData baroCorrectionExtent;
|
|
|
|
static volatile bool baro_temp_correction_enabled;
|
|
|
|
static float baro_temp_bias = 0;
|
|
|
|
static float baro_temperature = NAN;
|
|
|
|
static uint8_t baro_temp_calibration_count = 0;
|
|
|
|
|
2012-03-26 05:25:52 +02:00
|
|
|
static int8_t rotate = 0;
|
|
|
|
|
2011-12-12 21:39:38 +01:00
|
|
|
/**
|
|
|
|
* Initialise the module. Called before the start function
|
|
|
|
* \returns 0 on success or -1 if initialisation failed
|
|
|
|
*/
|
|
|
|
int32_t SensorsInitialize(void)
|
|
|
|
{
|
2014-12-23 19:14:31 +01:00
|
|
|
source_data = (sensor_data *)pios_malloc(MAX_SENSOR_DATA_SIZE);
|
2013-05-18 19:36:45 +02:00
|
|
|
GyroSensorInitialize();
|
|
|
|
AccelSensorInitialize();
|
2013-05-20 10:33:02 +02:00
|
|
|
MagSensorInitialize();
|
2014-12-29 12:28:42 +01:00
|
|
|
BaroSensorInitialize();
|
2013-05-18 14:17:26 +02:00
|
|
|
RevoCalibrationInitialize();
|
2014-12-29 12:28:42 +01:00
|
|
|
RevoSettingsInitialize();
|
2013-05-18 14:17:26 +02:00
|
|
|
AttitudeSettingsInitialize();
|
2013-12-26 16:55:57 +01:00
|
|
|
AccelGyroSettingsInitialize();
|
2012-03-26 05:25:52 +02:00
|
|
|
|
2013-05-18 14:17:26 +02:00
|
|
|
rotate = 0;
|
2011-12-24 22:56:37 +01:00
|
|
|
|
2014-12-29 12:28:42 +01:00
|
|
|
RevoSettingsConnectCallback(&settingsUpdatedCb);
|
2013-05-18 14:17:26 +02:00
|
|
|
RevoCalibrationConnectCallback(&settingsUpdatedCb);
|
|
|
|
AttitudeSettingsConnectCallback(&settingsUpdatedCb);
|
2013-12-26 16:55:57 +01:00
|
|
|
AccelGyroSettingsConnectCallback(&settingsUpdatedCb);
|
2014-12-29 13:03:54 +01:00
|
|
|
|
2013-05-18 14:17:26 +02:00
|
|
|
return 0;
|
2011-12-12 21:39:38 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Start the task. Expects all objects to be initialized by this point.
|
|
|
|
* \returns 0 on success or -1 if initialisation failed
|
|
|
|
*/
|
|
|
|
int32_t SensorsStart(void)
|
|
|
|
{
|
2013-05-18 14:17:26 +02:00
|
|
|
// Start main task
|
2014-06-18 01:47:43 +02:00
|
|
|
xTaskCreate(SensorsTask, "Sensors", STACK_SIZE_BYTES / 4, NULL, TASK_PRIORITY, &sensorsTaskHandle);
|
2013-05-18 14:17:26 +02:00
|
|
|
PIOS_TASK_MONITOR_RegisterTask(TASKINFO_RUNNING_SENSORS, sensorsTaskHandle);
|
2014-12-23 19:14:31 +01:00
|
|
|
REGISTER_WDG();
|
2013-05-18 14:17:26 +02:00
|
|
|
return 0;
|
2011-12-12 21:39:38 +01:00
|
|
|
}
|
|
|
|
|
2013-06-04 05:37:40 +02:00
|
|
|
MODULE_INITCALL(SensorsInitialize, SensorsStart);
|
2011-12-12 21:39:38 +01:00
|
|
|
|
|
|
|
int32_t accel_test;
|
|
|
|
int32_t gyro_test;
|
|
|
|
int32_t mag_test;
|
2013-05-18 14:17:26 +02:00
|
|
|
// int32_t pressure_test;
|
2011-12-12 21:39:38 +01:00
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The sensor task. This polls the gyros at 500 Hz and pumps that data to
|
|
|
|
* stabilization and to the attitude loop
|
2013-05-18 14:17:26 +02:00
|
|
|
*
|
2011-12-12 21:39:38 +01:00
|
|
|
*/
|
2012-01-28 16:41:03 +01:00
|
|
|
|
|
|
|
uint32_t sensor_dt_us;
|
2013-05-05 09:02:24 +02:00
|
|
|
static void SensorsTask(__attribute__((unused)) void *parameters)
|
2011-12-12 21:39:38 +01:00
|
|
|
{
|
2013-05-18 14:17:26 +02:00
|
|
|
portTickType lastSysTime;
|
2014-12-23 19:14:31 +01:00
|
|
|
sensor_fetch_context sensor_context;
|
|
|
|
bool error = false;
|
|
|
|
const PIOS_SENSORS_Instance *sensors_list = PIOS_SENSORS_GetList();
|
|
|
|
PIOS_SENSORS_Instance *sensor;
|
2011-12-12 21:39:38 +01:00
|
|
|
|
2013-05-18 14:17:26 +02:00
|
|
|
AlarmsClear(SYSTEMALARMS_ALARM_SENSORS);
|
2014-12-23 19:14:31 +01:00
|
|
|
settingsUpdatedCb(NULL);
|
|
|
|
|
2014-12-27 18:50:03 +01:00
|
|
|
// Performance counters
|
|
|
|
PERF_INIT_COUNTER(counterAccelSamples, 0x53000001);
|
|
|
|
PERF_INIT_COUNTER(counterAccelPeriod, 0x53000002);
|
|
|
|
PERF_INIT_COUNTER(counterMagPeriod, 0x53000003);
|
|
|
|
PERF_INIT_COUNTER(counterBaroPeriod, 0x53000004);
|
|
|
|
PERF_INIT_COUNTER(counterSensorPeriod, 0x53000005);
|
2014-12-29 13:03:54 +01:00
|
|
|
PERF_INIT_COUNTER(counterSensorResets, 0x53000006);
|
2014-12-27 18:50:03 +01:00
|
|
|
|
2014-12-23 19:14:31 +01:00
|
|
|
// Test sensors
|
|
|
|
bool sensors_test = true;
|
|
|
|
uint8_t count = 0;
|
|
|
|
LL_FOREACH((PIOS_SENSORS_Instance *)sensors_list, sensor) {
|
|
|
|
sensors_test &= PIOS_SENSORS_Test(sensor);
|
|
|
|
count++;
|
2013-05-18 14:17:26 +02:00
|
|
|
}
|
2012-03-21 08:00:32 +01:00
|
|
|
|
2014-12-23 19:14:31 +01:00
|
|
|
PIOS_Assert(count);
|
|
|
|
RELOAD_WDG();
|
|
|
|
if (!sensors_test) {
|
2013-05-18 14:17:26 +02:00
|
|
|
AlarmsSet(SYSTEMALARMS_ALARM_SENSORS, SYSTEMALARMS_ALARM_CRITICAL);
|
|
|
|
while (1) {
|
|
|
|
vTaskDelay(10);
|
|
|
|
}
|
|
|
|
}
|
2014-12-23 19:14:31 +01:00
|
|
|
|
2013-05-18 14:17:26 +02:00
|
|
|
// Main task loop
|
|
|
|
lastSysTime = xTaskGetTickCount();
|
2014-12-29 13:03:54 +01:00
|
|
|
uint32_t reset_counter = 0;
|
2014-12-23 19:14:31 +01:00
|
|
|
|
2013-05-18 14:17:26 +02:00
|
|
|
while (1) {
|
|
|
|
// TODO: add timeouts to the sensor reads and set an error if the fail
|
|
|
|
if (error) {
|
2014-12-23 19:14:31 +01:00
|
|
|
RELOAD_WDG();
|
2013-05-18 14:17:26 +02:00
|
|
|
lastSysTime = xTaskGetTickCount();
|
2014-12-23 19:14:31 +01:00
|
|
|
vTaskDelayUntil(&lastSysTime, sensor_period_ticks);
|
2013-05-18 14:17:26 +02:00
|
|
|
AlarmsSet(SYSTEMALARMS_ALARM_SENSORS, SYSTEMALARMS_ALARM_CRITICAL);
|
|
|
|
error = false;
|
|
|
|
} else {
|
|
|
|
AlarmsClear(SYSTEMALARMS_ALARM_SENSORS);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-12-23 19:14:31 +01:00
|
|
|
// reset the fetch context
|
|
|
|
clearContext(&sensor_context);
|
|
|
|
LL_FOREACH((PIOS_SENSORS_Instance *)sensors_list, sensor) {
|
|
|
|
// we will wait on the sensor that's marked as primary( that means the sensor with higher sample rate)
|
2014-12-29 12:28:42 +01:00
|
|
|
bool is_primary = (sensor->type & PIOS_SENSORS_TYPE_3AXIS_ACCEL);
|
2013-05-18 14:17:26 +02:00
|
|
|
|
2014-12-23 19:14:31 +01:00
|
|
|
if (!sensor->driver->is_polled) {
|
|
|
|
const QueueHandle_t queue = PIOS_SENSORS_GetQueue(sensor);
|
|
|
|
while (xQueueReceive(queue,
|
|
|
|
(void *)source_data,
|
|
|
|
(is_primary && !sensor_context.count) ? sensor_period_ticks : 0) == pdTRUE) {
|
|
|
|
accumulateSamples(&sensor_context, source_data);
|
2013-05-18 14:17:26 +02:00
|
|
|
}
|
2014-12-23 19:14:31 +01:00
|
|
|
if (sensor_context.count) {
|
2014-12-29 12:28:42 +01:00
|
|
|
processSamples3d(&sensor_context, sensor);
|
2014-12-23 19:14:31 +01:00
|
|
|
clearContext(&sensor_context);
|
|
|
|
} else if (is_primary) {
|
2014-12-29 13:03:54 +01:00
|
|
|
PIOS_SENSOR_Reset(sensor);
|
|
|
|
reset_counter++;
|
|
|
|
PERF_TRACK_VALUE(counterSensorResets, reset_counter);
|
2013-05-18 14:17:26 +02:00
|
|
|
error = true;
|
|
|
|
}
|
2014-12-23 19:14:31 +01:00
|
|
|
} else {
|
|
|
|
if (PIOS_SENSORS_Poll(sensor)) {
|
2014-12-27 18:49:08 +01:00
|
|
|
PIOS_SENSOR_Fetch(sensor, (void *)source_data, MAX_SENSORS_PER_INSTANCE);
|
2014-12-29 12:28:42 +01:00
|
|
|
if (sensor->type & PIOS_SENSORS_TYPE_3D) {
|
|
|
|
accumulateSamples(&sensor_context, source_data);
|
|
|
|
processSamples3d(&sensor_context, sensor);
|
|
|
|
} else {
|
|
|
|
processSamples1d(&source_data->sensorSample1Axis, sensor);
|
|
|
|
}
|
2014-12-23 19:14:31 +01:00
|
|
|
clearContext(&sensor_context);
|
2013-05-18 14:17:26 +02:00
|
|
|
}
|
2014-12-23 19:14:31 +01:00
|
|
|
}
|
|
|
|
}
|
2014-12-27 18:50:03 +01:00
|
|
|
PERF_MEASURE_PERIOD(counterSensorPeriod);
|
2014-12-23 19:14:31 +01:00
|
|
|
RELOAD_WDG();
|
|
|
|
vTaskDelayUntil(&lastSysTime, sensor_period_ticks);
|
|
|
|
}
|
|
|
|
}
|
2014-10-07 11:40:39 +02:00
|
|
|
|
2014-12-23 19:14:31 +01:00
|
|
|
static void clearContext(sensor_fetch_context *sensor_context)
|
|
|
|
{
|
|
|
|
// clear the context once it has finished
|
|
|
|
for (uint32_t i = 0; i < MAX_SENSORS_PER_INSTANCE; i++) {
|
|
|
|
sensor_context->accum[i].x = 0;
|
|
|
|
sensor_context->accum[i].y = 0;
|
|
|
|
sensor_context->accum[i].z = 0;
|
|
|
|
}
|
|
|
|
sensor_context->temperature = 0;
|
|
|
|
sensor_context->count = 0;
|
|
|
|
}
|
2013-05-18 14:17:26 +02:00
|
|
|
|
2014-12-23 19:14:31 +01:00
|
|
|
static void accumulateSamples(sensor_fetch_context *sensor_context, sensor_data *sample)
|
|
|
|
{
|
|
|
|
for (uint32_t i = 0; (i < MAX_SENSORS_PER_INSTANCE) && (i < sample->sensorSample3Axis.count); i++) {
|
|
|
|
sensor_context->accum[i].x += sample->sensorSample3Axis.sample[i].x;
|
|
|
|
sensor_context->accum[i].y += sample->sensorSample3Axis.sample[i].y;
|
|
|
|
sensor_context->accum[i].z += sample->sensorSample3Axis.sample[i].z;
|
|
|
|
}
|
|
|
|
sensor_context->temperature += sample->sensorSample3Axis.temperature;
|
|
|
|
sensor_context->count++;
|
|
|
|
}
|
2013-05-18 14:17:26 +02:00
|
|
|
|
2014-12-29 12:28:42 +01:00
|
|
|
static void processSamples3d(sensor_fetch_context *sensor_context, const PIOS_SENSORS_Instance *sensor)
|
2014-12-23 19:14:31 +01:00
|
|
|
{
|
|
|
|
float samples[3];
|
|
|
|
float temperature;
|
|
|
|
float scales[MAX_SENSORS_PER_INSTANCE];
|
|
|
|
|
|
|
|
PIOS_SENSORS_GetScales(sensor, scales, MAX_SENSORS_PER_INSTANCE);
|
|
|
|
float inv_count = 1.0f / (float)sensor_context->count;
|
2014-12-29 12:28:42 +01:00
|
|
|
if ((sensor->type & PIOS_SENSORS_TYPE_3AXIS_ACCEL) ||
|
2014-12-23 19:14:31 +01:00
|
|
|
(sensor->type == PIOS_SENSORS_TYPE_3AXIS_MAG)) {
|
|
|
|
float t = inv_count * scales[0];
|
|
|
|
samples[0] = ((float)sensor_context->accum[0].x * t);
|
|
|
|
samples[1] = ((float)sensor_context->accum[0].y * t);
|
|
|
|
samples[2] = ((float)sensor_context->accum[0].z * t);
|
|
|
|
temperature = (float)sensor_context->temperature * inv_count * 0.01f;
|
|
|
|
if (sensor->type == PIOS_SENSORS_TYPE_3AXIS_MAG) {
|
|
|
|
handleMag(samples, temperature);
|
2014-12-27 18:50:03 +01:00
|
|
|
PERF_MEASURE_PERIOD(counterMagPeriod);
|
2014-12-23 19:14:31 +01:00
|
|
|
return;
|
|
|
|
} else {
|
2014-12-27 18:50:03 +01:00
|
|
|
PERF_TRACK_VALUE(counterAccelSamples, sensor_context->count);
|
|
|
|
PERF_MEASURE_PERIOD(counterAccelPeriod);
|
2014-12-23 19:14:31 +01:00
|
|
|
handleAccel(samples, temperature);
|
2013-05-18 14:17:26 +02:00
|
|
|
}
|
2014-12-23 19:14:31 +01:00
|
|
|
}
|
2013-05-18 14:17:26 +02:00
|
|
|
|
2014-12-29 12:28:42 +01:00
|
|
|
if (sensor->type & PIOS_SENSORS_TYPE_3AXIS_GYRO) {
|
2014-12-23 19:14:31 +01:00
|
|
|
uint8_t index = 0;
|
|
|
|
if (sensor->type == PIOS_SENSORS_TYPE_3AXIS_GYRO_ACCEL) {
|
|
|
|
index = 1;
|
2014-10-11 23:54:21 +02:00
|
|
|
}
|
2014-12-23 19:14:31 +01:00
|
|
|
float t = inv_count * scales[index];
|
|
|
|
samples[0] = ((float)sensor_context->accum[index].x * t);
|
|
|
|
samples[1] = ((float)sensor_context->accum[index].y * t);
|
|
|
|
samples[2] = ((float)sensor_context->accum[index].z * t);
|
|
|
|
temperature = (float)sensor_context->temperature * inv_count * 0.01f;
|
|
|
|
handleGyro(samples, temperature);
|
|
|
|
return;
|
|
|
|
}
|
2014-12-29 12:28:42 +01:00
|
|
|
}
|
2014-10-07 11:23:26 +02:00
|
|
|
|
2014-12-29 12:28:42 +01:00
|
|
|
static void processSamples1d(PIOS_SENSORS_1Axis_SensorsWithTemp *sample, const PIOS_SENSORS_Instance *sensor)
|
|
|
|
{
|
|
|
|
switch (sensor->type) {
|
|
|
|
case PIOS_SENSORS_TYPE_1AXIS_BARO:
|
2014-12-27 18:50:03 +01:00
|
|
|
PERF_MEASURE_PERIOD(counterBaroPeriod);
|
2014-12-29 12:28:42 +01:00
|
|
|
handleBaro(sample->sample, sample->temperature);
|
|
|
|
return;
|
|
|
|
|
|
|
|
default:
|
|
|
|
PIOS_Assert(0);
|
2014-12-23 19:14:31 +01:00
|
|
|
}
|
|
|
|
}
|
2014-10-07 11:23:26 +02:00
|
|
|
|
2014-12-29 12:28:42 +01:00
|
|
|
static void handleAccel(float *samples, float temperature)
|
2014-12-23 19:14:31 +01:00
|
|
|
{
|
|
|
|
AccelSensorData accelSensorData;
|
2014-10-07 11:23:26 +02:00
|
|
|
|
2014-12-23 19:14:31 +01:00
|
|
|
updateAccelTempBias(temperature);
|
|
|
|
float accels_out[3] = { samples[0] * agcal.accel_scale.X - agcal.accel_bias.X - accel_temp_bias[0],
|
|
|
|
samples[1] * agcal.accel_scale.Y - agcal.accel_bias.Y - accel_temp_bias[1],
|
|
|
|
samples[2] * agcal.accel_scale.Z - agcal.accel_bias.Z - accel_temp_bias[2] };
|
2014-01-31 21:01:09 +01:00
|
|
|
|
2014-12-23 19:14:31 +01:00
|
|
|
rot_mult(R, accels_out, samples);
|
|
|
|
accelSensorData.x = samples[0];
|
|
|
|
accelSensorData.y = samples[1];
|
|
|
|
accelSensorData.z = samples[2];
|
2014-12-29 12:28:42 +01:00
|
|
|
accelSensorData.temperature = temperature;
|
2014-12-23 19:14:31 +01:00
|
|
|
AccelSensorSet(&accelSensorData);
|
|
|
|
}
|
2014-12-29 12:28:42 +01:00
|
|
|
|
|
|
|
static void handleGyro(float *samples, float temperature)
|
2014-12-23 19:14:31 +01:00
|
|
|
{
|
|
|
|
GyroSensorData gyroSensorData;
|
2014-10-07 11:23:26 +02:00
|
|
|
|
2014-12-23 19:14:31 +01:00
|
|
|
updateGyroTempBias(temperature);
|
|
|
|
float gyros_out[3] = { samples[0] * agcal.gyro_scale.X - agcal.gyro_bias.X - gyro_temp_bias[0],
|
|
|
|
samples[1] * agcal.gyro_scale.Y - agcal.gyro_bias.Y - gyro_temp_bias[1],
|
|
|
|
samples[2] * agcal.gyro_scale.Z - agcal.gyro_bias.Z - gyro_temp_bias[2] };
|
2014-01-31 21:01:09 +01:00
|
|
|
|
2014-12-23 19:14:31 +01:00
|
|
|
rot_mult(R, gyros_out, samples);
|
|
|
|
gyroSensorData.temperature = temperature;
|
|
|
|
gyroSensorData.x = samples[0];
|
|
|
|
gyroSensorData.y = samples[1];
|
|
|
|
gyroSensorData.z = samples[2];
|
2013-05-18 14:17:26 +02:00
|
|
|
|
2014-12-23 19:14:31 +01:00
|
|
|
GyroSensorSet(&gyroSensorData);
|
|
|
|
}
|
2013-05-18 14:17:26 +02:00
|
|
|
|
2014-12-29 12:28:42 +01:00
|
|
|
static void handleMag(float *samples, float temperature)
|
2014-12-23 19:14:31 +01:00
|
|
|
{
|
|
|
|
MagSensorData mag;
|
|
|
|
float mags[3] = { (float)samples[1] - mag_bias[0],
|
|
|
|
(float)samples[0] - mag_bias[1],
|
|
|
|
(float)samples[2] - mag_bias[2] };
|
2012-06-12 20:10:24 +02:00
|
|
|
|
2014-12-23 19:14:31 +01:00
|
|
|
rot_mult(mag_transform, mags, samples);
|
2013-07-17 15:28:55 +02:00
|
|
|
|
2014-12-23 19:14:31 +01:00
|
|
|
mag.x = samples[0];
|
|
|
|
mag.y = samples[1];
|
|
|
|
mag.z = samples[2];
|
|
|
|
mag.temperature = temperature;
|
2013-07-17 15:28:55 +02:00
|
|
|
|
2014-12-23 19:14:31 +01:00
|
|
|
MagSensorSet(&mag);
|
|
|
|
}
|
2013-05-18 14:17:26 +02:00
|
|
|
|
2014-12-29 12:28:42 +01:00
|
|
|
static void handleBaro(float sample, float temperature)
|
|
|
|
{
|
|
|
|
updateBaroTempBias(temperature);
|
|
|
|
sample -= baro_temp_bias;
|
|
|
|
|
|
|
|
float altitude = 44330.0f * (1.0f - powf((sample) / PIOS_CONST_MKS_STD_ATMOSPHERE_F, (1.0f / 5.255f)));
|
|
|
|
|
|
|
|
if (!isnan(altitude)) {
|
|
|
|
BaroSensorData data;
|
|
|
|
data.Altitude = altitude;
|
|
|
|
data.Temperature = temperature;
|
|
|
|
data.Pressure = sample;
|
|
|
|
// Update the BasoSensor UAVObject
|
|
|
|
BaroSensorSet(&data);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-12-23 19:14:31 +01:00
|
|
|
static void updateAccelTempBias(float temperature)
|
|
|
|
{
|
|
|
|
if (isnan(accel_temperature)) {
|
|
|
|
accel_temperature = temperature;
|
|
|
|
}
|
2014-12-29 12:28:42 +01:00
|
|
|
accel_temperature = temp_alpha_gyro_accel * (temperature - accel_temperature) + accel_temperature;
|
2014-12-23 19:14:31 +01:00
|
|
|
|
|
|
|
if ((accel_temp_calibrated) && !accel_temp_calibration_count) {
|
|
|
|
accel_temp_calibration_count = TEMP_CALIB_INTERVAL;
|
|
|
|
if (accel_temp_calibrated) {
|
|
|
|
float ctemp = boundf(accel_temperature, agcal.temp_calibrated_extent.max, agcal.temp_calibrated_extent.min);
|
|
|
|
accel_temp_bias[0] = agcal.accel_temp_coeff.X * ctemp;
|
|
|
|
accel_temp_bias[1] = agcal.accel_temp_coeff.Y * ctemp;
|
|
|
|
accel_temp_bias[2] = agcal.accel_temp_coeff.Z * ctemp;
|
2013-05-18 14:17:26 +02:00
|
|
|
}
|
2014-12-23 19:14:31 +01:00
|
|
|
}
|
|
|
|
accel_temp_calibration_count--;
|
|
|
|
}
|
2014-12-29 12:28:42 +01:00
|
|
|
|
2014-12-23 19:14:31 +01:00
|
|
|
static void updateGyroTempBias(float temperature)
|
|
|
|
{
|
|
|
|
if (isnan(gyro_temperature)) {
|
|
|
|
gyro_temperature = temperature;
|
|
|
|
}
|
2013-05-18 14:17:26 +02:00
|
|
|
|
2014-12-29 12:28:42 +01:00
|
|
|
gyro_temperature = temp_alpha_gyro_accel * (temperature - gyro_temperature) + gyro_temperature;
|
2014-12-23 19:14:31 +01:00
|
|
|
|
|
|
|
if (gyro_temp_calibrated && !gyro_temp_calibration_count) {
|
|
|
|
gyro_temp_calibration_count = TEMP_CALIB_INTERVAL;
|
|
|
|
|
|
|
|
if (gyro_temp_calibrated) {
|
|
|
|
float ctemp = boundf(gyro_temperature, agcal.temp_calibrated_extent.max, agcal.temp_calibrated_extent.min);
|
|
|
|
gyro_temp_bias[0] = (agcal.gyro_temp_coeff.X + agcal.gyro_temp_coeff.X2 * ctemp) * ctemp;
|
|
|
|
gyro_temp_bias[1] = (agcal.gyro_temp_coeff.Y + agcal.gyro_temp_coeff.Y2 * ctemp) * ctemp;
|
|
|
|
gyro_temp_bias[2] = (agcal.gyro_temp_coeff.Z + agcal.gyro_temp_coeff.Z2 * ctemp) * ctemp;
|
|
|
|
}
|
2013-05-18 14:17:26 +02:00
|
|
|
}
|
2014-12-23 19:14:31 +01:00
|
|
|
gyro_temp_calibration_count--;
|
2011-12-12 21:39:38 +01:00
|
|
|
}
|
|
|
|
|
2014-12-29 12:28:42 +01:00
|
|
|
static void updateBaroTempBias(float temperature)
|
|
|
|
{
|
|
|
|
baro_temperature = temp_alpha_baro * (temperature - baro_temperature) + baro_temperature;
|
|
|
|
baro_temp_calibration_count--;
|
|
|
|
if (isnan(baro_temperature)) {
|
|
|
|
baro_temperature = temperature;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (baro_temp_correction_enabled && !baro_temp_calibration_count) {
|
|
|
|
baro_temp_calibration_count = BARO_TEMP_CALIB_INTERVAL;
|
|
|
|
// pressure bias = A + B*t + C*t^2 + D * t^3
|
|
|
|
// in case the temperature is outside of the calibrated range, uses the nearest extremes
|
|
|
|
float ctemp = boundf(baro_temperature, baroCorrectionExtent.max, baroCorrectionExtent.min);
|
|
|
|
baro_temp_bias = baroCorrection.a + ((baroCorrection.d * ctemp + baroCorrection.c) * ctemp + baroCorrection.b) * ctemp;
|
|
|
|
}
|
|
|
|
}
|
2011-12-12 21:39:38 +01:00
|
|
|
/**
|
|
|
|
* Locally cache some variables from the AtttitudeSettings object
|
|
|
|
*/
|
2013-05-18 14:17:26 +02:00
|
|
|
static void settingsUpdatedCb(__attribute__((unused)) UAVObjEvent *objEv)
|
|
|
|
{
|
|
|
|
RevoCalibrationGet(&cal);
|
2013-12-26 16:55:57 +01:00
|
|
|
AccelGyroSettingsGet(&agcal);
|
2014-05-08 23:18:38 +02:00
|
|
|
mag_bias[0] = cal.mag_bias.X;
|
|
|
|
mag_bias[1] = cal.mag_bias.Y;
|
|
|
|
mag_bias[2] = cal.mag_bias.Z;
|
2014-01-31 21:01:09 +01:00
|
|
|
|
|
|
|
accel_temp_calibrated = (agcal.temp_calibrated_extent.max - agcal.temp_calibrated_extent.min > .1f) &&
|
2014-03-23 22:07:09 +01:00
|
|
|
(fabsf(agcal.accel_temp_coeff.X) > 1e-9f || fabsf(agcal.accel_temp_coeff.Y) > 1e-9f || fabsf(agcal.accel_temp_coeff.Z) > 1e-9f);
|
2014-01-31 21:01:09 +01:00
|
|
|
|
2014-02-02 18:53:35 +01:00
|
|
|
gyro_temp_calibrated = (agcal.temp_calibrated_extent.max - agcal.temp_calibrated_extent.min > .1f) &&
|
2014-03-23 22:07:09 +01:00
|
|
|
(fabsf(agcal.gyro_temp_coeff.X) > 1e-9f || fabsf(agcal.gyro_temp_coeff.Y) > 1e-9f ||
|
|
|
|
fabsf(agcal.gyro_temp_coeff.Z) > 1e-9f || fabsf(agcal.gyro_temp_coeff.Z2) > 1e-9f);
|
2014-01-31 21:01:09 +01:00
|
|
|
|
2013-05-18 14:17:26 +02:00
|
|
|
|
|
|
|
AttitudeSettingsData attitudeSettings;
|
|
|
|
AttitudeSettingsGet(&attitudeSettings);
|
|
|
|
|
|
|
|
// Indicates not to expend cycles on rotation
|
2014-05-28 22:58:28 +02:00
|
|
|
if (fabsf(attitudeSettings.BoardRotation.Roll) < ZERO_ROT_ANGLE
|
|
|
|
&& fabsf(attitudeSettings.BoardRotation.Pitch) < ZERO_ROT_ANGLE &&
|
|
|
|
fabsf(attitudeSettings.BoardRotation.Yaw) < ZERO_ROT_ANGLE) {
|
2013-05-18 14:17:26 +02:00
|
|
|
rotate = 0;
|
|
|
|
} else {
|
2013-07-17 15:28:55 +02:00
|
|
|
rotate = 1;
|
|
|
|
}
|
2014-05-28 22:58:28 +02:00
|
|
|
|
2014-05-08 23:18:38 +02:00
|
|
|
const float rpy[3] = { attitudeSettings.BoardRotation.Roll,
|
|
|
|
attitudeSettings.BoardRotation.Pitch,
|
|
|
|
attitudeSettings.BoardRotation.Yaw };
|
2014-05-28 22:58:28 +02:00
|
|
|
|
|
|
|
float rotationQuat[4];
|
2013-07-17 15:28:55 +02:00
|
|
|
RPY2Quaternion(rpy, rotationQuat);
|
|
|
|
|
2014-05-28 22:58:28 +02:00
|
|
|
if (fabsf(attitudeSettings.BoardLevelTrim.Roll) > ZERO_ROT_ANGLE ||
|
|
|
|
fabsf(attitudeSettings.BoardLevelTrim.Pitch) > ZERO_ROT_ANGLE) {
|
|
|
|
float trimQuat[4];
|
|
|
|
float sumQuat[4];
|
|
|
|
rotate = 1;
|
|
|
|
|
|
|
|
const float trimRpy[3] = { attitudeSettings.BoardLevelTrim.Roll, attitudeSettings.BoardLevelTrim.Pitch, 0.0f };
|
|
|
|
RPY2Quaternion(trimRpy, trimQuat);
|
|
|
|
|
|
|
|
quat_mult(rotationQuat, trimQuat, sumQuat);
|
|
|
|
Quaternion2R(sumQuat, R);
|
|
|
|
} else {
|
|
|
|
Quaternion2R(rotationQuat, R);
|
|
|
|
}
|
2014-08-24 00:16:34 +02:00
|
|
|
matrix_mult_3x3f((float(*)[3])RevoCalibrationmag_transformToArray(cal.mag_transform), R, mag_transform);
|
2014-12-29 12:28:42 +01:00
|
|
|
|
|
|
|
RevoSettingsBaroTempCorrectionPolynomialGet(&baroCorrection);
|
|
|
|
RevoSettingsBaroTempCorrectionExtentGet(&baroCorrectionExtent);
|
|
|
|
baro_temp_correction_enabled = !(baroCorrectionExtent.max - baroCorrectionExtent.min < 0.1f ||
|
|
|
|
(baroCorrection.a < 1e-9f && baroCorrection.b < 1e-9f && baroCorrection.c < 1e-9f && baroCorrection.d < 1e-9f));
|
2011-12-12 21:39:38 +01:00
|
|
|
}
|
|
|
|
/**
|
2013-05-18 14:17:26 +02:00
|
|
|
* @}
|
|
|
|
* @}
|
|
|
|
*/
|