-
Notifications
You must be signed in to change notification settings - Fork 23
/
formgps_udpcomm.cpp
333 lines (277 loc) · 10.4 KB
/
formgps_udpcomm.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
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
#include <QNetworkDatagram>
#include "formgps.h"
#include "aogproperty.h"
#include "cnmea.h"
#include <QHostAddress>
#define BitConverter_ToDouble(data,position) (*(reinterpret_cast<double *>(&(data[position]))))
#define BitConverter_ToSingle(data,position) (*(reinterpret_cast<float *>(&(data[position]))))
#define BitConverter_ToUInt16(data,position) (*(reinterpret_cast<quint16 *>(&(data[position]))))
#define BitConverter_ToInt16(data,position) (*(reinterpret_cast<qint16 *>(&(data[position]))))
#define UDP_NMEA_PORT 9999
//const QHostAddress epAgIO = QHostAddress("127.0.0.1"); // TODO for everything good
const QHostAddress epAgIO = QHostAddress("127.255.255.255"); //TODO for windows
const int epAgIO_port = 17777;
void FormGPS::ReceiveFromAgIO()
{
double head253, rollK, Lat, Lon;
/* this runs in the main GUI thread, it won't ever run at the
* exact same time as the timer callback, so we can safely just
* append to the rawBuffer, even if it's about to be processed
* by the parser.
*/
QByteArray datagram_data;
//TODO: is this right or should we just work with one datagram at a time?
datagram_data = udpSocket->receiveDatagram().data();
while (udpSocket->hasPendingDatagrams()) {
//receive all the data
datagram_data.append(udpSocket->receiveDatagram().data());
}
char *data = datagram_data.data();
if (datagram_data.length() > 4 && data[0] == (char)0x80 && data[1] == (char)0x81)
{
int Length = max((data[4]) + 5, 5);
if (datagram_data.length() > Length)
{
char CK_A = 0;
for (int j = 2; j < Length; j++)
{
CK_A += data[j];
}
if (data[Length] != (char)CK_A)
{
return;
}
}
else
{
return;
}
switch ((uchar)data[3])
{
case 0xD6:
if (udpWatch.elapsed() < udpWatchLimit)
{
udpWatchCounts++;
//TODO implement nmea logging
/*
if (isLogNMEA) pn.logNMEASentence.Append("*** "
+ DateTime.UtcNow.ToString("ss.ff -> ", CultureInfo.InvariantCulture)
+ udpWatch.ElapsedMilliseconds + "\r\n");
*/
return;
}
udpWatch.restart();
Lon = BitConverter_ToDouble(data, 5);
Lat = BitConverter_ToDouble(data, 13);
if (Lon != glm::DOUBLE_MAX && Lat != glm::DOUBLE_MAX)
{
if (timerSim.isActive())
DisableSim();
pn.longitude = Lon;
pn.latitude = Lat;
pn.ConvertWGS84ToLocal(Lat, Lon, pn.fix.northing, pn.fix.easting);
//From dual antenna heading sentences
float temp = BitConverter_ToSingle(data, 21);
if (temp != glm::DOUBLE_MAX)
{
pn.headingTrueDual = temp + pn.headingTrueDualOffset;
if (pn.headingTrueDual >= 360) pn.headingTrueDual -= 360;
else if (pn.headingTrueDual < 0) pn.headingTrueDual += 360;
if (ahrs.isDualAsIMU) ahrs.imuHeading = pn.headingTrueDual;
}
//from single antenna sentences (VTG,RMC)
pn.headingTrue = BitConverter_ToSingle(data, 25);
//always save the speed.
temp = BitConverter_ToSingle(data, 29);
if (temp != glm::FLOAT_MAX)
{
pn.vtgSpeed = temp;
}
//roll in degrees
temp = BitConverter_ToSingle(data, 33);
if (temp != glm::FLOAT_MAX)
{
if (ahrs.isRollInvert) temp *= -1;
ahrs.imuRoll = temp - ahrs.rollZero;
}
if (temp == glm::FLOAT_MIN)
ahrs.imuRoll = 0;
//altitude in meters
temp = BitConverter_ToSingle(data, 37);
if (temp != glm::FLOAT_MAX)
pn.altitude = temp;
ushort sats = BitConverter_ToUInt16(data, 41);
if (sats != glm::USHORT_MAX)
pn.satellitesTracked = sats;
char fix = data[43];
if (fix != glm::BYTE_MAX)
pn.fixQuality = fix;
ushort hdop = BitConverter_ToUInt16(data, 44);
if (hdop != glm::USHORT_MAX)
pn.hdop = hdop * 0.01;
ushort age = BitConverter_ToUInt16(data, 46);
if (age != glm::USHORT_MAX)
pn.age = age * 0.01;
ushort imuHead = BitConverter_ToUInt16(data, 48);
if (imuHead != glm::USHORT_MAX)
{
ahrs.imuHeading = imuHead;
ahrs.imuHeading *= 0.1;
}
short imuRol = BitConverter_ToInt16(data, 50);
if (imuRol != glm::SHORT_MAX)
{
rollK = imuRol;
if (ahrs.isRollInvert) rollK *= -0.1;
else rollK *= 0.1;
rollK -= ahrs.rollZero;
ahrs.imuRoll = ahrs.imuRoll * ahrs.rollFilter + rollK * (1 - ahrs.rollFilter);
}
short imuPich = BitConverter_ToInt16(data, 52);
if (imuPich != glm::SHORT_MAX)
{
ahrs.imuPitch = imuPich;
}
short imuYaw = BitConverter_ToInt16(data, 54);
if (imuYaw != glm::SHORT_MAX)
{
ahrs.imuYawRate = imuYaw;
}
sentenceCounter = 0;
//TODO: implement logging
/*
if (isLogNMEA)
pn.logNMEASentence.Append(
DateTime.UtcNow.ToString("mm:ss.ff",CultureInfo.InvariantCulture)+ " " +
Lat.ToString("N7") + " " + Lon.ToString("N7") );
*/
UpdateFixPosition();
}
break;
case 0xD3: //external IMU
if (datagram_data.length() != 14)
break;
if (ahrs.imuRoll > 25 || ahrs.imuRoll < -25) ahrs.imuRoll = 0;
//Heading
ahrs.imuHeading = (qint16)((data[6] << 8) + data[5]);
ahrs.imuHeading *= 0.1;
//Roll
rollK = (qint16)((data[8] << 8) + data[7]);
if (ahrs.isRollInvert) rollK *= -0.1;
else rollK *= 0.1;
rollK -= ahrs.rollZero;
ahrs.imuRoll = ahrs.imuRoll * ahrs.rollFilter + rollK * (1 - ahrs.rollFilter);
//Angular velocity
ahrs.angVel = (qint16)((data[10] << 8) + data[9]);
ahrs.angVel /= -2;
break;
case 0xD4: //imu disconnect pgn
if (data[5] == (char)1)
{
ahrs.imuHeading = 99999;
ahrs.imuRoll = 88888;
ahrs.angVel = 0;
}
break;
case 253: //return from autosteer module
//Steer angle actual
if (datagram_data.length() != 14)
break;
mc.actualSteerAngleChart = (qint16)((data[6] << 8) + data[5]);
mc.actualSteerAngleDegrees = (double)mc.actualSteerAngleChart * 0.01;
//Heading
head253 = (qint16)((data[8] << 8) + data[7]);
if (head253 != 9999)
{
ahrs.imuHeading = head253 * 0.1;
}
//Roll
rollK = (qint16)((data[10] << 8) + data[9]);
if (rollK != 8888)
{
if (ahrs.isRollInvert) rollK *= -0.1;
else rollK *= 0.1;
rollK -= ahrs.rollZero;
ahrs.imuRoll = ahrs.imuRoll * ahrs.rollFilter + rollK * (1 - ahrs.rollFilter);
}
//else ahrs.imuRoll = 88888;
//switch status
mc.workSwitchHigh = (data[11] & 1) == (char)1;
mc.steerSwitchHigh = (data[11] & 2) == (char)2;
//the pink steer dot reset
steerModuleConnectedCounter = 0;
//Actual PWM
mc.pwmDisplay = data[12];
//TODO implement NMEA logging
/*
if (isLogNMEA)
pn.logNMEASentence.Append(
DateTime.UtcNow.ToString("mm:ss.ff", CultureInfo.InvariantCulture) + " AS " +
mc.actualSteerAngleDegrees.ToString("N1") + "\r\n"
);
*/
break;
case 250:
if (datagram_data.length() != 14)
break;
mc.sensorData = data[5];
break;
case 234://MTZ8302 Feb 2020
//Steer angle actual
if (datagram_data.length() != 14)
break;
for (int i=0; i < 8 ; i++)
mc.ss[i] = data[i+5];
DoRemoteSwitches();
break;
}
}
//qDebug() << pn->rawBuffer ;
}
void FormGPS::DisableSim()
{
isFirstFixPositionSet = false;
isGPSPositionInitialized = false;
isFirstHeadingSet = false;
startCounter = 0;
//TODO, turn off sim in UI
//panelSim.Visible = false;
//timerSim.Enabled = false;
//simulatorOnToolStripMenuItem.Checked = false;
property_setMenu_isSimulatorOn = false;
//TODO, do we need to save explicitly?
//Properties.Settings.Default.Save();
return;
}
void FormGPS::StartLoopbackServer()
{
AOGSettings s;
int port = 15555; //property?
if(udpSocket) stopUDPServer();
udpSocket = new QUdpSocket(this); //should auto delete with the form
udpSocket->bind(QHostAddress::Any, port); //by default, bind to all interfaces.
//TODO: change to localhost
connect(udpSocket,SIGNAL(readyRead()),this,SLOT(ReceiveFromAgIO()));
udpWatch.restart();
}
void FormGPS::stopUDPServer()
{
if(udpSocket) {
udpSocket->close();
delete udpSocket;
udpSocket = NULL;
}
}
void FormGPS::SendPgnToLoop(QByteArray byteData) //10 bytes
{
if (udpSocket != NULL && byteData.length() >2)
{
int crc = 0;
for (int i = 2; i + 1 < byteData.length(); i++)
{
crc += byteData[i];
}
byteData[byteData.length() - 1] = (char)crc;
udpSocket->writeDatagram(byteData,epAgIO,epAgIO_port);
}
}