diff --git a/client/libs/dr26_analogue/dr26.cpp b/client/libs/dr26_analogue/dr26.cpp index ad740ba0ead26bf77ca4621fde3af17024470de0..1f01f0820e05d25db819606361626869352e3851 100644 --- a/client/libs/dr26_analogue/dr26.cpp +++ b/client/libs/dr26_analogue/dr26.cpp @@ -2,15 +2,10 @@ static const char *TAG = "DR26"; void ForteDR26::setup() { - - // This was changed by Bilal on november 25th from - // Wire.begin(6,7); - pinMode(2, OUTPUT); Wire.begin(); - // ads.setGain(GAIN_ONE); - // ads.begin() ? Serial.println("ADS initialized") : Serial.println("failed to initialize ADS"); ads1.setGain(GAIN_ONE); + ads1.setDataRate(RATE_ADS1115_8SPS); ads1.begin() ? ESP_LOGD(TAG, "ADS initialized") : ESP_LOGE(TAG, "failed to initialize ADS"); delay(100); channel = 0; @@ -18,20 +13,33 @@ void ForteDR26::setup() { float ForteDR26::readData() { float volts = 0; - for (int i = 0; i < 10; i++) { - int16_t adc = 0; - float volt = 0; - try { - adc = ads1.readADC_SingleEnded(channel); - volt = ads1.computeVolts(adc); - } - catch (NoDataAvailableException &e) { - throw NoDataAvailableException(); //propagate exception - } - volts += volt; - } - volts /= 10; + // adjust data rate instead of yourself. ADC averages on its on with low data rate + // FIXME: Test which datarate gets most stable result. If it still is not good enough consider adding a small capacitor or averaging again + // for (int i = 0; i < 10; i++) { + // int16_t adc = 0; + // float volt = 0; + // try { + // adc = ads1.readADC_SingleEnded(channel); + // volt = ads1.computeVolts(adc); + // } + // catch (NoDataAvailableException &e) { + // throw NoDataAvailableException(); //propagate exception + // } + // volts += volt; + // } + + // volts /= 10; + + + int16_t adc = 0; + try { + adc = ads1.readADC_SingleEnded(channel); + volts = ads1.computeVolts(adc); + } + catch (NoDataAvailableException &e) { + throw NoDataAvailableException(); //propagate exception + } return volts; } @@ -61,14 +69,13 @@ void ForteDR26::setChannel(int c) { std::list<Message> ForteDR26::buildMessages() { std::list<Message> messages; float data = readData(); - MeasurementData IncrementData{data, 0, {}, "CIRCUMFERENCE_INCREMENT"}; + MeasurementData IncrementData{data, channel, {}, "CIRCUMFERENCE_INCREMENT"}; messages.emplace_back(IncrementData, sensorInformation, Time::getInstance().getEpochSeconds()); return messages; } -// FIXME: Channel shadows a member variable, we don't need it, just take the member variable instead // FIXME: passing the measurementType as a string is not a good idea, we should use an enum like we do for other sensors -std::list<Message> ForteDR26::buildMessages(int channel, std::string measurementType) { +std::list<Message> ForteDR26::buildMessages(std::string measurementType) { std::list<Message> messages; float data = readData(); MeasurementData IncrementData{data, channel, {}, measurementType}; diff --git a/client/libs/dr26_analogue/dr26.hpp b/client/libs/dr26_analogue/dr26.hpp index b60c0997ec0180bc9e145d2588387022f9999f2a..e1bc6b3e6c1857716a4e0fc6a0870ffd48774fbb 100644 --- a/client/libs/dr26_analogue/dr26.hpp +++ b/client/libs/dr26_analogue/dr26.hpp @@ -16,7 +16,7 @@ class ForteDR26 : public ForteSensor<float> { void changeGain(adsGain_t gain); void setChannel(int channel); std::list<Message> buildMessages() override; - std::list<Message> buildMessages(int channel,std::string mesurment_type); + std::list<Message> buildMessages(std::string mesurment_type); [[nodiscard]] SensorInformation getSensorInformation() const override; static Adafruit_ADS1115 ads1;