Skip to content

Test apps for moisture and CO2 sensors #434

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 14 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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 "")
Expand Down
10 changes: 10 additions & 0 deletions libs/sensors/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
)

43 changes: 43 additions & 0 deletions libs/sensors/include/PollingSensors.h
Original file line number Diff line number Diff line change
@@ -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
34 changes: 34 additions & 0 deletions libs/sensors/src/PollingSensors.cpp
Original file line number Diff line number Diff line change
@@ -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;
}
8 changes: 8 additions & 0 deletions supported_build_configurations.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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
4 changes: 4 additions & 0 deletions test-apps/test-CO2-sensor/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -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)
20 changes: 20 additions & 0 deletions test-apps/test-CO2-sensor/src/main.cpp
Original file line number Diff line number Diff line change
@@ -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);
}
}
4 changes: 4 additions & 0 deletions test-apps/test-moisture-sensor/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -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)
20 changes: 20 additions & 0 deletions test-apps/test-moisture-sensor/src/main.cpp
Original file line number Diff line number Diff line change
@@ -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);
}
}