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>
|
|
|
|
|
|
|
|
#include <homelocation.h>
|
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>
|
|
|
|
#include <attitudestate.h>
|
2013-04-20 23:02:51 +02:00
|
|
|
#include <attitudesettings.h>
|
|
|
|
#include <revocalibration.h>
|
|
|
|
#include <flightstatus.h>
|
2013-05-02 23:31:14 +02:00
|
|
|
#include <taskinfo.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>
|
|
|
|
|
2011-12-12 21:39:38 +01:00
|
|
|
// Private constants
|
2012-07-25 17:50:32 +02:00
|
|
|
#define STACK_SIZE_BYTES 1000
|
2013-05-18 14:17:26 +02:00
|
|
|
#define TASK_PRIORITY (tskIDLE_PRIORITY + 3)
|
|
|
|
#define SENSOR_PERIOD 2
|
2011-12-12 21:39:38 +01:00
|
|
|
|
|
|
|
// Private types
|
|
|
|
|
|
|
|
|
|
|
|
// Private functions
|
|
|
|
static void SensorsTask(void *parameters);
|
2013-05-18 14:17:26 +02:00
|
|
|
static void settingsUpdatedCb(UAVObjEvent *objEv);
|
2013-05-20 10:33:02 +02:00
|
|
|
// static void magOffsetEstimation(MagSensorData *mag);
|
2011-12-12 21:39:38 +01:00
|
|
|
|
2012-07-25 18:23:27 +02:00
|
|
|
// Private variables
|
|
|
|
static xTaskHandle sensorsTaskHandle;
|
|
|
|
RevoCalibrationData cal;
|
|
|
|
|
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 };
|
|
|
|
static float mag_scale[3] = { 0, 0, 0 };
|
|
|
|
static float accel_bias[3] = { 0, 0, 0 };
|
|
|
|
static float accel_scale[3] = { 0, 0, 0 };
|
|
|
|
static float gyro_staticbias[3] = { 0, 0, 0 };
|
|
|
|
static float gyro_scale[3] = { 0, 0, 0 };
|
2011-12-15 07:57:19 +01:00
|
|
|
|
2013-05-18 14:17:26 +02:00
|
|
|
static float R[3][3] = {
|
|
|
|
{ 0 }
|
|
|
|
};
|
2012-03-26 05:25:52 +02:00
|
|
|
static int8_t rotate = 0;
|
|
|
|
|
2011-12-12 21:39:38 +01:00
|
|
|
/**
|
|
|
|
* API for sensor fusion algorithms:
|
|
|
|
* Configure(xQueueHandle gyro, xQueueHandle accel, xQueueHandle mag, xQueueHandle baro)
|
|
|
|
* Stores all the queues the algorithm will pull data from
|
|
|
|
* FinalizeSensors() -- before saving the sensors modifies them based on internal state (gyro bias)
|
|
|
|
* Update() -- queries queues and updates the attitude estiamte
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Initialise the module. Called before the start function
|
|
|
|
* \returns 0 on success or -1 if initialisation failed
|
|
|
|
*/
|
|
|
|
int32_t SensorsInitialize(void)
|
|
|
|
{
|
2013-05-18 19:36:45 +02:00
|
|
|
GyroSensorInitialize();
|
|
|
|
AccelSensorInitialize();
|
2013-05-20 10:33:02 +02:00
|
|
|
MagSensorInitialize();
|
2013-05-18 14:17:26 +02:00
|
|
|
RevoCalibrationInitialize();
|
|
|
|
AttitudeSettingsInitialize();
|
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
|
|
|
|
2013-05-18 14:17:26 +02:00
|
|
|
RevoCalibrationConnectCallback(&settingsUpdatedCb);
|
|
|
|
AttitudeSettingsConnectCallback(&settingsUpdatedCb);
|
2012-03-26 05:25:52 +02: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
|
|
|
|
xTaskCreate(SensorsTask, (signed char *)"Sensors", STACK_SIZE_BYTES / 4, NULL, TASK_PRIORITY, &sensorsTaskHandle);
|
|
|
|
PIOS_TASK_MONITOR_RegisterTask(TASKINFO_RUNNING_SENSORS, sensorsTaskHandle);
|
2013-05-19 21:56:23 +02:00
|
|
|
#ifdef PIOS_INCLUDE_WDG
|
2013-05-18 14:17:26 +02:00
|
|
|
PIOS_WDG_RegisterFlag(PIOS_WDG_SENSORS);
|
2013-05-19 21:56:23 +02:00
|
|
|
#endif
|
2011-12-12 21:39:38 +01:00
|
|
|
|
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
|
|
|
*
|
2012-01-05 02:47:57 +01:00
|
|
|
* This function has a lot of if/defs right now to allow these configurations:
|
|
|
|
* 1. BMA180 accel and MPU6000 gyro
|
|
|
|
* 2. MPU6000 gyro and accel
|
|
|
|
* 3. BMA180 accel and L3GD20 gyro
|
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;
|
|
|
|
uint32_t accel_samples = 0;
|
|
|
|
uint32_t gyro_samples = 0;
|
|
|
|
int32_t accel_accum[3] = { 0, 0, 0 };
|
|
|
|
int32_t gyro_accum[3] = { 0, 0, 0 };
|
|
|
|
float gyro_scaling = 0;
|
|
|
|
float accel_scaling = 0;
|
|
|
|
static int32_t timeval;
|
2011-12-12 21:39:38 +01:00
|
|
|
|
2013-05-18 14:17:26 +02:00
|
|
|
AlarmsClear(SYSTEMALARMS_ALARM_SENSORS);
|
2011-12-12 21:39:38 +01:00
|
|
|
|
2013-05-18 14:17:26 +02:00
|
|
|
UAVObjEvent ev;
|
|
|
|
settingsUpdatedCb(&ev);
|
2012-01-28 07:21:58 +01:00
|
|
|
|
2013-05-18 14:17:26 +02:00
|
|
|
const struct pios_board_info *bdinfo = &pios_board_info_blob;
|
2012-03-21 08:00:32 +01:00
|
|
|
|
2013-05-18 14:17:26 +02:00
|
|
|
switch (bdinfo->board_rev) {
|
|
|
|
case 0x01:
|
2012-03-21 08:00:32 +01:00
|
|
|
#if defined(PIOS_INCLUDE_L3GD20)
|
2013-05-18 14:17:26 +02:00
|
|
|
gyro_test = PIOS_L3GD20_Test();
|
2012-01-28 07:21:58 +01:00
|
|
|
#endif
|
|
|
|
#if defined(PIOS_INCLUDE_BMA180)
|
2013-05-18 14:17:26 +02:00
|
|
|
accel_test = PIOS_BMA180_Test();
|
2012-03-21 08:00:32 +01:00
|
|
|
#endif
|
2013-05-18 14:17:26 +02:00
|
|
|
break;
|
|
|
|
case 0x02:
|
2012-03-21 08:00:32 +01:00
|
|
|
#if defined(PIOS_INCLUDE_MPU6000)
|
2013-05-18 14:17:26 +02:00
|
|
|
gyro_test = PIOS_MPU6000_Test();
|
|
|
|
accel_test = gyro_test;
|
2012-01-05 02:47:57 +01:00
|
|
|
#endif
|
2013-05-18 14:17:26 +02:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
PIOS_DEBUG_Assert(0);
|
|
|
|
}
|
2012-03-21 08:00:32 +01:00
|
|
|
|
2012-03-14 08:45:42 +01:00
|
|
|
#if defined(PIOS_INCLUDE_HMC5883)
|
2013-05-18 14:17:26 +02:00
|
|
|
mag_test = PIOS_HMC5883_Test();
|
2012-05-12 05:09:04 +02:00
|
|
|
#else
|
2013-05-18 14:17:26 +02:00
|
|
|
mag_test = 0;
|
2012-03-14 08:45:42 +01:00
|
|
|
#endif
|
|
|
|
|
2013-05-18 14:17:26 +02:00
|
|
|
if (accel_test < 0 || gyro_test < 0 || mag_test < 0) {
|
|
|
|
AlarmsSet(SYSTEMALARMS_ALARM_SENSORS, SYSTEMALARMS_ALARM_CRITICAL);
|
|
|
|
while (1) {
|
2013-05-19 21:56:23 +02:00
|
|
|
#ifdef PIOS_INCLUDE_WDG
|
2013-05-18 14:17:26 +02:00
|
|
|
PIOS_WDG_UpdateFlag(PIOS_WDG_SENSORS);
|
2013-05-19 21:56:23 +02:00
|
|
|
#endif
|
2013-05-18 14:17:26 +02:00
|
|
|
vTaskDelay(10);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Main task loop
|
|
|
|
lastSysTime = xTaskGetTickCount();
|
|
|
|
bool error = false;
|
|
|
|
uint32_t mag_update_time = PIOS_DELAY_GetRaw();
|
|
|
|
while (1) {
|
|
|
|
// TODO: add timeouts to the sensor reads and set an error if the fail
|
|
|
|
sensor_dt_us = PIOS_DELAY_DiffuS(timeval);
|
|
|
|
timeval = PIOS_DELAY_GetRaw();
|
|
|
|
|
|
|
|
if (error) {
|
2013-05-19 21:56:23 +02:00
|
|
|
#ifdef PIOS_INCLUDE_WDG
|
2013-05-18 14:17:26 +02:00
|
|
|
PIOS_WDG_UpdateFlag(PIOS_WDG_SENSORS);
|
2013-05-19 21:56:23 +02:00
|
|
|
#endif
|
2013-05-18 14:17:26 +02:00
|
|
|
lastSysTime = xTaskGetTickCount();
|
|
|
|
vTaskDelayUntil(&lastSysTime, SENSOR_PERIOD / portTICK_RATE_MS);
|
|
|
|
AlarmsSet(SYSTEMALARMS_ALARM_SENSORS, SYSTEMALARMS_ALARM_CRITICAL);
|
|
|
|
error = false;
|
|
|
|
} else {
|
|
|
|
AlarmsClear(SYSTEMALARMS_ALARM_SENSORS);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
for (int i = 0; i < 3; i++) {
|
|
|
|
accel_accum[i] = 0;
|
|
|
|
gyro_accum[i] = 0;
|
|
|
|
}
|
|
|
|
accel_samples = 0;
|
|
|
|
gyro_samples = 0;
|
|
|
|
|
2013-05-18 19:36:45 +02:00
|
|
|
AccelSensorData accelSensorData;
|
|
|
|
GyroSensorData gyroSensorData;
|
2013-05-18 14:17:26 +02:00
|
|
|
|
|
|
|
switch (bdinfo->board_rev) {
|
|
|
|
case 0x01: // L3GD20 + BMA180 board
|
2012-03-21 08:00:32 +01:00
|
|
|
#if defined(PIOS_INCLUDE_BMA180)
|
2013-05-18 14:17:26 +02:00
|
|
|
{
|
|
|
|
struct pios_bma180_data accel;
|
|
|
|
|
|
|
|
int32_t read_good;
|
|
|
|
int32_t count;
|
|
|
|
|
|
|
|
count = 0;
|
|
|
|
while ((read_good = PIOS_BMA180_ReadFifo(&accel)) != 0 && !error) {
|
|
|
|
error = ((xTaskGetTickCount() - lastSysTime) > SENSOR_PERIOD) ? true : error;
|
|
|
|
}
|
|
|
|
if (error) {
|
|
|
|
// Unfortunately if the BMA180 ever misses getting read, then it will not
|
|
|
|
// trigger more interrupts. In this case we must force a read to kickstarts
|
|
|
|
// it.
|
|
|
|
struct pios_bma180_data data;
|
|
|
|
PIOS_BMA180_ReadAccels(&data);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
while (read_good == 0) {
|
|
|
|
count++;
|
|
|
|
|
|
|
|
accel_accum[1] += accel.x;
|
|
|
|
accel_accum[0] += accel.y;
|
|
|
|
accel_accum[2] -= accel.z;
|
|
|
|
|
|
|
|
read_good = PIOS_BMA180_ReadFifo(&accel);
|
|
|
|
}
|
|
|
|
accel_samples = count;
|
|
|
|
accel_scaling = PIOS_BMA180_GetScale();
|
|
|
|
|
|
|
|
// Get temp from last reading
|
2013-05-18 19:36:45 +02:00
|
|
|
accelSensorData.temperature = 25.0f + ((float)accel.temperature - 2.0f) / 2.0f;
|
2013-05-18 14:17:26 +02:00
|
|
|
}
|
|
|
|
#endif /* if defined(PIOS_INCLUDE_BMA180) */
|
2012-03-21 08:00:32 +01:00
|
|
|
#if defined(PIOS_INCLUDE_L3GD20)
|
2013-05-18 14:17:26 +02:00
|
|
|
{
|
|
|
|
struct pios_l3gd20_data gyro;
|
|
|
|
gyro_samples = 0;
|
|
|
|
xQueueHandle gyro_queue = PIOS_L3GD20_GetQueue();
|
|
|
|
|
|
|
|
if (xQueueReceive(gyro_queue, (void *)&gyro, 4) == errQUEUE_EMPTY) {
|
|
|
|
error = true;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
gyro_samples = 1;
|
|
|
|
gyro_accum[1] += gyro.gyro_x;
|
|
|
|
gyro_accum[0] += gyro.gyro_y;
|
|
|
|
gyro_accum[2] -= gyro.gyro_z;
|
|
|
|
|
|
|
|
gyro_scaling = PIOS_L3GD20_GetScale();
|
|
|
|
|
|
|
|
// Get temp from last reading
|
2013-05-18 19:36:45 +02:00
|
|
|
gyroSensorData.temperature = gyro.temperature;
|
2013-05-18 14:17:26 +02:00
|
|
|
}
|
|
|
|
#endif /* if defined(PIOS_INCLUDE_L3GD20) */
|
|
|
|
break;
|
|
|
|
case 0x02: // MPU6000 board
|
|
|
|
case 0x03: // MPU6000 board
|
2012-03-21 08:00:32 +01:00
|
|
|
#if defined(PIOS_INCLUDE_MPU6000)
|
2013-05-18 14:17:26 +02:00
|
|
|
{
|
|
|
|
struct pios_mpu6000_data mpu6000_data;
|
|
|
|
xQueueHandle queue = PIOS_MPU6000_GetQueue();
|
|
|
|
|
|
|
|
while (xQueueReceive(queue, (void *)&mpu6000_data, gyro_samples == 0 ? 10 : 0) != errQUEUE_EMPTY) {
|
|
|
|
gyro_accum[0] += mpu6000_data.gyro_x;
|
|
|
|
gyro_accum[1] += mpu6000_data.gyro_y;
|
|
|
|
gyro_accum[2] += mpu6000_data.gyro_z;
|
|
|
|
|
|
|
|
accel_accum[0] += mpu6000_data.accel_x;
|
|
|
|
accel_accum[1] += mpu6000_data.accel_y;
|
|
|
|
accel_accum[2] += mpu6000_data.accel_z;
|
|
|
|
|
|
|
|
gyro_samples++;
|
|
|
|
accel_samples++;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (gyro_samples == 0) {
|
|
|
|
PIOS_MPU6000_ReadGyros(&mpu6000_data);
|
|
|
|
error = true;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
gyro_scaling = PIOS_MPU6000_GetScale();
|
|
|
|
accel_scaling = PIOS_MPU6000_GetAccelScale();
|
|
|
|
|
2013-05-18 19:36:45 +02:00
|
|
|
gyroSensorData.temperature = 35.0f + ((float)mpu6000_data.temperature + 512.0f) / 340.0f;
|
|
|
|
accelSensorData.temperature = 35.0f + ((float)mpu6000_data.temperature + 512.0f) / 340.0f;
|
2013-05-18 14:17:26 +02:00
|
|
|
}
|
2012-03-21 08:00:32 +01:00
|
|
|
#endif /* PIOS_INCLUDE_MPU6000 */
|
2013-05-18 14:17:26 +02:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
PIOS_DEBUG_Assert(0);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Scale the accels
|
|
|
|
float accels[3] = { (float)accel_accum[0] / accel_samples,
|
|
|
|
(float)accel_accum[1] / accel_samples,
|
|
|
|
(float)accel_accum[2] / accel_samples };
|
|
|
|
float accels_out[3] = { accels[0] * accel_scaling * accel_scale[0] - accel_bias[0],
|
|
|
|
accels[1] * accel_scaling * accel_scale[1] - accel_bias[1],
|
|
|
|
accels[2] * accel_scaling * accel_scale[2] - accel_bias[2] };
|
|
|
|
if (rotate) {
|
|
|
|
rot_mult(R, accels_out, accels);
|
2013-05-18 19:36:45 +02:00
|
|
|
accelSensorData.x = accels[0];
|
|
|
|
accelSensorData.y = accels[1];
|
|
|
|
accelSensorData.z = accels[2];
|
2013-05-18 14:17:26 +02:00
|
|
|
} else {
|
2013-05-18 19:36:45 +02:00
|
|
|
accelSensorData.x = accels_out[0];
|
|
|
|
accelSensorData.y = accels_out[1];
|
|
|
|
accelSensorData.z = accels_out[2];
|
2013-05-18 14:17:26 +02:00
|
|
|
}
|
2013-05-18 19:36:45 +02:00
|
|
|
AccelSensorSet(&accelSensorData);
|
2013-05-18 14:17:26 +02:00
|
|
|
|
|
|
|
// Scale the gyros
|
|
|
|
float gyros[3] = { (float)gyro_accum[0] / gyro_samples,
|
|
|
|
(float)gyro_accum[1] / gyro_samples,
|
|
|
|
(float)gyro_accum[2] / gyro_samples };
|
|
|
|
float gyros_out[3] = { gyros[0] * gyro_scaling * gyro_scale[0] - gyro_staticbias[0],
|
|
|
|
gyros[1] * gyro_scaling * gyro_scale[1] - gyro_staticbias[1],
|
|
|
|
gyros[2] * gyro_scaling * gyro_scale[2] - gyro_staticbias[2] };
|
|
|
|
if (rotate) {
|
|
|
|
rot_mult(R, gyros_out, gyros);
|
2013-05-18 19:36:45 +02:00
|
|
|
gyroSensorData.x = gyros[0];
|
|
|
|
gyroSensorData.y = gyros[1];
|
|
|
|
gyroSensorData.z = gyros[2];
|
2013-05-18 14:17:26 +02:00
|
|
|
} else {
|
2013-05-18 19:36:45 +02:00
|
|
|
gyroSensorData.x = gyros_out[0];
|
|
|
|
gyroSensorData.y = gyros_out[1];
|
|
|
|
gyroSensorData.z = gyros_out[2];
|
2013-05-18 14:17:26 +02:00
|
|
|
}
|
|
|
|
|
2013-05-18 19:36:45 +02:00
|
|
|
GyroSensorSet(&gyroSensorData);
|
2013-05-18 14:17:26 +02:00
|
|
|
|
|
|
|
// Because most crafts wont get enough information from gravity to zero yaw gyro, we try
|
|
|
|
// and make it average zero (weakly)
|
2012-06-12 20:10:24 +02:00
|
|
|
|
|
|
|
#if defined(PIOS_INCLUDE_HMC5883)
|
2013-05-20 10:33:02 +02:00
|
|
|
MagSensorData mag;
|
2013-05-18 14:17:26 +02:00
|
|
|
if (PIOS_HMC5883_NewDataAvailable() || PIOS_DELAY_DiffuS(mag_update_time) > 150000) {
|
|
|
|
int16_t values[3];
|
|
|
|
PIOS_HMC5883_ReadMag(values);
|
|
|
|
float mags[3] = { (float)values[1] * mag_scale[0] - mag_bias[0],
|
|
|
|
(float)values[0] * mag_scale[1] - mag_bias[1],
|
|
|
|
-(float)values[2] * mag_scale[2] - mag_bias[2] };
|
|
|
|
if (rotate) {
|
|
|
|
float mag_out[3];
|
|
|
|
rot_mult(R, mags, mag_out);
|
|
|
|
mag.x = mag_out[0];
|
|
|
|
mag.y = mag_out[1];
|
|
|
|
mag.z = mag_out[2];
|
|
|
|
} else {
|
|
|
|
mag.x = mags[0];
|
|
|
|
mag.y = mags[1];
|
|
|
|
mag.z = mags[2];
|
|
|
|
}
|
|
|
|
|
2013-05-20 10:33:02 +02:00
|
|
|
MagSensorSet(&mag);
|
2013-05-18 14:17:26 +02:00
|
|
|
mag_update_time = PIOS_DELAY_GetRaw();
|
|
|
|
}
|
|
|
|
#endif /* if defined(PIOS_INCLUDE_HMC5883) */
|
|
|
|
|
2013-05-19 21:56:23 +02:00
|
|
|
#ifdef PIOS_INCLUDE_WDG
|
2013-05-18 14:17:26 +02:00
|
|
|
PIOS_WDG_UpdateFlag(PIOS_WDG_SENSORS);
|
2013-05-19 21:56:23 +02:00
|
|
|
#endif
|
2013-05-18 14:17:26 +02:00
|
|
|
|
|
|
|
lastSysTime = xTaskGetTickCount();
|
|
|
|
}
|
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-07-31 15:24:26 +02:00
|
|
|
mag_bias[0] = cal.mag_bias.fields.X;
|
|
|
|
mag_bias[1] = cal.mag_bias.fields.Y;
|
|
|
|
mag_bias[2] = cal.mag_bias.fields.Z;
|
|
|
|
mag_scale[0] = cal.mag_scale.fields.X;
|
|
|
|
mag_scale[1] = cal.mag_scale.fields.Y;
|
|
|
|
mag_scale[2] = cal.mag_scale.fields.Z;
|
|
|
|
accel_bias[0] = cal.accel_bias.fields.X;
|
|
|
|
accel_bias[1] = cal.accel_bias.fields.Y;
|
|
|
|
accel_bias[2] = cal.accel_bias.fields.Z;
|
|
|
|
accel_scale[0] = cal.accel_scale.fields.X;
|
|
|
|
accel_scale[1] = cal.accel_scale.fields.Y;
|
|
|
|
accel_scale[2] = cal.accel_scale.fields.Z;
|
|
|
|
gyro_staticbias[0] = cal.gyro_bias.fields.X;
|
|
|
|
gyro_staticbias[1] = cal.gyro_bias.fields.Y;
|
|
|
|
gyro_staticbias[2] = cal.gyro_bias.fields.Z;
|
|
|
|
gyro_scale[0] = cal.gyro_scale.fields.X;
|
|
|
|
gyro_scale[1] = cal.gyro_scale.fields.Y;
|
|
|
|
gyro_scale[2] = cal.gyro_scale.fields.Z;
|
2013-05-18 14:17:26 +02:00
|
|
|
|
|
|
|
|
|
|
|
AttitudeSettingsData attitudeSettings;
|
|
|
|
AttitudeSettingsGet(&attitudeSettings);
|
|
|
|
|
|
|
|
// Indicates not to expend cycles on rotation
|
2013-08-01 01:28:50 +02:00
|
|
|
if (attitudeSettings.BoardRotation.fields.Roll == 0 && attitudeSettings.BoardRotation.fields.Pitch == 0 &&
|
2013-07-31 15:24:26 +02:00
|
|
|
attitudeSettings.BoardRotation.fields.Yaw == 0) {
|
2013-05-18 14:17:26 +02:00
|
|
|
rotate = 0;
|
|
|
|
} else {
|
|
|
|
float rotationQuat[4];
|
2013-07-31 15:24:26 +02:00
|
|
|
const float rpy[3] = { attitudeSettings.BoardRotation.fields.Roll,
|
2013-08-01 01:28:50 +02:00
|
|
|
attitudeSettings.BoardRotation.fields.Pitch,
|
|
|
|
attitudeSettings.BoardRotation.fields.Yaw };
|
2013-05-18 14:17:26 +02:00
|
|
|
RPY2Quaternion(rpy, rotationQuat);
|
|
|
|
Quaternion2R(rotationQuat, R);
|
|
|
|
rotate = 1;
|
|
|
|
}
|
2011-12-12 21:39:38 +01:00
|
|
|
}
|
|
|
|
/**
|
2013-05-18 14:17:26 +02:00
|
|
|
* @}
|
|
|
|
* @}
|
|
|
|
*/
|