33 const std::chrono::milliseconds timeout)
34 : usart(usart), baudRate(baudrate), crc(crc),
35 logger(
Logging::getLogger(sensorName)), maxTimeout(timeout)
44 uint8_t result = 0x00;
47 for (i = 0; i < length; i++)
59 uint16_t result = 0x0000;
62 for (i = 0; i < length; i++)
64 result = (uint8_t)(result >> 8) | (result << 8);
66 result ^= (uint8_t)(result & 0xff) >> 4;
67 result ^= result << 12;
68 result ^= (result & 0x00ff) << 5;
76 int checksumOffset = 0;
79 while (checksumOffset < length && command[checksumOffset] !=
'*')
82 if (checksumOffset == length)
85 TRACE(
"No checksum in the command!\n");
92 if (length != checksumOffset + 5)
94 TRACE(
"16 bit Checksum wrong length: %d != %d --> %s\n", length,
95 checksumOffset + 5, command);
101 if (strtol(command + checksumOffset + 1, NULL, 16) !=
104 TRACE(
"Different checksum: %s\n", command);
110 if (length != checksumOffset + 3)
112 TRACE(
"8 bit Checksum wrong length: %d != %d --> %s\n", length,
113 checksumOffset + 3, command);
119 if (strtol(command + checksumOffset + 1, NULL, 16) !=
122 TRACE(
"Different checksum: %s\n", command);
133 std::string command =
"VNWRG,06,00";
164 command =
"VNWRG,30,0,0,0,0,3,0,1";
171 command =
"VNWRG,30,0,0,0,0,1,0,1";
218 std::string command = fmt::format(
"{}{}",
"VNWRG,5,",
baudRate);
234 const int modelNumberOffset = 10;
245 miosix::Thread::sleep(100);
253 if (strncmp(expectedModelNumber,
recvString.data() + modelNumberOffset,
254 strlen(expectedModelNumber)) != 0)
272 if (strncmp(message,
"$VNERR,", 7) == 0)
275 int errorCode = atoi(&message[7]);
298 D(assert((convergeRate >= 1 && convergeRate <= 5) &&
299 "convergeRate must be between 1 and 5"));
303 const uint8_t hsiMode = 1;
307 const uint8_t hsiOutput = 3;
309 std::string command =
310 fmt::format(
"VNWRG,44,{},{},{}", hsiMode, hsiOutput, convergeRate);
319 const uint8_t hsiMode = 0;
323 const uint8_t hsiOutput = 3;
326 const uint8_t convergeRate = 5;
328 std::string command =
329 fmt::format(
"VNWRG,44,{},{},{}", hsiMode, hsiOutput, convergeRate);
341 LOG_ERR(
logger,
"getHSIEstimatorValues: unable to send string command");
345 miosix::Thread::sleep(100);
350 "getHSIEstimatorValues: unable to receive string command");
357 "getHSIEstimatorValues: checksum verification failed: {}",
366 const Eigen::Vector3f& b)
368 std::string command =
369 fmt::format(
"VNWRG,23,{},{},{},{},{},{},{},{},{},{},{},{}", c(0, 0),
370 c(0, 1), c(0, 2), c(1, 0), c(1, 1), c(1, 2), c(2, 0),
371 c(2, 1), c(2, 2), b(0), b(1), b(2));
377 const Eigen::Vector3f& b)
379 std::string command =
380 fmt::format(
"VNWRG,25,{},{},{},{},{},{},{},{},{},{},{},{}", c(0, 0),
381 c(0, 1), c(0, 2), c(1, 0), c(1, 1), c(1, 2), c(2, 0),
382 c(2, 1), c(2, 2), b(0), b(1), b(2));
388 const Eigen::Vector3f& b)
390 std::string command =
391 fmt::format(
"VNWRG,84,{},{},{},{},{},{},{},{},{},{},{},{}", c(0, 0),
392 c(0, 1), c(0, 2), c(1, 0), c(1, 1), c(1, 2), c(2, 0),
393 c(2, 1), c(2, 2), b(0), b(1), b(2));
405 LOG_ERR(
logger,
"saveConfiguration: unable to send string command");
410 miosix::Thread::sleep(500);
414 LOG_WARN(
logger,
"saveConfiguration: unable to receive string command");
420 LOG_ERR(
logger,
"saveConfiguration: checksum verification failed: {}",
436 "restoreFactorySettings: unable to send string command");
440 miosix::Thread::sleep(100);
445 "restoreFactorySettings: unable to receive string command");
452 "restoreFactorySettings: checksum verification failed: {}",
458 miosix::Thread::sleep(2000);
474 command = fmt::format(
"{}{}{}{}",
"$", command,
"*", checksum);
485 command = fmt::format(
"{}{}{}{}",
"$", command,
"*", checksum);
491 command = fmt::format(
"{}{}{}",
"$", command,
"*XX\n");
505 float waitTime = command.size() * 8000;
507 waitTime = ceil(waitTime);
509 miosix::Thread::sleep(waitTime);
522 while (i < maxLength && command[i] !=
'\n')
539 LOG_ERR(
logger,
"Write register failed: could not send the command");
545 LOG_ERR(
logger,
"Write register failed: recvStringCommand() failed");
#define LOG_WARN(logger,...)
#define LOG_ERR(logger,...)
Driver for STM32F4 low level USART/UART peripheral.
void writeString(const char *buffer)
Write a string to the serial, comprising the '\0' character.
void clearQueue()
Clears the rxQueue.
void setBaudrate(int baudrate)
Set the baudrate in the BRR register.
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.
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 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...
std::string getHSIEstimatorValues()
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.
Driver for the VN100S IMU.