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.gnss.attitude;
18  
19  import org.hipparchus.Field;
20  import org.hipparchus.CalculusFieldElement;
21  import org.hipparchus.util.FastMath;
22  import org.orekit.frames.Frame;
23  import org.orekit.time.AbsoluteDate;
24  import org.orekit.utils.ExtendedPositionProvider;
25  import org.orekit.utils.TimeStampedAngularCoordinates;
26  import org.orekit.utils.TimeStampedFieldAngularCoordinates;
27  
28  /**
29   * Attitude providers for GPS block IIR navigation satellites.
30   * <p>
31   * This class is based on the May 2017 version of J. Kouba eclips.f
32   * subroutine available at <a href="http://acc.igs.org/orbits">IGS Analysis
33   * Center Coordinator site</a>. The eclips.f code itself is not used ; its
34   * hard-coded data are used and its low level models are used, but the
35   * structure of the code and the API have been completely rewritten.
36   * </p>
37   * @author J. Kouba original fortran routine
38   * @author Luc Maisonobe Java translation
39   * @since 9.2
40   */
41  public class GPSBlockIIR extends AbstractGNSSAttitudeProvider {
42  
43      /** Default yaw rates for all spacecrafts in radians per seconds. */
44      public static final double DEFAULT_YAW_RATE = FastMath.toRadians(0.2);
45  
46      /** Margin on turn end. */
47      private static final double END_MARGIN = 1800.0;
48  
49      /** Yaw rate. */
50      private final double yawRate;
51  
52      /** Simple constructor.
53       * @param yawRate yaw rate to use in radians per seconds (typically {@link #DEFAULT_YAW_RATE})
54       * @param validityStart start of validity for this provider
55       * @param validityEnd end of validity for this provider
56       * @param sun provider for Sun position
57       * @param inertialFrame inertial frame where velocity are computed
58       */
59      public GPSBlockIIR(final double yawRate,
60                         final AbsoluteDate validityStart, final AbsoluteDate validityEnd,
61                         final ExtendedPositionProvider sun, final Frame inertialFrame) {
62          super(validityStart, validityEnd, sun, inertialFrame);
63          this.yawRate = yawRate;
64      }
65  
66      /** {@inheritDoc} */
67      @Override
68      protected TimeStampedAngularCoordinates correctedYaw(final GNSSAttitudeContext context) {
69  
70          // noon beta angle limit from yaw rate
71          final double aNoon  = FastMath.atan(context.getMuRate() / yawRate);
72          final double cNoon  = FastMath.cos(aNoon);
73          final double cNight = -cNoon;
74  
75          if (context.setUpTurnRegion(cNight, cNoon)) {
76  
77              final double absBeta = FastMath.abs(context.beta(context.getDate()));
78              context.setHalfSpan(absBeta * FastMath.sqrt(aNoon / absBeta - 1.0), END_MARGIN);
79              if (context.inTurnTimeRange()) {
80  
81                  // we need to ensure beta sign does not change during the turn
82                  final double beta     = context.getSecuredBeta();
83                  final double phiStart = context.getYawStart(beta);
84                  final double dtStart  = context.timeSinceTurnStart();
85                  final double phiDot;
86                  final double linearPhi;
87  
88                  if (context.inSunSide()) {
89                      // noon turn
90                      phiDot    = -FastMath.copySign(yawRate, beta);
91                      linearPhi = phiStart + phiDot * dtStart;
92                  } else {
93                      // midnight turn
94                      phiDot    = FastMath.copySign(yawRate, beta);
95                      linearPhi = phiStart + phiDot * dtStart;
96                  }
97  
98                  if (context.linearModelStillActive(linearPhi, phiDot)) {
99                      // we are still in the linear model phase
100                     return context.turnCorrectedAttitude(linearPhi, phiDot);
101                 }
102 
103             }
104 
105         }
106 
107         // in nominal yaw mode
108         return context.nominalYaw(context.getDate());
109 
110     }
111 
112     /** {@inheritDoc} */
113     @Override
114     protected <T extends CalculusFieldElement<T>> TimeStampedFieldAngularCoordinates<T> correctedYaw(final GNSSFieldAttitudeContext<T> context) {
115 
116         final Field<T> field = context.getDate().getField();
117 
118         // noon beta angle limit from yaw rate
119         final T      aNoon  = FastMath.atan(context.getMuRate().divide(yawRate));
120         final double cNoon  = FastMath.cos(aNoon.getReal());
121         final double cNight = -cNoon;
122 
123         if (context.setUpTurnRegion(cNight, cNoon)) {
124 
125             final T absBeta = FastMath.abs(context.beta(context.getDate()));
126             context.setHalfSpan(absBeta.multiply(FastMath.sqrt(aNoon.divide(absBeta).subtract(1.0))), END_MARGIN);
127             if (context.inTurnTimeRange()) {
128 
129                 // we need to ensure beta sign does not change during the turn
130                 final T beta     = context.getSecuredBeta();
131                 final T phiStart = context.getYawStart(beta);
132                 final T dtStart  = context.timeSinceTurnStart();
133                 final T phiDot;
134                 final T linearPhi;
135 
136                 if (context.inSunSide()) {
137                     // noon turn
138                     phiDot    = field.getZero().newInstance(-FastMath.copySign(yawRate, beta.getReal()));
139                     linearPhi = phiStart.add(phiDot.multiply(dtStart));
140                 } else {
141                     // midnight turn
142                     phiDot    = field.getZero().newInstance(FastMath.copySign(yawRate, beta.getReal()));
143                     linearPhi = phiStart.add(phiDot.multiply(dtStart));
144                 }
145 
146                 if (context.linearModelStillActive(linearPhi, phiDot)) {
147                     // we are still in the linear model phase
148                     return context.turnCorrectedAttitude(linearPhi, phiDot);
149                 }
150 
151             }
152 
153         }
154 
155         // in nominal yaw mode
156         return context.nominalYaw(context.getDate());
157 
158     }
159 
160 }