File size: 3,969 Bytes
5ab75a9
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
function [dTCA,X1CA,X2CA] = FindNearbyCA(X1,X2,MotionMode,RelTol)
% FindNearbyCA - Find the close-approach (CA) point from an input primary
%                and secondary position/velocity inertial state.
%
% Syntax: [TCA,X1CA,X2CA] = FindCA(X1,X2,MotionMode,RelTol)
%
%    X1      - Primary object's pos/vel state vector in ECI coordinates
%              (6x1) [m & m/s] or [km & km/s] 
%    X2      - Secondary object's pos/vel state vector in ECI coordinates
%              (6x1) [m & m/s] or [km & km/s] 
%    MotionMode - 'LINEAR' (currently implemented)
%               - 'TWOBODY' (possible future addition)
%    RelTol  - Tolerance for finding CA (reserved for TWOBODY MotionMode)
%
% Outputs:
%    dTCA    - Offset time to CA (s)
%    X1CA    - Primary  ECI state at CA (6x1) [m & m/s] or [km & km/s] 
%    X2CA    - Seconary ECI state at CA (6x1) [m & m/s] or [km & km/s] 
%
% Example/Validation Cases:
%    Conjunctions with unacceptably large TCA offsets:
%     25544_conj_44437_20190727_160507_20190727_160507 (dTCA = 497.068 s)
%     43042_conj_43043_20190505_061624_20190428_061624 (dTCA = 28.329 s)
%     25544_conj_44437_20190803_160507_20190727_160507 (dTCA = 27.198 s)
%     26998_conj_81790_20180707_061819_20180707_061819 (dTCA = 0.173 s)
%    Conjunctions with negligbly small TCA offsets:
%     40115_conj_24925_20181003_073833_20180928_144418 (dTCA = 9.91E-06 s)
%     43689_conj_31620_20190725_050922_20190719_152452 (dTCA = 1.92E-08 s)
%     
% Other m-files required: None
% Subfunctions: None
% MAT-files required: None
%
% See also: None
%
% Last revision: 2018-SEP-04
%
% ----------------- BEGIN CODE -----------------

    % Set up defaults
    
    Nargin = nargin;

    if Nargin < 3 || isempty(MotionMode)
        MotionMode = 'LINEAR';
    else
        MotionMode = upper(MotionMode);
    end
    
    % Initialize motion mode
    
    switch MotionMode
        case 'LINEAR'
            % Linear motion mode
            motion = 1;
        case 'TWOBODY'
            % 2-body motion mode
            motion = 2;
            % Get relative tolerance for convergence
            if Nargin < 4 || isempty(RelTol)
                RelTol = 1E-12; %#ok<NASGU>
            end
        otherwise
            error('Invalid motion mode');
    end

    % Handle different motion modes

    if motion == 1 % Linear motion

        % Primary and secondary cartesian positions and velocities
        r1 = X1(1:3);
        v1 = X1(4:6);
        r2 = X2(1:3);
        v2 = X2(4:6);

        % Relative velocity
        v = v2-v1;
        vmag2 = v'*v;



        % Handle zero relative velocity case

        if vmag2 == 0

            % No TCA offset can be calculated for zero relative velocity

            dTCA = NaN;

            % Primary and secondary CA positions remain unchanged for the

            % case of linear motion with zero relative velocity

            r1CA = r1;

            r2CA = r2;

        else

            % TCA offset for linear relative motion

            dTCA = -((r2-r1)'*v)/vmag2;
            % Primary and secondary positions at the linear-motion TCA
            r1CA = r1+dTCA*v1;
            r2CA = r2+dTCA*v2;
        end

        % Return final linear motion states
        
        X1CA = [r1CA; v1];
        X2CA = [r2CA; v2];
        
    elseif motion == 2 % Two-body motion
        
        error('TWOBODY motion mode not implemented');
        
    end
    
    return;

% ----------------- END OF CODE ------------------
%
% Please record any changes to the software in the change history 
% shown below:
%
% ----------------- CHANGE HISTORY ------------------
% Developer      |    Date     |     Description
% ---------------------------------------------------
% D. Hall        | 2019-AUG-07 | Initial Development
% D. Hall        | 2019-SEP-04 | Expanded comments and description
%

end