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/ArduSub/AP_Arming_Sub.cpp
Views: 1798
#include "AP_Arming_Sub.h"1#include "Sub.h"23bool AP_Arming_Sub::rc_calibration_checks(bool display_failure)4{5const RC_Channel *channels[] = {6sub.channel_roll,7sub.channel_pitch,8sub.channel_throttle,9sub.channel_yaw10};11return rc_checks_copter_sub(display_failure, channels);12}1314bool AP_Arming_Sub::has_disarm_function() const {15bool has_shift_function = false;16// make sure the craft has a disarm button assigned before it is armed17// check all the standard btn functions18for (uint8_t i = 0; i < 16; i++) {19switch (sub.get_button(i)->function(false)) {20case JSButton::k_shift :21has_shift_function = true;22break;23case JSButton::k_arm_toggle :24return true;25case JSButton::k_disarm :26return true;27}28}2930// check all the shift functions if there's shift assigned31if (has_shift_function) {32for (uint8_t i = 0; i < 16; i++) {33switch (sub.get_button(i)->function(true)) {34case JSButton::k_arm_toggle :35case JSButton::k_disarm :36return true;37}38}39}40return false;41}4243bool AP_Arming_Sub::pre_arm_checks(bool display_failure)44{45if (armed) {46return true;47}48// don't allow arming unless there is a disarm button configured49if (!has_disarm_function()) {50check_failed(display_failure, "Must assign a disarm or arm_toggle button");51return false;52}5354return AP_Arming::pre_arm_checks(display_failure);55}5657bool AP_Arming_Sub::ins_checks(bool display_failure)58{59// call parent class checks60if (!AP_Arming::ins_checks(display_failure)) {61return false;62}6364// additional sub-specific checks65if (check_enabled(ARMING_CHECK_INS)) {66char failure_msg[50] = {};67if (!AP::ahrs().pre_arm_check(false, failure_msg, sizeof(failure_msg))) {68check_failed(ARMING_CHECK_INS, display_failure, "AHRS: %s", failure_msg);69return false;70}71}7273return true;74}7576bool AP_Arming_Sub::arm(AP_Arming::Method method, bool do_arming_checks)77{78static bool in_arm_motors = false;7980// exit immediately if already in this function81if (in_arm_motors) {82return false;83}8485in_arm_motors = true;8687if (!AP_Arming::arm(method, do_arming_checks)) {88AP_Notify::events.arming_failed = true;89in_arm_motors = false;90return false;91}9293#if HAL_LOGGING_ENABLED94// let logger know that we're armed (it may open logs e.g.)95AP::logger().set_vehicle_armed(true);96#endif9798// disable cpu failsafe because initialising everything takes a while99sub.mainloop_failsafe_disable();100101// notify that arming will occur (we do this early to give plenty of warning)102AP_Notify::flags.armed = true;103// call notify update a few times to ensure the message gets out104for (uint8_t i=0; i<=10; i++) {105AP::notify().update();106}107108#if CONFIG_HAL_BOARD == HAL_BOARD_SITL109send_arm_disarm_statustext("Arming motors");110#endif111112AP_AHRS &ahrs = AP::ahrs();113114sub.initial_armed_bearing = ahrs.yaw_sensor;115116if (!ahrs.home_is_set()) {117// Reset EKF altitude if home hasn't been set yet (we use EKF altitude as substitute for alt above home)118119// Always use absolute altitude for ROV120// ahrs.resetHeightDatum();121// AP::logger().Write_Event(LogEvent::EKF_ALT_RESET);122} else if (!ahrs.home_is_locked()) {123// Reset home position if it has already been set before (but not locked)124if (!sub.set_home_to_current_location(false)) {125// ignore this failure126}127}128129hal.util->set_soft_armed(true);130131// enable output to motors132sub.enable_motor_output();133134// finally actually arm the motors135sub.motors.armed(true);136137#if HAL_LOGGING_ENABLED138// log flight mode in case it was changed while vehicle was disarmed139AP::logger().Write_Mode((uint8_t)sub.control_mode, sub.control_mode_reason);140#endif141142// reenable failsafe143sub.mainloop_failsafe_enable();144145// perf monitor ignores delay due to arming146AP::scheduler().perf_info.ignore_this_loop();147148// flag exiting this function149in_arm_motors = false;150151// if we do not have an ekf origin then we can't use the WMM tables152if (!sub.ensure_ekf_origin()) {153gcs().send_text(MAV_SEVERITY_WARNING, "Compass performance degraded");154if (check_enabled(ARMING_CHECK_PARAMETERS)) {155check_failed(ARMING_CHECK_PARAMETERS, true, "No world position, check ORIGIN_* parameters");156return false;157}158}159// return success160return true;161}162163bool AP_Arming_Sub::disarm(const AP_Arming::Method method, bool do_disarm_checks)164{165// return immediately if we are already disarmed166if (!sub.motors.armed()) {167return false;168}169170if (!AP_Arming::disarm(method, do_disarm_checks)) {171return false;172}173174#if CONFIG_HAL_BOARD == HAL_BOARD_SITL175send_arm_disarm_statustext("Disarming motors");176#endif177178auto &ahrs = AP::ahrs();179180// save compass offsets learned by the EKF if enabled181if (ahrs.use_compass() && AP::compass().get_learn_type() == Compass::LEARN_EKF) {182for (uint8_t i=0; i<COMPASS_MAX_INSTANCES; i++) {183Vector3f magOffsets;184if (ahrs.getMagOffsets(i, magOffsets)) {185AP::compass().set_and_save_offsets(i, magOffsets);186}187}188}189190// send disarm command to motors191sub.motors.armed(false);192193// reset the mission194sub.mission.reset();195196#if HAL_LOGGING_ENABLED197AP::logger().set_vehicle_armed(false);198#endif199200hal.util->set_soft_armed(false);201202// clear input holds203sub.clear_input_hold();204205return true;206}207208209