From ca86e1b1e2696e1ff20877594399ad1535e91f99 Mon Sep 17 00:00:00 2001 From: Diego Freitas Date: Tue, 17 Mar 2026 08:43:00 -0300 Subject: [PATCH] Aprimorado controle manual no rover --- AgroBase/AgroBase/Models/GeneralJoystick.cs | 88 ++++++++++++++++++++- AgroBase/AgroBase/Services/SerialService.cs | 20 +++-- 2 files changed, 98 insertions(+), 10 deletions(-) diff --git a/AgroBase/AgroBase/Models/GeneralJoystick.cs b/AgroBase/AgroBase/Models/GeneralJoystick.cs index a2948543f..61d287761 100644 --- a/AgroBase/AgroBase/Models/GeneralJoystick.cs +++ b/AgroBase/AgroBase/Models/GeneralJoystick.cs @@ -515,7 +515,8 @@ namespace AgroBase .ToList(); botoes_pressionados_g.ForEach(botao => { - Console.WriteLine($"Botao pressionado: {botao.botao}, Valor: {string.Join(", ", botao.dados.Select(y => y.valor))}"); + EnviarComandoMotorAnalogico(botao.botao, botao.dados?.LastOrDefault()?.valor ?? 0, false); + //Console.WriteLine($"Botao pressionado: {botao.botao}, Valor: {string.Join(", ", botao.dados.Select(y => y.valor))}"); BotoesJoyPressionados.Add(botao.botao); var Comando = DeParaComandos.FirstOrDefault(x => x.botaoJoy == botao.botao); if (Comando != null) @@ -525,7 +526,8 @@ namespace AgroBase }); _botoesSoltos.ForEach(botao => { - Console.WriteLine($"Botao solto: {botao}"); + EnviarComandoMotorAnalogico(botao, 0, true); + //Console.WriteLine($"Botao solto: {botao}"); BotoesJoyPressionados.Remove(botao); var Comando = DeParaComandos.FirstOrDefault(x => x.botaoJoy == botao); if (Comando != null && Comando.PararAoSoltar) @@ -535,6 +537,86 @@ namespace AgroBase }); } + private static void EnviarComandoMotorAnalogico(BotoesJoystick botao, double valor, bool solto) + { + T_Code dispositivo = T_Code.Vzo; + double percent = 0; + double centro = 32767; + double deadzone = 3000; + double maximo = 65535.0; + + switch (botao) + { + case BotoesJoystick.AEEsquerda: + dispositivo = T_Code.Dir; + if (valor < centro - deadzone) + percent = (centro - valor) / centro; + else + percent = 0; + break; + + case BotoesJoystick.AEDireita: + dispositivo = T_Code.Dir; + if (valor > centro + deadzone) + percent = (valor - centro) / (maximo - centro); + else + percent = 0; + break; + + case BotoesJoystick.AECima: + if (valor < centro - deadzone) + percent = (centro - valor) / centro; + else + percent = 0; + break; + + case BotoesJoystick.AEBaixo: + if (valor > centro + deadzone) + percent = (valor - centro) / (maximo - centro); + else + percent = 0; + break; + + case BotoesJoystick.L2A: + dispositivo = T_Code.Mov; + percent = valor / maximo; + break; + + case BotoesJoystick.R2A: + dispositivo = T_Code.Mov; + percent = valor / maximo; + break; + + case BotoesJoystick.Xis: + case BotoesJoystick.Quadrado: + dispositivo = T_Code.Mov; + percent = 1.0; + break + ; + case BotoesJoystick.SEsquerda: + case BotoesJoystick.SDireita: + dispositivo = T_Code.Dir; + percent = 1.0; + break; + } + + percent = FuncoesMatematicas.Clamp(percent, 0, 100); + + Console.WriteLine($"botao: {botao}, valor: {valor}, percentual: {percent * 100.0:F2}%"); + + var pControle = Variaveis.OperacaoEmAndamento.Parametros.Controle; + var _Controle = Variaveis.OperacaoEmAndamento.Controle; + switch (dispositivo) + { + case T_Code.Dir: + _Controle.Angulo = solto ? 0 : pControle.DirAnguloMaximo * percent; + break; + case T_Code.Mov: + _Controle.PercentualVelocidadeSP = solto ? 0 : pControle.MovVelocidadeSErvasPercent * percent; + break; + } + } + public static void EnviaComandoMotor(Keys Tecla, T_Code Disp = T_Code.Vzo, string ID = null, bool ForcarComando = false) { // Verifica se deve interromper o fluxo @@ -643,7 +725,7 @@ namespace AgroBase Pressionado = true; } // Controle dos Bicos Pulverizadores (ATU) - else if ((Comando.Comando == Keys.D1 || Comando.Comando == Keys.D2 || Comando.Comando == Keys.D3 || Comando.Comando == Keys.D4) && Variaveis.OperacaoEmAndamento.DispAtu.Dados.BicosPulverizadores.Any(x => x.ID == Comando.IDcontrolar && !x.ComandoAtuar)) + else if ((Comando.Comando == Keys.D1 || Comando.Comando == Keys.D2 || Comando.Comando == Keys.D3 || Comando.Comando == Keys.D4) && (Variaveis.OperacaoEmAndamento.DispAtu?.Dados?.BicosPulverizadores?.Any(x => x?.ID == Comando.IDcontrolar && !(x?.ComandoAtuar ?? false)) ?? false)) { Comando.Dispositivo = T_Code.Atu; Pressionado = true; diff --git a/AgroBase/AgroBase/Services/SerialService.cs b/AgroBase/AgroBase/Services/SerialService.cs index 54a195bf5..e56456bf7 100644 --- a/AgroBase/AgroBase/Services/SerialService.cs +++ b/AgroBase/AgroBase/Services/SerialService.cs @@ -519,14 +519,20 @@ namespace AgroBase.Services private static async Task ProcurarDispositivoGPS(SerialPort Porta) { + SerialPort _porta = new SerialPort(Porta.PortName, 115200); try { - Porta.BaudRate = 115200; - Porta.Open(); + _porta.ReadTimeout = 500; + _porta.WriteTimeout = 500; + _porta.Handshake = Handshake.None; + _porta.DtrEnable = false; + _porta.RtsEnable = false; + + _porta.Open(); await Task.Delay(100); var nmea = $"gngga com3 1\r\n"; - Porta.Write(Encoding.ASCII.GetBytes(nmea), 0, nmea.Length); - AtualizarConsole($"{Porta.PortName} - Procurando dispositivo GPS"); + _porta.Write(Encoding.ASCII.GetBytes(nmea), 0, nmea.Length); + AtualizarConsole($"{_porta.PortName} - Procurando dispositivo GPS"); await Task.Delay(1000); string Recebido = ""; @@ -534,7 +540,7 @@ namespace AgroBase.Services if (string.IsNullOrEmpty(Recebido)) { //Recebido = Porta.ReadLine(); - byte[] buffer = LerDadosDaPortaSerial(Porta, 15); + byte[] buffer = LerDadosDaPortaSerial(_porta, 15); Recebido = Encoding.ASCII.GetString(buffer); } @@ -543,7 +549,7 @@ namespace AgroBase.Services (Recebido.Contains("$GNGGA") || Recebido.Contains("$GPVTG") || Recebido.Contains("$GPGSV") || Recebido.Contains("$GNTHS")) ) { - GPSService.AtualizarPortaCOM(Porta); + GPSService.AtualizarPortaCOM(_porta); AtualizarConsole("Dispositivo GPS encontrado"); return true; } @@ -555,7 +561,7 @@ namespace AgroBase.Services } finally { - Porta.Close(); + _porta.Close(); } }