forked from dotnet/iot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Program.cs
235 lines (204 loc) · 6.93 KB
/
Program.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
// 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.Collections.Generic;
using System.Device.I2c;
using System.Device.Pwm;
using System.Diagnostics;
using System.Threading;
using System.Linq;
using Iot.Device.ServoMotor;
using Iot.Device.Pwm;
// Some SG90s can do 180 angle range but some other will be oscillating on the edge values
// Max angle which doesn't cause any issues found experimentally was as below.
// The ones which can do 180 will have the minimum pulse width at around 520uS.
const int AngleRange = 173;
const int MinPulseWidthMicroseconds = 600;
const int MaxPulseWidthMicroseconds = 2590;
var busId = 1;
var selectedI2cAddress = 0b000000; // A5 A4 A3 A2 A1 A0
var deviceAddress = Pca9685.I2cAddressBase + selectedI2cAddress;
I2cConnectionSettings settings = new(busId, deviceAddress);
using I2cDevice device = I2cDevice.Create(settings);
using Pca9685 pca9685 = new(device);
Console.WriteLine(
$"PCA9685 is ready on I2C bus {device.ConnectionSettings.BusId} with address {device.ConnectionSettings.DeviceAddress}");
Console.WriteLine($"PWM Frequency: {pca9685.PwmFrequency}Hz");
Console.WriteLine();
PrintHelp();
while (true)
{
string[]? command = Console.ReadLine()?.ToLower()?.Split(' ');
if (command?[0] is not { Length: >0 })
{
return;
}
switch (command[0][0])
{
case 'q':
pca9685.SetDutyCycleAllChannels(0.0);
return;
case 'f':
{
var freq = double.Parse(command[1]);
pca9685.PwmFrequency = freq;
Console.WriteLine($"PWM Frequency has been set to {pca9685.PwmFrequency}Hz");
break;
}
case 'd':
{
switch (command.Length)
{
case 2:
{
double value = double.Parse(command[1]);
pca9685.SetDutyCycleAllChannels(value);
Console.WriteLine($"PWM duty cycle has been set to {value}");
break;
}
case 3:
{
int channel = int.Parse(command[1]);
double value = double.Parse(command[2]);
pca9685.SetDutyCycle(channel, value);
Console.WriteLine($"PWM duty cycle has been set to {value}");
break;
}
}
break;
}
case 'h':
{
PrintHelp();
break;
}
case 't':
{
int channel = int.Parse(command[1]);
ServoDemo(pca9685, channel);
PrintHelp();
break;
}
}
}
ServoMotor CreateServo(Pca9685 pca9685, int channel) =>
new ServoMotor(
pca9685.CreatePwmChannel(channel),
AngleRange, MinPulseWidthMicroseconds, MaxPulseWidthMicroseconds);
void PrintServoDemoHelp()
{
Console.WriteLine("Q return to previous menu");
Console.WriteLine("C calibrate");
Console.WriteLine("{angle} set angle (0 - 180)");
Console.WriteLine();
}
void ServoDemo(Pca9685 pca9685, int channel)
{
using ServoMotor servo = CreateServo(pca9685, channel);
PrintServoDemoHelp();
while (true)
{
string? command = Console.ReadLine()?.ToLower();
if (command is null)
{
return;
}
if (command[0] is 'q')
{
return;
}
if (command[0] == 'c')
{
CalibrateServo(servo);
PrintServoDemoHelp();
}
else
{
double value = double.Parse(command);
servo.WriteAngle(value);
Console.WriteLine($"Angle set to {value}. PWM duty cycle = {pca9685.GetDutyCycle(channel)}");
}
}
}
void PrintHelp()
{
Console.WriteLine("Command:");
Console.WriteLine(" F {freq_hz} set PWM frequency (Hz)");
Console.WriteLine(" D {value} set duty cycle (0.0 .. 1.0) for all channels");
Console.WriteLine(" S {channel} {value} set duty cycle (0.0 .. 1.0) for specific channel");
Console.WriteLine(" T {channel} servo test (defaults to SG90 params)");
Console.WriteLine(" H prints help");
Console.WriteLine(" Q quit");
Console.WriteLine();
}
void CalibrateServo(ServoMotor servo)
{
int maximumAngle = 180;
int minimumPulseWidthMicroseconds = 520;
int maximumPulseWidthMicroseconds = 2590;
Console.WriteLine("Searching for minimum pulse width");
CalibratePulseWidth(servo, ref minimumPulseWidthMicroseconds);
Console.WriteLine();
Console.WriteLine("Searching for maximum pulse width");
CalibratePulseWidth(servo, ref maximumPulseWidthMicroseconds);
Console.WriteLine("Searching for angle range");
Console.WriteLine(
"What is the angle range? (type integer with your angle range or enter to move to MIN/MAX)");
while (true)
{
servo.WritePulseWidth(maximumPulseWidthMicroseconds);
Console.WriteLine("Servo is now at MAX");
if (int.TryParse(Console.ReadLine(), out maximumAngle))
{
break;
}
servo.WritePulseWidth(minimumPulseWidthMicroseconds);
Console.WriteLine("Servo is now at MIN");
if (int.TryParse(Console.ReadLine(), out maximumAngle))
{
break;
}
}
servo.Calibrate(maximumAngle, minimumPulseWidthMicroseconds, maximumPulseWidthMicroseconds);
Console.WriteLine($"Angle range: {maximumAngle}");
Console.WriteLine($"Min PW [uS]: {minimumPulseWidthMicroseconds}");
Console.WriteLine($"Max PW [uS]: {maximumPulseWidthMicroseconds}");
}
void CalibratePulseWidth(ServoMotor servo, ref int pulseWidthMicroSeconds)
{
void SetPulseWidth(ref int pulseWidth)
{
pulseWidth = Math.Max(pulseWidth, 0);
servo.WritePulseWidth(pulseWidth);
}
Console.WriteLine("Use A/Z (1x); S/X (10x); D/C (100x)");
Console.WriteLine("Press enter to accept value");
while (true)
{
SetPulseWidth(ref pulseWidthMicroSeconds);
Console.WriteLine($"Current value: {pulseWidthMicroSeconds}");
switch (Console.ReadKey().Key)
{
case ConsoleKey.A:
pulseWidthMicroSeconds++;
break;
case ConsoleKey.Z:
pulseWidthMicroSeconds--;
break;
case ConsoleKey.S:
pulseWidthMicroSeconds += 10;
break;
case ConsoleKey.X:
pulseWidthMicroSeconds -= 10;
break;
case ConsoleKey.D:
pulseWidthMicroSeconds += 100;
break;
case ConsoleKey.C:
pulseWidthMicroSeconds -= 100;
break;
case ConsoleKey.Enter:
return;
}
}
}