2015-06-01 02:04:25 -03:00
/*
Lead developers : Matthew Ridley and Andrew Tridgell
2016-05-05 19:10:08 -03:00
2016-04-23 17:52:26 -03:00
Please contribute your ideas ! See http : //dev.ardupilot.org for details
2015-06-01 02:04:25 -03:00
This program is free software : you can redistribute it and / or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation , either version 3 of the License , or
( at your option ) any later version .
This program is distributed in the hope that it will be useful ,
but WITHOUT ANY WARRANTY ; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE . See the
GNU General Public License for more details .
You should have received a copy of the GNU General Public License
along with this program . If not , see < http : //www.gnu.org/licenses/>.
*/
2016-05-05 19:10:08 -03:00
# pragma once
2015-06-01 02:04:25 -03:00
////////////////////////////////////////////////////////////////////////////////
// Header includes
////////////////////////////////////////////////////////////////////////////////
2016-03-31 18:43:36 -03:00
# include <cmath>
2015-06-01 02:04:25 -03:00
# include <stdarg.h>
# include <stdio.h>
2015-08-11 03:28:40 -03:00
# include <AP_Common/AP_Common.h>
# include <AP_HAL/AP_HAL.h>
# include <AP_Param/AP_Param.h>
# include <StorageManager/StorageManager.h>
# include <AP_GPS/AP_GPS.h> // ArduPilot GPS library
# include <AP_Baro/AP_Baro.h> // ArduPilot barometer library
# include <AP_Compass/AP_Compass.h> // ArduPilot Mega Magnetometer Library
# include <AP_Math/AP_Math.h> // ArduPilot Mega Vector/Matrix math Library
# include <AP_InertialSensor/AP_InertialSensor.h> // Inertial Sensor Library
2015-10-21 18:27:35 -03:00
# include <AP_AccelCal/AP_AccelCal.h> // interface and maths for accelerometer calibration
2015-08-11 03:28:40 -03:00
# include <AP_AHRS/AP_AHRS.h> // ArduPilot Mega DCM Library
# include <Filter/Filter.h> // Filter library
# include <AP_SerialManager/AP_SerialManager.h> // Serial manager library
# include <AP_Declination/AP_Declination.h> // ArduPilot Mega Declination Helper Library
2019-01-18 00:23:42 -04:00
# include <AP_Logger/AP_Logger.h>
2016-06-16 00:07:07 -03:00
# include <AC_PID/AC_PID.h>
2015-08-11 03:28:40 -03:00
# include <AP_Scheduler/AP_Scheduler.h> // main loop scheduler
2015-09-22 22:51:47 -03:00
# include <AP_NavEKF2/AP_NavEKF2.h>
2016-07-14 02:08:43 -03:00
# include <AP_NavEKF3/AP_NavEKF3.h>
2015-08-11 03:28:40 -03:00
# include <AP_Vehicle/AP_Vehicle.h>
# include <AP_Mission/AP_Mission.h>
# include <AP_Terrain/AP_Terrain.h>
# include <AP_Rally/AP_Rally.h>
# include <AP_Notify/AP_Notify.h> // Notify library
# include <AP_BattMonitor/AP_BattMonitor.h> // Battery monitor library
# include <AP_Airspeed/AP_Airspeed.h>
# include <AP_BoardConfig/AP_BoardConfig.h>
2017-05-06 06:11:29 -03:00
# include <AP_BoardConfig/AP_BoardConfig_CAN.h>
2015-08-11 03:28:40 -03:00
# include <AP_OpticalFlow/AP_OpticalFlow.h>
# include <AP_RangeFinder/AP_RangeFinder.h>
2016-11-23 03:09:08 -04:00
# include <AP_Beacon/AP_Beacon.h>
2019-04-04 08:06:28 -03:00
# include <AP_Common/AP_FWVersion.h>
2015-06-01 02:04:25 -03:00
// Configuration
# include "config.h"
# include "defines.h"
2018-04-26 23:27:00 -03:00
# include "RC_Channel.h"
2015-06-01 02:04:25 -03:00
# include "Parameters.h"
2016-05-27 10:14:08 -03:00
# include "GCS_Mavlink.h"
2017-02-13 21:30:32 -04:00
# include "GCS_Tracker.h"
2015-08-11 03:28:40 -03:00
2019-03-01 02:40:18 -04:00
# ifdef ENABLE_SCRIPTING
# include <AP_Scripting/AP_Scripting.h>
# endif
2015-10-22 12:36:14 -03:00
# if CONFIG_HAL_BOARD == HAL_BOARD_SITL
# include <SITL/SITL.h>
# endif
2015-10-19 15:32:37 -03:00
class Tracker : public AP_HAL : : HAL : : Callbacks {
2015-06-01 02:04:25 -03:00
public :
2016-05-27 10:14:08 -03:00
friend class GCS_MAVLINK_Tracker ;
2017-02-13 21:30:32 -04:00
friend class GCS_Tracker ;
2015-06-01 02:04:25 -03:00
friend class Parameters ;
Tracker ( void ) ;
2015-10-19 15:32:37 -03:00
2017-09-12 15:25:20 -03:00
static const AP_FWVersion fwver ;
2017-08-19 06:53:03 -03:00
2015-10-19 15:32:37 -03:00
// HAL::Callbacks implementation.
void setup ( ) override ;
void loop ( ) override ;
2015-06-01 02:04:25 -03:00
private :
Parameters g ;
// main loop scheduler
2017-12-12 21:06:15 -04:00
AP_Scheduler scheduler ;
2017-08-23 04:43:34 -03:00
2015-06-01 02:04:25 -03:00
// notification object for LEDs, buzzers etc
2017-12-12 21:06:15 -04:00
AP_Notify notify ;
2015-06-01 02:04:25 -03:00
2015-06-08 01:14:10 -03:00
uint32_t start_time_ms = 0 ;
2015-06-01 02:04:25 -03:00
2019-01-18 00:23:42 -04:00
AP_Logger logger ;
2015-12-27 03:05:14 -04:00
2017-12-12 21:06:15 -04:00
AP_GPS gps ;
2015-06-01 02:04:25 -03:00
2017-12-12 21:06:15 -04:00
AP_Baro barometer ;
2015-06-01 02:04:25 -03:00
2017-12-12 21:06:15 -04:00
Compass compass ;
2015-06-01 02:04:25 -03:00
2017-12-12 21:06:15 -04:00
AP_InertialSensor ins ;
2015-06-01 02:04:25 -03:00
2019-07-09 03:15:19 -03:00
RangeFinder rng ;
2015-06-01 02:04:25 -03:00
// Inertial Navigation EKF
# if AP_AHRS_NAVEKF_AVAILABLE
2018-03-05 17:05:20 -04:00
NavEKF2 EKF2 { & ahrs , rng } ;
NavEKF3 EKF3 { & ahrs , rng } ;
2018-03-10 06:13:53 -04:00
AP_AHRS_NavEKF ahrs { EKF2 , EKF3 } ;
2015-06-01 02:04:25 -03:00
# else
2018-03-10 06:13:53 -04:00
AP_AHRS_DCM ahrs ;
2015-06-01 02:04:25 -03:00
# endif
# if CONFIG_HAL_BOARD == HAL_BOARD_SITL
2015-10-22 10:04:42 -03:00
SITL : : SITL sitl ;
2015-06-01 02:04:25 -03:00
# endif
/**
antenna control channels
*/
2018-04-26 23:27:00 -03:00
RC_Channels_Tracker rc_channels ;
2017-01-06 22:51:56 -04:00
SRV_Channels servo_channels ;
2015-06-01 02:04:25 -03:00
2016-07-27 23:19:54 -03:00
LowPassFilterFloat yaw_servo_out_filt ;
LowPassFilterFloat pitch_servo_out_filt ;
bool yaw_servo_out_filt_init = false ;
bool pitch_servo_out_filt_init = false ;
2017-12-12 21:06:15 -04:00
AP_SerialManager serial_manager ;
2017-02-13 21:30:32 -04:00
GCS_Tracker _gcs ; // avoid using this; use gcs()
GCS_Tracker & gcs ( ) { return _gcs ; }
2015-06-01 02:04:25 -03:00
2017-12-12 21:06:15 -04:00
AP_BoardConfig BoardConfig ;
2015-06-01 02:04:25 -03:00
2017-05-06 06:11:29 -03:00
# if HAL_WITH_UAVCAN
// board specific config for CAN bus
2017-12-12 21:06:15 -04:00
AP_BoardConfig_CAN BoardConfig_CAN ;
2017-05-06 06:11:29 -03:00
# endif
2017-11-26 21:51:11 -04:00
// Battery Sensors
2018-03-01 23:38:30 -04:00
AP_BattMonitor battery { MASK_LOG_CURRENT ,
FUNCTOR_BIND_MEMBER ( & Tracker : : handle_battery_failsafe , void , const char * , const int8_t ) ,
nullptr } ;
2017-11-26 21:51:11 -04:00
2015-06-01 02:04:25 -03:00
struct Location current_loc ;
enum ControlMode control_mode = INITIALISING ;
2019-03-01 02:40:18 -04:00
# ifdef ENABLE_SCRIPTING
AP_Scripting scripting ;
# endif
2015-06-01 02:04:25 -03:00
// Vehicle state
struct {
bool location_valid ; // true if we have a valid location for the vehicle
Location location ; // lat, long in degrees * 10^7; alt in meters * 100
Location location_estimate ; // lat, long in degrees * 10^7; alt in meters * 100
2016-06-13 05:50:41 -03:00
uint32_t last_update_us ; // last position update in microseconds
2015-06-01 02:04:25 -03:00
uint32_t last_update_ms ; // last position update in milliseconds
2016-06-13 00:52:03 -03:00
Vector3f vel ; // the vehicle's velocity in m/s
2016-07-06 04:49:52 -03:00
int32_t relative_alt ; // the vehicle's relative altitude in meters * 100
2015-06-01 02:04:25 -03:00
} vehicle ;
// Navigation controller state
struct {
float bearing ; // bearing to vehicle in centi-degrees
float distance ; // distance to vehicle in meters
float pitch ; // pitch to vehicle in degrees (positive means vehicle is above tracker, negative means below)
2016-07-13 23:40:49 -03:00
float angle_error_pitch ; // angle error between target and current pitch in centi-degrees
float angle_error_yaw ; // angle error between target and current yaw in centi-degrees
2016-05-24 08:53:57 -03:00
float alt_difference_baro ; // altitude difference between tracker and vehicle in meters according to the barometer. positive value means vehicle is above tracker
float alt_difference_gps ; // altitude difference between tracker and vehicle in meters according to the gps. positive value means vehicle is above tracker
2015-06-01 02:04:25 -03:00
float altitude_offset ; // offset in meters which is added to tracker altitude to align altitude measurements with vehicle's barometer
bool manual_control_yaw : 1 ; // true if tracker yaw is under manual control
bool manual_control_pitch : 1 ; // true if tracker pitch is manually controlled
bool need_altitude_calibration : 1 ; // true if tracker altitude has not been determined (true after startup)
bool scan_reverse_pitch : 1 ; // controls direction of pitch movement in SCAN mode
bool scan_reverse_yaw : 1 ; // controls direction of yaw movement in SCAN mode
2016-07-13 23:40:49 -03:00
} nav_status = { 0.0f , 0.0f , 0.0f , 0.0f , 0.0f , 0.0f , 0.0f , 0.0f , false , false , true , false , false } ;
2015-06-01 02:04:25 -03:00
// setup the var_info table
AP_Param param_loader { var_info } ;
uint8_t one_second_counter = 0 ;
bool target_set = false ;
2019-02-28 17:38:28 -04:00
bool stationary = true ; // are we using the start lat and log?
2015-06-01 02:04:25 -03:00
static const AP_Scheduler : : Task scheduler_tasks [ ] ;
static const AP_Param : : Info var_info [ ] ;
2015-12-27 03:05:14 -04:00
static const struct LogStructure log_structure [ ] ;
2015-06-01 02:04:25 -03:00
2019-03-10 07:37:25 -03:00
// true if the compass's initial location has been set
2019-02-08 15:22:08 -04:00
bool compass_init_location ;
2018-09-05 08:41:18 -03:00
// AntennaTracker.cpp
2015-06-01 02:04:25 -03:00
void one_second_loop ( ) ;
2015-12-27 03:05:14 -04:00
void ten_hz_logging_loop ( ) ;
2018-09-05 08:41:18 -03:00
// control_auto.cpp
2015-06-01 02:04:25 -03:00
void update_auto ( void ) ;
2016-07-27 23:31:48 -03:00
void calc_angle_error ( float pitch , float yaw , bool direction_reversed ) ;
void convert_ef_to_bf ( float pitch , float yaw , float & bf_pitch , float & bf_yaw ) ;
bool convert_bf_to_ef ( float pitch , float yaw , float & ef_pitch , float & ef_yaw ) ;
bool get_ef_yaw_direction ( ) ;
2018-09-05 08:41:18 -03:00
// control_manual.cpp
2015-06-01 02:04:25 -03:00
void update_manual ( void ) ;
2018-09-05 08:41:18 -03:00
// control_scan.cpp
2015-06-01 02:04:25 -03:00
void update_scan ( void ) ;
2018-09-05 08:41:18 -03:00
// control_servo_test.cpp
2015-06-01 02:04:25 -03:00
bool servo_test_set_servo ( uint8_t servo_num , uint16_t pwm ) ;
2018-09-05 08:41:18 -03:00
// GCS_Mavlink.cpp
void send_nav_controller_output ( mavlink_channel_t chan ) ;
// Log.cpp
void Log_Write_Attitude ( ) ;
void Log_Write_Vehicle_Baro ( float pressure , float altitude ) ;
void Log_Write_Vehicle_Pos ( int32_t lat , int32_t lng , int32_t alt , const Vector3f & vel ) ;
void Log_Write_Vehicle_Startup_Messages ( ) ;
void log_init ( void ) ;
// Parameters.cpp
void load_parameters ( void ) ;
// radio.cpp
2015-06-01 02:04:25 -03:00
void read_radio ( ) ;
2018-09-05 08:41:18 -03:00
// sensors.cpp
2015-06-01 02:04:25 -03:00
void update_ahrs ( ) ;
2019-02-08 15:22:08 -04:00
void compass_save ( ) ;
void init_compass_location ( ) ;
2015-06-01 02:04:25 -03:00
void update_compass ( void ) ;
2015-10-21 18:27:35 -03:00
void accel_cal_update ( void ) ;
2015-06-01 02:04:25 -03:00
void update_GPS ( void ) ;
2018-09-05 08:41:18 -03:00
void handle_battery_failsafe ( const char * type_str , const int8_t action ) ;
// servos.cpp
2015-06-01 02:04:25 -03:00
void init_servos ( ) ;
void update_pitch_servo ( float pitch ) ;
2016-07-13 23:40:49 -03:00
void update_pitch_position_servo ( void ) ;
2015-06-01 02:04:25 -03:00
void update_pitch_onoff_servo ( float pitch ) ;
2018-09-05 08:41:18 -03:00
void update_pitch_cr_servo ( float pitch ) ;
2015-06-01 02:04:25 -03:00
void update_yaw_servo ( float yaw ) ;
2016-07-13 23:40:49 -03:00
void update_yaw_position_servo ( void ) ;
2015-06-01 02:04:25 -03:00
void update_yaw_onoff_servo ( float yaw ) ;
2018-09-05 08:41:18 -03:00
void update_yaw_cr_servo ( float yaw ) ;
// system.cpp
2015-06-01 02:04:25 -03:00
void init_tracker ( ) ;
bool get_home_eeprom ( struct Location & loc ) ;
2019-02-20 21:43:55 -04:00
bool set_home_eeprom ( const Location & temp ) WARN_IF_UNUSED ;
bool set_home ( const Location & temp ) WARN_IF_UNUSED ;
2015-06-01 02:04:25 -03:00
void arm_servos ( ) ;
void disarm_servos ( ) ;
void prepare_servos ( ) ;
2018-02-21 19:02:00 -04:00
void set_mode ( enum ControlMode mode , mode_reason_t reason ) ;
2018-09-05 08:41:18 -03:00
bool should_log ( uint32_t mask ) ;
2019-03-10 07:37:25 -03:00
bool start_command_callback ( const AP_Mission : : Mission_Command & cmd ) { return false ; }
void exit_mission_callback ( ) { return ; }
bool verify_command_callback ( const AP_Mission : : Mission_Command & cmd ) { return false ; }
2018-09-05 08:41:18 -03:00
// tracking.cpp
2015-06-01 02:04:25 -03:00
void update_vehicle_pos_estimate ( ) ;
void update_tracker_position ( ) ;
void update_bearing_and_distance ( ) ;
void update_tracking ( void ) ;
void tracking_update_position ( const mavlink_global_position_int_t & msg ) ;
void tracking_update_pressure ( const mavlink_scaled_pressure_t & msg ) ;
void tracking_manual_control ( const mavlink_manual_control_t & msg ) ;
void update_armed_disarmed ( ) ;
2019-03-10 07:37:25 -03:00
// Mission library
AP_Mission mission {
FUNCTOR_BIND_MEMBER ( & Tracker : : start_command_callback , bool , const AP_Mission : : Mission_Command & ) ,
FUNCTOR_BIND_MEMBER ( & Tracker : : verify_command_callback , bool , const AP_Mission : : Mission_Command & ) ,
FUNCTOR_BIND_MEMBER ( & Tracker : : exit_mission_callback , void ) } ;
2015-06-01 02:04:25 -03:00
public :
void mavlink_delay_cb ( ) ;
} ;
extern const AP_HAL : : HAL & hal ;
extern Tracker tracker ;