I want to control the drone using MAVSDK

Is there a command to simply move the drone forward, backward, ascend and descend using MAVSDK? If so, could you please share the code?

1 Like

There is manual control command possible using mavsdk.
For eg: if you are using Python, you can find the description here and an example code here.

For moving, you need to translate the required directions to the appropriate control axis.
For eg: in your case,
forward/backward → Pitch
ascend/descend → Thrust

You can explore the mavsdk documentation for more features.

Thank you for your reply.
I can’t open the code example link, can you share it again?
Instead of manual control with a joystick, etc., is there a command that can give directions in meters, such as moving forward 1m or moving 2m to the right?
thank you.

in C++
move 1m to north and 2m to east:

 mavsdk::Telemetry::PositionVelocityNed currentPosition = telemetry.position_velocity_ned();
 mavsdk::Offboard::PositionNedYaw targetPostion;
 targetPosition.north_m = currentPosition.north_m + 1;
 targetPosition.east_m = currentPosition.east_m + 2;
 targetPosition.down_m = currentPostion.down_m;
1 Like

I would probably suggest offboard position control as @Artem_Borisov suggested, rather than manual control because it comes in units like meters or meters/second which I believe is what you’re after.

1 Like

Thank you for your reply.
Actually, I tried off-board control using the program shown below, but after going up, it immediately went down and stopped moving near the ground.
If you have any questions, please let me know.
The drone I’m using is X500V2, FC is Pixhawks6C, and firmware is PX4.

#!/usr/bin/env python3

import asyncio

from mavsdk import System
from mavsdk.offboard import (OffboardError, VelocityBodyYawspeed)

async def run():
    """ Does Offboard control using velocity body coordinates. """

    drone = System()
    await drone.connect(system_address="serial:///dev/ttyACM0:57600")

    print("Waiting for drone to connect...")
    async for state in drone.core.connection_state():
        if state.is_connected:
            print(f"-- Connected to drone!")

    print("Waiting for drone to have a global position estimate...")
    async for health in drone.telemetry.health():
        if health.is_global_position_ok and health.is_home_position_ok:
            print("-- Global position estimate OK")

    print("-- Arming")
    await drone.action.arm()

    print("-- Setting initial setpoint")
    await drone.offboard.set_velocity_body(
        VelocityBodyYawspeed(0.0, 0.0, 0.0, 0.0))

    print("-- Starting offboard")
        await drone.offboard.start()
    except OffboardError as error:
        print(f"Starting offboard mode failed with error code: \
        print("-- Disarming")
        await drone.action.disarm()

    print("-- Turn clock-wise and climb")
    await drone.offboard.set_velocity_body(
        VelocityBodyYawspeed(0.0, 0.0, -1.0, 0.0))
    await asyncio.sleep(4)

    print("-- Turn back anti-clockwise")
    await drone.offboard.set_velocity_body(
        VelocityBodyYawspeed(1.0, 0.0, 0.0, 0.0))
    await asyncio.sleep(1)

    print("-- Wait for a bit")
    await drone.offboard.set_velocity_body(
        VelocityBodyYawspeed(0.0, 0.0, 0.0, 0.0))
    await asyncio.sleep(8)

    await drone.offboard.set_velocity_body(
    await asyncio.sleep(1)

    print("-- Stopping offboard")
        await drone.offboard.stop()
    except OffboardError as error:
        print(f"Stopping offboard mode failed with error code: \

if __name__ == "__main__":
    # Run the asyncio loop

You need to check what errors come up over telemetry. Or at least look at the log, or share it here.

I’m new to PX4. May I know does offboard still working in a GPS-denied environment?

Please don’t necrobump. Open a new thread with your question and give some context.