Skip to content

Commit

Permalink
Use new class Sensors to generate data to test Madgwick estimator.
Browse files Browse the repository at this point in the history
  • Loading branch information
Mayitzin committed Sep 18, 2023
1 parent 3721291 commit 1de2a80
Showing 1 changed file with 17 additions and 22 deletions.
39 changes: 17 additions & 22 deletions tests/test_estimators.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,11 @@
REFERENCE_GRAVITY_VECTOR = np.array([0.0, 0.0, NORMAL_GRAVITY])
ACC_NOISE_STD_DEVIATION = np.linalg.norm(REFERENCE_GRAVITY_VECTOR)/100.0

# Basic parameters
NUM_SAMPLES = 1000
SAMPLING_FREQUENCY = 100.0
THRESHOLD = 8e-2

def __gaussian_filter(in_array: np.ndarray, size: int = 10, sigma: float = 1.0) -> np.ndarray:
"""
Gaussian filter over an array
Expand Down Expand Up @@ -142,17 +147,6 @@ def __centrifugal_force(angular_velocities: np.ndarray) -> np.ndarray:
Ac[:, 2] = angular_velocities[:, 2]
return np.c_[np.cross(Ab, Ac)[:, 0], np.cross(Aa, Ac)[:, 1], np.cross(Aa, Ab)[:, 2]]

# Generate random attitudes
NUM_SAMPLES = 500
SAMPLING_FREQUENCY = 100.0
NOISE_SIGMA = abs(np.random.standard_normal(3) * 0.1) * ahrs.RAD2DEG
ANGULAR_POSITIONS = random_angpos(num_samples=NUM_SAMPLES, span=(-np.pi, np.pi), max_positions=20)
REFERENCE_QUATERNIONS = ahrs.QuaternionArray(rpy=ANGULAR_POSITIONS)
REFERENCE_ROTATIONS = REFERENCE_QUATERNIONS.to_DCM()

# Other parameters
THRESHOLD = 8e-2

class Sensors:
"""
Generate synthetic sensor data of a hypothetical strapdown inertial
Expand Down Expand Up @@ -266,6 +260,13 @@ def generate(self, rotations: np.ndarray) -> None:
self.accelerometers += np.random.standard_normal((self.num_samples, 3)) * self.acc_noise
self.magnetometers += np.random.standard_normal((self.num_samples, 3)) * self.mag_noise

# Generate random attitudes
NOISE_SIGMA = abs(np.random.standard_normal(3) * 0.1) * ahrs.RAD2DEG
ANGULAR_POSITIONS = random_angpos(num_samples=NUM_SAMPLES, span=(-np.pi, np.pi), max_positions=20)
REFERENCE_QUATERNIONS = ahrs.QuaternionArray(rpy=ANGULAR_POSITIONS)
REFERENCE_ROTATIONS = REFERENCE_QUATERNIONS.to_DCM()
SENSOR_DATA = Sensors(quaternions=REFERENCE_QUATERNIONS, freq=SAMPLING_FREQUENCY, gyr_noise=NOISE_SIGMA)

class TestTRIAD(unittest.TestCase):
def setUp(self) -> None:
# Rotated reference vectors + noise
Expand Down Expand Up @@ -612,17 +613,11 @@ def test_wrong_input_vector_types(self):
class TestMadgwick(unittest.TestCase):
def setUp(self) -> None:
# Create random attitudes
num_samples = 1000
a_ref = REFERENCE_GRAVITY_VECTOR
m_ref = ahrs.common.frames.ned2enu(REFERENCE_MAGNETIC_VECTOR)
gyros = random_angvel(num_samples=num_samples, span=(-np.pi, np.pi))
self.Qts = ahrs.QuaternionArray(ahrs.filters.AngularRate(gyros).Q)
rotations = self.Qts.to_DCM()
# Add noise to reference vectors and rotate them by the random attitudes
self.noise_sigma = 1e-2
self.gyr = gyros + np.random.standard_normal((num_samples, 3)) * self.noise_sigma
self.Rg = np.array([R @ a_ref for R in rotations]) + np.random.standard_normal((num_samples, 3)) * self.noise_sigma
self.Rm = np.array([R @ m_ref for R in rotations]) + np.random.standard_normal((num_samples, 3)) * self.noise_sigma
self.Qts = REFERENCE_QUATERNIONS
self.gyr = SENSOR_DATA.gyroscopes
self.Rg = SENSOR_DATA.accelerometers
self.Rm = SENSOR_DATA.magnetometers
self.noise_sigma = np.nanmean(NOISE_SIGMA)
self.gain = np.sqrt(3/4) * self.noise_sigma

def test_imu(self):
Expand Down

0 comments on commit 1de2a80

Please sign in to comment.