diff --git a/README.md b/README.md index c641b6d..75062e3 100644 --- a/README.md +++ b/README.md @@ -6,18 +6,20 @@ ArduPilot + ROS 2 + Gazebo (ARG) simulation for landing on a moving platform. **Terminal 1 - Gazebo:** ```bash +source activate.sh ./scripts/run_ardupilot_sim.sh runway ``` **Terminal 2 - Controller:** ```bash +source activate.sh ./scripts/run_ardupilot_controller.sh ``` ## World Options ```bash -./scripts/run_ardupilot_sim.sh runway # Default +./scripts/run_ardupilot_sim.sh runway # Default (outdoor) ./scripts/run_ardupilot_sim.sh warehouse # Indoor ./scripts/run_ardupilot_sim.sh custom # Custom landing pad ./scripts/run_ardupilot_sim.sh my_world # gazebo/worlds/my_world.sdf @@ -25,12 +27,32 @@ ArduPilot + ROS 2 + Gazebo (ARG) simulation for landing on a moving platform. ## Installation +### Ubuntu (Native or WSL2) + ```bash ./setup/install_ubuntu.sh ./setup/install_ardupilot.sh source ~/.bashrc ``` +### Windows (via WSL2) + +```powershell +# PowerShell as Administrator +wsl --install -d Ubuntu-24.04 +# Restart, then in Ubuntu terminal: +./setup/install_ubuntu.sh --with-ardupilot +``` + +### macOS + +```bash +./setup/install_macos.sh +python standalone_simulation.py # Gazebo not supported +``` + +See [Installation Guide](docs/installation.md) for detailed platform instructions. + ## Project Structure ``` @@ -56,11 +78,44 @@ simulation/ | COMMAND | Send commands to rover | | LAND | Land on rover | +## Environment Setup + +Always activate the environment before running: + +```bash +source activate.sh +``` + +This sets up: +- ROS 2 environment +- ArduPilot venv (with empy, pymavlink, etc.) +- Gazebo resource paths +- ArduPilot tools path + +## Troubleshooting + +**sim_vehicle.py not found:** +```bash +source ~/.ardupilot_env +``` + +**empy not found:** +```bash +source ~/venv-ardupilot/bin/activate +pip install empy==3.3.4 +``` + +**Gazebo slow (software rendering):** +```bash +glxinfo | grep "OpenGL renderer" +# Should show GPU, not "llvmpipe" +``` + ## Documentation -- [Installation](docs/installation.md) -- [ArduPilot Guide](docs/ardupilot.md) -- [Drone Controller](docs/drone_guide.md) -- [Custom Worlds](docs/gazebo_worlds.md) -- [Blender Models](docs/blender_models.md) -- [Architecture](docs/architecture.md) \ No newline at end of file +- [Installation](docs/installation.md) - Full setup for all platforms + WSL +- [ArduPilot Guide](docs/ardupilot.md) - SITL setup and troubleshooting +- [Drone Controller](docs/drone_guide.md) - Landing algorithm +- [Custom Worlds](docs/gazebo_worlds.md) - Create Gazebo environments +- [Blender Models](docs/blender_models.md) - 3D model workflow +- [Architecture](docs/architecture.md) - System design \ No newline at end of file diff --git a/docs/ardupilot.md b/docs/ardupilot.md index 6195fd0..8925578 100644 --- a/docs/ardupilot.md +++ b/docs/ardupilot.md @@ -1,13 +1,23 @@ # ArduPilot + Gazebo Simulation +## Prerequisites + +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:** +**Terminal 1 - Start Gazebo:** ```bash ./scripts/run_ardupilot_sim.sh runway ``` -**Terminal 2:** +**Terminal 2 - Start Controller:** ```bash ./scripts/run_ardupilot_controller.sh ``` @@ -21,33 +31,41 @@ └─────────────────┘ └─────────────────┘ └─────────────────┘ ``` +**Communication:** +- Gazebo ↔ SITL: JSON sensor data over UDP (port 9002) +- SITL ↔ Your Code: MAVLink over TCP (port 5760) + ## World Options ```bash -./scripts/run_ardupilot_sim.sh runway # Default -./scripts/run_ardupilot_sim.sh warehouse # Indoor -./scripts/run_ardupilot_sim.sh zephyr # Fixed-wing +./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 ``` ## GPS-Denied Mode -The simulation runs in GPS-denied mode by default. +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: +# In MAVProxy console: param set ARMING_CHECK 0 mode stabilize arm throttle force +rc 3 1500 # Throttle up ``` ## Controller Options ```bash ./scripts/run_ardupilot_controller.sh # Auto takeoff -./scripts/run_ardupilot_controller.sh --no-takeoff # Manual +./scripts/run_ardupilot_controller.sh --no-takeoff # Manual control ./scripts/run_ardupilot_controller.sh -a 10 # 10m altitude ``` @@ -55,18 +73,130 @@ arm throttle force | File | Purpose | |------|---------| -| `scripts/run_ardupilot_sim.sh` | Gazebo + GPU detection | -| `scripts/run_ardupilot_controller.sh` | SITL + Controller | -| `scripts/run_ardupilot.py` | MAVLink interface | -| `src/drone_controller.py` | Your algorithm | +| `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 + +# 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 +``` ## Troubleshooting -### "No JSON sensor message" -Start Gazebo (Terminal 1) before the controller. +### "No JSON sensor message" or "No heartbeat" -### Drone doesn't respond -Check mode is GUIDED in MAVProxy. +**Cause:** Gazebo isn't running or ArduPilot plugin isn't loaded. -### Simulation laggy -Check GPU: `glxinfo | grep "OpenGL renderer"` +**Fix:** +1. Start Gazebo first (Terminal 1) +2. Wait for world to fully load +3. Then start controller (Terminal 2) + +### "sim_vehicle.py not found" + +**Cause:** ArduPilot tools not in PATH. + +**Fix:** +```bash +source ~/.ardupilot_env +# or +export PATH=$PATH:~/ardupilot/Tools/autotest +``` + +### "empy not found" during waf build + +**Cause:** Wrong Python environment. + +**Fix:** +```bash +source ~/venv-ardupilot/bin/activate +pip install empy==3.3.4 +cd ~/ardupilot +./waf configure --board sitl +./waf copter +``` + +### 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. + +**Fix:** +```bash +# Rebuild the ArduPilot Gazebo plugin +cd ~/ardupilot_gazebo/build +cmake .. -DCMAKE_BUILD_TYPE=Release +make -j4 +``` + +### Simulation is laggy + +**Cause:** Software rendering or GPU not detected. + +**Check:** +```bash +glxinfo | grep "OpenGL renderer" +``` + +Should show your GPU name, not "llvmpipe" or "software". + +**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 +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 +``` diff --git a/docs/installation.md b/docs/installation.md index d617ae9..7ce282f 100644 --- a/docs/installation.md +++ b/docs/installation.md @@ -1,6 +1,6 @@ # Installation -## Quick Install +## Quick Install (Ubuntu/WSL) ```bash ./setup/install_ubuntu.sh @@ -13,11 +13,101 @@ source ~/.bashrc | Component | Location | |-----------|----------| | ArduPilot SITL | `~/ardupilot` | +| ArduPilot venv | `~/venv-ardupilot` | | ardupilot_gazebo | `~/ardupilot_gazebo` | | Gazebo Harmonic | System | | ROS 2 | System | | MAVProxy | `~/.local/bin` | +## Platform-Specific Installation + +### Ubuntu (Native) + +```bash +./setup/install_ubuntu.sh --with-ardupilot +``` + +### Windows (via WSL2) + +WSL2 (Windows Subsystem for Linux) is the recommended way to run this simulation on Windows. + +#### Step 1: Install WSL2 + +Open PowerShell as Administrator: +```powershell +wsl --install -d Ubuntu-24.04 +``` + +Restart your computer when prompted. + +#### Step 2: Configure WSL2 for GUI Apps + +WSL2 on Windows 11 has built-in GUI support (WSLg). For Windows 10, you may need an X server: + +```powershell +# Windows 11 - WSLg is automatic, no extra setup needed + +# Windows 10 - Install VcXsrv or X410 from Microsoft Store +``` + +#### Step 3: Install in WSL + +Open Ubuntu from Start menu: +```bash +# Clone the project (or copy from Windows) +git clone https://github.com/your-repo/RDC_Simulation.git +cd RDC_Simulation + +# Run installation +./setup/install_ubuntu.sh +./setup/install_ardupilot.sh +source ~/.bashrc +``` + +#### Step 4: GPU Acceleration in WSL + +For best performance with Gazebo: + +```bash +# Check GPU availability +glxinfo | grep "OpenGL renderer" + +# Should show your GPU, not "llvmpipe" +# If using NVIDIA, install NVIDIA GPU driver for WSL: +# https://developer.nvidia.com/cuda/wsl +``` + +#### WSL Tips + +- **Access Windows files:** `/mnt/c/Users/YourName/` +- **Open file explorer:** `explorer.exe .` +- **Copy to clipboard:** `cat file | clip.exe` +- **Memory limit:** Create `%UserProfile%\.wslconfig`: + ```ini + [wsl2] + memory=8GB + processors=4 + ``` + +### macOS + +Gazebo is not officially supported on macOS. Use standalone mode: + +```bash +./setup/install_macos.sh +python standalone_simulation.py +``` + +For full simulation, use a Linux VM (UTM for Apple Silicon) or Docker. + +### Arch Linux + +```bash +./setup/install_arch.sh +``` + +ROS 2 and Gazebo are available via AUR - see script output for details. + ## Dependencies ```bash @@ -34,29 +124,107 @@ pip install -r requirements.txt ## Verify Installation ```bash +# Source the environment +source activate.sh + +# Check ArduPilot tools sim_vehicle.py --help +which mavproxy.py + +# Check Gazebo gz sim --help ``` +## Environment Setup + +After installation, always source the environment before running: + +```bash +source activate.sh +# or +source ~/.bashrc # if ArduPilot was installed +``` + +The activation script sets up: +- ROS 2 environment +- ArduPilot Python venv (with empy, pymavlink, etc.) +- Gazebo resource paths +- ArduPilot tools path + ## Troubleshooting -### sim_vehicle.py not found +### "empy not found" or waf configure fails + +The ArduPilot venv isn't activated. Run: ```bash +source ~/venv-ardupilot/bin/activate +pip install empy==3.3.4 +``` + +### sim_vehicle.py not found + +```bash +source ~/.ardupilot_env +# or export PATH=$PATH:~/ardupilot/Tools/autotest ``` ### mavproxy.py not found + ```bash export PATH=$PATH:~/.local/bin ``` ### pexpect error + ```bash pip install pexpect ``` -### Gazebo slow +### Gazebo slow / Software rendering + ```bash glxinfo | grep "OpenGL renderer" ``` -Should show GPU, not "llvmpipe". + +Should show your GPU, not "llvmpipe". Install proper GPU drivers: + +- **NVIDIA:** `sudo apt install nvidia-driver-XXX` +- **AMD:** `sudo apt install mesa-vulkan-drivers` +- **Intel:** Usually works out of box +- **WSL:** Install NVIDIA WSL driver from nvidia.com + +### Wrong Python environment + +If you see errors about missing packages that should be installed: + +```bash +# Check which Python is being used +which python3 + +# Should be: ~/venv-ardupilot/bin/python3 +# If not, activate the correct venv: +source ~/venv-ardupilot/bin/activate +``` + +### WSL: Display not working + +```bash +# Check DISPLAY variable +echo $DISPLAY + +# For WSLg (Windows 11), should be auto-set +# For Windows 10 with X server: +export DISPLAY=:0 +``` + +### WSL: Out of memory + +Create `%UserProfile%\.wslconfig`: +```ini +[wsl2] +memory=8GB +swap=4GB +``` + +Then restart WSL: `wsl --shutdown` diff --git a/setup/install_arch.sh b/setup/install_arch.sh index e4c5336..ef2db3b 100755 --- a/setup/install_arch.sh +++ b/setup/install_arch.sh @@ -123,8 +123,18 @@ else echo "[INFO] ROS 2 not found - standalone mode available" fi -# Activate Python venv -if [ -f "$SCRIPT_DIR/venv/bin/activate" ]; then +# Source ArduPilot environment (if installed) +if [ -f "$HOME/.ardupilot_env" ]; then + source "$HOME/.ardupilot_env" + echo "[OK] ArduPilot environment" +fi + +# Activate ArduPilot venv (has empy and other dependencies) +if [ -f "$HOME/venv-ardupilot/bin/activate" ]; then + source "$HOME/venv-ardupilot/bin/activate" + echo "[OK] ArduPilot venv" +# Fallback to project venv +elif [ -f "$SCRIPT_DIR/venv/bin/activate" ]; then source "$SCRIPT_DIR/venv/bin/activate" echo "[OK] Python venv activated" fi @@ -132,6 +142,16 @@ fi # Set Gazebo model path if available export GZ_SIM_RESOURCE_PATH="$SCRIPT_DIR/gazebo/models:$GZ_SIM_RESOURCE_PATH" +# Add paths for ArduPilot tools +export PATH="$PATH:$HOME/.local/bin" +export PATH="$PATH:$HOME/ardupilot/Tools/autotest" + +# ArduPilot Gazebo plugin +if [ -d "$HOME/ardupilot_gazebo/build" ]; then + export GZ_SIM_SYSTEM_PLUGIN_PATH="$HOME/ardupilot_gazebo/build:$GZ_SIM_SYSTEM_PLUGIN_PATH" + export GZ_SIM_RESOURCE_PATH="$HOME/ardupilot_gazebo/models:$HOME/ardupilot_gazebo/worlds:$GZ_SIM_RESOURCE_PATH" +fi + echo "" echo "Environment ready! Run:" echo " python standalone_simulation.py" diff --git a/setup/install_ubuntu.sh b/setup/install_ubuntu.sh index ac4df57..32e286c 100755 --- a/setup/install_ubuntu.sh +++ b/setup/install_ubuntu.sh @@ -177,8 +177,18 @@ elif [ -f "/opt/ros/humble/setup.bash" ]; then echo "[OK] ROS 2 humble" fi -# Activate Python venv -if [ -f "$SCRIPT_DIR/venv/bin/activate" ]; then +# Source ArduPilot environment (if installed) +if [ -f "$HOME/.ardupilot_env" ]; then + source "$HOME/.ardupilot_env" + echo "[OK] ArduPilot environment" +fi + +# Activate ArduPilot venv (has empy and other dependencies) +if [ -f "$HOME/venv-ardupilot/bin/activate" ]; then + source "$HOME/venv-ardupilot/bin/activate" + echo "[OK] ArduPilot venv" +# Fallback to project venv +elif [ -f "$SCRIPT_DIR/venv/bin/activate" ]; then source "$SCRIPT_DIR/venv/bin/activate" echo "[OK] Python venv" fi @@ -186,13 +196,14 @@ fi # Set Gazebo paths export GZ_SIM_RESOURCE_PATH="$SCRIPT_DIR/gazebo/models:$GZ_SIM_RESOURCE_PATH" -# Add ~/.local/bin to PATH (for mavproxy.py) +# Add paths for ArduPilot tools and MAVProxy export PATH="$PATH:$HOME/.local/bin" +export PATH="$PATH:$HOME/ardupilot/Tools/autotest" -# ArduPilot workspace -if [ -f "$HOME/ardu_ws/install/setup.bash" ]; then - source "$HOME/ardu_ws/install/setup.bash" - echo "[OK] ArduPilot workspace" +# ArduPilot Gazebo plugin +if [ -d "$HOME/ardupilot_gazebo/build" ]; then + export GZ_SIM_SYSTEM_PLUGIN_PATH="$HOME/ardupilot_gazebo/build:$GZ_SIM_SYSTEM_PLUGIN_PATH" + export GZ_SIM_RESOURCE_PATH="$HOME/ardupilot_gazebo/models:$HOME/ardupilot_gazebo/worlds:$GZ_SIM_RESOURCE_PATH" fi echo "" diff --git a/setup/install_windows.ps1 b/setup/install_windows.ps1 index 753502e..e5f42f9 100644 --- a/setup/install_windows.ps1 +++ b/setup/install_windows.ps1 @@ -221,12 +221,34 @@ Write-Host "" Write-Host "With moving rover:" -ForegroundColor Yellow Write-Host " python standalone_simulation.py --pattern circular --speed 0.3" -ForegroundColor White Write-Host "" -Write-Host "==============================================" -ForegroundColor Cyan -Write-Host " Want ROS 2 + Gazebo? Use WSL2" -ForegroundColor Cyan -Write-Host "==============================================" -ForegroundColor Cyan +Write-Host "======================================================" -ForegroundColor Cyan +Write-Host " Want ROS 2 + Gazebo + ArduPilot? Use WSL2" -ForegroundColor Cyan +Write-Host "======================================================" -ForegroundColor Cyan Write-Host "" -Write-Host "For the full experience with ROS 2 and Gazebo:" -ForegroundColor Gray -Write-Host " 1. Install WSL2: wsl --install -d Ubuntu-22.04" -ForegroundColor White -Write-Host " 2. Open Ubuntu and run: ./setup/install_ubuntu.sh" -ForegroundColor White -Write-Host " 3. See docs/installation.md for details" -ForegroundColor White +Write-Host "For the full simulation with Gazebo and ArduPilot:" -ForegroundColor Gray +Write-Host "" +Write-Host "Step 1: Install WSL2" -ForegroundColor Yellow +Write-Host " wsl --install -d Ubuntu-24.04" -ForegroundColor White +Write-Host " # Restart your computer when prompted" -ForegroundColor DarkGray +Write-Host "" +Write-Host "Step 2: Open Ubuntu and install" -ForegroundColor Yellow +Write-Host " cd /mnt/c/path/to/RDC_Simulation" -ForegroundColor White +Write-Host " ./setup/install_ubuntu.sh --with-ardupilot" -ForegroundColor White +Write-Host " source ~/.bashrc" -ForegroundColor White +Write-Host "" +Write-Host "Step 3: Run simulation (2 terminals)" -ForegroundColor Yellow +Write-Host " Terminal 1: ./scripts/run_ardupilot_sim.sh runway" -ForegroundColor White +Write-Host " Terminal 2: ./scripts/run_ardupilot_controller.sh" -ForegroundColor White +Write-Host "" +Write-Host "GPU Acceleration in WSL:" -ForegroundColor Yellow +Write-Host " - Windows 11: WSLg auto-enabled (no setup needed)" -ForegroundColor DarkGray +Write-Host " - NVIDIA GPU: Install WSL driver from nvidia.com/cuda/wsl" -ForegroundColor DarkGray +Write-Host " - Check: glxinfo | grep 'OpenGL renderer'" -ForegroundColor DarkGray +Write-Host "" +Write-Host "WSL Tips:" -ForegroundColor Yellow +Write-Host " - Access Windows files: /mnt/c/Users/YourName/" -ForegroundColor DarkGray +Write-Host " - Open folder in Explorer: explorer.exe ." -ForegroundColor DarkGray +Write-Host " - Increase memory: Create %UserProfile%\.wslconfig" -ForegroundColor DarkGray +Write-Host "" +Write-Host "See docs/installation.md for detailed instructions." -ForegroundColor Gray Write-Host ""