forked from adafruit/Adafruit_BMP085_Unified
Added temp compensated altitude calculations
This commit is contained in:
@@ -19,6 +19,7 @@
|
|||||||
#endif
|
#endif
|
||||||
|
|
||||||
#include <Wire.h>
|
#include <Wire.h>
|
||||||
|
#include <Math.h>
|
||||||
#include <limits.h>
|
#include <limits.h>
|
||||||
|
|
||||||
#include "Adafruit_BMP085.h"
|
#include "Adafruit_BMP085.h"
|
||||||
@@ -322,14 +323,29 @@ void Adafruit_BMP085::getTemperature(float *temp)
|
|||||||
|
|
||||||
/**************************************************************************/
|
/**************************************************************************/
|
||||||
/*!
|
/*!
|
||||||
Converts pressure in hPa to altitude in meters
|
Calculates the altitude (in meters) from the specified atmospheric
|
||||||
|
pressure (in hPa), sea-level pressure (in hPa), and temperature (in <20>C)
|
||||||
|
|
||||||
|
@param seaLevel Sea-level pressure in hPa
|
||||||
|
@param atmospheric Atmospheric pressure in hPa
|
||||||
|
@param temp Temperature in degrees Celsius
|
||||||
*/
|
*/
|
||||||
/**************************************************************************/
|
/**************************************************************************/
|
||||||
float Adafruit_BMP085::pressureToAltitude(float pressure_hPa)
|
float Adafruit_BMP085::pressureToAltitude(float seaLevel, float atmospheric, float temp)
|
||||||
{
|
{
|
||||||
return (pressure_hPa
|
/* Hyposometric formula: */
|
||||||
/ (float) pow(1.0F - (SENSORS_PRESSURE_SEALEVELHPA / 44330.0f), 5.255f))
|
/* */
|
||||||
/ 10;
|
/* ((P0/P)^(1/5.257) - 1) * (T + 273.15) */
|
||||||
|
/* h = ------------------------------------- */
|
||||||
|
/* 0.0065 */
|
||||||
|
/* */
|
||||||
|
/* where: h = height (in meters) */
|
||||||
|
/* P0 = sea-level pressure (in hPa) */
|
||||||
|
/* P = atmospheric pressure (in hPa) */
|
||||||
|
/* T = temperature (in <20>C) */
|
||||||
|
|
||||||
|
return (((float)pow((seaLevel/atmospheric), 0.190223F) - 1.0F)
|
||||||
|
* (temp + 273.15F)) / 0.0065F;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**************************************************************************/
|
/**************************************************************************/
|
||||||
|
@@ -96,7 +96,7 @@ class Adafruit_BMP085 : public Adafruit_Sensor
|
|||||||
bool begin(bmp085_mode_t mode = BMP085_MODE_ULTRAHIGHRES);
|
bool begin(bmp085_mode_t mode = BMP085_MODE_ULTRAHIGHRES);
|
||||||
void getTemperature(float *temp);
|
void getTemperature(float *temp);
|
||||||
void getPressure(float *pressure);
|
void getPressure(float *pressure);
|
||||||
float pressureToAltitude(float pressure_hPa);
|
float pressureToAltitude(float seaLevel, float atmospheric, float temp);
|
||||||
void getEvent(sensors_event_t*);
|
void getEvent(sensors_event_t*);
|
||||||
void getSensor(sensor_t*);
|
void getSensor(sensor_t*);
|
||||||
|
|
||||||
|
@@ -23,6 +23,7 @@
|
|||||||
|
|
||||||
History
|
History
|
||||||
=======
|
=======
|
||||||
|
2013/JUN/17 - Updated altitude calculations (KTOWN)
|
||||||
2013/FEB/13 - First version (KTOWN)
|
2013/FEB/13 - First version (KTOWN)
|
||||||
*/
|
*/
|
||||||
|
|
||||||
@@ -79,7 +80,7 @@ void setup(void)
|
|||||||
*/
|
*/
|
||||||
/**************************************************************************/
|
/**************************************************************************/
|
||||||
void loop(void)
|
void loop(void)
|
||||||
{
|
{
|
||||||
/* Get a new sensor event */
|
/* Get a new sensor event */
|
||||||
sensors_event_t event;
|
sensors_event_t event;
|
||||||
bmp.getEvent(&event);
|
bmp.getEvent(&event);
|
||||||
@@ -87,12 +88,37 @@ void loop(void)
|
|||||||
/* Display the results (barometric pressure is measure in hPa) */
|
/* Display the results (barometric pressure is measure in hPa) */
|
||||||
if (event.pressure)
|
if (event.pressure)
|
||||||
{
|
{
|
||||||
|
/* Display atmospheric pressue in hPa */
|
||||||
Serial.print(event.pressure); Serial.print(" hPa");
|
Serial.print(event.pressure); Serial.print(" hPa");
|
||||||
Serial.print(" ("); Serial.print(bmp.pressureToAltitude(event.pressure)); Serial.println(" m)");
|
|
||||||
|
/* Calculating altitude with reasonable accuracy requires pressure *
|
||||||
|
* sea level pressure for your position at the moment the data is *
|
||||||
|
* converted, as well as the ambient temperature in degress *
|
||||||
|
* celcius. If you don't have these values, a 'generic' value of *
|
||||||
|
* 1013.25 hPa can be used (defined as SENSORS_PRESSURE_SEALEVELHPA *
|
||||||
|
* in sensors.h), but this isn't ideal and will give variable *
|
||||||
|
* results from one day to the next. *
|
||||||
|
* *
|
||||||
|
* You can usually find the current SLP value by looking at weather *
|
||||||
|
* websites or from environmental information centers near any major *
|
||||||
|
* airport. *
|
||||||
|
* *
|
||||||
|
* For example, for Paris, France you can check the current mean *
|
||||||
|
* pressure and sea level at: http://bit.ly/16Au8ol */
|
||||||
|
|
||||||
|
float seaLevelPressure = SENSORS_PRESSURE_SEALEVELHPA;
|
||||||
|
float temperature;
|
||||||
|
bmp.getTemperature(&temperature);
|
||||||
|
|
||||||
|
Serial.print(" (");
|
||||||
|
Serial.print(bmp.pressureToAltitude(seaLevelPressure,
|
||||||
|
event.pressure,
|
||||||
|
temperature));
|
||||||
|
Serial.println(" m)");
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
Serial.println("Sensor error");
|
Serial.println("Sensor error");
|
||||||
}
|
}
|
||||||
delay(250);
|
delay(250);
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user