1
0

Compare commits

...

2 Commits

Author SHA1 Message Date
f77501b559 ignore *.user files 2025-01-06 00:24:18 +01:00
407c3a1731 improve graph curves calculations 2025-01-06 00:21:12 +01:00
3 changed files with 58 additions and 59 deletions

3
.gitignore vendored
View File

@@ -3,4 +3,5 @@ obj/
/packages/ /packages/
riderModule.iml riderModule.iml
/_ReSharper.Caches/ /_ReSharper.Caches/
/.idea/ /.idea/
*.user

View File

@@ -1,4 +0,0 @@
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="Current" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<ItemGroup />
</Project>

View File

@@ -11,7 +11,7 @@ public partial class App : Form
private const int MinSampleSize = 50, MaxSampleSize = 600; private const int MinSampleSize = 50, MaxSampleSize = 600;
private const float MinTimeSpan = 5F, MaxTimeSpan = 60F; private const float MinTimeSpan = 5F, MaxTimeSpan = 60F;
private const double RefreshRateMs = 1000.0 / 60.0; private const int FramesPerSecond = 30;
private static readonly Color StatusGrayColor = Color.FromArgb(70, 70, 70), GreenColor = Color.FromArgb(0, 255, 0); private static readonly Color StatusGrayColor = Color.FromArgb(70, 70, 70), GreenColor = Color.FromArgb(0, 255, 0);
@@ -60,7 +60,7 @@ public partial class App : Form
InitializeComponent(); InitializeComponent();
var timer = new System.Timers.Timer(RefreshRateMs); var timer = new System.Timers.Timer(1000.0 / FramesPerSecond);
timer.SynchronizingObject = this; timer.SynchronizingObject = this;
timer.Elapsed += delegate { OnDrawTimer(); }; timer.Elapsed += delegate { OnDrawTimer(); };
_timer = timer; _timer = timer;
@@ -71,7 +71,7 @@ public partial class App : Form
if (disposing) if (disposing)
{ {
components?.Dispose(); components?.Dispose();
_timer.Stop(); _timer.Close();
_arduinoCom?.Dispose(); _arduinoCom?.Dispose();
_font12.Dispose(); _font12.Dispose();
_font14.Dispose(); _font14.Dispose();
@@ -123,33 +123,43 @@ public partial class App : Form
arduinoCom.AddSerialObserver(Listener.Create<Sample>(sample => _sampleQueue.Enqueue(sample))); arduinoCom.AddSerialObserver(Listener.Create<Sample>(sample => _sampleQueue.Enqueue(sample)));
} }
private PointF CalculateGraphPosition(Rectangle curveRect, int sampleIndex, int sampleSize, float value,
private PointF CalculateGraphPosition(Rectangle graphRect, float graphMargin, int i, int sampleSize, float value,
float minValue, float maxValue, long timestamp, long minTimestamp, long maxTimestamp) =>
CalculateGraphPosition(new RectangleF(graphRect.X, graphRect.Y, graphRect.Width, graphRect.Height), graphMargin,
i, sampleSize, value, minValue, maxValue, timestamp, minTimestamp, maxTimestamp);
private PointF CalculateGraphPosition(RectangleF graphRect, float graphMargin, int i, int sampleSize, float value,
float minValue, float maxValue, long timestamp, long minTimestamp, long maxTimestamp) float minValue, float maxValue, long timestamp, long minTimestamp, long maxTimestamp)
{ {
var x = _sampleOrTimeGraph var x = CalculateGraphPositionX(curveRect, sampleIndex, sampleSize, timestamp, minTimestamp, maxTimestamp);
? Utils.Map<float, float, float>(i, 0, sampleSize - 1, graphRect.Width, 0F) var y = CalculateGraphPositionY(curveRect, value, minValue, maxValue);
: Utils.Map<float, float, float>(timestamp, minTimestamp, maxTimestamp, 0F, graphRect.Width);
var y = minValue == maxValue
? graphRect.Height / 2F
: Utils.Map<float, float, float>(value, minValue, maxValue, graphRect.Height - graphMargin, graphMargin);
return new PointF(x, y); return new PointF(x, y);
} }
private float CalculateGraphPositionX(Rectangle curveRect, int sampleIndex, int sampleSize, long timestamp,
long minTimestamp, long maxTimestamp) => _sampleOrTimeGraph
? CalculateGraphPositionX(curveRect, sampleIndex, sampleSize)
: CalculateGraphPositionX(curveRect, timestamp, minTimestamp, maxTimestamp);
private static float CalculateGraphPositionX(Rectangle curveRect, int sampleIndex, int sampleSize) =>
Utils.Map<int, float, float>(sampleIndex, 1, sampleSize, curveRect.Right, curveRect.Left);
private static float CalculateGraphPositionX(Rectangle curveRect, long timestamp, long minTimestamp,
long maxTimestamp) =>
Utils.Map<long, float, float>(timestamp, minTimestamp, maxTimestamp, curveRect.Left, curveRect.Right);
private static float CalculateGraphPositionY(Rectangle curveRect, float value, float minValue, float maxValue) =>
minValue == maxValue
? (curveRect.Top + curveRect.Bottom - 1) / 2F
: Utils.Map<float, float, float>(value, minValue, maxValue, curveRect.Bottom - 1, curveRect.Top);
private void OnDrawTimer() private void OnDrawTimer()
{ {
Console.WriteLine("A");
while (_sampleQueue.Count > 0) while (_sampleQueue.Count > 0)
{ {
_samples.Add(_sampleQueue.Dequeue()); _samples.Add(_sampleQueue.Dequeue());
} }
_frameTimeStart = TimeProvider.System.GetTimestamp(); var frameTimeStart = TimeProvider.System.GetTimestamp();
Console.WriteLine(TimeProvider.System.GetElapsedTime(_frameTimeStart, frameTimeStart).TotalMilliseconds);
_frameTimeStart = frameTimeStart;
speedGaugeCanvas.Refresh(); speedGaugeCanvas.Refresh();
graphPanel.Refresh(); graphPanel.Refresh();
@@ -247,6 +257,9 @@ public partial class App : Form
brakeLabel.BackColor = sample is { BrakeFlag: true } ? Color.DarkRed : StatusGrayColor; brakeLabel.BackColor = sample is { BrakeFlag: true } ? Color.DarkRed : StatusGrayColor;
_lastDrawnSample = sample; _lastDrawnSample = sample;
Console.WriteLine(TimeProvider.System.GetElapsedTime(frameTimeStart).TotalMilliseconds);
Console.WriteLine("B");
} }
private void speedGaugePanel_Paint(object sender, PaintEventArgs e) private void speedGaugePanel_Paint(object sender, PaintEventArgs e)
@@ -488,15 +501,19 @@ public partial class App : Form
g.TranslateTransform(graphRect.Left, graphRect.Top); g.TranslateTransform(graphRect.Left, graphRect.Top);
const int graphMargin = 24; const int graphMargin = 24;
var curveRect = Rectangle.FromLTRB(0, graphMargin, graphRect.Width, graphRect.Height - graphMargin + 1);
ctx.Stroke(100); ctx.Stroke(100);
ctx.StrokeWeight(1); ctx.StrokeWeight(1);
ctx.SetDashes(5, 5); ctx.SetDashes(5, 5);
g.DrawLine(ctx.DrawPen, 0, graphMargin - 1, graphRect.Width, graphMargin - 1); g.DrawLine(ctx.DrawPen, 0, curveRect.Top - 1, graphRect.Width, curveRect.Top - 1);
g.DrawLine(ctx.DrawPen, 0, graphRect.Height - graphMargin + 1, graphRect.Width, g.DrawLine(ctx.DrawPen, 0, curveRect.Bottom, graphRect.Width,
graphRect.Height - graphMargin + 1); curveRect.Bottom);
// MARQUEURS DES SECONDES
ctx.SetDashes(3, 7); ctx.SetDashes(3, 7);
g.SetClip(curveRect);
if (_sampleOrTimeGraph) if (_sampleOrTimeGraph)
{ {
@@ -508,9 +525,9 @@ public partial class App : Form
if (sample.DrawTimeTick) if (sample.DrawTimeTick)
{ {
var tickX = Utils.Map<float, float, float>(i, 0, sampleSize, graphRect.Width, 0); var tickX = CalculateGraphPositionX(curveRect, i, sampleSize);
g.DrawLine(ctx.DrawPen, tickX, graphMargin, tickX, graphRect.Height - graphMargin); g.DrawLine(ctx.DrawPen, tickX, curveRect.Top, tickX, curveRect.Bottom - 1);
} }
} }
} }
@@ -533,8 +550,11 @@ public partial class App : Form
} }
} }
g.ResetClip();
ctx.ResetDashes(); ctx.ResetDashes();
// GRAPHES DES VALEURS
var points = new List<PointF>(); var points = new List<PointF>();
// ERREUR // ERREUR
@@ -573,14 +593,11 @@ public partial class App : Form
if (sampleCount > 0) if (sampleCount > 0)
{ {
g.SetClip(new RectangleF(0, graphMargin, graphRect.Width, graphRect.Height - 2 * graphMargin + 1)); g.SetClip(curveRect);
if (minValue <= 0 && maxValue >= 0) if (minValue <= 0 && maxValue >= 0)
{ {
var zero = minValue == maxValue var zero = CalculateGraphPositionY(curveRect, 0, minValue, maxValue);
? graphRect.Height / 2F
: Utils.Map<float, float, float>(0, minValue, maxValue, graphRect.Height - graphMargin,
graphMargin);
ctx.Stroke(63, 63, 127); ctx.Stroke(63, 63, 127);
ctx.StrokeWeight(1); ctx.StrokeWeight(1);
@@ -620,7 +637,7 @@ public partial class App : Form
continue; continue;
} }
var p = CalculateGraphPosition(graphRect, graphMargin, i, sampleSize, sample.Error, minValue, var p = CalculateGraphPosition(curveRect, i, sampleSize, sample.Error, minValue,
maxValue, sample.Timestamp, minTimestamp, maxTimestamp); maxValue, sample.Timestamp, minTimestamp, maxTimestamp);
points.Add(p); points.Add(p);
@@ -691,7 +708,7 @@ public partial class App : Form
if (sampleCount > 0) if (sampleCount > 0)
{ {
g.SetClip(new RectangleF(0, graphMargin, graphRect.Width, graphRect.Height - 2 * graphMargin + 1)); g.SetClip(curveRect);
ctx.Stroke(255, 255, 0); ctx.Stroke(255, 255, 0);
ctx.StrokeWeight(1); ctx.StrokeWeight(1);
@@ -724,7 +741,7 @@ public partial class App : Form
continue; continue;
} }
var p = CalculateGraphPosition(graphRect, graphMargin, i, sampleSize, sample.Integral, minValue, var p = CalculateGraphPosition(curveRect, i, sampleSize, sample.Integral, minValue,
maxValue, maxValue,
sample.Timestamp, minTimestamp, maxTimestamp); sample.Timestamp, minTimestamp, maxTimestamp);
@@ -796,7 +813,7 @@ public partial class App : Form
if (sampleCount > 0) if (sampleCount > 0)
{ {
g.SetClip(new RectangleF(0, graphMargin, graphRect.Width, graphRect.Height - 2 * graphMargin + 1)); g.SetClip(curveRect);
ctx.Stroke(255, 63, 63); ctx.Stroke(255, 63, 63);
ctx.StrokeWeight(1); ctx.StrokeWeight(1);
@@ -829,7 +846,7 @@ public partial class App : Form
continue; continue;
} }
var p = CalculateGraphPosition(graphRect, graphMargin, i, sampleSize, sample.Derivative, minValue, var p = CalculateGraphPosition(curveRect, i, sampleSize, sample.Derivative, minValue,
maxValue, maxValue,
sample.Timestamp, minTimestamp, maxTimestamp); sample.Timestamp, minTimestamp, maxTimestamp);
@@ -896,7 +913,7 @@ public partial class App : Form
if (sampleCount > 0) if (sampleCount > 0)
{ {
g.SetClip(new RectangleF(0, graphMargin, graphRect.Width, graphRect.Height - 2 * graphMargin + 1)); g.SetClip(curveRect);
// CONSIGNE // CONSIGNE
@@ -910,22 +927,7 @@ public partial class App : Form
{ {
i++; i++;
// if (!sample.Active) var p = CalculateGraphPosition(curveRect, i, sampleSize, sample.Consigne, minValue,
// {
// if (points.Count > 0)
// {
// if (points.Count > 1)
// {
// g.DrawLines(ctx.DrawPen, CollectionsMarshal.AsSpan(points));
// }
//
// points.Clear();
// }
//
// continue;
// }
var p = CalculateGraphPosition(graphRect, graphMargin, i, sampleSize, sample.Consigne, minValue,
maxValue, maxValue,
sample.Timestamp, minTimestamp, maxTimestamp); sample.Timestamp, minTimestamp, maxTimestamp);
@@ -955,7 +957,7 @@ public partial class App : Form
{ {
i++; i++;
var p = CalculateGraphPosition(graphRect, graphMargin, i, sampleSize, sample.Speed, minValue, var p = CalculateGraphPosition(curveRect, i, sampleSize, sample.Speed, minValue,
maxValue, maxValue,
sample.Timestamp, minTimestamp, maxTimestamp); sample.Timestamp, minTimestamp, maxTimestamp);
@@ -1027,7 +1029,7 @@ public partial class App : Form
if (sampleCount > 0) if (sampleCount > 0)
{ {
g.SetClip(new RectangleF(0, graphMargin, graphRect.Width, graphRect.Height - 2 * graphMargin + 1)); g.SetClip(curveRect);
ctx.Stroke(127, 255, 127); ctx.Stroke(127, 255, 127);
ctx.StrokeWeight(1); ctx.StrokeWeight(1);
@@ -1060,7 +1062,7 @@ public partial class App : Form
continue; continue;
} }
var p = CalculateGraphPosition(graphRect, graphMargin, i, sampleSize, sample.Acceleration, minValue, var p = CalculateGraphPosition(curveRect, i, sampleSize, sample.Acceleration, minValue,
maxValue, maxValue,
sample.Timestamp, minTimestamp, maxTimestamp); sample.Timestamp, minTimestamp, maxTimestamp);
@@ -1132,7 +1134,7 @@ public partial class App : Form
if (sampleCount > 0) if (sampleCount > 0)
{ {
g.SetClip(new RectangleF(0, graphMargin, graphRect.Width, graphRect.Height - 2 * graphMargin + 1)); g.SetClip(curveRect);
ctx.Stroke(255, 127, 255); ctx.Stroke(255, 127, 255);
ctx.StrokeWeight(1); ctx.StrokeWeight(1);
@@ -1158,7 +1160,7 @@ public partial class App : Form
continue; continue;
} }
var p = CalculateGraphPosition(graphRect, graphMargin, i, sampleSize, sample.Servo, minValue, var p = CalculateGraphPosition(curveRect, i, sampleSize, sample.Servo, minValue,
maxValue, maxValue,
sample.Timestamp, minTimestamp, maxTimestamp); sample.Timestamp, minTimestamp, maxTimestamp);