2015-05-29 23:12:49 -03:00
|
|
|
#include "Copter.h"
|
|
|
|
|
2019-05-09 23:18:49 -03:00
|
|
|
Mode::_TakeOff Mode::takeoff;
|
2023-09-28 02:05:51 -03:00
|
|
|
_AutoTakeoff Mode::auto_takeoff;
|
2019-11-15 16:28:08 -04:00
|
|
|
|
2015-04-29 23:52:19 -03:00
|
|
|
// This file contains the high-level takeoff logic for Loiter, PosHold, AltHold, Sport modes.
|
|
|
|
// The take-off can be initiated from a GCS NAV_TAKEOFF command which includes a takeoff altitude
|
|
|
|
// A safe takeoff speed is calculated and used to calculate a time_ms
|
|
|
|
// the pos_control target is then slowly increased until time_ms expires
|
2015-04-30 03:06:55 -03:00
|
|
|
|
2019-05-09 23:18:49 -03:00
|
|
|
bool Mode::do_user_takeoff_start(float takeoff_alt_cm)
|
2017-12-12 06:09:48 -04:00
|
|
|
{
|
2018-03-16 03:22:14 -03:00
|
|
|
copter.flightmode->takeoff.start(takeoff_alt_cm);
|
2017-12-12 06:09:48 -04:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2015-04-29 23:52:19 -03:00
|
|
|
// initiate user takeoff - called when MAVLink TAKEOFF command is received
|
2019-05-09 23:18:49 -03:00
|
|
|
bool Mode::do_user_takeoff(float takeoff_alt_cm, bool must_navigate)
|
2015-04-30 03:06:55 -03:00
|
|
|
{
|
2017-12-12 06:09:48 -04:00
|
|
|
if (!copter.motors->armed()) {
|
|
|
|
return false;
|
|
|
|
}
|
2019-05-09 23:18:49 -03:00
|
|
|
if (!copter.ap.land_complete) {
|
2017-12-12 06:09:48 -04:00
|
|
|
// can't takeoff again!
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if (!has_user_takeoff(must_navigate)) {
|
|
|
|
// this mode doesn't support user takeoff
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if (takeoff_alt_cm <= copter.current_loc.alt) {
|
|
|
|
// can't takeoff downwards...
|
|
|
|
return false;
|
|
|
|
}
|
2015-07-01 09:32:40 -03:00
|
|
|
|
2020-10-13 20:19:42 -03:00
|
|
|
// Vehicles using motor interlock should return false if motor interlock is disabled.
|
|
|
|
// Interlock must be enabled to allow the controller to spool up the motor(s) for takeoff.
|
|
|
|
if (!motors->get_interlock() && copter.ap.using_interlock) {
|
2017-12-12 06:09:48 -04:00
|
|
|
return false;
|
|
|
|
}
|
2015-07-01 09:32:40 -03:00
|
|
|
|
2017-12-12 06:09:48 -04:00
|
|
|
if (!do_user_takeoff_start(takeoff_alt_cm)) {
|
|
|
|
return false;
|
2015-04-30 03:06:55 -03:00
|
|
|
}
|
2017-12-12 06:09:48 -04:00
|
|
|
|
|
|
|
copter.set_auto_armed(true);
|
|
|
|
return true;
|
2015-04-30 03:06:55 -03:00
|
|
|
}
|
|
|
|
|
2021-05-19 11:07:38 -03:00
|
|
|
// start takeoff to specified altitude above home in centimeters
|
2019-05-09 23:18:49 -03:00
|
|
|
void Mode::_TakeOff::start(float alt_cm)
|
2015-04-30 03:06:55 -03:00
|
|
|
{
|
2015-04-29 23:52:19 -03:00
|
|
|
// initialise takeoff state
|
2018-03-16 03:22:14 -03:00
|
|
|
_running = true;
|
2021-05-13 02:40:05 -03:00
|
|
|
take_off_start_alt = copter.pos_control->get_pos_target_z_cm();
|
2021-05-19 11:07:38 -03:00
|
|
|
take_off_complete_alt = take_off_start_alt + alt_cm;
|
2015-04-30 03:06:55 -03:00
|
|
|
}
|
|
|
|
|
2015-04-30 03:52:32 -03:00
|
|
|
// stop takeoff
|
2019-05-09 23:18:49 -03:00
|
|
|
void Mode::_TakeOff::stop()
|
2015-04-30 03:52:32 -03:00
|
|
|
{
|
2018-03-16 03:22:14 -03:00
|
|
|
_running = false;
|
2022-08-26 22:09:22 -03:00
|
|
|
// Check if we have progressed far enough through the takeoff process that the
|
|
|
|
// aircraft may have left the ground but not yet detected the climb.
|
|
|
|
if (copter.attitude_control->get_throttle_in() > copter.get_non_takeoff_throttle()) {
|
|
|
|
copter.set_land_complete(false);
|
|
|
|
}
|
2015-04-30 03:52:32 -03:00
|
|
|
}
|
|
|
|
|
2021-05-19 11:07:38 -03:00
|
|
|
// do_pilot_takeoff - controls the vertical position controller during the process of taking off
|
|
|
|
// take off is complete when the vertical target reaches the take off altitude.
|
2021-05-13 02:40:05 -03:00
|
|
|
// climb is cancelled if pilot_climb_rate_cm becomes negative
|
|
|
|
// sets take off to complete when target altitude is within 1% of the take off altitude
|
|
|
|
void Mode::_TakeOff::do_pilot_takeoff(float& pilot_climb_rate_cm)
|
2015-04-30 03:06:55 -03:00
|
|
|
{
|
2015-04-30 04:40:38 -03:00
|
|
|
// return pilot_climb_rate if take-off inactive
|
2018-03-16 03:22:14 -03:00
|
|
|
if (!_running) {
|
2015-04-30 04:40:38 -03:00
|
|
|
return;
|
2015-04-30 03:06:55 -03:00
|
|
|
}
|
|
|
|
|
2022-08-26 22:09:22 -03:00
|
|
|
if (copter.ap.land_complete) {
|
|
|
|
// send throttle to attitude controller with angle boost
|
|
|
|
float throttle = constrain_float(copter.attitude_control->get_throttle_in() + copter.G_Dt / copter.g2.takeoff_throttle_slew_time, 0.0, 1.0);
|
|
|
|
copter.attitude_control->set_throttle_out(throttle, true, 0.0);
|
|
|
|
// tell position controller to reset alt target and reset I terms
|
|
|
|
copter.pos_control->init_z_controller();
|
2023-03-20 21:31:39 -03:00
|
|
|
if (throttle >= MIN(copter.g2.takeoff_throttle_max, 0.9) ||
|
2022-08-26 22:09:22 -03:00
|
|
|
(copter.pos_control->get_z_accel_cmss() >= 0.5 * copter.pos_control->get_max_accel_z_cmss()) ||
|
|
|
|
(copter.pos_control->get_vel_desired_cms().z >= constrain_float(pilot_climb_rate_cm, copter.pos_control->get_max_speed_up_cms() * 0.1, copter.pos_control->get_max_speed_up_cms() * 0.5)) ||
|
|
|
|
(is_positive(take_off_complete_alt - take_off_start_alt) && copter.pos_control->get_pos_target_z_cm() - take_off_start_alt > 0.5 * (take_off_complete_alt - take_off_start_alt))) {
|
|
|
|
// throttle > 90%
|
|
|
|
// acceleration > 50% maximum acceleration
|
|
|
|
// velocity > 10% maximum velocity && commanded climb rate
|
|
|
|
// velocity > 50% maximum velocity
|
|
|
|
// altitude change greater than half complete alt from start off alt
|
|
|
|
copter.set_land_complete(false);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
float pos_z = take_off_complete_alt;
|
|
|
|
float vel_z = pilot_climb_rate_cm;
|
2015-04-30 03:06:55 -03:00
|
|
|
|
2022-08-26 22:09:22 -03:00
|
|
|
// command the aircraft to the take off altitude and current pilot climb rate
|
|
|
|
copter.pos_control->input_pos_vel_accel_z(pos_z, vel_z, 0);
|
2015-04-30 04:40:38 -03:00
|
|
|
|
2022-08-26 22:09:22 -03:00
|
|
|
// stop take off early and return if negative climb rate is commanded or we are within 0.1% of our take off altitude
|
|
|
|
if (is_negative(pilot_climb_rate_cm) ||
|
|
|
|
(take_off_complete_alt - take_off_start_alt) * 0.999f < copter.pos_control->get_pos_target_z_cm() - take_off_start_alt) {
|
|
|
|
stop();
|
|
|
|
}
|
2015-04-30 04:40:38 -03:00
|
|
|
}
|
2015-04-30 03:06:55 -03:00
|
|
|
}
|
2016-06-04 23:37:55 -03:00
|
|
|
|
2022-02-10 23:05:43 -04:00
|
|
|
// auto_takeoff_run - controls the vertical position controller during the process of taking off in auto modes
|
|
|
|
// auto_takeoff_complete set to true when target altitude is within 10% of the take off altitude and less than 50% max climb rate
|
2023-09-28 02:05:51 -03:00
|
|
|
void _AutoTakeoff::run()
|
2019-11-15 16:28:08 -04:00
|
|
|
{
|
2023-09-28 02:05:51 -03:00
|
|
|
const auto &g2 = copter.g2;
|
|
|
|
const auto &inertial_nav = copter.inertial_nav;
|
|
|
|
const auto &wp_nav = copter.wp_nav;
|
|
|
|
auto *motors = copter.motors;
|
|
|
|
auto *pos_control = copter.pos_control;
|
|
|
|
auto *attitude_control = copter.attitude_control;
|
|
|
|
|
2019-11-15 16:28:08 -04:00
|
|
|
// if not armed set throttle to zero and exit immediately
|
|
|
|
if (!motors->armed() || !copter.ap.auto_armed) {
|
2020-10-13 20:19:42 -03:00
|
|
|
// do not spool down tradheli when on the ground with motor interlock enabled
|
2023-09-28 02:05:51 -03:00
|
|
|
copter.flightmode->make_safe_ground_handling(copter.is_tradheli() && motors->get_interlock());
|
2022-12-31 21:50:08 -04:00
|
|
|
// update auto_takeoff_no_nav_alt_cm
|
2023-09-28 02:05:51 -03:00
|
|
|
no_nav_alt_cm = inertial_nav.get_position_z_up_cm() + g2.wp_navalt_min * 100;
|
2022-02-10 23:05:43 -04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// get terrain offset
|
|
|
|
float terr_offset = 0.0f;
|
2023-09-28 02:05:51 -03:00
|
|
|
if (terrain_alt && !wp_nav->get_terrain_offset(terr_offset)) {
|
2022-08-19 04:52:05 -03:00
|
|
|
// trigger terrain failsafe
|
|
|
|
copter.failsafe_terrain_on_event();
|
2019-11-15 16:28:08 -04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-11-28 09:19:17 -04:00
|
|
|
// set motors to full range
|
|
|
|
motors->set_desired_spool_state(AP_Motors::DesiredSpoolState::THROTTLE_UNLIMITED);
|
|
|
|
|
2022-02-10 23:05:43 -04:00
|
|
|
// aircraft stays in landed state until rotor speed run up has finished
|
2022-08-26 22:09:22 -03:00
|
|
|
if (motors->get_spool_state() != AP_Motors::SpoolState::THROTTLE_UNLIMITED) {
|
2020-09-30 08:05:37 -03:00
|
|
|
// motors have not completed spool up yet so relax navigation and position controllers
|
2022-02-10 23:05:43 -04:00
|
|
|
pos_control->relax_velocity_controller_xy();
|
|
|
|
pos_control->update_xy_controller();
|
2021-05-19 11:07:38 -03:00
|
|
|
pos_control->relax_z_controller(0.0f); // forces throttle output to decay to zero
|
2020-09-30 08:05:37 -03:00
|
|
|
pos_control->update_z_controller();
|
2021-05-24 10:42:19 -03:00
|
|
|
attitude_control->reset_yaw_target_and_rate();
|
2020-09-30 08:05:37 -03:00
|
|
|
attitude_control->reset_rate_controller_I_terms();
|
2022-08-26 22:09:22 -03:00
|
|
|
attitude_control->input_thrust_vector_rate_heading(pos_control->get_thrust_vector(), 0.0);
|
2022-12-31 21:50:08 -04:00
|
|
|
// update auto_takeoff_no_nav_alt_cm
|
2023-09-28 02:05:51 -03:00
|
|
|
no_nav_alt_cm = inertial_nav.get_position_z_up_cm() + g2.wp_navalt_min * 100;
|
2022-08-26 22:09:22 -03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// aircraft stays in landed state until vertical movement is detected or 90% throttle is reached
|
|
|
|
if (copter.ap.land_complete) {
|
|
|
|
// send throttle to attitude controller with angle boost
|
|
|
|
float throttle = constrain_float(copter.attitude_control->get_throttle_in() + copter.G_Dt / copter.g2.takeoff_throttle_slew_time, 0.0, 1.0);
|
|
|
|
copter.attitude_control->set_throttle_out(throttle, true, 0.0);
|
|
|
|
// tell position controller to reset alt target and reset I terms
|
|
|
|
copter.pos_control->init_z_controller();
|
|
|
|
pos_control->relax_velocity_controller_xy();
|
|
|
|
pos_control->update_xy_controller();
|
|
|
|
attitude_control->reset_rate_controller_I_terms();
|
|
|
|
attitude_control->input_thrust_vector_rate_heading(pos_control->get_thrust_vector(), 0.0);
|
2023-03-20 21:31:39 -03:00
|
|
|
if (throttle >= MIN(copter.g2.takeoff_throttle_max, 0.9) ||
|
2022-08-26 22:09:22 -03:00
|
|
|
(copter.pos_control->get_z_accel_cmss() >= 0.5 * copter.pos_control->get_max_accel_z_cmss()) ||
|
|
|
|
(copter.pos_control->get_vel_desired_cms().z >= 0.1 * copter.pos_control->get_max_speed_up_cms()) ||
|
2023-09-28 02:05:51 -03:00
|
|
|
( no_nav_active && (inertial_nav.get_position_z_up_cm() >= no_nav_alt_cm))) {
|
2022-08-26 22:09:22 -03:00
|
|
|
// throttle > 90%
|
|
|
|
// acceleration > 50% maximum acceleration
|
|
|
|
// velocity > 10% maximum velocity
|
|
|
|
// altitude change greater than half auto_takeoff_no_nav_alt_cm
|
|
|
|
copter.set_land_complete(false);
|
|
|
|
}
|
2020-09-30 08:05:37 -03:00
|
|
|
return;
|
2019-11-15 16:28:08 -04:00
|
|
|
}
|
|
|
|
|
2019-11-28 09:19:17 -04:00
|
|
|
// check if we are not navigating because of low altitude
|
2023-09-28 02:05:51 -03:00
|
|
|
if (no_nav_active) {
|
2019-11-28 09:19:17 -04:00
|
|
|
// check if vehicle has reached no_nav_alt threshold
|
2023-09-28 02:05:51 -03:00
|
|
|
if (inertial_nav.get_position_z_up_cm() >= no_nav_alt_cm) {
|
|
|
|
no_nav_active = false;
|
2019-11-28 09:19:17 -04:00
|
|
|
}
|
2022-02-10 23:05:43 -04:00
|
|
|
pos_control->relax_velocity_controller_xy();
|
|
|
|
} else {
|
|
|
|
Vector2f vel;
|
|
|
|
Vector2f accel;
|
|
|
|
pos_control->input_vel_accel_xy(vel, accel);
|
2019-11-28 09:19:17 -04:00
|
|
|
}
|
2022-02-10 23:05:43 -04:00
|
|
|
pos_control->update_xy_controller();
|
2019-11-28 09:19:17 -04:00
|
|
|
|
2022-02-10 23:05:43 -04:00
|
|
|
// command the aircraft to the take off altitude
|
2023-09-28 02:05:51 -03:00
|
|
|
float pos_z = complete_alt_cm + terr_offset;
|
2022-02-10 23:05:43 -04:00
|
|
|
float vel_z = 0.0;
|
|
|
|
copter.pos_control->input_pos_vel_accel_z(pos_z, vel_z, 0.0);
|
|
|
|
|
2021-05-19 11:07:38 -03:00
|
|
|
// run the vertical position controller and set output throttle
|
2022-02-10 23:05:43 -04:00
|
|
|
pos_control->update_z_controller();
|
2019-11-15 16:28:08 -04:00
|
|
|
|
2022-09-24 11:12:33 -03:00
|
|
|
// call attitude controller with auto yaw
|
2023-09-28 02:05:51 -03:00
|
|
|
attitude_control->input_thrust_vector_heading(pos_control->get_thrust_vector(), copter.flightmode->auto_yaw.get_heading());
|
2022-02-10 23:05:43 -04:00
|
|
|
|
2022-12-29 01:35:26 -04:00
|
|
|
// takeoff complete when we are less than 1% of the stopping distance from the target altitude
|
|
|
|
// and 10% our maximum climb rate
|
|
|
|
const float vel_threshold_fraction = 0.1;
|
|
|
|
// stopping distance from vel_threshold_fraction * max velocity
|
|
|
|
const float stop_distance = 0.5 * sq(vel_threshold_fraction * copter.pos_control->get_max_speed_up_cms()) / copter.pos_control->get_max_accel_z_cmss();
|
|
|
|
bool reached_altitude = copter.pos_control->get_pos_target_z_cm() >= pos_z - stop_distance;
|
|
|
|
bool reached_climb_rate = copter.pos_control->get_vel_desired_cms().z < copter.pos_control->get_max_speed_up_cms() * vel_threshold_fraction;
|
2023-09-28 02:05:51 -03:00
|
|
|
complete = reached_altitude && reached_climb_rate;
|
2022-02-10 23:05:43 -04:00
|
|
|
|
|
|
|
// calculate completion for location in case it is needed for a smooth transition to wp_nav
|
2023-09-28 02:05:51 -03:00
|
|
|
if (complete) {
|
|
|
|
const Vector3p& _complete_pos = copter.pos_control->get_pos_target_cm();
|
|
|
|
complete_pos = Vector3p{_complete_pos.x, _complete_pos.y, pos_z};
|
2021-09-17 06:52:35 -03:00
|
|
|
}
|
2019-11-15 16:28:08 -04:00
|
|
|
}
|
|
|
|
|
2023-09-28 02:05:51 -03:00
|
|
|
void _AutoTakeoff::start(float _complete_alt_cm, bool _terrain_alt)
|
2016-06-04 23:37:55 -03:00
|
|
|
{
|
2022-12-29 01:35:26 -04:00
|
|
|
// auto_takeoff_complete_alt_cm is a problem if equal to auto_takeoff_start_alt_cm
|
2023-09-28 02:05:51 -03:00
|
|
|
complete_alt_cm = _complete_alt_cm;
|
|
|
|
terrain_alt = _terrain_alt;
|
|
|
|
complete = false;
|
2022-12-31 21:50:08 -04:00
|
|
|
// initialise auto_takeoff_no_nav_alt_cm
|
2023-09-28 02:05:51 -03:00
|
|
|
const auto &g2 = copter.g2;
|
|
|
|
const auto &inertial_nav = copter.inertial_nav;
|
|
|
|
no_nav_alt_cm = inertial_nav.get_position_z_up_cm() + g2.wp_navalt_min * 100;
|
|
|
|
if ((g2.wp_navalt_min > 0) && (copter.flightmode->is_disarmed_or_landed() || !copter.motors->get_interlock())) {
|
2019-11-28 09:19:17 -04:00
|
|
|
// we are not flying, climb with no navigation to current alt-above-ekf-origin + wp_navalt_min
|
2023-09-28 02:05:51 -03:00
|
|
|
no_nav_active = true;
|
2016-06-04 23:37:55 -03:00
|
|
|
} else {
|
2023-09-28 02:05:51 -03:00
|
|
|
no_nav_active = false;
|
2016-06-04 23:37:55 -03:00
|
|
|
}
|
|
|
|
}
|
2018-04-30 06:50:04 -03:00
|
|
|
|
2024-09-10 09:12:31 -03:00
|
|
|
// return takeoff final target position in cm from the EKF origin if takeoff has completed successfully
|
|
|
|
bool _AutoTakeoff::get_completion_pos(Vector3p& pos_neu_cm)
|
2022-02-10 23:05:43 -04:00
|
|
|
{
|
|
|
|
// only provide location if takeoff has completed
|
2023-09-28 02:05:51 -03:00
|
|
|
if (!complete) {
|
2022-02-10 23:05:43 -04:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2024-09-10 09:12:31 -03:00
|
|
|
pos_neu_cm = complete_pos;
|
2022-02-10 23:05:43 -04:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2019-05-09 23:18:49 -03:00
|
|
|
bool Mode::is_taking_off() const
|
2018-04-30 06:50:04 -03:00
|
|
|
{
|
|
|
|
if (!has_user_takeoff(false)) {
|
|
|
|
return false;
|
|
|
|
}
|
2021-02-03 10:04:38 -04:00
|
|
|
return takeoff.running();
|
2018-04-30 06:50:04 -03:00
|
|
|
}
|