2011-04-16 17:44:44 -03:00
|
|
|
// -*- tab-width: 4; Mode: C++; c-basic-offset: 4; indent-tabs-mode: nil -*-
|
|
|
|
/*****************************************************************************
|
|
|
|
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-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
|
2011-07-17 07:32:00 -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
|
|
|
|
static int8_t planner_mode(uint8_t argc, const Menu::arg *argv); // in planner.pde
|
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
|
|
|
|
static int8_t main_menu_help(uint8_t argc, const Menu::arg *argv)
|
|
|
|
{
|
|
|
|
Serial.printf_P(PSTR("Commands:\n"
|
2011-04-21 02:15:45 -03:00
|
|
|
" logs\n"
|
|
|
|
" setup\n"
|
|
|
|
" test\n"
|
2011-06-20 12:17:08 -03:00
|
|
|
" planner\n"
|
|
|
|
"\n"
|
2011-04-16 17:44:44 -03:00
|
|
|
"Move the slide switch and reset to FLY.\n"
|
|
|
|
"\n"));
|
|
|
|
return(0);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Command/function table for the top-level menu.
|
|
|
|
const struct Menu::command main_menu_commands[] PROGMEM = {
|
|
|
|
// command function called
|
|
|
|
// ======= ===============
|
|
|
|
{"logs", process_logs},
|
|
|
|
{"setup", setup_mode},
|
|
|
|
{"test", test_mode},
|
2011-06-20 12:17:08 -03:00
|
|
|
{"help", main_menu_help},
|
|
|
|
{"planner", planner_mode}
|
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
|
|
|
|
2011-10-27 04:35:25 -03:00
|
|
|
// the user wants the CLI. It never exits
|
|
|
|
static void run_cli(void)
|
|
|
|
{
|
|
|
|
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);
|
|
|
|
|
|
|
|
usb_connected = !digitalRead(USB_MUX_PIN);
|
|
|
|
if (!usb_connected) {
|
|
|
|
// 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
|
|
|
|
|
2011-04-16 17:44:44 -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
|
|
|
|
//
|
|
|
|
Serial.begin(SERIAL0_BAUD, 128, 256);
|
2011-04-16 17:44:44 -03:00
|
|
|
|
|
|
|
// GPS serial port.
|
|
|
|
//
|
2011-04-17 02:17:42 -03:00
|
|
|
#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)
|
|
|
|
Serial1.begin(38400, 256, 16);
|
2011-04-17 02:17:42 -03:00
|
|
|
#endif
|
2011-04-16 17:44:44 -03:00
|
|
|
|
2011-09-17 15:25:31 -03:00
|
|
|
Serial.printf_P(PSTR("\n\nInit " THISFIRMWARE
|
2011-10-28 07:34:10 -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
|
|
|
|
2011-12-09 02:25:02 -04:00
|
|
|
//
|
|
|
|
// Initialize Wire and SPI libraries
|
|
|
|
//
|
2011-12-11 23:59:27 -04:00
|
|
|
#ifndef DESKTOP_BUILD
|
2011-12-28 05:34:59 -04:00
|
|
|
I2c.begin();
|
2012-01-04 01:36:47 -04:00
|
|
|
I2c.timeOut(5);
|
|
|
|
// initially set a fast I2c speed, and drop it on first failures
|
|
|
|
I2c.setSpeed(true);
|
2011-12-11 23:59:27 -04:00
|
|
|
#endif
|
2011-12-09 02:25:02 -04:00
|
|
|
SPI.begin();
|
|
|
|
SPI.setClockDivider(SPI_CLOCK_DIV16); // 1MHZ SPI rate
|
2011-11-12 23:48:32 -04:00
|
|
|
//
|
|
|
|
// Initialize the isr_registry.
|
|
|
|
//
|
|
|
|
isr_registry.init();
|
2011-04-16 17:44:44 -03:00
|
|
|
|
|
|
|
//
|
|
|
|
// Check the EEPROM format version before loading any parameters from EEPROM.
|
|
|
|
//
|
|
|
|
report_version();
|
|
|
|
|
2011-06-16 14:03:26 -03:00
|
|
|
// setup IO pins
|
|
|
|
pinMode(A_LED_PIN, OUTPUT); // GPS status LED
|
2011-11-12 23:44:36 -04:00
|
|
|
digitalWrite(A_LED_PIN, LED_OFF);
|
|
|
|
|
|
|
|
pinMode(B_LED_PIN, OUTPUT); // GPS status LED
|
|
|
|
digitalWrite(B_LED_PIN, LED_OFF);
|
|
|
|
|
|
|
|
pinMode(C_LED_PIN, OUTPUT); // GPS status LED
|
|
|
|
digitalWrite(C_LED_PIN, LED_OFF);
|
|
|
|
|
|
|
|
#if SLIDE_SWITCH_PIN > 0
|
|
|
|
pinMode(SLIDE_SWITCH_PIN, INPUT); // To enter interactive mode
|
|
|
|
#endif
|
2011-11-12 23:48:32 -04:00
|
|
|
#if CONFIG_PUSHBUTTON == ENABLED
|
2011-06-16 14:03:26 -03:00
|
|
|
pinMode(PUSHBUTTON_PIN, INPUT); // unused
|
2011-11-12 23:48:32 -04:00
|
|
|
#endif
|
|
|
|
#if CONFIG_RELAY == ENABLED
|
2011-06-16 14:03:26 -03:00
|
|
|
DDRL |= B00000100; // Set Port L, pin 2 to output for the relay
|
2011-11-12 23:48:32 -04:00
|
|
|
#endif
|
2012-04-12 10:53:54 -03:00
|
|
|
|
|
|
|
#if COPTER_LEDS == ENABLED
|
|
|
|
pinMode(COPTER_LED_1, OUTPUT); //Motor LED
|
|
|
|
pinMode(COPTER_LED_2, OUTPUT); //Motor LED
|
|
|
|
pinMode(COPTER_LED_3, OUTPUT); //Motor LED
|
|
|
|
pinMode(COPTER_LED_4, OUTPUT); //Motor LED
|
|
|
|
pinMode(COPTER_LED_5, OUTPUT); //Motor or Aux LED
|
|
|
|
pinMode(COPTER_LED_6, OUTPUT); //Motor or Aux LED
|
|
|
|
pinMode(COPTER_LED_7, OUTPUT); //Motor or GPS LED
|
|
|
|
pinMode(COPTER_LED_8, OUTPUT); //Motor or GPS LED
|
2012-05-27 13:21:20 -03:00
|
|
|
|
|
|
|
if ( !bitRead(g.copter_leds_mode, 3) ){
|
2011-09-18 21:12:28 -03:00
|
|
|
piezo_beep();
|
2012-05-15 13:00:21 -03:00
|
|
|
}
|
2012-05-27 13:21:20 -03:00
|
|
|
|
2012-05-15 13:00:21 -03:00
|
|
|
#endif
|
2012-05-27 13:21:20 -03:00
|
|
|
|
2011-09-18 21:12:28 -03:00
|
|
|
|
2012-02-12 07:27:51 -04:00
|
|
|
// load parameters from EEPROM
|
|
|
|
load_parameters();
|
2011-04-16 17:44:44 -03:00
|
|
|
|
2011-11-20 05:42:51 -04:00
|
|
|
// init the GCS
|
|
|
|
gcs0.init(&Serial);
|
|
|
|
|
|
|
|
#if USB_MUX_PIN > 0
|
|
|
|
if (!usb_connected) {
|
|
|
|
// we are not connected via USB, re-init UART0 with right
|
|
|
|
// baud rate
|
2012-03-30 02:53:50 -03:00
|
|
|
Serial.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
|
2012-03-30 02:53:50 -03:00
|
|
|
Serial3.begin(map_baudrate(g.serial3_baud, SERIAL3_BAUD), 128, 256);
|
2011-11-20 05:42:51 -04:00
|
|
|
gcs3.init(&Serial3);
|
|
|
|
#endif
|
2011-08-01 08:39:17 -03:00
|
|
|
|
2011-12-02 16:54:36 -04:00
|
|
|
// identify ourselves correctly with the ground station
|
|
|
|
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"));
|
2011-12-28 00:53:05 -04:00
|
|
|
do_erase_logs();
|
2011-12-17 19:19:41 -04:00
|
|
|
}
|
2011-12-28 00:53:05 -04:00
|
|
|
if (g.log_bitmask != 0){
|
|
|
|
DataFlash.start_new_log();
|
|
|
|
}
|
|
|
|
#endif
|
2011-12-17 19:19:41 -04:00
|
|
|
|
2011-04-17 02:17:42 -03:00
|
|
|
#ifdef RADIO_OVERRIDE_DEFAULTS
|
2011-04-16 17:44:44 -03:00
|
|
|
{
|
|
|
|
int16_t rc_override[8] = RADIO_OVERRIDE_DEFAULTS;
|
|
|
|
APM_RC.setHIL(rc_override);
|
|
|
|
}
|
2011-04-17 02:17:42 -03:00
|
|
|
#endif
|
2011-06-16 14:03:26 -03:00
|
|
|
|
|
|
|
#if FRAME_CONFIG == HELI_FRAME
|
2012-04-04 11:03:21 -03:00
|
|
|
motors.servo_manual = false;
|
|
|
|
motors.init_swash(); // heli initialisation
|
2011-06-16 14:03:26 -03:00
|
|
|
#endif
|
2011-04-16 17:44:44 -03:00
|
|
|
|
2011-11-12 23:48:32 -04:00
|
|
|
RC_Channel::set_apm_rc(&APM_RC);
|
2011-04-16 17:44:44 -03:00
|
|
|
init_rc_in(); // sets up rc channels from radio
|
|
|
|
init_rc_out(); // sets up the timer libs
|
2011-11-21 01:24:32 -04:00
|
|
|
|
2011-07-30 17:42:54 -03:00
|
|
|
init_camera();
|
2011-04-17 02:17:42 -03:00
|
|
|
|
2011-11-12 23:48:32 -04:00
|
|
|
timer_scheduler.init( &isr_registry );
|
|
|
|
|
2011-11-12 22:47:54 -04:00
|
|
|
#if HIL_MODE != HIL_MODE_ATTITUDE
|
2011-11-12 23:48:32 -04:00
|
|
|
#if CONFIG_ADC == ENABLED
|
2011-10-12 02:20:23 -03:00
|
|
|
// begin filtering the ADC Gyros
|
2011-11-13 01:06:40 -04:00
|
|
|
adc.Init(&timer_scheduler); // APM ADC library initialization
|
2011-11-12 23:48:32 -04:00
|
|
|
#endif // CONFIG_ADC
|
2011-10-04 07:55:10 -03:00
|
|
|
|
2011-12-09 02:47:59 -04:00
|
|
|
barometer.init(&timer_scheduler);
|
2011-11-12 22:47:54 -04:00
|
|
|
|
2011-11-12 23:48:32 -04:00
|
|
|
#endif // HIL_MODE
|
2011-04-16 17:44:44 -03:00
|
|
|
|
|
|
|
// Do GPS init
|
|
|
|
g_gps = &g_gps_driver;
|
2012-06-10 03:36:18 -03:00
|
|
|
// GPS Initialization
|
|
|
|
g_gps->init(GPS::GPS_ENGINE_AIRBORNE_1G);
|
2011-08-01 05:08:52 -03:00
|
|
|
g_gps->callback = mavlink_delay;
|
2011-04-16 17:44:44 -03:00
|
|
|
|
|
|
|
if(g.compass_enabled)
|
|
|
|
init_compass();
|
|
|
|
|
2011-07-21 20:14:53 -03:00
|
|
|
// init the optical flow sensor
|
|
|
|
if(g.optflow_enabled) {
|
|
|
|
init_optflow();
|
|
|
|
}
|
2011-12-23 18:42:05 -04:00
|
|
|
|
2011-07-30 17:42:54 -03:00
|
|
|
|
2011-10-18 03:51:47 -03:00
|
|
|
// agmatthews USERHOOKS
|
|
|
|
#ifdef USERHOOK_INIT
|
|
|
|
USERHOOK_INIT
|
|
|
|
#endif
|
2011-11-19 18:01:47 -04:00
|
|
|
|
2011-10-27 04:35:25 -03:00
|
|
|
#if CLI_ENABLED == ENABLED && CLI_SLIDER_ENABLED == ENABLED
|
2011-04-16 17:44:44 -03:00
|
|
|
// If the switch is in 'menu' mode, run the main menu.
|
|
|
|
//
|
|
|
|
// Since we can't be sure that the setup or test mode won't leave
|
|
|
|
// the system in an odd state, we don't let the user exit the top
|
|
|
|
// menu; they must reset in order to fly.
|
|
|
|
//
|
2011-06-16 14:03:26 -03:00
|
|
|
if (check_startup_for_CLI()) {
|
2011-11-12 23:44:36 -04:00
|
|
|
digitalWrite(A_LED_PIN, LED_ON); // turn on setup-mode LED
|
2011-10-15 17:09:04 -03:00
|
|
|
Serial.printf_P(PSTR("\nCLI:\n\n"));
|
2011-10-27 04:35:25 -03:00
|
|
|
run_cli();
|
2011-04-16 17:44:44 -03:00
|
|
|
}
|
2011-10-27 04:35:25 -03:00
|
|
|
#else
|
2011-11-19 18:01:47 -04:00
|
|
|
Serial.printf_P(PSTR("\nPress ENTER 3 times for CLI\n\n"));
|
2011-07-17 07:34:05 -03:00
|
|
|
#endif // CLI_ENABLED
|
|
|
|
|
2011-04-16 17:44:44 -03:00
|
|
|
GPS_enabled = false;
|
|
|
|
|
2011-11-08 18:10:09 -04:00
|
|
|
#if HIL_MODE == HIL_MODE_DISABLED
|
2011-04-16 17:44:44 -03:00
|
|
|
// Read in the GPS
|
|
|
|
for (byte counter = 0; ; counter++) {
|
|
|
|
g_gps->update();
|
|
|
|
if (g_gps->status() != 0){
|
|
|
|
GPS_enabled = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (counter >= 2) {
|
|
|
|
GPS_enabled = false;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2011-11-08 18:10:09 -04:00
|
|
|
#else
|
|
|
|
GPS_enabled = true;
|
|
|
|
#endif
|
2011-07-10 21:47:08 -03:00
|
|
|
|
2011-07-30 17:42:54 -03:00
|
|
|
// lengthen the idle timeout for gps Auto_detect
|
|
|
|
// ---------------------------------------------
|
|
|
|
g_gps->idleTimeout = 20000;
|
2011-06-16 14:03:26 -03:00
|
|
|
|
2011-07-10 21:47:08 -03:00
|
|
|
// print the GPS status
|
2011-07-30 17:42:54 -03:00
|
|
|
// --------------------
|
2011-04-16 17:44:44 -03:00
|
|
|
report_gps();
|
2011-07-10 21:47:08 -03:00
|
|
|
|
2011-07-30 17:42:54 -03:00
|
|
|
#if HIL_MODE != HIL_MODE_ATTITUDE
|
|
|
|
// read Baro pressure at ground
|
|
|
|
//-----------------------------
|
|
|
|
init_barometer();
|
|
|
|
#endif
|
2011-12-14 01:21:35 -04:00
|
|
|
|
2011-12-11 03:40:59 -04:00
|
|
|
// initialise sonar
|
2012-01-11 03:40:39 -04:00
|
|
|
#if CONFIG_SONAR == ENABLED
|
2011-12-11 03:40:59 -04:00
|
|
|
init_sonar();
|
|
|
|
#endif
|
2011-07-30 17:42:54 -03:00
|
|
|
|
|
|
|
// initialize commands
|
|
|
|
// -------------------
|
|
|
|
init_commands();
|
|
|
|
|
|
|
|
// set the correct flight mode
|
|
|
|
// ---------------------------
|
|
|
|
reset_control_switch();
|
|
|
|
|
2011-11-05 01:41:51 -03:00
|
|
|
// init the Z damopener
|
|
|
|
// --------------------
|
2011-12-23 18:42:05 -04:00
|
|
|
#if ACCEL_ALT_HOLD != 0
|
2011-11-05 01:41:51 -03:00
|
|
|
init_z_damper();
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
2011-08-01 01:47:15 -03:00
|
|
|
startup_ground();
|
2011-07-30 17:42:54 -03:00
|
|
|
|
2011-11-20 04:49:56 -04:00
|
|
|
#if LOGGING_ENABLED == ENABLED
|
2011-06-16 14:03:26 -03:00
|
|
|
Log_Write_Startup();
|
2012-02-17 02:08:19 -04:00
|
|
|
Log_Write_Data(10, (float)g.pi_stabilize_roll.kP());
|
|
|
|
Log_Write_Data(11, (float)g.pi_stabilize_roll.kI());
|
|
|
|
|
|
|
|
Log_Write_Data(12, (float)g.pid_rate_roll.kP());
|
|
|
|
Log_Write_Data(13, (float)g.pid_rate_roll.kI());
|
|
|
|
Log_Write_Data(14, (float)g.pid_rate_roll.kD());
|
|
|
|
Log_Write_Data(15, (float)g.stabilize_d.get());
|
|
|
|
|
|
|
|
Log_Write_Data(16, (float)g.pi_loiter_lon.kP());
|
|
|
|
Log_Write_Data(17, (float)g.pi_loiter_lon.kI());
|
|
|
|
|
|
|
|
Log_Write_Data(18, (float)g.pid_nav_lon.kP());
|
|
|
|
Log_Write_Data(19, (float)g.pid_nav_lon.kI());
|
|
|
|
Log_Write_Data(20, (float)g.pid_nav_lon.kD());
|
|
|
|
|
|
|
|
Log_Write_Data(21, (int32_t)g.auto_slew_rate.get());
|
2012-02-26 15:13:28 -04:00
|
|
|
|
|
|
|
Log_Write_Data(22, (float)g.pid_loiter_rate_lon.kP());
|
|
|
|
Log_Write_Data(23, (float)g.pid_loiter_rate_lon.kI());
|
|
|
|
Log_Write_Data(24, (float)g.pid_loiter_rate_lon.kD());
|
2011-11-20 04:49:56 -04:00
|
|
|
#endif
|
2011-06-16 14:03:26 -03:00
|
|
|
|
2011-04-16 17:44:44 -03:00
|
|
|
SendDebug("\nReady to FLY ");
|
2011-07-30 17:42:54 -03:00
|
|
|
}
|
2011-06-28 03:31:18 -03:00
|
|
|
|
2012-01-04 19:15:14 -04:00
|
|
|
|
2011-07-30 17:42:54 -03:00
|
|
|
//********************************************************************************
|
|
|
|
//This function does all the calibrations, etc. that we need during a ground start
|
|
|
|
//********************************************************************************
|
|
|
|
static void startup_ground(void)
|
|
|
|
{
|
2011-10-11 06:12:37 -03:00
|
|
|
gcs_send_text_P(SEVERITY_LOW,PSTR("GROUND START"));
|
2011-07-30 17:42:54 -03:00
|
|
|
|
|
|
|
#if HIL_MODE != HIL_MODE_ATTITUDE
|
|
|
|
// Warm up and read Gyro offsets
|
|
|
|
// -----------------------------
|
2011-12-13 03:19:41 -04:00
|
|
|
imu.init(IMU::COLD_START, mavlink_delay, flash_leds, &timer_scheduler);
|
2011-09-17 15:25:31 -03:00
|
|
|
#if CLI_ENABLED == ENABLED
|
|
|
|
report_imu();
|
|
|
|
#endif
|
2011-07-30 17:42:54 -03:00
|
|
|
#endif
|
|
|
|
|
|
|
|
// 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
|
|
|
}
|
|
|
|
|
2011-09-04 21:15:36 -03:00
|
|
|
/*
|
|
|
|
#define YAW_HOLD 0
|
|
|
|
#define YAW_ACRO 1
|
|
|
|
#define YAW_AUTO 2
|
|
|
|
#define YAW_LOOK_AT_HOME 3
|
|
|
|
|
|
|
|
#define ROLL_PITCH_STABLE 0
|
|
|
|
#define ROLL_PITCH_ACRO 1
|
2011-09-16 03:33:00 -03:00
|
|
|
#define ROLL_PITCH_AUTO 2
|
2011-09-04 21:15:36 -03:00
|
|
|
|
|
|
|
#define THROTTLE_MANUAL 0
|
|
|
|
#define THROTTLE_HOLD 1
|
|
|
|
#define THROTTLE_AUTO 2
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
2011-07-17 07:32:00 -03:00
|
|
|
static void set_mode(byte mode)
|
2011-04-16 17:44:44 -03:00
|
|
|
{
|
2011-11-19 15:08:03 -04:00
|
|
|
// if we don't have GPS lock
|
|
|
|
if(home_is_set == false){
|
|
|
|
// our max mode should be
|
2012-01-27 20:27:16 -04:00
|
|
|
if (mode > ALT_HOLD && mode != OF_LOITER)
|
2011-11-19 15:08:03 -04:00
|
|
|
mode = STABILIZE;
|
|
|
|
}
|
|
|
|
|
2012-01-27 20:27:16 -04:00
|
|
|
// nothing but OF_LOITER for OptFlow only
|
2011-12-23 18:42:05 -04:00
|
|
|
if (g.optflow_enabled && GPS_enabled == false){
|
2012-01-27 20:27:16 -04:00
|
|
|
if (mode > ALT_HOLD && mode != OF_LOITER)
|
2011-12-23 18:42:05 -04:00
|
|
|
mode = STABILIZE;
|
|
|
|
}
|
|
|
|
|
2012-02-22 01:48:07 -04:00
|
|
|
control_mode = mode;
|
|
|
|
control_mode = constrain(control_mode, 0, NUM_MODES - 1);
|
2011-04-16 17:44:44 -03:00
|
|
|
|
|
|
|
// used to stop fly_aways
|
2012-04-04 11:03:21 -03:00
|
|
|
motors.auto_armed(g.rc_3.control_in > 0);
|
2011-04-16 17:44:44 -03:00
|
|
|
|
2011-10-29 01:29:10 -03:00
|
|
|
// clearing value used in interactive alt hold
|
|
|
|
manual_boost = 0;
|
|
|
|
|
2012-01-13 02:25:29 -04:00
|
|
|
// clearing value used to force the copter down in landing mode
|
|
|
|
landing_boost = 0;
|
2012-06-04 02:25:27 -03:00
|
|
|
reset_throttle_flag = false;
|
2012-01-13 02:25:29 -04:00
|
|
|
|
2012-02-19 17:13:42 -04:00
|
|
|
// do we want to come to a stop or pass a WP?
|
|
|
|
slow_wp = false;
|
|
|
|
|
2012-01-06 01:14:23 -04:00
|
|
|
// do not auto_land if we are leaving RTL
|
2012-04-16 12:12:30 -03:00
|
|
|
loiter_timer = 0;
|
2012-01-06 01:14:23 -04:00
|
|
|
|
2012-01-08 02:26:56 -04:00
|
|
|
// if we change modes, we must clear landed flag
|
|
|
|
land_complete = false;
|
|
|
|
|
2012-01-06 14:21:50 -04:00
|
|
|
// debug to Serial terminal
|
2012-02-22 01:48:07 -04:00
|
|
|
//Serial.println(flight_mode_strings[control_mode]);
|
2011-05-14 23:02:09 -03:00
|
|
|
|
2011-09-04 21:15:36 -03:00
|
|
|
// report the GPS and Motor arming status
|
2011-06-16 14:03:26 -03:00
|
|
|
led_mode = NORMAL_LEDS;
|
|
|
|
|
2011-04-16 17:44:44 -03:00
|
|
|
switch(control_mode)
|
|
|
|
{
|
|
|
|
case ACRO:
|
2012-02-11 02:25:24 -04:00
|
|
|
yaw_mode = YAW_HOLD;
|
2011-09-04 21:15:36 -03:00
|
|
|
roll_pitch_mode = ROLL_PITCH_ACRO;
|
|
|
|
throttle_mode = THROTTLE_MANUAL;
|
2011-04-16 17:44:44 -03:00
|
|
|
break;
|
|
|
|
|
|
|
|
case STABILIZE:
|
2011-09-04 21:15:36 -03:00
|
|
|
yaw_mode = YAW_HOLD;
|
|
|
|
roll_pitch_mode = ROLL_PITCH_STABLE;
|
|
|
|
throttle_mode = THROTTLE_MANUAL;
|
|
|
|
break;
|
|
|
|
|
2011-04-16 17:44:44 -03:00
|
|
|
case ALT_HOLD:
|
2011-09-04 21:15:36 -03:00
|
|
|
yaw_mode = ALT_HOLD_YAW;
|
|
|
|
roll_pitch_mode = ALT_HOLD_RP;
|
|
|
|
throttle_mode = ALT_HOLD_THR;
|
|
|
|
|
2012-03-10 16:44:18 -04:00
|
|
|
force_new_altitude(max(current_loc.alt, 100));
|
2011-04-16 17:44:44 -03:00
|
|
|
break;
|
|
|
|
|
|
|
|
case AUTO:
|
2011-09-04 21:15:36 -03:00
|
|
|
yaw_mode = AUTO_YAW;
|
|
|
|
roll_pitch_mode = AUTO_RP;
|
|
|
|
throttle_mode = AUTO_THR;
|
|
|
|
|
|
|
|
// loads the commands from where we left off
|
2011-09-12 16:57:36 -03:00
|
|
|
init_commands();
|
2011-04-16 17:44:44 -03:00
|
|
|
break;
|
|
|
|
|
2011-07-30 17:42:54 -03:00
|
|
|
case CIRCLE:
|
2011-09-04 21:15:36 -03:00
|
|
|
yaw_mode = CIRCLE_YAW;
|
|
|
|
roll_pitch_mode = CIRCLE_RP;
|
|
|
|
throttle_mode = CIRCLE_THR;
|
2012-01-11 03:40:39 -04:00
|
|
|
set_next_WP(¤t_loc);
|
2012-05-27 13:21:20 -03:00
|
|
|
circle_WP = next_WP;
|
2012-01-03 14:35:18 -04:00
|
|
|
circle_angle = 0;
|
2011-09-04 21:15:36 -03:00
|
|
|
break;
|
|
|
|
|
2011-04-16 17:44:44 -03:00
|
|
|
case LOITER:
|
2011-09-04 21:15:36 -03:00
|
|
|
yaw_mode = LOITER_YAW;
|
|
|
|
roll_pitch_mode = LOITER_RP;
|
|
|
|
throttle_mode = LOITER_THR;
|
2012-01-11 03:40:39 -04:00
|
|
|
set_next_WP(¤t_loc);
|
2012-06-15 02:18:26 -03:00
|
|
|
#if INERTIAL_NAV == ENABLED
|
|
|
|
zero_accels();
|
|
|
|
#endif
|
2011-04-16 17:44:44 -03:00
|
|
|
break;
|
|
|
|
|
2011-09-30 03:27:23 -03:00
|
|
|
case POSITION:
|
|
|
|
yaw_mode = YAW_HOLD;
|
|
|
|
roll_pitch_mode = ROLL_PITCH_AUTO;
|
|
|
|
throttle_mode = THROTTLE_MANUAL;
|
2012-01-11 03:40:39 -04:00
|
|
|
set_next_WP(¤t_loc);
|
2011-09-30 03:27:23 -03:00
|
|
|
break;
|
|
|
|
|
2011-07-02 19:44:59 -03:00
|
|
|
case GUIDED:
|
2011-09-04 21:15:36 -03:00
|
|
|
yaw_mode = YAW_AUTO;
|
|
|
|
roll_pitch_mode = ROLL_PITCH_AUTO;
|
|
|
|
throttle_mode = THROTTLE_AUTO;
|
2012-01-03 14:35:18 -04:00
|
|
|
next_WP = current_loc;
|
2011-09-12 16:57:36 -03:00
|
|
|
set_next_WP(&guided_WP);
|
2011-07-02 19:44:59 -03:00
|
|
|
break;
|
|
|
|
|
2011-12-09 19:33:47 -04:00
|
|
|
case LAND:
|
2012-01-08 02:26:56 -04:00
|
|
|
yaw_mode = LOITER_YAW;
|
|
|
|
roll_pitch_mode = LOITER_RP;
|
2011-12-09 19:33:47 -04:00
|
|
|
throttle_mode = THROTTLE_AUTO;
|
2012-01-08 02:26:56 -04:00
|
|
|
do_land();
|
2011-12-09 19:33:47 -04:00
|
|
|
break;
|
|
|
|
|
2012-04-15 18:55:34 -03:00
|
|
|
case APPROACH:
|
|
|
|
yaw_mode = LOITER_YAW;
|
|
|
|
roll_pitch_mode = LOITER_RP;
|
|
|
|
throttle_mode = THROTTLE_AUTO;
|
|
|
|
do_approach();
|
|
|
|
break;
|
|
|
|
|
2011-04-16 17:44:44 -03:00
|
|
|
case RTL:
|
2011-09-04 21:15:36 -03:00
|
|
|
yaw_mode = RTL_YAW;
|
|
|
|
roll_pitch_mode = RTL_RP;
|
|
|
|
throttle_mode = RTL_THR;
|
|
|
|
|
2011-04-16 17:44:44 -03:00
|
|
|
do_RTL();
|
|
|
|
break;
|
|
|
|
|
2012-01-27 20:27:16 -04:00
|
|
|
case OF_LOITER:
|
|
|
|
yaw_mode = OF_LOITER_YAW;
|
|
|
|
roll_pitch_mode = OF_LOITER_RP;
|
|
|
|
throttle_mode = OF_LOITER_THR;
|
|
|
|
set_next_WP(¤t_loc);
|
|
|
|
break;
|
|
|
|
|
2011-04-16 17:44:44 -03:00
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2012-01-06 14:21:50 -04:00
|
|
|
if(failsafe){
|
|
|
|
// this is to allow us to fly home without interactive throttle control
|
|
|
|
throttle_mode = THROTTLE_AUTO;
|
|
|
|
// does not wait for us to be in high throttle, since the
|
|
|
|
// Receiver will be outputting low throttle
|
2012-04-04 11:03:21 -03:00
|
|
|
motors.auto_armed(true);
|
2012-01-06 14:21:50 -04:00
|
|
|
}
|
|
|
|
|
2012-03-07 02:22:14 -04:00
|
|
|
// called to calculate gain for alt hold
|
|
|
|
update_throttle_cruise();
|
2011-11-13 01:46:57 -04:00
|
|
|
|
|
|
|
if(roll_pitch_mode <= ROLL_PITCH_ACRO){
|
|
|
|
// We are under manual attitude control
|
2012-01-20 14:11:18 -04:00
|
|
|
// remove the navigation from roll and pitch command
|
|
|
|
reset_nav_params();
|
|
|
|
// remove the wind compenstaion
|
|
|
|
reset_wind_I();
|
|
|
|
// Clears the alt hold compensation
|
|
|
|
reset_throttle_I();
|
2011-11-13 01:46:57 -04:00
|
|
|
}
|
|
|
|
|
2011-05-29 17:27:14 -03:00
|
|
|
Log_Write_Mode(control_mode);
|
2011-04-16 17:44:44 -03:00
|
|
|
}
|
|
|
|
|
2011-07-17 07:32:00 -03:00
|
|
|
static void set_failsafe(boolean mode)
|
2011-04-16 17:44:44 -03:00
|
|
|
{
|
|
|
|
// only act on changes
|
|
|
|
// -------------------
|
|
|
|
if(failsafe != mode){
|
|
|
|
|
|
|
|
// store the value so we don't trip the gate twice
|
|
|
|
// -----------------------------------------------
|
|
|
|
failsafe = mode;
|
|
|
|
|
|
|
|
if (failsafe == false){
|
|
|
|
// We've regained radio contact
|
|
|
|
// ----------------------------
|
|
|
|
failsafe_off_event();
|
|
|
|
|
|
|
|
}else{
|
|
|
|
// We've lost radio contact
|
|
|
|
// ------------------------
|
|
|
|
failsafe_on_event();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-07-17 07:32:00 -03:00
|
|
|
static void
|
2011-04-16 17:44:44 -03:00
|
|
|
init_simple_bearing()
|
|
|
|
{
|
2012-03-11 05:36:12 -03:00
|
|
|
initial_simple_bearing = ahrs.yaw_sensor;
|
2011-04-16 17:44:44 -03:00
|
|
|
}
|
|
|
|
|
2012-01-13 20:48:05 -04:00
|
|
|
static void update_throttle_cruise()
|
|
|
|
{
|
|
|
|
int16_t tmp = g.pi_alt_hold.get_integrator();
|
|
|
|
if(tmp != 0){
|
|
|
|
g.throttle_cruise += tmp;
|
2012-01-20 14:11:18 -04:00
|
|
|
reset_throttle_I();
|
2012-01-13 20:48:05 -04:00
|
|
|
}
|
|
|
|
|
2012-03-07 02:22:14 -04:00
|
|
|
// recalc kp
|
2012-03-10 16:44:49 -04:00
|
|
|
//g.pid_throttle.kP((float)g.throttle_cruise.get() / 981.0);
|
2012-03-07 02:22:14 -04:00
|
|
|
//Serial.printf("kp:%1.4f\n",kp);
|
2011-05-09 14:40:32 -03:00
|
|
|
}
|
|
|
|
|
2011-11-13 00:03:16 -04:00
|
|
|
#if CLI_SLIDER_ENABLED == ENABLED && CLI_ENABLED == ENABLED
|
2011-07-17 07:32:00 -03:00
|
|
|
static boolean
|
2011-06-16 14:03:26 -03:00
|
|
|
check_startup_for_CLI()
|
|
|
|
{
|
|
|
|
return (digitalRead(SLIDE_SWITCH_PIN) == 0);
|
|
|
|
}
|
2011-10-09 08:39:23 -03:00
|
|
|
#endif // CLI_ENABLED
|
2011-08-01 08:39:17 -03:00
|
|
|
|
|
|
|
/*
|
|
|
|
map from a 8 bit EEPROM baud rate to a real baud rate
|
|
|
|
*/
|
|
|
|
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;
|
|
|
|
}
|
2011-10-15 17:09:04 -03:00
|
|
|
//Serial.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)
|
|
|
|
{
|
|
|
|
bool usb_check = !digitalRead(USB_MUX_PIN);
|
|
|
|
if (usb_check == usb_connected) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// the user has switched to/from the telemetry port
|
|
|
|
usb_connected = usb_check;
|
|
|
|
if (usb_connected) {
|
2012-03-30 02:53:50 -03:00
|
|
|
Serial.begin(SERIAL0_BAUD);
|
2011-11-20 05:42:51 -04:00
|
|
|
} else {
|
2012-03-30 02:53:50 -03:00
|
|
|
Serial.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
|
|
|
|
|
|
|
/*
|
|
|
|
called by gyro/accel init to flash LEDs so user
|
|
|
|
has some mesmerising lights to watch while waiting
|
|
|
|
*/
|
|
|
|
void flash_leds(bool on)
|
|
|
|
{
|
|
|
|
digitalWrite(A_LED_PIN, on?LED_OFF:LED_ON);
|
|
|
|
digitalWrite(C_LED_PIN, on?LED_ON:LED_OFF);
|
|
|
|
}
|
2012-03-01 08:36:51 -04:00
|
|
|
|
|
|
|
#ifndef DESKTOP_BUILD
|
|
|
|
/*
|
|
|
|
* Read Vcc vs 1.1v internal reference
|
|
|
|
*
|
|
|
|
* This call takes about 150us total. ADC conversion is 13 cycles of
|
|
|
|
* 125khz default changes the mux if it isn't set, and return last
|
|
|
|
* reading (allows necessary settle time) otherwise trigger the
|
|
|
|
* conversion
|
|
|
|
*/
|
|
|
|
uint16_t board_voltage(void)
|
|
|
|
{
|
|
|
|
const uint8_t mux = (_BV(REFS0)|_BV(MUX4)|_BV(MUX3)|_BV(MUX2)|_BV(MUX1));
|
|
|
|
|
|
|
|
if (ADMUX == mux) {
|
|
|
|
ADCSRA |= _BV(ADSC); // Convert
|
|
|
|
uint16_t counter=4000; // normally takes about 1700 loops
|
|
|
|
while (bit_is_set(ADCSRA, ADSC) && counter) // Wait
|
|
|
|
counter--;
|
|
|
|
if (counter == 0) {
|
|
|
|
// we don't actually expect this timeout to happen,
|
2012-03-02 22:29:26 -04:00
|
|
|
// but we don't want any more code that could hang. We
|
|
|
|
// report 0V so it is clear in the logs that we don't know
|
|
|
|
// the value
|
|
|
|
return 0;
|
2012-03-01 08:36:51 -04:00
|
|
|
}
|
|
|
|
uint32_t result = ADCL | ADCH<<8;
|
2012-03-02 22:29:26 -04:00
|
|
|
return 1126400UL / result; // Read and back-calculate Vcc in mV
|
|
|
|
}
|
|
|
|
// switch mux, settle time is needed. We don't want to delay
|
|
|
|
// waiting for the settle, so report 0 as a "don't know" value
|
|
|
|
ADMUX = mux;
|
|
|
|
return 0; // we don't know the current voltage
|
2012-03-01 08:36:51 -04:00
|
|
|
}
|
|
|
|
#endif
|