2015-05-29 23:12:49 -03:00
|
|
|
#include "Copter.h"
|
|
|
|
|
2013-11-03 01:26:19 -04:00
|
|
|
// Traditional helicopter variables and functions
|
|
|
|
|
|
|
|
#if FRAME_CONFIG == HELI_FRAME
|
|
|
|
|
|
|
|
#ifndef HELI_DYNAMIC_FLIGHT_SPEED_MIN
|
2021-10-09 11:46:31 -03:00
|
|
|
#define HELI_DYNAMIC_FLIGHT_SPEED_MIN 250 // we are in "dynamic flight" when the speed is over 2.5m/s for 2 seconds
|
2013-11-03 01:26:19 -04:00
|
|
|
#endif
|
|
|
|
|
|
|
|
// counter to control dynamic flight profile
|
|
|
|
static int8_t heli_dynamic_flight_counter;
|
|
|
|
|
2013-11-10 08:05:58 -04:00
|
|
|
// heli_init - perform any special initialisation required for the tradheli
|
2015-05-29 23:12:49 -03:00
|
|
|
void Copter::heli_init()
|
2013-11-10 08:05:58 -04:00
|
|
|
{
|
2015-10-12 22:05:49 -03:00
|
|
|
// pre-load stab col values as mode is initialized as Stabilize, but stabilize_init() function is not run on start-up.
|
|
|
|
input_manager.set_use_stab_col(true);
|
|
|
|
input_manager.set_stab_col_ramp(1.0);
|
2013-11-09 01:25:06 -04:00
|
|
|
}
|
|
|
|
|
2013-11-03 01:26:19 -04:00
|
|
|
// heli_check_dynamic_flight - updates the dynamic_flight flag based on our horizontal velocity
|
|
|
|
// should be called at 50hz
|
2015-05-29 23:12:49 -03:00
|
|
|
void Copter::check_dynamic_flight(void)
|
2013-11-03 01:26:19 -04:00
|
|
|
{
|
2019-04-09 09:16:58 -03:00
|
|
|
if (motors->get_spool_state() != AP_Motors::SpoolState::THROTTLE_UNLIMITED ||
|
2019-11-01 00:47:47 -03:00
|
|
|
flightmode->is_landing()) {
|
2013-11-03 01:26:19 -04:00
|
|
|
heli_dynamic_flight_counter = 0;
|
|
|
|
heli_flags.dynamic_flight = false;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool moving = false;
|
|
|
|
|
|
|
|
// with GPS lock use inertial nav to determine if we are moving
|
2015-01-02 07:43:50 -04:00
|
|
|
if (position_ok()) {
|
2017-10-13 00:42:09 -03:00
|
|
|
// get horizontal speed
|
2021-10-20 05:29:57 -03:00
|
|
|
const float speed = inertial_nav.get_speed_xy_cms();
|
2017-10-13 00:42:09 -03:00
|
|
|
moving = (speed >= HELI_DYNAMIC_FLIGHT_SPEED_MIN);
|
2013-11-03 01:26:19 -04:00
|
|
|
}else{
|
|
|
|
// with no GPS lock base it on throttle and forward lean angle
|
2017-01-09 03:31:26 -04:00
|
|
|
moving = (motors->get_throttle() > 0.8f || ahrs.pitch_sensor < -1500);
|
2013-11-03 01:26:19 -04:00
|
|
|
}
|
|
|
|
|
2019-11-01 02:13:55 -03:00
|
|
|
if (!moving && rangefinder_state.enabled && rangefinder.status_orient(ROTATION_PITCH_270) == RangeFinder::Status::Good) {
|
2015-09-03 23:06:00 -03:00
|
|
|
// when we are more than 2m from the ground with good
|
|
|
|
// rangefinder lock consider it to be dynamic flight
|
2017-02-09 07:30:59 -04:00
|
|
|
moving = (rangefinder.distance_cm_orient(ROTATION_PITCH_270) > 200);
|
2015-09-03 23:06:00 -03:00
|
|
|
}
|
|
|
|
|
2013-11-03 01:26:19 -04:00
|
|
|
if (moving) {
|
|
|
|
// if moving for 2 seconds, set the dynamic flight flag
|
|
|
|
if (!heli_flags.dynamic_flight) {
|
|
|
|
heli_dynamic_flight_counter++;
|
|
|
|
if (heli_dynamic_flight_counter >= 100) {
|
|
|
|
heli_flags.dynamic_flight = true;
|
|
|
|
heli_dynamic_flight_counter = 100;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}else{
|
|
|
|
// if not moving for 2 seconds, clear the dynamic flight flag
|
|
|
|
if (heli_flags.dynamic_flight) {
|
|
|
|
if (heli_dynamic_flight_counter > 0) {
|
|
|
|
heli_dynamic_flight_counter--;
|
|
|
|
}else{
|
|
|
|
heli_flags.dynamic_flight = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-05-09 18:07:52 -03:00
|
|
|
// update_heli_control_dynamics - pushes several important factors up into AP_MotorsHeli.
|
|
|
|
// should be run between the rate controller and the servo updates.
|
2015-05-29 23:12:49 -03:00
|
|
|
void Copter::update_heli_control_dynamics(void)
|
2014-05-09 18:07:52 -03:00
|
|
|
{
|
2020-11-15 19:33:43 -04:00
|
|
|
|
2020-12-01 17:36:36 -04:00
|
|
|
if (!motors->using_leaky_integrator()) {
|
|
|
|
//turn off leaky_I
|
|
|
|
attitude_control->use_leaky_i(false);
|
|
|
|
if (ap.land_complete || ap.land_complete_maybe) {
|
|
|
|
motors->set_land_complete(true);
|
|
|
|
} else {
|
|
|
|
motors->set_land_complete(false);
|
|
|
|
}
|
2020-11-15 19:33:43 -04:00
|
|
|
} else {
|
2020-12-01 17:36:36 -04:00
|
|
|
// Use Leaky_I if we are not moving fast
|
|
|
|
attitude_control->use_leaky_i(!heli_flags.dynamic_flight);
|
2020-11-15 19:33:43 -04:00
|
|
|
motors->set_land_complete(false);
|
|
|
|
}
|
2015-10-23 19:41:01 -03:00
|
|
|
|
2017-01-09 03:31:26 -04:00
|
|
|
if (ap.land_complete || (is_zero(motors->get_desired_rotor_speed()))){
|
2015-10-23 19:41:01 -03:00
|
|
|
// if we are landed or there is no rotor power demanded, decrement slew scalar
|
|
|
|
hover_roll_trim_scalar_slew--;
|
|
|
|
} else {
|
|
|
|
// if we are not landed and motor power is demanded, increment slew scalar
|
|
|
|
hover_roll_trim_scalar_slew++;
|
|
|
|
}
|
2016-06-04 22:21:31 -03:00
|
|
|
hover_roll_trim_scalar_slew = constrain_int16(hover_roll_trim_scalar_slew, 0, scheduler.get_loop_rate_hz());
|
2015-10-23 19:41:01 -03:00
|
|
|
|
|
|
|
// set hover roll trim scalar, will ramp from 0 to 1 over 1 second after we think helicopter has taken off
|
2018-11-16 11:48:42 -04:00
|
|
|
attitude_control->set_hover_roll_trim_scalar((float) hover_roll_trim_scalar_slew/(float) scheduler.get_loop_rate_hz());
|
2014-05-09 18:07:52 -03:00
|
|
|
}
|
|
|
|
|
2019-11-01 00:47:47 -03:00
|
|
|
bool Copter::should_use_landing_swash() const
|
|
|
|
{
|
|
|
|
if (flightmode->has_manual_throttle() ||
|
2020-01-30 03:29:36 -04:00
|
|
|
flightmode->mode_number() == Mode::Number::DRIFT) {
|
2019-11-01 00:47:47 -03:00
|
|
|
// manual modes always uses full swash range
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if (flightmode->is_landing()) {
|
|
|
|
// landing with non-manual throttle mode always uses limit swash range
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
if (ap.land_complete) {
|
|
|
|
// when landed in non-manual throttle mode limit swash range
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
if (!ap.auto_armed) {
|
|
|
|
// when waiting to takeoff in non-manual throttle mode limit swash range
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
if (!heli_flags.dynamic_flight) {
|
|
|
|
// Just in case we are unsure of being in non-manual throttle
|
|
|
|
// mode, limit swash range in low speed and hovering flight.
|
|
|
|
// This will catch any non-manual throttle mode attempting a
|
|
|
|
// landing and driving the collective too low before the land
|
|
|
|
// complete flag is set.
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2013-11-06 08:39:39 -04:00
|
|
|
// heli_update_landing_swash - sets swash plate flag so higher minimum is used when landed or landing
|
|
|
|
// should be called soon after update_land_detector in main code
|
2015-05-29 23:12:49 -03:00
|
|
|
void Copter::heli_update_landing_swash()
|
2013-11-06 08:39:39 -04:00
|
|
|
{
|
2019-11-01 00:47:47 -03:00
|
|
|
motors->set_collective_for_landing(should_use_landing_swash());
|
2021-10-29 13:17:45 -03:00
|
|
|
update_collective_low_flag(channel_throttle->get_control_in());
|
2013-11-06 08:39:39 -04:00
|
|
|
}
|
|
|
|
|
2020-01-07 01:12:54 -04:00
|
|
|
// convert motor interlock switch's position to desired rotor speed expressed as a value from 0 to 1
|
|
|
|
// returns zero if motor interlock auxiliary switch hasn't been defined
|
|
|
|
float Copter::get_pilot_desired_rotor_speed() const
|
|
|
|
{
|
|
|
|
RC_Channel *rc_ptr = rc().find_channel_for_option(RC_Channel::AUX_FUNC::MOTOR_INTERLOCK);
|
|
|
|
if (rc_ptr != nullptr) {
|
2020-01-09 00:22:37 -04:00
|
|
|
rc_ptr->set_range(1000);
|
2020-01-07 01:12:54 -04:00
|
|
|
return (float)rc_ptr->get_control_in() * 0.001f;
|
|
|
|
}
|
|
|
|
return 0.0f;
|
|
|
|
}
|
|
|
|
|
2013-11-08 04:29:54 -04:00
|
|
|
// heli_update_rotor_speed_targets - reads pilot input and passes new rotor speed targets to heli motors object
|
2015-05-29 23:12:49 -03:00
|
|
|
void Copter::heli_update_rotor_speed_targets()
|
2013-11-08 04:29:54 -04:00
|
|
|
{
|
2015-06-16 23:00:48 -03:00
|
|
|
|
|
|
|
static bool rotor_runup_complete_last = false;
|
|
|
|
|
2013-11-08 04:29:54 -04:00
|
|
|
// get rotor control method
|
2017-01-09 03:31:26 -04:00
|
|
|
uint8_t rsc_control_mode = motors->get_rsc_mode();
|
2020-01-07 01:12:54 -04:00
|
|
|
|
2015-08-12 19:57:33 -03:00
|
|
|
switch (rsc_control_mode) {
|
2021-01-20 16:44:08 -04:00
|
|
|
case ROTOR_CONTROL_MODE_PASSTHROUGH:
|
2019-02-09 19:58:42 -04:00
|
|
|
// pass through pilot desired rotor speed from the RC
|
2020-01-07 01:12:54 -04:00
|
|
|
if (get_pilot_desired_rotor_speed() > 0.01) {
|
|
|
|
ap.motor_interlock_switch = true;
|
|
|
|
motors->set_desired_rotor_speed(get_pilot_desired_rotor_speed());
|
2015-08-12 19:57:33 -03:00
|
|
|
} else {
|
2020-01-07 01:12:54 -04:00
|
|
|
ap.motor_interlock_switch = false;
|
2017-01-09 03:31:26 -04:00
|
|
|
motors->set_desired_rotor_speed(0.0f);
|
2015-08-12 19:57:33 -03:00
|
|
|
}
|
|
|
|
break;
|
2021-01-20 16:44:08 -04:00
|
|
|
case ROTOR_CONTROL_MODE_SETPOINT:
|
|
|
|
case ROTOR_CONTROL_MODE_THROTTLECURVE:
|
|
|
|
case ROTOR_CONTROL_MODE_AUTOTHROTTLE:
|
2019-01-26 10:33:32 -04:00
|
|
|
if (motors->get_interlock()) {
|
2017-01-09 03:31:26 -04:00
|
|
|
motors->set_desired_rotor_speed(motors->get_rsc_setpoint());
|
2015-08-12 19:57:33 -03:00
|
|
|
}else{
|
2017-01-09 03:31:26 -04:00
|
|
|
motors->set_desired_rotor_speed(0.0f);
|
2015-08-12 19:57:33 -03:00
|
|
|
}
|
|
|
|
break;
|
2013-11-08 04:29:54 -04:00
|
|
|
}
|
2015-06-16 23:00:48 -03:00
|
|
|
|
|
|
|
// when rotor_runup_complete changes to true, log event
|
2017-01-09 03:31:26 -04:00
|
|
|
if (!rotor_runup_complete_last && motors->rotor_runup_complete()){
|
2019-10-25 03:06:05 -03:00
|
|
|
AP::logger().Write_Event(LogEvent::ROTOR_RUNUP_COMPLETE);
|
2017-01-09 03:31:26 -04:00
|
|
|
} else if (rotor_runup_complete_last && !motors->rotor_runup_complete()){
|
2019-10-25 03:06:05 -03:00
|
|
|
AP::logger().Write_Event(LogEvent::ROTOR_SPEED_BELOW_CRITICAL);
|
2015-06-16 23:00:48 -03:00
|
|
|
}
|
2017-01-09 03:31:26 -04:00
|
|
|
rotor_runup_complete_last = motors->rotor_runup_complete();
|
2013-11-08 04:29:54 -04:00
|
|
|
}
|
|
|
|
|
2019-11-28 16:21:07 -04:00
|
|
|
|
|
|
|
// heli_update_autorotation - determines if aircraft is in autorotation and sets motors flag and switches
|
|
|
|
// to autorotation flight mode if manual collective is not being used.
|
|
|
|
void Copter::heli_update_autorotation()
|
|
|
|
{
|
|
|
|
#if MODE_AUTOROTATE_ENABLED == ENABLED
|
2020-04-01 11:42:39 -03:00
|
|
|
// check if flying and interlock disengaged
|
2021-11-24 23:18:34 -04:00
|
|
|
if (!ap.land_complete && !motors->get_interlock() && g2.arot.is_enable()) {
|
|
|
|
if (!flightmode->has_manual_throttle()) {
|
2020-04-01 11:42:39 -03:00
|
|
|
// set autonomous autorotation flight mode
|
|
|
|
set_mode(Mode::Number::AUTOROTATE, ModeReason::AUTOROTATION_START);
|
|
|
|
}
|
|
|
|
// set flag to facilitate both auto and manual autorotations
|
2019-11-28 16:21:07 -04:00
|
|
|
heli_flags.in_autorotation = true;
|
|
|
|
} else {
|
|
|
|
heli_flags.in_autorotation = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// sets autorotation flags through out libraries
|
|
|
|
heli_set_autorotation(heli_flags.in_autorotation);
|
2021-11-24 23:18:34 -04:00
|
|
|
if (!ap.land_complete && g2.arot.is_enable()) {
|
2019-11-28 16:21:07 -04:00
|
|
|
motors->set_enable_bailout(true);
|
|
|
|
} else {
|
|
|
|
motors->set_enable_bailout(false);
|
|
|
|
}
|
|
|
|
#else
|
|
|
|
heli_flags.in_autorotation = false;
|
|
|
|
motors->set_enable_bailout(false);
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
#if MODE_AUTOROTATE_ENABLED == ENABLED
|
2020-04-01 11:42:39 -03:00
|
|
|
// heli_set_autorotation - set the autorotation flag throughout libraries
|
2019-11-28 16:21:07 -04:00
|
|
|
void Copter::heli_set_autorotation(bool autorotation)
|
|
|
|
{
|
|
|
|
motors->set_in_autorotation(autorotation);
|
|
|
|
}
|
|
|
|
#endif
|
2021-10-29 13:17:45 -03:00
|
|
|
|
|
|
|
// update collective low flag. Use a debounce time of 400 milliseconds.
|
|
|
|
void Copter::update_collective_low_flag(int16_t throttle_control)
|
|
|
|
{
|
|
|
|
static uint32_t last_nonzero_collective_ms = 0;
|
|
|
|
uint32_t tnow_ms = millis();
|
|
|
|
|
|
|
|
if (throttle_control > 0) {
|
|
|
|
last_nonzero_collective_ms = tnow_ms;
|
|
|
|
heli_flags.coll_stk_low = false;
|
|
|
|
} else if (tnow_ms - last_nonzero_collective_ms > 400) {
|
|
|
|
heli_flags.coll_stk_low = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-01-13 04:23:09 -04:00
|
|
|
#endif // FRAME_CONFIG == HELI_FRAME
|