PER Firmware
Loading...
Searching...
No Matches
gps.h
Go to the documentation of this file.
1#ifndef GPS_H
2#define GPS_H
3
4// todo modernize with C23
5
13#include <stdint.h>
14
15/*
16 Instructions on configuring GPS:
17 1. GPS will not send messages without a fix, so find a space where the GPS gets a fix (use UCenter for this)
18 2. Pull up configuration menu, go to "msg" configuration
19 2a. Select "01-07 NAV-PVT", and enable it on your desired communication port and USB
20 2b. Find any messages containing "NMEA", and disable them for USB and your desired communication port
21 2c. Send this config change to the GPS
22 3. Go to "PRT (Port)" configuration
23 3a. This is where you configure your peripheral. Currently, USART1, with baudrate at 115200 is used. Also, ensure that "UBX" is selected ad protocol out.
24 3b. Send this config change to the GPS
25 4. Go to "RATE" configuration
26 4a. Select 40ms as the measurement period
27 4b. Send this config change to the GPS
28 5. Go to "CFG" setting, and hit the "send" button to once again save changes to the GPS.
29 6. At this point, confirm you changes by turning it off and reconnecting it, ensuring the correct message is sent at 40ms
30*/
31
32// GPS RX buffer size
33#define GPS_RX_BUF_SIZE 100 // The total message length is 100 bytes
34
35// GPS UBX NAV-PVT message header
36#define UBX_NAV_PVT_HEADER_B0 0xB5 // UBX message header sync byte 0
37#define UBX_NAV_PVT_HEADER_B1 0x62 // UBX message header sync byte 1
38#define UBX_NAV_PVT_CLASS 0x01 // UBX message class for NAV-PVT
39#define UBX_NAV_PVT_MSG_ID 0x07 // UBX message ID for NAV-PVT
40
41// Bitmask for NAV_PVT_t.valid flags
42typedef uint8_t gps_valid_flags_t;
43#define GPS_VALID_DATE ((gps_valid_flags_t)0x01) // Valid UTC Date
44#define GPS_VALID_TIME ((gps_valid_flags_t)0x02) // Valid UTC Time
45#define GPS_VALID_FULLY_RESOLVED ((gps_valid_flags_t)0x04) // UTC fully resolved
46#define GPS_VALID_MAG ((gps_valid_flags_t)0x08) // Valid Magnetic Declination
47
48// GPS Fix Types for NAV_PVT_t.fixType
49typedef uint8_t gps_fix_type_t;
50#define GPS_FIX_TYPE_NONE ((gps_fix_type_t)0x00) // No fix
51#define GPS_FIX_TYPE_DEAD_RECKONING ((gps_fix_type_t)0x01) // Dead Reckoning only
52#define GPS_FIX_TYPE_GNSS_2D ((gps_fix_type_t)0x02) // 2D fix
53#define GPS_FIX_TYPE_GNSS_3D ((gps_fix_type_t)0x03) // 3D fix
54#define GPS_FIX_TYPE_GNSS_DEAD_RECKONING ((gps_fix_type_t)0x04) // GNSS + Dead Reckoning
55#define GPS_FIX_TYPE_TIME_ONLY ((gps_fix_type_t)0x05) // Time only fix
56
57// GPS Fix Status Flags for NAV_PVT_t.fixStatus
58typedef uint8_t gps_flags1_t;
59#define GPS_FLAG1_GNSS_FIX_OK ((gps_flags1_t)0x01) // GPS fix OK
60#define GPS_FLAG1_DIFF_SOLN ((gps_flags1_t)0x02) // Differential GPS fix
61
62#define GPS_FLAG1_PSM_MASK ((gps_flags1_t)0x1C) // PSM (Power Save Mode) mask
63#define GPS_FLAG1_PSM_OFF ((gps_flags1_t)0x00) // PSM off
64#define GPS_FLAG1_PSM_ENABLED ((gps_flags1_t)0x04) // PSM on
65#define GPS_FLAG1_PSM_ACQUIRED ((gps_flags1_t)0x08) // PSM active
66#define GPS_FLAG1_PSM_TRACKING ((gps_flags1_t)0x0C) // PSM tracking
67#define GPS_FLAG1_PSM_POWER_OPTIMIZED ((gps_flags1_t)0x10) // PSM power optimized
68#define GPS_FLAG1_PSM_INACTIVE ((gps_flags1_t)0x14) // PSM inactive
69
70#define GPS_FLAG1_HEADING_VALID ((gps_flags1_t)0x20) // Heading is valid
71#define GPS_FLAG1_CARRIER_PHASE_MASK ((gps_flags1_t)0xC0) // Carrier phase is valid
72#define GPS_FLAG1_CARRIER_PHASE_NONE ((gps_flags1_t)0x00) // No carrier phase
73#define GPS_FLAG1_CARRIER_PHASE_FLOAT ((gps_flags1_t)0x40) // Carrier phase float
74#define GPS_FLAG1_CARRIER_PHASE_FIXED ((gps_flags1_t)0x80) // Carrier phase fixed
75
76typedef uint8_t gps_flags2_t;
77#define GPS_FLAGS2_CONFIRMED_AVAILABLE ((gps_flags2_t)0x20) // UTC date and time are confirmed available
78#define GPS_FLAGS2_CONFIRMED_DATE ((gps_flags2_t)0x40) // UTC date is confirmed
79#define GPS_FLAGS2_CONFIRMED_TIME ((gps_flags2_t)0x80) // UTC time is confirmed
80
81typedef struct {
82 uint32_t iTOW;
83 uint16_t year;
84 uint8_t month;
85 uint8_t day;
86 uint8_t hour;
87 uint8_t minute;
88 uint8_t second;
89
90 gps_valid_flags_t valid; // GPS_VALID_*
91
92 uint32_t timeAccuracy; // time accuracy in nanoseconds
93 int32_t nano; // nanoseconds of second
94
95 gps_fix_type_t fixType; // GPS_FIX_TYPE_*
96 gps_flags1_t flags1; // GPS_FIX_FLAG_*
97 gps_flags2_t flags2; // GPS_FLAGS2_*
98
99 uint8_t numSatellites; // Number of satellites used in Nav Solution
100 int32_t longitude; // Longitude in 1e-7 degrees
101 int32_t latitude; // Latitude in 1e-7 degrees
102 int32_t height; // Height above ellipsoid in mm
103 int32_t heightMSL; // Height above mean sea level in mm
104 uint32_t horizontalAccuracy; // Horizontal accuracy estimate in mm
105 uint32_t verticalAccuracy; // Vertical accuracy estimate in mm
106
107 int32_t velNorth; // NED North Velocity in mm/s
108 int32_t velEast; // NED East Velocity in mm/s
109 int32_t velDown; // NED DownVelocity in mm/s
110 int32_t groundSpeed; // Ground Speed in mm/s
111 int32_t headingMotion; // Heading of motion
112 uint32_t speedAccuracy; // Speed accuracy estimate in mm/s
113
114 uint16_t positionDOP; // Position Dilution of Precision (0.01 scale)
115 uint8_t reserved[6];
116
117 int32_t headingVehicle; // Heading of vehicle in 1e-5 degrees
118
119 int16_t magneticDec; // Magnetic declination in 1e-2 degrees
120 uint16_t magneticAcc; // Magnetic declination accuracy in 1e-2 degrees
121} NAV_PVT_t;
122
123typedef struct {
124 uint8_t gps_rx_buffer[GPS_RX_BUF_SIZE]; // Raw message as received from GPS
125
126 NAV_PVT_t data; // Decoded message
127 bool isValidDate;
128 bool isValidTime;
129 bool isFullyResolved;
130 bool isValidMag;
131 bool hasGNSSFix;
133
134static inline int32_t bytes_to_int32(uint8_t b0, uint8_t b1, uint8_t b2, uint8_t b3) {
135 uint32_t value = (uint32_t)b0 | ((uint32_t)b1 << 8) | ((uint32_t)b2 << 16) | ((uint32_t)b3 << 24);
136 return (int32_t)value; // Cast to signed int32_t
137}
138
139static inline uint32_t bytes_to_uint32(uint8_t b0, uint8_t b1, uint8_t b2, uint8_t b3) {
140 return (uint32_t)b0 | ((uint32_t)b1 << 8) | ((uint32_t)b2 << 16) | ((uint32_t)b3 << 24);
141}
142
143static inline uint16_t bytes_to_uint16(uint8_t b0, uint8_t b1) {
144 return (uint16_t)b0 | ((uint16_t)b1 << 8);
145}
146
147static inline int16_t bytes_to_int16(uint8_t b0, uint8_t b1) {
148 uint16_t value = (uint16_t)b0 | ((uint16_t)b1 << 8);
149 return (int16_t)value; // Cast to signed int16_t
150}
151
152static inline bool is_valid_header(uint8_t sync0, uint8_t sync1, uint8_t class_id, uint8_t msg_id) {
153 return (sync0 == UBX_NAV_PVT_HEADER_B0) && (sync1 == UBX_NAV_PVT_HEADER_B1) && (class_id == UBX_NAV_PVT_CLASS) && (msg_id == UBX_NAV_PVT_MSG_ID);
154}
155
163bool GPS_Decode(GPS_Handle_t *gps);
164
165#endif // GPS_H
bool GPS_Decode(GPS_Handle_t *gps)
Function to decode periodic GPS UBX message.
Definition gps.c:10
Definition gps.h:123
Definition gps.h:81