summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJacob Palecki <[email protected]>2020-08-12 20:39:53 -0700
committerJacob Palecki <[email protected]>2020-08-12 20:39:53 -0700
commit30e1391b224ae028f4476e06a07415a0285ac6c9 (patch)
treea70694de4ca5e8e9f947ce6a1355c7a134d491b8
parentFactor accel calculations into calculation classes (diff)
downloadrawaccel-30e1391b224ae028f4476e06a07415a0285ac6c9.tar.xz
rawaccel-30e1391b224ae028f4476e06a07415a0285ac6c9.zip
Almost working
-rw-r--r--grapher/Form1.cs22
-rw-r--r--grapher/Models/Calculations/AccelCalculator.cs71
-rw-r--r--grapher/Models/Calculations/AccelChartData.cs31
-rw-r--r--grapher/Models/Calculations/AccelData.cs19
-rw-r--r--grapher/Models/Charts/AccelCharts.cs59
-rw-r--r--grapher/Models/Charts/ChartXY.cs6
-rw-r--r--grapher/Models/Fields/FieldXY.cs7
-rw-r--r--grapher/Models/Options/OptionXY.cs11
-rw-r--r--grapher/grapher.csproj1
9 files changed, 175 insertions, 52 deletions
diff --git a/grapher/Form1.cs b/grapher/Form1.cs
index da9bd5f..a90cfbc 100644
--- a/grapher/Form1.cs
+++ b/grapher/Form1.cs
@@ -89,10 +89,19 @@ namespace grapher
Marshal.FreeHGlobal(args_ptr);
- var sensitivity = new OptionXY(sensitivityBoxX, sensitivityBoxY, sensXYLock, this, 1, sensitivityLabel, "Sensitivity");
+ var accelCharts = new AccelCharts(
+ this,
+ new ChartXY(AccelerationChart, AccelerationChartY),
+ new ChartXY(VelocityChart, VelocityChartY),
+ new ChartXY(GainChart, GainChartY),
+ showVelocityGainToolStripMenuItem,
+ new CheckBox[] { sensXYLock, weightXYLock, capXYLock });
+
+
+ var sensitivity = new OptionXY(sensitivityBoxX, sensitivityBoxY, sensXYLock, this, 1, sensitivityLabel, "Sensitivity", accelCharts);
var rotation = new Option(rotationBox, this, 0, rotationLabel, "Rotation");
- var weight = new OptionXY(weightBoxFirst, weightBoxSecond, weightXYLock, this, 1, weightLabel, "Weight");
- var cap = new OptionXY(capBoxX, capBoxY, capXYLock, this, 0, capLabel, "Cap");
+ var weight = new OptionXY(weightBoxFirst, weightBoxSecond, weightXYLock, this, 1, weightLabel, "Weight", accelCharts);
+ var cap = new OptionXY(capBoxX, capBoxY, capXYLock, this, 0, capLabel, "Cap", accelCharts);
var offset = new Option(offsetBox, this, 0, offsetLabel, "Offset");
// The name and layout of these options is handled by AccelerationOptions object.
@@ -124,12 +133,7 @@ namespace grapher
AccelGUI = new AccelGUI(
this,
- new AccelCharts(
- this,
- new ChartXY(AccelerationChart, AccelerationChartY),
- new ChartXY(VelocityChart, VelocityChartY),
- new ChartXY(GainChart, GainChartY),
- showVelocityGainToolStripMenuItem),
+ accelCharts,
managedAcceleration,
accelerationOptions,
sensitivity,
diff --git a/grapher/Models/Calculations/AccelCalculator.cs b/grapher/Models/Calculations/AccelCalculator.cs
index 2d8d892..a768ee9 100644
--- a/grapher/Models/Calculations/AccelCalculator.cs
+++ b/grapher/Models/Calculations/AccelCalculator.cs
@@ -9,6 +9,9 @@ namespace grapher.Models.Calculations
{
public static class AccelCalculator
{
+ public const int MaxCombined = 100;
+ public const int MaxXY = 150;
+
public struct MagnitudeData
{
public double magnitude;
@@ -16,51 +19,59 @@ namespace grapher.Models.Calculations
public int y;
}
- public static ReadOnlyCollection<MagnitudeData> Magnitudes = GetMagnitudes();
+ public static ReadOnlyCollection<MagnitudeData> MagnitudesCombined = GetMagnitudes();
+ public static ReadOnlyCollection<MagnitudeData> MagnitudesX = GetMagnitudesX();
+ public static ReadOnlyCollection<MagnitudeData> MagnitudesY = GetMagnitudesY();
public static void Calculate(AccelData data, ManagedAccel accel, double starter)
{
data.Clear();
+ Calculate(data.Combined, accel, starter, MagnitudesCombined);
+ Calculate(data.X, accel, starter, MagnitudesX);
+ Calculate(data.Y, accel, starter, MagnitudesY);
+ }
+
+ public static void Calculate(AccelChartData data, ManagedAccel accel, double starter, ICollection<MagnitudeData> magnitudeData)
+ {
double lastInputMagnitude = 0;
double lastOutputMagnitude = 0;
- foreach (var magnitudeData in Magnitudes)
+ foreach (var magnitudeDatum in MagnitudesCombined)
{
- var output = accel.Accelerate(magnitudeData.x, magnitudeData.y, 1);
+ var output = accel.Accelerate(magnitudeDatum.x, magnitudeDatum.y, 1);
var outMagnitude = Magnitude(output.Item1, output.Item2);
- var ratio = magnitudeData.magnitude > 0 ? outMagnitude / magnitudeData.magnitude : starter;
+ var ratio = magnitudeDatum.magnitude > 0 ? outMagnitude / magnitudeDatum.magnitude : starter;
- var inDiff = magnitudeData.magnitude - lastInputMagnitude;
+ var inDiff = magnitudeDatum.magnitude - lastInputMagnitude;
var outDiff = outMagnitude - lastOutputMagnitude;
var slope = inDiff > 0 ? outDiff / inDiff : starter;
- if (!data.OrderedAccelPoints.ContainsKey(magnitudeData.magnitude))
+ if (!data.AccelPoints.ContainsKey(magnitudeDatum.magnitude))
{
- data.OrderedAccelPoints.Add(magnitudeData.magnitude, ratio);
+ data.AccelPoints.Add(magnitudeDatum.magnitude, ratio);
}
- if (!data.OrderedVelocityPoints.ContainsKey(magnitudeData.magnitude))
+ if (!data.VelocityPoints.ContainsKey(magnitudeDatum.magnitude))
{
- data.OrderedVelocityPoints.Add(magnitudeData.magnitude, outMagnitude);
+ data.VelocityPoints.Add(magnitudeDatum.magnitude, outMagnitude);
}
- if (!data.OrderedGainPoints.ContainsKey(magnitudeData.magnitude))
+ if (!data.GainPoints.ContainsKey(magnitudeDatum.magnitude))
{
- data.OrderedGainPoints.Add(magnitudeData.magnitude, slope);
+ data.GainPoints.Add(magnitudeDatum.magnitude, slope);
}
- lastInputMagnitude = magnitudeData.magnitude;
+ lastInputMagnitude = magnitudeDatum.magnitude;
lastOutputMagnitude = outMagnitude;
}
-
}
public static ReadOnlyCollection<MagnitudeData> GetMagnitudes()
{
var magnitudes = new List<MagnitudeData>();
- for (int i = 0; i < 100; i++)
+ for (int i = 0; i < MaxCombined; i++)
{
for (int j = 0; j <= i; j++)
{
@@ -77,6 +88,38 @@ namespace grapher.Models.Calculations
return magnitudes.AsReadOnly();
}
+ public static ReadOnlyCollection<MagnitudeData> GetMagnitudesX()
+ {
+ var magnitudes = new List<MagnitudeData>();
+
+ for (int i = 0; i < MaxXY; i++)
+ {
+ MagnitudeData magnitudeData;
+ magnitudeData.magnitude = i;
+ magnitudeData.x = i;
+ magnitudeData.y = 0;
+ magnitudes.Add(magnitudeData);
+ }
+
+ return magnitudes.AsReadOnly();
+ }
+
+ public static ReadOnlyCollection<MagnitudeData> GetMagnitudesY()
+ {
+ var magnitudes = new List<MagnitudeData>();
+
+ for (int i = 0; i < MaxXY; i++)
+ {
+ MagnitudeData magnitudeData;
+ magnitudeData.magnitude = i;
+ magnitudeData.x = 0;
+ magnitudeData.y = i;
+ magnitudes.Add(magnitudeData);
+ }
+
+ return magnitudes.AsReadOnly();
+ }
+
public static double Magnitude(int x, int y)
{
return Math.Sqrt(x * x + y * y);
diff --git a/grapher/Models/Calculations/AccelChartData.cs b/grapher/Models/Calculations/AccelChartData.cs
new file mode 100644
index 0000000..243827a
--- /dev/null
+++ b/grapher/Models/Calculations/AccelChartData.cs
@@ -0,0 +1,31 @@
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Text;
+using System.Threading.Tasks;
+
+namespace grapher.Models.Calculations
+{
+ public class AccelChartData
+ {
+ public AccelChartData()
+ {
+ AccelPoints = new SortedDictionary<double, double>();
+ VelocityPoints = new SortedDictionary<double, double>();
+ GainPoints = new SortedDictionary<double, double>();
+ }
+
+ public SortedDictionary<double, double> AccelPoints { get; }
+
+ public SortedDictionary<double, double> VelocityPoints { get; }
+
+ public SortedDictionary<double, double> GainPoints { get; }
+
+ public void Clear()
+ {
+ AccelPoints.Clear();
+ VelocityPoints.Clear();
+ GainPoints.Clear();
+ }
+ }
+}
diff --git a/grapher/Models/Calculations/AccelData.cs b/grapher/Models/Calculations/AccelData.cs
index d304660..a2a6a3a 100644
--- a/grapher/Models/Calculations/AccelData.cs
+++ b/grapher/Models/Calculations/AccelData.cs
@@ -10,23 +10,22 @@ namespace grapher.Models.Calculations
{
public AccelData()
{
- OrderedAccelPoints = new SortedDictionary<double, double>();
- OrderedVelocityPoints = new SortedDictionary<double, double>();
- OrderedGainPoints = new SortedDictionary<double, double>();
+ Combined = new AccelChartData();
+ X = new AccelChartData();
+ Y = new AccelChartData();
}
- public SortedDictionary<double, double> OrderedAccelPoints { get; }
+ public AccelChartData Combined { get; }
- public SortedDictionary<double, double> OrderedVelocityPoints { get; }
+ public AccelChartData X { get; }
- public SortedDictionary<double, double> OrderedGainPoints { get; }
+ public AccelChartData Y { get; }
public void Clear()
{
- OrderedAccelPoints.Clear();
- OrderedVelocityPoints.Clear();
- OrderedGainPoints.Clear();
+ Combined.Clear();
+ X.Clear();
+ Y.Clear();
}
-
}
}
diff --git a/grapher/Models/Charts/AccelCharts.cs b/grapher/Models/Charts/AccelCharts.cs
index 4ed44c7..0728abc 100644
--- a/grapher/Models/Charts/AccelCharts.cs
+++ b/grapher/Models/Charts/AccelCharts.cs
@@ -22,13 +22,15 @@ namespace grapher
ChartXY sensitivityChart,
ChartXY velocityChart,
ChartXY gainChart,
- ToolStripMenuItem enableVelocityAndGain)
+ ToolStripMenuItem enableVelocityAndGain,
+ ICollection<CheckBox> checkBoxesXY)
{
ContaingForm = form;
SensitivityChart = sensitivityChart;
VelocityChart = velocityChart;
GainChart = gainChart;
EnableVelocityAndGain = enableVelocityAndGain;
+ CheckBoxesXY = checkBoxesXY;
SensitivityChart.SetTop(0);
VelocityChart.SetHeight(SensitivityChart.Height);
@@ -56,15 +58,38 @@ namespace grapher
public ToolStripMenuItem EnableVelocityAndGain { get; }
+ private ICollection<CheckBox> CheckBoxesXY { get; }
+
private bool Combined { get; set; }
private int FormBorderHeight { get; }
public void Bind(AccelData data)
{
- SensitivityChart.Bind(data.OrderedAccelPoints);
- VelocityChart.Bind(data.OrderedVelocityPoints);
- GainChart.Bind(data.OrderedGainPoints);
+ if (Combined)
+ {
+ SensitivityChart.Bind(data.Combined.AccelPoints);
+ VelocityChart.Bind(data.Combined.VelocityPoints);
+ GainChart.Bind(data.Combined.GainPoints);
+ }
+ else
+ {
+ SensitivityChart.BindXY(data.X.AccelPoints, data.Y.AccelPoints);
+ VelocityChart.BindXY(data.X.VelocityPoints, data.Y.VelocityPoints);
+ GainChart.BindXY(data.X.GainPoints, data.Y.GainPoints);
+ }
+ }
+
+ public void RefreshXY()
+ {
+ if (CheckBoxesXY.All(box => box.Checked))
+ {
+ ShowCombined();
+ }
+ else
+ {
+ ShowXandYSeparate();
+ }
}
private void OnEnableClick(object sender, EventArgs e)
@@ -105,20 +130,26 @@ namespace grapher
private void ShowXandYSeparate()
{
- SensitivityChart.SetSeparate();
- VelocityChart.SetSeparate();
- GainChart.SetSeparate();
- UpdateFormWidth();
- Combined = false;
+ if (Combined)
+ {
+ SensitivityChart.SetSeparate();
+ VelocityChart.SetSeparate();
+ GainChart.SetSeparate();
+ UpdateFormWidth();
+ Combined = false;
+ }
}
private void ShowCombined()
{
- SensitivityChart.SetCombined();
- VelocityChart.SetCombined();
- GainChart.SetCombined();
- UpdateFormWidth();
- Combined = true;
+ if (!Combined)
+ {
+ SensitivityChart.SetCombined();
+ VelocityChart.SetCombined();
+ GainChart.SetCombined();
+ UpdateFormWidth();
+ Combined = true;
+ }
}
private void UpdateFormWidth()
diff --git a/grapher/Models/Charts/ChartXY.cs b/grapher/Models/Charts/ChartXY.cs
index a57801e..7bb7ac8 100644
--- a/grapher/Models/Charts/ChartXY.cs
+++ b/grapher/Models/Charts/ChartXY.cs
@@ -95,6 +95,12 @@ namespace grapher
ChartX.Series[0].Points.DataBindXY(data.Keys, data.Values);
}
+ public void BindXY(IDictionary dataX, IDictionary dataY)
+ {
+ ChartX.Series[0].Points.DataBindXY(dataX.Keys, dataX.Values);
+ ChartY.Series[0].Points.DataBindXY(dataY.Keys, dataY.Values);
+ }
+
public void SetCombined()
{
if (!Combined)
diff --git a/grapher/Models/Fields/FieldXY.cs b/grapher/Models/Fields/FieldXY.cs
index 87e0b9c..609af9d 100644
--- a/grapher/Models/Fields/FieldXY.cs
+++ b/grapher/Models/Fields/FieldXY.cs
@@ -13,13 +13,14 @@ namespace grapher
public const string ShortenedFormatString = "0.###";
- public FieldXY(TextBox xBox, TextBox yBox, CheckBox lockCheckBox, Form containingForm, double defaultData)
+ public FieldXY(TextBox xBox, TextBox yBox, CheckBox lockCheckBox, Form containingForm, double defaultData, AccelCharts accelCharts)
{
XField = new Field(xBox, containingForm, defaultData);
YField = new Field(yBox, containingForm, defaultData);
YField.FormatString = ShortenedFormatString;
LockCheckBox = lockCheckBox;
LockCheckBox.CheckedChanged += new System.EventHandler(CheckChanged);
+ AccelCharts = accelCharts;
XField.Box.Width = (YField.Box.Left + YField.Box.Width - XField.Box.Left - DefaultSeparation) / 2;
YField.Box.Width = XField.Box.Width;
@@ -58,6 +59,8 @@ namespace grapher
public Field YField { get; }
+ private AccelCharts AccelCharts { get; }
+
private bool Combined { get; set; }
private int DefaultWidthX { get; }
@@ -76,6 +79,8 @@ namespace grapher
{
SetSeparate();
}
+
+ AccelCharts.RefreshXY();
}
public void SetCombined()
diff --git a/grapher/Models/Options/OptionXY.cs b/grapher/Models/Options/OptionXY.cs
index ca1559d..90a46d7 100644
--- a/grapher/Models/Options/OptionXY.cs
+++ b/grapher/Models/Options/OptionXY.cs
@@ -21,8 +21,9 @@ namespace grapher
CheckBox lockCheckBox,
Form containingForm,
double defaultData,
- Label label)
- : this(new FieldXY(xBox, yBox, lockCheckBox, containingForm, defaultData), label)
+ Label label,
+ AccelCharts accelCharts)
+ : this(new FieldXY(xBox, yBox, lockCheckBox, containingForm, defaultData, accelCharts), label)
{
}
@@ -33,14 +34,16 @@ namespace grapher
Form containingForm,
double defaultData,
Label label,
- string startingName):
+ string startingName,
+ AccelCharts accelCharts):
this(
xBox,
yBox,
lockCheckBox,
containingForm,
defaultData,
- label)
+ label,
+ accelCharts)
{
SetName(startingName);
}
diff --git a/grapher/grapher.csproj b/grapher/grapher.csproj
index cc36a5e..6ce0fe4 100644
--- a/grapher/grapher.csproj
+++ b/grapher/grapher.csproj
@@ -48,6 +48,7 @@
</ItemGroup>
<ItemGroup>
<Compile Include="Models\Calculations\AccelCalculator.cs" />
+ <Compile Include="Models\Calculations\AccelChartData.cs" />
<Compile Include="Models\Calculations\AccelData.cs" />
<Compile Include="Models\Charts\AccelCharts.cs" />
<Compile Include="Models\AccelGUI.cs" />