#include "SerialService.h" #include "Pinout.h" #include "Utils.h" #include #ifndef SensorCorrenteModel #define SensorCorrenteModel class SensorCorrente { public: static void ConfigurarSensor(std::vector& lista, std::vector& 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 MontarMensagemCAN(CanMessagePosicaoDados posicao) { std::vector data; data.push_back(ID_Num); data.push_back(static_cast(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(); } }; void SensorCorrente::ConfigurarSensor(std::vector& lista, std::vector& data, const String& Mod_ID) { if (data.size() < 8) return; uint8_t idNum = data[1]; CanMessagePosicaoDados posicao = (CanMessagePosicaoDados)data[2]; switch (posicao) { case CanMessagePosicaoDados::Config1: { 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; }); if (conectar) { if (it == lista.end()) { SensorCorrente* sensor = new SensorCorrente(Mod_ID, "sCOR_" + String(idNum)); sensor->ID_Num = idNum; sensor->_Endereco = endereco; sensor->_A_Shunt = aMax; sensor->Inicializar(); lista.push_back(sensor); PrintTela("Sensor de corrente adicionado via CAN: sCOR_" + String(idNum)); } } else { if (it != lista.end()) { SensorCorrente* sensor = *it; sensor->Desligar(); delete sensor; lista.erase(it); PrintTela("Sensor de corrente removido via CAN: sCOR_" + String(idNum)); } } break; } } } #endif