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 IIA 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 GPSBlockIIA extends AbstractGNSSAttitudeProvider {
42  
43      /** Default yaw bias (rad). */
44      public static final double DEFAULT_YAW_BIAS = FastMath.toRadians(0.5);
45  
46      /** Satellite-Sun angle limit for a midnight turn maneuver. */
47      private static final double NIGHT_TURN_LIMIT = FastMath.toRadians(180.0 - 13.25);
48  
49      /** Margin on turn end. */
50      private static final double END_MARGIN = 1800.0;
51  
52      /** Default yaw rates for all spacecrafts in radians per seconds (indexed by prnNumber, hence entry 0 is unused). */
53      private static final double[] DEFAULT_YAW_RATES = new double[] {
54          Double.NaN,  // unused entry 0
55          FastMath.toRadians(0.1211), FastMath.toRadians(0.1339), FastMath.toRadians(0.1230), FastMath.toRadians(0.1233),
56          FastMath.toRadians(0.1180), FastMath.toRadians(0.1266), FastMath.toRadians(0.1269), FastMath.toRadians(0.1033),
57          FastMath.toRadians(0.1278), FastMath.toRadians(0.0978), FastMath.toRadians(0.2000), FastMath.toRadians(0.1990),
58          FastMath.toRadians(0.2000), FastMath.toRadians(0.0815), FastMath.toRadians(0.1303), FastMath.toRadians(0.0838),
59          FastMath.toRadians(0.1401), FastMath.toRadians(0.1069), FastMath.toRadians(0.0980), FastMath.toRadians(0.1030),
60          FastMath.toRadians(0.1366), FastMath.toRadians(0.1025), FastMath.toRadians(0.1140), FastMath.toRadians(0.1089),
61          FastMath.toRadians(0.1001), FastMath.toRadians(0.1227), FastMath.toRadians(0.1194), FastMath.toRadians(0.1260),
62          FastMath.toRadians(0.1228), FastMath.toRadians(0.1165), FastMath.toRadians(0.0969), FastMath.toRadians(0.1140)
63      };
64  
65      /** Yaw rate for current spacecraft. */
66      private final double yawRate;
67  
68      /** Yaw bias. */
69      private final double yawBias;
70  
71      /** Simple constructor.
72       * @param yawRate yaw rate to use in radians per seconds (typically {@link #getDefaultYawRate(int) GPSBlockIIA.getDefaultYawRate(prnNumber)})
73       * @param yawBias yaw bias to use (rad) (typicall {@link #DEFAULT_YAW_BIAS})
74       * @param validityStart start of validity for this provider
75       * @param validityEnd end of validity for this provider
76       * @param sun provider for Sun position
77       * @param inertialFrame inertial frame where velocity are computed
78       * @since 9.3
79       */
80      public GPSBlockIIA(final double yawRate, final double yawBias,
81                         final AbsoluteDate validityStart, final AbsoluteDate validityEnd,
82                         final ExtendedPositionProvider sun, final Frame inertialFrame) {
83          super(validityStart, validityEnd, sun, inertialFrame);
84          this.yawRate = yawRate;
85          this.yawBias = yawBias;
86      }
87  
88      /** Get the default yaw rate for a satellite.
89       * @param prnNumber satellite PRN
90       * @return default yaw rate for the specified satellite
91       * @since 10.0
92       */
93      public static double getDefaultYawRate(final int prnNumber) {
94          return DEFAULT_YAW_RATES[prnNumber];
95      }
96  
97      /** {@inheritDoc} */
98      @Override
99      protected TimeStampedAngularCoordinates correctedYaw(final GNSSAttitudeContext context) {
100 
101         // noon beta angle limit from yaw rate
102         final double aNoon  = FastMath.atan(context.getMuRate() / yawRate);
103         final double aNight = NIGHT_TURN_LIMIT;
104         final double cNoon  = FastMath.cos(aNoon);
105         final double cNight = FastMath.cos(aNight);
106 
107         if (context.setUpTurnRegion(cNight, cNoon)) {
108 
109             final double absBeta = FastMath.abs(context.beta(context.getDate()));
110             context.setHalfSpan(context.inSunSide() ?
111                                 absBeta * FastMath.sqrt(aNoon / absBeta - 1.0) :
112                                 context.inOrbitPlaneAbsoluteAngle(aNight - FastMath.PI),
113                                 END_MARGIN);
114             if (context.inTurnTimeRange()) {
115 
116                 // we need to ensure beta sign does not change during the turn
117                 final double beta     = context.getSecuredBeta();
118                 final double phiStart = context.getYawStart(beta);
119                 final double dtStart  = context.timeSinceTurnStart();
120                 final double linearPhi;
121                 final double phiDot;
122                 if (context.inSunSide()) {
123                     // noon turn
124                     if (beta > 0 && beta < yawBias) {
125                         // noon turn problem for small positive beta in block IIA
126                         // rotation is in the wrong direction for these spacecrafts
127                         phiDot    = FastMath.copySign(yawRate, beta);
128                         linearPhi = phiStart + phiDot * dtStart;
129                     } else {
130                         // regular noon turn
131                         phiDot    = -FastMath.copySign(yawRate, beta);
132                         linearPhi = phiStart + phiDot * dtStart;
133                     }
134                 } else {
135                     // midnight turn
136                     phiDot    = yawRate;
137                     linearPhi = phiStart + phiDot * dtStart;
138                 }
139 
140                 if (context.linearModelStillActive(linearPhi, phiDot)) {
141                     // we are still in the linear model phase
142                     return context.turnCorrectedAttitude(linearPhi, phiDot);
143                 }
144 
145             }
146 
147         }
148 
149         // in nominal yaw mode
150         return context.nominalYaw(context.getDate());
151 
152     }
153 
154     /** {@inheritDoc} */
155     @Override
156     protected <T extends CalculusFieldElement<T>> TimeStampedFieldAngularCoordinates<T> correctedYaw(final GNSSFieldAttitudeContext<T> context) {
157 
158         final Field<T> field = context.getDate().getField();
159 
160         // noon beta angle limit from yaw rate
161         final T      aNoon  = FastMath.atan(context.getMuRate().divide(yawRate));
162         final T      aNight = field.getZero().newInstance(NIGHT_TURN_LIMIT);
163         final double cNoon  = FastMath.cos(aNoon.getReal());
164         final double cNight = FastMath.cos(aNight.getReal());
165 
166         if (context.setUpTurnRegion(cNight, cNoon)) {
167 
168             final T absBeta = FastMath.abs(context.beta(context.getDate()));
169             context.setHalfSpan(context.inSunSide() ?
170                                 absBeta.multiply(FastMath.sqrt(aNoon.divide(absBeta).subtract(1.0))) :
171                                 context.inOrbitPlaneAbsoluteAngle(aNight.subtract(aNoon.getPi())),
172                                 END_MARGIN);
173             if (context.inTurnTimeRange()) {
174 
175                 // we need to ensure beta sign does not change during the turn
176                 final T beta     = context.getSecuredBeta();
177                 final T phiStart = context.getYawStart(beta);
178                 final T dtStart  = context.timeSinceTurnStart();
179                 final T linearPhi;
180                 final T phiDot;
181                 if (context.inSunSide()) {
182                     // noon turn
183                     if (beta.getReal() > 0 && beta.getReal() < yawBias) {
184                         // noon turn problem for small positive beta in block IIA
185                         // rotation is in the wrong direction for these spacecrafts
186                         phiDot    = field.getZero().newInstance(FastMath.copySign(yawRate, beta.getReal()));
187                         linearPhi = phiStart.add(phiDot.multiply(dtStart));
188                     } else {
189                         // regular noon turn
190                         phiDot    = field.getZero().newInstance(-FastMath.copySign(yawRate, beta.getReal()));
191                         linearPhi = phiStart.add(phiDot.multiply(dtStart));
192                     }
193                 } else {
194                     // midnight turn
195                     phiDot    = field.getZero().newInstance(yawRate);
196                     linearPhi = phiStart.add(phiDot.multiply(dtStart));
197                 }
198 
199                 if (context.linearModelStillActive(linearPhi, phiDot)) {
200                     // we are still in the linear model phase
201                     return context.turnCorrectedAttitude(linearPhi, phiDot);
202                 }
203 
204             }
205 
206         }
207 
208         // in nominal yaw mode
209         return context.nominalYaw(context.getDate());
210 
211     }
212 
213 }