Skip to content

Mission planning with the SDK

The SDK supports creating and running missions on the drone using the mission attribute of the Drone class. Missions are created using a set of instructions that define the actions the drone should perform.

Creating a mission

As an example we will create a mission that goes to the seabed, takes a picture with the main camera, and returns to the surface.

First we define the instructions we want to use. The available instructions are outlined in the Instruction class.

import blueye.protocol as bp

go_to_seabed = bp.Instruction(go_to_seabed_command={"desired_speed": 0.3})
take_picture = bp.Instruction(camera_command={
    "camera_action": bp.CameraAction.CAMERA_ACTION_TAKE_PHOTO
})
go_to_surface = bp.Instruction(go_to_surface_command={"desired_speed": 0.3})

Next we create the mission using these instructions. The mission is created by passing a list of instructions to the prepare_new_mission function. It copies the instructions and numbers them 1 to n in list order, the same numbering the Blueye app uses. The drone reports progress by these ids in MissionStatus.completed_instruction_ids and ReferenceAutoPilot.active_instruction_id.

from blueye.sdk.mission import prepare_new_mission
go_to_seabed_mission = prepare_new_mission(
    instruction_list = [go_to_seabed, take_picture, go_to_surface],
    mission_id = 0,
    mission_name = "Go to seabed and take a picture",
)

Loading and running the mission

With the mission created we can now transfer it to the drone and run it. The easiest way to do this is to use the load_and_run method, which will transfer the mission the drone, wait for it to become ready and then start the mission.

load_and_run returns as soon as the drone has been told to start. Keep the script connected until the mission has ended: the SDK pets the drone's watchdog from a background thread, and when the script exits the drone aborts the mission about 3 seconds later. See Connecting to the drone for what else the drone does then.

A mission does not always end in MISSION_STATE_COMPLETED. The drone aborts it (MISSION_STATE_ABORTED) when it loses its position for more than 3 seconds, when it is stuck, when a waypoint is too far away, or when the watchdog fires. A mission with waypoints that is started without a valid position ends in MISSION_STATE_FAILED_TO_START_MISSION. Another client can clear the mission, which shows as MISSION_STATE_INACTIVE, or load its own in place of yours, which passes through MISSION_STATE_INACTIVE to MISSION_STATE_READY or MISSION_STATE_FAILED_TO_LOAD_MISSION. A restart of the drone's mission node also reports MISSION_STATE_INACTIVE. Wait for all of these, and treat a status of None as not reported yet. load_and_run returns after the drone has reported MISSION_STATE_READY for the new mission, so an INACTIVE read afterwards is not left over from before it. A replacement loaded within the same second can show MISSION_STATE_READY without an INACTIVE in between, which the loop below does not catch; a script that must detect that can give its mission a nonzero mission_id and compare it with MissionStatus.id, which the drone reports for the mission it holds.

import time

import blueye.protocol as bp
from blueye.sdk import Drone

d = Drone()

d.mission.load_and_run(go_to_seabed_mission)

end_states = (
    bp.MissionState.MISSION_STATE_COMPLETED,
    bp.MissionState.MISSION_STATE_ABORTED,
    bp.MissionState.MISSION_STATE_FAILED_TO_START_MISSION,
    bp.MissionState.MISSION_STATE_FAILED_TO_LOAD_MISSION,
    bp.MissionState.MISSION_STATE_INACTIVE,
)
status = d.mission.get_status()
while status is None or status.state not in end_states:
    time.sleep(0.1)
    status = d.mission.get_status()
print(f"Mission ended with state {status.state.name}")

d.disconnect()

Sensor requirements

Some instructions need a sensor the drone may not have. The drone checks them when the mission starts and while it runs:

Instruction Needs Without it
Waypoint, go to home A valid position estimate. A DVL, the drone's own Blueye GNSS, UGPS, USBL or NMEA input over UDP all count, unlike for station keeping. The mission does not start: MISSION_STATE_FAILED_TO_START_MISSION with a NOTIFICATION_TYPE_DRONE_POSITION_IS_UNKNOWN notification. A running mission is aborted after 3 seconds without a valid position.
Go to home The pilot's position as well, sent to the drone as PilotGPSPositionCtrl. The Blueye app sends it; the SDK has no method for it yet. The drone reports the position it holds in PilotGPSPositionTel. The drone reports NOTIFICATION_TYPE_USER_POSITION_IS_UNKNOWN and does not move on from the instruction. Pause or clear the mission.
Photos by distance (CAMERA_ACTION_TAKE_PHOTOS_DISTANCE) A valid position estimate, as for waypoints. The drone reports NOTIFICATION_TYPE_DRONE_POSITION_IS_UNKNOWN and takes no photos.
Depth set point measured from the seabed An altitude sensor: a DVL or a downward-facing Ping sonar. Without any altitude report the instruction is skipped with NOTIFICATION_TYPE_INSTRUCTION_SKIPPED. With a sensor that has no valid reading yet, the drone descends as if the bottom were 50 m below until the sensor sees it.
Tilt multibeam servo A multibeam servo on a guest port. The instruction is skipped with NOTIFICATION_TYPE_INSTRUCTION_SKIPPED.
Go to seabed, go to surface, depth set point from the surface, single photo, photos by time, tilt, wait Nothing beyond the built-in sensors.

