#include "AP_Arming_Sub.h"
#include "Sub.h"
bool AP_Arming_Sub::rc_calibration_checks(bool display_failure)
{
const RC_Channel *channels[] = {
sub.channel_roll,
sub.channel_pitch,
sub.channel_throttle,
sub.channel_yaw
};
return rc_checks_copter_sub(display_failure, channels);
}
bool AP_Arming_Sub::has_disarm_function() const {
bool has_shift_function = false;
for (uint8_t i = 0; i < 16; i++) {
switch (sub.get_button(i)->function(false)) {
case JSButton::k_shift :
has_shift_function = true;
break;
case JSButton::k_arm_toggle :
return true;
case JSButton::k_disarm :
return true;
}
}
if (has_shift_function) {
for (uint8_t i = 0; i < 16; i++) {
switch (sub.get_button(i)->function(true)) {
case JSButton::k_arm_toggle :
case JSButton::k_disarm :
return true;
}
}
}
if (rc().find_channel_for_option(RC_Channel::AUX_FUNC::MOTOR_ESTOP) ||
rc().find_channel_for_option(RC_Channel::AUX_FUNC::DISARM) ||
rc().find_channel_for_option(RC_Channel::AUX_FUNC::ARMDISARM) ||
rc().find_channel_for_option(RC_Channel::AUX_FUNC::ARM_EMERGENCY_STOP)) {
return true;
}
return false;
}
bool AP_Arming_Sub::pre_arm_checks(bool display_failure)
{
if (armed) {
return true;
}
if (!has_disarm_function()) {
check_failed(display_failure, "Must assign a disarm or arm_toggle button or disarm aux function");
return false;
}
return AP_Arming::pre_arm_checks(display_failure);
}
bool AP_Arming_Sub::ins_checks(bool display_failure)
{
if (!AP_Arming::ins_checks(display_failure)) {
return false;
}
if (check_enabled(Check::INS)) {
char failure_msg[50] = {};
if (!AP::ahrs().pre_arm_check(false, failure_msg, sizeof(failure_msg))) {
check_failed(Check::INS, display_failure, "AHRS: %s", failure_msg);
return false;
}
}
return true;
}
bool AP_Arming_Sub::arm(AP_Arming::Method method, bool do_arming_checks)
{
static bool in_arm_motors = false;
if (in_arm_motors) {
return false;
}
in_arm_motors = true;
if (check_enabled(Check::RC) &&
rc().option_is_enabled(RC_Channels::Option::ARMING_CHECK_THROTTLE) &&
(sub.g.thr_arming_position == WITHIN_THR_TRIM)) {
const char *rc_item = "Throttle";
if (!sub.channel_throttle->in_trim_dz()) {
check_failed(Check::RC, true, "%s not centered/close to trim", rc_item);
AP_Notify::events.arming_failed = true;
in_arm_motors = false;
return false;
}
}
if (!AP_Arming::arm(method, do_arming_checks)) {
AP_Notify::events.arming_failed = true;
in_arm_motors = false;
return false;
}
#if HAL_LOGGING_ENABLED
AP::logger().set_vehicle_armed(true);
#endif
sub.mainloop_failsafe_disable();
AP_Notify::flags.armed = true;
for (uint8_t i=0; i<=10; i++) {
AP::notify().update();
}
send_arm_disarm_statustext("Arming motors");
AP_AHRS &ahrs = AP::ahrs();
sub.initial_armed_bearing = ahrs.yaw_sensor;
if (!ahrs.home_is_set()) {
} else if (!ahrs.home_is_locked()) {
if (!sub.set_home_to_current_location(false)) {
}
}
hal.util->set_soft_armed(true);
sub.enable_motor_output();
sub.motors.armed(true);
#if HAL_LOGGING_ENABLED
AP::logger().Write_Mode((uint8_t)sub.control_mode, sub.control_mode_reason);
#endif
sub.mainloop_failsafe_enable();
AP::scheduler().perf_info.ignore_this_loop();
in_arm_motors = false;
Location origin_loc;
if (!ahrs.get_origin(origin_loc)) {
gcs().send_text(MAV_SEVERITY_WARNING, "Compass performance degraded");
if (check_enabled(Check::PARAMETERS)) {
check_failed(Check::PARAMETERS, true, "No world position, check AHRS_ORIGIN_* parameters");
return false;
}
}
return true;
}
bool AP_Arming_Sub::disarm(const AP_Arming::Method method, bool do_disarm_checks)
{
if (!sub.motors.armed()) {
return false;
}
if (!AP_Arming::disarm(method, do_disarm_checks)) {
return false;
}
send_arm_disarm_statustext("Disarming motors");
auto &ahrs = AP::ahrs();
if (ahrs.use_compass() && AP::compass().get_learn_type() == Compass::LearnType::COPY_FROM_EKF) {
for (uint8_t i=0; i<COMPASS_MAX_INSTANCES; i++) {
Vector3f magOffsets;
if (ahrs.getMagOffsets(i, magOffsets)) {
AP::compass().set_and_save_offsets(i, magOffsets);
}
}
}
sub.motors.armed(false);
sub.mission.reset();
#if HAL_LOGGING_ENABLED
AP::logger().set_vehicle_armed(false);
#endif
hal.util->set_soft_armed(false);
sub.clear_input_hold();
return true;
}