Update Install Scripts

This commit is contained in:
2026-02-09 04:20:22 +00:00
parent e70d4a8fcd
commit 5c0d3e2fb4
10 changed files with 1072 additions and 836 deletions

552
setup.sh
View File

@@ -1,17 +1,21 @@
#!/bin/bash
# UAV-UGV Simulation - Complete Installation Script
# Installs everything needed for GPS-denied navigation simulation
# Compatible with Ubuntu 22.04/24.04 and WSL2
set -e
# Colors for output
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
BLUE='\033[0;34m'
NC='\033[0m' # No Color
CYAN='\033[0;36m'
NC='\033[0m'
print_header() {
echo ""
echo -e "${BLUE}==========================================${NC}"
echo -e "${BLUE} UAV-UGV Simulation Setup${NC}"
echo -e "${BLUE} GPS-Denied Navigation with Geofencing${NC}"
echo -e "${BLUE} $1${NC}"
echo -e "${BLUE}==========================================${NC}"
echo ""
}
@@ -20,6 +24,10 @@ print_step() {
echo -e "${GREEN}[$1/$TOTAL_STEPS] $2${NC}"
}
print_info() {
echo -e "${CYAN}INFO: $1${NC}"
}
print_warning() {
echo -e "${YELLOW}WARNING: $1${NC}"
}
@@ -28,21 +36,14 @@ print_error() {
echo -e "${RED}ERROR: $1${NC}"
}
print_info() {
echo -e "${BLUE}INFO: $1${NC}"
}
# Detect environment
detect_environment() {
IS_WSL=false
IS_WSL2=false
UBUNTU_VERSION=""
ROS_DISTRO=""
# Detect WSL
if grep -qEi "(microsoft|wsl)" /proc/version 2>/dev/null; then
IS_WSL=true
if grep -qi "wsl2" /proc/version 2>/dev/null || [ -f /run/WSL ]; then
if [ -f /run/WSL ] || grep -qi "wsl2" /proc/version 2>/dev/null; then
IS_WSL2=true
fi
fi
@@ -54,330 +55,319 @@ detect_environment() {
UBUNTU_CODENAME="$VERSION_CODENAME"
fi
# Determine ROS distro based on Ubuntu version
# Determine ROS distro
case "$UBUNTU_VERSION" in
"22.04")
ROS_DISTRO="humble"
;;
"24.04")
ROS_DISTRO="jazzy"
;;
"20.04")
ROS_DISTRO="galactic"
;;
*)
ROS_DISTRO="humble" # Default fallback
;;
"22.04") ROS_DISTRO="humble" ;;
"24.04") ROS_DISTRO="jazzy" ;;
"20.04") ROS_DISTRO="galactic" ;;
*) ROS_DISTRO="humble" ;;
esac
echo -e "${BLUE}Detected Environment:${NC}"
echo " - Ubuntu: $UBUNTU_VERSION ($UBUNTU_CODENAME)"
echo " - WSL: $IS_WSL"
echo " - WSL2: $IS_WSL2"
echo " - ROS 2 Distro: $ROS_DISTRO"
echo ""
}
# Check if ROS 2 is installed
check_ros2_installed() {
if [ -d "/opt/ros/$ROS_DISTRO" ]; then
return 0
else
return 1
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
cd "$SCRIPT_DIR"
print_header "UAV-UGV Simulation - Complete Setup"
echo "GPS-Denied Navigation with Geofencing"
echo ""
detect_environment
echo -e "${CYAN}Detected Environment:${NC}"
echo " Ubuntu: $UBUNTU_VERSION ($UBUNTU_CODENAME)"
echo " WSL: $IS_WSL | WSL2: $IS_WSL2"
echo " ROS 2 Target: $ROS_DISTRO"
echo ""
TOTAL_STEPS=10
STEP=1
# ============================================================================
# STEP 1: System Update
# ============================================================================
print_step $STEP "Updating system packages"
sudo apt-get update
sudo apt-get upgrade -y
((STEP++))
# ============================================================================
# STEP 2: Install Base Dependencies
# ============================================================================
print_step $STEP "Installing base dependencies"
sudo apt-get install -y \
curl \
gnupg \
lsb-release \
software-properties-common \
wget \
git \
gitk \
build-essential \
cmake \
python3-dev \
python3-pip \
python3-venv \
python3-opencv \
python3-matplotlib \
python3-lxml \
python3-yaml \
python3-scipy \
python3-future \
libopencv-dev \
libxml2-dev \
libxslt1-dev \
ccache \
gawk \
libtool-bin
# WSL-specific packages
if $IS_WSL; then
print_info "Installing WSL GUI support packages"
sudo apt-get install -y \
x11-apps \
x11-xserver-utils \
dbus-x11 \
mesa-utils \
libgl1-mesa-glx
fi
((STEP++))
# ============================================================================
# STEP 3: Install ROS 2
# ============================================================================
print_step $STEP "Installing ROS 2 $ROS_DISTRO"
if [ ! -d "/opt/ros/$ROS_DISTRO" ]; then
# Add ROS 2 repository
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
}
# Install ROS 2 repository
install_ros2_repo() {
print_info "Setting up ROS 2 repository..."
sudo apt-get install -y software-properties-common curl gnupg lsb-release
# Add ROS 2 GPG key
sudo curl -sSL https://raw.githubusercontent.com/ros/rosdistro/master/ros.key \
-o /usr/share/keyrings/ros-archive-keyring.gpg
# Add repository
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
sudo apt-get update
}
# Setup WSL-specific configurations
setup_wsl() {
print_info "Configuring WSL-specific settings..."
# Create WSL environment setup
WSL_ENV_FILE="$SCRIPT_DIR/wsl_env.sh"
cat > "$WSL_ENV_FILE" << 'WSLEOF'
#!/bin/bash
# WSL-specific environment variables
# Detect WSL version and set DISPLAY
if grep -qi "wsl2" /proc/version 2>/dev/null || [ -f /run/WSL ]; then
# WSL2 with WSLg (Windows 11)
if [ -d "/mnt/wslg" ]; then
export DISPLAY=:0
else
# WSL2 without WSLg (Windows 10) - use X server
export DISPLAY=$(cat /etc/resolv.conf | grep nameserver | awk '{print $2}'):0
fi
sudo apt-get install -y ros-${ROS_DISTRO}-desktop python3-colcon-common-extensions
else
# WSL1
export DISPLAY=localhost:0
print_info "ROS 2 $ROS_DISTRO already installed"
fi
# Performance settings for Gazebo in WSL
export LIBGL_ALWAYS_INDIRECT=0
# If GPU acceleration isn't working, uncomment this:
# export LIBGL_ALWAYS_SOFTWARE=1
# Mesa driver settings (helps with some rendering issues)
export MESA_GL_VERSION_OVERRIDE=3.3
export MESA_GLSL_VERSION_OVERRIDE=330
# Gazebo specific
export OGRE_RTT_MODE=Copy
WSLEOF
chmod +x "$WSL_ENV_FILE"
# Add to bashrc if not already present
if ! grep -q "wsl_env.sh" ~/.bashrc 2>/dev/null; then
echo "" >> ~/.bashrc
echo "# WSL environment for UAV-UGV simulation" >> ~/.bashrc
echo "if [ -f \"$WSL_ENV_FILE\" ]; then source \"$WSL_ENV_FILE\"; fi" >> ~/.bashrc
fi
print_info "WSL environment configured. Source ~/.bashrc to apply."
}
# Setup X11 for GUI applications
setup_x11_wsl() {
print_info "Installing X11 utilities for GUI support..."
sudo apt-get install -y \
x11-apps \
x11-xserver-utils \
dbus-x11 \
libgl1-mesa-glx \
mesa-utils 2>/dev/null || true
}
# Main setup
print_header
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
cd "$SCRIPT_DIR"
# Detect environment
detect_environment
# Determine total steps based on what's needed
if check_ros2_installed; then
TOTAL_STEPS=7
else
TOTAL_STEPS=9
print_warning "ROS 2 $ROS_DISTRO not found. Will attempt to install."
fi
if $IS_WSL; then
TOTAL_STEPS=$((TOTAL_STEPS + 1))
fi
STEP=1
# Step: Update system
print_step $STEP "Updating system packages..."
sudo apt-get update
((STEP++))
# Step: WSL-specific setup
if $IS_WSL; then
print_step $STEP "Setting up WSL environment..."
setup_wsl
setup_x11_wsl
((STEP++))
fi
# Step: Install ROS 2 if not present
if ! check_ros2_installed; then
print_step $STEP "Installing ROS 2 repository..."
install_ros2_repo
((STEP++))
print_step $STEP "Installing ROS 2 $ROS_DISTRO..."
sudo apt-get install -y ros-${ROS_DISTRO}-desktop || {
print_warning "Failed to install ros-${ROS_DISTRO}-desktop, trying base..."
sudo apt-get install -y ros-${ROS_DISTRO}-ros-base
}
((STEP++))
fi
# Step: Install system dependencies
print_step $STEP "Installing system dependencies..."
# ============================================================================
# STEP 4: Install ROS 2 Packages
# ============================================================================
print_step $STEP "Installing ROS 2 packages"
sudo apt-get install -y \
python3-pip \
python3-venv \
python3-opencv \
libopencv-dev \
python3-colcon-common-extensions \
build-essential \
cmake \
git || true
((STEP++))
ros-${ROS_DISTRO}-mavros \
ros-${ROS_DISTRO}-mavros-extras \
ros-${ROS_DISTRO}-cv-bridge \
ros-${ROS_DISTRO}-image-transport \
ros-${ROS_DISTRO}-tf2 \
ros-${ROS_DISTRO}-tf2-ros \
ros-${ROS_DISTRO}-tf2-geometry-msgs \
ros-${ROS_DISTRO}-gazebo-ros-pkgs 2>/dev/null || \
print_warning "Some ROS packages not available for $ROS_DISTRO"
# Step: Install ROS 2 packages
print_step $STEP "Installing ROS 2 packages..."
ROS_PACKAGES=(
"ros-${ROS_DISTRO}-mavros"
"ros-${ROS_DISTRO}-mavros-extras"
"ros-${ROS_DISTRO}-cv-bridge"
"ros-${ROS_DISTRO}-image-transport"
"ros-${ROS_DISTRO}-tf2"
"ros-${ROS_DISTRO}-tf2-ros"
"ros-${ROS_DISTRO}-tf2-geometry-msgs"
)
# Gazebo packages differ by distro
if [ "$ROS_DISTRO" = "humble" ]; then
ROS_PACKAGES+=("ros-${ROS_DISTRO}-gazebo-ros-pkgs")
elif [ "$ROS_DISTRO" = "jazzy" ]; then
# Jazzy uses Gazebo Harmonic (gz-sim)
ROS_PACKAGES+=("ros-${ROS_DISTRO}-ros-gz")
fi
# Install available packages (some may not exist for all distros)
for pkg in "${ROS_PACKAGES[@]}"; do
sudo apt-get install -y "$pkg" 2>/dev/null || {
print_warning "Package $pkg not available, skipping..."
}
done
((STEP++))
# Step: Install MAVROS GeographicLib datasets
print_step $STEP "Installing MAVROS GeographicLib datasets..."
# Install GeographicLib datasets for MAVROS
GEOGRAPHICLIB_SCRIPT="/opt/ros/${ROS_DISTRO}/lib/mavros/install_geographiclib_datasets.sh"
if [ -f "$GEOGRAPHICLIB_SCRIPT" ]; then
if [ ! -f /usr/share/GeographicLib/geoids/egm96-5.pgm ]; then
sudo "$GEOGRAPHICLIB_SCRIPT" || print_warning "GeographicLib datasets installation failed"
else
print_info "GeographicLib datasets already installed"
print_info "Installing GeographicLib datasets (this may take a minute)"
sudo "$GEOGRAPHICLIB_SCRIPT" || print_warning "GeographicLib installation failed"
fi
else
print_warning "MAVROS not installed, skipping GeographicLib datasets"
fi
((STEP++))
# Step: Create Python virtual environment
print_step $STEP "Creating Python virtual environment..."
# ============================================================================
# STEP 5: Install Gazebo
# ============================================================================
print_step $STEP "Installing Gazebo"
sudo apt-get install -y gazebo libgazebo-dev || \
sudo apt-get install -y gazebo11 libgazebo11-dev || \
print_warning "Gazebo installation may require manual setup"
((STEP++))
# ============================================================================
# STEP 6: Install ArduPilot SITL
# ============================================================================
print_step $STEP "Installing ArduPilot SITL"
ARDUPILOT_HOME="$HOME/ardupilot"
if [ ! -d "$ARDUPILOT_HOME" ]; then
print_info "Cloning ArduPilot repository..."
cd "$HOME"
git clone --recurse-submodules https://github.com/ArduPilot/ardupilot.git
cd ardupilot
git submodule update --init --recursive
else
print_info "ArduPilot already exists, updating..."
cd "$ARDUPILOT_HOME"
git fetch origin
git submodule update --init --recursive
fi
# Install ArduPilot prerequisites
print_info "Installing ArduPilot prerequisites..."
cd "$ARDUPILOT_HOME"
USER_NONINTERACTIVE=1 Tools/environment_install/install-prereqs-ubuntu.sh -y || true
# Reload profile
. ~/.profile 2>/dev/null || true
# Build ArduPilot SITL
print_info "Building ArduPilot SITL (this may take several minutes)..."
cd "$ARDUPILOT_HOME"
./waf configure --board sitl
./waf copter
./waf rover
((STEP++))
# ============================================================================
# STEP 7: Install ardupilot_gazebo Plugin
# ============================================================================
print_step $STEP "Installing ardupilot_gazebo plugin"
ARDUPILOT_GAZEBO_HOME="$HOME/ardupilot_gazebo"
if [ ! -d "$ARDUPILOT_GAZEBO_HOME" ]; then
print_info "Cloning ardupilot_gazebo..."
cd "$HOME"
git clone https://github.com/ArduPilot/ardupilot_gazebo.git
else
print_info "ardupilot_gazebo already exists, updating..."
cd "$ARDUPILOT_GAZEBO_HOME"
git pull origin main || true
fi
cd "$ARDUPILOT_GAZEBO_HOME"
mkdir -p build && cd build
cmake ..
make -j$(nproc)
sudo make install
((STEP++))
# ============================================================================
# STEP 8: Setup Python Virtual Environment
# ============================================================================
print_step $STEP "Setting up Python environment"
cd "$SCRIPT_DIR"
if [ ! -d "venv" ]; then
python3 -m venv venv
fi
source venv/bin/activate
# Upgrade pip and install dependencies
pip install --upgrade pip
pip install -r requirements.txt || {
print_warning "Some Python packages failed to install. Check requirements.txt"
}
pip install -r requirements.txt || print_warning "Some Python packages failed"
deactivate
((STEP++))
# Step: Build ROS 2 package
print_step $STEP "Building ROS 2 package..."
if [ -f "/opt/ros/${ROS_DISTRO}/setup.bash" ]; then
source /opt/ros/${ROS_DISTRO}/setup.bash
# Try to build if in a ROS workspace
if [ -d "$SCRIPT_DIR/../src" ] || [ -f "$SCRIPT_DIR/package.xml" ]; then
cd "$SCRIPT_DIR/.."
colcon build --packages-select uav_ugv_simulation --symlink-install 2>/dev/null || {
print_warning "Colcon build skipped. To build manually:"
print_info " cd ~/ros2_ws && colcon build --packages-select uav_ugv_simulation"
}
cd "$SCRIPT_DIR"
fi
# ============================================================================
# STEP 9: Configure Environment
# ============================================================================
print_step $STEP "Configuring environment"
# Create WSL environment file
if $IS_WSL; then
cat > "$SCRIPT_DIR/wsl_env.sh" << 'WSLEOF'
#!/bin/bash
# WSL Environment for UAV-UGV Simulation
if [ -d "/mnt/wslg" ]; then
export DISPLAY=:0
else
print_warning "ROS 2 not found, skipping package build"
export DISPLAY=$(cat /etc/resolv.conf 2>/dev/null | grep nameserver | awk '{print $2}'):0
fi
((STEP++))
# Step: Make scripts executable and create activation script
print_step $STEP "Finalizing setup..."
chmod +x scripts/*.sh 2>/dev/null || true
export LIBGL_ALWAYS_INDIRECT=0
export MESA_GL_VERSION_OVERRIDE=3.3
export MESA_GLSL_VERSION_OVERRIDE=330
export OGRE_RTT_MODE=Copy
# Source Gazebo setup
[ -f /usr/share/gazebo/setup.bash ] && source /usr/share/gazebo/setup.bash
[ -f /usr/share/gazebo-11/setup.bash ] && source /usr/share/gazebo-11/setup.bash
WSLEOF
chmod +x "$SCRIPT_DIR/wsl_env.sh"
fi
# Create activation script
cat > activate_venv.sh << EOF
cat > "$SCRIPT_DIR/activate_venv.sh" << EOF
#!/bin/bash
SCRIPT_DIR="\$(cd "\$(dirname "\${BASH_SOURCE[0]}")" && pwd)"
# Activate Python virtual environment
# ROS 2
source /opt/ros/${ROS_DISTRO}/setup.bash
# Gazebo
[ -f /usr/share/gazebo/setup.bash ] && source /usr/share/gazebo/setup.bash
[ -f /usr/share/gazebo-11/setup.bash ] && source /usr/share/gazebo-11/setup.bash
# Python venv
source "\$SCRIPT_DIR/venv/bin/activate"
# Source ROS 2
if [ -f "/opt/ros/${ROS_DISTRO}/setup.bash" ]; then
source /opt/ros/${ROS_DISTRO}/setup.bash
fi
# Source workspace if built
if [ -f "\$SCRIPT_DIR/../install/setup.bash" ]; then
source "\$SCRIPT_DIR/../install/setup.bash"
elif [ -f "\$SCRIPT_DIR/install/setup.bash" ]; then
source "\$SCRIPT_DIR/install/setup.bash"
fi
# Gazebo paths
export GAZEBO_MODEL_PATH="\$SCRIPT_DIR/models:\$GAZEBO_MODEL_PATH"
export GAZEBO_RESOURCE_PATH="\$SCRIPT_DIR/worlds:\$GAZEBO_RESOURCE_PATH"
export GAZEBO_MODEL_PATH="\$SCRIPT_DIR/models:\$HOME/ardupilot_gazebo/models:\${GAZEBO_MODEL_PATH:-}"
export GAZEBO_RESOURCE_PATH="\$SCRIPT_DIR/worlds:\$HOME/ardupilot_gazebo/worlds:\${GAZEBO_RESOURCE_PATH:-}"
# ArduPilot Gazebo (if installed)
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
# ArduPilot
export PATH="\$PATH:\$HOME/ardupilot/Tools/autotest"
export ARDUPILOT_HOME="\$HOME/ardupilot"
# WSL environment (if applicable)
if [ -f "\$SCRIPT_DIR/wsl_env.sh" ]; then
source "\$SCRIPT_DIR/wsl_env.sh"
# WSL
if grep -qEi "(microsoft|wsl)" /proc/version 2>/dev/null; then
[ -f "\$SCRIPT_DIR/wsl_env.sh" ] && source "\$SCRIPT_DIR/wsl_env.sh"
fi
echo -e "\033[0;32mEnvironment activated (ROS 2 ${ROS_DISTRO})\033[0m"
echo "Run: bash scripts/run_simulation.sh"
EOF
chmod +x activate_venv.sh
# Summary
echo ""
echo -e "${GREEN}==========================================${NC}"
echo -e "${GREEN} Setup Complete!${NC}"
echo -e "${GREEN}==========================================${NC}"
echo "Run simulation: bash scripts/run_simulation.sh"
echo "With rendering: bash scripts/run_simulation.sh --software-render"
EOF
chmod +x "$SCRIPT_DIR/activate_venv.sh"
((STEP++))
# ============================================================================
# STEP 10: Make Scripts Executable
# ============================================================================
print_step $STEP "Finalizing installation"
chmod +x "$SCRIPT_DIR/scripts/"*.sh 2>/dev/null || true
chmod +x "$SCRIPT_DIR/activate_venv.sh" 2>/dev/null || true
((STEP++))
# ============================================================================
# COMPLETE
# ============================================================================
print_header "Installation Complete!"
echo -e "${GREEN}All components installed:${NC}"
echo " - ROS 2 $ROS_DISTRO"
echo " - Gazebo"
echo " - ArduPilot SITL ($ARDUPILOT_HOME)"
echo " - ardupilot_gazebo ($ARDUPILOT_GAZEBO_HOME)"
echo " - MAVROS"
echo " - Python dependencies"
echo ""
if $IS_WSL; then
echo -e "${YELLOW}WSL Setup Notes:${NC}"
echo " - WSL environment file created: wsl_env.sh"
echo " - For GUI apps, ensure X server is running (Windows 11 has WSLg built-in)"
echo " - See docs/wsl_setup_guide.md for detailed instructions"
echo " - GUI apps require WSLg (Windows 11) or VcXsrv (Windows 10)"
echo " - Use --software-render flag if graphics are slow"
echo ""
fi
echo -e "${BLUE}Next steps:${NC}"
echo " 1. source ~/.bashrc"
echo " 2. source activate_venv.sh"
echo " 3. bash scripts/run_simulation.sh"
echo -e "${CYAN}To run the simulation:${NC}"
echo ""
echo -e "${BLUE}GPS-Denied Navigation:${NC}"
echo " - All navigation uses LOCAL coordinates"
echo " - GPS is ONLY used for geofencing"
echo " cd $SCRIPT_DIR"
echo " source activate_venv.sh"
echo " bash scripts/run_simulation.sh"
if $IS_WSL; then
echo ""
if ! check_ros2_installed && [ ! -f "/opt/ros/${ROS_DISTRO}/setup.bash" ]; then
echo -e "${YELLOW}IMPORTANT:${NC}"
echo " ROS 2 installation may have failed. Please install manually:"
echo " See: https://docs.ros.org/en/${ROS_DISTRO}/Installation.html"
echo ""
echo " # Or with software rendering for WSL:"
echo " bash scripts/run_simulation.sh --software-render"
fi
echo ""
echo -e "${GREEN}==========================================${NC}"