CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutSign UpSign In
Ardupilot

Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place. Commercial Alternative to JupyterHub.

GitHub Repository: Ardupilot/ardupilot
Path: blob/master/libraries/AP_AIS/AP_AIS.h
Views: 1798
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
// removed the given index from the AIVDM buffer shift following elements
98
void buffer_shift(uint8_t i);
99
100
// find vessel in existing list, if not then return NEW_NOTHROW index if possible
101
bool get_vessel_index(uint32_t mmsi, uint16_t &index, uint32_t lat = 0, uint32_t lon = 0) WARN_IF_UNUSED;
102
void clear_list_item(uint16_t index);
103
104
// decode the payload
105
bool payload_decode(const char *payload) WARN_IF_UNUSED;
106
107
// decode specific message types
108
bool decode_position_report(const char *payload, uint8_t type) WARN_IF_UNUSED;
109
bool decode_base_station_report(const char *payload) WARN_IF_UNUSED;
110
bool decode_static_and_voyage_data(const char *payload) WARN_IF_UNUSED;
111
112
// read the specified bits from the char array each char giving 6 bits
113
void get_char(const char *payload, char *array, uint16_t low, uint16_t high);
114
uint32_t get_bits(const char *payload, uint16_t low, uint16_t high);
115
int32_t get_bits_signed(const char *payload, uint16_t low, uint16_t high);
116
// un-encode the ASCII payload armoring
117
uint8_t payload_char_decode(const char c);
118
119
// log a raw AIVDM message
120
void log_raw(const AIVDM *msg);
121
122
// try and decode NMEA message
123
bool decode(char c) WARN_IF_UNUSED;
124
125
// decode each term
126
bool decode_latest_term() WARN_IF_UNUSED;
127
128
// variables for decoding NMEA sentence
129
char _term[AIVDM_PAYLOAD_SIZE]; // buffer for the current term within the current sentence
130
uint8_t _term_offset; // offset within the _term buffer where the next character should be placed
131
uint8_t _term_number; // term index within the current sentence
132
uint8_t _checksum; // checksum accumulator
133
bool _term_is_checksum; // current term is the checksum
134
bool _sentence_valid; // is current sentence valid so far
135
bool _sentence_done; // true if this sentence has already been decoded
136
137
static AP_AIS *_singleton;
138
};
139
140
#endif // AP_AIS_ENABLED
141
142