I cannot manually set the depth of the ROV

Hi, I want to set depth for ROV. I use the link set-target-depth-attitude in the code to set the depth, I found that no matter what number I set in the set_target_depth() function, the ROV was never able to reach my preset depth. ROV always maintains depth at a depth of about 15cm from the surface of the water. Why is this? Looking forward to your reply.

Example of how to set target depth in depth hold mode with pymavlink

import time
import math
# Import mavutil
from pymavlink import mavutil
# Imports for attitude
from pymavlink.quaternion import QuaternionBase

def set_target_depth(depth):
    """ Sets the target depth while in depth-hold mode.

    Uses https://mavlink.io/en/messages/common.html#SET_POSITION_TARGET_GLOBAL_INT

    'depth' is technically an altitude, so set as negative meters below the surface
        -> set_target_depth(-1.5) # sets target to 1.5m below the water surface.

        int(1e3 * (time.time() - boot_time)), # ms since boot
        master.target_system, master.target_component,
        type_mask=( # ignore everything except z position
            mavutil.mavlink.POSITION_TARGET_TYPEMASK_X_IGNORE |
            mavutil.mavlink.POSITION_TARGET_TYPEMASK_Y_IGNORE |
            # DON'T mavutil.mavlink.POSITION_TARGET_TYPEMASK_Z_IGNORE |
            mavutil.mavlink.POSITION_TARGET_TYPEMASK_VX_IGNORE |
            mavutil.mavlink.POSITION_TARGET_TYPEMASK_VY_IGNORE |
            mavutil.mavlink.POSITION_TARGET_TYPEMASK_VZ_IGNORE |
            mavutil.mavlink.POSITION_TARGET_TYPEMASK_AX_IGNORE |
            mavutil.mavlink.POSITION_TARGET_TYPEMASK_AY_IGNORE |
            mavutil.mavlink.POSITION_TARGET_TYPEMASK_AZ_IGNORE |
            # DON'T mavutil.mavlink.POSITION_TARGET_TYPEMASK_FORCE_SET |
            mavutil.mavlink.POSITION_TARGET_TYPEMASK_YAW_IGNORE |
        ), lat_int=0, lon_int=0, alt=depth, # (x, y WGS84 frame pos - not used), z [m]
        vx=0, vy=0, vz=0, # velocities in NED frame [m/s] (not used)
        afx=0, afy=0, afz=0, yaw=0, yaw_rate=0
        # accelerations in NED frame [N], yaw, yaw_rate
        #  (all not supported yet, ignored in GCS Mavlink)

def set_target_attitude(roll, pitch, yaw):
    """ Sets the target attitude while in depth-hold mode.

    'roll', 'pitch', and 'yaw' are angles in degrees.

        int(1e3 * (time.time() - boot_time)), # ms since boot
        master.target_system, master.target_component,
        # allow throttle to be controlled by depth_hold mode
        # -> attitude quaternion (w, x, y, z | zero-rotation is 1, 0, 0, 0)
        QuaternionBase([math.radians(angle) for angle in (roll, pitch, yaw)]),
        0, 0, 0, 0 # roll rate, pitch rate, yaw rate, thrust

# Create the connection
master = mavutil.mavlink_connection('udpin:')
boot_time = time.time()
# Wait a heartbeat before sending commands

# arm ArduSub autopilot and wait until confirmed

# set the desired operating mode
DEPTH_HOLD_MODE = master.mode_mapping()[DEPTH_HOLD]
while not master.wait_heartbeat().custom_mode == DEPTH_HOLD_MODE:

# set a depth target

# clean up (disarm) at the end

Try commenting out the X_IGNORE and Y_IGNORE lines as well, see this example: Don't ignore position · clydemcqueen/ardusub-gitbook@6b26cd3 · GitHub

ArduSub will ignore the x and y in ALT_HOLD mode – only the z will be used, see: ardupilot/GCS_Mavlink.cpp at master · ArduPilot/ardupilot · GitHub

I hope this helps.

1 Like

Hi @clyde, thanks for picking up on this - that’s a bug, so I’ve raised an issue for it here :slight_smile:

@robotmark in the meantime Clyde’s suggested workaround of not setting the ignore flags of X and Y position should avoid the issue, although maybe put a note in any code that does that that it will need to be modified once you’re using a newer version of ArduSub that has the fix included.

Thank you !

Thank you very much !