KalmanEstimator.java

  1. /* Copyright 2002-2019 CS Systèmes d'Information
  2.  * Licensed to CS Systèmes d'Information (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.propagation.conversion.NumericalPropagatorBuilder;
  30. import org.orekit.propagation.conversion.PropagatorBuilder;
  31. import org.orekit.propagation.numerical.NumericalPropagator;
  32. import org.orekit.time.AbsoluteDate;
  33. import org.orekit.utils.ParameterDriver;
  34. import org.orekit.utils.ParameterDriversList;
  35. import org.orekit.utils.ParameterDriversList.DelegatingDriver;


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

  69.     /** Builders for numerical propagators. */
  70.     private List<NumericalPropagatorBuilder> propagatorBuilders;

  71.     /** Reference date. */
  72.     private final AbsoluteDate referenceDate;

  73.     /** Kalman filter process model. */
  74.     private final Model processModel;

  75.     /** Filter. */
  76.     private final ExtendedKalmanFilter<MeasurementDecorator> filter;

  77.     /** Observer to retrieve current estimation info. */
  78.     private KalmanObserver observer;

  79.     /** Kalman filter estimator constructor (package private).
  80.      * @param decomposer decomposer to use for the correction phase
  81.      * @param propagatorBuilders propagators builders used to evaluate the orbit.
  82.      * @param processNoiseMatricesProviders providers for process noise matrices
  83.      * @param estimatedMeasurementParameters measurement parameters to estimate
  84.      */
  85.     KalmanEstimator(final MatrixDecomposer decomposer,
  86.                     final List<NumericalPropagatorBuilder> propagatorBuilders,
  87.                     final List<CovarianceMatrixProvider> processNoiseMatricesProviders,
  88.                     final ParameterDriversList estimatedMeasurementParameters) {

  89.         this.propagatorBuilders = propagatorBuilders;
  90.         this.referenceDate      = propagatorBuilders.get(0).getInitialOrbitDate();
  91.         this.observer           = null;

  92.         // Build the process model and measurement model
  93.         this.processModel = new Model(propagatorBuilders, processNoiseMatricesProviders,
  94.                                       estimatedMeasurementParameters);

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

  96.     }

  97.     /** Set the observer.
  98.      * @param observer the observer
  99.      */
  100.     public void setObserver(final KalmanObserver observer) {
  101.         this.observer = observer;
  102.     }

  103.     /** Get the current measurement number.
  104.      * @return current measurement number
  105.      */
  106.     public int getCurrentMeasurementNumber() {
  107.         return processModel.getCurrentMeasurementNumber();
  108.     }

  109.     /** Get the current date.
  110.      * @return current date
  111.      */
  112.     public AbsoluteDate getCurrentDate() {
  113.         return processModel.getCurrentDate();
  114.     }

  115.     /** Get the "physical" estimated state (i.e. not normalized)
  116.      * @return the "physical" estimated state
  117.      */
  118.     public RealVector getPhysicalEstimatedState() {
  119.         return processModel.getPhysicalEstimatedState();
  120.     }

  121.     /** Get the "physical" estimated covariance matrix (i.e. not normalized)
  122.      * @return the "physical" estimated covariance matrix
  123.      */
  124.     public RealMatrix getPhysicalEstimatedCovarianceMatrix() {
  125.         return processModel.getPhysicalEstimatedCovarianceMatrix();
  126.     }

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

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

  156.     /** Get the propagator parameters supported by this estimator.
  157.      * @param estimatedOnly if true, only estimated parameters are returned
  158.      * @return propagator parameters supported by this estimator
  159.      */
  160.     public ParameterDriversList getPropagationParametersDrivers(final boolean estimatedOnly) {

  161.         final ParameterDriversList estimated = new ParameterDriversList();
  162.         for (PropagatorBuilder builder : propagatorBuilders) {
  163.             for (final DelegatingDriver delegating : builder.getPropagationParametersDrivers().getDrivers()) {
  164.                 if (delegating.isSelected() || !estimatedOnly) {
  165.                     for (final ParameterDriver driver : delegating.getRawDrivers()) {
  166.                         estimated.add(driver);
  167.                     }
  168.                 }
  169.             }
  170.         }
  171.         return estimated;
  172.     }

  173.     /** Get the list of estimated measurements parameters.
  174.      * @return the list of estimated measurements parameters
  175.      */
  176.     public ParameterDriversList getEstimatedMeasurementsParameters() {
  177.         return processModel.getEstimatedMeasurementsParameters();
  178.     }

  179.     /** Process a single measurement.
  180.      * <p>
  181.      * Update the filter with the new measurement by calling the estimate method.
  182.      * </p>
  183.      * @param observedMeasurement the measurement to process
  184.      * @return estimated propagators
  185.      */
  186.     public NumericalPropagator[] estimationStep(final ObservedMeasurement<?> observedMeasurement) {
  187.         try {
  188.             final ProcessEstimate estimate = filter.estimationStep(decorate(observedMeasurement));
  189.             processModel.finalizeEstimation(observedMeasurement, estimate);
  190.             if (observer != null) {
  191.                 observer.evaluationPerformed(processModel);
  192.             }
  193.             return processModel.getEstimatedPropagators();
  194.         } catch (MathRuntimeException mrte) {
  195.             throw new OrekitException(mrte);
  196.         }
  197.     }

  198.     /** Process several measurements.
  199.      * @param observedMeasurements the measurements to process in <em>chronologically sorted</em> order
  200.      * @return estimated propagators
  201.      */
  202.     public NumericalPropagator[] processMeasurements(final Iterable<ObservedMeasurement<?>> observedMeasurements) {
  203.         NumericalPropagator[] propagators = null;
  204.         for (ObservedMeasurement<?> observedMeasurement : observedMeasurements) {
  205.             propagators = estimationStep(observedMeasurement);
  206.         }
  207.         return propagators;
  208.     }

  209.     /** Decorate an observed measurement.
  210.      * <p>
  211.      * The "physical" measurement noise matrix is the covariance matrix of the measurement.
  212.      * Normalizing it consists in applying the following equation: Rn[i,j] =  R[i,j]/σ[i]/σ[j]
  213.      * Thus the normalized measurement noise matrix is the matrix of the correlation coefficients
  214.      * between the different components of the measurement.
  215.      * </p>
  216.      * @param observedMeasurement the measurement
  217.      * @return decorated measurement
  218.      */
  219.     private MeasurementDecorator decorate(final ObservedMeasurement<?> observedMeasurement) {

  220.         // Normalized measurement noise matrix contains 1 on its diagonal and correlation coefficients
  221.         // of the measurement on its non-diagonal elements.
  222.         // Indeed, the "physical" measurement noise matrix is the covariance matrix of the measurement
  223.         // Normalizing it leaves us with the matrix of the correlation coefficients
  224.         final RealMatrix covariance;
  225.         if (observedMeasurement instanceof PV) {
  226.             // For PV measurements we do have a covariance matrix and thus a correlation coefficients matrix
  227.             final PV pv = (PV) observedMeasurement;
  228.             covariance = MatrixUtils.createRealMatrix(pv.getCorrelationCoefficientsMatrix());
  229.         } else {
  230.             // For other measurements we do not have a covariance matrix.
  231.             // Thus the correlation coefficients matrix is an identity matrix.
  232.             covariance = MatrixUtils.createRealIdentityMatrix(observedMeasurement.getDimension());
  233.         }

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

  235.     }

  236. }