2013-10-13 04:14:13 -03:00
|
|
|
// -*- tab-width: 4; Mode: C++; c-basic-offset: 4; indent-tabs-mode: nil -*-
|
|
|
|
|
2014-08-13 01:43:56 -03:00
|
|
|
// mission storage
|
|
|
|
static const StorageAccess wp_storage(StorageManager::StorageMission);
|
|
|
|
|
2013-10-13 04:14:13 -03:00
|
|
|
static void init_tracker()
|
|
|
|
{
|
2015-01-28 01:27:03 -04:00
|
|
|
// initialise console serial port
|
|
|
|
serial_manager.init_console();
|
2013-10-13 04:14:13 -03:00
|
|
|
|
|
|
|
cliSerial->printf_P(PSTR("\n\nInit " THISFIRMWARE
|
|
|
|
"\n\nFree RAM: %u\n"),
|
2014-02-18 03:55:32 -04:00
|
|
|
hal.util->available_memory());
|
2013-10-13 04:14:13 -03:00
|
|
|
|
|
|
|
// Check the EEPROM format version before loading any parameters from EEPROM
|
|
|
|
load_parameters();
|
|
|
|
|
2014-03-26 18:06:50 -03:00
|
|
|
BoardConfig.init();
|
|
|
|
|
2015-01-28 01:27:03 -04:00
|
|
|
// initialise serial ports
|
|
|
|
serial_manager.init();
|
2013-10-13 04:14:13 -03:00
|
|
|
|
|
|
|
// init baro before we start the GCS, so that the CLI baro test works
|
|
|
|
barometer.init();
|
|
|
|
|
|
|
|
// init the GCS
|
2015-03-27 22:53:24 -03:00
|
|
|
gcs[0].setup_uart(serial_manager, AP_SerialManager::SerialProtocol_Console, 0);
|
2014-03-05 02:38:22 -04:00
|
|
|
|
2014-12-09 23:32:30 -04:00
|
|
|
// set up snooping on other mavlink destinations
|
|
|
|
gcs[0].set_snoop(mavlink_snoop);
|
|
|
|
|
2013-10-13 04:14:13 -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, 5);
|
|
|
|
|
2014-03-22 05:31:28 -03:00
|
|
|
// we start by assuming USB connected, as we initialed the serial
|
|
|
|
// port with SERIAL0_BAUD. check_usb_mux() fixes this if need be.
|
|
|
|
usb_connected = true;
|
|
|
|
check_usb_mux();
|
|
|
|
|
2015-01-28 01:27:03 -04:00
|
|
|
// setup serial port for telem1
|
2015-03-27 22:53:24 -03:00
|
|
|
gcs[1].setup_uart(serial_manager, AP_SerialManager::SerialProtocol_MAVLink, 0);
|
2013-10-13 04:14:13 -03:00
|
|
|
|
2015-01-28 02:23:26 -04:00
|
|
|
#if MAVLINK_COMM_NUM_BUFFERS > 2
|
|
|
|
// setup serial port for telem2
|
2015-03-27 22:53:24 -03:00
|
|
|
gcs[2].setup_uart(serial_manager, AP_SerialManager::SerialProtocol_MAVLink, 1);
|
2015-01-28 02:23:26 -04:00
|
|
|
#endif
|
|
|
|
|
2013-10-13 04:14:13 -03:00
|
|
|
mavlink_system.sysid = g.sysid_this_mav;
|
|
|
|
|
|
|
|
if (g.compass_enabled==true) {
|
|
|
|
if (!compass.init() || !compass.read()) {
|
|
|
|
cliSerial->println_P(PSTR("Compass initialisation failed!"));
|
|
|
|
g.compass_enabled = false;
|
|
|
|
} else {
|
|
|
|
ahrs.set_compass(&compass);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// GPS Initialization
|
2015-01-28 01:27:03 -04:00
|
|
|
gps.init(NULL, serial_manager);
|
2013-10-13 04:14:13 -03:00
|
|
|
|
|
|
|
ahrs.init();
|
|
|
|
ahrs.set_fly_forward(false);
|
|
|
|
|
|
|
|
ins.init(AP_InertialSensor::WARM_START, ins_sample_rate);
|
|
|
|
ahrs.reset();
|
|
|
|
|
|
|
|
init_barometer();
|
|
|
|
|
2015-01-28 01:27:03 -04:00
|
|
|
// set serial ports non-blocking
|
|
|
|
serial_manager.set_blocking_writes_all(false);
|
2013-10-13 04:14:13 -03:00
|
|
|
|
2014-10-06 02:56:25 -03:00
|
|
|
// initialise servos
|
|
|
|
init_servos();
|
2013-10-13 04:14:13 -03:00
|
|
|
|
2014-03-22 05:09:01 -03:00
|
|
|
// use given start positions - useful for indoor testing, and
|
|
|
|
// while waiting for GPS lock
|
|
|
|
current_loc.lat = g.start_latitude * 1.0e7f;
|
|
|
|
current_loc.lng = g.start_longitude * 1.0e7f;
|
2014-03-05 01:47:47 -04:00
|
|
|
|
2014-03-22 05:09:01 -03:00
|
|
|
// see if EEPROM has a default location as well
|
2014-04-09 01:30:27 -03:00
|
|
|
if (current_loc.lat == 0 && current_loc.lng == 0) {
|
|
|
|
get_home_eeprom(current_loc);
|
|
|
|
}
|
2014-03-02 03:00:37 -04:00
|
|
|
|
2013-10-13 04:14:13 -03:00
|
|
|
gcs_send_text_P(SEVERITY_LOW,PSTR("\nReady to track."));
|
2014-03-02 03:00:37 -04:00
|
|
|
hal.scheduler->delay(1000); // Why????
|
2014-03-06 18:13:53 -04:00
|
|
|
|
|
|
|
set_mode(AUTO); // tracking
|
2014-03-22 05:09:01 -03:00
|
|
|
|
|
|
|
if (g.startup_delay > 0) {
|
|
|
|
// arm servos with trim value to allow them to start up (required
|
|
|
|
// for some servos)
|
|
|
|
prepare_servos();
|
|
|
|
}
|
2014-08-03 04:38:19 -03:00
|
|
|
|
|
|
|
// calibrate pressure on startup by default
|
|
|
|
nav_status.need_altitude_calibration = true;
|
2014-03-02 03:00:37 -04:00
|
|
|
}
|
|
|
|
|
2013-10-13 04:14:13 -03:00
|
|
|
// updates the status of the notify objects
|
|
|
|
// should be called at 50hz
|
|
|
|
static void update_notify()
|
|
|
|
{
|
|
|
|
notify.update();
|
|
|
|
}
|
|
|
|
|
2014-03-02 03:00:37 -04:00
|
|
|
/*
|
|
|
|
fetch HOME from EEPROM
|
|
|
|
*/
|
2014-03-22 05:09:01 -03:00
|
|
|
static bool get_home_eeprom(struct Location &loc)
|
2014-03-02 03:00:37 -04:00
|
|
|
{
|
|
|
|
// Find out proper location in memory by using the start_byte position + the index
|
|
|
|
// --------------------------------------------------------------------------------
|
|
|
|
if (g.command_total.get() == 0) {
|
2014-03-22 05:09:01 -03:00
|
|
|
return false;
|
2014-03-02 03:00:37 -04:00
|
|
|
}
|
|
|
|
|
2014-03-22 05:09:01 -03:00
|
|
|
// read WP position
|
2014-08-13 01:43:56 -03:00
|
|
|
loc.options = wp_storage.read_byte(0);
|
|
|
|
loc.alt = wp_storage.read_uint32(1);
|
|
|
|
loc.lat = wp_storage.read_uint32(5);
|
|
|
|
loc.lng = wp_storage.read_uint32(9);
|
2014-03-22 05:09:01 -03:00
|
|
|
|
|
|
|
return true;
|
2014-03-02 03:00:37 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
static void set_home_eeprom(struct Location temp)
|
|
|
|
{
|
2014-08-13 01:43:56 -03:00
|
|
|
wp_storage.write_byte(0, temp.options);
|
|
|
|
wp_storage.write_uint32(1, temp.alt);
|
|
|
|
wp_storage.write_uint32(5, temp.lat);
|
|
|
|
wp_storage.write_uint32(9, temp.lng);
|
2014-03-02 03:00:37 -04:00
|
|
|
|
|
|
|
// Now have a home location in EEPROM
|
|
|
|
g.command_total.set_and_save(1); // At most 1 entry for HOME
|
|
|
|
}
|
|
|
|
|
|
|
|
static void set_home(struct Location temp)
|
|
|
|
{
|
|
|
|
set_home_eeprom(temp);
|
2014-03-05 01:47:47 -04:00
|
|
|
current_loc = temp;
|
2014-03-02 03:00:37 -04:00
|
|
|
}
|
2014-03-03 20:37:15 -04:00
|
|
|
|
|
|
|
static void arm_servos()
|
2014-03-22 05:09:01 -03:00
|
|
|
{
|
2014-03-03 20:37:15 -04:00
|
|
|
channel_yaw.enable_out();
|
|
|
|
channel_pitch.enable_out();
|
|
|
|
}
|
|
|
|
|
|
|
|
static void disarm_servos()
|
|
|
|
{
|
|
|
|
channel_yaw.disable_out();
|
|
|
|
channel_pitch.disable_out();
|
|
|
|
}
|
2014-03-06 18:13:53 -04:00
|
|
|
|
2014-03-22 05:09:01 -03:00
|
|
|
/*
|
|
|
|
setup servos to trim value after initialising
|
|
|
|
*/
|
|
|
|
static void prepare_servos()
|
|
|
|
{
|
|
|
|
start_time_ms = hal.scheduler->millis();
|
|
|
|
channel_yaw.radio_out = channel_yaw.radio_trim;
|
|
|
|
channel_pitch.radio_out = channel_pitch.radio_trim;
|
|
|
|
channel_yaw.output();
|
|
|
|
channel_pitch.output();
|
|
|
|
}
|
|
|
|
|
2014-03-06 18:13:53 -04:00
|
|
|
static void set_mode(enum ControlMode mode)
|
|
|
|
{
|
|
|
|
if(control_mode == mode) {
|
|
|
|
// don't switch modes if we are already in the correct mode.
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
control_mode = mode;
|
2014-03-22 05:09:01 -03:00
|
|
|
|
|
|
|
switch (control_mode) {
|
|
|
|
case AUTO:
|
|
|
|
case MANUAL:
|
2014-04-09 02:27:56 -03:00
|
|
|
case SCAN:
|
2014-03-22 05:09:01 -03:00
|
|
|
arm_servos();
|
|
|
|
break;
|
|
|
|
|
|
|
|
case STOP:
|
|
|
|
case INITIALISING:
|
|
|
|
disarm_servos();
|
|
|
|
break;
|
|
|
|
}
|
2014-03-06 18:13:53 -04:00
|
|
|
}
|
|
|
|
|
2014-09-29 06:47:29 -03:00
|
|
|
/*
|
|
|
|
set_mode() wrapper for MAVLink SET_MODE
|
|
|
|
*/
|
|
|
|
static bool mavlink_set_mode(uint8_t mode)
|
|
|
|
{
|
|
|
|
switch (mode) {
|
|
|
|
case AUTO:
|
|
|
|
case MANUAL:
|
|
|
|
case SCAN:
|
|
|
|
case STOP:
|
|
|
|
set_mode((enum ControlMode)mode);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2014-03-22 05:31:28 -03:00
|
|
|
static void check_usb_mux(void)
|
|
|
|
{
|
|
|
|
bool usb_check = hal.gpio->usb_connected();
|
|
|
|
if (usb_check == usb_connected) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// the user has switched to/from the telemetry port
|
|
|
|
usb_connected = usb_check;
|
|
|
|
|
|
|
|
#if CONFIG_HAL_BOARD == HAL_BOARD_APM2
|
|
|
|
// the APM2 has a MUX setup where the first serial port switches
|
|
|
|
// between USB and a TTL serial connection. When on USB we use
|
|
|
|
// SERIAL0_BAUD, but when connected as a TTL serial port we run it
|
|
|
|
// at SERIAL1_BAUD.
|
|
|
|
if (usb_connected) {
|
2015-04-01 19:13:41 -03:00
|
|
|
serial_manager.set_console_baud(AP_SerialManager::SerialProtocol_Console, 0);
|
2014-03-22 05:31:28 -03:00
|
|
|
} else {
|
2015-03-27 22:53:24 -03:00
|
|
|
serial_manager.set_console_baud(AP_SerialManager::SerialProtocol_MAVLink, 0);
|
2014-03-22 05:31:28 -03:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
}
|