-
Notifications
You must be signed in to change notification settings - Fork 13.4k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
RTL based on remaining flight time estimation #18646
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
uint64 timestamp # time since system start (microseconds) | ||
|
||
bool valid # Flag indicating whether the time estiamtes are valid | ||
float32 time_estimate # [s] Estimated time for RTL | ||
float32 safe_time_estimate # [s] Same as time_estimate, but with safety factor and safety margin included (factor*t + margin) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -2134,7 +2134,9 @@ Commander::run() | |||||
|
||||||
_cpuload_sub.update(&_cpuload); | ||||||
|
||||||
battery_status_check(); | ||||||
if (_battery_status_subs.updated()) { | ||||||
battery_status_check(); | ||||||
} | ||||||
|
||||||
/* If in INIT state, try to proceed to STANDBY state */ | ||||||
if (!_status_flags.condition_calibration_enabled && _status.arming_state == vehicle_status_s::ARMING_STATE_INIT) { | ||||||
|
@@ -3523,77 +3525,69 @@ void Commander::avoidance_check() | |||||
|
||||||
void Commander::battery_status_check() | ||||||
{ | ||||||
// We need to update the status flag if ANY battery is updated, because the system source might have | ||||||
// changed, or might be nothing (if there is no battery connected) | ||||||
if (!_battery_status_subs.updated()) { | ||||||
// Nothing has changed since the last time this function was called, so nothing needs to be done now. | ||||||
return; | ||||||
} | ||||||
|
||||||
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. | ||||||
uint8_t worst_warning = battery_status_s::BATTERY_WARNING_NONE; | ||||||
// 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; | ||||||
|
||||||
|
||||||
// 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; | ||||||
} | ||||||
for (auto &battery_sub : _battery_status_subs) { | ||||||
battery_status_s battery; | ||||||
|
||||||
if (hrt_elapsed_time(&batteries[i].timestamp) > hrt_elapsed_time(&oldest_update)) { | ||||||
oldest_update = batteries[i].timestamp; | ||||||
if (!battery_sub.copy(&battery)) { | ||||||
continue; | ||||||
} | ||||||
|
||||||
// Sum up current from all batteries. | ||||||
_battery_current += batteries[i].current_filtered_a; | ||||||
if (battery.connected) { | ||||||
num_connected_batteries++; | ||||||
|
||||||
// average levels from all batteries | ||||||
battery_level += batteries[i].remaining; | ||||||
} | ||||||
if (battery.warning > worst_warning) { | ||||||
worst_warning = battery.warning; | ||||||
} | ||||||
|
||||||
battery_level /= num_connected_batteries; | ||||||
if (battery.timestamp < oldest_update) { | ||||||
oldest_update = battery.timestamp; | ||||||
} | ||||||
|
||||||
_rtl_flight_time_sub.update(); | ||||||
float battery_usage_to_home = 0; | ||||||
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; | ||||||
} | ||||||
|
||||||
if (hrt_absolute_time() - _rtl_flight_time_sub.get().timestamp < 2_s) { | ||||||
battery_usage_to_home = _rtl_flight_time_sub.get().rtl_limit_fraction; | ||||||
// Sum up current from all batteries. | ||||||
_battery_current += battery.current_filtered_a; | ||||||
} | ||||||
} | ||||||
|
||||||
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; | ||||||
rtl_time_estimate_s rtl_time_estimate{}; | ||||||
|
||||||
if (battery_at_home < _param_bat_crit_thr.get()) { | ||||||
battery_range_warning = battery_status_s::BATTERY_WARNING_CRITICAL; | ||||||
// 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 if (battery_at_home < _param_bat_low_thr.get()) { | ||||||
battery_range_warning = battery_status_s::BATTERY_WARNING_LOW; | ||||||
} else { | ||||||
mavlink_log_emergency(&_mavlink_log_pub, "Flight time low, land now!"); | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||||||
} | ||||||
} | ||||||
|
||||||
if (battery_range_warning > worst_warning) { | ||||||
worst_warning = battery_range_warning; | ||||||
_rtl_time_actions_done = true; | ||||||
} | ||||||
|
||||||
bool battery_warning_level_increased_while_armed = false; | ||||||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
How about
"Remaining" flight time low, return to land
?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I guess we should also add an event here and not only the legacy mavlink log?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
#18711