2011-04-16 17:44:44 -03:00
// -*- tab-width: 4; Mode: C++; c-basic-offset: 4; indent-tabs-mode: nil -*-
/*****************************************************************************
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
*****************************************************************************/
2011-07-17 07:34:05 -03:00
#if CLI_ENABLED == ENABLED
2011-04-16 17:44:44 -03:00
// Functions called from the top-level menu
2012-08-21 23:19:50 -03:00
static int8_t process_logs(uint8_t argc, const Menu::arg *argv); // in Log.pde
static int8_t setup_mode(uint8_t argc, const Menu::arg *argv); // in setup.pde
static int8_t test_mode(uint8_t argc, const Menu::arg *argv); // in test.cpp
2012-11-24 03:20:03 -04:00
static int8_t reboot_board(uint8_t argc, const Menu::arg *argv);
2011-04-16 17:44:44 -03:00
// This is the help function
// PSTR is an AVR macro to read strings from flash memory
// printf_P is a version of print_f that reads from flash memory
2012-08-21 23:19:50 -03:00
static int8_t main_menu_help(uint8_t argc, const Menu::arg *argv)
2011-04-16 17:44:44 -03:00
{
2012-11-21 02:08:03 -04:00
cliSerial->printf_P(PSTR("Commands:\n"
2012-08-21 23:19:50 -03:00
" logs\n"
" setup\n"
" test\n"
2012-11-24 03:20:03 -04:00
" reboot\n"
2012-08-21 23:19:50 -03:00
"\n"));
return(0);
2011-04-16 17:44:44 -03:00
}
// Command/function table for the top-level menu.
const struct Menu::command main_menu_commands[] PROGMEM = {
// command function called
// ======= ===============
2012-08-21 23:19:50 -03:00
{"logs", process_logs},
{"setup", setup_mode},
{"test", test_mode},
2012-11-24 03:20:03 -04:00
{"reboot", reboot_board},
2012-08-21 23:19:50 -03:00
{"help", main_menu_help},
2011-04-16 17:44:44 -03:00
};
// Create the top-level menu object.
2011-09-17 15:25:31 -03:00
MENU(main_menu, THISFIRMWARE, main_menu_commands);
2011-04-16 17:44:44 -03:00
2012-11-24 03:20:03 -04:00
static int8_t reboot_board(uint8_t argc, const Menu::arg *argv)
{
reboot_apm();
return 0;
}
2011-10-27 04:35:25 -03:00
// the user wants the CLI. It never exits
2012-12-12 19:46:20 -04:00
static void run_cli(AP_HAL::UARTDriver *port)
2011-10-27 04:35:25 -03:00
{
2012-11-21 02:08:03 -04:00
cliSerial = port;
Menu::set_port(port);
port->set_blocking_writes(true);
2012-12-23 17:51:33 -04:00
// disable the mavlink delay callback
hal.scheduler->register_delay_callback(NULL, 5);
2013-03-02 04:54:18 -04:00
// disable main_loop failsafe
failsafe_disable();
2013-03-21 00:07:00 -03:00
// cut the engines
if(motors.armed()) {
motors.armed(false);
motors.output();
}
2011-10-27 04:35:25 -03:00
while (1) {
main_menu.run();
}
}
2011-07-17 07:34:05 -03:00
#endif // CLI_ENABLED
2011-07-17 07:32:00 -03:00
static void init_ardupilot()
2011-04-16 17:44:44 -03:00
{
2011-11-20 05:42:51 -04:00
#if USB_MUX_PIN > 0
2011-11-25 19:11:25 -04:00
// on the APM2 board we have a mux thet switches UART0 between
2011-11-20 05:42:51 -04:00
// USB and the board header. If the right ArduPPM firmware is
// installed we can detect if USB is connected using the
// USB_MUX_PIN
pinMode(USB_MUX_PIN, INPUT);
2013-05-19 23:29:34 -03:00
ap_system.usb_connected = !digitalRead(USB_MUX_PIN);
2012-11-11 09:42:10 -04:00
if (!ap_system.usb_connected) {
2011-11-20 05:42:51 -04:00
// USB is not connected, this means UART0 may be a Xbee, with
// its darned bricking problem. We can't write to it for at
// least one second after powering up. Simplest solution for
// now is to delay for 1 second. Something more elegant may be
// added later
delay(1000);
}
#endif
2012-08-21 23:19:50 -03:00
// Console serial port
//
// The console port buffers are defined to be sufficiently large to support
2012-03-30 02:53:50 -03:00
// the MAVLink protocol efficiently
//
2013-01-10 19:31:55 -04:00
#if HIL_MODE != HIL_MODE_DISABLED
// we need more memory for HIL, as we get a much higher packet rate
hal.uartA->begin(SERIAL0_BAUD, 256, 256);
#else
// use a bit less for non-HIL operation
hal.uartA->begin(SERIAL0_BAUD, 128, 128);
#endif
2011-04-16 17:44:44 -03:00
2012-08-21 23:19:50 -03:00
// GPS serial port.
//
#if GPS_PROTOCOL != GPS_PROTOCOL_IMU
2012-06-08 03:39:48 -03:00
// standard gps running. Note that we need a 256 byte buffer for some
// GPS types (eg. UBLOX)
2012-12-13 15:48:01 -04:00
hal.uartB->begin(38400, 256, 16);
2012-08-21 23:19:50 -03:00
#endif
2011-04-16 17:44:44 -03:00
2012-11-21 02:08:03 -04:00
cliSerial->printf_P(PSTR("\n\nInit " THISFIRMWARE
2012-08-21 23:19:50 -03:00
"\n\nFree RAM: %u\n"),
2011-10-09 08:38:14 -03:00
memcheck_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-08-21 23:19:50 -03:00
// setup IO pins
pinMode(A_LED_PIN, OUTPUT); // GPS status LED
digitalWrite(A_LED_PIN, LED_OFF);
2011-11-12 23:44:36 -04:00
2012-08-21 23:19:50 -03:00
pinMode(B_LED_PIN, OUTPUT); // GPS status LED
digitalWrite(B_LED_PIN, LED_OFF);
2011-11-12 23:44:36 -04:00
2012-08-21 23:19:50 -03:00
pinMode(C_LED_PIN, OUTPUT); // GPS status LED
digitalWrite(C_LED_PIN, LED_OFF);
2011-11-12 23:44:36 -04:00
2012-12-13 15:48:01 -04:00
relay.init();
2012-04-12 10:53:54 -03:00
#if COPTER_LEDS == ENABLED
2013-04-01 01:10:12 -03:00
copter_leds_init();
2012-05-15 13:00:21 -03:00
#endif
2012-05-27 13:21:20 -03:00
2012-02-12 07:27:51 -04:00
// load parameters from EEPROM
load_parameters();
2011-04-16 17:44:44 -03:00
2013-05-25 00:21:29 -03:00
#if HIL_MODE != HIL_MODE_ATTITUDE
barometer.init();
#endif
2012-08-21 23:19:50 -03:00
// init the GCS
2012-12-13 15:48:01 -04:00
gcs0.init(hal.uartA);
2011-11-20 05:42:51 -04:00
2012-12-13 19:27:42 -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, 5);
2011-11-20 05:42:51 -04:00
#if USB_MUX_PIN > 0
2012-11-11 09:42:10 -04:00
if (!ap_system.usb_connected) {
2011-11-20 05:42:51 -04:00
// we are not connected via USB, re-init UART0 with right
// baud rate
2012-12-13 19:27:42 -04:00
hal.uartA->begin(map_baudrate(g.serial3_baud, SERIAL3_BAUD));
2011-11-20 05:42:51 -04:00
}
#else
// we have a 2nd serial port for telemetry
2013-01-10 19:31:55 -04:00
hal.uartC->begin(map_baudrate(g.serial3_baud, SERIAL3_BAUD), 128, 128);
2012-12-13 15:48:01 -04:00
gcs3.init(hal.uartC);
2011-11-20 05:42:51 -04:00
#endif
2011-08-01 08:39:17 -03:00
2011-12-02 16:54:36 -04:00
// identify ourselves correctly with the ground station
2012-08-21 23:19:50 -03:00
mavlink_system.sysid = g.sysid_this_mav;
2012-02-02 18:53:08 -04:00
mavlink_system.type = 2; //MAV_QUADROTOR;
2012-02-11 02:25:24 -04:00
2011-12-28 00:53:05 -04:00
#if LOGGING_ENABLED == ENABLED
DataFlash.Init();
if (!DataFlash.CardInserted()) {
gcs_send_text_P(SEVERITY_LOW, PSTR("No dataflash inserted"));
g.log_bitmask.set(0);
} else if (DataFlash.NeedErase()) {
2011-12-17 19:19:41 -04:00
gcs_send_text_P(SEVERITY_LOW, PSTR("ERASING LOGS"));
2012-08-21 23:19:50 -03:00
do_erase_logs();
2013-03-20 23:54:04 -03:00
gcs0.reset_cli_timeout();
2012-08-21 23:19:50 -03:00
}
2011-12-28 00:53:05 -04:00
#endif
2011-12-17 19:19:41 -04:00
2012-08-21 23:19:50 -03:00
#if FRAME_CONFIG == HELI_FRAME
motors.servo_manual = false;
motors.init_swash(); // heli initialisation
#endif
2011-04-16 17:44:44 -03:00
2012-08-21 23:19:50 -03:00
init_rc_in(); // sets up rc channels from radio
2013-05-16 04:32:00 -03:00
init_rc_out(); // sets up motors and output to escs
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.
*/
2012-12-13 15:48:01 -04:00
hal.scheduler->register_timer_failsafe(failsafe_check, 1000);
2012-06-29 08:51:05 -03:00
2011-11-12 22:47:54 -04:00
#if HIL_MODE != HIL_MODE_ATTITUDE
2012-08-21 23:19:50 -03:00
#if CONFIG_ADC == ENABLED
// begin filtering the ADC Gyros
2012-12-13 15:48:01 -04:00
adc.Init(); // APM ADC library initialization
2012-08-21 23:19:50 -03:00
#endif // CONFIG_ADC
2011-11-12 23:48:32 -04:00
#endif // HIL_MODE
2011-04-16 17:44:44 -03:00
2012-08-21 23:19:50 -03:00
// Do GPS init
g_gps = &g_gps_driver;
2012-06-10 03:36:18 -03:00
// GPS Initialization
2012-12-17 22:36:57 -04:00
g_gps->init(hal.uartB, GPS::GPS_ENGINE_AIRBORNE_1G);
2011-04-16 17:44:44 -03:00
2012-08-21 23:19:50 -03:00
if(g.compass_enabled)
init_compass();
2011-04-16 17:44:44 -03:00
2012-08-21 23:19:50 -03:00
// init the optical flow sensor
if(g.optflow_enabled) {
init_optflow();
}
2011-12-23 18:42:05 -04:00
2012-12-04 01:00:51 -04:00
// initialise inertial nav
inertial_nav.init();
2011-07-30 17:42:54 -03:00
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:07:04 -03:00
#if CLI_ENABLED == ENABLED
2012-11-21 02:08:03 -04:00
const prog_char_t *msg = PSTR("\nPress ENTER 3 times to start interactive setup\n");
cliSerial->println_P(msg);
#if USB_MUX_PIN == 0
2012-12-13 15:48:01 -04:00
hal.uartC->println_P(msg);
2012-11-21 02:08:03 -04:00
#endif
2011-07-17 07:34:05 -03:00
#endif // CLI_ENABLED
2013-03-18 02:19:31 -03:00
#if HIL_MODE != HIL_MODE_DISABLED
while (!barometer.healthy) {
// the barometer becomes healthy when we get the first
// HIL_STATE message
gcs_send_text_P(SEVERITY_LOW, PSTR("Waiting for first HIL_STATE message"));
delay(1000);
}
#endif
2012-08-21 23:19:50 -03:00
#if HIL_MODE != HIL_MODE_ATTITUDE
// read Baro pressure at ground
//-----------------------------
init_barometer();
#endif
// initialise sonar
#if CONFIG_SONAR == ENABLED
init_sonar();
#endif
2013-01-22 23:35:10 -04:00
#if FRAME_CONFIG == HELI_FRAME
2013-05-25 00:21:29 -03:00
// initialise controller filters
init_rate_controllers();
2012-11-26 22:21:12 -04:00
#endif // HELI_FRAME
2012-08-21 23:19:50 -03:00
// initialize commands
// -------------------
init_commands();
// set the correct flight mode
// ---------------------------
reset_control_switch();
startup_ground();
2011-07-30 17:42:54 -03:00
2011-11-20 04:49:56 -04:00
#if LOGGING_ENABLED == ENABLED
2012-08-21 23:19:50 -03:00
Log_Write_Startup();
2011-11-20 04:49:56 -04:00
#endif
2011-06-16 14:03:26 -03:00
2012-12-13 15:48:01 -04:00
cliSerial->print_P(PSTR("\nReady to FLY "));
}
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
//******************************************************************************
2011-07-30 17:42:54 -03:00
static void startup_ground(void)
{
2012-08-21 23:19:50 -03:00
gcs_send_text_P(SEVERITY_LOW,PSTR("GROUND START"));
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();
2012-08-21 23:19:50 -03:00
// Warm up and read Gyro offsets
// -----------------------------
2012-12-14 00:12:39 -04:00
ins.init(AP_InertialSensor::COLD_START,
2012-11-29 07:56:54 -04:00
ins_sample_rate,
2012-12-13 15:48:01 -04:00
flash_leds);
2012-08-21 23:19:50 -03:00
#if CLI_ENABLED == ENABLED
2012-11-05 00:32:38 -04:00
report_ins();
2012-08-21 23:19:50 -03:00
#endif
2011-07-30 17:42:54 -03:00
2012-08-21 02:38:31 -03:00
// setup fast AHRS gains to get right attitude
ahrs.set_fast_gains(true);
2012-09-29 12:25:40 -03:00
#if SECONDARY_DMP_ENABLED == ENABLED
ahrs2.init(&timer_scheduler);
ahrs2.set_as_secondary(true);
ahrs2.set_fast_gains(true);
#endif
2012-08-21 23:19:50 -03:00
// reset the leds
// ---------------------------
clear_leds();
2012-01-04 19:15:14 -04:00
2012-01-20 14:11:18 -04:00
// when we re-calibrate the gyros,
// all previous I values are invalid
2012-01-04 19:15:14 -04:00
reset_I_all();
2011-04-16 17:44:44 -03:00
}
2013-05-29 17:12:24 -03:00
// returns true or false whether mode requires GPS
static bool mode_requires_GPS(uint8_t mode) {
switch(mode) {
case AUTO:
case GUIDED:
case LOITER:
case RTL:
case CIRCLE:
case POSITION:
return true;
default:
return false;
}
return false;
}
2012-12-08 01:23:32 -04:00
// set_mode - change flight mode and perform any necessary initialisation
2012-12-12 19:46:20 -04:00
static void set_mode(uint8_t mode)
2012-08-21 23:19:50 -03:00
{
2012-11-23 02:57:49 -04:00
// Switch to stabilize mode if requested mode requires a GPS lock
2013-05-29 17:12:24 -03:00
if(g_gps->status() != GPS::GPS_OK_FIX_3D && mode_requires_GPS(mode)) {
mode = STABILIZE;
} else if(mode == RTL && !ap.home_is_set) {
2012-08-21 23:19:50 -03:00
mode = STABILIZE;
}
2011-09-04 21:15:36 -03:00
2012-11-23 02:57:49 -04:00
// Switch to stabilize if OF_LOITER requested but no optical flow sensor
if (mode == OF_LOITER && !g.optflow_enabled ) {
mode = STABILIZE;
2012-08-21 23:19:50 -03:00
}
2011-09-04 21:15:36 -03:00
2012-12-14 00:12:39 -04:00
control_mode = mode;
2013-04-21 10:44:57 -03:00
control_mode = constrain_int16(control_mode, 0, NUM_MODES - 1);
2012-08-21 23:19:50 -03:00
// if we change modes, we must clear landed flag
2012-11-10 01:55:51 -04:00
set_land_complete(false);
2012-08-21 23:19:50 -03:00
// report the GPS and Motor arming status
led_mode = NORMAL_LEDS;
switch(control_mode)
{
case ACRO:
2012-11-10 01:55:51 -04:00
ap.manual_throttle = true;
ap.manual_attitude = true;
2013-01-25 02:57:55 -04:00
set_yaw_mode(ACRO_YAW);
set_roll_pitch_mode(ACRO_RP);
set_throttle_mode(ACRO_THR);
2013-02-18 01:58:24 -04:00
set_nav_mode(NAV_NONE);
2012-10-14 05:47:46 -03:00
// reset acro axis targets to current attitude
2012-11-10 01:55:51 -04:00
if(g.axis_enabled){
2012-10-27 18:13:46 -03:00
roll_axis = ahrs.roll_sensor;
pitch_axis = ahrs.pitch_sensor;
nav_yaw = ahrs.yaw_sensor;
2012-10-14 05:47:46 -03:00
}
2012-08-21 23:19:50 -03:00
break;
case STABILIZE:
2012-11-10 01:55:51 -04:00
ap.manual_throttle = true;
ap.manual_attitude = true;
2012-12-08 01:23:32 -04:00
set_yaw_mode(YAW_HOLD);
set_roll_pitch_mode(ROLL_PITCH_STABLE);
2013-01-25 02:57:55 -04:00
set_throttle_mode(THROTTLE_MANUAL_TILT_COMPENSATED);
2013-01-24 00:36:55 -04:00
set_nav_mode(NAV_NONE);
2012-08-21 23:19:50 -03:00
break;
case ALT_HOLD:
2012-11-10 01:55:51 -04:00
ap.manual_throttle = false;
ap.manual_attitude = true;
2012-12-08 01:23:32 -04:00
set_yaw_mode(ALT_HOLD_YAW);
set_roll_pitch_mode(ALT_HOLD_RP);
2012-11-24 03:45:28 -04:00
set_throttle_mode(ALT_HOLD_THR);
2013-02-18 01:58:24 -04:00
set_nav_mode(NAV_NONE);
2012-08-21 23:19:50 -03:00
break;
case AUTO:
2012-11-10 01:55:51 -04:00
ap.manual_throttle = false;
ap.manual_attitude = false;
2013-05-09 22:51:13 -03:00
// roll-pitch, throttle and yaw modes will all be set by the first nav command
init_commands(); // clear the command queues. will be reloaded when "run_autopilot" calls "update_commands" function
2012-08-21 23:19:50 -03:00
break;
case CIRCLE:
2012-11-10 01:55:51 -04:00
ap.manual_throttle = false;
ap.manual_attitude = false;
2012-12-08 01:23:32 -04:00
set_roll_pitch_mode(CIRCLE_RP);
set_throttle_mode(CIRCLE_THR);
2013-01-25 02:57:55 -04:00
set_nav_mode(CIRCLE_NAV);
2013-03-22 05:38:07 -03:00
set_yaw_mode(CIRCLE_YAW);
2012-08-21 23:19:50 -03:00
break;
case LOITER:
2012-11-10 01:55:51 -04:00
ap.manual_throttle = false;
ap.manual_attitude = false;
2012-12-08 01:23:32 -04:00
set_yaw_mode(LOITER_YAW);
set_roll_pitch_mode(LOITER_RP);
2012-11-24 03:45:28 -04:00
set_throttle_mode(LOITER_THR);
2013-01-25 02:57:55 -04:00
set_nav_mode(LOITER_NAV);
2012-08-21 23:19:50 -03:00
break;
case POSITION:
2012-11-10 01:55:51 -04:00
ap.manual_throttle = true;
ap.manual_attitude = false;
2013-01-25 02:57:55 -04:00
set_yaw_mode(POSITION_YAW);
set_roll_pitch_mode(POSITION_RP);
set_throttle_mode(POSITION_THR);
set_nav_mode(POSITION_NAV);
2013-04-01 03:45:23 -03:00
wp_nav.clear_angle_limit(); // ensure there are no left over angle limits from throttle controller. To-Do: move this to the exit routine of throttle controller
2012-08-21 23:19:50 -03:00
break;
case GUIDED:
2012-11-10 01:55:51 -04:00
ap.manual_throttle = false;
ap.manual_attitude = false;
2013-04-20 03:36:24 -03:00
set_yaw_mode(get_wp_yaw_mode(false));
2012-12-12 10:28:26 -04:00
set_roll_pitch_mode(GUIDED_RP);
set_throttle_mode(GUIDED_THR);
2013-01-25 02:57:55 -04:00
set_nav_mode(GUIDED_NAV);
2012-08-21 23:19:50 -03:00
break;
case LAND:
2013-02-18 01:58:24 -04:00
// To-Do: it is messy to set manual_attitude here because the do_land function is reponsible for setting the roll_pitch_mode
2013-06-01 12:41:37 -03:00
if( g_gps->status() == GPS::GPS_OK_FIX_3D ) {
2012-11-23 02:57:49 -04:00
// switch to loiter if we have gps
ap.manual_attitude = false;
}else{
// otherwise remain with stabilize roll and pitch
ap.manual_attitude = true;
}
2012-11-10 01:55:51 -04:00
ap.manual_throttle = false;
2013-06-03 03:20:37 -03:00
do_land(NULL); // land at current location
2012-08-21 23:19:50 -03:00
break;
case RTL:
2012-11-10 01:55:51 -04:00
ap.manual_throttle = false;
ap.manual_attitude = false;
2012-11-29 08:08:19 -04:00
do_RTL();
2012-08-21 23:19:50 -03:00
break;
case OF_LOITER:
2012-11-10 01:55:51 -04:00
ap.manual_throttle = false;
ap.manual_attitude = false;
2012-12-08 01:23:32 -04:00
set_yaw_mode(OF_LOITER_YAW);
set_roll_pitch_mode(OF_LOITER_RP);
2012-11-24 03:45:28 -04:00
set_throttle_mode(OF_LOITER_THR);
2013-01-25 02:57:55 -04:00
set_nav_mode(OF_LOITER_NAV);
2012-08-21 23:19:50 -03:00
break;
// THOR
// These are the flight modes for Toy mode
// See the defines for the enumerated values
case TOY_A:
2012-11-10 01:55:51 -04:00
ap.manual_throttle = false;
ap.manual_attitude = true;
2012-12-08 01:23:32 -04:00
set_yaw_mode(YAW_TOY);
set_roll_pitch_mode(ROLL_PITCH_TOY);
2012-11-24 03:45:28 -04:00
set_throttle_mode(THROTTLE_AUTO);
2013-01-24 00:36:55 -04:00
set_nav_mode(NAV_NONE);
2012-08-21 23:19:50 -03:00
// save throttle for fast exit of Alt hold
saved_toy_throttle = g.rc_3.control_in;
break;
case TOY_M:
2012-11-10 01:55:51 -04:00
ap.manual_throttle = false;
ap.manual_attitude = true;
2012-12-08 01:23:32 -04:00
set_yaw_mode(YAW_TOY);
set_roll_pitch_mode(ROLL_PITCH_TOY);
2013-01-24 00:36:55 -04:00
set_nav_mode(NAV_NONE);
2012-11-24 03:45:28 -04:00
set_throttle_mode(THROTTLE_HOLD);
2012-08-21 23:19:50 -03:00
break;
default:
break;
}
2011-09-04 21:15:36 -03:00
2012-11-10 01:55:51 -04:00
if(ap.manual_attitude) {
2012-08-21 23:19:50 -03:00
// We are under manual attitude control
// remove the navigation from roll and pitch command
reset_nav_params();
}
Log_Write_Mode(control_mode);
2011-04-16 17:44:44 -03:00
}
2011-07-17 07:32:00 -03:00
static void
2011-04-16 17:44:44 -03:00
init_simple_bearing()
{
2012-08-21 23:19:50 -03:00
initial_simple_bearing = ahrs.yaw_sensor;
2013-01-12 11:17:44 -04:00
if (g.log_bitmask != 0) {
Log_Write_Data(DATA_INIT_SIMPLE_BEARING, initial_simple_bearing);
}
2011-04-16 17:44:44 -03:00
}
2013-04-17 09:25:32 -03:00
// update_auto_armed - update status of auto_armed flag
static void update_auto_armed()
{
// disarm checks
if(ap.auto_armed){
// if motors are disarmed, auto_armed should also be false
if(!motors.armed()) {
set_auto_armed(false);
return;
}
// if in stabilize or acro flight mode and throttle is zero, auto-armed should become false
if(control_mode <= ACRO && g.rc_3.control_in == 0 && !ap.failsafe_radio) {
set_auto_armed(false);
}
}else{
// arm checks
// if motors are armed and throttle is above zero auto_armed should be true
if(motors.armed() && g.rc_3.control_in != 0) {
set_auto_armed(true);
}
}
}
2011-08-01 08:39:17 -03:00
/*
2012-08-21 23:19:50 -03:00
* map from a 8 bit EEPROM baud rate to a real baud rate
2011-08-01 08:39:17 -03:00
*/
static uint32_t map_baudrate(int8_t rate, uint32_t default_baud)
{
switch (rate) {
2012-02-04 04:06:22 -04:00
case 1: return 1200;
case 2: return 2400;
case 4: return 4800;
2011-08-01 08:39:17 -03:00
case 9: return 9600;
case 19: return 19200;
case 38: return 38400;
case 57: return 57600;
case 111: return 111100;
case 115: return 115200;
}
2012-11-21 02:08:03 -04:00
//cliSerial->println_P(PSTR("Invalid SERIAL3_BAUD"));
2011-08-01 08:39:17 -03:00
return default_baud;
}
2011-11-20 05:42:51 -04:00
#if USB_MUX_PIN > 0
static void check_usb_mux(void)
{
2013-05-19 23:29:34 -03:00
bool usb_check = !digitalRead(USB_MUX_PIN);
2012-11-11 09:42:10 -04:00
if (usb_check == ap_system.usb_connected) {
2011-11-20 05:42:51 -04:00
return;
}
// the user has switched to/from the telemetry port
2012-11-11 09:42:10 -04:00
ap_system.usb_connected = usb_check;
if (ap_system.usb_connected) {
2012-12-13 19:27:42 -04:00
hal.uartA->begin(SERIAL0_BAUD);
2011-11-20 05:42:51 -04:00
} else {
2012-12-13 19:27:42 -04:00
hal.uartA->begin(map_baudrate(g.serial3_baud, SERIAL3_BAUD));
2011-11-20 05:42:51 -04:00
}
}
#endif
2011-12-13 03:19:41 -04:00
/*
2012-08-21 23:19:50 -03:00
* called by gyro/accel init to flash LEDs so user
* has some mesmerising lights to watch while waiting
2011-12-13 03:19:41 -04:00
*/
void flash_leds(bool on)
{
2012-08-21 23:19:50 -03:00
digitalWrite(A_LED_PIN, on ? LED_OFF : LED_ON);
digitalWrite(C_LED_PIN, on ? LED_ON : LED_OFF);
2011-12-13 03:19:41 -04:00
}
2012-03-01 08:36:51 -04:00
/*
* Read Vcc vs 1.1v internal reference
*/
uint16_t board_voltage(void)
{
2012-12-13 15:48:01 -04:00
return board_vcc_analog_source->read_latest();
2012-03-01 08:36:51 -04:00
}
2012-10-22 04:45:24 -03:00
2012-11-24 03:20:03 -04:00
/*
force a software reset of the APM
*/
2012-12-13 19:27:42 -04:00
static void reboot_apm(void) {
hal.scheduler->reboot();
2012-11-24 03:20:03 -04:00
}
2012-10-22 04:45:24 -03:00
//
// print_flight_mode - prints flight mode to serial port.
//
static void
2013-04-20 02:18:22 -03:00
print_flight_mode(AP_HAL::BetterStream *port, uint8_t mode)
2012-10-22 04:45:24 -03:00
{
switch (mode) {
case STABILIZE:
2013-04-20 02:18:22 -03:00
port->print_P(PSTR("STABILIZE"));
2012-10-22 04:45:24 -03:00
break;
case ACRO:
2013-04-20 02:18:22 -03:00
port->print_P(PSTR("ACRO"));
2012-10-22 04:45:24 -03:00
break;
case ALT_HOLD:
2013-04-20 02:18:22 -03:00
port->print_P(PSTR("ALT_HOLD"));
2012-10-22 04:45:24 -03:00
break;
case AUTO:
2013-04-20 02:18:22 -03:00
port->print_P(PSTR("AUTO"));
2012-10-22 04:45:24 -03:00
break;
case GUIDED:
2013-04-20 02:18:22 -03:00
port->print_P(PSTR("GUIDED"));
2012-10-22 04:45:24 -03:00
break;
case LOITER:
2013-04-20 02:18:22 -03:00
port->print_P(PSTR("LOITER"));
2012-10-22 04:45:24 -03:00
break;
case RTL:
2013-04-20 02:18:22 -03:00
port->print_P(PSTR("RTL"));
2012-10-22 04:45:24 -03:00
break;
case CIRCLE:
2013-04-20 02:18:22 -03:00
port->print_P(PSTR("CIRCLE"));
2012-10-22 04:45:24 -03:00
break;
case POSITION:
2013-04-20 02:18:22 -03:00
port->print_P(PSTR("POSITION"));
2012-10-22 04:45:24 -03:00
break;
case LAND:
2013-04-20 02:18:22 -03:00
port->print_P(PSTR("LAND"));
2012-10-22 04:45:24 -03:00
break;
case OF_LOITER:
2013-04-20 02:18:22 -03:00
port->print_P(PSTR("OF_LOITER"));
2012-10-22 04:45:24 -03:00
break;
case TOY_M:
2013-04-20 02:18:22 -03:00
port->print_P(PSTR("TOY_M"));
2012-10-22 04:45:24 -03:00
break;
case TOY_A:
2013-04-20 02:18:22 -03:00
port->print_P(PSTR("TOY_A"));
2012-10-22 04:45:24 -03:00
break;
default:
2013-04-20 02:18:22 -03:00
port->printf_P(PSTR("Mode(%u)"), (unsigned)mode);
2012-10-22 04:45:24 -03:00
break;
}
}