Files
RDC_Simulation/setup/install_macos.sh
2025-12-31 23:50:26 +00:00

190 lines
6.4 KiB
Bash
Executable File

#!/bin/bash
# =============================================================================
# Drone Simulation - macOS Installation Script
# =============================================================================
# Installs ROS 2 Humble via robostack (conda), PyBullet, and dependencies
# Uses a conda environment for all packages
#
# Usage:
# chmod +x install_macos.sh
# ./install_macos.sh
#
# Tested on: macOS Ventura, Sonoma (Apple Silicon & Intel)
# =============================================================================
set -e # Exit on error
echo "=============================================="
echo " Drone Simulation - macOS Installation"
echo "=============================================="
# Get the script directory and project root
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
PROJECT_ROOT="$(dirname "$SCRIPT_DIR")"
ENV_NAME="drone_simulation"
echo "[INFO] Project root: $PROJECT_ROOT"
# Detect architecture
ARCH=$(uname -m)
echo "[INFO] Detected architecture: $ARCH"
# -----------------------------------------------------------------------------
# Step 1: Install Homebrew (if not present)
# -----------------------------------------------------------------------------
echo ""
echo "[STEP 1/5] Checking Homebrew..."
if ! command -v brew &> /dev/null; then
echo "[INFO] Installing Homebrew..."
/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"
# Add Homebrew to PATH for Apple Silicon
if [[ "$ARCH" == "arm64" ]]; then
echo 'eval "$(/opt/homebrew/bin/brew shellenv)"' >> ~/.zprofile
eval "$(/opt/homebrew/bin/brew shellenv)"
fi
else
echo "[INFO] Homebrew already installed"
fi
# Update Homebrew
brew update
# -----------------------------------------------------------------------------
# Step 2: Install Miniforge (conda)
# -----------------------------------------------------------------------------
echo ""
echo "[STEP 2/5] Installing Miniforge (conda)..."
if ! command -v conda &> /dev/null; then
echo "[INFO] Downloading Miniforge..."
if [[ "$ARCH" == "arm64" ]]; then
curl -L -O "https://github.com/conda-forge/miniforge/releases/latest/download/Miniforge3-MacOSX-arm64.sh"
bash Miniforge3-MacOSX-arm64.sh -b -p $HOME/miniforge3
rm Miniforge3-MacOSX-arm64.sh
else
curl -L -O "https://github.com/conda-forge/miniforge/releases/latest/download/Miniforge3-MacOSX-x86_64.sh"
bash Miniforge3-MacOSX-x86_64.sh -b -p $HOME/miniforge3
rm Miniforge3-MacOSX-x86_64.sh
fi
# Initialize conda
$HOME/miniforge3/bin/conda init zsh bash
# Source conda for this session
source $HOME/miniforge3/etc/profile.d/conda.sh
else
echo "[INFO] Conda already installed"
# Ensure conda is available in this session
source $(conda info --base)/etc/profile.d/conda.sh
fi
# -----------------------------------------------------------------------------
# Step 3: Create conda environment with ROS 2
# -----------------------------------------------------------------------------
echo ""
echo "[STEP 3/5] Creating conda environment with ROS 2..."
# Remove existing environment if present
conda deactivate 2>/dev/null || true
conda env remove -n $ENV_NAME 2>/dev/null || true
# Create new environment
conda create -n $ENV_NAME python=3.11 -y
# Activate environment
conda activate $ENV_NAME
# Add robostack channels
conda config --env --add channels conda-forge
conda config --env --add channels robostack-staging
echo "[INFO] Installing ROS 2 Humble via robostack (this may take a while)..."
conda install ros-humble-desktop ros-humble-geometry-msgs ros-humble-std-msgs -y
# -----------------------------------------------------------------------------
# Step 4: Install Python Dependencies
# -----------------------------------------------------------------------------
echo ""
echo "[STEP 4/5] Installing Python dependencies..."
pip install pybullet pyinstaller
# -----------------------------------------------------------------------------
# Step 5: Create Activation Script
# -----------------------------------------------------------------------------
echo ""
echo "[STEP 5/5] Creating activation script..."
ACTIVATE_SCRIPT="$PROJECT_ROOT/activate.sh"
cat > "$ACTIVATE_SCRIPT" << 'EOF'
#!/bin/bash
# =============================================================================
# Drone Competition - Environment Activation Script (macOS)
# =============================================================================
# This script activates the conda environment with ROS 2.
#
# Usage:
# source activate.sh
# =============================================================================
# Get the directory where this script is located
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
# Initialize conda
if [ -f "$HOME/miniforge3/etc/profile.d/conda.sh" ]; then
source "$HOME/miniforge3/etc/profile.d/conda.sh"
elif [ -f "$(conda info --base)/etc/profile.d/conda.sh" ]; then
source "$(conda info --base)/etc/profile.d/conda.sh"
fi
# Activate conda environment
conda activate drone_competition
echo "[OK] Conda environment 'drone_competition' activated"
echo ""
echo "Environment ready! You can now run:"
echo " python simulation_host.py"
echo " python ros_bridge.py"
echo ""
EOF
chmod +x "$ACTIVATE_SCRIPT"
echo "[INFO] Created activation script: $ACTIVATE_SCRIPT"
# -----------------------------------------------------------------------------
# Verification
# -----------------------------------------------------------------------------
echo ""
echo "=============================================="
echo " Installation Complete!"
echo "=============================================="
echo ""
echo "Verifying installation..."
echo ""
echo -n " ROS 2: "
ros2 --version 2>/dev/null && echo "" || echo "FAILED"
echo -n " PyBullet: "
python3 -c "import pybullet; print('OK')" 2>/dev/null || echo "FAILED"
echo -n " rclpy: "
python3 -c "import rclpy; print('OK')" 2>/dev/null || echo "FAILED"
echo -n " PyInstaller: "
python3 -c "import PyInstaller; print('OK')" 2>/dev/null || echo "FAILED"
echo ""
echo "=============================================="
echo " IMPORTANT: Activate the environment first!"
echo "=============================================="
echo ""
echo "Before running any scripts, activate the environment:"
echo " source $ACTIVATE_SCRIPT"
echo ""
echo "Then run the simulation:"
echo " python simulation_host.py"
echo ""