2015-06-01 02:04:25 -03:00
|
|
|
#include "Tracker.h"
|
|
|
|
|
2014-08-13 01:43:56 -03:00
|
|
|
// mission storage
|
|
|
|
static const StorageAccess wp_storage(StorageManager::StorageMission);
|
|
|
|
|
2020-01-16 06:25:40 -04:00
|
|
|
void Tracker::init_ardupilot()
|
2013-10-13 04:14:13 -03:00
|
|
|
{
|
2017-01-21 02:17:26 -04:00
|
|
|
// initialise notify
|
2018-07-25 22:11:00 -03:00
|
|
|
notify.init();
|
2017-01-21 02:17:26 -04:00
|
|
|
AP_Notify::flags.pre_arm_check = true;
|
|
|
|
AP_Notify::flags.pre_arm_gps_check = true;
|
|
|
|
|
2019-04-26 14:56:25 -03:00
|
|
|
// initialise battery
|
|
|
|
battery.init();
|
|
|
|
|
2013-10-13 04:14:13 -03:00
|
|
|
// init baro before we start the GCS, so that the CLI baro test works
|
2018-04-11 09:55:31 -03:00
|
|
|
barometer.set_log_baro_bit(MASK_LOG_IMU);
|
2013-10-13 04:14:13 -03:00
|
|
|
barometer.init();
|
|
|
|
|
2016-05-16 00:33:43 -03:00
|
|
|
// setup telem slots with serial ports
|
2019-06-19 01:24:31 -03:00
|
|
|
gcs().setup_uarts();
|
2022-03-15 20:24:27 -03:00
|
|
|
// update_send so that if the first packet we receive happens to
|
|
|
|
// be an arm message we don't trigger an internal error when we
|
|
|
|
// try to initialise stream rates in the main loop.
|
|
|
|
gcs().update_send();
|
2015-05-02 09:38:58 -03:00
|
|
|
|
2024-01-10 00:40:09 -04:00
|
|
|
#if HAL_LOGGING_ENABLED
|
2015-12-27 03:05:14 -04:00
|
|
|
log_init();
|
|
|
|
#endif
|
|
|
|
|
2019-02-08 15:22:08 -04:00
|
|
|
// initialise compass
|
2019-03-25 09:42:51 -03:00
|
|
|
AP::compass().set_log_bit(MASK_LOG_COMPASS);
|
2019-03-24 00:26:06 -03:00
|
|
|
AP::compass().init();
|
2013-10-13 04:14:13 -03:00
|
|
|
|
|
|
|
// GPS Initialization
|
2017-06-28 22:19:50 -03:00
|
|
|
gps.set_log_gps_bit(MASK_LOG_GPS);
|
2017-06-27 05:13:05 -03:00
|
|
|
gps.init(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();
|
|
|
|
|
2018-03-18 01:15:13 -03:00
|
|
|
barometer.calibrate();
|
2013-10-13 04:14:13 -03:00
|
|
|
|
2024-01-10 00:40:09 -04:00
|
|
|
#if HAL_LOGGING_ENABLED
|
2019-01-18 00:23:42 -04:00
|
|
|
// initialise AP_Logger library
|
2019-01-18 00:24:08 -04:00
|
|
|
logger.setVehicle_Startup_Writer(FUNCTOR_BIND(&tracker, &Tracker::Log_Write_Vehicle_Startup_Messages, void));
|
2024-01-10 00:40:09 -04:00
|
|
|
#endif
|
2017-05-01 03:22:35 -03:00
|
|
|
|
2019-12-03 21:44:08 -04:00
|
|
|
// initialise rc channels including setting mode
|
2021-09-13 21:05:42 -03:00
|
|
|
rc().convert_options(RC_Channel::AUX_FUNC::ARMDISARM_UNUSED, RC_Channel::AUX_FUNC::ARMDISARM);
|
2021-09-20 22:46:58 -03:00
|
|
|
rc().init();
|
2019-12-03 21:44:08 -04: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 {
|
2017-07-08 21:28:33 -03: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
|
|
|
|
|
|
|
hal.scheduler->delay(1000); // Why????
|
2014-03-06 18:13:53 -04:00
|
|
|
|
2019-09-13 12:41:55 -03:00
|
|
|
Mode *newmode = mode_from_mode_num((Mode::Number)g.initial_mode.get());
|
|
|
|
if (newmode == nullptr) {
|
|
|
|
newmode = &mode_manual;
|
2019-03-10 07:54:17 -03:00
|
|
|
}
|
2019-09-13 12:41:55 -03:00
|
|
|
set_mode(*newmode, ModeReason::STARTUP);
|
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-03-02 03:00:37 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
fetch HOME from EEPROM
|
|
|
|
*/
|
2023-02-02 18:58:39 -04:00
|
|
|
bool Tracker::get_home_eeprom(Location &loc) const
|
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
|
2019-01-01 23:24:53 -04:00
|
|
|
loc = {
|
|
|
|
int32_t(wp_storage.read_uint32(5)),
|
|
|
|
int32_t(wp_storage.read_uint32(9)),
|
|
|
|
int32_t(wp_storage.read_uint32(1)),
|
2019-03-14 22:44:11 -03:00
|
|
|
Location::AltFrame::ABSOLUTE
|
2019-01-01 23:24:53 -04:00
|
|
|
};
|
2014-03-22 05:09:01 -03:00
|
|
|
|
|
|
|
return true;
|
2014-03-02 03:00:37 -04:00
|
|
|
}
|
|
|
|
|
2019-02-20 21:43:55 -04:00
|
|
|
bool Tracker::set_home_eeprom(const Location &temp)
|
2014-03-02 03:00:37 -04:00
|
|
|
{
|
2019-01-01 23:24:53 -04:00
|
|
|
wp_storage.write_byte(0, 0);
|
2014-08-13 01:43:56 -03:00
|
|
|
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
|
2019-02-20 21:43:55 -04:00
|
|
|
return true;
|
2014-03-02 03:00:37 -04:00
|
|
|
}
|
|
|
|
|
2019-02-20 21:43:55 -04:00
|
|
|
bool Tracker::set_home(const Location &temp)
|
2014-03-02 03:00:37 -04:00
|
|
|
{
|
2018-05-16 01:49:01 -03:00
|
|
|
// check EKF origin has been set
|
2017-09-18 02:38:26 -03:00
|
|
|
Location ekf_origin;
|
|
|
|
if (ahrs.get_origin(ekf_origin)) {
|
2018-05-29 21:49:29 -03:00
|
|
|
if (!ahrs.set_home(temp)) {
|
2019-02-20 21:43:55 -04:00
|
|
|
return false;
|
2018-05-29 21:49:29 -03:00
|
|
|
}
|
2017-09-18 02:38:26 -03:00
|
|
|
}
|
2019-02-20 21:43:55 -04:00
|
|
|
|
|
|
|
if (!set_home_eeprom(temp)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
current_loc = temp;
|
|
|
|
|
|
|
|
return true;
|
2017-09-18 02:38:26 -03:00
|
|
|
}
|
|
|
|
|
2015-06-01 02:04:25 -03:00
|
|
|
void Tracker::arm_servos()
|
2017-01-06 22:51:56 -04:00
|
|
|
{
|
|
|
|
hal.util->set_soft_armed(true);
|
2024-01-10 00:40:09 -04:00
|
|
|
#if HAL_LOGGING_ENABLED
|
2019-01-18 00:23:42 -04:00
|
|
|
logger.set_vehicle_armed(true);
|
2024-01-10 00:40:09 -04:00
|
|
|
#endif
|
2014-03-03 20:37:15 -04:00
|
|
|
}
|
|
|
|
|
2015-06-01 02:04:25 -03:00
|
|
|
void Tracker::disarm_servos()
|
2014-03-03 20:37:15 -04:00
|
|
|
{
|
2017-01-06 22:51:56 -04:00
|
|
|
hal.util->set_soft_armed(false);
|
2024-01-10 00:40:09 -04:00
|
|
|
#if HAL_LOGGING_ENABLED
|
2019-01-18 00:23:42 -04:00
|
|
|
logger.set_vehicle_armed(false);
|
2024-01-10 00:40:09 -04:00
|
|
|
#endif
|
2014-03-03 20:37:15 -04:00
|
|
|
}
|
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();
|
2019-11-24 21:52:19 -04:00
|
|
|
SRV_Channels::set_output_limit(SRV_Channel::k_tracker_yaw, SRV_Channel::Limit::TRIM);
|
|
|
|
SRV_Channels::set_output_limit(SRV_Channel::k_tracker_pitch, SRV_Channel::Limit::TRIM);
|
2017-01-06 22:51:56 -04:00
|
|
|
SRV_Channels::calc_pwm();
|
|
|
|
SRV_Channels::output_ch_all();
|
2014-03-22 05:09:01 -03:00
|
|
|
}
|
|
|
|
|
2019-09-13 12:41:55 -03:00
|
|
|
void Tracker::set_mode(Mode &newmode, const ModeReason reason)
|
2014-03-06 18:13:53 -04:00
|
|
|
{
|
2021-02-02 19:48:14 -04:00
|
|
|
control_mode_reason = reason;
|
|
|
|
|
2019-09-13 12:41:55 -03:00
|
|
|
if (mode == &newmode) {
|
2014-03-06 18:13:53 -04:00
|
|
|
// don't switch modes if we are already in the correct mode.
|
|
|
|
return;
|
|
|
|
}
|
2019-09-13 12:41:55 -03:00
|
|
|
mode = &newmode;
|
2014-03-22 05:09:01 -03:00
|
|
|
|
2019-09-13 12:41:55 -03:00
|
|
|
if (mode->requires_armed_servos()) {
|
2014-03-22 05:09:01 -03:00
|
|
|
arm_servos();
|
2019-09-13 12:41:55 -03:00
|
|
|
} else {
|
2014-03-22 05:09:01 -03:00
|
|
|
disarm_servos();
|
|
|
|
}
|
2016-02-09 04:00:18 -04:00
|
|
|
|
2024-01-10 00:40:09 -04:00
|
|
|
#if HAL_LOGGING_ENABLED
|
2016-02-09 04:00:18 -04:00
|
|
|
// log mode change
|
2019-09-13 12:41:55 -03:00
|
|
|
logger.Write_Mode((uint8_t)mode->number(), reason);
|
2024-01-10 00:40:09 -04:00
|
|
|
#endif
|
2019-09-13 12:41:55 -03:00
|
|
|
gcs().send_message(MSG_HEARTBEAT);
|
2019-03-10 07:49:28 -03:00
|
|
|
|
|
|
|
nav_status.bearing = ahrs.yaw_sensor * 0.01f;
|
2014-03-06 18:13:53 -04:00
|
|
|
}
|
|
|
|
|
2019-10-17 00:49:06 -03:00
|
|
|
bool Tracker::set_mode(const uint8_t new_mode, const ModeReason reason)
|
|
|
|
{
|
2019-09-13 12:41:55 -03:00
|
|
|
Mode *fred = nullptr;
|
|
|
|
switch ((Mode::Number)new_mode) {
|
|
|
|
case Mode::Number::INITIALISING:
|
|
|
|
return false;
|
|
|
|
case Mode::Number::AUTO:
|
|
|
|
fred = &mode_auto;
|
|
|
|
break;
|
|
|
|
case Mode::Number::MANUAL:
|
|
|
|
fred = &mode_manual;
|
|
|
|
break;
|
|
|
|
case Mode::Number::SCAN:
|
|
|
|
fred = &mode_scan;
|
|
|
|
break;
|
|
|
|
case Mode::Number::SERVOTEST:
|
|
|
|
fred = &mode_servotest;
|
|
|
|
break;
|
|
|
|
case Mode::Number::STOP:
|
|
|
|
fred = &mode_stop;
|
|
|
|
break;
|
2019-09-25 06:55:59 -03:00
|
|
|
case Mode::Number::GUIDED:
|
|
|
|
fred = &mode_guided;
|
|
|
|
break;
|
2019-10-17 00:49:06 -03:00
|
|
|
}
|
2019-09-13 12:41:55 -03:00
|
|
|
if (fred == nullptr) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
set_mode(*fred, reason);
|
|
|
|
return true;
|
2019-10-17 00:49:06 -03:00
|
|
|
}
|
|
|
|
|
2024-01-10 00:40:09 -04:00
|
|
|
#if HAL_LOGGING_ENABLED
|
2015-12-27 03:05:14 -04:00
|
|
|
/*
|
|
|
|
should we log a message type now?
|
|
|
|
*/
|
|
|
|
bool Tracker::should_log(uint32_t mask)
|
|
|
|
{
|
2019-01-18 00:23:42 -04:00
|
|
|
if (!logger.should_log(mask)) {
|
2017-06-14 20:27:25 -03:00
|
|
|
return false;
|
|
|
|
}
|
2015-12-27 03:05:14 -04:00
|
|
|
return true;
|
|
|
|
}
|
2024-01-10 00:40:09 -04:00
|
|
|
#endif
|
2019-11-03 20:42:37 -04:00
|
|
|
|
|
|
|
#include <AP_AdvancedFailsafe/AP_AdvancedFailsafe.h>
|
2019-11-03 20:44:42 -04:00
|
|
|
#include <AP_Avoidance/AP_Avoidance.h>
|
|
|
|
#include <AP_ADSB/AP_ADSB.h>
|
2019-11-03 20:42:37 -04:00
|
|
|
|
2023-02-19 18:48:39 -04:00
|
|
|
#if AP_ADVANCEDFAILSAFE_ENABLED
|
2019-11-03 20:42:37 -04:00
|
|
|
// dummy method to avoid linking AFS
|
|
|
|
bool AP_AdvancedFailsafe::gcs_terminate(bool should_terminate, const char *reason) {return false;}
|
|
|
|
AP_AdvancedFailsafe *AP::advancedfailsafe() { return nullptr; }
|
2023-02-19 18:48:39 -04:00
|
|
|
#endif // AP_ADVANCEDFAILSAFE_ENABLED
|
2020-09-19 05:39:30 -03:00
|
|
|
#if HAL_ADSB_ENABLED
|
2019-11-03 20:44:42 -04:00
|
|
|
// dummy method to avoid linking AP_Avoidance
|
|
|
|
AP_Avoidance *AP::ap_avoidance() { return nullptr; }
|
2020-09-19 05:39:30 -03:00
|
|
|
#endif
|