159 lines
4.3 KiB
C++
159 lines
4.3 KiB
C++
#ifndef SensorChuvaModel
|
|
#define SensorChuvaModel
|
|
|
|
#include "SerialService.h"
|
|
#include "Pinout.h"
|
|
#include "Utils.h"
|
|
|
|
class SensorChuva {
|
|
public:
|
|
|
|
SensorChuva(String _modID, String _id) {
|
|
Mod_ID = _modID;
|
|
_ID = _id;
|
|
}
|
|
|
|
String Mod_ID = "";
|
|
String _ID;
|
|
int ID_Num;
|
|
PinoModel _pino;
|
|
|
|
double tensao = 0;
|
|
double percentual = -1;
|
|
|
|
bool Iniciado = false;
|
|
bool Checando = false;
|
|
|
|
void Inicializar() {
|
|
if (Iniciado) {
|
|
MostrarLog("Sensor já inicializado");
|
|
return;
|
|
}
|
|
|
|
_pino.Conectar();
|
|
Iniciado = _pino.Definido();
|
|
|
|
MostrarLog(Iniciado ? "Sensor iniciado" : "Sensor nao iniciado");
|
|
}
|
|
|
|
void Desligar() {
|
|
if (!Iniciado) {
|
|
MostrarLog("Sensor nao esta inicializado");
|
|
return;
|
|
}
|
|
|
|
_pino.Desconectar(); // Desconecta o pino
|
|
|
|
MostrarLog("Sensor Desligado");
|
|
|
|
Iniciado = false;
|
|
}
|
|
|
|
void RequisitarDados() {
|
|
AferirChuva();
|
|
}
|
|
|
|
std::vector<uint8_t> MontarMensagemCAN(CanMessagePosicaoDados posicao) {
|
|
std::vector<uint8_t> data;
|
|
data.push_back(static_cast<uint8_t>(posicao));
|
|
data.push_back(ID_Num);
|
|
switch (posicao) {
|
|
case CanMessagePosicaoDados::Status: {
|
|
data.push_back(Iniciado ? 1 : 0);
|
|
break;
|
|
}
|
|
case CanMessagePosicaoDados::Dados1: {
|
|
int16_t pInt = static_cast<int16_t>(round(percentual * 100.0f));
|
|
data.push_back((uint8_t)(pInt >> 8));
|
|
data.push_back((uint8_t)(pInt & 0xFF));
|
|
break;
|
|
}
|
|
}
|
|
return data;
|
|
}
|
|
|
|
static std::vector<uint8_t> ConfigurarSensor(std::vector<SensorChuva*>& lista, std::vector<uint8_t>& data, const String& Mod_ID) {
|
|
std::vector<uint8_t> status;
|
|
if (data.size() < 2) return status;
|
|
CanMessagePosicaoDados posicao = (CanMessagePosicaoDados)data[0];
|
|
uint8_t idNum = data[1];
|
|
auto it = std::find_if(lista.begin(), lista.end(), [idNum](SensorChuva* s) { return s->ID_Num == idNum; });
|
|
bool jaExiste = it != lista.end();
|
|
SensorChuva* sensor;
|
|
|
|
switch (posicao) {
|
|
case CanMessagePosicaoDados::Config1: {
|
|
if (data.size() < 7) return status;
|
|
bool conectar = data[3] == 1;
|
|
TiposBarramentos tipoBarramento = (TiposBarramentos)data[4];
|
|
int pino = data[5];
|
|
int canalMux = (data[6] == 255) ? -1 : data[6];
|
|
|
|
if (conectar) {
|
|
if (!jaExiste) sensor = new SensorChuva(Mod_ID, "sCHV_" + String(idNum));
|
|
else sensor = *it;
|
|
|
|
if (!sensor->Iniciado) {
|
|
sensor->ID_Num = idNum;
|
|
sensor->_pino = PinoModel(pino, tipoBarramento, _INPUT, ANALOGICO, idNum, canalMux);
|
|
sensor->Inicializar();
|
|
if (!jaExiste) {
|
|
lista.push_back(sensor);
|
|
sensor->MostrarLog("Sensor de chuva adicionado via CAN: sCHV_" + String(idNum));
|
|
}
|
|
}
|
|
status = sensor->MontarMensagemCAN(CanMessagePosicaoDados::Status);
|
|
} else {
|
|
if (jaExiste) {
|
|
sensor = *it;
|
|
sensor->Desligar();
|
|
status = sensor->MontarMensagemCAN(CanMessagePosicaoDados::Status);
|
|
sensor->MostrarLog("Sensor de chuva removido via CAN: sCHV_" + String(idNum));
|
|
delete sensor;
|
|
lista.erase(it);
|
|
}
|
|
}
|
|
break;
|
|
}
|
|
}
|
|
return status;
|
|
}
|
|
|
|
private:
|
|
|
|
bool DegugMode = true;
|
|
|
|
void MostrarLog(String mensagem) {
|
|
if (DegugMode) {
|
|
PrintTela("[CHV " + _ID + "] " + mensagem);
|
|
}
|
|
}
|
|
|
|
const float Vs = 5.0f; // Tensao de alimentacao do sensor = 5V
|
|
static constexpr float R1 = 9.94f;
|
|
static constexpr float R2 = 5.4f;
|
|
static constexpr float divisor = R1 / (R1 + R2); // R1 = 5K6, R2 = 10K
|
|
static constexpr float vOffset = (1.211f / 0.76f); // v real / v ads
|
|
const float tSeco = 1.133f;
|
|
const float tMolhado = 0.47f;
|
|
|
|
void AferirChuva() {
|
|
if (!Iniciado) return;
|
|
|
|
int leitura = _pino.get();
|
|
tensao = (float)leitura / 4095.0f * Vs;
|
|
tensao = tensao * divisor * vOffset;
|
|
|
|
percentual = constrain(
|
|
100.0f * (tSeco - tensao) / (tSeco - tMolhado),
|
|
0.0f, 100.0f
|
|
);
|
|
|
|
MostrarLog("Leitura: " + String(leitura) + ", Tensao: " + String(tensao) + ", Percentual: " + String(percentual));
|
|
}
|
|
|
|
|
|
};
|
|
|
|
#endif
|