Skip to content
Snippets Groups Projects
Verified Commit d1da0558 authored by Zoe Michaela Dietmar Pfister's avatar Zoe Michaela Dietmar Pfister :gay_pride_flag:
Browse files

Move to another ADS Library

parent 06107be2
No related branches found
No related tags found
2 merge requests!39Merge Develop into Main,!35Refactoring changes, changes in ADC selection, Energy consumption sensor, added compilation tests
Pipeline #109568 passed
#include <Arduino.h>
#include "dr26.hpp"
#include <Arduino.h>
#include <f_deep_sleep.hpp>
static const char *TAG = "MAIN";
DR26 dr26_0_1;
DR26 dr26_2_3;
DR26 dr26_0_1{ADS1115_MUX::ADS1115_COMP_0_1, ADS1115_RANGE::ADS1115_RANGE_4096};
DR26 dr26_2_3{ADS1115_MUX::ADS1115_COMP_2_3, ADS1115_RANGE::ADS1115_RANGE_4096};
// one loop takes ~2200 ms
void setup() {
......@@ -15,19 +16,18 @@ void setup() {
Serial.begin(115200);
dr26_0_1.setup();
dr26_2_3.setup();
dr26_0_1.setChannel(ADSChannel::A0_1);
dr26_2_3.setChannel(ADSChannel::A2_3);
// disable led
gpio_set_direction(GPIO_NUM_32, GPIO_MODE_OUTPUT);
ESP_LOGD(TAG, "Setup took %ld ms", millis() - ts);
Serial.println("Uptime(ms),out0_1,out2_3");
}
void loop() {
unsigned long ts = millis();
auto dr26_0_1_data = dr26_0_1.readData();
auto dr26_2_3_data = dr26_2_3.readData();
Serial.printf("%ld,%f,%f\n", millis(), dr26_0_1_data.getValue(), dr26_2_3_data.getValue());
}
\ No newline at end of file
DeepSleep::deep_sleep(2);
}
void loop() {}
\ No newline at end of file
......@@ -8,16 +8,27 @@ void DR26::setup() {
return;
}
Wire.begin();
ads.setGain(gain);
ads.setDataRate(RATE_ADS1115_8SPS);
if (ads.begin()) {
if (ads.init()) {
ads.setVoltageRange_mV(voltageRange);
ads.setMeasureMode(ADS1115_SINGLE);
ESP_LOGD(TAG, "ADS initialized");
sensorConnected = true;
} else {
ESP_LOGE(TAG, "failed to initialize ADS");
ESP_LOGE(TAG, "failed to initialize ADS, defaulting to 0 to Ground compare");
sensorConnected = false;
channel = ADS1115_MUX::ADS1115_COMP_0_GND;
}
}
float DR26::readChannel(ADS1115_MUX compareChannel) {
float voltage = 0.0;
ads.setCompareChannels(compareChannel);
ads.startSingleMeasurement();
while (ads.isBusy()) {
}
channel = ADSChannel::A0;
voltage = ads.getResult_V(); // alternative: getResult_mV for Millivolt
return voltage;
}
Measurement DR26::readData() {
......@@ -29,24 +40,10 @@ Measurement DR26::readData() {
ErrorType::SENSOR_INIT_FAILED};
}
switch (channel) {
case ADSChannel::A0:
case ADSChannel::A1:
case ADSChannel::A2:
case ADSChannel::A3:
adc = ads.readADC_SingleEnded(static_cast<uint8_t>(channel));
break;
case ADSChannel::A0_1:
adc = ads.readADC_Differential_0_1();
break;
case ADSChannel::A2_3:
adc = ads.readADC_Differential_2_3();
break;
}
volts = ads.computeVolts(adc);
volts = readChannel(channel);
return {volts, static_cast<uint8_t>(channel), NO_I2C_ADDRESS, MeasurementType::CIRCUMFERENCE_INCREMENT, ErrorType::DATA_OK};
return {volts, static_cast<uint8_t>(channel), NO_I2C_ADDRESS, MeasurementType::CIRCUMFERENCE_INCREMENT,
ErrorType::DATA_OK};
}
// The following functions change the ADC input range: be careful
......@@ -64,11 +61,11 @@ Measurement DR26::readData() {
// 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 DR26::setGain(adsGain_t newGain) {
ads.setGain(newGain);
void DR26::setVoltageRange(ADS1115_RANGE newVoltageRange) {
ads.setVoltageRange_mV(newVoltageRange);
}
void DR26::setChannel(ADSChannel newChannel) {
void DR26::setChannel(ADS1115_MUX newChannel) {
channel = newChannel;
}
......@@ -82,4 +79,4 @@ std::list<Message> DR26::buildMessages() {
SensorInformation DR26::getSensorInformation() const {
return sensorInformation;
}
DR26::DR26(ADSChannel channel, adsGain_t gain) : channel(channel), gain(gain) {}
DR26::DR26(ADS1115_MUX channel, ADS1115_RANGE voltageRange) : channel(channel), voltageRange(voltageRange) {}
#ifndef _DR26
#define _DR26
#include "Adafruit_ADS1X15.h"
#include "ForteSensor.hpp"
#include "Message.hpp"
#include "NoDataAvailableException.hpp"
#include "Pinout.hpp"
#include "esp_log.h"
#include <ADS1115_WE.h>
#include <Wire.h>
enum class ADSChannel : uint8_t {
......@@ -22,17 +22,18 @@ class DR26 : public ForteSensor<Measurement> {
public:
void setup() override;
Measurement readData() override;
void setGain(adsGain_t newGain);
void setChannel(ADSChannel newChannel);
void setVoltageRange(ADS1115_RANGE newVoltageRange);
void setChannel(ADS1115_MUX newChannel);
std::list<Message> buildMessages() override;
[[nodiscard]] SensorInformation getSensorInformation() const override;
explicit DR26(ADSChannel channel, adsGain_t gain);
explicit DR26(ADS1115_MUX channel, ADS1115_RANGE voltageRange);
private:
Adafruit_ADS1115 ads;
float readChannel(ADS1115_MUX compareChannel);
ADS1115_WE ads;
const SensorInformation sensorInformation{HardwareName::DRS26, SensorProtocol::Analog};
ADSChannel channel;
adsGain_t gain = GAIN_ONE;
ADS1115_MUX channel;
ADS1115_RANGE voltageRange = ADS1115_RANGE::ADS1115_RANGE_4096;
bool sensorConnected = false;
};
......
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