2023-10-09 16:06:23 +00:00
using AgroBase.Forms ;
using AgroBase.Models.Modules ;
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 ;
2023-11-30 00:41:22 +00:00
using System.Windows.Forms ;
2023-10-09 16:06:23 +00:00
using static AgroBase . Models . Enuns ;
namespace AgroBase.Models
{
2024-01-03 10:52:37 +00:00
public class MovimentacaoModel : DispositivoBaseModel
2023-10-09 16:06:23 +00:00
{
public static int _TaxaAmostragem { get ; set ; } = 500 ;
2024-01-22 17:07:27 +00:00
public static int RampaMin { get ; set ; } = 0 ;
2024-01-31 12:16:02 +00:00
public static int RampaMax { get ; set ; } = 1023 ;
2023-11-30 00:41:22 +00:00
public static int PulsosPorRevolucao { get ; set ; } = 22 ;
2023-10-09 16:06:23 +00:00
public static bool _FOC { get ; set ; } = true ;
2024-01-31 17:23:52 +00:00
public static double Kp { get ; set ; } = 1.75 ;
2023-11-30 00:41:22 +00:00
public static double Ki { get ; set ; } = 2.1 ;
public static double Kd { get ; set ; } = 0.08 ;
2024-01-31 12:16:02 +00:00
public static int _DelayBalanceamento { get ; set ; } = 2000 ;
public static int _MargemRPM { get ; set ; } = 5 ;
2024-01-31 17:23:52 +00:00
public static int _MaxOffsetRPM { get ; set ; } = 25 ;
2024-01-03 10:52:37 +00:00
public static double DiametroRoda { get ; set ; } = 0.3556 ;
2023-10-09 16:06:23 +00:00
public bool Conectado { get ; set ; } = false ;
2023-11-30 00:41:22 +00:00
public static int LogsManter { get ; set ; } = 60 ;
2023-10-09 16:06:23 +00:00
public static List < PinoutModel > Pinout { get ; set ; }
2023-11-30 00:41:22 +00:00
public static List < FuncoesPinout > Funcoes { get ; set ; } = new List < FuncoesPinout > ( ) { FuncoesPinout . Geral } ;
public List < MovSensoriamentoMotor > Motores { get ; set ; }
public static Direcao UltimaDirecao { get ; set ; } = Direcao . Parado ;
public int TempoEmAtividade { get ; set ; } = 0 ;
public double DistanciaPercorridaTotal { get ; set ; } = 0 ;
public double DistanciaPercorridaParcial { get ; set ; } = 0 ;
public Timer tmrRegistrador { get ; set ; }
2024-01-31 12:16:02 +00:00
2023-11-30 00:41:22 +00:00
public static MovimentacaoModel CarregarParametrosIniciais ( )
2023-10-09 16:06:23 +00:00
{
2023-11-30 00:41:22 +00:00
return new MovimentacaoModel ( )
{
Motores = new List < MovSensoriamentoMotor > ( )
{
new MovSensoriamentoMotor ( ) {
Canal = 0 ,
ID = "ET" ,
Descricao = "Esquerdo Trás" ,
ConfigSentidos = new ConfiguracaoSentidoMotor ( ) {
Direita = Sentido . Antihorario ,
Esquerda = Sentido . Horario ,
Frente = Sentido . Horario ,
Tras = Sentido . Antihorario
} ,
Funcoes = new List < FuncoesPinout > ( ) {
FuncoesPinout . PWM ,
FuncoesPinout . HallA ,
FuncoesPinout . HallB ,
FuncoesPinout . HallC ,
FuncoesPinout . DIR ,
FuncoesPinout . BRK ,
FuncoesPinout . STP ,
FuncoesPinout . TMP ,
}
} ,
new MovSensoriamentoMotor ( ) {
Canal = 1 ,
ID = "EF" ,
Descricao = "Esquerdo Frente" ,
ConfigSentidos = new ConfiguracaoSentidoMotor ( ) {
Direita = Sentido . Antihorario ,
Esquerda = Sentido . Horario ,
Frente = Sentido . Horario ,
Tras = Sentido . Antihorario
} ,
Funcoes = new List < FuncoesPinout > ( ) {
FuncoesPinout . PWM ,
FuncoesPinout . HallA ,
FuncoesPinout . HallB ,
FuncoesPinout . HallC ,
FuncoesPinout . DIR ,
FuncoesPinout . BRK ,
FuncoesPinout . STP ,
FuncoesPinout . TMP ,
}
} ,
new MovSensoriamentoMotor ( ) {
Canal = 2 ,
ID = "DT" ,
Descricao = "Direito Trás" ,
ConfigSentidos = new ConfiguracaoSentidoMotor ( ) {
Direita = Sentido . Horario ,
Esquerda = Sentido . Antihorario ,
Frente = Sentido . Horario ,
Tras = Sentido . Antihorario
} ,
Funcoes = new List < FuncoesPinout > ( ) {
FuncoesPinout . PWM ,
FuncoesPinout . HallA ,
FuncoesPinout . HallB ,
FuncoesPinout . HallC ,
FuncoesPinout . DIR ,
FuncoesPinout . BRK ,
FuncoesPinout . STP ,
FuncoesPinout . TMP ,
}
} ,
new MovSensoriamentoMotor ( ) {
Canal = 3 ,
ID = "DF" ,
Descricao = "Direito Frente" ,
ConfigSentidos = new ConfiguracaoSentidoMotor ( ) {
Direita = Sentido . Antihorario ,
Esquerda = Sentido . Horario ,
Frente = Sentido . Antihorario ,
Tras = Sentido . Horario
} ,
Funcoes = new List < FuncoesPinout > ( ) {
FuncoesPinout . PWM ,
FuncoesPinout . HallA ,
FuncoesPinout . HallB ,
FuncoesPinout . HallC ,
FuncoesPinout . DIR ,
FuncoesPinout . BRK ,
FuncoesPinout . STP ,
FuncoesPinout . TMP ,
}
}
2023-10-09 16:06:23 +00:00
} ,
2023-11-30 00:41:22 +00:00
} ;
}
2023-10-09 16:06:23 +00:00
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
2023-11-30 00:41:22 +00:00
_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
RampaMin . ToString ( "0000" ) + ";" + // 14 ~ 17
2024-01-31 17:23:52 +00:00
RampaMax . ToString ( "0000" ) + ";" + // 19 ~ 22
PulsosPorRevolucao . ToString ( "000" ) + ";" + // 24 ~ 26
_DelayBalanceamento . ToString ( "000" ) + ";" + // 28 ~ 32
_MargemRPM . ToString ( "00" ) + ";" + // 34 ~ 35
_MaxOffsetRPM . ToString ( "00" ) ; // 37 ~ 38
2023-10-09 16:06:23 +00:00
Config . Add ( ProtocoloMOD ) ;
return Config ;
}
public void RecebeProtocoloSerial ( string Protocolo )
{
if ( ! string . IsNullOrEmpty ( Protocolo ) & & Protocolo [ 0 ] . ToString ( ) = = SerialService . BeginSensor )
{
try
{
string [ ] Dados = Protocolo . Replace ( SerialService . BeginSensor , "" ) . Split ( SerialService . EndLine [ 0 ] ) [ 0 ] . Split ( ';' ) ;
S_Code TipoSensor = ( S_Code ) ( int . Parse ( Dados [ 0 ] ) ) ;
2023-11-30 00:41:22 +00:00
MovSensoriamentoMotor Motor = Motores . FirstOrDefault ( x = > x . ID = = Dados [ 1 ] ) ;
2023-10-09 16:06:23 +00:00
if ( Motor = = null )
{
if ( Dados [ 1 ] = = "MD" )
{
Conectado = Dados [ 2 ] = = "1" ;
}
return ;
}
bool AtualizaGrafico = false ;
try
{
switch ( TipoSensor )
{
case S_Code . sRPM :
{
double rpm = 0 ;
double . TryParse ( Dados [ 2 ] . Replace ( "." , "," ) , out rpm ) ;
Motor . RPM = rpm ;
double potencia = 0 ;
double . TryParse ( Dados [ 3 ] . Replace ( "." , "," ) , out potencia ) ;
Motor . Potencia = potencia ;
2023-11-30 00:41:22 +00:00
int aceleracao = 0 ;
int . TryParse ( Dados [ 4 ] , out aceleracao ) ;
Motor . Aceleracao = ( StatusMotor ) aceleracao ;
int sentido = 0 ;
int . TryParse ( Dados [ 5 ] , out sentido ) ;
Motor . Sentido = ( Sentido ) sentido ;
Motor . Revertendo = Dados [ 6 ] = = "1" ;
2024-01-31 12:16:02 +00:00
double rpm_offset = 0 ;
double . TryParse ( Dados [ 7 ] . Replace ( "." , "," ) , out rpm_offset ) ;
Motor . RPM_Offset = rpm_offset ;
2023-10-09 16:06:23 +00:00
AtualizaGrafico = true ;
break ;
}
case S_Code . sTST :
{
bool testando = Dados [ 2 ] = = "1" ;
if ( ! Motor . Testando & & testando )
{
Motor . LogTestes = new List < SensoriamentoMotorTestes > ( ) ;
}
Motor . Testando = testando ;
if ( Motor . Testando & & Dados . Length > = 5 )
{
string componente = Dados [ 1 ] ;
string resultado = Dados [ 2 ] ;
string comando = Dados [ 3 ] ;
string leitura = Dados [ 4 ] ;
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 ;
}
case S_Code . sTMP :
{
double t = 0 ;
double . TryParse ( Dados [ 2 ] . Replace ( "." , "," ) , out t ) ;
Motor . Temperatura = t ;
2024-01-18 19:22:28 +00:00
/ * double u = 0 ;
2023-10-09 16:06:23 +00:00
double . TryParse ( Dados [ 3 ] . Replace ( "." , "," ) , out u ) ;
Motor . Umidade = u ;
long i = 0 ;
long . TryParse ( Dados [ 4 ] . Replace ( "." , "," ) , out i ) ;
2024-01-18 19:22:28 +00:00
Motor . IndiceCalor = i ; * /
2023-10-09 16:06:23 +00:00
AtualizaGrafico = true ;
break ;
}
}
}
catch ( Exception ex )
{
2023-11-30 00:41:22 +00:00
Console . WriteLine ( ex . Message ) ;
2023-10-09 16:06:23 +00:00
}
if ( AtualizaGrafico )
{
2023-11-30 00:41:22 +00:00
Motor . LogGrafico . Add ( new MovSensorimanetoGrafico ( )
2023-10-09 16:06:23 +00:00
{
Momento = DateTime . Now ,
2023-11-30 00:41:22 +00:00
ComponenteID = Motor . ID ,
2023-10-09 16:06:23 +00:00
RPM_SP = Motor . RPM_SP ,
RPM = Motor . RPM ,
2024-01-31 12:16:02 +00:00
RPM_Offset = Motor . RPM_Offset ,
2023-10-09 16:06:23 +00:00
Periodo = Motor . Intervalo ,
Potencia = Motor . Potencia ,
2023-11-30 00:41:22 +00:00
PotenciaSP = Motor . Potencia_SP ,
2023-10-09 16:06:23 +00:00
Kp = Kp ,
Ki = Ki ,
Kd = Kd ,
Temperatura = Motor . Temperatura ,
Umidade = Motor . Umidade ,
IndiceCalor = Motor . IndiceCalor ,
2023-11-30 00:41:22 +00:00
Sentido = Motor . Sentido ,
2024-01-03 10:52:37 +00:00
Velocidade = Motor . VelocidadeInstantanea ,
2023-10-09 16:06:23 +00:00
} ) ;
2023-11-30 00:41:22 +00:00
if ( Motor . LogGrafico . Count ( ) > LogsManter )
2023-10-09 16:06:23 +00:00
{
2023-11-30 00:41:22 +00:00
Motor . LogGrafico . RemoveAt ( 0 ) ;
2023-10-09 16:06:23 +00:00
}
2023-11-30 00:41:22 +00:00
//frmInstancial.frmPlotMovimentacao.AtualizaGrafico();
/ * if ( frmPrincipal . Dispositivos . Any ( x = > x . Dispositivo = = T_Code . Mov ) )
{
( ( frmMovPlot ) ( frmPrincipal . Dispositivos . First ( x = > x . Dispositivo = = T_Code . Mov ) . frmPlot ) ) . AtualizaGrafico ( ) ;
} * /
frmInstancial . frmAcompanhamento . AtualizaGrafico ( ) ;
2023-10-09 16:06:23 +00:00
}
}
catch ( Exception ex )
{
2023-11-30 00:41:22 +00:00
Console . WriteLine ( ex . Message ) ;
2023-10-09 16:06:23 +00:00
}
}
}
public string GetTaxaAmostragem ( )
{
return _TaxaAmostragem . ToString ( "00000" ) ;
}
public void SetTaxaAmostragem ( int TaxaAmostragem )
{
_TaxaAmostragem = TaxaAmostragem ;
}
public bool GetStatusConexao ( )
{
return Conectado ;
}
public void PopulaGridSensoriamento ( )
{
frmInstancial . frmPrincipal . gridSenMotores . Rows . Clear ( ) ;
frmInstancial . frmPrincipal . gridSenMotores . Columns . Clear ( ) ;
frmInstancial . frmPrincipal . gridSenMotores . Columns . Add ( "clMotor" , "Motor" ) ;
frmInstancial . frmPrincipal . gridSenMotores . Columns . Add ( "clInicializado" , "Inicializado" ) ;
2023-11-30 00:41:22 +00:00
frmInstancial . frmPrincipal . gridSenMotores . Columns . Add ( "clSentidoSP" , "Sentido SP" ) ;
2023-10-09 16:06:23 +00:00
frmInstancial . frmPrincipal . gridSenMotores . Columns . Add ( "clSentido" , "Sentido" ) ;
2023-11-30 00:41:22 +00:00
frmInstancial . frmPrincipal . gridSenMotores . Columns . Add ( "clStatus" , "Status" ) ;
2023-10-09 16:06:23 +00:00
frmInstancial . frmPrincipal . gridSenMotores . Columns . Add ( "clPotencia" , "Potencia" ) ;
2024-01-31 12:16:02 +00:00
frmInstancial . frmPrincipal . gridSenMotores . Columns . Add ( "clRPMOffset" , "RPM Offset" ) ;
2024-01-31 17:23:52 +00:00
frmInstancial . frmPrincipal . gridSenMotores . Columns . Add ( "clRPM" , "RPM" ) ;
2023-10-09 16:06:23 +00:00
frmInstancial . frmPrincipal . gridSenMotores . Columns . Add ( "clVelocidade" , "Velocidade" ) ;
frmInstancial . frmPrincipal . gridSenMotores . Columns . Add ( "clTemperatura" , "Temperatura" ) ;
2024-01-18 19:22:28 +00:00
//frmInstancial.frmPrincipal.gridSenMotores.Columns.Add("clUmidade", "Umidade");
2023-10-09 16:06:23 +00:00
foreach ( var Motor in Motores )
{
frmInstancial . frmPrincipal . gridSenMotores . Rows . Add ( new string [ ]
{
Motor . Descricao ,
Motor . Inicializado ? "Sim" : "Não" ,
2023-11-30 00:41:22 +00:00
Enum . GetName ( typeof ( Sentido ) , Motor . Sentido_SP ) ,
2023-10-09 16:06:23 +00:00
Enum . GetName ( typeof ( Sentido ) , Motor . Sentido ) ,
2023-11-30 00:41:22 +00:00
Enum . GetName ( typeof ( StatusMotor ) , Motor . Aceleracao ) ,
2024-01-18 19:22:28 +00:00
Motor . Potencia . ToString ( ) ,
2024-01-31 12:16:02 +00:00
Motor . RPM_Offset . ToString ( ) ,
2024-01-31 17:23:52 +00:00
Motor . RPM . ToString ( ) ,
2023-11-30 00:41:22 +00:00
Motor . VelocidadeInstantanea . ToString ( ) ,
Motor . Temperatura . ToString ( ) ,
2024-01-18 19:22:28 +00:00
//Motor.Umidade.ToString(),
2023-10-09 16:06:23 +00:00
} ) ;
}
}
public void AtualizaGridSensoriamento ( )
{
foreach ( var Motor in Motores )
{
2024-01-03 10:52:37 +00:00
Motor . VelocidadeInstantanea = CalculaVelocidadeRPM ( Motor . RPM ) ;
2023-10-09 16:06:23 +00:00
frmInstancial . frmPrincipal . gridSenMotores . Rows [ Motor . Canal ] . Cells [ 1 ] . Value = Motor . Inicializado ? "Sim" : "Não" ;
2023-11-30 00:41:22 +00:00
frmInstancial . frmPrincipal . gridSenMotores . Rows [ Motor . Canal ] . Cells [ 2 ] . Value = Enum . GetName ( typeof ( Sentido ) , Motor . Sentido_SP ) ;
frmInstancial . frmPrincipal . gridSenMotores . Rows [ Motor . Canal ] . Cells [ 3 ] . Value = Enum . GetName ( typeof ( Sentido ) , Motor . Sentido ) ;
frmInstancial . frmPrincipal . gridSenMotores . Rows [ Motor . Canal ] . Cells [ 4 ] . Value = Enum . GetName ( typeof ( StatusMotor ) , Motor . Aceleracao ) ;
2024-01-18 19:22:28 +00:00
frmInstancial . frmPrincipal . gridSenMotores . Rows [ Motor . Canal ] . Cells [ 5 ] . Value = Motor . Potencia . ToString ( ) ;
2024-01-31 17:23:52 +00:00
frmInstancial . frmPrincipal . gridSenMotores . Rows [ Motor . Canal ] . Cells [ 6 ] . Value = Motor . RPM_Offset . ToString ( ) ;
frmInstancial . frmPrincipal . gridSenMotores . Rows [ Motor . Canal ] . Cells [ 7 ] . Value = Motor . RPM . ToString ( ) ;
2024-01-31 12:16:02 +00:00
frmInstancial . frmPrincipal . gridSenMotores . Rows [ Motor . Canal ] . Cells [ 8 ] . Value = Motor . VelocidadeInstantanea . ToString ( ) ;
frmInstancial . frmPrincipal . gridSenMotores . Rows [ Motor . Canal ] . Cells [ 9 ] . Value = Motor . Temperatura . ToString ( ) ;
//frmInstancial.frmPrincipal.gridSenMotores.Rows[Motor.Canal].Cells[10].Value = Motor.Umidade.ToString();
2023-10-09 16:06:23 +00:00
}
}
public List < PinoutModel > CarregarPinout ( string nomeArquivo )
{
if ( ! File . Exists ( nomeArquivo ) )
{
2024-01-18 19:22:28 +00:00
var newPinout = PinoutModel . CarregarPinoutPadrao_S3 ( ) ;
2023-10-09 16:06:23 +00:00
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 ) ) ;
}
2023-11-30 00:41:22 +00:00
public void IniciarRegistrador ( )
{
tmrRegistrador = new Timer ( )
{
Interval = 1000 ,
Enabled = false
} ;
tmrRegistrador . Tick + = TmrRegistrador_Tick ;
2024-01-31 12:16:02 +00:00
tmrRegistrador . Start ( ) ;
2023-11-30 00:41:22 +00:00
}
private void TmrRegistrador_Tick ( object sender , EventArgs e )
{
if ( UltimaDirecao ! = Direcao . Parado )
{
TempoEmAtividade + + ;
double VelocidadeMedia = Motores . Average ( x = > x . VelocidadeInstantanea * 1000 / 3600 ) ;
DistanciaPercorridaTotal + = 1 * VelocidadeMedia ;
DistanciaPercorridaParcial + = ( UltimaDirecao = = Direcao . Frente ? 1 : UltimaDirecao = = Direcao . Tras ? - 1 : 0 ) * VelocidadeMedia ;
}
}
2024-01-03 10:52:37 +00:00
public static double CalculaVelocidadeRPM ( double RPM )
{
double VelocidadeInstantanea = Math . Round ( ( Math . PI * DiametroRoda * 60 * RPM ) / 1000 , 2 ) ; // Km/h
return VelocidadeInstantanea ;
}
public static double CalculaRPMVelocidade ( double Velocidade )
{
double RPM = Math . Round ( ( Velocidade * 1000 ) / ( Math . PI * DiametroRoda * 60 ) , 2 ) ;
return RPM ;
}
2024-01-31 12:16:02 +00:00
2023-10-09 16:06:23 +00:00
}
2023-11-30 00:41:22 +00:00
public class MovSensoriamentoMotor
2023-10-09 16:06:23 +00:00
{
public int Canal { get ; set ; }
public string ID { get ; set ; }
public string Descricao { get ; set ; }
public bool Comandar { get ; set ; } = false ;
2024-01-31 17:23:52 +00:00
public bool Alarme { get ; set ; } = false ;
2023-10-09 16:06:23 +00:00
public bool Inicializado { get ; set ; } = false ;
2023-11-30 00:41:22 +00:00
public bool Revertendo { get ; set ; } = false ;
2023-10-09 16:06:23 +00:00
public int RPM_SP { get ; set ; } = 0 ;
public double Potencia { get ; set ; } = 0 ;
2023-11-30 00:41:22 +00:00
public double Potencia_SP { get ; set ; } = 0 ;
2023-10-09 16:06:23 +00:00
public int Rampa { get ; set ; } = 0 ;
public int Precisao { get ; set ; } = 0 ;
2023-11-30 00:41:22 +00:00
public Sentido Sentido_SP { get ; set ; } = Sentido . Parado ;
2023-10-09 16:06:23 +00:00
public Sentido Sentido { get ; set ; } = Sentido . Parado ;
2023-11-30 00:41:22 +00:00
public StatusMotor Aceleracao { get ; set ; } = StatusMotor . Estavel ;
2023-10-09 16:06:23 +00:00
public double RPM { get ; set ; } = 0 ;
2024-01-31 12:16:02 +00:00
public double RPM_Offset { get ; set ; } = 0 ;
2023-10-09 16:06:23 +00:00
public long Intervalo { get ; set ; } = 0 ;
public double VelocidadeInstantanea { get ; set ; } = 0 ;
public double Temperatura { get ; set ; } = 0 ;
public double Umidade { get ; set ; } = 0 ;
public double IndiceCalor { get ; set ; } = 0 ;
public ConfiguracaoSentidoMotor ConfigSentidos { get ; set ; }
public bool Testando { get ; set ; } = false ;
2024-01-31 17:23:52 +00:00
public List < AlarmeModel > Alarmes { get ; set ; } = new List < AlarmeModel > ( ) ;
2023-10-09 16:06:23 +00:00
public List < SensoriamentoMotorTestes > LogTestes { get ; set ; } = new List < SensoriamentoMotorTestes > ( ) ;
2023-11-30 00:41:22 +00:00
public List < MovSensorimanetoGrafico > LogGrafico { get ; set ; } = new List < MovSensorimanetoGrafico > ( ) ;
public List < FuncoesPinout > Funcoes { get ; set ; }
2023-10-09 16:06:23 +00:00
public string ProtocoloConfiguracaoMotor ( bool Conectar )
{
var _pinout = MovimentacaoModel . Pinout . Where ( x = > x . ComponenteID = = ID ) . ToList ( ) ;
return ( ( int ) F_Code . Cfg ) . ToString ( ) +
ID + ";" + // 0 ~ 1
( Conectar ? "1" : "0" ) + ";" + // 3
Canal . ToString ( ) + ";" + // 5
2024-01-31 17:23:52 +00:00
( Alarme ? "1" : "0" ) + ";" + // 7
2023-11-30 00:41:22 +00:00
( Comandar ? "1" : "0" ) + ";" + // 9
MovimentacaoModel . Kp . ToString ( "0.000" ) . Replace ( "," , "." ) + "," + // 11 ~ 15
MovimentacaoModel . Ki . ToString ( "0.000" ) . Replace ( "," , "." ) + "," + // 17 ~ 21
MovimentacaoModel . Kd . ToString ( "0.000" ) . Replace ( "," , "." ) + "," + // 23 ~ 27
( _pinout . Any ( x = > x . Funcao = = FuncoesPinout . PWM ) ? _pinout . FirstOrDefault ( x = > x . Funcao = = FuncoesPinout . PWM ) . Pino . ToString ( "00" ) : "-1" ) + "," + // 29 ~ 30
( _pinout . Any ( x = > x . Funcao = = FuncoesPinout . DIR ) ? _pinout . FirstOrDefault ( x = > x . Funcao = = FuncoesPinout . DIR ) . Pino . ToString ( "00" ) : "-1" ) + "," + // 32 ~ 33
( _pinout . Any ( x = > x . Funcao = = FuncoesPinout . BRK ) ? _pinout . FirstOrDefault ( x = > x . Funcao = = FuncoesPinout . BRK ) . Pino . ToString ( "00" ) : "-1" ) + "," + // 35 ~ 36
( _pinout . Any ( x = > x . Funcao = = FuncoesPinout . STP ) ? _pinout . FirstOrDefault ( x = > x . Funcao = = FuncoesPinout . STP ) . Pino . ToString ( "00" ) : "-1" ) + "," + // 38 ~ 39
( _pinout . Any ( x = > x . Funcao = = FuncoesPinout . HallA ) ? _pinout . FirstOrDefault ( x = > x . Funcao = = FuncoesPinout . HallA ) . Pino . ToString ( "00" ) : "-1" ) + "," + // 41 ~ 42
( _pinout . Any ( x = > x . Funcao = = FuncoesPinout . HallB ) ? _pinout . FirstOrDefault ( x = > x . Funcao = = FuncoesPinout . HallB ) . Pino . ToString ( "00" ) : "-1" ) + "," + // 44 ~ 45
( _pinout . Any ( x = > x . Funcao = = FuncoesPinout . HallC ) ? _pinout . FirstOrDefault ( x = > x . Funcao = = FuncoesPinout . HallC ) . Pino . ToString ( "00" ) : "-1" ) + "," + // 47 ~ 48
( _pinout . Any ( x = > x . Funcao = = FuncoesPinout . TMP ) ? _pinout . FirstOrDefault ( x = > x . Funcao = = FuncoesPinout . TMP ) . Pino . ToString ( "00" ) : "-1" ) ; // 50 ~ 51
2023-10-09 16:06:23 +00:00
}
2024-01-31 17:23:52 +00:00
public string ProtocoloComando ( Direcao DirecaoAtual , bool Individual = false )
2023-10-09 16:06:23 +00:00
{
2024-01-31 17:23:52 +00:00
if ( Comandar & & ! Individual )
2023-10-09 16:06:23 +00:00
{
2024-01-31 17:23:52 +00:00
if ( ! Alarme )
{
MovimentacaoModel . UltimaDirecao = DirecaoAtual ;
}
2023-11-30 00:41:22 +00:00
RPM_SP = DirecaoAtual = = Direcao . Parado ? 0 : GeneralJoystick . RPM ;
2023-10-09 16:06:23 +00:00
2023-11-30 00:41:22 +00:00
switch ( DirecaoAtual )
2023-10-09 16:06:23 +00:00
{
case Direcao . Frente :
2023-11-30 00:41:22 +00:00
Sentido_SP = ConfigSentidos . Frente ;
2023-10-09 16:06:23 +00:00
break ;
case Direcao . Tras :
2023-11-30 00:41:22 +00:00
Sentido_SP = ConfigSentidos . Tras ;
2023-10-09 16:06:23 +00:00
break ;
case Direcao . Esquerda :
2023-11-30 00:41:22 +00:00
Sentido_SP = ConfigSentidos . Esquerda ;
2023-10-09 16:06:23 +00:00
break ;
case Direcao . Direita :
2023-11-30 00:41:22 +00:00
Sentido_SP = ConfigSentidos . Direita ;
2023-10-09 16:06:23 +00:00
break ;
case Direcao . Parado :
default :
2023-11-30 00:41:22 +00:00
Sentido_SP = ConfigSentidos . Parado ;
2023-10-09 16:06:23 +00:00
break ;
}
2023-11-30 00:41:22 +00:00
string ProtocoloMotor = "" ;
if ( Revertendo & & false )
{
return ProtocoloMotor ;
}
ProtocoloMotor =
( ( int ) F_Code . Cmd ) . ToString ( ) +
ID + ";" + // 0 ~ 1
( ( int ) Sentido_SP ) . ToString ( ) + ";" + // 3
RPM_SP . ToString ( "000" ) + ";" + // 5 ~ 7
2024-01-31 17:23:52 +00:00
( Alarme ? "1" : "0" ) ; // 9
2023-11-30 00:41:22 +00:00
if ( Sentido_SP ! = Sentido . Parado )
{
Revertendo = true ;
}
2023-10-09 16:06:23 +00:00
return ProtocoloMotor ;
}
return "" ;
}
public string ProtocoloTeste ( )
{
if ( Comandar )
{
string ProtocoloMotor =
( ( int ) F_Code . Tst ) . ToString ( ) +
ID ;
return ProtocoloMotor ;
}
return "" ;
}
2024-01-31 17:23:52 +00:00
public void AtualizarTemperaturaMotor ( SensorModel Sensor )
{
Temperatura = Sensor . Leituras [ 0 ] ;
AlarmeModel _Alarme = new AlarmeModel ( )
{
Tipo = S_Code . sTMP ,
Valor = Sensor . Leituras [ 0 ] ,
Maximo = Temperatura > Sensor . LimiteMaximo ,
Minimo = Temperatura < Sensor . LimiteMinimo ,
ParaMaximo = true ,
ParaMinimo = true
} ;
bool Adicionado = false ;
var Alm = Alarmes . FirstOrDefault ( x = > x . Tipo = = _Alarme . Tipo ) ;
if ( Alm = = null )
{
Alarmes . Add ( _Alarme ) ;
Alm = Alarmes . FirstOrDefault ( x = > x . Tipo = = _Alarme . Tipo ) ;
Adicionado = true ;
}
bool AlarmeAnterior = ( Alm . Maximo & & Alm . ParaMaximo ) | | ( Alm . Minimo & & Alm . ParaMinimo ) ;
bool AlarmeAtual = ( _Alarme . Maximo & & _Alarme . ParaMaximo ) | | ( _Alarme . Minimo & & _Alarme . ParaMinimo ) ;
if ( Inicializado & & ( ! AlarmeAnterior | | Adicionado ) & & AlarmeAtual ) // Motor iniciado, Não estava em alarme anteriormente ou foi adicionado agora, alarme atual
{
Variaveis . DispositivosConectados . FirstOrDefault ( x = > x . Dispositivo = = T_Code . Mov ) . EnviarDadosSerial ( ProtocoloComando ( Direcao . Parado , true ) ) ;
Potencia = 0 ;
RPM_SP = 0 ;
}
Alm . Valor = _Alarme . Valor ;
Alm . Maximo = _Alarme . Maximo ;
Alm . Minimo = _Alarme . Minimo ;
Alarme = Alarmes . Any ( x = > ( x . Maximo & & x . ParaMaximo ) | | ( x . Minimo & & x . ParaMinimo ) ) ;
}
2023-10-09 16:06:23 +00:00
}
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 ; }
}
2023-11-30 00:41:22 +00:00
public class MovSensorimanetoGrafico
2023-10-09 16:06:23 +00:00
{
public DateTime Momento { get ; set ; }
2023-11-30 00:41:22 +00:00
public string ComponenteID { get ; set ; }
2023-10-09 16:06:23 +00:00
public int RPM_SP { get ; set ; }
public double RPM { get ; set ; }
2024-01-31 12:16:02 +00:00
public double RPM_Offset { get ; set ; }
2023-10-09 16:06:23 +00:00
public long Periodo { get ; set ; }
public double Potencia { get ; set ; }
2023-11-30 00:41:22 +00:00
public double PotenciaSP { get ; set ; }
2023-10-09 16:06:23 +00:00
public double Kp { get ; set ; }
public double Ki { get ; set ; }
public double Kd { get ; set ; }
public double Temperatura { get ; set ; }
public double Umidade { get ; set ; }
public double IndiceCalor { get ; set ; }
2023-11-30 00:41:22 +00:00
public Sentido Sentido { get ; set ; }
2024-01-03 10:52:37 +00:00
public double Velocidade { get ; set ; }
2023-10-09 16:06:23 +00:00
}
2023-11-30 00:41:22 +00:00
2023-10-09 16:06:23 +00:00
}