2015-05-29 23:12:49 -03:00
|
|
|
#include "Copter.h"
|
2019-10-12 09:04:45 -03:00
|
|
|
#include <AP_BLHeli/AP_BLHeli.h>
|
2015-05-29 23:12:49 -03:00
|
|
|
|
2011-04-16 17:44:44 -03:00
|
|
|
/*****************************************************************************
|
2012-08-21 23:19:50 -03: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.
|
|
|
|
*
|
2011-04-16 17:44:44 -03:00
|
|
|
*****************************************************************************/
|
|
|
|
|
2015-05-29 23:12:49 -03:00
|
|
|
static void mavlink_delay_cb_static()
|
|
|
|
{
|
|
|
|
copter.mavlink_delay_cb();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static void failsafe_check_static()
|
|
|
|
{
|
|
|
|
copter.failsafe_check();
|
|
|
|
}
|
|
|
|
|
|
|
|
void Copter::init_ardupilot()
|
2011-04-16 17:44:44 -03:00
|
|
|
{
|
2015-01-19 09:52:54 -04:00
|
|
|
// initialise serial port
|
|
|
|
serial_manager.init_console();
|
2014-02-25 18:40:29 -04:00
|
|
|
|
2017-08-18 19:45:02 -03:00
|
|
|
hal.console->printf("\n\nInit %s"
|
2017-08-11 01:08:41 -03:00
|
|
|
"\n\nFree RAM: %u\n",
|
2018-06-13 08:28:01 -03:00
|
|
|
AP::fwversion().fw_string,
|
2017-08-11 01:08:41 -03:00
|
|
|
(unsigned)hal.util->available_memory());
|
2011-04-16 17:44:44 -03:00
|
|
|
|
2012-08-21 23:19:50 -03:00
|
|
|
//
|
2012-09-13 09:49:11 -03:00
|
|
|
// Report firmware version code expect on console (check of actual EEPROM format version is done in load_parameters function)
|
2012-08-21 23:19:50 -03:00
|
|
|
//
|
|
|
|
report_version();
|
2011-04-16 17:44:44 -03:00
|
|
|
|
2012-02-12 07:27:51 -04:00
|
|
|
// load parameters from EEPROM
|
|
|
|
load_parameters();
|
2011-04-16 17:44:44 -03:00
|
|
|
|
2018-02-13 01:46:47 -04:00
|
|
|
// time per loop - this gets updated in the main loop() based on
|
|
|
|
// actual loop rate
|
2018-02-13 10:34:25 -04:00
|
|
|
G_Dt = 1.0 / scheduler.get_loop_rate_hz();
|
2017-08-29 09:42:44 -03:00
|
|
|
|
2018-02-28 08:20:57 -04:00
|
|
|
#if STATS_ENABLED == ENABLED
|
2016-10-13 03:30:30 -03:00
|
|
|
// initialise stats module
|
|
|
|
g2.stats.init();
|
2018-02-28 08:20:57 -04:00
|
|
|
#endif
|
2016-10-13 03:30:30 -03:00
|
|
|
|
2016-10-16 19:19:08 -03:00
|
|
|
// identify ourselves correctly with the ground station
|
|
|
|
mavlink_system.sysid = g.sysid_this_mav;
|
|
|
|
|
2016-08-03 04:17:31 -03:00
|
|
|
// initialise serial ports
|
2015-01-19 09:52:54 -04:00
|
|
|
serial_manager.init();
|
|
|
|
|
2016-08-03 04:17:31 -03:00
|
|
|
// setup first port early to allow BoardConfig to report errors
|
2019-06-18 07:38:00 -03:00
|
|
|
gcs().setup_console();
|
2017-02-13 22:14:55 -04:00
|
|
|
|
2016-08-03 04:17:31 -03:00
|
|
|
// Register mavlink_delay_cb, which will run anytime you have
|
|
|
|
// more than 5ms remaining in your call to hal.scheduler->delay
|
|
|
|
hal.scheduler->register_delay_callback(mavlink_delay_cb_static, 5);
|
|
|
|
|
|
|
|
BoardConfig.init();
|
2017-05-06 06:11:50 -03:00
|
|
|
#if HAL_WITH_UAVCAN
|
|
|
|
BoardConfig_CAN.init();
|
|
|
|
#endif
|
2016-08-03 04:17:31 -03:00
|
|
|
|
2016-10-28 19:08:22 -03:00
|
|
|
// init cargo gripper
|
|
|
|
#if GRIPPER_ENABLED == ENABLED
|
|
|
|
g2.gripper.init();
|
|
|
|
#endif
|
|
|
|
|
2020-06-09 12:03:31 -03:00
|
|
|
#if AC_FENCE == ENABLED
|
2019-08-23 02:58:16 -03:00
|
|
|
fence.init();
|
2020-06-09 12:03:31 -03:00
|
|
|
#endif
|
|
|
|
|
2017-10-04 23:21:23 -03:00
|
|
|
// init winch and wheel encoder
|
|
|
|
winch_init();
|
|
|
|
|
2013-12-19 01:12:19 -04:00
|
|
|
// initialise notify system
|
2018-07-25 22:10:47 -03:00
|
|
|
notify.init();
|
2016-03-20 22:19:11 -03:00
|
|
|
notify_flight_mode();
|
2013-12-19 01:12:19 -04:00
|
|
|
|
|
|
|
// initialise battery monitor
|
|
|
|
battery.init();
|
|
|
|
|
2015-08-28 03:00:55 -03:00
|
|
|
// Init RSSI
|
|
|
|
rssi.init();
|
|
|
|
|
2013-05-25 00:21:29 -03:00
|
|
|
barometer.init();
|
|
|
|
|
2016-05-16 00:33:43 -03:00
|
|
|
// setup telem slots with serial ports
|
2019-06-19 01:24:38 -03:00
|
|
|
gcs().setup_uarts();
|
2015-05-15 01:24:18 -03:00
|
|
|
|
2018-06-23 04:11:05 -03:00
|
|
|
#if OSD_ENABLED == ENABLED
|
2018-06-23 04:34:25 -03:00
|
|
|
osd.init();
|
2018-06-23 04:11:05 -03:00
|
|
|
#endif
|
|
|
|
|
2011-12-28 00:53:05 -04:00
|
|
|
#if LOGGING_ENABLED == ENABLED
|
2015-05-29 23:12:49 -03:00
|
|
|
log_init();
|
2011-12-28 00:53:05 -04:00
|
|
|
#endif
|
2011-12-17 19:19:41 -04:00
|
|
|
|
2015-11-15 21:58:15 -04:00
|
|
|
// update motor interlock state
|
|
|
|
update_using_interlock();
|
|
|
|
|
2015-09-01 19:49:59 -03:00
|
|
|
#if FRAME_CONFIG == HELI_FRAME
|
|
|
|
// trad heli specific initialisation
|
|
|
|
heli_init();
|
|
|
|
#endif
|
2017-08-26 09:59:03 -03:00
|
|
|
#if FRAME_CONFIG == HELI_FRAME
|
|
|
|
input_manager.set_loop_rate(scheduler.get_loop_rate_hz());
|
|
|
|
#endif
|
|
|
|
|
2012-08-21 23:19:50 -03:00
|
|
|
init_rc_in(); // sets up rc channels from radio
|
2017-01-09 03:31:26 -04:00
|
|
|
|
|
|
|
// allocate the motors class
|
|
|
|
allocate_motors();
|
|
|
|
|
2019-03-31 04:31:42 -03:00
|
|
|
// initialise rc channels including setting mode
|
|
|
|
rc().init();
|
|
|
|
|
2017-05-13 03:11:04 -03:00
|
|
|
// sets up motors and output to escs
|
|
|
|
init_rc_out();
|
|
|
|
|
2019-07-22 00:11:01 -03:00
|
|
|
// check if we should enter esc calibration mode
|
|
|
|
esc_calibration_startup_check();
|
|
|
|
|
2017-05-13 03:11:04 -03:00
|
|
|
// motors initialised so parameters can be sent
|
|
|
|
ap.initialised_params = true;
|
2013-05-16 04:32:00 -03:00
|
|
|
|
2014-07-26 04:30:31 -03:00
|
|
|
relay.init();
|
|
|
|
|
2012-10-09 00:30:17 -03:00
|
|
|
/*
|
|
|
|
* setup the 'main loop is dead' check. Note that this relies on
|
|
|
|
* the RC library being initialised.
|
|
|
|
*/
|
2015-05-29 23:12:49 -03:00
|
|
|
hal.scheduler->register_timer_failsafe(failsafe_check_static, 1000);
|
2012-06-29 08:51:05 -03:00
|
|
|
|
2012-08-21 23:19:50 -03:00
|
|
|
// Do GPS init
|
2017-06-27 05:13:15 -03:00
|
|
|
gps.set_log_gps_bit(MASK_LOG_GPS);
|
|
|
|
gps.init(serial_manager);
|
2014-02-25 18:40:29 -04:00
|
|
|
|
2019-03-25 09:42:58 -03:00
|
|
|
AP::compass().set_log_bit(MASK_LOG_COMPASS);
|
2019-03-24 00:26:33 -03:00
|
|
|
AP::compass().init();
|
2011-04-16 17:44:44 -03:00
|
|
|
|
2015-01-03 21:29:22 -04:00
|
|
|
#if OPTFLOW == ENABLED
|
2015-01-02 20:15:46 -04:00
|
|
|
// make optflow available to AHRS
|
|
|
|
ahrs.set_optflow(&optflow);
|
2015-01-03 21:29:22 -04:00
|
|
|
#endif
|
2015-01-02 20:15:46 -04:00
|
|
|
|
2015-08-13 22:12:10 -03:00
|
|
|
// init Location class
|
2016-04-21 22:58:36 -03:00
|
|
|
#if AP_TERRAIN_AVAILABLE && AC_TERRAIN
|
2019-01-01 22:54:31 -04:00
|
|
|
Location::set_terrain(&terrain);
|
2017-01-09 03:31:26 -04:00
|
|
|
wp_nav->set_terrain(&terrain);
|
2015-08-13 22:12:10 -03:00
|
|
|
#endif
|
2018-05-29 22:34:10 -03:00
|
|
|
|
2019-06-05 07:47:32 -03:00
|
|
|
#if AC_OAPATHPLANNER_ENABLED == ENABLED
|
|
|
|
g2.oa.init();
|
|
|
|
#endif
|
|
|
|
|
2017-01-09 03:31:26 -04:00
|
|
|
attitude_control->parameter_sanity_check();
|
2017-08-29 09:42:44 -03:00
|
|
|
pos_control->set_dt(scheduler.get_loop_period_s());
|
|
|
|
|
2012-08-21 23:19:50 -03:00
|
|
|
// init the optical flow sensor
|
2014-07-11 23:34:55 -03:00
|
|
|
init_optflow();
|
2011-12-23 18:42:05 -04:00
|
|
|
|
2015-03-21 09:02:06 -03:00
|
|
|
#if MOUNT == ENABLED
|
2015-01-11 02:37:26 -04:00
|
|
|
// initialise camera mount
|
2019-08-27 03:24:00 -03:00
|
|
|
camera_mount.init();
|
2015-03-21 09:02:06 -03:00
|
|
|
#endif
|
2015-01-11 02:37:26 -04:00
|
|
|
|
2015-02-16 00:39:18 -04:00
|
|
|
#if PRECISION_LANDING == ENABLED
|
|
|
|
// initialise precision landing
|
|
|
|
init_precland();
|
|
|
|
#endif
|
|
|
|
|
2017-08-04 03:29:06 -03:00
|
|
|
// initialise landing gear position
|
|
|
|
landinggear.init();
|
|
|
|
|
2011-10-18 03:51:47 -03:00
|
|
|
#ifdef USERHOOK_INIT
|
2012-08-21 23:19:50 -03:00
|
|
|
USERHOOK_INIT
|
2011-10-18 03:51:47 -03:00
|
|
|
#endif
|
2011-11-19 18:01:47 -04:00
|
|
|
|
2013-03-18 02:19:31 -03:00
|
|
|
#if HIL_MODE != HIL_MODE_DISABLED
|
2014-08-13 10:54:41 -03:00
|
|
|
while (barometer.get_last_update() == 0) {
|
|
|
|
// the barometer begins updating when we get the first
|
2013-03-18 02:19:31 -03:00
|
|
|
// HIL_STATE message
|
2017-07-08 21:56:49 -03:00
|
|
|
gcs().send_text(MAV_SEVERITY_WARNING, "Waiting for first HIL_STATE message");
|
2013-03-18 02:19:31 -03:00
|
|
|
delay(1000);
|
|
|
|
}
|
2014-10-15 20:33:24 -03:00
|
|
|
|
|
|
|
// set INS to HIL mode
|
|
|
|
ins.set_hil_mode();
|
2013-03-18 02:19:31 -03:00
|
|
|
#endif
|
|
|
|
|
2012-08-21 23:19:50 -03:00
|
|
|
// read Baro pressure at ground
|
|
|
|
//-----------------------------
|
2018-04-11 09:50:53 -03:00
|
|
|
barometer.set_log_baro_bit(MASK_LOG_IMU);
|
2018-03-18 01:16:22 -03:00
|
|
|
barometer.calibrate();
|
2012-08-21 23:19:50 -03:00
|
|
|
|
2016-04-27 08:37:04 -03:00
|
|
|
// initialise rangefinder
|
|
|
|
init_rangefinder();
|
2012-08-21 23:19:50 -03:00
|
|
|
|
2016-08-15 03:53:45 -03:00
|
|
|
// init proximity sensor
|
|
|
|
init_proximity();
|
|
|
|
|
2018-02-22 00:53:20 -04:00
|
|
|
#if BEACON_ENABLED == ENABLED
|
2016-10-24 02:46:33 -03:00
|
|
|
// init beacons used for non-gps position estimation
|
2018-02-22 00:53:20 -04:00
|
|
|
g2.beacon.init();
|
|
|
|
#endif
|
2016-10-24 02:46:33 -03:00
|
|
|
|
2017-03-01 07:18:11 -04:00
|
|
|
// init visual odometry
|
|
|
|
init_visual_odom();
|
|
|
|
|
2018-03-29 10:49:27 -03:00
|
|
|
#if RPM_ENABLED == ENABLED
|
2015-08-07 02:34:56 -03:00
|
|
|
// initialise AP_RPM library
|
|
|
|
rpm_sensor.init();
|
2018-03-29 10:49:27 -03:00
|
|
|
#endif
|
2015-08-07 02:34:56 -03:00
|
|
|
|
2018-02-21 22:06:07 -04:00
|
|
|
#if MODE_AUTO_ENABLED == ENABLED
|
2014-03-11 00:54:22 -03:00
|
|
|
// initialise mission library
|
2018-04-24 20:31:01 -03:00
|
|
|
mode_auto.mission.init();
|
2018-02-21 22:06:07 -04:00
|
|
|
#endif
|
2014-03-11 00:54:22 -03:00
|
|
|
|
2018-02-21 23:58:28 -04:00
|
|
|
#if MODE_SMARTRTL_ENABLED == ENABLED
|
2017-09-14 10:21:33 -03:00
|
|
|
// initialize SmartRTL
|
2017-09-08 23:45:31 -03:00
|
|
|
g2.smart_rtl.init();
|
2018-02-21 23:58:28 -04:00
|
|
|
#endif
|
2017-07-26 14:14:40 -03:00
|
|
|
|
2019-01-18 00:23:42 -04:00
|
|
|
// initialise AP_Logger library
|
2019-01-18 00:24:08 -04:00
|
|
|
logger.setVehicle_Startup_Writer(FUNCTOR_BIND(&copter, &Copter::Log_Write_Vehicle_Startup_Messages, void));
|
2017-05-01 03:04:03 -03:00
|
|
|
|
2015-09-17 03:44:14 -03:00
|
|
|
startup_INS_ground();
|
|
|
|
|
2019-03-01 02:40:53 -04:00
|
|
|
#ifdef ENABLE_SCRIPTING
|
2019-11-26 01:40:30 -04:00
|
|
|
g2.scripting.init();
|
2019-03-01 02:40:53 -04:00
|
|
|
#endif // ENABLE_SCRIPTING
|
|
|
|
|
2015-09-17 03:44:14 -03:00
|
|
|
// set landed flags
|
|
|
|
set_land_complete(true);
|
|
|
|
set_land_complete_maybe(true);
|
2011-07-30 17:42:54 -03:00
|
|
|
|
2014-07-11 03:27:29 -03: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
|
2015-01-19 09:52:54 -04:00
|
|
|
serial_manager.set_blocking_writes_all(false);
|
2014-07-11 03:27:29 -03:00
|
|
|
|
2015-04-13 03:57:44 -03:00
|
|
|
// enable CPU failsafe
|
|
|
|
failsafe_enable();
|
|
|
|
|
2017-06-27 01:42:20 -03:00
|
|
|
ins.set_log_raw_bit(MASK_LOG_IMU_RAW);
|
2015-05-06 23:09:00 -03:00
|
|
|
|
2017-04-14 21:52:05 -03:00
|
|
|
// enable output to motors
|
2017-08-20 03:24:33 -03:00
|
|
|
if (arming.rc_calibration_checks(true)) {
|
2017-04-14 21:52:05 -03:00
|
|
|
enable_motor_output();
|
|
|
|
}
|
|
|
|
|
2017-04-29 21:48:01 -03:00
|
|
|
// disable safety if requested
|
2017-05-25 05:53:20 -03:00
|
|
|
BoardConfig.init_safety();
|
2017-04-29 21:48:01 -03:00
|
|
|
|
2020-06-04 23:15:25 -03:00
|
|
|
vehicle_setup();
|
|
|
|
|
2017-08-11 01:08:41 -03:00
|
|
|
hal.console->printf("\nReady to FLY ");
|
2014-09-09 10:17:46 -03:00
|
|
|
|
|
|
|
// flag that initialisation has completed
|
|
|
|
ap.initialised = true;
|
2019-04-10 22:56:38 -03:00
|
|
|
|
|
|
|
#if AP_PARAM_KEY_DUMP
|
|
|
|
AP_Param::show_all(hal.console, true);
|
|
|
|
#endif
|
2012-12-13 15:48:01 -04:00
|
|
|
}
|
|
|
|
|
2012-07-14 23:26:17 -03:00
|
|
|
|
2012-12-13 15:48:01 -04:00
|
|
|
//******************************************************************************
|
2011-07-30 17:42:54 -03:00
|
|
|
//This function does all the calibrations, etc. that we need during a ground start
|
2012-12-13 15:48:01 -04:00
|
|
|
//******************************************************************************
|
2015-09-17 03:44:14 -03:00
|
|
|
void Copter::startup_INS_ground()
|
2011-07-30 17:42:54 -03:00
|
|
|
{
|
2013-01-13 01:04:04 -04:00
|
|
|
// initialise ahrs (may push imu calibration into the mpu6000 if using that device).
|
|
|
|
ahrs.init();
|
2014-04-21 05:11:53 -03:00
|
|
|
ahrs.set_vehicle_class(AHRS_VEHICLE_COPTER);
|
2013-01-13 01:04:04 -04:00
|
|
|
|
2015-09-17 03:44:14 -03:00
|
|
|
// Warm up and calibrate gyro offsets
|
2015-12-26 00:00:03 -04:00
|
|
|
ins.init(scheduler.get_loop_rate_hz());
|
2015-09-17 03:44:14 -03:00
|
|
|
|
|
|
|
// reset ahrs including gyro bias
|
|
|
|
ahrs.reset();
|
|
|
|
}
|
|
|
|
|
2019-07-10 17:52:38 -03:00
|
|
|
// update the harmonic notch filter center frequency dynamically
|
|
|
|
void Copter::update_dynamic_notch()
|
|
|
|
{
|
|
|
|
const float ref_freq = ins.get_gyro_harmonic_notch_center_freq_hz();
|
|
|
|
const float ref = ins.get_gyro_harmonic_notch_reference();
|
|
|
|
|
|
|
|
if (is_zero(ref)) {
|
|
|
|
ins.update_harmonic_notch_freq_hz(ref_freq);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-10-12 09:04:45 -03:00
|
|
|
switch (ins.get_gyro_harmonic_notch_tracking_mode()) {
|
|
|
|
case HarmonicNotch_UpdateThrottle: // throttle based tracking
|
|
|
|
// set the harmonic notch filter frequency approximately scaled on motor rpm implied by throttle
|
|
|
|
ins.update_harmonic_notch_freq_hz(ref_freq * MAX(1.0f, sqrtf(motors->get_throttle_out() / ref)));
|
|
|
|
break;
|
|
|
|
|
2019-07-10 17:52:38 -03:00
|
|
|
#if RPM_ENABLED == ENABLED
|
2019-10-12 09:04:45 -03:00
|
|
|
case HarmonicNotch_UpdateRPM: // rpm sensor based tracking
|
|
|
|
if (rpm_sensor.healthy(0)) {
|
|
|
|
// set the harmonic notch filter frequency from the main rotor rpm
|
|
|
|
ins.update_harmonic_notch_freq_hz(MAX(ref_freq, rpm_sensor.get_rpm(0) * ref / 60.0f));
|
|
|
|
} else {
|
|
|
|
ins.update_harmonic_notch_freq_hz(ref_freq);
|
|
|
|
}
|
|
|
|
break;
|
2019-07-10 17:52:38 -03:00
|
|
|
#endif
|
2019-10-12 09:04:45 -03:00
|
|
|
#ifdef HAVE_AP_BLHELI_SUPPORT
|
|
|
|
case HarmonicNotch_UpdateBLHeli: // BLHeli based tracking
|
|
|
|
ins.update_harmonic_notch_freq_hz(MAX(ref_freq, AP_BLHeli::get_singleton()->get_average_motor_frequency_hz() * ref));
|
|
|
|
break;
|
|
|
|
#endif
|
|
|
|
case HarmonicNotch_Fixed: // static
|
|
|
|
default:
|
|
|
|
ins.update_harmonic_notch_freq_hz(ref_freq);
|
|
|
|
break;
|
2019-07-10 17:52:38 -03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-01-02 07:43:50 -04:00
|
|
|
// position_ok - returns true if the horizontal absolute position is ok and home position is set
|
2019-07-04 21:12:57 -03:00
|
|
|
bool Copter::position_ok() const
|
2013-07-19 23:01:10 -03:00
|
|
|
{
|
2015-06-15 03:53:41 -03:00
|
|
|
// return false if ekf failsafe has triggered
|
|
|
|
if (failsafe.ekf) {
|
2015-03-19 00:36:20 -03:00
|
|
|
return false;
|
|
|
|
}
|
2015-01-05 23:15:35 -04:00
|
|
|
|
2015-06-15 03:53:41 -03:00
|
|
|
// check ekf position estimate
|
2015-10-13 23:52:48 -03:00
|
|
|
return (ekf_position_ok() || optflow_position_ok());
|
2015-06-15 03:53:41 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
// ekf_position_ok - returns true if the ekf claims it's horizontal absolute position estimate is ok and home position is set
|
2019-07-04 21:12:57 -03:00
|
|
|
bool Copter::ekf_position_ok() const
|
2015-06-15 03:53:41 -03:00
|
|
|
{
|
|
|
|
if (!ahrs.have_inertial_nav()) {
|
|
|
|
// do not allow navigation with dcm position
|
2015-03-19 00:36:20 -03:00
|
|
|
return false;
|
|
|
|
}
|
2015-01-05 23:15:35 -04:00
|
|
|
|
2015-03-19 00:36:20 -03:00
|
|
|
// 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
|
2017-01-09 03:31:26 -04:00
|
|
|
if (!motors->armed()) {
|
2015-03-19 00:36:20 -03:00
|
|
|
return ((filt_status.flags.horiz_pos_abs || filt_status.flags.pred_horiz_pos_abs));
|
2015-01-02 07:43:50 -04:00
|
|
|
} else {
|
2015-03-19 00:36:20 -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);
|
2013-07-19 23:01:10 -03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-01-02 07:45:10 -04:00
|
|
|
// optflow_position_ok - returns true if optical flow based position estimate is ok
|
2019-07-04 21:12:57 -03:00
|
|
|
bool Copter::optflow_position_ok() const
|
2015-01-02 07:45:10 -04:00
|
|
|
{
|
2017-03-01 07:18:11 -04:00
|
|
|
#if OPTFLOW != ENABLED && VISUAL_ODOMETRY_ENABLED != ENABLED
|
2015-01-02 07:45:10 -04:00
|
|
|
return false;
|
|
|
|
#else
|
2017-03-01 07:18:11 -04: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 VISUAL_ODOMETRY_ENABLED == ENABLED
|
|
|
|
if (g2.visual_odom.enabled()) {
|
|
|
|
enabled = true;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
if (!enabled) {
|
2015-01-02 07:45:10 -04:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2015-01-05 23:16:24 -04:00
|
|
|
// get filter status from EKF
|
2015-01-05 01:14:27 -04:00
|
|
|
nav_filter_status filt_status = inertial_nav.get_filter_status();
|
2015-08-27 08:54:09 -03:00
|
|
|
|
|
|
|
// if disarmed we accept a predicted horizontal relative position
|
2017-01-09 03:31:26 -04:00
|
|
|
if (!motors->armed()) {
|
2015-08-27 08:54:09 -03:00
|
|
|
return (filt_status.flags.pred_horiz_pos_rel);
|
|
|
|
} else {
|
|
|
|
return (filt_status.flags.horiz_pos_rel && !filt_status.flags.const_pos_mode);
|
|
|
|
}
|
2015-01-02 07:45:10 -04:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2013-04-17 09:25:32 -03:00
|
|
|
// update_auto_armed - update status of auto_armed flag
|
2015-05-29 23:12:49 -03:00
|
|
|
void Copter::update_auto_armed()
|
2013-04-17 09:25:32 -03:00
|
|
|
{
|
|
|
|
// disarm checks
|
|
|
|
if(ap.auto_armed){
|
|
|
|
// if motors are disarmed, auto_armed should also be false
|
2017-01-09 03:31:26 -04:00
|
|
|
if(!motors->armed()) {
|
2013-04-17 09:25:32 -03:00
|
|
|
set_auto_armed(false);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
// if in stabilize or acro flight mode and throttle is zero, auto-armed should become false
|
2017-12-05 19:56:38 -04:00
|
|
|
if(flightmode->has_manual_throttle() && ap.throttle_zero && !failsafe.radio) {
|
2013-04-17 09:25:32 -03:00
|
|
|
set_auto_armed(false);
|
|
|
|
}
|
2015-06-10 19:33:42 -03:00
|
|
|
// if helicopters are on the ground, and the motor is switched off, auto-armed should be false
|
|
|
|
// so that rotor runup is checked again before attempting to take-off
|
2019-04-09 09:16:58 -03:00
|
|
|
if(ap.land_complete && motors->get_spool_state() != AP_Motors::SpoolState::THROTTLE_UNLIMITED && ap.using_interlock) {
|
2015-06-10 19:33:42 -03:00
|
|
|
set_auto_armed(false);
|
|
|
|
}
|
2013-04-17 09:25:32 -03:00
|
|
|
}else{
|
|
|
|
// arm checks
|
2013-07-19 17:16:37 -03:00
|
|
|
|
|
|
|
// for tradheli if motors are armed and throttle is above zero and the motor is started, auto_armed should be true
|
2019-02-28 05:03:00 -04:00
|
|
|
if(motors->armed() && ap.using_interlock) {
|
2019-04-09 09:16:58 -03:00
|
|
|
if(!ap.throttle_zero && motors->get_spool_state() == AP_Motors::SpoolState::THROTTLE_UNLIMITED) {
|
2019-02-28 05:03:00 -04:00
|
|
|
set_auto_armed(true);
|
|
|
|
}
|
2013-04-17 09:25:32 -03:00
|
|
|
// if motors are armed and throttle is above zero auto_armed should be true
|
2018-03-14 17:16:16 -03:00
|
|
|
// if motors are armed and we are in throw mode, then auto_armed should be true
|
2019-02-28 05:03:00 -04:00
|
|
|
} else if (motors->armed() && !ap.using_interlock) {
|
2019-09-07 20:33:56 -03:00
|
|
|
if(!ap.throttle_zero || control_mode == Mode::Number::THROW) {
|
2019-02-28 05:03:00 -04:00
|
|
|
set_auto_armed(true);
|
|
|
|
}
|
2013-04-17 09:25:32 -03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-10-16 21:37:49 -03:00
|
|
|
/*
|
|
|
|
should we log a message type now?
|
|
|
|
*/
|
2015-05-29 23:12:49 -03:00
|
|
|
bool Copter::should_log(uint32_t mask)
|
2014-10-16 21:37:49 -03:00
|
|
|
{
|
2014-10-17 04:07:47 -03:00
|
|
|
#if LOGGING_ENABLED == ENABLED
|
2019-01-18 00:23:42 -04:00
|
|
|
ap.logging_started = logger.logging_started();
|
|
|
|
return logger.should_log(mask);
|
2014-10-17 04:07:47 -03:00
|
|
|
#else
|
|
|
|
return false;
|
|
|
|
#endif
|
2016-01-19 01:26:14 -04:00
|
|
|
}
|
2016-12-12 06:22:56 -04:00
|
|
|
|
|
|
|
// return MAV_TYPE corresponding to frame class
|
2018-03-22 05:50:24 -03:00
|
|
|
MAV_TYPE Copter::get_frame_mav_type()
|
2016-12-12 06:22:56 -04:00
|
|
|
{
|
|
|
|
switch ((AP_Motors::motor_frame_class)g2.frame_class.get()) {
|
|
|
|
case AP_Motors::MOTOR_FRAME_QUAD:
|
2017-01-11 20:27:02 -04:00
|
|
|
case AP_Motors::MOTOR_FRAME_UNDEFINED:
|
2016-12-12 06:22:56 -04:00
|
|
|
return MAV_TYPE_QUADROTOR;
|
|
|
|
case AP_Motors::MOTOR_FRAME_HEXA:
|
|
|
|
case AP_Motors::MOTOR_FRAME_Y6:
|
|
|
|
return MAV_TYPE_HEXAROTOR;
|
|
|
|
case AP_Motors::MOTOR_FRAME_OCTA:
|
|
|
|
case AP_Motors::MOTOR_FRAME_OCTAQUAD:
|
|
|
|
return MAV_TYPE_OCTOROTOR;
|
|
|
|
case AP_Motors::MOTOR_FRAME_HELI:
|
2017-03-14 06:47:18 -03:00
|
|
|
case AP_Motors::MOTOR_FRAME_HELI_DUAL:
|
2017-08-27 02:38:00 -03:00
|
|
|
case AP_Motors::MOTOR_FRAME_HELI_QUAD:
|
2016-12-12 06:22:56 -04:00
|
|
|
return MAV_TYPE_HELICOPTER;
|
|
|
|
case AP_Motors::MOTOR_FRAME_TRI:
|
|
|
|
return MAV_TYPE_TRICOPTER;
|
|
|
|
case AP_Motors::MOTOR_FRAME_SINGLE:
|
|
|
|
case AP_Motors::MOTOR_FRAME_COAX:
|
2017-02-11 01:50:34 -04:00
|
|
|
case AP_Motors::MOTOR_FRAME_TAILSITTER:
|
2016-12-12 06:22:56 -04:00
|
|
|
return MAV_TYPE_COAXIAL;
|
2017-05-13 22:15:02 -03:00
|
|
|
case AP_Motors::MOTOR_FRAME_DODECAHEXA:
|
2018-04-27 02:57:00 -03:00
|
|
|
return MAV_TYPE_DODECAROTOR;
|
2016-12-12 06:22:56 -04:00
|
|
|
}
|
2016-12-14 21:47:58 -04:00
|
|
|
// unknown frame so return generic
|
2016-12-12 06:22:56 -04:00
|
|
|
return MAV_TYPE_GENERIC;
|
|
|
|
}
|
|
|
|
|
|
|
|
// return string corresponding to frame_class
|
|
|
|
const char* Copter::get_frame_string()
|
|
|
|
{
|
|
|
|
switch ((AP_Motors::motor_frame_class)g2.frame_class.get()) {
|
|
|
|
case AP_Motors::MOTOR_FRAME_QUAD:
|
|
|
|
return "QUAD";
|
|
|
|
case AP_Motors::MOTOR_FRAME_HEXA:
|
|
|
|
return "HEXA";
|
|
|
|
case AP_Motors::MOTOR_FRAME_Y6:
|
|
|
|
return "Y6";
|
|
|
|
case AP_Motors::MOTOR_FRAME_OCTA:
|
|
|
|
return "OCTA";
|
|
|
|
case AP_Motors::MOTOR_FRAME_OCTAQUAD:
|
|
|
|
return "OCTA_QUAD";
|
|
|
|
case AP_Motors::MOTOR_FRAME_HELI:
|
|
|
|
return "HELI";
|
2017-03-14 06:47:18 -03:00
|
|
|
case AP_Motors::MOTOR_FRAME_HELI_DUAL:
|
|
|
|
return "HELI_DUAL";
|
2017-08-27 02:38:00 -03:00
|
|
|
case AP_Motors::MOTOR_FRAME_HELI_QUAD:
|
|
|
|
return "HELI_QUAD";
|
2016-12-12 06:22:56 -04:00
|
|
|
case AP_Motors::MOTOR_FRAME_TRI:
|
|
|
|
return "TRI";
|
|
|
|
case AP_Motors::MOTOR_FRAME_SINGLE:
|
|
|
|
return "SINGLE";
|
|
|
|
case AP_Motors::MOTOR_FRAME_COAX:
|
|
|
|
return "COAX";
|
2017-02-11 07:29:04 -04:00
|
|
|
case AP_Motors::MOTOR_FRAME_TAILSITTER:
|
|
|
|
return "TAILSITTER";
|
2017-05-13 22:15:02 -03:00
|
|
|
case AP_Motors::MOTOR_FRAME_DODECAHEXA:
|
|
|
|
return "DODECA_HEXA";
|
2016-12-12 06:22:56 -04:00
|
|
|
case AP_Motors::MOTOR_FRAME_UNDEFINED:
|
|
|
|
default:
|
|
|
|
return "UNKNOWN";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-01-09 03:31:26 -04:00
|
|
|
/*
|
|
|
|
allocate the motors class
|
|
|
|
*/
|
|
|
|
void Copter::allocate_motors(void)
|
|
|
|
{
|
|
|
|
switch ((AP_Motors::motor_frame_class)g2.frame_class.get()) {
|
|
|
|
#if FRAME_CONFIG != HELI_FRAME
|
|
|
|
case AP_Motors::MOTOR_FRAME_QUAD:
|
|
|
|
case AP_Motors::MOTOR_FRAME_HEXA:
|
|
|
|
case AP_Motors::MOTOR_FRAME_Y6:
|
|
|
|
case AP_Motors::MOTOR_FRAME_OCTA:
|
|
|
|
case AP_Motors::MOTOR_FRAME_OCTAQUAD:
|
2017-05-13 22:15:02 -03:00
|
|
|
case AP_Motors::MOTOR_FRAME_DODECAHEXA:
|
2017-01-09 15:44:54 -04:00
|
|
|
default:
|
2017-08-26 09:59:03 -03:00
|
|
|
motors = new AP_MotorsMatrix(copter.scheduler.get_loop_rate_hz());
|
2017-03-14 06:47:18 -03:00
|
|
|
motors_var_info = AP_MotorsMatrix::var_info;
|
2017-01-09 03:31:26 -04:00
|
|
|
break;
|
|
|
|
case AP_Motors::MOTOR_FRAME_TRI:
|
2017-08-26 09:59:03 -03:00
|
|
|
motors = new AP_MotorsTri(copter.scheduler.get_loop_rate_hz());
|
2017-03-14 06:47:18 -03:00
|
|
|
motors_var_info = AP_MotorsTri::var_info;
|
2017-02-07 19:47:36 -04:00
|
|
|
AP_Param::set_frame_type_flags(AP_PARAM_FRAME_TRICOPTER);
|
2017-01-09 03:31:26 -04:00
|
|
|
break;
|
|
|
|
case AP_Motors::MOTOR_FRAME_SINGLE:
|
2017-08-26 09:59:03 -03:00
|
|
|
motors = new AP_MotorsSingle(copter.scheduler.get_loop_rate_hz());
|
2017-03-14 06:47:18 -03:00
|
|
|
motors_var_info = AP_MotorsSingle::var_info;
|
2017-01-09 03:31:26 -04:00
|
|
|
break;
|
|
|
|
case AP_Motors::MOTOR_FRAME_COAX:
|
2017-08-26 09:59:03 -03:00
|
|
|
motors = new AP_MotorsCoax(copter.scheduler.get_loop_rate_hz());
|
2017-03-14 06:47:18 -03:00
|
|
|
motors_var_info = AP_MotorsCoax::var_info;
|
2017-01-09 03:31:26 -04:00
|
|
|
break;
|
2017-02-11 01:50:34 -04:00
|
|
|
case AP_Motors::MOTOR_FRAME_TAILSITTER:
|
2017-08-26 09:59:03 -03:00
|
|
|
motors = new AP_MotorsTailsitter(copter.scheduler.get_loop_rate_hz());
|
2017-03-14 06:47:18 -03:00
|
|
|
motors_var_info = AP_MotorsTailsitter::var_info;
|
2017-02-11 01:50:34 -04:00
|
|
|
break;
|
2017-01-09 03:31:26 -04:00
|
|
|
#else // FRAME_CONFIG == HELI_FRAME
|
2017-03-14 06:47:18 -03:00
|
|
|
case AP_Motors::MOTOR_FRAME_HELI_DUAL:
|
2017-08-26 09:59:03 -03:00
|
|
|
motors = new AP_MotorsHeli_Dual(copter.scheduler.get_loop_rate_hz());
|
2017-03-14 06:47:18 -03:00
|
|
|
motors_var_info = AP_MotorsHeli_Dual::var_info;
|
|
|
|
AP_Param::set_frame_type_flags(AP_PARAM_FRAME_HELI);
|
|
|
|
break;
|
2017-08-27 02:38:00 -03:00
|
|
|
|
|
|
|
case AP_Motors::MOTOR_FRAME_HELI_QUAD:
|
|
|
|
motors = new AP_MotorsHeli_Quad(copter.scheduler.get_loop_rate_hz());
|
|
|
|
motors_var_info = AP_MotorsHeli_Quad::var_info;
|
|
|
|
AP_Param::set_frame_type_flags(AP_PARAM_FRAME_HELI);
|
|
|
|
break;
|
2017-03-14 06:47:18 -03:00
|
|
|
|
2017-01-09 03:31:26 -04:00
|
|
|
case AP_Motors::MOTOR_FRAME_HELI:
|
2017-01-09 15:44:54 -04:00
|
|
|
default:
|
2017-08-26 09:59:03 -03:00
|
|
|
motors = new AP_MotorsHeli_Single(copter.scheduler.get_loop_rate_hz());
|
2017-03-14 06:47:18 -03:00
|
|
|
motors_var_info = AP_MotorsHeli_Single::var_info;
|
2017-02-07 19:47:36 -04:00
|
|
|
AP_Param::set_frame_type_flags(AP_PARAM_FRAME_HELI);
|
2017-05-25 05:53:20 -03:00
|
|
|
break;
|
2017-01-09 03:31:26 -04:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
if (motors == nullptr) {
|
|
|
|
AP_HAL::panic("Unable to allocate FRAME_CLASS=%u", (unsigned)g2.frame_class.get());
|
|
|
|
}
|
2017-03-14 06:47:18 -03:00
|
|
|
AP_Param::load_object_from_eeprom(motors, motors_var_info);
|
2017-01-09 03:31:26 -04:00
|
|
|
|
2018-12-12 23:53:56 -04:00
|
|
|
ahrs_view = ahrs.create_view(ROTATION_NONE);
|
2017-02-11 07:29:21 -04:00
|
|
|
if (ahrs_view == nullptr) {
|
|
|
|
AP_HAL::panic("Unable to allocate AP_AHRS_View");
|
|
|
|
}
|
2017-03-14 06:47:18 -03:00
|
|
|
|
|
|
|
const struct AP_Param::GroupInfo *ac_var_info;
|
|
|
|
|
2017-01-09 03:31:26 -04:00
|
|
|
#if FRAME_CONFIG != HELI_FRAME
|
2017-08-29 09:42:44 -03:00
|
|
|
attitude_control = new AC_AttitudeControl_Multi(*ahrs_view, aparm, *motors, scheduler.get_loop_period_s());
|
2017-03-14 06:47:18 -03:00
|
|
|
ac_var_info = AC_AttitudeControl_Multi::var_info;
|
2017-01-09 03:31:26 -04:00
|
|
|
#else
|
2017-08-29 09:42:44 -03:00
|
|
|
attitude_control = new AC_AttitudeControl_Heli(*ahrs_view, aparm, *motors, scheduler.get_loop_period_s());
|
2017-03-14 06:47:18 -03:00
|
|
|
ac_var_info = AC_AttitudeControl_Heli::var_info;
|
2017-01-09 03:31:26 -04:00
|
|
|
#endif
|
|
|
|
if (attitude_control == nullptr) {
|
|
|
|
AP_HAL::panic("Unable to allocate AttitudeControl");
|
|
|
|
}
|
2017-03-14 06:47:18 -03:00
|
|
|
AP_Param::load_object_from_eeprom(attitude_control, ac_var_info);
|
2017-01-09 03:31:26 -04:00
|
|
|
|
2017-11-20 21:49:11 -04:00
|
|
|
pos_control = new AC_PosControl(*ahrs_view, inertial_nav, *motors, *attitude_control);
|
2017-01-09 03:31:26 -04:00
|
|
|
if (pos_control == nullptr) {
|
|
|
|
AP_HAL::panic("Unable to allocate PosControl");
|
|
|
|
}
|
2017-01-09 23:39:04 -04:00
|
|
|
AP_Param::load_object_from_eeprom(pos_control, pos_control->var_info);
|
2017-01-09 03:31:26 -04:00
|
|
|
|
2019-06-05 07:47:32 -03:00
|
|
|
#if AC_OAPATHPLANNER_ENABLED == ENABLED
|
|
|
|
wp_nav = new AC_WPNav_OA(inertial_nav, *ahrs_view, *pos_control, *attitude_control);
|
|
|
|
#else
|
2017-02-11 18:34:00 -04:00
|
|
|
wp_nav = new AC_WPNav(inertial_nav, *ahrs_view, *pos_control, *attitude_control);
|
2019-06-05 07:47:32 -03:00
|
|
|
#endif
|
2017-01-09 03:31:26 -04:00
|
|
|
if (wp_nav == nullptr) {
|
|
|
|
AP_HAL::panic("Unable to allocate WPNav");
|
|
|
|
}
|
2017-01-09 23:39:04 -04:00
|
|
|
AP_Param::load_object_from_eeprom(wp_nav, wp_nav->var_info);
|
2017-01-09 03:31:26 -04:00
|
|
|
|
2018-03-27 23:13:37 -03:00
|
|
|
loiter_nav = new AC_Loiter(inertial_nav, *ahrs_view, *pos_control, *attitude_control);
|
|
|
|
if (loiter_nav == nullptr) {
|
|
|
|
AP_HAL::panic("Unable to allocate LoiterNav");
|
|
|
|
}
|
|
|
|
AP_Param::load_object_from_eeprom(loiter_nav, loiter_nav->var_info);
|
|
|
|
|
2018-02-23 05:51:17 -04:00
|
|
|
#if MODE_CIRCLE_ENABLED == ENABLED
|
2017-02-11 18:34:00 -04:00
|
|
|
circle_nav = new AC_Circle(inertial_nav, *ahrs_view, *pos_control);
|
2018-02-22 01:06:25 -04:00
|
|
|
if (circle_nav == nullptr) {
|
2017-01-09 03:31:26 -04:00
|
|
|
AP_HAL::panic("Unable to allocate CircleNav");
|
|
|
|
}
|
2017-01-09 23:39:04 -04:00
|
|
|
AP_Param::load_object_from_eeprom(circle_nav, circle_nav->var_info);
|
2018-02-23 05:51:17 -04:00
|
|
|
#endif
|
2017-01-09 05:54:18 -04:00
|
|
|
|
|
|
|
// reload lines from the defaults file that may now be accessible
|
2018-06-26 12:54:36 -03:00
|
|
|
AP_Param::reload_defaults_file(true);
|
2017-01-09 05:54:18 -04:00
|
|
|
|
|
|
|
// now setup some frame-class specific defaults
|
|
|
|
switch ((AP_Motors::motor_frame_class)g2.frame_class.get()) {
|
|
|
|
case AP_Motors::MOTOR_FRAME_Y6:
|
|
|
|
attitude_control->get_rate_roll_pid().kP().set_default(0.1);
|
|
|
|
attitude_control->get_rate_roll_pid().kD().set_default(0.006);
|
|
|
|
attitude_control->get_rate_pitch_pid().kP().set_default(0.1);
|
|
|
|
attitude_control->get_rate_pitch_pid().kD().set_default(0.006);
|
|
|
|
attitude_control->get_rate_yaw_pid().kP().set_default(0.15);
|
|
|
|
attitude_control->get_rate_yaw_pid().kI().set_default(0.015);
|
|
|
|
break;
|
|
|
|
case AP_Motors::MOTOR_FRAME_TRI:
|
2019-06-27 06:36:47 -03:00
|
|
|
attitude_control->get_rate_yaw_pid().filt_D_hz().set_default(100);
|
2017-01-09 05:54:18 -04:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
2017-01-10 02:48:51 -04:00
|
|
|
|
2017-05-24 23:09:33 -03:00
|
|
|
// brushed 16kHz defaults to 16kHz pulses
|
2018-03-15 07:24:22 -03:00
|
|
|
if (motors->get_pwm_type() == AP_Motors::PWM_TYPE_BRUSHED) {
|
2017-05-24 23:09:33 -03:00
|
|
|
g.rc_speed.set_default(16000);
|
|
|
|
}
|
|
|
|
|
2017-02-13 05:37:51 -04:00
|
|
|
// upgrade parameters. This must be done after allocating the objects
|
|
|
|
convert_pid_parameters();
|
2019-02-27 20:03:24 -04:00
|
|
|
#if FRAME_CONFIG == HELI_FRAME
|
|
|
|
convert_tradheli_parameters();
|
|
|
|
#endif
|
2017-01-09 03:31:26 -04:00
|
|
|
}
|
2019-01-08 06:43:51 -04:00
|
|
|
|
|
|
|
bool Copter::is_tradheli() const
|
|
|
|
{
|
|
|
|
#if FRAME_CONFIG == HELI_FRAME
|
|
|
|
return true;
|
|
|
|
#else
|
|
|
|
return false;
|
|
|
|
#endif
|
|
|
|
}
|