-
Notifications
You must be signed in to change notification settings - Fork 0
/
m5stick_gps.ino
166 lines (164 loc) · 5.34 KB
/
m5stick_gps.ino
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
#include <M5StickC.h>
#include <NMEAGPS.h>
#include <stdio.h>
#include <string>
#define PI 3.14159265
#define MAG 38
HardwareSerial SerialGPS(2);
typedef struct GPGSV
{
int satnumber;
int elevation;
int azimuth;
int cn;
};
typedef struct GPRMC
{
int h;
int m;
int s;
String stat;
float latitude;
String nors;
float longitude;
String eorw;
float velocity;
float degree;
int d;
int mo;
int y;
};
String term;
int totalsentence;
String numberofsatsstring;
short numberofsats;
char test;
GPGSV gpgsv[20];
GPRMC gprmc;
int counter = 0;
void setup()
{
M5.begin();
M5.Lcd.setRotation(0);
SerialGPS.begin(9600, SERIAL_8N1, 36, 36);
// put your setup code here, to run once:
M5.Lcd.setTextColor(TFT_WHITE, TFT_BLACK);
M5.Lcd.drawCircle(40, 40, 38, BLUE);
M5.Lcd.drawLine(0, 40, 80, 40, BLUE);
M5.Lcd.drawLine(40, 0, 40, 80, BLUE);
}
void refresh()
{
}
void loop()
{
String sentenceType = "";
term = "";
while (SerialGPS.available() > 0)
{
Serial.println("counter=" + String(counter));
counter++;
if (counter >= 40)
{
counter = 0;
M5.Lcd.fillRect(0, 0, 80, 80, BLACK);
}
M5.Lcd.drawCircle(40, 40, 38, BLUE);
M5.Lcd.drawLine(0, 40, 80, 40, BLUE);
M5.Lcd.drawLine(40, 0, 40, 80, BLUE);
for (int i = 0; i < 20; i++)
{
if (gpgsv[i].cn != 0 && gpgsv[i].satnumber != 0)
{
M5.Lcd.fillCircle(sin((gpgsv[i].azimuth) / (180 / PI)) * cos(gpgsv[i].elevation / (180 / PI)) * MAG + 40, -1 * cos((gpgsv[i].azimuth) / (180 / PI)) * cos(gpgsv[i].elevation / (180 / PI)) * MAG + 40, 1, RED);
}
else if (gpgsv[i].cn == 0 && gpgsv[i].satnumber != 0)
{
Serial.println("refreshed");
M5.Lcd.fillCircle(sin((gpgsv[i].azimuth) / (180 / PI)) * cos(gpgsv[i].elevation / (180 / PI)) * MAG + 40, -1 * cos((gpgsv[i].azimuth) / (180 / PI)) * cos(gpgsv[i].elevation / (180 / PI)) * MAG + 40, 1, WHITE);
}
gpgsv[i].satnumber = gpgsv[i].elevation = gpgsv[i].azimuth = gpgsv[i].cn = 0;
}
term = SerialGPS.readStringUntil(',');
Serial.println("term: " + term);
if (term == "$GPGSV")
{
int numberofsentences = SerialGPS.readStringUntil(',').toInt();
int sentencenumber = SerialGPS.readStringUntil(',').toInt();
int numberofsats = SerialGPS.readStringUntil(',').toInt();
int number;
if (numberofsats - (4 * (sentencenumber - 1)) >= 4)
{
number = 4;
}
else
{
number = numberofsats - (4 * (sentencenumber - 1));
}
Serial.println(number);
Serial.println(numberofsats);
for (int i = 0; i < number; i++)
{
gpgsv[(numberofsentences - 1) * 4 + i].satnumber = SerialGPS.readStringUntil(',').toInt();
gpgsv[(numberofsentences - 1) * 4 + i].elevation = SerialGPS.readStringUntil(',').toInt();
gpgsv[(numberofsentences - 1) * 4 + i].azimuth = SerialGPS.readStringUntil(',').toInt();
if (i + 1 == numberofsats - (4 * (sentencenumber - 1)) || i == 3)
{
gpgsv[(numberofsentences - 1) * 4 + i].cn = SerialGPS.readStringUntil('*').toInt();
}
else
{
gpgsv[(numberofsentences - 1) * 4 + i].cn = SerialGPS.readStringUntil(',').toInt();
}
Serial.printf("%d,%d,%d,%d\n", gpgsv[(numberofsentences - 1) * 4 + i].satnumber, gpgsv[(numberofsentences - 1) * 4 + i].elevation, gpgsv[(numberofsentences - 1) * 4 + i].azimuth, gpgsv[(numberofsentences - 1) * 4 + i].cn);
}
Serial.println("void: " + SerialGPS.readStringUntil('\n'));
if (sentencenumber == 4)
{
}
}
else if (term == "$GPRMC")
{
int temp = SerialGPS.readStringUntil('.').toInt();
float tempdouble;
gprmc.s = temp % 100;
gprmc.m = (temp / 100) % 100;
gprmc.h = (temp / 10000) % 100;
SerialGPS.readStringUntil(',');
gprmc.stat = SerialGPS.readStringUntil(',');
tempdouble = SerialGPS.readStringUntil(',').toFloat();
gprmc.latitude = floorf(tempdouble / 100);
gprmc.latitude += fmod(tempdouble, 100) / 60;
gprmc.nors = SerialGPS.readStringUntil(',');
tempdouble = SerialGPS.readStringUntil(',').toFloat();
gprmc.longitude = floorf(tempdouble / 100);
gprmc.longitude += fmod(tempdouble, 100) / 60;
gprmc.eorw = SerialGPS.readStringUntil(',');
gprmc.velocity = SerialGPS.readStringUntil(',').toFloat();
gprmc.degree = SerialGPS.readStringUntil(',').toFloat();
temp = SerialGPS.readStringUntil(',').toInt();
gprmc.y = temp % 100;
gprmc.mo = (temp / 100) % 100;
gprmc.d = (temp / 10000) % 100;
SerialGPS.readStringUntil('\n');
char tempstr[20] = {'\0'};
sprintf(tempstr, "20%d/%d/%d ", gprmc.y, gprmc.mo, gprmc.d);
M5.Lcd.drawString(tempstr, 0, 80);
sprintf(tempstr, "%d:%d:%d ", gprmc.h, gprmc.m, gprmc.s);
M5.Lcd.drawString(tempstr, 0, 90);
sprintf(tempstr, "%08.6fN ", gprmc.latitude);
M5.Lcd.drawString(tempstr, 0, 100);
sprintf(tempstr, "%08.6fE ", gprmc.longitude);
M5.Lcd.drawString(tempstr, 0, 110);
sprintf(tempstr, "v=%f ", gprmc.velocity);
M5.Lcd.drawString(tempstr, 0, 120);
sprintf(tempstr, "azi=%06.3f ", gprmc.degree);
M5.Lcd.drawString(tempstr, 0, 130);
//M5.Lcd.drawString(term, 0, 5);
}
else
{
Serial.println("trash: " + SerialGPS.readStringUntil('\n'));
}
}
}