/
DCMotor.cs
145 lines (127 loc) · 3.48 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
using Meadow.Foundation.ICs.IOExpanders;
using System;
namespace Meadow.Foundation.FeatherWings
{
/// <summary>
/// Motor commands
/// </summary>
public enum Commmand
{
/// <summary>
/// Move forward
/// </summary>
FORWARD,
/// <summary>
/// Move backwards
/// </summary>
BACKWARD,
/// <summary>
/// Release
/// </summary>
RELEASE
}
/// <summary>
/// Represents a DC Motor
/// </summary>
public class DCMotor : Motor
{
readonly byte _pwmPin;
readonly byte _in1;
readonly byte _in2;
/// <summary>
/// Creates a DCMotor driver
/// </summary>
/// <param name="num"></param>
/// <param name="pca9685"></param>
/// <exception cref="ArgumentException"></exception>
public DCMotor(short num, Pca9685 pca9685) : base(pca9685)
{
if (num < 0 || num > 3)
{
throw new ArgumentException("Motor must be between 0 and 3");
}
switch (num)
{
case 0:
_pwmPin = 8;
_in2 = 9;
_in1 = 10;
break;
case 1:
_pwmPin = 13;
_in2 = 12;
_in1 = 11;
break;
case 2:
_pwmPin = 2;
_in2 = 3;
_in1 = 4;
break;
case 3:
_pwmPin = 7;
_in2 = 6;
_in1 = 5;
break;
}
Run(Commmand.RELEASE);
}
/// <summary>
/// Controls the motor direction/action
/// </summary>
/// <param name="command">The action</param>
public virtual void Run(Commmand command)
{
if (command == Commmand.FORWARD)
{
pca9685.SetPin(_in2, false);
pca9685.SetPin(_in1, true);
}
if (command == Commmand.BACKWARD)
{
pca9685.SetPin(_in2, true);
pca9685.SetPin(_in1, false);
}
if (command == Commmand.RELEASE)
{
pca9685.SetPin(_in1, false);
pca9685.SetPin(_in2, false);
}
}
/// <summary>
/// Control the DC Motor speed/throttle
/// </summary>
/// <param name="speed">The 8-bit PWM value, 0 is off, 255 is on</param>
public override void SetSpeed(short speed)
{
if (speed < 0)
{
speed = 0;
}
if (speed > 255)
{
speed = 255;
}
pca9685.SetPwm(_pwmPin, 0, speed * 16);
}
/// <summary>
/// Control the DC Motor speed/throttle
/// </summary>
/// <param name="speed">The 12-bit PWM value, 0 is off, 4096 is on</param>
public void PreciseSpeed(short speed)
{
if (speed > 4096)
speed = 4096;
if (speed < 0)
speed = 0;
pca9685.SetPwm(_pwmPin, 0, speed);
}
/// <summary>
/// Stops the motor
/// </summary>
public void Stop()
{
Run(Commmand.RELEASE);
pca9685.SetPwm(_pwmPin, 0, 0);
}
}
}