The drone also aborts a mission when a waypoint is more than 300 m away, when it detects that it is stuck (NOTIFICATION_TYPE_DRONE_IS_STUCK), and when the client in control stops petting the watchdog, see Connecting to the drone. PositionEstimateTel.position_estimate.is_valid is the position validity the mission checks, navigation_sensors in the same message lists each sensor with its own validity, and get_altitude returns None while the altitude is not valid.

Getting status updates from the mission

The mission progress is published through the NotificationTel messages. These messages can be read manually with the usual telemetry methods, ie. telemetry.get or telemetry.add_msg_callback.

Alternatively, if one initializes the Drone object with log_notifications=True, the notifications will be logged with pythons logging module. This is the recommended way to get notified about mission progress.

from blueye.sdk import Drone

d = Drone(log_notifications=True)

The termial output can however quickly become cluttered when the logs are being printed inbetween the program output. In such cases, it can be useful to run the program in one terminal and then run a separate terminal to read the logs. This can be accomplished by configuring a simple TCP server to listen for log messages. See the Receiving logs in another terminal section for an example of how to do this.

Exporting to JSON

The mission can be exported to a JSON file using the export_to_json method. This allows you to save the mission for later use or share it with others. It is also possible to load the json file in the Blueye App.

import blueye.protocol as bp
from blueye.sdk.mission import export_to_json, prepare_new_mission

# Create a mission
mission = prepare_new_mission(
    instruction_list=[bp.Instruction(go_to_seabed_command={"desired_speed": 0.3})]
)

# Export the mission to a JSON file
export_to_json(mission, "exported_mission.json")

Importing from JSON

You can also import a mission from a JSON file using the import_from_json method. This allows you to load a previously saved mission and run it on the drone.

from blueye.sdk import Drone
from blueye.sdk.mission import import_from_json

mission = import_from_json("exported_mission.json")

d = Drone()
d.mission.load_and_run(mission)
# Stay connected until the mission has ended, as shown above

Examples

Here are some examples outlining how to use some of the mission planning features

Tilt camera

This is a simple example that tilts the camera up and down. Useful for testing with the drone out of the water. The drone limits a tilt instruction to its own tilt range: ±30° on the Pro, X1, X3 and X3 Ultra and ±90° on the X7, see Blueye drone models.

examples/mission_tilt.py

import logging
import logging.handlers
import time

import blueye.protocol as bp

from blueye.sdk import Drone
from blueye.sdk.mission import prepare_new_mission

# Publish runtime logs over TCP (including mission status notifications)
logger = logging.getLogger("blueye.sdk")
logger.setLevel(logging.DEBUG)
socket_handler = logging.handlers.SocketHandler(
    "localhost", logging.handlers.DEFAULT_TCP_LOGGING_PORT
)
logger.addHandler(socket_handler)

# Create some instructions for the mission
tilt_camera_center = bp.Instruction(tilt_main_camera_command={"tilt_angle": {"value": 0.0}})
tilt_camera_top = bp.Instruction(tilt_main_camera_command={"tilt_angle": {"value": 30.0}})
tilt_camera_bottom = bp.Instruction(tilt_main_camera_command={"tilt_angle": {"value": -30.0}})
wait = bp.Instruction(wait_for_command={"wait_for_seconds": 4})

# Create a mission with the instructions
mission = prepare_new_mission(
    instruction_list=[tilt_camera_top, wait, tilt_camera_bottom, wait, tilt_camera_center, wait],
    mission_id=0,
    mission_name="Tilt camera",
)

# Establish a connection to the drone
d = Drone(log_notifications=True)

# Send the mission to the drone and start it
d.mission.load_and_run(mission)

# Stay connected until the mission has ended. If the script exits, the SDK stops petting the
# drone's watchdog and the drone aborts the mission about 3 seconds later. The status is None
# until the drone has reported one, and a mission can end aborted or failed as well as completed.
# It is also over when another client clears it (INACTIVE) or loads its own in its place
# (INACTIVE, then READY or FAILED_TO_LOAD_MISSION).
end_states = (
    bp.MissionState.MISSION_STATE_COMPLETED,
    bp.MissionState.MISSION_STATE_ABORTED,
    bp.MissionState.MISSION_STATE_FAILED_TO_START_MISSION,
    bp.MissionState.MISSION_STATE_FAILED_TO_LOAD_MISSION,
    bp.MissionState.MISSION_STATE_INACTIVE,
)
status = d.mission.get_status()
while status is None or status.state not in end_states:
    time.sleep(0.1)
    status = d.mission.get_status()
print(f"Mission ended with state {status.state.name}")

