2015-05-29 23:12:49 -03:00
|
|
|
#include "Copter.h"
|
|
|
|
|
2019-02-25 10:32:52 -04:00
|
|
|
#if MODE_BRAKE_ENABLED == ENABLED
|
|
|
|
|
2015-04-22 18:10:10 -03:00
|
|
|
/*
|
2016-07-25 15:45:29 -03:00
|
|
|
* Init and run calls for brake flight mode
|
2015-04-22 18:10:10 -03:00
|
|
|
*/
|
|
|
|
|
2015-05-17 00:22:20 -03:00
|
|
|
// brake_init - initialise brake controller
|
2017-12-10 23:51:13 -04:00
|
|
|
bool Copter::ModeBrake::init(bool ignore_checks)
|
2015-04-22 18:10:10 -03:00
|
|
|
{
|
2019-02-28 20:52:28 -04:00
|
|
|
// set target to current position
|
|
|
|
wp_nav->init_brake_target(BRAKE_MODE_DECEL_RATE);
|
2015-04-22 18:10:10 -03:00
|
|
|
|
2019-02-28 20:52:28 -04:00
|
|
|
// initialize vertical speed and acceleration
|
|
|
|
pos_control->set_max_speed_z(BRAKE_MODE_SPEED_Z, BRAKE_MODE_SPEED_Z);
|
|
|
|
pos_control->set_max_accel_z(BRAKE_MODE_DECEL_RATE);
|
2015-04-22 18:10:10 -03:00
|
|
|
|
2019-02-28 20:52:28 -04:00
|
|
|
// initialise position and desired velocity
|
|
|
|
if (!pos_control->is_active_z()) {
|
|
|
|
pos_control->set_alt_target_to_current_alt();
|
|
|
|
pos_control->set_desired_velocity_z(inertial_nav.get_velocity_z());
|
|
|
|
}
|
2015-04-22 18:10:10 -03:00
|
|
|
|
2019-02-28 20:52:28 -04:00
|
|
|
_timeout_ms = 0;
|
2016-01-05 02:12:25 -04:00
|
|
|
|
2019-02-28 20:52:28 -04:00
|
|
|
return true;
|
2015-04-22 18:10:10 -03:00
|
|
|
}
|
|
|
|
|
2015-05-17 00:22:20 -03:00
|
|
|
// brake_run - runs the brake controller
|
2015-04-22 18:10:10 -03:00
|
|
|
// should be called at 100hz or more
|
2017-12-10 23:51:13 -04:00
|
|
|
void Copter::ModeBrake::run()
|
2015-04-22 18:10:10 -03:00
|
|
|
{
|
2019-02-28 05:03:23 -04:00
|
|
|
// if not armed set throttle to zero and exit immediately
|
2019-04-08 05:15:57 -03:00
|
|
|
if (is_disarmed_or_landed()) {
|
2019-03-14 20:45:00 -03:00
|
|
|
make_safe_spool_down();
|
2019-02-28 02:16:19 -04:00
|
|
|
wp_nav->init_brake_target(BRAKE_MODE_DECEL_RATE);
|
2015-04-22 18:10:10 -03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-02-28 05:03:23 -04:00
|
|
|
// set motors to full range
|
2019-04-09 09:16:58 -03:00
|
|
|
motors->set_desired_spool_state(AP_Motors::DesiredSpoolState::THROTTLE_UNLIMITED);
|
2019-02-28 05:03:23 -04:00
|
|
|
|
2015-04-22 18:10:10 -03:00
|
|
|
// relax stop target if we might be landed
|
2015-05-06 01:38:38 -03:00
|
|
|
if (ap.land_complete_maybe) {
|
2018-03-27 23:13:37 -03:00
|
|
|
loiter_nav->soften_for_landing();
|
2015-05-17 02:28:55 -03:00
|
|
|
}
|
|
|
|
|
2015-05-17 00:22:20 -03:00
|
|
|
// run brake controller
|
2018-09-05 03:47:50 -03:00
|
|
|
wp_nav->update_brake();
|
2015-04-22 18:10:10 -03:00
|
|
|
|
|
|
|
// call attitude controller
|
2017-06-26 05:48:04 -03:00
|
|
|
attitude_control->input_euler_angle_roll_pitch_euler_rate_yaw(wp_nav->get_roll(), wp_nav->get_pitch(), 0.0f);
|
2015-04-22 18:10:10 -03:00
|
|
|
|
|
|
|
// update altitude target and call position controller
|
2019-02-28 05:03:00 -04:00
|
|
|
// protects heli's from inflight motor interlock disable
|
2019-04-09 09:16:58 -03:00
|
|
|
if (motors->get_desired_spool_state() == AP_Motors::DesiredSpoolState::GROUND_IDLE && !ap.land_complete) {
|
2019-02-28 05:03:00 -04:00
|
|
|
pos_control->set_alt_target_from_climb_rate(-abs(g.land_speed), G_Dt, false);
|
|
|
|
} else {
|
|
|
|
pos_control->set_alt_target_from_climb_rate_ff(0.0f, G_Dt, false);
|
|
|
|
}
|
2017-01-09 03:31:26 -04:00
|
|
|
pos_control->update_z_controller();
|
2016-01-05 02:12:25 -04:00
|
|
|
|
2016-03-22 22:00:19 -03:00
|
|
|
if (_timeout_ms != 0 && millis()-_timeout_start >= _timeout_ms) {
|
2018-02-07 22:21:09 -04:00
|
|
|
if (!copter.set_mode(LOITER, MODE_REASON_BRAKE_TIMEOUT)) {
|
|
|
|
copter.set_mode(ALT_HOLD, MODE_REASON_BRAKE_TIMEOUT);
|
2016-01-05 02:12:25 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-12-10 23:51:13 -04:00
|
|
|
void Copter::ModeBrake::timeout_to_loiter_ms(uint32_t timeout_ms)
|
2016-01-05 02:12:25 -04:00
|
|
|
{
|
2016-03-22 22:00:19 -03:00
|
|
|
_timeout_start = millis();
|
|
|
|
_timeout_ms = timeout_ms;
|
2015-04-22 18:10:10 -03:00
|
|
|
}
|
2019-02-25 10:32:52 -04:00
|
|
|
|
|
|
|
#endif
|