agrobot_base/AgroBase/OperationControl/Windows/MainWindow.xaml.cs

157 lines
5.1 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

using System.Windows;
using ScottPlot; // Plot
using ScottPlot.Plottables; // ISignal / IScatter
using System.Timers;
using Colors = ScottPlot.Colors; // Timer
using System.Windows.Threading;
using OperationControl.Controls;
namespace OperationControl.Windows
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
// buffers (X = tempo em segundos)
private readonly List<double> xs = new();
private readonly List<double> ysCorr = new(); // Corrente (A)
private readonly List<double> ysVolt = new(); // Tensão (V)
private readonly List<double> ysTemp = new(); // Temperatura (°C)
private readonly List<double> ysRpm = new(); // RPM
private readonly System.Timers.Timer tmr = new(1000) { AutoReset = true }; // 10 Hz
private double t = 0;
private readonly Random rng = new();
DispatcherTimer _clock = new DispatcherTimer();
Random _rand = new Random();
public MainWindow()
{
InitializeComponent();
// Título / eixos
Plot.Plot.Title("MOV ET Corrente / Tensão / Temp / RPM (últimos 60s)");
Plot.Plot.Axes.Bottom.Label.Text = "Tempo (s)";
Plot.Plot.Axes.Left.Label.Text = "Unidades (A, V, °C, RPM)";
// inicializa com um ponto para cada série
PushSample(initial: true);
// legenda
Plot.Plot.ShowLegend(Alignment.UpperRight);
// loop
tmr.Elapsed += (_, __) => Dispatcher.Invoke(() => PushSample());
tmr.Start();
DataContext = this;
HDG.CourseHeading = 0;
_clock.Interval = TimeSpan.FromMilliseconds(150);
_clock.Tick += (s, e) =>
{
IMU.RollDeg += (_rand.NextDouble() - 0.5) * 2; // suavemente variando
IMU.PitchDeg += (_rand.NextDouble() - 0.5) * 2;
HDG.Heading += (_rand.NextDouble() - 0.5) * 2;
HDG.CourseHeading += (_rand.NextDouble() - 0.5) * 5;
// disparar notificação se estiver usando INotifyPropertyChanged
};
_clock.Start();
// atualizar as caixas
BBoxes3D.SetBboxes(new[]
{
new LivoxBboxModel{ cx=1.2, cy=0.5, cz=0.4, d=0.6, w=0.4, h=0.8 },
new LivoxBboxModel{ cx=3.0, cy=-0.7, cz=0.6, d=0.8, w=0.5, h=1.2 },
});
}
private void PushSample(bool initial = false)
{
// avança tempo
if (!initial) t += 1; // 10 Hz
xs.Add(t);
// ------- MOCK de dados plausíveis -------
// Corrente (A): 312 A com ruído
double corr = 8 + 2 * Math.Sin(t * 0.7) + (rng.NextDouble() - 0.5);
corr = Math.Clamp(corr, 0, 30);
// Tensão (V): 3642 V, ripple leve e queda lenta
double volt = 40.5 + 0.6 * Math.Sin(t * 0.15) - 0.002 * t;
volt = Math.Clamp(volt, 30, 42);
// Temperatura (°C): sobe devagar
double temp = 38 + 4 * Math.Sin(t * 0.05) + 0.01 * t;
temp = Math.Clamp(temp, 20, 85);
// RPM: 01500 com variação
double rpm = 1100 + 200 * Math.Sin(t * 0.9) + 80 * (rng.NextDouble() - 0.5);
rpm = Math.Clamp(rpm, 0, 2000);
ysCorr.Add(corr);
ysVolt.Add(volt);
ysTemp.Add(temp);
ysRpm.Add(rpm);
// mantém só últimos 60 s
while (xs.Count > 0 && xs[^1] - xs[0] > 60.0)
{
xs.RemoveAt(0);
ysCorr.RemoveAt(0);
ysVolt.RemoveAt(0);
ysTemp.RemoveAt(0);
ysRpm.RemoveAt(0);
}
// redesenha (v5: passe arrays diretamente)
Plot.Plot.Clear();
// Corrente (A)
var scCorr = Plot.Plot.Add.Scatter(xs.ToArray(), ysCorr.ToArray());
scCorr.Label = "Corrente (A)";
scCorr.Color = ScottPlot.Colors.Cyan;
scCorr.LineWidth = 2;
// Tensão (V)
var scVolt = Plot.Plot.Add.Scatter(xs.ToArray(), ysVolt.ToArray());
scVolt.Label = "Tensão (V)";
scVolt.Color = ScottPlot.Colors.Lime;
scVolt.LineWidth = 2;
// Temperatura (°C)
var scTemp = Plot.Plot.Add.Scatter(xs.ToArray(), ysTemp.ToArray());
scTemp.Label = "Temp (°C)";
scTemp.Color = ScottPlot.Colors.Magenta;
scTemp.LineWidth = 2;
// RPM
var scRpm = Plot.Plot.Add.Scatter(xs.ToArray(), ysRpm.ToArray());
scRpm.Label = "RPM";
scRpm.Color = ScottPlot.Colors.Orange;
scRpm.LineWidth = 2;
// janela de 60 s rolando
double xEnd = xs[^1];
double xStart = Math.Max(0, xEnd - 60.0);
Plot.Plot.Axes.SetLimitsX(xStart, xEnd);
Plot.Plot.Axes.AutoScaleY(); // ajusta Y automaticamente para caber tudo
Plot.Refresh();
}
}
}