diff --git a/src/lib/FlightTasks/tasks/FlightTaskAutoLine.cpp b/src/lib/FlightTasks/tasks/FlightTaskAutoLine.cpp index 5d898e125b..0ac04337ff 100644 --- a/src/lib/FlightTasks/tasks/FlightTaskAutoLine.cpp +++ b/src/lib/FlightTasks/tasks/FlightTaskAutoLine.cpp @@ -55,21 +55,11 @@ bool FlightTaskAutoLine::update() // always reset constraints because they might change depending on the type _setDefaultConstraints(); + _updateAltitudeAboveGround(); + bool follow_line = _type == WaypointType::loiter || _type == WaypointType::position; bool follow_line_prev = _type_previous == WaypointType::loiter || _type_previous == WaypointType::position; - // Altitude above ground is by default just the negation of the current local position in D-direction. - _alt_above_ground = -_position(2); - - if (PX4_ISFINITE(_dist_to_bottom)) { - // We have a valid distance to ground measurement - _alt_above_ground = _dist_to_bottom; - - } else if (_sub_home_position->get().valid_alt) { - // if home position is set, then altitude above ground is relative to the home position - _alt_above_ground = -_position(2) + _sub_home_position->get().z; - } - // 1st time that vehicle starts to follow line. Reset all setpoints to current vehicle state. if (follow_line && !follow_line_prev) { _reset(); @@ -521,6 +511,21 @@ float FlightTaskAutoLine::_getVelocityFromAngle(const float angle) return math::constrain(speed_close, min_cruise_speed, _mc_cruise_speed); } +void FlightTaskAutoLine::_updateAltitudeAboveGround() +{ + // Altitude above ground is by default just the negation of the current local position in D-direction. + _alt_above_ground = -_position(2); + + if (PX4_ISFINITE(_dist_to_bottom)) { + // We have a valid distance to ground measurement + _alt_above_ground = _dist_to_bottom; + + } else if (_sub_home_position->get().valid_alt) { + // if home position is set, then altitude above ground is relative to the home position + _alt_above_ground = -_position(2) + _sub_home_position->get().z; + } +} + void FlightTaskAutoLine::updateParams() { FlightTaskAuto::updateParams(); diff --git a/src/lib/FlightTasks/tasks/FlightTaskAutoLine.hpp b/src/lib/FlightTasks/tasks/FlightTaskAutoLine.hpp index 5c224e717b..006bb82744 100644 --- a/src/lib/FlightTasks/tasks/FlightTaskAutoLine.hpp +++ b/src/lib/FlightTasks/tasks/FlightTaskAutoLine.hpp @@ -86,6 +86,7 @@ protected: void _updateInternalWaypoints(); /* Depending on state of vehicle, the internal waypoints might differ from target (for instance if offtrack). */ void _generateSetpoints(); /**< Generate velocity and position setpoint for following line. */ void _generateAltitudeSetpoints(); /**< Generate velocity and position setpoints for following line along z. */ + void _updateAltitudeAboveGround(); /**< Computes altitude above ground based on sensors available. */ void _generateXYsetpoints(); /**< Generate velocity and position setpoints for following line along xy. */ void updateParams() override; /**< See ModuleParam class */