418 lines
18 KiB
C#
418 lines
18 KiB
C#
using AgroBase.Forms;
|
|
using AgroBase.Forms.Direcional;
|
|
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.Enuns;
|
|
|
|
namespace AgroBase.Models.Modules
|
|
{
|
|
public class DirecionalModel : DispositivoBaseModel
|
|
{
|
|
public static int _TaxaAmostragem { get; set; } = 500;
|
|
public bool Conectado { get; set; } = false;
|
|
public bool AA_Ref000 { get; set; } = false;
|
|
public bool AA_Ref090 { get; set; } = false;
|
|
public bool AA_Ref270 { get; set; } = false;
|
|
public static int LogsManter { get; set; } = 60;
|
|
public static List<PinoutModel> Pinout { get; set; }
|
|
public static List<FuncoesPinout> Funcoes { get; set; } = new List<FuncoesPinout>() { FuncoesPinout.Geral };
|
|
public List<DirSensoriamentoMotor> Motores { get; set; }
|
|
|
|
|
|
public static DirecionalModel CarregarParametrosIniciais()
|
|
{
|
|
return new DirecionalModel()
|
|
{
|
|
Motores = new List<DirSensoriamentoMotor>()
|
|
{
|
|
new DirSensoriamentoMotor() {
|
|
Canal = 0,
|
|
ID = "ET",
|
|
Descricao = "Esquerdo Trás",
|
|
ConfigSentidos = new ConfiguracaoSentidoMotor() {
|
|
Direita = Sentido.Antihorario,
|
|
Esquerda = Sentido.Horario,
|
|
Frente = Sentido.Parado,
|
|
Tras = Sentido.Parado
|
|
},
|
|
Funcoes = new List<FuncoesPinout>() {
|
|
FuncoesPinout.PUL,
|
|
FuncoesPinout.DIR,
|
|
FuncoesPinout.ENA,
|
|
FuncoesPinout.EncoderA,
|
|
FuncoesPinout.EncoderB,
|
|
FuncoesPinout.REF000,
|
|
FuncoesPinout.REF090,
|
|
FuncoesPinout.REF270,
|
|
}
|
|
},
|
|
new DirSensoriamentoMotor() {
|
|
Canal = 1,
|
|
ID = "EF",
|
|
Descricao = "Esquerdo Frente",
|
|
ConfigSentidos = new ConfiguracaoSentidoMotor() {
|
|
Direita = Sentido.Antihorario,
|
|
Esquerda = Sentido.Horario,
|
|
Frente = Sentido.Parado,
|
|
Tras = Sentido.Parado
|
|
},
|
|
Funcoes = new List<FuncoesPinout>() {
|
|
FuncoesPinout.PUL,
|
|
FuncoesPinout.DIR,
|
|
FuncoesPinout.ENA,
|
|
FuncoesPinout.EncoderA,
|
|
FuncoesPinout.EncoderB,
|
|
FuncoesPinout.REF000,
|
|
FuncoesPinout.REF090,
|
|
FuncoesPinout.REF270,
|
|
}
|
|
},
|
|
new DirSensoriamentoMotor() {
|
|
Canal = 2,
|
|
ID = "DT",
|
|
Descricao = "Direito Trás",
|
|
ConfigSentidos = new ConfiguracaoSentidoMotor() {
|
|
Direita = Sentido.Antihorario,
|
|
Esquerda = Sentido.Horario,
|
|
Frente = Sentido.Parado,
|
|
Tras = Sentido.Parado
|
|
},
|
|
Funcoes = new List<FuncoesPinout>() {
|
|
FuncoesPinout.PUL,
|
|
FuncoesPinout.DIR,
|
|
FuncoesPinout.ENA,
|
|
FuncoesPinout.EncoderA,
|
|
FuncoesPinout.EncoderB,
|
|
FuncoesPinout.REF000,
|
|
FuncoesPinout.REF090,
|
|
FuncoesPinout.REF270,
|
|
}
|
|
},
|
|
new DirSensoriamentoMotor() {
|
|
Canal = 3,
|
|
ID = "DF",
|
|
Descricao = "Direito Frente",
|
|
ConfigSentidos = new ConfiguracaoSentidoMotor() {
|
|
Direita = Sentido.Antihorario,
|
|
Esquerda = Sentido.Horario,
|
|
Frente = Sentido.Parado,
|
|
Tras = Sentido.Parado
|
|
},
|
|
Funcoes = new List<FuncoesPinout>() {
|
|
FuncoesPinout.PUL,
|
|
FuncoesPinout.DIR,
|
|
FuncoesPinout.ENA,
|
|
FuncoesPinout.EncoderA,
|
|
FuncoesPinout.EncoderB,
|
|
FuncoesPinout.REF000,
|
|
FuncoesPinout.REF090,
|
|
FuncoesPinout.REF270,
|
|
}
|
|
}
|
|
}
|
|
|
|
};
|
|
}
|
|
|
|
public List<string> ProtocoloConfiguracao(bool Conectar)
|
|
{
|
|
List<string> Config = Motores.Select(x => x.ProtocoloConfiguracaoMotor(Conectar)).ToList();
|
|
string ProtocoloMOD = ((int)F_Code.Cfg).ToString() +
|
|
"MD" + ";" + // 0 ~ 1
|
|
(Conectar ? "1" : "0") + ";" + // 3
|
|
_TaxaAmostragem.ToString("00000") + ";" + // 5 ~ 9
|
|
(Pinout.Any(x => x.Funcao == FuncoesPinout.Geral) ? Pinout.FirstOrDefault(x => x.Funcao == FuncoesPinout.Geral).Pino.ToString("00") : "-1") + ";" + // 11 ~ 12
|
|
(AA_Ref000 ? "1" : "0") + ";" + // 14
|
|
(AA_Ref090 ? "1" : "0") + ";" + // 16
|
|
(AA_Ref270 ? "1" : "0"); // 18
|
|
Config.Add(ProtocoloMOD);
|
|
return Config;
|
|
}
|
|
|
|
public void RecebeProtocoloSerial(string Protocolo)
|
|
{
|
|
// #ET;00000;00000;00000;000
|
|
if (!string.IsNullOrEmpty(Protocolo) && Protocolo[0].ToString() == SerialService.BeginSensor)
|
|
{
|
|
string[] Dados = Protocolo.Replace(SerialService.BeginSensor, "").Split(SerialService.EndLine[0])[0].Split(';');
|
|
S_Code TipoSensor = (S_Code)(int.Parse(Dados[0]));
|
|
DirSensoriamentoMotor Motor = Motores.FirstOrDefault(x => x.ID == Dados[1]);
|
|
if (Motor == null)
|
|
{
|
|
if (Dados[1] == "MD")
|
|
{
|
|
Conectado = Dados[2] == "1";
|
|
}
|
|
return;
|
|
}
|
|
try
|
|
{
|
|
bool AtualizaGrafico = false;
|
|
|
|
switch (TipoSensor)
|
|
{
|
|
case S_Code.sENC:
|
|
{
|
|
int _sentido = 0;
|
|
int.TryParse(Dados[2].ToString(), out _sentido);
|
|
Motor.Sentido = (Sentido)_sentido;
|
|
int _angulo = 0;
|
|
int.TryParse(Dados[3].ToString(), out _angulo);
|
|
Motor.Angulo = _angulo;
|
|
Motor.Ref000 = Dados[4] == "1";
|
|
Motor.Ref090 = Dados[5] == "1";
|
|
Motor.Ref270 = Dados[6] == "1";
|
|
|
|
bool r1 = Dados[7] == "1";
|
|
bool r2 = Dados[8] == "1";
|
|
|
|
Motor.Referenciando = r1;
|
|
Motor.Referenciado = r2;
|
|
|
|
AtualizaGrafico = true;
|
|
break;
|
|
}
|
|
case S_Code.sTST:
|
|
{
|
|
bool testando = Dados[1] == "1";
|
|
if (!Motor.Testando && testando)
|
|
{
|
|
Motor.LogTestes = new List<SensoriamentoMotorTestes>();
|
|
}
|
|
Motor.Testando = testando;
|
|
if (Motor.Testando && Dados.Length >= 4)
|
|
{
|
|
string componente = Dados[2];
|
|
string resultado = Dados[3];
|
|
string comando = Dados[4];
|
|
string leitura = Dados[5];
|
|
Motor.LogTestes.Add(new SensoriamentoMotorTestes()
|
|
{
|
|
Componente = componente,
|
|
Sucesso = resultado == "SUCESSO",
|
|
Resultado = resultado,
|
|
Comando = comando,
|
|
Leitura = leitura,
|
|
Momento = DateTime.Now
|
|
});
|
|
}
|
|
break;
|
|
}
|
|
case S_Code.sCFG:
|
|
{
|
|
Motor.Inicializado = Dados[2] == "1";
|
|
break;
|
|
}
|
|
|
|
}
|
|
|
|
if (AtualizaGrafico)
|
|
{
|
|
Motor.LogGrafico.Add(new DirSensorimanetoGrafico()
|
|
{
|
|
Momento = DateTime.Now,
|
|
ComponenteID = Motor.ID,
|
|
Angulo_SP = Motor.Angulo_SP,
|
|
Angulo = Motor.Angulo,
|
|
Sentido = Motor.Sentido,
|
|
Ref0 = Motor.Ref000,
|
|
Ref90 = Motor.Ref090,
|
|
Ref270 = Motor.Ref270,
|
|
});
|
|
|
|
if (Motor.LogGrafico.Count() > LogsManter)
|
|
{
|
|
Motor.LogGrafico.RemoveAt(0);
|
|
}
|
|
|
|
if (Variaveis.DispositivosConectados.Any(x => x.Dispositivo == T_Code.Dir))
|
|
{
|
|
//((frmDirPlot)(frmPrincipal.Dispositivos.First(x => x.Dispositivo == T_Code.Dir).frmPlot)).AtualizaGrafico();
|
|
frmInstancial.frmAcompanhamento.AtualizaGrafico();
|
|
}
|
|
|
|
}
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
Console.WriteLine(ex.Message);
|
|
}
|
|
|
|
|
|
|
|
}
|
|
}
|
|
|
|
public string GetTaxaAmostragem()
|
|
{
|
|
return _TaxaAmostragem.ToString("00000");
|
|
}
|
|
|
|
public void SetTaxaAmostragem(int TaxaAmostragem)
|
|
{
|
|
_TaxaAmostragem = TaxaAmostragem;
|
|
}
|
|
|
|
public bool GetStatusConexao()
|
|
{
|
|
return Conectado;
|
|
}
|
|
|
|
public void PopulaGridSensoriamento()
|
|
{
|
|
|
|
}
|
|
|
|
public void AtualizaGridSensoriamento()
|
|
{
|
|
|
|
}
|
|
|
|
public List<PinoutModel> CarregarPinout(string nomeArquivo)
|
|
{
|
|
if (!File.Exists(nomeArquivo))
|
|
{
|
|
var newPinout = PinoutModel.CarregarPinoutPadrao();
|
|
File.WriteAllText(nomeArquivo, JsonConvert.SerializeObject(newPinout));
|
|
}
|
|
var pinoutText = File.ReadAllText(nomeArquivo);
|
|
Pinout = JsonConvert.DeserializeObject<PinoutModel[]>(pinoutText).ToList();
|
|
return Pinout;
|
|
}
|
|
|
|
public void SalvarPinout(List<PinoutModel> newPinout, string nomeArquivo)
|
|
{
|
|
File.WriteAllText(nomeArquivo, JsonConvert.SerializeObject(newPinout));
|
|
}
|
|
|
|
public void IniciarRegistrador()
|
|
{
|
|
|
|
}
|
|
}
|
|
|
|
public class DirSensoriamentoMotor
|
|
{
|
|
public int Canal { get; set; }
|
|
public string ID { get; set; }
|
|
public string Descricao { get; set; }
|
|
public bool Comandar { get; set; } = false;
|
|
public bool Inicializado { get; set; } = false;
|
|
public Sentido Sentido_SP { get; set; } = Sentido.Parado;
|
|
public Sentido Sentido { get; set; } = Sentido.Parado;
|
|
public double Angulo_SP { get; set; } = 0;
|
|
public double Angulo { get; set; } = 0;
|
|
public double Angulo_Offset { get; set; } = 0;
|
|
public int Velocidade { get; set; } = 50;
|
|
public bool Ref000 { get; set; } = false;
|
|
public bool Ref090 { get; set; } = false;
|
|
public bool Ref180 { get; set; } = false;
|
|
public bool Ref270 { get; set; } = false;
|
|
public bool Referenciando { get; set; } = false;
|
|
public bool Referenciado { get; set; } = false;
|
|
public bool Controlar { get; set; } = false;
|
|
public bool Estabilizar { get; set; } = false;
|
|
public bool Referenciar { get; set; } = false;
|
|
public bool Livre { get; set; } = false;
|
|
public ConfiguracaoSentidoMotor ConfigSentidos { get; set; }
|
|
public bool Testando { get; set; } = false;
|
|
public List<SensoriamentoMotorTestes> LogTestes { get; set; } = new List<SensoriamentoMotorTestes>();
|
|
public List<DirSensorimanetoGrafico> LogGrafico { get; set; } = new List<DirSensorimanetoGrafico>();
|
|
public List<FuncoesPinout> Funcoes { get; set; }
|
|
|
|
|
|
public string ProtocoloConfiguracaoMotor(bool Conectar)
|
|
{
|
|
var _pinout = DirecionalModel.Pinout.Where(x => x.ComponenteID == ID).ToList();
|
|
return ((int)F_Code.Cfg).ToString() +
|
|
ID + ";" + // 0 ~ 1
|
|
(Conectar ? "1" : "0") + ";" + // 3
|
|
Canal.ToString() + ";" + // 5
|
|
(Comandar ? "1" : "0") + ";" + // 7
|
|
(Estabilizar ? "1" : "0") + ";" + // 9
|
|
(Referenciar ? "1" : "0") + ";" + // 11
|
|
(_pinout.Any(x => x.Funcao == FuncoesPinout.PUL) ? _pinout.FirstOrDefault(x => x.Funcao == FuncoesPinout.PUL).Pino.ToString("00") : "-1") + "," + // 13 ~ 14
|
|
(_pinout.Any(x => x.Funcao == FuncoesPinout.DIR) ? _pinout.FirstOrDefault(x => x.Funcao == FuncoesPinout.DIR).Pino.ToString("00") : "-1") + "," + // 16 ~ 17
|
|
(_pinout.Any(x => x.Funcao == FuncoesPinout.ENA) ? _pinout.FirstOrDefault(x => x.Funcao == FuncoesPinout.ENA).Pino.ToString("00") : "-1") + "," + // 19 ~ 20
|
|
(_pinout.Any(x => x.Funcao == FuncoesPinout.EncoderA) ? _pinout.FirstOrDefault(x => x.Funcao == FuncoesPinout.EncoderA).Pino.ToString("00") : "-1") + "," + // 22 ~ 23
|
|
(_pinout.Any(x => x.Funcao == FuncoesPinout.EncoderB) ? _pinout.FirstOrDefault(x => x.Funcao == FuncoesPinout.EncoderB).Pino.ToString("00") : "-1") + "," + // 25 ~ 26
|
|
(_pinout.Any(x => x.Funcao == FuncoesPinout.REF000) ? _pinout.FirstOrDefault(x => x.Funcao == FuncoesPinout.REF000).Pino.ToString("00") : "-1") + "," + // 28 ~ 29
|
|
(_pinout.Any(x => x.Funcao == FuncoesPinout.REF090) ? _pinout.FirstOrDefault(x => x.Funcao == FuncoesPinout.REF090).Pino.ToString("00") : "-1") + "," + // 31 ~ 32
|
|
(_pinout.Any(x => x.Funcao == FuncoesPinout.REF270) ? _pinout.FirstOrDefault(x => x.Funcao == FuncoesPinout.REF270).Pino.ToString("00") : "-1") + ";" + // 34 ~ 35
|
|
Angulo_Offset.ToString(Angulo_Offset < 0 ? "000" : "0000"); // 37 ~ 40
|
|
}
|
|
|
|
public string ProtocoloComando(Direcao Direcao, bool _Ref = false)
|
|
{
|
|
if (Comandar && Controlar)
|
|
{
|
|
Sentido_SP =
|
|
Direcao == Direcao.Cima ? ConfigSentidos.Direita :
|
|
Direcao == Direcao.Baixo ? ConfigSentidos.Esquerda :
|
|
!Livre && Sentido_SP == Sentido.Horario ? Sentido.Antihorario :
|
|
!Livre && Sentido_SP == Sentido.Antihorario ? Sentido.Horario :
|
|
Sentido.Parado;
|
|
|
|
Angulo_SP = Livre ? -1 : (Direcao != Direcao.Cima && Direcao != Direcao.Baixo) ? 0 : GeneralJoystick.Angulo;
|
|
Velocidade = GeneralJoystick.Velocidade;
|
|
|
|
string ProtocoloMotor =
|
|
((int)F_Code.Cmd).ToString() +
|
|
ID + ";" + // 0 ~ 1
|
|
((int)Sentido_SP).ToString() + ";" + // 3
|
|
Angulo_SP.ToString(Angulo_SP < 0 ? "00" : "000") + ";" + // 5 ~ 7
|
|
Velocidade.ToString("000") + ";" + // 9 ~ 11
|
|
(_Ref ? "1" : "0") + ";" + // 13
|
|
Angulo_Offset.ToString(Angulo_Offset < 0 ? "000" : "0000"); // 15 ~ 18
|
|
return ProtocoloMotor;
|
|
}
|
|
return "";
|
|
}
|
|
|
|
public string ProtocoloTeste()
|
|
{
|
|
if (Comandar)
|
|
{
|
|
string ProtocoloMotor =
|
|
((int)F_Code.Tst).ToString() +
|
|
ID; // 0 ~ 1;
|
|
return ProtocoloMotor;
|
|
}
|
|
return "";
|
|
}
|
|
|
|
|
|
}
|
|
|
|
public class SensoriamentoMotorTestes
|
|
{
|
|
public DateTime Momento { get; set; }
|
|
public string Componente { get; set; }
|
|
public bool Sucesso { get; set; }
|
|
public string Resultado { get; set; }
|
|
public string Comando { get; set; }
|
|
public string Leitura { get; set; }
|
|
}
|
|
|
|
public class DirSensorimanetoGrafico
|
|
{
|
|
public DateTime Momento { get; set; }
|
|
public string ComponenteID { get; set; }
|
|
public double Angulo_SP { get; set; }
|
|
public double Angulo { get; set; }
|
|
public bool Ref0 { get; set; }
|
|
public bool Ref90 { get; set; }
|
|
public bool Ref270 { get; set; }
|
|
public Sentido Sentido { get; set; }
|
|
}
|
|
|
|
|
|
}
|