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/control_modes.cpp
Views: 1798
#include "Plane.h"12#include "quadplane.h"3#include "qautotune.h"45Mode *Plane::mode_from_mode_num(const enum Mode::Number num)6{7Mode *ret = nullptr;8switch (num) {9case Mode::Number::MANUAL:10ret = &mode_manual;11break;12case Mode::Number::CIRCLE:13ret = &mode_circle;14break;15case Mode::Number::STABILIZE:16ret = &mode_stabilize;17break;18case Mode::Number::TRAINING:19ret = &mode_training;20break;21case Mode::Number::ACRO:22ret = &mode_acro;23break;24case Mode::Number::FLY_BY_WIRE_A:25ret = &mode_fbwa;26break;27case Mode::Number::FLY_BY_WIRE_B:28ret = &mode_fbwb;29break;30case Mode::Number::CRUISE:31ret = &mode_cruise;32break;33case Mode::Number::AUTOTUNE:34ret = &mode_autotune;35break;36case Mode::Number::AUTO:37ret = &mode_auto;38break;39case Mode::Number::RTL:40ret = &mode_rtl;41break;42case Mode::Number::LOITER:43ret = &mode_loiter;44break;45case Mode::Number::AVOID_ADSB:46#if HAL_ADSB_ENABLED47ret = &mode_avoidADSB;48break;49#endif50// if ADSB is not compiled in then fallthrough to guided51case Mode::Number::GUIDED:52ret = &mode_guided;53break;54case Mode::Number::INITIALISING:55ret = &mode_initializing;56break;57#if HAL_QUADPLANE_ENABLED58case Mode::Number::QSTABILIZE:59ret = &mode_qstabilize;60break;61case Mode::Number::QHOVER:62ret = &mode_qhover;63break;64case Mode::Number::QLOITER:65ret = &mode_qloiter;66break;67case Mode::Number::QLAND:68ret = &mode_qland;69break;70case Mode::Number::QRTL:71ret = &mode_qrtl;72break;73case Mode::Number::QACRO:74ret = &mode_qacro;75break;76#if QAUTOTUNE_ENABLED77case Mode::Number::QAUTOTUNE:78ret = &mode_qautotune;79break;80#endif81#endif // HAL_QUADPLANE_ENABLED82case Mode::Number::TAKEOFF:83ret = &mode_takeoff;84break;85#if MODE_AUTOLAND_ENABLED86case Mode::Number::AUTOLAND:87ret = &mode_autoland;88break;89#endif //MODE_AUTOLAND_ENABLED90case Mode::Number::THERMAL:91#if HAL_SOARING_ENABLED92ret = &mode_thermal;93#endif94break;95#if HAL_QUADPLANE_ENABLED96case Mode::Number::LOITER_ALT_QLAND:97ret = &mode_loiter_qland;98break;99#endif // HAL_QUADPLANE_ENABLED100101}102return ret;103}104105void RC_Channels_Plane::read_mode_switch()106{107if (millis() - plane.failsafe.last_valid_rc_ms > 100) {108// only use signals that are less than 0.1s old.109return;110}111RC_Channels::read_mode_switch();112}113114void RC_Channel_Plane::mode_switch_changed(modeswitch_pos_t new_pos)115{116if (new_pos < 0 || (uint8_t)new_pos > plane.num_flight_modes) {117// should not have been called118return;119}120121plane.set_mode_by_number((Mode::Number)plane.flight_modes[new_pos].get(), ModeReason::RC_COMMAND);122}123124/*125called when entering autotune126*/127void Plane::autotune_start(void)128{129const bool tune_roll = g2.axis_bitmask.get() & int8_t(AutoTuneAxis::ROLL);130const bool tune_pitch = g2.axis_bitmask.get() & int8_t(AutoTuneAxis::PITCH);131const bool tune_yaw = g2.axis_bitmask.get() & int8_t(AutoTuneAxis::YAW);132if (tune_roll || tune_pitch || tune_yaw) {133gcs().send_text(MAV_SEVERITY_INFO, "Started autotune");134if (tune_roll) {135rollController.autotune_start();136}137if (tune_pitch) {138pitchController.autotune_start();139}140if (tune_yaw) {141yawController.autotune_start();142}143autotuning = true;144gcs().send_text(MAV_SEVERITY_INFO, "Autotuning %s%s%s", tune_roll?"roll ":"", tune_pitch?"pitch ":"", tune_yaw?"yaw":"");145} else {146gcs().send_text(MAV_SEVERITY_INFO, "No axis selected for tuning!");147}148}149150/*151called when exiting autotune152*/153void Plane::autotune_restore(void)154{155rollController.autotune_restore();156pitchController.autotune_restore();157yawController.autotune_restore();158if (autotuning) {159autotuning = false;160gcs().send_text(MAV_SEVERITY_INFO, "Stopped autotune");161}162}163164/*165enable/disable autotune for AUTO modes166*/167void Plane::autotune_enable(bool enable)168{169if (enable) {170autotune_start();171} else {172autotune_restore();173}174}175176/*177are we flying inverted?178*/179bool Plane::fly_inverted(void)180{181if (control_mode == &plane.mode_manual) {182return false;183}184if (inverted_flight) {185// controlled with aux switch186return true;187}188if (control_mode == &mode_auto && auto_state.inverted_flight) {189return true;190}191return false;192}193194195