Hummingbird Flight Software
Flight software for the Hummingbird FCU quadcopter flight controller. Designed to run on the Teensy 4.1. Developed with VSCode+PlatformIO.
gnss_computer.h
Go to the documentation of this file.
1 
2 
3 
11 #pragma once
12 
13 
14 
15 #include <Arduino.h>
16 #include <Wire.h>
17 #include "hummingbird_config.h"
18 #include "maths/vectors.h"
19 #include "TinyGPS++.h"
20 #include "constants.h"
22 
23 
24 #if defined(DEBUG) && defined(DEBUG_PORT)
25  /* Print GNSS debug messages to the debug serial port */
26  #define GNSS_DEBUG
27 #endif
28 
29 
30 
31 
36 constexpr uint32_t GNSS_POS_LOCK_TIMEOUT = 120000;
37 
38 /* Baud rate that the unconfigured GPS has when powered up */
39 constexpr int32_t GNSS_DEFAULT_BAUD = 9600;
40 
41 /* Minimum number of GNSS satellites to ensure a valid fix */
42 constexpr uint32_t GNSS_MIN_SATS = 5;
43 
44 /* I2C address of the UBLOX GPS module */
45 constexpr uint8_t GNSS_I2C_ADDR = 0x42;
46 
47 /* I2C buffer size of the device. 32 bytes for Arduino Nano and Teensy 4.1 */
48 constexpr uint16_t GNSS_I2C_BUFFSIZE = 32;
49 
50 
51 /* Possible baud rates for the GNSS sensor */
52 // typedef enum
53 // {
54 // GNSS_BAUD_9600, // 9600 serial baud rate (default when NEO-M8N powers up)
55 // GNSS_BAUD_38400, // 38.4k serial baud rate
56 // GNSS_BAUD_115200, // 115.2K serial baud rate
57 // GNSS_BAUD_230400 // 230.4k serial baud rate
58 // } GNSSBaudRates_t;
59 
60 /* Possible GNSS networks to connect to. */
61 typedef enum
62 {
63  GNSS_NET_GPS, // Connect to GPS network, max. 10Hz on NEO-M8N
64  GNSS_NET_GLONASS, // Connect to GLONASS network, max. 10Hz on NEO-M8N
65  GNSS_NET_GPS_GLONASS // Connect to both GPS and GLONASS, max. 5Hz on NEO-M8N
67 
68 /* Various dynamic models for the GPS receiver's sensor fusion algorithms */
69 typedef enum
70 {
71  GNSS_DYNAMICS_PORTABLE, // Portable dynamic model, 12000m alt, 310m/s hvel, 50m/s vvel, med. dev.
72  GNSS_DYNAMICS_PEDESTRIAN, // Pedestrian dynamic model, 9000m alt, 30m/s hvel, 20m/s vvel, small dev.
73  GNSS_DYNAMICS_AIRBORNE_1G // Airborne 1G dynamic model, 50000m alt, 100m/s hvel, 100m/s vvel, large dev.
75 
76 /* GPS fix status codes */
77 typedef enum
78 {
79  // GNSS_FIX_NO_GPS, // No GPS sensor connected/detected
80  GNSS_FIX_NONE, // Receiving valid GPS data, but with no position lock
81  GNSS_FIX_2D, // Receiving valid GPS data with a 2D lock
82  GNSS_FIX_3D // Receiving valid GPS data with a 3D lock
84 
85 /* GPS inavigation rates */
86 typedef enum
87 {
88  GNSS_NAVRATE_5HZ = 5, // 5 Hz navigation rate/output rate
89  GNSS_NAVRATE_10HZ = 10 // 10 Hz navigation rate/output rate
91 
92 
93 /* GPS status types */
94 typedef enum
95 {
96  // GNSS_I2C_COMM_ERROR // If an error was encountered communicating to the GPS over I2C
97  // GNSS_CONFIG_SUCCESS, // Successful configuration
98  // GNSS_CONFIG_NO_DEVICE, // No GPS sensor connected/detected
99  // GNSS_CONFIG_RECONNECT_ERROR, // When we change the baud rate and cannot reconnect to the GPS
100  // GNSS_CONFIG_NO_HOME_LOCATION // No home location specified
101 } GNSSStatus_t;
102 
103 
104 
106 {
107 public:
108  GNSSComputer(TwoWire *userWire = &GPS_I2C);
110 
111  // Do not allow copies (singleton)
112  static GNSSComputer &GetInstance();
113  GNSSComputer(const GNSSComputer &) = delete;
114  GNSSComputer &operator=(const GNSSComputer &) = delete;
115 
116  bool ConfigureDevice(
117  GNSSNetworks_t userNetwork = GNSS_NET_GPS,
120  );
121  bool WaitForSatellites(uint32_t nSats = GNSS_MIN_SATS);
122  bool ListenForData();
123 
124  TinyGPSPlus NMEAParser; // TinyGPS++ GPS object
125  // TinyGPSCustom PDOPParser; // Parse GxGSA for PDOP
126 
127 protected:
128 private:
129 
130  // GGA
131  // ortho. height
132  // geoid sep.
133  // gps quality/fix type?
134  // TinyGPSCustom GeoidSepParser; // Parse GxGGA for geoid separation [m] (Sep = AltWGS84 - AltMSL)
135 
136  // GSA
137  // pdop
138  // hdop
139  // vdop
140  // TinyGPSCustom PDOPParser; // Parse GxGSA for PDOP
141  // TinyGPSCustom HDOPParser; // Parse GxGSA for HDOP
142  // TinyGPSCustom VDOPParser; // Parse GxGSA for VDOP
143 
144  // VTG
145  // mag. track/cog
146  // true track/cog
147  // speed, kts
148  // speed over gnd, kph
149  // TinyGPSCustom TrueTrackParser; // Parse GxVTG for true track [deg]
150  // TinyGPSCustom GroundSpeedParser; // Parse GxVTG for ground speed [kts]
151 
152  Vectord PosLLA; // [rad, rad, m] Lat, lon, altitude
153  Vectorf PosECEF; // [m, m, m] ECEF position
154  Vectorf VelECEF; // [m/s, m/s, m/s] ECEF velocity
155  void SendUBXConfigMessage(const uint8_t *msg, size_t len);
156  // LPF for smoothing gps pos (to smooth out impulses/sharp changes)
157  // LPF for smoothing altitude
158 private:
159  bool isConfigured; // True if ConfigureDevice() was called, false if not
160  uint32_t lastDataCheck; // [ms] millis() of when last checked for new data.
161  uint32_t dataPollWait; // [ms] Period between polling for new data.
162  int32_t gpsBaud; // Baud rate for serial connection
163  float navTs; // [sec] GPS navigation sample period
164  float navRate; // [Hz] GPS navigation rate
165 
166 
167  // GNSSBaudRates_t gpsBaud; // Baud rate for GPS serial communication
168  GNSSNetworks_t network; // Satellite network(s) that the GPS is connected to
169  GNSSDynamics_t dynamicModel; // Dynamic model for the GPS fusion algorithms
170  GNSSNavRate_t updateRate; // [Hz] navigation rate from gps
171  TwoWire *gpsWire; // HW I2C bus that the GPS is connected to
172 };
173 
174 // Only one instance
175 extern GNSSComputer &GPS;
Definition: gnss_computer.h:106
TwoWire * gpsWire
Definition: gnss_computer.h:171
GNSSComputer(TwoWire *userWire=&GPS_I2C)
Definition: gnss_computer.cpp:10
bool WaitForSatellites(uint32_t nSats=GNSS_MIN_SATS)
Wait for satellites to be received by the GPS.
Definition: gnss_computer.cpp:235
GNSSDynamics_t dynamicModel
Definition: gnss_computer.h:169
Vectorf VelECEF
Definition: gnss_computer.h:154
GNSSComputer(const GNSSComputer &)=delete
GNSSComputer & operator=(const GNSSComputer &)=delete
GNSSNetworks_t network
Definition: gnss_computer.h:168
Vectorf PosECEF
Definition: gnss_computer.h:153
void SendUBXConfigMessage(const uint8_t *msg, size_t len)
Send UBX configuration message over I2C.
Definition: gnss_computer.cpp:502
bool ConfigureDevice(GNSSNetworks_t userNetwork=GNSS_NET_GPS, GNSSDynamics_t userDynModel=GNSS_DYNAMICS_PEDESTRIAN, GNSSNavRate_t userODR=GNSS_NAVRATE_10HZ)
Configure the GPS sensor.
Definition: gnss_computer.cpp:37
float navTs
Definition: gnss_computer.h:163
uint32_t lastDataCheck
Definition: gnss_computer.h:160
uint32_t dataPollWait
Definition: gnss_computer.h:161
GNSSNavRate_t updateRate
Definition: gnss_computer.h:170
static GNSSComputer & GetInstance()
Definition: gnss_computer.cpp:544
bool ListenForData()
Read data from GPS I2C port and feed the TinyGPS NMEA parser data.
Definition: gnss_computer.cpp:340
Vectord PosLLA
Definition: gnss_computer.h:152
TinyGPSPlus NMEAParser
Definition: gnss_computer.h:124
~GNSSComputer()
Definition: gnss_computer.h:109
float navRate
Definition: gnss_computer.h:164
bool isConfigured
Definition: gnss_computer.h:159
int32_t gpsBaud
Definition: gnss_computer.h:162
A vector object is definied by it's rows/length.
Definition: vectors.h:155
A vector object is definied by it's rows/length.
Definition: vectors.h:46
constexpr uint32_t GNSS_MIN_SATS
Definition: gnss_computer.h:42
GNSSDynamics_t
Definition: gnss_computer.h:70
@ GNSS_DYNAMICS_PORTABLE
Definition: gnss_computer.h:71
@ GNSS_DYNAMICS_PEDESTRIAN
Definition: gnss_computer.h:72
@ GNSS_DYNAMICS_AIRBORNE_1G
Definition: gnss_computer.h:73
GNSSNetworks_t
Definition: gnss_computer.h:62
@ GNSS_NET_GLONASS
Definition: gnss_computer.h:64
@ GNSS_NET_GPS_GLONASS
Definition: gnss_computer.h:65
@ GNSS_NET_GPS
Definition: gnss_computer.h:63
GNSSComputer & GPS
Definition: gnss_computer.cpp:550
constexpr uint32_t GNSS_POS_LOCK_TIMEOUT
Teensy 4.1 has a 64 byte serial buffer that can be expanded with: Serial1.addMemoryForRead(void *buff...
Definition: gnss_computer.h:36
constexpr uint16_t GNSS_I2C_BUFFSIZE
Definition: gnss_computer.h:48
GNSSNavRate_t
Definition: gnss_computer.h:87
@ GNSS_NAVRATE_5HZ
Definition: gnss_computer.h:88
@ GNSS_NAVRATE_10HZ
Definition: gnss_computer.h:89
constexpr int32_t GNSS_DEFAULT_BAUD
Definition: gnss_computer.h:39
GNSSFix_t
Definition: gnss_computer.h:78
@ GNSS_FIX_NONE
Definition: gnss_computer.h:80
@ GNSS_FIX_3D
Definition: gnss_computer.h:82
@ GNSS_FIX_2D
Definition: gnss_computer.h:81
GNSSStatus_t
Definition: gnss_computer.h:95
constexpr uint8_t GNSS_I2C_ADDR
Definition: gnss_computer.h:45
#define GPS_I2C
Definition: hummingbird_config.h:42