Skip to content

Commit

Permalink
Temperature compensation for conductivity.
Browse files Browse the repository at this point in the history
  • Loading branch information
dakriy committed May 27, 2019
1 parent ce16456 commit 4b34620
Show file tree
Hide file tree
Showing 7 changed files with 65 additions and 10 deletions.
3 changes: 2 additions & 1 deletion ROV/src/Core/Event.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,14 +38,15 @@ namespace Core {
LightChange, // Data in LightChangeDetails
VideoRecord, // Data in bool
Shutdown, // No extra data
TemperatureTaken, // Data in float

Count
};

EventType type = Count;


std::variant<CameraMovement, SensorsRequested, LightChangeDetails, bool> data;
std::variant<CameraMovement, SensorsRequested, LightChangeDetails, bool, float> data;

explicit Event(EventType t) : type(t) {}
};
Expand Down
19 changes: 15 additions & 4 deletions ROV/src/Sensors/Conductivity.cpp
Original file line number Diff line number Diff line change
@@ -1,19 +1,30 @@
#include "Conductivity.h"
#include "../Core/GlobalContext.h"

Sensor::Conductivity::Conductivity() {
tempTaken = GlobalContext::get_core_event_handler()->add_event_callback([&](const Core::Event * e) {
float temp = std::get<float>(e->data);
conductivity.setTemperature(temp);
return true;
}, Core::Event::TemperatureTaken);
}

const Sensor::SensorInfo &Sensor::Conductivity::getSensorInfo() {
return info;
}

bool Sensor::Conductivity::setup() {
auto s = conductivity.init();
// conductivity.singleCalibrate(53065.f);
return s;
return conductivity.init();
}

void Sensor::Conductivity::initiateConversion() {
conductivity.activate();
}

float Sensor::Conductivity::queryDevice() {
return conductivity.getTDM();
return conductivity.getPSS();
}

Sensor::Conductivity::~Conductivity() {
GlobalContext::get_core_event_handler()->unhook_event_callback_for_all_events(tempTaken);
}
9 changes: 7 additions & 2 deletions ROV/src/Sensors/Conductivity.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,26 @@

#include "Sensor.h"
#include "lib/ECEZ0.h"
#include "../Core/Event.h"
#include "../Core/EventHandler.h"

namespace Sensor {
class Conductivity : public Sensor{
protected:
const SensorInfo info{
.id = static_cast<sf::Uint8>(SensorId::Conductivity),
.maxFrequency = 1.f / 0.64f,
.name = "Conductivity",
.units = "ppm"
.name = "Salinity",
.units = "unitless"
};
ECEZ0 conductivity;
EVENT_FUNC_INDEX_CORE tempTaken = nullptr;
public:
Conductivity();
const SensorInfo& getSensorInfo() override;
bool setup() override;
void initiateConversion() override;
float queryDevice() override;
~Conductivity() override;
};
}
1 change: 1 addition & 0 deletions ROV/src/Sensors/Sensor.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,5 +33,6 @@ namespace Sensor {
virtual const SensorInfo& getSensorInfo() = 0;
virtual void initiateConversion() = 0;
virtual float queryDevice() = 0;
virtual ~Sensor() = default;
};
}
7 changes: 6 additions & 1 deletion ROV/src/Sensors/Temperature.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#include "Temperature.h"
#include "../Core/GlobalContext.h"

const Sensor::SensorInfo &Sensor::Temperature::getSensorInfo() {
return info;
Expand All @@ -13,5 +14,9 @@ void Sensor::Temperature::initiateConversion() {
}

float Sensor::Temperature::queryDevice() {
return temp.temperature();
float t = temp.temperature();
auto ev = std::make_unique<Core::Event>(Core::Event::TemperatureTaken);
ev->data = t;
GlobalContext::get_engine()->add_event(std::move(ev));
return t;
}
30 changes: 29 additions & 1 deletion ROV/src/Sensors/lib/ECEZ0.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
* @param uS
*/
void Sensor::ECEZ0::calibrate(float lowuS, float highuS) {
activate();
// Clear calibrations
writeReg(CALIBRATION_REQUEST_REGISTER, 0x01);
delay(2000);
Expand Down Expand Up @@ -55,6 +56,9 @@ void Sensor::ECEZ0::calibrate(float lowuS, float highuS) {
writeReg(CALIBRATION_REQUEST_REGISTER, 0x05);
delay(1000);

writeReg(MODE_REGISTER, 0x00);
delay(1000);

// Confirm
auto v = readReg(CALIBRATION_CONFIRMATION_REGISTER);
return;
Expand Down Expand Up @@ -91,6 +95,7 @@ float Sensor::ECEZ0::getEC() {
}

void Sensor::ECEZ0::singleCalibrate(float uS) {
activate();
// Clear calibrations
writeReg(CALIBRATION_REQUEST_REGISTER, 0x01);
delay(2000);
Expand All @@ -116,6 +121,10 @@ void Sensor::ECEZ0::singleCalibrate(float uS) {

delay(1000);

writeReg(MODE_REGISTER, 0x00);

delay(1000);

// Confirm
auto v = readReg(CALIBRATION_CONFIRMATION_REGISTER);
return;
Expand All @@ -138,9 +147,28 @@ void Sensor::ECEZ0::activate() {

}

float Sensor::ECEZ0::getTDM() {
float Sensor::ECEZ0::getTDS() {
// Put into hibernate mode
writeReg(MODE_REGISTER, 0x00);

return read4Reg(TDS_REGISTER_START);
}

void Sensor::ECEZ0::setTemperature(float temp) {
auto tempToSend = static_cast<unsigned>(temp * 100);
std::vector<unsigned char> bytes(4);
for (unsigned i = 0; i < 4; ++i) {
bytes[3-i] = (tempToSend >> (i * 8));
}

for (unsigned i = 0; i < 4; ++i) {
writeReg(TEMP_REGISTER_START + i, bytes[i]);
}
}

float Sensor::ECEZ0::getPSS() {
// Hibernate
writeReg(MODE_REGISTER, 0x00);

return read4Reg(PSS_REGISTER_START);
}
6 changes: 5 additions & 1 deletion ROV/src/Sensors/lib/ECEZ0.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,12 @@ namespace Sensor {
const unsigned char CALIBRATION_REGISTER_START = 0x0A;
const unsigned char CALIBRATION_REQUEST_REGISTER = 0x0E;
const unsigned char CALIBRATION_CONFIRMATION_REGISTER = 0x0F;
const unsigned char TEMP_REGISTER_START = 0x10;
const unsigned char MODE_REGISTER = 0x06;
// 4 bytes long
const unsigned char EC_REGISTER_START = 0x18;
const unsigned char TDS_REGISTER_START = 0x1C;
const unsigned char PSS_REGISTER_START = 0x20;

int fd = -1;

Expand All @@ -26,9 +28,11 @@ namespace Sensor {
// to step through it one at a time.
void calibrate(float lowuS, float highuS);
void singleCalibrate(float uS);
void setTemperature(float temp);
bool init();
void activate();
float getEC();
float getTDM();
float getTDS();
float getPSS();
};
}

0 comments on commit 4b34620

Please sign in to comment.