2010-09-27 09:28:34 +02:00
|
|
|
/**
|
|
|
|
******************************************************************************
|
|
|
|
* @addtogroup OpenPilotModules OpenPilot Modules
|
|
|
|
* @{
|
|
|
|
* @addtogroup AltitudeModule Altitude Module
|
|
|
|
* @brief Communicate with BMP085 and update @ref BaroAltitude "BaroAltitude UAV Object"
|
|
|
|
* @{
|
|
|
|
*
|
|
|
|
* @file altitude.c
|
|
|
|
* @author The OpenPilot Team, http://www.openpilot.org Copyright (C) 2010.
|
|
|
|
* @brief Altitude module, handles temperature and pressure readings from BMP085
|
|
|
|
*
|
|
|
|
* @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
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Output object: BaroAltitude
|
|
|
|
*
|
|
|
|
* This module will periodically update the value of the BaroAltitude object.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "openpilot.h"
|
2012-01-14 21:04:49 +01:00
|
|
|
#include "hwsettings.h"
|
2011-06-17 07:13:19 +02:00
|
|
|
#include "altitude.h"
|
2013-03-28 17:20:50 +01:00
|
|
|
#if defined(PIOS_INCLUDE_BMP085)
|
2010-09-27 09:28:34 +02:00
|
|
|
#include "baroaltitude.h" // object that will be updated by the module
|
2013-03-28 17:20:50 +01:00
|
|
|
#endif
|
2011-01-28 20:21:22 +01:00
|
|
|
#if defined(PIOS_INCLUDE_HCSR04)
|
|
|
|
#include "sonaraltitude.h" // object that will be updated by the module
|
|
|
|
#endif
|
2010-09-27 09:28:34 +02:00
|
|
|
|
|
|
|
// Private constants
|
2011-01-09 01:03:19 +01:00
|
|
|
#define STACK_SIZE_BYTES 500
|
2011-01-09 21:49:33 +01:00
|
|
|
#define TASK_PRIORITY (tskIDLE_PRIORITY+1)
|
2012-01-14 21:17:24 +01:00
|
|
|
#define UPDATE_PERIOD 50
|
2010-09-27 09:28:34 +02:00
|
|
|
|
|
|
|
// Private types
|
|
|
|
|
|
|
|
// Private variables
|
|
|
|
static xTaskHandle taskHandle;
|
|
|
|
|
2011-01-05 13:19:49 +01:00
|
|
|
// down sampling variables
|
2013-03-28 17:20:50 +01:00
|
|
|
#if defined(PIOS_INCLUDE_BMP085)
|
2011-01-05 13:19:49 +01:00
|
|
|
#define alt_ds_size 4
|
|
|
|
static int32_t alt_ds_temp = 0;
|
|
|
|
static int32_t alt_ds_pres = 0;
|
|
|
|
static int alt_ds_count = 0;
|
2013-03-28 17:20:50 +01:00
|
|
|
#endif
|
2011-01-05 12:26:47 +01:00
|
|
|
|
2012-01-14 21:04:49 +01:00
|
|
|
static bool altitudeEnabled;
|
2013-03-28 17:20:50 +01:00
|
|
|
static uint8_t hwsettings_rcvrport;;
|
2012-01-14 21:04:49 +01:00
|
|
|
|
2011-01-05 13:19:49 +01:00
|
|
|
// Private functions
|
2010-09-27 09:28:34 +02:00
|
|
|
static void altitudeTask(void *parameters);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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 AltitudeStart()
|
2010-09-27 09:28:34 +02:00
|
|
|
{
|
2012-01-14 21:04:49 +01:00
|
|
|
|
|
|
|
if (altitudeEnabled) {
|
2013-03-28 17:20:50 +01:00
|
|
|
#if defined(PIOS_INCLUDE_BMP085)
|
2012-01-14 21:04:49 +01:00
|
|
|
BaroAltitudeInitialize();
|
2013-03-28 17:20:50 +01:00
|
|
|
#endif
|
2011-06-18 18:59:02 +02:00
|
|
|
#if defined(PIOS_INCLUDE_HCSR04)
|
2013-03-28 17:20:50 +01:00
|
|
|
SonarAltitudeInitialize();
|
2011-06-18 18:59:02 +02:00
|
|
|
#endif
|
2010-09-27 09:28:34 +02:00
|
|
|
|
2012-01-14 21:04:49 +01:00
|
|
|
// Start main task
|
|
|
|
xTaskCreate(altitudeTask, (signed char *)"Altitude", STACK_SIZE_BYTES/4, NULL, TASK_PRIORITY, &taskHandle);
|
|
|
|
TaskMonitorAdd(TASKINFO_RUNNING_ALTITUDE, taskHandle);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
return -1;
|
2011-06-20 07:35:40 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Initialise the module, called on startup
|
|
|
|
* \returns 0 on success or -1 if initialisation failed
|
|
|
|
*/
|
|
|
|
int32_t AltitudeInitialize()
|
|
|
|
{
|
2013-03-25 15:02:16 +01:00
|
|
|
#ifdef MODULE_ALTITUDE_BUILTIN
|
2012-01-14 22:55:55 +01:00
|
|
|
altitudeEnabled = 1;
|
|
|
|
#else
|
2012-01-14 21:04:49 +01:00
|
|
|
HwSettingsInitialize();
|
|
|
|
uint8_t optionalModules[HWSETTINGS_OPTIONALMODULES_NUMELEM];
|
|
|
|
HwSettingsOptionalModulesGet(optionalModules);
|
|
|
|
if (optionalModules[HWSETTINGS_OPTIONALMODULES_ALTITUDE] == HWSETTINGS_OPTIONALMODULES_ENABLED) {
|
|
|
|
altitudeEnabled = 1;
|
|
|
|
} else {
|
|
|
|
altitudeEnabled = 0;
|
|
|
|
}
|
2012-01-14 22:55:55 +01:00
|
|
|
#endif
|
2011-06-20 07:35:40 +02:00
|
|
|
|
2013-03-28 17:20:50 +01:00
|
|
|
#if defined(PIOS_INCLUDE_BMP085)
|
2011-01-05 13:19:49 +01:00
|
|
|
// init down-sampling data
|
2011-05-26 21:06:47 +02:00
|
|
|
alt_ds_temp = 0;
|
|
|
|
alt_ds_pres = 0;
|
|
|
|
alt_ds_count = 0;
|
2013-03-28 17:20:50 +01:00
|
|
|
#endif
|
|
|
|
HwSettingsCC_RcvrPortGet(&hwsettings_rcvrport);
|
2010-09-27 09:28:34 +02:00
|
|
|
return 0;
|
|
|
|
}
|
2011-07-13 05:44:32 +02:00
|
|
|
MODULE_INITCALL(AltitudeInitialize, AltitudeStart)
|
2010-09-27 09:28:34 +02:00
|
|
|
/**
|
|
|
|
* Module thread, should not return.
|
|
|
|
*/
|
2013-05-05 09:02:24 +02:00
|
|
|
static void altitudeTask(__attribute__((unused)) void *parameters)
|
2010-09-27 09:28:34 +02:00
|
|
|
{
|
|
|
|
portTickType lastSysTime;
|
2011-05-26 21:06:47 +02:00
|
|
|
|
2011-01-28 20:21:22 +01:00
|
|
|
#if defined(PIOS_INCLUDE_HCSR04)
|
|
|
|
SonarAltitudeData sonardata;
|
2013-04-14 11:23:47 +02:00
|
|
|
int32_t value = 0, timeout = 5;
|
|
|
|
float coeff = 0.25, height_out = 0, height_in = 0;
|
2013-03-28 17:20:50 +01:00
|
|
|
if(hwsettings_rcvrport==HWSETTINGS_CC_RCVRPORT_DISABLED) {
|
|
|
|
PIOS_HCSR04_Trigger();
|
|
|
|
}
|
2011-01-28 20:21:22 +01:00
|
|
|
#endif
|
2013-03-28 17:20:50 +01:00
|
|
|
#if defined(PIOS_INCLUDE_BMP085)
|
|
|
|
BaroAltitudeData data;
|
2010-09-27 09:28:34 +02:00
|
|
|
PIOS_BMP085_Init();
|
2013-03-28 17:20:50 +01:00
|
|
|
#endif
|
2011-05-26 21:06:47 +02:00
|
|
|
|
2010-09-27 09:28:34 +02:00
|
|
|
// Main task loop
|
|
|
|
lastSysTime = xTaskGetTickCount();
|
2011-01-05 13:19:49 +01:00
|
|
|
while (1)
|
|
|
|
{
|
2011-01-28 20:21:22 +01:00
|
|
|
#if defined(PIOS_INCLUDE_HCSR04)
|
2013-03-28 17:20:50 +01:00
|
|
|
// Compute the current altitude
|
|
|
|
if(hwsettings_rcvrport==HWSETTINGS_CC_RCVRPORT_DISABLED) {
|
2013-04-14 11:23:47 +02:00
|
|
|
if(PIOS_HCSR04_Completed()) {
|
2013-03-28 17:20:50 +01:00
|
|
|
value = PIOS_HCSR04_Get();
|
2013-04-14 11:23:47 +02:00
|
|
|
//from 3.4cm to 5.1m
|
|
|
|
if((value > 100) && (value < 15000)) {
|
|
|
|
height_in = value * 0.00034f / 2.0f;
|
2013-03-28 17:20:50 +01:00
|
|
|
height_out = (height_out * (1 - coeff)) + (height_in * coeff);
|
|
|
|
sonardata.Altitude = height_out; // m/us
|
|
|
|
}
|
|
|
|
|
|
|
|
// Update the AltitudeActual UAVObject
|
|
|
|
SonarAltitudeSet(&sonardata);
|
2013-04-14 11:23:47 +02:00
|
|
|
timeout = 5;
|
2013-03-28 17:20:50 +01:00
|
|
|
PIOS_HCSR04_Trigger();
|
|
|
|
}
|
2013-04-14 11:23:47 +02:00
|
|
|
if(!(timeout--)) {
|
2013-03-28 17:20:50 +01:00
|
|
|
//retrigger
|
2013-04-14 11:23:47 +02:00
|
|
|
timeout = 5;
|
2013-03-28 17:20:50 +01:00
|
|
|
PIOS_HCSR04_Trigger();
|
2011-01-28 20:21:22 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
2013-03-28 17:20:50 +01:00
|
|
|
#if defined(PIOS_INCLUDE_BMP085)
|
2010-09-27 09:28:34 +02:00
|
|
|
// Update the temperature data
|
|
|
|
PIOS_BMP085_StartADC(TemperatureConv);
|
2011-11-26 01:38:16 +01:00
|
|
|
#ifdef PIOS_BMP085_HAS_GPIOS
|
2010-09-27 09:28:34 +02:00
|
|
|
xSemaphoreTake(PIOS_BMP085_EOC, portMAX_DELAY);
|
2011-11-26 01:38:16 +01:00
|
|
|
#else
|
|
|
|
vTaskDelay(5 / portTICK_RATE_MS);
|
|
|
|
#endif
|
2010-09-27 09:28:34 +02:00
|
|
|
PIOS_BMP085_ReadADC();
|
2011-01-05 13:19:49 +01:00
|
|
|
alt_ds_temp += PIOS_BMP085_GetTemperature();
|
2011-05-26 21:06:47 +02:00
|
|
|
|
2010-09-27 09:28:34 +02:00
|
|
|
// Update the pressure data
|
|
|
|
PIOS_BMP085_StartADC(PressureConv);
|
2011-11-26 01:38:16 +01:00
|
|
|
#ifdef PIOS_BMP085_HAS_GPIOS
|
2010-09-27 09:28:34 +02:00
|
|
|
xSemaphoreTake(PIOS_BMP085_EOC, portMAX_DELAY);
|
2011-11-26 01:38:16 +01:00
|
|
|
#else
|
|
|
|
vTaskDelay(26 / portTICK_RATE_MS);
|
|
|
|
#endif
|
2010-09-27 09:28:34 +02:00
|
|
|
PIOS_BMP085_ReadADC();
|
2011-01-05 13:19:49 +01:00
|
|
|
alt_ds_pres += PIOS_BMP085_GetPressure();
|
2011-05-26 21:06:47 +02:00
|
|
|
|
2011-01-05 13:19:49 +01:00
|
|
|
if (++alt_ds_count >= alt_ds_size)
|
2011-05-26 21:06:47 +02:00
|
|
|
{
|
|
|
|
alt_ds_count = 0;
|
2011-01-05 13:19:49 +01:00
|
|
|
|
2012-01-14 21:04:49 +01:00
|
|
|
// Convert from 1/10ths of degC to degC
|
|
|
|
data.Temperature = alt_ds_temp / (10.0 * alt_ds_size);
|
|
|
|
alt_ds_temp = 0;
|
2011-01-05 12:26:47 +01:00
|
|
|
|
2012-01-14 21:04:49 +01:00
|
|
|
// Convert from Pa to kPa
|
|
|
|
data.Pressure = alt_ds_pres / (1000.0f * alt_ds_size);
|
|
|
|
alt_ds_pres = 0;
|
2010-09-27 09:28:34 +02:00
|
|
|
|
2012-01-14 21:04:49 +01:00
|
|
|
// Compute the current altitude (all pressures in kPa)
|
|
|
|
data.Altitude = 44330.0 * (1.0 - powf((data.Pressure / (BMP085_P0 / 1000.0)), (1.0 / 5.255)));
|
2010-09-27 09:28:34 +02:00
|
|
|
|
2012-01-14 21:04:49 +01:00
|
|
|
// Update the AltitudeActual UAVObject
|
|
|
|
BaroAltitudeSet(&data);
|
|
|
|
}
|
2013-03-28 17:20:50 +01:00
|
|
|
#endif
|
2010-09-27 09:28:34 +02:00
|
|
|
|
|
|
|
// Delay until it is time to read the next sample
|
|
|
|
vTaskDelayUntil(&lastSysTime, UPDATE_PERIOD / portTICK_RATE_MS);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2011-05-26 21:06:47 +02:00
|
|
|
* @}
|
2010-09-27 09:28:34 +02:00
|
|
|
* @}
|
|
|
|
*/
|