forked from JanuszBedkowski/mandeye_controller
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgnss.cpp
More file actions
189 lines (173 loc) · 5.19 KB
/
gnss.cpp
File metadata and controls
189 lines (173 loc) · 5.19 KB
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
#include "gnss.h"
#include <iostream>
#include <thread>
#include <exception>
#include "minmea.h"
namespace mandeye
{
//! Removes all non-printable characters from the line, replacing them with the format <0xXX>, where XX is the hexadecimal value of the character.
std::string sanitizeLine(const std::string& line)
{
std::ostringstream sanitizedLine;
for (char c : line)
{
if (std::isprint(c) || std::isspace(c))
{
sanitizedLine << c;
}
else
{
char sanitizedChar[12];
snprintf(sanitizedChar, sizeof(sanitizedChar), "<0x%02X>", static_cast<unsigned char>(c));
sanitizedLine << sanitizedChar;
}
}
return sanitizedLine.str();
}
nlohmann::json GNSSClient::produceStatus()
{
nlohmann::json data;
data["init_success"] = init_succes;
std::lock_guard<std::mutex> lock(m_bufferMutex);
data["nmea"]["last_line"] = sanitizeLine(m_lastLine);
data["gga"]["time"]["h"] = lastGGA.time.hours;
data["gga"]["time"]["m"] = lastGGA.time.minutes;
data["gga"]["time"]["s"] = lastGGA.time.seconds;
data["gga"]["fix_quality"] = lastGGA.fix_quality;
data["gga"]["satellites_tracked"] = lastGGA.satellites_tracked;
data["gga"]["latitude"] = minmea_tocoord(&lastGGA.latitude);
data["gga"]["longitude"] = minmea_tocoord(&lastGGA.longitude);
data["gga"]["hdop"] = minmea_tofloat(&lastGGA.hdop);
data["gga"]["satellites_tracked"] = lastGGA.satellites_tracked;
data["gga"]["altitude"] = minmea_tofloat(&lastGGA.altitude);
data["gga"]["height"] = minmea_tofloat(&lastGGA.height);
data["gga"]["dgps_age"] = minmea_tofloat(&lastGGA.dgps_age);
data["is_logging"] = m_isLogging;
data["message_count"] = m_messageCount.load();
data["buffer_size"] = m_buffer.size();
return data;
}
bool GNSSClient::startListener(const std::string& portName, LibSerial::BaudRate baudRate) {
try
{
if (init_succes)
{
return true;
}
if (m_serialPort.IsOpen())
{
m_serialPort.Close();
}
m_serialPort.Open(portName, std::ios_base::in);
m_serialPort.SetBaudRate(baudRate);
init_succes = true;
m_serialPortThread = std::thread(&GNSSClient::worker, this);
}catch(std::exception& e)
{
std::cout << "Failed to open port " << portName <<" : " << e.what() << std::endl;
init_succes = false;
return false;
}
return true;
}
void GNSSClient::worker()
{
std::cout << "Worker started" << std::endl;
while(m_serialPort.IsOpen())
{
std::string line;
m_serialPort.ReadLine(line);
{
std::lock_guard<std::mutex> lock(m_bufferMutex);
m_lastLine = line;
}
bool is_vaild = minmea_check(line.c_str(), true);
if (is_vaild)
{
const double laserTimestamp = GetTimeStamp();
m_rawbuffer.emplace_back(RawEntryToLine(line, GetTimeStamp()));
minmea_sentence_gga gga;
bool isGGA = minmea_parse_gga(&gga, line.c_str());
if (isGGA)
{
if (m_dataCallback)
{
m_dataCallback(gga);
}
std::string csvline = GgaToCsvLine(gga, laserTimestamp);
std::lock_guard<std::mutex> lock(m_bufferMutex);
std::swap(m_lastLine, line);
lastGGA = gga;
m_messageCount++;
if(m_isLogging)
{
m_buffer.emplace_back(csvline);
}
}
}
else
{
std::cout << "Invalid line: " << line << std::endl;
}
}
}
void GNSSClient::startLog() {
std::lock_guard<std::mutex> lock(m_bufferMutex);
m_buffer.clear();
m_rawbuffer.clear();
m_isLogging = true;
}
void GNSSClient::stopLog() {
std::lock_guard<std::mutex> lock(m_bufferMutex);
m_isLogging = false;
}
std::deque<std::string> GNSSClient::retrieveData()
{
std::lock_guard<std::mutex> lock(m_bufferMutex);
std::deque<std::string> ret;
std::swap(ret, m_buffer);
return ret;
}
std::deque<std::string> GNSSClient::retrieveRawData()
{
std::lock_guard<std::mutex> lock(m_bufferMutex);
std::deque<std::string> ret;
std::swap(ret, m_rawbuffer);
return ret;
}
//! Convert a minmea_sentence_gga to a CSV line
std::string GNSSClient::GgaToCsvLine(const minmea_sentence_gga& gga, double laserTimestamp)
{
auto now = std::chrono::system_clock::now();
auto duration = now.time_since_epoch();
auto millis = std::chrono::duration_cast<std::chrono::milliseconds>(duration);
std::stringstream oss;
oss << std::setprecision(20) << static_cast<uint_least64_t>(laserTimestamp * 1000000000.0) << " ";
oss << minmea_tocoord(&gga.latitude) << " ";
oss << minmea_tocoord(&gga.longitude) << " ";
oss << minmea_tofloat(&gga.altitude) << " ";
oss << minmea_tofloat(&gga.hdop) << " ";
oss << gga.satellites_tracked << " ";
oss << minmea_tofloat(&gga.height) << " ";
oss << minmea_tofloat(&gga.dgps_age) << " ";
oss << gga.time.hours << ":" << gga.time.minutes << ":" << gga.time.seconds << " ";
oss << gga.fix_quality << " ";
oss << millis.count() << "\n";
return oss.str();
}
std::string GNSSClient::RawEntryToLine(const std::string& line, double laserTimestamp)
{
auto now = std::chrono::system_clock::now();
auto duration = now.time_since_epoch();
auto millis = std::chrono::duration_cast<std::chrono::milliseconds>(duration);
std::stringstream oss;
oss << std::setprecision(20) << static_cast<uint_least64_t>(laserTimestamp * 1000000000.0) << " ";
oss << millis.count() << " ";
oss << line << " ";
return oss.str();
}
void GNSSClient::setDataCallback(const std::function<void(const minmea_sentence_gga& gga)>& callback)
{
m_dataCallback = callback;
}
} // namespace mandeye