Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Ardupilot
GitHub Repository: Ardupilot/ardupilot
Path: blob/master/libraries/AP_ExternalAHRS/AP_ExternalAHRS_SBG.h
6351 views
1
/*
2
Copyright (C) 2025 Kraus Hamdani Aerospace Inc. All rights reserved.
3
4
This program is free software: you can redistribute it and/or modify
5
it under the terms of the GNU General Public License as published by
6
the Free Software Foundation, either version 3 of the License, or
7
(at your option) any later version.
8
9
This program is distributed in the hope that it will be useful,
10
but WITHOUT ANY WARRANTY; without even the implied warranty of
11
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12
GNU General Public License for more details.
13
14
You should have received a copy of the GNU General Public License
15
along with this program. If not, see <http://www.gnu.org/licenses/>.
16
*/
17
/*
18
support for serial connected SBG INS system
19
*/
20
21
#pragma once
22
23
#include "AP_ExternalAHRS_config.h"
24
25
#if AP_EXTERNAL_AHRS_SBG_ENABLED
26
27
#include "AP_ExternalAHRS_backend.h"
28
#include "AP_ExternalAHRS_SBG_structs.h"
29
30
class AP_ExternalAHRS_SBG : public AP_ExternalAHRS_backend {
31
32
public:
33
AP_ExternalAHRS_SBG(AP_ExternalAHRS *frontend, AP_ExternalAHRS::state_t &state);
34
35
// get serial port number, -1 for not enabled
36
int8_t get_port(void) const override { return (uart == nullptr) ? -1 : port_num; }
37
38
// accessors for AP_AHRS
39
bool healthy(void) const override { return last_received_ms > 0 && (AP_HAL::millis() - last_received_ms < 500); }
40
bool initialised(void) const override { return setup_complete; };
41
bool pre_arm_check(char *failure_msg, uint8_t failure_msg_len) const override;
42
43
void get_filter_status(nav_filter_status &status) const override;
44
45
// TODO: implement this
46
bool get_variances(float &velVar, float &posVar, float &hgtVar, Vector3f &magVar, float &tasVar) const override { return false; };
47
48
// check for new data
49
void update() override { }
50
51
// Get model/type name
52
const char* get_name() const override { return "SBG"; }
53
54
protected:
55
56
uint8_t num_gps_sensors(void) const override {
57
return 1;
58
}
59
60
private:
61
62
static constexpr uint8_t SBG_PACKET_SYNC1 = 0xFF;
63
static constexpr uint8_t SBG_PACKET_SYNC2 = 0x5A;
64
static constexpr uint8_t SBG_PACKET_ETX = 0x33;
65
static constexpr uint16_t SBG_PACKET_PAYLOAD_SIZE_MAX = 100; // real sbgCom limit is 4086 but the largest packet we parse is SbgLogEkfNavData=72 bytes
66
static constexpr uint16_t SBG_PACKET_OVERHEAD = 9; // sync1, sync2, id, class, lenLSB, lenMSB, crcLSB, crcMSB, etx
67
68
struct Cached {
69
struct {
70
AP_ExternalAHRS::gps_data_message_t gps_data;
71
AP_ExternalAHRS::mag_data_message_t mag_data;
72
AP_ExternalAHRS::baro_data_message_t baro_data;
73
AP_ExternalAHRS::ins_data_message_t ins_data;
74
AP_ExternalAHRS::airspeed_data_message_t airspeed_data;
75
76
float baro_height;
77
78
uint32_t gps_ms;
79
uint32_t mag_ms;
80
uint32_t baro_ms;
81
uint32_t ins_ms;
82
uint32_t airspeed_ms;
83
} sensors;
84
85
struct {
86
SbgLogUtcData sbgLogUtcData;
87
SbgLogGpsVel sbgLogGpsVel;
88
SbgLogGpsPos sbgLogGpsPos;
89
SbgLogImuData sbgLogImuData;
90
SbgLogFastImuData sbgLogFastImuData;
91
SbgEComLogImuShort sbgEComLogImuShort;
92
SbgLogEkfEulerData sbgLogEkfEulerData;
93
SbgLogEkfQuatData sbgLogEkfQuatData;
94
SbgLogEkfNavData sbgLogEkfNavData; // biggest msg we care about, 72 bytes
95
SbgLogAirData sbgLogAirData;
96
SbgLogMag sbgLogMag;
97
SbgEComDeviceInfo sbgEComDeviceInfo;
98
} sbg;
99
} cached;
100
101
struct PACKED sbgMessage {
102
103
uint8_t msgid = 0;
104
uint8_t msgclass = 0;
105
uint16_t len = 0;
106
uint8_t data[SBG_PACKET_PAYLOAD_SIZE_MAX];
107
108
sbgMessage() {};
109
110
sbgMessage(const uint8_t msgClass_, const uint8_t msgId_) {
111
msgid = msgId_;
112
msgclass = msgClass_;
113
};
114
115
sbgMessage(const uint8_t msgClass_, const uint8_t msgId_, const uint8_t* payload, const uint16_t payload_len) {
116
msgid = msgId_;
117
msgclass = msgClass_;
118
119
if (payload_len > 0 && payload_len <= sizeof(data)) {
120
memcpy(&data, payload, payload_len);
121
len = payload_len;
122
}
123
};
124
};
125
126
enum class SBG_PACKET_PARSE_STATE : uint8_t {
127
SYNC1,
128
SYNC2,
129
MSG,
130
CLASS,
131
LEN1,
132
LEN2,
133
DATA,
134
CRC1,
135
CRC2,
136
ETX,
137
DROP_THIS_PACKET
138
};
139
140
141
struct SBG_PACKET_INBOUND_STATE {
142
SBG_PACKET_PARSE_STATE parser;
143
uint16_t data_count;
144
uint16_t crc;
145
sbgMessage msg;
146
uint16_t data_count_skip; // if we are parsing for a packet larger than we can accept, just stop parsing and wait for this many bytes to pass on by
147
} _inbound_state;
148
149
void handle_msg(const sbgMessage &msg);
150
static bool parse_byte(const uint8_t data, sbgMessage &msg, SBG_PACKET_INBOUND_STATE &inbound_state);
151
void update_thread();
152
bool check_uart();
153
static uint16_t calcCRC(const void *pBuffer, uint16_t bufferSize);
154
static bool send_sbgMessage(AP_HAL::UARTDriver *_uart, const sbgMessage &msg);
155
static void safe_copy_msg_to_object(uint8_t* dest, const uint16_t dest_len, const uint8_t* src, const uint16_t src_len);
156
static bool SbgEkfStatus_is_fullNav(const uint32_t ekfStatus);
157
158
static AP_GPS_FixType SbgGpsPosStatus_to_GpsFixType(const uint32_t gpsPosStatus);
159
160
uint32_t send_MagData_ms;
161
uint32_t send_AirData_ms;
162
uint32_t send_mag_error_last_ms;
163
uint32_t send_air_error_last_ms;
164
static bool send_MagData(AP_HAL::UARTDriver *_uart);
165
static bool send_AirData(AP_HAL::UARTDriver *_uart);
166
167
AP_HAL::UARTDriver *uart;
168
int8_t port_num;
169
uint32_t baudrate;
170
bool setup_complete;
171
uint32_t version_check_ms;
172
uint32_t last_received_ms;
173
};
174
175
#endif // AP_EXTERNAL_AHRS_SBG_ENABLED
176
177
178