From f91377ad59045c81d60bca8ce95d8b1fc1db0a65 Mon Sep 17 00:00:00 2001 From: Zoe Pfister <zoe.pfister@student.uibk.ac.at> Date: Fri, 2 Sep 2022 16:15:49 +0200 Subject: [PATCH] move from void* to generic type in forte_sensor.hpp --- .../include/NoDataAvailableException.hpp | 8 ++ client/client/include/forte_sensor.hpp | 12 +-- client/client/lib/dr26_analogue/src/dr26.cpp | 73 ++++++++++--------- client/client/lib/dr26_analogue/src/dr26.hpp | 18 ++--- client/client/lib/drs26_digital/drs26.cpp | 58 ++++++++------- client/client/lib/drs26_digital/drs26.hpp | 28 +++---- client/client/lib/ina219/ina219.cpp | 44 ++++++----- client/client/lib/ina219/ina219.hpp | 35 +++++---- client/client/lib/scd30/scd30.cpp | 39 +++++----- client/client/lib/scd30/scd30.hpp | 29 ++++---- client/client/src/main.cpp | 48 ++++++------ 11 files changed, 214 insertions(+), 178 deletions(-) create mode 100644 client/client/include/NoDataAvailableException.hpp diff --git a/client/client/include/NoDataAvailableException.hpp b/client/client/include/NoDataAvailableException.hpp new file mode 100644 index 0000000..a5de09a --- /dev/null +++ b/client/client/include/NoDataAvailableException.hpp @@ -0,0 +1,8 @@ +#pragma once + +#include <exception> +#include <iostream> + +struct NoDataAvailableException : public std::exception { + const char *what() const throw() { return "Sensor could not read data"; } +}; \ No newline at end of file diff --git a/client/client/include/forte_sensor.hpp b/client/client/include/forte_sensor.hpp index 8e030b9..36c4e75 100644 --- a/client/client/include/forte_sensor.hpp +++ b/client/client/include/forte_sensor.hpp @@ -1,13 +1,15 @@ #ifndef _FORTE_SENSOR #define _FORTE_SENSOR +// #include "Message.hpp" +template <class T> class Forte_Sensor { - public: - virtual void* read_data() = 0; - virtual void setup() = 0; - - private: + public: + virtual T read_data() = 0; + virtual void setup() = 0; + // virtual Message build_message() = 0; + private: }; #endif \ No newline at end of file diff --git a/client/client/lib/dr26_analogue/src/dr26.cpp b/client/client/lib/dr26_analogue/src/dr26.cpp index e08a572..1ed6d19 100644 --- a/client/client/lib/dr26_analogue/src/dr26.cpp +++ b/client/client/lib/dr26_analogue/src/dr26.cpp @@ -1,43 +1,48 @@ #include "dr26.hpp" - Adafruit_ADS1115 ads; -void Forte_DR26 :: setup(adsGain_t gain){ - Wire.begin(I2C_SDA, I2C_SCL); - ads.setGain(gain); - ads.begin() ? - Serial.println("ADS initialized") : - Serial.println("failed to initialize ADS"); - delay(100); +void Forte_DR26 ::setup() +{ + Wire.begin(I2C_SDA, I2C_SCL); + // ads.setGain(0); + ads.begin() ? Serial.println("ADS initialized") : Serial.println("failed to initialize ADS"); + delay(100); +} + +float Forte_DR26 ::read_data() +{ + float volts = 0; + for (int i = 0; i < 10; i++) { + int16_t adc = ads.readADC_SingleEnded(0); + float volt = ads.computeVolts(adc); + volts += volt; + } + volts /= 10; + return volts; } -float* Forte_DR26 :: read_data(){ - float volts = 0; - for(int i=0; i<10; i++){ - int16_t adc = ads.readADC_SingleEnded(0); - float volt = ads.computeVolts(adc); - volts += volt; - } - volts /= 10; - return &volts; +// The following functions change the ADC input range: be careful +// to never to exceed VDD +0.3V max, or to exceed the upper and +// lower limits if you adjust the input range. +// SETTING THE GAIN VALUE INCORRECTLY MAY DESTROY THE ADC! +// The maximum output of the dendrometer is 2.5V, and so a gain of +// one (max 4.096V) or two (max 2.048V) is optimal. Changing the gain +// changes the accuracy of the sensor: higher gain gives a higher +// precision but a smaller range. +// +// GAIN_TWOTHIRDS // 2/3x gain +/- 6.144V 1 bit = 0.1875mV (default) +// GAIN_ONE // 1x gain +/- 4.096V 1 bit = 0.125mV +// GAIN_TWO // 2x gain +/- 2.048V 1 bit = 0.0625mV +// GAIN_FOUR // 4x gain +/- 1.024V 1 bit = 0.03125mV +// GAIN_EIGHT // 8x gain +/- 0.512V 1 bit = 0.015625mV +// GAIN_SIXTEEN // 16x gain +/- 0.256V 1 bit = 0.0078125mV +void Forte_DR26 ::change_Gain(adsGain_t gain) +{ + ads.setGain(gain); } - // The following functions change the ADC input range: be careful - // to never to exceed VDD +0.3V max, or to exceed the upper and - // lower limits if you adjust the input range. - // SETTING THE GAIN VALUE INCORRECTLY MAY DESTROY THE ADC! - // The maximum output of the dendrometer is 2.5V, and so a gain of - // one (max 4.096V) or two (max 2.048V) is optimal. Changing the gain - // changes the accuracy of the sensor: higher gain gives a higher - // precision but a smaller range. - // - // GAIN_TWOTHIRDS // 2/3x gain +/- 6.144V 1 bit = 0.1875mV (default) - // GAIN_ONE // 1x gain +/- 4.096V 1 bit = 0.125mV - // GAIN_TWO // 2x gain +/- 2.048V 1 bit = 0.0625mV - // GAIN_FOUR // 4x gain +/- 1.024V 1 bit = 0.03125mV - // GAIN_EIGHT // 8x gain +/- 0.512V 1 bit = 0.015625mV - // GAIN_SIXTEEN // 16x gain +/- 0.256V 1 bit = 0.0078125mV -void Forte_DR26 :: change_Gain(adsGain_t gain){ - ads.setGain(gain); +Message Forte_DR26::build_message() +{ + throw "Not implemented"; } \ No newline at end of file diff --git a/client/client/lib/dr26_analogue/src/dr26.hpp b/client/client/lib/dr26_analogue/src/dr26.hpp index 0a7c53c..6105c29 100644 --- a/client/client/lib/dr26_analogue/src/dr26.hpp +++ b/client/client/lib/dr26_analogue/src/dr26.hpp @@ -1,20 +1,20 @@ #ifndef _DR26 #define _DR26 +#include "Message.hpp" #include "forte_sensor.hpp" #include "pinout.hpp" -#include <Wire.h> #include <Adafruit_ADS1X15.h> +#include <Wire.h> -class Forte_DR26 : public Forte_Sensor{ - public: - void setup(adsGain_t gain); - float* read_data(); - void change_Gain(adsGain_t gain); - - private: - +class Forte_DR26 : public Forte_Sensor<float> { + public: + void setup(); + float read_data(); + void change_Gain(adsGain_t gain); + Message build_message(); + private: }; #endif \ No newline at end of file diff --git a/client/client/lib/drs26_digital/drs26.cpp b/client/client/lib/drs26_digital/drs26.cpp index 4c3378a..45f038d 100644 --- a/client/client/lib/drs26_digital/drs26.cpp +++ b/client/client/lib/drs26_digital/drs26.cpp @@ -2,41 +2,47 @@ /* It happens for some reason that the sensor cant get reached every 2 time Because the sensor use sdi12 protocoll we have to wait aproxemettly 1 secound between the commands -It is not known how lond the response takes so we use a while loop which can be a risk wehre the programm can get stuck +It is not known how lond the response takes so we use a while loop which can be a risk wehre the programm can get stuck */ void Forte_DRS26 ::setup() { - drs26.begin(SDI_DATA); + drs26.begin(4); } -out_data_drs26 *Forte_DRS26 ::read_data() +out_data_drs26 Forte_DRS26 ::read_data() { - String sdiResponse = ""; - String measurement_command="1M!"; //The drs26 sensor uses the sdi12 protocoll , in the sdi12 protocoll is the measurement command is specified as 1M!=Sebsir measurement request at adress 1 - String data_command="1D0!"; //and the followed data command 1D0! = Sensor data request at adress 1 + String sdiResponse = ""; + String measurement_command = + "1M!"; // The drs26 sensor uses the sdi12 protocoll , in the sdi12 protocoll is the measurement command is + // specified as 1M!=Sebsir measurement request at adress 1 + String data_command = "1D0!"; // and the followed data command 1D0! = Sensor data request at adress 1 - drs26.sendCommand(measurement_command); - delay(1000); - drs26.sendCommand(data_command); + drs26.sendCommand(measurement_command); + delay(1000); + drs26.sendCommand(data_command); - while (drs26.available()) - { - char next_character = drs26.read(); - if ((next_character != '\n') && (next_character != '\r')) - { - sdiResponse += next_character; - delay(10); // 1 character ~ 7.5ms - } - } + data = {-1, -1, -1}; - if (sdiResponse.length() > 1) - { - data.id = sdiResponse.substring(0, 8).toInt(); - data.circumference = sdiResponse.substring(9, 15).toFloat(); - data.temperatur = sdiResponse.substring(16, 22).toFloat(); - return &data; - } + while (drs26.available()) { + char next_character = drs26.read(); + if ((next_character != '\n') && (next_character != '\r')) { + sdiResponse += next_character; + delay(10); // 1 character ~ 7.5ms + } + } - return 0; + if (sdiResponse.length() > 1) { + data.id = sdiResponse.substring(0, 8).toInt(); + data.circumference = sdiResponse.substring(9, 15).toFloat(); + data.temperatur = sdiResponse.substring(16, 22).toFloat(); + } + return data; +} + +Message Forte_DRS26 ::build_message() +{ + // auto message = Message(); + // message.add_data(data.circumference, data.id); + throw "Not yet implemented"; } \ No newline at end of file diff --git a/client/client/lib/drs26_digital/drs26.hpp b/client/client/lib/drs26_digital/drs26.hpp index 4438387..1c5116d 100644 --- a/client/client/lib/drs26_digital/drs26.hpp +++ b/client/client/lib/drs26_digital/drs26.hpp @@ -1,27 +1,27 @@ #ifndef _DRS26 #define _DRS26 +#include "Message.hpp" +#include "Wire.h" #include "forte_sensor.hpp" #include "pinout.hpp" #include <SDI12.h> -#include "Wire.h" - struct out_data_drs26 { - int id; - float circumference; - float temperatur; - }; + int id; + float circumference; + float temperatur; +}; +class Forte_DRS26 : public Forte_Sensor<out_data_drs26> { + public: + void setup(); + out_data_drs26 read_data(); + Message build_message(); -class Forte_DRS26 : public Forte_Sensor{ - public: - void setup(); - out_data_drs26* read_data(); - - private: - SDI12 drs26; - out_data_drs26 data; + private: + SDI12 drs26; + out_data_drs26 data; }; #endif \ No newline at end of file diff --git a/client/client/lib/ina219/ina219.cpp b/client/client/lib/ina219/ina219.cpp index 7f4beb3..0f710d2 100644 --- a/client/client/lib/ina219/ina219.cpp +++ b/client/client/lib/ina219/ina219.cpp @@ -1,26 +1,30 @@ #include "ina219.hpp" +void Forte_INA219 ::setup() +{ + Wire.begin(I2C_SDA, I2C_SCL); + if (!ina219.init()) { + // Sensor init went wrong + return; + } +} + +out_data_ina219 Forte_INA219 ::read_data() +{ + if (!ina219.getOverflow()) { + data.shuntVoltage_mV = ina219.getShuntVoltage_mV(); + data.busVoltage_V = ina219.getBusVoltage_V(); + data.current_mA = ina219.getCurrent_mA(); + data.power_mW = ina219.getBusPower(); + data.loadVoltage_V = data.busVoltage_V + (data.shuntVoltage_mV / 1000); + data.ina219_overflow = ina219.getOverflow(); -void Forte_INA219 :: setup(){ - Wire.begin(I2C_SDA, I2C_SCL); - if(!ina219.init()){ - // Sensor init went wrong - return; - } + return data; + } else + return data; } -out_data_ina219* Forte_INA219 :: read_data(){ - if(!ina219.getOverflow()) - { - data.shuntVoltage_mV = ina219.getShuntVoltage_mV(); - data.busVoltage_V= ina219.getBusVoltage_V(); - data.current_mA= ina219.getCurrent_mA(); - data.power_mW= ina219.getBusPower(); - data.loadVoltage_V = data.busVoltage_V + (data.shuntVoltage_mV/1000); - data.ina219_overflow=ina219.getOverflow(); - - return &data; - } - else - return 0; +Message Forte_INA219::build_message() +{ + throw "Not yet implemented"; } \ No newline at end of file diff --git a/client/client/lib/ina219/ina219.hpp b/client/client/lib/ina219/ina219.hpp index 67217e1..cd964cc 100644 --- a/client/client/lib/ina219/ina219.hpp +++ b/client/client/lib/ina219/ina219.hpp @@ -1,31 +1,30 @@ #ifndef _INA219 #define _INA219 +#include "Message.hpp" +#include "Wire.h" #include "forte_sensor.hpp" #include "pinout.hpp" #include <INA219_WE.h> -#include "Wire.h" - - struct out_data_ina219 { - float shuntVoltage_mV = 0.0; - float loadVoltage_V = 0.0; - float busVoltage_V = 0.0; - float current_mA = 0.0; - float power_mW = 0.0; - bool ina219_overflow = false; - }; + float shuntVoltage_mV = 0.0; + float loadVoltage_V = 0.0; + float busVoltage_V = 0.0; + float current_mA = 0.0; + float power_mW = 0.0; + bool ina219_overflow = false; +}; +class Forte_INA219 : public Forte_Sensor<out_data_ina219> { + public: + void setup(); + out_data_ina219 read_data(); + Message build_message(); -class Forte_INA219 : public Forte_Sensor{ - public: - void setup(); - out_data_ina219* read_data(); - - private: - INA219_WE ina219; - out_data_ina219 data; + private: + INA219_WE ina219; + out_data_ina219 data; }; #endif \ No newline at end of file diff --git a/client/client/lib/scd30/scd30.cpp b/client/client/lib/scd30/scd30.cpp index c7c2084..222089c 100644 --- a/client/client/lib/scd30/scd30.cpp +++ b/client/client/lib/scd30/scd30.cpp @@ -1,23 +1,28 @@ #include "scd30.hpp" - -void Forte_SCD30 :: setup(){ - Wire.begin(I2C_SDA, I2C_SCL); - if(!airSensor.begin()){ - // Sensor init went wrong - return; - } +void Forte_SCD30 ::setup() +{ + Wire.begin(I2C_SDA, I2C_SCL); + if (!airSensor.begin()) { + // Sensor init went wrong + return; + } } -out_data_scd30* Forte_SCD30 :: read_data(){ - if(airSensor.dataAvailable()) - { - data.C02= airSensor.getCO2(); - data.Temperature = airSensor.getTemperature(); - data.Humidity = airSensor.getHumidity(); +out_data_scd30 Forte_SCD30 ::read_data() +{ + if (airSensor.dataAvailable()) { + data.C02 = airSensor.getCO2(); + data.Temperature = airSensor.getTemperature(); + data.Humidity = airSensor.getHumidity(); + + return data; + } + throw NoDataAvailableException(); + // return out_data_scd30{-1, -1, -1}; +} - return &data; - } - else - return 0; +Message Forte_SCD30::build_message() +{ + throw "Not yet implemented"; } \ No newline at end of file diff --git a/client/client/lib/scd30/scd30.hpp b/client/client/lib/scd30/scd30.hpp index 82a10cd..c3bf697 100644 --- a/client/client/lib/scd30/scd30.hpp +++ b/client/client/lib/scd30/scd30.hpp @@ -1,27 +1,28 @@ #ifndef _SCD30 #define _SCD30 +#include "Message.hpp" +#include "NoDataAvailableException.hpp" #include "forte_sensor.hpp" #include "pinout.hpp" -#include <Wire.h> #include <SparkFun_SCD30_Arduino_Library.h> +#include <Wire.h> -struct out_data_scd30 -{ - float C02; - float Temperature; - float Humidity; +struct out_data_scd30 { + float C02; + float Temperature; + float Humidity; }; +class Forte_SCD30 : public Forte_Sensor<out_data_scd30> { + public: + void setup(); + out_data_scd30 read_data(); + Message build_message(); -class Forte_SCD30 : public Forte_Sensor{ - public: - void setup(); - out_data_scd30* read_data(); - - private: - SCD30 airSensor; - out_data_scd30 data; + private: + SCD30 airSensor; + out_data_scd30 data; }; #endif \ No newline at end of file diff --git a/client/client/src/main.cpp b/client/client/src/main.cpp index 54220ba..a9cdb27 100644 --- a/client/client/src/main.cpp +++ b/client/client/src/main.cpp @@ -1,31 +1,37 @@ #include <Arduino.h> -#include <scd30.hpp> #include <dr26.hpp> #include <drs26.hpp> #include <ina219.hpp> +#include <scd30.hpp> -// Forte_SCD30 scd30; +Forte_DRS26 drs26; -void setup() { - Serial.begin(9600); - // scd30.setup(); +void setup() +{ + Serial.begin(9600); + drs26.setup(); } -void loop() { - - void* data; - // data = scd30.read_data(); +void loop() +{ + + out_data_drs26 data; + + try { + data = drs26.read_data(); + // auto message = scd30.build_message(); + // message.send(); + } catch (const NoDataAvailableException &e) { + std::cerr << e.what() << '\n'; + } + + Serial.print("Sensor Circumference: "); + Serial.println(data.circumference); + Serial.print("Temperature "); + Serial.println(data.temperatur); + Serial.print("Id "); + Serial.println(data.id); + Serial.println(); - if(data==0) - { - Serial.println("Waiting for data !"); - Serial.println(); - } - // else{ - // Serial.print("Sensor CO2 "); Serial.println(data->C02); - // Serial.print("Humidity "); Serial.println(data->Humidity); - // Serial.print("Temperature "); Serial.println(data->Temperature); - // Serial.println(); - // } - delay(5000); + delay(5000); } -- GitLab