MSACRobustKnownFrameMagnetometerCalibrator.java
/*
* Copyright (C) 2020 Alberto Irurueta Carro (alberto@irurueta.com)
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.irurueta.navigation.inertial.calibration.magnetometer;
import com.irurueta.navigation.LockedException;
import com.irurueta.navigation.NotReadyException;
import com.irurueta.navigation.inertial.calibration.CalibrationException;
import com.irurueta.navigation.inertial.calibration.StandardDeviationFrameBodyMagneticFluxDensity;
import com.irurueta.numerical.robust.MSACRobustEstimator;
import com.irurueta.numerical.robust.MSACRobustEstimatorListener;
import com.irurueta.numerical.robust.RobustEstimator;
import com.irurueta.numerical.robust.RobustEstimatorException;
import com.irurueta.numerical.robust.RobustEstimatorMethod;
import java.io.IOException;
import java.util.List;
/**
* Robustly estimates magnetometer hard-iron biases, soft-iron cross
* couplings and scaling factors using MSAC algorithm.
* <p>
* To use this calibrator at least 4 measurements at different known
* frames must be provided. In other words, magnetometer samples must
* be obtained at 4 different positions or orientations.
* Notice that frame velocities are ignored by this calibrator.
* <p>
* Measured magnetic flux density is assumed to follow the model shown below:
* <pre>
* mBmeas = bm + (I + Mm) * mBtrue + w
* </pre>
* Where:
* - mBmeas is the measured magnetic flux density. This is a 3x1 vector.
* - bm is magnetometer hard-iron bias. Ideally, on a perfect magnetometer,
* this should be a 3x1 zero vector.
* - I is the 3x3 identity matrix.
* - Mm is the 3x3 soft-iron matrix containing cross-couplings and scaling
* factors. Ideally, on a perfect magnetometer, this should be a 3x3 zero
* matrix.
* - mBtrue is ground-truth magnetic flux density. This is a 3x1 vector.
* - w is measurement noise. This is a 3x1 vector.
*/
public class MSACRobustKnownFrameMagnetometerCalibrator extends RobustKnownFrameMagnetometerCalibrator {
/**
* Constant defining default threshold to determine whether samples are
* inliers or not.
*/
public static final double DEFAULT_THRESHOLD = 1e-2;
/**
* Minimum value that can be set as threshold.
* Threshold must be strictly greater than 0.0.
*/
public static final double MIN_THRESHOLD = 0.0;
/**
* Threshold to determine whether samples are inliers or not when
* testing possible estimation solutions.
*/
private double threshold = DEFAULT_THRESHOLD;
/**
* Constructor.
*/
public MSACRobustKnownFrameMagnetometerCalibrator() {
super();
}
/**
* Constructor.
*
* @param listener listener to be notified of events such as when estimation
* starts, ends or its progress significantly changes.
*/
public MSACRobustKnownFrameMagnetometerCalibrator(final RobustKnownFrameMagnetometerCalibratorListener listener) {
super(listener);
}
/**
* Constructor.
*
* @param measurements list of body magnetic flux density measurements with standard
* deviations taken at different frames (positions and
* orientations).
*/
public MSACRobustKnownFrameMagnetometerCalibrator(
final List<StandardDeviationFrameBodyMagneticFluxDensity> measurements) {
super(measurements);
}
/**
* Constructor.
*
* @param measurements list of body magnetic flux density measurements with standard
* deviations taken at different frames (positions and
* orientations).
* @param listener listener to handle events raised by this calibrator.
*/
public MSACRobustKnownFrameMagnetometerCalibrator(
final List<StandardDeviationFrameBodyMagneticFluxDensity> measurements,
final RobustKnownFrameMagnetometerCalibratorListener listener) {
super(measurements, listener);
}
/**
* Constructor.
*
* @param commonAxisUsed indicates whether z-axis is assumed to be common
* for the accelerometer, gyroscope and magnetometer.
*/
public MSACRobustKnownFrameMagnetometerCalibrator(final boolean commonAxisUsed) {
super(commonAxisUsed);
}
/**
* Constructor.
*
* @param commonAxisUsed indicates whether z-axis is assumed to be common
* for the accelerometer, gyroscope and magnetometer.
* @param listener listener to handle events raised by this calibrator.
*/
public MSACRobustKnownFrameMagnetometerCalibrator(
final boolean commonAxisUsed, final RobustKnownFrameMagnetometerCalibratorListener listener) {
super(commonAxisUsed, listener);
}
/**
* Constructor.
*
* @param measurements list of body magnetic flux density measurements with standard
* deviations taken at different frames (positions and
* orientations).
* @param commonAxisUsed indicates whether z-axis is assumed to be common
* for the accelerometer, gyroscope and magnetometer.
*/
public MSACRobustKnownFrameMagnetometerCalibrator(
final List<StandardDeviationFrameBodyMagneticFluxDensity> measurements, final boolean commonAxisUsed) {
super(measurements, commonAxisUsed);
}
/**
* Constructor.
*
* @param measurements list of body magnetic flux density measurements with standard
* deviations taken at different frames (positions and
* orientations).
* @param commonAxisUsed indicates whether z-axis is assumed to be common
* for the accelerometer, gyroscope and magnetometer.
* @param listener listener to handle events raised by this calibrator.
*/
public MSACRobustKnownFrameMagnetometerCalibrator(
final List<StandardDeviationFrameBodyMagneticFluxDensity> measurements, final boolean commonAxisUsed,
final RobustKnownFrameMagnetometerCalibratorListener listener) {
super(measurements, commonAxisUsed, listener);
}
/**
* Returns threshold to determine whether samples are inliers or not.
*
* @return threshold to determine whether samples are inliers or not.
*/
public double getThreshold() {
return threshold;
}
/**
* Sets threshold to determine whether samples are inliers or not.
*
* @param threshold threshold to be set.
* @throws IllegalArgumentException if provided value is equal or less than
* zero.
* @throws LockedException if calibrator is currently running.
*/
public void setThreshold(final double threshold) throws LockedException {
if (running) {
throw new LockedException();
}
if (threshold <= MIN_THRESHOLD) {
throw new IllegalArgumentException();
}
this.threshold = threshold;
}
/**
* Estimates magnetometer calibration parameters containing hard-iron
* bias and soft-iron scale factors and cross-coupling errors.
*
* @throws LockedException if calibrator is currently running.
* @throws NotReadyException if calibrator is not ready.
* @throws CalibrationException if estimation fails for numerical reasons.
*/
@SuppressWarnings("DuplicatedCode")
@Override
public void calibrate() throws LockedException, NotReadyException, CalibrationException {
if (running) {
throw new LockedException();
}
if (!isReady()) {
throw new NotReadyException();
}
final var innerEstimator = new MSACRobustEstimator<>(new MSACRobustEstimatorListener<PreliminaryResult>() {
@Override
public double getThreshold() {
return threshold;
}
@Override
public int getTotalSamples() {
return measurements.size();
}
@Override
public int getSubsetSize() {
return preliminarySubsetSize;
}
@Override
public void estimatePreliminarSolutions(
final int[] samplesIndices, final List<PreliminaryResult> solutions) {
computePreliminarySolutions(samplesIndices, solutions);
}
@Override
public double computeResidual(final PreliminaryResult currentEstimation, final int i) {
return computeError(measurements.get(i), currentEstimation);
}
@Override
public boolean isReady() {
return MSACRobustKnownFrameMagnetometerCalibrator.super.isReady();
}
@Override
public void onEstimateStart(final RobustEstimator<PreliminaryResult> estimator) {
// no action needed
}
@Override
public void onEstimateEnd(final RobustEstimator<PreliminaryResult> estimator) {
// no action needed
}
@Override
public void onEstimateNextIteration(
final RobustEstimator<PreliminaryResult> estimator, final int iteration) {
if (listener != null) {
listener.onCalibrateNextIteration(
MSACRobustKnownFrameMagnetometerCalibrator.this, iteration);
}
}
@Override
public void onEstimateProgressChange(
final RobustEstimator<PreliminaryResult> estimator, final float progress) {
if (listener != null) {
listener.onCalibrateProgressChange(
MSACRobustKnownFrameMagnetometerCalibrator.this, progress);
}
}
});
try {
running = true;
if (listener != null) {
listener.onCalibrateStart(this);
}
inliersData = null;
setupWmmEstimator();
innerEstimator.setConfidence(confidence);
innerEstimator.setMaxIterations(maxIterations);
innerEstimator.setProgressDelta(progressDelta);
final var preliminaryResult = innerEstimator.estimate();
inliersData = innerEstimator.getInliersData();
attemptRefine(preliminaryResult);
if (listener != null) {
listener.onCalibrateEnd(this);
}
} catch (final com.irurueta.numerical.LockedException e) {
throw new LockedException(e);
} catch (final com.irurueta.numerical.NotReadyException e) {
throw new NotReadyException(e);
} catch (final RobustEstimatorException | IOException e) {
throw new CalibrationException(e);
} finally {
running = false;
}
}
/**
* Returns method being used for robust estimation.
*
* @return method being used for robust estimation.
*/
@Override
public RobustEstimatorMethod getMethod() {
return RobustEstimatorMethod.MSAC;
}
/**
* Indicates whether this calibrator requires quality scores for each
* measurement or not.
*
* @return true if quality scores are required, false otherwise.
*/
@Override
public boolean isQualityScoresRequired() {
return false;
}
}