177 lines
5.0 KiB
C++
177 lines
5.0 KiB
C++
#include "SerialService.h"
|
|
#include "Pinout.h"
|
|
#include "Utils.h"
|
|
#include <Adafruit_INA219.h>
|
|
|
|
#ifndef SensorCorrenteModel
|
|
#define SensorCorrenteModel
|
|
|
|
class SensorCorrente {
|
|
public:
|
|
|
|
static std::vector<uint8_t> ConfigurarSensor(std::vector<SensorCorrente*>& lista, std::vector<uint8_t>& data, const String& Mod_ID);
|
|
|
|
SensorCorrente(String _modID, String _id) {
|
|
Mod_ID = _modID;
|
|
_ID = _id;
|
|
}
|
|
|
|
// Variáveis
|
|
Adafruit_INA219 sINA219;
|
|
|
|
// Definições
|
|
String Mod_ID = "";
|
|
String _ID;
|
|
int ID_Num;
|
|
byte _Endereco;
|
|
double _A_Shunt;
|
|
|
|
bool Iniciado = false;
|
|
float busVoltage = 0;
|
|
float shuntVoltage = 0;
|
|
float current = 0;
|
|
float power = 0;
|
|
|
|
void Inicializar() {
|
|
if (Iniciado) {
|
|
PrintTela(_ID + " ja inicializado");
|
|
return;
|
|
}
|
|
|
|
if (_Endereco > 0x00) {
|
|
Wire.beginTransmission(_Endereco);
|
|
byte error = Wire.endTransmission();
|
|
if (error == 0) {
|
|
sINA219 = Adafruit_INA219(_Endereco);
|
|
Iniciado = sINA219.begin();
|
|
if (Iniciado) {
|
|
if (_A_Shunt == 3.2) {
|
|
sINA219.setCalibration_32V_2A();
|
|
}
|
|
else if (_A_Shunt == 50) {
|
|
sINA219.setCalibration_50A_75mV();
|
|
}
|
|
else if (_A_Shunt == 100) {
|
|
sINA219.setCalibration_100A_75mV();
|
|
}
|
|
PrintTela(_ID + " iniciado");
|
|
}
|
|
else {
|
|
PrintTela(_ID + " nao iniciado!");
|
|
}
|
|
}
|
|
else {
|
|
Iniciado = false;
|
|
PrintTela(_ID + " nao iniciado, endereco " + _Endereco + " nao encontrado!");
|
|
}
|
|
}
|
|
else {
|
|
PrintTela("Endereco incorreto!");
|
|
}
|
|
}
|
|
|
|
void Desligar() {
|
|
if (!Iniciado) {
|
|
PrintTela(_ID + " nao esta inicializado");
|
|
return;
|
|
}
|
|
|
|
PrintTela(_ID + " Desligado");
|
|
|
|
Iniciado = false;
|
|
}
|
|
|
|
void RequisitarDados() {
|
|
AferirCorrente();
|
|
}
|
|
|
|
std::vector<uint8_t> MontarMensagemCAN(CanMessagePosicaoDados posicao) {
|
|
std::vector<uint8_t> data;
|
|
data.push_back(ID_Num);
|
|
data.push_back(static_cast<uint8_t>(posicao));
|
|
switch (posicao) {
|
|
case CanMessagePosicaoDados::Status: {
|
|
data.push_back(Iniciado ? 1 : 0);
|
|
break;
|
|
}
|
|
case CanMessagePosicaoDados::Dados1: {
|
|
// BusVoltage com 2 bytes, precisão x100
|
|
uint16_t busVoltsInt = (uint16_t)(busVoltage * 100);
|
|
data.push_back((uint8_t)(busVoltsInt >> 8));
|
|
data.push_back((uint8_t)(busVoltsInt & 0xFF));
|
|
// Corrente com 2 bytes (em mA)
|
|
uint16_t correnteInt = (uint16_t)(current);
|
|
data.push_back((uint8_t)(correnteInt >> 8));
|
|
data.push_back((uint8_t)(correnteInt & 0xFF));
|
|
// Potência com 2 bytes
|
|
uint16_t potenciaInt = (uint16_t)(power);
|
|
data.push_back((uint8_t)(potenciaInt >> 8));
|
|
data.push_back((uint8_t)(potenciaInt & 0xFF));
|
|
break;
|
|
}
|
|
}
|
|
return data;
|
|
}
|
|
|
|
private:
|
|
void AferirCorrente() {
|
|
if (!Iniciado) return;
|
|
|
|
busVoltage = sINA219.getBusVoltage_V();
|
|
//shuntVoltage = sINA219.getShuntVoltage_mV();
|
|
current = sINA219.getCurrent_mA();
|
|
power = sINA219.getPower_mW();
|
|
}
|
|
|
|
|
|
};
|
|
|
|
std::vector<uint8_t> SensorCorrente::ConfigurarSensor(std::vector<SensorCorrente*>& lista, std::vector<uint8_t>& data, const String& Mod_ID) {
|
|
std::vector<uint8_t> status;
|
|
if (data.size() < 3) return status;
|
|
uint8_t idNum = data[1];
|
|
CanMessagePosicaoDados posicao = (CanMessagePosicaoDados)data[2];
|
|
switch (posicao) {
|
|
case CanMessagePosicaoDados::Config1: {
|
|
if (data.size() < 8) return status;
|
|
bool conectar = data[4] == 1;
|
|
uint8_t endereco = data[5];
|
|
// _a_max vem como float*100 => uint16_t
|
|
uint16_t aMaxInt = ((uint16_t)data[6] << 8) | data[7];
|
|
float aMax = aMaxInt / 100.0;
|
|
auto it = std::find_if(lista.begin(), lista.end(), [idNum](SensorCorrente* s) { return s->ID_Num == idNum; });
|
|
bool existente = (it != lista.end());
|
|
if (conectar) {
|
|
SensorCorrente* sensor;
|
|
if (!existente) {
|
|
sensor = new SensorCorrente(Mod_ID, "sCOR_" + String(idNum));
|
|
}
|
|
else {
|
|
sensor = *it;
|
|
}
|
|
sensor->ID_Num = idNum;
|
|
sensor->_Endereco = endereco;
|
|
sensor->_A_Shunt = aMax;
|
|
sensor->Inicializar();
|
|
if (!existente) {
|
|
lista.push_back(sensor);
|
|
PrintTela("Sensor de corrente adicionado via CAN: sCOR_" + String(idNum));
|
|
}
|
|
status = sensor->MontarMensagemCAN(CanMessagePosicaoDados::Status);
|
|
} else {
|
|
if (existente) {
|
|
SensorCorrente* sensor = *it;
|
|
sensor->Desligar();
|
|
status = sensor->MontarMensagemCAN(CanMessagePosicaoDados::Status);
|
|
delete sensor;
|
|
lista.erase(it);
|
|
PrintTela("Sensor de corrente removido via CAN: sCOR_" + String(idNum));
|
|
}
|
|
}
|
|
break;
|
|
}
|
|
}
|
|
return status;
|
|
}
|
|
|
|
#endif |