Skyward boardcore
Loading...
Searching...
No Matches
UBXFrame.h
Go to the documentation of this file.
1/* Copyright (c) 2022 Skyward Experimental Rocketry
2 * Authors: Damiano Amatruda, Alberto Nidasio
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a copy
5 * of this software and associated documentation files (the "Software"), to deal
6 * in the Software without restriction, including without limitation the rights
7 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8 * copies of the Software, and to permit persons to whom the Software is
9 * furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice shall be included in
12 * all copies or substantial portions of the Software.
13 *
14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
20 * THE SOFTWARE.
21 */
22
23#pragma once
24
25#include <stdint.h>
26
27#include <algorithm>
28#include <cstring>
29
30namespace Boardcore
31{
32
36enum class UBXMessage : uint16_t
37{
38 UBX_NAV_PVT = 0x0701, // Navigation position velocity time solution
39 UBX_ACK_NAK = 0x0005, // Message acknowledged
40 UBX_ACK_ACK = 0x0105, // Message not acknowledged
41 UBX_CFG_PRT = 0x0006, // Port configuration
42 UBX_CFG_MSG = 0x0106, // Set message rate
43 UBX_CFG_RST = 0x0406, // Reset receiver
44 UBX_CFG_RATE = 0x0806, // Navigation/measurement rate settings
45 UBX_CFG_NAV5 = 0x2406, // Navigation engine settings
46};
47
52{
53 static constexpr uint16_t MAX_PAYLOAD_LENGTH = 92;
54 static constexpr uint16_t MAX_FRAME_LENGTH = MAX_PAYLOAD_LENGTH + 8;
55 static constexpr uint8_t PREAMBLE[] = {0xb5, 0x62};
56 static constexpr uint8_t WAIT = 0xff;
57
58 uint8_t preamble[2];
59 uint16_t message;
60 uint16_t payloadLength;
62 uint8_t checksum[2];
63
64 UBXFrame() = default;
65
70 UBXFrame(UBXMessage message, const uint8_t* payload,
71 uint16_t payloadLength);
72
76 uint16_t getLength() const;
77
81 uint16_t getRealPayloadLength() const;
82
83 UBXMessage getMessage() const;
84
89 bool isValid() const;
90
96 void writePacked(uint8_t* frame) const;
97
105 void readPacked(const uint8_t* frame);
106
112 void calcChecksum(uint8_t* checksum) const;
113};
114
118struct UBXAckFrame : public UBXFrame
119{
123 struct __attribute__((packed)) Payload
124 {
125 uint16_t ackMessage;
126 };
127
128 Payload& getPayload() const;
129
131
135 bool isAck() const;
136
140 bool isNack() const;
141
145 bool isValid() const;
146};
147
151struct UBXPvtFrame : public UBXFrame
152{
153public:
157 struct __attribute__((packed)) Payload
158 {
159 uint32_t iTOW; // GPS time of week of the navigation epoch [ms]
160 uint16_t year; // Year (UTC) [y]
161 uint8_t month; // Month, range 1..12 (UTC) [month]
162 uint8_t day; // Day of month, range 1..31 (UTC) [d]
163 uint8_t hour; // Hour of day, range 0..23 (UTC) [h]
164 uint8_t min; // Minute of hour, range 0..59 (UTC) [min]
165 uint8_t sec; // Seconds of minute, range 0..60 (UTC) [s]
166 uint8_t valid; // Validity flags
167 uint32_t tAcc; // Time accuracy estimate (UTC) [ns]
168 int32_t nano; // Fraction of second, range -1e9 .. 1e9 (UTC) [ns]
169 uint8_t fixType; // GNSS fix Type
170 uint8_t flags; // Fix status flags
171 uint8_t flags2; // Additional flags
172 uint8_t numSV; // Number of satellites used in Nav Solution
173 int32_t lon; // Longitude {1e-7} [deg]
174 int32_t lat; // Latitude {1e-7} [deg]
175 int32_t height; // Height above ellipsoid [mm]
176 int32_t hMSL; // Height above mean sea level [mm]
177 uint32_t hAcc; // Horizontal accuracy estimate [mm]
178 uint32_t vAcc; // Vertical accuracy estimate [mm]
179 int32_t velN; // NED north velocity [mm/s]
180 int32_t velE; // NED east velocity [mm/s]
181 int32_t velD; // NED down velocity [mm/s]
182 int32_t gSpeed; // Ground Speed (2-D) [mm/s]
183 int32_t headMot; // Heading of motion (2-D) {1e-5} [deg]
184 uint32_t sAcc; // Speed accuracy estimate [mm/s]
185 uint32_t headAcc; // Heading accuracy estimate (both motion and
186 // vehicle) {1e-5} [deg]
187 uint16_t pDOP; // Position DOP {0.01}
188 uint16_t flags3; // Additional flags
189 uint8_t reserved0[4]; // Reserved
190 int32_t headVeh; // Heading of vehicle (2-D) {1e-5} [deg]
191 int16_t magDec; // Magnetic declination {1e-2} [deg]
192 uint16_t magAcc; // Magnetic declination accuracy {1e-2} [deg]
193 };
194
195 Payload& getPayload() const;
196
200 bool isValid() const;
201};
202
203inline UBXFrame::UBXFrame(UBXMessage message, const uint8_t* payload,
204 uint16_t payloadLength)
205 : message(static_cast<uint16_t>(message)), payloadLength(payloadLength)
206{
207 memcpy(preamble, PREAMBLE, 2);
208 if (payload != nullptr)
209 memcpy(this->payload, payload, getRealPayloadLength());
211}
212
213inline uint16_t UBXFrame::getLength() const { return payloadLength + 8; }
214
215inline uint16_t UBXFrame::getRealPayloadLength() const
216{
217 return std::min(payloadLength, MAX_PAYLOAD_LENGTH);
218}
219
221{
222 return static_cast<UBXMessage>(message);
223}
224
225inline bool UBXFrame::isValid() const
226{
227 if (memcmp(preamble, PREAMBLE, 2) != 0)
228 return false;
229
231 return false;
232
233 uint8_t validChecksum[2];
234 calcChecksum(validChecksum);
235 return memcmp(checksum, validChecksum, 2) == 0;
236}
237
238inline void UBXFrame::writePacked(uint8_t* frame) const
239{
240 memcpy(frame, preamble, 2);
241 memcpy(&frame[2], &message, 2);
242 memcpy(&frame[4], &payloadLength, 2);
243 memcpy(&frame[6], payload, getRealPayloadLength());
244 memcpy(&frame[6 + payloadLength], checksum, 2);
245}
246
247inline void UBXFrame::readPacked(const uint8_t* frame)
248{
249 memcpy(preamble, frame, 2);
250 memcpy(&message, &frame[2], 2);
251 memcpy(&payloadLength, &frame[4], 2);
252 memcpy(payload, &frame[6], getRealPayloadLength());
253 memcpy(checksum, &frame[6 + payloadLength], 2);
254}
255
256inline void UBXFrame::calcChecksum(uint8_t* checksum) const
257{
258 uint8_t data[getRealPayloadLength() + 4];
259 memcpy(data, &message, 2);
260 memcpy(&data[2], &payloadLength, 2);
261 memcpy(&data[4], payload, getRealPayloadLength());
262
263 checksum[0] = 0;
264 checksum[1] = 0;
265
266 for (size_t i = 0; i < sizeof(data); ++i)
267 {
268 checksum[0] += data[i];
269 checksum[1] += checksum[0];
270 }
271}
272
273inline UBXAckFrame::Payload& UBXAckFrame::getPayload() const
274{
275 return (Payload&)payload;
276}
277
279{
280 return static_cast<UBXMessage>(getPayload().ackMessage);
281}
282
283inline bool UBXAckFrame::isAck() const
284{
286}
287
288inline bool UBXAckFrame::isNack() const
289{
291}
292
293inline bool UBXAckFrame::isValid() const
294{
295 return UBXFrame::isValid() && (isAck() || isNack());
296}
297
298inline UBXPvtFrame::Payload& UBXPvtFrame::getPayload() const
299{
300 return (Payload&)payload;
301}
302
303inline bool UBXPvtFrame::isValid() const
304{
306}
307
308} // namespace Boardcore
Driver for the VN100S IMU.
UBXMessage
UBX messages enumeration.
Definition UBXFrame.h:37
UBX frames UBX-ACK-ACK and UBX-ACK-NAK.
Definition UBXFrame.h:119
bool isNack() const
Tells whether the frame is a nak.
Definition UBXFrame.h:288
Payload & getPayload() const
Definition UBXFrame.h:273
struct __attribute__((packed)) Payload
Payload of UBX frames UBX-ACK-ACK and UBX-ACK-NAK.
Definition UBXFrame.h:123
bool isValid() const
Tells whether the frame is an ack frame.
Definition UBXFrame.h:293
bool isAck() const
Tells whether the frame is an ack.
Definition UBXFrame.h:283
UBXMessage getAckMessage() const
Definition UBXFrame.h:278
Generic UBX frame.
Definition UBXFrame.h:52
void calcChecksum(uint8_t *checksum) const
Computes the frame checksum.
Definition UBXFrame.h:256
UBXMessage getMessage() const
Definition UBXFrame.h:220
uint8_t preamble[2]
Definition UBXFrame.h:58
static constexpr uint16_t MAX_PAYLOAD_LENGTH
Definition UBXFrame.h:53
uint16_t getLength() const
Return the total frame length.
Definition UBXFrame.h:213
static constexpr uint8_t WAIT
Definition UBXFrame.h:56
uint16_t payloadLength
Definition UBXFrame.h:60
static constexpr uint16_t MAX_FRAME_LENGTH
Definition UBXFrame.h:54
void writePacked(uint8_t *frame) const
Writes the current message into the given array.
Definition UBXFrame.h:238
bool isValid() const
Tells whether the current frame is valid or not. Checks the preamble and the checksum.
Definition UBXFrame.h:225
uint16_t getRealPayloadLength() const
Return the stored payload length.
Definition UBXFrame.h:215
static constexpr uint8_t PREAMBLE[]
Definition UBXFrame.h:55
uint8_t payload[MAX_PAYLOAD_LENGTH]
Definition UBXFrame.h:61
uint8_t checksum[2]
Definition UBXFrame.h:62
void readPacked(const uint8_t *frame)
Reads a raw frame.
Definition UBXFrame.h:247
UBX frame UBX-NAV-PVT.
Definition UBXFrame.h:152
Payload & getPayload() const
Definition UBXFrame.h:298
bool isValid() const
Tells whether the frame is an ack frame.
Definition UBXFrame.h:303
struct __attribute__((packed)) Payload
Payload of UBX frame UBX-NAV-PVT.
Definition UBXFrame.h:157