Автозаряд и авторазряд will do well.

This commit is contained in:
ftc
2024-03-03 18:19:35 +03:00
parent acaba420d6
commit c93ec551a6
9 changed files with 432 additions and 48 deletions
+24
View File
@@ -0,0 +1,24 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
namespace charger_gui
{
class Logger
{
private string fileName;
public Logger(string fileName)
{
this.fileName = fileName;
}
public void Log(string s)
{
var toLog = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + " " + s + "\n";
File.AppendAllText(fileName, toLog);
}
}
}
+30 -7
View File
@@ -16,6 +16,7 @@
<Grid.RowDefinitions>
<RowDefinition Height="30"/>
<RowDefinition Height="3*"/>
<RowDefinition Height="*"/>
</Grid.RowDefinitions>
<TextBlock Grid.Row="0" Grid.Column="0" Text="Select Port" VerticalAlignment="Stretch" HorizontalAlignment="Stretch" FontSize="20"/>
<ComboBox Name="Ports" Grid.Row="0" Grid.Column="1" ItemsSource="{Binding Path=Ports}" SelectionChanged="Ports_SelectionChanged"/>
@@ -30,15 +31,37 @@
<RowDefinition Height="30"/>
<RowDefinition Height="30"/>
<RowDefinition Height="30"/>
<RowDefinition Height="30"/>
<RowDefinition Height="30"/>
<RowDefinition Height="30"/>
<RowDefinition Height="30"/>
<RowDefinition Height="30"/>
</Grid.RowDefinitions>
<TextBlock Grid.Row="0" Grid.Column="0" FontSize="20" Text="I_acc"/>
<TextBlock Grid.Row="0" Grid.Column="1" FontSize="20" Text="{Binding Path=IAcc,Mode=OneWay,UpdateSourceTrigger=PropertyChanged}" />
<TextBlock Grid.Row="1" Grid.Column="0" FontSize="20" Text="U_acc"/>
<TextBlock Grid.Row="1" Grid.Column="1" FontSize="20" Text="{Binding Path=UAcc,Mode=OneWay, UpdateSourceTrigger=PropertyChanged}" />
<TextBlock Grid.Row="2" Grid.Column="0" FontSize="20" Text="I_lamp"/>
<TextBlock Grid.Row="2" Grid.Column="1" FontSize="20" Text="{Binding Path=ILamp,Mode=OneWay,UpdateSourceTrigger=PropertyChanged}" />
<TextBlock Grid.Row="3" Grid.Column="0" FontSize="20" Text="U_lamp"/>
<TextBlock Grid.Row="3" Grid.Column="1" FontSize="20" Text="{Binding Path=ULamp,Mode=OneWay,UpdateSourceTrigger=PropertyChanged}" />
<TextBlock Grid.Row="0" Grid.Column="1" FontSize="20" Text="{Binding Path=IAcc,Mode=OneWay,StringFormat=F4}" />
<TextBlock Grid.Row="1" Grid.Column="0" FontSize="20" Text="U1_acc"/>
<TextBlock Grid.Row="1" Grid.Column="1" FontSize="20" Text="{Binding Path=U1Acc,Mode=OneWay,StringFormat=F4}" />
<TextBlock Grid.Row="2" Grid.Column="0" FontSize="20" Text="U2_acc"/>
<TextBlock Grid.Row="2" Grid.Column="1" FontSize="20" Text="{Binding Path=U2Acc,Mode=OneWay,StringFormat=F4}" />
<TextBlock Grid.Row="3" Grid.Column="0" FontSize="20" Text="I_lamp"/>
<TextBlock Grid.Row="3" Grid.Column="1" FontSize="20" Text="{Binding Path=ILamp,Mode=OneWay,StringFormat=F4}" />
<TextBlock Grid.Row="4" Grid.Column="0" FontSize="20" Text="U1_lamp"/>
<TextBlock Grid.Row="4" Grid.Column="1" FontSize="20" Text="{Binding Path=U1Lamp,Mode=OneWay,StringFormat=F4}" />
<TextBlock Grid.Row="5" Grid.Column="0" FontSize="20" Text="U2_lamp"/>
<TextBlock Grid.Row="5" Grid.Column="1" FontSize="20" Text="{Binding Path=U2Lamp,Mode=OneWay,StringFormat=F4}" />
<TextBlock Grid.Row="6" Grid.Column="0" FontSize="20" Text="CHARGE"/>
<Ellipse Grid.Row="6" Grid.Column="1" Width="20" Height="20" Fill="{Binding Path=ChargeStatus,Mode=OneWay,UpdateSourceTrigger=PropertyChanged}" />
<TextBlock Grid.Row="7" Grid.Column="0" FontSize="20" Text="LAMP"/>
<Ellipse Grid.Row="7" Grid.Column="1" Width="20" Height="20" Fill="{Binding Path=LampStatus,Mode=OneWay,UpdateSourceTrigger=PropertyChanged}" />
</Grid>
<StackPanel Grid.Row="1" Grid.Column="1">
<Button Content="Flip Lamp" Height="40" Click="LampButton_Click"/>
<Button Content="Flip Charge" Height="40" Click="FlipChargeButton_Click"/>
</StackPanel>
<StackPanel Grid.Row="1" Grid.Column="2">
<Button Content="Discharge" Height="40" Click="DischargeButton_Click"/>
<Button Content="Сharge" Height="40" Click="ChargeButton_Click"/>
</StackPanel>
<TextBlock Grid.Row="2" Grid.Column="0" Grid.ColumnSpan="4" Text="{Binding Path=OperationStatus,Mode=OneWay}" />
</Grid>
</Window>
+44 -15
View File
@@ -1,4 +1,5 @@
using System;
using charger_gui.operations;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
@@ -22,33 +23,61 @@ namespace charger_gui
public partial class MainWindow : Window
{
private UpdateWorker worker;
private DispatcherTimer updateTimer;
private Logger log;
MainWindowViewModel model;
public MainWindow()
{
InitializeComponent();
log = new Logger("charger.log");
model = new MainWindowViewModel();
DataContext = model;
worker = new UpdateWorker(model);
updateTimer = new DispatcherTimer();
updateTimer.Tick += OnTimer;
updateTimer.Interval = TimeSpan.FromMilliseconds(50);
updateTimer.Start();
}
private void OnTimer(object sender, EventArgs e)
{
model.OnPropertyChanged("IAcc");
model.OnPropertyChanged("UAcc");
model.OnPropertyChanged("ILamp");
model.OnPropertyChanged("ULamp");
model.OnPropertyChanged("ConnectionStatus");
}
private void Ports_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
worker.SetPort(Ports.SelectedItem.ToString());
}
private void LampButton_Click(object sender, RoutedEventArgs e)
{
if (model.CurrentOperation != null)
{
model.CurrentOperation.Cancel();
}
model.CurrentOperation = null;
model.IsLampOn = !model.IsLampOn;
}
private void FlipChargeButton_Click(object sender, RoutedEventArgs e)
{
if (model.CurrentOperation != null)
{
model.CurrentOperation.Cancel();
}
model.CurrentOperation = null;
model.IsCharging = !model.IsCharging;
}
private void DischargeButton_Click(object sender, RoutedEventArgs e)
{
if (model.CurrentOperation != null)
{
model.CurrentOperation.Cancel();
}
model.CurrentOperation = new DischargeOperation(log, model);
model.CurrentOperation.Start();
}
private void ChargeButton_Click(object sender, RoutedEventArgs e)
{
if (model.CurrentOperation != null)
{
model.CurrentOperation.Cancel();
}
model.CurrentOperation = new ChargeOperation(log, model);
model.CurrentOperation.Start();
}
}
}
+153 -18
View File
@@ -1,4 +1,5 @@
using System;
using charger_gui.operations;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.IO.Ports;
@@ -23,43 +24,102 @@ namespace charger_gui
}
}
public float iAcc;
public string IAcc
private float iAcc;
public float IAcc
{
get
{
return string.Format("{0,8:0.0000}", iAcc);
return iAcc;
}
set
{
iAcc = value;
OnPropertyChanged("IAcc");
}
}
public float uAcc;
public string UAcc
private float u1Acc;
public float U1Acc
{
get
{
return string.Format("{0,8:0.0000}", uAcc);
return u1Acc;
}
set
{
u1Acc = value;
OnPropertyChanged("U1Acc");
}
}
public float iLamp;
public string ILamp
private float u2Acc;
public float U2Acc
{
get
{
return string.Format("{0,8:0.0000}", iLamp);
return u2Acc;
}
set
{
u2Acc = value;
OnPropertyChanged("U2Acc");
}
}
public float uLamp;
public string ULamp
private float iLamp;
public float ILamp
{
get
{
return string.Format("{0,8:0.0000}", uLamp);
return iLamp;
}
set
{
iLamp = value;
OnPropertyChanged("ILamp");
}
}
public bool isConnected;
private float u1Lamp;
public float U1Lamp
{
get
{
return u1Lamp;
}
set
{
u1Lamp = value;
OnPropertyChanged("U1Lamp");
}
}
private float u2Lamp;
public float U2Lamp
{
get
{
return u2Lamp;
}
set
{
u2Lamp = value;
OnPropertyChanged("U2Lamp");
}
}
private bool isConnected;
public bool IsConnected
{
set
{
if (isConnected != value)
{
isConnected = value;
OnPropertyChanged("ConnectionStatus");
}
}
}
private Brush connectedBrush = new SolidColorBrush(Color.FromRgb(0, 255, 0));
private Brush notConnectedBrush = new SolidColorBrush(Color.FromRgb(255, 0, 0));
@@ -72,17 +132,92 @@ namespace charger_gui
}
}
private bool isCharging;
public bool IsCharging
{
get { return isCharging; }
set
{
if (isCharging != value)
{
isCharging = value;
OnPropertyChanged("ChargeStatus");
}
}
}
public Brush ChargeStatus
{
get
{
if (isCharging) return connectedBrush;
else return notConnectedBrush;
}
}
private bool isLampOn;
public bool IsLampOn
{
get { return isLampOn; }
set
{
if (isLampOn != value)
{
isLampOn = value;
OnPropertyChanged("LampStatus");
}
}
}
public Brush LampStatus
{
get
{
if (isLampOn) return connectedBrush;
else return notConnectedBrush;
}
}
private AutomaticOperation currentOperation;
public AutomaticOperation CurrentOperation
{
get
{
return currentOperation;
}
set
{
if (currentOperation != null)
{
currentOperation.StatusChanged -= OnCurrentOperationStatusChanged;
}
currentOperation = value;
currentOperation.StatusChanged += OnCurrentOperationStatusChanged;
OnPropertyChanged("OperationStatus");
}
}
private void OnCurrentOperationStatusChanged()
{
OnPropertyChanged("OperationStatus");
}
public string OperationStatus
{
get
{
if (currentOperation == null) return "MANUAL MODE";
return currentOperation.Status;
}
}
private void FillPorts()
{
ports = new List<string>(SerialPort.GetPortNames());
}
public event PropertyChangedEventHandler PropertyChanged;
public void OnPropertyChanged(string propertyName)
public void OnPropertyChanged(string name = null)
{
PropertyChangedEventHandler handler = PropertyChanged;
if (handler == null) return;
handler(this, new PropertyChangedEventArgs(propertyName));
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(name));
}
}
}
+16 -8
View File
@@ -11,6 +11,8 @@ namespace charger_gui
class UpdateWorker
{
const int SLAVE_ADDRESS = 1;
const int CHARGE_REGISTER = 4;
const int LAMP_REGISTER = 5;
private MainWindowViewModel model;
private Thread workingThread;
@@ -21,7 +23,7 @@ namespace charger_gui
isWorking = false;
shouldRestart = false;
workingThread = new Thread(Work);
model.isConnected = false;
model.IsConnected = false;
}
private bool isWorking;
@@ -47,6 +49,7 @@ namespace charger_gui
private void Work()
{
while (true)
{
try
@@ -57,6 +60,7 @@ namespace charger_gui
serialPort.DataBits = 8;
serialPort.Parity = Parity.None;
serialPort.StopBits = StopBits.One;
serialPort.ReadTimeout = 50;
serialPort.Open();
// create modbus master
@@ -64,19 +68,23 @@ namespace charger_gui
while (!shouldRestart)
{
var r1 = master.ReadHoldingRegisters(SLAVE_ADDRESS, 0, 4);
model.iAcc = ToFloat(r1[0], r1[1]);
model.iLamp = ToFloat(r1[2], r1[3]);
var r2 = master.ReadHoldingRegisters(SLAVE_ADDRESS, 6, 4);
model.uAcc = ToFloat(r2[0], r2[1]);
model.uLamp = ToFloat(r2[2], r2[3]);
model.IAcc = ToFloat(r1[0], r1[1]);
model.ILamp = ToFloat(r1[2], r1[3]);
var r2 = master.ReadHoldingRegisters(SLAVE_ADDRESS, 6, 8);
model.U1Acc = ToFloat(r2[0], r2[1]);
model.U2Acc = ToFloat(r2[2], r2[3]);
model.U1Lamp = ToFloat(r2[4], r2[5]);
model.U2Lamp = ToFloat(r2[6], r2[7]);
master.WriteSingleRegister(SLAVE_ADDRESS, CHARGE_REGISTER, (ushort)(model.IsCharging ? 1 : 0));
master.WriteSingleRegister(SLAVE_ADDRESS, LAMP_REGISTER, (ushort)(model.IsLampOn ? 1 : 0));
// ничего не упало, коннект есть значит
model.isConnected = true;
model.IsConnected = true;
}
}
}
catch (Exception e)
{
model.isConnected = false;
model.IsConnected = false;
Thread.Sleep(100);
// will continue
}
+4
View File
@@ -56,6 +56,10 @@
<Generator>MSBuild:Compile</Generator>
<SubType>Designer</SubType>
</ApplicationDefinition>
<Compile Include="Logger.cs" />
<Compile Include="operations\AutomaticOperation.cs" />
<Compile Include="operations\ChargeOperation.cs" />
<Compile Include="operations\DischargeOperation.cs" />
<Compile Include="UpdateWorker.cs" />
<Page Include="MainWindow.xaml">
<Generator>MSBuild:Compile</Generator>
@@ -0,0 +1,63 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
namespace charger_gui.operations
{
class AutomaticOperation
{
private Thread workingThread;
public bool IsComplete
{
get
{
return workingThread == null || workingThread.IsAlive;
}
}
public event Action StatusChanged;
public virtual string Status
{
get
{
return "UNKNOWN";
}
}
protected Logger log;
protected MainWindowViewModel model;
public AutomaticOperation(Logger log, MainWindowViewModel model)
{
this.log = log;
this.model = model;
}
public void Start()
{
workingThread = new Thread(Work);
workingThread.Start();
}
public void Cancel()
{
if (workingThread != null)
{
workingThread.Abort();
}
}
protected void OnStatusChanged()
{
StatusChanged?.Invoke();
}
protected virtual void Work()
{
}
}
}
+51
View File
@@ -0,0 +1,51 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
namespace charger_gui.operations
{
class ChargeOperation : AutomaticOperation
{
const float MAX_VOLTAGE = 14.5f;
private int passed;
private bool isComplete = false;
public override string Status
{
get
{
if (isComplete) return "Charging complete in " + passed + " seconds";
else return "Charging, passed " + passed + " seconds";
}
}
public ChargeOperation(Logger log, MainWindowViewModel model) : base(log, model)
{
}
protected override void Work()
{
log.Log("Charging started");
var start = DateTime.Now;
model.IsCharging = true;
model.IsLampOn = false;
Thread.Sleep(500);
while (model.U2Acc < MAX_VOLTAGE)
{
Thread.Sleep(1000);
passed = (int)((DateTime.Now - start).TotalSeconds);
OnStatusChanged();
log.Log("Charging " + passed + "s " + model.U2Acc.ToString("F4"));
if (model.U1Acc < 10f)
{
log.Log("Charging error: NO POWER. " + model.U1Acc.ToString("F4"));
}
}
log.Log("Charging complete in " + passed + " seconds");
isComplete = true;
OnStatusChanged();
}
}
}
@@ -0,0 +1,47 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
namespace charger_gui.operations
{
class DischargeOperation : AutomaticOperation
{
const float MIN_VOLTAGE = 10.8f;
private int passed;
private bool isComplete = false;
public override string Status
{
get
{
if (isComplete) return "Discharging complete in " + passed + " seconds";
else return "Discharging, passed " + passed + " seconds";
}
}
public DischargeOperation(Logger log, MainWindowViewModel model) : base(log, model)
{
}
protected override void Work()
{
log.Log("Discharging started");
var start = DateTime.Now;
model.IsCharging = false;
model.IsLampOn = true;
Thread.Sleep(500);
while (model.U2Acc > MIN_VOLTAGE)
{
Thread.Sleep(1000);
passed = (int)((DateTime.Now - start).TotalSeconds);
OnStatusChanged();
log.Log("Discharging " + passed + "s " + model.U2Acc.ToString("F4"));
}
log.Log("Discharging complete in " + passed + " seconds");
isComplete = true;
OnStatusChanged();
}
}
}