2014-01-24 02:47:42 -04:00
|
|
|
/// -*- tab-width: 4; Mode: C++; c-basic-offset: 4; indent-tabs-mode: nil -*-
|
|
|
|
|
2015-05-29 23:12:49 -03:00
|
|
|
#include "Copter.h"
|
|
|
|
|
2014-01-24 02:47:42 -04:00
|
|
|
static bool land_with_gps;
|
|
|
|
|
2014-07-06 06:16:27 -03:00
|
|
|
static uint32_t land_start_time;
|
|
|
|
static bool land_pause;
|
|
|
|
|
2014-01-24 02:47:42 -04:00
|
|
|
// land_init - initialise land controller
|
2015-05-29 23:12:49 -03:00
|
|
|
bool Copter::land_init(bool ignore_checks)
|
2014-01-24 02:47:42 -04:00
|
|
|
{
|
|
|
|
// check if we have GPS and decide which LAND we're going to do
|
2015-01-02 07:43:50 -04:00
|
|
|
land_with_gps = position_ok();
|
2014-01-24 02:47:42 -04:00
|
|
|
if (land_with_gps) {
|
2014-01-25 00:41:17 -04:00
|
|
|
// set target to stopping point
|
|
|
|
Vector3f stopping_point;
|
|
|
|
wp_nav.get_loiter_stopping_point_xy(stopping_point);
|
2014-05-15 10:19:18 -03:00
|
|
|
wp_nav.init_loiter_target(stopping_point);
|
2014-01-24 02:47:42 -04:00
|
|
|
}
|
2014-02-15 04:37:24 -04:00
|
|
|
|
2014-04-29 23:17:59 -03:00
|
|
|
// initialize vertical speeds and leash lengths
|
|
|
|
pos_control.set_speed_z(wp_nav.get_speed_down(), wp_nav.get_speed_up());
|
|
|
|
pos_control.set_accel_z(wp_nav.get_accel_z());
|
2014-04-30 04:29:32 -03:00
|
|
|
|
|
|
|
// initialise altitude target to stopping point
|
|
|
|
pos_control.set_target_to_stopping_point_z();
|
|
|
|
|
2014-07-06 06:16:27 -03:00
|
|
|
land_start_time = millis();
|
|
|
|
|
|
|
|
land_pause = false;
|
|
|
|
|
2014-01-24 02:47:42 -04:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
// land_run - runs the land controller
|
|
|
|
// should be called at 100hz or more
|
2015-05-29 23:12:49 -03:00
|
|
|
void Copter::land_run()
|
2014-01-24 02:47:42 -04:00
|
|
|
{
|
2014-01-24 03:23:33 -04:00
|
|
|
if (land_with_gps) {
|
|
|
|
land_gps_run();
|
|
|
|
}else{
|
|
|
|
land_nogps_run();
|
|
|
|
}
|
|
|
|
}
|
2014-01-24 02:47:42 -04:00
|
|
|
|
2014-01-24 03:23:33 -04:00
|
|
|
// land_run - runs the land controller
|
|
|
|
// horizontal position controlled with loiter controller
|
|
|
|
// should be called at 100hz or more
|
2015-05-29 23:12:49 -03:00
|
|
|
void Copter::land_gps_run()
|
2014-01-24 03:23:33 -04:00
|
|
|
{
|
|
|
|
int16_t roll_control = 0, pitch_control = 0;
|
|
|
|
float target_yaw_rate = 0;
|
|
|
|
|
2015-04-17 14:49:08 -03:00
|
|
|
// if not auto armed or landed or motor interlock not enabled set throttle to zero and exit immediately
|
|
|
|
if(!ap.auto_armed || ap.land_complete || !motors.get_interlock()) {
|
2015-07-01 15:38:32 -03:00
|
|
|
#if FRAME_CONFIG == HELI_FRAME // Helicopters always stabilize roll/pitch/yaw
|
|
|
|
// call attitude controller
|
|
|
|
attitude_control.angle_ef_roll_pitch_rate_ef_yaw_smooth(0, 0, 0, get_smoothing_gain());
|
|
|
|
attitude_control.set_throttle_out(0,false,g.throttle_filt);
|
2015-07-15 07:47:46 -03:00
|
|
|
#else // multicopters do not stabilize roll/pitch/yaw when disarmed
|
2015-04-16 01:54:29 -03:00
|
|
|
attitude_control.set_throttle_out_unstabilized(0,true,g.throttle_filt);
|
2015-07-01 15:38:32 -03:00
|
|
|
#endif
|
2014-01-24 03:23:33 -04:00
|
|
|
wp_nav.init_loiter_target();
|
2014-01-24 02:47:42 -04:00
|
|
|
|
|
|
|
#if LAND_REQUIRE_MIN_THROTTLE_TO_DISARM == ENABLED
|
2014-01-24 03:23:33 -04:00
|
|
|
// disarm when the landing detector says we've landed and throttle is at minimum
|
2014-10-07 12:29:24 -03:00
|
|
|
if (ap.land_complete && (ap.throttle_zero || failsafe.radio)) {
|
2014-01-24 03:23:33 -04:00
|
|
|
init_disarm_motors();
|
|
|
|
}
|
2014-01-24 02:47:42 -04:00
|
|
|
#else
|
2014-01-24 03:23:33 -04:00
|
|
|
// disarm when the landing detector says we've landed
|
|
|
|
if (ap.land_complete) {
|
|
|
|
init_disarm_motors();
|
|
|
|
}
|
|
|
|
#endif
|
2014-01-24 02:47:42 -04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2014-08-29 03:25:37 -03:00
|
|
|
// relax loiter target if we might be landed
|
2015-05-06 01:38:38 -03:00
|
|
|
if (ap.land_complete_maybe) {
|
2014-08-29 03:25:37 -03:00
|
|
|
wp_nav.loiter_soften_for_landing();
|
|
|
|
}
|
|
|
|
|
2014-01-24 03:23:33 -04:00
|
|
|
// process pilot inputs
|
|
|
|
if (!failsafe.radio) {
|
2014-07-06 06:05:43 -03:00
|
|
|
if (g.land_repositioning) {
|
|
|
|
// apply SIMPLE mode transform to pilot inputs
|
|
|
|
update_simple_mode();
|
2014-01-24 03:23:33 -04:00
|
|
|
|
2014-07-06 06:05:43 -03:00
|
|
|
// process pilot's roll and pitch input
|
2015-05-11 23:24:13 -03:00
|
|
|
roll_control = channel_roll->control_in;
|
|
|
|
pitch_control = channel_pitch->control_in;
|
2014-07-06 06:05:43 -03:00
|
|
|
}
|
2014-01-24 03:23:33 -04:00
|
|
|
|
|
|
|
// get pilot's desired yaw rate
|
2015-05-11 23:24:13 -03:00
|
|
|
target_yaw_rate = get_pilot_desired_yaw_rate(channel_yaw->control_in);
|
2014-01-24 03:23:33 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// process roll, pitch inputs
|
|
|
|
wp_nav.set_pilot_desired_acceleration(roll_control, pitch_control);
|
2014-01-24 02:47:42 -04:00
|
|
|
|
2014-01-24 03:23:33 -04:00
|
|
|
// run loiter controller
|
2014-11-15 23:06:05 -04:00
|
|
|
wp_nav.update_loiter(ekfGndSpdLimit, ekfNavVelGainScaler);
|
2014-01-24 02:47:42 -04:00
|
|
|
|
2014-01-24 03:23:33 -04:00
|
|
|
// call attitude controller
|
2014-02-13 22:56:46 -04:00
|
|
|
attitude_control.angle_ef_roll_pitch_rate_ef_yaw(wp_nav.get_roll(), wp_nav.get_pitch(), target_yaw_rate);
|
2014-01-24 02:47:42 -04:00
|
|
|
|
2014-12-18 03:13:48 -04:00
|
|
|
// pause 4 seconds before beginning land descent
|
2014-07-06 06:16:27 -03:00
|
|
|
float cmb_rate;
|
|
|
|
if(land_pause && millis()-land_start_time < 4000) {
|
|
|
|
cmb_rate = 0;
|
|
|
|
} else {
|
|
|
|
land_pause = false;
|
2014-12-18 00:11:28 -04:00
|
|
|
cmb_rate = get_land_descent_speed();
|
2014-07-06 06:16:27 -03:00
|
|
|
}
|
|
|
|
|
2014-12-18 03:13:48 -04:00
|
|
|
// record desired climb rate for logging
|
2014-12-18 00:11:28 -04:00
|
|
|
desired_climb_rate = cmb_rate;
|
|
|
|
|
2014-01-24 03:23:33 -04:00
|
|
|
// update altitude target and call position controller
|
2014-11-13 20:57:25 -04:00
|
|
|
pos_control.set_alt_target_from_climb_rate(cmb_rate, G_Dt, true);
|
2014-01-24 03:23:33 -04:00
|
|
|
pos_control.update_z_controller();
|
|
|
|
}
|
|
|
|
|
|
|
|
// land_nogps_run - runs the land controller
|
|
|
|
// pilot controls roll and pitch angles
|
|
|
|
// should be called at 100hz or more
|
2015-05-29 23:12:49 -03:00
|
|
|
void Copter::land_nogps_run()
|
2014-01-24 03:23:33 -04:00
|
|
|
{
|
2014-12-03 21:25:42 -04:00
|
|
|
float target_roll = 0.0f, target_pitch = 0.0f;
|
2014-01-24 03:23:33 -04:00
|
|
|
float target_yaw_rate = 0;
|
|
|
|
|
2015-07-01 15:38:32 -03:00
|
|
|
// process pilot inputs
|
|
|
|
if (!failsafe.radio) {
|
|
|
|
if (g.land_repositioning) {
|
|
|
|
// apply SIMPLE mode transform to pilot inputs
|
|
|
|
update_simple_mode();
|
|
|
|
|
|
|
|
// get pilot desired lean angles
|
|
|
|
get_pilot_desired_lean_angles(channel_roll->control_in, channel_pitch->control_in, target_roll, target_pitch);
|
|
|
|
}
|
|
|
|
|
|
|
|
// get pilot's desired yaw rate
|
|
|
|
target_yaw_rate = get_pilot_desired_yaw_rate(channel_yaw->control_in);
|
|
|
|
}
|
|
|
|
|
2015-04-17 14:49:08 -03:00
|
|
|
// if not auto armed or landed or motor interlock not enabled set throttle to zero and exit immediately
|
|
|
|
if(!ap.auto_armed || ap.land_complete || !motors.get_interlock()) {
|
2015-07-01 15:38:32 -03:00
|
|
|
#if FRAME_CONFIG == HELI_FRAME // Helicopters always stabilize roll/pitch/yaw
|
|
|
|
// call attitude controller
|
|
|
|
attitude_control.angle_ef_roll_pitch_rate_ef_yaw_smooth(target_roll, target_pitch, target_yaw_rate, get_smoothing_gain());
|
|
|
|
attitude_control.set_throttle_out(0,false,g.throttle_filt);
|
2015-07-15 07:47:46 -03:00
|
|
|
#else // multicopters do not stabilize roll/pitch/yaw when disarmed
|
2015-04-16 01:54:29 -03:00
|
|
|
attitude_control.set_throttle_out_unstabilized(0,true,g.throttle_filt);
|
2015-07-01 15:38:32 -03:00
|
|
|
#endif
|
|
|
|
|
2014-01-24 03:23:33 -04:00
|
|
|
#if LAND_REQUIRE_MIN_THROTTLE_TO_DISARM == ENABLED
|
|
|
|
// disarm when the landing detector says we've landed and throttle is at minimum
|
2014-10-07 12:29:24 -03:00
|
|
|
if (ap.land_complete && (ap.throttle_zero || failsafe.radio)) {
|
2014-01-24 03:23:33 -04:00
|
|
|
init_disarm_motors();
|
|
|
|
}
|
|
|
|
#else
|
|
|
|
// disarm when the landing detector says we've landed
|
|
|
|
if (ap.land_complete) {
|
|
|
|
init_disarm_motors();
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// call attitude controller
|
2014-02-19 07:52:00 -04:00
|
|
|
attitude_control.angle_ef_roll_pitch_rate_ef_yaw_smooth(target_roll, target_pitch, target_yaw_rate, get_smoothing_gain());
|
2014-01-24 03:23:33 -04:00
|
|
|
|
2014-12-18 03:13:48 -04:00
|
|
|
// pause 4 seconds before beginning land descent
|
2014-07-06 06:16:27 -03:00
|
|
|
float cmb_rate;
|
|
|
|
if(land_pause && millis()-land_start_time < LAND_WITH_DELAY_MS) {
|
|
|
|
cmb_rate = 0;
|
|
|
|
} else {
|
|
|
|
land_pause = false;
|
2014-12-18 00:11:28 -04:00
|
|
|
cmb_rate = get_land_descent_speed();
|
2014-07-06 06:16:27 -03:00
|
|
|
}
|
|
|
|
|
2014-12-18 03:13:48 -04:00
|
|
|
// record desired climb rate for logging
|
2014-12-18 00:11:28 -04:00
|
|
|
desired_climb_rate = cmb_rate;
|
|
|
|
|
2014-01-24 03:23:33 -04:00
|
|
|
// call position controller
|
2014-11-13 20:57:25 -04:00
|
|
|
pos_control.set_alt_target_from_climb_rate(cmb_rate, G_Dt, true);
|
2014-01-24 03:23:33 -04:00
|
|
|
pos_control.update_z_controller();
|
2014-01-24 02:47:42 -04:00
|
|
|
}
|
|
|
|
|
2014-12-18 00:11:28 -04:00
|
|
|
// get_land_descent_speed - high level landing logic
|
2014-01-24 02:47:42 -04:00
|
|
|
// returns climb rate (in cm/s) which should be passed to the position controller
|
|
|
|
// should be called at 100hz or higher
|
2015-05-29 23:12:49 -03:00
|
|
|
float Copter::get_land_descent_speed()
|
2014-01-24 02:47:42 -04:00
|
|
|
{
|
2014-07-08 02:41:39 -03:00
|
|
|
#if CONFIG_SONAR == ENABLED
|
2015-04-17 03:37:29 -03:00
|
|
|
bool sonar_ok = sonar_enabled && (sonar.status() == RangeFinder::RangeFinder_Good);
|
2014-07-08 02:41:39 -03:00
|
|
|
#else
|
|
|
|
bool sonar_ok = false;
|
|
|
|
#endif
|
2014-01-24 02:47:42 -04:00
|
|
|
// if we are above 10m and the sonar does not sense anything perform regular alt hold descent
|
2015-02-10 08:33:07 -04:00
|
|
|
if (pos_control.get_pos_target().z >= pv_alt_above_origin(LAND_START_ALT) && !(sonar_ok && sonar_alt_health >= SONAR_ALT_HEALTH_MAX)) {
|
2014-01-24 02:47:42 -04:00
|
|
|
return pos_control.get_speed_down();
|
|
|
|
}else{
|
|
|
|
return -abs(g.land_speed);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-04-27 03:08:58 -03:00
|
|
|
// land_do_not_use_GPS - forces land-mode to not use the GPS but instead rely on pilot input for roll and pitch
|
|
|
|
// called during GPS failsafe to ensure that if we were already in LAND mode that we do not use the GPS
|
|
|
|
// has no effect if we are not already in LAND mode
|
2015-05-29 23:12:49 -03:00
|
|
|
void Copter::land_do_not_use_GPS()
|
2014-04-27 03:08:58 -03:00
|
|
|
{
|
|
|
|
land_with_gps = false;
|
|
|
|
}
|
2014-07-06 06:16:27 -03:00
|
|
|
|
|
|
|
// set_mode_land_with_pause - sets mode to LAND and triggers 4 second delay before descent starts
|
2014-12-14 23:48:54 -04:00
|
|
|
// this is always called from a failsafe so we trigger notification to pilot
|
2015-05-29 23:12:49 -03:00
|
|
|
void Copter::set_mode_land_with_pause()
|
2014-07-06 06:16:27 -03:00
|
|
|
{
|
|
|
|
set_mode(LAND);
|
|
|
|
land_pause = true;
|
2014-12-14 23:48:54 -04:00
|
|
|
|
|
|
|
// alert pilot to mode change
|
|
|
|
AP_Notify::events.failsafe_mode_change = 1;
|
2014-07-06 06:16:27 -03:00
|
|
|
}
|
2014-09-15 21:16:41 -03:00
|
|
|
|
|
|
|
// landing_with_GPS - returns true if vehicle is landing using GPS
|
2015-05-29 23:12:49 -03:00
|
|
|
bool Copter::landing_with_GPS() {
|
2014-09-15 21:16:41 -03:00
|
|
|
return (control_mode == LAND && land_with_gps);
|
|
|
|
}
|