From 33e0483b0684ecc44fbfcdc4d6446b32df4e7d1e Mon Sep 17 00:00:00 2001 From: Zoe Pfister <zoe.pfister@uibk.ac.at> Date: Fri, 10 Mar 2023 13:34:15 +0100 Subject: [PATCH] rename ForteRS485 class name, add docstrings --- client/client_central_mast/src/main.cpp | 10 ++-- client/client_satellite/platformio.ini | 2 + .../SentecSensors/SentecRainGaugeSensor.h | 21 ++++++++ .../SentecSolarRadiationSensor.h | 5 ++ client/libs/rs485/RS485HardwareSerial.h | 6 +++ client/libs/rs485/rs485.cpp | 21 ++++---- client/libs/rs485/rs485.hpp | 53 ++++++++++++------- 7 files changed, 83 insertions(+), 35 deletions(-) diff --git a/client/client_central_mast/src/main.cpp b/client/client_central_mast/src/main.cpp index 83c0d9a..310ce03 100644 --- a/client/client_central_mast/src/main.cpp +++ b/client/client_central_mast/src/main.cpp @@ -10,7 +10,7 @@ static const char *TAG = "MAIN"; #define uS_TO_S_FACTOR 1000000 /* Conversion factor for micro seconds to seconds */ #define TIME_TO_SLEEP 0 * 1 /* Time ESP32 will go to sleep (in seconds) */ -Forte_RS485 rs485; +ForteRS485 rs485; ForteINA219 ina219; SEM404 rainGaugeSensor = SEM404(2, 19); @@ -48,9 +48,9 @@ void setup() { ina219.setup(); try { - Forte_RS485::powerOnRS485Sensors(); + ForteRS485::powerOnRS485Sensors(); auto messages = rainGaugeSensor.buildMessages(); - Forte_RS485::powerOffRS485Sensors(); + ForteRS485::powerOffRS485Sensors(); // split into arrays of 6 messages TODO: Make this cleaner with default constructor std::array<Message, 6> messages_1 = {Message::nullMessage(), Message::nullMessage(), Message::nullMessage(), Message::nullMessage(), Message::nullMessage(), Message::nullMessage()}; @@ -78,7 +78,7 @@ void setup() { // demo of how to reset the rain gauge #ifdef RESET_RAIN_GAUGE if (messages_1.front().getClientDataPackage().getMeasurementData().getValue() > 20) { - Forte_RS485::powerOnRS485Sensors(); + ForteRS485::powerOnRS485Sensors(); if (rainGaugeSensor.resetPrecipitation() == ErrorType::SEM404_COULD_NOT_RESET_PRECIPITATION) { ESP_LOGE(TAG, "Could not reset precipitation"); auto precipitationErrorMessage = Message( @@ -90,7 +90,7 @@ void setup() { Message::nullMessage(), Message::nullMessage()}; Message::sendMessages(messages_error); } - Forte_RS485::powerOffRS485Sensors(); + ForteRS485::powerOffRS485Sensors(); } #endif diff --git a/client/client_satellite/platformio.ini b/client/client_satellite/platformio.ini index 1a9a189..09b747e 100644 --- a/client/client_satellite/platformio.ini +++ b/client/client_satellite/platformio.ini @@ -4,6 +4,8 @@ board = esp32dev framework = arduino monitor_speed = 115200 lib_ldf_mode = deep +upload_port = /dev/ttyUSB0 +monitor_port = /dev/ttyUSB0 lib_extra_dirs = ../libs ../../shared-libs diff --git a/client/libs/SentecSensors/SentecRainGaugeSensor.h b/client/libs/SentecSensors/SentecRainGaugeSensor.h index 0425285..669801a 100644 --- a/client/libs/SentecSensors/SentecRainGaugeSensor.h +++ b/client/libs/SentecSensors/SentecRainGaugeSensor.h @@ -14,6 +14,11 @@ struct out_data_rain_gauge { ErrorType precipitationError; }; +/** + * @brief Class to read data from the Sentec rain gauge sensor (SEM404) + * @implements ForteSensor<out_data_rain_gauge> + * @extends SentecSensorRS485 + */ class SEM404 : public SentecSensorRS485, ForteSensor<out_data_rain_gauge> { public: using SentecSensorRS485::SentecSensorRS485; @@ -21,10 +26,19 @@ class SEM404 : public SentecSensorRS485, ForteSensor<out_data_rain_gauge> { // precipitation values [mm] unsigned int precipitation = 0; // prcp since reset? (I THINK!!!) + /** + * @brief Reset the precipitation value + * @return ErrorType. If the reset was unsuccessful, this will return ErrorType::SEM404_RESET_PRECIPITATION_ERROR. + * Otherwise it returns the error given from the device + */ ErrorType resetPrecipitation(); void resetSensor(); + /** + * @brief Get the instantaneous precipitation value + * @return out_data_rain_gauge + */ out_data_rain_gauge getInstantaneousPrecipitation(); String getPrecipitationStr(); @@ -34,6 +48,13 @@ class SEM404 : public SentecSensorRS485, ForteSensor<out_data_rain_gauge> { std::list<Message> buildMessages() override; [[nodiscard]] SensorInformation getSensorInformation() const override; + private: + /** + * @brief Check if the answer frame is equal to the query frame + * @param answerFrame The frame returned from the device + * @param queryFrame The frame sent to the device + * @return true if the answer frame is equal to the query frame + */ bool isAnswerFrameEqualToQueryFrame(byte *answerFrame, byte *queryFrame); }; #endif // CLIENT_CENTRAL_MAST_SENTECRAINGAUGESENSOR_H diff --git a/client/libs/SentecSensors/SentecSolarRadiationSensor.h b/client/libs/SentecSensors/SentecSolarRadiationSensor.h index 3942914..3c1edc9 100644 --- a/client/libs/SentecSensors/SentecSolarRadiationSensor.h +++ b/client/libs/SentecSensors/SentecSolarRadiationSensor.h @@ -13,6 +13,11 @@ struct out_data_solar_radiation { ErrorType solarError; }; +/** + * @brief Class to read data from the Sentec solar radiation sensor (SEM228A) + * @implements ForteSensor<out_data_solar_radiation> + * @extends SentecSensorRS485 + */ class SEM228A : public SentecSensorRS485, public ForteSensor<out_data_solar_radiation> { public: using SentecSensorRS485::SentecSensorRS485; diff --git a/client/libs/rs485/RS485HardwareSerial.h b/client/libs/rs485/RS485HardwareSerial.h index 0b6fbd1..b5c688c 100644 --- a/client/libs/rs485/RS485HardwareSerial.h +++ b/client/libs/rs485/RS485HardwareSerial.h @@ -8,6 +8,12 @@ #include <HardwareSerial.h> #include <memory> + +/** + * @brief Singleton class to get the RS485 serial port + * @example std::shared_ptr<HardwareSerial> RS485 = RS485HardwareSerial::getInstance().getRS485Serial(); + + */ class RS485HardwareSerial { public: static RS485HardwareSerial &getInstance() diff --git a/client/libs/rs485/rs485.cpp b/client/libs/rs485/rs485.cpp index cb108ad..58c8c8b 100644 --- a/client/libs/rs485/rs485.cpp +++ b/client/libs/rs485/rs485.cpp @@ -3,8 +3,9 @@ #include "SentecSolarRadiationSensor.h" // RS485 control //#define RS485Serial Serial2 -#define RXPin 14 // Serial Receive pin -#define TXPin 15 // Serial Transmit pin + +#define RX_PIN 14 // Serial Receive pin +#define TX_PIN 15 // Serial Transmit pin #define RE_DE_PIN 19 // Line to pull high or low to receive or send data from RS485 @@ -17,20 +18,20 @@ static const char *TAG = "RS485"; SEM228A solarSensor(1, RE_DE_PIN); SEM404 rainGauge = SEM404(2, RE_DE_PIN); -void Forte_RS485::setup() { +void ForteRS485::setup() { RS485Serial = RS485HardwareSerial::getInstance().getRS485Serial(); // configure the pins to be output only pinMode(RE_DE_PIN, OUTPUT); pinMode(POWER_SWITCH_PIN_12V, OUTPUT); pinMode(POWER_SWITCH_PIN_5V, OUTPUT); - RS485Serial->begin(4800, SERIAL_8N1, TXPin, RXPin); + RS485Serial->begin(4800, SERIAL_8N1, TX_PIN, RX_PIN); } -void Forte_RS485::teardown() { +void ForteRS485::teardown() { RS485Serial->end(); } -out_data_rs485 Forte_RS485::readData() { +out_data_rs485 ForteRS485::readData() { powerOnRS485Sensors(); out_data_rs485 output{}; @@ -40,21 +41,21 @@ out_data_rs485 Forte_RS485::readData() { powerOffRS485Sensors(); return output; } -void Forte_RS485::powerOffRS485Sensors() { +void ForteRS485::powerOffRS485Sensors() { digitalWrite(POWER_SWITCH_PIN_12V, LOW); digitalWrite(POWER_SWITCH_PIN_5V, LOW); gpio_hold_en((gpio_num_t)POWER_SWITCH_PIN_12V); gpio_hold_en((gpio_num_t)POWER_SWITCH_PIN_5V); } -void Forte_RS485::powerOnRS485Sensors() { // Power on sensor +void ForteRS485::powerOnRS485Sensors() { // Power on sensor digitalWrite(POWER_SWITCH_PIN_12V, HIGH); digitalWrite(POWER_SWITCH_PIN_5V, HIGH); // Wait for sensors to power up // TODO minimize delay delay(1000); } -std::list<Message> Forte_RS485::buildMessages() { +std::list<Message> ForteRS485::buildMessages() { std::list<Message> messages; out_data_rs485 output = readData(); @@ -69,6 +70,6 @@ std::list<Message> Forte_RS485::buildMessages() { return messages; } -SensorInformation Forte_RS485::getSensorInformation() const { +SensorInformation ForteRS485::getSensorInformation() const { return sensorInformation; } \ No newline at end of file diff --git a/client/libs/rs485/rs485.hpp b/client/libs/rs485/rs485.hpp index 00d4802..823fecb 100644 --- a/client/libs/rs485/rs485.hpp +++ b/client/libs/rs485/rs485.hpp @@ -1,35 +1,48 @@ #ifndef _RS485 #define _RS485 +#include "ForteSensor.hpp" +#include "Message.hpp" +#include "SentecSensorsRS485.h" #include <MeasurementTypes.h> -#include <SentecSolarRadiationSensor.h> #include <RS485HardwareSerial.h> #include <SentecRainGaugeSensor.h> -#include "Message.hpp" -#include "ForteSensor.hpp" -#include "SentecSensorsRS485.h" +#include <SentecSolarRadiationSensor.h> + +/** + * @brief Struct to hold all the data from the RS485 sensors + */ struct out_data_rs485 { - out_data_solar_radiation solar; - out_data_rain_gauge precipitation; + out_data_solar_radiation solar; + out_data_rain_gauge precipitation; }; -class Forte_RS485: public ForteSensor<out_data_rs485> { - public: - void setup() override; - out_data_rs485 readData() override; - std::list<Message> buildMessages() override; - [[nodiscard]] SensorInformation getSensorInformation() const override; - void teardown(); - static void powerOnRS485Sensors(); - static void powerOffRS485Sensors(); +/** + * @brief Class to read data from both the Sentec solar radiation sensor and the Sentec rain gauge sensor at once + * This class is required to be instantiated and setup in order to use RS485 communication. + */ +class ForteRS485 : public ForteSensor<out_data_rs485> { + public: + void setup() override; + out_data_rs485 readData() override; + std::list<Message> buildMessages() override; + [[nodiscard]] SensorInformation getSensorInformation() const override; + void teardown(); - private: + /** + * @brief Power on the RS485 sensors + */ + static void powerOnRS485Sensors(); - std::shared_ptr<HardwareSerial> RS485Serial; + /** + * @brief Power off the RS485 sensors + */ + static void powerOffRS485Sensors(); - const SensorInformation - sensorInformation{HardwareName::RS485, SensorProtocol::RS485}; + private: + std::shared_ptr<HardwareSerial> RS485Serial; + const SensorInformation sensorInformation{HardwareName::RS485, SensorProtocol::RS485}; }; -#endif \ No newline at end of file +#endif \ No newline at end of file -- GitLab