Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Ardupilot
GitHub Repository: Ardupilot/ardupilot
Path: blob/master/libraries/AP_AIS/AP_AIS.h
9673 views
1
/*
2
This program is free software: you can redistribute it and/or modify
3
it under the terms of the GNU General Public License as published by
4
the Free Software Foundation, either version 3 of the License, or
5
(at your option) any later version.
6
7
This program is distributed in the hope that it will be useful,
8
but WITHOUT ANY WARRANTY; without even the implied warranty of
9
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10
GNU General Public License for more details.
11
12
You should have received a copy of the GNU General Public License
13
along with this program. If not, see <http://www.gnu.org/licenses/>.
14
*/
15
#pragma once
16
17
#include "AP_AIS_config.h"
18
19
#if AP_AIS_ENABLED
20
// 0 fully disabled and compiled out
21
// 1 compiled in and enabled
22
// 2 compiled in with dummy methods, none functional, except rover which never uses dummy methods functionality
23
24
#include <AP_Param/AP_Param.h>
25
#include <AP_Common/AP_ExpandingArray.h>
26
#include <GCS_MAVLink/GCS_MAVLink.h>
27
28
#define AIVDM_BUFFER_SIZE 10
29
#define AIVDM_PAYLOAD_SIZE 65
30
31
class AP_AIS
32
{
33
public:
34
AP_AIS();
35
36
CLASS_NO_COPY(AP_AIS);
37
38
// get singleton instance
39
static AP_AIS *get_singleton();
40
41
// return true if AIS is enabled
42
bool enabled() const;
43
44
// Initialize the AIS object and prepare it for use
45
void init();
46
47
// update AIS, expected to be called at 20hz
48
void update();
49
50
// send mavlink AIS message
51
void send(mavlink_channel_t chan);
52
53
// parameter block
54
static const struct AP_Param::GroupInfo var_info[];
55
56
private:
57
58
// parameters
59
AP_Int8 _type; // type of AIS receiver
60
AP_Int16 _max_list; // maximum number of vessels to track at once
61
AP_Int16 _time_out; // time in seconds that a vessel will be dropped from the list
62
AP_Int16 _log_options; // logging options bitmask
63
64
enum class AISType {
65
NONE = 0,
66
NMEA = 1,
67
};
68
69
enum options {
70
AIS_OPTIONS_LOG_ALL_RAW = 1<<0,
71
AIS_OPTIONS_LOG_UNSUPPORTED_RAW = 1<<1,
72
AIS_OPTIONS_LOG_DECODED = 1<<2,
73
};
74
75
struct AIVDM {
76
uint8_t num;
77
uint8_t total;
78
uint8_t ID;
79
char payload[AIVDM_PAYLOAD_SIZE];
80
};
81
AIVDM _incoming;
82
AIVDM _AIVDM_buffer[AIVDM_BUFFER_SIZE];
83
84
struct ais_vehicle_t {
85
mavlink_ais_vessel_t info;
86
uint32_t last_update_ms; // last time this was refreshed, allows timeouts
87
uint32_t last_send_ms; // last time this message was sent via mavlink, stops us spamming the link
88
};
89
90
// list of the vessels that are being tracked
91
AP_ExpandingArray<ais_vehicle_t> _list {8};
92
93
AP_HAL::UARTDriver *_uart;
94
95
uint16_t _send_index; // index of the last vessel send over mavlink
96
97
// Send a AIS vessel to the object avoidance data base if its position is valid
98
void send_to_object_avoidance_database(const struct ais_vehicle_t &vessel);
99
100
// Return true if location is valid
101
bool check_location(int32_t lat, int32_t lng) const;
102
103
// removed the given index from the AIVDM buffer shift following elements
104
void buffer_shift(uint8_t i);
105
106
// find vessel index in existing list, if not then return new index if possible, returns true if index is valid
107
bool get_vessel_index(uint32_t mmsi, uint16_t &index, int32_t lat = 0, int32_t lon = 0) WARN_IF_UNUSED;
108
void clear_list_item(uint16_t index);
109
110
// decode the payload
111
bool payload_decode(const char *payload) WARN_IF_UNUSED;
112
113
// Apply scale to lat lon felids, avoiding integer overflow
114
int32_t scale_lat_lon(const int32_t val) const;
115
116
// decode specific message types
117
bool decode_position_report(const char *payload, uint8_t type) WARN_IF_UNUSED;
118
bool decode_base_station_report(const char *payload) WARN_IF_UNUSED;
119
bool decode_static_and_voyage_data(const char *payload) WARN_IF_UNUSED;
120
121
// read the specified bits from the char array each char giving 6 bits
122
void get_char(const char *payload, char *array, uint16_t low, uint16_t high);
123
uint32_t get_bits(const char *payload, uint16_t low, uint16_t high);
124
int32_t get_bits_signed(const char *payload, uint16_t low, uint16_t high);
125
// un-encode the ASCII payload armoring
126
uint8_t payload_char_decode(const char c);
127
128
// log a raw AIVDM message
129
void log_raw(const AIVDM *msg);
130
131
// try and decode NMEA message
132
bool decode(char c) WARN_IF_UNUSED;
133
134
// decode each term
135
bool decode_latest_term() WARN_IF_UNUSED;
136
137
// variables for decoding NMEA sentence
138
char _term[AIVDM_PAYLOAD_SIZE]; // buffer for the current term within the current sentence
139
uint8_t _term_offset; // offset within the _term buffer where the next character should be placed
140
uint8_t _term_number; // term index within the current sentence
141
uint8_t _checksum; // checksum accumulator
142
bool _term_is_checksum; // current term is the checksum
143
bool _sentence_valid; // is current sentence valid so far
144
bool _sentence_done; // true if this sentence has already been decoded
145
146
static AP_AIS *_singleton;
147
};
148
149
#endif // AP_AIS_ENABLED
150
151