Mejorado osBase

This commit is contained in:
Miguel 2024-05-16 18:45:14 +02:00
parent f3e5992433
commit 77ff643642
16 changed files with 204 additions and 41 deletions

View File

@ -58,8 +58,8 @@
<Resource Include="Icons\save.png" /> <Resource Include="Icons\save.png" />
<Resource Include="Icons\start.png" /> <Resource Include="Icons\start.png" />
<Resource Include="Icons\stop.png" /> <Resource Include="Icons\stop.png" />
<Resource Include="motor2.png" /> <Resource Include="imagenes\motor2.png" />
<Resource Include="tank.png" /> <Resource Include="imagenes\tank.png" />
</ItemGroup> </ItemGroup>
</Project> </Project>

View File

@ -39,7 +39,9 @@ namespace CtrEditor
public ObservableCollection<TipoSimulable> ListaOsBase { get; } = new ObservableCollection<TipoSimulable>(); public ObservableCollection<TipoSimulable> ListaOsBase { get; } = new ObservableCollection<TipoSimulable>();
private ObservableCollection<osBase> _objetosSimulables = new ObservableCollection<osBase>(); private ObservableCollection<osBase> _objetosSimulables = new ObservableCollection<osBase>();
public PLCViewModel plcViewModelData; public PLCViewModel plcViewModelData;
public Stopwatch stopwatch; public Stopwatch stopwatch_PLCRefresh;
public Stopwatch stopwatch_SimRefresh;
private bool isSimulationRunning; private bool isSimulationRunning;
private bool isConnected; private bool isConnected;
@ -88,7 +90,8 @@ namespace CtrEditor
TBConnectPLCCommand = new RelayCommand(ConnectPLC, () => !IsConnected); TBConnectPLCCommand = new RelayCommand(ConnectPLC, () => !IsConnected);
TBDisconnectPLCCommand = new RelayCommand(DisconnectPLC, () => IsConnected); TBDisconnectPLCCommand = new RelayCommand(DisconnectPLC, () => IsConnected);
stopwatch = new Stopwatch(); stopwatch_PLCRefresh = new Stopwatch();
stopwatch_SimRefresh = new Stopwatch();
} }
public void LoadInitialData() public void LoadInitialData()
@ -172,6 +175,7 @@ namespace CtrEditor
_timerSimulacion.Start(); _timerSimulacion.Start();
simulationManager.stopwatch.Start(); simulationManager.stopwatch.Start();
stopwatch_SimRefresh.Start();
IsSimulationRunning = true; IsSimulationRunning = true;
} }
@ -179,11 +183,18 @@ namespace CtrEditor
{ {
_timerSimulacion.Stop(); _timerSimulacion.Stop();
simulationManager.stopwatch.Stop(); simulationManager.stopwatch.Stop();
stopwatch_SimRefresh.Stop();
IsSimulationRunning = false; IsSimulationRunning = false;
} }
private void OnTickSimulacion(object sender, EventArgs e) 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) foreach (var objetoSimulable in ObjetosSimulables)
objetoSimulable.UpdateGeometryStep(); objetoSimulable.UpdateGeometryStep();
@ -191,7 +202,7 @@ namespace CtrEditor
simulationManager.Step(); simulationManager.Step();
foreach (var objetoSimulable in ObjetosSimulables) foreach (var objetoSimulable in ObjetosSimulables)
objetoSimulable.UpdateControl(); objetoSimulable.UpdateControl((int)elapsedMilliseconds);
} }
@ -213,16 +224,16 @@ namespace CtrEditor
if (!isConnected) if (!isConnected)
IsConnected = true; IsConnected = true;
// Detener el cronómetro y obtener el tiempo transcurrido en milisegundos // Detener el cronómetro y obtener el tiempo transcurrido en milisegundos
stopwatch.Stop(); stopwatch_PLCRefresh.Stop();
float elapsedMilliseconds = (float)stopwatch.Elapsed.TotalMilliseconds; float elapsedMilliseconds = (float)stopwatch_PLCRefresh.Elapsed.TotalMilliseconds;
// Reiniciar el cronómetro para la próxima medición // Reiniciar el cronómetro para la próxima medición
stopwatch.Restart(); stopwatch_PLCRefresh.Restart();
foreach (var objetoSimulable in ObjetosSimulables) foreach (var objetoSimulable in ObjetosSimulables)
objetoSimulable.UpdatePLC(plcViewModelData.PLCInterface, (int) elapsedMilliseconds); objetoSimulable.UpdatePLC(plcViewModelData.PLCInterface, (int) elapsedMilliseconds);
} else } else
stopwatch.Stop(); stopwatch_PLCRefresh.Stop();
} }

