0.4.1
Loading...
Searching...
No Matches
EspressifUartSensor.cpp
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
10
12#include "util/Logger.hpp"
13
15 : _name(std::move(name)), _buffer(uart::sensors::UartSensor::DefaultReadBufferSize)
16{
18}
19
31
32std::unique_ptr<uart::protocol::Packet> NAV::vendor::espressif::EspressifUartSensor::findPacket(uint8_t dataByte)
33{
34 if (_buffer.size() == _buffer.capacity())
35 {
36 // Buffer is full
38 LOG_ERROR("{}: Discarding current packet, because buffer is full.", _name);
39 }
40
42 {
43 // This byte must be the start char
44 if (dataByte == BINARY_SYNC_CHAR_1)
45 {
48 _buffer.push_back(dataByte);
49 }
50 }
52 {
53 _buffer.push_back(dataByte);
54
56 {
57 // This byte must be the second sync char
58 if (dataByte == BINARY_SYNC_CHAR_2)
59 {
61 }
62 else
63 {
65 }
66 }
68 {
70 _binaryPayloadLength = static_cast<uint16_t>(dataByte);
71 }
73 {
75 _binaryPayloadLength |= static_cast<uint16_t>(static_cast<uint16_t>(dataByte) << 8U);
78 LOG_DATA("{}: Binary packet: payload length={}", _name, _binaryPayloadLength);
79 }
80 else
81 {
82 // We are currently collecting data for our packet.
84
86 {
87 // We have a possible binary packet!
88 auto p = std::make_unique<uart::protocol::Packet>(_buffer, &_sensor);
89
90 if (p->isValid())
91 {
92 // We have a valid binary packet!!!.
94 return p;
95 }
96 // Invalid packet!
97 LOG_ERROR("{}: Invalid binary packet: payload length={}", _name, _binaryPayloadLength);
99 }
100 }
101 }
102
103 return nullptr;
104}
105
106void NAV::vendor::espressif::EspressifUartSensor::packetFinderFunction(const std::vector<uint8_t>& data, const uart::xplat::TimeStamp& timestamp, uart::sensors::UartSensor::ValidPacketFoundHandler dispatchPacket, void* dispatchPacketUserData, void* userData)
107{
108 auto* sensor = static_cast<EspressifUartSensor*>(userData);
109
110 for (size_t i = 0; i < data.size(); i++, sensor->_runningDataIndex++)
111 {
112 auto packetPointer = sensor->findPacket(data.at(i));
113
114 if (packetPointer != nullptr)
115 {
116 uart::protocol::Packet packet = *packetPointer;
117 dispatchPacket(dispatchPacketUserData, packet, sensor->_runningDataIndex, timestamp);
118 }
119 }
120}
121
122uart::protocol::Packet::Type NAV::vendor::espressif::EspressifUartSensor::packetTypeFunction(const uart::protocol::Packet& packet)
123{
124 if (packet.getRawData().at(0) == BINARY_SYNC_CHAR_1)
125 {
126 if (packet.getRawData().at(1) == BINARY_SYNC_CHAR_2)
127 {
128 return uart::protocol::Packet::Type::TYPE_BINARY;
129 }
130 }
131
132 return uart::protocol::Packet::Type::TYPE_UNKNOWN;
133}
134
136{
137 if (packet.getRawDataLength() <= 8)
138 {
139 return false;
140 }
141
142 std::pair<uint8_t, uint8_t> checksum = ublox::checksumUBX(packet.getRawData());
143
144 return packet.getRawData().at(packet.getRawDataLength() - 2) == checksum.first
145 && packet.getRawData().at(packet.getRawDataLength() - 1) == checksum.second;
146
147 LOG_CRITICAL("Can't calculate checksum of packet with unknown type");
148 return false;
149}
150
151bool NAV::vendor::espressif::EspressifUartSensor::isErrorFunction([[maybe_unused]] const uart::protocol::Packet& packet)
152{
153 return false;
154}
155
156bool NAV::vendor::espressif::EspressifUartSensor::isResponseFunction([[maybe_unused]] const uart::protocol::Packet& packet)
157{
158 return false;
159}
Class to read out Espressif Sensors.
Helper Functions to work with Espressif Sensors.
Utility class for logging to console and file.
#define LOG_CRITICAL(...)
Critical Event, which causes the program to work entirely and throws an exception.
Definition Logger.hpp:75
#define LOG_DATA
All output which occurs repeatedly every time observations are received.
Definition Logger.hpp:29
#define LOG_ERROR
Error occurred, which stops part of the program to work, but not everything.
Definition Logger.hpp:73
Abstract Uart Sensor Class.
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.
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.
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.
size_t _numOfBytesRemainingForCompletePacket
Amount of bytes remaining for a complete packet.
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.
static constexpr uart::Endianness ENDIANNESS
Endianess of the sensor.
std::pair< uint8_t, uint8_t > checksumUBX(const std::vector< uint8_t > &data)
Calculates the two UBX checksums for the provided data vector.
void move(std::vector< T > &v, size_t sourceIdx, size_t targetIdx)
Moves an element within a vector to a new position.
Definition Vector.hpp:27