forked from schwehr/libais
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathais1_2_3.cpp
146 lines (126 loc) · 4.61 KB
/
ais1_2_3.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
// Since Apr 2010
#include "ais.h"
#include <iostream>
#include <bitset>
#include <string>
#include <cassert>
#include <cmath>
#include <cstdlib>
using namespace std;
Ais1_2_3::Ais1_2_3(const char *nmea_payload) {
assert(nmea_payload);
assert(nmea_ord_initialized); // Make sure we have the lookup table built
init();
if (strlen(nmea_payload) != 168/6) { status = AIS_ERR_BAD_BIT_COUNT; return; }
std::bitset<168> bs; // 1 slot
status = aivdm_to_bits(bs, nmea_payload);
if (had_error()) return;
message_id = ubits(bs, 0, 6);
if (message_id < 1 or message_id > 3) {
status = AIS_ERR_WRONG_MSG_TYPE;
return;
}
repeat_indicator = ubits(bs,6,2);
mmsi = ubits(bs,8,30);
nav_status = ubits(bs,38,4);
/*const int*/ rot_raw = sbits(bs,42,8);
rot_over_range = abs(rot_raw) > 126 ? true : false ;
//rot = 4.733 * sqrt(fabs(rot_raw)); // FIX: this was wrong... double check
rot = pow( (rot_raw/4.733), 2 );
if (rot_raw < 0) rot = -rot;
sog = ubits(bs,50,10) / 10.;
position_accuracy = ubits(bs,60,1);
x = sbits(bs, 61, 28) / 600000.;
y = sbits(bs, 89, 27) / 600000.;
cog = ubits(bs, 116, 12) / 10.;
true_heading = ubits(bs, 128, 9);
timestamp = ubits(bs, 137, 6);
special_manoeuvre = ubits(bs, 143, 2);
spare = ubits(bs, 145, 3);
raim = bool(bs[148]);
sync_state = ubits(bs, 149, 2);
// Set all to invalid - this way we don't have to track it in multiple places
received_stations = -1; received_stations_valid = false;
slot_number = -1; slot_number_valid = false;
utc_hour = utc_min = -1; utc_valid = false;
utc_spare = -1;
slot_offset = -1; slot_offset_valid = false;
slot_increment = -1; slot_increment_valid = false;
slots_to_allocate = -1; slots_to_allocate_valid = false;
keep_flag = false; keep_flag_valid = false;
if ( 1 == message_id || 2 == message_id) {
slot_timeout = ubits(bs,151,3);
slot_timeout_valid = true;
switch (slot_timeout) {
case 0:
slot_offset = ubits(bs, 154, 14);
slot_offset_valid = true;
break;
case 1:
utc_hour = ubits(bs, 154, 5);
utc_min = ubits(bs, 159, 7);
utc_spare = ubits(bs, 166, 2);
utc_valid = true;
break;
case 2: // FALLTHROUGH
case 4: // FALLTHROUGH
case 6:
slot_number = ubits(bs, 154, 14);
slot_number_valid = true;
break;
case 3: // FALLTHROUGH
case 5: // FALLTHROUGH
case 7:
received_stations = ubits(bs, 154, 14);
received_stations_valid = true;
break;
default:
assert (false);
}
} else {
// ITDMA
assert (3 == message_id);
slot_increment = ubits(bs, 151, 13);
slot_increment_valid = true;
slots_to_allocate = ubits(bs, 164, 3);
slots_to_allocate_valid = true;
keep_flag = bool(bs[167]);
keep_flag_valid = true;
}
}
void
Ais1_2_3::print(bool verbose/*=false*/) {
std::cout << "Class A Position: " << message_id
<< std::endl;
if (!verbose) return;
cout << "\trow_raw: " << rot_raw << endl;
cout << "\trot: " << rot << " -> " << (rot_over_range? "greater than": " ") << " " << rot << endl;
cout << "\tsog: " << sog << endl;
cout << "\tpos_acc: " << position_accuracy << endl;
cout << "\tpos_x: " << x << endl;
cout << "\tpos_y: " << y << endl;
//cout << "cog_raw: " << ubits(bs,116,12) << endl;
cout << "\tcog: " << cog << endl;
cout << "\ttrue_heading:" << true_heading << endl;
cout << "\ttimestamp: " << timestamp << endl;
cout << "\tspecial_manoeuvre: " << special_manoeuvre << endl;
cout << "\tspare: " << spare << endl;
cout << "\tsync_state: " << sync_state << endl;
if ( 1 == message_id || 2 == message_id) {
cout << "\tSOTDMA type " << message_id << endl;
cout << "\t\tslot_offset: " << slot_offset << endl;
cout << "\t\tslot_timeout: " << slot_timeout << endl;
cout << "\t\tslot_number: " << slot_number << endl;
cout << "\t\treceived_stations: " << received_stations << endl;
} else {
assert (3 == message_id);
cout << "\tITDMA type" << endl;
cout << "\t\tslot_increment: " << slot_increment << endl;
cout << "\t\tslots_to_allocate: " << slots_to_allocate << endl;
cout << "\t\tkeep_flag: " << (keep_flag?"keep":"do_not_keep") << endl;
}
}
std::ostream& operator<< (std::ostream& o, Ais1_2_3 const& a)
{
return o << a.message_id << ": " << a.mmsi ;
}