-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathGPS.ino
192 lines (176 loc) · 5.34 KB
/
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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
/*********************************************************************************************************************************/
// Get data from the GPS, Get other variables, generate the two messages, print some debug info
void CheckGPS()
{
processGPSData();
process_wspr_message();
printGPSData();
}
/*********************************************************************************************************************************/
// This custom version of delay() ensures that the gps object is being "fed".
static void smartDelay(unsigned long ms)
{
unsigned long start = millis();
do
{
while (SerialGPS.available())
gps.encode(SerialGPS.read());
} while (millis() - start < ms);
}
/*********************************************************************************************************************************/
// Blink a LED for visual status information
// Blink 'nr' times for 'ms' milliseconds
void BlinkLED(unsigned int nr, unsigned int ms)
{
if (LED_PIN > 0)
{
int i = 0;
for (i=0; i<nr; i++)
{
digitalWrite(LED_PIN,HIGH);
delay(ms);
digitalWrite(LED_PIN,LOW);
delay(ms);
}
}
}
/*********************************************************************************************************************************/
// Print an unsigned long long to Serial output
void printull(unsigned long long ull)
{
char buf[16];
if(ull > 0xFFFFFFFFLL)
{
sprintf(buf, "%lu%08lu", (unsigned long)(ull>>32), (unsigned long)(ull&0xFFFFFFFFULL));
}
else
{
sprintf(buf, "%lu", (unsigned long)ull);
}
Serial.println( buf );
}
/*********************************************************************************************************************************/
// Process all data from the GPS and check the GPS time to see if we need to start send WSPR
static void processGPSData()
{
// Get the statyus of the satellites
if (gps.satellites.isValid())
{
UGPS.Satellites = gps.satellites.value();
}
else
{
UGPS.Satellites = 0;
}
// Time
if (gps.time.isValid())
{
UGPS.Hours = gps.time.hour();
UGPS.Minutes = gps.time.minute();
UGPS.Seconds = gps.time.second();
}
else
{
UGPS.Hours = 0;
UGPS.Minutes = 0;
UGPS.Seconds = 0;
}
// Position
if (gps.location.isValid())
{
// Set the GPS location
UGPS.Longitude = gps.location.lng();
UGPS.Latitude = gps.location.lat();
UGPS.GPS_valid = 1;
// Translate the GPS position to a Maidenhead location
Get_MaidenHead();
// Blink the LED twice for 50ms to show that the GPS location is valid
BlinkLED(2,50);
// Check if it is time to transmit
if ((UGPS.Seconds <= 2) && (UGPS.Minutes % 2 == 0))
{
//We need to get the correct frequency en transmit power
switch (UGPS.Minutes)
{
case 0:
case 12:
case 24:
case 36:
case 48: freq = freqArray[0]; break;
case 2:
case 14:
case 26:
case 38:
case 50: freq = freqArray[1]; break;
case 4:
case 16:
case 28:
case 40:
case 52: freq = freqArray[2]; break;
case 6:
case 18:
case 30:
case 42:
case 54: freq = freqArray[3]; break;
case 8:
case 20:
case 32:
case 44:
case 56: freq = freqArray[4]; break;
default: freq = freqArray[5];
}
UGPS.sendMsg1 = true;
}
}
else
{
// Invalid GPS location
UGPS.Longitude = 0;
UGPS.Latitude = 0;
UGPS.GPS_valid = 0;
}
}
/*********************************************************************************************************************************/
void printGPSData()
{
#if defined(DEVMODE)
Serial.print(F(" Valid Time: ")); Serial.println(gps.time.isValid());
Serial.print(F(" Valid Location: ")); Serial.println(gps.location.isValid());
Serial.print(F(" Time: ")); Serial.print(UGPS.Hours); Serial.print(F(":")); Serial.print(UGPS.Minutes); Serial.print(F(":")); Serial.println(UGPS.Seconds);
Serial.print(F(" Latitude: ")); Serial.println(UGPS.Latitude, 6);
Serial.print(F(" Longitude: ")); Serial.println(UGPS.Longitude, 6);
Serial.print(F(" Satellites: ")); Serial.println(UGPS.Satellites);
Serial.print(F(" Maidenhead: ")); Serial.println(MaidenHead);
Serial.println();
Serial.println(F("-------------------------"));
#endif
}
/*********************************************************************************************************************************/
// Calculate the Maidenhead locator out of GPS coordinates
void Get_MaidenHead()
{
int o1, o2, o3;
int a1, a2, a3;
double remainder;
// longitude
remainder = UGPS.Longitude + 180.0;
o1 = (int)(remainder / 20.0);
remainder = remainder - (double)o1 * 20.0;
o2 = (int)(remainder / 2.0);
remainder = remainder - 2.0 * (double)o2;
o3 = (int)(12.0 * remainder);
// latitude
remainder = UGPS.Latitude + 90.0;
a1 = (int)(remainder / 10.0);
remainder = remainder - (double)a1 * 10.0;
a2 = (int)(remainder);
remainder = remainder - (double)a2;
a3 = (int)(24.0 * remainder);
MaidenHead[0] = (char)o1 + 'A';
MaidenHead[1] = (char)a1 + 'A';
MaidenHead[2] = (char)o2 + '0';
MaidenHead[3] = (char)a2 + '0';
MaidenHead[4] = (char)o3 + 'A';
MaidenHead[5] = (char)a3 + 'A';
MaidenHead[6] = (char)0;
}