diff --git a/CMakeLists.txt b/CMakeLists.txt index 0a747dc2..a5ab067e 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -116,6 +116,8 @@ add_app_subdirectory(${TEST_APPS_DIR}/test-quadrature64cpr) add_app_subdirectory(${TEST_APPS_DIR}/test-stress-can) add_app_subdirectory(${TEST_APPS_DIR}/test-units) add_app_subdirectory(${TEST_APPS_DIR}/test-watchdog) +add_app_subdirectory(${TEST_APPS_DIR}/test-CO2-sensor) +add_app_subdirectory(${TEST_APPS_DIR}/test-moisture-sensor) # Ensure APP was specified if ("$ENV{APP}" STREQUAL "") diff --git a/libs/sensors/CMakeLists.txt b/libs/sensors/CMakeLists.txt index 835cb5c9..e3eb9625 100644 --- a/libs/sensors/CMakeLists.txt +++ b/libs/sensors/CMakeLists.txt @@ -21,3 +21,13 @@ target_link_libraries(CurrentSensor Sensor mbed-os ) +add_library(PollingSensors STATIC) +target_sources(PollingSensors PRIVATE src/PollingSensors.cpp) +target_include_directories(PollingSensors PUBLIC include) +target_link_libraries(PollingSensors + PRIVATE + Sensor + mbed-os + Logger + ) + diff --git a/libs/sensors/include/PollingSensors.h b/libs/sensors/include/PollingSensors.h new file mode 100644 index 00000000..a2374ca6 --- /dev/null +++ b/libs/sensors/include/PollingSensors.h @@ -0,0 +1,43 @@ +#pragma once + +#include "Sensor.h" +#include "mbed.h" + +namespace Sensor { +class PollingSensors final : Sensor { + public: + PollingSensors(PinName moisture_in, PinName co2_in); + + float read() override {} + + float alternateRead() override {} + + bool getStatus() const override {} + + [[nodiscard]] bool reset() override {} + + [[nodiscard]] bool update() override {} + + /* Functions specific to sensor modules*/ + + /* This function returns Moisture sensor value + * 0-300 - dry + * 300-700 - humid + * 700-900 - wet + * Returns -1 for fault + * */ + float moisture_monitoring(); + + /* This function returns CO2 sensor value in ppm\ + * Returns -1 for fault and 0 for preheating + * */ + float C02_monitoring(); + + private: + /*Pins configuration for moisture sensing*/ + AnalogIn m_moisture_in_adc; + + /*Pins configuration for CO2 sensing*/ + AnalogIn m_CO2_in_adc; +}; +} // namespace Sensor \ No newline at end of file diff --git a/libs/sensors/src/PollingSensors.cpp b/libs/sensors/src/PollingSensors.cpp new file mode 100644 index 00000000..56a0208e --- /dev/null +++ b/libs/sensors/src/PollingSensors.cpp @@ -0,0 +1,34 @@ +#include "PollingSensors.h" + +#include "Logger.h" + +using namespace Sensor; + +PollingSensors::PollingSensors(PinName moisture_in, PinName co2_in) + : m_moisture_in_adc(moisture_in), m_CO2_in_adc(co2_in) {} + +/*https://www.dfrobot.com/product-599.html*/ +float PollingSensors::moisture_monitoring() { + float moisture_reading = (m_moisture_in_adc.read_voltage() * 950) / 4.2; + + if (moisture_reading < 0 && moisture_reading > 950) { + Utility::logger << "!!! MOISTURE FAULT:" << moisture_reading << "\n"; + return -1; + } + return moisture_reading; +} + +/*https://www.dfrobot.com/product-1549.html*/ +float PollingSensors::C02_monitoring() { + float CO2_reading = m_CO2_in_adc.read_voltage() * 1000; + + if (CO2_reading <= 0) { + Utility::logger << "!!! CO2 FAULT:" << CO2_reading << "\n"; + return -1; + } else if (CO2_reading < 400) { + Utility::logger << "!!! CO2 PREHEATING:" << CO2_reading << "\n"; + return 0; + } + float concentration = ((CO2_reading - 400) * 50.0) / 16.0; + return concentration; +} diff --git a/supported_build_configurations.yaml b/supported_build_configurations.yaml index 0a385884..2668f03c 100644 --- a/supported_build_configurations.yaml +++ b/supported_build_configurations.yaml @@ -105,5 +105,13 @@ test-stress-can: - ARM_REV2_2021 - GIMBAL_REV2_2021 - PDB_REV2_2021 + - SCIENCE_REV2_2021 + - UWRT_NUCLEO + +test-moisture-sensor: + - SCIENCE_REV2_2021 + - UWRT_NUCLEO + +test-CO2-sensor: - SCIENCE_REV2_2021 - UWRT_NUCLEO \ No newline at end of file diff --git a/test-apps/test-CO2-sensor/CMakeLists.txt b/test-apps/test-CO2-sensor/CMakeLists.txt new file mode 100644 index 00000000..85178bb9 --- /dev/null +++ b/test-apps/test-CO2-sensor/CMakeLists.txt @@ -0,0 +1,4 @@ +add_executable(test-CO2-sensor) +target_sources(test-CO2-sensor PRIVATE src/main.cpp) +target_link_libraries(test-CO2-sensor PRIVATE PollingSensors Logger) +mbed_set_post_build(test-CO2-sensor) diff --git a/test-apps/test-CO2-sensor/src/main.cpp b/test-apps/test-CO2-sensor/src/main.cpp new file mode 100644 index 00000000..be46e436 --- /dev/null +++ b/test-apps/test-CO2-sensor/src/main.cpp @@ -0,0 +1,20 @@ +#include "PollingSensors.h" +#include "PinNames.h" +#include "Logger.h" + +DigitalOut led1(LED1); + +int main() { + Sensor::PollingSensors CO2_sensor(PA_0,PA_1); + + while (1) { + + printf("\r\nReading CO2 Levels...\r\n"); + + Utility::logger << "CO2: " << ((unsigned int) CO2_sensor.C02_monitoring()) << "\r\n"; + + printf("\r\n-----------------------------\r\n"); + + ThisThread::sleep_for(100ms); + } +} diff --git a/test-apps/test-moisture-sensor/CMakeLists.txt b/test-apps/test-moisture-sensor/CMakeLists.txt new file mode 100644 index 00000000..bf396ddb --- /dev/null +++ b/test-apps/test-moisture-sensor/CMakeLists.txt @@ -0,0 +1,4 @@ +add_executable(test-moisture-sensor) +target_sources(test-moisture-sensor PRIVATE src/main.cpp) +target_link_libraries(test-moisture-sensor PRIVATE PollingSensors Logger) +mbed_set_post_build(test-moisture-sensor) diff --git a/test-apps/test-moisture-sensor/src/main.cpp b/test-apps/test-moisture-sensor/src/main.cpp new file mode 100644 index 00000000..10402263 --- /dev/null +++ b/test-apps/test-moisture-sensor/src/main.cpp @@ -0,0 +1,20 @@ +#include "PollingSensors.h" +#include "PinNames.h" +#include "Logger.h" + +DigitalOut led1(LED1); + +int main() { + Sensor::PollingSensors moisture_sensor(PA_0, PA_1); + + while (1) { + + printf("\r\nReading moisture Levels...\r\n"); + + Utility::logger << "Moisture: " << ((unsigned int) moisture_sensor.moisture_monitoring()) << "\r\n"; + + printf("\r\n-----------------------------\r\n"); + + ThisThread::sleep_for(100ms); + } +}