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

  18. import org.hipparchus.geometry.euclidean.threed.Vector3D;
  19. import org.hipparchus.util.FastMath;
  20. import org.orekit.orbits.Orbit;
  21. import org.orekit.propagation.SpacecraftState;
  22. import org.orekit.propagation.events.handlers.EventHandler;
  23. import org.orekit.propagation.events.handlers.StopOnIncreasing;
  24. import org.orekit.utils.PVCoordinates;
  25. import org.orekit.utils.PVCoordinatesProvider;

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

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

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

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

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

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

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

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

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

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

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

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

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

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

  154. }