AlignmentDetector.java

  1. /* Copyright 2002-2025 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(new EventDetectionSettings(maxCheck, threshold, EventDetectionSettings.DEFAULT_MAX_ITER),
  69.              new StopOnIncreasing(), body, alignAngle);
  70.     }

  71.     /** Build a new alignment detector.
  72.      * @param detectionSettings detection settings
  73.      * @param body the body to align
  74.      * @param alignAngle the alignment angle (rad)
  75.      * @since 13.0
  76.      */
  77.     public AlignmentDetector(final EventDetectionSettings detectionSettings,
  78.                              final PVCoordinatesProvider body,
  79.                              final double alignAngle) {
  80.         this(detectionSettings, new StopOnIncreasing(), body, alignAngle);
  81.     }

  82.     /** Build a new alignment detector.
  83.      * <p>The orbit is used only to set an upper bound for the max check interval
  84.      * to period/3.</p>
  85.      * @param threshold convergence threshold (s)
  86.      * @param orbit initial orbit
  87.      * @param body the body to align
  88.      * @param alignAngle the alignment angle (rad)
  89.      */
  90.     public AlignmentDetector(final double threshold,
  91.                              final Orbit orbit,
  92.                              final PVCoordinatesProvider body,
  93.                              final double alignAngle) {
  94.         this(orbit.getKeplerianPeriod() / 3, threshold, body, alignAngle);
  95.     }

  96.     /** Protected constructor with full parameters.
  97.      * <p>
  98.      * This constructor is not public as users are expected to use the builder
  99.      * API with the various {@code withXxx()} methods to set up the instance
  100.      * in a readable manner without using a huge amount of parameters.
  101.      * </p>
  102.      * @param detectionSettings detection settings
  103.      * @param handler event handler to call at event occurrences
  104.      * @param body the body to align
  105.      * @param alignAngle the alignment angle (rad)
  106.      * @since 13.0
  107.      */
  108.     protected AlignmentDetector(final EventDetectionSettings detectionSettings, final EventHandler handler,
  109.                                 final PVCoordinatesProvider body,
  110.                                 final double alignAngle) {
  111.         super(detectionSettings, handler);
  112.         final SinCos sc    = FastMath.sinCos(alignAngle);
  113.         this.body          = body;
  114.         this.alignAngle    = alignAngle;
  115.         this.cosAlignAngle = sc.cos();
  116.         this.sinAlignAngle = sc.sin();
  117.     }

  118.     /** {@inheritDoc} */
  119.     @Override
  120.     protected AlignmentDetector create(final EventDetectionSettings detectionSettings, final EventHandler newHandler) {
  121.         return new AlignmentDetector(detectionSettings, newHandler, body, alignAngle);
  122.     }

  123.     /** Get the body to align.
  124.      * @return the body to align
  125.      */
  126.     public PVCoordinatesProvider getPVCoordinatesProvider() {
  127.         return body;
  128.     }

  129.     /** Get the alignment angle (rad).
  130.      * @return the alignment angle
  131.      */
  132.     public double getAlignAngle() {
  133.         return alignAngle;
  134.     }

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

  160. }