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 mavlink_delay_cb_static()
|
|
|
|
{
|
2016-01-14 15:30:56 -04:00
|
|
|
sub.mavlink_delay_cb();
|
2015-12-30 18:57:56 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
{
|
|
|
|
// initialise serial port
|
|
|
|
serial_manager.init_console();
|
|
|
|
|
2017-08-19 06:51:55 -03:00
|
|
|
hal.console->printf("\n\nInit %s"
|
|
|
|
"\n\nFree RAM: %u\n",
|
2018-06-13 07:27:47 -03:00
|
|
|
AP::fwversion().fw_string,
|
2017-08-19 06:51:55 -03:00
|
|
|
(unsigned)hal.util->available_memory());
|
2015-12-30 18:57:56 -04:00
|
|
|
|
|
|
|
// load parameters from EEPROM
|
|
|
|
load_parameters();
|
|
|
|
|
|
|
|
BoardConfig.init();
|
2017-05-06 06:12:08 -03:00
|
|
|
#if HAL_WITH_UAVCAN
|
|
|
|
BoardConfig_CAN.init();
|
|
|
|
#endif
|
2015-12-30 18:57:56 -04:00
|
|
|
|
2018-01-10 15:03:21 -04:00
|
|
|
#if AP_FEATURE_BOARD_DETECT
|
2017-08-18 18:11:03 -03:00
|
|
|
// Detection won't work until after BoardConfig.init()
|
|
|
|
switch (AP_BoardConfig::get_board_type()) {
|
|
|
|
case AP_BoardConfig::PX4_BOARD_PIXHAWK2:
|
2017-10-20 15:11:05 -03:00
|
|
|
AP_Param::set_by_name("GND_EXT_BUS", 0);
|
2017-08-18 18:11:03 -03:00
|
|
|
break;
|
|
|
|
default:
|
2017-10-20 15:11:05 -03:00
|
|
|
AP_Param::set_by_name("GND_EXT_BUS", 1);
|
2017-08-18 18:11:03 -03:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
#else
|
|
|
|
AP_Param::set_default_by_name("GND_EXT_BUS", 1);
|
|
|
|
#endif
|
|
|
|
|
2017-06-09 00:43:07 -03:00
|
|
|
// identify ourselves correctly with the ground station
|
|
|
|
mavlink_system.sysid = g.sysid_this_mav;
|
|
|
|
|
2015-12-30 18:57:56 -04:00
|
|
|
// initialise serial port
|
|
|
|
serial_manager.init();
|
|
|
|
|
2017-02-21 20:54:56 -04:00
|
|
|
// setup first port early to allow BoardConfig to report errors
|
|
|
|
gcs().chan(0).setup_uart(serial_manager, AP_SerialManager::SerialProtocol_MAVLink, 0);
|
|
|
|
|
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
|
|
|
|
|
|
|
|
// initialise notify system
|
|
|
|
notify.init(true);
|
|
|
|
|
|
|
|
// initialise battery monitor
|
|
|
|
battery.init();
|
2017-02-03 17:33:27 -04:00
|
|
|
|
2015-12-30 18:57:56 -04:00
|
|
|
barometer.init();
|
|
|
|
|
2017-01-13 15:51:48 -04:00
|
|
|
celsius.init();
|
|
|
|
|
2015-12-30 18:57:56 -04:00
|
|
|
// Register the mavlink service callback. This will run
|
|
|
|
// anytime there are more than 5ms remaining in a call to
|
|
|
|
// hal.scheduler->delay.
|
|
|
|
hal.scheduler->register_delay_callback(mavlink_delay_cb_static, 5);
|
|
|
|
|
2016-06-08 01:49:10 -03:00
|
|
|
// setup telem slots with serial ports
|
2017-02-21 20:54:56 -04:00
|
|
|
gcs().setup_uarts(serial_manager);
|
2015-12-30 18:57:56 -04:00
|
|
|
|
|
|
|
#if LOGGING_ENABLED == ENABLED
|
|
|
|
log_init();
|
|
|
|
#endif
|
|
|
|
|
2017-02-14 01:51:38 -04:00
|
|
|
gcs().set_dataflash(&DataFlash);
|
2016-02-24 15:10:07 -04:00
|
|
|
|
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
|
|
|
|
|
|
|
// initialise which outputs Servo and Relay events can use
|
|
|
|
ServoRelayEvents.set_channel_mask(~motors.get_motor_mask());
|
|
|
|
|
|
|
|
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
|
|
|
|
2017-02-03 17:33:27 -04:00
|
|
|
if (g.compass_enabled) {
|
2015-12-30 18:57:56 -04:00
|
|
|
init_compass();
|
2017-02-03 17:33:27 -04:00
|
|
|
}
|
2015-12-30 18:57:56 -04:00
|
|
|
|
|
|
|
#if OPTFLOW == ENABLED
|
|
|
|
// make optflow available to AHRS
|
|
|
|
ahrs.set_optflow(&optflow);
|
|
|
|
#endif
|
|
|
|
|
2016-05-03 01:45:37 -03:00
|
|
|
// init Location class
|
|
|
|
#if AP_TERRAIN_AVAILABLE && AC_TERRAIN
|
2017-02-03 17:33:27 -04:00
|
|
|
Location_Class::set_terrain(&terrain);
|
|
|
|
wp_nav.set_terrain(&terrain);
|
2016-05-03 01:45:37 -03:00
|
|
|
#endif
|
2016-12-04 12:49:46 -04:00
|
|
|
|
|
|
|
#if AVOIDANCE_ENABLED == ENABLED
|
2017-02-03 17:33:27 -04:00
|
|
|
wp_nav.set_avoidance(&avoid);
|
2018-03-28 01:54:24 -03:00
|
|
|
loiter_nav.set_avoidance(&avoid);
|
2016-12-04 12:49:46 -04:00
|
|
|
#endif
|
2016-07-05 21:18:58 -03:00
|
|
|
|
2015-12-30 18:57:56 -04:00
|
|
|
pos_control.set_dt(MAIN_LOOP_SECONDS);
|
|
|
|
|
|
|
|
// init the optical flow sensor
|
2017-04-11 16:32:03 -03:00
|
|
|
#if OPTFLOW == ENABLED
|
2015-12-30 18:57:56 -04:00
|
|
|
init_optflow();
|
2017-04-11 16:32:03 -03:00
|
|
|
#endif
|
2015-12-30 18:57:56 -04:00
|
|
|
|
|
|
|
#if MOUNT == ENABLED
|
|
|
|
// initialise camera mount
|
2017-08-23 21:59:47 -03:00
|
|
|
camera_mount.init(serial_manager);
|
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);
|
2017-02-03 17:33:27 -04:00
|
|
|
EKF2.set_baro_alt_noise(10.0f); // Readings won't correspond with rest of INS
|
|
|
|
EKF3.set_baro_alt_noise(10.0f);
|
2017-02-20 17:06:09 -04:00
|
|
|
} else {
|
|
|
|
EKF2.set_baro_alt_noise(0.1f);
|
|
|
|
EKF3.set_baro_alt_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();
|
|
|
|
|
2017-05-01 03:14:32 -03:00
|
|
|
// initialise DataFlash library
|
2018-04-20 09:41:04 -03:00
|
|
|
#if LOGGING_ENABLED == ENABLED
|
2017-05-01 03:14:32 -03:00
|
|
|
DataFlash.set_mission(&mission);
|
|
|
|
DataFlash.setVehicle_Startup_Log_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();
|
|
|
|
|
|
|
|
// 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
|
|
|
|
|
|
|
// init vehicle capabilties
|
|
|
|
init_capabilities();
|
|
|
|
|
2017-04-29 21:48:01 -03:00
|
|
|
// disable safety if requested
|
|
|
|
BoardConfig.init_safety();
|
|
|
|
|
2017-04-13 19:53:48 -03:00
|
|
|
hal.console->print("\nInit complete");
|
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));
|
|
|
|
} else {
|
|
|
|
// 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);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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
|
|
|
{
|
|
|
|
#if OPTFLOW != ENABLED
|
|
|
|
return false;
|
|
|
|
#else
|
|
|
|
// return immediately if optflow is not enabled or EKF not used
|
|
|
|
if (!optflow.enabled() || !ahrs.have_inertial_nav()) {
|
|
|
|
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);
|
|
|
|
} else {
|
|
|
|
return (filt_status.flags.horiz_pos_rel && !filt_status.flags.const_pos_mode);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
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
|
2017-06-30 08:35:49 -03:00
|
|
|
ap.logging_started = DataFlash.logging_started();
|
|
|
|
return DataFlash.should_log(mask);
|
2015-12-30 18:57:56 -04:00
|
|
|
#else
|
|
|
|
return false;
|
|
|
|
#endif
|
|
|
|
}
|