AlignmentDetector.java

  1. /* Copyright 2002-2020 CS GROUP
  2.  * Licensed to CS GROUP (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.ode.events.Action;
  20. import org.hipparchus.util.FastMath;
  21. import org.hipparchus.util.SinCos;
  22. import org.orekit.orbits.Orbit;
  23. import org.orekit.propagation.SpacecraftState;
  24. import org.orekit.propagation.events.handlers.EventHandler;
  25. import org.orekit.propagation.events.handlers.StopOnIncreasing;
  26. import org.orekit.utils.PVCoordinates;
  27. import org.orekit.utils.PVCoordinatesProvider;

  28. /** Finder for satellite/body alignment events in orbital plane.
  29.  * <p>This class finds alignment events.</p>
  30.  * <p>Alignment means the conjunction, with some threshold angle, between the satellite
  31.  * position and the projection in the orbital plane of some body position.</p>
  32.  * <p>The default handler behavior is to {@link 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.     /** Body to align. */
  40.     private final PVCoordinatesProvider body;

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

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

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

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

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

  72.     /** Build a new alignment detector.
  73.      * <p>The orbit is used only to set an upper bound for the max check interval
  74.      * to period/3.</p>
  75.      * @param threshold convergence threshold (s)
  76.      * @param orbit initial orbit
  77.      * @param body the body to align
  78.      * @param alignAngle the alignment angle (rad)
  79.      */
  80.     public AlignmentDetector(final double threshold,
  81.                              final Orbit orbit,
  82.                              final PVCoordinatesProvider body,
  83.                              final double alignAngle) {
  84.         this(orbit.getKeplerianPeriod() / 3, threshold, body, alignAngle);
  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 body the body to align
  97.      * @param alignAngle the alignment angle (rad)
  98.      */
  99.     private AlignmentDetector(final double maxCheck, final double threshold,
  100.                               final int maxIter, final EventHandler<? super AlignmentDetector> handler,
  101.                               final PVCoordinatesProvider body,
  102.                               final double alignAngle) {
  103.         super(maxCheck, threshold, maxIter, handler);
  104.         final SinCos sc    = FastMath.sinCos(alignAngle);
  105.         this.body          = body;
  106.         this.alignAngle    = alignAngle;
  107.         this.cosAlignAngle = sc.cos();
  108.         this.sinAlignAngle = sc.sin();
  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. }