Surviving GPS-Denied Environments with VELOCITY_MASK
If you're flying a micro-UAV indoors using only an optical flow sensor and a downward-facing LiDAR, standard autonomous flight commands will quickly disappoint you.
When you issue a traditional MAV_CMD_NAV_TAKEOFF, ArduPilot expects to have access to global position information. Without GPS, the navigation stack may refuse to move.
To work around this, we bypassed the higher-level navigation controller and injected raw MAVLink commands over serial using set_attitude_target_send(). This allowed the drone to climb using only its onboard sensors.
Getting airborne, however, is only half the battle.
Keeping the drone in the air is where things get interesting.
The "Zero-Throttle Drop" Trap
During our first tethered test flights, we managed to climb to roughly one meter in GUIDED mode.
To command a stable hover, we switched the vehicle into LOITER.
The instant the mode changed, the motors slowed down and the drone dropped.
What happened?
The issue wasn't hardware.
It was a software conflict.
GUIDED mode is completely autonomous and ignores pilot stick inputs.
LOITER, on the other hand, is a hybrid mode.
As soon as the flight controller entered LOITER, it checked the RC transmitter.
Our throttle stick was resting at zero.
ArduPilot interpreted that as a pilot command to descend.
The Pure Autonomy Fix: VELOCITY_MASK
Instead of relying on physical joysticks, we stayed entirely inside GUIDED mode and continuously sent a zero-velocity command.
Using set_position_target_local_ned_send(), we can specify a bitmask that tells the Pixhawk exactly which fields matter.
A mask value of 3527 (0b110111000111) means:
- Ignore position coordinates
- Ignore acceleration
- Ignore yaw
- Enforce X, Y, and Z velocity
By commanding:
Vx = 0
Vy = 0
Vz = 0
we are effectively telling the flight controller:
Hold perfectly still.
Velocity Hold Loop
VELOCITY_MASK = 3527
# Maintain hover for 10 seconds
for i in range(20):
master.mav.set_position_target_local_ned_send(
0,
master.target_system,
master.target_component,
mavutil.mavlink.MAV_FRAME_LOCAL_NED,
VELOCITY_MASK,
0, 0, 0, # Position (ignored)
0, 0, 0, # Velocity = 0 m/s
0, 0, 0, # Acceleration (ignored)
0, 0 # Yaw and yaw rate (ignored)
)
time.sleep(0.5)
With this loop running, optical flow measurements continuously feed the EKF3.
If the drone drifts due to airflow or frame vibrations, the Pixhawk automatically adjusts motor outputs to force the physical velocity back toward zero.
Why Velocity Commands Work Better Than Position Commands Indoors
Position commands assume absolute position knowledge.
Indoors, position estimates often drift.
Velocity control is far more forgiving.
Instead of saying:
Be exactly at coordinate X=0.52 meters.
we say:
Maintain zero velocity.
This lets the flight controller use optical flow and LiDAR to make tiny corrections without fighting an imperfect global reference frame.
The Panic Button
Never fly without an emergency failsafe.
If your Python script crashes unexpectedly while the vehicle remains armed, the drone will continue executing the last command it received.
That's a bad day.
We wrapped the entire flight sequence inside an emergency interceptor.
Emergency Landing Function
import sys
def emergency_land():
print("\n[!!!] EMERGENCY: INITIATING LAND")
mode_id = master.mode_mapping()['LAND']
master.mav.set_mode_send(
master.target_system,
mavutil.mavlink.MAV_MODE_FLAG_CUSTOM_MODE_ENABLED,
mode_id
)
master.mav.command_long_send(
master.target_system,
master.target_component,
mavutil.mavlink.MAV_CMD_NAV_LAND,
0,
0, 0, 0, 0, 0, 0, 0
)
sys.exit(0)
Wrapping the Flight Loop
try:
# 1. Arm
# 2. Takeoff
# 3. Velocity hold
pass
except KeyboardInterrupt:
emergency_land()
except Exception as e:
print(f"ERROR: {e}")
emergency_land()
Now, pressing Ctrl+C immediately overrides the current mission and forces a controlled descent.
The flight controller handles the landing sequence and disarms automatically after touchdown.
Final Thoughts
One of the biggest lessons we learned was this:
Separate high-level logic from low-level reflexes.
Let your companion computer make decisions.
Let the flight controller do what it does best:
- Sensor fusion
- State estimation
- Motor control
- Stabilization
- Recovery
By respecting this separation, you can build an autonomous stack that remains reliable even in GPS-denied environments.
And that's exactly what robotics is all about.
Not replacing the flight controller.
Working with it.











