1   /* Copyright 2002-2024 Luc Maisonobe
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  
18  package org.orekit.files.ccsds.ndm.adm.acm;
19  
20  import java.util.ArrayList;
21  import java.util.Collections;
22  import java.util.List;
23  
24  import org.hipparchus.geometry.euclidean.threed.RotationOrder;
25  import org.orekit.errors.OrekitException;
26  import org.orekit.errors.OrekitMessages;
27  import org.orekit.files.ccsds.definitions.AdMethodType;
28  import org.orekit.files.ccsds.ndm.adm.AttitudeEndpoints;
29  import org.orekit.files.ccsds.section.CommentsContainer;
30  
31  /** Attitude determination data.
32   * @author Luc Maisonobe
33   * @since 12.0
34   */
35  public class AttitudeDetermination extends CommentsContainer {
36  
37      /** Endpoints (i.e. frames A, B and their relationship). */
38      private final AttitudeEndpoints endpoints;
39  
40      /** Identification number. */
41      private String id;
42  
43      /** Identification of previous orbit determination. */
44      private String prevId;
45  
46      /** Attitude determination method. */
47      private AdMethodType method;
48  
49      /** Source of attitude estimate. */
50      private String source;
51  
52      /** Rotation order for Euler angles. */
53      private RotationOrder eulerRotSeq;
54  
55      /** Number of states for {@link AdMethodType#EKF}, {@link AdMethodType#BATCH} or {@link AdMethodType#FILTER_SMOOTHER}. */
56      private int nbStates;
57  
58      /** Attitude states. */
59      private AttitudeElementsType attitudeStates;
60  
61      /** Type of attitude error state. */
62      private AttitudeCovarianceType covarianceType;
63  
64      /** Attitude rate states. */
65      private RateElementsType rateStates;
66  
67      /** Rate random walk if {@link #rateStates} is {@link RateElementsType#GYRO_BIAS}. */
68      private double sigmaU;
69  
70      /** Angle random walk if {@link #rateStates} is {@link RateElementsType#GYRO_BIAS}. */
71      private double sigmaV;
72  
73      /** Process noise standard deviation if {@link #rateStates} is {@link RateElementsType#ANGVEL}. */
74      private double rateProcessNoiseStdDev;
75  
76      /** Sensors used. */
77      private List<AttitudeDeterminationSensor> sensorsUsed;
78  
79      /** Simple constructor.
80       */
81      public AttitudeDetermination() {
82          endpoints   = new AttitudeEndpoints();
83          sensorsUsed = new ArrayList<>();
84          nbStates    = -1;
85      }
86  
87      /** {@inheritDoc} */
88      @Override
89      public void validate(final double version) {
90          super.validate(version);
91          checkNotNull(attitudeStates, AttitudeDeterminationKey.ATTITUDE_STATES.name());
92          endpoints.checkExternalFrame(AttitudeDeterminationKey.REF_FRAME_A,
93                                       AttitudeDeterminationKey.REF_FRAME_B);
94  
95          // check sensors in increasing number
96          for (int number = 1; number <= sensorsUsed.size(); ++number) {
97              final AttitudeDeterminationSensor sensor = findSensor(number);
98              if (sensor != null) {
99                  sensor.validate(version);
100             } else {
101                 // no sensor has the expected index
102                 throw new OrekitException(OrekitMessages.CCSDS_MISSING_SENSOR_INDEX, number);
103             }
104 
105         }
106 
107     }
108 
109     /** Find sensor by number.
110      * @param number number of the sensor
111      * @return sensor with specified number, or null if not found
112      */
113     private AttitudeDeterminationSensor findSensor(final int number) {
114         for (final AttitudeDeterminationSensor sensor : sensorsUsed) {
115             if (sensor.getSensorNumber() == number) {
116                 return sensor;
117             }
118         }
119         return null;
120     }
121 
122     /** Get the endpoints (i.e. frames A, B and their relationship).
123      * @return endpoints
124      */
125     public AttitudeEndpoints getEndpoints() {
126         return endpoints;
127     }
128 
129     /** Get identification number.
130      * @return identification number
131      */
132     public String getId() {
133         return id;
134     }
135 
136     /** Set identification number.
137      * @param id identification number
138      */
139     public void setId(final String id) {
140         this.id = id;
141     }
142 
143     /** Get identification of previous orbit determination.
144      * @return identification of previous orbit determination
145      */
146     public String getPrevId() {
147         return prevId;
148     }
149 
150     /** Set identification of previous orbit determination.
151      * @param prevId identification of previous orbit determination
152      */
153     public void setPrevId(final String prevId) {
154         this.prevId = prevId;
155     }
156 
157     /** Get attitude determination method.
158      * @return attitude determination method
159      */
160     public AdMethodType getMethod() {
161         return method;
162     }
163 
164     /** Set attitude determination method.
165      * @param method attitude determination method
166      */
167     public void setMethod(final AdMethodType method) {
168         this.method = method;
169     }
170 
171     /** Get source of attitude estimate.
172      * @return source of attitude estimate
173      */
174     public String getSource() {
175         return source;
176     }
177 
178     /** Set source of attitude estimate.
179      * @param source source of attitude estimate
180      */
181     public void setSource(final String source) {
182         this.source = source;
183     }
184 
185     /** Get the rotation order for Euler angles.
186      * @return rotation order for Euler angles
187      */
188     public RotationOrder getEulerRotSeq() {
189         return eulerRotSeq;
190     }
191 
192     /** Set the rotation order for Euler angles.
193      * @param eulerRotSeq rotation order for Euler angles
194      */
195     public void setEulerRotSeq(final RotationOrder eulerRotSeq) {
196         this.eulerRotSeq = eulerRotSeq;
197     }
198 
199     /** Get number of states for {@link AdMethodType#EKF}, {@link AdMethodType#BATCH} or {@link AdMethodType#FILTER_SMOOTHER}.
200      * @return number of states
201      */
202     public int getNbStates() {
203         return nbStates;
204     }
205 
206     /** Set number of states for {@link AdMethodType#EKF}, {@link AdMethodType#BATCH} or {@link AdMethodType#FILTER_SMOOTHER}.
207      * @param nbStates number of states
208      */
209     public void setNbStates(final int nbStates) {
210         this.nbStates = nbStates;
211     }
212 
213     /** Get attitude states.
214      * @return attitude states
215      */
216     public AttitudeElementsType getAttitudeStates() {
217         return attitudeStates;
218     }
219 
220     /** Set attitude states.
221      * @param attitudeStates attitude states
222      */
223     public void setAttitudeStates(final AttitudeElementsType attitudeStates) {
224         this.attitudeStates = attitudeStates;
225     }
226 
227     /** Get type of attitude error state.
228      * @return type of attitude error state
229      */
230     public AttitudeCovarianceType getCovarianceType() {
231         return covarianceType;
232     }
233 
234     /** Set type of attitude error state.
235      * @param covarianceType type of attitude error state
236      */
237     public void setCovarianceType(final AttitudeCovarianceType covarianceType) {
238         this.covarianceType = covarianceType;
239     }
240 
241     /** Get attitude rate states.
242      * @return attitude rate states
243      */
244     public RateElementsType getRateStates() {
245         return rateStates;
246     }
247 
248     /** Set attitude rate states.
249      * @param rateStates attitude rate states
250      */
251     public void setRateStates(final RateElementsType rateStates) {
252         this.rateStates = rateStates;
253     }
254 
255     /** Get rate random walk if {@link #rateStates} is {@link RateElementsType#GYRO_BIAS}.
256      * @return rate random walk if {@link #rateStates} is {@link RateElementsType#GYRO_BIAS}
257      */
258     public double getSigmaU() {
259         return sigmaU;
260     }
261 
262     /** Set rate random walk if {@link #rateStates} is {@link RateElementsType#GYRO_BIAS}.
263      * @param sigmaU rate random walk if {@link #rateStates} is {@link RateElementsType#GYRO_BIAS}
264      */
265     public void setSigmaU(final double sigmaU) {
266         this.sigmaU = sigmaU;
267     }
268 
269     /** Get angle random walk if {@link #rateStates} is {@link RateElementsType#GYRO_BIAS}.
270      * @return angle random walk if {@link #rateStates} is {@link RateElementsType#GYRO_BIAS}
271      */
272     public double getSigmaV() {
273         return sigmaV;
274     }
275 
276     /** Set angle random walk if {@link #rateStates} is {@link RateElementsType#GYRO_BIAS}.
277      * @param sigmaV angle random walk if {@link #rateStates} is {@link RateElementsType#GYRO_BIAS}
278      */
279     public void setSigmaV(final double sigmaV) {
280         this.sigmaV = sigmaV;
281     }
282 
283     /** Get process noise standard deviation if {@link #rateStates} is {@link RateElementsType#ANGVEL}.
284      * @return process noise standard deviation if {@link #rateStates} is {@link RateElementsType#ANGVEL}
285      */
286     public double getRateProcessNoiseStdDev() {
287         return rateProcessNoiseStdDev;
288     }
289 
290     /** Set process noise standard deviation if {@link #rateStates} is {@link RateElementsType#ANGVEL}.
291      * @param rateProcessNoiseStdDev process noise standard deviation if {@link #rateStates} is {@link RateElementsType#ANGVEL}
292      */
293     public void setRateProcessNoiseStdDev(final double rateProcessNoiseStdDev) {
294         this.rateProcessNoiseStdDev = rateProcessNoiseStdDev;
295     }
296 
297     /** Get sensors used.
298      * @return sensors used
299      */
300     public List<AttitudeDeterminationSensor> getSensorsUsed() {
301         return Collections.unmodifiableList(sensorsUsed);
302     }
303 
304     /** Add a sensor used.
305      * @param sensor sensor to add
306      */
307     public void addSensor(final AttitudeDeterminationSensor sensor) {
308         for (final AttitudeDeterminationSensor existing : sensorsUsed) {
309             if (sensor.getSensorNumber() == existing.getSensorNumber()) {
310                 throw new OrekitException(OrekitMessages.CCSDS_SENSOR_INDEX_ALREADY_USED, sensor.getSensorNumber());
311             }
312         }
313         sensorsUsed.add(sensor);
314     }
315 
316 }