feat: Simplify drone controller to basic flight pattern, overhaul installation guide, and update related scripts.

This commit is contained in:
2026-01-07 21:02:31 +00:00
parent 760293d896
commit e266f75fca
9 changed files with 919 additions and 1407 deletions

View File

@@ -1,202 +1,184 @@
# ArduPilot + Gazebo Simulation
# ArduPilot + Gazebo Guide
## Prerequisites
## Overview
Before running, ensure your environment is set up:
```bash
source activate.sh
# or
source ~/.ardupilot_env && source ~/venv-ardupilot/bin/activate
```
## Quick Start (2 Terminals)
**Terminal 1 - Start Gazebo:**
```bash
./scripts/run_ardupilot_sim.sh runway
```
**Terminal 2 - Start Controller:**
```bash
./scripts/run_ardupilot_controller.sh
```
This project uses:
- **ArduPilot SITL**: Software-in-the-loop flight controller
- **Gazebo**: 3D physics simulation
- **MAVLink**: Communication protocol
## Architecture
```
┌─────────────────┐ ┌────────────────┐ ┌─────────────────┐
│ Gazebo │◄───►│ ArduPilot SITL │◄───►│ run_ardupilot.py
│ (Physics) │JSON │ (Flight Ctrl) │MAV │ (Your Logic)
└─────────────────┘ └────────────────┘ └─────────────────┘
┌─────────────┐ JSON (UDP 9002) ┌─────────────┐ MAVLink (TCP 5760) ┌─────────────┐
Gazebo │◄──────────────────────►│ ArduPilot │◄────────────────────────►│ Your
(Physics) sensor data │ SITL │ commands/telemetry │ Controller
└─────────────┘ └─────────────┘ └─────────────┘
```
**Communication:**
- Gazebo ↔ SITL: JSON sensor data over UDP (port 9002)
- SITL ↔ Your Code: MAVLink over TCP (port 5760)
## Quick Start
## World Options
### Step 1: Start Gazebo
```bash
./scripts/run_ardupilot_sim.sh runway # Default (outdoor runway)
./scripts/run_ardupilot_sim.sh warehouse # Indoor warehouse
./scripts/run_ardupilot_sim.sh gimbal # With camera gimbal
./scripts/run_ardupilot_sim.sh zephyr # Fixed-wing aircraft
./scripts/run_ardupilot_sim.sh custom # Your custom world
./scripts/run_ardupilot_sim.sh my_world # gazebo/worlds/my_world.sdf
cd ~/RDC_Simulation
./scripts/run_ardupilot_sim.sh runway
```
## GPS-Denied Mode
Wait until the drone appears in the 3D view.
The simulation runs in GPS-denied mode by default. The controller disables GPS checks and uses visual navigation.
For manual debugging with MAVProxy:
```bash
sim_vehicle.py -v ArduCopter -f gazebo-iris --model JSON --console
# In MAVProxy console:
param set ARMING_CHECK 0
mode stabilize
arm throttle force
rc 3 1500 # Throttle up
```
## Controller Options
### Step 2: Start SITL
```bash
./scripts/run_ardupilot_controller.sh # Auto takeoff
./scripts/run_ardupilot_controller.sh --no-takeoff # Manual control
./scripts/run_ardupilot_controller.sh -a 10 # 10m altitude
```
## Files
| File | Purpose |
|------|---------|
| `scripts/run_ardupilot_sim.sh` | Gazebo launcher with GPU detection |
| `scripts/run_ardupilot_controller.sh` | SITL + Controller launcher |
| `scripts/run_ardupilot.py` | MAVLink interface script |
| `src/drone_controller.py` | Your landing algorithm |
## Environment Variables
The scripts automatically set these, but for manual runs:
```bash
# ArduPilot tools
export PATH=$PATH:~/ardupilot/Tools/autotest
# ArduPilot venv (has empy, pymavlink, etc.)
source ~/venv-ardupilot/bin/activate
sim_vehicle.py -v ArduCopter -f gazebo-iris --model JSON --console
```
# Gazebo plugin paths
export GZ_SIM_SYSTEM_PLUGIN_PATH=~/ardupilot_gazebo/build:$GZ_SIM_SYSTEM_PLUGIN_PATH
export GZ_SIM_RESOURCE_PATH=~/ardupilot_gazebo/models:~/ardupilot_gazebo/worlds:$GZ_SIM_RESOURCE_PATH
You should see telemetry scrolling (NOT "No JSON sensor message").
### Step 3: Run Controller
```bash
source ~/venv-ardupilot/bin/activate
cd ~/RDC_Simulation
python scripts/run_ardupilot.py --pattern square
```
## Flight Patterns
```bash
# Square pattern (5m sides, 5m altitude)
python scripts/run_ardupilot.py --pattern square --size 5 --altitude 5
# Circle pattern (5m radius)
python scripts/run_ardupilot.py --pattern circle --size 5
# Hover in place for testing
python scripts/run_ardupilot.py --pattern hover
# Larger pattern at higher altitude
python scripts/run_ardupilot.py --pattern square --size 10 --altitude 15
```
## Controller Code
The flight controller is in `src/drone_controller.py`:
```python
from src.drone_controller import SimpleDroneController
drone = SimpleDroneController()
drone.connect()
drone.set_mode("GUIDED")
drone.arm()
drone.takeoff(5.0)
drone.fly_square(size=5, altitude=5)
drone.land()
```
### Key Methods
| Method | Description |
|--------|-------------|
| `connect()` | Connect to SITL via MAVLink |
| `set_mode(mode)` | Set flight mode (GUIDED, LAND, etc.) |
| `arm()` | Arm motors (force arm) |
| `takeoff(alt)` | Take off to altitude |
| `goto(x, y, z)` | Go to position (NED frame) |
| `fly_to_and_wait(x, y, alt)` | Go to position and wait |
| `fly_square(size, alt)` | Fly square pattern |
| `fly_circle(radius, alt)` | Fly circle pattern |
| `land()` | Land the drone |
## Available Worlds
```bash
# Outdoor runway (default)
./scripts/run_ardupilot_sim.sh runway
# Indoor warehouse
./scripts/run_ardupilot_sim.sh warehouse
```
## MAVLink Connection
The controller connects via TCP port 5760:
```python
# config.py
MAVLINK = {
"connection_string": "tcp:127.0.0.1:5760",
}
```
## Troubleshooting
### "No JSON sensor message" or "No heartbeat"
### "No JSON sensor message received"
**Cause:** Gazebo isn't running or ArduPilot plugin isn't loaded.
SITL isn't receiving data from Gazebo.
**Fix:**
1. Start Gazebo first (Terminal 1)
2. Wait for world to fully load
3. Then start controller (Terminal 2)
1. Make sure Gazebo is running FIRST
2. Wait for the world to fully load
3. Then start SITL
### "sim_vehicle.py not found"
**Cause:** ArduPilot tools not in PATH.
### Drone won't arm
**Fix:**
```bash
source ~/.ardupilot_env
# or
export PATH=$PATH:~/ardupilot/Tools/autotest
# Check pre-arm status in MAVProxy
arm status
# Force arm
arm throttle force
```
### "empy not found" during waf build
**Cause:** Wrong Python environment.
### Wrong Python environment
**Fix:**
```bash
source ~/venv-ardupilot/bin/activate
pip install empy==3.3.4
cd ~/ardupilot
./waf configure --board sitl
./waf copter
which python3 # Should be ~/venv-ardupilot/bin/python3
```
### Drone doesn't respond to commands
**Possible causes:**
1. Not in GUIDED mode - check MAVProxy: `mode`
2. Not armed - run: `arm throttle force`
3. Pre-arm checks failing - run: `param set ARMING_CHECK 0`
### Drone immediately crashes in Gazebo
**Cause:** Usually a physics/plugin issue.
### Gazebo plugin not loading
**Fix:**
```bash
# Rebuild the ArduPilot Gazebo plugin
cd ~/ardupilot_gazebo/build
cmake .. -DCMAKE_BUILD_TYPE=Release
make -j4
# Check plugin exists
ls ~/ardupilot_gazebo/build/libArduPilotPlugin.so
# Check paths
echo $GZ_SIM_SYSTEM_PLUGIN_PATH
# Should include: ~/ardupilot_gazebo/build
```
### Simulation is laggy
## Manual SITL Control
**Cause:** Software rendering or GPU not detected.
Use MAVProxy commands:
**Check:**
```bash
glxinfo | grep "OpenGL renderer"
# In SITL console
mode GUIDED
arm throttle force
takeoff 5
# Move to position
guided 10 0 -5
# Land
mode LAND
```
Should show your GPU name, not "llvmpipe" or "software".
## Useful Commands
**Fix for WSL:**
- Install NVIDIA drivers for WSL: https://developer.nvidia.com/cuda/wsl
- Ensure WSLg is enabled (Windows 11)
### MAVProxy console not opening
**Cause:** Missing `--console` flag or display issues.
**Fix:**
```bash
# Run directly
# List Gazebo topics
gz topic -l
# Check SITL status
sim_vehicle.py -v ArduCopter -f gazebo-iris --model JSON --console --map
# If display errors:
export DISPLAY=:0 # Linux/WSL with X server
```
## Advanced: Custom Takeoff
To customize the takeoff behavior, edit `scripts/run_ardupilot.py`:
```python
# Change altitude
TAKEOFF_ALTITUDE = 15 # meters
# Change timeout
CONNECTION_TIMEOUT = 60 # seconds
```
## Logs
ArduPilot logs are saved to:
```
~/ardupilot/logs/
~/ardupilot/build/sitl/logs/
```
View with:
```bash
mavlogdump.py --types GPS,ATT ~/ardupilot/logs/LASTLOG.BIN
# View camera (if available)
gz topic -e -t /camera
```