#include "Copter.h"
void Copter::run_rate_controller_main()
{
const float last_loop_time_s = AP::scheduler().get_last_loop_time_s();
pos_control->set_dt_s(last_loop_time_s);
attitude_control->set_dt_s(last_loop_time_s);
if (!using_rate_thread) {
motors->set_dt_s(last_loop_time_s);
attitude_control->rate_controller_run();
}
attitude_control->rate_controller_target_reset();
}
void Copter::update_throttle_hover()
{
if (!motors->armed() || ap.land_complete || standby_active) {
return;
}
if (flightmode->has_manual_throttle() || (copter.flightmode->mode_number() == Mode::Number::DRIFT)) {
return;
}
if (!is_zero(pos_control->get_vel_desired_U_ms())) {
return;
}
float vel_d_ms;
if (!AP::ahrs().get_velocity_D(vel_d_ms, vibration_check.high_vibes)) {
return;
}
float throttle = motors->get_throttle();
if ((throttle > 0.0f) && (fabsf(vel_d_ms) < 0.6) &&
(fabsf(ahrs.get_roll_rad() - attitude_control->get_roll_trim_rad()) < radians(5)) && (fabsf(ahrs.get_pitch_rad()) < radians(5))) {
motors->update_throttle_hover(0.01f);
#if HAL_GYROFFT_ENABLED
gyro_fft.update_freq_hover(0.01f, motors->get_throttle_out());
#endif
}
}
float Copter::get_pilot_desired_climb_rate_ms()
{
if (!rc().has_valid_input()) {
return 0.0f;
}
float throttle_control = copter.channel_throttle->get_control_in();
#if TOY_MODE_ENABLED
if (g2.toy_mode.enabled()) {
g2.toy_mode.throttle_adjust(throttle_control);
}
#endif
throttle_control = constrain_float(throttle_control, 0.0f, 1000.0f);
g.throttle_deadzone.set(constrain_int16(g.throttle_deadzone, 0, 400));
float desired_rate_ms = 0.0f;
const float mid_stick = get_throttle_mid();
const float deadband_top = mid_stick + g.throttle_deadzone;
const float deadband_bottom = mid_stick - g.throttle_deadzone;
if (throttle_control < deadband_bottom) {
desired_rate_ms = get_pilot_speed_dn() * 0.01 * (throttle_control - deadband_bottom) / deadband_bottom;
} else if (throttle_control > deadband_top) {
desired_rate_ms = g.pilot_speed_up_cms * 0.01 * (throttle_control - deadband_top) / (1000.0 - deadband_top);
} else {
desired_rate_ms = 0.0f;
}
return desired_rate_ms;
}
float Copter::get_non_takeoff_throttle()
{
return MAX(0,motors->get_throttle_hover() / 2.0);
}
void Copter::set_accel_throttle_I_from_pilot_throttle()
{
float pilot_throttle = constrain_float(attitude_control->get_throttle_in(), 0.0, 1.0);
pos_control->D_get_accel_pid().set_integrator(-(pilot_throttle - motors->get_throttle_hover()));
}
uint16_t Copter::get_pilot_speed_dn() const
{
if (g2.pilot_speed_dn_cms == 0) {
return abs(g.pilot_speed_up_cms);
} else {
return abs(g2.pilot_speed_dn_cms);
}
}