CelestialBodyPointed.java

  1. /* Copyright 2002-2024 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.attitudes;

  18. import org.hipparchus.Field;
  19. import org.hipparchus.CalculusFieldElement;
  20. import org.hipparchus.geometry.euclidean.threed.FieldRotation;
  21. import org.hipparchus.geometry.euclidean.threed.FieldVector3D;
  22. import org.hipparchus.geometry.euclidean.threed.Rotation;
  23. import org.hipparchus.geometry.euclidean.threed.Vector3D;
  24. import org.orekit.frames.FieldTransform;
  25. import org.orekit.frames.Frame;
  26. import org.orekit.frames.Transform;
  27. import org.orekit.time.AbsoluteDate;
  28. import org.orekit.time.FieldAbsoluteDate;
  29. import org.orekit.utils.FieldPVCoordinates;
  30. import org.orekit.utils.FieldPVCoordinatesProvider;
  31. import org.orekit.utils.PVCoordinates;
  32. import org.orekit.utils.PVCoordinatesProvider;


  33. /**
  34.  * This class handles a celestial body pointed attitude provider.
  35.  * <p>The celestial body pointed law is defined by two main elements:
  36.  * <ul>
  37.  *   <li>a celestial body towards which some satellite axis is exactly aimed</li>
  38.  *   <li>a phasing reference defining the rotation around the pointing axis</li>
  39.  * </ul>
  40.  *
  41.  * <p>
  42.  * The celestial body implicitly defines two of the three degrees of freedom
  43.  * and the phasing reference defines the remaining degree of freedom. This definition
  44.  * can be represented as first aligning exactly the satellite pointing axis to
  45.  * the current direction of the celestial body, and then to find the rotation
  46.  * around this axis such that the satellite phasing axis is in the half-plane
  47.  * defined by a cut line on the pointing axis and containing the celestial
  48.  * phasing reference.
  49.  * </p>
  50.  * <p>
  51.  * In order for this definition to work, the user must ensure that the phasing
  52.  * reference is <strong>never</strong> aligned with the pointing reference.
  53.  * Since the pointed body moves as the date changes, this should be ensured
  54.  * regardless of the date. A simple way to do this for Sun, Moon or any planet
  55.  * pointing is to choose a phasing reference far from the ecliptic plane. Using
  56.  * <code>Vector3D.PLUS_K</code>, the equatorial pole, is perfect in these cases.
  57.  * </p>
  58.  * <p>Instances of this class are guaranteed to be immutable.</p>
  59.  * @author Luc Maisonobe
  60.  */
  61. public class CelestialBodyPointed implements AttitudeProvider {

  62.     /** Frame in which {@link #phasingCel} is defined. */
  63.     private final Frame celestialFrame;

  64.     /** Celestial body to point at. */
  65.     private final PVCoordinatesProvider pointedBody;

  66.     /** Phasing reference, in celestial frame. */
  67.     private final Vector3D phasingCel;

  68.     /** Satellite axis aiming at the pointed body, in satellite frame. */
  69.     private final Vector3D pointingSat;

  70.     /** Phasing reference, in satellite frame. */
  71.     private final Vector3D phasingSat;

  72.     /** Creates new instance.
  73.      * @param celestialFrame frame in which <code>phasingCel</code> is defined
  74.      * @param pointedBody celestial body to point at
  75.      * @param phasingCel phasing reference, in celestial frame
  76.      * @param pointingSat satellite vector defining the pointing direction
  77.      * @param phasingSat phasing reference, in satellite frame
  78.      */
  79.     public CelestialBodyPointed(final Frame celestialFrame,
  80.                                 final PVCoordinatesProvider pointedBody,
  81.                                 final Vector3D phasingCel,
  82.                                 final Vector3D pointingSat,
  83.                                 final Vector3D phasingSat) {
  84.         this.celestialFrame = celestialFrame;
  85.         this.pointedBody    = pointedBody;
  86.         this.phasingCel     = phasingCel;
  87.         this.pointingSat    = pointingSat;
  88.         this.phasingSat     = phasingSat;
  89.     }

  90.     /** {@inheritDoc} */
  91.     public Attitude getAttitude(final PVCoordinatesProvider pvProv,
  92.                                 final AbsoluteDate date, final Frame frame) {

  93.         final PVCoordinates satPV = pvProv.getPVCoordinates(date, celestialFrame);

  94.         // compute celestial references at the specified date
  95.         final PVCoordinates bodyPV    = pointedBody.getPVCoordinates(date, celestialFrame);
  96.         final PVCoordinates pointing  = new PVCoordinates(satPV, bodyPV);
  97.         final Vector3D      pointingP = pointing.getPosition();
  98.         final double        r2        = Vector3D.dotProduct(pointingP, pointingP);

  99.         // evaluate instant rotation axis due to sat and body motion only (no phasing yet)
  100.         final Vector3D rotAxisCel =
  101.             new Vector3D(1 / r2, Vector3D.crossProduct(pointingP, pointing.getVelocity()));

  102.         // fix instant rotation to take phasing constraint into account
  103.         // (adding a rotation around pointing axis ensuring the motion of the phasing axis
  104.         //  is constrained in the pointing-phasing plane)
  105.         final Vector3D v1    = Vector3D.crossProduct(rotAxisCel, phasingCel);
  106.         final Vector3D v2    = Vector3D.crossProduct(pointingP,  phasingCel);
  107.         final double   compensation = -Vector3D.dotProduct(v1, v2) / v2.getNormSq();
  108.         final Vector3D phasedRotAxisCel = new Vector3D(1.0, rotAxisCel, compensation, pointingP);

  109.         // compute transform from celestial frame to satellite frame
  110.         final Rotation celToSatRotation =
  111.             new Rotation(pointingP, phasingCel, pointingSat, phasingSat);

  112.         // build transform combining rotation and instant rotation axis
  113.         Transform transform = new Transform(date, celToSatRotation, celToSatRotation.applyTo(phasedRotAxisCel));
  114.         if (frame != celestialFrame) {
  115.             // prepend transform from specified frame to celestial frame
  116.             transform = new Transform(date, frame.getTransformTo(celestialFrame, date), transform);
  117.         }

  118.         // build the attitude
  119.         return new Attitude(date, frame, transform.getRotation(), transform.getRotationRate(), transform.getRotationAcceleration());

  120.     }

  121.     /** {@inheritDoc} */
  122.     public <T extends CalculusFieldElement<T>> FieldAttitude<T> getAttitude(final FieldPVCoordinatesProvider<T> pvProv,
  123.                                                                         final FieldAbsoluteDate<T> date,
  124.                                                                         final Frame frame) {

  125.         final Field<T> field = date.getField();
  126.         final FieldPVCoordinates<T> satPV = pvProv.getPVCoordinates(date, celestialFrame);

  127.         // compute celestial references at the specified date
  128.         final FieldPVCoordinates<T> bodyPV    = new FieldPVCoordinates<>(field,
  129.                                                                          pointedBody.getPVCoordinates(date.toAbsoluteDate(),
  130.                                                                                                       celestialFrame));
  131.         final FieldPVCoordinates<T> pointing  = new FieldPVCoordinates<>(satPV, bodyPV);
  132.         final FieldVector3D<T>      pointingP = pointing.getPosition();
  133.         final T                     r2        = FieldVector3D.dotProduct(pointingP, pointingP);

  134.         // evaluate instant rotation axis due to sat and body motion only (no phasing yet)
  135.         final FieldVector3D<T> rotAxisCel =
  136.             new FieldVector3D<>(r2.reciprocal(), FieldVector3D.crossProduct(pointingP, pointing.getVelocity()));

  137.         // fix instant rotation to take phasing constraint into account
  138.         // (adding a rotation around pointing axis ensuring the motion of the phasing axis
  139.         //  is constrained in the pointing-phasing plane)
  140.         final FieldVector3D<T> v1           = FieldVector3D.crossProduct(rotAxisCel, phasingCel);
  141.         final FieldVector3D<T> v2           = FieldVector3D.crossProduct(pointingP,  phasingCel);
  142.         final T                compensation = FieldVector3D.dotProduct(v1, v2).negate().divide(v2.getNormSq());
  143.         final FieldVector3D<T> phasedRotAxisCel = new FieldVector3D<>(field.getOne(), rotAxisCel, compensation, pointingP);

  144.         // compute transform from celestial frame to satellite frame
  145.         final FieldRotation<T> celToSatRotation =
  146.             new FieldRotation<>(pointingP, new FieldVector3D<>(field, phasingCel),
  147.                             new FieldVector3D<>(field, pointingSat), new FieldVector3D<>(field, phasingSat));

  148.         // build transform combining rotation and instant rotation axis
  149.         FieldTransform<T> transform = new FieldTransform<>(date, celToSatRotation, celToSatRotation.applyTo(phasedRotAxisCel));
  150.         if (frame != celestialFrame) {
  151.             // prepend transform from specified frame to celestial frame
  152.             transform = new FieldTransform<>(date, frame.getTransformTo(celestialFrame, date), transform);
  153.         }

  154.         // build the attitude
  155.         return new FieldAttitude<>(date, frame,
  156.                         transform.getRotation(), transform.getRotationRate(), transform.getRotationAcceleration());

  157.     }

  158. }