Skip to content

Commit

Permalink
Improve code style and readability.
Browse files Browse the repository at this point in the history
  • Loading branch information
Mayitzin committed Mar 29, 2022
1 parent 05bf740 commit 6561849
Show file tree
Hide file tree
Showing 11 changed files with 64 additions and 27 deletions.
3 changes: 2 additions & 1 deletion ahrs/filters/angular.py
Original file line number Diff line number Diff line change
Expand Up @@ -372,7 +372,8 @@ def _compute_all(self):
return Q

def update(self, q: np.ndarray, gyr: np.ndarray, method: str = 'closed', order: int = 1, dt: float = None) -> np.ndarray:
"""Update the quaternion estimation
"""
Update the quaternion estimation
Estimate quaternion :math:`\\mathbf{q}_{t+1}` from given a-priori
quaternion :math:`\\mathbf{q}_t` with a given angular rate measurement
Expand Down
11 changes: 6 additions & 5 deletions ahrs/filters/aqua.py
Original file line number Diff line number Diff line change
Expand Up @@ -696,17 +696,17 @@ def adaptive_gain(gain: float, a_local: np.ndarray, t1: float = 0.1, t2: float =
a_local : numpy.ndarray
Measured local acceleration vector.
t1 : float, default: 0.1
First threshold
First threshold.
t2 : float, default: 0.2
Second threshold
Second threshold.
g : float, default: 9.809196
Reference gravitational acceleration in m/s^2. The estimated gravity in
Munich, Germany (``9.809196``) is used as default reference value.
Returns
-------
alpha : float
Gain factor
Gain factor.
Examples
--------
Expand Down Expand Up @@ -842,7 +842,8 @@ def _compute_all(self):
return Q

def Omega(self, x: np.ndarray) -> np.ndarray:
"""Omega operator.
"""
Omega operator.
Given a vector :math:`\\mathbf{x}\\in\\mathbb{R}^3`, return a
:math:`4\\times 4` matrix of the form:
Expand Down Expand Up @@ -892,7 +893,7 @@ def estimate(self, acc: np.ndarray, mag: np.ndarray = None) -> np.ndarray:
Quaternion from Earth-Field Observations
Algebraic estimation of a quaternion as a function of an observation of
the Earth's gravitational and magnetic fields.
the Earth's gravitational force and magnetic fields.
It decomposes the quaternion :math:`\\mathbf{q}` into two auxiliary
quaternions :math:`\\mathbf{q}_{\\mathrm{acc}}` and
Expand Down
12 changes: 8 additions & 4 deletions ahrs/filters/ekf.py
Original file line number Diff line number Diff line change
Expand Up @@ -873,8 +873,12 @@
"""

import numpy as np
from ..common.orientation import q2R, ecompass, acc2q
from ..common.mathfuncs import cosd, sind, skew
from ..common.orientation import q2R
from ..common.orientation import ecompass
from ..common.orientation import acc2q
from ..common.mathfuncs import cosd
from ..common.mathfuncs import sind
from ..common.mathfuncs import skew

class EKF:
"""
Expand Down Expand Up @@ -1017,7 +1021,7 @@ def _set_reference_frames(self, mref: float, frame: str = 'NED') -> None:
# Magnetic Reference Vector
if mref is None:
# Local magnetic reference of Munich, Germany
from ..common.mathfuncs import MUNICH_LATITUDE, MUNICH_LONGITUDE, MUNICH_HEIGHT
from ..common.constants import MUNICH_LATITUDE, MUNICH_LONGITUDE, MUNICH_HEIGHT
from ..utils.wmm import WMM
wmm = WMM(latitude=MUNICH_LATITUDE, longitude=MUNICH_LONGITUDE, height=MUNICH_HEIGHT)
self.m_ref = np.array([wmm.X, wmm.Y, wmm.Z]) if frame.upper() == 'NED' else np.array([wmm.Y, wmm.X, -wmm.Z])
Expand Down Expand Up @@ -1324,7 +1328,7 @@ def update(self, q: np.ndarray, gyr: np.ndarray, acc: np.ndarray, mag: np.ndarra
if mag is not None:
m_norm = np.linalg.norm(mag)
if m_norm == 0:
raise ValueError(f"Invalid geomagnetic field. Its magnitude must be greater than zero.")
raise ValueError("Invalid geomagnetic field. Its magnitude must be greater than zero.")
self.z = np.r_[a, mag/m_norm]
self.R = np.diag(np.repeat(self.noises[1:] if mag is not None else self.noises[1], 3))
# ----- Prediction -----
Expand Down
9 changes: 7 additions & 2 deletions ahrs/filters/flae.py
Original file line number Diff line number Diff line change
Expand Up @@ -334,7 +334,11 @@

import warnings
import numpy as np
from ..common.mathfuncs import *
from ..common.mathfuncs import cosd
from ..common.mathfuncs import sind
from ..common.constants import MUNICH_LATITUDE
from ..common.constants import MUNICH_LONGITUDE
from ..common.constants import MUNICH_HEIGHT

warnings.filterwarnings('error')

Expand All @@ -352,7 +356,8 @@ def _assert_valid_method(method : str, valid_methods : list) -> None:
raise ValueError(f"Given method '{method}' is not valid. Try '{joint_methods}'")

class FLAE:
"""Fast Linear Attitude Estimator
"""
Fast Linear Attitude Estimator
Parameters
----------
Expand Down
6 changes: 4 additions & 2 deletions ahrs/filters/fqa.py
Original file line number Diff line number Diff line change
Expand Up @@ -232,10 +232,12 @@
"""

import numpy as np
from ..common.constants import *
from ..common.constants import MUNICH_LATITUDE
from ..common.constants import MUNICH_LONGITUDE
from ..common.constants import MUNICH_HEIGHT
from ..common.orientation import q_prod, q_conj

# Reference Observations in Munich, Germany
# Reference Observations in Munich, Germany at current date
from ..utils.wmm import WMM
MAG = WMM(latitude=MUNICH_LATITUDE, longitude=MUNICH_LONGITUDE, height=MUNICH_HEIGHT).magnetic_elements

Expand Down
2 changes: 1 addition & 1 deletion ahrs/filters/madgwick.py
Original file line number Diff line number Diff line change
Expand Up @@ -517,7 +517,7 @@ def __init__(self, gyr: np.ndarray = None, acc: np.ndarray = None, mag: np.ndarr
self.Q = self._compute_all()

def _set_gain(self, **kwargs) -> None:
"""Set the gain parameter"""
"""Set the gain parameter."""
self.gain_imu: float = kwargs.get('gain_imu', 0.033)
self.gain_marg: float = kwargs.get('gain_marg', 0.041)
self.gain: float = kwargs.get('beta') # Setting gain with `beta` will be removed in the future.
Expand Down
3 changes: 2 additions & 1 deletion ahrs/filters/mahony.py
Original file line number Diff line number Diff line change
Expand Up @@ -271,7 +271,8 @@


class Mahony:
"""Mahony's Nonlinear Complementary Filter on SO(3)
"""
Mahony's Nonlinear Complementary Filter on SO(3)
If ``acc`` and ``gyr`` are given as parameters, the orientations will be
immediately computed with method ``updateIMU``.
Expand Down
13 changes: 10 additions & 3 deletions ahrs/filters/quest.py
Original file line number Diff line number Diff line change
Expand Up @@ -202,9 +202,14 @@
from typing import Union
import numpy as np

from ..common.mathfuncs import *
from ..common.mathfuncs import cosd
from ..common.mathfuncs import sind
from ..common.constants import MUNICH_LATITUDE
from ..common.constants import MUNICH_LONGITUDE
from ..common.constants import MUNICH_HEIGHT
from ..utils.wmm import WMM
from ..utils.wgs84 import WGS

# Reference Observations in Munich, Germany
GRAVITY = WGS().normal_gravity(MUNICH_LATITUDE, MUNICH_HEIGHT)
wmm = WMM(latitude=MUNICH_LATITUDE, longitude=MUNICH_LONGITUDE, height=MUNICH_HEIGHT)
Expand Down Expand Up @@ -280,7 +285,8 @@ def __init__(self, acc: np.ndarray = None, mag: np.ndarray = None, **kw):
self.Q = self._compute_all()

def _compute_all(self) -> np.ndarray:
"""Estimate the quaternions given all data.
"""
Estimate the quaternions given all data.
Attributes ``acc`` and ``mag`` must contain data.
Expand All @@ -299,7 +305,8 @@ def _compute_all(self) -> np.ndarray:
return np.array([self.estimate(self.acc[t], self.mag[t]) for t in range(num_samples)])

def estimate(self, acc: np.ndarray, mag: np.ndarray) -> np.ndarray:
"""Attitude Estimation.
"""
Attitude Estimation.
Parameters
----------
Expand Down
6 changes: 5 additions & 1 deletion ahrs/filters/triad.py
Original file line number Diff line number Diff line change
Expand Up @@ -243,7 +243,11 @@

import numpy as np
from ..common.orientation import chiaverini
from ..common.mathfuncs import *
from ..common.constants import MUNICH_LATITUDE
from ..common.constants import MUNICH_LONGITUDE
from ..common.constants import MUNICH_HEIGHT
from ..common.mathfuncs import cosd
from ..common.mathfuncs import sind

# Reference Observations in Munich, Germany
from ..utils.wmm import WMM
Expand Down
8 changes: 5 additions & 3 deletions ahrs/utils/wgs84.py
Original file line number Diff line number Diff line change
Expand Up @@ -495,7 +495,8 @@ def __init__(self, a: float = EARTH_EQUATOR_RADIUS, f: float = EARTH_FLATTENING,
self.is_geodetic &= np.isclose(self.w, EARTH_ROTATION)

def normal_gravity(self, lat: float, h: float = 0.0) -> float:
"""Normal Gravity on (or above) Ellipsoidal Surface
"""
Normal Gravity on (or above) Ellipsoidal Surface
Estimate the normal gravity on or above the surface of an ellipsoidal
body using Somigliana's formula (on surface) and a series expansion
Expand Down Expand Up @@ -1060,7 +1061,8 @@ def dynamic_inertial_moment_about_Y(self):
return np.sqrt(5)*self.mass*self.a**2*((1-1/DYNAMIC_ELLIPTICITY)*EARTH_C20_DYN + EARTH_C22_DYN/np.sqrt(3))

class WGSTest(unittest.TestCase):
"""Test Gravitational Models
"""
Test Gravitational Models
All values used to test the WGS model are retrieved from its latest report
as defined in Appendix B.
Expand All @@ -1070,7 +1072,7 @@ class WGSTest(unittest.TestCase):
"""
def test_wgs84(self):
"""Test WGS84 with Earth's properties"""
"""Test WGS84 with Earth's properties."""
wgs = WGS()
self.assertAlmostEqual(wgs.a, 6_378_137.0, 1)
self.assertAlmostEqual(wgs.f, 1/298.257223563, 7)
Expand Down
18 changes: 14 additions & 4 deletions ahrs/utils/wmm.py
Original file line number Diff line number Diff line change
Expand Up @@ -214,9 +214,17 @@
import pkgutil
from io import StringIO
from typing import Union, Tuple, Dict

# Third-Party Dependencies
import numpy as np
from ..common.constants import *
from ..common.constants import EARTH_EQUATOR_RADIUS
from ..common.constants import EARTH_POLAR_RADIUS
from ..common.constants import EARTH_MEAN_RADIUS
from ..common.constants import MUNICH_LATITUDE
from ..common.constants import MUNICH_LONGITUDE
from ..common.constants import MUNICH_HEIGHT
from ..common.constants import DEG2RAD
from ..common.constants import RAD2DEG
from ..common.frames import ned2enu

def geodetic2spherical(lat: float, lon: float, h: float, a: float = EARTH_EQUATOR_RADIUS/1000.0, b: float = EARTH_POLAR_RADIUS/1000.0) -> Tuple[float, float, float]:
Expand Down Expand Up @@ -555,7 +563,8 @@ def reset_date(self, date: Union[datetime.date, int, float]) -> None:
self.wmm_filename = 'WMM2015/WMM.COF' if self.date_dec < 2020.0 else 'WMM2020/WMM.COF'

def denormalize_coefficients(self, latitude: float) -> None:
"""Recursively estimate associated Legendre polynomials and derivatives
"""
Recursively estimate associated Legendre polynomials and derivatives
done in a recursive way as described by Michael Plett in [Wertz]_ for
an efficient computation.
Expand Down Expand Up @@ -594,7 +603,7 @@ def denormalize_coefficients(self, latitude: float) -> None:
where :math:`K` is either :math:`(n-m)/2` or :math:`(n-m-1)/2`,
whichever is an integer. For a computational improvement, the terms are
calculated recursively.
We have to denormalize the coefficients from Schmidt to Gauss. The
Gauss functions :math:`P^{n, m}` are related to Schmidt functions
:math:`P_n^m` as:
Expand Down Expand Up @@ -850,7 +859,8 @@ def magnetic_field(self, latitude: float, longitude: float, height: float = 0.0,

@property
def magnetic_elements(self) -> Dict[str, float]:
"""Main geomagnetic elements in a dictionary
"""
Main geomagnetic elements in a dictionary
======= =============================================
Element Definition
Expand Down

0 comments on commit 6561849

Please sign in to comment.