From 77ff643642ae054f3b2407c9ba4e99a678c33884 Mon Sep 17 00:00:00 2001 From: Miguel Date: Thu, 16 May 2024 18:45:14 +0200 Subject: [PATCH] Mejorado osBase --- CtrEditor.csproj | 4 +- MainViewModel.cs | 25 +++-- MainWindow.xaml | 2 +- ObjetosSim/osBase.cs | 50 +++++++++- ObjetosSim/ucBotella.xaml.cs | 2 +- ObjetosSim/ucBoton.xaml.cs | 2 +- ObjetosSim/ucGuia.xaml.cs | 2 +- ObjetosSim/ucSensTemperatura.xaml.cs | 9 +- ObjetosSim/ucTanque.xaml | 2 +- ObjetosSim/ucTanque.xaml.cs | 139 ++++++++++++++++++++++++--- ObjetosSim/ucTransporteGuias.xaml.cs | 2 +- ObjetosSim/ucTransporteTTop.xaml.cs | 2 +- ObjetosSim/ucVMmotorSim.xaml | 2 +- ObjetosSim/ucVMmotorSim.xaml.cs | 2 +- motor2.png => imagenes/motor2.png | Bin tank.png => imagenes/tank.png | Bin 16 files changed, 204 insertions(+), 41 deletions(-) rename motor2.png => imagenes/motor2.png (100%) rename tank.png => imagenes/tank.png (100%) diff --git a/CtrEditor.csproj b/CtrEditor.csproj index eab6b69..9a88f2c 100644 --- a/CtrEditor.csproj +++ b/CtrEditor.csproj @@ -58,8 +58,8 @@ - - + + diff --git a/MainViewModel.cs b/MainViewModel.cs index 9be5ac6..61d074b 100644 --- a/MainViewModel.cs +++ b/MainViewModel.cs @@ -39,7 +39,9 @@ namespace CtrEditor public ObservableCollection ListaOsBase { get; } = new ObservableCollection(); private ObservableCollection _objetosSimulables = new ObservableCollection(); public PLCViewModel plcViewModelData; - public Stopwatch stopwatch; + public Stopwatch stopwatch_PLCRefresh; + public Stopwatch stopwatch_SimRefresh; + private bool isSimulationRunning; private bool isConnected; @@ -88,7 +90,8 @@ namespace CtrEditor TBConnectPLCCommand = new RelayCommand(ConnectPLC, () => !IsConnected); TBDisconnectPLCCommand = new RelayCommand(DisconnectPLC, () => IsConnected); - stopwatch = new Stopwatch(); + stopwatch_PLCRefresh = new Stopwatch(); + stopwatch_SimRefresh = new Stopwatch(); } public void LoadInitialData() @@ -172,6 +175,7 @@ namespace CtrEditor _timerSimulacion.Start(); simulationManager.stopwatch.Start(); + stopwatch_SimRefresh.Start(); IsSimulationRunning = true; } @@ -179,11 +183,18 @@ namespace CtrEditor { _timerSimulacion.Stop(); simulationManager.stopwatch.Stop(); + stopwatch_SimRefresh.Stop(); IsSimulationRunning = false; } private void OnTickSimulacion(object sender, EventArgs e) { + // Detener el cronómetro y obtener el tiempo transcurrido en milisegundos + stopwatch_SimRefresh.Stop(); + float elapsedMilliseconds = (float)stopwatch_SimRefresh.Elapsed.TotalMilliseconds; + + // Reiniciar el cronómetro para la próxima medición + stopwatch_SimRefresh.Restart(); foreach (var objetoSimulable in ObjetosSimulables) objetoSimulable.UpdateGeometryStep(); @@ -191,7 +202,7 @@ namespace CtrEditor simulationManager.Step(); foreach (var objetoSimulable in ObjetosSimulables) - objetoSimulable.UpdateControl(); + objetoSimulable.UpdateControl((int)elapsedMilliseconds); } @@ -213,16 +224,16 @@ namespace CtrEditor if (!isConnected) IsConnected = true; // Detener el cronómetro y obtener el tiempo transcurrido en milisegundos - stopwatch.Stop(); - float elapsedMilliseconds = (float)stopwatch.Elapsed.TotalMilliseconds; + stopwatch_PLCRefresh.Stop(); + float elapsedMilliseconds = (float)stopwatch_PLCRefresh.Elapsed.TotalMilliseconds; // Reiniciar el cronómetro para la próxima medición - stopwatch.Restart(); + stopwatch_PLCRefresh.Restart(); foreach (var objetoSimulable in ObjetosSimulables) objetoSimulable.UpdatePLC(plcViewModelData.PLCInterface, (int) elapsedMilliseconds); } else - stopwatch.Stop(); + stopwatch_PLCRefresh.Stop(); } diff --git a/MainWindow.xaml b/MainWindow.xaml index d7a6a26..b65635a 100644 --- a/MainWindow.xaml +++ b/MainWindow.xaml @@ -121,7 +121,7 @@ - + diff --git a/ObjetosSim/osBase.cs b/ObjetosSim/osBase.cs index d89efec..624d1da 100644 --- a/ObjetosSim/osBase.cs +++ b/ObjetosSim/osBase.cs @@ -17,6 +17,7 @@ using CtrEditor.Simulacion; using System.Windows.Media; using Microsoft.Xna.Framework; using FarseerPhysics.Dynamics; +using Siemens.Simatic.Simulation.Runtime; namespace CtrEditor.ObjetosSim { @@ -27,7 +28,7 @@ namespace CtrEditor.ObjetosSim { string Nombre { get; } - void UpdateControl(); + void UpdateControl(int elapsedMilliseconds); } public interface IDataContainer @@ -51,7 +52,7 @@ namespace CtrEditor.ObjetosSim public abstract string Nombre { get; set; } - public abstract void UpdateControl(); + public abstract void UpdateControl(int elapsedMilliseconds); public abstract void UpdateGeometryStart(); public abstract void UpdateGeometryStep(); public abstract void UpdatePLC(PLCModel plc, int elapsedMilliseconds); @@ -85,6 +86,51 @@ namespace CtrEditor.ObjetosSim return null; } + public bool LeerBitTag(PLCModel plc, string Tag) + { + if (!string.IsNullOrEmpty(Tag)) + { + if (Tag=="1") return true; + else if (Tag=="0") return false; + if (plc != null) + return plc.LeerTagBool(Tag); + } + return false; + } + + public void EscribirBitTag(PLCModel plc, string Tag, bool Value) + { + if (!string.IsNullOrEmpty(Tag)) + if (plc != null) + plc.EscribirTagBool(Tag, Value); + } + + public void EscribirWordTagScaled(PLCModel plc, string Tag, float Value, float IN_scale_Min, float IN_scale_Max, float OUT_scale_Min, float OUT_scale_Max) + { + if (plc != null) + if (!string.IsNullOrEmpty(Tag)) + { + SDataValue plcData = new SDataValue(); + plcData.UInt16 = (ushort)(((Value - IN_scale_Min) / (IN_scale_Max - IN_scale_Min) * (OUT_scale_Max - OUT_scale_Min)) + OUT_scale_Min); + plc.EscribirTag(Tag, plcData); + } + } + + public float LeerWordTagScaled(PLCModel plc, string Tag, float IN_scale_Min, float IN_scale_Max, float OUT_scale_Min, float OUT_scale_Max) + { + if (!string.IsNullOrEmpty(Tag)) + { + if (float.TryParse(Tag, out float v)) + return v; + if (plc != null) + { + SDataValue plcData = plc.LeerTag(Tag); + float Value = plcData.UInt16; // WORD + return (((Value - OUT_scale_Min) / (OUT_scale_Max - OUT_scale_Min) * (IN_scale_Max - IN_scale_Min)) + IN_scale_Min); + } + } + return 0; + } public void CanvasSetLeftinMeter(float left) { diff --git a/ObjetosSim/ucBotella.xaml.cs b/ObjetosSim/ucBotella.xaml.cs index 3674a3e..25364f9 100644 --- a/ObjetosSim/ucBotella.xaml.cs +++ b/ObjetosSim/ucBotella.xaml.cs @@ -141,7 +141,7 @@ namespace CtrEditor.ObjetosSim public override void UpdatePLC(PLCModel plc, int elapsedMilliseconds) { } - public override void UpdateControl() + public override void UpdateControl(int elapsedMilliseconds) { CenterX = Simulacion_Botella.CenterX; CenterY = Simulacion_Botella.CenterY; diff --git a/ObjetosSim/ucBoton.xaml.cs b/ObjetosSim/ucBoton.xaml.cs index dfacf72..e8b43dd 100644 --- a/ObjetosSim/ucBoton.xaml.cs +++ b/ObjetosSim/ucBoton.xaml.cs @@ -154,7 +154,7 @@ namespace CtrEditor.ObjetosSim plc.EscribirTagBool(_tag, Estado); } - public override void UpdateControl() + public override void UpdateControl(int elapsedMilliseconds) { } diff --git a/ObjetosSim/ucGuia.xaml.cs b/ObjetosSim/ucGuia.xaml.cs index 37089d3..a53bf3f 100644 --- a/ObjetosSim/ucGuia.xaml.cs +++ b/ObjetosSim/ucGuia.xaml.cs @@ -115,7 +115,7 @@ namespace CtrEditor.ObjetosSim // Se llama antes de la simulacion ActualizarGeometrias(); } - public override void UpdateControl() + public override void UpdateControl(int elapsedMilliseconds) { } public override void UpdateGeometryStep() diff --git a/ObjetosSim/ucSensTemperatura.xaml.cs b/ObjetosSim/ucSensTemperatura.xaml.cs index d93f030..72c92e3 100644 --- a/ObjetosSim/ucSensTemperatura.xaml.cs +++ b/ObjetosSim/ucSensTemperatura.xaml.cs @@ -158,15 +158,10 @@ namespace CtrEditor.ObjetosSim } public override void UpdatePLC(PLCModel plc, int elapsedMilliseconds) { - if (Tag.Length > 0) - { - SDataValue s = new SDataValue(); - s.UInt16 = (ushort)((Value / 100.0 * Max_OUT_Scaled) + Min_OUT_Scaled); - plc.EscribirTag(Tag, s); - } + EscribirWordTagScaled(plc, Tag, Value, 0, 100, Min_OUT_Scaled, Max_OUT_Scaled); } - public override void UpdateControl() + public override void UpdateControl(int elapsedMilliseconds) { } diff --git a/ObjetosSim/ucTanque.xaml b/ObjetosSim/ucTanque.xaml index c50dc78..f8e0cd6 100644 --- a/ObjetosSim/ucTanque.xaml +++ b/ObjetosSim/ucTanque.xaml @@ -15,7 +15,7 @@ diff --git a/ObjetosSim/ucTanque.xaml.cs b/ObjetosSim/ucTanque.xaml.cs index 210039e..0bb4d24 100644 --- a/ObjetosSim/ucTanque.xaml.cs +++ b/ObjetosSim/ucTanque.xaml.cs @@ -17,6 +17,7 @@ using System.Windows.Media; using System.Windows.Media.Imaging; using System.Windows.Navigation; using System.Windows.Shapes; +using static System.Windows.Forms.VisualStyles.VisualStyleElement; namespace CtrEditor.ObjetosSim { @@ -34,23 +35,124 @@ namespace CtrEditor.ObjetosSim private float _top; private float _angulo; private float _level; - private string _tag; + private string _tagNivel_Word; private float _max_OUT_Scaled; private float _min_OUT_Scaled; + private float _velocidadIngreso; + private float _velocidadSalida; + private string _tagIngresoAbierto_Bool; + private string _tagSalidaAbierta_Bool; + private bool _IngresoAbierto_Bool; + private bool _SalidaAbierta_Bool; + private float _capacidadLitros; - public string Tag + + public float Capacidad_Litros { - get => _tag; + get => _capacidadLitros; set { - if (_tag != value) + if (_capacidadLitros != value) { - _tag = value; - OnPropertyChanged(nameof(Tag)); + _capacidadLitros = value; + OnPropertyChanged(nameof(Capacidad_Litros)); } } } + public bool Ingreso_Abierto + { + get => _IngresoAbierto_Bool; + set + { + if (_IngresoAbierto_Bool != value) + { + _IngresoAbierto_Bool = value; + OnPropertyChanged(nameof(Ingreso_Abierto)); + } + } + } + + public bool Salida_Abierta + { + get => _SalidaAbierta_Bool; + set + { + if (_SalidaAbierta_Bool != value) + { + _SalidaAbierta_Bool = value; + OnPropertyChanged(nameof(Salida_Abierta)); + } + } + } + + public string TagNivel_Word + { + get => _tagNivel_Word; + set + { + if (_tagNivel_Word != value) + { + _tagNivel_Word = value; + OnPropertyChanged(nameof(TagNivel_Word)); + } + } + } + + public string TagIngresoAbierto_Bool + { + get => _tagIngresoAbierto_Bool; + set + { + if (_tagIngresoAbierto_Bool != value) + { + _tagIngresoAbierto_Bool = value; + OnPropertyChanged(nameof(TagIngresoAbierto_Bool)); + } + } + } + + public string TagSalidaAbierta_Bool + { + get => _tagSalidaAbierta_Bool; + set + { + if (_tagSalidaAbierta_Bool != value) + { + _tagSalidaAbierta_Bool = value; + OnPropertyChanged(nameof(TagSalidaAbierta_Bool)); + } + } + } + + public float Velocidad_Ingreso + { + get => _velocidadIngreso; + set + { + if (_velocidadIngreso != value) + { + _velocidadIngreso = value; + OnPropertyChanged(nameof(Velocidad_Ingreso)); + } + } + } + + public float Velocidad_Salida + { + get => _velocidadSalida; + set + { + if (_velocidadSalida != value) + { + _velocidadSalida = value; + OnPropertyChanged(nameof(Velocidad_Salida)); + } + } + } + + + public float Min_OUT_Scaled { get => _min_OUT_Scaled; @@ -160,19 +262,28 @@ namespace CtrEditor.ObjetosSim public override void UpdateGeometryStep() { } + public override void UpdatePLC(PLCModel plc, int elapsedMilliseconds) { - if (Tag.Length > 0) - { - SDataValue s = new SDataValue(); - s.UInt16 = (ushort)((Level / 100.0 * Max_OUT_Scaled) + Min_OUT_Scaled); - plc.EscribirTag(Tag, s); - } + EscribirWordTagScaled(plc, TagNivel_Word, Level, 0, 100, Min_OUT_Scaled, Max_OUT_Scaled); + Ingreso_Abierto = LeerBitTag(plc, _tagIngresoAbierto_Bool); + Salida_Abierta = LeerBitTag(plc, _tagSalidaAbierta_Bool); } - public override void UpdateControl() + public override void UpdateControl(int elapsedMilliseconds) { - + if (Salida_Abierta && Level > 0) + { + var l_por_milisegundo = Velocidad_Salida / 60000.0f; + Level -= l_por_milisegundo * elapsedMilliseconds; + if (Level < 0) Level = 0; + } + if (Ingreso_Abierto && Level < 100) + { + var l_por_milisegundo = Velocidad_Ingreso / 60000.0f; + Level += l_por_milisegundo * elapsedMilliseconds; + if (Level > 100) Level = 100; + } } public override void ucLoaded() { diff --git a/ObjetosSim/ucTransporteGuias.xaml.cs b/ObjetosSim/ucTransporteGuias.xaml.cs index c2b2881..963b71b 100644 --- a/ObjetosSim/ucTransporteGuias.xaml.cs +++ b/ObjetosSim/ucTransporteGuias.xaml.cs @@ -193,7 +193,7 @@ namespace CtrEditor.ObjetosSim public override void UpdateGeometryStep() { } - public override void UpdateControl() + public override void UpdateControl(int elapsedMilliseconds) { } public override void UpdatePLC(PLCModel plc, int elapsedMilliseconds) diff --git a/ObjetosSim/ucTransporteTTop.xaml.cs b/ObjetosSim/ucTransporteTTop.xaml.cs index 219305b..0d802af 100644 --- a/ObjetosSim/ucTransporteTTop.xaml.cs +++ b/ObjetosSim/ucTransporteTTop.xaml.cs @@ -156,7 +156,7 @@ namespace CtrEditor.ObjetosSim _osMotor = ObtenerLink(_motor, typeof(osVMmotorSim)); } - public override void UpdateControl() + public override void UpdateControl(int elapsedMilliseconds) { } public override void ucLoaded() diff --git a/ObjetosSim/ucVMmotorSim.xaml b/ObjetosSim/ucVMmotorSim.xaml index c555554..0379f7e 100644 --- a/ObjetosSim/ucVMmotorSim.xaml +++ b/ObjetosSim/ucVMmotorSim.xaml @@ -12,7 +12,7 @@ - diff --git a/ObjetosSim/ucVMmotorSim.xaml.cs b/ObjetosSim/ucVMmotorSim.xaml.cs index 4d8e4c6..edad0f9 100644 --- a/ObjetosSim/ucVMmotorSim.xaml.cs +++ b/ObjetosSim/ucVMmotorSim.xaml.cs @@ -236,7 +236,7 @@ namespace CtrEditor.ObjetosSim return hzIncrementsRamp; } - public override void UpdateControl() + public override void UpdateControl(int elapsedMilliseconds) { } diff --git a/motor2.png b/imagenes/motor2.png similarity index 100% rename from motor2.png rename to imagenes/motor2.png diff --git a/tank.png b/imagenes/tank.png similarity index 100% rename from tank.png rename to imagenes/tank.png