Skip to content

Commit 1c6a2ae

Browse files
committed
Plane: make RNGFND_LANDING a bitmask
this gives greater control of when the rangefinder will be used. A particularly important one is the ability to disable it for VTOL assist as false positives can be quite common and drains the battery
1 parent 8d34fab commit 1c6a2ae

File tree

14 files changed

+69
-38
lines changed

14 files changed

+69
-38
lines changed

ArduPlane/GCS_Plane.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@ void GCS_Plane::update_vehicle_sensor_status_flags(void)
116116
const RangeFinder *rangefinder = RangeFinder::get_singleton();
117117
if (rangefinder && rangefinder->has_orientation(plane.rangefinder_orientation())) {
118118
control_sensors_present |= MAV_SYS_STATUS_SENSOR_LASER_POSITION;
119-
if (plane.g.rangefinder_landing) {
119+
if (uint16_t(plane.g.rangefinder_landing.get()) != 0) {
120120
control_sensors_enabled |= MAV_SYS_STATUS_SENSOR_LASER_POSITION;
121121
}
122122
if (rangefinder->has_data_orient(plane.rangefinder_orientation())) {

ArduPlane/Parameters.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -785,9 +785,9 @@ const AP_Param::Info Plane::var_info[] = {
785785
GOBJECT(rangefinder, "RNGFND", RangeFinder),
786786

787787
// @Param: RNGFND_LANDING
788-
// @DisplayName: Enable rangefinder for landing
789-
// @Description: This enables the use of a rangefinder for automatic landing. The rangefinder will be used both on the landing approach and for final flare
790-
// @Values: 0:Disabled,1:Enabled
788+
// @DisplayName: Enable use of rangefinder
789+
// @Description: Sets the use of a rangefinder for automatic landing and other use cases. When enabled for landing and takeoff the rangefinder will be used both on the landing approach and for final flare as well as as VTOL landing and for takeoffs and throttle suppression when close to the ground. When enabled for assist the rangefinder will be used for VTOL assistance. When enabled for climb the rangefinder will be used for the initial climb in QRTL and AUTO. Set to 0 to disable use of the rangefinder.
790+
// @Bitmask: 0:All, 1:TakeoffAndLanding, 2:Assist, 3:InitialClimb
791791
// @User: Standard
792792
GSCALAR(rangefinder_landing, "RNGFND_LANDING", 0),
793793
#endif

ArduPlane/Plane.h

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -226,6 +226,7 @@ class Plane : public AP_Vehicle {
226226
Rotation rangefinder_orientation(void) const {
227227
return Rotation(g2.rangefinder_land_orient.get());
228228
}
229+
229230
#endif
230231

231232
#if AP_MAVLINK_MAV_CMD_SET_HAGL_ENABLED
@@ -896,8 +897,9 @@ class Plane : public AP_Vehicle {
896897
void adjust_altitude_target();
897898
void setup_alt_slope(void);
898899
int32_t get_RTL_altitude_cm() const;
899-
float relative_ground_altitude(bool use_rangefinder_if_available);
900-
float relative_ground_altitude(bool use_rangefinder_if_available, bool use_terrain_if_available);
900+
bool rangefinder_use(enum RangeFinderUse rangefinder_use) const;
901+
float relative_ground_altitude(enum RangeFinderUse rangefinder_use);
902+
float relative_ground_altitude(enum RangeFinderUse rangefinder_use, bool use_terrain_if_available);
901903
void set_target_altitude_current(void);
902904
void set_target_altitude_location(const Location &loc);
903905
int32_t relative_target_altitude_cm(void);

ArduPlane/VTOL_Assist.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ bool VTOL_Assist::should_assist(float aspeed, bool have_airspeed)
106106
alt_error.reset();
107107

108108
} else {
109-
const float height_above_ground = plane.relative_ground_altitude(plane.g.rangefinder_landing);
109+
const float height_above_ground = plane.relative_ground_altitude(RangeFinderUse::ASSIST);
110110
if (alt_error.update(height_above_ground < alt, now_ms, tigger_delay_ms, clear_delay_ms)) {
111111
gcs().send_text(MAV_SEVERITY_WARNING, "Alt assist %.1fm", height_above_ground);
112112
}

ArduPlane/altitude.cpp

Lines changed: 27 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@ int32_t Plane::get_RTL_altitude_cm() const
115115
return relative altitude in meters (relative to terrain, if available,
116116
or home otherwise)
117117
*/
118-
float Plane::relative_ground_altitude(bool use_rangefinder_if_available, bool use_terrain_if_available)
118+
float Plane::relative_ground_altitude(enum RangeFinderUse use_rangefinder, bool use_terrain_if_available)
119119
{
120120
#if AP_MAVLINK_MAV_CMD_SET_HAGL_ENABLED
121121
float height_AGL;
@@ -126,13 +126,13 @@ float Plane::relative_ground_altitude(bool use_rangefinder_if_available, bool us
126126
#endif // AP_MAVLINK_MAV_CMD_SET_HAGL_ENABLED
127127

128128
#if AP_RANGEFINDER_ENABLED
129-
if (use_rangefinder_if_available && rangefinder_state.in_range) {
129+
if (rangefinder_use(use_rangefinder) && rangefinder_state.in_range) {
130130
return rangefinder_state.height_estimate;
131131
}
132132
#endif
133133

134134
#if HAL_QUADPLANE_ENABLED && AP_RANGEFINDER_ENABLED
135-
if (use_rangefinder_if_available && quadplane.in_vtol_land_final() &&
135+
if (rangefinder_use(use_rangefinder) && quadplane.in_vtol_land_final() &&
136136
rangefinder.status_orient(rangefinder_orientation()) == RangeFinder::Status::OutOfRangeLow) {
137137
// a special case for quadplane landing when rangefinder goes
138138
// below minimum. Consider our height above ground to be zero
@@ -163,13 +163,29 @@ float Plane::relative_ground_altitude(bool use_rangefinder_if_available, bool us
163163
return relative_altitude;
164164
}
165165

166+
/*
167+
return true if we should use the rangefinder for a specific use case
168+
*/
169+
bool Plane::rangefinder_use(enum RangeFinderUse use_rangefinder) const
170+
{
171+
const uint8_t use = uint8_t(g.rangefinder_landing.get());
172+
if (use == uint8_t(RangeFinderUse::NONE)) {
173+
return false;
174+
}
175+
if (use & uint8_t(RangeFinderUse::ALL)) {
176+
// if ALL bit is set then ignore other bits
177+
return true;
178+
}
179+
return (use & uint8_t(use_rangefinder)) != 0;
180+
}
181+
166182
// Helper for above method using terrain if the vehicle is currently terrain following
167-
float Plane::relative_ground_altitude(bool use_rangefinder_if_available)
183+
float Plane::relative_ground_altitude(enum RangeFinderUse use_rangefinder)
168184
{
169185
#if AP_TERRAIN_AVAILABLE
170-
return relative_ground_altitude(use_rangefinder_if_available, target_altitude.terrain_following);
186+
return relative_ground_altitude(use_rangefinder, target_altitude.terrain_following);
171187
#else
172-
return relative_ground_altitude(use_rangefinder_if_available, false);
188+
return relative_ground_altitude(use_rangefinder, false);
173189
#endif
174190
}
175191

@@ -667,7 +683,7 @@ float Plane::rangefinder_correction(void)
667683
}
668684

669685
// for now we only support the rangefinder for landing
670-
bool using_rangefinder = (g.rangefinder_landing && flight_stage == AP_FixedWing::FlightStage::LAND);
686+
bool using_rangefinder = (rangefinder_use(RangeFinderUse::TAKEOFF_LANDING) && flight_stage == AP_FixedWing::FlightStage::LAND);
671687
if (!using_rangefinder) {
672688
return 0;
673689
}
@@ -682,7 +698,7 @@ float Plane::rangefinder_correction(void)
682698
void Plane::rangefinder_terrain_correction(float &height)
683699
{
684700
#if AP_TERRAIN_AVAILABLE
685-
if (!g.rangefinder_landing ||
701+
if (!rangefinder_use(RangeFinderUse::TAKEOFF_LANDING) ||
686702
flight_stage != AP_FixedWing::FlightStage::LAND ||
687703
!terrain_enabled_in_current_mode()) {
688704
return;
@@ -765,7 +781,7 @@ void Plane::rangefinder_height_update(void)
765781
#endif
766782
if (!rangefinder_state.in_use &&
767783
flightstage_good_for_rangefinder_landing &&
768-
g.rangefinder_landing) {
784+
rangefinder_use(RangeFinderUse::TAKEOFF_LANDING)) {
769785
rangefinder_state.in_use = true;
770786
gcs().send_text(MAV_SEVERITY_INFO, "Rangefinder engaged at %.2fm", (double)rangefinder_state.height_estimate);
771787
}
@@ -795,7 +811,7 @@ void Plane::rangefinder_height_update(void)
795811
if (now - rangefinder_state.last_correction_time_ms > 5000) {
796812
rangefinder_state.correction = correction;
797813
rangefinder_state.initial_correction = correction;
798-
if (g.rangefinder_landing) {
814+
if (rangefinder_use(RangeFinderUse::TAKEOFF_LANDING)) {
799815
landing.set_initial_slope();
800816
}
801817
rangefinder_state.last_correction_time_ms = now;
@@ -931,7 +947,7 @@ float Plane::get_landing_height(bool &rangefinder_active)
931947
#if AP_RANGEFINDER_ENABLED
932948
// possibly correct with rangefinder
933949
height -= rangefinder_correction();
934-
rangefinder_active = g.rangefinder_landing && rangefinder_state.in_range;
950+
rangefinder_active = rangefinder_use(RangeFinderUse::TAKEOFF_LANDING) && rangefinder_state.in_range;
935951
#endif
936952

937953
return height;

ArduPlane/defines.h

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -189,3 +189,15 @@ enum class FenceAutoEnable : uint8_t {
189189
AutoDisableFloorOnly=2,
190190
WhenArmed=3
191191
};
192+
193+
/*
194+
bitmask of options for RGFND_LANDING
195+
*/
196+
enum class RangeFinderUse : uint8_t {
197+
NONE = 0U,
198+
ALL = (1U<<0),
199+
TAKEOFF_LANDING = (1U<<1),
200+
ASSIST = (1U<<2),
201+
CLIMB = (1U<<3),
202+
};
203+

ArduPlane/mode_autoland.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -162,7 +162,7 @@ bool ModeAutoLand::_enter()
162162
#else
163163
const bool use_terrain = false;
164164
#endif
165-
const float dist_to_climb = terrain_alt_min - plane.relative_ground_altitude(plane.g.rangefinder_landing, use_terrain);
165+
const float dist_to_climb = terrain_alt_min - plane.relative_ground_altitude(RangeFinderUse::CLIMB, use_terrain);
166166
if (is_positive(dist_to_climb)) {
167167
// Copy loiter and update target altitude to current altitude plus climb altitude
168168
cmd_climb = cmd_loiter;

ArduPlane/mode_qland.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ bool ModeQLand::_enter()
99
quadplane.throttle_wait = false;
1010
quadplane.setup_target_position();
1111
poscontrol.set_state(QuadPlane::QPOS_LAND_DESCEND);
12-
quadplane.last_land_final_agl = plane.relative_ground_altitude(plane.g.rangefinder_landing);
12+
quadplane.last_land_final_agl = plane.relative_ground_altitude(RangeFinderUse::TAKEOFF_LANDING);
1313
quadplane.landing_detect.lower_limit_start_ms = 0;
1414
quadplane.landing_detect.land_start_ms = 0;
1515
#if AP_LANDINGGEAR_ENABLED

ArduPlane/mode_qloiter.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -157,7 +157,7 @@ void ModeQLoiter::run()
157157
}
158158
#endif // AP_ICENGINE_ENABLED
159159
}
160-
float height_above_ground = plane.relative_ground_altitude(plane.g.rangefinder_landing);
160+
float height_above_ground = plane.relative_ground_altitude(RangeFinderUse::TAKEOFF_LANDING);
161161
float descent_rate_cms = quadplane.landing_descent_rate_cms(height_above_ground);
162162

163163
if (poscontrol.get_state() == QuadPlane::QPOS_LAND_FINAL && !quadplane.option_is_set(QuadPlane::OPTION::DISABLE_GROUND_EFFECT_COMP)) {

ArduPlane/mode_qrtl.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ bool ModeQRTL::_enter()
3333
const bool use_terrain = false;
3434
#endif
3535

36-
const float dist_to_climb = target_alt - plane.relative_ground_altitude(plane.g.rangefinder_landing, use_terrain);
36+
const float dist_to_climb = target_alt - plane.relative_ground_altitude(RangeFinderUse::CLIMB, use_terrain);
3737
if (is_positive(dist_to_climb)) {
3838
// climb before returning, only next waypoint altitude is used
3939
submode = SubMode::climb;

0 commit comments

Comments
 (0)