惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

月光博客
月光博客
Martin Fowler
Martin Fowler
博客园_首页
量子位
T
Tailwind CSS Blog
博客园 - Franky
G
Google Developers Blog
D
DataBreaches.Net
Vercel News
Vercel News
B
Blog
Recent Announcements
Recent Announcements
S
SegmentFault 最新的问题
M
MIT News - Artificial intelligence
爱范儿
爱范儿
博客园 - 【当耐特】
The Cloudflare Blog
H
Help Net Security
云风的 BLOG
云风的 BLOG
P
Proofpoint News Feed
C
Check Point Blog
有赞技术团队
有赞技术团队
Microsoft Security Blog
Microsoft Security Blog
酷 壳 – CoolShell
酷 壳 – CoolShell
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More

DEV Community

Authentication Security Deep Dive: From Brute Force to Salted Hashing (With Java Examples) Why AI Systems Don’t Fail — They Drift Spilling beans for how i learn for exam😁"Reinforcement Learning Cheat Sheet" I Replaced Chrome with Safari for AI Browser Automation. Here's What Broke (and What Finally Worked) How Python Borrows Other People's Work The $40 Architecture: Processing 1 Billion API Requests with 99.99% Uptime Vibe Coding: A Workflow Guide (From Zero to SaaS) Most webhook security guides protect the wrong side. The scary part is delivery. Headless CMS for TanStack Start: Build a Blog with Cosmic EU Age Verification App "Hacked in 2 Minutes" — What Actually Happened Comfy Cloud’s delete function does not actually remove files Running AI Models on GPU Cloud Servers: A Beginner Guide Event-driven media intelligence with AWS Step Functions and Bedrock I scored 500 AI prompts across 8 quality dimensions — here's what broke How to Call Google Gemini API from Next.js (Free Tier, No Backend Needed) The Portal Protocol: Reclaiming Human Connection in the Age of AI How to Fix Your Team's Scattered Knowledge Problem With a Self-Hosted Forum Intro to tc Cloud Functors: A Graph-First Mental Model for the Modern Cloud Designing Multi-Tenant Backends With Both Ownership and Team Access I Built a Neumorphic CSS Library with 77+ Components — Here's What I Learned PostgreSQL Performance Optimization: Why Connection Pooling Is Critical at Scale Cómo construí un SaaS multi-rubro para gestionar expensas en Argentina con FastAPI + Vue 3 🚀 I Built an Ethical Hacking Scanner Tool – Open Source Project I Replaced /usage and /context in Claude Code With a Single Statusline A Pythonic Way to Handle Emails (IMAP/SMTP) with Auto-Discovery and AI-Ready Design I Collected 8.9 Million Polymarket Price Points — Here's What I Found About How Markets Really Move EcoTrack AI — Carbon Footprint Tracker & Dashboard Everyone's Using AI. No One Agrees How. 5 self-hosted ebook managers worth trying in 2026 Building Your First AI Agent with LangChain: From Chatbot to Autonomous Assistant
Building a Custom Autonomous Drone Stack - Part 3: The Ze...
Harsh Pandhe · 2026-06-25 · via DEV Community

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.