1   /* Copyright 2002-2019 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.gnss.attitude;
18  
19  import org.hipparchus.Field;
20  import org.hipparchus.RealFieldElement;
21  import org.hipparchus.util.FastMath;
22  import org.orekit.frames.Frame;
23  import org.orekit.time.AbsoluteDate;
24  import org.orekit.utils.ExtendedPVCoordinatesProvider;
25  import org.orekit.utils.TimeStampedAngularCoordinates;
26  import org.orekit.utils.TimeStampedFieldAngularCoordinates;
27  
28  /**
29   * Attitude providers for GPS block IIF 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      /** Serializable UID. */
47      private static final long serialVersionUID = 20171114L;
48  
49      /** Margin on turn end. */
50      private final double END_MARGIN = 1800.0;
51  
52      /** Yaw rate. */
53      private final double yawRate;
54  
55      /** Simple constructor.
56       * @param yawRate yaw rate to use in radians per seconds (typically {@link #DEFAULT_YAW_RATE})
57       * @param validityStart start of validity for this provider
58       * @param validityEnd end of validity for this provider
59       * @param sun provider for Sun position
60       * @param inertialFrame inertial frame where velocity are computed
61       */
62      public GPSBlockIIR(final double yawRate,
63                         final AbsoluteDateDate">AbsoluteDate validityStart, final AbsoluteDate validityEnd,
64                         final ExtendedPVCoordinatesProvider sun, final Frame inertialFrame) {
65          super(validityStart, validityEnd, sun, inertialFrame);
66          this.yawRate = yawRate;
67      }
68  
69      /** {@inheritDoc} */
70      @Override
71      protected TimeStampedAngularCoordinates correctedYaw(final GNSSAttitudeContext context) {
72  
73          // noon beta angle limit from yaw rate
74          final double aNoon  = FastMath.atan(context.getMuRate() / yawRate);
75          final double cNoon  = FastMath.cos(aNoon);
76          final double cNight = -cNoon;
77  
78          if (context.setUpTurnRegion(cNight, cNoon)) {
79  
80              final double absBeta = FastMath.abs(context.beta(context.getDate()));
81              context.setHalfSpan(absBeta * FastMath.sqrt(aNoon / absBeta - 1.0), END_MARGIN);
82              if (context.inTurnTimeRange()) {
83  
84                  // we need to ensure beta sign does not change during the turn
85                  final double beta     = context.getSecuredBeta();
86                  final double phiStart = context.getYawStart(beta);
87                  final double dtStart  = context.timeSinceTurnStart();
88                  final double phiDot;
89                  final double linearPhi;
90  
91                  if (context.inSunSide()) {
92                      // noon turn
93                      phiDot    = -FastMath.copySign(yawRate, beta);
94                      linearPhi = phiStart + phiDot * dtStart;
95                  } else {
96                      // midnight turn
97                      phiDot    = FastMath.copySign(yawRate, beta);
98                      linearPhi = phiStart + phiDot * dtStart;
99                  }
100 
101                 if (context.linearModelStillActive(linearPhi, phiDot)) {
102                     // we are still in the linear model phase
103                     return context.turnCorrectedAttitude(linearPhi, phiDot);
104                 }
105 
106             }
107 
108         }
109 
110         // in nominal yaw mode
111         return context.nominalYaw(context.getDate());
112 
113     }
114 
115     /** {@inheritDoc} */
116     @Override
117     protected <T extends RealFieldElement<T>> TimeStampedFieldAngularCoordinates<T> correctedYaw(final GNSSFieldAttitudeContext<T> context) {
118 
119         final Field<T> field = context.getDate().getField();
120 
121         // noon beta angle limit from yaw rate
122         final T      aNoon  = FastMath.atan(context.getMuRate().divide(yawRate));
123         final double cNoon  = FastMath.cos(aNoon.getReal());
124         final double cNight = -cNoon;
125 
126         if (context.setUpTurnRegion(cNight, cNoon)) {
127 
128             final T absBeta = FastMath.abs(context.beta(context.getDate()));
129             context.setHalfSpan(absBeta.multiply(FastMath.sqrt(aNoon.divide(absBeta).subtract(1.0))), END_MARGIN);
130             if (context.inTurnTimeRange()) {
131 
132                 // we need to ensure beta sign does not change during the turn
133                 final T beta     = context.getSecuredBeta();
134                 final T phiStart = context.getYawStart(beta);
135                 final T dtStart  = context.timeSinceTurnStart();
136                 final T phiDot;
137                 final T linearPhi;
138 
139                 if (context.inSunSide()) {
140                     // noon turn
141                     phiDot    = field.getZero().add(-FastMath.copySign(yawRate, beta.getReal()));
142                     linearPhi = phiStart.add(phiDot.multiply(dtStart));
143                 } else {
144                     // midnight turn
145                     phiDot    = field.getZero().add(FastMath.copySign(yawRate, beta.getReal()));
146                     linearPhi = phiStart.add(phiDot.multiply(dtStart));
147                 }
148 
149                 if (context.linearModelStillActive(linearPhi, phiDot)) {
150                     // we are still in the linear model phase
151                     return context.turnCorrectedAttitude(linearPhi, phiDot);
152                 }
153 
154             }
155 
156         }
157 
158         // in nominal yaw mode
159         return context.nominalYaw(context.getDate());
160 
161     }
162 
163 }