Skip to main content

Lightweight Pixhawk/ArduPilot controller utilities using pymavlink

Project description

pixhawkcontroller

Lightweight Python utilities to connect and control Pixhawk / ArduPilot flight controllers using pymavlink.

Build PyPI License Python


✨ Features

  • 🔌 Auto-detect Pixhawk on serial ports (Windows, Linux, macOS).
  • 🌐 Supports Serial, UDP, and TCP connections:
    • COMx (Windows), /dev/ttyUSBx (Linux), /dev/tty.usbmodem* (macOS)
    • udp:127.0.0.1:14550 for SITL
    • tcp:192.168.1.100:5760 for network connections
  • 🛠 Servo control: Set or repeat PWM outputs.
  • 🎛 RC channel override with safety reset.
  • 🎶 Play tones via the flight controller buzzer (e.g., Twinkle Twinkle, Mario tune).
  • 📡 Telemetry snapshot (mode, GPS fix, battery, armed flag, location).
  • 🛡 Vehicle info decoding (autopilot type, version, board, vendor/product IDs).
  • 🚁 Flight mode switching (ArduCopter, ArduPlane, Rover supported).

📦 Installation

# Recommended: create a fresh virtual environment
python3 -m venv venv
source venv/bin/activate  # or venv\Scripts\activate on Windows

# Install from PyPI (when released)
pip install pixhawkcontroller

# OR install from source
git clone https://github.com/Shahriar88/pixhawkcontroller.git
cd pixhawkcontroller
pip install -e .

Dependencies:


🚀 Quick Start

1. Import and connect

from pixhawkcontroller import FlightControllerInterface, TonesQb

# Auto-detect Pixhawk (USB VID/PID known)
fc = FlightControllerInterface()
fc.connect()

# Or explicitly:
# fc = FlightControllerInterface(device='udp:127.0.0.1:14550')  # Connect to SITL (simulator)
# fc = FlightControllerInterface(device='COM3', baudrate=115200)  # Windows COM port
# fc = FlightControllerInterface(device='/dev/ttyUSB0', baudrate=115200)  # Linux USB port

2. Print flight controller info

fc.print_info()

This prints firmware, board ID, MAVLink system ID, and more.


3. Control examples

# Direct servo control:
# - First argument: servo output number (1–16 depending on config)
# - Second argument: PWM microseconds (typical RC range ~1000–2000 µs)
fc.set_servo(9, 1500)

# Repeat servo pulses:
# Toggle a servo output several times with a delay between movements
fc.repeat_servo(7, 1900, repeat_count=3, cycle_time=0.5)

# RC override (use carefully!):
# Temporarily override pilot’s RC input, e.g., throttle at mid-stick
fc.arm()
fc.set_rc_pwm(3, 1500)   # Channel 3 = throttle on many setups
fc.clear_rc_overrides()
fc.disarm()

# Play a tune on the buzzer
fc.play_tune(TonesQb.twinkle_little_star)

# Print a telemetry snapshot (mode, GPS, battery, location, armed state)
fc.print_telemetry()

4. Close connection

fc.close()

Always close the MAVLink connection before exiting.


🎶 Tunes

This project uses QBasic PLAY-style tone strings (not RTTTL).
They are directly compatible with the ArduPilot buzzer and can be tested using the official ToneTester tool.

Preloaded tunes in TonesQb:

class TonesQb:
    twinkle_little_star = "T200 L4CCGGAAG2FFEEDDC2"
    def_tone = "MFT240L8 O4aO5060708dc O4aO5dc O4aO5dc L16dcdcdcdc"

👉 Explanation:

  • T200 → Tempo (200 quarter notes per minute)
  • L4 → Default note length (quarter notes)
  • CCGGAAG2 → Sequence of notes (with octaves and lengths)
  • MFT240 → Music format / tempo modifier
  • O4, O5 → Set octave (O4 = 4th octave, O5 = 5th)
  • Letters (a, c, d) represent notes; numbers modify duration

Adding Your Own Tunes

You can extend TonesQb or make your own class:

class MyTunes:
    super_mario = (
        "T120 L8 O5 "
        "E6E6P32E6 L4C6E6G6 P G C6 P E A B L16A# A G."
    )