View File

@ -121,7 +121,7 @@
</ToolBarTray> </ToolBarTray>
<ScrollViewer Grid.Row="1" x:Name="ImagenEnTrabajoScrollViewer" HorizontalScrollBarVisibility="Auto" VerticalScrollBarVisibility="Auto" PanningMode="Both"> <ScrollViewer Grid.Row="1" x:Name="ImagenEnTrabajoScrollViewer" HorizontalScrollBarVisibility="Auto" VerticalScrollBarVisibility="Auto" PanningMode="Both">
<Canvas x:Name="ImagenEnTrabajoCanvas" Margin="20"> <Canvas x:Name="ImagenEnTrabajoCanvas" Margin="200">
<!-- El Margin puede ser ajustado según el espacio adicional que quieras proporcionar --> <!-- El Margin puede ser ajustado según el espacio adicional que quieras proporcionar -->
<Canvas.LayoutTransform> <Canvas.LayoutTransform>
<ScaleTransform ScaleX="1" ScaleY="1"/> <ScaleTransform ScaleX="1" ScaleY="1"/>

View File

@ -17,6 +17,7 @@ using CtrEditor.Simulacion;
using System.Windows.Media; using System.Windows.Media;
using Microsoft.Xna.Framework; using Microsoft.Xna.Framework;
using FarseerPhysics.Dynamics; using FarseerPhysics.Dynamics;
using Siemens.Simatic.Simulation.Runtime;
namespace CtrEditor.ObjetosSim namespace CtrEditor.ObjetosSim
{ {
@ -27,7 +28,7 @@ namespace CtrEditor.ObjetosSim
{ {
string Nombre { get; } string Nombre { get; }
void UpdateControl(); void UpdateControl(int elapsedMilliseconds);
} }
public interface IDataContainer public interface IDataContainer
@ -51,7 +52,7 @@ namespace CtrEditor.ObjetosSim
public abstract string Nombre { get; set; } public abstract string Nombre { get; set; }
public abstract void UpdateControl(); public abstract void UpdateControl(int elapsedMilliseconds);
public abstract void UpdateGeometryStart(); public abstract void UpdateGeometryStart();
public abstract void UpdateGeometryStep(); public abstract void UpdateGeometryStep();
public abstract void UpdatePLC(PLCModel plc, int elapsedMilliseconds); public abstract void UpdatePLC(PLCModel plc, int elapsedMilliseconds);
@ -85,6 +86,51 @@ namespace CtrEditor.ObjetosSim
return null; 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) public void CanvasSetLeftinMeter(float left)
{ {

View File

@ -141,7 +141,7 @@ namespace CtrEditor.ObjetosSim
public override void UpdatePLC(PLCModel plc, int elapsedMilliseconds) { } public override void UpdatePLC(PLCModel plc, int elapsedMilliseconds) { }
public override void UpdateControl() public override void UpdateControl(int elapsedMilliseconds)
{ {
CenterX = Simulacion_Botella.CenterX; CenterX = Simulacion_Botella.CenterX;
CenterY = Simulacion_Botella.CenterY; CenterY = Simulacion_Botella.CenterY;

View File

@ -154,7 +154,7 @@ namespace CtrEditor.ObjetosSim
plc.EscribirTagBool(_tag, Estado); plc.EscribirTagBool(_tag, Estado);
} }
public override void UpdateControl() public override void UpdateControl(int elapsedMilliseconds)
{ {
} }

View File

@ -115,7 +115,7 @@ namespace CtrEditor.ObjetosSim
// Se llama antes de la simulacion // Se llama antes de la simulacion
ActualizarGeometrias(); ActualizarGeometrias();
} }
public override void UpdateControl() public override void UpdateControl(int elapsedMilliseconds)
{ {
} }
public override void UpdateGeometryStep() public override void UpdateGeometryStep()

View File

@ -158,15 +158,10 @@ namespace CtrEditor.ObjetosSim
} }
public override void UpdatePLC(PLCModel plc, int elapsedMilliseconds) public override void UpdatePLC(PLCModel plc, int elapsedMilliseconds)
{ {
if (Tag.Length > 0) EscribirWordTagScaled(plc, Tag, Value, 0, 100, Min_OUT_Scaled, Max_OUT_Scaled);
{
SDataValue s = new SDataValue();
s.UInt16 = (ushort)((Value / 100.0 * Max_OUT_Scaled) + Min_OUT_Scaled);
plc.EscribirTag(Tag, s);
}
} }
public override void UpdateControl() public override void UpdateControl(int elapsedMilliseconds)
{ {
} }

View File

