-
Notifications
You must be signed in to change notification settings - Fork 22
Expand file tree
/
Copy pathTrafficState.java
More file actions
537 lines (476 loc) · 12.7 KB
/
TrafficState.java
File metadata and controls
537 lines (476 loc) · 12.7 KB
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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
/*
* Copyright (c) 2016-2021 United States Government as represented by
* the National Aeronautics and Space Administration. No copyright
* is claimed in the United States under Title 17, U.S.Code. All Other
* Rights Reserved.
*/
package gov.nasa.larcfm.ACCoRD;
import java.util.List;
import gov.nasa.larcfm.Util.EuclideanProjection;
import gov.nasa.larcfm.Util.Position;
import gov.nasa.larcfm.Util.Projection;
import gov.nasa.larcfm.Util.Units;
import gov.nasa.larcfm.Util.Util;
import gov.nasa.larcfm.Util.Vect3;
import gov.nasa.larcfm.Util.Velocity;
import gov.nasa.larcfm.Util.f;
/** Horizontal solution */
public class TrafficState {
private final String id_;
private Position pos_;
private Velocity gvel_; // Ground velocity
private Velocity avel_; // Air velocity
private EuclideanProjection eprj_; // Projection
private int alerter_; // Index to alert levels used by this aircraft
private SUMData sum_; // SUM data
private Position posxyz_; // Projected position
private Vect3 sxyz_; // 3-D Cartesion position
private Velocity velxyz_; // Projected air velocity
/**
* Create an non-valid aircraft state
*/
public TrafficState() {
id_ = "_NoAc_";
pos_ = Position.INVALID;
gvel_ = Velocity.INVALID;
avel_ = Velocity.INVALID;
eprj_ = Projection.createProjection(Position.ZERO_LL);
posxyz_ = Position.INVALID;
sxyz_ = Vect3.INVALID;
velxyz_ = Velocity.INVALID;
alerter_ = 0;
sum_ = SUMData.EMPTY;
}
public static final TrafficState INVALID = new TrafficState();
/**
* Create a traffic state that is not lat/lon
* @param id Aircraft's identifier
* @param pos Aircraft's position
* @param vel Aircraft's ground velocity
* @param airvel Aircraft's air velocity
*/
private TrafficState(String id, Position pos, Velocity vel, Velocity airvel) {
id_ = id;
pos_ = pos;
gvel_ = vel;
avel_ = airvel;
posxyz_ = pos;
sxyz_ = pos.vect3();
velxyz_ = airvel;
eprj_ = Projection.createProjection(Position.ZERO_LL);
alerter_ = 1;
sum_ = new SUMData();
}
/**
* Create a traffic state
* @param id Aircraft's identifier
* @param pos Aircraft's position
* @param vel Aircraft's ground velocity
* @param eprj Euclidean projection
*/
private TrafficState(String id, Position pos, Velocity vel, Vect3 wind, EuclideanProjection eprj,int alerter) {
id_ = id;
pos_ = pos;
gvel_ = vel;
avel_ = wind.isZero() ? vel : vel.Sub(wind);
if (pos.isLatLon()) {
sxyz_ = eprj.project(pos);
posxyz_ = Position.make(sxyz_);
velxyz_ = eprj.projectVelocity(pos,avel_);
} else {
posxyz_ = pos;
sxyz_ = pos.vect3();
velxyz_ = avel_;
}
eprj_ = eprj;
alerter_ = alerter;
sum_ = new SUMData();
}
// Make a copy of acc
public TrafficState(TrafficState ac) {
id_ = ac.id_;
pos_ = ac.pos_;
gvel_ = ac.gvel_;
avel_ = ac.avel_;
posxyz_ = ac.posxyz_;
sxyz_ = ac.sxyz_;
velxyz_ = ac.velxyz_;
eprj_ = ac.eprj_;
alerter_ = ac.alerter_;
sum_ = new SUMData(ac.sum_);
}
/**
* Set air velocity.
* @param airvel New air velocity
*/
public void setAirVelocity(Velocity airvel) {
avel_ = airvel;
applyEuclideanProjection();
}
/**
* Set position to new_pos and apply Euclidean projection. This methods doesn't change ownship, i.e.,
* the resulting aircraft is considered as another intruder.
*/
public void setPosition(Position new_pos) {
pos_ = new_pos;
applyEuclideanProjection();
}
/**
* Apply Euclidean projection. Requires aircraft's position in lat/lon
*/
private void applyEuclideanProjection() {
if (pos_.isLatLon()) {
sxyz_ = eprj_.project(pos_);
Velocity v = eprj_.projectVelocity(pos_,avel_);
posxyz_ = Position.make(sxyz_);
velxyz_ = v;
} else {
posxyz_ = pos_;
sxyz_ = pos_.vect3();
velxyz_ = avel_;
}
}
/**
* Set aircraft as ownship
*/
public void setAsOwnship() {
if (isLatLon()) {
eprj_ = Projection.createProjection(pos_.lla().zeroAlt());
applyEuclideanProjection();
}
}
/**
* Make an ownship's aircraft
* @param id Ownship's identifier
* @param pos Ownship's position
* @param vel Ownship's ground velocity
* @param airvel Ownship's air velocity
*/
public static TrafficState makeOwnship(String id, Position pos, Velocity vel, Velocity airvel) {
TrafficState ac = new TrafficState(id,pos,vel,airvel);
ac.setAsOwnship();
return ac;
}
/**
* Set aircraft as intruder of ownship
*/
public void setAsIntruderOf(TrafficState ownship) {
if (isLatLon() && ownship.isLatLon()) {
eprj_ = ownship.getEuclideanProjection();
applyEuclideanProjection();
}
}
/**
* Make intruder aircraft
* @param id Intruder's identifier
* @param pos Intruder's position
* @param vel Intruder's ground velocity
* @return
*/
public TrafficState makeIntruder(String id, Position pos, Velocity vel) {
if (isLatLon() != pos.isLatLon()) {
return INVALID;
}
return new TrafficState(id,pos,vel,windVector(),eprj_,1);
}
/**
* Set alerter index for this aircraft
* @param alerter
*/
public void setAlerterIndex(int alerter) {
alerter_ = Util.max(0,alerter);
}
/**
* @return aircraft index for this aircraft. This index is 1-based.
*/
public int getAlerterIndex() {
return alerter_;
}
/**
* Set wind velocity
* @param wind_vector Wind velocity specified in the TO direction
*/
public void applyWindVector(Vect3 wind_vector) {
avel_ = wind_vector.isZero() ? gvel_ : gvel_.Sub(wind_vector);
applyEuclideanProjection();
}
/**
* Return wind velocity in the to direction
* @return
*/
public Vect3 windVector() {
return gvel_.vect3().Sub(avel_.vect3());
}
/**
* Return Euclidean projection
*/
public EuclideanProjection getEuclideanProjection() {
return eprj_;
}
public Vect3 get_s() {
return sxyz_;
}
public Vect3 get_v() {
return velxyz_.vect3();
}
public Vect3 pos_to_s(Position p) {
if (p.isLatLon()) {
if (!pos_.isLatLon()) {
return Vect3.INVALID;
}
return eprj_.project(p);
}
return p.vect3();
}
public Vect3 vel_to_v(Position p,Velocity v) {
if (p.isLatLon()) {
if (!pos_.isLatLon()) {
return Vect3.INVALID;
}
return eprj_.projectVelocity(p,v).vect3();
}
return v.vect3();
}
public Velocity inverseVelocity(Velocity v) {
return eprj_.inverseVelocity(get_s(),v,true);
}
/**
* Project aircraft state offset time, which can be positive or negative, in the direction of the
* ground velocity. This methods doesn't change the current aircraft, i.e., the resulting aircraft is considered as
* another aircraft.
* @param offset Offset time.
* @return Projected aircraft.
*/
public TrafficState linearProjection(double offset) {
Position new_pos = pos_.linear(gvel_,offset);
TrafficState ac = new TrafficState(this);
ac.setPosition(new_pos);
return ac;
}
/**
* Index of aircraft id in traffic list. If aircraft is not in the list, returns -1
* @param traffic
* @param id
*/
public static int findAircraftIndex(List<TrafficState> traffic, String id) {
for (int i=0; i < traffic.size(); ++i) {
TrafficState ac = traffic.get(i);
if (ac.getId().equals(id)) {
return i;
}
}
return -1;
}
public static String listToString(List<String> traffic) {
String s = "{";
boolean comma = false;
for (String id : traffic) {
if (comma) {
s+=", ";
} else {
comma = true;
}
s += id;
}
return s+"}";
}
public String toPVS() {
return "(# id:= \"" + id_ + "\", s:= "+get_s().toPVS()+
", v:= "+get_v().toPVS()+
", alerter:= "+alerter_+
", unc := (# s_EW_std:= "+f.FmPrecision(sum_.get_s_EW_std())+
", s_NS_std:= "+f.FmPrecision(sum_.get_s_NS_std())+
", s_EN_std:= "+f.FmPrecision(sum_.get_s_EN_std())+
", sz_std:= "+f.FmPrecision(sum_.get_sz_std())+
", v_EW_std:= "+f.FmPrecision(sum_.get_v_EW_std())+
", v_NS_std:= "+f.FmPrecision(sum_.get_v_NS_std())+
", v_EN_std:= "+f.FmPrecision(sum_.get_v_EN_std())+
", vz_std:= "+f.FmPrecision(sum_.get_vz_std())+
" #) #)";
}
public String listToPVSAircraftList(List<TrafficState> traffic) {
String s = "";
s += "(: ";
s += toPVS();
for (TrafficState ac : traffic) {
s += ", ";
s += ac.toPVS();
}
return s+" :)";
}
public static String listToPVSStringList(List<String> traffic) {
if (traffic.isEmpty()) {
return "null[string]";
} else {
String s = "(:";
boolean comma = false;
for (String id : traffic) {
if (comma) {
s += ", ";
} else {
s += " ";
comma = true;
}
s += "\"" + id + "\"";
}
return s+" :)";
}
}
/**
* @return true if this is a valid aircraft state
*/
public boolean isValid() {
return !pos_.isInvalid() && !gvel_.isInvalid();
}
/**
* @return true if aircraft position is specified in lat/lon instead of x,y
*/
public boolean isLatLon() {
return pos_.isLatLon();
}
/**
* @return Aircraft's identifier
*/
public String getId() {
return id_;
}
/**
* @return Aircraft's position
*/
public Position getPosition() {
return pos_;
}
/**
* @return Aircraft's velocity (can be ground or air depending on whether a wind vector was applied or not)
*/
public Velocity getVelocity() {
return avel_;
}
/**
* @return Aircraft's ground velocity
*/
public Velocity getGroundVelocity() {
return gvel_;
}
/**
* @return Aircraft's air velocity
*/
public Velocity getAirVelocity() {
return avel_;
}
public Position positionXYZ() {
return posxyz_;
}
public Velocity velocityXYZ() {
return velxyz_;
}
/**
* Returns current horizontal direction in internal units [0 - 2pi] [rad] (clock-wise with respect to North)
* Direction may be heading or track, depending on whether a wind vector was provided or not.
*/
public double horizontalDirection() {
return avel_.compassAngle();
}
/**
* Returns current direction in given units [0 - 2pi] [u] (clock wise with respect to North)
* Direction may be heading or track, depending on whether a wind vector was provided or not.
*/
public double horizontalDirection(String utrk) {
return avel_.compassAngle(utrk);
}
/**
* Returns current horizontal speed in internal units.
* Horizontal speed may be airspeed or groundspeed, depending on whether a wind vector
* was provided or not.
*/
public double horizontalSpeed() {
return avel_.gs();
}
/**
* Returns current horizontal speed in given units.
* Horizontal speed may be airspeed or groundspeed, depending on whether a wind vector
* was provided or not.
*/
public double horizontalSpeed(String ugs) {
return avel_.groundSpeed(ugs);
}
/**
* Returns current vertical speed in internal units
*/
public double verticalSpeed() {
return avel_.vs();
}
/**
* Returns current vertical speed in given units
*/
public double verticalSpeed(String uvs) {
return avel_.verticalSpeed(uvs);
}
/**
* Returns current altitude in internal units
*/
public double altitude() {
return pos_.alt();
}
/**
* Returns current altitude in given units
*/
public double altitude(String ualt) {
return Units.to(ualt,pos_.alt());
}
/**
* @return SUM (Sensor Uncertainty Mitigation) data
*/
public SUMData sum() {
return sum_;
}
/**
* Set SUM (Sensor Uncertainty Mitigation) data
*/
public void setSUM(SUMData sum) {
sum_ = new SUMData(sum);
}
/**
* s_EW_std: East/West position standard deviation in internal units
* s_NS_std: North/South position standard deviation in internal units
* s_EN_std: East/North position standard deviation in internal units
*/
void setHorizontalPositionUncertainty(double s_EW_std, double s_NS_std, double s_EN_std) {
sum_.setHorizontalPositionUncertainty(s_EW_std, s_NS_std, s_EN_std);
}
/**
* sz_std : Vertical position standard deviation in internal units
*/
void setVerticalPositionUncertainty(double sz_std) {
sum_.setVerticalPositionUncertainty(sz_std);
}
/**
* v_EW_std: East/West velocity standard deviation in internal units
* v_NS_std: North/South velocity standard deviation in internal units
* v_EN_std: East/North velocity standard deviation in internal units
*/
void setHorizontalVelocityUncertainty(double v_EW_std, double v_NS_std, double v_EN_std) {
sum_.setHorizontalVelocityUncertainty(v_EW_std, v_NS_std, v_EN_std);
}
/**
* vz_std : Vertical velocity standard deviation in internal units
*/
void setVerticalSpeedUncertainty(double vz_std) {
sum_.setVerticalSpeedUncertainty(vz_std);
}
/**
* Set all uncertainties to 0
*/
void resetUncertainty() {
sum_.resetUncertainty();
}
public boolean sameId(TrafficState ac) {
return isValid() && ac.isValid() && id_.equals(ac.id_);
}
public String toString() {
return "("+id_+", "+pos_.toString()+", "+avel_.toString()+")";
}
public String rawString() {
return "id: "+id_+", sxyz: "+sxyz_.toString()+", vxyz: "+velxyz_.vect3().toString();
}
}