/
GpioStepper.cs
218 lines (177 loc) · 6.91 KB
/
GpioStepper.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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
using Meadow.Peripherals;
using Meadow.Peripherals.Motors;
using Meadow.Units;
using System;
using System.Threading;
using System.Threading.Tasks;
namespace Meadow.Foundation.Motors.Stepper;
/// <summary>
/// Represents an abstract GPIO-based stepper motor.
/// </summary>
public abstract class GpioStepperBase : IStepperMotor
{
private double _stepsPerDegree;
/// <inheritdoc/>
public RotationDirection Direction { get; protected set; }
/// <inheritdoc/>
public abstract Angle Position { get; }
/// <inheritdoc/>
public abstract bool IsMoving { get; }
/// <inheritdoc/>
public abstract Task Rotate(int steps, RotationDirection direction, Frequency rate, CancellationToken cancellationToken = default);
/// <inheritdoc/>
public abstract Task Stop(CancellationToken cancellationToken = default);
/// <inheritdoc/>
public abstract Task ResetPosition(CancellationToken cancellationToken = default);
/// <inheritdoc/>
public abstract AngularVelocity MaxVelocity { get; }
/// <inheritdoc/>
public abstract int StepsPerRevolution { get; }
/// <summary>
/// Initializes a new instance of the <see cref="GpioStepperBase"/> class.
/// </summary>
protected GpioStepperBase()
{
}
/// <summary>
/// Gets the frequency corresponding to the specified angular velocity.
/// </summary>
/// <param name="velocity">The angular velocity.</param>
/// <returns>The frequency required for the specified angular velocity.</returns>
protected Frequency GetFrequencyForVelocity(AngularVelocity velocity)
{
if (StepsPerRevolution <= 0) throw new Exception("StepsPerRevolution must be greater than 0");
if (_stepsPerDegree == 0)
{
_stepsPerDegree = StepsPerRevolution / 360f;
}
return new Frequency(velocity.DegreesPerSecond * _stepsPerDegree * 4, Frequency.UnitType.Hertz);
}
/// <inheritdoc/>
public Task GoTo(Angle position, AngularVelocity velocity, CancellationToken cancellationToken = default)
{
RotationDirection shortestDirection;
if (Position == position)
{
// no move required
return Task.CompletedTask;
}
// determine shortest path to destination
double totalDistance;
if (position.Degrees < Position.Degrees)
{
totalDistance = Position.Degrees - position.Degrees;
if (totalDistance < 180)
{
shortestDirection = RotationDirection.CounterClockwise;
}
else
{
totalDistance = totalDistance - 180;
shortestDirection = RotationDirection.Clockwise;
}
}
else
{
totalDistance = position.Degrees - Position.Degrees;
if (totalDistance > 180)
{
totalDistance = totalDistance - 180;
shortestDirection = RotationDirection.CounterClockwise;
}
else
{
shortestDirection = RotationDirection.Clockwise;
}
}
return Rotate(new Angle(totalDistance, Angle.UnitType.Degrees), shortestDirection, velocity, cancellationToken);
}
/// <inheritdoc/>
public Task GoTo(Angle position, RotationDirection direction, AngularVelocity velocity, CancellationToken cancellationToken = default)
{
if (Position == position)
{
// no move required
return Task.CompletedTask;
}
var dest = position.Degrees;
var start = Position.Degrees;
while (dest < 0) dest += 360;
dest %= 360;
// convert velocity into frequency based on drive parameters
var freq = GetFrequencyForVelocity(velocity);
double totalDistance;
if (dest < start)
{
totalDistance = direction switch
{
RotationDirection.CounterClockwise => start - dest,
_ => 360 - start + dest
};
}
else
{
totalDistance = direction switch
{
RotationDirection.Clockwise => dest - start,
_ => start + 360 - dest
};
}
return Rotate((int)(totalDistance * _stepsPerDegree), direction, freq, cancellationToken);
}
/// <inheritdoc/>
public Task Rotate(Angle amountToRotate, RotationDirection direction, AngularVelocity velocity, CancellationToken cancellationToken = default)
{
// convert velocity into frequency based on drive parameters
var freq = GetFrequencyForVelocity(velocity);
var steps = (int)(amountToRotate.Degrees * _stepsPerDegree);
return Rotate(steps, direction, freq, cancellationToken);
}
/// <inheritdoc/>
public Task Run(RotationDirection direction, AngularVelocity velocity, CancellationToken cancellationToken = default)
{
// run until cancelled in the specified direction
if (IsMoving) throw new Exception("Cannot Run while the motor is already moving.");
var freq = GetFrequencyForVelocity(velocity);
return Rotate(-1, direction, freq, cancellationToken);
}
/// <inheritdoc/>
public Task RunFor(TimeSpan runTime, RotationDirection direction, AngularVelocity velocity, CancellationToken cancellationToken = default)
{
var timeoutTask = Task.Delay(runTime);
var motorTask = Run(direction, velocity, cancellationToken);
var t = Task.WaitAny(timeoutTask, motorTask);
if (t == 0)
{
// tell the motor to stop
Stop();
// wait for the motor to finish
motorTask.Wait();
}
return Task.CompletedTask;
}
/// <inheritdoc/>
public Task Run(RotationDirection direction, float speed, CancellationToken cancellationToken = default)
{
if (speed < 0 || speed > 100) throw new ArgumentOutOfRangeException(nameof(speed));
var velocity = new AngularVelocity(MaxVelocity.RevolutionsPerSecond * (speed / 100));
return Run(direction, velocity, cancellationToken);
}
/// <inheritdoc/>
public virtual Task RunFor(TimeSpan runTime, RotationDirection direction, float speed, CancellationToken cancellationToken = default)
{
if (speed < 0 || speed > 100) throw new ArgumentOutOfRangeException(nameof(speed));
var velocity = new AngularVelocity(MaxVelocity.RevolutionsPerSecond * (speed / 100));
return RunFor(runTime, direction, velocity, cancellationToken);
}
/// <inheritdoc/>
public Task RunFor(TimeSpan runTime, RotationDirection direction, CancellationToken cancellationToken = default)
{
return RunFor(runTime, direction, 100f, cancellationToken);
}
/// <inheritdoc/>
public Task Run(RotationDirection direction, CancellationToken cancellationToken = default)
{
return Run(direction, 100f, cancellationToken);
}
}