1   /* Copyright 2002-2024 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.propagation.events;
18  
19  import org.hipparchus.geometry.euclidean.threed.Vector3D;
20  import org.hipparchus.ode.events.Action;
21  import org.orekit.bodies.CelestialBodies;
22  import org.orekit.propagation.SpacecraftState;
23  import org.orekit.propagation.events.handlers.EventHandler;
24  import org.orekit.propagation.events.handlers.StopOnDecreasing;
25  import org.orekit.utils.PVCoordinates;
26  import org.orekit.utils.PVCoordinatesProvider;
27  
28  /** Detects when spacecraft comes close to a moving beacon, as seen from a moving observer.
29   * <p>The main use case for this detector is when the observer is in fact a ground
30   * station, modeled as a {@link org.orekit.frames.TopocentricFrame} and when the beacon
31   * is the {@link CelestialBodies#getSun() Sun}, for computing
32   * interferences for the telemetry link. Another similar case is when the beacon is
33   * another spacecraft, for interferences computation.</p>
34   * <p>The default handler behavior is to {@link Action#STOP stop}
35   * propagation when spacecraft enters the proximity zone. This can be changed by calling
36   * {@link #withHandler(EventHandler)} after construction.</p>
37   * @see org.orekit.propagation.Propagator#addEventDetector(EventDetector)
38   * @author Luc Maisonobe
39   * @since 8.0
40   */
41  public class AngularSeparationDetector extends AbstractDetector<AngularSeparationDetector> {
42  
43      /** Beacon at the center of the proximity zone. */
44      private final PVCoordinatesProvider beacon;
45  
46      /** Observer for the spacecraft, that may also see the beacon at the same time if they are too close. */
47      private final PVCoordinatesProvider observer;
48  
49      /** Proximity angle (rad). */
50      private final double proximityAngle;
51  
52      /** Build a new angular separation detector.
53       * @param beacon beacon at the center of the proximity zone
54       * @param observer observer for the spacecraft, that may also see
55       * the beacon at the same time if they are too close to each other
56       * @param proximityAngle proximity angle as seen from observer, at which events are triggered (rad)
57       */
58      public AngularSeparationDetector(final PVCoordinatesProvider beacon,
59                                       final PVCoordinatesProvider observer,
60                                       final double proximityAngle) {
61          this(AdaptableInterval.of(60.), 1.0e-3, 100, new StopOnDecreasing(),
62               beacon, observer, proximityAngle);
63      }
64  
65      /** Protected constructor with full parameters.
66       * <p>
67       * This constructor is not public as users are expected to use the builder
68       * API with the various {@code withXxx()} methods to set up the instance
69       * in a readable manner without using a huge amount of parameters.
70       * </p>
71       * @param maxCheck maximum checking interval
72       * @param threshold convergence threshold (s)
73       * @param maxIter maximum number of iterations in the event time search
74       * @param handler event handler to call at event occurrences
75       * @param beacon beacon at the center of the proximity zone
76       * @param observer observer for the spacecraft, that may also see
77       * the beacon at the same time if they are too close to each other
78       * @param proximityAngle proximity angle as seen from observer, at which events are triggered (rad)
79       */
80      protected AngularSeparationDetector(final AdaptableInterval maxCheck, final double threshold,
81                                          final int maxIter,
82                                          final EventHandler handler,
83                                          final PVCoordinatesProvider beacon,
84                                          final PVCoordinatesProvider observer,
85                                          final double proximityAngle) {
86          super(maxCheck, threshold, maxIter, handler);
87          this.beacon         = beacon;
88          this.observer       = observer;
89          this.proximityAngle = proximityAngle;
90      }
91  
92      /** {@inheritDoc} */
93      @Override
94      protected AngularSeparationDetector create(final AdaptableInterval newMaxCheck, final double newThreshold,
95                                                 final int newMaxIter, final EventHandler newHandler) {
96          return new AngularSeparationDetector(newMaxCheck, newThreshold, newMaxIter, newHandler,
97                                               beacon, observer, proximityAngle);
98      }
99  
100     /** Get the beacon at the center of the proximity zone.
101      * @return beacon at the center of the proximity zone
102      */
103     public PVCoordinatesProvider getBeacon() {
104         return beacon;
105     }
106 
107     /** Get the observer for the spacecraft.
108      * @return observer for the spacecraft
109      */
110     public PVCoordinatesProvider getObserver() {
111         return observer;
112     }
113 
114     /** Get the proximity angle (rad).
115      * @return the proximity angle
116      */
117     public double getProximityAngle() {
118         return proximityAngle;
119     }
120 
121     /** Compute the value of the switching function.
122      * <p>
123      * This function measures the angular separation between beacon and spacecraft
124      * as seen from the observer minus the proximity angle. It therefore triggers
125      * decreasing events when the spacecraft enters the proximity zone and increasing
126      * events when it leaves the proximity zone.
127      * </p>
128      * <p>
129      * No shadowing effect is taken into account, so this method is computed and
130      * may trigger events even when the spacecraft is below horizon for an observer
131      * which is a ground station. If such effects must be taken into account the
132      * detector must be associated with a {@link EventEnablingPredicateFilter predicate
133      * filter} where the {@link EnablingPredicate predicate function} is based on elevation.
134      * </p>
135      * @param s the current state information: date, kinematics, attitude
136      * @return value of the switching function
137      */
138     public double g(final SpacecraftState s) {
139         final PVCoordinates sPV = s.getPVCoordinates();
140         final Vector3D bP = beacon.getPosition(s.getDate(), s.getFrame());
141         final Vector3D oP = observer.getPosition(s.getDate(), s.getFrame());
142         final double separation = Vector3D.angle(sPV.getPosition().subtract(oP),
143                                                  bP.subtract(oP));
144         return separation - proximityAngle;
145     }
146 
147 }