-
Notifications
You must be signed in to change notification settings - Fork 0
/
Simulator.cs
176 lines (138 loc) · 4.94 KB
/
Simulator.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
using System;
namespace AutoPolarAlign
{
public class Simulator : IPolarAlignmentSolver, IPolarAlignmentMount
{
private Vec2 _trueAlignmentOffset;
public Vec2 TrueAlignmentOffset
{
get => _trueAlignmentOffset;
set
{
_trueAlignmentOffset = value;
}
}
private Vec2? _alignmentOffset;
public Vec2 AlignmentOffset
{
get
{
if (!_alignmentOffset.HasValue)
{
throw new Exception("Not yet solved");
}
return _alignmentOffset.Value;
}
private set => _alignmentOffset = value;
}
private readonly Random rng;
private readonly double offsetJitter;
private readonly double moveJitter;
private readonly double altBacklash;
private readonly double azBacklash;
public double ActualAltBacklash { get; private set; }
public double ActualAzBacklash { get; private set; }
public Vec2 TrueAltAxis { get; }
public Vec2 TrueAzAxis { get; }
public double StartSolveSuccessChance { get; set; } = 0.0;
public double EndSolveSuccessChance { get; set; } = 0.95;
public int WarmupSolveIterations { get; set; } = 5000;
private bool connected;
private int currentSolveIteration = 0;
public Simulator(Random rng, Vec2 initialAlignmentOffset, Vec2 altAxis, Vec2 azAxis, double offsetJitter, double moveJitter, double altBacklash, double azBacklash, double initialAltBacklash, double initialAzBacklash)
{
this.rng = rng;
this.offsetJitter = offsetJitter;
this.moveJitter = moveJitter;
this.altBacklash = altBacklash;
this.azBacklash = azBacklash;
ActualAltBacklash = initialAltBacklash;
ActualAzBacklash = initialAzBacklash;
TrueAltAxis = altAxis;
TrueAzAxis = azAxis;
TrueAlignmentOffset = initialAlignmentOffset;
}
public void Connect()
{
connected = true;
}
public void Disconnect()
{
connected = false;
}
public void Dispose()
{
Disconnect();
}
private void CheckConnected()
{
if (!connected)
{
throw new Exception("Not connected");
}
}
public bool Solve(bool repeatUntilSuccess)
{
CheckConnected();
currentSolveIteration++;
if (!repeatUntilSuccess && WarmupSolveIterations > 0)
{
double successChance = StartSolveSuccessChance + (EndSolveSuccessChance - StartSolveSuccessChance) / WarmupSolveIterations * Math.Min(WarmupSolveIterations, currentSolveIteration);
if (rng.NextDouble() > successChance)
{
return false;
}
}
AlignmentOffset = TrueAlignmentOffset + new Vec2((float)(rng.NextDouble() - 0.5) * offsetJitter, (float)(rng.NextDouble() - 0.5) * offsetJitter);
return true;
}
public void MoveAltitude(double amount)
{
CheckConnected();
ActualAltBacklash += amount;
if (ActualAltBacklash > altBacklash * 0.5f)
{
amount = Math.Sign(amount) * (ActualAltBacklash - altBacklash * 0.5f);
ActualAltBacklash = altBacklash * 0.5f;
}
else if (ActualAltBacklash < -altBacklash * 0.5f)
{
amount = Math.Sign(amount) * (-altBacklash * 0.5f - ActualAltBacklash);
ActualAltBacklash = -altBacklash * 0.5f;
}
else
{
amount = 0;
}
TrueAlignmentOffset += TrueAltAxis * (amount + (float)(rng.NextDouble() - 0.5) * moveJitter / TrueAltAxis.Length);
}
public void MoveAzimuth(double amount)
{
CheckConnected();
ActualAzBacklash += amount;
if (ActualAzBacklash > azBacklash * 0.5f)
{
amount = Math.Sign(amount) * (ActualAzBacklash - azBacklash * 0.5f);
ActualAzBacklash = azBacklash * 0.5f;
}
else if (ActualAzBacklash < -azBacklash * 0.5f)
{
amount = Math.Sign(amount) * (-azBacklash * 0.5f - ActualAzBacklash);
ActualAzBacklash = -azBacklash * 0.5f;
}
else
{
amount = 0;
}
TrueAlignmentOffset += TrueAzAxis * (amount + (float)(rng.NextDouble() - 0.5) * moveJitter / TrueAzAxis.Length);
}
public void StopAltitude()
{
CheckConnected();
}
public void StopAzimuth()
{
CheckConnected();
}
}
}