CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutSign UpSign In
Ardupilot

Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place. Commercial Alternative to JupyterHub.

GitHub Repository: Ardupilot/ardupilot
Path: blob/master/libraries/AP_GPS/GPS_Backend.h
Views: 1798
1
/*
2
This program is free software: you can redistribute it and/or modify
3
it under the terms of the GNU General Public License as published by
4
the Free Software Foundation, either version 3 of the License, or
5
(at your option) any later version.
6
7
This program is distributed in the hope that it will be useful,
8
but WITHOUT ANY WARRANTY; without even the implied warranty of
9
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10
GNU General Public License for more details.
11
12
You should have received a copy of the GNU General Public License
13
along with this program. If not, see <http://www.gnu.org/licenses/>.
14
*/
15
16
/*
17
GPS driver backend class
18
*/
19
#pragma once
20
21
#include "AP_GPS_config.h"
22
23
#if AP_GPS_ENABLED
24
25
#include <GCS_MAVLink/GCS_MAVLink.h>
26
#include <GCS_MAVLink/GCS_config.h>
27
#include <AP_RTC/JitterCorrection.h>
28
#include "AP_GPS.h"
29
#include "AP_GPS_config.h"
30
31
#ifndef AP_GPS_DEBUG_LOGGING_ENABLED
32
// enable this to log all bytes from the GPS. Also needs a call to
33
// log_data() in each backend
34
#define AP_GPS_DEBUG_LOGGING_ENABLED 0
35
#endif
36
37
#ifndef AP_GPS_MB_MIN_LAG
38
#define AP_GPS_MB_MIN_LAG 0.05f
39
#endif
40
41
#ifndef AP_GPS_MB_MAX_LAG
42
#define AP_GPS_MB_MAX_LAG 0.25f
43
#endif
44
45
#if AP_GPS_DEBUG_LOGGING_ENABLED
46
#include <AP_HAL/utility/RingBuffer.h>
47
#endif
48
49
class AP_GPS_Backend
50
{
51
public:
52
AP_GPS_Backend(AP_GPS &_gps, AP_GPS::Params &_params, AP_GPS::GPS_State &_state, AP_HAL::UARTDriver *_port);
53
54
// we declare a virtual destructor so that GPS drivers can
55
// override with a custom destructor if need be.
56
virtual ~AP_GPS_Backend(void) {}
57
58
// The read() method is the only one needed in each driver. It
59
// should return true when the backend has successfully received a
60
// valid packet from the GPS.
61
virtual bool read() = 0;
62
63
// Highest status supported by this GPS.
64
// Allows external system to identify type of receiver connected.
65
virtual AP_GPS::GPS_Status highest_supported_status(void) { return AP_GPS::GPS_OK_FIX_3D; }
66
67
virtual bool is_configured(void) const { return true; }
68
69
virtual void inject_data(const uint8_t *data, uint16_t len);
70
71
#if HAL_GCS_ENABLED
72
//MAVLink methods
73
virtual bool supports_mavlink_gps_rtk_message() const { return false; }
74
#if AP_GPS_GPS_RTK_SENDING_ENABLED || AP_GPS_GPS2_RTK_SENDING_ENABLED
75
virtual void send_mavlink_gps_rtk(mavlink_channel_t chan);
76
#endif
77
virtual void handle_msg(const mavlink_message_t &msg) { return ; }
78
#endif
79
80
virtual void broadcast_configuration_failure_reason(void) const { return ; }
81
82
#if HAL_MSP_GPS_ENABLED
83
virtual void handle_msp(const MSP::msp_gps_data_message_t &pkt) { return; }
84
#endif
85
#if HAL_EXTERNAL_AHRS_ENABLED
86
virtual void handle_external(const AP_ExternalAHRS::gps_data_message_t &pkt) { return; }
87
#endif
88
89
// driver specific lag, returns true if the driver is confident in the provided lag
90
virtual bool get_lag(float &lag) const { lag = 0.2f; return true; }
91
92
// driver specific health, returns true if the driver is healthy
93
virtual bool is_healthy(void) const { return true; }
94
// returns true if the GPS is doing any logging it is expected to
95
virtual bool logging_healthy(void) const { return true; }
96
97
virtual const char *name() const = 0;
98
99
void broadcast_gps_type() const;
100
#if HAL_LOGGING_ENABLED
101
virtual void Write_AP_Logger_Log_Startup_messages() const;
102
#endif
103
104
virtual bool prepare_for_arming(void) { return true; }
105
106
// optional support for retrieving RTCMv3 data from a moving baseline base
107
virtual bool get_RTCMV3(const uint8_t *&bytes, uint16_t &len) { return false; }
108
virtual void clear_RTCMV3(void) {};
109
110
virtual bool get_error_codes(uint32_t &error_codes) const { return false; }
111
112
// return iTOW of last message, or zero if not supported
113
uint32_t get_last_itow_ms(void) const;
114
115
// check if an option is set
116
bool option_set(const AP_GPS::DriverOptions option) const {
117
return gps.option_set(option);
118
}
119
120
protected:
121
AP_HAL::UARTDriver *port; ///< UART we are attached to
122
AP_GPS &gps; ///< access to frontend (for parameters)
123
AP_GPS::GPS_State &state; ///< public state for this instance
124
AP_GPS::Params &params;
125
126
uint64_t _last_pps_time_us;
127
JitterCorrection jitter_correction;
128
uint32_t _last_itow_ms;
129
bool _have_itow;
130
131
/*
132
fill in 3D velocity from 2D components
133
*/
134
void fill_3d_velocity(void);
135
136
/*
137
fill ground course and speed from velocity
138
*/
139
void velocity_to_speed_course(AP_GPS::GPS_State &s);
140
141
/*
142
fill in time_week_ms and time_week from BCD date and time components
143
assumes MTK19 millisecond form of bcd_time
144
*/
145
void make_gps_time(uint32_t bcd_date, uint32_t bcd_milliseconds);
146
147
void _detection_message(char *buffer, uint8_t buflen) const;
148
149
bool should_log() const;
150
151
/*
152
set a timestamp based on arrival time on uart at current byte,
153
assuming the message started nbytes ago
154
*/
155
void set_uart_timestamp(uint16_t nbytes);
156
157
void check_new_itow(uint32_t itow, uint32_t msg_length);
158
159
#if GPS_MOVING_BASELINE
160
bool calculate_moving_base_yaw(const float reported_heading_deg, const float reported_distance, const float reported_D);
161
bool calculate_moving_base_yaw(AP_GPS::GPS_State &interim_state, const float reported_heading_deg, const float reported_distance, const float reported_D);
162
#endif //GPS_MOVING_BASELINE
163
164
// get GPS type, for subtype config
165
AP_GPS::GPS_Type get_type() const {
166
return gps.get_type(state.instance);
167
}
168
169
virtual void set_pps_desired_freq(uint8_t freq) {}
170
171
#if AP_GPS_DEBUG_LOGGING_ENABLED
172
// log some data for debugging
173
void log_data(const uint8_t *data, uint16_t length);
174
#endif
175
176
// set alt in location, honouring GPS driver option for ellipsoid height
177
void set_alt_amsl_cm(AP_GPS::GPS_State &_state, int32_t alt_amsl_cm);
178
179
private:
180
// itow from previous message
181
uint64_t _pseudo_itow;
182
int32_t _pseudo_itow_delta_ms;
183
uint32_t _last_ms;
184
uint32_t _rate_ms;
185
uint32_t _last_rate_ms;
186
uint16_t _rate_counter;
187
188
#if AP_GPS_DEBUG_LOGGING_ENABLED
189
// support raw GPS logging
190
static struct loginfo {
191
int fd = -1;
192
ByteBuffer buf{16000};
193
} logging[2];
194
static bool log_thread_created;
195
static void logging_loop(void);
196
void logging_start(void);
197
#endif
198
199
};
200
201
#endif // AP_GPS_ENABLED
202
203