2013-11-03 01:26:19 -04:00
|
|
|
/// -*- tab-width: 4; Mode: C++; c-basic-offset: 4; indent-tabs-mode: nil -*-
|
|
|
|
|
|
|
|
// Traditional helicopter variables and functions
|
|
|
|
|
2014-05-10 10:34:30 -03:00
|
|
|
#include "heli.h"
|
|
|
|
|
2013-11-03 01:26:19 -04:00
|
|
|
#if FRAME_CONFIG == HELI_FRAME
|
|
|
|
|
|
|
|
#ifndef HELI_DYNAMIC_FLIGHT_SPEED_MIN
|
2013-11-16 03:58:14 -04:00
|
|
|
#define HELI_DYNAMIC_FLIGHT_SPEED_MIN 500 // we are in "dynamic flight" when the speed is over 1m/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
|
|
|
|
static void heli_init()
|
|
|
|
{
|
2014-05-10 11:33:46 -03:00
|
|
|
attitude_control.update_feedforward_filter_rates(MAIN_LOOP_SECONDS);
|
2014-05-11 20:19:01 -03:00
|
|
|
motors.set_dt(MAIN_LOOP_SECONDS);
|
2014-07-05 11:11:27 -03:00
|
|
|
// force recalculation of RSC ramp rates after setting _dt
|
|
|
|
motors.recalc_scalers();
|
2013-11-10 08:05:58 -04:00
|
|
|
}
|
|
|
|
|
2013-11-09 01:25:06 -04:00
|
|
|
// get_pilot_desired_collective - converts pilot input (from 0 ~ 1000) to a value that can be fed into the g.rc_3.servo_out function
|
|
|
|
static int16_t get_pilot_desired_collective(int16_t control_in)
|
|
|
|
{
|
|
|
|
// return immediately if reduce collective range for manual flight has not been configured
|
|
|
|
if (g.heli_stab_col_min == 0 && g.heli_stab_col_max == 1000) {
|
|
|
|
return control_in;
|
|
|
|
}
|
|
|
|
|
|
|
|
// scale pilot input to reduced collective range
|
|
|
|
float scalar = ((float)(g.heli_stab_col_max - g.heli_stab_col_min))/1000.0f;
|
|
|
|
int16_t collective_out = g.heli_stab_col_min + control_in * scalar;
|
|
|
|
collective_out = constrain_int16(collective_out, 0, 1000);
|
|
|
|
return collective_out;
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
static void check_dynamic_flight(void)
|
|
|
|
{
|
2014-02-03 08:06:34 -04:00
|
|
|
if (!motors.armed() || !motors.motor_runup_complete() ||
|
|
|
|
control_mode == LAND || (control_mode==RTL && rtl_state == Land) || (control_mode == AUTO && auto_mode == Auto_Land)) {
|
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
|
|
|
|
if (GPS_ok()) {
|
|
|
|
// get horizontal velocity
|
|
|
|
float velocity = inertial_nav.get_velocity_xy();
|
|
|
|
moving = (velocity >= HELI_DYNAMIC_FLIGHT_SPEED_MIN);
|
|
|
|
}else{
|
|
|
|
// with no GPS lock base it on throttle and forward lean angle
|
|
|
|
moving = (g.rc_3.servo_out > 800 || ahrs.pitch_sensor < -1500);
|
|
|
|
}
|
|
|
|
|
|
|
|
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.
|
|
|
|
static void update_heli_control_dynamics(void)
|
|
|
|
{
|
|
|
|
// Use Leaky_I if we are not moving fast
|
|
|
|
attitude_control.use_leaky_i(!heli_flags.dynamic_flight);
|
|
|
|
|
|
|
|
// To-Do: Update dynamic phase angle of swashplate
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
static void heli_update_landing_swash()
|
|
|
|
{
|
2014-02-03 08:06:34 -04:00
|
|
|
switch(control_mode) {
|
|
|
|
case ACRO:
|
|
|
|
case STABILIZE:
|
|
|
|
case DRIFT:
|
|
|
|
case SPORT:
|
2013-11-06 08:39:39 -04:00
|
|
|
// manual modes always uses full swash range
|
|
|
|
motors.set_collective_for_landing(false);
|
|
|
|
break;
|
|
|
|
|
2014-02-03 08:06:34 -04:00
|
|
|
case LAND:
|
2013-11-06 08:39:39 -04:00
|
|
|
// landing always uses limit swash range
|
|
|
|
motors.set_collective_for_landing(true);
|
|
|
|
break;
|
|
|
|
|
2014-02-03 08:06:34 -04:00
|
|
|
case RTL:
|
|
|
|
if (rtl_state == Land) {
|
|
|
|
motors.set_collective_for_landing(true);
|
|
|
|
}else{
|
|
|
|
motors.set_collective_for_landing(!heli_flags.dynamic_flight || ap.land_complete || !ap.auto_armed);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case AUTO:
|
|
|
|
if (auto_mode == Auto_Land) {
|
|
|
|
motors.set_collective_for_landing(true);
|
|
|
|
}else{
|
|
|
|
motors.set_collective_for_landing(!heli_flags.dynamic_flight || ap.land_complete || !ap.auto_armed);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
2013-11-06 08:39:39 -04:00
|
|
|
default:
|
|
|
|
// auto and hold use limited swash when landed
|
2014-01-05 17:09:10 -04:00
|
|
|
motors.set_collective_for_landing(!heli_flags.dynamic_flight || ap.land_complete || !ap.auto_armed);
|
2013-11-06 08:39:39 -04:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
static void heli_update_rotor_speed_targets()
|
|
|
|
{
|
|
|
|
// get rotor control method
|
|
|
|
uint8_t rsc_control_mode = motors.get_rsc_mode();
|
2014-05-09 18:25:25 -03:00
|
|
|
int16_t rsc_control_deglitched = rotor_speed_deglitch_filter.apply(g.rc_8.control_in);
|
2013-11-08 04:29:54 -04:00
|
|
|
|
|
|
|
switch (rsc_control_mode) {
|
|
|
|
case AP_MOTORS_HELI_RSC_MODE_NONE:
|
|
|
|
// even though pilot passes rotors speed directly to rotor ESC via receiver, motor lib needs to know if
|
|
|
|
// rotor is spinning in case we are using direct drive tailrotor which must be spun up at same time
|
|
|
|
case AP_MOTORS_HELI_RSC_MODE_CH8_PASSTHROUGH:
|
|
|
|
// pass through pilot desired rotor speed
|
2014-05-09 18:25:25 -03:00
|
|
|
motors.set_desired_rotor_speed(rsc_control_deglitched);
|
2013-11-08 04:29:54 -04:00
|
|
|
break;
|
|
|
|
case AP_MOTORS_HELI_RSC_MODE_SETPOINT:
|
|
|
|
// pass setpoint through as desired rotor speed
|
2014-05-09 18:25:25 -03:00
|
|
|
if (rsc_control_deglitched > 0) {
|
2013-11-08 04:29:54 -04:00
|
|
|
motors.set_desired_rotor_speed(motors.get_rsc_setpoint());
|
|
|
|
}else{
|
|
|
|
motors.set_desired_rotor_speed(0);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-01-13 04:23:09 -04:00
|
|
|
#endif // FRAME_CONFIG == HELI_FRAME
|