Gazebo Script Fixes

This commit is contained in:
2026-02-09 04:00:11 +00:00
parent 21b298faa2
commit e70d4a8fcd
3 changed files with 159 additions and 65 deletions

View File

@@ -1,11 +1,61 @@
#!/bin/bash #!/bin/bash
# Activate virtual environment and setup for UAV-UGV Simulation
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
# 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" source "$SCRIPT_DIR/venv/bin/activate"
source /opt/ros/humble/setup.bash fi
if [ -f "$SCRIPT_DIR/install/setup.bash" ]; then
# 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" source "$SCRIPT_DIR/install/setup.bash"
fi fi
export GAZEBO_MODEL_PATH="$SCRIPT_DIR/models:$GAZEBO_MODEL_PATH"
export GAZEBO_RESOURCE_PATH="$SCRIPT_DIR/worlds:$GAZEBO_RESOURCE_PATH" # Gazebo model and resource paths
echo "Environment activated." 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 "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"

View File

@@ -41,9 +41,19 @@ fi
echo -e "${GREEN}Using ROS 2: $ROS_DISTRO${NC}" 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 # Parse arguments
WORLD="${1:-$PROJECT_DIR/worlds/empty_custom.world}" WORLD=""
USE_NVIDIA=true USE_SOFTWARE_RENDER=false
HEADLESS=false HEADLESS=false
while [[ $# -gt 0 ]]; do while [[ $# -gt 0 ]]; do
@@ -56,12 +66,8 @@ while [[ $# -gt 0 ]]; do
HEADLESS=true HEADLESS=true
shift shift
;; ;;
--no-gpu) --no-gpu|--software-render)
USE_NVIDIA=false USE_SOFTWARE_RENDER=true
shift
;;
--software-render)
export LIBGL_ALWAYS_SOFTWARE=1
shift shift
;; ;;
--help) --help)
@@ -69,9 +75,8 @@ while [[ $# -gt 0 ]]; do
echo "" echo ""
echo "Options:" echo "Options:"
echo " --world FILE Specify world file (default: empty_custom.world)" echo " --world FILE Specify world file (default: empty_custom.world)"
echo " --headless Run without GUI" echo " --headless Run without GUI (gzserver only)"
echo " --no-gpu Disable GPU acceleration" echo " --software-render Force software rendering (recommended for WSL)"
echo " --software-render Force software rendering (useful in WSL)"
echo " --help Show this help" echo " --help Show this help"
echo "" echo ""
echo "Examples:" echo "Examples:"
@@ -83,24 +88,34 @@ while [[ $# -gt 0 ]]; do
*) *)
if [ -f "$1" ]; then if [ -f "$1" ]; then
WORLD="$1" 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 fi
shift shift
;; ;;
esac esac
done done
# Validate world file # Default world
if [ ! -f "$WORLD" ]; then if [ -z "$WORLD" ]; then
echo -e "${YELLOW}World file not found: $WORLD${NC}"
WORLD="$PROJECT_DIR/worlds/empty_custom.world" WORLD="$PROJECT_DIR/worlds/empty_custom.world"
echo "Using default: $WORLD"
fi fi
# Setup environment # Validate world file
export GAZEBO_MODEL_PATH="$PROJECT_DIR/models:$GAZEBO_MODEL_PATH" if [ ! -f "$WORLD" ]; then
export GAZEBO_RESOURCE_PATH="$PROJECT_DIR/worlds:$GAZEBO_RESOURCE_PATH" 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 if [ -d "$HOME/ardupilot_gazebo" ]; then
export GAZEBO_MODEL_PATH="$HOME/ardupilot_gazebo/models:$GAZEBO_MODEL_PATH" export GAZEBO_MODEL_PATH="$HOME/ardupilot_gazebo/models:$GAZEBO_MODEL_PATH"
export GAZEBO_RESOURCE_PATH="$HOME/ardupilot_gazebo/worlds:$GAZEBO_RESOURCE_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" source "$PROJECT_DIR/wsl_env.sh"
fi fi
# Check DISPLAY # Set DISPLAY if not set
if [ -z "$DISPLAY" ]; then if [ -z "$DISPLAY" ]; then
# Try to set DISPLAY for WSL
if [ -d "/mnt/wslg" ]; then if [ -d "/mnt/wslg" ]; then
export DISPLAY=:0 export DISPLAY=:0
else else
@@ -123,46 +137,41 @@ if $IS_WSL; then
fi fi
fi fi
echo -e "${BLUE}DISPLAY set to: $DISPLAY${NC}" echo -e "${BLUE}DISPLAY: $DISPLAY${NC}"
# Performance hint for WSL # Force software rendering in WSL by default if having issues
if [ -z "$LIBGL_ALWAYS_SOFTWARE" ]; then if ! $USE_SOFTWARE_RENDER; then
echo -e "${YELLOW}TIP: If Gazebo is slow, run with --software-render flag${NC}" echo -e "${YELLOW}TIP: If Gazebo crashes or has rendering issues, use --software-render${NC}"
fi fi
fi fi
# GPU setup for native Linux # Apply software rendering if requested or in WSL with issues
if $USE_NVIDIA && ! $IS_WSL; then if $USE_SOFTWARE_RENDER; then
if command -v nvidia-smi &> /dev/null; then echo -e "${YELLOW}Using software rendering${NC}"
echo -e "${GREEN}NVIDIA GPU detected${NC}" export LIBGL_ALWAYS_SOFTWARE=1
export __NV_PRIME_RENDER_OFFLOAD=1 export MESA_GL_VERSION_OVERRIDE=3.3
export __GLX_VENDOR_LIBRARY_NAME=nvidia export MESA_GLSL_VERSION_OVERRIDE=330
fi
fi fi
# Cleanup function # Cleanup function
cleanup() { cleanup() {
echo "" echo ""
echo -e "${YELLOW}Shutting down simulation...${NC}" echo -e "${YELLOW}Shutting down simulation...${NC}"
pkill -f "gazebo" 2>/dev/null || true
pkill -f "gzserver" 2>/dev/null || true pkill -f "gzserver" 2>/dev/null || true
pkill -f "gzclient" 2>/dev/null || true pkill -f "gzclient" 2>/dev/null || true
pkill -f "sim_vehicle.py" 2>/dev/null || true pkill -f "gazebo" 2>/dev/null || true
pkill -f "mavros" 2>/dev/null || true
pkill -f "ArduCopter" 2>/dev/null || true
sleep 1 sleep 1
echo -e "${GREEN}Cleanup complete.${NC}" echo -e "${GREEN}Cleanup complete.${NC}"
} }
trap cleanup EXIT INT TERM trap cleanup EXIT INT TERM
# Source ROS and workspace # Activate virtual environment if exists
source /opt/ros/${ROS_DISTRO}/setup.bash
if [ -f "$PROJECT_DIR/venv/bin/activate" ]; then if [ -f "$PROJECT_DIR/venv/bin/activate" ]; then
source "$PROJECT_DIR/venv/bin/activate" source "$PROJECT_DIR/venv/bin/activate"
fi fi
# Source workspace if built
if [ -f "$PROJECT_DIR/../install/setup.bash" ]; then if [ -f "$PROJECT_DIR/../install/setup.bash" ]; then
source "$PROJECT_DIR/../install/setup.bash" source "$PROJECT_DIR/../install/setup.bash"
elif [ -f "$PROJECT_DIR/install/setup.bash" ]; then elif [ -f "$PROJECT_DIR/install/setup.bash" ]; then
@@ -171,26 +180,27 @@ fi
echo "" echo ""
echo -e "${GREEN}Starting simulation with world: $(basename $WORLD)${NC}" echo -e "${GREEN}Starting simulation with world: $(basename $WORLD)${NC}"
echo -e "${BLUE}World path: $WORLD${NC}"
echo "" echo ""
# Launch based on ROS distro and Gazebo version # Check if ROS package exists
if [ "$ROS_DISTRO" = "jazzy" ]; then ROS_PKG_EXISTS=false
# Jazzy uses Gazebo Harmonic (gz-sim) if ros2 pkg list 2>/dev/null | grep -q "uav_ugv_simulation"; then
echo -e "${YELLOW}Note: Jazzy uses Gazebo Sim (Harmonic). World files may need conversion.${NC}" ROS_PKG_EXISTS=true
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 fi
ros2 launch uav_ugv_simulation full_simulation.launch.py world:="$WORLD" 2>&1 || { # Launch simulation
echo -e "${YELLOW}Launch file failed. Trying direct Gazebo launch...${NC}" if $ROS_PKG_EXISTS; then
$GAZEBO_CMD "$WORLD" 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 fi

34
wsl_env.sh Executable file
View File

@@ -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