verificacao do fluxo referenciamento direcional

This commit is contained in:
Diego Freitas 2025-04-07 07:29:41 -03:00
parent 9fbcbeb621
commit f15103bf25
9 changed files with 128 additions and 114 deletions

Binary file not shown.

View File

@ -75,14 +75,14 @@ namespace AgroBase.Models.Modules
{ {
get get
{ {
return !(Leitura?.Entradas?[0] ?? true); return (Leitura?.Entradas?[0] ?? false);
} }
} }
public bool IN2_Atuado public bool IN2_Atuado
{ {
get get
{ {
return !(Leitura?.Entradas?[1] ?? true); return (Leitura?.Entradas?[1] ?? false);
} }
} }
@ -323,10 +323,9 @@ namespace AgroBase.Models.Modules
{ {
byte Addr = EnderecoByte; byte Addr = EnderecoByte;
MKS057DCanService.AddrRef = Addr; MKS057DCanService.IniciarReferenciamento(Addr);
MKS057DCanService.UltimoReferenciamento = DateTime.Now;
await Task.Delay(2000); await Task.Delay(1000);
//await MKS057DService.EnviarComandoControle(MKS057DService.MontarComandoSetZero(Addr)); //await MKS057DService.EnviarComandoControle(MKS057DService.MontarComandoSetZero(Addr));
MKS057DCanService.SetZero(Addr); MKS057DCanService.SetZero(Addr);
@ -339,7 +338,7 @@ namespace AgroBase.Models.Modules
double _anguloAnterior = Angulo; double _anguloAnterior = Angulo;
double _velocidadeAnterior = 0; double _velocidadeAnterior = 0;
int _angulo = 45; int _anguloSP = 45;
int _offset = 0; int _offset = 0;
int tentativas = 0; int tentativas = 0;
double _anguloEntrada = 9999; double _anguloEntrada = 9999;
@ -355,22 +354,20 @@ namespace AgroBase.Models.Modules
if (SentidoReal != Sentido.Parado) if (SentidoReal != Sentido.Parado)
{ {
_sentido = SentidoReal == Sentido.Horario ? Sentido.Antihorario : Sentido.Horario; _sentido = SentidoReal == Sentido.Horario ? Sentido.Antihorario : Sentido.Horario;
_angulo *= _sentido == Sentido.Antihorario ? -1 : 1; _anguloSP *= _sentido == Sentido.Antihorario ? -1 : 1;
} }
if (!Sucesso) while (!Sucesso && tentativas < MaxTentativas && TimeoutGeral > DateTime.Now)
{
while (!Sucesso && tentativas < MaxTentativas && DateTime.Now < TimeoutGeral && MKS057DCanService.Referenciando)
{ {
// Se o angulo ja foi visitado, entao pode se mover mais rapido, senao, se move mais devagar // Se o angulo ja foi visitado, entao pode se mover mais rapido, senao, se move mais devagar
int VelocidadeGiro = DefineVelocidadeReferenciamento(EstadoInicial, anguloMaxVisitado, anguloMinVisitado); int VelocidadeGiro = DefineVelocidadeReferenciamento(EstadoInicial, anguloMaxVisitado, anguloMinVisitado);
//await MKS057DService.EnviarComandoControle(MKS057DService.MontarComandoMovimento(Addr, _angulo, VelocidadeGiro, _sentido)); //await MKS057DService.EnviarComandoControle(MKS057DService.MontarComandoMovimento(Addr, _angulo, VelocidadeGiro, _sentido));
MKS057DCanService.Movimento(Addr, _angulo, VelocidadeGiro, _sentido); MKS057DCanService.Movimento(Addr, _anguloSP, VelocidadeGiro, _sentido);
DateTime Inicio = DateTime.Now; DateTime Inicio = DateTime.Now;
bool inverterGiro = false; bool inverterGiro = false;
while (!Sucesso && !inverterGiro && Inicio.AddSeconds(120) > DateTime.Now && MKS057DCanService.Referenciando) while (!Sucesso && !inverterGiro && Inicio.AddSeconds(120) > DateTime.Now)
{ {
bool _ref = IN1_Atuado; bool _ref = IN1_Atuado;
@ -416,7 +413,7 @@ namespace AgroBase.Models.Modules
// Realiza a verificacao de necessidade de reverter a rotacao do motor apenas se ainda nao encontrou o angulo de entrada do sensor // Realiza a verificacao de necessidade de reverter a rotacao do motor apenas se ainda nao encontrou o angulo de entrada do sensor
if (_anguloEntrada == 9999) if (_anguloEntrada == 9999)
{ {
inverterGiro = (_angulo > 0 && Angulo >= (_angulo - 0.5)) || (_angulo < 0 && Angulo <= (_angulo + 0.5)); inverterGiro = (_anguloSP > 0 && Angulo >= (_anguloSP - 0.5)) || (_anguloSP < 0 && Angulo <= (_anguloSP + 0.5));
} }
Sucesso = _anguloEntrada != 9999 && _anguloSaida != 9999; Sucesso = _anguloEntrada != 9999 && _anguloSaida != 9999;
@ -428,8 +425,9 @@ namespace AgroBase.Models.Modules
if (Sucesso) if (Sucesso)
{ {
//await MKS057DService.EnviarComandoControle(MKS057DService.MontarComandoParada(Addr)); //await MKS057DService.EnviarComandoControle(MKS057DService.MontarComandoParada(Addr));
MKS057DCanService.Parar(Addr);
await Task.Delay(1500); //MKS057DCanService.Parar(Addr);
//await Task.Delay(1500);
int _centro = Convert.ToInt32((_anguloEntrada + _anguloSaida) / 2.0); int _centro = Convert.ToInt32((_anguloEntrada + _anguloSaida) / 2.0);
@ -442,17 +440,17 @@ namespace AgroBase.Models.Modules
break; break;
} }
await Task.Delay(200); // Verificações rápidas. //await Task.Delay(200); // Verificações rápidas.
VelocidadeGiro = DefineVelocidadeReferenciamento(EstadoInicial, anguloMaxVisitado, anguloMinVisitado); VelocidadeGiro = DefineVelocidadeReferenciamento(EstadoInicial, anguloMaxVisitado, anguloMinVisitado);
if ( if (
(_anguloAnterior == Angulo && RPM == 0) || // Verifica se o angulo nao foi alterado desde a ultima leitura, e o motor nao esta em movimento //(_anguloAnterior == Angulo && RPM == 0) || // Verifica se o angulo nao foi alterado desde a ultima leitura, e o motor nao esta em movimento
(_velocidadeAnterior != VelocidadeGiro) // Verifica se mudou a velocidade de giro do motor de acordo com o angulo atual (_velocidadeAnterior != VelocidadeGiro) // Verifica se mudou a velocidade de giro do motor de acordo com o angulo atual
) )
{ {
//await MKS057DService.EnviarComandoControle(MKS057DService.MontarComandoMovimento(Addr, _angulo, VelocidadeGiro, _sentido)); //await MKS057DService.EnviarComandoControle(MKS057DService.MontarComandoMovimento(Addr, _angulo, VelocidadeGiro, _sentido));
MKS057DCanService.Movimento(Addr, _angulo, VelocidadeGiro, _sentido); MKS057DCanService.Movimento(Addr, _anguloSP, VelocidadeGiro, _sentido);
} }
_anguloAnterior = Angulo; _anguloAnterior = Angulo;
@ -470,17 +468,17 @@ namespace AgroBase.Models.Modules
if (!Sucesso && inverterGiro) if (!Sucesso && inverterGiro)
{ {
//await MKS057DService.EnviarComandoControle(MKS057DService.MontarComandoParada(Addr)); //await MKS057DService.EnviarComandoControle(MKS057DService.MontarComandoParada(Addr));
MKS057DCanService.Parar(Addr);
await Task.Delay(500); //MKS057DCanService.Parar(Addr);
//await Task.Delay(500);
_sentido = _sentido == Sentido.Horario ? Sentido.Antihorario : Sentido.Horario; _sentido = _sentido == Sentido.Horario ? Sentido.Antihorario : Sentido.Horario;
_offset = Math.Min(100, _offset + 25); // Limita o offset máximo para evitar valores extremos. _offset = Math.Min(100, _offset + 25); // Limita o offset máximo para evitar valores extremos.
int mx = _sentido == Sentido.Antihorario ? -1 : 1; int mx = _sentido == Sentido.Antihorario ? -1 : 1;
_angulo = (Math.Abs(_angulo) + _offset) * mx; _anguloSP = (Math.Abs(_anguloSP) + _offset) * mx;
tentativas++; tentativas++;
} }
} }
}
if (!Sucesso) if (!Sucesso)
{ {
@ -488,8 +486,7 @@ namespace AgroBase.Models.Modules
MKS057DCanService.Parar(Addr); MKS057DCanService.Parar(Addr);
} }
MKS057DCanService.AddrRef = 0x00; MKS057DCanService.FinalizarReferenciamento();
MKS057DCanService.UltimoReferenciamento = DateTime.Now;
} }
private int DefineVelocidadeReferenciamento(bool EstadoInicial, double anguloMaxVisitado, double anguloMinVisitado) private int DefineVelocidadeReferenciamento(bool EstadoInicial, double anguloMaxVisitado, double anguloMinVisitado)

View File

@ -364,7 +364,6 @@ namespace AgroBase.Models.Modules
} }
} }
MKS057DCanService.TaxaAmostragem = MKS057DCanService.Referenciando ? 200 : 500;
tmrRegistradorDIR.SetInterval(MKS057DCanService.TaxaAmostragem); tmrRegistradorDIR.SetInterval(MKS057DCanService.TaxaAmostragem);
} }

View File

@ -32,7 +32,13 @@ namespace AgroBase.Services
} }
} }
public static DateTime UltimoReferenciamento = DateTime.MinValue; public static DateTime UltimoReferenciamento = DateTime.MinValue;
public static int TaxaAmostragem { get; set; } = 500; public static int TaxaAmostragem
{
get
{
return Referenciando ? 200 : 1000;
}
}
public MKS057DCanService() public MKS057DCanService()
@ -133,6 +139,18 @@ namespace AgroBase.Services
return val; return val;
} }
public static void IniciarReferenciamento(byte Addr)
{
AddrRef = Addr;
UltimoReferenciamento = DateTime.Now;
}
public static void FinalizarReferenciamento()
{
AddrRef = 0x00;
UltimoReferenciamento = DateTime.Now;
}
#endregion #endregion
#region COMANDOS #region COMANDOS