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

update dr26 library, early exit if sensor not connected, minimize rs485 delay,...

update dr26 library, early exit if sensor not connected, minimize rs485 delay, minor SentecSensorsRS485 refactor, logging changes in host
parent 0d5eefaa
No related branches found
No related tags found
2 merge requests!39Merge Develop into Main,!30Renamed Sentec Sensors, made RS485 serial connection a singleton shared...
Pipeline #103329 passed
......@@ -130,18 +130,8 @@ ErrorType SentecSensorRS485::getResponse() {
ESP_LOGD(TAG, "Response: 0x%s", formatBytes(answerFrame, responseLength).c_str());
// TODO: Not optimal, has precedence
auto connectionEndedPrematurelyReturnCode = checkConnectionEndedPrematurely(idx, responseLength);
auto sensorConnectedReturnCode = checkSensorConnected();
auto crcReturnCode = checkCRC(responseLength);
if (sensorConnectedReturnCode != ErrorType::DATA_OK) {
returnCode = sensorConnectedReturnCode;
} else if (connectionEndedPrematurelyReturnCode != ErrorType::DATA_OK) {
returnCode = connectionEndedPrematurelyReturnCode;
} else if (crcReturnCode != ErrorType::DATA_OK) {
returnCode = crcReturnCode;
}
// TODO: Test this @future_zoe
returnCode = checkForErrors(idx, responseLength);
// breaking after first successfull try
if (returnCode == ErrorType::DATA_OK) {
......@@ -149,9 +139,25 @@ ErrorType SentecSensorRS485::getResponse() {
}
}
// ESP_LOGE(TAG, "Returncode: %d", returnCode);
return returnCode;
}
ErrorType SentecSensorRS485::checkForErrors(int idx, int responseLength) {
// TODO: Not optimal, has precedence
auto connectionEndedPrematurelyReturnCode = checkConnectionEndedPrematurely(idx, responseLength);
auto sensorConnectedReturnCode = checkSensorConnected();
auto crcReturnCode = checkCRC(responseLength);
if (sensorConnectedReturnCode != ErrorType::DATA_OK) {
return sensorConnectedReturnCode;
} else if (connectionEndedPrematurelyReturnCode != ErrorType::DATA_OK) {
return connectionEndedPrematurelyReturnCode;
} else if (crcReturnCode != ErrorType::DATA_OK) {
return crcReturnCode;
}
return ErrorType::DATA_OK;
}
ErrorType SentecSensorRS485::checkCRC(int responseLength) {
word crc_received = word(answerFrame[responseLength - 2], answerFrame[responseLength - 1]);
word crc = calculateCRC(answerFrame, responseLength - 2);
......
......@@ -80,6 +80,7 @@ class SentecSensorRS485 {
ErrorType checkSensorConnected() const;
ErrorType checkConnectionEndedPrematurely(int idx, int responseLength) const;
ErrorType checkCRC(int responseLength);
ErrorType checkForErrors(int idx, int responseLength);
};
#endif
......@@ -3,8 +3,11 @@
static const char *TAG = "DR26";
void ForteDR26::setup() {
if (sensorConnected) {
ESP_LOGD(TAG, "ADS already initialized");
return;
}
Wire.begin();
ads.setGain(GAIN_ONE);
ads.setDataRate(RATE_ADS1115_8SPS);
if (ads.begin()) {
......@@ -14,8 +17,6 @@ void ForteDR26::setup() {
ESP_LOGE(TAG, "failed to initialize ADS");
sensorConnected = false;
}
delay(100);
channel = 0;
}
......@@ -23,25 +24,22 @@ Measurement ForteDR26::readData() {
float volts = 0;
int16_t adc = 0;
if (!sensorConnected) {
return {ERROR_VALUE, 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) {
} 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};
}
if (!sensorConnected) {
return {ERROR_VALUE, channel, NO_I2C_ADDRESS, MeasurementType::CIRCUMFERENCE_INCREMENT,
ErrorType::SENSOR_INIT_FAILED};
return {ERROR_VALUE, channel, NO_I2C_ADDRESS, MeasurementType::CIRCUMFERENCE_INCREMENT, ErrorType::NO_DATA};
}
return {volts, channel, NO_I2C_ADDRESS, MeasurementType::CIRCUMFERENCE_INCREMENT,
ErrorType::DATA_OK};
return {volts, channel, NO_I2C_ADDRESS, MeasurementType::CIRCUMFERENCE_INCREMENT, ErrorType::DATA_OK};
}
// The following functions change the ADC input range: be careful
......@@ -70,9 +68,7 @@ void ForteDR26::setChannel(int c) {
std::list<Message> ForteDR26::buildMessages() {
std::list<Message> messages;
auto data = readData();
messages.emplace_back(data,
sensorInformation,
Time::getInstance().getEpochSeconds());
messages.emplace_back(data, sensorInformation, Time::getInstance().getEpochSeconds());
return messages;
}
......@@ -80,7 +76,5 @@ SensorInformation ForteDR26::getSensorInformation() const {
return sensorInformation;
}
// TODO: What is this?
Adafruit_ADS1115 ForteDR26::ads = ads;
......@@ -23,7 +23,7 @@ class ForteDR26: public ForteSensor<Measurement> {
const SensorInformation
sensorInformation{HardwareName::DRS26, SensorProtocol::Analog};
int channel;
bool sensorConnected = true;
bool sensorConnected = false;
};
#endif
\ No newline at end of file
......@@ -52,7 +52,7 @@ void ForteRS485::powerOnRS485Sensors() { // Power on sensor
digitalWrite(POWER_SWITCH_PIN_12V, HIGH);
digitalWrite(POWER_SWITCH_PIN_5V, HIGH); // Wait for sensors to power up
// TODO minimize delay
delay(1000);
delay(100);
}
std::list<Message> ForteRS485::buildMessages() {
......
......@@ -13,12 +13,14 @@ namespace SDCardLogger {
printToSerial = printToSerialAsWell;
}
int vprintf_into_sd(const char *szFormat, va_list args) {
String logstring = "[" + rtc.getDateTime() + "] ";
logstring += szFormat;
auto timeSinceBootMs = esp_timer_get_time() / 1000;
String logstring = String(timeSinceBootMs) + ", ";
// print current core
logstring += "Core: ";
logstring += xPortGetCoreID();
logstring += ". ";
logstring += ", ";
logstring += "[" + rtc.getDateTime() + "] ";
logstring += szFormat;
if (!SDUtilities::isSDAvailable()) {
if (printToSerial) {
logstring += " (SD card not available)\n";
......
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