@ -15,7 +15,7 @@
<Grid> <Grid>
<Image x:Name="TankImage" <Image x:Name="TankImage"
Source="/tank.png" Source="/imagenes/tank.png"
Width="{Binding Alto, Converter={StaticResource MeterToPixelConverter}}" Width="{Binding Alto, Converter={StaticResource MeterToPixelConverter}}"
Height="{Binding Ancho, Converter={StaticResource MeterToPixelConverter}}" Height="{Binding Ancho, Converter={StaticResource MeterToPixelConverter}}"
Stretch="Uniform"> Stretch="Uniform">

View File

@ -17,6 +17,7 @@ using System.Windows.Media;
using System.Windows.Media.Imaging; using System.Windows.Media.Imaging;
using System.Windows.Navigation; using System.Windows.Navigation;
using System.Windows.Shapes; using System.Windows.Shapes;
using static System.Windows.Forms.VisualStyles.VisualStyleElement;
namespace CtrEditor.ObjetosSim namespace CtrEditor.ObjetosSim
{ {
@ -34,23 +35,124 @@ namespace CtrEditor.ObjetosSim
private float _top; private float _top;
private float _angulo; private float _angulo;
private float _level; private float _level;
private string _tag; private string _tagNivel_Word;
private float _max_OUT_Scaled; private float _max_OUT_Scaled;
private float _min_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 set
{ {
if (_tag != value) if (_capacidadLitros != value)
{ {
_tag = value; _capacidadLitros = value;
OnPropertyChanged(nameof(Tag)); 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 public float Min_OUT_Scaled
{ {
get => _min_OUT_Scaled; get => _min_OUT_Scaled;
@ -160,19 +262,28 @@ namespace CtrEditor.ObjetosSim
public override void UpdateGeometryStep() public override void UpdateGeometryStep()
{ {
} }
public override void UpdatePLC(PLCModel plc, int elapsedMilliseconds) public override void UpdatePLC(PLCModel plc, int elapsedMilliseconds)
{ {
if (Tag.Length > 0) EscribirWordTagScaled(plc, TagNivel_Word, Level, 0, 100, Min_OUT_Scaled, Max_OUT_Scaled);
{ Ingreso_Abierto = LeerBitTag(plc, _tagIngresoAbierto_Bool);
SDataValue s = new SDataValue(); Salida_Abierta = LeerBitTag(plc, _tagSalidaAbierta_Bool);
s.UInt16 = (ushort)((Level / 100.0 * Max_OUT_Scaled) + Min_OUT_Scaled);
plc.EscribirTag(Tag, s);
}
} }
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() public override void ucLoaded()
{ {

View File

@ -193,7 +193,7 @@ namespace CtrEditor.ObjetosSim
public override void UpdateGeometryStep() public override void UpdateGeometryStep()
{ {
} }
public override void UpdateControl() public override void UpdateControl(int elapsedMilliseconds)
{ {
} }
public override void UpdatePLC(PLCModel plc, int elapsedMilliseconds) public override void UpdatePLC(PLCModel plc, int elapsedMilliseconds)

View File

@ -156,7 +156,7 @@ namespace CtrEditor.ObjetosSim
_osMotor = ObtenerLink(_motor, typeof(osVMmotorSim)); _osMotor = ObtenerLink(_motor, typeof(osVMmotorSim));
} }
public override void UpdateControl() public override void UpdateControl(int elapsedMilliseconds)
{ {
} }
public override void ucLoaded() public override void ucLoaded()

View File

@ -12,7 +12,7 @@
</UserControl.Resources> </UserControl.Resources>
<Grid> <Grid>
<Image Source="/motor2.png" <Image Source="/imagenes/motor2.png"
Width="{Binding Tamano, Converter={StaticResource MeterToPixelConverter}}" Width="{Binding Tamano, Converter={StaticResource MeterToPixelConverter}}"
Height="{Binding Tamano, Converter={StaticResource MeterToPixelConverter}}" Height="{Binding Tamano, Converter={StaticResource MeterToPixelConverter}}"
Stretch="Uniform"/> Stretch="Uniform"/>

View File

@ -236,7 +236,7 @@ namespace CtrEditor.ObjetosSim
return hzIncrementsRamp; return hzIncrementsRamp;
} }
public override void UpdateControl() public override void UpdateControl(int elapsedMilliseconds)
{ {
} }

View File

Before

Width:  |  Height:  |  Size: 18 KiB

After

Width:  |  Height:  |  Size: 18 KiB

View File

Before

Width:  |  Height:  |  Size: 2.5 KiB

After

Width:  |  Height:  |  Size: 2.5 KiB