diff --git a/src/shared/sensors/VN100/VN100.cpp b/src/shared/sensors/VN100/VN100.cpp index e55f1937fe1d7f91037eab57213b679e46fbd501..cd8d8c2704bbebeb5e573b022d9181ca36a1e4f6 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 d2d5c6c943c088c09a9eae55417276fa07a228ff..e6880433fc7ecce1a27ae17c078bcbea1e6ca2d9 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 2e93b1eff2ce610def3f0bfe76feed3f7418efc2..7b46b4cb8c3755bdfc09c4121634bed485771a23 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);