APM飞控程序解读 下载本文

#elif CONFIG_BARO == AP_BARO_PX4 static AP_Baro_PX4 barometer;

#elif CONFIG_BARO == AP_BARO_MS5611

#if CONFIG_MS5611_SERIAL == AP_BARO_MS5611_SPI

static AP_Baro_MS5611 barometer(&AP_Baro_MS5611::spi); #elif CONFIG_MS5611_SERIAL == AP_BARO_MS5611_I2C static AP_Baro_MS5611 barometer(&AP_Baro_MS5611::i2c); #else

#error Unrecognized CONFIG_MS5611_SERIAL setting. #endif #endif

#if CONFIG_HAL_BOARD == HAL_BOARD_PX4 static AP_Compass_PX4 compass; #else

static AP_Compass_HMC5843 compass; #endif #endif

// real GPS selection

#if GPS_PROTOCOL == GPS_PROTOCOL_AUTO AP_GPS_Auto g_gps_driver(&g_gps);

#elif GPS_PROTOCOL == GPS_PROTOCOL_NMEA AP_GPS_NMEA g_gps_driver;

#elif GPS_PROTOCOL == GPS_PROTOCOL_SIRF AP_GPS_SIRF g_gps_driver;

#elif GPS_PROTOCOL == GPS_PROTOCOL_UBLOX AP_GPS_UBLOX g_gps_driver;

#elif GPS_PROTOCOL == GPS_PROTOCOL_MTK AP_GPS_MTK g_gps_driver;

#elif GPS_PROTOCOL == GPS_PROTOCOL_MTK19 AP_GPS_MTK19 g_gps_driver;

#elif GPS_PROTOCOL == GPS_PROTOCOL_NONE AP_GPS_None g_gps_driver;

#else

#error Unrecognised GPS_PROTOCOL setting. #endif // GPS PROTOCOL

static AP_AHRS_DCM ahrs(&ins, g_gps);

#elif HIL_MODE == HIL_MODE_SENSORS // sensor emulators

static AP_ADC_HIL adc; static AP_Baro_HIL barometer;

static AP_Compass_HIL compass;

static AP_GPS_HIL g_gps_driver; static AP_InertialSensor_HIL ins;

static AP_AHRS_DCM ahrs(&ins, g_gps);

#if CONFIG_HAL_BOARD == HAL_BOARD_AVR_SITL

// When building for SITL we use the HIL barometer and compass drivers static SITL sitl; #endif

#elif HIL_MODE == HIL_MODE_ATTITUDE static AP_ADC_HIL adc; static AP_InertialSensor_HIL ins;

static AP_AHRS_HIL ahrs(&ins, g_gps); static AP_GPS_HIL g_gps_driver;

static AP_Compass_HIL compass; // never used static AP_Baro_HIL barometer;

#if CONFIG_HAL_BOARD == HAL_BOARD_AVR_SITL

// When building for SITL we use the HIL barometer and compass drivers static SITL sitl; #endif

#else

#error Unrecognised HIL_MODE setting. #endif // HIL MODE

//////////////////////////////////////////////////////////////////////////////// // Optical flow sensor

//////////////////////////////////////////////////////////////////////////////// #if OPTFLOW == ENABLED

static AP_OpticalFlow_ADNS3080 optflow; #else

static AP_OpticalFlow optflow; #endif

//////////////////////////////////////////////////////////////////////////////// // GCS selection

//////////////////////////////////////////////////////////////////////////////// static GCS_MAVLINK gcs0; static GCS_MAVLINK gcs3;

//////////////////////////////////////////////////////////////////////////////// // SONAR selection

//////////////////////////////////////////////////////////////////////////////// //

ModeFilterInt16_Size3 sonar_mode_filter(1); #if CONFIG_SONAR == ENABLED

static AP_HAL::AnalogSource *sonar_analog_source; static AP_RangeFinder_MaxsonarXL *sonar; #endif

//////////////////////////////////////////////////////////////////////////////// // User variables

//////////////////////////////////////////////////////////////////////////////// #ifdef USERHOOK_VARIABLES #include USERHOOK_VARIABLES #endif

//////////////////////////////////////////////////////////////////////////////// // Global variables

////////////////////////////////////////////////////////////////////////////////

/* Radio values

* Channel assignments * 1 Ailerons (rudder if no ailerons) * 2 Elevator * 3 Throttle * 4 Rudder (if we have ailerons) * 5 Mode - 3 position switch * 6 User assignable * 7 trainer switch - sets throttle nominal (toggle switch), sets accels to Level (hold >

1 second)

* 8 TBD

* Each Aux channel can be configured to have any of the available auxiliary functions assigned to it.

* See libraries/RC_Channel/RC_Channel_aux.h for more information */

//Documentation of GLobals: static union { struct {

uint8_t home_is_set : 1; // 0

uint8_t simple_mode : 2; // 1,2 // This is the state of simple mode : 0 = disabled ; 1 = SIMPLE ; 2 = SUPERSIMPLE

