2015-05-29 23:12:49 -03:00
|
|
|
#include "Copter.h"
|
|
|
|
|
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
|
2015-05-29 23:12:49 -03:00
|
|
|
bool Copter::brake_init(bool ignore_checks)
|
2015-04-22 18:10:10 -03:00
|
|
|
{
|
2015-10-13 23:52:48 -03:00
|
|
|
if (position_ok() || ignore_checks) {
|
2015-04-22 18:10:10 -03:00
|
|
|
|
|
|
|
// set desired acceleration to zero
|
2017-01-09 03:31:26 -04:00
|
|
|
wp_nav->clear_pilot_desired_acceleration();
|
2015-04-22 18:10:10 -03:00
|
|
|
|
|
|
|
// set target to current position
|
2017-01-09 03:31:26 -04:00
|
|
|
wp_nav->init_brake_target(BRAKE_MODE_DECEL_RATE);
|
2015-04-22 18:10:10 -03:00
|
|
|
|
|
|
|
// initialize vertical speed and acceleration
|
2017-01-09 03:31:26 -04:00
|
|
|
pos_control->set_speed_z(BRAKE_MODE_SPEED_Z, BRAKE_MODE_SPEED_Z);
|
|
|
|
pos_control->set_accel_z(BRAKE_MODE_DECEL_RATE);
|
2015-04-22 18:10:10 -03:00
|
|
|
|
2015-10-27 10:06:50 -03:00
|
|
|
// initialise position and desired velocity
|
2017-01-09 03:31:26 -04:00
|
|
|
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());
|
2016-10-14 09:28:32 -03:00
|
|
|
}
|
2015-04-22 18:10:10 -03:00
|
|
|
|
2016-01-05 02:12:25 -04:00
|
|
|
brake_timeout_ms = 0;
|
|
|
|
|
2015-04-22 18:10:10 -03:00
|
|
|
return true;
|
|
|
|
}else{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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
|
2015-05-29 23:12:49 -03:00
|
|
|
void Copter::brake_run()
|
2015-04-22 18:10:10 -03:00
|
|
|
{
|
|
|
|
// if not auto armed set throttle to zero and exit immediately
|
2017-01-09 03:31:26 -04:00
|
|
|
if (!motors->armed() || !ap.auto_armed || !motors->get_interlock()) {
|
|
|
|
wp_nav->init_brake_target(BRAKE_MODE_DECEL_RATE);
|
2015-07-01 15:38:32 -03:00
|
|
|
#if FRAME_CONFIG == HELI_FRAME // Helicopters always stabilize roll/pitch/yaw
|
|
|
|
// call attitude controller
|
2017-01-09 03:31:26 -04:00
|
|
|
attitude_control->input_euler_angle_roll_pitch_euler_rate_yaw(0, 0, 0, get_smoothing_gain());
|
|
|
|
attitude_control->set_throttle_out(0,false,g.throttle_filt);
|
2016-01-13 03:09:42 -04:00
|
|
|
#else
|
2017-01-09 03:31:26 -04:00
|
|
|
motors->set_desired_spool_state(AP_Motors::DESIRED_SPIN_WHEN_ARMED);
|
2016-01-13 03:09:42 -04:00
|
|
|
// multicopters do not stabilize roll/pitch/yaw when disarmed
|
2017-01-09 03:31:26 -04:00
|
|
|
attitude_control->set_throttle_out_unstabilized(0,true,g.throttle_filt);
|
2015-07-01 15:38:32 -03:00
|
|
|
#endif
|
2017-01-09 03:31:26 -04:00
|
|
|
pos_control->relax_alt_hold_controllers(0.0f);
|
2015-04-22 18:10:10 -03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// relax stop target if we might be landed
|
2015-05-06 01:38:38 -03:00
|
|
|
if (ap.land_complete_maybe) {
|
2017-01-09 03:31:26 -04:00
|
|
|
wp_nav->loiter_soften_for_landing();
|
2015-05-17 02:28:55 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
// if landed immediately disarm
|
|
|
|
if (ap.land_complete) {
|
|
|
|
init_disarm_motors();
|
2015-04-22 18:10:10 -03:00
|
|
|
}
|
|
|
|
|
2016-01-13 03:09:42 -04:00
|
|
|
// set motors to full range
|
2017-01-09 03:31:26 -04:00
|
|
|
motors->set_desired_spool_state(AP_Motors::DESIRED_THROTTLE_UNLIMITED);
|
2016-01-13 03:09:42 -04:00
|
|
|
|
2015-05-17 00:22:20 -03:00
|
|
|
// run brake controller
|
2017-01-09 03:31:26 -04:00
|
|
|
wp_nav->update_brake(ekfGndSpdLimit, ekfNavVelGainScaler);
|
2015-04-22 18:10:10 -03:00
|
|
|
|
|
|
|
// call attitude controller
|
2017-01-09 03:31:26 -04:00
|
|
|
attitude_control->input_euler_angle_roll_pitch_euler_rate_yaw(wp_nav->get_roll(), wp_nav->get_pitch(), 0.0f, get_smoothing_gain());
|
2015-04-22 18:10:10 -03:00
|
|
|
|
|
|
|
// body-frame rate controller is run directly from 100hz loop
|
|
|
|
|
|
|
|
// update altitude target and call position controller
|
2017-01-09 03:31:26 -04:00
|
|
|
pos_control->set_alt_target_from_climb_rate_ff(0.0f, G_Dt, false);
|
|
|
|
pos_control->update_z_controller();
|
2016-01-05 02:12:25 -04:00
|
|
|
|
2016-05-12 23:39:37 -03:00
|
|
|
if (brake_timeout_ms != 0 && millis()-brake_timeout_start >= brake_timeout_ms) {
|
|
|
|
if (!set_mode(LOITER, MODE_REASON_BRAKE_TIMEOUT)) {
|
|
|
|
set_mode(ALT_HOLD, MODE_REASON_BRAKE_TIMEOUT);
|
2016-01-05 02:12:25 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void Copter::brake_timeout_to_loiter_ms(uint32_t timeout_ms)
|
|
|
|
{
|
|
|
|
brake_timeout_start = millis();
|
|
|
|
brake_timeout_ms = timeout_ms;
|
2015-04-22 18:10:10 -03:00
|
|
|
}
|