Skip to content

Commit 74af708

Browse files
authored
Fix RTL mission item bugs (#1125)
* Fix RTL mission item bugs * Address copilot review comments * Address copilot review comments * Revert "Address copilot review comments" This reverts commit 367c3fc.
1 parent 5bfc5cd commit 74af708

1 file changed

Lines changed: 64 additions & 39 deletions

File tree

gcs/src/components/mapComponents/missionItems.jsx

Lines changed: 64 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -44,11 +44,16 @@ export default function MissionItems({ missionItems }) {
4444
[missionItems],
4545
)
4646

47+
const displayedMissionItems = useMemo(
48+
() => filteredMissionItems.filter((item) => item.command !== 20),
49+
[filteredMissionItems],
50+
)
51+
4752
const takeoffWaypoint = useMemo(() => {
4853
return missionItems.find((item) => item.command === 22)
4954
}, [missionItems])
5055

51-
const { solid: listOfLineCoords, dotted: listOfDottedLineCoords } = useMemo(
56+
const { solid: listOfLineCoords, dotted: listOfDottedLineSegments } = useMemo(
5257
() => getListOfLineCoordinates(filteredMissionItems),
5358
[filteredMissionItems, homePosition, takeoffWaypoint],
5459
)
@@ -57,23 +62,23 @@ export default function MissionItems({ missionItems }) {
5762
if (filteredMissionItems.length === 0) return { solid: [], dotted: [] }
5863

5964
const lineCoordsList = []
60-
const dottedLineCoordsList = []
61-
62-
// Stop processing waypoints after a land command
63-
const landCommandIndex = filteredMissionItems.findIndex((item) =>
64-
[21, 189].includes(item.command),
65-
)
66-
const itemsToProcess =
67-
landCommandIndex === -1
68-
? filteredMissionItems
69-
: filteredMissionItems.slice(0, landCommandIndex + 1)
65+
const dottedLineSegmentsList = []
66+
const stopCommandItem = [...missionItems]
67+
.filter((item) => [20, 21, 189].includes(item.command))
68+
.sort((a, b) => a.seq - b.seq)
69+
.at(0)
70+
const rtlMissionItem =
71+
stopCommandItem && stopCommandItem.command === 20 ? stopCommandItem : null
72+
let homeCoord = null
73+
74+
// Stop processing waypoints after first RTL/land command in mission sequence.
75+
const itemsToProcess = stopCommandItem
76+
? filteredMissionItems.filter((item) => item.seq <= stopCommandItem.seq)
77+
: filteredMissionItems
7078

7179
// Use home as the starting point
7280
if (homePosition) {
73-
const homeCoord = [
74-
intToCoord(homePosition.lon),
75-
intToCoord(homePosition.lat),
76-
]
81+
homeCoord = [intToCoord(homePosition.lon), intToCoord(homePosition.lat)]
7782
if (
7883
takeoffWaypoint !== undefined &&
7984
takeoffWaypoint.seq < itemsToProcess[0].seq // If the takeoff waypoint is before the first displayed waypoint
@@ -82,43 +87,60 @@ export default function MissionItems({ missionItems }) {
8287
lineCoordsList.push(homeCoord)
8388
} else {
8489
// Draw a dotted line from the home position to the first displayed waypoint
85-
dottedLineCoordsList.push(homeCoord)
86-
dottedLineCoordsList.push([
87-
intToCoord(itemsToProcess[0].y),
88-
intToCoord(itemsToProcess[0].x),
90+
dottedLineSegmentsList.push([
91+
homeCoord,
92+
[intToCoord(itemsToProcess[0].y), intToCoord(itemsToProcess[0].x)],
8993
])
9094
}
9195
}
9296

93-
itemsToProcess.forEach((item) => {
97+
const itemsForConnectedPath = rtlMissionItem
98+
? itemsToProcess.filter((item) => item.seq !== rtlMissionItem.seq)
99+
: itemsToProcess
100+
101+
itemsForConnectedPath.forEach((item) => {
94102
lineCoordsList.push([intToCoord(item.y), intToCoord(item.x)])
95103
})
96104

97-
// Join the last item to first item if aircraft does not land, with a dotted line
105+
// If mission has no terminating land command, show return-to-home as dotted.
98106
if (
99-
![21, 189].includes(itemsToProcess[itemsToProcess.length - 1].command)
107+
![21, 189].includes(itemsToProcess[itemsToProcess.length - 1].command) &&
108+
!rtlMissionItem
100109
) {
101-
dottedLineCoordsList.push([
102-
intToCoord(itemsToProcess[0].y), // Use itemsToProcess here
103-
intToCoord(itemsToProcess[0].x),
104-
])
105-
dottedLineCoordsList.push([
106-
intToCoord(itemsToProcess[itemsToProcess.length - 1].y), // Use itemsToProcess here
110+
const lastItemCoord = [
111+
intToCoord(itemsToProcess[itemsToProcess.length - 1].y),
107112
intToCoord(itemsToProcess[itemsToProcess.length - 1].x),
108-
])
113+
]
114+
115+
const returnEndpoint = homeCoord || [
116+
intToCoord(itemsToProcess[0].y),
117+
intToCoord(itemsToProcess[0].x),
118+
]
119+
120+
dottedLineSegmentsList.push([lastItemCoord, returnEndpoint])
121+
}
122+
123+
// If RTL is present, draw a solid line from the last positional waypoint back to home.
124+
if (rtlMissionItem && homeCoord && itemsForConnectedPath.length > 0) {
125+
lineCoordsList.push(homeCoord)
109126
}
110127

111128
// Connect jump commands to previously displayed item and jump target item
112-
const jumpCommandItems = missionItems.filter((item) => item.command === 177)
129+
const jumpCommandItems = missionItems.filter(
130+
(item) =>
131+
item.command === 177 &&
132+
(!stopCommandItem || item.seq <= stopCommandItem.seq),
133+
)
113134
jumpCommandItems.forEach((jumpItem) => {
114-
const nextItem = filteredMissionItems.find((item) => {
135+
const nextItem = itemsToProcess.find((item) => {
115136
return item.seq === jumpItem.param1
116137
})
117138
if (nextItem === undefined) return
118139

119-
const lastFilteredItem = filteredMissionItems
140+
const lastFilteredItem = itemsToProcess
120141
.filter((item) => item.seq < jumpItem.seq)
121142
.at(-1)
143+
if (!lastFilteredItem) return
122144

123145
lineCoordsList.push([
124146
intToCoord(lastFilteredItem.y),
@@ -127,13 +149,13 @@ export default function MissionItems({ missionItems }) {
127149
lineCoordsList.push([intToCoord(nextItem.y), intToCoord(nextItem.x)])
128150
})
129151

130-
return { solid: lineCoordsList, dotted: dottedLineCoordsList }
152+
return { solid: lineCoordsList, dotted: dottedLineSegmentsList }
131153
}
132154

133155
return (
134156
<>
135157
{/* Show mission item LABELS */}
136-
{filteredMissionItems.map((item, index) => {
158+
{displayedMissionItems.map((item, index) => {
137159
return (
138160
<MarkerPin
139161
key={index}
@@ -155,11 +177,14 @@ export default function MissionItems({ missionItems }) {
155177
lineProps={{ "line-width": 2 }}
156178
/>
157179

158-
<DrawLineCoordinates
159-
coordinates={listOfDottedLineCoords}
160-
colour={tailwindColors.yellow[400]}
161-
lineProps={{ "line-width": 2, "line-dasharray": [4, 6] }}
162-
/>
180+
{listOfDottedLineSegments.map((segment, index) => (
181+
<DrawLineCoordinates
182+
key={index}
183+
coordinates={segment}
184+
colour={tailwindColors.yellow[400]}
185+
lineProps={{ "line-width": 2, "line-dasharray": [4, 6] }}
186+
/>
187+
))}
163188
</>
164189
)
165190
}

0 commit comments

Comments
 (0)