Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place. Commercial Alternative to JupyterHub.
Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place. Commercial Alternative to JupyterHub.
Path: blob/master/ArduPlane/RC_Channel_Plane.cpp
Views: 1798
#include "Plane.h"12#include "RC_Channel_Plane.h"3#include "qautotune.h"45// defining these two macros and including the RC_Channels_VarInfo6// header defines the parameter information common to all vehicle7// types8#define RC_CHANNELS_SUBCLASS RC_Channels_Plane9#define RC_CHANNEL_SUBCLASS RC_Channel_Plane1011#include <RC_Channel/RC_Channels_VarInfo.h>1213// note that this callback is not presently used on Plane:14int8_t RC_Channels_Plane::flight_mode_channel_number() const15{16return plane.g.flight_mode_channel.get();17}1819bool RC_Channels_Plane::in_rc_failsafe() const20{21return (plane.rc_failsafe_active() || plane.failsafe.rc_failsafe);22}2324bool RC_Channels_Plane::has_valid_input() const25{26if (in_rc_failsafe()) {27return false;28}29if (plane.failsafe.throttle_counter != 0) {30return false;31}32return true;33}3435RC_Channel * RC_Channels_Plane::get_arming_channel(void) const36{37return plane.channel_rudder;38}3940void RC_Channel_Plane::do_aux_function_change_mode(const Mode::Number number,41const AuxSwitchPos ch_flag)42{43switch(ch_flag) {44case AuxSwitchPos::HIGH: {45// engage mode (if not possible we remain in current flight mode)46plane.set_mode_by_number(number, ModeReason::AUX_FUNCTION);47break;48}49default:50// return to flight mode switch's flight mode if we are currently51// in this mode52if (plane.control_mode->mode_number() == number) {53rc().reset_mode_switch();54}55}56}5758#if HAL_QUADPLANE_ENABLED59void RC_Channel_Plane::do_aux_function_q_assist_state(AuxSwitchPos ch_flag)60{61switch(ch_flag) {62case AuxSwitchPos::HIGH:63gcs().send_text(MAV_SEVERITY_INFO, "QAssist: Force enabled");64plane.quadplane.assist.set_state(VTOL_Assist::STATE::FORCE_ENABLED);65break;6667case AuxSwitchPos::MIDDLE:68gcs().send_text(MAV_SEVERITY_INFO, "QAssist: Enabled");69plane.quadplane.assist.set_state(VTOL_Assist::STATE::ASSIST_ENABLED);70break;7172case AuxSwitchPos::LOW:73gcs().send_text(MAV_SEVERITY_INFO, "QAssist: Disabled");74plane.quadplane.assist.set_state(VTOL_Assist::STATE::ASSIST_DISABLED);75break;76}77}78#endif // HAL_QUADPLANE_ENABLED7980void RC_Channel_Plane::do_aux_function_crow_mode(AuxSwitchPos ch_flag)81{82switch(ch_flag) {83case AuxSwitchPos::HIGH:84plane.crow_mode = Plane::CrowMode::CROW_DISABLED;85gcs().send_text(MAV_SEVERITY_INFO, "Crow Flaps Disabled");86break;87case AuxSwitchPos::MIDDLE:88gcs().send_text(MAV_SEVERITY_INFO, "Progressive Crow Flaps");89plane.crow_mode = Plane::CrowMode::PROGRESSIVE;90break;91case AuxSwitchPos::LOW:92plane.crow_mode = Plane::CrowMode::NORMAL;93gcs().send_text(MAV_SEVERITY_INFO, "Normal Crow Flaps");94break;95}96}9798#if HAL_SOARING_ENABLED99void RC_Channel_Plane::do_aux_function_soaring_3pos(AuxSwitchPos ch_flag)100{101SoaringController::ActiveStatus desired_state = SoaringController::ActiveStatus::SOARING_DISABLED;102103switch (ch_flag) {104case AuxSwitchPos::LOW:105desired_state = SoaringController::ActiveStatus::SOARING_DISABLED;106break;107case AuxSwitchPos::MIDDLE:108desired_state = SoaringController::ActiveStatus::MANUAL_MODE_CHANGE;109break;110case AuxSwitchPos::HIGH:111desired_state = SoaringController::ActiveStatus::AUTO_MODE_CHANGE;112break;113}114115plane.g2.soaring_controller.set_pilot_desired_state(desired_state);116}117#endif118119void RC_Channel_Plane::do_aux_function_flare(AuxSwitchPos ch_flag)120{121switch(ch_flag) {122case AuxSwitchPos::HIGH:123plane.flare_mode = Plane::FlareMode::ENABLED_PITCH_TARGET;124break;125case AuxSwitchPos::MIDDLE:126plane.flare_mode = Plane::FlareMode::ENABLED_NO_PITCH_TARGET;127break;128case AuxSwitchPos::LOW:129plane.flare_mode = Plane::FlareMode::FLARE_DISABLED;130break;131}132}133134135void RC_Channel_Plane::init_aux_function(const RC_Channel::AUX_FUNC ch_option,136const RC_Channel::AuxSwitchPos ch_flag)137{138switch(ch_option) {139// the following functions do not need to be initialised:140case AUX_FUNC::AUTO:141case AUX_FUNC::CIRCLE:142case AUX_FUNC::ACRO:143case AUX_FUNC::TRAINING:144case AUX_FUNC::FLAP:145case AUX_FUNC::GUIDED:146case AUX_FUNC::INVERTED:147case AUX_FUNC::LOITER:148case AUX_FUNC::MANUAL:149case AUX_FUNC::RTL:150case AUX_FUNC::TAKEOFF:151case AUX_FUNC::FBWA:152case AUX_FUNC::AIRBRAKE:153#if HAL_QUADPLANE_ENABLED154case AUX_FUNC::QRTL:155case AUX_FUNC::QSTABILIZE:156#endif157case AUX_FUNC::FBWA_TAILDRAGGER:158case AUX_FUNC::FWD_THR:159case AUX_FUNC::LANDING_FLARE:160#if HAL_PARACHUTE_ENABLED161case AUX_FUNC::PARACHUTE_RELEASE:162#endif163case AUX_FUNC::MODE_SWITCH_RESET:164case AUX_FUNC::CRUISE:165#if HAL_QUADPLANE_ENABLED166case AUX_FUNC::ARMDISARM_AIRMODE:167#endif168case AUX_FUNC::PLANE_AUTO_LANDING_ABORT:169case AUX_FUNC::TRIM_TO_CURRENT_SERVO_RC:170case AUX_FUNC::EMERGENCY_LANDING_EN:171case AUX_FUNC::FW_AUTOTUNE:172case AUX_FUNC::VFWD_THR_OVERRIDE:173case AUX_FUNC::PRECISION_LOITER:174#if QAUTOTUNE_ENABLED175case AUX_FUNC::AUTOTUNE_TEST_GAINS:176#endif177#if AP_QUICKTUNE_ENABLED178case AUX_FUNC::QUICKTUNE:179#endif180break;181182case AUX_FUNC::SOARING:183#if HAL_QUADPLANE_ENABLED184case AUX_FUNC::Q_ASSIST:185case AUX_FUNC::AIRMODE:186case AUX_FUNC::WEATHER_VANE_ENABLE:187#endif188#if AP_AIRSPEED_AUTOCAL_ENABLE189case AUX_FUNC::ARSPD_CALIBRATE:190#endif191case AUX_FUNC::TER_DISABLE:192case AUX_FUNC::CROW_SELECT:193#if AP_ICENGINE_ENABLED194case AUX_FUNC::ICE_START_STOP:195#endif196run_aux_function(ch_option, ch_flag, AuxFuncTrigger::Source::INIT, ch_in);197break;198199case AUX_FUNC::REVERSE_THROTTLE:200plane.have_reverse_throttle_rc_option = true;201// setup input throttle as a range. This is needed as init_aux_function is called202// after set_control_channels()203if (plane.channel_throttle) {204plane.channel_throttle->set_range(100);205}206// note that we don't call do_aux_function() here as we don't207// want to startup with reverse thrust208break;209210default:211// handle in parent class212RC_Channel::init_aux_function(ch_option, ch_flag);213break;214}215}216217// do_aux_function - implement the function invoked by auxiliary switches218bool RC_Channel_Plane::do_aux_function(const AuxFuncTrigger &trigger)219{220const AUX_FUNC &ch_option = trigger.func;221const AuxSwitchPos &ch_flag = trigger.pos;222223switch(ch_option) {224case AUX_FUNC::INVERTED:225plane.inverted_flight = (ch_flag == AuxSwitchPos::HIGH);226break;227228case AUX_FUNC::REVERSE_THROTTLE:229plane.reversed_throttle = (ch_flag == AuxSwitchPos::HIGH);230gcs().send_text(MAV_SEVERITY_INFO, "RevThrottle: %s", plane.reversed_throttle?"ENABLE":"DISABLE");231break;232233case AUX_FUNC::AUTO:234do_aux_function_change_mode(Mode::Number::AUTO, ch_flag);235break;236237case AUX_FUNC::CIRCLE:238do_aux_function_change_mode(Mode::Number::CIRCLE, ch_flag);239break;240241case AUX_FUNC::ACRO:242do_aux_function_change_mode(Mode::Number::ACRO, ch_flag);243break;244245case AUX_FUNC::TRAINING:246do_aux_function_change_mode(Mode::Number::TRAINING, ch_flag);247break;248249case AUX_FUNC::LOITER:250do_aux_function_change_mode(Mode::Number::LOITER, ch_flag);251break;252253case AUX_FUNC::GUIDED:254do_aux_function_change_mode(Mode::Number::GUIDED, ch_flag);255break;256257case AUX_FUNC::MANUAL:258do_aux_function_change_mode(Mode::Number::MANUAL, ch_flag);259break;260261case AUX_FUNC::RTL:262do_aux_function_change_mode(Mode::Number::RTL, ch_flag);263break;264265case AUX_FUNC::TAKEOFF:266do_aux_function_change_mode(Mode::Number::TAKEOFF, ch_flag);267break;268269case AUX_FUNC::FBWA:270do_aux_function_change_mode(Mode::Number::FLY_BY_WIRE_A, ch_flag);271break;272273#if HAL_QUADPLANE_ENABLED274case AUX_FUNC::QRTL:275do_aux_function_change_mode(Mode::Number::QRTL, ch_flag);276break;277278case AUX_FUNC::QSTABILIZE:279do_aux_function_change_mode(Mode::Number::QSTABILIZE, ch_flag);280break;281282case AUX_FUNC::VFWD_THR_OVERRIDE: {283const bool enable = (ch_flag == AuxSwitchPos::HIGH);284if (enable != plane.quadplane.vfwd_enable_active) {285plane.quadplane.vfwd_enable_active = enable;286gcs().send_text(MAV_SEVERITY_INFO, "QFwdThr: %s", enable?"ON":"OFF");287}288break;289}290#endif291292#if HAL_SOARING_ENABLED293case AUX_FUNC::SOARING:294do_aux_function_soaring_3pos(ch_flag);295break;296#endif297298case AUX_FUNC::FLAP:299case AUX_FUNC::FBWA_TAILDRAGGER:300case AUX_FUNC::AIRBRAKE:301break; // input labels, nothing to do302303#if HAL_QUADPLANE_ENABLED304case AUX_FUNC::Q_ASSIST:305do_aux_function_q_assist_state(ch_flag);306break;307#endif308309case AUX_FUNC::FWD_THR:310break; // VTOL forward throttle input label, nothing to do311312case AUX_FUNC::TER_DISABLE:313switch (ch_flag) {314case AuxSwitchPos::HIGH:315plane.non_auto_terrain_disable = true;316if (plane.control_mode->allows_terrain_disable()) {317plane.set_target_altitude_current();318}319break;320case AuxSwitchPos::MIDDLE:321break;322case AuxSwitchPos::LOW:323plane.non_auto_terrain_disable = false;324if (plane.control_mode->allows_terrain_disable()) {325plane.set_target_altitude_current();326}327break;328}329gcs().send_text(MAV_SEVERITY_INFO, "NON AUTO TERRN: %s", plane.non_auto_terrain_disable?"OFF":"ON");330break;331332case AUX_FUNC::CROW_SELECT:333do_aux_function_crow_mode(ch_flag);334break;335336#if HAL_QUADPLANE_ENABLED337case AUX_FUNC::AIRMODE:338switch (ch_flag) {339case AuxSwitchPos::HIGH:340plane.quadplane.air_mode = AirMode::ON;341plane.quadplane.throttle_wait = false;342break;343case AuxSwitchPos::MIDDLE:344break;345case AuxSwitchPos::LOW:346plane.quadplane.air_mode = AirMode::OFF;347break;348}349break;350#endif351352#if AP_AIRSPEED_AUTOCAL_ENABLE353case AUX_FUNC::ARSPD_CALIBRATE:354switch (ch_flag) {355case AuxSwitchPos::HIGH:356plane.airspeed.set_calibration_enabled(true);357break;358case AuxSwitchPos::MIDDLE:359break;360case AuxSwitchPos::LOW:361plane.airspeed.set_calibration_enabled(false);362break;363}364break;365#endif366367case AUX_FUNC::LANDING_FLARE:368do_aux_function_flare(ch_flag);369break;370371#if HAL_PARACHUTE_ENABLED372case AUX_FUNC::PARACHUTE_RELEASE:373if (ch_flag == AuxSwitchPos::HIGH) {374plane.parachute_manual_release();375}376break;377#endif378379case AUX_FUNC::MODE_SWITCH_RESET:380rc().reset_mode_switch();381break;382383case AUX_FUNC::CRUISE:384do_aux_function_change_mode(Mode::Number::CRUISE, ch_flag);385break;386387#if HAL_QUADPLANE_ENABLED388case AUX_FUNC::ARMDISARM_AIRMODE:389RC_Channel::do_aux_function_armdisarm(ch_flag);390if (plane.arming.is_armed()) {391plane.quadplane.air_mode = AirMode::ON;392plane.quadplane.throttle_wait = false;393}394break;395396case AUX_FUNC::WEATHER_VANE_ENABLE: {397if (plane.quadplane.weathervane != nullptr) {398switch (ch_flag) {399case AuxSwitchPos::HIGH:400plane.quadplane.weathervane->allow_weathervaning(true);401break;402case AuxSwitchPos::MIDDLE:403// nothing404break;405case AuxSwitchPos::LOW:406plane.quadplane.weathervane->allow_weathervaning(false);407break;408}409}410break;411}412#endif413414case AUX_FUNC::PLANE_AUTO_LANDING_ABORT:415switch(ch_flag) {416case AuxSwitchPos::HIGH:417IGNORE_RETURN(plane.trigger_land_abort(0));418break;419case AuxSwitchPos::MIDDLE:420case AuxSwitchPos::LOW:421break;422}423break;424425case AUX_FUNC::TRIM_TO_CURRENT_SERVO_RC:426if (ch_flag == AuxSwitchPos::HIGH) {427plane.trim_radio();428}429break;430431case AUX_FUNC::EMERGENCY_LANDING_EN:432switch (ch_flag) {433case AuxSwitchPos::HIGH:434plane.emergency_landing = true;435break;436case AuxSwitchPos::MIDDLE:437break;438case AuxSwitchPos::LOW:439plane.emergency_landing = false;440break;441}442break;443444case AUX_FUNC::FW_AUTOTUNE:445if (ch_flag == AuxSwitchPos::HIGH && plane.control_mode->mode_allows_autotuning()) {446plane.autotune_enable(true);447} else if (ch_flag == AuxSwitchPos::HIGH) {448GCS_SEND_TEXT(MAV_SEVERITY_WARNING, "Autotuning not allowed in this mode!");449} else {450plane.autotune_enable(false);451}452break;453454case AUX_FUNC::PRECISION_LOITER:455// handled by lua scripting, just ignore here456break;457458#if QAUTOTUNE_ENABLED459case AUX_FUNC::AUTOTUNE_TEST_GAINS:460plane.quadplane.qautotune.do_aux_function(ch_flag);461break;462#endif463464#if AP_QUICKTUNE_ENABLED465case AUX_FUNC::QUICKTUNE:466plane.quicktune.update_switch_pos(ch_flag);467break;468#endif469470#if AP_ICENGINE_ENABLED471case AUX_FUNC::ICE_START_STOP:472plane.g2.ice_control.do_aux_function(trigger);473break;474#endif475476default:477return RC_Channel::do_aux_function(trigger);478}479480return true;481}482483484