123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401 |
- #include "AP_Landing.h"
- #include <GCS_MAVLink/GCS.h>
- #include <AP_HAL/AP_HAL.h>
- #include <AP_LandingGear/AP_LandingGear.h>
- #include <AP_AHRS/AP_AHRS.h>
- #include <AP_GPS/AP_GPS.h>
- void AP_Landing::type_slope_do_land(const AP_Mission::Mission_Command& cmd, const float relative_altitude)
- {
- initial_slope = 0;
- slope = 0;
-
- type_slope_flags.post_stats = false;
- type_slope_stage = SLOPE_STAGE_NORMAL;
- gcs().send_text(MAV_SEVERITY_INFO, "Landing approach start at %.1fm", (double)relative_altitude);
- }
- void AP_Landing::type_slope_verify_abort_landing(const Location &prev_WP_loc, Location &next_WP_loc, bool &throttle_suppressed)
- {
-
-
- throttle_suppressed = false;
- nav_controller->update_heading_hold(prev_WP_loc.get_bearing_to(next_WP_loc));
- }
- bool AP_Landing::type_slope_verify_land(const Location &prev_WP_loc, Location &next_WP_loc, const Location ¤t_loc,
- const float height, const float sink_rate, const float wp_proportion, const uint32_t last_flying_ms, const bool is_armed, const bool is_flying, const bool rangefinder_state_in_range)
- {
-
-
-
-
- if (type_slope_stage == SLOPE_STAGE_NORMAL) {
- const bool heading_lined_up = abs(nav_controller->bearing_error_cd()) < 1000 && !nav_controller->data_is_stale();
- const bool on_flight_line = fabsf(nav_controller->crosstrack_error()) < 5.0f && !nav_controller->data_is_stale();
- const bool below_prev_WP = current_loc.alt < prev_WP_loc.alt;
- if ((mission.get_prev_nav_cmd_id() == MAV_CMD_NAV_LOITER_TO_ALT) ||
- (wp_proportion >= 0 && heading_lined_up && on_flight_line) ||
- (wp_proportion > 0.15f && heading_lined_up && below_prev_WP) ||
- (wp_proportion > 0.5f)) {
- type_slope_stage = SLOPE_STAGE_APPROACH;
- }
- }
-
-
-
-
-
-
-
- const bool on_approach_stage = type_slope_is_on_approach();
- const bool below_flare_alt = (height <= flare_alt);
- const bool below_flare_sec = (flare_sec > 0 && height <= sink_rate * flare_sec);
- const bool probably_crashed = (aparm.crash_detection_enable && fabsf(sink_rate) < 0.2f && !is_flying);
- if ((on_approach_stage && below_flare_alt) ||
- (on_approach_stage && below_flare_sec && (wp_proportion > 0.5)) ||
- (!rangefinder_state_in_range && wp_proportion >= 1) ||
- probably_crashed) {
- const AP_GPS &gps = AP::gps();
- if (type_slope_stage != SLOPE_STAGE_FINAL) {
- type_slope_flags.post_stats = true;
- if (is_flying && (AP_HAL::millis()-last_flying_ms) > 3000) {
- gcs().send_text(MAV_SEVERITY_CRITICAL, "Flare crash detected: speed=%.1f", (double)gps.ground_speed());
- } else {
- gcs().send_text(MAV_SEVERITY_INFO, "Flare %.1fm sink=%.2f speed=%.1f dist=%.1f",
- (double)height, (double)sink_rate,
- (double)gps.ground_speed(),
- (double)current_loc.get_distance(next_WP_loc));
- }
-
- type_slope_stage = SLOPE_STAGE_FINAL;
-
-
-
- AP_LandingGear *LG_inst = AP_LandingGear::get_singleton();
- if (LG_inst != nullptr && !LG_inst->check_before_land()) {
- type_slope_request_go_around();
- gcs().send_text(MAV_SEVERITY_CRITICAL, "Landing gear was not deployed");
- }
- }
- if (gps.ground_speed() < 3) {
-
-
-
-
- aparm.airspeed_cruise_cm.load();
- aparm.min_gndspeed_cm.load();
- aparm.throttle_cruise.load();
- }
- } else if (type_slope_stage == SLOPE_STAGE_APPROACH && pre_flare_airspeed > 0) {
- bool reached_pre_flare_alt = pre_flare_alt > 0 && (height <= pre_flare_alt);
- bool reached_pre_flare_sec = pre_flare_sec > 0 && (height <= sink_rate * pre_flare_sec);
- if (reached_pre_flare_alt || reached_pre_flare_sec) {
- type_slope_stage = SLOPE_STAGE_PREFLARE;
- }
- }
-
- struct Location land_WP_loc = next_WP_loc;
- int32_t land_bearing_cd = prev_WP_loc.get_bearing_to(next_WP_loc);
- land_WP_loc.offset_bearing(land_bearing_cd * 0.01f, prev_WP_loc.get_distance(current_loc) + 200);
- nav_controller->update_waypoint(prev_WP_loc, land_WP_loc);
-
-
- if (type_slope_flags.post_stats && !is_armed) {
- type_slope_flags.post_stats = false;
- gcs().send_text(MAV_SEVERITY_INFO, "Distance from LAND point=%.2fm", (double)current_loc.get_distance(next_WP_loc));
- }
-
- if (type_slope_stage == SLOPE_STAGE_FINAL) {
- disarm_if_autoland_complete_fn();
- }
-
- return false;
- }
- void AP_Landing::type_slope_adjust_landing_slope_for_rangefinder_bump(AP_Vehicle::FixedWing::Rangefinder_State &rangefinder_state, Location &prev_WP_loc, Location &next_WP_loc, const Location ¤t_loc, const float wp_distance, int32_t &target_altitude_offset_cm)
- {
-
-
-
- float correction_delta = fabsf(rangefinder_state.last_stable_correction) - fabsf(rangefinder_state.correction);
- if (slope_recalc_shallow_threshold <= 0 ||
- fabsf(correction_delta) < slope_recalc_shallow_threshold) {
- return;
- }
- rangefinder_state.last_stable_correction = rangefinder_state.correction;
- float corrected_alt_m = (adjusted_altitude_cm_fn() - next_WP_loc.alt)*0.01f - rangefinder_state.correction;
- float total_distance_m = prev_WP_loc.get_distance(next_WP_loc);
- float top_of_glide_slope_alt_m = total_distance_m * corrected_alt_m / wp_distance;
- prev_WP_loc.alt = top_of_glide_slope_alt_m*100 + next_WP_loc.alt;
-
- setup_landing_glide_slope(prev_WP_loc, next_WP_loc, current_loc, target_altitude_offset_cm);
- if (rangefinder_state.correction >= 0) {
-
-
- } else if (slope_recalc_steep_threshold_to_abort > 0 && !type_slope_flags.has_aborted_due_to_slope_recalc) {
-
-
-
-
-
-
- float new_slope_deg = degrees(atanf(slope));
- float initial_slope_deg = degrees(atanf(initial_slope));
-
- if (new_slope_deg - initial_slope_deg > slope_recalc_steep_threshold_to_abort) {
- gcs().send_text(MAV_SEVERITY_INFO, "Landing slope too steep, aborting (%.0fm %.1fdeg)",
- (double)rangefinder_state.correction, (double)(new_slope_deg - initial_slope_deg));
- alt_offset = rangefinder_state.correction;
- flags.commanded_go_around = true;
- type_slope_flags.has_aborted_due_to_slope_recalc = true;
- Log();
- }
- }
- }
- bool AP_Landing::type_slope_request_go_around(void)
- {
- flags.commanded_go_around = true;
- return true;
- }
- void AP_Landing::type_slope_setup_landing_glide_slope(const Location &prev_WP_loc, const Location &next_WP_loc, const Location ¤t_loc, int32_t &target_altitude_offset_cm)
- {
- float total_distance = prev_WP_loc.get_distance(next_WP_loc);
-
-
- if (total_distance < 1) {
- total_distance = 1;
- }
-
- float sink_height = (prev_WP_loc.alt - next_WP_loc.alt)*0.01f;
-
- float groundspeed = ahrs.groundspeed();
- if (groundspeed < 0.5f) {
- groundspeed = 0.5f;
- }
-
- float sink_time = total_distance / groundspeed;
- if (sink_time < 0.5f) {
- sink_time = 0.5f;
- }
-
- float sink_rate = sink_height / sink_time;
-
- float aim_height = flare_sec * sink_rate;
- if (aim_height <= 0) {
- aim_height = flare_alt;
- }
-
- if (flare_alt > 0 && aim_height > flare_alt*2) {
- aim_height = flare_alt*2;
- }
-
- bool is_first_calc = is_zero(slope);
- slope = (sink_height - aim_height) / total_distance;
- if (is_first_calc) {
- gcs().send_text(MAV_SEVERITY_INFO, "Landing glide slope %.1f degrees", (double)degrees(atanf(slope)));
- }
-
- float flare_time = aim_height / SpdHgt_Controller->get_land_sinkrate();
-
-
- float flare_distance = groundspeed * flare_time;
-
- if (flare_distance > total_distance/2) {
- flare_distance = total_distance/2;
- }
-
-
- const float land_projection = 500;
- int32_t land_bearing_cd = prev_WP_loc.get_bearing_to(next_WP_loc);
-
-
- Location loc = next_WP_loc;
- loc.offset_bearing(land_bearing_cd * 0.01f, -flare_distance);
- loc.alt += aim_height*100;
-
- loc.offset_bearing(land_bearing_cd * 0.01f, land_projection);
- loc.alt -= slope * land_projection * 100;
-
- target_altitude_offset_cm = loc.alt - prev_WP_loc.alt;
-
- float land_proportion = current_loc.line_path_proportion(prev_WP_loc, loc);
-
- set_target_altitude_proportion_fn(loc, 1.0f - land_proportion);
-
- constrain_target_altitude_location_fn(loc, prev_WP_loc);
- }
- int32_t AP_Landing::type_slope_get_target_airspeed_cm(void)
- {
-
-
- const float land_airspeed = SpdHgt_Controller->get_land_airspeed();
- int32_t target_airspeed_cm = aparm.airspeed_cruise_cm;
- switch (type_slope_stage) {
- case SLOPE_STAGE_APPROACH:
- if (land_airspeed >= 0) {
- target_airspeed_cm = land_airspeed * 100;
- }
- break;
- case SLOPE_STAGE_PREFLARE:
- case SLOPE_STAGE_FINAL:
- if (pre_flare_airspeed > 0) {
-
- target_airspeed_cm = pre_flare_airspeed * 100;
- } else if (land_airspeed >= 0) {
- target_airspeed_cm = land_airspeed * 100;
- }
- break;
- default:
- break;
- }
-
- const int32_t head_wind_compensation_cm = head_wind() * 0.5f * 100;
-
- return constrain_int32(target_airspeed_cm + head_wind_compensation_cm, target_airspeed_cm, aparm.airspeed_cruise_cm);
- }
- int32_t AP_Landing::type_slope_constrain_roll(const int32_t desired_roll_cd, const int32_t level_roll_limit_cd)
- {
- if (type_slope_stage == SLOPE_STAGE_FINAL) {
- return constrain_int32(desired_roll_cd, level_roll_limit_cd * -1, level_roll_limit_cd);
- } else {
- return desired_roll_cd;
- }
- }
- bool AP_Landing::type_slope_is_flaring(void) const
- {
- return (type_slope_stage == SLOPE_STAGE_FINAL);
- }
- bool AP_Landing::type_slope_is_on_approach(void) const
- {
- return (type_slope_stage == SLOPE_STAGE_APPROACH ||
- type_slope_stage == SLOPE_STAGE_PREFLARE);
- }
- bool AP_Landing::type_slope_is_expecting_impact(void) const
- {
- return (type_slope_stage == SLOPE_STAGE_PREFLARE ||
- type_slope_stage == SLOPE_STAGE_FINAL);
- }
- bool AP_Landing::type_slope_is_complete(void) const
- {
- return (type_slope_stage == SLOPE_STAGE_FINAL);
- }
- void AP_Landing::type_slope_log(void) const
- {
-
- AP::logger().Write("LAND", "TimeUS,stage,f1,f2,slope,slopeInit,altO", "QBBBfff",
- AP_HAL::micros64(),
- type_slope_stage,
- flags,
- type_slope_flags,
- (double)slope,
- (double)initial_slope,
- (double)alt_offset);
- }
- bool AP_Landing::type_slope_is_throttle_suppressed(void) const
- {
- return type_slope_stage == SLOPE_STAGE_FINAL;
- }
|