Testing Tunes

Copy any string into the ToneTester and press play to preview it on your PC before sending it to your Pixhawk.


🛡 Safety Notes

⚠️ Important: Test first in SITL or with propellers removed.

  • Commands like .arm(), .set_servo(), and .set_rc_pwm() can move motors/servos.

  • Always confirm your vehicle type and wiring before sending commands.

  • To run ArduPilot in simulation:

    sim_vehicle.py -v ArduCopter -w --console --map
    

🧩 Project Structure

pixhawkcontroller/
├── __init__.py        # Exports FlightControllerInterface, TonesQb
├── main.py            # Core implementation
├── __version__.py    
setup.py
README.md
LICENSE
pyproject.toml

📂 Extended Example

Here’s a full demo script that shows multiple features in one go:

import time
from pixhawkcontroller import FlightControllerInterface, TonesQb

fc = FlightControllerInterface()
fc.connect()

# Print board info
fc.print_info()

# Switch between flight modes (varies by vehicle type)
for mode in ["MANUAL","GUIDED", "AUTO", "RTL"]:
    fc.set_mode(mode)
	time.sleep(1)
    fc.print_telemetry()

# Servo control (PWM ranges)
fc.set_servo(9, 900)   # low end
time.sleep(2)
fc.set_servo(9, 1500)  # neutral
time.sleep(2)
fc.set_servo(9, 1900)  # high end

# RC override (channel 3 = throttle mid)
fc.arm()
fc.set_rc_pwm(3, 1500)
time.sleep(2)
fc.clear_rc_overrides()
fc.disarm()

# Play buzzer tunes
fc.play_tune(TonesQb.def_tone)
time.sleep(1)
fc.play_tune(TonesQb.twinkle_little_star)

# Telemetry snapshot
fc.print_telemetry()

# Clean up
fc.close()

✅ Requirements

  • Python 3.8+ (tested on 3.9–3.12)
  • ArduPilot firmware (Copter/Plane/Rover/Sub) speaking MAVLink
  • Windows, Linux, or macOS with access to the Pixhawk serial device
  • ✅ Verified working on Pixhawk 2.4.8 hardware

🧪 Supported / Not Supported

  • ✅ ArduPilot-based controllers (Pixhawk family) over Serial/UDP/TCP via pymavlink
  • ❌ PX4 APIs (not targeted; may work for generic MAVLink pieces but not guaranteed)

🛠 Troubleshooting

No device found / auto-detect fails

  • On Linux, ensure your user is in the dialout (or equivalent) group, then re-login:
    sudo usermod -aG dialout $USER
    

📚 References


🤝 Contributing

PRs and issues welcome!
Open an issue if you spot a bug or want a feature.


📜 License

GPL-3.0-or-later © 2025 Md Shahriar Forhad
See LICENSE for details.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

pixhawkcontroller-0.0.1.tar.gz (32.0 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

pixhawkcontroller-0.0.1-py3-none-any.whl (29.1 kB view details)

Uploaded Python 3

File details

Details for the file pixhawkcontroller-0.0.1.tar.gz.

File metadata

  • Download URL: pixhawkcontroller-0.0.1.tar.gz
  • Upload date:
  • Size: 32.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.7

File hashes

Hashes for pixhawkcontroller-0.0.1.tar.gz
Algorithm Hash digest
SHA256 24d794cd2fc29f483d99fdfadd8235dab3c046e12c14e415ecb401ce759b5fed
MD5 59ea41fd536e44bbf707030bc979fadd
BLAKE2b-256 b599fed07d44e80e3806e6708048f5eb1de741eeca608f4104d1b71e6b72c0ec

See more details on using hashes here.

File details

Details for the file pixhawkcontroller-0.0.1-py3-none-any.whl.

File metadata

File hashes

Hashes for pixhawkcontroller-0.0.1-py3-none-any.whl
Algorithm Hash digest
SHA256 0b79b94cfbe8de2fd2a747d452ee59011e95aa53b6989d0bf2e3fee38412caa7
MD5 72c02fef976f9d0c86de30ba243a9ebb
BLAKE2b-256 1d24d2c704fb455e10b5d12bf06b346ec2c795a4b13fd2e492013220ed63a824

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page