Parsing GPS data

We have built the gpsdemo project to read GPS data via the UART interface. We can see that the GPS module output shows raw data. To obtain the current position of our location with the GPS module, we should parse our GPS data. There are a number of libraries that we can use to parse GPS data. 

For our project, we can use the minmea library (https://github.com/kosma/minmea). You can download the minmea project and extract it into components for our project. You can see how to to implement minmea as the ESP32 component in Figure 7.6:

Figure 7.6: The project structure for the gpsdemo project

Now we can modify our gpsdemo project. We do this by adding minmea.h in our main program file:

// minmea
#include "minmea.h"

We also define latitude, longitude, fix_quality, and satellites_tracked as variables to hold our current GPS data:

// GPS variables and initial state
float latitude = -1.0;
float longitude = -1.0;
int fix_quality = -1;
int satellites_tracked = -1;

We define the parse_gps_nmea() function to parse GPS data. We can use the minmea_sentence_id() function from the minmea library to identify the GPS data type.

If the GPS data type is MINMEA_SENTENCE_RMC, we can extract the GPS position using the minmea_parse_rmc() function. We will get the minmea_sentence_rmc struct after calling the minmea_parse_rmc() function; I will identify various data types, as follows:

void parse_gps_nmea(char* line){
// parse the line
switch (minmea_sentence_id(line, false)) {
case MINMEA_SENTENCE_RMC: {
...
float new_longitude = minmea_tocoord(&frame.longitude);
...
}


}

Our parse_gps_nmea() function will be called in the uart_event_task() function. We put this on while()as shown in the following script:

static void uart_event_task(void *pvParameters)
{
while (1) {
char *line = read_line(GPS_UART_NUM);

parse_gps_nmea(line);

}
/* Should never get here */
vTaskDelete(NULL);
}

You can now save the codes, compile, and upload the project into the ESP32 board. Open the serial tool to see the program output; Figure 7.7 shows my current location via the GPS module:

Figure 7.7: The program output from the gpsdemo project
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.225.255.134