Skip to content
Snippets Groups Projects
Commit f1b4566a authored by roaa1g19's avatar roaa1g19
Browse files

First commit for GPS code, Using HardwareSerial for Serial comms.

_rodGPS_SimpleDistance() function to calculate distance between two points.
_rodGPS_MakeReading() to make a reading from GPS IC.
_rodGPS_PrintGPRMC() to print data
simple_GPRMC struct defined to store data given by IC. Use std::atof to convert the strings to float values

Signed-off-by: default avatarRodrigo Amaral <roaa1g19@soton.ac.uk>
parent 6f617c70
No related branches found
No related tags found
No related merge requests found
.pio
.vscode/.browse.c_cpp.db*
.vscode/c_cpp_properties.json
.vscode/launch.json
.vscode/ipch
{
// See http://go.microsoft.com/fwlink/?LinkId=827846
// for the documentation about the extensions.json format
"recommendations": [
"platformio.platformio-ide"
]
}
{
"files.associations": {
"array": "cpp",
"*.tcc": "cpp",
"cctype": "cpp",
"clocale": "cpp",
"cmath": "cpp",
"cstdarg": "cpp",
"cstddef": "cpp",
"cstdint": "cpp",
"cstdio": "cpp",
"cstdlib": "cpp",
"cstring": "cpp",
"ctime": "cpp",
"cwchar": "cpp",
"cwctype": "cpp",
"deque": "cpp",
"unordered_map": "cpp",
"unordered_set": "cpp",
"vector": "cpp",
"exception": "cpp",
"algorithm": "cpp",
"functional": "cpp",
"system_error": "cpp",
"tuple": "cpp",
"type_traits": "cpp",
"fstream": "cpp",
"initializer_list": "cpp",
"iomanip": "cpp",
"iosfwd": "cpp",
"istream": "cpp",
"limits": "cpp",
"memory": "cpp",
"new": "cpp",
"ostream": "cpp",
"numeric": "cpp",
"sstream": "cpp",
"stdexcept": "cpp",
"streambuf": "cpp",
"cinttypes": "cpp",
"utility": "cpp",
"typeinfo": "cpp"
}
}
\ No newline at end of file
This directory is intended for project header files.
A header file is a file containing C declarations and macro definitions
to be shared between several project source files. You request the use of a
header file in your project source file (C, C++, etc) located in `src` folder
by including it, with the C preprocessing directive `#include'.
```src/main.c
#include "header.h"
int main (void)
{
...
}
```
Including a header file produces the same results as copying the header file
into each source file that needs it. Such copying would be time-consuming
and error-prone. With a header file, the related declarations appear
in only one place. If they need to be changed, they can be changed in one
place, and programs that include the header file will automatically use the
new version when next recompiled. The header file eliminates the labor of
finding and changing all the copies as well as the risk that a failure to
find one copy will result in inconsistencies within a program.
In C, the usual convention is to give header files names that end with `.h'.
It is most portable to use only letters, digits, dashes, and underscores in
header file names, and at most one dot.
Read more about using header files in official GCC documentation:
* Include Syntax
* Include Operation
* Once-Only Headers
* Computed Includes
https://gcc.gnu.org/onlinedocs/cpp/Header-Files.html
This directory is intended for project specific (private) libraries.
PlatformIO will compile them to static libraries and link into executable file.
The source code of each library should be placed in a an own separate directory
("lib/your_library_name/[here are source files]").
For example, see a structure of the following two libraries `Foo` and `Bar`:
|--lib
| |
| |--Bar
| | |--docs
| | |--examples
| | |--src
| | |- Bar.c
| | |- Bar.h
| | |- library.json (optional, custom build options, etc) https://docs.platformio.org/page/librarymanager/config.html
| |
| |--Foo
| | |- Foo.c
| | |- Foo.h
| |
| |- README --> THIS FILE
|
|- platformio.ini
|--src
|- main.c
and a contents of `src/main.c`:
```
#include <Foo.h>
#include <Bar.h>
int main (void)
{
...
}
```
PlatformIO Library Dependency Finder will find automatically dependent
libraries scanning project source files.
More information about PlatformIO Library Dependency Finder
- https://docs.platformio.org/page/librarymanager/ldf.html
; PlatformIO Project Configuration File
;
; Build options: build flags, source filter
; Upload options: custom upload port, speed and extra flags
; Library options: dependencies, extra library storages
; Advanced options: extra scripting
;
; Please visit documentation for the other options and examples
; https://docs.platformio.org/page/projectconf.html
[env:esp32dev]
platform = espressif32
board = esp32dev
framework = arduino
monitor_speed = 9600
upload_port = com9
#include "rodGPS.hpp"
static simple_GPRMC gprmc_buff;
static simple_GPRMC test;
void setup()
{
Serial2.begin(9600);
Serial.begin(9600);
while (!Serial){;} /*Used for native USB support */
Serial.println("\n-------------------------\n");
}
void loop()
{
_rodGPS_MakeReading(&gprmc_buff, &test);
Serial.print(std::atof("32.575"));
Serial.write("\n\n");
}
#include "rodGPS.hpp"
void _rodGPS_PrintGPRMC(simple_GPRMC* gprmc_data)
{
Serial.write("\nValidity: ");
Serial.write(gprmc_data->VALID);
Serial.write("\nTime: ");
Serial.write(gprmc_data->TIME[0]);
Serial.write(gprmc_data->TIME[1]);
Serial.write(':');
Serial.write(gprmc_data->TIME[2]);
Serial.write(gprmc_data->TIME[3]);
Serial.write(':');
Serial.write(gprmc_data->TIME[4]);
Serial.write(gprmc_data->TIME[5]);
Serial.write(" Date: ");
Serial.write(gprmc_data->DATE[0]);
Serial.write(gprmc_data->DATE[1]);
Serial.write('/');
Serial.write(gprmc_data->DATE[2]);
Serial.write(gprmc_data->DATE[3]);
Serial.write('/');
Serial.write(gprmc_data->DATE[4]);
Serial.write(gprmc_data->DATE[5]);
Serial.write("\nLatitude: ");
Serial.write((uint8_t*)&(gprmc_data->LATI),10);
Serial.write(gprmc_data->LATI_DIR);
Serial.write(" Longitude: ");
Serial.write((uint8_t*)&(gprmc_data->LONG),11);
Serial.write(gprmc_data->LONG_DIR);
Serial.write("\n\n");
}
double _rodGPS_SimpleDistance(simple_GPRMC* coord_1, simple_GPRMC* coord_2)
{
double dx = 111.3 * cos((std::atof(coord_1->LATI) + std::atof(coord_2->LATI) / 2 * 0.01745)*(PI / 180)) * (std::atof(coord_1->LONG) - std::atof(coord_2->LONG));
double dy = 111.3 * ((std::atof(coord_1->LATI)) - (std::atof(coord_2->LATI)));
return sqrt( dx*dx + dy*dy );
}
static char char_buff[5];
byte _rodGPS_MakeReading(simple_GPRMC* gprmc1, simple_GPRMC* gprmc2)
{
while(Serial2.available() == 0) {;}
while(Serial2.available() > 0)
{
int c = Serial2.read();
while(c != '$') {c = Serial2.read();}
for(char i = 0; i < 5; i++)
char_buff[i] = Serial2.read();
if(char_buff[2] == 'R' | char_buff[3] == 'M' | char_buff[4] == 'R') /* Check it's GPRMC data, there are several coming trough */
{
byte struct_count = 0;
while ((c = Serial2.read()) != '\n')
{
if(c == ',') /* The Data structure of GPRMC is divided in commas */
struct_count++;
if(struct_count == 1) /* Time HH MM SS MSMS */
{
for(byte ii = 0; ii < 9; ii++)
{
gprmc1->TIME[ii] = Serial2.read();
}
}
else if(struct_count == 2) /* Validity, A-ok V-void */
{
gprmc1->VALID = Serial2.read();
}
else if(struct_count == 3) /* Latitude 5 decimal places */
{
for(byte ii = 0; ii < 10; ii++)
gprmc1->LATI[ii] = Serial2.read();
}
else if(struct_count == 4) /* Latitude direction N / S */
{
gprmc1->LATI_DIR = Serial2.read();
}
else if(struct_count == 5) /* Longitude 5 decimal cases */
{
for(byte ii = 0; ii < 11; ii++)
gprmc1->LONG[ii] = Serial2.read();
}
else if(struct_count == 6) /* Longitutde direction E / W */
{
gprmc1->LONG_DIR = Serial2.read();
}
else if(struct_count == 9) /* Date DD MM YY */
{
for(byte ii = 0; ii < 6; ii++)
gprmc1->DATE[ii] = Serial2.read();
}
}
_rodGPS_PrintGPRMC(gprmc1);
Serial.write("Distance: ");
Serial.println(String((_rodGPS_SimpleDistance(gprmc1,gprmc2)),2));
Serial.write("\n\n");
for(byte i = 0; i < 10; i++)
gprmc2->LATI[i] = gprmc1->LATI[i];
for(byte i = 0; i < 11; i++)
gprmc2->LONG[i] = gprmc1->LONG[i];
}
}
return 0;
}
\ No newline at end of file
#include <Arduino.h>
#include <HardwareSerial.h>
#include <string.h>
#ifndef RODGPS_HPP
#define RODGPS_HPP
typedef struct simple_GPRMC
{
char TIME[9];
char DATE[6];
char VALID;
char LATI[10];
char LATI_DIR;
char LONG[11];
char LONG_DIR;
};
void _rodGPS_PrintGPRMC(simple_GPRMC* gprmc_data);
double _rodGPS_SimpleDistance(simple_GPRMC* coord_1, simple_GPRMC* coord_2);
byte _rodGPS_MakeReading(simple_GPRMC* gprmc1, simple_GPRMC* gprmc2);
#endif /* RODGPS_HPP */
\ No newline at end of file
This directory is intended for PlatformIO Unit Testing and project tests.
Unit Testing is a software testing method by which individual units of
source code, sets of one or more MCU program modules together with associated
control data, usage procedures, and operating procedures, are tested to
determine whether they are fit for use. Unit testing finds problems early
in the development cycle.
More information about PlatformIO Unit Testing:
- https://docs.platformio.org/page/plus/unit-testing.html
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment