agrobot_base/Firmware/Modulos/SensorCorrenteModel.h

168 lines
4.7 KiB
C++

#include "SerialService.h"
#include "Pinout.h"
#include "Utils.h"
#include <Adafruit_INA219.h>
#ifndef SensorCorrenteModel
#define SensorCorrenteModel
class SensorCorrente {
public:
static void InicializarLista(std::vector<SensorCorrente*>& lista, const String& parametros, 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;
}
PrintTela("Tentando iniciar o sensor de corrente no endereco " + String(_Endereco));
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 AferirCorrente() {
if (Iniciado) {
busVoltage = sINA219.getBusVoltage_V();
shuntVoltage = sINA219.getShuntVoltage_mV();
current = sINA219.getCurrent_mA();
power = sINA219.getPower_mW();
}
}
void RequisitarDados() {
AferirCorrente();
}
std::vector<uint8_t> MontarMensagemCAN() {
std::vector<uint8_t> data;
data.push_back(ID_Num);
data.push_back(Iniciado ? 1 : 0);
// 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));*/
return data;
}
};
// Função para inicializar os sensores de corrente com IDs e pinos específicos
void SensorCorrente::InicializarLista(std::vector<SensorCorrente*>& lista, const String& parametros, const String& Mod_ID) {
// Verifica se há sensores já na lista
if (!lista.empty()) {
PrintTela("Limpando lista de sensores de corrente existente...");
for (auto* sensor : lista) {
sensor->Desligar(); // Desliga cada sensor antes de remover
PrintTela("Sensor de Corrente " + sensor->_ID + " desligado.");
delete sensor;
}
lista.clear(); // Limpa a lista após desligar todos os itens
}
if (parametros.length() > 0) {
std::vector<String> _config = SplitString(parametros, SplitConfig); // Divide a configuração por vírgulas
for (const String& config : _config) {
std::vector<String> partes = SplitString(config, SplitSubParams); // Divide cada configuração pelo '-'
if (partes.size() >= 6) {
String id = partes[0];
bool conectar = partes[1] == "1";
String _endereco = partes[4];
int _a_max = partes[5].toDouble();
if (conectar) {
SensorCorrente* sensor = new SensorCorrente(Mod_ID, id);
sensor->ID_Num = lista.size();
sensor->_Endereco = stringToByte(_endereco);
sensor->_A_Shunt = _a_max;
sensor->Inicializar();
lista.push_back(sensor);
PrintTela("Sensor de corrente " + id + " inicializado no endereco " + _endereco);
}
} else {
PrintTela("Configuração inválida para sensor de corrente: " + config);
}
}
} else {
PrintTela("Configuração de sensor de corrente não encontrada.");
}
}
#endif