0.4.1
Loading...
Searching...
No Matches
EspressifUartSensor.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 EspressifUartSensor.hpp
10/// @brief Class to read out Espressif Sensors
11/// @author R. Lintz (r-lintz@gmx.de) (master thesis)
12/// @author T. Topp (topp@ins.uni-stuttgart.de)
13/// @date 2024-01-08
14
15#pragma once
16
17#include <memory>
18
19#include "uart/sensors/sensors.hpp"
20
22{
23/// @brief Class to read out Espressif Sensors
25{
26 public:
27 /// @brief Constructor
28 /// @param[in] name Name of the Parent Node
29 explicit EspressifUartSensor(std::string name);
30
31 /// @brief Default constructor
33 /// @brief Destructor
35 /// @brief Copy constructor
37 /// @brief Move constructor
39 /// @brief Copy assignment operator
41 /// @brief Move assignment operator
43 /// @brief Arrow operator overload
44 uart::sensors::UartSensor* operator->() { return &_sensor; };
45
46 /// @brief Collects data bytes and searches for packages inside of them
47 /// @param[in] dataByte The next data byte
48 /// @return nullptr if no packet found yet, otherwise a pointer to the packet
49 std::unique_ptr<uart::protocol::Packet> findPacket(uint8_t dataByte);
50
51 static constexpr uint8_t BINARY_SYNC_CHAR_1 = 0xB5; ///< µ - First sync character which begins a new binary message
52 static constexpr uint8_t BINARY_SYNC_CHAR_2 = 0x62; ///< b - Second sync character which begins a new binary message
53
54 private:
55 /// Name of the Parent Node
56 const std::string _name;
57
58 /// UartSensor object which handles the UART interface
67
68 /// @brief Function which is called to find packets in the provided data buffer
69 /// @param[in] data Raw data buffer which has potential packets inside
70 /// @param[in] timestamp Timestamp then the data in the buffer was received
71 /// @param[in] dispatchPacket Function to call when a complete packet was found
72 /// @param[in] dispatchPacketUserData User data to forward to the dispatchPacket function
73 /// @param[in] userData User data provided when regisering this function. Should contain the sensor object
74 static void packetFinderFunction(const std::vector<uint8_t>& data,
75 const uart::xplat::TimeStamp& timestamp,
76 uart::sensors::UartSensor::ValidPacketFoundHandler dispatchPacket, void* dispatchPacketUserData,
77 void* userData);
78
79 /// @brief Function which is called to determine the packet type (ascii/binary)
80 /// @param[in] packet Packet to check the type of
81 /// @return The type of the packet
82 static uart::protocol::Packet::Type packetTypeFunction(const uart::protocol::Packet& packet);
83
84 /// @brief Function which is called to verify packet integrity
85 /// @param[in] packet Packet to calculate the checksum for
86 /// @return True if the packet is fault free
87 static bool checksumFunction(const uart::protocol::Packet& packet);
88
89 /// @brief Function which determines, if the packet is an Error Packet
90 /// @param[in] packet The packet to check
91 static bool isErrorFunction(const uart::protocol::Packet& packet);
92
93 /// @brief Function which determines, if the packet is a Response
94 /// @param[in] packet The packet to check
95 static bool isResponseFunction(const uart::protocol::Packet& packet);
96
97 static constexpr uart::Endianness ENDIANNESS = uart::Endianness::ENDIAN_LITTLE; ///< Endianess of the sensor
98 static constexpr size_t PACKET_HEADER_LENGTH = 2; ///< Length of the header of each packet
99
100 bool _currentlyBuildingBinaryPacket{ false }; ///< Flag if currently a binary packet is built
101
102 bool _binarySyncChar2Found{ false }; ///< Flag if the second binary end character was found
103 bool _binaryPayloadLength1Found{ false }; ///< Flag if the first byte of the payload length was found
104 bool _binaryPayloadLength2Found{ false }; ///< Flag if the second byte of the payload length was found
105
106 /// Payload length of the current packet
107 uint16_t _binaryPayloadLength{ 0 };
108
109 /// Buffer to collect messages till they are complete
110 std::vector<uint8_t> _buffer;
111
112 /// Used for correlating raw data with where the packet was found for the end user.
113 size_t _runningDataIndex{ 0 };
114 /// Amount of bytes remaining for a complete packet
116
117 /// @brief Resets the current message tracking
118 void resetTracking();
119};
120
121} // namespace NAV::vendor::espressif
size_t _runningDataIndex
Used for correlating raw data with where the packet was found for the end user.
uart::sensors::UartSensor _sensor
UartSensor object which handles the UART interface.
static constexpr uint8_t BINARY_SYNC_CHAR_2
b - Second sync character which begins a new binary message
std::unique_ptr< uart::protocol::Packet > findPacket(uint8_t dataByte)
Collects data bytes and searches for packages inside of them.
static constexpr uint8_t BINARY_SYNC_CHAR_1
µ - First sync character which begins a new binary message
EspressifUartSensor()=default
Default constructor.
static constexpr size_t PACKET_HEADER_LENGTH
Length of the header of each packet.
bool _currentlyBuildingBinaryPacket
Flag if currently a binary packet is built.
bool _binaryPayloadLength1Found
Flag if the first byte of the payload length was found.
EspressifUartSensor(std::string name)
Constructor.
static uart::protocol::Packet::Type packetTypeFunction(const uart::protocol::Packet &packet)
Function which is called to determine the packet type (ascii/binary)
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.
EspressifUartSensor(EspressifUartSensor &&)=delete
Move constructor.
bool _binarySyncChar2Found
Flag if the second binary end character was found.
static bool checksumFunction(const uart::protocol::Packet &packet)
Function which is called to verify packet integrity.
const std::string _name
Name of the Parent Node.
static bool isErrorFunction(const uart::protocol::Packet &packet)
Function which determines, if the packet is an Error Packet.
EspressifUartSensor & operator=(EspressifUartSensor &&)=delete
Move assignment operator.
size_t _numOfBytesRemainingForCompletePacket
Amount of bytes remaining for a complete packet.
EspressifUartSensor & operator=(const EspressifUartSensor &)=delete
Copy assignment operator.
uint16_t _binaryPayloadLength
Payload length of the current packet.
bool _binaryPayloadLength2Found
Flag if the second byte of the payload length was found.
static bool isResponseFunction(const uart::protocol::Packet &packet)
Function which determines, if the packet is a Response.
void resetTracking()
Resets the current message tracking.
std::vector< uint8_t > _buffer
Buffer to collect messages till they are complete.
EspressifUartSensor(const EspressifUartSensor &)=delete
Copy constructor.
uart::sensors::UartSensor * operator->()
Arrow operator overload.
static constexpr uart::Endianness ENDIANNESS
Endianess of the sensor.