2016-01-14 15:30:56 -04:00
|
|
|
#include "Sub.h"
|
2015-12-30 18:57:56 -04:00
|
|
|
|
|
|
|
/*****************************************************************************
|
|
|
|
* The init_ardupilot function processes everything we need for an in - air restart
|
|
|
|
* We will determine later if we are actually on the ground and process a
|
|
|
|
* ground start in that case.
|
|
|
|
*
|
|
|
|
*****************************************************************************/
|
|
|
|
|
|
|
|
static void failsafe_check_static()
|
|
|
|
{
|
2017-04-06 18:58:26 -03:00
|
|
|
sub.mainloop_failsafe_check();
|
2015-12-30 18:57:56 -04:00
|
|
|
}
|
|
|
|
|
2016-01-14 15:30:56 -04:00
|
|
|
void Sub::init_ardupilot()
|
2015-12-30 18:57:56 -04:00
|
|
|
{
|
|
|
|
BoardConfig.init();
|
2020-05-31 08:49:45 -03:00
|
|
|
#if HAL_MAX_CAN_PROTOCOL_DRIVERS
|
|
|
|
can_mgr.init();
|
2017-05-06 06:12:08 -03:00
|
|
|
#endif
|
2015-12-30 18:57:56 -04:00
|
|
|
|
2016-11-17 01:12:06 -04:00
|
|
|
// init cargo gripper
|
|
|
|
#if GRIPPER_ENABLED == ENABLED
|
|
|
|
g2.gripper.init();
|
2015-12-30 18:57:56 -04:00
|
|
|
#endif
|
|
|
|
|
2019-10-31 21:05:08 -03:00
|
|
|
#if AC_FENCE == ENABLED
|
2019-08-23 03:03:54 -03:00
|
|
|
fence.init();
|
2019-10-31 21:05:08 -03:00
|
|
|
#endif
|
2019-08-23 03:03:54 -03:00
|
|
|
|
2015-12-30 18:57:56 -04:00
|
|
|
// initialise notify system
|
2018-07-25 22:10:53 -03:00
|
|
|
notify.init();
|
2015-12-30 18:57:56 -04:00
|
|
|
|
|
|
|
// initialise battery monitor
|
|
|
|
battery.init();
|
2017-02-03 17:33:27 -04:00
|
|
|
|
2015-12-30 18:57:56 -04:00
|
|
|
barometer.init();
|
|
|
|
|
2020-12-02 06:15:44 -04:00
|
|
|
#if AP_FEATURE_BOARD_DETECT
|
|
|
|
// Detection won't work until after BoardConfig.init()
|
|
|
|
switch (AP_BoardConfig::get_board_type()) {
|
|
|
|
case AP_BoardConfig::PX4_BOARD_PIXHAWK2:
|
2021-07-12 16:37:59 -03:00
|
|
|
AP_Param::set_default_by_name("BARO_EXT_BUS", 0);
|
2020-12-02 06:15:44 -04:00
|
|
|
break;
|
2020-12-03 20:14:00 -04:00
|
|
|
case AP_BoardConfig::PX4_BOARD_PIXHAWK:
|
2021-07-12 16:37:59 -03:00
|
|
|
AP_Param::set_by_name("BARO_EXT_BUS", 1);
|
2020-12-03 20:14:00 -04:00
|
|
|
break;
|
2020-12-02 06:15:44 -04:00
|
|
|
default:
|
2021-07-12 16:37:59 -03:00
|
|
|
AP_Param::set_default_by_name("BARO_EXT_BUS", 1);
|
2020-12-02 06:15:44 -04:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
#else
|
2021-07-12 16:37:59 -03:00
|
|
|
AP_Param::set_default_by_name("BARO_EXT_BUS", 1);
|
2020-12-02 06:15:44 -04:00
|
|
|
#endif
|
|
|
|
celsius.init(barometer.external_bus());
|
|
|
|
|
2016-06-08 01:49:10 -03:00
|
|
|
// setup telem slots with serial ports
|
2019-06-19 01:25:04 -03:00
|
|
|
gcs().setup_uarts();
|
2015-12-30 18:57:56 -04:00
|
|
|
|
|
|
|
#if LOGGING_ENABLED == ENABLED
|
|
|
|
log_init();
|
|
|
|
#endif
|
|
|
|
|
2018-04-26 23:26:38 -03:00
|
|
|
// initialise rc channels including setting mode
|
|
|
|
rc().init();
|
|
|
|
|
2015-12-30 18:57:56 -04:00
|
|
|
init_rc_in(); // sets up rc channels from radio
|
|
|
|
init_rc_out(); // sets up motors and output to escs
|
2017-02-03 17:33:27 -04:00
|
|
|
init_joystick(); // joystick initialization
|
2015-12-30 18:57:56 -04:00
|
|
|
|
|
|
|
relay.init();
|
|
|
|
|
|
|
|
/*
|
|
|
|
* setup the 'main loop is dead' check. Note that this relies on
|
|
|
|
* the RC library being initialised.
|
|
|
|
*/
|
|
|
|
hal.scheduler->register_timer_failsafe(failsafe_check_static, 1000);
|
|
|
|
|
|
|
|
// Do GPS init
|
2017-06-27 05:13:32 -03:00
|
|
|
gps.set_log_gps_bit(MASK_LOG_GPS);
|
|
|
|
gps.init(serial_manager);
|
2015-12-30 18:57:56 -04:00
|
|
|
|
2019-03-25 09:43:12 -03:00
|
|
|
AP::compass().set_log_bit(MASK_LOG_COMPASS);
|
2019-03-24 00:27:18 -03:00
|
|
|
AP::compass().init();
|
2015-12-30 18:57:56 -04:00
|
|
|
|
2017-04-11 16:32:03 -03:00
|
|
|
#if OPTFLOW == ENABLED
|
2021-05-28 00:24:19 -03:00
|
|
|
// initialise optical flow sensor
|
|
|
|
optflow.init(MASK_LOG_OPTFLOW);
|
2017-04-11 16:32:03 -03:00
|
|
|
#endif
|
2015-12-30 18:57:56 -04:00
|
|
|
|
2020-07-24 14:30:21 -03:00
|
|
|
#if HAL_MOUNT_ENABLED
|
2015-12-30 18:57:56 -04:00
|
|
|
// initialise camera mount
|
2019-08-27 03:24:12 -03:00
|
|
|
camera_mount.init();
|
2019-11-20 12:40:23 -04:00
|
|
|
// This step ncessary so the servo is properly initialized
|
|
|
|
camera_mount.set_angle_targets(0, 0, 0);
|
|
|
|
// for some reason the call to set_angle_targets changes the mode to mavlink targeting!
|
|
|
|
camera_mount.set_mode(MAV_MOUNT_MODE_RC_TARGETING);
|
2015-12-30 18:57:56 -04:00
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef USERHOOK_INIT
|
|
|
|
USERHOOK_INIT
|
|
|
|
#endif
|
|
|
|
|
2017-05-03 19:13:06 -03:00
|
|
|
// Init baro and determine if we have external (depth) pressure sensor
|
2018-04-11 09:53:38 -03:00
|
|
|
barometer.set_log_baro_bit(MASK_LOG_IMU);
|
2018-03-18 01:18:59 -03:00
|
|
|
barometer.calibrate(false);
|
2017-02-03 00:18:27 -04:00
|
|
|
barometer.update();
|
2016-01-20 23:29:51 -04:00
|
|
|
|
2017-02-20 17:06:09 -04:00
|
|
|
for (uint8_t i = 0; i < barometer.num_instances(); i++) {
|
2017-04-15 02:03:56 -03:00
|
|
|
if (barometer.get_type(i) == AP_Baro::BARO_TYPE_WATER) {
|
2017-02-20 17:06:09 -04:00
|
|
|
barometer.set_primary_baro(i);
|
2017-04-15 02:03:56 -03:00
|
|
|
depth_sensor_idx = i;
|
2017-05-03 19:13:06 -03:00
|
|
|
ap.depth_sensor_present = true;
|
|
|
|
sensor_health.depth = barometer.healthy(depth_sensor_idx); // initialize health flag
|
|
|
|
break; // Go with the first one we find
|
2017-02-20 17:06:09 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-05-03 19:13:06 -03:00
|
|
|
if (!ap.depth_sensor_present) {
|
2017-02-20 17:06:09 -04:00
|
|
|
// We only have onboard baro
|
2017-02-03 17:33:27 -04:00
|
|
|
// No external underwater depth sensor detected
|
2017-02-20 17:06:09 -04:00
|
|
|
barometer.set_primary_baro(0);
|
2020-04-21 20:58:10 -03:00
|
|
|
ahrs.set_alt_measurement_noise(10.0f); // Readings won't correspond with rest of INS
|
2017-02-20 17:06:09 -04:00
|
|
|
} else {
|
2020-04-21 20:58:10 -03:00
|
|
|
ahrs.set_alt_measurement_noise(0.1f);
|
2017-02-03 17:33:27 -04:00
|
|
|
}
|
2017-02-03 00:18:27 -04:00
|
|
|
|
|
|
|
leak_detector.init();
|
|
|
|
|
2017-02-03 17:33:27 -04:00
|
|
|
last_pilot_heading = ahrs.yaw_sensor;
|
2016-09-05 14:21:46 -03:00
|
|
|
|
2016-05-22 21:50:44 -03:00
|
|
|
// initialise rangefinder
|
|
|
|
#if RANGEFINDER_ENABLED == ENABLED
|
|
|
|
init_rangefinder();
|
2015-12-30 18:57:56 -04:00
|
|
|
#endif
|
|
|
|
|
|
|
|
// initialise AP_RPM library
|
2016-11-25 18:58:31 -04:00
|
|
|
#if RPM_ENABLED == ENABLED
|
2015-12-30 18:57:56 -04:00
|
|
|
rpm_sensor.init();
|
2016-11-25 18:58:31 -04:00
|
|
|
#endif
|
2015-12-30 18:57:56 -04:00
|
|
|
|
|
|
|
// initialise mission library
|
|
|
|
mission.init();
|
|
|
|
|
2019-01-18 00:23:42 -04:00
|
|
|
// initialise AP_Logger library
|
2018-04-20 09:41:04 -03:00
|
|
|
#if LOGGING_ENABLED == ENABLED
|
2019-01-18 00:24:08 -04:00
|
|
|
logger.setVehicle_Startup_Writer(FUNCTOR_BIND(&sub, &Sub::Log_Write_Vehicle_Startup_Messages, void));
|
2018-04-20 09:41:04 -03:00
|
|
|
#endif
|
2017-05-01 03:14:32 -03:00
|
|
|
|
2015-12-30 18:57:56 -04:00
|
|
|
startup_INS_ground();
|
|
|
|
|
2019-03-01 02:41:05 -04:00
|
|
|
#ifdef ENABLE_SCRIPTING
|
2019-11-26 01:40:53 -04:00
|
|
|
g2.scripting.init();
|
2019-03-01 02:41:05 -04:00
|
|
|
#endif // ENABLE_SCRIPTING
|
|
|
|
|
2020-07-18 19:01:36 -03:00
|
|
|
g2.airspeed.init();
|
|
|
|
|
2015-12-30 18:57:56 -04:00
|
|
|
// we don't want writes to the serial port to cause us to pause
|
|
|
|
// mid-flight, so set the serial ports non-blocking once we are
|
|
|
|
// ready to fly
|
|
|
|
serial_manager.set_blocking_writes_all(false);
|
|
|
|
|
|
|
|
// enable CPU failsafe
|
2017-04-06 18:58:26 -03:00
|
|
|
mainloop_failsafe_enable();
|
2015-12-30 18:57:56 -04:00
|
|
|
|
2017-06-27 03:36:18 -03:00
|
|
|
ins.set_log_raw_bit(MASK_LOG_IMU_RAW);
|
2015-12-30 18:57:56 -04:00
|
|
|
|
|
|
|
// flag that initialisation has completed
|
|
|
|
ap.initialised = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
//******************************************************************************
|
|
|
|
//This function does all the calibrations, etc. that we need during a ground start
|
|
|
|
//******************************************************************************
|
2016-01-14 15:30:56 -04:00
|
|
|
void Sub::startup_INS_ground()
|
2015-12-30 18:57:56 -04:00
|
|
|
{
|
|
|
|
// initialise ahrs (may push imu calibration into the mpu6000 if using that device).
|
|
|
|
ahrs.init();
|
2017-02-18 12:09:13 -04:00
|
|
|
ahrs.set_vehicle_class(AHRS_VEHICLE_SUBMARINE);
|
2015-12-30 18:57:56 -04:00
|
|
|
|
|
|
|
// Warm up and calibrate gyro offsets
|
|
|
|
ins.init(scheduler.get_loop_rate_hz());
|
|
|
|
|
|
|
|
// reset ahrs including gyro bias
|
|
|
|
ahrs.reset();
|
|
|
|
}
|
|
|
|
|
2017-10-18 21:36:37 -03:00
|
|
|
// calibrate gyros - returns true if successfully calibrated
|
2015-12-30 18:57:56 -04:00
|
|
|
// position_ok - returns true if the horizontal absolute position is ok and home position is set
|
2016-01-14 15:30:56 -04:00
|
|
|
bool Sub::position_ok()
|
2015-12-30 18:57:56 -04:00
|
|
|
{
|
|
|
|
// return false if ekf failsafe has triggered
|
|
|
|
if (failsafe.ekf) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// check ekf position estimate
|
|
|
|
return (ekf_position_ok() || optflow_position_ok());
|
|
|
|
}
|
|
|
|
|
|
|
|
// ekf_position_ok - returns true if the ekf claims it's horizontal absolute position estimate is ok and home position is set
|
2016-01-14 15:30:56 -04:00
|
|
|
bool Sub::ekf_position_ok()
|
2015-12-30 18:57:56 -04:00
|
|
|
{
|
|
|
|
if (!ahrs.have_inertial_nav()) {
|
|
|
|
// do not allow navigation with dcm position
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// with EKF use filter status and ekf check
|
|
|
|
nav_filter_status filt_status = inertial_nav.get_filter_status();
|
|
|
|
|
|
|
|
// if disarmed we accept a predicted horizontal position
|
|
|
|
if (!motors.armed()) {
|
|
|
|
return ((filt_status.flags.horiz_pos_abs || filt_status.flags.pred_horiz_pos_abs));
|
|
|
|
}
|
2018-06-28 12:18:49 -03:00
|
|
|
|
|
|
|
// once armed we require a good absolute position and EKF must not be in const_pos_mode
|
|
|
|
return (filt_status.flags.horiz_pos_abs && !filt_status.flags.const_pos_mode);
|
2015-12-30 18:57:56 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// optflow_position_ok - returns true if optical flow based position estimate is ok
|
2016-01-14 15:30:56 -04:00
|
|
|
bool Sub::optflow_position_ok()
|
2015-12-30 18:57:56 -04:00
|
|
|
{
|
2020-04-06 01:14:10 -03:00
|
|
|
// return immediately if EKF not used
|
|
|
|
if (!ahrs.have_inertial_nav()) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// return immediately if neither optflow nor visual odometry is enabled
|
|
|
|
bool enabled = false;
|
|
|
|
#if OPTFLOW == ENABLED
|
|
|
|
if (optflow.enabled()) {
|
|
|
|
enabled = true;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
#if HAL_VISUALODOM_ENABLED
|
|
|
|
if (visual_odom.enabled()) {
|
|
|
|
enabled = true;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
if (!enabled) {
|
2015-12-30 18:57:56 -04:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// get filter status from EKF
|
|
|
|
nav_filter_status filt_status = inertial_nav.get_filter_status();
|
|
|
|
|
|
|
|
// if disarmed we accept a predicted horizontal relative position
|
|
|
|
if (!motors.armed()) {
|
|
|
|
return (filt_status.flags.pred_horiz_pos_rel);
|
|
|
|
}
|
2018-06-28 12:18:49 -03:00
|
|
|
return (filt_status.flags.horiz_pos_rel && !filt_status.flags.const_pos_mode);
|
2015-12-30 18:57:56 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
should we log a message type now?
|
|
|
|
*/
|
2016-01-14 15:30:56 -04:00
|
|
|
bool Sub::should_log(uint32_t mask)
|
2015-12-30 18:57:56 -04:00
|
|
|
{
|
|
|
|
#if LOGGING_ENABLED == ENABLED
|
2019-01-18 00:23:42 -04:00
|
|
|
ap.logging_started = logger.logging_started();
|
|
|
|
return logger.should_log(mask);
|
2015-12-30 18:57:56 -04:00
|
|
|
#else
|
|
|
|
return false;
|
|
|
|
#endif
|
|
|
|
}
|
2019-11-03 20:52:06 -04:00
|
|
|
|
|
|
|
#include <AP_AdvancedFailsafe/AP_AdvancedFailsafe.h>
|
2019-11-03 20:52:57 -04:00
|
|
|
#include <AP_Avoidance/AP_Avoidance.h>
|
|
|
|
#include <AP_ADSB/AP_ADSB.h>
|
2019-11-03 20:52:06 -04:00
|
|
|
|
|
|
|
// dummy method to avoid linking AFS
|
|
|
|
bool AP_AdvancedFailsafe::gcs_terminate(bool should_terminate, const char *reason) { return false; }
|
|
|
|
AP_AdvancedFailsafe *AP::advancedfailsafe() { return nullptr; }
|
2020-09-19 05:38:51 -03:00
|
|
|
#if HAL_ADSB_ENABLED
|
2019-11-03 20:52:57 -04:00
|
|
|
// dummy method to avoid linking AP_Avoidance
|
|
|
|
AP_Avoidance *AP::ap_avoidance() { return nullptr; }
|
2020-09-19 05:38:51 -03:00
|
|
|
#endif
|