PositionAngleDetector.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.propagation.events;

  18. import org.hipparchus.util.FastMath;
  19. import org.hipparchus.util.MathUtils;
  20. import org.orekit.errors.OrekitException;
  21. import org.orekit.errors.OrekitIllegalArgumentException;
  22. import org.orekit.errors.OrekitInternalError;
  23. import org.orekit.errors.OrekitMessages;
  24. import org.orekit.orbits.CircularOrbit;
  25. import org.orekit.orbits.EquinoctialOrbit;
  26. import org.orekit.orbits.KeplerianOrbit;
  27. import org.orekit.orbits.OrbitType;
  28. import org.orekit.orbits.PositionAngle;
  29. import org.orekit.propagation.SpacecraftState;
  30. import org.orekit.propagation.events.handlers.EventHandler;
  31. import org.orekit.propagation.events.handlers.StopOnIncreasing;

  32. /** Detector for in-orbit position angle.
  33.  * <p>
  34.  * The detector is based on anomaly for {@link OrbitType#KEPLERIAN Keplerian}
  35.  * orbits, latitude argument for {@link OrbitType#CIRCULAR circular} orbits,
  36.  * or longitude argument for {@link OrbitType#EQUINOCTIAL equinoctial} orbits.
  37.  * It does not support {@link OrbitType#CARTESIAN Cartesian} orbits. The
  38.  * angles can be either {@link PositionAngle#TRUE true}, {link {@link PositionAngle#MEAN
  39.  * mean} or {@link PositionAngle#ECCENTRIC eccentric} angles.
  40.  * </p>
  41.  * @author Luc Maisonobe
  42.  * @since 7.1
  43.  */
  44. public class PositionAngleDetector extends AbstractDetector<PositionAngleDetector> {

  45.     /** Serializable UID. */
  46.     private static final long serialVersionUID = 20150825L;

  47.     /** Orbit type defining the angle type. */
  48.     private final OrbitType orbitType;

  49.     /** Type of position angle. */
  50.     private final PositionAngle positionAngle;

  51.     /** Fixed angle to be crossed. */
  52.     private final double angle;

  53.     /** Sign to apply for angle difference. */
  54.     private double sign;

  55.     /** Previous angle difference. */
  56.     private double previousDelta;

  57.     /** Build a new detector.
  58.      * <p>The new instance uses default values for maximal checking interval
  59.      * ({@link #DEFAULT_MAXCHECK}) and convergence threshold ({@link
  60.      * #DEFAULT_THRESHOLD}).</p>
  61.      * @param orbitType orbit type defining the angle type
  62.      * @param positionAngle type of position angle
  63.      * @param angle fixed angle to be crossed
  64.      * @exception OrekitIllegalArgumentException if orbit type is {@link OrbitType#CARTESIAN}
  65.      */
  66.     public PositionAngleDetector(final OrbitType orbitType, final PositionAngle positionAngle,
  67.                                  final double angle)
  68.         throws OrekitIllegalArgumentException {
  69.         this(DEFAULT_MAXCHECK, DEFAULT_THRESHOLD, orbitType, positionAngle, angle);
  70.     }

  71.     /** Build a detector.
  72.      * @param maxCheck maximal checking interval (s)
  73.      * @param threshold convergence threshold (s)
  74.      * @param orbitType orbit type defining the angle type
  75.      * @param positionAngle type of position angle
  76.      * @param angle fixed angle to be crossed
  77.      * @exception OrekitIllegalArgumentException if orbit type is {@link OrbitType#CARTESIAN}
  78.      */
  79.     public PositionAngleDetector(final double maxCheck, final double threshold,
  80.                                  final OrbitType orbitType, final PositionAngle positionAngle,
  81.                                  final double angle)
  82.         throws OrekitIllegalArgumentException {
  83.         this(maxCheck, threshold, DEFAULT_MAX_ITER, new StopOnIncreasing<PositionAngleDetector>(),
  84.              orbitType, positionAngle, angle);
  85.     }

  86.     /** Private constructor with full parameters.
  87.      * <p>
  88.      * This constructor is private as users are expected to use the builder
  89.      * API with the various {@code withXxx()} methods to set up the instance
  90.      * in a readable manner without using a huge amount of parameters.
  91.      * </p>
  92.      * @param maxCheck maximum checking interval (s)
  93.      * @param threshold convergence threshold (s)
  94.      * @param maxIter maximum number of iterations in the event time search
  95.      * @param handler event handler to call at event occurrences
  96.      * @param orbitType orbit type defining the angle type
  97.      * @param positionAngle type of position angle
  98.      * @param angle fixed angle to be crossed
  99.      * @exception OrekitIllegalArgumentException if orbit type is {@link OrbitType#CARTESIAN}
  100.      */
  101.     private PositionAngleDetector(final double maxCheck, final double threshold,
  102.                                      final int maxIter, final EventHandler<? super PositionAngleDetector> handler,
  103.                                      final OrbitType orbitType, final PositionAngle positionAngle,
  104.                                      final double angle)
  105.         throws OrekitIllegalArgumentException {
  106.         super(maxCheck, threshold, maxIter, handler);
  107.         if (orbitType == OrbitType.CARTESIAN) {
  108.             final String sep = ", ";
  109.             throw new OrekitIllegalArgumentException(OrekitMessages.ORBIT_TYPE_NOT_ALLOWED,
  110.                                                      orbitType,
  111.                                                      OrbitType.KEPLERIAN   + sep +
  112.                                                      OrbitType.CIRCULAR    + sep +
  113.                                                      OrbitType.EQUINOCTIAL);
  114.         }
  115.         this.orbitType     = orbitType;
  116.         this.positionAngle = positionAngle;
  117.         this.angle         = angle;
  118.         this.sign          = +1.0;
  119.         this.previousDelta = Double.NaN;
  120.     }

  121.     /** {@inheritDoc} */
  122.     @Override
  123.     protected PositionAngleDetector create(final double newMaxCheck, final double newThreshold,
  124.                                               final int newMaxIter,
  125.                                               final EventHandler<? super PositionAngleDetector> newHandler) {
  126.         return new PositionAngleDetector(newMaxCheck, newThreshold, newMaxIter, newHandler,
  127.                                          orbitType, positionAngle, angle);
  128.     }

  129.     /** Get the orbit type defining the angle type.
  130.      * @return orbit type defining the angle type
  131.      */
  132.     public OrbitType getOrbitType() {
  133.         return orbitType;
  134.     }

  135.     /** Get the type of position angle.
  136.      * @return type of position angle
  137.      */
  138.     public PositionAngle getPositionAngle() {
  139.         return positionAngle;
  140.     }

  141.     /** Get the fixed angle to be crossed (radians).
  142.      * @return fixed angle to be crossed (radians)
  143.      */
  144.     public double getAngle() {
  145.         return angle;
  146.     }

  147.     /** Compute the value of the detection function.
  148.      * <p>
  149.      * The value is the angle difference between the spacecraft and the fixed
  150.      * angle to be crossed, with some sign tweaks to ensure continuity.
  151.      * These tweaks imply the {@code increasing} flag in events detection becomes
  152.      * irrelevant here! As an example, the angle always increase in a Keplerian
  153.      * orbit, but this g function will increase and decrease so it
  154.      * will cross the zero value once per orbit, in increasing and decreasing
  155.      * directions on alternate orbits..
  156.      * </p>
  157.      * @param s the current state information: date, kinematics, attitude
  158.      * @return angle difference between the spacecraft and the fixed
  159.      * angle, with some sign tweaks to ensure continuity
  160.      * @exception OrekitException if some specific error occurs
  161.      */
  162.     public double g(final SpacecraftState s) throws OrekitException {

  163.         // get angle
  164.         final double currentAngle;
  165.         switch (orbitType) {
  166.             case KEPLERIAN:
  167.                 currentAngle = ((KeplerianOrbit) orbitType.convertType(s.getOrbit())).getAnomaly(positionAngle);
  168.                 break;
  169.             case CIRCULAR:
  170.                 currentAngle = ((CircularOrbit) orbitType.convertType(s.getOrbit())).getAlpha(positionAngle);
  171.                 break;
  172.             case EQUINOCTIAL:
  173.                 currentAngle = ((EquinoctialOrbit) orbitType.convertType(s.getOrbit())).getL(positionAngle);
  174.                 break;
  175.             default:
  176.                 // this should never happen as type was checked at construction
  177.                 throw new OrekitInternalError(null);
  178.         }

  179.         // angle difference
  180.         double delta = MathUtils.normalizeAngle(sign * (currentAngle - angle), 0.0);

  181.         // ensure continuity
  182.         if (FastMath.abs(delta - previousDelta) > FastMath.PI) {
  183.             sign  = -sign;
  184.             delta = MathUtils.normalizeAngle(sign * (currentAngle - angle), 0.0);
  185.         }
  186.         previousDelta = delta;

  187.         return delta;

  188.     }

  189. }