Skip to content
Snippets Groups Projects
Commit f91377ad authored by Zoe Pfister's avatar Zoe Pfister :speech_balloon:
Browse files

move from void* to generic type in forte_sensor.hpp

parent 7b8e9721
No related branches found
No related tags found
5 merge requests!39Merge Develop into Main,!19development into master,!17Inital Host, initial Client,!10merge serial comm and sd write into espnow,!8merge sensor_readout into develop
#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
#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
#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
#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
......@@ -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
#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
#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
#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
#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
#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
#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);
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment