-
Notifications
You must be signed in to change notification settings - Fork 0
/
GNSSDemo.cpp
222 lines (182 loc) · 5.76 KB
/
GNSSDemo.cpp
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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
/* USC RPL HAMSTER v2.3 U-Blox GPS Tests
Author: Vivek Ramachandran <[email protected]>
Jamie Smith
Adhyyan Sekhsaria
*/
#include "BarGraph.h"
#include "GNSSDemo.h"
#include "GNSSConstructor.h"
#include <array>
#include <cinttypes>
#include <cmath>
using namespace UBlox;
/**
* @brief Constructs the object.
*/
GNSSDemo::GNSSDemo():
gnss(constructGNSS())
{
bool sensorInitialized = gnss.begin(false);
if(!sensorInitialized)
{
printf("begin(): FAIL!\r\n");
printf(" -> Failed to contact GPS.\r\n");
}
else
{
printf("begin(): PASS\r\n");
gnss.checkVersion(true, false);
}
}
void GNSSDemo::test_readGNSSConfig() {
gnss.printGNSSConfig();
}
void GNSSDemo::test_configure()
{
Timer configTimer;
configTimer.start();
if(gnss.configure())
{
printf("Successfully configured in %" PRIi64 " ms\r\n", configTimer.elapsed_time().count());
}
else
{
printf("Configuration failed.\r\n");
}
}
void GNSSDemo::test_antennaPower() {
Timer measureTimer;
measureTimer.start();
uint8_t powerStat = static_cast<uint8_t>(gnss.getAntennaPowerStatus());
measureTimer.stop();
const char* status[] = {"OFF", "ON", "DK", "FAIL TO COMMUNICATE"};
printf("Read antenna current power status as %s in %" PRIi64 " ms\r\n",
status[powerStat],
measureTimer.elapsed_time().count());
}
#define UPDATE_RATE 1s // seconds
#define TERMINAL_WIDTH 80 // chars
#define MAX_SATELLITES 32
#define GRAPH_HEIGHT 20 // lines
#define VT100_CLEAR_SCREEN "\x1b[2J"
#define VT100_CURSOR_HOME "\x1b[H"
bool shouldSkipSatellite(SatelliteInfo & satellite)
{
return !satellite.svUsed;
}
SatelliteInfo satelliteInfo[MAX_SATELLITES];
uint32_t heights[40];
std::string labels[40][2];
void GNSSDemo::test_HUD()
{
Timer updateTimer;
updateTimer.start();
//initialise bar graph
BarGraph bargraph;
bargraph.setAxis("Carrier to Noise Ratio", "db", "SAT ID");
bargraph.setGraphHeight(40);
bargraph.setLabelRows(2);
bargraph.setScalingValue(0);
bargraph.setHeights(heights);
bargraph.setLabels(&labels[0][0]);
FileHandle *stdin_fh = mbed_file_handle(STDIN_FILENO);
stdin_fh->set_blocking(false);
while(!stdin_fh->readable()) // exit when the user types a char
{
while(updateTimer.elapsed_time() < UPDATE_RATE)
{
ThisThread::sleep_for(1ms);
}
updateTimer.reset();
// get the data we need
gnss.update(200ms);
ssize_t numSatellites = gnss.getSatelliteInfo(satelliteInfo, MAX_SATELLITES);
if(numSatellites < 0)
{
// error reading satellites, assume 0 satellites
numSatellites = 0;
}
// paint the screen
printf(VT100_CLEAR_SCREEN VT100_CURSOR_HOME);
// Title
printf("---------- U-Blox GPS HUD ---------- (press any key to exit)\r\n");
// Position
printf(">Position: %.06f %c, %.06f %c, Height %.02f m\r\n",
std::abs(gnss.position.latitude), gnss.position.latitude > 0 ? 'N' : 'S',
std::abs(gnss.position.longitude), gnss.position.longitude > 0 ? 'E' : 'W',
gnss.position.height / 1000.0f);
// velocity
printf(">Velocity: %.02f m/s N, %.02f m/s E, %.02f m/s Down\r\n",
gnss.velocity.northVel * .02f,
gnss.velocity.eastVel * .02f,
gnss.velocity.downVel * .02f);
// accuracy
printf(">Fix: Quality: %" PRIu8 ", Num Satellites: %" PRIu8 ", Position Accuracy: %" PRIu32 " cm\r\n",
static_cast<uint8_t>(gnss.fixQuality.fixQuality),
gnss.fixQuality.numSatellites,
gnss.fixQuality.posAccuracy);
// time
printf(">Time: %" PRIu8 "/%" PRIu8"/%" PRIu16" %" PRIu8":%" PRIu8 ":%" PRIu8 "\r\n",
gnss.time.month,
gnss.time.day,
gnss.time.year,
gnss.time.hour,
gnss.time.minute,
gnss.time.second);
// graph
int currentIndex = 0;
for(ssize_t index = 0; index < numSatellites; index++){
if(shouldSkipSatellite(satelliteInfo[index])){
continue;
}
heights[currentIndex] = satelliteInfo[index].signalStrength;
labels[currentIndex][0] = std::to_string(satelliteInfo[index].satelliteID);
labels[currentIndex][1] = satelliteInfo[index].getGNSSName();
currentIndex++;
}
bargraph.setNumCols(currentIndex);
bargraph.print();
}
stdin_fh->set_blocking(true);
}
void GNSSDemo::hotStartTest()
{
gnss.softwareReset(UBloxGPS::SWResetType::HOT_START);
gnss.begin(true);
}
#if HAMSTER_SIMULATOR != 1
int main()
#else
int gps_test_main()
#endif
{
//declare the test harness
GNSSDemo harness;
while(1){
int test=-1;
printf("\r\n\nGNSS Demo Test Suite:\r\n");
//MENU. ADD AN OPTION FOR EACH TEST.
printf("Select a test: \n\r");
printf("0. Exit test suite\r\n");
printf("1. Reconfigure GNSS settings (must be done first to get data from GNSSs that have not been configured or those without flash memory)\r\n");
printf("2. Read antenna power stats\r\n");
printf("3. View data in GNSS HUD\r\n");
printf("4. Read satellite system status\r\n");
printf("5. Software reset GNSS (hot start)\r\n");
scanf("%d", &test);
getc(stdin);
printf("Running test %d:\r\n\n", test);
//SWITCH. ADD A CASE FOR EACH TEST.
switch(test) {
case 0: printf("Exiting test suite.\r\n"); return 0;
case 1: harness.test_configure(); break;
case 2: harness.test_antennaPower(); break;
case 3: harness.test_HUD(); break;
case 4: harness.test_readGNSSConfig(); break;
case 5: harness.hotStartTest(); break;
default: printf("Invalid test number. Please run again.\r\n"); break;
}
printf("done.\r\n");
}
return 0;
}