AlignmentDetector.java

  1. /* Copyright 2002-2013 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.apache.commons.math3.geometry.euclidean.threed.Vector3D;
  19. import org.apache.commons.math3.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.
  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 AbstractReconfigurableDetector<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.      * <p>The orbit is used only to set an upper bound for the max check interval
  63.      * to period/3.</p>
  64.      * @param threshold convergence threshold (s)
  65.      * @param orbit initial orbit
  66.      * @param body the body to align
  67.      * @param alignAngle the alignment angle (rad)
  68.      */
  69.     public AlignmentDetector(final double threshold,
  70.                              final Orbit orbit,
  71.                              final PVCoordinatesProvider body,
  72.                              final double alignAngle) {
  73.         this(orbit.getKeplerianPeriod() / 3, threshold, DEFAULT_MAX_ITER,
  74.              new StopOnIncreasing<AlignmentDetector>(),
  75.              body, alignAngle);
  76.     }

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

  100.     /** {@inheritDoc} */
  101.     @Override
  102.     protected AlignmentDetector create(final double newMaxCheck, final double newThreshold,
  103.                                        final int newMaxIter, final EventHandler<AlignmentDetector> newHandler) {
  104.         return new AlignmentDetector(newMaxCheck, newThreshold, newMaxIter, newHandler,
  105.                                      body, alignAngle);
  106.     }

  107.     /** Get the body to align.
  108.      * @return the body to align
  109.      */
  110.     public PVCoordinatesProvider getPVCoordinatesProvider() {
  111.         return body;
  112.     }

  113.     /** Get the alignment angle (rad).
  114.      * @return the alignment angle
  115.      */
  116.     public double getAlignAngle() {
  117.         return alignAngle;
  118.     }

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

  146. }