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.attitudes;
18  
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.RotationConvention;
24  import org.hipparchus.geometry.euclidean.threed.Vector3D;
25  import org.orekit.frames.FieldTransform;
26  import org.orekit.frames.Frame;
27  import org.orekit.frames.Transform;
28  import org.orekit.time.AbsoluteDate;
29  import org.orekit.time.FieldAbsoluteDate;
30  import org.orekit.utils.FieldPVCoordinatesProvider;
31  import org.orekit.utils.PVCoordinatesProvider;
32  
33  
34  /**
35   * This class handles a spin stabilized attitude provider.
36   * <p>Spin stabilized laws are handled as wrappers for an underlying
37   * non-rotating law. This underlying law is typically an instance
38   * of {@link CelestialBodyPointed} with the pointing axis equal to
39   * the rotation axis, but can in fact be anything.</p>
40   * <p>Instances of this class are guaranteed to be immutable.</p>
41   * @author Luc Maisonobe
42   */
43  public class SpinStabilized implements AttitudeProviderModifier {
44  
45      /** Underlying non-rotating attitude provider.  */
46      private final AttitudeProvider nonRotatingLaw;
47  
48      /** Start date of the rotation. */
49      private final AbsoluteDate start;
50  
51      /** Rotation axis in satellite frame. */
52      private final Vector3D axis;
53  
54      /** Spin rate in radians per seconds. */
55      private final double rate;
56  
57      /** Spin vector. */
58      private final Vector3D spin;
59  
60      /** Creates a new instance.
61       * @param nonRotatingLaw underlying non-rotating attitude provider
62       * @param start start date of the rotation
63       * @param axis rotation axis in satellite frame
64       * @param rate spin rate in radians per seconds
65       */
66      public SpinStabilized(final AttitudeProvider nonRotatingLaw,
67                            final AbsoluteDate start,
68                            final Vector3D axis, final double rate) {
69          this.nonRotatingLaw = nonRotatingLaw;
70          this.start          = start;
71          this.axis           = axis;
72          this.rate           = rate;
73          this.spin           = new Vector3D(rate / axis.getNorm(), axis);
74      }
75  
76      /** {@inheritDoc} */
77      @Override
78      public AttitudeProvider getUnderlyingAttitudeProvider() {
79          return nonRotatingLaw;
80      }
81  
82      /** {@inheritDoc} */
83      @Override
84      public Attitude getAttitude(final PVCoordinatesProvider pvProv,
85                                  final AbsoluteDate date, final Frame frame) {
86  
87          // get attitude from underlying non-rotating law
88          final Attitude base = nonRotatingLaw.getAttitude(pvProv, date, frame);
89          final Transform baseTransform = new Transform(date, base.getOrientation());
90  
91          // compute spin transform due to spin from reference to current date
92          final Transform spinInfluence =
93              new Transform(date,
94                            new Rotation(axis,
95                                         rate * date.durationFrom(start),
96                                         RotationConvention.FRAME_TRANSFORM),
97                            spin);
98  
99          // combine the two transforms
100         final Transform combined = new Transform(date, baseTransform, spinInfluence);
101 
102         // build the attitude
103         return new Attitude(date, frame,
104                             combined.getRotation(), combined.getRotationRate(), combined.getRotationAcceleration());
105 
106     }
107 
108     /** {@inheritDoc} */
109     @Override
110     public Rotation getAttitudeRotation(final PVCoordinatesProvider pvProv, final AbsoluteDate date, final Frame frame) {
111         // get rotation from underlying non-rotating law
112         final Rotation baseRotation = nonRotatingLaw.getAttitudeRotation(pvProv, date, frame);
113 
114         // compute spin rotation due to spin from reference to current date
115         final Rotation spinInfluence = new Rotation(axis, rate * date.durationFrom(start), RotationConvention.FRAME_TRANSFORM);
116 
117         // combine the two rotations
118         return baseRotation.compose(spinInfluence, RotationConvention.FRAME_TRANSFORM);
119     }
120 
121     /** {@inheritDoc} */
122     @Override
123     public <T extends CalculusFieldElement<T>> FieldAttitude<T> getAttitude(final FieldPVCoordinatesProvider<T> pvProv,
124                                                                         final FieldAbsoluteDate<T> date,
125                                                                         final Frame frame) {
126 
127         // get attitude from underlying non-rotating law
128         final FieldAttitude<T> base = nonRotatingLaw.getAttitude(pvProv, date, frame);
129         final FieldTransform<T> baseTransform = new FieldTransform<>(date, base.getOrientation());
130 
131         // compute spin transform due to spin from reference to current date
132         final FieldTransform<T> spinInfluence =
133             new FieldTransform<>(date,
134                                  new FieldRotation<>(new FieldVector3D<>(date.getField(), axis),
135                                                      date.durationFrom(start).multiply(rate),
136                                                      RotationConvention.FRAME_TRANSFORM),
137                                  new FieldVector3D<>(date.getField(), spin));
138 
139         // combine the two transforms
140         final FieldTransform<T> combined = new FieldTransform<>(date, baseTransform, spinInfluence);
141 
142         // build the attitude
143         return new FieldAttitude<>(date, frame,
144                                    combined.getRotation(), combined.getRotationRate(), combined.getRotationAcceleration());
145 
146     }
147 
148     /** {@inheritDoc} */
149     @Override
150     public <T extends CalculusFieldElement<T>> FieldRotation<T> getAttitudeRotation(final FieldPVCoordinatesProvider<T> pvProv,
151                                                                                     final FieldAbsoluteDate<T> date,
152                                                                                     final Frame frame) {
153 
154         // get attitude from underlying non-rotating law
155         final FieldRotation<T> baseRotation = nonRotatingLaw.getAttitudeRotation(pvProv, date, frame);
156 
157         // compute spin rotation due to spin from reference to current date
158         final FieldRotation<T> spinInfluence =
159                 new FieldRotation<>(new FieldVector3D<>(date.getField(), axis),
160                                 date.durationFrom(start).multiply(rate),
161                                 RotationConvention.FRAME_TRANSFORM);
162 
163         // combine the two rotations
164         return baseRotation.compose(spinInfluence, RotationConvention.FRAME_TRANSFORM);
165     }
166 }