|
| 1 | +function [ lat, lon ] = CA_Avoid( aircraftA, aircraftB, timeToCollision) |
| 2 | +% Version of collision avoidance function to test and debug |
| 3 | +% |
| 4 | +% |
| 5 | +% INPUT: |
| 6 | +% aircraftA/B: struct |
| 7 | +% 1. lat: latitude of current poition |
| 8 | +% 2. lon: longitude of current poition |
| 9 | +% 3. safetyBubble: radius of safety bubble at predicted collision |
| 10 | +% point |
| 11 | +% |
| 12 | + |
| 13 | + %aircraftInfo struct (code uses this) |
| 14 | + |
| 15 | + % aircraftInfo() { |
| 16 | + % //Initializes all values to zero |
| 17 | + % //Descriptions are in the aircraftInfo struct |
| 18 | + % lat [3] = {0}; |
| 19 | + % lon [3] = {0}; |
| 20 | + % alt [3] = {0}; |
| 21 | + % velocityX [2] = {0}; |
| 22 | + % velocityY [2] = {0}; |
| 23 | + % xAcc = 0; |
| 24 | + % yAcc = 0; |
| 25 | + % futureDistx [3] = {0}; |
| 26 | + % futureDisty [3] = {0}; |
| 27 | + % Hdg = 0; |
| 28 | + % safetyBubble = 10; //Roughly 60 ft |
| 29 | + % priority = 0; |
| 30 | + |
| 31 | + |
| 32 | + avoidBufferSize = 100; % From autopilot_interface.h |
| 33 | +% makeCircle = 0; |
| 34 | +% headOnCollision = 0; |
| 35 | +% approachingFromRight = 0; |
| 36 | +% % mavlink_mission_item_t avoidWaypoint; |
| 37 | +% turnRight = false; |
| 38 | + |
| 39 | +% //---------------------------------------------------- |
| 40 | +% //Store mission waypoints inside a vector |
| 41 | +% //---------------------------------------------------- |
| 42 | +% |
| 43 | +% Request_Waypoints(); |
| 44 | + |
| 45 | +% //------------------------------------------------------- |
| 46 | +% //Calculate avoid waypoint |
| 47 | +% //------------------------------------------------------- |
| 48 | + |
| 49 | + |
| 50 | +% |
| 51 | +% (our airplane) |
| 52 | +% |
| 53 | +% A-----------------B (other airplane) |
| 54 | +% | / |
| 55 | +% | / |
| 56 | +% | / |
| 57 | +% | ^ / |
| 58 | +% | / / |
| 59 | +% | K / |
| 60 | +% | / / |
| 61 | +% | v / |
| 62 | +% | / |
| 63 | +% | / |
| 64 | +% | / |
| 65 | +% | / |
| 66 | +% |/ |
| 67 | +% C (Avoid waypoint) |
| 68 | + |
| 69 | +RADIUS_EARTH = 6371e3; % meters |
| 70 | + |
| 71 | +latA = aircraftA(1); |
| 72 | +lonA = aircraftA(2); |
| 73 | +buffA = aircraftA(3); |
| 74 | + |
| 75 | +latB = aircraftB(1); |
| 76 | +lonB = aircraftB(2); |
| 77 | +buffB = aircraftB(3); |
| 78 | + |
| 79 | + %//This will allow the distance of the new waypoint to be smaller or larger depending on the timeToCollision |
| 80 | + AVOID_BUFFER = avoidBufferSize + (buffB / timeToCollision); |
| 81 | + |
| 82 | + if (timeToCollision < 1) |
| 83 | + AVOID_BUFFER = avoidBufferSize; |
| 84 | + end |
| 85 | + |
| 86 | +% Length k = distance needed between avoidWaypoint and B (meters) |
| 87 | + k = buffB + AVOID_BUFFER; |
| 88 | + % Angular distance |
| 89 | + Ad = k/RADIUS_EARTH; |
| 90 | + %addToFile(convertToString(k), "k: distance between waypoint and B"); |
| 91 | + |
| 92 | + |
| 93 | + |
| 94 | + % Guard against division by zero when finding slopes |
| 95 | + %!!!!!Increment value(.00000001) IS YET TO BE DETERMINED!!!!! |
| 96 | + if (lonB - lonA == 0) |
| 97 | + lonB = lonB + .00000001; |
| 98 | + end |
| 99 | + if (latB - latA == 0) |
| 100 | + latB = latB + .00000001; |
| 101 | + end |
| 102 | + |
| 103 | + % Bearing from A to B: from https://www.igismap.com/formula-to-find-bearing-or-heading-angle-between-two-points-latitude-longitude/ |
| 104 | + dLon = lonB - lonA; |
| 105 | + dLat = latB - latA; |
| 106 | + x = cosd(latB) * sind(dLon); |
| 107 | + y = cosd(latA) * sind(latB) - sind(latA) * cosd(latB) * cosd(dLon); |
| 108 | + |
| 109 | + |
| 110 | + % Special cases in dealing with NED frame |
| 111 | + bearingAB = atan2d(y,x); |
| 112 | + if and(dLon < 0, dLat > 0) || and(dLon > 0, dLat < 0) |
| 113 | + bearingAB = bearingAB + 180; |
| 114 | + end |
| 115 | + |
| 116 | + % Slope of line BC: right now it is simply a 80 degree turn |
| 117 | + bearingBC = bearingAB + 80; |
| 118 | + |
| 119 | + % displacement in meters of new waypoint in x direction (North/South) |
| 120 | + lat = asind(sind(latA) * cos(Ad) + cosd(latA)* sin(Ad) * cosd(bearingBC)); |
| 121 | + % Longitude displacement |
| 122 | + lon_disp = atan2d( sind(bearingBC) * sin(Ad) * cosd(latA), cos(Ad) - sind(latA) * sind(latB)); |
| 123 | + lon = lonA + lon_disp; |
| 124 | + |
| 125 | + |
| 126 | + % insert_waypoint( avoidWaypoint, currentWaypoint); |
| 127 | + % [ lat , lon ] = NewAvoidWaypoint(xDisplacement, yDisplacement, [aircraftA(1),aircraftB(2)]); |
| 128 | + |
| 129 | + end |
0 commit comments