275 lines
8.3 KiB
C#
275 lines
8.3 KiB
C#
using CtrEditor.Simulacion;
|
|
using LibS7Adv;
|
|
using System.Windows;
|
|
using System.Windows.Controls;
|
|
using System.Windows.Media;
|
|
using CommunityToolkit.Mvvm.ComponentModel;
|
|
using System.Diagnostics;
|
|
using CtrEditor.FuncionesBase;
|
|
using Xceed.Wpf.Toolkit.PropertyGrid.Attributes;
|
|
using JsonIgnoreAttribute = Newtonsoft.Json.JsonIgnoreAttribute;
|
|
using System.ComponentModel;
|
|
|
|
namespace CtrEditor.ObjetosSim
|
|
{
|
|
/// <summary>
|
|
/// Interaction logic for ucPhotocell.xaml
|
|
/// </summary>
|
|
public partial class osPhotocell : osBase, IosBase
|
|
{
|
|
private simBarrera Simulation_Photocell;
|
|
Stopwatch timer;
|
|
double timer_lastPositive;
|
|
double timer_lastNegative;
|
|
|
|
public static string NombreClase()
|
|
{
|
|
return "Fotocélula";
|
|
}
|
|
private string nombre = NombreClase();
|
|
|
|
[property: Category("Identificación")]
|
|
[property: Description("Nombre identificativo del objeto")]
|
|
[property: Name("Nombre")]
|
|
public override string Nombre
|
|
{
|
|
get => nombre;
|
|
set => SetProperty(ref nombre, value);
|
|
}
|
|
|
|
[ObservableProperty]
|
|
[property: Description("Color del sensor")]
|
|
[property: Category("Apariencia")]
|
|
[property: Name("Color")]
|
|
Brush color;
|
|
|
|
[ObservableProperty]
|
|
[property: Description("Indica si la luz está cortada por un objeto")]
|
|
[property: Category("Información")]
|
|
[property: Name("Luz Cortada")]
|
|
bool luzCortada;
|
|
|
|
partial void OnLuzCortadaChanged(bool value)
|
|
{
|
|
if (LuzCortada)
|
|
Color = Brushes.Blue;
|
|
else
|
|
Color = Brushes.Green;
|
|
if (Tipo_NC)
|
|
EscribirBitTag(TagPhotocell_OUT, !LuzCortada);
|
|
else
|
|
EscribirBitTag(TagPhotocell_OUT, LuzCortada);
|
|
if (Filter_Frecuency < 1)
|
|
{
|
|
Filter_Frecuency = 1;
|
|
Frecuency = 0;
|
|
}
|
|
|
|
if (!value)
|
|
{
|
|
Lenght_positive_pulse = (float)(timer.Elapsed.TotalMilliseconds - timer_lastPositive);
|
|
timer_lastNegative = timer.Elapsed.TotalMilliseconds;
|
|
}
|
|
else
|
|
{
|
|
Lenght_negative_pulse = (float)(timer.Elapsed.TotalMilliseconds - timer_lastNegative);
|
|
timer_lastPositive = timer.Elapsed.TotalMilliseconds;
|
|
Lenght_FP_to_FP = Lenght_positive_pulse + Lenght_negative_pulse;
|
|
Frecuency = (Frecuency * (filter_Frecuency - 1) + (1000 / Lenght_FP_to_FP)) / filter_Frecuency;
|
|
}
|
|
|
|
}
|
|
|
|
[ObservableProperty]
|
|
[property: Description("Ancho del haz de luz del sensor")]
|
|
[property: Category("Configuración")]
|
|
[property: Name("Ancho del Haz")]
|
|
float ancho_Haz_De_Luz;
|
|
|
|
[ObservableProperty]
|
|
[property: Description("Distancia al cuello de la botella")]
|
|
[property: Category("Información")]
|
|
[property: Name("Distancia al Cuello")]
|
|
float distancia_cuello;
|
|
|
|
[ObservableProperty]
|
|
[property: Description("Tipo de detección: cuello de botella o botella completa")]
|
|
[property: Category("Configuración")]
|
|
[property: Name("Detectar Cuello")]
|
|
bool detectarCuello;
|
|
|
|
partial void OnDetectarCuelloChanged(bool value)
|
|
{
|
|
if (Simulation_Photocell == null) return;
|
|
|
|
Simulation_Photocell.DetectNeck = value;
|
|
}
|
|
|
|
|
|
[ObservableProperty]
|
|
[property: Description("Filtro de frecuencia para la señal")]
|
|
[property: Category("Configuración")]
|
|
[property: Name("Filtro de Frecuencia")]
|
|
float filter_Frecuency;
|
|
|
|
partial void OnFilter_FrecuencyChanged(float value)
|
|
{
|
|
if (value <= 0)
|
|
Filter_Frecuency = 10;
|
|
}
|
|
|
|
[ObservableProperty]
|
|
[property: Category("Información")]
|
|
[property: Description("Frecuencia calculada")]
|
|
[property: Name("Frecuencia")]
|
|
float frecuency;
|
|
|
|
[ObservableProperty]
|
|
[property: Category("Información")]
|
|
[property: Description("Duración del pulso positivo")]
|
|
[property: Name("Duración Pulso Positivo")]
|
|
float lenght_positive_pulse;
|
|
|
|
[ObservableProperty]
|
|
[property: Category("Información")]
|
|
[property: Description("Duración del pulso negativo")]
|
|
[property: Name("Duración Pulso Negativo")]
|
|
float lenght_negative_pulse;
|
|
|
|
[ObservableProperty]
|
|
[property: Category("Información")]
|
|
[property: Description("Tiempo de flanco positivo a flanco positivo")]
|
|
[property: Name("Tiempo FP a FP")]
|
|
float lenght_FP_to_FP;
|
|
|
|
[ObservableProperty]
|
|
[property: Description("Tipo de contacto: Normalmente Cerrado")]
|
|
[property: Category("Configuración")]
|
|
[property: Name("Tipo NC")]
|
|
public bool tipo_NC;
|
|
|
|
partial void OnTipo_NCChanged(bool value)
|
|
{
|
|
OnLuzCortadaChanged(LuzCortada);
|
|
}
|
|
|
|
[ObservableProperty]
|
|
[property: Category("Enlace PLC")]
|
|
[property: Description("Tag de salida de la fotocélula")]
|
|
[property: Name("Tag de Salida")]
|
|
public string tagPhotocell_OUT;
|
|
|
|
|
|
public override void LeftChanged(float value)
|
|
{
|
|
ActualizarGeometrias();
|
|
}
|
|
|
|
public override void TopChanged(float value)
|
|
{
|
|
ActualizarGeometrias();
|
|
}
|
|
|
|
public override void AnguloChanged(float value)
|
|
{
|
|
ActualizarGeometrias();
|
|
}
|
|
|
|
public override void AltoChanged(float value)
|
|
{
|
|
ActualizarGeometrias();
|
|
}
|
|
|
|
public override void AnchoChanged(float value)
|
|
{
|
|
ActualizarGeometrias();
|
|
}
|
|
|
|
|
|
private void ActualizarGeometrias()
|
|
{
|
|
if (_visualRepresentation is ucPhotocell uc)
|
|
UpdateRectangle(Simulation_Photocell, uc.Photocell, Ancho_Haz_De_Luz, Ancho, Angulo);
|
|
}
|
|
|
|
public osPhotocell()
|
|
{
|
|
Ancho = 1;
|
|
Alto = 0.03f;
|
|
Ancho_Haz_De_Luz = 0.01f;
|
|
Frecuency = 0;
|
|
timer = new Stopwatch();
|
|
timer.Start();
|
|
}
|
|
|
|
public override void UpdateGeometryStart()
|
|
{
|
|
// Se llama antes de la simulacion
|
|
ActualizarGeometrias();
|
|
}
|
|
public override void UpdateControl(int elapsedMilliseconds)
|
|
{
|
|
Distancia_cuello = Simulation_Photocell.Distancia;
|
|
if (DetectarCuello)
|
|
LuzCortada = Simulation_Photocell.LuzCortadaNeck;
|
|
else
|
|
LuzCortada = Simulation_Photocell.LuzCortada > 0;
|
|
}
|
|
public override void UpdatePLCPrimerCiclo()
|
|
{
|
|
// Escribimos el valor actual al iniciar la conexion
|
|
// Esto es util para NC principalmente
|
|
|
|
OnLuzCortadaChanged(LuzCortada);
|
|
}
|
|
public override void UpdatePLC(PLCViewModel plc, int elapsedMilliseconds)
|
|
{
|
|
}
|
|
public override void ucLoaded()
|
|
{
|
|
// El UserControl ya se ha cargado y podemos obtener las coordenadas para
|
|
// crear el objeto de simulacion
|
|
base.ucLoaded();
|
|
|
|
if (_visualRepresentation is ucPhotocell uc)
|
|
Simulation_Photocell = AddBarrera(simulationManager, uc.Photocell, Alto, Ancho, Angulo, DetectarCuello);
|
|
}
|
|
public override void ucUnLoaded()
|
|
{
|
|
// El UserControl se esta eliminando
|
|
// eliminar el objeto de simulacion
|
|
simulationManager.Remove(Simulation_Photocell);
|
|
}
|
|
|
|
|
|
}
|
|
|
|
public partial class ucPhotocell : UserControl, IDataContainer
|
|
{
|
|
public osBase? Datos { get; set; }
|
|
public int zIndex_fromFrames { get; set; }
|
|
|
|
public ucPhotocell()
|
|
{
|
|
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_Base()
|
|
{
|
|
return ZIndexEnum.Fotocelula;
|
|
}
|
|
|
|
|
|
}
|
|
}
|