verificacao do fluxo referenciamento direcional
This commit is contained in:
parent
9fbcbeb621
commit
f15103bf25
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
|
|
@ -75,14 +75,14 @@ namespace AgroBase.Models.Modules
|
|||
{
|
||||
get
|
||||
{
|
||||
return !(Leitura?.Entradas?[0] ?? true);
|
||||
return (Leitura?.Entradas?[0] ?? false);
|
||||
}
|
||||
}
|
||||
public bool IN2_Atuado
|
||||
{
|
||||
get
|
||||
{
|
||||
return !(Leitura?.Entradas?[1] ?? true);
|
||||
return (Leitura?.Entradas?[1] ?? false);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -323,10 +323,9 @@ namespace AgroBase.Models.Modules
|
|||
{
|
||||
byte Addr = EnderecoByte;
|
||||
|
||||
MKS057DCanService.AddrRef = Addr;
|
||||
MKS057DCanService.UltimoReferenciamento = DateTime.Now;
|
||||
MKS057DCanService.IniciarReferenciamento(Addr);
|
||||
|
||||
await Task.Delay(2000);
|
||||
await Task.Delay(1000);
|
||||
|
||||
//await MKS057DService.EnviarComandoControle(MKS057DService.MontarComandoSetZero(Addr));
|
||||
MKS057DCanService.SetZero(Addr);
|
||||
|
|
@ -339,7 +338,7 @@ namespace AgroBase.Models.Modules
|
|||
|
||||
double _anguloAnterior = Angulo;
|
||||
double _velocidadeAnterior = 0;
|
||||
int _angulo = 45;
|
||||
int _anguloSP = 45;
|
||||
int _offset = 0;
|
||||
int tentativas = 0;
|
||||
double _anguloEntrada = 9999;
|
||||
|
|
@ -355,130 +354,129 @@ namespace AgroBase.Models.Modules
|
|||
if (SentidoReal != Sentido.Parado)
|
||||
{
|
||||
_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
|
||||
int VelocidadeGiro = DefineVelocidadeReferenciamento(EstadoInicial, anguloMaxVisitado, anguloMinVisitado);
|
||||
|
||||
//await MKS057DService.EnviarComandoControle(MKS057DService.MontarComandoMovimento(Addr, _angulo, VelocidadeGiro, _sentido));
|
||||
MKS057DCanService.Movimento(Addr, _anguloSP, VelocidadeGiro, _sentido);
|
||||
|
||||
DateTime Inicio = DateTime.Now;
|
||||
bool inverterGiro = false;
|
||||
while (!Sucesso && !inverterGiro && Inicio.AddSeconds(120) > DateTime.Now)
|
||||
{
|
||||
// Se o angulo ja foi visitado, entao pode se mover mais rapido, senao, se move mais devagar
|
||||
int VelocidadeGiro = DefineVelocidadeReferenciamento(EstadoInicial, anguloMaxVisitado, anguloMinVisitado);
|
||||
bool _ref = IN1_Atuado;
|
||||
|
||||
//await MKS057DService.EnviarComandoControle(MKS057DService.MontarComandoMovimento(Addr, _angulo, VelocidadeGiro, _sentido));
|
||||
MKS057DCanService.Movimento(Addr, _angulo, VelocidadeGiro, _sentido);
|
||||
|
||||
DateTime Inicio = DateTime.Now;
|
||||
bool inverterGiro = false;
|
||||
while (!Sucesso && !inverterGiro && Inicio.AddSeconds(120) > DateTime.Now && MKS057DCanService.Referenciando)
|
||||
// Comecou com o sensor ja atuado
|
||||
if (EstadoInicial)
|
||||
{
|
||||
bool _ref = IN1_Atuado;
|
||||
|
||||
// Comecou com o sensor ja atuado
|
||||
if (EstadoInicial)
|
||||
// Quando houver alteracao na leitura do sensor
|
||||
if (_ref != EstadoAnterior)
|
||||
{
|
||||
// Quando houver alteracao na leitura do sensor
|
||||
if (_ref != EstadoAnterior)
|
||||
// Se estava atuado e agora nao esta mais, e ainda nao definiu o angulo de entrada
|
||||
if (!_ref && _anguloEntrada == 9999)
|
||||
{
|
||||
// Se estava atuado e agora nao esta mais, e ainda nao definiu o angulo de entrada
|
||||
if (!_ref && _anguloEntrada == 9999)
|
||||
{
|
||||
_anguloEntrada = Angulo;
|
||||
inverterGiro = true;
|
||||
}
|
||||
// Se estava atuado e agora nao esta mais, e ja definiu o angulo de entrada, e ainda nao definiu o angulo de saida
|
||||
else if (!_ref && _anguloEntrada != 9999 && _anguloSaida == 9999)
|
||||
{
|
||||
_anguloSaida = Angulo;
|
||||
}
|
||||
_anguloEntrada = Angulo;
|
||||
inverterGiro = true;
|
||||
}
|
||||
// Se estava atuado e agora nao esta mais, e ja definiu o angulo de entrada, e ainda nao definiu o angulo de saida
|
||||
else if (!_ref && _anguloEntrada != 9999 && _anguloSaida == 9999)
|
||||
{
|
||||
_anguloSaida = Angulo;
|
||||
}
|
||||
}
|
||||
// Comecou com o sensor nao atuado
|
||||
else
|
||||
}
|
||||
// Comecou com o sensor nao atuado
|
||||
else
|
||||
{
|
||||
// Quando houver alteracao na leitura do sensor
|
||||
if (_ref != EstadoAnterior)
|
||||
{
|
||||
// Quando houver alteracao na leitura do sensor
|
||||
if (_ref != EstadoAnterior)
|
||||
// Se nao estava atuado e agora esta, e ainda nao definiu o angulo de entrada
|
||||
if (_ref && _anguloEntrada == 9999)
|
||||
{
|
||||
// Se nao estava atuado e agora esta, e ainda nao definiu o angulo de entrada
|
||||
if (_ref && _anguloEntrada == 9999)
|
||||
{
|
||||
_anguloEntrada = Angulo;
|
||||
}
|
||||
// Se estava atuado e agora nao esta mais, e ja definiu o angulo de entrada, e ainda nao definiu o angulo de saida
|
||||
else if (!_ref && _anguloEntrada != 9999 && _anguloSaida == 9999)
|
||||
{
|
||||
_anguloSaida = Angulo;
|
||||
inverterGiro = true;
|
||||
}
|
||||
_anguloEntrada = Angulo;
|
||||
}
|
||||
// Se estava atuado e agora nao esta mais, e ja definiu o angulo de entrada, e ainda nao definiu o angulo de saida
|
||||
else if (!_ref && _anguloEntrada != 9999 && _anguloSaida == 9999)
|
||||
{
|
||||
_anguloSaida = Angulo;
|
||||
inverterGiro = true;
|
||||
}
|
||||
}
|
||||
|
||||
// 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)
|
||||
{
|
||||
inverterGiro = (_angulo > 0 && Angulo >= (_angulo - 0.5)) || (_angulo < 0 && Angulo <= (_angulo + 0.5));
|
||||
}
|
||||
|
||||
Sucesso = _anguloEntrada != 9999 && _anguloSaida != 9999;
|
||||
|
||||
EstadoAnterior = _ref;
|
||||
|
||||
//Console.WriteLine($"Ref {Addr}: Angulo SP = {_angulo}, Velocidade: {VelocidadeGiro}, Angulo Atual = {Angulo}, Sentido SP = {_sentido.ToString()}, Passou = {inverterGiro}, Sucesso = {Sucesso}, Entrada: {_ref}");
|
||||
|
||||
if (Sucesso)
|
||||
{
|
||||
//await MKS057DService.EnviarComandoControle(MKS057DService.MontarComandoParada(Addr));
|
||||
MKS057DCanService.Parar(Addr);
|
||||
await Task.Delay(1500);
|
||||
|
||||
int _centro = Convert.ToInt32((_anguloEntrada + _anguloSaida) / 2.0);
|
||||
|
||||
//await MKS057DService.EnviarComandoControle(MKS057DService.MontarComandoMovimento(Addr, _centro, 36, Sentido.Horario));
|
||||
MKS057DCanService.Movimento(Addr, _centro, 36, Sentido.Horario);
|
||||
await Task.Delay(2500);
|
||||
//await MKS057DService.EnviarComandoControle(MKS057DService.MontarComandoSetZero(Addr));
|
||||
MKS057DCanService.SetZero(Addr);
|
||||
await Task.Delay(1000);
|
||||
break;
|
||||
}
|
||||
|
||||
await Task.Delay(200); // Verificações rápidas.
|
||||
|
||||
VelocidadeGiro = DefineVelocidadeReferenciamento(EstadoInicial, anguloMaxVisitado, anguloMinVisitado);
|
||||
|
||||
if (
|
||||
(_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
|
||||
)
|
||||
{
|
||||
//await MKS057DService.EnviarComandoControle(MKS057DService.MontarComandoMovimento(Addr, _angulo, VelocidadeGiro, _sentido));
|
||||
MKS057DCanService.Movimento(Addr, _angulo, VelocidadeGiro, _sentido);
|
||||
}
|
||||
|
||||
_anguloAnterior = Angulo;
|
||||
_velocidadeAnterior = VelocidadeGiro;
|
||||
if (Angulo > anguloMaxVisitado)
|
||||
{
|
||||
anguloMaxVisitado = Angulo;
|
||||
}
|
||||
else if (Angulo < anguloMinVisitado)
|
||||
{
|
||||
anguloMinVisitado = Angulo;
|
||||
}
|
||||
}
|
||||
|
||||
if (!Sucesso && inverterGiro)
|
||||
// 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)
|
||||
{
|
||||
inverterGiro = (_anguloSP > 0 && Angulo >= (_anguloSP - 0.5)) || (_anguloSP < 0 && Angulo <= (_anguloSP + 0.5));
|
||||
}
|
||||
|
||||
Sucesso = _anguloEntrada != 9999 && _anguloSaida != 9999;
|
||||
|
||||
EstadoAnterior = _ref;
|
||||
|
||||
//Console.WriteLine($"Ref {Addr}: Angulo SP = {_angulo}, Velocidade: {VelocidadeGiro}, Angulo Atual = {Angulo}, Sentido SP = {_sentido.ToString()}, Passou = {inverterGiro}, Sucesso = {Sucesso}, Entrada: {_ref}");
|
||||
|
||||
if (Sucesso)
|
||||
{
|
||||
//await MKS057DService.EnviarComandoControle(MKS057DService.MontarComandoParada(Addr));
|
||||
MKS057DCanService.Parar(Addr);
|
||||
await Task.Delay(500);
|
||||
|
||||
//MKS057DCanService.Parar(Addr);
|
||||
//await Task.Delay(1500);
|
||||
|
||||
_sentido = _sentido == Sentido.Horario ? Sentido.Antihorario : Sentido.Horario;
|
||||
_offset = Math.Min(100, _offset + 25); // Limita o offset máximo para evitar valores extremos.
|
||||
int mx = _sentido == Sentido.Antihorario ? -1 : 1;
|
||||
_angulo = (Math.Abs(_angulo) + _offset) * mx;
|
||||
tentativas++;
|
||||
int _centro = Convert.ToInt32((_anguloEntrada + _anguloSaida) / 2.0);
|
||||
|
||||
//await MKS057DService.EnviarComandoControle(MKS057DService.MontarComandoMovimento(Addr, _centro, 36, Sentido.Horario));
|
||||
MKS057DCanService.Movimento(Addr, _centro, 36, Sentido.Horario);
|
||||
await Task.Delay(2500);
|
||||
//await MKS057DService.EnviarComandoControle(MKS057DService.MontarComandoSetZero(Addr));
|
||||
MKS057DCanService.SetZero(Addr);
|
||||
await Task.Delay(1000);
|
||||
break;
|
||||
}
|
||||
|
||||
//await Task.Delay(200); // Verificações rápidas.
|
||||
|
||||
VelocidadeGiro = DefineVelocidadeReferenciamento(EstadoInicial, anguloMaxVisitado, anguloMinVisitado);
|
||||
|
||||
if (
|
||||
//(_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
|
||||
)
|
||||
{
|
||||
//await MKS057DService.EnviarComandoControle(MKS057DService.MontarComandoMovimento(Addr, _angulo, VelocidadeGiro, _sentido));
|
||||
MKS057DCanService.Movimento(Addr, _anguloSP, VelocidadeGiro, _sentido);
|
||||
}
|
||||
|
||||
_anguloAnterior = Angulo;
|
||||
_velocidadeAnterior = VelocidadeGiro;
|
||||
if (Angulo > anguloMaxVisitado)
|
||||
{
|
||||
anguloMaxVisitado = Angulo;
|
||||
}
|
||||
else if (Angulo < anguloMinVisitado)
|
||||
{
|
||||
anguloMinVisitado = Angulo;
|
||||
}
|
||||
}
|
||||
|
||||
if (!Sucesso && inverterGiro)
|
||||
{
|
||||
//await MKS057DService.EnviarComandoControle(MKS057DService.MontarComandoParada(Addr));
|
||||
|
||||
//MKS057DCanService.Parar(Addr);
|
||||
//await Task.Delay(500);
|
||||
|
||||
_sentido = _sentido == Sentido.Horario ? Sentido.Antihorario : Sentido.Horario;
|
||||
_offset = Math.Min(100, _offset + 25); // Limita o offset máximo para evitar valores extremos.
|
||||
int mx = _sentido == Sentido.Antihorario ? -1 : 1;
|
||||
_anguloSP = (Math.Abs(_anguloSP) + _offset) * mx;
|
||||
tentativas++;
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -488,8 +486,7 @@ namespace AgroBase.Models.Modules
|
|||
MKS057DCanService.Parar(Addr);
|
||||
}
|
||||
|
||||
MKS057DCanService.AddrRef = 0x00;
|
||||
MKS057DCanService.UltimoReferenciamento = DateTime.Now;
|
||||
MKS057DCanService.FinalizarReferenciamento();
|
||||
}
|
||||
|
||||
private int DefineVelocidadeReferenciamento(bool EstadoInicial, double anguloMaxVisitado, double anguloMinVisitado)
|
||||
|
|
|
|||
|
|
@ -364,7 +364,6 @@ namespace AgroBase.Models.Modules
|
|||
}
|
||||
}
|
||||
|
||||
MKS057DCanService.TaxaAmostragem = MKS057DCanService.Referenciando ? 200 : 500;
|
||||
tmrRegistradorDIR.SetInterval(MKS057DCanService.TaxaAmostragem);
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -32,7 +32,13 @@ namespace AgroBase.Services
|
|||
}
|
||||
}
|
||||
public static DateTime UltimoReferenciamento = DateTime.MinValue;
|
||||
public static int TaxaAmostragem { get; set; } = 500;
|
||||
public static int TaxaAmostragem
|
||||
{
|
||||
get
|
||||
{
|
||||
return Referenciando ? 200 : 1000;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public MKS057DCanService()
|
||||
|
|
@ -133,6 +139,18 @@ namespace AgroBase.Services
|
|||
return val;
|
||||
}
|
||||
|
||||
public static void IniciarReferenciamento(byte Addr)
|
||||
{
|
||||
AddrRef = Addr;
|
||||
UltimoReferenciamento = DateTime.Now;
|
||||
}
|
||||
|
||||
public static void FinalizarReferenciamento()
|
||||
{
|
||||
AddrRef = 0x00;
|
||||
UltimoReferenciamento = DateTime.Now;
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region COMANDOS
|
||||
|
|
|
|||
Loading…
Reference in New Issue