2015-06-01 02:04:25 -03:00
|
|
|
#include "Tracker.h"
|
2016-05-05 19:10:08 -03:00
|
|
|
#include "version.h"
|
2015-06-01 02:04:25 -03:00
|
|
|
|
2014-08-13 01:43:56 -03:00
|
|
|
// mission storage
|
|
|
|
static const StorageAccess wp_storage(StorageManager::StorageMission);
|
|
|
|
|
2015-06-01 02:04:25 -03:00
|
|
|
static void mavlink_snoop_static(const mavlink_message_t* msg)
|
|
|
|
{
|
2015-07-03 08:07:56 -03:00
|
|
|
tracker.mavlink_snoop(msg);
|
2015-06-01 02:04:25 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
static void mavlink_delay_cb_static()
|
|
|
|
{
|
|
|
|
tracker.mavlink_delay_cb();
|
|
|
|
}
|
|
|
|
|
|
|
|
void Tracker::init_tracker()
|
2013-10-13 04:14:13 -03:00
|
|
|
{
|
2015-01-28 01:27:03 -04:00
|
|
|
// initialise console serial port
|
|
|
|
serial_manager.init_console();
|
2013-10-13 04:14:13 -03:00
|
|
|
|
2015-10-25 17:10:41 -03:00
|
|
|
hal.console->printf("\n\nInit " THISFIRMWARE
|
2015-10-24 18:45:41 -03:00
|
|
|
"\n\nFree RAM: %u\n",
|
2015-06-01 02:04:25 -03: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();
|
|
|
|
|
2016-08-03 04:17:24 -03:00
|
|
|
GCS_MAVLINK::set_dataflash(&DataFlash);
|
2014-03-26 18:06:50 -03:00
|
|
|
|
2016-10-16 19:21:08 -03:00
|
|
|
mavlink_system.sysid = g.sysid_this_mav;
|
|
|
|
|
2015-01-28 01:27:03 -04:00
|
|
|
// initialise serial ports
|
|
|
|
serial_manager.init();
|
2013-10-13 04:14:13 -03:00
|
|
|
|
2016-08-03 04:17:24 -03:00
|
|
|
// setup first port early to allow BoardConfig to report errors
|
|
|
|
gcs[0].setup_uart(serial_manager, AP_SerialManager::SerialProtocol_MAVLink, 0);
|
|
|
|
|
|
|
|
// 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();
|
|
|
|
|
2013-10-13 04:14:13 -03:00
|
|
|
// init baro before we start the GCS, so that the CLI baro test works
|
|
|
|
barometer.init();
|
|
|
|
|
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();
|
|
|
|
|
2016-05-16 00:33:43 -03:00
|
|
|
// setup telem slots with serial ports
|
2016-08-03 04:17:24 -03:00
|
|
|
for (uint8_t i = 1; i < MAVLINK_COMM_NUM_BUFFERS; i++) {
|
2016-05-16 00:33:43 -03:00
|
|
|
gcs[i].setup_uart(serial_manager, AP_SerialManager::SerialProtocol_MAVLink, i);
|
|
|
|
gcs[i].set_snoop(mavlink_snoop_static);
|
|
|
|
}
|
2015-05-02 09:38:58 -03:00
|
|
|
|
2015-12-27 03:05:14 -04:00
|
|
|
#if LOGGING_ENABLED == ENABLED
|
|
|
|
log_init();
|
|
|
|
#endif
|
|
|
|
|
2013-10-13 04:14:13 -03:00
|
|
|
if (g.compass_enabled==true) {
|
|
|
|
if (!compass.init() || !compass.read()) {
|
2015-10-25 16:50:51 -03:00
|
|
|
hal.console->println("Compass initialisation failed!");
|
2013-10-13 04:14:13 -03:00
|
|
|
g.compass_enabled = false;
|
|
|
|
} else {
|
|
|
|
ahrs.set_compass(&compass);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// GPS Initialization
|
2016-10-28 10:09:31 -03:00
|
|
|
gps.init(nullptr, serial_manager);
|
2013-10-13 04:14:13 -03:00
|
|
|
|
|
|
|
ahrs.init();
|
|
|
|
ahrs.set_fly_forward(false);
|
|
|
|
|
2015-12-26 00:08:38 -04:00
|
|
|
ins.init(scheduler.get_loop_rate_hz());
|
2013-10-13 04:14:13 -03:00
|
|
|
ahrs.reset();
|
|
|
|
|
2016-05-23 22:31:41 -03:00
|
|
|
init_barometer(true);
|
2013-10-13 04:14:13 -03:00
|
|
|
|
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
|
2015-08-27 02:38:00 -03:00
|
|
|
// sanity check location
|
2015-10-27 00:50:29 -03:00
|
|
|
if (fabsf(g.start_latitude) <= 90.0f && fabsf(g.start_longitude) <= 180.0f) {
|
2015-08-27 02:38:00 -03:00
|
|
|
current_loc.lat = g.start_latitude * 1.0e7f;
|
|
|
|
current_loc.lng = g.start_longitude * 1.0e7f;
|
2016-07-05 23:03:33 -03:00
|
|
|
} else {
|
2015-11-18 14:47:45 -04:00
|
|
|
gcs_send_text(MAV_SEVERITY_NOTICE, "Ignoring invalid START_LATITUDE or START_LONGITUDE parameter");
|
2015-08-27 02:38:00 -03:00
|
|
|
}
|
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
|
|
|
|
2015-07-31 19:30:03 -03:00
|
|
|
init_capabilities();
|
|
|
|
|
2015-11-18 14:47:45 -04:00
|
|
|
gcs_send_text(MAV_SEVERITY_INFO,"Ready 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
|
|
|
|
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
|
2015-06-01 02:04:25 -03:00
|
|
|
void Tracker::update_notify()
|
2013-10-13 04:14:13 -03:00
|
|
|
{
|
|
|
|
notify.update();
|
|
|
|
}
|
|
|
|
|
2014-03-02 03:00:37 -04:00
|
|
|
/*
|
|
|
|
fetch HOME from EEPROM
|
|
|
|
*/
|
2015-06-01 02:04:25 -03:00
|
|
|
bool Tracker::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
|
|
|
}
|
|
|
|
|
2015-06-01 02:04:25 -03:00
|
|
|
void Tracker::set_home_eeprom(struct Location temp)
|
2014-03-02 03:00:37 -04:00
|
|
|
{
|
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
|
|
|
|
}
|
|
|
|
|
2015-06-01 02:04:25 -03:00
|
|
|
void Tracker::set_home(struct Location temp)
|
2014-03-02 03:00:37 -04:00
|
|
|
{
|
|
|
|
set_home_eeprom(temp);
|
2014-03-05 01:47:47 -04:00
|
|
|
current_loc = temp;
|
2015-10-02 08:53:28 -03:00
|
|
|
GCS_MAVLINK::send_home_all(temp);
|
2014-03-02 03:00:37 -04:00
|
|
|
}
|
2014-03-03 20:37:15 -04:00
|
|
|
|
2015-06-01 02:04:25 -03:00
|
|
|
void Tracker::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();
|
|
|
|
}
|
|
|
|
|
2015-06-01 02:04:25 -03:00
|
|
|
void Tracker::disarm_servos()
|
2014-03-03 20:37:15 -04:00
|
|
|
{
|
|
|
|
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
|
|
|
|
*/
|
2015-06-01 02:04:25 -03:00
|
|
|
void Tracker::prepare_servos()
|
2014-03-22 05:09:01 -03:00
|
|
|
{
|
2015-11-19 23:04:37 -04:00
|
|
|
start_time_ms = AP_HAL::millis();
|
AntennaTracker: Fix up after refactoring RC_Channel class
Further to refactor of RC_Channel class which included
adding get_xx set_xx methods, change reads and writes to the public members
to calls to get and set functionsss
old public member(int16_t) get function -> int16_t set function (int16_t)
(expression where c is an object of type RC_Channel)
c.radio_in c.get_radio_in() c.set_radio_in(v)
c.control_in c.get_control_in() c.set_control_in(v)
c.servo_out c.get_servo_out() c.set_servo_out(v)
c.pwm_out c.get_pwm_out() // use existing
c.radio_out c.get_radio_out() c.set_radio_out(v)
c.radio_max c.get_radio_max() c.set_radio_max(v)
c.radio_min c.get_radio_min() c.set_radio_min(v)
c.radio_trim c.get_radio_trim() c.set_radio_trim(v);
c.min_max_configured() // return true if min and max are configured
Because data members of RC_Channels are now private and so cannot be written directly
some overloads are provided in the Plane classes to provide the old functionality
new overload Plane::stick_mix_channel(RC_Channel *channel)
which forwards to the previously existing
void stick_mix_channel(RC_Channel *channel, int16_t &servo_out);
new overload Plane::channel_output_mixer(Rc_Channel* , RC_Channel*)const
which forwards to
(uint8_t mixing_type, int16_t & chan1, int16_t & chan2)const;
Rename functions
RC_Channel_aux::set_radio_trim(Aux_servo_function_t function)
to RC_Channel_aux::set_trim_to_radio_in_for(Aux_servo_function_t function)
RC_Channel_aux::set_servo_out(Aux_servo_function_t function, int16_t value)
to RC_Channel_aux::set_servo_out_for(Aux_servo_function_t function, int16_t value)
Rationale:
RC_Channel is a complicated class, which combines
several functionalities dealing with stick inputs
in pwm and logical units, logical and actual actuator
outputs, unit conversion etc, etc
The intent of this PR is to clarify existing use of
the class. At the basic level it should now be possible
to grep all places where private variable is set by
searching for the set_xx function.
(The wider purpose is to provide a more generic and
logically simpler method of output mixing. This is a small step)
2016-05-08 05:51:39 -03:00
|
|
|
channel_yaw.set_radio_out(channel_yaw.get_radio_trim());
|
|
|
|
channel_pitch.set_radio_out(channel_pitch.get_radio_trim());
|
2014-03-22 05:09:01 -03:00
|
|
|
channel_yaw.output();
|
|
|
|
channel_pitch.output();
|
|
|
|
}
|
|
|
|
|
2015-06-01 02:04:25 -03:00
|
|
|
void Tracker::set_mode(enum ControlMode mode)
|
2014-03-06 18:13:53 -04:00
|
|
|
{
|
|
|
|
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-10-06 08:45:07 -03:00
|
|
|
case SERVO_TEST:
|
2014-03-22 05:09:01 -03:00
|
|
|
arm_servos();
|
|
|
|
break;
|
|
|
|
|
|
|
|
case STOP:
|
|
|
|
case INITIALISING:
|
|
|
|
disarm_servos();
|
|
|
|
break;
|
|
|
|
}
|
2016-02-09 04:00:18 -04:00
|
|
|
|
|
|
|
// log mode change
|
|
|
|
DataFlash.Log_Write_Mode(control_mode);
|
2014-03-06 18:13:53 -04:00
|
|
|
}
|
|
|
|
|
2014-09-29 06:47:29 -03:00
|
|
|
/*
|
|
|
|
set_mode() wrapper for MAVLink SET_MODE
|
|
|
|
*/
|
2015-06-01 02:04:25 -03:00
|
|
|
bool Tracker::mavlink_set_mode(uint8_t mode)
|
2014-09-29 06:47:29 -03:00
|
|
|
{
|
|
|
|
switch (mode) {
|
|
|
|
case AUTO:
|
|
|
|
case MANUAL:
|
|
|
|
case SCAN:
|
2014-10-06 08:45:07 -03:00
|
|
|
case SERVO_TEST:
|
2014-09-29 06:47:29 -03:00
|
|
|
case STOP:
|
|
|
|
set_mode((enum ControlMode)mode);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2015-06-01 02:04:25 -03:00
|
|
|
void Tracker::check_usb_mux(void)
|
2014-03-22 05:31:28 -03:00
|
|
|
{
|
|
|
|
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;
|
|
|
|
}
|
2015-12-27 03:05:14 -04:00
|
|
|
|
|
|
|
/*
|
|
|
|
should we log a message type now?
|
|
|
|
*/
|
|
|
|
bool Tracker::should_log(uint32_t mask)
|
|
|
|
{
|
|
|
|
if (!(mask & g.log_bitmask) || in_mavlink_delay) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|