diff --git a/spatz/models/pressure.py b/spatz/models/pressure.py new file mode 100644 index 0000000..020dc0c --- /dev/null +++ b/spatz/models/pressure.py @@ -0,0 +1,44 @@ +from typing import Any +import numpy as np + +from enum import Enum + + +class PressUnit: + Pa = 0 + hPa = 1 + + +class Altimeter: + def __init__(self, press_b: float = 1013.25, alt_b: float = 0, unit: PressUnit = PressUnit.hPa): + """A simple model for estimating the altitude based on pressure measurements. + A reference height and the corresponding pressure value can be provided for a more precise model. + + Args: + press_b (float, optional): The pressure measured at the reference altitude. Defaults to 1013.25 hPa. + alt_b (float, optional): The reference altitude in meters. Defaults to 0m. + unit (PressUnit, optional): The unit used for all pressure values. Defaults to PressUnit.hPa. + """ + self.__press_0 = press_b / (1 - (alt_b / 44330)**5255) + + def predict(self, press: float, unit: PressUnit = PressUnit.hPa) -> float: + """Estimates the altitude based on a pressure measurement. + + Based on the formulas provided here: + https://cdn-shop.adafruit.com/datasheets/BST-BMP180-DS000-09.pdf + + Args: + press (float): The pressure measurement. + unit (PressUnit, optional): The unit of the pressure measurement. Defaults to PressUnit.hPa. + + Returns: + float: Returns the estimated altitude in meters. + """ + if unit != PressUnit.hPa: + to_hpa = { + PressUnit.Pa: press / 1e2 + } + + press = to_hpa[unit] + + return 44330 * (1 - (press / self.__press_0)**(1 / 5255))