d.disconnect()
Go to seabed

Another simple example outlining how to create some simple instructions and run them on the drone. This example goes to the seabed, takes a picture with the main camera, and returns to the surface.

examples/mission_seabed.py

import logging
import logging.handlers
import time

import blueye.protocol as bp

from blueye.sdk import Drone
from blueye.sdk.mission import prepare_new_mission

# Set up logging
logger = logging.getLogger("blueye.sdk")
logger.setLevel(logging.DEBUG)
socket_handler = logging.handlers.SocketHandler(
    "localhost", logging.handlers.DEFAULT_TCP_LOGGING_PORT
)
logger.addHandler(socket_handler)

# Create some instructions for the mission
go_to_seabed = bp.Instruction(go_to_seabed_command={"desired_speed": 0.3})
take_picture = bp.Instruction(
    camera_command={"camera_action": bp.CameraAction.CAMERA_ACTION_TAKE_PHOTO}
)
go_to_surface = bp.Instruction(go_to_surface_command={"desired_speed": 0.3})

# Create a mission with the instructions
mission = prepare_new_mission(
    instruction_list=[go_to_seabed, take_picture, go_to_surface],
    mission_id=0,
    mission_name="Go to seabed and take a picture",
)

# Establish a connection to the drone
d = Drone(log_notifications=True)

# Send the mission to the drone and start it
d.mission.load_and_run(mission)

# Stay connected until the mission has ended. If the script exits, the SDK stops petting the
# drone's watchdog and the drone aborts the mission about 3 seconds later. The status is None
# until the drone has reported one, and a mission can end aborted or failed as well as completed.
# It is also over when another client clears it (INACTIVE) or loads its own in its place
# (INACTIVE, then READY or FAILED_TO_LOAD_MISSION).
end_states = (
    bp.MissionState.MISSION_STATE_COMPLETED,
    bp.MissionState.MISSION_STATE_ABORTED,
    bp.MissionState.MISSION_STATE_FAILED_TO_START_MISSION,
    bp.MissionState.MISSION_STATE_FAILED_TO_LOAD_MISSION,
    bp.MissionState.MISSION_STATE_INACTIVE,
)
status = d.mission.get_status()
while status is None or status.state not in end_states:
    time.sleep(0.1)
    status = d.mission.get_status()
print(f"Mission ended with state {status.state.name}")

d.disconnect()
Go to waypoints

This example shows how one can create multiple waypoints and have the drone go to each of them in sequence. examples/mission_go_to_waypoint.py

import logging
import logging.handlers
import time

import blueye.protocol as bp

from blueye.sdk import Drone
from blueye.sdk.mission import create_waypoint_instruction, prepare_new_mission

# Publish runtime logs over TCP (including mission status notifications)
logger = logging.getLogger("blueye.sdk")
logger.setLevel(logging.DEBUG)
socket_handler = logging.handlers.SocketHandler(
    "localhost", logging.handlers.DEFAULT_TCP_LOGGING_PORT
)
logger.addHandler(socket_handler)

# Create some waypoints in a (roughly) square pattern
#    10.4174°E           10.4176°E
#    B ------------------ C 63.4418°N
#    |                    |
#    |                    |
#    |                    |
#    A ------------------ D 63.4415°N
wp_a = create_waypoint_instruction("Point A", 63.4415, 10.4174, 0)
wp_b = create_waypoint_instruction("Point B", 63.4418, 10.4174, 0)
wp_c = create_waypoint_instruction("Point C", 63.4418, 10.4176, 0)
wp_d = create_waypoint_instruction("Point D", 63.4415, 10.4176, 0)


# Create a mission with the instructions
mission = prepare_new_mission(
    instruction_list=[wp_a, wp_b, wp_c, wp_d, wp_a],
    mission_id=0,
    mission_name="Go to waypoints",
)

# Establish a connection to the drone
d = Drone(log_notifications=True)

# Send the mission to the drone and start it
d.mission.load_and_run(mission)

# Stay connected until the mission has ended. If the script exits, the SDK stops petting the
# drone's watchdog and the drone aborts the mission about 3 seconds later. The status is None
# until the drone has reported one, and a mission can end aborted or failed as well as completed.
# It is also over when another client clears it (INACTIVE) or loads its own in its place
# (INACTIVE, then READY or FAILED_TO_LOAD_MISSION).
end_states = (
    bp.MissionState.MISSION_STATE_COMPLETED,
    bp.MissionState.MISSION_STATE_ABORTED,
    bp.MissionState.MISSION_STATE_FAILED_TO_START_MISSION,
    bp.MissionState.MISSION_STATE_FAILED_TO_LOAD_MISSION,
    bp.MissionState.MISSION_STATE_INACTIVE,
)
status = d.mission.get_status()
while status is None or status.state not in end_states:
    time.sleep(0.1)
    status = d.mission.get_status()
print(f"Mission ended with state {status.state.name}")

d.disconnect()