Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
import { useDispatch, useSelector } from "react-redux"
import { coordToInt } from "../../helpers/dataFormatters"
import { COPTER_MISSION_ITEM_COMMANDS_LIST } from "../../helpers/mavlinkConstants"
import {
createNewSpecificMissionItem,
selectActiveTab,
selectContextMenu,
} from "../../redux/slices/missionSlice"
import ContextMenuItem from "./contextMenuItem"

function getMissionCommandIdByName(name) {
const key = Object.keys(COPTER_MISSION_ITEM_COMMANDS_LIST).find(
(k) => COPTER_MISSION_ITEM_COMMANDS_LIST[k] === name,
)
if (key === undefined) {
// Command name not found; return null to indicate error
return null
}
return parseInt(key)
}

const defaultCommandAltitude = 30 // TODO: make this user configurable
Comment thread
1Blademaster marked this conversation as resolved.

export default function ContextMenuSpecificCommandItems() {
const dispatch = useDispatch()
const activeTab = useSelector(selectActiveTab)
const contextMenuState = useSelector(selectContextMenu)

function addSpecificMissionItem(commandData) {
dispatch(createNewSpecificMissionItem(commandData))
}

// TODO: Add support for loiter commands in sub-menu, as well as modal input for parameters e.g. takeoff altitude

if (activeTab === "mission") {
return (
<>
<ContextMenuItem
onClick={() =>
addSpecificMissionItem({
command: getMissionCommandIdByName("MAV_CMD_NAV_WAYPOINT"),
x: coordToInt(contextMenuState.gpsCoords.lat),
y: coordToInt(contextMenuState.gpsCoords.lng),
z: defaultCommandAltitude,
})
}
>
<p>Add waypoint</p>
</ContextMenuItem>
<ContextMenuItem
onClick={() =>
addSpecificMissionItem({
command: getMissionCommandIdByName("MAV_CMD_NAV_TAKEOFF"),
x: 0,
y: 0,
z: defaultCommandAltitude,
})
}
>
<p>Add takeoff</p>
</ContextMenuItem>
<ContextMenuItem
onClick={() =>
addSpecificMissionItem({
command: getMissionCommandIdByName("MAV_CMD_NAV_LAND"),
x: coordToInt(contextMenuState.gpsCoords.lat),
y: coordToInt(contextMenuState.gpsCoords.lng),
z: 1,
})
}
>
<p>Add land</p>
</ContextMenuItem>
<ContextMenuItem
onClick={() =>
addSpecificMissionItem({
command: getMissionCommandIdByName(
"MAV_CMD_NAV_RETURN_TO_LAUNCH",
),
x: 0,
y: 0,
z: 0,
})
}
>
<p>Add RTL</p>
</ContextMenuItem>
</>
)
}
}
6 changes: 4 additions & 2 deletions gcs/src/components/missions/missionsMap.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ import {
import {
clearDrawingItems,
createFencePolygon,
createNewDrawingItem,
createNewDefaultDrawingItem,
getFrameKey,
removeDrawingItem,
selectActiveTab,
Expand All @@ -61,6 +61,7 @@ import {
setPlannedHomePositionToDronesHomePositionThunk,
updateContextMenuState,
} from "../../redux/slices/missionSlice"
import ContextMenuSpecificCommandItems from "../mapComponents/contextMenuSpecificCommandItems"

const tailwindColors = resolveConfig(tailwindConfig).theme.colors

Expand Down Expand Up @@ -321,7 +322,7 @@ function MapSectionNonMemo({
addNewPolygonVertex(lat, lon)
} else {
dispatch(
createNewDrawingItem({ x: coordToInt(lat), y: coordToInt(lon) }),
createNewDefaultDrawingItem({ x: coordToInt(lat), y: coordToInt(lon) }),
)
}
}}
Expand Down Expand Up @@ -446,6 +447,7 @@ function MapSectionNonMemo({
</ContextMenuItem>
</>
)}
<ContextMenuSpecificCommandItems />
<Divider />
<ContextMenuItem onClick={zoomToDrone}>
<p>Zoom to drone</p>
Expand Down
37 changes: 35 additions & 2 deletions gcs/src/redux/slices/missionSlice.js
Original file line number Diff line number Diff line change
Expand Up @@ -213,7 +213,7 @@ const missionInfoSlice = createSlice({
[state.activeTab]: true,
}
},
createNewDrawingItem: (state, action) => {
createNewDefaultDrawingItem: (state, action) => {
const { x, y } = action.payload
const drawingItem = newMissionItem(x, y, state.targetInfo)

Expand All @@ -238,6 +238,38 @@ const missionInfoSlice = createSlice({
[state.activeTab]: true,
}
},
createNewSpecificMissionItem: (state, action) => {
const { x, y, z, command } = action.payload

if (!command) {
console.error("Invalid command for new mission item:", action.payload)
return
}

const drawingItem = newMissionItem(x, y, state.targetInfo)
const _type = `${state.activeTab}Items`

drawingItem.seq = state.drawingItems[_type].length
drawingItem.z = z
drawingItem.command = command
drawingItem.mission_type = { mission: 0, fence: 1, rally: 2 }[
state.activeTab
]
if (action.payload.frame) {
drawingItem.frame = getFrameKey(action.payload.frame)
}
if (action.payload.params) {
Object.keys(action.payload.params).forEach((key) => {
drawingItem[key] = action.payload.params[key]
})
}

state.drawingItems[_type].push(drawingItem)
state.unwrittenChanges = {
...state.unwrittenChanges,
[state.activeTab]: true,
}
},
clearDrawingItems: (state) => {
const _type = `${state.activeTab}Items`

Expand Down Expand Up @@ -514,7 +546,8 @@ export const {
updateDrawingItem,
removeDrawingItem,
reorderDrawingItem,
createNewDrawingItem,
createNewDefaultDrawingItem,
createNewSpecificMissionItem,
clearDrawingItems,
createFencePolygon,
setDrawingMissionItems,
Expand Down
Loading