AlignmentDetector.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.geometry.euclidean.threed.Vector3D;
  19. import org.hipparchus.util.FastMath;
  20. import org.orekit.errors.OrekitException;
  21. import org.orekit.orbits.Orbit;
  22. import org.orekit.propagation.SpacecraftState;
  23. import org.orekit.propagation.events.handlers.EventHandler;
  24. import org.orekit.propagation.events.handlers.StopOnIncreasing;
  25. import org.orekit.utils.PVCoordinates;
  26. import org.orekit.utils.PVCoordinatesProvider;

  27. /** Finder for satellite/body alignment events in orbital plane.
  28.  * <p>This class finds alignment events.</p>
  29.  * <p>Alignment means the conjunction, with some threshold angle, between the satellite
  30.  * position and the projection in the orbital plane of some body position.</p>
  31.  * <p>The default handler behavior is to {@link
  32.  * org.orekit.propagation.events.handlers.EventHandler.Action#STOP stop}
  33.  * propagation when alignment is reached. This can be changed by calling
  34.  * {@link #withHandler(EventHandler)} after construction.</p>
  35.  * @see org.orekit.propagation.Propagator#addEventDetector(EventDetector)
  36.  * @author Pascal Parraud
  37.  */
  38. public class AlignmentDetector extends AbstractDetector<AlignmentDetector> {

  39.     /** Serializable UID. */
  40.     private static final long serialVersionUID = 20131118L;

  41.     /** Body to align. */
  42.     private final PVCoordinatesProvider body;

  43.     /** Alignment angle (rad). */
  44.     private final double alignAngle;

  45.     /** Cosinus of alignment angle. */
  46.     private final double cosAlignAngle;

  47.     /** Sinus of alignment angle. */
  48.     private final double sinAlignAngle;

  49.     /** Build a new alignment detector.
  50.      * <p>The orbit is used only to set an upper bound for the max check interval
  51.      * to period/3 and to set the convergence threshold according to orbit size.</p>
  52.      * @param orbit initial orbit
  53.      * @param body the body to align
  54.      * @param alignAngle the alignment angle (rad)
  55.      */
  56.     public AlignmentDetector(final Orbit orbit,
  57.                              final PVCoordinatesProvider body,
  58.                              final double alignAngle) {
  59.         this(1.0e-13 * orbit.getKeplerianPeriod(), orbit, body, alignAngle);
  60.     }

  61.     /** Build a new alignment detector.
  62.      * @param maxCheck maximum checking interval (s)
  63.      * @param threshold convergence threshold (s)
  64.      * @param body the body to align
  65.      * @param alignAngle the alignment angle (rad)
  66.      */
  67.     public AlignmentDetector(final double maxCheck, final double threshold,
  68.                              final PVCoordinatesProvider body,
  69.                              final double alignAngle) {
  70.         this(maxCheck, threshold, DEFAULT_MAX_ITER,
  71.              new StopOnIncreasing<AlignmentDetector>(),
  72.              body, alignAngle);
  73.     }

  74.     /** Build a new alignment detector.
  75.      * <p>The orbit is used only to set an upper bound for the max check interval
  76.      * to period/3.</p>
  77.      * @param threshold convergence threshold (s)
  78.      * @param orbit initial orbit
  79.      * @param body the body to align
  80.      * @param alignAngle the alignment angle (rad)
  81.      */
  82.     public AlignmentDetector(final double threshold,
  83.                              final Orbit orbit,
  84.                              final PVCoordinatesProvider body,
  85.                              final double alignAngle) {
  86.         this(orbit.getKeplerianPeriod() / 3, threshold, body, alignAngle);
  87.     }

  88.     /** Private constructor with full parameters.
  89.      * <p>
  90.      * This constructor is private as users are expected to use the builder
  91.      * API with the various {@code withXxx()} methods to set up the instance
  92.      * in a readable manner without using a huge amount of parameters.
  93.      * </p>
  94.      * @param maxCheck maximum checking interval (s)
  95.      * @param threshold convergence threshold (s)
  96.      * @param maxIter maximum number of iterations in the event time search
  97.      * @param handler event handler to call at event occurrences
  98.      * @param body the body to align
  99.      * @param alignAngle the alignment angle (rad)
  100.      */
  101.     private AlignmentDetector(final double maxCheck, final double threshold,
  102.                               final int maxIter, final EventHandler<? super AlignmentDetector> handler,
  103.                               final PVCoordinatesProvider body,
  104.                               final double alignAngle) {
  105.         super(maxCheck, threshold, maxIter, handler);
  106.         this.body          = body;
  107.         this.alignAngle    = alignAngle;
  108.         this.cosAlignAngle = FastMath.cos(alignAngle);
  109.         this.sinAlignAngle = FastMath.sin(alignAngle);
  110.     }

  111.     /** {@inheritDoc} */
  112.     @Override
  113.     protected AlignmentDetector create(final double newMaxCheck, final double newThreshold,
  114.                                        final int newMaxIter, final EventHandler<? super AlignmentDetector> newHandler) {
  115.         return new AlignmentDetector(newMaxCheck, newThreshold, newMaxIter, newHandler,
  116.                                      body, alignAngle);
  117.     }

  118.     /** Get the body to align.
  119.      * @return the body to align
  120.      */
  121.     public PVCoordinatesProvider getPVCoordinatesProvider() {
  122.         return body;
  123.     }

  124.     /** Get the alignment angle (rad).
  125.      * @return the alignment angle
  126.      */
  127.     public double getAlignAngle() {
  128.         return alignAngle;
  129.     }

  130.     /** Compute the value of the switching function.
  131.      * This function measures the difference between the alignment angle and the
  132.      * angle between the satellite position and the body position projection in the
  133.      * orbital plane.
  134.      * @param s the current state information: date, kinematics, attitude
  135.      * @return value of the switching function
  136.      * @exception OrekitException if some specific error occurs
  137.      */
  138.     public double g(final SpacecraftState s) throws OrekitException {
  139.         final PVCoordinates pv = s.getPVCoordinates();
  140.         final Vector3D a  = pv.getPosition().normalize();
  141.         final Vector3D b  = Vector3D.crossProduct(pv.getMomentum(), a).normalize();
  142.         final Vector3D x  = new Vector3D(cosAlignAngle, a,  sinAlignAngle, b);
  143.         final Vector3D y  = new Vector3D(sinAlignAngle, a, -cosAlignAngle, b);
  144.         final Vector3D pb = body.getPVCoordinates(s.getDate(), s.getFrame()).getPosition();
  145.         final double beta = FastMath.atan2(Vector3D.dotProduct(pb, y), Vector3D.dotProduct(pb, x));
  146.         final double betm = -FastMath.PI - beta;
  147.         final double betp =  FastMath.PI - beta;
  148.         if (beta < betm) {
  149.             return betm;
  150.         } else if (beta < betp) {
  151.             return beta;
  152.         } else {
  153.             return betp;
  154.         }
  155.     }

  156. }