0
0
Pcb-designHow-ToBeginner · 4 min read

How to Send Velocity Command Using DroneKit: Simple Guide

To send a velocity command using dronekit, use the vehicle.send_mavlink() method with a properly constructed SET_POSITION_TARGET_LOCAL_NED message specifying velocity components. This lets you control the drone's movement by setting velocity in the x, y, and z directions.
📐

Syntax

Use the vehicle.send_mavlink() function with a SET_POSITION_TARGET_LOCAL_NED MAVLink message to send velocity commands. The key parameters include:

  • vx, vy, vz: Velocity in meters per second along the x (forward), y (right), and z (down) axes.
  • type_mask: A bitmask to specify which fields to ignore or use. For velocity control, position and acceleration bits are ignored.
  • coordinate_frame: Usually set to MAV_FRAME_LOCAL_NED for local velocity commands.
python
msg = vehicle.message_factory.set_position_target_local_ned_encode(
    0,       # time_boot_ms (not used)
    0, 0,    # target system, target component
    mavutil.mavlink.MAV_FRAME_LOCAL_NED, # coordinate frame
    0b0000111111000111, # type_mask (only velocity enabled)
    0, 0, 0, # x, y, z positions (not used)
    vx, vy, vz, # velocity in m/s
    0, 0, 0, # acceleration (not used)
    0, 0)    # yaw, yaw_rate (not used)
vehicle.send_mavlink(msg)
💻

Example

This example connects to the drone, arms it, takes off to 10 meters, then sends a velocity command to move forward at 5 m/s for 5 seconds before landing.

python
from dronekit import connect, VehicleMode
from pymavlink import mavutil
import time

# Connect to the vehicle
vehicle = connect('127.0.0.1:14550', wait_ready=True)

# Arm and takeoff to 10 meters
def arm_and_takeoff(aTargetAltitude):
    while not vehicle.is_armable:
        time.sleep(1)
    vehicle.mode = VehicleMode("GUIDED")
    vehicle.armed = True
    while not vehicle.armed:
        time.sleep(1)
    vehicle.simple_takeoff(aTargetAltitude)
    while True:
        if vehicle.location.global_relative_frame.alt >= aTargetAltitude * 0.95:
            break
        time.sleep(1)

arm_and_takeoff(10)

# Function to send velocity command

def send_velocity(vx, vy, vz, duration):
    msg = vehicle.message_factory.set_position_target_local_ned_encode(
        0, 0, 0, mavutil.mavlink.MAV_FRAME_LOCAL_NED,
        0b0000111111000111, # ignore pos, accel, yaw
        0, 0, 0, # pos
        vx, vy, vz, # velocity
        0, 0, 0, # accel
        0, 0) # yaw
    for _ in range(duration):
        vehicle.send_mavlink(msg)
        vehicle.flush()
        time.sleep(1)

# Move forward at 5 m/s for 5 seconds
send_velocity(5, 0, 0, 5)

# Land the vehicle
vehicle.mode = VehicleMode("LAND")
vehicle.close()
Output
Vehicle arms, takes off to 10m altitude, moves forward at 5 m/s for 5 seconds, then lands.
⚠️

Common Pitfalls

  • Not setting the correct type_mask: If you don't mask out position and acceleration fields, the drone may ignore velocity commands.
  • Forgetting to send commands repeatedly: Velocity commands must be sent continuously (e.g., every second) to maintain movement.
  • Incorrect coordinate frame: Use MAV_FRAME_LOCAL_NED for local velocity; using other frames can cause unexpected motion.
  • Not arming or setting mode to GUIDED: The drone must be armed and in GUIDED mode to accept velocity commands.
python
## Wrong way: Missing type_mask or wrong mask
msg_wrong = vehicle.message_factory.set_position_target_local_ned_encode(
    0, 0, 0, mavutil.mavlink.MAV_FRAME_LOCAL_NED,
    0, # type_mask zero means all fields used (wrong)
    0, 0, 0,
    5, 0, 0,
    0, 0, 0,
    0, 0)
vehicle.send_mavlink(msg_wrong)

## Right way: Proper type_mask to enable velocity only
msg_right = vehicle.message_factory.set_position_target_local_ned_encode(
    0, 0, 0, mavutil.mavlink.MAV_FRAME_LOCAL_NED,
    0b0000111111000111,
    0, 0, 0,
    5, 0, 0,
    0, 0, 0,
    0, 0)
vehicle.send_mavlink(msg_right)
📊

Quick Reference

Velocity Command Cheat Sheet:

  • vx, vy, vz: Velocity in m/s along x (forward), y (right), z (down).
  • type_mask = 0b0000111111000111: Enables velocity control only.
  • coordinate_frame = MAV_FRAME_LOCAL_NED: Use local frame for velocity commands.
  • Send commands repeatedly (e.g., every second) to maintain velocity.
  • Ensure vehicle is armed and in GUIDED mode.

Key Takeaways

Use vehicle.send_mavlink() with SET_POSITION_TARGET_LOCAL_NED message to send velocity commands.
Set type_mask to 0b0000111111000111 to enable velocity control and ignore position/acceleration.
Send velocity commands repeatedly to keep the drone moving at the desired speed.
Always arm the drone and set mode to GUIDED before sending velocity commands.
Use MAV_FRAME_LOCAL_NED coordinate frame for local velocity control.