diff --git a/AgroBase/.vs/AgroBase/FileContentIndex/57ddc087-206b-4a8b-8a18-7c1c31afa7c2.vsidx b/AgroBase/.vs/AgroBase/FileContentIndex/efd2c32e-da06-46bc-a5ed-cbcb6045c42b.vsidx similarity index 69% rename from AgroBase/.vs/AgroBase/FileContentIndex/57ddc087-206b-4a8b-8a18-7c1c31afa7c2.vsidx rename to AgroBase/.vs/AgroBase/FileContentIndex/efd2c32e-da06-46bc-a5ed-cbcb6045c42b.vsidx index c682b222b..3ab2a7609 100644 Binary files a/AgroBase/.vs/AgroBase/FileContentIndex/57ddc087-206b-4a8b-8a18-7c1c31afa7c2.vsidx and b/AgroBase/.vs/AgroBase/FileContentIndex/efd2c32e-da06-46bc-a5ed-cbcb6045c42b.vsidx differ diff --git a/AgroBase/.vs/AgroBase/v17/.suo b/AgroBase/.vs/AgroBase/v17/.suo index a1b69020d..546004315 100644 Binary files a/AgroBase/.vs/AgroBase/v17/.suo and b/AgroBase/.vs/AgroBase/v17/.suo differ diff --git a/AgroBase/AgroBase/Models/Modules/MovimentacaoModel.cs b/AgroBase/AgroBase/Models/Modules/MovimentacaoModel.cs index 01f84212f..63012ec82 100644 --- a/AgroBase/AgroBase/Models/Modules/MovimentacaoModel.cs +++ b/AgroBase/AgroBase/Models/Modules/MovimentacaoModel.cs @@ -429,7 +429,7 @@ namespace AgroBase.Models.Modules { string Mod_ID = Variaveis.OperacaoEmAndamento.DispMvd.Dados.Modulos.FirstOrDefault(x => x.MovMotor._EnderecoCAN_Tx == _EnderecoCAN_Tx)?.Modulo_ID ?? ""; var ServoFreio = Variaveis.OperacaoEmAndamento.DispSen.Dados.Servos.FirstOrDefault(x => x.ID.Contains(Mod_ID) && x.Inicializado && x.Controlar); - if (ServoFreio != null) + if (ServoFreio != null && ServoFreio.LeituraAngulo != AnguloFreio) { GeneralJoystick.EnviarComandoSensoriamento(S_Code.sFRO, ServoFreio.ID, AnguloFreio); } diff --git a/AgroBase/AgroBase/Models/Modules/SensoriamentoModel.cs b/AgroBase/AgroBase/Models/Modules/SensoriamentoModel.cs index 256feecac..966397f3f 100644 --- a/AgroBase/AgroBase/Models/Modules/SensoriamentoModel.cs +++ b/AgroBase/AgroBase/Models/Modules/SensoriamentoModel.cs @@ -2049,7 +2049,7 @@ namespace AgroBase.Models.Modules { _sensor.BusVoltage = Parametros.Count > 0 ? Parametros[0] : -1; _sensor.Current = Parametros.Count > 1 ? Parametros[1] : -1; - _sensor.Power = Parametros.Count > 2 ? (Parametros[2] / 100.0) : -1; + _sensor.Power = Parametros.Count > 2 ? Parametros[2] : -1; break; } case CanMessagePosicaoDados.Dados2: @@ -2324,9 +2324,10 @@ namespace AgroBase.Models.Modules } case S_Code.sCOR: { - double busVoltage = ConverterByteParaDouble(data, 2); - double current = ConverterByteParaDouble(data, 4); - double power = ConverterByteParaDouble(data, 6); + double busVoltage = ConverterByteParaDouble(data, 2); // continua igual + double current = ConverterByteParaDouble(data, 4) * 1000.0; // volts / 100, amps * 10 (volta para mA) + double power = ConverterByteParaDouble(data, 6) * 1000.0; // volta para mW + Console.WriteLine($"[AB36V] Tensao {busVoltage}, Corrente: {current}, Power: {power}"); DefinirOuCriar(ID_Num, posicao, componente, new List() { busVoltage, current, power }); break; } @@ -2399,8 +2400,8 @@ namespace AgroBase.Models.Modules { case S_Code.sCOR: { - double energia = ConverterByteParaDouble(data, 2); - double temperatura = ConverterByteParaDouble(data, 4); + double energia = ConverterByteParaDouble(data, 2) * 1000.0; // volta para mWh + double temperatura = ConverterByteParaDouble(data, 4); // volta para °C DefinirOuCriar(ID_Num, posicao, componente, new List() { energia, temperatura }); break; } diff --git a/Firmware/Modulos/I2CService.h b/Firmware/Modulos/I2CService.h index e144535ce..31b9aef1f 100644 --- a/Firmware/Modulos/I2CService.h +++ b/Firmware/Modulos/I2CService.h @@ -44,7 +44,7 @@ class I2CService { } I2CIniciado = Wire.begin(_pinoSDA, _pinoSCL); //Wire.setTimeout(50); - PrintTela("[I2C] I2C inicializado nos pinos SDA=" + String(_pinoSDA) + " e SCL=" + String(_pinoSCL) + " - Res " + I2CIniciado); + MostrarLog("I2C", "I2C inicializado nos pinos SDA=" + String(_pinoSDA) + " e SCL=" + String(_pinoSCL) + " - Res " + I2CIniciado); if (i2cMutex == nullptr) { i2cMutex = xSemaphoreCreateMutex(); } @@ -68,7 +68,7 @@ class I2CService { byte error = Wire.endTransmission(); if ((millis() - t0) > timeoutMs) { - PrintTela("[I2C] Timeout verificando endereco " + String(_Endereco)); + MostrarLog("I2C", "Timeout verificando endereco " + String(_Endereco)); return false; } @@ -77,13 +77,13 @@ class I2CService { static bool SolicitarAcessoI2C(int idSensor = 0, int canalMux = -1, uint32_t timeoutMs = 200) { if (!I2CIniciado) { - PrintTela("[I2C] Solicitacao de acesso negada para o sensor " + String(idSensor) + " - I2C Nao inicializado"); + MostrarLog("I2C", "Solicitacao de acesso negada para o sensor " + String(idSensor) + " - I2C Nao inicializado"); return false; } TickType_t timeoutTicks = pdMS_TO_TICKS(timeoutMs); if (xSemaphoreTake(i2cMutex, timeoutTicks) != pdTRUE) { - PrintTela("[I2C] Timeout ao tentar acessar o I2C - ID: " + String(idSensor)); + MostrarLog("I2C", "Timeout ao tentar acessar o I2C - ID: " + String(idSensor)); return false; } @@ -124,7 +124,7 @@ class I2CService { Wire.beginTransmission(addr); Wire.write(reg); if (Wire.endTransmission(false) != 0) { - PrintTela("[I2C] Falha ao endTransmission (leitura)"); + MostrarLog("I2C", "Falha ao endTransmission (leitura)"); return false; } delay(1); // <----- ADICIONAR este delay aqui @@ -132,7 +132,7 @@ class I2CService { size_t received = Wire.requestFrom((int)addr, (int)qtd); if (received != qtd || (millis() - t0) > timeoutMs) { - PrintTela("[I2C] Timeout ou leitura incompleta"); + MostrarLog("I2C", "Timeout ou leitura incompleta"); return false; } @@ -140,7 +140,7 @@ class I2CService { if (Wire.available()) { buffer[i] = Wire.read(); } else { - PrintTela("[I2C] Dados insuficientes disponíveis"); + MostrarLog("I2C", "Dados insuficientes disponíveis"); return false; } } @@ -159,7 +159,7 @@ class I2CService { int resultado = Wire.endTransmission(); if ((millis() - t0) > timeoutMs || resultado != 0) { - PrintTela("[I2C] Falha na escrita ou timeout"); + MostrarLog("I2C", "Falha na escrita ou timeout"); return false; } @@ -179,112 +179,120 @@ class I2CService { Wire.beginTransmission(endereco); Wire.write(1 << canalMux); Wire.endTransmission(); - PrintTela("[MUX] 0x" + String(endereco) + " - Canal alterado de " + String(canalAtivo) + " para " + String(canalMux)); + MostrarLog("MUX", "0x" + String(endereco) + " - Canal alterado de " + String(canalAtivo) + " para " + String(canalMux)); canalAtivo = canalMux; } return true; } - PrintTela("[MUX] Nao foi possivel trocar o canal para " + String(canalMux)); + MostrarLog("MUX", "Nao foi possivel trocar o canal para " + String(canalMux)); return false; } static void IniciarMUX(byte endereco = enderecoMux, bool Mandatorio = false) { if (!I2CIniciado) { - PrintTela("[MUX] Impossivel iniciar TCA9548A, I2C nao iniciado!"); + MostrarLog("MUX", "Impossivel iniciar TCA9548A, I2C nao iniciado!"); return; } - PrintTela("[MUX] Iniciando TCA9548A..."); + MostrarLog("MUX", "Iniciando TCA9548A..."); if (!MuxIniciado) { SolicitarAcessoI2C(); MuxIniciado = VerificaEnderecoBarramento(endereco); if (!MuxIniciado) { - PrintTela("[MUX] TCA9548A nao Iniciado, endereco " + String(endereco) + " nao encontrado no barramento"); + MostrarLog("MUX", "TCA9548A nao Iniciado, endereco " + String(endereco) + " nao encontrado no barramento"); if (Mandatorio) { - PrintTela("[MUX] Conexão obrigatória, tentando novamente em 5 segundos..."); + MostrarLog("MUX", "Conexão obrigatória, tentando novamente em 5 segundos..."); delay(5000); IniciarMUX(endereco, Mandatorio); } else { - PrintTela("[MUX] TCA9548A nao mandatorio, continuando o fluxo..."); + MostrarLog("MUX", "TCA9548A nao mandatorio, continuando o fluxo..."); } } else { canalAtivo = -1; // Nenhum canal ativo ainda idAtualUsandoI2C = -1; i2cMutex = xSemaphoreCreateMutex(); - PrintTela("[MUX] TCA9548A Iniciado"); + MostrarLog("MUX", "TCA9548A Iniciado"); } LiberarAcessoI2C(); } else { - PrintTela("[MUX] TCA9548A ja Iniciado"); + MostrarLog("MUX", "TCA9548A ja Iniciado"); } } static void IniciarMCP(byte endereco = enderecoMcp, bool Mandatorio = false) { if (!I2CIniciado) { - PrintTela("[MCP] Impossivel iniciar MCP23X17, I2C nao iniciado!"); + MostrarLog("MCP", "Impossivel iniciar MCP23X17, I2C nao iniciado!"); return; } - PrintTela("[MCP] Iniciando MCP23X17..."); + MostrarLog("MCP", "Iniciando MCP23X17..."); if (!McpIniciado) { SolicitarAcessoI2C(); McpIniciado = mcp.begin_I2C(endereco, &Wire); if (!McpIniciado) { - PrintTela("[MCP] Erro ao iniciar MCP23X17!"); + MostrarLog("MCP", "Erro ao iniciar MCP23X17!"); if (Mandatorio) { - PrintTela("[MCP] Conexão obrigatória, tentando novamente em 5 segundos..."); + MostrarLog("MCP", "Conexão obrigatória, tentando novamente em 5 segundos..."); delay(5000); IniciarMCP(endereco, Mandatorio); } else { - PrintTela("[MCP] MCP23X17 nao mandatorio, continuando o fluxo..."); + MostrarLog("MCP", "MCP23X17 nao mandatorio, continuando o fluxo..."); } } else { - PrintTela("[MCP] MCP23X17 Iniciado"); + MostrarLog("MCP", "MCP23X17 Iniciado"); } LiberarAcessoI2C(); } else { - PrintTela("[MCP] MCP23X17 ja Iniciado"); + MostrarLog("MCP", "MCP23X17 ja Iniciado"); } } static void IniciarADS(byte endereco = enderecoAds, bool Mandatorio = false) { if (!I2CIniciado) { - PrintTela("[ADS] Impossivel iniciar ADS1115, I2C nao iniciado!"); + MostrarLog("ADS", "Impossivel iniciar ADS1115, I2C nao iniciado!"); return; } - PrintTela("[ADS] Iniciando ADS1115..."); + MostrarLog("ADS", "Iniciando ADS1115..."); if (!AdsIniciado) { SolicitarAcessoI2C(); AdsIniciado = ads.begin(endereco, &Wire); if (!AdsIniciado) { - PrintTela("[ADS] Erro ao iniciar ADS1115!"); + MostrarLog("ADS", "Erro ao iniciar ADS1115!"); if (Mandatorio) { - PrintTela("[ADS] Conexão obrigatória, tentando novamente em 5 segundos..."); + MostrarLog("ADS", "Conexão obrigatória, tentando novamente em 5 segundos..."); delay(5000); IniciarADS(endereco, Mandatorio); } else { - PrintTela("[ADS] ADS1115 nao mandatorio, continuando o fluxo..."); + MostrarLog("ADS", "ADS1115 nao mandatorio, continuando o fluxo..."); } } else { ads.setGain(GAIN_ONE); - PrintTela("[ADS] ADS1115 Iniciado"); + MostrarLog("ADS", "ADS1115 Iniciado"); } LiberarAcessoI2C(); } else { - PrintTela("[ADS] ADS1115 ja Iniciado"); + MostrarLog("ADS", "ADS1115 ja Iniciado"); } } private: + static bool DebugMode; + + static void MostrarLog(String Componente, String mensagem) { + if (DebugMode) { + PrintTela("[" + Componente + "] " + mensagem); + } + } + static int _pinoSDA; static int _pinoSCL; static unsigned long tempoEntradaI2C; @@ -304,7 +312,7 @@ class I2CService { static void VerificarI2CPreso() { if (idAtualUsandoI2C >= 0 && (millis() - tempoEntradaI2C > LimiteTempoI2C)) { - PrintTela("[I2C] Semáforo preso pelo ID " + String(idAtualUsandoI2C) + " — forçando liberação!"); + MostrarLog("I2C", "Semáforo preso pelo ID " + String(idAtualUsandoI2C) + " — forçando liberação!"); RecriarMutex(); IniciarI2C(); } @@ -312,6 +320,7 @@ class I2CService { }; +bool I2CService::DebugMode = false; int I2CService::_pinoSDA = 1; int I2CService::_pinoSCL = 2; unsigned long I2CService::LimiteTempoI2C = 200; diff --git a/Firmware/Modulos/ReleModel.h b/Firmware/Modulos/ReleModel.h index ac86d4296..5dfb333dd 100644 --- a/Firmware/Modulos/ReleModel.h +++ b/Firmware/Modulos/ReleModel.h @@ -8,9 +8,6 @@ class Rele { public: - static std::vector ConfigurarRele(std::vector& lista, std::vector& data, const String& Mod_ID); - static void ComandarRele(std::vector& lista, std::vector& data); - Rele(String _modID, String _id) { Mod_ID = _modID; _ID = _id; @@ -28,7 +25,7 @@ class Rele { void Inicializar() { if (Iniciado) { - PrintTela(_ID + " ja inicializado"); + MostrarLog("Rele ja inicializado"); return; } @@ -37,25 +34,25 @@ class Rele { Iniciado = _pino.Definido(); if (Iniciado) { - PrintTela(_ID + " iniciado"); + MostrarLog("Rele iniciado"); } else { - PrintTela(_ID + " nao iniciado"); + MostrarLog("Rele nao iniciado"); } } void Desligar() { if (!Iniciado) { - PrintTela(_ID + " nao esta inicializado"); + MostrarLog("Rele nao esta inicializado"); return; } // Redefinir as configurações para os valores iniciais _pino.Desconectar(); - PrintTela(_ID + " Desligado"); + MostrarLog("Rele Desligado"); Iniciado = false; } @@ -67,10 +64,8 @@ class Rele { status = !status; } - PrintTela("Atualizando status do rele de ", false); - PrintTela(String(_Status), false); - PrintTela(" para ", false); - PrintTela(String(novoEstado)); + MostrarLog("Atualizando status do rele de " + String(_Status) + " para " + String(novoEstado)); + _pino.set(status); _Status = (Estado)_pino.get(); } @@ -103,74 +98,85 @@ class Rele { return data; } -}; - - -std::vector Rele::ConfigurarRele(std::vector& lista, std::vector& data, const String& Mod_ID) { - std::vector 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](Rele* s) { return s->ID_Num == idNum; }); - bool jaExiste = it != lista.end(); - Rele* sensor; - switch (posicao) { - case CanMessagePosicaoDados::Config1: { - if (data.size() < 7) return status; - bool conectar = data[3] == 1; - bool atuacaoNivelAlto = data[4] == 1; - TiposBarramentos tipoBarramento = (TiposBarramentos)data[5]; - int pino = data[6]; - if (conectar) { - if (!jaExiste) { - sensor = new Rele(Mod_ID, "sRLE_" + String(idNum)); - } - else { - sensor = *it; - } - if (!sensor->Iniciado) { - sensor->ID_Num = idNum; - sensor->_pino = PinoModel(pino, tipoBarramento, _OUTPUT); - sensor->AtuacaoNivelAlto = atuacaoNivelAlto; - sensor->Inicializar(); - if (!jaExiste) { - lista.push_back(sensor); - PrintTela("Rele adicionado via CAN: sRLE_" + String(idNum)); - } - } - status = sensor->MontarMensagemCAN(CanMessagePosicaoDados::Status); - } else { - if (jaExiste) { - sensor = *it; - sensor->Desligar(); - status = sensor->MontarMensagemCAN(CanMessagePosicaoDados::Status); - delete sensor; - lista.erase(it); - PrintTela("Rele removido via CAN: sRLE_" + String(idNum)); - } - } - break; - } - } - return status; -} - -void Rele::ComandarRele(std::vector& lista, std::vector& data) { - if (data.size() < 3) return; - CanMessagePosicaoDados posicao = (CanMessagePosicaoDados)data[0]; - uint8_t idNum = data[1]; - for (auto* _rele : lista) { - if (_rele->ID_Num == idNum) { + static std::vector ConfigurarRele(std::vector& lista, std::vector& data, const String& Mod_ID) { + std::vector 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](Rele* s) { return s->ID_Num == idNum; }); + bool jaExiste = it != lista.end(); + Rele* sensor; switch (posicao) { - case CanMessagePosicaoDados::Command1: { - Estado novoEstado = (Estado)data[2]; - _rele->AtualizarStatus(novoEstado); + case CanMessagePosicaoDados::Config1: { + if (data.size() < 7) return status; + bool conectar = data[3] == 1; + bool atuacaoNivelAlto = data[4] == 1; + TiposBarramentos tipoBarramento = (TiposBarramentos)data[5]; + int pino = data[6]; + if (conectar) { + if (!jaExiste) { + sensor = new Rele(Mod_ID, "sRLE_" + String(idNum)); + } + else { + sensor = *it; + } + if (!sensor->Iniciado) { + sensor->ID_Num = idNum; + sensor->_pino = PinoModel(pino, tipoBarramento, _OUTPUT); + sensor->AtuacaoNivelAlto = atuacaoNivelAlto; + sensor->Inicializar(); + if (!jaExiste) { + lista.push_back(sensor); + sensor->MostrarLog("Rele adicionado via CAN: sRLE_" + String(idNum)); + } + } + status = sensor->MontarMensagemCAN(CanMessagePosicaoDados::Status); + } else { + if (jaExiste) { + sensor = *it; + sensor->Desligar(); + status = sensor->MontarMensagemCAN(CanMessagePosicaoDados::Status); + sensor->MostrarLog("Rele removido via CAN: sRLE_" + String(idNum)); + delete sensor; + lista.erase(it); + } + } break; } } - return; + return status; } - } -} + + static void ComandarRele(std::vector& lista, std::vector& data) { + if (data.size() < 3) return; + CanMessagePosicaoDados posicao = (CanMessagePosicaoDados)data[0]; + uint8_t idNum = data[1]; + for (auto* _rele : lista) { + if (_rele->ID_Num == idNum) { + switch (posicao) { + case CanMessagePosicaoDados::Command1: { + Estado novoEstado = (Estado)data[2]; + _rele->AtualizarStatus(novoEstado); + break; + } + } + return; + } + } + } + + + private: + bool DebugMode = false; + + void MostrarLog(String mensagem) { + if (DebugMode) { + PrintTela("[RLE " + _ID + "] " + mensagem); + } + } + +}; + + #endif \ No newline at end of file diff --git a/Firmware/Modulos/SensorCorrenteModel.h b/Firmware/Modulos/SensorCorrenteModel.h index 012e42c5a..84bc09e1b 100644 --- a/Firmware/Modulos/SensorCorrenteModel.h +++ b/Firmware/Modulos/SensorCorrenteModel.h @@ -7,6 +7,7 @@ #include #include #include +#include class SensorCorrente { public: @@ -113,8 +114,8 @@ class SensorCorrente { } case CanMessagePosicaoDados::Dados1: { uint16_t volts = busVoltage * 100; - uint16_t amps = current * 100; - uint16_t watts = power * 10; + uint16_t amps = current / 10; + uint16_t watts = power / 10; data.push_back(volts >> 8); data.push_back(volts & 0xFF); data.push_back(amps >> 8); data.push_back(amps & 0xFF); data.push_back(watts >> 8); data.push_back(watts & 0xFF); @@ -122,7 +123,7 @@ class SensorCorrente { } case CanMessagePosicaoDados::Dados2: { if (isINA219) break; - uint16_t wh = energy * 10; + uint16_t wh = energy / 10; uint16_t temp = temperature * 100; data.push_back(wh >> 8); data.push_back(wh & 0xFF); data.push_back(temp >> 8); data.push_back(temp & 0xFF); @@ -172,9 +173,9 @@ class SensorCorrente { sensor = *it; sensor->Desligar(); status = sensor->MontarMensagemCAN(CanMessagePosicaoDados::Status); + sensor->MostrarLog("Sensor de corrente removido via CAN: sCOR_" + String(idNum)); delete sensor; lista.erase(it); - sensor->MostrarLog("Sensor de corrente removido via CAN: sCOR_" + String(idNum)); } } break; @@ -185,17 +186,18 @@ class SensorCorrente { private: - bool DebugMode = true; + bool DebugMode = false; void MostrarLog(String mensagem) { if (DebugMode) { PrintTela("[COR " + _ID + "] " + mensagem); } } + SimpleKalmanFilter filtro = SimpleKalmanFilter(10, 10, 0.05); Adafruit_INA219 sINA219; static constexpr float currentLSB_10A = 0.0001; - static constexpr float currentLSB_50A = 0.00119; - static constexpr float currentLSB_100A = 0.00243; + static constexpr float currentLSB_50A = 0.01367; + static constexpr float currentLSB_100A = 0.0243; static constexpr float shuntLSB = 1.0e-6; static constexpr float vBusLSB = 1.23e-5; float currentLSB = 0.0001; @@ -236,9 +238,10 @@ class SensorCorrente { if (!I2CService::SolicitarAcessoI2C(ID_Num, _CanalMUX)) return; if (isINA219) { - busVoltage = sINA219.getBusVoltage_V(); - current = sINA219.getCurrent_mA(); - power = sINA219.getPower_mW(); + busVoltage = sINA219.getBusVoltage_V(); // V + float _current = sINA219.getCurrent_mA(); // mA + current = filtro.updateEstimate(_current); // mA + power = sINA219.getPower_mW(); //mW MostrarLog("BusVoltage: " + String(busVoltage) + ", Current: " + String(current) + ", Power: " + String(power)); } @@ -249,11 +252,14 @@ class SensorCorrente { uint64_t rawEnergy = ((uint64_t)LerRegistradorINA(0x0A, 3) << 16) | LerRegistradorINA(0x0B, 2); uint16_t rawTemp = LerRegistradorINA(0x0F, 2); - busVoltage = rawBus * vBusLSB; - power = rawPower * currentLSB * 3.125; - current = rawCurrent * currentLSB; - energy = rawEnergy * currentLSB * 3.125 * 16 / 3600.0; - temperature = ((int16_t)rawTemp) * 0.0078125; + float K = 50.58; + + busVoltage = rawBus * vBusLSB; // V + power = rawPower * currentLSB * K; // mW + float _current = rawCurrent * currentLSB; // mA + current = filtro.updateEstimate(_current); // mA + energy = rawEnergy * currentLSB * K * 16 / 3600.0 / 1000.0; // Wh + temperature = ((int16_t)rawTemp) * 0.0078125; // C MostrarLog("BusVoltage: " + String(busVoltage) + ", Current: " + String(current) + ", Power: " + String(power) + ", Energy: " + String(energy) + ", Temperature: " + String(temperature)); } diff --git a/Firmware/Modulos/SensorIMUModel.h b/Firmware/Modulos/SensorIMUModel.h index 0ea52c7dd..020be4720 100644 --- a/Firmware/Modulos/SensorIMUModel.h +++ b/Firmware/Modulos/SensorIMUModel.h @@ -11,8 +11,6 @@ class SensorIMU { public: - static std::vector ConfigurarSensor(std::vector& lista, std::vector& data, const String& Mod_ID); - String Mod_ID; String _ID; int ID_Num; @@ -52,7 +50,7 @@ class SensorIMU { void Inicializar() { if (Iniciado) { - PrintTela(_ID + " ja inicializado"); + MostrarLog("Sensor ja inicializado"); return; } @@ -64,9 +62,9 @@ class SensorIMU { mpu.beginAccel(); mpu.beginGyro(); //mpu.beginMag(); - PrintTela("MPU9250 iniciado"); + MostrarLog("MPU9250 iniciado"); } else { - PrintTela("MPU9250 nao encontrado no endereco " + String(_EnderecoMPU)); + MostrarLog("MPU9250 nao encontrado no endereco " + String(_EnderecoMPU)); } bool BmpEncontrado = I2CService::VerificaEnderecoBarramento(_EnderecoBMP); @@ -78,21 +76,21 @@ class SensorIMU { Adafruit_BMP280::SAMPLING_X16, Adafruit_BMP280::FILTER_X16, Adafruit_BMP280::STANDBY_MS_500); - PrintTela("BMP280 iniciado"); + MostrarLog("BMP280 iniciado"); } else { - PrintTela("Erro ao iniciar BMP280"); + MostrarLog("Erro ao iniciar BMP280"); } } else { - PrintTela("BMP280 nao encontrado no endereco " + String(_EnderecoBMP)); + MostrarLog("BMP280 nao encontrado no endereco " + String(_EnderecoBMP)); } Iniciado = MpuIniciado || BmpIniciado; if (Iniciado) { filterDelay = 1000.0f / filterHz; xTaskCreatePinnedToCore(&SensorIMU::IMUTaskWrapper, "IMUTask", 4096, this, 10, &IMUTaskHandle, tskNO_AFFINITY); - PrintTela(_ID + " iniciado"); + MostrarLog("Sensor iniciado"); } I2CService::LiberarAcessoI2C(ID_Num); @@ -100,7 +98,7 @@ class SensorIMU { void Desligar() { if (!Iniciado) { - PrintTela(_ID + " nao esta inicializado"); + MostrarLog("Sensor nao esta inicializado"); return; } @@ -118,7 +116,7 @@ class SensorIMU { IMUTaskHandle = NULL; } - PrintTela(_ID + " Desligado"); + MostrarLog("Sensor Desligado"); } void RequisitarDados() { @@ -196,8 +194,64 @@ class SensorIMU { return data; } + static std::vector ConfigurarSensor(std::vector& lista, std::vector& data, const String& Mod_ID) { + std::vector 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](SensorIMU* s) { return s->ID_Num == idNum; }); + bool jaExiste = it != lista.end(); + SensorIMU* sensor; + switch (posicao) { + case CanMessagePosicaoDados::Config1: { + if (data.size() < 6) return status; + bool conectar = data[3] == 1; + uint8_t enderecoMpu = data[4]; + uint8_t enderecoBmp = data[5]; + if (conectar) { + if (!jaExiste) { + sensor = new SensorIMU(Mod_ID, "sIMU_" + String(idNum)); + } + else { + sensor = *it; + } + if (!sensor->Iniciado) { + sensor->ID_Num = idNum; + sensor->_EnderecoMPU = enderecoMpu; + sensor->_EnderecoBMP = enderecoBmp; + sensor->Inicializar(); + if (!jaExiste) { + lista.push_back(sensor); + sensor->MostrarLog("Sensor IMU adicionado via CAN: sIMU_" + String(idNum)); + } + } + status = sensor->MontarMensagemCAN(CanMessagePosicaoDados::Status); + } else { + if (jaExiste) { + sensor = *it; + sensor->Desligar(); + status = sensor->MontarMensagemCAN(CanMessagePosicaoDados::Status); + sensor->MostrarLog("Sensor IMU removido via CAN: sIMU_" + String(idNum)); + delete sensor; + lista.erase(it); + } + } + break; + } + } + return status; + } + private: + bool DebugMode = true; + + void MostrarLog(String mensagem) { + if (DebugMode) { + PrintTela("[IMU " + _ID + "]" + mensagem); + } + } + TaskHandle_t IMUTaskHandle = NULL; static void IMUTaskWrapper(void *pvParameters) { SensorIMU *sensor = static_cast(pvParameters); @@ -272,55 +326,9 @@ class SensorIMU { I2CService::LiberarAcessoI2C(ID_Num); } + }; -std::vector SensorIMU::ConfigurarSensor(std::vector& lista, std::vector& data, const String& Mod_ID) { - std::vector 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](SensorIMU* s) { return s->ID_Num == idNum; }); - bool jaExiste = it != lista.end(); - SensorIMU* sensor; - switch (posicao) { - case CanMessagePosicaoDados::Config1: { - if (data.size() < 6) return status; - bool conectar = data[3] == 1; - uint8_t enderecoMpu = data[4]; - uint8_t enderecoBmp = data[5]; - if (conectar) { - if (!jaExiste) { - sensor = new SensorIMU(Mod_ID, "sIMU_" + String(idNum)); - } - else { - sensor = *it; - } - if (!sensor->Iniciado) { - sensor->ID_Num = idNum; - sensor->_EnderecoMPU = enderecoMpu; - sensor->_EnderecoBMP = enderecoBmp; - sensor->Inicializar(); - if (!jaExiste) { - lista.push_back(sensor); - PrintTela("Sensor IMU adicionado via CAN: sIMU_" + String(idNum)); - } - } - status = sensor->MontarMensagemCAN(CanMessagePosicaoDados::Status); - } else { - if (jaExiste) { - sensor = *it; - sensor->Desligar(); - status = sensor->MontarMensagemCAN(CanMessagePosicaoDados::Status); - delete sensor; - lista.erase(it); - PrintTela("Sensor IMU removido via CAN: sIMU_" + String(idNum)); - } - } - break; - } - } - return status; -} #endif diff --git a/Firmware/Modulos/SensorTemperaturaModel.h b/Firmware/Modulos/SensorTemperaturaModel.h index ebaae910b..324e9a651 100644 --- a/Firmware/Modulos/SensorTemperaturaModel.h +++ b/Firmware/Modulos/SensorTemperaturaModel.h @@ -120,9 +120,9 @@ class SensorTemperatura { sensor = *it; sensor->Desligar(); status = sensor->MontarMensagemCAN(CanMessagePosicaoDados::Status); + sensor->MostrarLog("Sensor de temperatura removido via CAN: sTMP_" + String(idNum)); delete sensor; lista.erase(it); - sensor->MostrarLog("Sensor de temperatura removido via CAN: sTMP_" + String(idNum)); } } break; diff --git a/Firmware/Modulos/ServoFreioModel.h b/Firmware/Modulos/ServoFreioModel.h index a219eaa2b..390620aba 100644 --- a/Firmware/Modulos/ServoFreioModel.h +++ b/Firmware/Modulos/ServoFreioModel.h @@ -9,9 +9,6 @@ class ServoFreio { public: - static std::vector ConfigurarServo(std::vector& lista, std::vector& data, const String& Mod_ID); - static void ComandarServo(std::vector& lista, std::vector& data); - ServoFreio(String _modID, String _id) { Mod_ID = _modID; _ID = _id; @@ -32,7 +29,7 @@ class ServoFreio { void Inicializar() { if (Iniciado) { - PrintTela(_ID + " ja inicializado"); + MostrarLog("Servo ja inicializado"); return; } @@ -42,16 +39,16 @@ class ServoFreio { Iniciado = true; - PrintTela(_ID + " iniciado"); + MostrarLog("Servo iniciado"); } else { - PrintTela(_ID + " nao iniciado"); + MostrarLog("Servo nao iniciado"); } } void Desligar() { if (!Iniciado) { - PrintTela(_ID + " nao esta inicializado"); + MostrarLog("Servo nao esta inicializado"); return; } @@ -60,14 +57,14 @@ class ServoFreio { _Servo.detach(); } - PrintTela(_ID + " Desligado"); + MostrarLog("Servo Desligado"); Iniciado = false; } void AtualizarAngulo(int novoAngulo) { if (Iniciado) { - PrintTela("Angulo do freio " + _ID + " atualizado de " + String(_Angulo) + " para " + String(novoAngulo)); + MostrarLog("Angulo atualizado de " + String(_Angulo) + " para " + String(novoAngulo)); _Angulo = novoAngulo; @@ -96,74 +93,88 @@ class ServoFreio { return data; } -}; - - -std::vector ServoFreio::ConfigurarServo(std::vector& lista, std::vector& data, const String& Mod_ID) { - std::vector 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](ServoFreio* s) { return s->ID_Num == idNum; }); - bool jaExiste = it != lista.end(); - ServoFreio* sensor; - switch (posicao) { - case CanMessagePosicaoDados::Config1: { - if (data.size() < 7) return status; - bool conectar = data[3] == 1; - bool incremental = data[4] == 1; - TiposBarramentos tipoBarramento = (TiposBarramentos)data[5]; - int pino = data[6]; - if (conectar) { - if (!jaExiste) { - sensor = new ServoFreio(Mod_ID, "sFRO_" + String(idNum)); - } - else { - sensor = *it; - } - if (!sensor->Iniciado) { - sensor->ID_Num = idNum; - sensor->Incremental = incremental; - sensor->_pino = PinoModel(pino, tipoBarramento, _OUTPUT); - sensor->Inicializar(); - if (!jaExiste) { - lista.push_back(sensor); - PrintTela("Servo Freio adicionado via CAN: sFRO_" + String(idNum)); - } - } - status = sensor->MontarMensagemCAN(CanMessagePosicaoDados::Status); - } else { - if (jaExiste) { - sensor = *it; - sensor->Desligar(); - status = sensor->MontarMensagemCAN(CanMessagePosicaoDados::Status); - delete sensor; - lista.erase(it); - PrintTela("Servo Freio removido via CAN: sFRO_" + String(idNum)); - } - } - break; - } - } - return status; -} - -void ServoFreio::ComandarServo(std::vector& lista, std::vector& data) { - if (data.size() < 3) return; - CanMessagePosicaoDados posicao = (CanMessagePosicaoDados)data[0]; - uint8_t idNum = data[1]; - for (auto* _freio : lista) { - if (_freio->ID_Num == idNum) { + static std::vector ConfigurarServo(std::vector& lista, std::vector& data, const String& Mod_ID) { + std::vector 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](ServoFreio* s) { return s->ID_Num == idNum; }); + bool jaExiste = it != lista.end(); + ServoFreio* sensor; switch (posicao) { - case CanMessagePosicaoDados::Command1: { - int novoAngulo = (int)data[2]; - _freio->AtualizarAngulo(novoAngulo); + case CanMessagePosicaoDados::Config1: { + if (data.size() < 7) return status; + bool conectar = data[3] == 1; + bool incremental = data[4] == 1; + TiposBarramentos tipoBarramento = (TiposBarramentos)data[5]; + int pino = data[6]; + if (conectar) { + if (!jaExiste) { + sensor = new ServoFreio(Mod_ID, "sFRO_" + String(idNum)); + } + else { + sensor = *it; + } + if (!sensor->Iniciado) { + sensor->ID_Num = idNum; + sensor->Incremental = incremental; + sensor->_pino = PinoModel(pino, tipoBarramento, _OUTPUT); + sensor->Inicializar(); + if (!jaExiste) { + lista.push_back(sensor); + sensor->MostrarLog("Servo Freio adicionado via CAN: sFRO_" + String(idNum)); + } + } + status = sensor->MontarMensagemCAN(CanMessagePosicaoDados::Status); + } else { + if (jaExiste) { + sensor = *it; + sensor->Desligar(); + status = sensor->MontarMensagemCAN(CanMessagePosicaoDados::Status); + sensor->MostrarLog("Servo Freio removido via CAN: sFRO_" + String(idNum)); + delete sensor; + lista.erase(it); + } + } break; } } - return; + return status; } - } -} + + static void ComandarServo(std::vector& lista, std::vector& data) { + if (data.size() < 3) return; + CanMessagePosicaoDados posicao = (CanMessagePosicaoDados)data[0]; + uint8_t idNum = data[1]; + for (auto* _freio : lista) { + if (_freio->ID_Num == idNum) { + switch (posicao) { + case CanMessagePosicaoDados::Command1: { + int novoAngulo = (int)data[2]; + _freio->AtualizarAngulo(novoAngulo); + break; + } + } + return; + } + } + } + + + private: + bool DebugMode = true; + + void MostrarLog(String mensagem) { + if (DebugMode) { + PrintTela("[FRO " + _ID + "] " + mensagem); + } + } + +}; + + + + + #endif \ No newline at end of file diff --git a/Firmware/Modulos/SinaleiroModel.h b/Firmware/Modulos/SinaleiroModel.h index 38e2b7635..2de4f96f0 100644 --- a/Firmware/Modulos/SinaleiroModel.h +++ b/Firmware/Modulos/SinaleiroModel.h @@ -8,9 +8,6 @@ class Sinaleiro { public: - static std::vector ConfigurarSinaleiro(std::vector& lista, std::vector& data, const String& Mod_ID); - static void ComandarSinaleiro(std::vector& lista, std::vector& data); - Sinaleiro(String _modID, String _id, int _taxaAmostragem = 2000) { Mod_ID = _modID; _ID = _id; @@ -30,7 +27,7 @@ class Sinaleiro { void Inicializar() { if (Iniciado) { - PrintTela(_ID + " ja inicializado"); + MostrarLog("Sinaleiro ja inicializado"); return; } @@ -41,16 +38,16 @@ class Sinaleiro { if (Iniciado) { xTaskCreatePinnedToCore(&Sinaleiro::LEDTaskWrapper, "LEDTask", 5000, this, 1, &LEDTaskHandle, tskNO_AFFINITY); - PrintTela(_ID + " Iniciado"); + MostrarLog("Sinaleiro Iniciado"); } else { - PrintTela(_ID + " nao Iniciado"); + MostrarLog("Sinaleiro nao Iniciado"); } } void Desligar() { if (!Iniciado) { - PrintTela(_ID + " nao esta inicializado"); + MostrarLog("Sinaleiro nao esta inicializado"); return; } @@ -60,7 +57,7 @@ class Sinaleiro { // Redefinir as configurações para os valores iniciais _pino.Desconectar(); - PrintTela(_ID + " Desligado"); + MostrarLog("Sinaleiro Desligado"); Iniciado = false; } @@ -93,7 +90,86 @@ class Sinaleiro { return data; } + static std::vector ConfigurarSinaleiro(std::vector& lista, std::vector& data, const String& Mod_ID) { + std::vector 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](Sinaleiro* s) { return s->ID_Num == idNum; }); + bool jaExiste = it != lista.end(); + Sinaleiro* sensor; + switch (posicao) { + case CanMessagePosicaoDados::Config1: { + if (data.size() < 6) return status; + bool conectar = data[3] == 1; + TiposBarramentos tipoBarramento = (TiposBarramentos)data[4]; + int pino = data[5]; + if (conectar) { + if (!jaExiste) { + sensor = new Sinaleiro(Mod_ID, "sLED_" + String(idNum)); + } + else { + sensor = *it; + } + if (!sensor->Iniciado) { + sensor->ID_Num = idNum; + sensor->_pino = PinoModel(pino, tipoBarramento, _OUTPUT); + sensor->Inicializar(); + if (!jaExiste) { + lista.push_back(sensor); + sensor->MostrarLog("Sinaleiro adicionado via CAN: sLED_" + String(idNum)); + } + } + status = sensor->MontarMensagemCAN(CanMessagePosicaoDados::Status); + } else { + if (jaExiste) { + sensor = *it; + sensor->Desligar(); + status = sensor->MontarMensagemCAN(CanMessagePosicaoDados::Status); + sensor->MostrarLog("Sinaleiro removido via CAN: sLED_" + String(idNum)); + delete sensor; + lista.erase(it); + } + } + break; + } + } + return status; + } + + static void ComandarSinaleiro(std::vector& lista, std::vector& data) { + if (data.size() < 6) return; + CanMessagePosicaoDados posicao = (CanMessagePosicaoDados)data[0]; + uint8_t idNum = data[1]; + for (auto* _led : lista) { + if (_led->ID_Num == idNum) { + switch (posicao) { + case CanMessagePosicaoDados::Command1: { + StatusLED novoStatus = (StatusLED)data[2]; + int novaQtd = data[3]; + uint16_t novoTempo = (uint16_t)data[4] * 10; + uint16_t novaPausa = (uint16_t)data[5] * 10; + _led->_Status = novoStatus; + _led->_Qtd = novaQtd; + _led->_TempoMs = novoTempo; + _led->_PausaMs = novaPausa; + break; + } + } + return; + } + } + } + + private: + bool DebugMode = false; + void MostrarLog(String mensagem) { + if (DebugMode) { + PrintTela("[LED " + _ID + "]" + mensagem); + } + } + TaskHandle_t LEDTaskHandle = NULL; static void LEDTaskWrapper(void *pvParameters) { Sinaleiro *led = static_cast(pvParameters); @@ -143,75 +219,5 @@ class Sinaleiro { }; -std::vector Sinaleiro::ConfigurarSinaleiro(std::vector& lista, std::vector& data, const String& Mod_ID) { - std::vector 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](Sinaleiro* s) { return s->ID_Num == idNum; }); - bool jaExiste = it != lista.end(); - Sinaleiro* sensor; - switch (posicao) { - case CanMessagePosicaoDados::Config1: { - if (data.size() < 6) return status; - bool conectar = data[3] == 1; - TiposBarramentos tipoBarramento = (TiposBarramentos)data[4]; - int pino = data[5]; - if (conectar) { - if (!jaExiste) { - sensor = new Sinaleiro(Mod_ID, "sLED_" + String(idNum)); - } - else { - sensor = *it; - } - if (!sensor->Iniciado) { - sensor->ID_Num = idNum; - sensor->_pino = PinoModel(pino, tipoBarramento, _OUTPUT); - sensor->Inicializar(); - if (!jaExiste) { - lista.push_back(sensor); - PrintTela("Sinaleiro adicionado via CAN: sLED_" + String(idNum)); - } - } - status = sensor->MontarMensagemCAN(CanMessagePosicaoDados::Status); - } else { - if (jaExiste) { - sensor = *it; - sensor->Desligar(); - status = sensor->MontarMensagemCAN(CanMessagePosicaoDados::Status); - delete sensor; - lista.erase(it); - PrintTela("Sinaleiro removido via CAN: sLED_" + String(idNum)); - } - } - break; - } - } - return status; -} - -void Sinaleiro::ComandarSinaleiro(std::vector& lista, std::vector& data) { - if (data.size() < 6) return; - CanMessagePosicaoDados posicao = (CanMessagePosicaoDados)data[0]; - uint8_t idNum = data[1]; - for (auto* _led : lista) { - if (_led->ID_Num == idNum) { - switch (posicao) { - case CanMessagePosicaoDados::Command1: { - StatusLED novoStatus = (StatusLED)data[2]; - int novaQtd = data[3]; - uint16_t novoTempo = (uint16_t)data[4] * 10; - uint16_t novaPausa = (uint16_t)data[5] * 10; - _led->_Status = novoStatus; - _led->_Qtd = novaQtd; - _led->_TempoMs = novoTempo; - _led->_PausaMs = novaPausa; - break; - } - } - return; - } - } -} #endif \ No newline at end of file