KalmanEstimator.java

  1. /* Copyright 2002-2018 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.errors.OrekitExceptionWrapper;
  28. import org.orekit.estimation.measurements.ObservedMeasurement;
  29. import org.orekit.estimation.measurements.PV;
  30. import org.orekit.propagation.conversion.NumericalPropagatorBuilder;
  31. import org.orekit.propagation.conversion.PropagatorBuilder;
  32. import org.orekit.propagation.numerical.NumericalPropagator;
  33. import org.orekit.time.AbsoluteDate;
  34. import org.orekit.utils.ParameterDriver;
  35. import org.orekit.utils.ParameterDriversList;
  36. import org.orekit.utils.ParameterDriversList.DelegatingDriver;


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

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

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

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

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

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

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

  92.         this.propagatorBuilders = propagatorBuilders;
  93.         this.referenceDate      = propagatorBuilders.get(0).getInitialOrbitDate();
  94.         this.observer           = null;

  95.         // Build the process model and measurement model
  96.         this.processModel = new Model(propagatorBuilders, processNoiseMatricesProviders,
  97.                                       estimatedMeasurementParameters);

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

  99.     }

  100.     /** Set the observer.
  101.      * @param observer the observer
  102.      */
  103.     public void setObserver(final KalmanObserver observer) {
  104.         this.observer = observer;
  105.     }

  106.     /** Get the current measurement number.
  107.      * @return current measurement number
  108.      */
  109.     public int getCurrentMeasurementNumber() {
  110.         return processModel.getCurrentMeasurementNumber();
  111.     }

  112.     /** Get the current date.
  113.      * @return current date
  114.      */
  115.     public AbsoluteDate getCurrentDate() {
  116.         return processModel.getCurrentDate();
  117.     }

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

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

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

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

  161.     /** Get the propagator parameters supported by this estimator.
  162.      * @param estimatedOnly if true, only estimated parameters are returned
  163.      * @return propagator parameters supported by this estimator
  164.      * @exception OrekitException if different parameters have the same name
  165.      */
  166.     public ParameterDriversList getPropagationParametersDrivers(final boolean estimatedOnly)
  167.         throws OrekitException {

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

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

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

  209.     /** Process several measurements.
  210.      * @param observedMeasurements the measurements to process in <em>chronologically sorted</em> order
  211.      * @return estimated propagators
  212.      * @throws OrekitException if an error occurred during the estimation
  213.      */
  214.     public NumericalPropagator[] processMeasurements(final Iterable<ObservedMeasurement<?>> observedMeasurements)
  215.         throws OrekitException {
  216.         NumericalPropagator[] propagators = null;
  217.         for (ObservedMeasurement<?> observedMeasurement : observedMeasurements) {
  218.             propagators = estimationStep(observedMeasurement);
  219.         }
  220.         return propagators;
  221.     }

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

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

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

  248.     }

  249. }