0.4.1
Loading...
Searching...
No Matches
UbloxUartSensor.hpp
Go to the documentation of this file.
1// This file is part of INSTINCT, the INS Toolkit for Integrated
2// Navigation Concepts and Training by the Institute of Navigation of
3// the University of Stuttgart, Germany.
4//
5// This Source Code Form is subject to the terms of the Mozilla Public
6// License, v. 2.0. If a copy of the MPL was not distributed with this
7// file, You can obtain one at https://mozilla.org/MPL/2.0/.
8
9/// @file UbloxUartSensor.hpp
10/// @brief Class to read out Ublox Sensors
11/// @author T. Topp (topp@ins.uni-stuttgart.de)
12/// @date 2020-07-22
13
14#pragma once
15
16#include <memory>
17#include <vector>
18
19#include "uart/sensors/sensors.hpp"
20#include "util/Logger.hpp"
21
22namespace NAV::vendor::ublox
23{
24/// @brief Class to read out Ublox Sensors
26{
27 public:
28 /// @brief Constructor
29 /// @param[in] name Name of the Parent Node
30 explicit UbloxUartSensor(std::string name);
31
32 /// @brief Default constructor
33 UbloxUartSensor() = default;
34 /// @brief Destructor
35 ~UbloxUartSensor() = default;
36 /// @brief Copy constructor
38 /// @brief Move constructor
40 /// @brief Copy assignment operator
42 /// @brief Move assignment operator
44 /// @brief Arrow operator overload
45 uart::sensors::UartSensor* operator->() { return &_sensor; };
46
47 /// @brief Collects data bytes and searches for packages inside of them
48 /// @param[in] dataByte The next data byte
49 /// @return nullptr if no packet found yet, otherwise a pointer to the packet
50 std::unique_ptr<uart::protocol::Packet> findPacket(uint8_t dataByte);
51
52 static constexpr uint8_t BINARY_SYNC_CHAR_1 = 0xB5; ///< µ - First sync character which begins a new binary message
53 static constexpr uint8_t BINARY_SYNC_CHAR_2 = 0x62; ///< b - Second sync character which begins a new binary message
54 static constexpr uint8_t ASCII_START_CHAR = '$'; ///< Ascii character which begins a new ascii message
55
56 private:
57 /// Name of the Parent Node
58 const std::string _name;
59
60 /// UartSensor object which handles the UART interface
69
70 /// @brief Function which is called to find packets in the provided data buffer
71 /// @param[in] data Raw data buffer which has potential packets inside
72 /// @param[in] timestamp Timestamp then the data in the buffer was received
73 /// @param[in] dispatchPacket Function to call when a complete packet was found
74 /// @param[in] dispatchPacketUserData User data to forward to the dispatchPacket function
75 /// @param[in] userData User data provided when regisering this function. Should contain the sensor object
76 static void packetFinderFunction(const std::vector<uint8_t>& data,
77 const uart::xplat::TimeStamp& timestamp,
78 uart::sensors::UartSensor::ValidPacketFoundHandler dispatchPacket, void* dispatchPacketUserData,
79 void* userData);
80
81 /// @brief Function which is called to determine the packet type (ascii/binary)
82 /// @param[in] packet Packet to check the type of
83 /// @return The type of the packet
84 static uart::protocol::Packet::Type packetTypeFunction(const uart::protocol::Packet& packet);
85
86 /// @brief Function which is called to verify packet integrity
87 /// @param[in] packet Packet to calculate the checksum for
88 /// @return True if the packet is fault free
89 static bool checksumFunction(const uart::protocol::Packet& packet);
90
91 /// @brief Function which determines, if the packet is an Error Packet
92 /// @param[in] packet The packet to check
93 static bool isErrorFunction(const uart::protocol::Packet& packet);
94
95 /// @brief Function which determines, if the packet is a Response
96 /// @param[in] packet The packet to check
97 static bool isResponseFunction(const uart::protocol::Packet& packet);
98
99 static constexpr uart::Endianness ENDIANNESS = uart::Endianness::ENDIAN_LITTLE; ///< Endianess of the sensor
100 static constexpr size_t PACKET_HEADER_LENGTH = 2; ///< Length of the header of each packet
101 static constexpr uint8_t ASCII_END_CHAR_1 = '\r'; ///< First Ascii End character
102 static constexpr uint8_t ASCII_END_CHAR_2 = '\n'; ///< Second Ascii End character
103 static constexpr uint8_t ASCII_ESCAPE_CHAR = '\0'; ///< Ascii Escape charater
104
105 bool _currentlyBuildingAsciiPacket{ false }; ///< Flag if currently a ascii packet is built
106 bool _currentlyBuildingBinaryPacket{ false }; ///< Flag if currently a binary packet is built
107
108 bool _asciiEndChar1Found{ false }; ///< Flag if the first ascii end character was found
109 bool _binarySyncChar2Found{ false }; ///< Flag if the second binary end character was found
110 bool _binaryMsgClassFound{ false }; ///< Flag if the message class was found
111 bool _binaryMsgIdFound{ false }; ///< Flag if the message id was found
112 bool _binaryPayloadLength1Found{ false }; ///< Flag if the first byte of the payload length was found
113 bool _binaryPayloadLength2Found{ false }; ///< Flag if the second byte of the payload length was found
114
115 /// Message class of the current packet
116 uint8_t _binaryMsgClass{ 0 };
117 /// Message id of the current packet
118 uint8_t _binaryMsgId{ 0 };
119 /// Payload length of the current packet
120 uint16_t _binaryPayloadLength{ 0 };
121
122 /// Buffer to collect messages till they are complete
123 std::vector<uint8_t> _buffer;
124
125 /// Used for correlating raw data with where the packet was found for the end user.
126 size_t _runningDataIndex{ 0 };
127 /// Amount of bytes remaining for a complete packet
129
130#if LOG_LEVEL <= LOG_LEVEL_DATA
131 /// Bytes which were not recognized as messages
132 std::vector<uint8_t> _unrecognizedBytes;
133#endif
134
135 /// @brief Resets the current message tracking
136 void resetTracking();
137};
138
139} // namespace NAV::vendor::ublox
Utility class for logging to console and file.
uint8_t _binaryMsgClass
Message class of the current packet.
static constexpr uint8_t BINARY_SYNC_CHAR_2
b - Second sync character which begins a new binary message
bool _binaryMsgIdFound
Flag if the message id was found.
std::unique_ptr< uart::protocol::Packet > findPacket(uint8_t dataByte)
Collects data bytes and searches for packages inside of them.
UbloxUartSensor & operator=(UbloxUartSensor &&)=delete
Move assignment operator.
uart::sensors::UartSensor * operator->()
Arrow operator overload.
static void packetFinderFunction(const std::vector< uint8_t > &data, const uart::xplat::TimeStamp &timestamp, uart::sensors::UartSensor::ValidPacketFoundHandler dispatchPacket, void *dispatchPacketUserData, void *userData)
Function which is called to find packets in the provided data buffer.
static constexpr uint8_t ASCII_END_CHAR_2
Second Ascii End character.
static bool isErrorFunction(const uart::protocol::Packet &packet)
Function which determines, if the packet is an Error Packet.
UbloxUartSensor()=default
Default constructor.
bool _binaryMsgClassFound
Flag if the message class was found.
bool _binaryPayloadLength2Found
Flag if the second byte of the payload length was found.
UbloxUartSensor & operator=(const UbloxUartSensor &)=delete
Copy assignment operator.
const std::string _name
Name of the Parent Node.
bool _asciiEndChar1Found
Flag if the first ascii end character was found.
void resetTracking()
Resets the current message tracking.
static constexpr uint8_t ASCII_ESCAPE_CHAR
Ascii Escape charater.
bool _currentlyBuildingAsciiPacket
Flag if currently a ascii packet is built.
static uart::protocol::Packet::Type packetTypeFunction(const uart::protocol::Packet &packet)
Function which is called to determine the packet type (ascii/binary)
uart::sensors::UartSensor _sensor
UartSensor object which handles the UART interface.
static constexpr uint8_t ASCII_END_CHAR_1
First Ascii End character.
static constexpr size_t PACKET_HEADER_LENGTH
Length of the header of each packet.
bool _currentlyBuildingBinaryPacket
Flag if currently a binary packet is built.
static constexpr uint8_t ASCII_START_CHAR
Ascii character which begins a new ascii message.
static constexpr uart::Endianness ENDIANNESS
Endianess of the sensor.
bool _binaryPayloadLength1Found
Flag if the first byte of the payload length was found.
UbloxUartSensor(const UbloxUartSensor &)=delete
Copy constructor.
static constexpr uint8_t BINARY_SYNC_CHAR_1
µ - First sync character which begins a new binary message
size_t _numOfBytesRemainingForCompletePacket
Amount of bytes remaining for a complete packet.
std::vector< uint8_t > _unrecognizedBytes
Bytes which were not recognized as messages.
UbloxUartSensor(std::string name)
Constructor.
~UbloxUartSensor()=default
Destructor.
uint16_t _binaryPayloadLength
Payload length of the current packet.
size_t _runningDataIndex
Used for correlating raw data with where the packet was found for the end user.
static bool checksumFunction(const uart::protocol::Packet &packet)
Function which is called to verify packet integrity.
static bool isResponseFunction(const uart::protocol::Packet &packet)
Function which determines, if the packet is a Response.
std::vector< uint8_t > _buffer
Buffer to collect messages till they are complete.
UbloxUartSensor(UbloxUartSensor &&)=delete
Move constructor.
uint8_t _binaryMsgId
Message id of the current packet.
bool _binarySyncChar2Found
Flag if the second binary end character was found.