WSL Support

This commit is contained in:
2026-02-09 03:49:39 +00:00
parent e8d83f5638
commit 21b298faa2
4 changed files with 743 additions and 101 deletions

View File

@@ -1,53 +1,196 @@
#!/bin/bash
set -e
# Colors
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
BLUE='\033[0;34m'
NC='\033[0m'
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
PROJECT_DIR="$(dirname "$SCRIPT_DIR")"
echo "=========================================="
echo "UAV-UGV Simulation - Run Script"
echo "GPS-Denied Navigation with Geofencing"
echo "=========================================="
echo -e "${BLUE}==========================================${NC}"
echo -e "${BLUE} UAV-UGV Simulation${NC}"
echo -e "${BLUE} GPS-Denied Navigation with Geofencing${NC}"
echo -e "${BLUE}==========================================${NC}"
echo ""
WORLD="${1:-$PROJECT_DIR/worlds/empty_custom.world}"
if [ ! -f "$WORLD" ]; then
echo "World file not found: $WORLD"
echo "Using default world..."
WORLD="$PROJECT_DIR/worlds/empty_custom.world"
# Detect WSL
IS_WSL=false
if grep -qEi "(microsoft|wsl)" /proc/version 2>/dev/null; then
IS_WSL=true
echo -e "${YELLOW}Running in WSL environment${NC}"
fi
# Detect ROS distro
ROS_DISTRO=""
for distro in humble jazzy iron galactic; do
if [ -d "/opt/ros/$distro" ]; then
ROS_DISTRO="$distro"
break
fi
done
if [ -z "$ROS_DISTRO" ]; then
echo -e "${RED}ERROR: No ROS 2 installation found!${NC}"
echo "Please install ROS 2 first. See docs/setup_guide.md or docs/wsl_setup_guide.md"
exit 1
fi
echo -e "${GREEN}Using ROS 2: $ROS_DISTRO${NC}"
# Parse arguments
WORLD="${1:-$PROJECT_DIR/worlds/empty_custom.world}"
USE_NVIDIA=true
HEADLESS=false
while [[ $# -gt 0 ]]; do
case $1 in
--world)
WORLD="$2"
shift 2
;;
--headless)
HEADLESS=true
shift
;;
--no-gpu)
USE_NVIDIA=false
shift
;;
--software-render)
export LIBGL_ALWAYS_SOFTWARE=1
shift
;;
--help)
echo "Usage: $0 [OPTIONS] [WORLD_FILE]"
echo ""
echo "Options:"
echo " --world FILE Specify world file (default: empty_custom.world)"
echo " --headless Run without GUI"
echo " --no-gpu Disable GPU acceleration"
echo " --software-render Force software rendering (useful in WSL)"
echo " --help Show this help"
echo ""
echo "Examples:"
echo " $0 # Default world"
echo " $0 worlds/indoor_warehouse.world # Indoor environment"
echo " $0 --software-render # For WSL without GPU"
exit 0
;;
*)
if [ -f "$1" ]; then
WORLD="$1"
fi
shift
;;
esac
done
# Validate world file
if [ ! -f "$WORLD" ]; then
echo -e "${YELLOW}World file not found: $WORLD${NC}"
WORLD="$PROJECT_DIR/worlds/empty_custom.world"
echo "Using default: $WORLD"
fi
# Setup environment
export GAZEBO_MODEL_PATH="$PROJECT_DIR/models:$GAZEBO_MODEL_PATH"
export GAZEBO_RESOURCE_PATH="$PROJECT_DIR/worlds:$GAZEBO_RESOURCE_PATH"
if command -v nvidia-smi &> /dev/null; then
echo "NVIDIA GPU detected, enabling acceleration..."
export __NV_PRIME_RENDER_OFFLOAD=1
export __GLX_VENDOR_LIBRARY_NAME=nvidia
# ArduPilot Gazebo paths
if [ -d "$HOME/ardupilot_gazebo" ]; then
export GAZEBO_MODEL_PATH="$HOME/ardupilot_gazebo/models:$GAZEBO_MODEL_PATH"
export GAZEBO_RESOURCE_PATH="$HOME/ardupilot_gazebo/worlds:$GAZEBO_RESOURCE_PATH"
fi
# WSL-specific setup
if $IS_WSL; then
# Source WSL environment if exists
if [ -f "$PROJECT_DIR/wsl_env.sh" ]; then
source "$PROJECT_DIR/wsl_env.sh"
fi
# Check DISPLAY
if [ -z "$DISPLAY" ]; then
# Try to set DISPLAY for WSL
if [ -d "/mnt/wslg" ]; then
export DISPLAY=:0
else
export DISPLAY=$(cat /etc/resolv.conf | grep nameserver | awk '{print $2}'):0
fi
fi
echo -e "${BLUE}DISPLAY set to: $DISPLAY${NC}"
# Performance hint for WSL
if [ -z "$LIBGL_ALWAYS_SOFTWARE" ]; then
echo -e "${YELLOW}TIP: If Gazebo is slow, run with --software-render flag${NC}"
fi
fi
# GPU setup for native Linux
if $USE_NVIDIA && ! $IS_WSL; then
if command -v nvidia-smi &> /dev/null; then
echo -e "${GREEN}NVIDIA GPU detected${NC}"
export __NV_PRIME_RENDER_OFFLOAD=1
export __GLX_VENDOR_LIBRARY_NAME=nvidia
fi
fi
# Cleanup function
cleanup() {
echo ""
echo "Shutting down simulation..."
echo -e "${YELLOW}Shutting down simulation...${NC}"
pkill -f "gazebo" 2>/dev/null || true
pkill -f "gzserver" 2>/dev/null || true
pkill -f "gzclient" 2>/dev/null || true
pkill -f "sim_vehicle.py" 2>/dev/null || true
pkill -f "mavros" 2>/dev/null || true
echo "Cleanup complete."
pkill -f "ArduCopter" 2>/dev/null || true
sleep 1
echo -e "${GREEN}Cleanup complete.${NC}"
}
trap cleanup EXIT
trap cleanup EXIT INT TERM
echo "Starting simulation with world: $WORLD"
echo ""
# Source ROS and workspace
source /opt/ros/${ROS_DISTRO}/setup.bash
if [ -f "$PROJECT_DIR/venv/bin/activate" ]; then
source "$PROJECT_DIR/venv/bin/activate"
fi
source /opt/ros/humble/setup.bash 2>/dev/null || true
if [ -f "$PROJECT_DIR/install/setup.bash" ]; then
if [ -f "$PROJECT_DIR/../install/setup.bash" ]; then
source "$PROJECT_DIR/../install/setup.bash"
elif [ -f "$PROJECT_DIR/install/setup.bash" ]; then
source "$PROJECT_DIR/install/setup.bash"
fi
ros2 launch uav_ugv_simulation full_simulation.launch.py world:="$WORLD"
echo ""
echo -e "${GREEN}Starting simulation with world: $(basename $WORLD)${NC}"
echo ""
# Launch based on ROS distro and Gazebo version
if [ "$ROS_DISTRO" = "jazzy" ]; then
# Jazzy uses Gazebo Harmonic (gz-sim)
echo -e "${YELLOW}Note: Jazzy uses Gazebo Sim (Harmonic). World files may need conversion.${NC}"
ros2 launch uav_ugv_simulation full_simulation.launch.py world:="$WORLD" 2>&1 || {
echo -e "${YELLOW}Launch file failed. Trying direct Gazebo launch...${NC}"
gz sim "$WORLD"
}
else
# Humble and earlier use Gazebo Classic
if $HEADLESS; then
GAZEBO_CMD="gzserver --verbose"
else
GAZEBO_CMD="gazebo --verbose"
fi
ros2 launch uav_ugv_simulation full_simulation.launch.py world:="$WORLD" 2>&1 || {
echo -e "${YELLOW}Launch file failed. Trying direct Gazebo launch...${NC}"
$GAZEBO_CMD "$WORLD"
}
fi

