From 8e5cb22ac60f9a6513804bd034ce9b98e10b01d6 Mon Sep 17 00:00:00 2001
From: EmilioCorigliano <emilio.corigliano@mail.polimi.it>
Date: Mon, 3 Apr 2023 09:17:00 +0200
Subject: [PATCH] [VN100] Correctly using the usart bus instead of creating it
in the driver
---
src/shared/sensors/VN100/VN100.cpp | 25 +++++++++----------------
src/shared/sensors/VN100/VN100.h | 17 ++++++++---------
src/tests/sensors/test-vn100.cpp | 4 +++-
3 files changed, 20 insertions(+), 26 deletions(-)
diff --git a/src/shared/sensors/VN100/VN100.cpp b/src/shared/sensors/VN100/VN100.cpp
index e55f1937f..cd8d8c270 100644
--- a/src/shared/sensors/VN100/VN100.cpp
+++ b/src/shared/sensors/VN100/VN100.cpp
@@ -27,11 +27,10 @@
namespace Boardcore
{
-VN100::VN100(USARTType *portNumber, int baudRate, CRCOptions crc,
+VN100::VN100(USART &usart, int baudRate, CRCOptions crc,
uint16_t samplePeriod)
- : portNumber(portNumber), baudRate(baudRate), crc(crc)
+ : usart(usart), baudRate(baudRate), samplePeriod(samplePeriod), crc(crc)
{
- this->samplePeriod = samplePeriod;
}
bool VN100::init()
@@ -151,7 +150,7 @@ bool VN100::sampleRaw()
}
// Send the IMU sampling command
- serialInterface->writeString(preSampleImuString->c_str());
+ usart.writeString(preSampleImuString->c_str());
// Wait some time
// TODO dimension the time
@@ -200,9 +199,6 @@ bool VN100::closeAndReset()
// Free the recvString memory
delete recvString;
- // Free the serialInterface memory
- delete serialInterface;
-
return true;
}
@@ -241,7 +237,7 @@ VN100Data VN100::sampleData()
}
// Returns Quaternion, Magnetometer, Accelerometer and Gyro
- serialInterface->writeString(preSampleImuString->c_str());
+ usart.writeString(preSampleImuString->c_str());
// Wait some time
// TODO dimension the time
@@ -269,7 +265,7 @@ VN100Data VN100::sampleData()
// Returns Magnetometer, Accelerometer, Gyroscope, Temperature and Pressure
// (UNCOMPENSATED) DO NOT USE THESE MAGNETOMETER, ACCELEROMETER AND
// GYROSCOPE VALUES
- serialInterface->writeString(preSampleTempPressString->c_str());
+ usart.writeString(preSampleTempPressString->c_str());
// Wait some time
// TODO dimension the time
@@ -319,7 +315,7 @@ bool VN100::disableAsyncMessages(bool waitResponse)
bool VN100::configDefaultSerialPort()
{
// Initial default settings
- serialInterface = new USART(portNumber, 115200);
+ usart.setBaudrate(115200);
// Check correct serial init
return true;
@@ -342,11 +338,8 @@ bool VN100::configUserSerialPort()
return false;
}
- // Destroy the serial object
- delete serialInterface;
-
// I can open the serial with user's baud rate
- serialInterface = new USART(portNumber, baudRate);
+ usart.setBaudrate(baudRate);
// Check correct serial init
return true;
@@ -654,7 +647,7 @@ bool VN100::sendStringCommand(std::string command)
}
// I send the final command
- serialInterface->writeString(command.c_str());
+ usart.writeString(command.c_str());
// Wait some time
// TODO dimension the time
@@ -667,7 +660,7 @@ bool VN100::recvStringCommand(char *command, int maxLength)
{
int i = 0;
// Read the buffer
- if (!(serialInterface->readBlocking(command, maxLength)))
+ if (!usart.readBlocking(command, maxLength))
{
return false;
}
diff --git a/src/shared/sensors/VN100/VN100.h b/src/shared/sensors/VN100/VN100.h
index d2d5c6c94..e6880433f 100644
--- a/src/shared/sensors/VN100/VN100.h
+++ b/src/shared/sensors/VN100/VN100.h
@@ -80,13 +80,13 @@ public:
/**
* @brief Constructor.
*
- * @param USART port number.
+ * @param usart Serial bus used for the sensor.
* @param BaudRate different from the sensor's default [9600, 19200, 38400,
* 57600, 115200, 128000, 230400, 460800, 921600].
* @param Redundancy check option.
* @param samplePeriod Sampling period in ms
*/
- VN100(USARTType *portNumber = USART2, int baudRate = 921600,
+ VN100(USART &usart, int baudrate,
CRCOptions crc = CRCOptions::CRC_ENABLE_8,
uint16_t samplePeriod = 20);
@@ -238,8 +238,13 @@ private:
*/
uint16_t calculateChecksum16(uint8_t *message, int length);
- USARTType *portNumber;
+ /**
+ * @brief Serial interface that is needed to communicate
+ * with the sensor via ASCII codes.
+ */
+ USART &usart;
int baudRate;
+
uint16_t samplePeriod;
CRCOptions crc;
bool isInit = false;
@@ -266,12 +271,6 @@ private:
*/
unsigned int recvStringLength = 0;
- /**
- * @brief Serial interface that is needed to communicate
- * with the sensor via ASCII codes.
- */
- USARTInterface *serialInterface = nullptr;
-
/**
* @brief Mutex to synchronize the reading and writing of the threadSample
*/
diff --git a/src/tests/sensors/test-vn100.cpp b/src/tests/sensors/test-vn100.cpp
index 2e93b1eff..7b46b4cb8 100644
--- a/src/tests/sensors/test-vn100.cpp
+++ b/src/tests/sensors/test-vn100.cpp
@@ -31,7 +31,9 @@ int main()
{
VN100Data sample;
string sampleRaw;
- VN100 sensor{USART1, 921600, VN100::CRCOptions::CRC_ENABLE_16};
+ USART usart(USART1, 921600);
+ VN100 sensor{usart, 921600,
+ VN100::CRCOptions::CRC_ENABLE_16};
// Let the sensor start up
Thread::sleep(1000);
--
GitLab