Skip to content

Commit 4afb492

Browse files
PaulZCnseidle
authored andcommitted
Create GNSS_GetPosition_v3.ino
1 parent 504127b commit 4afb492

File tree

1 file changed

+196
-0
lines changed

1 file changed

+196
-0
lines changed
Lines changed: 196 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,196 @@
1+
/*
2+
RTK Surveyor Firmware - Test Sketch using u-blox GNSS Library v3 (I2C and SPI)
3+
*/
4+
5+
#include <Wire.h> // Needed for I2C to GNSS
6+
#include <SPI.h> // Needed for SPI to GNSS
7+
8+
#include <SparkFun_u-blox_GNSS_v3.h> //http://librarymanager/All#SparkFun_u-blox_GNSS_v3
9+
10+
// In this test, the GNSS object is instantiated inside the loop - to check for memory leaks
11+
12+
#define gnssSerial Serial1
13+
14+
// Define which hardware interface the module will use. This depends on the RTK hardware.
15+
// Most are I2C. Reference Station is SPI. Future hardware _could_ be Serial...
16+
typedef enum {
17+
RTK_GNSS_IS_I2C,
18+
RTK_GNSS_IS_SPI,
19+
RTK_GNSS_IS_SERIAL
20+
} RTK_GNSS_INTERFACE;
21+
RTK_GNSS_INTERFACE theGNSSinterface;
22+
23+
const int GNSS_SPI_CS = 4; // Chip select for the SPI interface - the "free" pin on Thing Plus C
24+
25+
void setup()
26+
{
27+
delay(1000);
28+
29+
Serial.begin(115200);
30+
Serial.println("RTK Surveyor Firmware - u-blox GNSS Test Sketch");
31+
32+
theGNSSinterface = RTK_GNSS_IS_I2C; // Select I2C for this test
33+
34+
// Prepare the correct hardware interface for GNSS comms
35+
36+
if (theGNSSinterface == RTK_GNSS_IS_SERIAL)
37+
{
38+
gnssSerial.begin(38400);
39+
}
40+
else if (theGNSSinterface == RTK_GNSS_IS_SPI)
41+
{
42+
SPI.begin(); // Redundant - just to show what could be done here
43+
}
44+
else // if (theGNSSinterface == RTK_GNSS_IS_I2C) // Catch-all. Default to I2C
45+
{
46+
Wire.begin();
47+
//Wire.setClock(400000);
48+
}
49+
50+
while (Serial.available()) // Make sure the Serial buffer is empty
51+
Serial.read();
52+
}
53+
54+
void loop()
55+
{
56+
// =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
57+
// Create an object of the GNSS super-class inside the loop - just as a test to check for memory leaks
58+
59+
SFE_UBLOX_GNSS_SUPER theGNSS;
60+
61+
// =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
62+
63+
//theGNSS.enableDebugging(Serial); // Uncomment this line to enable debug messages
64+
65+
// =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
66+
// Now begin the GNSS
67+
68+
bool beginSuccess = false;
69+
70+
if (theGNSSinterface == RTK_GNSS_IS_SERIAL)
71+
{
72+
beginSuccess = theGNSS.begin(gnssSerial);
73+
}
74+
75+
else if (theGNSSinterface == RTK_GNSS_IS_SPI)
76+
{
77+
beginSuccess = theGNSS.begin(SPI, GNSS_SPI_CS); // SPI, default to 4MHz
78+
79+
//beginSuccess = theGNSS.begin(SPI, GNSS_SPI_CS, 4000000); // Custom
80+
81+
//SPISettings customSPIsettings = SPISettings(4000000, MSBFIRST, SPI_MODE0);
82+
//beginSuccess = theGNSS.begin(SPI, GNSS_SPI_CS, customSPIsettings); // Custom
83+
}
84+
85+
else // if (theGNSSinterface == RTK_GNSS_IS_I2C) // Catch-all. Default to I2C
86+
{
87+
beginSuccess = theGNSS.begin(); // Wire, 0x42
88+
89+
//beginSuccess = theGNSS.begin(Wire, 0x42); // Custom
90+
}
91+
92+
// =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
93+
// Check begin was successful
94+
95+
if (!beginSuccess)
96+
{
97+
Serial.println(F("u-blox GNSS not detected. Please check wiring. Freezing."));
98+
while (1)
99+
{
100+
if (Serial.available())
101+
ESP.restart();
102+
delay(10);
103+
}
104+
}
105+
106+
// =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
107+
// Disable the NMEA, just to show how to do it:
108+
109+
if (theGNSSinterface == RTK_GNSS_IS_SERIAL)
110+
{
111+
// Assume we're connected to UART1. Could be UART2
112+
theGNSS.setUART1Output(COM_TYPE_UBX); //Set the UART1 port to output UBX only (turn off NMEA noise)
113+
}
114+
else if (theGNSSinterface == RTK_GNSS_IS_SPI)
115+
{
116+
theGNSS.setSPIOutput(COM_TYPE_UBX); //Set the SPI port to output UBX only (turn off NMEA noise)
117+
}
118+
else // if (theGNSSinterface == RTK_GNSS_IS_I2C) // Catch-all. Default to I2C
119+
{
120+
theGNSS.setI2COutput(COM_TYPE_UBX); //Set the I2C port to output UBX only (turn off NMEA noise)
121+
}
122+
123+
// =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
124+
//Query module info
125+
126+
if (theGNSS.getModuleInfo())
127+
{
128+
Serial.print(F("FWVER: "));
129+
Serial.print(theGNSS.getFirmwareVersionHigh());
130+
Serial.print(F("."));
131+
Serial.println(theGNSS.getFirmwareVersionLow());
132+
133+
Serial.print(F("Firmware: "));
134+
Serial.println(theGNSS.getFirmwareType());
135+
136+
Serial.print(F("PROTVER: "));
137+
Serial.print(theGNSS.getProtocolVersionHigh());
138+
Serial.print(F("."));
139+
Serial.println(theGNSS.getProtocolVersionLow());
140+
141+
Serial.print(F("MOD: "));
142+
Serial.println(theGNSS.getModuleName());
143+
}
144+
145+
// =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
146+
//Query module for HPPOSLLH data
147+
148+
if (theGNSS.getHPPOSLLH()) // Returns true when fresh data is available
149+
{
150+
int32_t latitude = theGNSS.getHighResLatitude();
151+
int8_t latitudeHp = theGNSS.getHighResLatitudeHp();
152+
int32_t longitude = theGNSS.getHighResLongitude();
153+
int8_t longitudeHp = theGNSS.getHighResLongitudeHp();
154+
int32_t ellipsoid = theGNSS.getElipsoid();
155+
int8_t ellipsoidHp = theGNSS.getElipsoidHp();
156+
int32_t msl = theGNSS.getMeanSeaLevel();
157+
int8_t mslHp = theGNSS.getMeanSeaLevelHp();
158+
uint32_t accuracy = theGNSS.getHorizontalAccuracy();
159+
160+
// Defines storage for the lat and lon as double
161+
double d_lat; // latitude
162+
double d_lon; // longitude
163+
164+
// Assemble the high precision latitude and longitude
165+
d_lat = ((double)latitude) / 10000000.0; // Convert latitude from degrees * 10^-7 to degrees
166+
d_lat += ((double)latitudeHp) / 1000000000.0; // Now add the high resolution component (degrees * 10^-9 )
167+
d_lon = ((double)longitude) / 10000000.0; // Convert longitude from degrees * 10^-7 to degrees
168+
d_lon += ((double)longitudeHp) / 1000000000.0; // Now add the high resolution component (degrees * 10^-9 )
169+
170+
float f_ellipsoid;
171+
float f_accuracy;
172+
173+
// Calculate the height above ellipsoid in mm * 10^-1
174+
f_ellipsoid = (ellipsoid * 10) + ellipsoidHp;
175+
f_ellipsoid = f_ellipsoid / 10000.0; // Convert from mm * 10^-1 to m
176+
177+
f_accuracy = accuracy / 10000.0; // Convert from mm * 10^-1 to m
178+
179+
// Finally, do the printing
180+
Serial.print("Lat (deg): ");
181+
Serial.print(d_lat, 9);
182+
Serial.print(", Lon (deg): ");
183+
Serial.print(d_lon, 9);
184+
185+
Serial.print(", Accuracy (m): ");
186+
Serial.print(f_accuracy, 4); // Print the accuracy with 4 decimal places
187+
188+
Serial.print(", Altitude (m): ");
189+
Serial.print(f_ellipsoid, 4); // Print the ellipsoid with 4 decimal places
190+
191+
Serial.println();
192+
}
193+
194+
if (Serial.available())
195+
ESP.restart();
196+
}

0 commit comments

Comments
 (0)