2017-02-26 19:18:38 -04:00
|
|
|
#include "Plane.h"
|
|
|
|
|
2018-07-29 19:57:55 -03:00
|
|
|
#if SOARING_ENABLED == ENABLED
|
|
|
|
|
2017-02-26 19:18:38 -04:00
|
|
|
/*
|
|
|
|
* ArduSoar support function
|
|
|
|
*
|
|
|
|
* Peter Braswell, Samuel Tabor, Andrey Kolobov, and Iain Guilliard
|
|
|
|
*/
|
|
|
|
void Plane::update_soaring() {
|
|
|
|
|
2019-07-15 07:27:29 -03:00
|
|
|
// Check if soaring is active. Also sets throttle suppressed
|
|
|
|
// status on active state changes.
|
2020-04-05 08:38:19 -03:00
|
|
|
plane.g2.soaring_controller.update_active_state();
|
|
|
|
|
|
|
|
if (!g2.soaring_controller.is_active()) {
|
2017-02-26 19:18:38 -04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
g2.soaring_controller.update_vario();
|
|
|
|
|
|
|
|
// Check for throttle suppression change.
|
2019-01-15 13:46:13 -04:00
|
|
|
switch (control_mode->mode_number()) {
|
|
|
|
case Mode::Number::AUTO:
|
2017-02-26 19:18:38 -04:00
|
|
|
g2.soaring_controller.suppress_throttle();
|
|
|
|
break;
|
2019-01-15 13:46:13 -04:00
|
|
|
case Mode::Number::FLY_BY_WIRE_B:
|
|
|
|
case Mode::Number::CRUISE:
|
2018-10-18 09:51:21 -03:00
|
|
|
if (!g2.soaring_controller.suppress_throttle() && aparm.throttle_max > 0) {
|
2017-07-09 00:49:39 -03:00
|
|
|
gcs().send_text(MAV_SEVERITY_INFO, "Soaring: forcing RTL");
|
2019-10-17 00:49:32 -03:00
|
|
|
set_mode(mode_rtl, ModeReason::SOARING_FBW_B_WITH_MOTOR_RUNNING);
|
2017-02-26 19:18:38 -04:00
|
|
|
}
|
|
|
|
break;
|
2019-01-15 13:46:13 -04:00
|
|
|
case Mode::Number::LOITER:
|
2018-10-18 09:51:21 -03:00
|
|
|
// Never use throttle in LOITER with soaring active.
|
|
|
|
g2.soaring_controller.set_throttle_suppressed(true);
|
2017-02-26 19:18:38 -04:00
|
|
|
break;
|
|
|
|
default:
|
2018-10-18 09:51:21 -03:00
|
|
|
// In any other mode allow throttle.
|
|
|
|
g2.soaring_controller.set_throttle_suppressed(false);
|
2017-02-26 19:18:38 -04:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Nothing to do if we are in powered flight
|
|
|
|
if (!g2.soaring_controller.get_throttle_suppressed() && aparm.throttle_max > 0) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-01-15 13:46:13 -04:00
|
|
|
switch (control_mode->mode_number()) {
|
2019-06-05 15:24:17 -03:00
|
|
|
default:
|
|
|
|
// nothing to do
|
|
|
|
break;
|
2019-01-15 13:46:13 -04:00
|
|
|
case Mode::Number::AUTO:
|
|
|
|
case Mode::Number::FLY_BY_WIRE_B:
|
|
|
|
case Mode::Number::CRUISE:
|
2017-02-26 19:18:38 -04:00
|
|
|
// Test for switch into thermalling mode
|
|
|
|
g2.soaring_controller.update_cruising();
|
|
|
|
|
|
|
|
if (g2.soaring_controller.check_thermal_criteria()) {
|
2019-06-05 15:24:17 -03:00
|
|
|
gcs().send_text(MAV_SEVERITY_INFO, "Soaring: Thermal detected, entering %s", mode_loiter.name());
|
2019-10-17 00:49:32 -03:00
|
|
|
set_mode(mode_loiter, ModeReason::SOARING_THERMAL_DETECTED);
|
2017-02-26 19:18:38 -04:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
2019-06-05 15:24:17 -03:00
|
|
|
case Mode::Number::LOITER: {
|
2017-02-26 19:18:38 -04:00
|
|
|
// Update thermal estimate and check for switch back to AUTO
|
|
|
|
g2.soaring_controller.update_thermalling(); // Update estimate
|
|
|
|
|
2019-06-22 14:51:17 -03:00
|
|
|
Vector2f prev_wp, next_wp;
|
|
|
|
|
|
|
|
// If previous mode was AUTO and there was a previous NAV command, we can use previous and next wps for drift calculation.
|
|
|
|
if (previous_mode->mode_number() == Mode::Number::AUTO) {
|
|
|
|
AP_Mission::Mission_Command current_nav_cmd = mission.get_current_nav_cmd();
|
|
|
|
AP_Mission::Mission_Command prev_nav_cmd;
|
|
|
|
|
|
|
|
if (!(mission.get_next_nav_cmd(mission.get_prev_nav_cmd_with_wp_index(), prev_nav_cmd) &&
|
|
|
|
prev_nav_cmd.content.location.get_vector_xy_from_origin_NE(prev_wp) &&
|
|
|
|
current_nav_cmd.content.location.get_vector_xy_from_origin_NE(next_wp))) {
|
|
|
|
prev_wp.x = 0.0;
|
|
|
|
prev_wp.y = 0.0;
|
|
|
|
next_wp.x = 0.0;
|
|
|
|
next_wp.y = 0.0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const SoaringController::LoiterStatus loiterStatus = g2.soaring_controller.check_cruise_criteria(prev_wp/100, next_wp/100);
|
2017-02-26 19:18:38 -04:00
|
|
|
|
2020-04-05 08:38:19 -03:00
|
|
|
if (loiterStatus == SoaringController::LoiterStatus::GOOD_TO_KEEP_LOITERING) {
|
2019-06-08 21:25:01 -03:00
|
|
|
// Reset loiter angle, so that the loiter exit heading criteria
|
|
|
|
// only starts expanding when we're ready to exit.
|
|
|
|
plane.loiter.sum_cd = 0;
|
2019-06-10 14:07:24 -03:00
|
|
|
plane.soaring_mode_timer = AP_HAL::millis();
|
2019-06-05 15:24:17 -03:00
|
|
|
break;
|
|
|
|
}
|
2017-02-26 19:18:38 -04:00
|
|
|
|
2019-06-10 14:07:24 -03:00
|
|
|
// Some other loiter status, we need to think about exiting loiter.
|
|
|
|
uint32_t timer = AP_HAL::millis() - plane.soaring_mode_timer;
|
|
|
|
|
2019-06-17 09:15:53 -03:00
|
|
|
// Check distance to home.
|
|
|
|
Vector3f position;
|
|
|
|
if (!ahrs.get_relative_position_NED_home(position)) {
|
|
|
|
return;
|
2019-06-17 23:44:32 -03:00
|
|
|
} else if (g2.soaring_controller.max_radius >= 0 &&
|
2019-06-17 09:15:53 -03:00
|
|
|
powf(position.x,2)+powf(position.y,2) > powf(g2.soaring_controller.max_radius,2) &&
|
|
|
|
previous_mode->mode_number()!=Mode::Number::AUTO) {
|
|
|
|
// Some other loiter status, and outside of maximum soaring radius, and previous mode wasn't AUTO
|
|
|
|
gcs().send_text(MAV_SEVERITY_INFO, "Soaring: Outside SOAR_MAX_RADIUS, RTL");
|
|
|
|
set_mode(mode_rtl, ModeReason::SOARING_DRIFT_EXCEEDED);
|
|
|
|
}
|
|
|
|
|
2019-06-05 15:24:17 -03:00
|
|
|
const char* strTooHigh = "Soaring: Too high, restoring ";
|
2019-06-07 10:39:57 -03:00
|
|
|
const char* strTooLow = "Soaring: Too low, restoring ";
|
2019-06-05 15:24:17 -03:00
|
|
|
const char* strTooWeak = "Soaring: Thermal ended, restoring ";
|
2019-06-09 07:01:16 -03:00
|
|
|
const char* strTooFar = "Soaring: Drifted too far, restoring ";
|
2017-02-26 19:18:38 -04:00
|
|
|
|
2019-06-07 12:08:28 -03:00
|
|
|
// Exit as soon as thermal state estimate deteriorates and we're lined up to next target
|
|
|
|
switch (previous_mode->mode_number()){
|
2019-06-05 15:24:17 -03:00
|
|
|
case Mode::Number::FLY_BY_WIRE_B: {
|
2019-06-07 12:08:28 -03:00
|
|
|
const bool homeIsSet = AP::ahrs().home_is_set();
|
|
|
|
const bool headingLinedupToHome = homeIsSet && plane.mode_loiter.isHeadingLinedUp(next_WP_loc, AP::ahrs().get_home());
|
2019-06-10 14:07:24 -03:00
|
|
|
if (homeIsSet && !headingLinedupToHome && loiterStatus!=SoaringController::LoiterStatus::ALT_TOO_LOW && timer<20e3) {
|
2019-06-07 12:08:28 -03:00
|
|
|
break;
|
|
|
|
}
|
2019-06-05 15:24:17 -03:00
|
|
|
switch (loiterStatus) {
|
2019-06-07 12:08:28 -03:00
|
|
|
case SoaringController::LoiterStatus::ALT_TOO_HIGH:
|
|
|
|
gcs().send_text(MAV_SEVERITY_INFO, "%s%s", strTooHigh, mode_fbwb.name());
|
|
|
|
set_mode(mode_fbwb, ModeReason::SOARING_ALT_TOO_HIGH);
|
2019-06-05 15:24:17 -03:00
|
|
|
break;
|
|
|
|
case SoaringController::LoiterStatus::ALT_TOO_LOW:
|
|
|
|
gcs().send_text(MAV_SEVERITY_INFO, "%s%s", strTooLow, mode_rtl.name());
|
|
|
|
set_mode(mode_rtl, ModeReason::SOARING_ALT_TOO_LOW);
|
|
|
|
break;
|
2019-06-07 12:08:28 -03:00
|
|
|
default:
|
2019-06-05 15:24:17 -03:00
|
|
|
case SoaringController::LoiterStatus::THERMAL_WEAK:
|
|
|
|
gcs().send_text(MAV_SEVERITY_INFO, "%s%s", strTooWeak, mode_fbwb.name());
|
|
|
|
set_mode(mode_fbwb, ModeReason::SOARING_THERMAL_ESTIMATE_DETERIORATED);
|
|
|
|
break;
|
2019-06-09 07:01:16 -03:00
|
|
|
case SoaringController::LoiterStatus::DRIFT_EXCEEDED:
|
|
|
|
gcs().send_text(MAV_SEVERITY_INFO, "%s%s", strTooFar, mode_fbwb.name());
|
|
|
|
set_mode(mode_fbwb, ModeReason::SOARING_DRIFT_EXCEEDED);
|
|
|
|
break;
|
2019-06-05 17:04:20 -03:00
|
|
|
} // switch louterStatus
|
|
|
|
break;
|
2019-06-07 12:08:28 -03:00
|
|
|
}
|
2019-06-05 15:24:17 -03:00
|
|
|
|
|
|
|
case Mode::Number::CRUISE: {
|
2020-01-28 00:14:43 -04:00
|
|
|
const bool headingLinedupToCruise = plane.mode_loiter.isHeadingLinedUp_cd(cruise_state.locked_heading_cd);
|
2019-06-10 14:07:24 -03:00
|
|
|
if (cruise_state.locked_heading && !headingLinedupToCruise && loiterStatus!=SoaringController::LoiterStatus::ALT_TOO_LOW && timer<20e3) {
|
2019-06-07 12:08:28 -03:00
|
|
|
break;
|
|
|
|
}
|
2019-06-05 15:24:17 -03:00
|
|
|
// return to cruise with old ground course
|
|
|
|
const CruiseState cruise = cruise_state;
|
|
|
|
switch (loiterStatus) {
|
|
|
|
case SoaringController::LoiterStatus::ALT_TOO_HIGH:
|
|
|
|
gcs().send_text(MAV_SEVERITY_INFO, "%s%s", strTooHigh, mode_cruise.name());
|
|
|
|
set_mode(mode_cruise, ModeReason::SOARING_ALT_TOO_HIGH);
|
|
|
|
break;
|
|
|
|
case SoaringController::LoiterStatus::ALT_TOO_LOW:
|
|
|
|
gcs().send_text(MAV_SEVERITY_INFO, "%s%s", strTooLow, mode_rtl.name());
|
|
|
|
set_mode(mode_rtl, ModeReason::SOARING_ALT_TOO_LOW);
|
|
|
|
break;
|
2019-06-07 12:08:28 -03:00
|
|
|
default:
|
2019-06-05 15:24:17 -03:00
|
|
|
case SoaringController::LoiterStatus::THERMAL_WEAK:
|
|
|
|
gcs().send_text(MAV_SEVERITY_INFO, "%s%s", strTooWeak, mode_cruise.name());
|
|
|
|
set_mode(mode_cruise, ModeReason::SOARING_THERMAL_ESTIMATE_DETERIORATED);
|
|
|
|
break;
|
2019-06-09 07:01:16 -03:00
|
|
|
case SoaringController::LoiterStatus::DRIFT_EXCEEDED:
|
|
|
|
gcs().send_text(MAV_SEVERITY_INFO, "%s%s", strTooFar, mode_cruise.name());
|
|
|
|
set_mode(mode_cruise, ModeReason::SOARING_DRIFT_EXCEEDED);
|
|
|
|
break;
|
2019-06-05 15:24:17 -03:00
|
|
|
} // switch loiterStatus
|
|
|
|
cruise_state = cruise;
|
|
|
|
set_target_altitude_current();
|
|
|
|
break;
|
2019-06-05 17:04:20 -03:00
|
|
|
} // case Cruise
|
2019-06-05 15:24:17 -03:00
|
|
|
|
2019-06-07 12:08:28 -03:00
|
|
|
case Mode::Number::AUTO: {
|
|
|
|
//Get the lat/lon of next Nav waypoint after this one:
|
2019-06-08 21:12:43 -03:00
|
|
|
AP_Mission::Mission_Command current_nav_cmd = mission.get_current_nav_cmd();;
|
2019-06-09 07:01:16 -03:00
|
|
|
const bool headingLinedupToWP = plane.mode_loiter.isHeadingLinedUp(next_WP_loc, current_nav_cmd.content.location);
|
2019-06-10 14:07:24 -03:00
|
|
|
if (!headingLinedupToWP && loiterStatus!=SoaringController::LoiterStatus::ALT_TOO_LOW && timer<20e3) {
|
2019-06-07 12:08:28 -03:00
|
|
|
break;
|
|
|
|
}
|
2019-06-05 15:24:17 -03:00
|
|
|
switch (loiterStatus) {
|
|
|
|
case SoaringController::LoiterStatus::ALT_TOO_HIGH:
|
|
|
|
gcs().send_text(MAV_SEVERITY_INFO, "%s%s", strTooHigh, mode_auto.name());
|
|
|
|
set_mode(mode_auto, ModeReason::SOARING_ALT_TOO_HIGH);
|
|
|
|
break;
|
|
|
|
case SoaringController::LoiterStatus::ALT_TOO_LOW:
|
|
|
|
gcs().send_text(MAV_SEVERITY_INFO, "%s%s", strTooLow, mode_auto.name());
|
|
|
|
set_mode(mode_auto, ModeReason::SOARING_ALT_TOO_LOW);
|
|
|
|
break;
|
2019-06-07 12:08:28 -03:00
|
|
|
default:
|
|
|
|
case SoaringController::LoiterStatus::THERMAL_WEAK:
|
2019-06-05 17:04:20 -03:00
|
|
|
gcs().send_text(MAV_SEVERITY_INFO, "%s%s", strTooWeak, mode_auto.name());
|
|
|
|
set_mode(mode_auto, ModeReason::SOARING_THERMAL_ESTIMATE_DETERIORATED);
|
2019-06-05 15:24:17 -03:00
|
|
|
break;
|
2019-06-09 07:01:16 -03:00
|
|
|
case SoaringController::LoiterStatus::DRIFT_EXCEEDED:
|
|
|
|
gcs().send_text(MAV_SEVERITY_INFO, "%s%s", strTooFar, mode_auto.name());
|
|
|
|
set_mode(mode_auto, ModeReason::SOARING_DRIFT_EXCEEDED);
|
|
|
|
break;
|
2019-06-07 12:08:28 -03:00
|
|
|
} // switch loiterStatus
|
2019-06-05 15:24:17 -03:00
|
|
|
break;
|
2019-06-07 12:08:28 -03:00
|
|
|
} // case AUTO
|
2019-06-05 17:04:20 -03:00
|
|
|
default: // all other modes
|
2019-06-05 15:24:17 -03:00
|
|
|
break;
|
|
|
|
} // switch previous_mode
|
2017-02-26 19:18:38 -04:00
|
|
|
break;
|
2019-06-05 17:04:20 -03:00
|
|
|
} // case loiter
|
|
|
|
} // switch control_mode
|
2019-06-05 15:24:17 -03:00
|
|
|
|
|
|
|
|
|
|
|
if (control_mode == &mode_loiter) {
|
|
|
|
// still in thermal - need to update the wp location
|
|
|
|
g2.soaring_controller.get_target(next_WP_loc);
|
|
|
|
|
2017-02-26 19:18:38 -04:00
|
|
|
}
|
2019-06-05 15:24:17 -03:00
|
|
|
|
2017-02-26 19:18:38 -04:00
|
|
|
}
|
|
|
|
|
2018-07-29 19:57:55 -03:00
|
|
|
#endif // SOARING_ENABLED
|