152 lines
4.7 KiB
C#
152 lines
4.7 KiB
C#
// EncoderMotorLinealViewModel.cs
|
|
using CommunityToolkit.Mvvm.ComponentModel;
|
|
using System.Windows.Media;
|
|
using System.ComponentModel;
|
|
using LibS7Adv;
|
|
using System.Diagnostics;
|
|
using Xceed.Wpf.Toolkit.PropertyGrid.Attributes;
|
|
using CtrEditor.FuncionesBase;
|
|
using System.Windows;
|
|
using System.Windows.Controls;
|
|
|
|
namespace CtrEditor.ObjetosSim
|
|
{
|
|
public partial class osEncoderMotorLineal : osBase, IosBase
|
|
{
|
|
private osBase Motor = null;
|
|
private Stopwatch Stopwatch = new Stopwatch();
|
|
private double stopwatch_last = 0;
|
|
|
|
public static string NombreClase()
|
|
{
|
|
return "Encoder Motor Lineal";
|
|
}
|
|
|
|
private string nombre = NombreClase();
|
|
public override string Nombre
|
|
{
|
|
get => nombre;
|
|
set => SetProperty(ref nombre, value);
|
|
}
|
|
|
|
[ObservableProperty]
|
|
private Brush color_oculto;
|
|
|
|
[ObservableProperty]
|
|
public float velocidadActual;
|
|
|
|
[ObservableProperty]
|
|
[property: Description("Pulsos por Hz por segundo : K")]
|
|
[property: Category("Encoder Config:")]
|
|
public float pulsos_Por_Hz;
|
|
|
|
[ObservableProperty]
|
|
[property: Description("Valor actual del encoder")]
|
|
[property: Category("Encoder Status:")]
|
|
public float valor_Actual;
|
|
|
|
[ObservableProperty]
|
|
[property: Description("Link to Motor")]
|
|
[property: Category("PLC link:")]
|
|
[property: ItemsSource(typeof(osBaseItemsSource<osVMmotorSim>))]
|
|
string id_Motor;
|
|
|
|
[ObservableProperty]
|
|
[property: Description("Tag para escribir el valor del encoder")]
|
|
[property: Category("PLC link:")]
|
|
string tag_Valor;
|
|
|
|
partial void OnId_MotorChanged(string value)
|
|
{
|
|
if (Motor != null)
|
|
Motor.PropertyChanged -= OnMotorPropertyChanged;
|
|
if (_mainViewModel != null && value != null && value.Length > 0)
|
|
{
|
|
Motor = (osVMmotorSim)_mainViewModel.ObjetosSimulables.FirstOrDefault(s => (s is osVMmotorSim && s.Nombre == value), null);
|
|
if (Motor != null)
|
|
Motor.PropertyChanged += OnMotorPropertyChanged;
|
|
}
|
|
}
|
|
|
|
private void OnMotorPropertyChanged(object sender, PropertyChangedEventArgs e)
|
|
{
|
|
if (e.PropertyName == nameof(osVMmotorSim.Nombre))
|
|
{
|
|
Id_Motor = ((osVMmotorSim)sender).Nombre;
|
|
}
|
|
}
|
|
|
|
public osEncoderMotorLineal()
|
|
{
|
|
Pulsos_Por_Hz = 3000; // Por defecto, un pulso por grado
|
|
Color_oculto = Brushes.Gray;
|
|
Stopwatch.Start();
|
|
}
|
|
|
|
public override void UpdatePLC(PLCViewModel plc, int elapsedMilliseconds)
|
|
{
|
|
if (Motor != null && Motor is osVMmotorSim motor)
|
|
{
|
|
VelocidadActual = motor.Velocidad;
|
|
|
|
// Calcular giros por segundo basado en la velocidad actual
|
|
float pulsosPorHz = (VelocidadActual * Pulsos_Por_Hz) / 100f;
|
|
|
|
// Considerar el sentido de giro
|
|
if (motor.Sentido_contrario)
|
|
pulsosPorHz = -pulsosPorHz;
|
|
|
|
// Calcular incremento de pulsos para este ciclo
|
|
float segundosTranscurridos = elapsedMilliseconds / 1000f;
|
|
float incrementoPulsos = pulsosPorHz * segundosTranscurridos;
|
|
|
|
// Actualizar valor del encoder
|
|
Valor_Actual = (Valor_Actual + incrementoPulsos);
|
|
|
|
// Actualizar color basado en si está girando
|
|
Color_oculto = Math.Abs(pulsosPorHz) > 0.01f ? Brushes.LightGreen : Brushes.Gray;
|
|
|
|
// Escribir valor al PLC si hay tag configurado
|
|
if (!string.IsNullOrEmpty(Tag_Valor))
|
|
{
|
|
EscribirDINTTag(tag_Valor, (int)Valor_Actual);
|
|
}
|
|
}
|
|
}
|
|
|
|
public override void ucLoaded()
|
|
{
|
|
base.ucLoaded();
|
|
OnId_MotorChanged(Id_Motor);
|
|
}
|
|
}
|
|
|
|
public partial class ucEncoderMotorLineal : UserControl, IDataContainer
|
|
{
|
|
public osBase? Datos { get; set; }
|
|
|
|
public ucEncoderMotorLineal()
|
|
{
|
|
InitializeComponent();
|
|
this.Loaded += OnLoaded;
|
|
this.Unloaded += OnUnloaded;
|
|
}
|
|
|
|
private void OnLoaded(object sender, RoutedEventArgs e)
|
|
{
|
|
Datos?.ucLoaded();
|
|
}
|
|
|
|
private void OnUnloaded(object sender, RoutedEventArgs e)
|
|
{
|
|
Datos?.ucUnLoaded();
|
|
}
|
|
|
|
public void Highlight(bool State) { }
|
|
|
|
public ZIndexEnum ZIndex()
|
|
{
|
|
return ZIndexEnum.Estaticos;
|
|
}
|
|
}
|
|
} |