KalmanEstimator.java

  1. /* Copyright 2002-2022 CS GROUP
  2.  * Licensed to CS GROUP (CS) under one or more
  3.  * contributor license agreements.  See the NOTICE file distributed with
  4.  * this work for additional information regarding copyright ownership.
  5.  * CS licenses this file to You under the Apache License, Version 2.0
  6.  * (the "License"); you may not use this file except in compliance with
  7.  * the License.  You may obtain a copy of the License at
  8.  *
  9.  *   http://www.apache.org/licenses/LICENSE-2.0
  10.  *
  11.  * Unless required by applicable law or agreed to in writing, software
  12.  * distributed under the License is distributed on an "AS IS" BASIS,
  13.  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14.  * See the License for the specific language governing permissions and
  15.  * limitations under the License.
  16.  */
  17. package org.orekit.estimation.sequential;

  18. import java.util.List;

  19. import org.hipparchus.exception.MathRuntimeException;
  20. import org.hipparchus.filtering.kalman.ProcessEstimate;
  21. import org.hipparchus.filtering.kalman.extended.ExtendedKalmanFilter;
  22. import org.hipparchus.linear.MatrixDecomposer;
  23. import org.hipparchus.linear.MatrixUtils;
  24. import org.hipparchus.linear.RealMatrix;
  25. import org.hipparchus.linear.RealVector;
  26. import org.orekit.errors.OrekitException;
  27. import org.orekit.estimation.measurements.ObservedMeasurement;
  28. import org.orekit.estimation.measurements.PV;
  29. import org.orekit.estimation.measurements.Position;
  30. import org.orekit.propagation.Propagator;
  31. import org.orekit.propagation.conversion.OrbitDeterminationPropagatorBuilder;
  32. import org.orekit.propagation.conversion.PropagatorBuilder;
  33. import org.orekit.propagation.numerical.NumericalPropagator;
  34. import org.orekit.propagation.semianalytical.dsst.DSSTPropagator;
  35. import org.orekit.time.AbsoluteDate;
  36. import org.orekit.utils.ParameterDriver;
  37. import org.orekit.utils.ParameterDriversList;
  38. import org.orekit.utils.ParameterDriversList.DelegatingDriver;


  39. /**
  40.  * Implementation of a Kalman filter to perform orbit determination.
  41.  * <p>
  42.  * The filter uses a {@link OrbitDeterminationPropagatorBuilder} to initialize its reference trajectory {@link NumericalPropagator}
  43.  * or {@link DSSTPropagator} .
  44.  * </p>
  45.  * <p>
  46.  * The estimated parameters are driven by {@link ParameterDriver} objects. They are of 3 different types:<ol>
  47.  *   <li><b>Orbital parameters</b>:The position and velocity of the spacecraft, or, more generally, its orbit.<br>
  48.  *       These parameters are retrieved from the reference trajectory propagator builder when the filter is initialized.</li>
  49.  *   <li><b>Propagation parameters</b>: Some parameters modelling physical processes (SRP or drag coefficients etc...).<br>
  50.  *       They are also retrieved from the propagator builder during the initialization phase.</li>
  51.  *   <li><b>Measurements parameters</b>: Parameters related to measurements (station biases, positions etc...).<br>
  52.  *       They are passed down to the filter in its constructor.</li>
  53.  * </ol>
  54.  * <p>
  55.  * The total number of estimated parameters is m, the size of the state vector.
  56.  * </p>
  57.  * <p>
  58.  * The Kalman filter implementation used is provided by the underlying mathematical library Hipparchus.
  59.  * All the variables seen by Hipparchus (states, covariances, measurement matrices...) are normalized
  60.  * using a specific scale for each estimated parameters or standard deviation noise for each measurement components.
  61.  * </p>
  62.  *
  63.  * <p>A {@link KalmanEstimator} object is built using the {@link KalmanEstimatorBuilder#build() build}
  64.  * method of a {@link KalmanEstimatorBuilder}.</p>
  65.  *
  66.  * @author Romain Gerbaud
  67.  * @author Maxime Journot
  68.  * @author Luc Maisonobe
  69.  * @since 9.2
  70.  */
  71. public class KalmanEstimator {

  72.     /** Builders for orbit propagators. */
  73.     private List<OrbitDeterminationPropagatorBuilder> propagatorBuilders;

  74.     /** Reference date. */
  75.     private final AbsoluteDate referenceDate;

  76.     /** Kalman filter process model. */
  77.     private final AbstractKalmanModel processModel;

  78.     /** Filter. */
  79.     private final ExtendedKalmanFilter<MeasurementDecorator> filter;

  80.     /** Observer to retrieve current estimation info. */
  81.     private KalmanObserver observer;

  82.     /** Kalman filter estimator constructor (package private).
  83.      * @param decomposer decomposer to use for the correction phase
  84.      * @param propagatorBuilders propagators builders used to evaluate the orbit.
  85.      * @param processNoiseMatricesProviders providers for process noise matrices
  86.      * @param estimatedMeasurementParameters measurement parameters to estimate
  87.      * @param measurementProcessNoiseMatrix provider for measurement process noise matrix
  88.      * @since 10.3
  89.      */
  90.     KalmanEstimator(final MatrixDecomposer decomposer,
  91.                     final List<OrbitDeterminationPropagatorBuilder> propagatorBuilders,
  92.                     final List<CovarianceMatrixProvider> processNoiseMatricesProviders,
  93.                     final ParameterDriversList estimatedMeasurementParameters,
  94.                     final CovarianceMatrixProvider measurementProcessNoiseMatrix) {

  95.         this.propagatorBuilders = propagatorBuilders;
  96.         this.referenceDate      = propagatorBuilders.get(0).getInitialOrbitDate();
  97.         this.observer           = null;

  98.         // Build the process model and measurement model
  99.         this.processModel = propagatorBuilders.get(0).buildKalmanModel(propagatorBuilders,
  100.                                                                        processNoiseMatricesProviders,
  101.                                                                        estimatedMeasurementParameters,
  102.                                                                        measurementProcessNoiseMatrix);

  103.         this.filter = new ExtendedKalmanFilter<>(decomposer, processModel, processModel.getEstimate());

  104.     }

  105.     /** Set the observer.
  106.      * @param observer the observer
  107.      */
  108.     public void setObserver(final KalmanObserver observer) {
  109.         this.observer = observer;
  110.     }

  111.     /** Get the current measurement number.
  112.      * @return current measurement number
  113.      */
  114.     public int getCurrentMeasurementNumber() {
  115.         return processModel.getCurrentMeasurementNumber();
  116.     }

  117.     /** Get the current date.
  118.      * @return current date
  119.      */
  120.     public AbsoluteDate getCurrentDate() {
  121.         return processModel.getCurrentDate();
  122.     }

  123.     /** Get the "physical" estimated state (i.e. not normalized)
  124.      * @return the "physical" estimated state
  125.      */
  126.     public RealVector getPhysicalEstimatedState() {
  127.         return processModel.getPhysicalEstimatedState();
  128.     }

  129.     /** Get the "physical" estimated covariance matrix (i.e. not normalized)
  130.      * @return the "physical" estimated covariance matrix
  131.      */
  132.     public RealMatrix getPhysicalEstimatedCovarianceMatrix() {
  133.         return processModel.getPhysicalEstimatedCovarianceMatrix();
  134.     }

  135.     /** Get the orbital parameters supported by this estimator.
  136.      * <p>
  137.      * If there are more than one propagator builder, then the names
  138.      * of the drivers have an index marker in square brackets appended
  139.      * to them in order to distinguish the various orbits. So for example
  140.      * with one builder generating Keplerian orbits the names would be
  141.      * simply "a", "e", "i"... but if there are several builders the
  142.      * names would be "a[0]", "e[0]", "i[0]"..."a[1]", "e[1]", "i[1]"...
  143.      * </p>
  144.      * @param estimatedOnly if true, only estimated parameters are returned
  145.      * @return orbital parameters supported by this estimator
  146.      */
  147.     public ParameterDriversList getOrbitalParametersDrivers(final boolean estimatedOnly) {

  148.         final ParameterDriversList estimated = new ParameterDriversList();
  149.         for (int i = 0; i < propagatorBuilders.size(); ++i) {
  150.             final String suffix = propagatorBuilders.size() > 1 ? "[" + i + "]" : null;
  151.             for (final ParameterDriver driver : propagatorBuilders.get(i).getOrbitalParametersDrivers().getDrivers()) {
  152.                 if (driver.isSelected() || !estimatedOnly) {
  153.                     if (suffix != null && !driver.getName().endsWith(suffix)) {
  154.                         // we add suffix only conditionally because the method may already have been called
  155.                         // and suffixes may have already been appended
  156.                         driver.setName(driver.getName() + suffix);
  157.                     }
  158.                     estimated.add(driver);
  159.                 }
  160.             }
  161.         }
  162.         return estimated;
  163.     }

  164.     /** Get the propagator parameters supported by this estimator.
  165.      * @param estimatedOnly if true, only estimated parameters are returned
  166.      * @return propagator parameters supported by this estimator
  167.      */
  168.     public ParameterDriversList getPropagationParametersDrivers(final boolean estimatedOnly) {

  169.         final ParameterDriversList estimated = new ParameterDriversList();
  170.         for (PropagatorBuilder builder : propagatorBuilders) {
  171.             for (final DelegatingDriver delegating : builder.getPropagationParametersDrivers().getDrivers()) {
  172.                 if (delegating.isSelected() || !estimatedOnly) {
  173.                     for (final ParameterDriver driver : delegating.getRawDrivers()) {
  174.                         estimated.add(driver);
  175.                     }
  176.                 }
  177.             }
  178.         }
  179.         return estimated;
  180.     }

  181.     /** Get the list of estimated measurements parameters.
  182.      * @return the list of estimated measurements parameters
  183.      */
  184.     public ParameterDriversList getEstimatedMeasurementsParameters() {
  185.         return processModel.getEstimatedMeasurementsParameters();
  186.     }

  187.     /** Process a single measurement.
  188.      * <p>
  189.      * Update the filter with the new measurement by calling the estimate method.
  190.      * </p>
  191.      * @param observedMeasurement the measurement to process
  192.      * @return estimated propagators
  193.      */
  194.     public Propagator[] estimationStep(final ObservedMeasurement<?> observedMeasurement) {
  195.         try {
  196.             final ProcessEstimate estimate = filter.estimationStep(decorate(observedMeasurement));
  197.             processModel.finalizeEstimation(observedMeasurement, estimate);
  198.             if (observer != null) {
  199.                 observer.evaluationPerformed(processModel);
  200.             }
  201.             return processModel.getEstimatedPropagators();
  202.         } catch (MathRuntimeException mrte) {
  203.             throw new OrekitException(mrte);
  204.         }
  205.     }

  206.     /** Process several measurements.
  207.      * @param observedMeasurements the measurements to process in <em>chronologically sorted</em> order
  208.      * @return estimated propagators
  209.      */
  210.     public Propagator[] processMeasurements(final Iterable<ObservedMeasurement<?>> observedMeasurements) {
  211.         Propagator[] propagators = null;
  212.         for (ObservedMeasurement<?> observedMeasurement : observedMeasurements) {
  213.             propagators = estimationStep(observedMeasurement);
  214.         }
  215.         return propagators;
  216.     }

  217.     /** Decorate an observed measurement.
  218.      * <p>
  219.      * The "physical" measurement noise matrix is the covariance matrix of the measurement.
  220.      * Normalizing it consists in applying the following equation: Rn[i,j] =  R[i,j]/σ[i]/σ[j]
  221.      * Thus the normalized measurement noise matrix is the matrix of the correlation coefficients
  222.      * between the different components of the measurement.
  223.      * </p>
  224.      * @param observedMeasurement the measurement
  225.      * @return decorated measurement
  226.      */
  227.     private MeasurementDecorator decorate(final ObservedMeasurement<?> observedMeasurement) {

  228.         // Normalized measurement noise matrix contains 1 on its diagonal and correlation coefficients
  229.         // of the measurement on its non-diagonal elements.
  230.         // Indeed, the "physical" measurement noise matrix is the covariance matrix of the measurement
  231.         // Normalizing it leaves us with the matrix of the correlation coefficients
  232.         final RealMatrix covariance;
  233.         if (observedMeasurement instanceof PV) {
  234.             // For PV measurements we do have a covariance matrix and thus a correlation coefficients matrix
  235.             final PV pv = (PV) observedMeasurement;
  236.             covariance = MatrixUtils.createRealMatrix(pv.getCorrelationCoefficientsMatrix());
  237.         } else if (observedMeasurement instanceof Position) {
  238.             // For Position measurements we do have a covariance matrix and thus a correlation coefficients matrix
  239.             final Position position = (Position) observedMeasurement;
  240.             covariance = MatrixUtils.createRealMatrix(position.getCorrelationCoefficientsMatrix());
  241.         } else {
  242.             // For other measurements we do not have a covariance matrix.
  243.             // Thus the correlation coefficients matrix is an identity matrix.
  244.             covariance = MatrixUtils.createRealIdentityMatrix(observedMeasurement.getDimension());
  245.         }

  246.         return new MeasurementDecorator(observedMeasurement, covariance, referenceDate);

  247.     }

  248. }