From e70d4a8fcdde668889bc38fb8d2f6303dc29981b Mon Sep 17 00:00:00 2001 From: default Date: Mon, 9 Feb 2026 04:00:11 +0000 Subject: [PATCH] Gazebo Script Fixes --- activate_venv.sh | 62 ++++++++++++++++-- scripts/run_simulation.sh | 128 ++++++++++++++++++++------------------ wsl_env.sh | 34 ++++++++++ 3 files changed, 159 insertions(+), 65 deletions(-) create mode 100755 wsl_env.sh diff --git a/activate_venv.sh b/activate_venv.sh index d331027..fd754d2 100755 --- a/activate_venv.sh +++ b/activate_venv.sh @@ -1,11 +1,61 @@ #!/bin/bash +# Activate virtual environment and setup for UAV-UGV Simulation + SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" -source "$SCRIPT_DIR/venv/bin/activate" -source /opt/ros/humble/setup.bash -if [ -f "$SCRIPT_DIR/install/setup.bash" ]; then + +# 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 + +# Source ROS 2 +if [ -n "$ROS_DISTRO" ] && [ -f "/opt/ros/${ROS_DISTRO}/setup.bash" ]; then + source /opt/ros/${ROS_DISTRO}/setup.bash +fi + +# Source Gazebo setup (critical for shader/resource paths) +if [ -f /usr/share/gazebo/setup.bash ]; then + source /usr/share/gazebo/setup.bash +elif [ -f /usr/share/gazebo-11/setup.bash ]; then + source /usr/share/gazebo-11/setup.bash +fi + +# Activate Python virtual environment +if [ -f "$SCRIPT_DIR/venv/bin/activate" ]; then + source "$SCRIPT_DIR/venv/bin/activate" +fi + +# Source ROS 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 -export GAZEBO_MODEL_PATH="$SCRIPT_DIR/models:$GAZEBO_MODEL_PATH" -export GAZEBO_RESOURCE_PATH="$SCRIPT_DIR/worlds:$GAZEBO_RESOURCE_PATH" -echo "Environment activated." + +# Gazebo model and resource paths +export GAZEBO_MODEL_PATH="$SCRIPT_DIR/models:${GAZEBO_MODEL_PATH:-}" +export GAZEBO_RESOURCE_PATH="$SCRIPT_DIR/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 + +# WSL environment (if applicable) +if grep -qEi "(microsoft|wsl)" /proc/version 2>/dev/null; then + if [ -f "$SCRIPT_DIR/wsl_env.sh" ]; then + source "$SCRIPT_DIR/wsl_env.sh" + fi +fi + +echo -e "\033[0;32mEnvironment activated (ROS 2 ${ROS_DISTRO:-not found})\033[0m" echo "Run: bash scripts/run_simulation.sh" +echo "" +echo "Options:" +echo " bash scripts/run_simulation.sh # Default world" +echo " bash scripts/run_simulation.sh --software-render # For WSL/rendering issues" diff --git a/scripts/run_simulation.sh b/scripts/run_simulation.sh index e167978..c93743b 100755 --- a/scripts/run_simulation.sh +++ b/scripts/run_simulation.sh @@ -41,9 +41,19 @@ fi echo -e "${GREEN}Using ROS 2: $ROS_DISTRO${NC}" +# Source ROS 2 +source /opt/ros/${ROS_DISTRO}/setup.bash + +# Source Gazebo setup (CRITICAL for shader paths) +if [ -f /usr/share/gazebo/setup.bash ]; then + source /usr/share/gazebo/setup.bash +elif [ -f /usr/share/gazebo-11/setup.bash ]; then + source /usr/share/gazebo-11/setup.bash +fi + # Parse arguments -WORLD="${1:-$PROJECT_DIR/worlds/empty_custom.world}" -USE_NVIDIA=true +WORLD="" +USE_SOFTWARE_RENDER=false HEADLESS=false while [[ $# -gt 0 ]]; do @@ -56,12 +66,8 @@ while [[ $# -gt 0 ]]; do HEADLESS=true shift ;; - --no-gpu) - USE_NVIDIA=false - shift - ;; - --software-render) - export LIBGL_ALWAYS_SOFTWARE=1 + --no-gpu|--software-render) + USE_SOFTWARE_RENDER=true shift ;; --help) @@ -69,9 +75,8 @@ while [[ $# -gt 0 ]]; do 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 " --headless Run without GUI (gzserver only)" + echo " --software-render Force software rendering (recommended for WSL)" echo " --help Show this help" echo "" echo "Examples:" @@ -83,24 +88,34 @@ while [[ $# -gt 0 ]]; do *) if [ -f "$1" ]; then WORLD="$1" + elif [ -f "$PROJECT_DIR/$1" ]; then + WORLD="$PROJECT_DIR/$1" + elif [ -f "$PROJECT_DIR/worlds/$1" ]; then + WORLD="$PROJECT_DIR/worlds/$1" fi shift ;; esac done -# Validate world file -if [ ! -f "$WORLD" ]; then - echo -e "${YELLOW}World file not found: $WORLD${NC}" +# Default world +if [ -z "$WORLD" ]; then 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" +# Validate world file +if [ ! -f "$WORLD" ]; then + echo -e "${RED}ERROR: World file not found: $WORLD${NC}" + echo "Available worlds:" + ls -1 "$PROJECT_DIR/worlds/"*.world 2>/dev/null || echo " (none found)" + exit 1 +fi -# ArduPilot Gazebo paths +# Setup Gazebo paths +export GAZEBO_MODEL_PATH="$PROJECT_DIR/models:${GAZEBO_MODEL_PATH:-}" +export GAZEBO_RESOURCE_PATH="$PROJECT_DIR/worlds:${GAZEBO_RESOURCE_PATH:-}" + +# ArduPilot Gazebo paths (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" @@ -113,9 +128,8 @@ if $IS_WSL; then source "$PROJECT_DIR/wsl_env.sh" fi - # Check DISPLAY + # Set DISPLAY if not set if [ -z "$DISPLAY" ]; then - # Try to set DISPLAY for WSL if [ -d "/mnt/wslg" ]; then export DISPLAY=:0 else @@ -123,46 +137,41 @@ if $IS_WSL; then fi fi - echo -e "${BLUE}DISPLAY set to: $DISPLAY${NC}" + echo -e "${BLUE}DISPLAY: $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}" + # Force software rendering in WSL by default if having issues + if ! $USE_SOFTWARE_RENDER; then + echo -e "${YELLOW}TIP: If Gazebo crashes or has rendering issues, use --software-render${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 +# Apply software rendering if requested or in WSL with issues +if $USE_SOFTWARE_RENDER; then + echo -e "${YELLOW}Using software rendering${NC}" + export LIBGL_ALWAYS_SOFTWARE=1 + export MESA_GL_VERSION_OVERRIDE=3.3 + export MESA_GLSL_VERSION_OVERRIDE=330 fi # Cleanup function cleanup() { echo "" 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 - pkill -f "ArduCopter" 2>/dev/null || true + pkill -f "gazebo" 2>/dev/null || true sleep 1 echo -e "${GREEN}Cleanup complete.${NC}" } trap cleanup EXIT INT TERM -# Source ROS and workspace -source /opt/ros/${ROS_DISTRO}/setup.bash - +# Activate virtual environment if exists if [ -f "$PROJECT_DIR/venv/bin/activate" ]; then source "$PROJECT_DIR/venv/bin/activate" fi +# Source workspace if built if [ -f "$PROJECT_DIR/../install/setup.bash" ]; then source "$PROJECT_DIR/../install/setup.bash" elif [ -f "$PROJECT_DIR/install/setup.bash" ]; then @@ -171,26 +180,27 @@ fi echo "" echo -e "${GREEN}Starting simulation with world: $(basename $WORLD)${NC}" +echo -e "${BLUE}World path: $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" - } +# Check if ROS package exists +ROS_PKG_EXISTS=false +if ros2 pkg list 2>/dev/null | grep -q "uav_ugv_simulation"; then + ROS_PKG_EXISTS=true +fi + +# Launch simulation +if $ROS_PKG_EXISTS; then + echo -e "${GREEN}Launching via ROS 2...${NC}" + ros2 launch uav_ugv_simulation full_simulation.launch.py world:="$WORLD" +else + echo -e "${YELLOW}ROS package not built. Launching Gazebo directly...${NC}" + echo -e "${BLUE}To build the package: cd ~/ros2_ws && colcon build --packages-select uav_ugv_simulation${NC}" + echo "" + + if $HEADLESS; then + gzserver --verbose "$WORLD" + else + gazebo --verbose "$WORLD" + fi fi diff --git a/wsl_env.sh b/wsl_env.sh new file mode 100755 index 0000000..0710f96 --- /dev/null +++ b/wsl_env.sh @@ -0,0 +1,34 @@ +#!/bin/bash +# WSL Environment Setup for UAV-UGV Simulation +# This file is sourced automatically when running the simulation in WSL + +# DISPLAY configuration +if [ -d "/mnt/wslg" ]; then + # WSLg (Windows 11) - native Wayland/X support + export DISPLAY=:0 + export WAYLAND_DISPLAY="${WAYLAND_DISPLAY:-wayland-0}" +else + # X server (Windows 10) - connect to VcXsrv or similar + export DISPLAY=$(cat /etc/resolv.conf 2>/dev/null | grep nameserver | awk '{print $2}'):0 +fi + +# OpenGL settings for better compatibility +export LIBGL_ALWAYS_INDIRECT=0 + +# Mesa driver overrides (helps with version requirements) +export MESA_GL_VERSION_OVERRIDE=3.3 +export MESA_GLSL_VERSION_OVERRIDE=330 + +# Gazebo specific settings +export OGRE_RTT_MODE=Copy + +# Source Gazebo setup for proper resource paths +if [ -f /usr/share/gazebo/setup.bash ]; then + source /usr/share/gazebo/setup.bash +elif [ -f /usr/share/gazebo-11/setup.bash ]; then + source /usr/share/gazebo-11/setup.bash +fi + +# Uncomment the following line to force software rendering +# This is more stable but slower - use if you see rendering errors +# export LIBGL_ALWAYS_SOFTWARE=1