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/Blimp/RC_Channel_Blimp.cpp
Views: 1798
#include "Blimp.h"12#include "RC_Channel_Blimp.h"345// defining these two macros and including the RC_Channels_VarInfo header defines the parameter information common to all vehicle types6#define RC_CHANNELS_SUBCLASS RC_Channels_Blimp7#define RC_CHANNEL_SUBCLASS RC_Channel_Blimp89#include <RC_Channel/RC_Channels_VarInfo.h>1011int8_t RC_Channels_Blimp::flight_mode_channel_number() const12{13return blimp.g.flight_mode_chan.get();14}1516void RC_Channel_Blimp::mode_switch_changed(modeswitch_pos_t new_pos)17{18if (new_pos < 0 || (uint8_t)new_pos > blimp.num_flight_modes) {19// should not have been called20return;21}2223if (!blimp.set_mode((Mode::Number)blimp.flight_modes[new_pos].get(), ModeReason::RC_COMMAND)) {24// alert user to mode change failure25if (blimp.ap.initialised) {26AP_Notify::events.user_mode_change_failed = 1;27}28return;29}3031// play a tone32// alert user to mode change (except if autopilot is just starting up)33if (blimp.ap.initialised) {34AP_Notify::events.user_mode_change = 1;35}36}3738bool RC_Channels_Blimp::in_rc_failsafe() const39{40return blimp.failsafe.radio;41}4243bool RC_Channels_Blimp::has_valid_input() const44{45if (blimp.failsafe.radio) {46return false;47}48if (blimp.failsafe.radio_counter != 0) {49return false;50}51return true;52}5354RC_Channel * RC_Channels_Blimp::get_arming_channel(void) const55{56return blimp.channel_yaw;57}5859// init_aux_switch_function - initialize aux functions60void RC_Channel_Blimp::init_aux_function(const AUX_FUNC ch_option, const AuxSwitchPos ch_flag)61{62// init channel options63switch (ch_option) {64// the following functions do not need to be initialised:65case AUX_FUNC::MANUAL:66break;67default:68RC_Channel::init_aux_function(ch_option, ch_flag);69break;70}71}7273// do_aux_function_change_mode - change mode based on an aux switch74// being moved75void RC_Channel_Blimp::do_aux_function_change_mode(const Mode::Number mode,76const AuxSwitchPos ch_flag)77{78switch (ch_flag) {79case AuxSwitchPos::HIGH: {80// engage mode (if not possible we remain in current flight mode)81const bool success = blimp.set_mode(mode, ModeReason::AUX_FUNCTION);82if (blimp.ap.initialised) {83if (success) {84AP_Notify::events.user_mode_change = 1;85} else {86AP_Notify::events.user_mode_change_failed = 1;87}88}89break;90}91default:92// return to flight mode switch's flight mode if we are currently93// in this mode94if (blimp.control_mode == mode) {95rc().reset_mode_switch();96}97}98}99100// do_aux_function - implement the function invoked by auxiliary switches101bool RC_Channel_Blimp::do_aux_function(const AuxFuncTrigger &trigger)102{103const AUX_FUNC &ch_option = trigger.func;104const AuxSwitchPos &ch_flag = trigger.pos;105106switch (ch_option) {107108case AUX_FUNC::SAVE_TRIM:109if ((ch_flag == AuxSwitchPos::HIGH) &&110(blimp.control_mode <= Mode::Number::MANUAL) &&111(blimp.channel_up->get_control_in() == 0)) {112blimp.save_trim();113}114break;115116case AUX_FUNC::LOITER:117do_aux_function_change_mode(Mode::Number::LOITER, ch_flag);118break;119120case AUX_FUNC::MANUAL:121do_aux_function_change_mode(Mode::Number::MANUAL, ch_flag);122break;123124default:125return RC_Channel::do_aux_function(trigger);126}127return true;128}129130// save_trim - adds roll and pitch trims from the radio to ahrs131void Blimp::save_trim()132{133// save roll and pitch trim134float roll_trim = ToRad((float)channel_right->get_control_in()*0.01f);135float pitch_trim = ToRad((float)channel_front->get_control_in()*0.01f);136ahrs.add_trim(roll_trim, pitch_trim);137LOGGER_WRITE_EVENT(LogEvent::SAVE_TRIM);138gcs().send_text(MAV_SEVERITY_INFO, "Trim saved");139}140141// auto_trim - slightly adjusts the ahrs.roll_trim and ahrs.pitch_trim towards the current stick positions142// meant to be called continuously while the pilot attempts to keep the blimp level143void Blimp::auto_trim_cancel()144{145auto_trim_counter = 0;146AP_Notify::flags.save_trim = false;147gcs().send_text(MAV_SEVERITY_INFO, "AutoTrim cancelled");148}149150void Blimp::auto_trim()151{152if (auto_trim_counter > 0) {153if (blimp.flightmode != &blimp.mode_manual ||154!blimp.motors->armed()) {155auto_trim_cancel();156return;157}158159// flash the leds160AP_Notify::flags.save_trim = true;161162if (!auto_trim_started) {163if (ap.land_complete) {164// haven't taken off yet165return;166}167auto_trim_started = true;168}169170if (ap.land_complete) {171// landed again.172auto_trim_cancel();173return;174}175176auto_trim_counter--;177178// calculate roll trim adjustment179float roll_trim_adjustment = ToRad((float)channel_right->get_control_in() / 4000.0f);180181// calculate pitch trim adjustment182float pitch_trim_adjustment = ToRad((float)channel_front->get_control_in() / 4000.0f);183184// add trim to ahrs object185// save to eeprom on last iteration186ahrs.add_trim(roll_trim_adjustment, pitch_trim_adjustment, (auto_trim_counter == 0));187188// on last iteration restore leds and accel gains to normal189if (auto_trim_counter == 0) {190AP_Notify::flags.save_trim = false;191gcs().send_text(MAV_SEVERITY_INFO, "AutoTrim: Trims saved");192}193}194}195196197