Skip to content

Commit e0c48fa

Browse files
authored
Add check and error message for mission item and mission type (#601)
* Add check and error message for mission item and mission type * Address copilot review comments
1 parent 8c4e9d1 commit e0c48fa

1 file changed

Lines changed: 67 additions & 0 deletions

File tree

radio/app/controllers/missionController.py

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,34 @@ def _convertCoordinate(self, coordinate) -> Number:
6161
f"Invalid coordinate type {type(coordinate)}. Must be int or float."
6262
)
6363

64+
def _getMissionName(self, mission_type: int) -> str:
65+
"""
66+
Get the name of the mission type.
67+
68+
Args:
69+
mission_type (int): The type of mission to get the name for.
70+
"""
71+
if mission_type == TYPE_MISSION:
72+
return "mission"
73+
elif mission_type == TYPE_FENCE:
74+
return "fence"
75+
elif mission_type == TYPE_RALLY:
76+
return "rally"
77+
else:
78+
raise ValueError(f"Invalid mission type {mission_type}")
79+
80+
def _getCommandName(self, command: int) -> str:
81+
"""
82+
Get the name of the command type.
83+
84+
Args:
85+
command (int): The command to get the name for.
86+
"""
87+
try:
88+
return mavutil.mavlink.enums["MAV_CMD"][command].name
89+
except KeyError:
90+
return f"Unknown command {command}"
91+
6492
def getCurrentMission(self, mission_type: int) -> Response:
6593
"""
6694
Get the current mission of a specific type from the drone.
@@ -652,6 +680,45 @@ def importMissionFromFile(self, mission_type: int, file_path: str) -> Response:
652680
}
653681

654682
for wp in loader.wpoints:
683+
# Check if mission type correlates to correct command
684+
if (
685+
(
686+
mission_type == TYPE_RALLY
687+
and wp.command != mavutil.mavlink.MAV_CMD_NAV_RALLY_POINT
688+
)
689+
or (
690+
mission_type == TYPE_FENCE
691+
and wp.command
692+
not in [
693+
mavutil.mavlink.MAV_CMD_NAV_FENCE_RETURN_POINT,
694+
mavutil.mavlink.MAV_CMD_NAV_FENCE_POLYGON_VERTEX_INCLUSION,
695+
mavutil.mavlink.MAV_CMD_NAV_FENCE_POLYGON_VERTEX_EXCLUSION,
696+
mavutil.mavlink.MAV_CMD_NAV_FENCE_CIRCLE_INCLUSION,
697+
mavutil.mavlink.MAV_CMD_NAV_FENCE_CIRCLE_EXCLUSION,
698+
]
699+
)
700+
or (
701+
mission_type == TYPE_MISSION
702+
and wp.command
703+
in [
704+
mavutil.mavlink.MAV_CMD_NAV_RALLY_POINT,
705+
mavutil.mavlink.MAV_CMD_NAV_FENCE_RETURN_POINT,
706+
mavutil.mavlink.MAV_CMD_NAV_FENCE_POLYGON_VERTEX_INCLUSION,
707+
mavutil.mavlink.MAV_CMD_NAV_FENCE_POLYGON_VERTEX_EXCLUSION,
708+
mavutil.mavlink.MAV_CMD_NAV_FENCE_CIRCLE_INCLUSION,
709+
mavutil.mavlink.MAV_CMD_NAV_FENCE_CIRCLE_EXCLUSION,
710+
]
711+
)
712+
):
713+
self.drone.logger.error(
714+
f"Waypoint command {self._getCommandName(wp.command)} does not match mission type {self._getMissionName(mission_type)}"
715+
)
716+
return {
717+
"success": False,
718+
"message": f"Could not load the waypoint file. Waypoint command {self._getCommandName(wp.command)} does not match mission type {self._getMissionName(mission_type)}",
719+
}
720+
721+
# Convert coordinates to the correct format
655722
if hasattr(wp, "x") and hasattr(wp, "y"):
656723
wp.x = self._convertCoordinate(wp.x)
657724
wp.y = self._convertCoordinate(wp.y)

0 commit comments

Comments
 (0)