Skyward boardcore
Loading...
Searching...
No Matches
VNCommonSerial.h
Go to the documentation of this file.
1/* Copyright (c) 2024 Skyward Experimental Rocketry
2 * Authors: Matteo Pignataro, Lorenzo Cucchi, Fabrizio Monti
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
26#include <drivers/usart/USART.h>
27#include <sensors/SensorData.h>
28
29namespace Boardcore
30{
31
33{
34public:
35 enum class CRCOptions : uint8_t
36 {
37 CRC_NO = 0x00,
38 CRC_ENABLE_8 = 0x08,
39 CRC_ENABLE_16 = 0x10
40 };
41
52 VNCommonSerial(USART& usart, int baudrate, const char* sensorName,
53 CRCOptions crc, const std::chrono::milliseconds timeout);
54
56
63 bool closeAndReset();
64
77 bool startHSIEstimator(uint8_t convergeRate);
78
84 bool stopHSIEstimator();
85
90 std::string getHSIEstimatorValues();
91
101 bool setMagnetometerCompensation(const Eigen::Matrix3f& c,
102 const Eigen::Vector3f& b);
103
113 bool setAccelerometerCompensation(const Eigen::Matrix3f& c,
114 const Eigen::Vector3f& b);
115
125 bool setGyroscopeCompensation(const Eigen::Matrix3f& c,
126 const Eigen::Vector3f& b);
127
134 bool saveConfiguration();
135
141
142protected:
151 uint8_t calculateChecksum8(const uint8_t* message, int length);
152
161 uint16_t calculateChecksum16(const uint8_t* message, int length);
162
171 bool verifyChecksum(char* command, int maxLength);
172
181 bool disableAsyncMessages(bool waitResponse);
182
190 bool setCrc(bool waitResponse = true);
191
196
203
212 bool verifyModelNumber(const char* expectedModelNumber);
213
222 template <typename T>
223 bool getBinaryOutput(T& binaryData, const char* const sampleCommand);
224
233 uint8_t checkErrorVN(const char* message);
234
247 bool sendStringCommand(std::string command);
248
258 bool recvStringCommand(char* command, int maxLength);
259
269 bool writeRegister(const std::string& command);
270
277
279
281
285 static const int DEFAULT_BAUDRATE = 115200;
286
290 static const uint8_t recvStringMaxDimension = 200;
291
295 std::array<char, recvStringMaxDimension> recvString;
296
300 uint8_t recvStringLength = 0;
301
302 bool isInit = false;
303
304private:
309 const std::chrono::milliseconds maxTimeout;
310};
311
312template <typename T>
314 const char* const sampleCommand)
315{
316 uint8_t initByte = 0;
317
318 // Remove any junk
320
321 usart.writeString(sampleCommand);
322
323 // Check the read of the 0xFA byte to find the start of the message
324 if (usart.readBlocking(&initByte, 1, maxTimeout) && initByte == 0xFA)
325 {
326 if (usart.readBlocking(&binaryData, sizeof(T), maxTimeout))
327 return true;
328 }
329
330 return false;
331}
332
333} // namespace Boardcore
Driver for STM32F4 low level USART/UART peripheral.
Definition USART.h:170
void writeString(const char *buffer)
Write a string to the serial, comprising the '\0' character.
Definition USART.cpp:564
void clearQueue()
Clears the rxQueue.
Definition USART.cpp:616
virtual bool readBlocking(void *buffer, size_t nBytes, std::chrono::nanoseconds timeout=std::chrono::nanoseconds::zero())
Blocking read operation to read nBytes until the data transfer is complete or the timeout is reached.
Definition USART.h:91
uint8_t checkErrorVN(const char *message)
Check if the message received from the sensor contains an error.
uint16_t calculateChecksum16(const uint8_t *message, int length)
Calculate the 16bit array on the given array.
bool getBinaryOutput(T &binaryData, const char *const sampleCommand)
Utility function used to retrieve the binary output from the sensor.
bool saveConfiguration()
Write the current register settings into non-volatile memory. Once the settings are stored in non-vol...
bool setCrc(bool waitResponse=true)
Sets the user selected crc method.
bool setMagnetometerCompensation(const Eigen::Matrix3f &c, const Eigen::Vector3f &b)
Set custom compensation parameters for the magnetometer.
bool startHSIEstimator(uint8_t convergeRate)
Start the real-time hard/soft iron calibration. The algorithm will continue until stopHSIEstimator() ...
VNCommonSerial(USART &usart, int baudrate, const char *sensorName, CRCOptions crc, const std::chrono::milliseconds timeout)
Constructor.
static const uint8_t recvStringMaxDimension
Maximum size of the receiving string.
bool verifyModelNumber(const char *expectedModelNumber)
Verify the model number of the sensor.
bool stopHSIEstimator()
Real-time hard/soft iron calibration algorithm is turned off.
bool disableAsyncMessages(bool waitResponse)
Disables the async messages that the sensor is default configured to send at 40Hz on startup.
static const int DEFAULT_BAUDRATE
Default baudrate value for the usart communication.
bool closeAndReset()
Method to reset the sensor to default values and to close the connection. Used if you need to close a...
std::array< char, recvStringMaxDimension > recvString
Buffer used to store the string received from the sensor.
bool restoreFactorySettings()
Restore the VN module’s factory default settings and reset the module.
bool recvStringCommand(char *command, int maxLength)
Receives a command from the sensor but swaps the first with a \0 to close the message.
uint8_t recvStringLength
Actual strlen() of the recvString.
bool setGyroscopeCompensation(const Eigen::Matrix3f &c, const Eigen::Vector3f &b)
Set custom compensation parameters for the gyroscope.
bool configUserSerialPort()
Configures the user defined serial communication.
void configDefaultSerialPort()
Configures the default serial communication.
bool verifyChecksum(char *command, int maxLength)
Method to verify the crc validity of a command.
bool sendStringCommand(std::string command)
Sends the command to the sensor with the correct checksum added so '*' symbol is not needed at the en...
bool writeRegister(const std::string &command)
Utility function used to set a register on the sensor. This function relies on sendStringCommand: DO ...
USART & usart
Serial interface that is needed to communicate with the sensor via ASCII codes.
bool setAccelerometerCompensation(const Eigen::Matrix3f &c, const Eigen::Vector3f &b)
Set custom compensation parameters for the accelerometer.
uint8_t calculateChecksum8(const uint8_t *message, int length)
Calculate the 8bit checksum on the given array.
This file includes all the types the logdecoder script will decode.