Files
PX4-Autopilot/Tools/setup/macos.sh
T
cb00217286 feat(sih): add optional support for non-flat terrain for MC, FW and VTOL (#29619)
* feat(simulator_sih): add a GeoTIFF terrain elevation model for SITL

SIH only simulates flat ground. This adds the loader for a terrain
elevation model (DEM) in a GeoTIFF file, the base for non-flat terrain
in SITL.

The DEM is read with libtiff and libgeotiff. A DEM in a projected CRS is
resampled once at load time with PROJ onto a WGS84 lat/lon grid, so a
lookup in the simulation loop is a plain bilinear interpolation. It is
built only for px4_sitl and only when the three libraries are found, so
hardware builds and SITL without them are unchanged.

Assisted-by: Claude:claude-opus-5-5
Signed-off-by: Beniamino Pozzan <beniamino.pozzan@auterion.com>

* feat(simulator_sih): use the terrain DEM for ground contact and the rangefinder

With PX4_SIH_TERRAIN set to a GeoTIFF DEM, SIH in SITL places the vehicle
on the terrain at start, lands it on the terrain, ray-casts the downward
distance sensor against it, and publishes it as the groundtruth
terrain_alt. Outside the DEM the ground stays flat at SIH_LOC_H0.

Terrain is only used for multicopters, VTOLs and fixed-wing; other
vehicle types keep flat ground. The DEM is loaded in task_spawn() so a
bad file fails the start command, and because the SIH task stack is too
small for libtiff and PROJ.

The groundtruth terrain_alt now holds the WGS84 terrain altitude, as the
message documents, instead of the height above the origin.

Assisted-by: Claude:claude-opus-5-5
Signed-off-by: Beniamino Pozzan <beniamino.pozzan@auterion.com>

* build(setup): install the SIH terrain dependencies with the simulation tools

Install libtiff, libgeotiff and PROJ so SITL builds SIH with terrain
elevation (DEM) support. They are optional: without them SIH builds as
before.

Assisted-by: Claude:claude-opus-5-5
Signed-off-by: Beniamino Pozzan <beniamino.pozzan@auterion.com>

* docs(sim_sih): document terrain elevation from a GeoTIFF DEM

Assisted-by: Claude:claude-opus-5-5
Signed-off-by: Beniamino Pozzan <beniamino.pozzan@auterion.com>

* docs(sim_sih): fix and clarify the terrain elevation section

Address review findings on the new terrain section: list the sample
types the loader actually reads, note that the prebuilt px4 package and
px4io/px4-sitl container are built without terrain support, and say
when SIH_LOC_H0 still sets the start altitude.

Also add a version badge, link GeoTIFF, EPSG and GDAL, mention the
dist_bottom groundtruth field, and correct the GLO-30 coverage.

Assisted-by: Claude:claude-opus-5-5
Signed-off-by: Hamish Willee <hamishwillee@gmail.com>

* fix: cast to double for MAC builds

Signed-off-by: Beniamino Pozzan <beniamino.pozzan@auterion.com>

* chore: implement TerrainBase interface

Assisted-by: Claude:claude-opus-5-5

Signed-off-by: Beniamino Pozzan <beniamino.pozzan@auterion.com>

* chore: improve isolation of terrain logic from sih core

Signed-off-by: Beniamino Pozzan <beniamino.pozzan@auterion.com>

* refactor(simulator_sih): move DEM handling into the terrain classes

SIH should only use the terrain, not manage it. TerrainFlat becomes the
base class, since flat ground is what every terrain model builds on, and
TerrainDem falls back to it until a DEM is loaded. Terrain.hpp picks the
model at compile time, so SIH holds the terrain by value with no #ifdef,
new/delete or pointer handover, and NuttX builds only get the flat model.

Tracking whether the vehicle is inside the DEM, and the matching
warnings and status, move from SIH into TerrainDem.

The DEM is still loaded in task_spawn() because the SIH task stack is too
small for libtiff and PROJ, so the terrain is a static member that the
instance uses directly instead of moving it.

A terrain file with an unsupported vehicle type now fails the start like
a bad file instead of silently using flat ground.

---------

Signed-off-by: Beniamino Pozzan <beniamino.pozzan@auterion.com>
Signed-off-by: Hamish Willee <hamishwillee@gmail.com>
Co-authored-by: Hamish Willee <hamishwillee@gmail.com>
Co-authored-by: Matthias Grob <maetugr@gmail.com>
2026-11-12 16:49:11 +11:00

206 lines
7.0 KiB
Bash
Executable File

#! /usr/bin/env bash
## Basch script to setup the PX4 development environment on macOS
## Works for Intel and Arm based Apple hardware
##
## Installs:
## - Common dependencies and tools for building PX4
## - Cross compilers for building hardware targets using NuttX
## - With --sim-tools: Gazebo Harmonic simulation stack
##
## --sim-tools installs Gazebo from the locked conda-forge environment in
## macos/pixi.toml, and loads it with the Python venv.
##
## Homebrew 4.5+ no longer auto-resolves cross-tap dependencies, so
## every tap and package is listed explicitly here rather than hidden
## behind meta-formulae. See PX4/homebrew-px4#104 for background.
##
# Abort on the first failing command.
set -e
# script directory
DIR=$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )
# Reinstall if --reinstall set
REINSTALL_FORMULAS=""
# Install simulation tools?
INSTALL_SIM=""
# Parse arguments
for arg in "$@"
do
if [[ $arg == "--reinstall" ]]; then
REINSTALL_FORMULAS=$arg
elif [[ $arg == "--sim-tools" ]]; then
INSTALL_SIM=$arg
fi
done
echo "[macos.sh] Installing the development dependencies for the PX4 Autopilot"
if ! command -v brew &> /dev/null
then
# install Homebrew if not installed yet
echo "[macos.sh] Installing Homebrew"
/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install.sh)"
fi
# discoteq/discoteq used to be the only source of flock (required by the
# NuttX apps archive step), but homebrew/core now carries the identical
# formula (same upstream, same version). Drop the old tap so `flock`
# resolves unambiguously from homebrew/core instead of erroring with
# "installed from the discoteq/discoteq tap but you are trying to install
# it from homebrew/core" on machines that still have it tapped.
if brew tap | grep -q '^discoteq/discoteq$'; then
brew uninstall flock 2>/dev/null || true
brew untap discoteq/discoteq
fi
# Required taps. Homebrew 4.5+ no longer auto-resolves cross-tap
# dependencies, so every tap that a package lives in must be added
# explicitly here before `brew install`.
#
# - osx-cross/arm: arm-gcc-bin@13 (ARM cross-compiler)
# - PX4/px4: fastdds, genromfs, kconfig-frontends (PX4-specific)
#
# Homebrew 6.0+ refuses to load formulae from third-party taps unless they
# are explicitly trusted ("Refusing to load formula ... from untrusted tap"),
# and recent versions validate every formula of a tap while tapping it. An
# untrusted tap therefore fails with "Cannot tap ...: invalid syntax in tap!",
# so the taps must be trusted *before* they are tapped. `brew trust` works on
# a tap that is not installed yet. Without the taps, `brew install` aborts
# on the first PX4/px4 formula before pouring any package (including ccache).
# `brew trust` only exists on Homebrew 6.0+; guard it so older versions,
# which don't gate untrusted taps, skip it silently.
if brew trust --help &> /dev/null; then
brew trust osx-cross/arm
brew trust PX4/px4
fi
brew tap osx-cross/arm
brew tap PX4/px4
# Package list. This replaces the px4-dev meta-formula, which is kept
# as a deprecated no-op upstream. See PX4/homebrew-px4 for history.
PX4_BREW_PACKAGES=(
astyle
bash-completion
ccache
cmake
fastdds
genromfs
kconfig-frontends
ncurses
ninja
osx-cross/arm/arm-gcc-bin@13
flock
python
python-tk
)
if [[ $REINSTALL_FORMULAS == "--reinstall" ]]; then
echo "[macos.sh] Re-installing PX4 toolchain dependencies"
brew doctor || true # warnings are informational here
brew reinstall "${PX4_BREW_PACKAGES[@]}"
else
echo "[macos.sh] Installing PX4 toolchain dependencies"
brew install "${PX4_BREW_PACKAGES[@]}"
fi
brew link --overwrite --force arm-gcc-bin@13
# Python dependencies
echo "[macos.sh] Installing Python3 dependencies"
# Resolve to git repo root based on script location (handles submodules and subdirectory invocation)
ROOT_DIR="$(git -C "$DIR" rev-parse --show-toplevel 2>/dev/null || echo "$DIR")"
VENV_DIR="$ROOT_DIR/.venv"
# Create virtual environment if it doesn't exist
if [ ! -d "$VENV_DIR" ]; then
echo "[macos.sh] Creating Python virtual environment at $VENV_DIR"
python3 -m venv "$VENV_DIR"
fi
# We need to have future to install pymavlink later.
"$VENV_DIR/bin/pip" install future
"$VENV_DIR/bin/pip" install -r "${DIR}/requirements.txt"
"$VENV_DIR/bin/pip" install -r "${DIR}/test-requirements.txt"
# Optional, but recommended additional simulation tools:
if [[ $INSTALL_SIM == "--sim-tools" ]]; then
# Gazebo and everything the gz modules link against (OpenCV for
# PX4-OpticalFlow, GStreamer for the camera plugin) come from the
# locked conda-forge environment in macos/pixi.toml instead of
# Homebrew. conda-forge never removes or rebuilds a published
# package, so the lock keeps installing the same gz, protobuf and
# abseil binaries. See macos/pixi.toml.
PX4_SIM_BREW_PACKAGES=(
exiftool
glog
graphviz
libgeotiff # SIH terrain elevation (GeoTIFF DEM), pulls libtiff and proj
pixi
)
if [[ $REINSTALL_FORMULAS == "--reinstall" ]]; then
echo "[macos.sh] Re-installing PX4 simulation dependencies"
brew reinstall "${PX4_SIM_BREW_PACKAGES[@]}"
else
echo "[macos.sh] Installing PX4 simulation dependencies"
brew install "${PX4_SIM_BREW_PACKAGES[@]}"
fi
PIXI_MANIFEST="${DIR}/macos/pixi.toml"
echo "[macos.sh] Installing Gazebo from ${PIXI_MANIFEST}"
pixi install --locked --manifest-path "$PIXI_MANIFEST"
# Load the Gazebo environment with the venv, so developers keep a
# single activation step. This is written by hand rather than taken
# from `pixi shell-hook`, which exports the PATH of the shell running
# this script instead of prepending to the user's, and sources every
# package's bash completions. The activate.d scripts set the paths
# the gz CLI (ruby gems) and renderer (OGRE) need. CMake needs the
# prefix spelled out: the gz config files are found through PATH, but
# the find_path(zmq.hpp) in gz-cmake's FindCPPZMQ is not. The env
# ships its own python, so the venv's bin goes back in front of it
# afterwards.
GZ_ENV_SCRIPT="$VENV_DIR/bin/px4-gz-env.sh"
cat > "$GZ_ENV_SCRIPT" <<-EOF
export CONDA_PREFIX="${DIR}/macos/.pixi/envs/default"
export PATH="\$CONDA_PREFIX/bin:\$PATH"
export CMAKE_PREFIX_PATH="\$CONDA_PREFIX\${CMAKE_PREFIX_PATH:+:\$CMAKE_PREFIX_PATH}"
for f in "\$CONDA_PREFIX"/etc/conda/activate.d/*.sh; do
. "\$f"
done
unset f
EOF
GZ_ENV_MARKER="# px4: load the Gazebo environment"
if ! grep -qF "$GZ_ENV_MARKER" "$VENV_DIR/bin/activate"; then
cat >> "$VENV_DIR/bin/activate" <<-EOF
$GZ_ENV_MARKER
. "\$VIRTUAL_ENV/bin/px4-gz-env.sh"
PATH="\$VIRTUAL_ENV/bin:\$PATH"
export PATH
EOF
fi
# XQuartz is required for Gazebo GUI display on macOS.
if ! brew list --cask xquartz &> /dev/null; then
echo "[macos.sh] Installing XQuartz (required for Gazebo display)"
brew install --cask xquartz
fi
fi
echo ""
echo "[macos.sh] All set! The PX4 Autopilot toolchain was installed."
echo ""
echo "Python dependencies were installed into a virtual environment at:"
echo " $VENV_DIR"
echo ""
echo "Activate it before building (run in each new terminal session):"
echo " source $VENV_DIR/bin/activate"
echo ""