1097 lines
46 KiB
C#
1097 lines
46 KiB
C#
using AgroBase.Comum;
|
|
using AgroBase.Forms;
|
|
using AgroBase.Services;
|
|
using Newtonsoft.Json;
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.IO;
|
|
using System.Linq;
|
|
using System.Text;
|
|
using System.Threading.Tasks;
|
|
using System.Windows.Forms;
|
|
using static AgroBase.Models.Enums;
|
|
using static AgroBase.Services.BLD300RService;
|
|
|
|
namespace AgroBase.Models.Modules
|
|
{
|
|
public class MovimentacaoUnificadaModel : DispositivoBaseModel
|
|
{
|
|
private bool _conectado = false;
|
|
public bool Conectado
|
|
{
|
|
get
|
|
{
|
|
return Modulos.Any(x => x.Conectado);
|
|
}
|
|
set
|
|
{
|
|
_conectado = value;
|
|
}
|
|
}
|
|
|
|
public static int _TaxaAmostragem { get; set; } = 1000;
|
|
public List<ModuloMvdModel> Modulos { get; set; } = new List<ModuloMvdModel>();
|
|
|
|
public static Direcao UltimaDirecao { get; set; } = Direcao.Parado;
|
|
public double DistanciaPercorridaOperacao { get; set; } = 0;
|
|
public int TempoEmAtividade { get; set; } = 0;
|
|
public double DistanciaPercorridaTotal { get; set; } = 0;
|
|
public double DistanciaPercorridaParcial { get; set; } = 0;
|
|
|
|
|
|
|
|
public static MovimentacaoUnificadaModel CarregarParametrosIniciais()
|
|
{
|
|
return new MovimentacaoUnificadaModel()
|
|
{
|
|
Modulos = new List<ModuloMvdModel>()
|
|
{
|
|
new ModuloMvdModel()
|
|
{
|
|
Modulo_ID = "ET",
|
|
MovMotor = MvdMotorBLDC.CarregarParametrosIniciais("ET", "0x01"),
|
|
DirMotor = MvdMotorPasso.CarregarParametrosIniciais("ET", "0x01"),
|
|
},
|
|
new ModuloMvdModel()
|
|
{
|
|
Modulo_ID = "EF",
|
|
MovMotor = MvdMotorBLDC.CarregarParametrosIniciais("EF", "0x03"),
|
|
DirMotor = MvdMotorPasso.CarregarParametrosIniciais("EF", "0x03")
|
|
},
|
|
new ModuloMvdModel()
|
|
{
|
|
Modulo_ID = "DT",
|
|
MovMotor = MvdMotorBLDC.CarregarParametrosIniciais("DT", "0x02"),
|
|
DirMotor = MvdMotorPasso.CarregarParametrosIniciais("DT", "0x02")
|
|
},
|
|
new ModuloMvdModel()
|
|
{
|
|
Modulo_ID = "DF",
|
|
MovMotor = MvdMotorBLDC.CarregarParametrosIniciais("DF", "0x04"),
|
|
DirMotor = MvdMotorPasso.CarregarParametrosIniciais("DF", "0x04")
|
|
},
|
|
},
|
|
};
|
|
}
|
|
|
|
public static void LimparDados(MovimentacaoUnificadaModel Dados)
|
|
{
|
|
Dados.DistanciaPercorridaParcial = 0;
|
|
Dados.DistanciaPercorridaTotal = 0;
|
|
Dados.DistanciaPercorridaOperacao = 0;
|
|
Dados.TempoEmAtividade = 0;
|
|
Dados.Modulos.ForEach(x =>
|
|
{
|
|
x.MovMotor.Inicializado = false;
|
|
x.MovMotor.Alarme = false;
|
|
x.MovMotor.Freio = false;
|
|
x.MovMotor.Ligado = false;
|
|
x.MovMotor.LogGrafico = new List<MvdMotorMOVSensorimanetoGrafico>();
|
|
x.MovMotor.TemperaturaDriver = 0;
|
|
x.MovMotor.Temperatura = 0;
|
|
x.MovMotor.RPM_Motor = 0;
|
|
x.MovMotor.RPM_SP = 0;
|
|
x.MovMotor.RPM_SP_C = 0;
|
|
x.MovMotor.Corrente_Max = 0;
|
|
x.MovMotor.Sentido = Sentido.Parado;
|
|
x.MovMotor.CodigoAlarme = 0;
|
|
x.MovMotor.Corrente = 0;
|
|
x.MovMotor.Tensao = 0;
|
|
x.UltimoProtocoloRecebido = DateTime.MaxValue.AddDays(-1);
|
|
|
|
x.DirMotor.Inicializado = false;
|
|
x.DirMotor.Angulo = 0;
|
|
x.DirMotor.Angulo_SP = 0;
|
|
x.DirMotor.Sentido = Sentido.Parado;
|
|
x.DirMotor.Sentido_SP = Sentido.Parado;
|
|
});
|
|
}
|
|
|
|
public Dictionary<string, List<string>> ProtocoloConfiguracao(bool Conectar)
|
|
{
|
|
var Config = Modulos.Where(x => x.MovMotor.Comandar || x.DirMotor.Comandar).ToDictionary(x => x.Modulo_ID, x => x.ProtocoloConfiguracao(Conectar).ToList());
|
|
return Config;
|
|
}
|
|
|
|
public void RecebeProtocoloSerial(string Protocolo)
|
|
{
|
|
string Mod_ID = Protocolo.Split(SerialService.SplitMessage[0])[0];
|
|
string mensagem = Protocolo.Substring(Mod_ID.Length + 1, Protocolo.Length - Mod_ID.Length - 1);
|
|
|
|
if (!string.IsNullOrEmpty(Protocolo) && mensagem[0].ToString() == SerialService.BeginSensor)
|
|
{
|
|
try
|
|
{
|
|
string[] Dados = mensagem.Replace(SerialService.BeginSensor, "").Split(SerialService.EndLine[0])[0].Split(SerialService.SplitMessage[0]);
|
|
S_Code TipoSensor = (S_Code)(int.Parse(Dados[0]));
|
|
string _IDcomponente = Dados[1];
|
|
|
|
ModuloMvdModel Modulo = Modulos.FirstOrDefault(x => x.Modulo_ID == Mod_ID);
|
|
|
|
if (Modulo == null)
|
|
{
|
|
Console.WriteLine("Módulo " + Mod_ID + " não encontrado!");
|
|
return;
|
|
}
|
|
|
|
Modulo.UltimoProtocoloRecebido = DateTime.Now;
|
|
|
|
/*if (_IDcomponente == Modulo.Modulo_ID)
|
|
{
|
|
return;
|
|
}*/
|
|
|
|
try
|
|
{
|
|
switch (TipoSensor)
|
|
{
|
|
case S_Code.sBLD:
|
|
{
|
|
Modulo.MovMotor.Inicializado = Dados[2] == "1";
|
|
double rpm = 0;
|
|
double.TryParse(Dados[3].Replace(".", ","), out rpm);
|
|
Modulo.MovMotor.RPM_Motor = rpm;
|
|
double temperatura = 0;
|
|
double.TryParse(Dados[4].Replace(".", ","), out temperatura);
|
|
temperatura = FuncoesMatematicas.CalcularTemperatura(temperatura, 0.0);
|
|
Modulo.MovMotor.Temperatura = temperatura;
|
|
double tensao = 0;
|
|
double.TryParse(Dados[5].Replace(".", ","), out tensao);
|
|
Modulo.MovMotor.Tensao = tensao;
|
|
double corrente_max = 0;
|
|
double.TryParse(Dados[6].Replace(".", ","), out corrente_max);
|
|
Modulo.MovMotor.Corrente_Max = corrente_max;
|
|
bool ligado = Dados[7] == "1";
|
|
Modulo.MovMotor.Ligado = ligado;
|
|
int sentido = 0;
|
|
int.TryParse(Dados[8], out sentido);
|
|
Modulo.MovMotor.Sentido = (Sentido)sentido;
|
|
bool freio = Dados[9] == "1";
|
|
Modulo.MovMotor.Freio = freio;
|
|
int codAlarme = 0;
|
|
int.TryParse(Dados[10], out codAlarme);
|
|
Modulo.MovMotor.CodigoAlarme = codAlarme;
|
|
double tempDriver = 0;
|
|
double.TryParse(Dados[11].Replace(".", ","), out tempDriver);
|
|
Modulo.MovMotor.TemperaturaDriver = tempDriver;
|
|
|
|
RegistrarLogMov(Modulo);
|
|
|
|
break;
|
|
}
|
|
case S_Code.sENC:
|
|
{
|
|
int _sentido = 0;
|
|
int.TryParse(Dados[2].ToString(), out _sentido);
|
|
Modulo.DirMotor.Sentido = (Sentido)_sentido;
|
|
int _angulo = 0;
|
|
int.TryParse(Dados[3].ToString(), out _angulo);
|
|
Modulo.DirMotor.Angulo = _angulo;
|
|
|
|
break;
|
|
}
|
|
case S_Code.sCFG:
|
|
{
|
|
bool Inicializado = Dados[2] == "1";
|
|
if (_IDcomponente == Modulo.MovMotor.ID)
|
|
{
|
|
Modulo.MovMotor.Inicializado = Inicializado;
|
|
}
|
|
else if (_IDcomponente == Modulo.DirMotor.ID)
|
|
{
|
|
Modulo.DirMotor.Inicializado = Inicializado;
|
|
}
|
|
|
|
break;
|
|
}
|
|
case S_Code.sMVD:
|
|
{
|
|
Modulo.MovMotor.Inicializado = Dados[2] == "1";
|
|
double rpm = 0;
|
|
double.TryParse(Dados[3].Replace(".", ","), out rpm);
|
|
Modulo.MovMotor.RPM_Motor = rpm;
|
|
double temperatura = 0;
|
|
double.TryParse(Dados[4].Replace(".", ","), out temperatura);
|
|
temperatura = FuncoesMatematicas.CalcularTemperatura(temperatura, 0.0);
|
|
Modulo.MovMotor.Temperatura = temperatura;
|
|
double tensao = 0;
|
|
double.TryParse(Dados[5].Replace(".", ","), out tensao);
|
|
Modulo.MovMotor.Tensao = tensao;
|
|
double corrente_max = 0;
|
|
double.TryParse(Dados[6].Replace(".", ","), out corrente_max);
|
|
Modulo.MovMotor.Corrente_Max = corrente_max;
|
|
bool ligado = Dados[7] == "1";
|
|
Modulo.MovMotor.Ligado = ligado;
|
|
int sentido = 0;
|
|
int.TryParse(Dados[8], out sentido);
|
|
Modulo.MovMotor.Sentido = (Sentido)sentido;
|
|
bool freio = Dados[9] == "1";
|
|
Modulo.MovMotor.Freio = freio;
|
|
int codAlarme = 0;
|
|
int.TryParse(Dados[10], out codAlarme);
|
|
Modulo.MovMotor.CodigoAlarme = codAlarme;
|
|
double tempDriver = 0;
|
|
double.TryParse(Dados[11].Replace(".", ","), out tempDriver);
|
|
Modulo.MovMotor.TemperaturaDriver = tempDriver;
|
|
Modulo.DirMotor.Inicializado = Dados[12] == "1";
|
|
int _sentido = 0;
|
|
int.TryParse(Dados[13].ToString(), out _sentido);
|
|
Modulo.DirMotor.Sentido = (Sentido)_sentido;
|
|
double _angulo = 0;
|
|
double.TryParse(Dados[14].ToString().Replace(".", ","), out _angulo);
|
|
Modulo.DirMotor.Angulo = _angulo;
|
|
|
|
/*Modulo.MovMotor.Inicializado = Dados[2] == "1";
|
|
double temperatura = 0;
|
|
double.TryParse(Dados[3].Replace(".", ","), out temperatura);
|
|
temperatura = FuncoesMatematicas.CalcularTemperatura(temperatura, 0.0);
|
|
Modulo.DirMotor.Inicializado = Dados[4] == "1";
|
|
int _sentido = 0;
|
|
int.TryParse(Dados[5].ToString(), out _sentido);
|
|
Modulo.DirMotor.Sentido = (Sentido)_sentido;
|
|
double _angulo = 0;
|
|
double.TryParse(Dados[6].ToString().Replace(".", ","), out _angulo);
|
|
Modulo.DirMotor.Angulo = _angulo;*/
|
|
|
|
RegistrarLogMov(Modulo);
|
|
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
Console.WriteLine(ex.Message);
|
|
}
|
|
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
Console.WriteLine(ex.Message);
|
|
}
|
|
|
|
}
|
|
}
|
|
|
|
public void RegistrarLogMov(ModuloMvdModel Modulo)
|
|
{
|
|
Modulo.MovMotor.LogGrafico.Add(new MvdMotorMOVSensorimanetoGrafico()
|
|
{
|
|
Momento = DateTime.Now,
|
|
Modulo_ID = Modulo.Modulo_ID,
|
|
RPM_SP = Modulo.MovMotor.RPM_SP,
|
|
RPM_Motor = Modulo.MovMotor.RPM_Motor,
|
|
RPM_Roda = Modulo.MovMotor.RPM_Roda,
|
|
Corrente = Modulo.MovMotor.Corrente,
|
|
Sentido = Modulo.MovMotor.Sentido,
|
|
Temperatura = Modulo.MovMotor.Temperatura,
|
|
TemperaturaDriver = Modulo.MovMotor.TemperaturaDriver,
|
|
Tensao = Modulo.MovMotor.Tensao,
|
|
Velocidade = Modulo.MovMotor.VelocidadeInstantanea
|
|
});
|
|
|
|
if (Modulo.MovMotor.LogGrafico.Count > 60)
|
|
{
|
|
Modulo.MovMotor.LogGrafico.RemoveAt(0);
|
|
}
|
|
|
|
frmInstancial.frmAcompanhamento.AtualizaGrafico();
|
|
}
|
|
|
|
public void DefineStatusConexao(string Mod_ID, bool conecatdo)
|
|
{
|
|
var Modulo = Modulos.FirstOrDefault(x => x.Modulo_ID == Mod_ID);
|
|
if (Modulo != null)
|
|
{
|
|
//Modulo.Conectado = conecatdo;
|
|
|
|
Variaveis.OperacaoEmAndamento.DispMvd.AcaoConexao();
|
|
}
|
|
}
|
|
|
|
public string GetTaxaAmostragem()
|
|
{
|
|
return _TaxaAmostragem.ToString("00000");
|
|
}
|
|
|
|
public void SetTaxaAmostragem(int TaxaAmostragem)
|
|
{
|
|
_TaxaAmostragem = TaxaAmostragem;
|
|
}
|
|
|
|
public bool GetStatusConexao()
|
|
{
|
|
return Conectado;
|
|
}
|
|
|
|
public void IniciarRegistrador()
|
|
{
|
|
Modulos.ForEach(x =>
|
|
{
|
|
x.MovMotor.Inicializado = false;
|
|
x.DirMotor.Inicializado = false;
|
|
});
|
|
LoopRegistrador().ConfigureAwait(false);
|
|
}
|
|
|
|
public async Task LoopRegistrador()
|
|
{
|
|
while (true)
|
|
{
|
|
await Task.Delay(_TaxaAmostragem);
|
|
|
|
if (Conectado)
|
|
{
|
|
if (UltimaDirecao != Direcao.Parado)
|
|
{
|
|
TempoEmAtividade++;
|
|
double VelocidadeMedia = Modulos.Where(x => x.Conectado).Average(x => x.MovMotor.VelocidadeInstantanea * _TaxaAmostragem / 3600);
|
|
DistanciaPercorridaTotal += 1 * VelocidadeMedia;
|
|
DistanciaPercorridaParcial += (UltimaDirecao == Direcao.Frente ? 1 : UltimaDirecao == Direcao.Tras ? -1 : 0) * VelocidadeMedia;
|
|
DistanciaPercorridaOperacao += 1 * VelocidadeMedia;
|
|
}
|
|
|
|
DateTime Agora = DateTime.Now;
|
|
if (Modulos.Any(x => x.Conectado && x.UltimoProtocoloRecebido.AddMilliseconds(_TaxaAmostragem * 20) < Agora))
|
|
{
|
|
foreach (var Modulo in Modulos.Where(x => x.Conectado && x.UltimoProtocoloRecebido.AddMilliseconds(_TaxaAmostragem * 20) < Agora))
|
|
{
|
|
Modulo.MovMotor.Inicializado = false;
|
|
Modulo.DirMotor.Inicializado = false;
|
|
}
|
|
}
|
|
|
|
/*foreach (var Modulo in Modulos.Where(x => (!x.MovMotor.Inicializado && x.MovMotor.Comandar) || (!x.DirMotor.Inicializado && x.DirMotor.Comandar)))
|
|
{
|
|
if (!Modulo.MovMotor.Inicializado && Modulo.MovMotor.Comandar)
|
|
{
|
|
string Protocolo = Modulo.MovMotor.ProtocoloConfiguracao(true);
|
|
Variaveis.OperacaoEmAndamento.DispMvd.AdicionarMensagemFila(Modulo.Modulo_ID, Protocolo, true, false);
|
|
}
|
|
if (!Modulo.DirMotor.Inicializado && Modulo.DirMotor.Comandar)
|
|
{
|
|
string Protocolo = Modulo.DirMotor.ProtocoloConfiguracao(true);
|
|
Variaveis.OperacaoEmAndamento.DispMvd.AdicionarMensagemFila(Modulo.Modulo_ID, Protocolo, true, false);
|
|
}
|
|
}*/
|
|
|
|
foreach (var Modulo in Modulos.Where(x => x.RequisitarDados && (x.MovMotor.Inicializado || x.DirMotor.Inicializado)))
|
|
{
|
|
string Protocolo = Modulo.ProtocoloAquisicaoDados();
|
|
Variaveis.OperacaoEmAndamento.DispMvd.AdicionarMensagemFila(Modulo.Modulo_ID, Protocolo, false, false);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
|
|
public async Task ExecutarMovimentoDirecionalCombinado(Direcao DirecaoAtual)
|
|
{
|
|
switch (Variaveis.OperacaoEmAndamento.Controle.TipoMovimento)
|
|
{
|
|
case TipoMovimentoDirecional.RotacionarNoEixo:
|
|
case TipoMovimentoDirecional.MovimentoLateral:
|
|
|
|
foreach (var Modulo in Modulos.Where(x => !x.DirMotor.PosicaoTipoMovimentoConcluido))
|
|
{
|
|
Modulo.DirMotor.ProtocoloComando(DirecaoAtual);
|
|
}
|
|
|
|
DateTime Agora = DateTime.Now;
|
|
bool Sucesso = Modulos.All(x => x.DirMotor.PosicaoTipoMovimentoConcluido);
|
|
while (Agora.AddMilliseconds(5000) > DateTime.Now && !Sucesso)
|
|
{
|
|
await Task.Delay(100);
|
|
Sucesso = Modulos.All(x => x.DirMotor.PosicaoTipoMovimentoConcluido);
|
|
}
|
|
|
|
if (Sucesso)
|
|
{
|
|
foreach (var Modulo in Modulos)
|
|
{
|
|
Modulo.MovMotor.ProtocoloComando(
|
|
DirecaoAtual == Direcao.Direita ? Direcao.Frente :
|
|
DirecaoAtual == Direcao.Esquerda ? Direcao.Tras :
|
|
Direcao.Parado
|
|
);
|
|
}
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
}
|
|
}
|
|
|
|
public class ModuloMvdModel
|
|
{
|
|
public string Modulo_ID { get; set; }
|
|
public bool Disponivel { get; set; }
|
|
public bool RequisitarDados { get; set; } = false;
|
|
|
|
public bool Conectado
|
|
{
|
|
get
|
|
{
|
|
return MovMotor.BLD_Inicializado || DirMotor.MKS_Inicializado;
|
|
}
|
|
}
|
|
public DateTime UltimoProtocoloRecebido { get; set; } = DateTime.MaxValue.AddDays(-1);
|
|
|
|
public MvdMotorBLDC MovMotor { get; set; }
|
|
public MvdMotorPasso DirMotor { get; set; }
|
|
|
|
public static PinoutDataModel Pinout { get; set; }
|
|
public static List<FuncoesPinout> Funcoes { get; set; } = new List<FuncoesPinout>()
|
|
{
|
|
FuncoesPinout.TX,
|
|
FuncoesPinout.RX,
|
|
FuncoesPinout.ModbusDI,
|
|
FuncoesPinout.ModbusRO,
|
|
FuncoesPinout.ModbusDE_RE
|
|
};
|
|
|
|
|
|
public List<string> ProtocoloConfiguracao(bool Conectar)
|
|
{
|
|
List<string> Config = new List<string>();
|
|
|
|
var _pinout = new PinoutDataModel()
|
|
{
|
|
ExpansorGPIO = Pinout.ExpansorGPIO,
|
|
EnderecoExpansor = Pinout.EnderecoExpansor,
|
|
Pinos = Pinout.Pinos.ToList(),
|
|
};
|
|
_pinout.Pinos = _pinout.Pinos.Where(x => x.ComponenteID == T_Code.Mvd.ToString()).ToList();
|
|
string ProtocoloMOD =
|
|
((int)F_Code.Cfg).ToString() + SerialService.SplitMessage +
|
|
Modulo_ID + SerialService.SplitParams +
|
|
(Conectar ? "1" : "0") + SerialService.SplitParams +
|
|
MovimentacaoUnificadaModel._TaxaAmostragem.ToString() + SerialService.SplitParams +
|
|
(RequisitarDados ? "1" : "0") + SerialService.SplitParams +
|
|
PinoutModel.PinoProtocolo(_pinout, Funcoes);
|
|
|
|
if (Conectar)
|
|
{
|
|
Config.Add(ProtocoloMOD);
|
|
}
|
|
|
|
if (MovMotor.Comandar)
|
|
{
|
|
Config.Add(MovMotor.ProtocoloConfiguracao(Conectar));
|
|
}
|
|
/*if (DirMotor.Comandar)
|
|
{
|
|
Config.Add(DirMotor.ProtocoloConfiguracao(Conectar));
|
|
}*/
|
|
|
|
if (!Conectar)
|
|
{
|
|
Config.Add(ProtocoloMOD);
|
|
}
|
|
|
|
Config.Add(ProtocoloVerificacao(false));
|
|
|
|
return Config;
|
|
}
|
|
|
|
public string ProtocoloVerificacao(bool Completo)
|
|
{
|
|
if (Completo)
|
|
{
|
|
string ProtocoloCheck =
|
|
SerialService.BeginLine +
|
|
0 + SerialService.SplitMessage +
|
|
Modulo_ID + SerialService.SplitMessage +
|
|
((int)F_Code.Chk).ToString() + SerialService.SplitMessage +
|
|
"check" +
|
|
SerialService.EndLine;
|
|
|
|
return ProtocoloCheck;
|
|
}
|
|
else
|
|
{
|
|
string ProtocoloCheck =
|
|
((int)F_Code.Chk).ToString() + SerialService.SplitMessage +
|
|
"check";
|
|
|
|
return ProtocoloCheck;
|
|
}
|
|
}
|
|
|
|
public string ProtocoloAquisicaoDados()
|
|
{
|
|
string ProtocoloMotor =
|
|
((int)F_Code.Req).ToString() + SerialService.SplitMessage +
|
|
Modulo_ID + SerialService.SplitParams +
|
|
"";
|
|
return ProtocoloMotor;
|
|
}
|
|
|
|
|
|
public void AtualizarSensoresMotor(SensorModel Sensor)
|
|
{
|
|
bool Computar = true;
|
|
double leitura0 = Sensor.Leituras.Count() > 0 ? Sensor.Leituras[0] : 0;
|
|
switch (Sensor.Componente)
|
|
{
|
|
case S_Code.sTMP:
|
|
MovMotor.Temperatura = leitura0;
|
|
break;
|
|
case S_Code.sCOR:
|
|
MovMotor.Corrente = Sensor.Leituras[0];
|
|
MovMotor.Tensao = Sensor.Leituras[1];
|
|
break;
|
|
default:
|
|
Computar = false;
|
|
break;
|
|
}
|
|
|
|
|
|
if (!Computar)
|
|
{
|
|
return;
|
|
}
|
|
|
|
AlarmeModel _Alarme = new AlarmeModel()
|
|
{
|
|
Tipo = Sensor.Componente,
|
|
Valor = leitura0,
|
|
Maximo = leitura0 > Sensor.LimiteMaximo,
|
|
Minimo = leitura0 < Sensor.LimiteMinimo,
|
|
ParaMaximo = false,
|
|
ParaMinimo = false
|
|
};
|
|
|
|
bool Adicionado = false;
|
|
var Alm = MovMotor.Alarmes.FirstOrDefault(x => x.Tipo == _Alarme.Tipo);
|
|
if (Alm == null)
|
|
{
|
|
Alm = _Alarme;
|
|
MovMotor.Alarmes.Add(Alm);
|
|
Adicionado = true;
|
|
}
|
|
|
|
bool AlarmeAnterior = (Alm.Maximo && Alm.ParaMaximo) || (Alm.Minimo && Alm.ParaMinimo);
|
|
bool AlarmeAtual = (_Alarme.Maximo && _Alarme.ParaMaximo) || (_Alarme.Minimo && _Alarme.ParaMinimo);
|
|
|
|
if (MovMotor.BLD_Inicializado && (!AlarmeAnterior || Adicionado) && AlarmeAtual) // Motor iniciado, Não estava em alarme anteriormente ou foi adicionado agora, alarme atual
|
|
{
|
|
//Variaveis.OperacaoEmAndamento.DispMov.AdicionarMensagemFila(Variaveis.OperacaoEmAndamento.DispMov.Dados.Modulo_ID, ProtocoloComando(Direcao.Frente, true), true, false);
|
|
}
|
|
|
|
Alm.Valor = _Alarme.Valor;
|
|
Alm.Maximo = _Alarme.Maximo;
|
|
Alm.Minimo = _Alarme.Minimo;
|
|
|
|
MovMotor.Alarme = MovMotor.Alarmes.Any(x => (x.Maximo && x.ParaMaximo) || (x.Minimo && x.ParaMinimo));
|
|
}
|
|
|
|
}
|
|
|
|
public class MvdMotorBLDC
|
|
{
|
|
public string ID { get; set; } = T_Code.Mov.ToString();
|
|
|
|
// SETs
|
|
public string Endereco { get; set; }
|
|
public bool Ligado { get; set; } = false;
|
|
public bool Freio { get; set; } = false;
|
|
public int CodigoAlarme { get; set; } = 0;
|
|
public int RPM_SP { get; set; } = 0;
|
|
public int RPM_SP_C { get; set; } = 0;
|
|
public Sentido Sentido_SP { get; set; } = Sentido.Parado;
|
|
public int NumeroPolos { get; set; } = 16;
|
|
public int Rampa { get; set; } = 0;
|
|
public int RPM_Max { get; set; } = 600;
|
|
public int Reducao { get; set; } = 27;
|
|
|
|
|
|
// GETs
|
|
public double RPM_Motor { get; set; } = 0;
|
|
public Sentido Sentido { get; set; } = Sentido.Parado;
|
|
public double Temperatura { get; set; } = 0;
|
|
public double TemperaturaDriver { get; set; } = 0;
|
|
public double Tensao { get; set; } = 0;
|
|
public double Corrente_Max { get; set; } = 0;
|
|
public double Corrente { get; set; } = 0;
|
|
public double RPM_Roda
|
|
{
|
|
get
|
|
{
|
|
return Convert.ToInt32(RPM_Motor / VariaveisEquipamento.RelacaoRPM);
|
|
}
|
|
}
|
|
public double VelocidadeInstantanea
|
|
{
|
|
get
|
|
{
|
|
return MovimentacaoModel.CalculaVelocidadeRPM(RPM_Roda);
|
|
}
|
|
}
|
|
|
|
|
|
public bool Comandar { get; set; } = false;
|
|
public bool Alarme { get; set; } = false;
|
|
public bool Inicializado { get; set; } = false;
|
|
public bool BLD_Inicializado
|
|
{
|
|
get
|
|
{
|
|
return BLD300RService.DadosLeitura.Any(x => x.Iniciado && Convert.ToByte(Endereco.Replace("0x", ""), 16) == x.Endereco);
|
|
}
|
|
}
|
|
|
|
|
|
|
|
public Dictionary<TipoMovimentoDirecional,ConfiguracaoSentidoMotor> ConfigSentidos { get; set; }
|
|
public List<FuncoesPinout> Funcoes { get; set; }
|
|
|
|
public List<MvdMotorMOVSensorimanetoGrafico> LogGrafico { get; set; } = new List<MvdMotorMOVSensorimanetoGrafico>();
|
|
public List<AlarmeModel> Alarmes { get; set; } = new List<AlarmeModel>();
|
|
|
|
|
|
public static MvdMotorBLDC CarregarParametrosIniciais(string Mod_ID, string _Endereco)
|
|
{
|
|
return new MvdMotorBLDC()
|
|
{
|
|
ID = T_Code.Mov.ToString(),
|
|
Endereco = _Endereco,
|
|
Funcoes = new List<FuncoesPinout>()
|
|
{
|
|
FuncoesPinout.TMP,
|
|
FuncoesPinout.ENA,
|
|
FuncoesPinout.DIR,
|
|
FuncoesPinout.PWM,
|
|
FuncoesPinout.BRK,
|
|
},
|
|
ConfigSentidos = new Dictionary<TipoMovimentoDirecional, ConfiguracaoSentidoMotor>()
|
|
{
|
|
{
|
|
TipoMovimentoDirecional.RodasDianteiras,
|
|
new ConfiguracaoSentidoMotor()
|
|
{
|
|
Direita = Sentido.Parado,
|
|
Esquerda = Sentido.Parado,
|
|
Frente = Mod_ID.Contains("E") ? Sentido.Antihorario : Sentido.Horario,
|
|
Tras = Mod_ID.Contains("E") ? Sentido.Horario : Sentido.Antihorario
|
|
}
|
|
},
|
|
{
|
|
TipoMovimentoDirecional.RodasTraseiras,
|
|
new ConfiguracaoSentidoMotor()
|
|
{
|
|
Direita = Sentido.Parado,
|
|
Esquerda = Sentido.Parado,
|
|
Frente = Mod_ID.Contains("E") ? Sentido.Antihorario : Sentido.Horario,
|
|
Tras = Mod_ID.Contains("E") ? Sentido.Horario : Sentido.Antihorario
|
|
}
|
|
},
|
|
{
|
|
TipoMovimentoDirecional.MovimentoDiagonal,
|
|
new ConfiguracaoSentidoMotor()
|
|
{
|
|
Direita = Sentido.Parado,
|
|
Esquerda = Sentido.Parado,
|
|
Frente = Mod_ID.Contains("E") ? Sentido.Antihorario : Sentido.Horario,
|
|
Tras = Mod_ID.Contains("E") ? Sentido.Horario : Sentido.Antihorario
|
|
}
|
|
},
|
|
{
|
|
TipoMovimentoDirecional.MovimentoArco,
|
|
new ConfiguracaoSentidoMotor()
|
|
{
|
|
Direita = Sentido.Parado,
|
|
Esquerda = Sentido.Parado,
|
|
Frente = Mod_ID.Contains("E") ? Sentido.Antihorario : Sentido.Horario,
|
|
Tras = Mod_ID.Contains("E") ? Sentido.Horario : Sentido.Antihorario
|
|
}
|
|
},
|
|
{
|
|
TipoMovimentoDirecional.RotacionarNoEixo,
|
|
new ConfiguracaoSentidoMotor()
|
|
{
|
|
Direita = Sentido.Parado,
|
|
Esquerda = Sentido.Parado,
|
|
Frente = Sentido.Antihorario,
|
|
Tras = Sentido.Horario
|
|
}
|
|
},
|
|
{
|
|
TipoMovimentoDirecional.MovimentoLateral,
|
|
new ConfiguracaoSentidoMotor()
|
|
{
|
|
Direita = Sentido.Parado,
|
|
Esquerda = Sentido.Parado,
|
|
Frente = Mod_ID.Contains("T") ? Sentido.Antihorario : Sentido.Horario,
|
|
Tras = Mod_ID.Contains("T") ? Sentido.Horario : Sentido.Antihorario
|
|
}
|
|
},
|
|
},
|
|
};
|
|
}
|
|
|
|
|
|
public string ProtocoloConfiguracao(bool Conectar)
|
|
{
|
|
var _pinout = new PinoutDataModel()
|
|
{
|
|
ExpansorGPIO = ModuloMvdModel.Pinout.ExpansorGPIO,
|
|
EnderecoExpansor = ModuloMvdModel.Pinout.EnderecoExpansor,
|
|
Pinos = ModuloMvdModel.Pinout.Pinos.ToList(),
|
|
};
|
|
_pinout.Pinos = _pinout.Pinos.Where(x => x.ComponenteID == ID).ToList();
|
|
|
|
string Protocolo =
|
|
((int)F_Code.Cfg).ToString() + SerialService.SplitMessage +
|
|
ID + SerialService.SplitParams +
|
|
(Conectar ? "1" : "0") + SerialService.SplitParams +
|
|
Endereco + SerialService.SplitParams +
|
|
NumeroPolos + SerialService.SplitParams +
|
|
Rampa + SerialService.SplitParams +
|
|
RPM_Max + SerialService.SplitParams +
|
|
PinoutModel.PinoProtocolo(_pinout, Funcoes);
|
|
|
|
return Protocolo;
|
|
}
|
|
|
|
public string ProtocoloComando(Direcao DirecaoAtual, bool CmdAlarme = false)
|
|
{
|
|
string ProtocoloMotor = "";
|
|
|
|
AcaoFreio AcaoDoFreio = AcaoFreio.SemAcao;
|
|
|
|
if (Comandar && (!Alarme || CmdAlarme))
|
|
{
|
|
if (!CmdAlarme)
|
|
{
|
|
MovimentacaoUnificadaModel.UltimaDirecao = DirecaoAtual;
|
|
}
|
|
|
|
RPM_SP_C = Variaveis.OperacaoEmAndamento.Controle.RPM_Motor;
|
|
RPM_SP =
|
|
DirecaoAtual == Direcao.Parado || CmdAlarme ? 0 :
|
|
Variaveis.OperacaoEmAndamento.Controle.RPM_Motor;
|
|
|
|
Ligado = RPM_SP > 0;
|
|
|
|
switch (DirecaoAtual)
|
|
{
|
|
case Direcao.Frente:
|
|
Sentido_SP = ConfigSentidos[Variaveis.OperacaoEmAndamento.Controle.TipoMovimento].Frente;
|
|
break;
|
|
case Direcao.Tras:
|
|
Sentido_SP = ConfigSentidos[Variaveis.OperacaoEmAndamento.Controle.TipoMovimento].Tras;
|
|
break;
|
|
}
|
|
|
|
Freio = DirecaoAtual == Direcao.EmFreio;
|
|
|
|
if (BLD_Inicializado)
|
|
{
|
|
byte Addr = Convert.ToByte(Endereco.Replace("0x", ""), 16);
|
|
var Leitura = BLD300RService.DadosLeitura.FirstOrDefault(x => x.Endereco == Addr);
|
|
DateTime Agora = DateTime.Now;
|
|
|
|
if (Leitura == null || Leitura.Direcao != Sentido_SP)
|
|
{
|
|
BLD300RService.AdicionarComandoNaFila(new BLDComandoModel()
|
|
{
|
|
Momento = Agora,
|
|
Endereco = Addr,
|
|
Parametro = BLD300RService.Parametros.First(x => x.Chave == "Direction"),
|
|
Valor = Sentido_SP == Sentido.Horario ? 1 : 0,
|
|
Get = false,
|
|
AguardaResposta = true,
|
|
TimeoutResposta = 200,
|
|
});
|
|
}
|
|
if (Ligado && (Leitura == null || Leitura.RPM_SP != RPM_SP_C))
|
|
{
|
|
BLD300RService.AdicionarComandoNaFila(new BLDComandoModel()
|
|
{
|
|
Momento = Agora.AddSeconds(1),
|
|
Endereco = Addr,
|
|
Parametro = BLD300RService.Parametros.First(x => x.Chave == "RPM_SP"),
|
|
Valor = RPM_SP,
|
|
Get = false,
|
|
AguardaResposta = true,
|
|
TimeoutResposta = 200,
|
|
});
|
|
}
|
|
if (Leitura == null || Leitura.Freado != Freio)
|
|
{
|
|
BLD300RService.AdicionarComandoNaFila(new BLDComandoModel()
|
|
{
|
|
Momento = Agora.AddSeconds(2),
|
|
Endereco = Addr,
|
|
Parametro = BLD300RService.Parametros.First(x => x.Chave == "Breaked"),
|
|
Valor = Freio ? 1 : 0,
|
|
Get = false,
|
|
AguardaResposta = true,
|
|
TimeoutResposta = 200,
|
|
});
|
|
|
|
AcaoDoFreio = Freio ? AcaoFreio.Ativar : AcaoFreio.Desativar;
|
|
}
|
|
if (Leitura == null || Leitura.Ativado != Ligado)
|
|
{
|
|
BLD300RService.AdicionarComandoNaFila(new BLDComandoModel()
|
|
{
|
|
Momento = Agora.AddSeconds(3),
|
|
Endereco = Addr,
|
|
Parametro = BLD300RService.Parametros.First(x => x.Chave == "Enabled"),
|
|
Valor = RPM_SP > 0 ? 1 : 0,
|
|
Get = false,
|
|
AguardaResposta = true,
|
|
TimeoutResposta = 200,
|
|
});
|
|
}
|
|
}
|
|
else if (Inicializado)
|
|
{
|
|
ProtocoloMotor =
|
|
((int)F_Code.Cmd).ToString() + SerialService.SplitMessage +
|
|
ID + SerialService.SplitParams +
|
|
((int)Sentido_SP).ToString() + SerialService.SplitParams +
|
|
RPM_SP.ToString() + SerialService.SplitParams +
|
|
(Freio ? "1" : "0");
|
|
}
|
|
}
|
|
|
|
if (AcaoDoFreio != AcaoFreio.SemAcao)
|
|
{
|
|
string Mod_ID = Variaveis.OperacaoEmAndamento.DispMvd.Dados.Modulos.FirstOrDefault(x => x.MovMotor.Endereco == Endereco)?.Modulo_ID ?? "";
|
|
var ServoFreio = Variaveis.OperacaoEmAndamento.DispSen.Dados.Servos.FirstOrDefault(x => x.ID.Contains(Mod_ID) && x.Inicializado && x.Controlar);
|
|
if (ServoFreio != null)
|
|
{
|
|
GeneralJoystick.EnviarComandoSensoriamento(S_Code.sFRO, ServoFreio.ID, AcaoDoFreio);
|
|
}
|
|
}
|
|
|
|
return ProtocoloMotor;
|
|
}
|
|
|
|
|
|
}
|
|
|
|
public class MvdMotorPasso
|
|
{
|
|
public string ID { get; set; } = T_Code.Dir.ToString();
|
|
|
|
// SETs
|
|
public string Endereco { get; set; }
|
|
public Sentido Sentido_SP { get; set; } = Sentido.Parado;
|
|
public double Angulo_SP { get; set; } = 0;
|
|
public double Angulo_Offset { get; set; } = 0;
|
|
|
|
// GETs
|
|
public Sentido Sentido { get; set; } = Sentido.Parado;
|
|
public double Angulo { get; set; } = 0;
|
|
|
|
|
|
public bool Comandar { get; set; } = false;
|
|
public bool Inicializado { get; set; } = false;
|
|
public bool MKS_Inicializado
|
|
{
|
|
get
|
|
{
|
|
return MKS057DService.DadosLeitura.Any(x => x.Iniciado && Convert.ToByte(Endereco.Replace("0x", ""), 16) == x.Endereco);
|
|
}
|
|
}
|
|
public bool PosicaoTipoMovimentoConcluido
|
|
{
|
|
get
|
|
{
|
|
var TipoMovimento = Variaveis.OperacaoEmAndamento.Controle.TipoMovimento;
|
|
switch (TipoMovimento)
|
|
{
|
|
case TipoMovimentoDirecional.RotacionarNoEixo:
|
|
case TipoMovimentoDirecional.MovimentoLateral:
|
|
return !Comandar || VariaveisEquipamento.AnguloMovimento[TipoMovimento] == Angulo;
|
|
default:
|
|
return !Comandar || Angulo_SP == Angulo;
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
public Dictionary<TipoMovimentoDirecional, ConfiguracaoSentidoMotor> ConfigSentidos { get; set; }
|
|
public List<FuncoesPinout> Funcoes { get; set; }
|
|
|
|
|
|
public static MvdMotorPasso CarregarParametrosIniciais(string Mod_ID, string _Endereco)
|
|
{
|
|
return new MvdMotorPasso()
|
|
{
|
|
ID = T_Code.Dir.ToString(),
|
|
Endereco = _Endereco,
|
|
Funcoes = new List<FuncoesPinout>()
|
|
{
|
|
FuncoesPinout.PUL,
|
|
FuncoesPinout.DIR,
|
|
FuncoesPinout.ENA,
|
|
FuncoesPinout.EncoderAbsoluto
|
|
},
|
|
ConfigSentidos = new Dictionary<TipoMovimentoDirecional, ConfiguracaoSentidoMotor>()
|
|
{
|
|
{
|
|
TipoMovimentoDirecional.RodasDianteiras,
|
|
new ConfiguracaoSentidoMotor()
|
|
{
|
|
Direita = Mod_ID.Contains("F") ? Sentido.Horario : Sentido.Antihorario,
|
|
Esquerda = Mod_ID.Contains("F") ? Sentido.Antihorario : Sentido.Horario,
|
|
Frente = Sentido.Parado,
|
|
Tras = Sentido.Parado
|
|
}
|
|
},
|
|
{
|
|
TipoMovimentoDirecional.RodasTraseiras,
|
|
new ConfiguracaoSentidoMotor()
|
|
{
|
|
Direita = Mod_ID.Contains("F") ? Sentido.Horario : Sentido.Antihorario,
|
|
Esquerda = Mod_ID.Contains("F") ? Sentido.Antihorario : Sentido.Horario,
|
|
Frente = Sentido.Parado,
|
|
Tras = Sentido.Parado
|
|
}
|
|
},
|
|
{
|
|
TipoMovimentoDirecional.MovimentoDiagonal,
|
|
new ConfiguracaoSentidoMotor()
|
|
{
|
|
Direita = Sentido.Horario,
|
|
Esquerda = Sentido.Antihorario,
|
|
Frente = Sentido.Parado,
|
|
Tras = Sentido.Parado
|
|
}
|
|
},
|
|
{
|
|
TipoMovimentoDirecional.MovimentoArco,
|
|
new ConfiguracaoSentidoMotor()
|
|
{
|
|
Direita = Mod_ID.Contains("F") ? Sentido.Horario : Sentido.Antihorario,
|
|
Esquerda = Mod_ID.Contains("F") ? Sentido.Antihorario : Sentido.Horario,
|
|
Frente = Sentido.Parado,
|
|
Tras = Sentido.Parado
|
|
}
|
|
},
|
|
{
|
|
TipoMovimentoDirecional.RotacionarNoEixo,
|
|
new ConfiguracaoSentidoMotor()
|
|
{
|
|
Direita = Mod_ID == "EF" || Mod_ID == "DT" ? Sentido.Horario : Sentido.Antihorario,
|
|
Esquerda = Mod_ID == "EF" || Mod_ID == "DT" ? Sentido.Antihorario : Sentido.Horario,
|
|
Frente = Sentido.Parado,
|
|
Tras = Sentido.Parado
|
|
}
|
|
},
|
|
{
|
|
TipoMovimentoDirecional.MovimentoLateral,
|
|
new ConfiguracaoSentidoMotor()
|
|
{
|
|
Direita = Mod_ID == "EF" || Mod_ID == "DT" ? Sentido.Horario : Sentido.Antihorario,
|
|
Esquerda = Mod_ID == "EF" || Mod_ID == "DT" ? Sentido.Antihorario : Sentido.Horario,
|
|
Frente = Sentido.Parado,
|
|
Tras = Sentido.Parado
|
|
}
|
|
},
|
|
}
|
|
};
|
|
}
|
|
|
|
|
|
public string ProtocoloConfiguracao(bool Conectar)
|
|
{
|
|
var _pinout = new PinoutDataModel()
|
|
{
|
|
ExpansorGPIO = ModuloMvdModel.Pinout.ExpansorGPIO,
|
|
EnderecoExpansor = ModuloMvdModel.Pinout.EnderecoExpansor,
|
|
Pinos = ModuloMvdModel.Pinout.Pinos.ToList(),
|
|
};
|
|
_pinout.Pinos = _pinout.Pinos.Where(x => x.ComponenteID == ID).ToList();
|
|
|
|
string Protocolo =
|
|
((int)F_Code.Cfg).ToString() + SerialService.SplitMessage +
|
|
ID + SerialService.SplitParams +
|
|
(Conectar ? "1" : "0") + SerialService.SplitParams +
|
|
//(Estabilizar ? "1" : "0") + SerialService.SplitParams +
|
|
Angulo_Offset.ToString() + SerialService.SplitParams +
|
|
PinoutModel.PinoProtocolo(_pinout, Funcoes);
|
|
|
|
return Protocolo;
|
|
}
|
|
|
|
public string ProtocoloComando(Direcao Direcao)
|
|
{
|
|
string ProtocoloMotor = "";
|
|
|
|
if (Comandar)
|
|
{
|
|
int AnguloTipoMovimento = VariaveisEquipamento.AnguloMovimento[Variaveis.OperacaoEmAndamento.Controle.TipoMovimento];
|
|
|
|
Angulo_SP =
|
|
(Direcao != Direcao.Esquerda && Direcao != Direcao.Direita) ? 0 :
|
|
AnguloTipoMovimento != -1 ? AnguloTipoMovimento :
|
|
Variaveis.OperacaoEmAndamento.Controle.Angulo;
|
|
|
|
Sentido_SP =
|
|
Direcao == Direcao.Direita ? ConfigSentidos[Variaveis.OperacaoEmAndamento.Controle.TipoMovimento].Direita :
|
|
Direcao == Direcao.Esquerda ? ConfigSentidos[Variaveis.OperacaoEmAndamento.Controle.TipoMovimento].Esquerda :
|
|
Sentido_SP == Sentido.Horario ? Sentido.Antihorario :
|
|
Sentido_SP == Sentido.Antihorario ? Sentido.Horario :
|
|
Sentido.Parado;
|
|
|
|
if (MKS_Inicializado)
|
|
{
|
|
int mx = Sentido_SP == Sentido.Antihorario ? -1 : 1;
|
|
|
|
int vel = Convert.ToInt32((Variaveis.OperacaoEmAndamento.Controle.VelocidadeMP / 100.0) * (MKS057DService.DadosLeitura.FirstOrDefault(x => x.Endereco == Convert.ToByte(Endereco.Replace("0x", ""), 16))?.Velocidade ?? 600));
|
|
|
|
MKS057DService.EnviarComandoControle(new MKS057ComandoModel()
|
|
{
|
|
Get = false,
|
|
Comando = Convert.ToByte("13"),
|
|
Endereco = Endereco,
|
|
Angulo = Angulo_SP * mx,
|
|
Velocidade = vel,
|
|
_Sentido = Sentido_SP,
|
|
ModoControle = ModosControle.AnguloAbsoluto,
|
|
AguardaResposta = false,
|
|
}).ConfigureAwait(false);
|
|
}
|
|
else if (Inicializado)
|
|
{
|
|
ProtocoloMotor =
|
|
((int)F_Code.Cmd).ToString() + SerialService.SplitMessage +
|
|
ID + SerialService.SplitParams +
|
|
((int)Sentido_SP).ToString() + SerialService.SplitParams +
|
|
Angulo_SP.ToString() + SerialService.SplitParams +
|
|
Variaveis.OperacaoEmAndamento.Controle.VelocidadeMP.ToString();
|
|
}
|
|
}
|
|
|
|
return ProtocoloMotor;
|
|
}
|
|
|
|
}
|
|
|
|
public class MvdMotorMOVSensorimanetoGrafico
|
|
{
|
|
public DateTime Momento { get; set; }
|
|
public string Modulo_ID { get; set; }
|
|
public int RPM_SP { get; set; }
|
|
public double RPM_Motor { get; set; }
|
|
public double RPM_Roda { get; set; }
|
|
public double Temperatura { get; set; }
|
|
public double TemperaturaDriver { get; set; }
|
|
public Sentido Sentido { get; set; }
|
|
public double Velocidade { get; set; }
|
|
public double Tensao { get; set; }
|
|
public double Corrente { get; set; }
|
|
}
|
|
|
|
|
|
|
|
}
|