Ardupilot Install Script Fix 2

This commit is contained in:
2026-01-04 00:49:28 +00:00
parent 23c619c4dd
commit 40286fa90c
4 changed files with 170 additions and 260 deletions

View File

@@ -1,22 +1,17 @@
#!/bin/bash
# =============================================================================
# ArduPilot ROS 2 + Gazebo Installation Script
# ArduPilot SITL + Gazebo Installation Script (MAVLink Mode)
# =============================================================================
# Installs the official ArduPilot ROS 2 packages with DDS and Gazebo support.
# Installs ArduPilot SITL with the ardupilot_gazebo plugin.
# This uses MAVLink instead of DDS - simpler and more reliable.
#
# Usage: ./install_ardupilot.sh
#
# This installs:
# 1. Micro-XRCE-DDS-Gen (required for DDS)
# 2. ArduPilot SITL with DDS support
# 3. ardupilot_gz - ArduPilot Gazebo integration
# 4. MAVProxy ground control station
# =============================================================================
set -e
echo "=============================================="
echo " ArduPilot ROS 2 + Gazebo Installation"
echo " ArduPilot SITL + Gazebo Installation"
echo "=============================================="
echo ""
@@ -25,175 +20,111 @@ SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
PROJECT_ROOT="$(dirname "$SCRIPT_DIR")"
# Directories
ARDUPILOT_WS="$HOME/ardu_ws"
ARDUPILOT_HOME="$HOME/ardupilot"
ARDUPILOT_GZ="$HOME/ardupilot_gazebo"
# Check for ROS 2
if [ ! -f "/opt/ros/humble/setup.bash" ] && [ ! -f "/opt/ros/jazzy/setup.bash" ]; then
echo "[ERROR] ROS 2 not found!"
echo "Install with: ./setup/install_ubuntu.sh"
exit 1
fi
# Source ROS 2
if [ -f "/opt/ros/humble/setup.bash" ]; then
source /opt/ros/humble/setup.bash
ROS_DISTRO="humble"
elif [ -f "/opt/ros/jazzy/setup.bash" ]; then
source /opt/ros/jazzy/setup.bash
ROS_DISTRO="jazzy"
else
echo "[WARN] ROS 2 not found, will install in standalone mode"
ROS_DISTRO=""
fi
GZ_VERSION="harmonic"
echo "[INFO] ROS 2: $ROS_DISTRO"
echo "[INFO] Gazebo: $GZ_VERSION"
echo "[INFO] Workspace: $ARDUPILOT_WS"
echo "[INFO] ROS 2: ${ROS_DISTRO:-not installed}"
echo ""
# -----------------------------------------------------------------------------
# Step 1: System Dependencies
# -----------------------------------------------------------------------------
echo "[STEP 1/7] Installing system dependencies..."
echo "[STEP 1/5] Installing system dependencies..."
sudo apt-get update
sudo apt-get install -y \
git cmake build-essential \
python3 python3-pip python3-dev python3-venv \
python3-vcstool python3-rosdep python3-colcon-common-extensions \
wget curl default-jre gradle
python3 python3-pip python3-dev \
wget curl
echo "[OK] System dependencies"
# -----------------------------------------------------------------------------
# Step 2: Install Micro-XRCE-DDS-Gen (required for DDS)
# Step 2: Clone and Setup ArduPilot
# -----------------------------------------------------------------------------
echo ""
echo "[STEP 2/7] Installing Micro-XRCE-DDS-Gen..."
echo "[STEP 2/5] Setting up ArduPilot SITL..."
XRCEDDSGEN_DIR="$HOME/Micro-XRCE-DDS-Gen"
if ! command -v microxrceddsgen &> /dev/null; then
if [ ! -d "$XRCEDDSGEN_DIR" ]; then
git clone --recurse-submodules https://github.com/eProsima/Micro-XRCE-DDS-Gen.git "$XRCEDDSGEN_DIR"
fi
cd "$XRCEDDSGEN_DIR"
./gradlew assemble
# Add to PATH
if ! grep -q "Micro-XRCE-DDS-Gen" ~/.bashrc; then
echo "" >> ~/.bashrc
echo "# Micro-XRCE-DDS-Gen" >> ~/.bashrc
echo "export PATH=\$PATH:$XRCEDDSGEN_DIR/scripts" >> ~/.bashrc
fi
export PATH=$PATH:$XRCEDDSGEN_DIR/scripts
echo "[OK] Micro-XRCE-DDS-Gen installed"
else
echo "[OK] Micro-XRCE-DDS-Gen already installed"
if [ ! -d "$ARDUPILOT_HOME" ]; then
git clone --recurse-submodules https://github.com/ArduPilot/ardupilot.git "$ARDUPILOT_HOME"
fi
cd "$ARDUPILOT_HOME"
# Install ArduPilot prerequisites
Tools/environment_install/install-prereqs-ubuntu.sh -y
. ~/.profile || true
# Build ArduCopter SITL (without DDS to avoid complexity)
./waf configure --board sitl
./waf copter
echo "[OK] ArduPilot SITL built"
# -----------------------------------------------------------------------------
# Step 3: Install Gazebo Harmonic
# -----------------------------------------------------------------------------
echo ""
echo "[STEP 3/7] Installing Gazebo Harmonic..."
echo "[STEP 3/5] Installing Gazebo..."
# Add Gazebo repo
sudo wget https://packages.osrfoundation.org/gazebo.gpg -O /usr/share/keyrings/pkgs-osrf-archive-keyring.gpg 2>/dev/null
echo "deb [arch=$(dpkg --print-architecture) signed-by=/usr/share/keyrings/pkgs-osrf-archive-keyring.gpg] http://packages.osrfoundation.org/gazebo/ubuntu-stable $(lsb_release -cs) main" | sudo tee /etc/apt/sources.list.d/gazebo-stable.list > /dev/null
sudo apt-get update
sudo apt-get install -y gz-harmonic || echo "[WARN] Could not install gz-harmonic"
echo "[OK] Gazebo"
# -----------------------------------------------------------------------------
# Step 4: Create ArduPilot Workspace
# -----------------------------------------------------------------------------
echo ""
echo "[STEP 4/7] Setting up ArduPilot workspace..."
mkdir -p "$ARDUPILOT_WS/src"
cd "$ARDUPILOT_WS"
# Import ArduPilot repos
vcs import --recursive src < <(cat << 'EOF'
repositories:
ardupilot:
type: git
url: https://github.com/ArduPilot/ardupilot.git
version: master
ardupilot_gz:
type: git
url: https://github.com/ArduPilot/ardupilot_gz.git
version: main
EOF
) || true
# Import Gazebo packages
vcs import --input https://raw.githubusercontent.com/ArduPilot/ardupilot_gz/main/ros2_gz.repos --recursive src 2>/dev/null || true
echo "[OK] Repositories imported"
# -----------------------------------------------------------------------------
# Step 5: Install ArduPilot Prerequisites
# -----------------------------------------------------------------------------
echo ""
echo "[STEP 5/7] Installing ArduPilot prerequisites..."
cd "$ARDUPILOT_WS/src/ardupilot"
Tools/environment_install/install-prereqs-ubuntu.sh -y
. ~/.profile || true
echo "[OK] ArduPilot prerequisites"
# -----------------------------------------------------------------------------
# Step 6: Configure rosdep and Build
# -----------------------------------------------------------------------------
echo ""
echo "[STEP 6/7] Building packages..."
# Configure rosdep
if [ ! -f /etc/ros/rosdep/sources.list.d/20-default.list ]; then
sudo rosdep init || true
fi
rosdep update
# Add Gazebo Harmonic sources
sudo wget https://raw.githubusercontent.com/osrf/osrf-rosdep/master/gz/00-gazebo.list \
-O /etc/ros/rosdep/sources.list.d/00-gazebo.list 2>/dev/null || true
rosdep update || true
# Install dependencies
cd "$ARDUPILOT_WS"
rosdep install --from-paths src --ignore-src -y || true
# Build
source /opt/ros/$ROS_DISTRO/setup.bash
export GZ_VERSION=$GZ_VERSION
colcon build --packages-up-to ardupilot_gz_bringup --symlink-install --allow-overriding ros_gz_bridge ros_gz_sim sdformat_urdf || {
echo "[WARN] Full build failed, trying ardupilot_sitl only..."
colcon build --packages-up-to ardupilot_sitl --symlink-install
# Install Gazebo
sudo apt-get install -y gz-harmonic || sudo apt-get install -y gz-garden || {
echo "[WARN] Could not install Gazebo Harmonic/Garden"
}
echo "[OK] Build complete"
echo "[OK] Gazebo installed"
# -----------------------------------------------------------------------------
# Step 7: Configure Environment
# Step 4: Build ArduPilot Gazebo Plugin
# -----------------------------------------------------------------------------
echo ""
echo "[STEP 7/7] Configuring environment..."
echo "[STEP 4/5] Building ArduPilot Gazebo plugin..."
BASHRC_MARKER="# === ArduPilot ROS 2 ==="
if [ ! -d "$ARDUPILOT_GZ" ]; then
git clone https://github.com/ArduPilot/ardupilot_gazebo.git "$ARDUPILOT_GZ"
fi
cd "$ARDUPILOT_GZ"
mkdir -p build && cd build
cmake .. -DCMAKE_BUILD_TYPE=Release
make -j$(nproc)
echo "[OK] ArduPilot Gazebo plugin built"
# -----------------------------------------------------------------------------
# Step 5: Configure Environment
# -----------------------------------------------------------------------------
echo ""
echo "[STEP 5/5] Configuring environment..."
BASHRC_MARKER="# === ArduPilot SITL ==="
if ! grep -q "$BASHRC_MARKER" ~/.bashrc; then
cat >> ~/.bashrc << EOF
$BASHRC_MARKER
export GZ_VERSION=$GZ_VERSION
export PATH=\$PATH:$ARDUPILOT_WS/src/ardupilot/Tools/autotest
export ARDUPILOT_HOME=$ARDUPILOT_HOME
export PATH=\$PATH:$ARDUPILOT_HOME/Tools/autotest
export PATH=\$PATH:\$HOME/.local/bin
source /opt/ros/$ROS_DISTRO/setup.bash
[ -f $ARDUPILOT_WS/install/setup.bash ] && source $ARDUPILOT_WS/install/setup.bash
export GZ_SIM_SYSTEM_PLUGIN_PATH=$ARDUPILOT_GZ/build:\$GZ_SIM_SYSTEM_PLUGIN_PATH
export GZ_SIM_RESOURCE_PATH=$ARDUPILOT_GZ/models:$ARDUPILOT_GZ/worlds:\$GZ_SIM_RESOURCE_PATH
EOF
echo "[OK] Environment configured"
else
@@ -201,20 +132,23 @@ else
fi
# Install MAVProxy
pip3 install --user pymavlink mavproxy || true
pip3 install --user pymavlink mavproxy
# Verify
# -----------------------------------------------------------------------------
# Verification
# -----------------------------------------------------------------------------
echo ""
echo "=============================================="
echo " Verifying Installation"
echo "=============================================="
source "$ARDUPILOT_WS/install/setup.bash" 2>/dev/null || true
source ~/.bashrc 2>/dev/null || true
export PATH=$PATH:$ARDUPILOT_HOME/Tools/autotest:$HOME/.local/bin
ros2 pkg list 2>/dev/null | grep -q "ardupilot_sitl" && echo "[OK] ardupilot_sitl" || echo "[WARN] ardupilot_sitl not found"
ros2 pkg list 2>/dev/null | grep -q "ardupilot_gz_bringup" && echo "[OK] ardupilot_gz_bringup" || echo "[WARN] ardupilot_gz_bringup not found"
command -v microxrceddsgen &> /dev/null && echo "[OK] microxrceddsgen" || echo "[WARN] microxrceddsgen not in PATH"
command -v mavproxy.py &> /dev/null && echo "[OK] MAVProxy" || echo "[WARN] MAVProxy not in PATH (add ~/.local/bin to PATH)"
command -v sim_vehicle.py &> /dev/null && echo "[OK] sim_vehicle.py" || echo "[WARN] sim_vehicle.py not found"
command -v gz &> /dev/null && echo "[OK] Gazebo (gz)" || echo "[WARN] Gazebo not found"
command -v mavproxy.py &> /dev/null && echo "[OK] MAVProxy" || echo "[WARN] MAVProxy not in PATH"
[ -f "$ARDUPILOT_GZ/build/libArduPilotPlugin.so" ] && echo "[OK] ArduPilot Gazebo plugin" || echo "[WARN] Plugin not built"
echo ""
echo "=============================================="
@@ -225,10 +159,12 @@ echo "Run: source ~/.bashrc"
echo ""
echo "Quick Start (2 terminals):"
echo ""
echo "Terminal 1:"
echo " source ~/ardu_ws/install/setup.bash"
echo " ros2 launch ardupilot_gz_bringup iris_runway.launch.py"
echo "Terminal 1 - Start Gazebo:"
echo " gz sim -v4 -r $ARDUPILOT_GZ/worlds/iris_runway.sdf"
echo ""
echo "Terminal 2:"
echo " mavproxy.py --console --map --master=:14550"
echo "Terminal 2 - Start SITL + MAVProxy:"
echo " sim_vehicle.py -v ArduCopter -f gazebo-iris --model JSON --console"
echo ""
echo "Or use sim_vehicle.py standalone (no Gazebo):"
echo " sim_vehicle.py -v ArduCopter --console --map"
echo ""