-
Notifications
You must be signed in to change notification settings - Fork 2.3k
/
ubx_m8p.cs
642 lines (538 loc) · 20.3 KB
/
ubx_m8p.cs
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
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
using System;
using uint8_t = System.Byte;
using uint16_t = System.UInt16;
using int32_t = System.Int32;
using uint32_t = System.UInt32;
using int8_t = System.SByte;
using MissionPlanner.Comms;
using System.Runtime.InteropServices;
using System.Threading;
namespace MissionPlanner.Utilities
{
public class Ubx : ICorrections
{
int step = 0;
public byte[] buffer = new byte[1024 * 8];
int payloadlen = 0;
int msglencount = 0;
public byte @class
{
get { return buffer[2]; }
}
public byte subclass
{
get { return buffer[3]; }
}
public int length
{
get
{
return 2 + 2 + 2 + 2 + payloadlen; // header2, class,subclass,length2,data,crc2
}
}
public byte[] packet
{
get { return buffer; }
}
public bool resetParser()
{
step = 0;
return true;
}
public int Read(byte data)
{
switch (step)
{
default:
case 0:
if (data == 0xb5)
{
step = 1;
buffer[0] = data;
}
break;
case 1:
if (data == 0x62)
{
buffer[1] = data;
step++;
}
else
step = 0;
break;
case 2:
buffer[2] = data;
step++;
break;
case 3:
buffer[3] = data;
step++;
break;
case 4:
buffer[4] = data;
payloadlen = data;
step++;
break;
case 5:
buffer[5] = data;
step++;
payloadlen += (data << 8);
msglencount = 0;
// reset on oversize packet
if (payloadlen > buffer.Length)
step = 0;
break;
case 6:
if (msglencount < (payloadlen))
{
buffer[msglencount + 6] = data;
msglencount++;
if (msglencount == payloadlen)
step++;
}
break;
case 7:
buffer[msglencount + 6] = data;
step++;
break;
case 8:
buffer[msglencount + 6 + 1] = data;
var crc = ubx_checksum(buffer, payloadlen + 6);
var crcpacket = new byte[] {buffer[msglencount + 6], data};
if (crc[0] == crcpacket[0] && crc[1] == crcpacket[1])
{
step = 0;
return (@class << 8) + subclass;
}
step = 0;
break;
}
return -1;
}
private static byte[] ubx_checksum(byte[] packet, int size, int offset = 2)
{
uint a = 0x00;
uint b = 0x00;
var i = offset;
while (i < size)
{
a += packet[i++];
b += a;
}
var ans = new byte[2];
ans[0] = (byte) (a & 0xFF);
ans[1] = (byte) (b & 0xFF);
return ans;
}
public static byte[] generate(byte cl, byte subclass, byte[] payload)
{
var data = new byte[2 + 2 + 2 + 2 + payload.Length];
data[0] = 0xb5;
data[1] = 0x62;
data[2] = cl;
data[3] = subclass;
data[4] = (byte) (payload.Length & 0xff);
data[5] = (byte) ((payload.Length >> 8) & 0xff);
Array.ConstrainedCopy(payload, 0, data, 6, payload.Length);
var checksum = ubx_checksum(data, data.Length - 2);
data[data.Length - 2] = checksum[0];
data[data.Length - 1] = checksum[1];
return data;
}
[StructLayout(LayoutKind.Sequential, Pack = 1)]
public struct ubx_mon_ver
{
[MarshalAs(UnmanagedType.ByValArray, SizeConst = 30)]
public byte[] swVersion;
[MarshalAs(UnmanagedType.ByValArray, SizeConst = 10)]
public Byte[] hwVersion;
[MarshalAs(UnmanagedType.ByValArray, SizeConst = 30)]
public byte[] extension;
}
[StructLayout(LayoutKind.Sequential, Pack = 1)]
public struct ubx_mon_hw
{
public int pinSel;
public int pinBank;
public int pinDir;
public int pinVal;
public ushort noisePerMS;
public ushort agcCnt;
public byte aStatus;
public byte aPower;
public byte flags;
public byte reserved1;
public int usedMask;
[MarshalAs(UnmanagedType.ByValArray, SizeConst = 17)]
public byte[] VP;
public byte jamInd;
public ushort reserved3;
public int pinIrq;
public int pullH;
public int pullL;
}
[StructLayout(LayoutKind.Sequential, Pack = 1)]
public struct ubx_nav_pvt
{
public uint32_t itow;
public uint16_t year;
public uint8_t month, day, hour, min, sec;
public uint8_t valid;
public uint32_t t_acc;
public int32_t nano;
public uint8_t fix_type;
public uint8_t flags;
public uint8_t flags2;
public uint8_t num_sv;
public int32_t lon, lat;
public int32_t height, h_msl;
public uint32_t h_acc, v_acc;
public int32_t velN, velE, velD, gspeed;
public int32_t head_mot;
public uint32_t s_acc;
public uint32_t head_acc;
public uint16_t p_dop;
[MarshalAs(UnmanagedType.ByValArray, SizeConst = 6)]
public uint8_t[] reserved1;
public uint32_t headVeh;
[MarshalAs(UnmanagedType.ByValArray, SizeConst = 4)]
public uint8_t[] reserved2;
}
[StructLayout(LayoutKind.Sequential, Pack = 1)]
public struct ubx_nav_svin
{
public uint8_t version;
[MarshalAs(UnmanagedType.ByValArray, SizeConst = 3)]
public uint8_t[] reserved1;
public uint32_t iTOW;
public uint32_t dur;
public int32_t meanX;
public int32_t meanY;
public int32_t meanZ;
public int8_t meanXHP;
public int8_t meanYHP;
public int8_t meanZHP;
public uint8_t reserved2;
public uint32_t meanAcc;
public uint32_t obs;
public uint8_t valid;
public uint8_t active;
[MarshalAs(UnmanagedType.ByValArray, SizeConst = 2)]
public uint8_t[] reserved3;
public double[] getECEF()
{
var X = meanX / 100.0 + meanXHP * 0.0001;
var Y = meanY / 100.0 + meanYHP * 0.0001;
var Z = meanZ / 100.0 + meanZHP * 0.0001;
return new double[] {X, Y, Z};
}
}
[StructLayout(LayoutKind.Sequential, Pack = 1)]
public struct ubx_nav_velned
{
public uint iTOW;
public int velN;
public int velE;
public int velD;
public uint speed;
public uint gSpeed;
public int heading;
public uint sAcc;
public uint cAcc;
}
[StructLayout(LayoutKind.Sequential, Pack = 1)]
public struct ubx_cfg_nav5_getset_s
{
public uint16_t mask;
public uint8_t dynModel;
public uint8_t fixMode;
public int32_t fixedAlt;
public uint32_t fixedAltVar;
public int8_t minElev;
public uint8_t drLimit;
public uint16_t pDop;
public uint16_t tDop;
public uint16_t pAcc;
public uint16_t tAcc;
public uint8_t staticHoldThresh;
public uint8_t dgnssTimeout;
public uint8_t cnoThreshNumSVs;
public uint8_t cnoThresh;
[MarshalAs(UnmanagedType.ByValArray, SizeConst = 2)]
public uint8_t[] reserved1;
public uint16_t staticHoldMaxDist;
public uint8_t utcStandard;
[MarshalAs(UnmanagedType.ByValArray, SizeConst = 5)]
public uint8_t[] reserved2;
}
[StructLayout(LayoutKind.Sequential, Pack = 1, Size = 40)]
public struct ubx_cfg_tmode3
{
public ubx_cfg_tmode3(double lat, double lng, double alt, double acc = 0.001)
{
version = 0;
reserved1 = 0;
if (Math.Abs(lat) > 90)
{
flags = 2; // fixed mode ecef
ecefXorLat = (int) (lat * 100);
ecefYorLon = (int) (lng * 100);
ecefZorAlt = (int) (alt * 100);
ecefXOrLatHP = (sbyte) ((lat * 100 - ecefXorLat) * 100.0);
ecefYOrLonHP = (sbyte) ((lng * 100 - ecefYorLon) * 100.0);
ecefZOrAltHP = (sbyte) ((alt * 100 - ecefZorAlt) * 100.0);
}
else
{
flags = 256 + 2; // lla + fixed mode
ecefXorLat = (int) (lat * 1e7);
ecefYorLon = (int) (lng * 1e7);
ecefZorAlt = (int) (alt * 100.0);
ecefXOrLatHP = (sbyte) ((lat * 1e7 - ecefXorLat) * 100.0);
ecefYOrLonHP = (sbyte) ((lng * 1e7 - ecefYorLon) * 100.0);
ecefZOrAltHP = (sbyte) ((alt * 100.0 - ecefZorAlt) * 100.0);
}
reserved2 = 0;
fixedPosAcc = (uint) (acc * 1000.0);
svinMinDur = 60;
svinAccLimit = 2000;
reserved3 = new byte[8];
}
public ubx_cfg_tmode3(uint DurationS, double AccLimit)
{
version = 0;
reserved1 = 0;
flags = 1; // surveyin mode
ecefXorLat = 0;
ecefYorLon = 0;
ecefZorAlt = 0;
ecefXOrLatHP = (sbyte) 0;
ecefYOrLonHP = (sbyte) 0;
ecefZOrAltHP = (sbyte) 0;
reserved2 = 0;
fixedPosAcc = 0;
svinMinDur = DurationS;
svinAccLimit = (uint) (AccLimit * 10000);
reserved3 = new byte[8];
}
public static ubx_cfg_tmode3 Disable
{
get
{
return new ubx_cfg_tmode3()
{
flags = 0, // disable
reserved3 = new byte[8]
};
}
}
public static implicit operator byte[](ubx_cfg_tmode3 input)
{
return MavlinkUtil.StructureToByteArray(input);
}
public enum modeflags
{
Disabled = 0,
SurveyIn = 1,
FixedECEF = 2,
LLA = 256,
FixedLLA = 258
}
public byte version;
public byte reserved1;
public ushort flags;
public int ecefXorLat; // 1e7
public int ecefYorLon;
public int ecefZorAlt;
public sbyte ecefXOrLatHP;
public sbyte ecefYOrLonHP;
public sbyte ecefZOrAltHP;
public byte reserved2;
public uint fixedPosAcc;
public uint svinMinDur;
public uint svinAccLimit;
[MarshalAs(UnmanagedType.ByValArray, SizeConst = 8)]
public byte[] reserved3;
public PointLatLngAlt getPointLatLngAlt()
{
if (flags == 2)
{
var X = ecefXorLat / 100.0 + ecefXOrLatHP * 0.0001;
var Y = ecefYorLon / 100.0 + ecefYOrLonHP * 0.0001;
var Z = ecefZorAlt / 100.0 + ecefZOrAltHP * 0.0001;
var pos = new double[] {X, Y, Z};
return new PointLatLngAlt(pos);
}
else if (flags == 258)
{
var X = ecefXorLat / 1e7 + ecefXOrLatHP / 1e9;
var Y = ecefYorLon / 1e7 + ecefYOrLonHP / 1e9;
var Z = ecefZorAlt / 100.0 + ecefZOrAltHP * 0.0001;
var pos = new double[] {X, Y, Z};
return new PointLatLngAlt(pos);
}
return null;
}
}
public void SetupM8P(ICommsSerial port, bool m8p_130plus = false, bool movingbase = false)
{
port.BaseStream.Flush();
var bauds = new[] {port.BaudRate, 9600, 38400, 57600, 115200, 230400, 460800};
// change the baudrate
foreach (var baud in bauds)
{
port.BaudRate = baud;
System.Threading.Thread.Sleep(50);
// U = bit 01010101 - often used for autobaud
port.Write("UU");
// port config - 460800 - uart1
var packet = generate(0x6, 0x00, new byte[]
{
0x01, 0x00, 0x00, 0x00, 0xD0, 0x08, 0x00, 0x00, 0x00, 0x08,
0x07, 0x00, 0x23, 0x00, 0x23, 0x00, 0x00, 0x00, 0x00, 0x00
});
port.Write(packet, 0, packet.Length);
port.BaseStream.Flush();
System.Threading.Thread.Sleep(100);
}
{
// port config - usb
var packet = generate(0x6, 0x00, new byte[]
{
0x03, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x23, 0x00, 0x23, 0x00, 0x00, 0x00, 0x00, 0x00
});
port.Write(packet, 0, packet.Length);
System.Threading.Thread.Sleep(300);
port.BaseStream.Flush();
port.BaudRate = 460800;
}
{
// set rate to 1hz
var packet = generate(0x6, 0x8, new byte[] {0xE8, 0x03, 0x01, 0x00, 0x01, 0x00});
port.Write(packet, 0, packet.Length);
System.Threading.Thread.Sleep(200);
// set navmode to stationary
{
packet = generate(0x6, 0x24,
new byte[]
{
0xFF, 0xFF, 0x02, 0x03, 0x00, 0x00, 0x00, 0x00, 0x10, 0x27, 0x00, 0x00, 0x0F, 0x00, 0xFA,
0x00,
0xFA, 0x00, 0x64, 0x00, 0x2C, 0x01, 0x00, 0x00, 0x00, 0x23, 0x10, 0x27, 0x00, 0x00, 0x00,
0x00,
0x00, 0x00, 0x00, 0x00
});
port.Write(packet, 0, packet.Length);
System.Threading.Thread.Sleep(200);
}
}
// turn off all nmea
for (int a = 0; a <= 0xf; a++)
{
if (a == 0xb || a == 0xc || a == 0xe)
continue;
turnon_off(port, 0xf0, (byte) a, 0);
}
// mon-ver
poll_msg(port, 0xa, 0x4);
// surveyin msg - for feedback
turnon_off(port, 0x01, 0x3b, 1);
// pvt msg - for feedback
turnon_off(port, 0x01, 0x07, 1);
// 1005 - 5s
turnon_off(port, 0xf5, 0x05, 5);
byte rate1 = 1;
byte rate2 = 0;
if (m8p_130plus)
{
rate1 = 0;
rate2 = 1;
}
// 1074 - 1s
turnon_off(port, 0xf5, 0x4a, rate2);
// 1077 - 1s
turnon_off(port, 0xf5, 0x4d, rate1);
// 1084 - 1s
turnon_off(port, 0xf5, 0x54, rate2);
// 1087 - 1s
turnon_off(port, 0xf5, 0x57, rate1);
// 1094 - 1s
turnon_off(port, 0xf5, 0x5e, rate2);
// 1097 - 1s
turnon_off(port, 0xf5, 0x61, rate1);
// 1124 - 1s
turnon_off(port, 0xf5, 0x7c, rate2);
// 1127 - 1s
turnon_off(port, 0xf5, 0x7f, rate1);
// 4072
turnon_off(port, 0xf5, 0xFE, 0);
// 1230 - 5s
turnon_off(port, 0xf5, 0xE6, 5);
// NAV-VELNED - 1s
turnon_off(port, 0x01, 0x12, 1);
// rxm-raw/rawx - 1s
turnon_off(port, 0x02, 0x15, 1);
turnon_off(port, 0x02, 0x10, 1);
// rxm-sfrb/sfrb - 2s
turnon_off(port, 0x02, 0x13, 2);
turnon_off(port, 0x02, 0x11, 2);
// mon-hw - 2s
turnon_off(port, 0x0a, 0x09, 2);
System.Threading.Thread.Sleep(100);
}
public void SetupBasePos(ICommsSerial port, PointLatLngAlt basepos, int surveyindur = 0, double surveyinacc = 0,
bool disable = false, bool movingbase = false)
{
if (movingbase)
disable = true;
System.Threading.Thread.Sleep(100);
System.Threading.Thread.Sleep(100);
if (surveyindur == 0)
surveyindur = 60;
if (surveyinacc == 0)
surveyinacc = 2;
if (disable)
{
// disable
var packet = generate(0x6, 0x71, ubx_cfg_tmode3.Disable);
port.Write(packet, 0, packet.Length);
// save - bbr
packet = generate(0x06, 0x09, new uint8_t[] {0, 0, 0, 0, 0xff, 0xff, 0, 0, 0, 0, 0, 0, 0x01});
port.Write(packet, 0, packet.Length);
Thread.Sleep(1000);
//reboot
packet = generate(0x06, 0x04, new uint8_t[] {0x14, 0xff, 2, 0});
port.Write(packet, 0, packet.Length);
Thread.Sleep(3000);
return;
}
if (basepos == PointLatLngAlt.Zero)
{
// survey in config
var packet = generate(0x6, 0x71, new ubx_cfg_tmode3((uint) surveyindur, surveyinacc));
port.Write(packet, 0, packet.Length);
}
else
{
byte[] data = new ubx_cfg_tmode3(basepos.Lat, basepos.Lng, basepos.Alt);
var packet = generate(0x6, 0x71, data);
port.Write(packet, 0, packet.Length);
}
}
public void turnon_off(ICommsSerial port, byte clas, byte subclass, byte every_xsamples)
{
byte[] datastruct1 = {clas, subclass, 0, every_xsamples, 0, every_xsamples, 0, 0};
var packet = generate(0x6, 0x1, datastruct1);
port.Write(packet, 0, packet.Length);
System.Threading.Thread.Sleep(10);
}
public void poll_msg(ICommsSerial port, byte clas, byte subclass)
{
byte[] datastruct1 = { };
var packet = generate(clas, subclass, datastruct1);
port.Write(packet, 0, packet.Length);
System.Threading.Thread.Sleep(10);
}
}
}