-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathNMEA0183Handlers.cpp
1897 lines (1747 loc) · 71.6 KB
/
NMEA0183Handlers.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
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
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
NMEA0183Handlers.cpp
2015 Copyright (c) Kave Oy, www.kave.fi All right reserved.
Author: Timo Lappalainen
This library is free software; you can redistribute it and/or
modify it as you like.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
*/
#include <N2kMsg.h>
#include <NMEA2000.h>
#include <N2kMessages.h>
#include <NMEA0183Messages.h>
#include "NMEA0183Handlers.h"
#include "AIS.h"
#define pi 3.1415926535897932384626433832795
#define kmhToms 1000.0 / 3600.0
#define knToms 1852.0 / 3600.0
#define degToRad pi / 180.0
#define radToDeg 180.0 / pi
#define msTokmh 3600.0 / 1000.0
#define msTokn 3600.0 / 1852.0
#define nmTom 1.852 * 1000
#define mToFathoms 0.546806649
#define mToFeet 3.2808398950131
extern tNMEA0183 NMEA0183;
extern tNMEA0183 NMEA0183_1;
extern tNMEA0183 NMEA0183_3;
extern Stream* NMEA0183HandlersDebugStream;
extern unsigned long NMEA0183TxCounter;
extern unsigned long NMEA0183RxCounter;
extern unsigned long N2kTxCounter;
extern unsigned long N2kRxCounter;
struct tNMEA0183Handler {
const char *Code;
void (*Handler)(const tNMEA0183Msg &NMEA0183Msg);
const char *NMEA0183seq;
};
// Predefinition for functions to make it possible for constant definition for NMEA0183Handlers
void HandleRMB(const tNMEA0183Msg &NMEA0183Msg);
void HandleRMC(const tNMEA0183Msg &NMEA0183Msg);
void HandleGGA(const tNMEA0183Msg &NMEA0183Msg);
void HandleHDT(const tNMEA0183Msg &NMEA0183Msg);
void HandleVTG(const tNMEA0183Msg &NMEA0183Msg);
void HandleVDM(const tNMEA0183Msg &NMEA0183Msg);
void HandleXTE(const tNMEA0183Msg &NMEA0183Msg);
void HandleZTG(const tNMEA0183Msg &NMEA0183Msg);
void HandleXXX(const tNMEA0183Msg &NMEA0183Msg);
// Internal variables
tNMEA2000 *pNMEA2000=0;
tBoatData *pBD=0;
tNMEA0183Handler NMEA0183Handlers[]={
{ "APA", &HandleXXX, "Autopilot Sentence - A" },
{ "APB", &HandleXXX, "Autopilot Sentence - B" },
{ "BOD", &HandleXXX, "Bearing Origin to Destination" },
{ "BWC", &HandleXXX, "Bearing and Distance to Waypoint" },
{ "GGA", &HandleGGA, "Global Positioning System Fix Data, Time, Position and fix related data fora GPS receiver" },
{ "HDT", &HandleHDT, "Heading - True" },
{ "MWV", &HandleXXX, "Wind Speed and Angle" },
{ "RMB", &HandleRMB, "Recommended Minimum Navigation Information - B" },
{ "RMC", &HandleRMC, "Recommended Minimum Navigation Information - C" },
{ "VDM", &HandleVDM, "AIS VHF Datalink Message" },
// { "VDO", &HandleVDM, "AIS VHF Datalink Own-vessel Message"},
{ "VTG", &HandleVTG, "Track made good and Ground speed" },
{ "WPL", &HandleXXX, "Waypoint Location" },
{ "XTE", &HandleXTE, "Cross-Track Error" },
{ "ZTG", &HandleZTG, "UTC & Time to Destination Waypoint" },
{0,0}
};
/*****************************************************************************
APA - Autopilot Sentence "A"
1 2 3 4 5 6 7 8 9 10 11
| | | | | | | | | | |
$--APA,A,A,x.xx,L,N,A,A,xxx,M,c-- - c*hh<CR><LF>
Field Number :
1) Status
V = LORAN - C Blink or SNR warning
V = general warning flag or other navigation systems when a reliable fix is not available
2) Status
V = Loran - C Cycle Lock warning flag
A = OK or not used
3) Cross Track Error Magnitude
4) Direction to steer, L or R
5) Cross Track Units(Nautic miles or kilometers)
6) Status
A = Arrival Circle Entered
7) Status
A = Perpendicular passed at waypoint
8) Bearing origin to destination
9) M = Magnetic, T = True
10) Destination Waypoint ID
11) checksum
*/
/*****************************************************************************
APB - Autopilot Sentence "B"
13 15
1 2 3 4 5 6 7 8 9 10 11 12| 14|
| | | | | | | | | | | | | | |
$--APB,A,A,x.x,a,N,A,A,x.x,a,c--c,x.x,a,x.x,a*hh<CR><LF>
Field Number :
1) Status
V = LORAN - C Blink or SNR warning
V = general warning flag or other navigation systems when a reliable fix is not available
2) Status
V = Loran - C Cycle Lock warning flag
A = OK or not used
3) Cross Track Error Magnitude
4) Direction to steer, L or R
5) Cross Track Units, N = Nautical Miles
6) Status
A = Arrival Circle Entered
7) Status
A = Perpendicular passed at waypoint
8) Bearing origin to destination
9) M = Magnetic, T = True
10) Destination Waypoint ID
11) Bearing, present position to Destination
12) M = Magnetic, T = True
13) Heading to steer to destination waypoint
14) M = Magnetic, T = True
15) Checksum
*/
/*****************************************************************************
BOD - Bearing Origin to Destination
1 2 3 4 5 6 7
| | | | | | |
$--BOD,x.x,T,x.x,M,c--c,c--c*hh<CR><LF>
Field Number :
1) Bearing Degrees, TRUE
2) T = True
3) Bearing Degrees, Magnetic
4) M = Magnetic
5) TO Waypoint
6) FROM Waypoint
7) Checksum
*/
/*****************************************************************************
BWC - Bearing and Distance to Waypoint, Latitude, N / S, Longitude, E / W, UTC, Status
11
1 2 3 4 5 6 7 8 9 10 | 12 13
| | | | | | | | | | | | |
$--BWC,hhmmss.ss,llll.ll,a,yyyyy.yy,a,x.x,T,x.x,M,x.x,N,c--c*hh<CR><LF>
Field Number :
1) UTCTime
2) Waypoint Latitude
3) N = North, S = South
4) Waypoint Longitude
5) E = East, W = West
6) Bearing, True
7) T = True
8) Bearing, Magnetic
9) M = Magnetic
10) Nautical Miles
11) N = Nautical Miles
12) Waypoint ID
13) Checksum
*/
/*****************************************************************************
MWV - Wind Speed and Angle
1 2 3 4 5
| | | | |
$--MWV,x.x,a,x.x,a*hh<CR><LF>
Field Number :
1) Wind Angle, 0 to 360 degrees
2) Reference, R = Relative, T = True
3) Wind Speed
4) Wind Speed Units, K / M / N
5) Status, A = Data Valid
6) Checksum
*/
/*****************************************************************************
WPL - Waypoint Location
1 2 3 4 5 6
| | | | | |
$--WPL,llll.ll,a,yyyyy.yy,a,c--c*hh<CR><LF>
Field Number :
1) Latitude
2) N or S(North or South)
3) Longitude
4) E or W(East or West)
5) Waypoint name
6) Checksum
*/
/*
Conversions from NMEA 0183 to NMEA 2000
NMEA 0183 NMEA 2000 PGN Comment
---------------------------------------------------------------------------------------------------------
APA
APB 127237 Heading/Track Control
129283 Cross Track Error
129284 Navigation Data
BOD
BWC
DIN 127488 Engine Parameters, Rapid Update According SeaSmart.Net protocol
127489 Engine Parameters, Dynamic specification v1.6.0
127493 Transmission Parameters, Dynamic
127505 Fluid Level
127508 Battery Status
DPT 128267 Water Depth
DTM 129044 Datum
*GGA 129029 GNSS Position Data ZDA or RMC are required
126992 System Time
129025 Position Rapid update
129033 Local Time Offset
129539 GNSS DOPs
GLL 129025 Position, Rapid Update See note (7)
HDG 127250 Vessel Heading
HDM, *HDT 127250 Vessel Heading Use variation and deviation from HDG
MDA 130311 Environmental Parameters Relative air humidity, air and water
130314 Actual Pressure temperature, atmospheric pressure,
130306 Wind Data wind data
MOB 127233 Man Overboard Notification (MOB)
MTW 130311 Environmental Parameters
MWD 130306 Wind Data
MWV 130306 Wind Data Theoretical wind sent as ground referenced to True North; calculated using COG/SOG
*RMB 129283 Cross Track Error Use data from APB
129284 Navigation Data Sent with true bearings, use ETA from ZTG
129285 Navigation � Route/WP information
*RMC 126992 System Time See note (7)
127250 Vessel Heading
127258 Magnetic Variation
129025 Position, Rapid Update
129026 COG & SOG, Rapid Update
129029 GNSS Position Data
129033 Local Time Offset
RSA 127245 Rudder
RTE 130066 Route and WP Service � Use data from WPL
Route/WP-List Attributes
130067 Route and WP Service �
Route - WP Name & Position
ROT 127251 Rate of Turn
VHW 127250 Vessel Heading
128259 Speed, Water referenced
*VDM, *VDO 129038 AIS Class A Position Report AIS VHF messages 1, 2 and 3
129039 AIS Class B Position Report AIS VHF message 18
129040 AIS Class B Extended Position Report AIS VHF message 19
129041 AIS Aids to Navigation (AtoN) Report AIS VHF message 21
129793 AIS UTC and Date Report AIS VHF messages 4 and 11
129794 AIS Class A Static+Voyage Rel Data AIS VHF message 5
129798 AIS SAR Aircraft Position Report AIS VHF message 9
129041 AIS Aids to Navigation (AtoN) Report AIS VHF message 21
129809 AIS Class B "CS" Static Data, Part A AIS VHF message 24
129810 AIS Class B "CS" Static Data, Part B AIS VHF message 24
VDR 129291 Set & Drift, Rapid Update
VLW 128275 Distance Log
*VTG 129026 COG & SOG, Rapid Update
VWR 130306 Wind Data
WPL 130074 Route and WP Service � Only waypoints not included to the route
WP List � WP Name & Position (the RTE should be received during 3 seconds after WPL)
*XTE 129283 Cross Track Error
ZDA 126992 System Time
129029 GNSS Position Data
129033 Local Time Offset
*ZTG 129284 Navigation Data use ETA from ZTG
Note (7): All NMEA 2000 periodic messages are sending with interval specified in the Standard.
Except PGN 127488, 127489, 127493, 127505 and 127508, these messages are sending immediately on receiving of DIN sentence.
Note (8): Sentences with no significant data (or data marked as invalid) may not be translated to
NMEA 2000 messages. NMEA 0183 sentences with invalid check sum are ignored.
* means NMEA 0183 currently suported
*/
// *****************************************************************************
void InitNMEA0183Handlers(tNMEA2000 *_NMEA2000, tBoatData *_BoatData) {
pNMEA2000=_NMEA2000;
pBD=_BoatData;
}
// *****************************************************************************
tN2kGNSSmethod GNSMethofNMEA0183ToN2k(int Method) {
switch (Method) {
case 0: return N2kGNSSm_noGNSS;
case 1: return N2kGNSSm_GNSSfix;
case 2: return N2kGNSSm_DGNSS;
default: return N2kGNSSm_noGNSS;
}
}
double toMagnetic(double True, double Variation) {
double magnetic = True - Variation;
while (magnetic<0) magnetic += pi*2;
while (magnetic >= pi*2) magnetic -= pi*2;
return magnetic;
}
// *****************************************************************************
void HandleNMEA0183Msg(const tNMEA0183Msg &NMEA0183Msg) {
int iHandler;
// Serial.println("NMEA0183RxCounter+1");
NMEA0183.SendMessage(NMEA0183Msg); // Forward all received NMEA0183 messages to the NMEA0183 out stream (USB im Teensy)
// NMEA0183_1.SendMessage(NMEA0183Msg); // Forward all received NMEA0183 messages to the NMEA0183 out1 stream (USB an TX1/RX1)
// NMEA0183_3.SendMessage(NMEA0183Msg); // Forward all received NMEA0183 messages to the NMEA0183 out3 stream (RS485 an TX3/RX3)
NMEA0183TxCounter = NMEA0183TxCounter+1;
// Find handler
for (iHandler=0; NMEA0183Handlers[iHandler].Code!=0 && !NMEA0183Msg.IsMessageCode(NMEA0183Handlers[iHandler].Code); iHandler++);
if (NMEA0183Handlers[iHandler].Code!=0) {
if (NMEA0183HandlersDebugStream != 0) {
NMEA0183HandlersDebugStream->print("NMEA0183 message parsed: ");
NMEA0183HandlersDebugStream->print(NMEA0183Handlers[iHandler].Code);
NMEA0183HandlersDebugStream->print(" - ");
NMEA0183HandlersDebugStream->println(NMEA0183Handlers[iHandler].NMEA0183seq);
}
/*
Serial.print("NMEA0183 message parsed: ");
Serial.print(NMEA0183Handlers[iHandler].Code);
Serial.print(" - ");
Serial.println(NMEA0183Handlers[iHandler].NMEA0183seq);
*/
NMEA0183Handlers[iHandler].Handler(NMEA0183Msg);
}
else {
if (NMEA0183HandlersDebugStream != 0) {
NMEA0183HandlersDebugStream->println("NMEA0183 message parsed, no Handler found.");
}
/*
Serial.print( "NMEA0183 message parsed, no Handler found: ");
Serial.println(NMEA0183Msg.MessageCode());
Serial.println("**********************************************");
*/
}
}
// NMEA0183 message Handler functions
void HandleXXX(const tNMEA0183Msg &NMEA0183Msg) {
if (NMEA0183HandlersDebugStream != 0) { NMEA0183HandlersDebugStream->println("no NMEA0183Handler available"); }
}
/*****************************************************************************
RMB - Recommended Minimum Navigation Information
14
1 2 3 4 5 6 7 8 9 10 11 12 13|
| | | | | | | | | | | | | |
$--RMB,A,x.x,a,c--c,c--c,llll.ll,a,yyyyy.yy,a,x.x,x.x,x.x,A*hh<CR><LF>
Field Number :
1) Status, V = Navigation receiver warning
2) Cross Track error - nautical miles
3) Direction to Steer, Left or Right
4) FROM Waypoint ID
5) TO Waypoint ID
6) Destination Waypoint Latitude
7) N or S
8) Destination Waypoint Longitude
9) E or W
10) Range to destination in nautical miles
11) Bearing to destination in degrees True
12) Destination closing velocity in knots
13) Arrival Status, A = Arrival Circle Entered
14) Checksum
*/
void HandleRMB(const tNMEA0183Msg &NMEA0183Msg) {
double XTE;
double Latitude;
double Longitude;
double DTW;
double BTW;
double VMG;
char arrivalAlarm;
char originID[NMEA0183_MAX_WP_NAME_LENGTH];
char destID[NMEA0183_MAX_WP_NAME_LENGTH];
// int originIDi;
// int destIDi;
// if (NMEA0183HandlersDebugStream != 0) { NMEA0183HandlersDebugStream->print("NMEA0183Msg="); NMEA0183HandlersDebugStream->println(NMEA0183Msg->Data[MAX_NMEA0183_MSG_LEN]); }
if (pBD == 0) return;
if (NMEA0183ParseRMB_nc(NMEA0183Msg, XTE, Latitude, Longitude, DTW, BTW, VMG, arrivalAlarm, *originID, *destID)) {
/*
if (NMEA0183HandlersDebugStream != 0) {
NMEA0183HandlersDebugStream->print("XTE="); NMEA0183HandlersDebugStream->println(XTE);
NMEA0183HandlersDebugStream->print("Latitude="); NMEA0183HandlersDebugStream->println(Latitude);
NMEA0183HandlersDebugStream->print("Longitude="); NMEA0183HandlersDebugStream->println(Longitude);
NMEA0183HandlersDebugStream->print("DTW="); NMEA0183HandlersDebugStream->println(DTW);
NMEA0183HandlersDebugStream->print("BTW="); NMEA0183HandlersDebugStream->println(BTW);
NMEA0183HandlersDebugStream->print("VMG="); NMEA0183HandlersDebugStream->println(VMG);
NMEA0183HandlersDebugStream->print("arrivalAlarm="); NMEA0183HandlersDebugStream->println(arrivalAlarm);
NMEA0183HandlersDebugStream->print("originID="); NMEA0183HandlersDebugStream->println(originID);
NMEA0183HandlersDebugStream->print("destID="); NMEA0183HandlersDebugStream->println(destID);
}
*/
/*
if (atoi(originID) == 0) {
originIDi = int(originID[0]) * 100 + int(originID[1]);
}
else {
originIDi = atoi(originID);
}
// Serial1.print("originID=");
// Serial1.print(originID);
// Serial1.print(", ");
// Serial1.println(originIDi);
if (atoi(destID) == 0) {
destIDi = int(destID[0]) * 100 + int(destID[1]);
}
else {
destIDi = atoi(destID);
}
// Serial1.print("destID=");
// Serial1.print(destID);
// Serial1.print(", ");
// Serial1.println(destIDi);
*/
/*
RMB 129283 Cross Track Error
129284 Navigation Data
This parameter group provides essential navigation data for following a route. Transmissions will originate from products that can
create and manage routes using waypoints. This information is intended for navigational repeaters.
Field # Field Description
1 Sequence ID
2 Distance to Destination Waypoint
3 Course/Bearing Ref.
4 Perpendicular Crossed
5 Arrival Circle Entered
6 Calculation Type
7 ETA Time
8 ETA Date
9 Bearing, Origin To Destination Waypoint
10 Bearing, Position To Destination Waypoint
11 Origin Waypoint Number
12 Destination Waypoint Number
13 Destination Wpt Latitude
14 Destination Wpt Longitude
15 Waypoint Closing Velocity
*/
if (pNMEA2000 != 0) {
tN2kMsg N2kMsg;
SetN2kXTE(N2kMsg, 1, N2kxtem_Autonomous, false, nmTom*XTE); // PGN 129283
pNMEA2000->SendMsg(N2kMsg);
N2kTxCounter = N2kTxCounter + 1;
// Navigation data
//void SetN2kPGN129284(tN2kMsg &N2kMsg, unsigned char SID, double DistanceToWaypoint, tN2kHeadingReference BearingReference,
// bool PerpendicularCrossed, bool ArrivalCircleEntered, tN2kDistanceCalculationType CalculationType,
// double ETATime, int16_t ETADate, double BearingOriginToDestinationWaypoint, double BearingPositionToDestinationWaypoint,
// uint8_t OriginWaypointNumber, uint8_t DestinationWaypointNumber,
// double DestinationLatitude, double DestinationLongitude, double WaypointClosingVelocity)
// SetN2kPGN129284(N2kMsg, 1, DTW*nmTom, N2khr_true, false, false, N2kdct_GreatCircle, 0, 0,
// BTW*degToRad, BTW*degToRad, 1, 2, Latitude, Longitude, 5* knToms);
// pNMEA2000->SendMsg(N2kMsg);
// N2kTxCounter = N2kTxCounter + 1;
//3B 9F D1 47 4F 54 4F 20 43 55 52 53 4F 52 00 00 00 00 00 30 30 30 31 8A 72 0F 6F 54 B6 09 00 FF
//3B9F2B474F544F20435552534F52000000000030303031 CA594F 56 2447 0800FF
//3B9FF0474F544F20435552534F52203200383730303032 CF6F54 6C 345B 0700FF
//3B9FB5474F544F20435552534F52203300353130303033 7954FE 50 E004 0700FF
N2kMsg.SetPGN(130848UL); // Raymarine proprietary PGN 130848
N2kMsg.Priority = 7;
N2kMsg.Destination = 255;
N2kMsg.AddByte(0x3b); // lower 8 Bit of Manufacturer Code, Raymarine=1851=0x073b
N2kMsg.AddByte(0x9f); // high 3 Bit = Industry Code, Marine Industry=4, 2 Bit Reserved, higher 3 Bit of Manufacturer Code"
N2kMsg.AddByte(0xd1); // Proprietary ID ??? ändert sich, zählt hoch, muss das ???
N2kMsg.AddStr0(destID, 16);
N2kMsg.AddStr("0001", 4); // oder WP-Number ??
// Serial.print("DTW="); Serial.println(DTW);
// Serial.print("BTW="); Serial.println(BTW);
N2kMsg.Add2ByteUInt(BTW*174.5); // BTW for MFD Axiom Pro 9 (Waypoint) and for i70, Bearing Mode in Unit setting must be set to TRUE
N2kMsg.Add2ByteUInt(BTW*174.5); // 1:0.0057° => 1/0.0057=174
N2kMsg.Add4ByteUDouble(DTW*nmTom, 0.01); // DTW in m
// N2kMsg.AddByte(true);
N2kMsg.AddByte(false); // autostep to next waypoint ???
pNMEA2000->SendMsg(N2kMsg);
N2kTxCounter = N2kTxCounter + 1;
/*
//3B 9F 01 00 47 4F 54 4F 20 43 55 52 53 4F 52 00 00 00 00 00 FF 00 FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF 0D FF FF FF FF FF FF FF FF
//3B9F0100474F544F20435552534F520000000000FF00FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF0DFFFFFFFFFFFFFFFF
//3B9F0200474F544F20435552534F522032003837FF00FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF0DFFFFFFFFFFFFFFFF
//3B9F0300474F544F20435552534F522033003531FF00FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF0DFFFFFFFFFFFFFFFF
N2kMsg.SetPGN(130918UL); // Raymarine proprietary PGN 130918
N2kMsg.Priority = 7;
N2kMsg.Destination = 255;
N2kMsg.AddByte(0x3b); // lower 8 Bit of Manufacturer Code, Raymarine=1851=0x073b
N2kMsg.AddByte(0x9f); // high 3 Bit = Industry Code, Marine Industry=4, 2 Bit Reserved, higher 3 Bit of Manufacturer Code"
N2kMsg.AddByte(0x01); // Proprietary ID ???
N2kMsg.AddByte(0x00);
// N2kMsg.AddStr("GOTO CURSOR", 16);
N2kMsg.AddStr(destID, 16);
N2kMsg.AddByte(0xff);
N2kMsg.AddByte(0x00);
N2kMsg.AddByte(0xff);
N2kMsg.AddByte(0xff);
N2kMsg.AddByte(0xff);
N2kMsg.AddByte(0xff);
N2kMsg.AddByte(0xff);
N2kMsg.AddByte(0xff);
N2kMsg.AddByte(0xff);
N2kMsg.AddByte(0xff);
N2kMsg.AddByte(0xff);
N2kMsg.AddByte(0xff);
N2kMsg.AddByte(0xff);
N2kMsg.AddByte(0xff);
N2kMsg.AddByte(0xff);
N2kMsg.AddByte(0xff);
N2kMsg.AddByte(0xff);
N2kMsg.AddByte(0xff);
N2kMsg.AddByte(0x0d);
N2kMsg.AddByte(0xff);
N2kMsg.AddByte(0xff);
N2kMsg.AddByte(0xff);
N2kMsg.AddByte(0xff);
N2kMsg.AddByte(0xff);
N2kMsg.AddByte(0xff);
N2kMsg.AddByte(0xff);
N2kMsg.AddByte(0xff);
pNMEA2000->SendMsg(N2kMsg);
N2kTxCounter = N2kTxCounter + 1;
*/
}
if (NMEA0183HandlersDebugStream != 0) {
}
}
else if (NMEA0183HandlersDebugStream != 0) { NMEA0183HandlersDebugStream->println("Failed to parse RMB"); }
}
/*****************************************************************************
RMC - Recommended Minimum Navigation Information
1 2 3 4 5 6 7 8 9 10 11 12
| | | | | | | | | | | |
$--RMC,hhmmss.ss,A,llll.ll,a,yyyyy.yy,a,x.x,x.x,xxxx,x.x,a*hh<CR><LF>
Field Number:
1) UTC Time
2) Status, V = Navigation receiver warning, P = Precise
3) Latitude
4) N or S
5) Longitude
6) E or W
7) Speed over ground, knots
8) Track made good, degrees true
9) Date, ddmmyy
10) Magnetic Variation, degrees
11) E or W
12) Checksum
*/
void HandleRMC(const tNMEA0183Msg &NMEA0183Msg) {
if (pBD==0) return;
if (NMEA0183ParseRMC_nc(NMEA0183Msg, pBD->GPSTime, pBD->Latitude, pBD->Longitude, pBD->COG, pBD->SOG, pBD->DaysSince1970, pBD->Variation)) {
/*
RMC 126992 System Time
127258 Magnetic Variation
129025 Position, Rapid Update
129026 COG & SOG, Rapid Update fehlt
*/
if (pNMEA2000 != 0) {
tN2kMsg N2kMsg;
SetN2kMagneticVariation(N2kMsg, 1, N2kmagvar_Calc, pBD->DaysSince1970, pBD->Variation);
pNMEA2000->SendMsg(N2kMsg);
N2kTxCounter = N2kTxCounter + 1;
SetN2kPGN129025(N2kMsg, pBD->Latitude, pBD->Longitude);
pNMEA2000->SendMsg(N2kMsg);
N2kTxCounter = N2kTxCounter + 1;
SetN2kCOGSOGRapid(N2kMsg, 1, N2khr_true, pBD->COG, pBD->SOG);
pNMEA2000->SendMsg(N2kMsg);
N2kTxCounter = N2kTxCounter + 1;
}
if (NMEA0183HandlersDebugStream != 0) {
}
}
else if (NMEA0183HandlersDebugStream != 0) { NMEA0183HandlersDebugStream->println("Failed to parse RMC"); }
}
/*****************************************************************************
GGA - Global Positioning System Fix Data, Time, Position and fix related data fora GPS receiver.
11
1 2 3 4 5 6 7 8 9 10 | 12 13 14 15
| | | | | | | | | | | | | | |
$--GGA,hhmmss.ss,llll.ll,a,yyyyy.yy,a,x,xx,x.x,x.x,M,x.x,M,x.x,xxxx*hh<CR><LF>
Field Number:
1) Universal Time Coordinated (UTC)
2) Latitude
3) N or S (North or South)
4) Longitude
5) E or W (East or West)
6) GPS Quality Indicator,
0 - fix not available,
1 - GPS fix,
2 - Differential GPS fix
7) Number of satellites in view, 00 - 12
8) Horizontal Dilution of precision
9) Antenna Altitude above/below mean-sea-level (geoid)
10) Units of antenna altitude, meters
11) Geoidal separation, the difference between the WGS-84 earth
ellipsoid and mean-sea-level (geoid), "-" means mean-sea-level
below ellipsoid
12) Units of geoidal separation, meters
13) Age of differential GPS data, time in seconds since last SC104
type 1 or 9 update, null field when DGPS is not used
14) Differential reference station ID, 0000-1023
15) Checksum
*/
void HandleGGA(const tNMEA0183Msg &NMEA0183Msg) {
if (pBD==0) return;
if (NMEA0183ParseGGA_nc(NMEA0183Msg,pBD->GPSTime,pBD->Latitude,pBD->Longitude,
pBD->GPSQualityIndicator,pBD->SatelliteCount,pBD->HDOP,pBD->Altitude,pBD->GeoidalSeparation,
pBD->DGPSAge,pBD->DGPSReferenceStationID)) {
/*
GGA 129029 GNSS Position Data
*/
if (pNMEA2000!=0) {
tN2kMsg N2kMsg;
SetN2kMagneticVariation(N2kMsg,1,N2kmagvar_Calc,pBD->DaysSince1970,pBD->Variation);
pNMEA2000->SendMsg(N2kMsg);
N2kTxCounter = N2kTxCounter + 1;
SetN2kGNSS(N2kMsg,1,pBD->DaysSince1970,pBD->GPSTime,pBD->Latitude,pBD->Longitude,pBD->Altitude,
N2kGNSSt_GPS,GNSMethofNMEA0183ToN2k(pBD->GPSQualityIndicator),pBD->SatelliteCount,pBD->HDOP,0,
pBD->GeoidalSeparation,1,N2kGNSSt_GPS,pBD->DGPSReferenceStationID,pBD->DGPSAge);
pNMEA2000->SendMsg(N2kMsg);
N2kTxCounter = N2kTxCounter + 1;
}
/*
if (NMEA0183HandlersDebugStream!=0) {
NMEA0183HandlersDebugStream->print("Time="); NMEA0183HandlersDebugStream->println(pBD->GPSTime);
NMEA0183HandlersDebugStream->print("Latitude="); NMEA0183HandlersDebugStream->println(pBD->Latitude,5);
NMEA0183HandlersDebugStream->print("Longitude="); NMEA0183HandlersDebugStream->println(pBD->Longitude,5);
NMEA0183HandlersDebugStream->print("Altitude="); NMEA0183HandlersDebugStream->println(pBD->Altitude,1);
NMEA0183HandlersDebugStream->print("GPSQualityIndicator="); NMEA0183HandlersDebugStream->println(pBD->GPSQualityIndicator);
NMEA0183HandlersDebugStream->print("SatelliteCount="); NMEA0183HandlersDebugStream->println(pBD->SatelliteCount);
NMEA0183HandlersDebugStream->print("HDOP="); NMEA0183HandlersDebugStream->println(pBD->HDOP);
NMEA0183HandlersDebugStream->print("GeoidalSeparation="); NMEA0183HandlersDebugStream->println(pBD->GeoidalSeparation);
NMEA0183HandlersDebugStream->print("DGPSAge="); NMEA0183HandlersDebugStream->println(pBD->DGPSAge);
NMEA0183HandlersDebugStream->print("DGPSReferenceStationID="); NMEA0183HandlersDebugStream->println(pBD->DGPSReferenceStationID);
}
*/
} else if (NMEA0183HandlersDebugStream!=0) { NMEA0183HandlersDebugStream->println("Failed to parse GGA"); }
}
#define PI_2 6.283185307179586476925286766559
/*****************************************************************************
HDT - Heading - True
1 2 3
| | |
$--HDT,x.x,T*hh<CR><LF>
Field Number:
1) Heading Degrees, true
2) T = True
3) Checksum
*/
void HandleHDT(const tNMEA0183Msg &NMEA0183Msg) {
if (pBD==0) return;
if (NMEA0183ParseHDT_nc(NMEA0183Msg,pBD->TrueHeading)) {
/*
HDT 127250 Vessel Heading
*/
if (pNMEA2000!=0) {
tN2kMsg N2kMsg;
double MHeading=pBD->TrueHeading-pBD->Variation;
while (MHeading<0) MHeading+=PI_2;
while (MHeading>=PI_2) MHeading-=PI_2;
SetN2kTrueHeading(N2kMsg,1,pBD->TrueHeading);
pNMEA2000->SendMsg(N2kMsg);
N2kTxCounter = N2kTxCounter + 1;
}
if (NMEA0183HandlersDebugStream!=0) {
NMEA0183HandlersDebugStream->print("True heading="); NMEA0183HandlersDebugStream->println(pBD->TrueHeading);
}
} else if (NMEA0183HandlersDebugStream!=0) { NMEA0183HandlersDebugStream->println("Failed to parse HDT"); }
}
/*****************************************************************************
VTG - Track made good and Ground speed
1 2 3 4 5 6 7 8 9
| | | | | | | | |
$--VTG,x.x,T,x.x,M,x.x,N,x.x,K*hh<CR><LF>
Field Number:
1) Track Degrees
2) T = True
3) Track Degrees
4) M = Magnetic
5) Speed Knots
6) N = Knots
7) Speed Kilometers Per Hour
8) K = Kilometers Per Hour
9) Checksum
*/
void HandleVTG(const tNMEA0183Msg &NMEA0183Msg) {
double MagneticCOG;
if (pBD==0) return;
if (NMEA0183ParseVTG_nc(NMEA0183Msg,pBD->COG,MagneticCOG,pBD->SOG)) {
pBD->Variation=pBD->COG-MagneticCOG; // Save variation for Magnetic heading
/*
VTG 129026 COG & SOG, Rapid Update
*/
if (pNMEA2000!=0) {
tN2kMsg N2kMsg;
SetN2kCOGSOGRapid(N2kMsg,1,N2khr_true,pBD->COG,pBD->SOG);
pNMEA2000->SendMsg(N2kMsg);
N2kTxCounter = N2kTxCounter + 1;
SetN2kBoatSpeed(N2kMsg,1,pBD->SOG);
pNMEA2000->SendMsg(N2kMsg);
N2kTxCounter = N2kTxCounter + 1;
}
if (NMEA0183HandlersDebugStream!=0) {
NMEA0183HandlersDebugStream->print("True heading="); NMEA0183HandlersDebugStream->println(pBD->TrueHeading);
}
} else if (NMEA0183HandlersDebugStream!=0) { NMEA0183HandlersDebugStream->println("Failed to parse VTG"); }
}
/*****************************************************************************
XTE - Cross - Track Error, Measured
1 2 3 4 5 6
| | | | | |
$--XTE,A,A,x.x,a,N,*hh<CR><LF>
Field Number :
1) Status
V = LORAN - C Blink or SNR warning
V = general warning flag or other navigation systems when a reliable fix is not available
2) Status
V = Loran - C Cycle Lock warning flag
A = OK or not used
3) Cross Track Error Magnitude
4) Direction to steer, L or R
5) Cross Track Units, N = Nautical Miles
6) Checksum
*/
void HandleXTE(const tNMEA0183Msg &NMEA0183Msg) {
double XTE;
if (pBD == 0) return;
if (NMEA0183ParseXTE_nc(NMEA0183Msg, XTE)) {
//XTE 129283 Cross Track Error
if (pNMEA2000 != 0) {
tN2kMsg N2kMsg;
SetN2kXTE(N2kMsg, 1, N2kxtem_Autonomous, false, nmTom*XTE);
pNMEA2000->SendMsg(N2kMsg);
N2kTxCounter = N2kTxCounter + 1;
}
if (NMEA0183HandlersDebugStream != 0) {
NMEA0183HandlersDebugStream->print("XTE="); NMEA0183HandlersDebugStream->println(XTE);
}
}
else if (NMEA0183HandlersDebugStream != 0) { NMEA0183HandlersDebugStream->println("Failed to parse XTE"); }
}
/*****************************************************************************
ZTG - UTC & Time to Destination Waypoint
1 2 3 4
| | | |
$--ZTG,hhmmss.ss,hhmmss.ss,c--c*hh<CR><LF>
Field Number:
1) Universal Time Coordinated (UTC)
2) Time Remaining
3) Destination Waypoint ID
4) Checksum
*/
void HandleZTG(const tNMEA0183Msg &NMEA0183Msg) {
/*
double MagneticCOG;
if (pBD == 0) return;
if (NMEA0183ParseZTG_nc(NMEA0183Msg, pBD->COG, MagneticCOG, pBD->SOG)) {
pBD->Variation = pBD->COG - MagneticCOG; // Save variation for Magnetic heading
if (pNMEA2000 != 0) {
tN2kMsg N2kMsg;
// SetN2kCOGSOGRapid(N2kMsg, 1, N2khr_true, pBD->COG, pBD->SOG);
// pNMEA2000->SendMsg(N2kMsg);
// SetN2kBoatSpeed(N2kMsg, 1, pBD->SOG);
// pNMEA2000->SendMsg(N2kMsg);
}
if (NMEA0183HandlersDebugStream != 0) {
NMEA0183HandlersDebugStream->print("True heading="); NMEA0183HandlersDebugStream->println(pBD->TrueHeading);
}
}
else if (NMEA0183HandlersDebugStream != 0) { NMEA0183HandlersDebugStream->println("Failed to parse ZTG"); }
*/
}
void printDegrees(long min4)
{
long intPart = min4 / 60L;
long fracPart = intPart % 10000L;
if (fracPart < 0)
fracPart = -fracPart;
char frac[6];
sprintf(frac, "%04ld", fracPart);
NMEA0183HandlersDebugStream->print(intPart / 10000L); NMEA0183HandlersDebugStream->print("."); NMEA0183HandlersDebugStream->println(frac);
}
double MinToDegrees(long min4)
{
double intPart;
intPart = min4 / 60;
intPart = intPart / 10000;
return (intPart);
}
/*****************************************************************************
!AIVDM/VDO - AIVDM packets are reports from other ships and AIVDO packets are reports from your own ship
1 2 3 4 5 6 7
| | | | | | |
!AIVDM,n,n,n,c,c--c,n*hh<CR><LF>
Field Number:
1) count of fragments in the currently accumulating message.
The payload size of each sentence is limited by NMEA 0183�s 82-character maximum,
so it is sometimes required to split a payload over several fragment sentences.
2) fragment number of this sentence. It will be one-based.
A sentence with a fragment count of 1 and a fragment number of 1 is complete in itself.
3) sequential message ID for multi-sentence messages
4) radio channel code. AIS uses the high side of the duplex from two VHF radio channels:
AIS Channel A is 161.975Mhz (87B); AIS Channel B is 162.025Mhz (88B). In the wild, channel codes 1 and 2 may also be encountered;
the standards do not prescribe an interpretation of these but it�s obvious enough.
5) data payload
6) number of fill bits requires to pad the data payload to a 6 bit boundary, ranging from 0 to 5.
Equivalently, subtracting 5 from this tells how many least significant bits of the last 6-bit nibble in the data payload should be ignored.
Note that this pad byte has a tricky interaction with the <[ITU-1371]> requirement for byte alignment in over-the-air AIS messages.
7) Checksum
*/
/* samples of alle AIS-msg
!AIVDM,1,1,,A,13u?etPv2;0n:dDPwUM1U1Cb069D,0*24
!AIVDM,1,1,,A,400TcdiuiT7VDR>3nIfr6>i00000,0*78
!AIVDM,2,1,0,A,58wt8Ui`g??r21`7S=:22058<v05Htp000000015>8OA;0sk,0*7B
!AIVDM,2,2,0,A,eQ8823mDm3kP00000000000,2*5D
!AIVDM,1,1,4,B,6>jR0600V:C0>da4P106P00,2*02
!AIVDM,2,1,9,B,61c2;qLPH1m@wsm6ARhp<ji6ATHd<C8f=Bhk>34k;S8i=3To,0*2C
!AIVDM,2,2,9,B,Djhi=3Di<2pp=34k>4D,2*03
!AIVDM,1,1,1,B,8>h8nkP0Glr=<hFI0D6??wvlFR06EuOwgwl?wnSwe7wvlOw?sAwwnSGmwvh0,0*17
!AIVDM,1,1,,A,95M2oQ@41Tr4L4H@eRvQ;2h20000,0*0F
!AIVDM,1,1,,B,;8u:8CAuiT7Bm2CIM=fsDJ100000,0*51
!AIVDM,1,1,,B,>>M4fWA<59B1@E=@,0*17
!AIVDM,1,1,,A,B6CdCm0t3`tba35f@V9faHi7kP06,0*58
!AIVDM,2,1,0,B,C8u:8C@t7@TnGCKfm6Po`e6N`:Va0L2J;06HV50JV?SjBPL3,0*28
!AIVDM,2,2,0,B,11RP,0*17
!AIVDM,2,1,5,B,E1c2;q@b44ah4ah0h:2ab@70VRpU<Bgpm4:gP50HH`Th`QF5,0*79
!AIVDM,2,2,5,B,1CQ1A83PCAH0,0*62
!AIVDM,1,1,,B,H1c2;qA@PU>0U>060<h5=>0:1Dp,2*7F
!AIVDM,1,1,,B,H1c2;qDTijklmno31<<C970`43<1,0*2A
!AIVDM,1,1,,A,KCQ9r=hrFUnH7P00,0*41
!AIVDM,1,1,,B,KC5E2b@U19PFdLbMuc5=ROv62<7m,0*16
!AIVDM,1,1,,B,K5DfMB9FLsM?P00d,0*70
*/
void HandleVDM(const tNMEA0183Msg &NMEA0183Msg) {
uint8_t pkgCnt;
uint8_t pkgNmb;
unsigned int seqMessageId;
char channel;
unsigned int length;
char bitstream[512] = "";
static char bitstream_total[512] = "";
unsigned int fillBits;
enum AIS::Nmea0183AisMessages msgType;
unsigned int msgNumeric;
uint8_t part_no;
uint8_t repeat;
uint32_t mmsi;
// uint32_t mothership_mmsi;
int32_t LAT;
int32_t LONG;
double LAT1;
double LONG1;
bool accuracy;
bool raim;
uint8_t seconds;
int32_t COG;
int32_t SOG;
int32_t HDG;
int8_t ROT;
double ROT1;
uint32_t imo;
uint8_t shiptype;
uint8_t to_port;
uint8_t to_starboard;
uint16_t to_bow;
uint16_t to_stern;
uint8_t epfd;
uint8_t draught;
uint8_t navstatus;
uint16_t year;
uint8_t month;
uint8_t day;
uint8_t hour;
uint8_t minute;
uint8_t aidtype;
char name[120];
char *shipname = name;
char dest[20];
char *destination = dest;
char sign[42];
char *callsign = sign;
char vendor[18];
const char *vendorid = vendor;
char name1[120];
char *aidname = name1;
char name2[88];
char *aidnameext = name2;
length = 500;
// NMEA0183.SendMessage(NMEA0183Msg); // Send VDM NMEA0183 message to the NMEA0183 out stream (USB im Teensy)
if (NMEA0183ParseVDM_nc(NMEA0183Msg, pkgCnt, pkgNmb, seqMessageId, channel, length, bitstream, fillBits)) {
//#define AIS_DEBUG
#ifdef AIS_DEBUG
Serial.print("pkgCnt="); Serial.println(pkgCnt);
Serial.print("pkgNmb="); Serial.println(pkgNmb);
Serial.print("seqMessageId="); Serial.println(seqMessageId);
Serial.print("channel="); Serial.println(channel);
Serial.print("length="); Serial.println(length);
Serial.print("bitstream="); Serial.print(bitstream); Serial.print(" len:"); Serial.println(strlen(bitstream));
Serial.print("fillBits="); Serial.println(fillBits);
#endif
// Multi packets - first packet
if (pkgCnt == 2 && pkgNmb == 1) {
strcpy(bitstream_total, bitstream);
#ifdef AIS_DEBUG
Serial.println("nach copy:");
Serial.print("bitstream ="); Serial.print(bitstream); Serial.print(" len:"); Serial.println(strlen(bitstream));
Serial.print("bitstream_total="); Serial.print(bitstream_total); Serial.print(" len:"); Serial.println(strlen(bitstream_total));
Serial.print("Multi packets - first, pkgCnt="); Serial.println(pkgCnt);
#endif
return;
}
// Multi packets - second packet
if (pkgCnt == 2 && pkgNmb == 2) {
#ifdef AIS_DEBUG
Serial.println("vor tranfer:");
Serial.print("bitstream= "); Serial.print(bitstream); Serial.print(" len:"); Serial.println(strlen(bitstream));
Serial.print("bitstream_total="); Serial.print(bitstream_total); Serial.print(" len:"); Serial.println(strlen(bitstream_total));
#endif
strcat(bitstream_total, bitstream);
strcpy(bitstream, bitstream_total);