-
-
Notifications
You must be signed in to change notification settings - Fork 114
/
Copy pathDCMotor.cs
272 lines (247 loc) · 13.6 KB
/
DCMotor.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
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
using System;
using System.Device.Gpio;
using System.Device.Pwm;
namespace Iot.Device.DCMotor
{
/// <summary>
/// Direct current (DC) motor
/// </summary>
public abstract class DCMotor : IDisposable
{
private const int DefaultPwmFrequency = 50;
private bool _shouldDispose;
/// <summary>
/// Constructs generic <see cref="DCMotor"/> instance
/// </summary>
/// <param name="controller"><see cref="GpioController"/> related with operations on pins</param>
/// <param name="shouldDispose">True to dispose the Gpio Controller</param>
protected DCMotor(GpioController? controller, bool shouldDispose)
{
_shouldDispose = shouldDispose || controller is null;
Controller = controller ?? new GpioController();
}
/// <summary>
/// Gets or sets the speed of the motor. Range is -1..1 or 0..1 for 1-pin connection.
/// 1 means maximum speed, 0 means no movement and -1 means movement in opposite direction.
/// </summary>
public abstract double Speed { get; set; }
/// <summary>
/// <see cref="GpioController"/> related with operations on pins
/// </summary>
protected GpioController Controller { get; set; }
/// <summary>
/// Creates <see cref="DCMotor"/> instance using only one pin which allows to control speed in one direction.
/// </summary>
/// <param name="speedControlChannel"><see cref="PwmChannel"/> used to control the speed of the motor</param>
/// <returns>DCMotor instance</returns>
/// <remarks>
/// PWM pin <paramref name="speedControlChannel"/> can be connected to either enable pin of the H-bridge.
/// or directly to the one of two inputs related with the motor direction (if H-bridge allows inputs to change frequently).
/// Connecting motor directly to GPIO pin is not recommended and may damage your board.
/// </remarks>
public static DCMotor Create(PwmChannel speedControlChannel)
{
if (speedControlChannel is null)
{
throw new ArgumentNullException(nameof(speedControlChannel));
}
return new DCMotor2PinNoEnable(speedControlChannel, -1, null, true);
}
/// <summary>
/// Creates <see cref="DCMotor"/> instance using only one pin which allows to control speed in one direction.
/// </summary>
/// <param name="speedControlPin">Pin used to control the speed of the motor with software PWM (frequency will default to 50Hz)</param>
/// <param name="controller"><see cref="GpioController"/> related to the <paramref name="speedControlPin"/></param>
/// <param name="shouldDispose">True to dispose the Gpio Controller</param>
/// <returns><see cref="DCMotor"/> instance</returns>
/// <remarks>
/// <paramref name="speedControlPin"/> can be connected to either enable pin of the H-bridge.
/// or directly to the on of two inputs related with the motor direction (if H-bridge allows inputs to change frequently).
/// Connecting motor directly to GPIO pin is not recommended and may damage your board.
/// </remarks>
public static DCMotor Create(int speedControlPin, GpioController? controller = null, bool shouldDispose = true)
{
if (speedControlPin == -1)
{
throw new ArgumentOutOfRangeException(nameof(speedControlPin));
}
controller = controller ?? new GpioController();
return new DCMotor2PinNoEnable(
PwmChannel.CreateFromPin(speedControlPin, DefaultPwmFrequency, 0.0),
-1,
controller,
shouldDispose);
}
/// <summary>
/// Creates <see cref="DCMotor"/> instance using two pins which allows to control speed in both directions.
/// </summary>
/// <param name="speedControlChannel"><see cref="PwmChannel"/> used to control the speed of the motor</param>
/// <param name="directionPin">Pin used to control the direction of the motor</param>
/// <param name="controller"><see cref="GpioController"/> related to the <paramref name="directionPin"/></param>
/// <param name="shouldDispose">True to dispose the Gpio Controller</param>
/// <param name="singleBiDirectionPin">True if a controller with one direction input is used,
/// false if a controller with two direction inputs is used</param>
/// <returns><see cref="DCMotor"/> instance</returns>
/// <remarks>
/// <paramref name="speedControlChannel"/> should be connected to the one of two inputs
/// related with the motor direction (if H-bridge allows inputs to change frequently),
/// or to PWM input if a controller with one direction input is used.
/// <paramref name="directionPin"/> should be connected to H-bridge input corresponding to one of the motor inputs.
/// or to direction input if a controller with one direction input is used.
/// Connecting motor directly to GPIO pin is not recommended and may damage your board.
/// </remarks>
public static DCMotor Create(PwmChannel speedControlChannel, int directionPin, GpioController? controller = null, bool shouldDispose = true, bool singleBiDirectionPin = false)
{
if (speedControlChannel is null)
{
throw new ArgumentNullException(nameof(speedControlChannel));
}
if (directionPin == -1)
{
throw new ArgumentOutOfRangeException(nameof(directionPin));
}
if (singleBiDirectionPin)
{
return new DCMotor2PinWithBiDirectionalPin(speedControlChannel, directionPin, controller, shouldDispose);
}
else
{
return new DCMotor2PinNoEnable(speedControlChannel, directionPin, controller, shouldDispose);
}
}
/// <summary>
/// Creates <see cref="DCMotor"/> instance using two pins which allows to control speed in both directions.
/// </summary>
/// <param name="speedControlPin">Pin used to control the speed of the motor with software PWM (frequency will default to 50Hz)</param>
/// <param name="directionPin">Pin used to control the direction of the motor</param>
/// <param name="controller">GPIO controller related to <paramref name="speedControlPin"/> and <paramref name="directionPin"/></param>
/// <param name="shouldDispose">True to dispose the Gpio Controller</param>
/// <param name="singleBiDirectionPin">True if a controller with one direction input is used,
/// false if a controller with two direction inputs is used</param>
/// <returns><see cref="DCMotor"/> instance</returns>
/// <remarks>
/// <paramref name="speedControlPin"/> should be connected to the one of two inputs
/// related with the motor direction (if H-bridge allows inputs to change frequently),
/// or to PWM input if a controller with one direction input is used.
/// <paramref name="directionPin"/> should be connected to H-bridge input corresponding to one of the motor inputs.
/// or to direction input if a controller with one direction input is used.
/// Connecting motor directly to GPIO pin is not recommended and may damage your board.
/// </remarks>
public static DCMotor Create(int speedControlPin, int directionPin, GpioController? controller = null, bool shouldDispose = true, bool singleBiDirectionPin = false)
{
if (speedControlPin == -1)
{
throw new ArgumentOutOfRangeException(nameof(speedControlPin));
}
if (directionPin == -1)
{
throw new ArgumentOutOfRangeException(nameof(directionPin));
}
controller = controller ?? new GpioController();
if (singleBiDirectionPin)
{
return new DCMotor2PinWithBiDirectionalPin(
PwmChannel.CreateFromPin(speedControlPin, DefaultPwmFrequency, 0.0),
directionPin,
controller,
shouldDispose);
}
else
{
return new DCMotor2PinNoEnable(
PwmChannel.CreateFromPin(speedControlPin, DefaultPwmFrequency, 0.0),
directionPin,
controller,
shouldDispose);
}
}
/// <summary>
/// Creates <see cref="DCMotor"/> instance using three pins which allows to control speed in both directions.
/// </summary>
/// <param name="speedControlChannel"><see cref="PwmChannel"/> used to control the speed of the motor</param>
/// <param name="directionPin">First pin used to control the direction of the motor</param>
/// <param name="otherDirectionPin">Second pin used to control the direction of the motor</param>
/// <param name="controller"><see cref="GpioController"/> related to <paramref name="directionPin"/> and <paramref name="otherDirectionPin"/></param>
/// <param name="shouldDispose">True to dispose the Gpio Controller</param>
/// <returns><see cref="DCMotor"/> instance</returns>
/// <remarks>
/// When speed is non-zero the value of <paramref name="otherDirectionPin"/> will always be opposite to that of <paramref name="directionPin"/>.
/// <paramref name="speedControlChannel"/> should be connected to enable pin of the H-bridge.
/// <paramref name="directionPin"/> should be connected to H-bridge input corresponding to one of the motor inputs.
/// <paramref name="otherDirectionPin"/> should be connected to H-bridge input corresponding to the remaining motor input.
/// Connecting motor directly to GPIO pin is not recommended and may damage your board.
/// </remarks>
public static DCMotor Create(PwmChannel speedControlChannel, int directionPin, int otherDirectionPin, GpioController? controller = null, bool shouldDispose = true)
{
if (speedControlChannel is null)
{
throw new ArgumentNullException(nameof(speedControlChannel));
}
if (directionPin == -1)
{
throw new ArgumentOutOfRangeException(nameof(directionPin));
}
if (otherDirectionPin == -1)
{
throw new ArgumentOutOfRangeException(nameof(otherDirectionPin));
}
return new DCMotor3Pin(
speedControlChannel,
directionPin,
otherDirectionPin,
controller,
shouldDispose);
}
/// <summary>
/// Creates <see cref="DCMotor"/> instance using three pins which allows to control speed in both directions.
/// </summary>
/// <param name="speedControlPin">Pin used to control the speed of the motor with software PWM (frequency will default to 50Hz)</param>
/// <param name="directionPin">First pin used to control the direction of the motor</param>
/// <param name="otherDirectionPin">Second pin used to control the direction of the motor</param>
/// <param name="controller"><see cref="GpioController"/> related to <paramref name="speedControlPin"/>, <paramref name="directionPin"/> and <paramref name="otherDirectionPin"/></param>
/// <param name="shouldDispose">True to dispose the Gpio Controller</param>
/// <returns><see cref="DCMotor"/> instance</returns>
/// <remarks>
/// When speed is non-zero the value of <paramref name="otherDirectionPin"/> will always be opposite to that of <paramref name="directionPin"/>
/// PWM pin <paramref name="speedControlPin"/> should be connected to enable pin of the H-bridge.
/// <paramref name="directionPin"/> should be connected to H-bridge input corresponding to one of the motor inputs.
/// <paramref name="otherDirectionPin"/> should be connected to H-bridge input corresponding to the remaining motor input.
/// Connecting motor directly to GPIO pin is not recommended and may damage your board.
/// </remarks>
public static DCMotor Create(int speedControlPin, int directionPin, int otherDirectionPin, GpioController? controller = null, bool shouldDispose = true)
{
if (speedControlPin == -1)
{
throw new ArgumentOutOfRangeException(nameof(speedControlPin));
}
if (directionPin == -1)
{
throw new ArgumentOutOfRangeException(nameof(directionPin));
}
if (otherDirectionPin == -1)
{
throw new ArgumentOutOfRangeException(nameof(otherDirectionPin));
}
controller = controller ?? new GpioController();
return new DCMotor3Pin(
PwmChannel.CreateFromPin(speedControlPin, DefaultPwmFrequency, 0.0),
directionPin,
otherDirectionPin,
controller,
shouldDispose);
}
/// <summary>
/// Releases the resources used by the <see cref="DCMotor"/> instance.
/// </summary>
public virtual void Dispose()
{
if (_shouldDispose)
{
Controller?.Dispose();
Controller = null!;
}
}
}
}