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.estimation.measurements.gnss;
18  
19  import org.hipparchus.analysis.UnivariateFunction;
20  import org.hipparchus.analysis.solvers.BracketingNthOrderBrentSolver;
21  import org.hipparchus.analysis.solvers.UnivariateSolver;
22  import org.hipparchus.geometry.euclidean.threed.Vector3D;
23  import org.orekit.errors.OrekitException;
24  import org.orekit.estimation.measurements.MeasurementCreator;
25  import org.orekit.estimation.measurements.ObservableSatellite;
26  import org.orekit.propagation.BoundedPropagator;
27  import org.orekit.propagation.SpacecraftState;
28  import org.orekit.time.AbsoluteDate;
29  import org.orekit.utils.Constants;
30  
31  public class OneWayGNSSRangeCreator extends MeasurementCreator {
32  
33      private final BoundedPropagator    ephemeris;
34      private final double               remoteClk;
35      private final Vector3D             antennaPhaseCenter1;
36      private final Vector3D             antennaPhaseCenter2;
37      private final ObservableSatellite  local;
38  
39      public OneWayGNSSRangeCreator(final BoundedPropagator ephemeris,
40                                    final double localClockOffset,
41                                    final double remoteClockOffset) {
42          this(ephemeris, localClockOffset, remoteClockOffset, Vector3D.ZERO, Vector3D.ZERO);
43      }
44  
45      public OneWayGNSSRangeCreator(final BoundedPropagator ephemeris,
46                                    final double localClockOffset,
47                                    final double remoteClockOffset,
48                                    final Vector3D antennaPhaseCenter1,
49                                    final Vector3D antennaPhaseCenter2) {
50          this.ephemeris           = ephemeris;
51          this.remoteClk           = remoteClockOffset;
52          this.antennaPhaseCenter1 = antennaPhaseCenter1;
53          this.antennaPhaseCenter2 = antennaPhaseCenter2;
54          this.local               = new ObservableSatellite(0);
55          this.local.getClockOffsetDriver().setValue(localClockOffset);
56      }
57  
58      public ObservableSatellite getLocalSatellite() {
59          return local;
60      }
61  
62      public void init(final SpacecraftState s0, final AbsoluteDate t, final double step) {
63          if (local.getClockOffsetDriver().getReferenceDate() == null) {
64              local.getClockOffsetDriver().setReferenceDate(s0.getDate());
65          }
66      }
67  
68      public void handleStep(final SpacecraftState currentState) {
69          try {
70              final double           localClk  = local.getClockOffsetDriver().getValue(currentState.getDate());
71              final double           deltaD    = Constants.SPEED_OF_LIGHT * (localClk - remoteClk);
72              final AbsoluteDate     date      = currentState.getDate();
73              final Vector3D         position  = currentState.toStaticTransform().getInverse().transformPosition(antennaPhaseCenter1);
74  
75              final UnivariateSolver solver = new BracketingNthOrderBrentSolver(1.0e-12, 5);
76  
77              final double downLinkDelay  = solver.solve(1000, new UnivariateFunction() {
78                  public double value(final double x) {
79                      final Vector3D other = ephemeris.
80                                      propagate(date.shiftedBy(-x)).
81                                      toTransform().
82                                      getInverse().
83                                      transformPosition(antennaPhaseCenter2);
84                      final double d = Vector3D.distance(position, other);
85                      return d - x * Constants.SPEED_OF_LIGHT;
86                  }
87              }, -1.0, 1.0);
88              final AbsoluteDate transitDate = currentState.getDate().shiftedBy(-downLinkDelay);
89              final Vector3D otherAtTransit =
90                              ephemeris.propagate(transitDate).
91                              toTransform().
92                              getInverse().
93                              transformPosition(antennaPhaseCenter2);
94              final double downLinkDistance = Vector3D.distance(position, otherAtTransit);
95  
96              // Generate measurement
97              addMeasurement(new OneWayGNSSRange(ephemeris, remoteClk, date.shiftedBy(localClk), downLinkDistance + deltaD, 1.0, 10, local));
98  
99          } catch (OrekitException oe) {
100             throw new OrekitException(oe);
101         }
102     }
103 
104 }