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

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