174
scripts/setup_wsl.sh Executable file
View File

@@ -0,0 +1,174 @@
#!/bin/bash
# WSL Quick Setup Script
# This script sets up the simulation environment specifically for WSL2
set -e
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
BLUE='\033[0;34m'
NC='\033[0m'
echo -e "${BLUE}==========================================${NC}"
echo -e "${BLUE} UAV-UGV Simulation - WSL Setup${NC}"
echo -e "${BLUE}==========================================${NC}"
echo ""
# Check if running in WSL
if ! grep -qEi "(microsoft|wsl)" /proc/version 2>/dev/null; then
echo -e "${YELLOW}This script is designed for WSL. Running anyway...${NC}"
fi
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
PROJECT_DIR="$(dirname "$SCRIPT_DIR")"
# Detect Ubuntu version
. /etc/os-release
echo -e "${BLUE}Detected: Ubuntu $VERSION_ID ($VERSION_CODENAME)${NC}"
# Determine ROS distro
case "$VERSION_ID" in
"22.04") ROS_DISTRO="humble" ;;
"24.04") ROS_DISTRO="jazzy" ;;
*) ROS_DISTRO="humble"; echo -e "${YELLOW}Unknown Ubuntu version, defaulting to Humble${NC}" ;;
esac
echo -e "${BLUE}Target ROS 2 distro: $ROS_DISTRO${NC}"
echo ""
# Step 1: Update and install prerequisites
echo -e "${GREEN}[1/6] Installing prerequisites...${NC}"
sudo apt-get update
sudo apt-get install -y \
software-properties-common \
curl \
gnupg \
lsb-release \
x11-apps \
x11-xserver-utils \
dbus-x11 \
mesa-utils \
libgl1-mesa-glx
# Step 2: Add ROS 2 repository
echo -e "${GREEN}[2/6] Adding ROS 2 repository...${NC}"
if [ ! -f /usr/share/keyrings/ros-archive-keyring.gpg ]; then
sudo curl -sSL https://raw.githubusercontent.com/ros/rosdistro/master/ros.key \
-o /usr/share/keyrings/ros-archive-keyring.gpg
fi
if [ ! -f /etc/apt/sources.list.d/ros2.list ]; then
echo "deb [arch=$(dpkg --print-architecture) signed-by=/usr/share/keyrings/ros-archive-keyring.gpg] \
http://packages.ros.org/ros2/ubuntu $(lsb_release -cs) main" | \
sudo tee /etc/apt/sources.list.d/ros2.list > /dev/null
fi
sudo apt-get update
# Step 3: Install ROS 2
echo -e "${GREEN}[3/6] Installing ROS 2 $ROS_DISTRO...${NC}"
if [ ! -d "/opt/ros/$ROS_DISTRO" ]; then
sudo apt-get install -y ros-${ROS_DISTRO}-desktop python3-colcon-common-extensions || {
echo -e "${YELLOW}Desktop install failed, trying base...${NC}"
sudo apt-get install -y ros-${ROS_DISTRO}-ros-base
}
else
echo -e "${BLUE}ROS 2 $ROS_DISTRO already installed${NC}"
fi
# Step 4: Install ROS packages
echo -e "${GREEN}[4/6] Installing ROS 2 packages...${NC}"
sudo apt-get install -y \
ros-${ROS_DISTRO}-mavros \
ros-${ROS_DISTRO}-mavros-extras \
ros-${ROS_DISTRO}-cv-bridge \
ros-${ROS_DISTRO}-image-transport \
ros-${ROS_DISTRO}-tf2-ros 2>/dev/null || {
echo -e "${YELLOW}Some packages unavailable for $ROS_DISTRO${NC}"
}
# Gazebo (different for Humble vs Jazzy)
if [ "$ROS_DISTRO" = "humble" ]; then
sudo apt-get install -y ros-humble-gazebo-ros-pkgs 2>/dev/null || true
elif [ "$ROS_DISTRO" = "jazzy" ]; then
sudo apt-get install -y ros-jazzy-ros-gz 2>/dev/null || true
fi
# GeographicLib datasets
echo -e "${GREEN}[5/6] Installing GeographicLib datasets...${NC}"
GEOGRAPHICLIB_SCRIPT="/opt/ros/${ROS_DISTRO}/lib/mavros/install_geographiclib_datasets.sh"
if [ -f "$GEOGRAPHICLIB_SCRIPT" ] && [ ! -f /usr/share/GeographicLib/geoids/egm96-5.pgm ]; then
sudo "$GEOGRAPHICLIB_SCRIPT" || echo -e "${YELLOW}GeographicLib install failed${NC}"
fi
# Step 5: Setup Python environment
echo -e "${GREEN}[6/6] Setting up Python environment...${NC}"
cd "$PROJECT_DIR"
sudo apt-get install -y python3-pip python3-venv python3-opencv libopencv-dev
if [ ! -d "venv" ]; then
python3 -m venv venv
fi
source venv/bin/activate
pip install --upgrade pip
pip install -r requirements.txt 2>/dev/null || echo -e "${YELLOW}Some pip packages failed${NC}"
# Create WSL environment file
cat > "$PROJECT_DIR/wsl_env.sh" << 'EOF'
#!/bin/bash
# WSL Environment Setup
# DISPLAY configuration
if [ -d "/mnt/wslg" ]; then
# WSLg (Windows 11)
export DISPLAY=:0
else
# X server (Windows 10)
export DISPLAY=$(cat /etc/resolv.conf | grep nameserver | awk '{print $2}'):0
fi
# OpenGL settings for WSL
export LIBGL_ALWAYS_INDIRECT=0
export MESA_GL_VERSION_OVERRIDE=3.3
# Uncomment for software rendering if GPU issues:
# export LIBGL_ALWAYS_SOFTWARE=1
# Gazebo performance
export OGRE_RTT_MODE=Copy
EOF
chmod +x "$PROJECT_DIR/wsl_env.sh"
# Add to bashrc
if ! grep -q "wsl_env.sh" ~/.bashrc 2>/dev/null; then
echo "" >> ~/.bashrc
echo "# UAV-UGV Simulation WSL environment" >> ~/.bashrc
echo "[ -f \"$PROJECT_DIR/wsl_env.sh\" ] && source \"$PROJECT_DIR/wsl_env.sh\"" >> ~/.bashrc
echo "[ -f /opt/ros/${ROS_DISTRO}/setup.bash ] && source /opt/ros/${ROS_DISTRO}/setup.bash" >> ~/.bashrc
fi
# Make scripts executable
chmod +x "$PROJECT_DIR/scripts/"*.sh 2>/dev/null || true
chmod +x "$PROJECT_DIR/setup.sh" 2>/dev/null || true
chmod +x "$PROJECT_DIR/activate_venv.sh" 2>/dev/null || true
echo ""
echo -e "${GREEN}==========================================${NC}"
echo -e "${GREEN} WSL Setup Complete!${NC}"
echo -e "${GREEN}==========================================${NC}"
echo ""
echo -e "${BLUE}Next steps:${NC}"
echo " 1. Close and reopen your terminal (or run: source ~/.bashrc)"
echo " 2. Test GUI: xcalc (should open calculator)"
echo " 3. Test Gazebo: gazebo --verbose"
echo " 4. Run simulation: source activate_venv.sh && bash scripts/run_simulation.sh"
echo ""
echo -e "${YELLOW}WSL Tips:${NC}"
echo " - Windows 11: GUI works out of the box (WSLg)"
echo " - Windows 10: Install VcXsrv and run XLaunch first"
echo " - Slow graphics? Run with: bash scripts/run_simulation.sh --software-render"
echo " - See docs/wsl_setup_guide.md for detailed help"
echo ""