# Terrain Following/Holding

PX4 supports [Terrain Following](#terrain_following) and [Terrain Hold](#terrain_hold) in [Position](https://px4.gitbook.io/px4-user-guide/flying/flight_modes/position_mc) and [Altitude modes](https://px4.gitbook.io/px4-user-guide/flying/flight_modes/altitude_mc), on *multicopters* and *VTOL vehicles in MC mode* that have a [distance sensor](https://px4.gitbook.io/px4-user-guide/drone_parts/peripherals/rangefinders).

:::note PX4 does not "natively" support terrain following in missions. *QGroundControl* can be used to define missions that *approximately* follow terrain (this just sets waypoint altitudes based on height above terrain, where terrain height at waypoints is obtained from a map database). :::

## Terrain Following

*Terrain following* enables a vehicle to automatically maintain a relatively constant height above ground level when traveling at low altitudes. This is useful for avoiding obstacles and for maintaining constant height when flying over varied terrain (e.g. for aerial photography).

:::tip This feature can be enabled in [Position](https://px4.gitbook.io/px4-user-guide/flying/flight_modes/position_mc) and [Altitude modes](https://px4.gitbook.io/px4-user-guide/flying/flight_modes/altitude_mc), on *multicopters* and *VTOL vehicles in MC mode* that have a [distance sensor](https://px4.gitbook.io/px4-user-guide/drone_parts/peripherals/rangefinders). :::

When *terrain following* is enabled, PX4 uses the output of the EKF estimator to provide the altitude estimate, and the estimated terrain altitude (calculated from distance sensor measurements using another estimator) to provide the altitude setpoint. As the distance to ground changes, the altitude setpoint adjusts to keep the height above ground constant.

At higher altitudes (when the estimator reports that the distance sensor data is invalid) the vehicle switches to *altitude following*, and will typically fly at a near-constant height above mean sea level (AMSL) using an absolute height sensor for altitude data.

:::note More precisely, the vehicle will use the available selected sources of altitude data as defined [here](https://px4.gitbook.io/px4-user-guide/advanced_config/tuning_the_ecl_ekf#height). :::

Terrain following is enabled by setting [MPC\_ALT\_MODE](https://px4.gitbook.io/px4-user-guide/advanced_config/parameter_reference#MPC_ALT_MODE) to `1`.

## Terrain Hold

*Terrain hold* uses a distance sensor to help a vehicle to better maintain a constant height above ground in altitude control modes, when horizontally stationary at low altitude. This allows a vehicle to avoid altitude changes due to barometer drift or excessive barometer interference from rotor wash.

:::note This feature can be enabled in [Position](https://px4.gitbook.io/px4-user-guide/flying/flight_modes/position_mc) and [Altitude modes](https://px4.gitbook.io/px4-user-guide/flying/flight_modes/altitude_mc), on *multicopters* and *VTOL vehicles in MC mode* that have a [distance sensor](https://px4.gitbook.io/px4-user-guide/drone_parts/peripherals/rangefinders). :::

When moving horizontally (`speed >` [MPC\_HOLD\_MAX\_XY](https://px4.gitbook.io/px4-user-guide/advanced_config/parameter_reference#MPC_HOLD_MAX_XY)), or above the altitude where the distance sensor is providing valid data, the vehicle will switch into *altitude following*.

Terrain holding is enabled by setting [MPC\_ALT\_MODE](https://px4.gitbook.io/px4-user-guide/advanced_config/parameter_reference#MPC_ALT_MODE) to `2`.

:::note *Terrain hold* is implemented similarly to [terrain following](#terrain_following). It uses the output of the EKF estimator to provide the altitude estimate, and the estimated terrain altitude (calculated from distance sensor measurements using a separate, single state terrain estimator) to provide the altitude setpoint. If the distance to ground changes due to external forces, the altitude setpoint adjusts to keep the height above ground constant. :::
