mirror of
https://gitee.com/mirrors_PX4/PX4-Autopilot.git
synced 2026-10-21 10:13:33 +08:00
Compute RTL time and react if lower than flight time
- Compute RTL time also during RTL - Calculate correct altitude when finding destination
This commit is contained in:
@@ -3606,17 +3606,6 @@ void Commander::avoidance_check()
|
||||
|
||||
void Commander::battery_status_check()
|
||||
{
|
||||
battery_status_s batteries[_battery_status_subs.size()];
|
||||
size_t num_connected_batteries = 0;
|
||||
|
||||
for (auto &battery_sub : _battery_status_subs) {
|
||||
if (battery_sub.copy(&batteries[num_connected_batteries])) {
|
||||
if (batteries[num_connected_batteries].connected) {
|
||||
num_connected_batteries++;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// There are possibly multiple batteries, and we can't know which ones serve which purpose. So the safest
|
||||
// option is to check if ANY of them have a warning, and specifically find which one has the most
|
||||
// urgent warning.
|
||||
@@ -3624,52 +3613,62 @@ void Commander::battery_status_check()
|
||||
// To make sure that all connected batteries are being regularly reported, we check which one has the
|
||||
// oldest timestamp.
|
||||
hrt_abstime oldest_update = hrt_absolute_time();
|
||||
size_t num_connected_batteries{0};
|
||||
float worst_battery_time_s{NAN};
|
||||
|
||||
_battery_current = 0.0f;
|
||||
float battery_level = 0.0f;
|
||||
|
||||
for (auto &battery_sub : _battery_status_subs) {
|
||||
battery_status_s battery;
|
||||
|
||||
// Only iterate over connected batteries. We don't care if a disconnected battery is not regularly publishing.
|
||||
for (size_t i = 0; i < num_connected_batteries; i++) {
|
||||
if (batteries[i].warning > worst_warning) {
|
||||
worst_warning = batteries[i].warning;
|
||||
if (!battery_sub.copy(&battery)) {
|
||||
continue;
|
||||
}
|
||||
|
||||
if (hrt_elapsed_time(&batteries[i].timestamp) > hrt_elapsed_time(&oldest_update)) {
|
||||
oldest_update = batteries[i].timestamp;
|
||||
}
|
||||
if (battery.connected) {
|
||||
num_connected_batteries++;
|
||||
|
||||
// Sum up current from all batteries.
|
||||
_battery_current += batteries[i].current_filtered_a;
|
||||
if (battery.warning > worst_warning) {
|
||||
worst_warning = battery.warning;
|
||||
}
|
||||
|
||||
// average levels from all batteries
|
||||
battery_level += batteries[i].remaining;
|
||||
}
|
||||
if (battery.timestamp < oldest_update) {
|
||||
oldest_update = battery.timestamp;
|
||||
}
|
||||
|
||||
battery_level /= num_connected_batteries;
|
||||
if (PX4_ISFINITE(battery.time_remaining_s)
|
||||
&& (!PX4_ISFINITE(worst_battery_time_s)
|
||||
|| (PX4_ISFINITE(worst_battery_time_s) && (battery.time_remaining_s < worst_battery_time_s)))) {
|
||||
worst_battery_time_s = battery.time_remaining_s;
|
||||
}
|
||||
|
||||
_rtl_flight_time_sub.update();
|
||||
float battery_usage_to_home = 0;
|
||||
|
||||
if (hrt_absolute_time() - _rtl_flight_time_sub.get().timestamp < 2_s) {
|
||||
battery_usage_to_home = _rtl_flight_time_sub.get().rtl_limit_fraction;
|
||||
}
|
||||
|
||||
uint8_t battery_range_warning = battery_status_s::BATTERY_WARNING_NONE;
|
||||
|
||||
if (PX4_ISFINITE(battery_usage_to_home)) {
|
||||
float battery_at_home = battery_level - battery_usage_to_home;
|
||||
|
||||
if (battery_at_home < _param_bat_crit_thr.get()) {
|
||||
battery_range_warning = battery_status_s::BATTERY_WARNING_CRITICAL;
|
||||
|
||||
} else if (battery_at_home < _param_bat_low_thr.get()) {
|
||||
battery_range_warning = battery_status_s::BATTERY_WARNING_LOW;
|
||||
// Sum up current from all batteries.
|
||||
_battery_current += battery.current_filtered_a;
|
||||
}
|
||||
}
|
||||
|
||||
if (battery_range_warning > worst_warning) {
|
||||
worst_warning = battery_range_warning;
|
||||
rtl_time_estimate_s rtl_time_estimate{};
|
||||
|
||||
// Compare estimate of RTL time to estimate of remaining flight time
|
||||
if (_rtl_time_estimate_sub.copy(&rtl_time_estimate)
|
||||
&& hrt_absolute_time() - rtl_time_estimate.timestamp < 2_s
|
||||
&& rtl_time_estimate.valid
|
||||
&& _armed.armed
|
||||
&& !_rtl_time_actions_done
|
||||
&& PX4_ISFINITE(worst_battery_time_s)
|
||||
&& rtl_time_estimate.safe_time_estimate >= worst_battery_time_s
|
||||
&& _internal_state.main_state != commander_state_s::MAIN_STATE_AUTO_RTL
|
||||
&& _internal_state.main_state != commander_state_s::MAIN_STATE_AUTO_LAND) {
|
||||
// Try to trigger RTL
|
||||
if (main_state_transition(_status, commander_state_s::MAIN_STATE_AUTO_RTL, _status_flags,
|
||||
_internal_state) == TRANSITION_CHANGED) {
|
||||
mavlink_log_emergency(&_mavlink_log_pub, "Flight time low, returning to land");
|
||||
|
||||
} else {
|
||||
mavlink_log_emergency(&_mavlink_log_pub, "Flight time low, land now!");
|
||||
}
|
||||
|
||||
_rtl_time_actions_done = true;
|
||||
}
|
||||
|
||||
bool battery_warning_level_increased_while_armed = false;
|
||||
|
||||
@@ -77,7 +77,7 @@
|
||||
#include <uORB/topics/offboard_control_mode.h>
|
||||
#include <uORB/topics/parameter_update.h>
|
||||
#include <uORB/topics/power_button_state.h>
|
||||
#include <uORB/topics/rtl_flight_time.h>
|
||||
#include <uORB/topics/rtl_time_estimate.h>
|
||||
#include <uORB/topics/safety.h>
|
||||
#include <uORB/topics/system_power.h>
|
||||
#include <uORB/topics/telemetry_status.h>
|
||||
@@ -321,6 +321,8 @@ private:
|
||||
bool _geofence_warning_action_on{false};
|
||||
bool _geofence_violated_prev{false};
|
||||
|
||||
bool _rtl_time_actions_done{false};
|
||||
|
||||
FailureDetector _failure_detector;
|
||||
bool _flight_termination_triggered{false};
|
||||
bool _lockdown_triggered{false};
|
||||
@@ -408,6 +410,7 @@ private:
|
||||
uORB::Subscription _iridiumsbd_status_sub{ORB_ID(iridiumsbd_status)};
|
||||
uORB::Subscription _land_detector_sub{ORB_ID(vehicle_land_detected)};
|
||||
uORB::Subscription _manual_control_setpoint_sub{ORB_ID(manual_control_setpoint)};
|
||||
uORB::Subscription _rtl_time_estimate_sub{ORB_ID(rtl_time_estimate)};
|
||||
uORB::Subscription _safety_sub{ORB_ID(safety)};
|
||||
uORB::Subscription _system_power_sub{ORB_ID(system_power)};
|
||||
uORB::Subscription _vehicle_angular_velocity_sub{ORB_ID(vehicle_angular_velocity)};
|
||||
@@ -431,7 +434,6 @@ private:
|
||||
uORB::SubscriptionData<offboard_control_mode_s> _offboard_control_mode_sub{ORB_ID(offboard_control_mode)};
|
||||
uORB::SubscriptionData<vehicle_global_position_s> _global_position_sub{ORB_ID(vehicle_global_position)};
|
||||
uORB::SubscriptionData<vehicle_local_position_s> _local_position_sub{ORB_ID(vehicle_local_position)};
|
||||
uORB::SubscriptionData<rtl_flight_time_s> _rtl_flight_time_sub{ORB_ID(rtl_flight_time)};
|
||||
|
||||
// Publications
|
||||
uORB::Publication<actuator_armed_s> _armed_pub{ORB_ID(actuator_armed)};
|
||||
|
||||
Reference in New Issue
Block a user