diff --git a/src/boards/Parafoil/Sensors/Sensors.cpp b/src/boards/Parafoil/Sensors/Sensors.cpp index a3352b24beec1b5040d1c17028696cdb3ec943d7..2426c74fe1edf5cb39ff5e67b6641ffd7c1b0aa7 100644 --- a/src/boards/Parafoil/Sensors/Sensors.cpp +++ b/src/boards/Parafoil/Sensors/Sensors.cpp @@ -113,6 +113,7 @@ BatteryVoltageSensorData Sensors::getBatteryVoltageLastSample() // return data; // } +// TODO check used task scheduler Sensors::Sensors(TaskScheduler* sched) : scheduler(sched), sensorsCounter(0) {} // TODO check calibration of gyro @@ -128,6 +129,7 @@ bool Sensors::start() lis3mdlInit(); h3lisInit(); lps22Init(); + lps22DevInit(); ubxGpsInit(); ads131Init(); internalADCInit(); @@ -300,8 +302,8 @@ void Sensors::h3lisInit() ([&]() -> SensorInfo { return manager->getSensorInfo(h3lis331dl); }); sensorsInit[sensorsCounter++] = h3lis331Status; } -void Sensors::lps22Init() +void Sensors::lps22Init() { ModuleManager& modules = ModuleManager::getInstance(); @@ -330,6 +332,36 @@ void Sensors::lps22Init() sensorsInit[sensorsCounter++] = lps22Status; } +void Sensors::lps22DevInit() +{ + ModuleManager& modules = ModuleManager::getInstance(); + miosix::GpioPin cs(GPIOG_BASE, 7); + cs.mode(miosix::Mode::OUTPUT); + cs.high(); + // Get the correct SPI configuration + SPIBusConfig config = LPS22DF::getDefaultSPIConfig(); + config.clockDivider = SPI::ClockDivider::DIV_16; + + // Configure the device + LPS22DF::Config sensorConfig; + sensorConfig.avg = LPS22DF_AVG; + sensorConfig.odr = LPS22DF_ODR; + + // Create sensor instance with configured parameters + lps22dfDev = + new LPS22DF(modules.get<Buses>()->spi1, cs, config, sensorConfig); + + // Emplace the sensor inside the map + SensorInfo info("LPS22DFDev", LPS22DF_PERIOD, + bind(&Sensors::lps22DevCallback, this)); + sensorMap.emplace(make_pair(lps22dfDev, info)); + + // used for the sensor state + // auto lps22DevStatus = + // ([&]() -> SensorInfo { return manager->getSensorInfo(lps22dfDev); }); + // sensorsInit[sensorsCounter++] = lps22DevStatus; +} + void Sensors::ubxGpsInit() { ModuleManager& modules = ModuleManager::getInstance(); @@ -460,11 +492,21 @@ void Sensors::h3lisCallback() H3LIS331DLData lastSample = h3lis331dl->getLastSample(); Logger::getInstance().log(lastSample); } + void Sensors::lps22Callback() { - LPS22DFData lastSample = lps22df->getLastSample(); + LPS22DF1_Data lastSample = + static_cast<LPS22DF1_Data>(lps22df->getLastSample()); + Logger::getInstance().log(lastSample); +} + +void Sensors::lps22DevCallback() +{ + LPS22DF2_Data lastSample = + static_cast<LPS22DF2_Data>(lps22dfDev->getLastSample()); Logger::getInstance().log(lastSample); } + void Sensors::ubxGpsCallback() { UBXGPSData lastSample = ubxGps->getLastSample(); diff --git a/src/boards/Parafoil/Sensors/Sensors.h b/src/boards/Parafoil/Sensors/Sensors.h index fbb7996d8fa2859647a75850439259a645b21a1e..7813c4472e4a3a9dfb8c4e81a672b53a9d71083e 100644 --- a/src/boards/Parafoil/Sensors/Sensors.h +++ b/src/boards/Parafoil/Sensors/Sensors.h @@ -37,6 +37,8 @@ #include <utils/ModuleManager/ModuleManager.hpp> +#include "SensorsData.h" + namespace Parafoil { @@ -103,6 +105,9 @@ private: void lps22Init(); void lps22Callback(); + void lps22DevInit(); + void lps22DevCallback(); + void ubxGpsInit(); void ubxGpsCallback(); @@ -119,6 +124,7 @@ private: Boardcore::LIS3MDL* lis3mdl = nullptr; Boardcore::H3LIS331DL* h3lis331dl = nullptr; Boardcore::LPS22DF* lps22df = nullptr; + Boardcore::LPS22DF* lps22dfDev = nullptr; Boardcore::UBXGPSSpi* ubxGps = nullptr; Boardcore::ADS131M08* ads131 = nullptr; Boardcore::InternalADC* internalADC = nullptr; @@ -130,6 +136,7 @@ private: // Mutexes for sampling miosix::FastMutex lis3mdlMutex; miosix::FastMutex lps22Mutex; + miosix::FastMutex lps22DevMutex; miosix::FastMutex h3lisMutex; miosix::FastMutex bmx160Mutex; miosix::FastMutex bmx160WithCorrectionMutex; diff --git a/src/boards/Parafoil/Sensors/SensorsData.h b/src/boards/Parafoil/Sensors/SensorsData.h new file mode 100644 index 0000000000000000000000000000000000000000..d9ea92d2562891a95cba3d5638c65e6da47f9865 --- /dev/null +++ b/src/boards/Parafoil/Sensors/SensorsData.h @@ -0,0 +1,68 @@ + +/* Copyright (c) 2023 Skyward Experimental Rocketry + * Author: Matteo Pignataro, Federico Mandelli + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ +#pragma once +#include <sensors/LPS22DF/LPS22DFData.h> +namespace Parafoil +{ +struct LPS22DF1_Data : Boardcore::LPS22DFData +{ + explicit LPS22DF1_Data(const Boardcore::LPS22DFData& data) + : Boardcore::LPS22DFData(data) + { + } + + LPS22DF1_Data() {} + + static std::string header() + { + return "pressureTimestamp,pressure,temperatureTimestamp,temperature\n "; + } + + void print(std::ostream& os) const + { + os << pressureTimestamp << "," << pressure << "," + << temperatureTimestamp << "," << temperature << "\n"; + } +}; + +struct LPS22DF2_Data : Boardcore::LPS22DFData +{ + explicit LPS22DF2_Data(const Boardcore::LPS22DFData& data) + : Boardcore::LPS22DFData(data) + { + } + + LPS22DF2_Data() {} + + static std::string header() + { + return "pressureTimestamp,pressure,temperatureTimestamp,temperature\n "; + } + + void print(std::ostream& os) const + { + os << pressureTimestamp << "," << pressure << "," + << temperatureTimestamp << "," << temperature << "\n"; + } +}; +} // namespace Parafoil