How to use ellipsoid calibration module

This example shows how the initial calibration for compasses can be produced using an ellipsoid fit on data. These data are expected to cover rotation along the 3 axis.

import km3compass as kc
import matplotlib.pyplot as plt

Loading some data

Load some calibration data

filename = "../tests/compass_3_1101_calibration.csk"
reader = kc.readerCSK(filename)
print(reader.module_IDs)
File loaded, 3141 rows
        1 module(s)
        - 817320249
Number of measurements after removing duplicates : 315
[817320249]

For illustration purpose, let’s display the raw data from the CSK file :

fig, axes = plt.subplots(3, 1, sharex=True)

for i in range(3):
    mag_label = f"AHRS_H{i}"
    axes[i].scatter(reader.df["datetime"], reader.df[mag_label])
    axes[i].set_ylabel(mag_label)
    axes[i].grid()

axes[-1].set_xlabel("Time")

plt.tight_layout()
plot ellipsoid calibration

Apply the calibration procedure

The procedure is applid with calibration_ellipsoid_fit. The result is then recovered as calibration_object

cal = kc.calibration_ellipsoid_fit(reader)
calibration = cal.calibration
print(calibration)
----------------------------------------
Calibration for compass 1101
Type: "AHRS-CALIBRATION-v4", source: "km3compass-0.6.1"
A norm = 1.0093407387084277
A xyz offsets = [ 0.00236272 -0.01580294 -0.0069185 ]
A rotation matrix =
[[ 0.99821321 -0.0093684  -0.00139031]
 [-0.0093684   1.00815135 -0.00553579]
 [-0.00139031 -0.00553579  0.99380922]]
H norm = 0.47169594872620957
H xyz offsets = [-0.02002571 -0.01306807  0.04686185]
H rotation matrix =
[[ 0.96772421  0.01165101 -0.00396049]
 [ 0.01165101  1.01567039 -0.01098966]
 [-0.00396049 -0.01098966  1.0176837 ]]
----------------------------------------

Export the result as a json string

The calibration_object embed a to_json method that convert the calibration in json directly. This method return a json_str, and if a filename is provided as argument will also save it in a file.

json_str = calibration.to_json("test_calib_km3ant.json")
print(json_str)
{"TestType": "AHRS-CALIBRATION-v4", "TestResult": "OK", "UPI": "3.4.3.4/LSM303/3.1101", "TestParameters": [{"Name": "AHRS_Firmware_Version", "Unit": "", "Values": ["CLB"]}, {"Name": "AHRS_Kalman_Filter_Enable", "Unit": "", "Values": [false]}, {"Name": "AHRS_Magnetic_Declination", "Unit": "deg", "Values": [0.0]}, {"Name": "AHRS_Acceleration_Gain", "Unit": "", "Values": [0.001, 0.001, 0.001]}, {"Name": "AHRS_Acceleration_Offset", "Unit": "g/ms^2", "Values": [0.002362717619316212, -0.0158029412139867, -0.006918503708048108]}, {"Name": "AHRS_Acceleration_Rotation", "Unit": "", "Values": [0.9982132104922449, -0.009368402073140618, -0.0013903093590448084, -0.009368402073140616, 1.0081513508065618, -0.005535792894008644, -0.0013903093590446262, -0.0055357928940087155, 0.9938092216523631]}, {"Name": "AHRS_Gyroscope_Gain", "Unit": "", "Values": [8.66, 8.66, 8.66]}, {"Name": "AHRS_Gyroscopic_Rotation", "Unit": "", "Values": [1.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 1.0]}, {"Name": "AHRS_Magnetic_Rotation", "Unit": "", "Values": [0.9677242108159647, 0.011651014599784706, -0.003960487757330733, 0.0116510145997847, 1.0156703942965624, -0.010989659457093485, -0.003960487757330745, -0.010989659457093718, 1.0176836962206754]}, {"Name": "AHRS_Matrix_Column", "Unit": "", "Values": [0, 1, 2, 0, 1, 2, 0, 1, 2]}, {"Name": "AHRS_Matrix_Row", "Unit": "", "Values": [0, 0, 0, 1, 1, 1, 2, 2, 2]}, {"Name": "AHRS_Vector_Index", "Unit": "", "Values": [0, 1, 2]}, {"Name": "AHRS_Magnetic_XMin", "Unit": "G", "Values": [-1.0200257078439041]}, {"Name": "AHRS_Magnetic_XMax", "Unit": "G", "Values": [0.979974292156096]}, {"Name": "AHRS_Magnetic_YMin", "Unit": "G", "Values": [-1.0130680667031269]}, {"Name": "AHRS_Magnetic_YMax", "Unit": "G", "Values": [0.9869319332968731]}, {"Name": "AHRS_Magnetic_ZMin", "Unit": "G", "Values": [-0.9531381530940973]}, {"Name": "AHRS_Magnetic_ZMax", "Unit": "G", "Values": [1.0468618469059028]}]}

Applying the produced calibration

To apply this calibration to the data, the calibration_module is used :

calibrated_data = kc.calibration_module(reader, reader.module_IDs[0], calibration)
DOM mac address : 08:00:30:b7:51:39

The resulting object can be used to display the calibrated data:

fig, axes = plt.subplots(3, 1, sharex=True)

for i in range(3):
    mag_label = f"AHRS_H{i}"
    axes[i].scatter(calibrated_data.df["datetime"], calibrated_data.df[mag_label])
    axes[i].set_ylabel(mag_label)
    axes[i].grid()

axes[-1].set_xlabel("Time")

plt.tight_layout()
plot ellipsoid calibration

Total running time of the script: (0 minutes 0.715 seconds)

Gallery generated by Sphinx-Gallery