2015-05-29 23:12:49 -03:00
|
|
|
#include "Copter.h"
|
|
|
|
|
2022-12-04 06:09:51 -04:00
|
|
|
/*************************************************************
|
|
|
|
* Attitude Rate controllers and timing
|
|
|
|
****************************************************************/
|
|
|
|
|
2024-02-11 01:19:56 -04:00
|
|
|
/*
|
|
|
|
update rate controller when run from main thread (normal operation)
|
|
|
|
*/
|
|
|
|
void Copter::run_rate_controller_main()
|
2022-12-04 06:09:51 -04:00
|
|
|
{
|
|
|
|
// set attitude and position controller loop time
|
|
|
|
const float last_loop_time_s = AP::scheduler().get_last_loop_time_s();
|
|
|
|
pos_control->set_dt(last_loop_time_s);
|
2024-02-11 01:19:56 -04:00
|
|
|
attitude_control->set_dt(last_loop_time_s);
|
2022-12-04 06:09:51 -04:00
|
|
|
|
2024-02-11 01:19:56 -04:00
|
|
|
if (!using_rate_thread) {
|
|
|
|
motors->set_dt(last_loop_time_s);
|
|
|
|
// only run the rate controller if we are not using the rate thread
|
|
|
|
attitude_control->rate_controller_run();
|
|
|
|
}
|
2024-09-24 08:41:54 -03:00
|
|
|
// reset sysid and other temporary inputs
|
2024-08-15 06:21:56 -03:00
|
|
|
attitude_control->rate_controller_target_reset();
|
2022-12-04 06:09:51 -04:00
|
|
|
}
|
|
|
|
|
2012-11-23 02:57:49 -04:00
|
|
|
/*************************************************************
|
|
|
|
* throttle control
|
|
|
|
****************************************************************/
|
|
|
|
|
2016-06-09 09:42:15 -03:00
|
|
|
// update estimated throttle required to hover (if necessary)
|
|
|
|
// called at 100hz
|
|
|
|
void Copter::update_throttle_hover()
|
2012-11-23 02:57:49 -04:00
|
|
|
{
|
2021-06-29 08:44:10 -03:00
|
|
|
// if not armed or landed or on standby then exit
|
2021-05-17 20:17:08 -03:00
|
|
|
if (!motors->armed() || ap.land_complete || standby_active) {
|
2014-02-03 01:06:08 -04:00
|
|
|
return;
|
2012-11-23 02:57:49 -04:00
|
|
|
}
|
2014-02-03 01:06:08 -04:00
|
|
|
|
2016-06-09 09:42:15 -03:00
|
|
|
// do not update in manual throttle modes or Drift
|
2020-01-30 03:29:36 -04:00
|
|
|
if (flightmode->has_manual_throttle() || (copter.flightmode->mode_number() == Mode::Number::DRIFT)) {
|
2016-06-09 09:42:15 -03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// do not update while climbing or descending
|
2021-05-11 01:42:02 -03:00
|
|
|
if (!is_zero(pos_control->get_vel_desired_cms().z)) {
|
2016-06-09 09:42:15 -03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2014-02-03 01:06:08 -04:00
|
|
|
// get throttle output
|
2017-01-09 03:31:26 -04:00
|
|
|
float throttle = motors->get_throttle();
|
2014-02-03 01:06:08 -04:00
|
|
|
|
2020-11-15 19:33:43 -04:00
|
|
|
// calc average throttle if we are in a level hover. accounts for heli hover roll trim
|
2021-10-20 05:29:57 -03:00
|
|
|
if (throttle > 0.0f && fabsf(inertial_nav.get_velocity_z_up_cms()) < 60 &&
|
2022-02-26 09:50:22 -04:00
|
|
|
fabsf(ahrs.roll_sensor-attitude_control->get_roll_trim_cd()) < 500 && labs(ahrs.pitch_sensor) < 500) {
|
2016-06-09 09:42:15 -03:00
|
|
|
// Can we set the time constant automatically
|
2017-01-09 03:31:26 -04:00
|
|
|
motors->update_throttle_hover(0.01f);
|
2019-07-18 16:19:08 -03:00
|
|
|
#if HAL_GYROFFT_ENABLED
|
|
|
|
gyro_fft.update_freq_hover(0.01f, motors->get_throttle_out());
|
|
|
|
#endif
|
2012-11-24 00:41:17 -04:00
|
|
|
}
|
2012-11-23 02:57:49 -04:00
|
|
|
}
|
|
|
|
|
2016-01-05 07:04:19 -04:00
|
|
|
// get_pilot_desired_climb_rate - transform pilot's throttle input to climb rate in cm/s
|
2012-11-23 02:57:49 -04:00
|
|
|
// without any deadzone at the bottom
|
2015-05-29 23:12:49 -03:00
|
|
|
float Copter::get_pilot_desired_climb_rate(float throttle_control)
|
2012-11-23 02:57:49 -04:00
|
|
|
{
|
|
|
|
// throttle failsafe check
|
2024-02-07 00:14:28 -04:00
|
|
|
if (failsafe.radio || !rc().has_ever_seen_rc_input()) {
|
2015-04-30 03:10:38 -03:00
|
|
|
return 0.0f;
|
2012-11-23 02:57:49 -04:00
|
|
|
}
|
|
|
|
|
2024-09-04 00:48:37 -03:00
|
|
|
#if TOY_MODE_ENABLED
|
2018-01-18 02:49:20 -04:00
|
|
|
if (g2.toy_mode.enabled()) {
|
|
|
|
// allow throttle to be reduced after throttle arming and for
|
|
|
|
// slower descent close to the ground
|
|
|
|
g2.toy_mode.throttle_adjust(throttle_control);
|
|
|
|
}
|
|
|
|
#endif
|
2019-04-10 23:33:21 -03:00
|
|
|
|
2012-11-23 02:57:49 -04:00
|
|
|
// ensure a reasonable throttle value
|
2016-01-05 07:04:19 -04:00
|
|
|
throttle_control = constrain_float(throttle_control,0.0f,1000.0f);
|
2012-11-23 02:57:49 -04:00
|
|
|
|
2014-09-02 00:38:58 -03:00
|
|
|
// ensure a reasonable deadzone
|
2022-07-05 00:08:57 -03:00
|
|
|
g.throttle_deadzone.set(constrain_int16(g.throttle_deadzone, 0, 400));
|
2014-09-02 00:38:58 -03:00
|
|
|
|
2021-03-10 00:32:53 -04:00
|
|
|
float desired_rate = 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;
|
|
|
|
|
2012-11-23 02:57:49 -04:00
|
|
|
// check throttle is above, below or in the deadband
|
2014-11-17 18:36:13 -04:00
|
|
|
if (throttle_control < deadband_bottom) {
|
2012-11-23 02:57:49 -04:00
|
|
|
// below the deadband
|
2017-11-08 09:25:53 -04:00
|
|
|
desired_rate = get_pilot_speed_dn() * (throttle_control-deadband_bottom) / deadband_bottom;
|
2019-04-10 23:33:21 -03:00
|
|
|
} else if (throttle_control > deadband_top) {
|
2012-11-23 02:57:49 -04:00
|
|
|
// above the deadband
|
2017-11-08 09:25:53 -04:00
|
|
|
desired_rate = g.pilot_speed_up * (throttle_control-deadband_top) / (1000.0f-deadband_top);
|
2019-04-10 23:33:21 -03:00
|
|
|
} else {
|
2012-11-23 02:57:49 -04:00
|
|
|
// must be in the deadband
|
2015-04-30 03:10:38 -03:00
|
|
|
desired_rate = 0.0f;
|
2012-11-23 02:57:49 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
return desired_rate;
|
|
|
|
}
|
|
|
|
|
2014-07-17 06:15:50 -03:00
|
|
|
// get_non_takeoff_throttle - a throttle somewhere between min and mid throttle which should not lead to a takeoff
|
2015-05-29 23:12:49 -03:00
|
|
|
float Copter::get_non_takeoff_throttle()
|
2014-07-17 06:15:50 -03:00
|
|
|
{
|
2017-01-09 03:31:26 -04:00
|
|
|
return MAX(0,motors->get_throttle_hover()/2.0f);
|
2014-07-17 06:15:50 -03:00
|
|
|
}
|
|
|
|
|
2014-02-11 09:33:08 -04:00
|
|
|
// set_accel_throttle_I_from_pilot_throttle - smoothes transition from pilot controlled throttle to autopilot throttle
|
2016-10-14 03:34:24 -03:00
|
|
|
void Copter::set_accel_throttle_I_from_pilot_throttle()
|
2013-01-11 23:20:37 -04:00
|
|
|
{
|
2016-10-14 03:34:24 -03:00
|
|
|
// get last throttle input sent to attitude controller
|
2017-01-09 03:31:26 -04:00
|
|
|
float pilot_throttle = constrain_float(attitude_control->get_throttle_in(), 0.0f, 1.0f);
|
2013-01-11 23:20:37 -04:00
|
|
|
// shift difference between pilot's throttle and hover throttle into accelerometer I
|
2017-11-20 21:49:11 -04:00
|
|
|
pos_control->get_accel_z_pid().set_integrator((pilot_throttle-motors->get_throttle_hover()) * 1000.0f);
|
2013-01-11 23:20:37 -04:00
|
|
|
}
|
2015-04-23 02:57:49 -03:00
|
|
|
|
2015-08-11 08:25:20 -03:00
|
|
|
// rotate vector from vehicle's perspective to North-East frame
|
|
|
|
void Copter::rotate_body_frame_to_NE(float &x, float &y)
|
|
|
|
{
|
|
|
|
float ne_x = x*ahrs.cos_yaw() - y*ahrs.sin_yaw();
|
|
|
|
float ne_y = x*ahrs.sin_yaw() + y*ahrs.cos_yaw();
|
|
|
|
x = ne_x;
|
|
|
|
y = ne_y;
|
|
|
|
}
|
2017-11-08 09:25:53 -04:00
|
|
|
|
|
|
|
// It will return the PILOT_SPEED_DN value if non zero, otherwise if zero it returns the PILOT_SPEED_UP value.
|
2021-02-01 12:26:21 -04:00
|
|
|
uint16_t Copter::get_pilot_speed_dn() const
|
2017-11-08 09:25:53 -04:00
|
|
|
{
|
|
|
|
if (g2.pilot_speed_dn == 0) {
|
|
|
|
return abs(g.pilot_speed_up);
|
|
|
|
} else {
|
|
|
|
return abs(g2.pilot_speed_dn);
|
|
|
|
}
|
|
|
|
}
|