uint8_t pre_arm_rc_check : 1; // 3 // true if rc input pre-arm checks have been completed successfully uint8_t pre_arm_check : 1; // 4 // true if all pre-arm checks (rc, accel calibration, gps lock) have been performed

uint8_t auto_armed : 1; // 5 // stops auto missions from beginning until throttle is raised uint8_t logging_started : 1; // 6 // true if dataflash logging has started

uint8_t do_flip : 1; // 7 // Used to enable flip code uint8_t takeoff_complete : 1; // 8

uint8_t land_complete : 1; // 9 // true if we have detected a landing

uint8_t new_radio_frame : 1; // 10 // Set true if we have new PWM data to act on from the Radio uint8_t CH7_flag : 2; // 11,12 // ch7 aux switch : 0 is low or false, 1 is center or true, 2 is high

uint8_t CH8_flag : 2; // 13,14 // ch8 aux switch : 0 is low or false, 1 is center or true, 2 is high

uint8_t usb_connected : 1; // 15 // true if APM is powered from USB connection uint8_t yaw_stopped : 1; // 16 // Used to manage the Yaw hold capabilities

uint8_t disable_stab_rate_limit : 1; // 17 // disables limits rate request from the stability controller

uint8_t rc_receiver_present : 1; // 18 // true if we have an rc receiver present (i.e. if we've ever received an update };

uint32_t value; } ap;

//////////////////////////////////////////////////////////////////////////////// // Radio

//////////////////////////////////////////////////////////////////////////////// // This is the state of the flight control system

// There are multiple states defined such as STABILIZE, ACRO, static int8_t control_mode = STABILIZE;

// Used to maintain the state of the previous control switch position // This is set to -1 when we need to re-read the switch static uint8_t oldSwitchPosition; static RCMapper rcmap;

// receiver RSSI

static uint8_t receiver_rssi;

//////////////////////////////////////////////////////////////////////////////// // Failsafe

//////////////////////////////////////////////////////////////////////////////// static struct {

uint8_t rc_override_active : 1; // 0 // true if rc control are overwritten by ground station uint8_t radio : 1; // 1 // A status flag for the radio failsafe uint8_t battery : 1; // 2 // A status flag for the battery failsafe uint8_t gps : 1; // 3 // A status flag for the gps failsafe

uint8_t gcs : 1; // 4 // A status flag for the ground station failsafe

int8_t radio_counter; // number of iterations with throttle below throttle_fs_value

uint32_t last_heartbeat_ms; // the time when the last HEARTBEAT message arrived from a GCS - used for triggering gcs failsafe } failsafe;

//////////////////////////////////////////////////////////////////////////////// // Motor Output

//////////////////////////////////////////////////////////////////////////////// #if FRAME_CONFIG == QUAD_FRAME #define MOTOR_CLASS AP_MotorsQuad #elif FRAME_CONFIG == TRI_FRAME #define MOTOR_CLASS AP_MotorsTri #elif FRAME_CONFIG == HEXA_FRAME #define MOTOR_CLASS AP_MotorsHexa #elif FRAME_CONFIG == Y6_FRAME #define MOTOR_CLASS AP_MotorsY6 #elif FRAME_CONFIG == OCTA_FRAME #define MOTOR_CLASS AP_MotorsOcta #elif FRAME_CONFIG == OCTA_QUAD_FRAME #define MOTOR_CLASS AP_MotorsOctaQuad #elif FRAME_CONFIG == HELI_FRAME #define MOTOR_CLASS AP_MotorsHeli #else

#error Unrecognised frame type #endif

#if FRAME_CONFIG == HELI_FRAME // helicopter constructor requires more arguments

static MOTOR_CLASS motors(&g.rc_1, &g.rc_2, &g.rc_3, &g.rc_4, &g.rc_8, &g.heli_servo_1, &g.heli_servo_2, &g.heli_servo_3, &g.heli_servo_4); #elif FRAME_CONFIG == TRI_FRAME // tri constructor requires additional rc_7 argument to allow tail servo reversing static MOTOR_CLASS motors(&g.rc_1, &g.rc_2, &g.rc_3, &g.rc_4, &g.rc_7); #else

static MOTOR_CLASS motors(&g.rc_1, &g.rc_2, &g.rc_3, &g.rc_4); #endif

//////////////////////////////////////////////////////////////////////////////// // PIDs

////////////////////////////////////////////////////////////////////////////////

// This is a convienience accessor for the IMU roll rates. It's currently the raw IMU rates // and not the adjusted omega rates, but the name is stuck static Vector3f omega;

// This is used to hold radio tuning values for in-flight CH6 tuning float tuning_value;

// used to limit the rate that the pid controller output is logged so that it doesn't negatively affect performance static uint8_t pid_log_counter;

//////////////////////////////////////////////////////////////////////////////// // LED output

//////////////////////////////////////////////////////////////////////////////// // Blinking indicates GPS status

static uint8_t copter_leds_GPS_blink; // Blinking indicates battery status static uint8_t copter_leds_motor_blink; // Navigation confirmation blinks static int8_t copter_leds_nav_blink;

//////////////////////////////////////////////////////////////////////////////// // GPS variables

////////////////////////////////////////////////////////////////////////////////