-
Notifications
You must be signed in to change notification settings - Fork 0
/
Axis.cs
137 lines (118 loc) · 3.61 KB
/
Axis.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
using System;
namespace AutoPolarAlign
{
public class Axis
{
private double _position;
public double Position
{
get => _position;
set
{
var offset = value - _position;
MoveBacklash(offset);
_position = value;
LastDirection = Math.Sign(offset);
}
}
public double Limit { get; set; } = double.MaxValue;
private double _backlashCompensation;
public double BacklashCompensation
{
get => _backlashCompensation;
set
{
_backlashCompensation = value;
ConstrainEstimatedBacklash();
}
}
public double CalibrationDistance { get; set; }
public Vec2 CalibratedDirection { get; set; }
public double CalibratedMagnitude { get; set; }
public double EstimatedBacklash { get; private set; }
public int LastDirection { get; private set; }
public string Name { get; }
public Axis(string name)
{
Name = name;
}
public void Reset(double position = 0.0f)
{
Position = position;
EstimatedBacklash = 0.0f;
LastDirection = 0;
}
public void ClearBacklash(int direction)
{
if (direction > 0)
{
EstimatedBacklash = BacklashCompensation * 0.5;
}
else if (direction < 0)
{
EstimatedBacklash = -BacklashCompensation * 0.5;
}
}
public double EstimateCompensatedMove(double amount, double backlashCompensationPercent = 1.0)
{
if (Math.Sign(amount) != Math.Sign(EstimatedBacklash))
{
amount += Math.Sign(amount) * (BacklashCompensation * 0.5 + Math.Abs(EstimatedBacklash)) * backlashCompensationPercent;
}
else
{
amount += Math.Sign(amount) * (BacklashCompensation * 0.5 - Math.Abs(EstimatedBacklash)) * backlashCompensationPercent;
}
return amount;
}
public bool Move(double amount, out double movedAmount)
{
movedAmount = 0;
if (amount > 0)
{
if (Position >= Limit - double.Epsilon)
{
return false;
}
else if (Position + amount >= Limit)
{
movedAmount = Limit - Position;
Position = Limit;
return true;
}
}
else
{
if (Position <= -Limit + double.Epsilon)
{
return false;
}
else if (Position + amount <= -Limit)
{
movedAmount = -Limit - Position;
Position = -Limit;
return true;
}
}
movedAmount = amount;
Position += amount;
return true;
}
private void MoveBacklash(double amount)
{
EstimatedBacklash += amount;
ConstrainEstimatedBacklash();
}
private void ConstrainEstimatedBacklash()
{
if (EstimatedBacklash > BacklashCompensation * 0.5)
{
ClearBacklash(1);
}
else if (EstimatedBacklash < -BacklashCompensation * 0.5)
{
ClearBacklash(-1);
}
}
}
}