From dfb56cfdbc226e370976416e3b65c4e38b857cc5 Mon Sep 17 00:00:00 2001 From: Alberto Nidasio <alberto.nidasio@skywarder.eu> Date: Thu, 1 Jun 2023 12:38:15 +0200 Subject: [PATCH] [MS5803] Fixed temperature conversion --- src/shared/drivers/i2c/I2C.h | 3 ++ src/shared/sensors/MS5803/MS5803.cpp | 4 +- src/shared/sensors/MS5803/MS5803I2C.cpp | 4 +- src/tests/sensors/test-bmp280-i2c.cpp | 32 +++++----------- src/tests/sensors/test-ms5803-i2c.cpp | 51 +++++++++---------------- 5 files changed, 32 insertions(+), 62 deletions(-) diff --git a/src/shared/drivers/i2c/I2C.h b/src/shared/drivers/i2c/I2C.h index f9b7d01c5..507239b10 100644 --- a/src/shared/drivers/i2c/I2C.h +++ b/src/shared/drivers/i2c/I2C.h @@ -20,7 +20,10 @@ * THE SOFTWARE. */ +#pragma once + #include "I2CDriver.h" + namespace Boardcore { diff --git a/src/shared/sensors/MS5803/MS5803.cpp b/src/shared/sensors/MS5803/MS5803.cpp index 681d662ee..a7e38cdfa 100644 --- a/src/shared/sensors/MS5803/MS5803.cpp +++ b/src/shared/sensors/MS5803/MS5803.cpp @@ -161,10 +161,8 @@ MS5803Data MS5803::updateData() (((((int64_t)rawPressure) * sens) / 2097152.0) - offs) / 32786.0; // Pressure in Pascal - temp /= 100.0f; - return MS5803Data(TimestampTimer::getTimestamp(), pressure, - lastTemperatureTimestamp, temp); + lastTemperatureTimestamp, temp / 100.0f); } } // namespace Boardcore diff --git a/src/shared/sensors/MS5803/MS5803I2C.cpp b/src/shared/sensors/MS5803/MS5803I2C.cpp index edb63bf95..e6e887b70 100644 --- a/src/shared/sensors/MS5803/MS5803I2C.cpp +++ b/src/shared/sensors/MS5803/MS5803I2C.cpp @@ -190,10 +190,8 @@ MS5803Data MS5803I2C::updateData() (((((int64_t)rawPressure) * sens) / 2097152.0) - offs) / 32786.0; // Pressure in Pascal - temp /= 100.0f; - return MS5803Data(TimestampTimer::getTimestamp(), pressure, - lastTemperatureTimestamp, temp); + lastTemperatureTimestamp, temp / 100.0f); } uint16_t MS5803I2C::readReg(uint8_t reg) diff --git a/src/tests/sensors/test-bmp280-i2c.cpp b/src/tests/sensors/test-bmp280-i2c.cpp index 58edd892e..a913521be 100644 --- a/src/tests/sensors/test-bmp280-i2c.cpp +++ b/src/tests/sensors/test-bmp280-i2c.cpp @@ -1,4 +1,4 @@ -/* Copyright (c) 2021 Skyward Experimental Rocketry +/* Copyright (c) 2023 Skyward Experimental Rocketry * Author: Alberto Nidasio * * Permission is hereby granted, free of charge, to any person obtaining a copy @@ -33,36 +33,24 @@ int main() { I2C bus(I2C3, scl, sda); BMP280I2C bmp280(bus); - bmp280.init(); - if (!bmp280.selfTest()) - printf("Self test failed!\n"); - - printf("Forced mode\n"); - for (int i = 0; i < 10; i++) + if (!bmp280.init()) { - bmp280.setSensorMode(BMP280I2C::FORCED_MODE); - - Thread::sleep(bmp280.getMaxMeasurementTime()); - - bmp280.sample(); - - printf("temp: %.2f DegC\tpress: %.2f hPa\n", - bmp280.getLastSample().temperature, - bmp280.getLastSample().pressure); + printf("Init failed\n"); + } - Thread::sleep(1000); + if (!bmp280.selfTest()) + { + printf("Self test failed\n"); } - printf("Normal mode\n"); - bmp280.setSensorMode(BMP280I2C::NORMAL_MODE); while (true) { bmp280.sample(); - printf("temp: %.2f DegC\tpress: %.2f Pa\n", - bmp280.getLastSample().temperature, - bmp280.getLastSample().pressure); + auto data = bmp280.getLastSample(); + printf("[%.2f]: %.2fPa %.2f°\n", data.pressureTimestamp / 1e6, + data.pressure, data.temperature); Thread::sleep(50); // 25Hz } diff --git a/src/tests/sensors/test-ms5803-i2c.cpp b/src/tests/sensors/test-ms5803-i2c.cpp index d8d90cc76..7a7b83ddb 100644 --- a/src/tests/sensors/test-ms5803-i2c.cpp +++ b/src/tests/sensors/test-ms5803-i2c.cpp @@ -1,5 +1,5 @@ -/* Copyright (c) 2018 Skyward Experimental Rocketry - * Author: Nuno Barcellos +/* Copyright (c) 2023 Skyward Experimental Rocketry + * Author: Alberto Nidasio * * Permission is hereby granted, free of charge, to any person obtaining a copy * of this software and associated documentation files (the "Software"), to deal @@ -27,47 +27,30 @@ using namespace miosix; GpioPin scl(GPIOA_BASE, 8); GpioPin sda(GPIOC_BASE, 9); -/** - * This test is intended to be run on the Death Stack X - */ int main() { - printf("Starting...\n"); - I2C bus(I2C3, scl, sda); - I2CDriver::I2CSlaveConfig slaveConfig{0x76, I2CDriver::Addressing::BIT7, - I2CDriver::Speed::STANDARD}; + MS5803I2C ms5803(bus, 10); - while (true) + if (!ms5803.init()) { - uint8_t reg = 0xD0; - uint8_t data; - if (bus.readRegister(slaveConfig, reg, data)) - printf("data: 0x%02x\n", data); - - delayMs(100); + printf("MS5803 Init failed\n"); } - // printf("2...\n"); - // MS5803I2C sensor(bus, 10); - - // printf("3...\n"); - - // if (!sensor.init()) - // { - // printf("MS5803 Init failed\n"); - // } - // printf("pressureTimestamp,press,temperatureTimestamp,temp\n"); + if (!ms5803.selfTest()) + { + printf("Self test failed\n"); + } - // while (true) - // { - // sensor.sample(); + while (true) + { + ms5803.sample(); - // MS5803Data data = sensor.getLastSample(); - // printf("%llu,%f,%llu,%f\n", data.pressureTimestamp, data.pressure, - // data.temperatureTimestamp, data.temperature); + auto data = ms5803.getLastSample(); + printf("[%.2f]: %.2fPa %.2f°\n", data.pressureTimestamp / 1e6, + data.pressure, data.temperature); - // Thread::sleep(20); - // } + Thread::sleep(50); // 25Hz + } } -- GitLab