Skip to content
Snippets Groups Projects
Commit a3dc59a7 authored by User expired's avatar User expired
Browse files

use internal averaging of ads1115

parent 4c507b15
No related branches found
No related tags found
3 merge requests!39Merge Develop into Main,!19development into master,!18Power management satellite
...@@ -2,15 +2,10 @@ ...@@ -2,15 +2,10 @@
static const char *TAG = "DR26"; static const char *TAG = "DR26";
void ForteDR26::setup() { void ForteDR26::setup() {
// This was changed by Bilal on november 25th from
// Wire.begin(6,7);
pinMode(2, OUTPUT);
Wire.begin(); Wire.begin();
// ads.setGain(GAIN_ONE);
// ads.begin() ? Serial.println("ADS initialized") : Serial.println("failed to initialize ADS");
ads1.setGain(GAIN_ONE); ads1.setGain(GAIN_ONE);
ads1.setDataRate(RATE_ADS1115_8SPS);
ads1.begin() ? ESP_LOGD(TAG, "ADS initialized") : ESP_LOGE(TAG, "failed to initialize ADS"); ads1.begin() ? ESP_LOGD(TAG, "ADS initialized") : ESP_LOGE(TAG, "failed to initialize ADS");
delay(100); delay(100);
channel = 0; channel = 0;
...@@ -18,20 +13,33 @@ void ForteDR26::setup() { ...@@ -18,20 +13,33 @@ void ForteDR26::setup() {
float ForteDR26::readData() { float ForteDR26::readData() {
float volts = 0; 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; return volts;
} }
...@@ -61,14 +69,13 @@ void ForteDR26::setChannel(int c) { ...@@ -61,14 +69,13 @@ void ForteDR26::setChannel(int c) {
std::list<Message> ForteDR26::buildMessages() { std::list<Message> ForteDR26::buildMessages() {
std::list<Message> messages; std::list<Message> messages;
float data = readData(); float data = readData();
MeasurementData IncrementData{data, 0, {}, "CIRCUMFERENCE_INCREMENT"}; MeasurementData IncrementData{data, channel, {}, "CIRCUMFERENCE_INCREMENT"};
messages.emplace_back(IncrementData, sensorInformation, Time::getInstance().getEpochSeconds()); messages.emplace_back(IncrementData, sensorInformation, Time::getInstance().getEpochSeconds());
return messages; 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 // 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; std::list<Message> messages;
float data = readData(); float data = readData();
MeasurementData IncrementData{data, channel, {}, measurementType}; MeasurementData IncrementData{data, channel, {}, measurementType};
......
...@@ -16,7 +16,7 @@ class ForteDR26 : public ForteSensor<float> { ...@@ -16,7 +16,7 @@ class ForteDR26 : public ForteSensor<float> {
void changeGain(adsGain_t gain); void changeGain(adsGain_t gain);
void setChannel(int channel); void setChannel(int channel);
std::list<Message> buildMessages() override; 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; [[nodiscard]] SensorInformation getSensorInformation() const override;
static Adafruit_ADS1115 ads1; static Adafruit_ADS1115 ads1;
......
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