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

Rename ForteDR26 to DR26. Allow more fine grained adc channel selection, add...

Rename ForteDR26 to DR26. Allow more fine grained adc channel selection, add constructor for easier use
parent 27d2f804
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
#include <Arduino.h>
#include "dr26.hpp"
static const char *TAG = "MAIN";
DR26 dr26_0_1;
DR26 dr26_2_3;
// one loop takes ~2200 ms
void setup() {
// disable brownout
// WRITE_PERI_REG(RTC_CNTL_BROWN_OUT_REG, 0);
unsigned long ts = millis();
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
......@@ -2,13 +2,13 @@
static const char *TAG = "DR26";
void ForteDR26::setup() {
void DR26::setup() {
if (sensorConnected) {
ESP_LOGD(TAG, "ADS already initialized");
return;
}
Wire.begin();
ads.setGain(GAIN_ONE);
ads.setGain(gain);
ads.setDataRate(RATE_ADS1115_8SPS);
if (ads.begin()) {
ESP_LOGD(TAG, "ADS initialized");
......@@ -17,29 +17,36 @@ void ForteDR26::setup() {
ESP_LOGE(TAG, "failed to initialize ADS");
sensorConnected = false;
}
channel = 0;
channel = ADSChannel::A0;
}
Measurement ForteDR26::readData() {
Measurement DR26::readData() {
float volts = 0;
int16_t adc = 0;
if (!sensorConnected) {
return {ERROR_VALUE, channel, NO_I2C_ADDRESS, MeasurementType::CIRCUMFERENCE_INCREMENT,
return {ERROR_VALUE, static_cast<uint8_t>(channel), NO_I2C_ADDRESS, MeasurementType::CIRCUMFERENCE_INCREMENT,
ErrorType::SENSOR_INIT_FAILED};
}
try {
// TODO: This function might never return if conversionComplete is never true. Check if this is the case
// FIXME: How to notice from the dr if a value is incorrect?
adc = ads.readADC_SingleEnded(channel);
volts = ads.computeVolts(adc);
} catch (NoDataAvailableException &e) {
// FIXME: This catch block will never be called since we aint throwing any exceptions
return {ERROR_VALUE, channel, NO_I2C_ADDRESS, MeasurementType::CIRCUMFERENCE_INCREMENT, ErrorType::NO_DATA};
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;
}
return {volts, channel, NO_I2C_ADDRESS, MeasurementType::CIRCUMFERENCE_INCREMENT, ErrorType::DATA_OK};
volts = ads.computeVolts(adc);
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
......@@ -57,24 +64,22 @@ Measurement ForteDR26::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 ForteDR26::changeGain(adsGain_t gain) {
ads.setGain(gain);
void DR26::setGain(adsGain_t newGain) {
ads.setGain(newGain);
}
void ForteDR26::setChannel(int c) {
channel = c;
void DR26::setChannel(ADSChannel newChannel) {
channel = newChannel;
}
std::list<Message> ForteDR26::buildMessages() {
std::list<Message> DR26::buildMessages() {
std::list<Message> messages;
auto data = readData();
messages.emplace_back(data, sensorInformation, Time::getInstance().getEpochSeconds());
return messages;
}
SensorInformation ForteDR26::getSensorInformation() const {
SensorInformation DR26::getSensorInformation() const {
return sensorInformation;
}
// TODO: What is this?
Adafruit_ADS1115 ForteDR26::ads = ads;
DR26::DR26(ADSChannel channel, adsGain_t gain) : channel(channel), gain(gain) {}
......@@ -4,25 +4,36 @@
#include "Adafruit_ADS1X15.h"
#include "ForteSensor.hpp"
#include "Message.hpp"
#include "NoDataAvailableException.hpp"
#include "Pinout.hpp"
#include "esp_log.h"
#include <Wire.h>
#include "NoDataAvailableException.hpp"
class ForteDR26: public ForteSensor<Measurement> {
public:
void setup() override;
Measurement readData() override;
void changeGain(adsGain_t gain);
void setChannel(int channel);
std::list<Message> buildMessages() override;
[[nodiscard]] SensorInformation getSensorInformation() const override;
Adafruit_ADS1115 ads;
enum class ADSChannel : uint8_t {
A0 = 0,
A1 = 1,
A2 = 2,
A3 = 3,
A0_1 = 4,
A2_3 = 5,
};
class DR26 : public ForteSensor<Measurement> {
public:
void setup() override;
Measurement readData() override;
void setGain(adsGain_t newGain);
void setChannel(ADSChannel newChannel);
std::list<Message> buildMessages() override;
[[nodiscard]] SensorInformation getSensorInformation() const override;
explicit DR26(ADSChannel channel, adsGain_t gain);
private:
const SensorInformation sensorInformation{HardwareName::DRS26, SensorProtocol::Analog};
uint8_t channel;
bool sensorConnected = false;
private:
Adafruit_ADS1115 ads;
const SensorInformation sensorInformation{HardwareName::DRS26, SensorProtocol::Analog};
ADSChannel channel;
adsGain_t gain = GAIN_ONE;
bool sensorConnected = false;
};
#endif
\ No newline at end of file
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