forked from dotnet/iot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathProgram.cs
50 lines (45 loc) · 1.69 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
// 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;
using System.Device.Pwm.Drivers;
using System.Diagnostics;
using System.Threading;
using Iot.Device.DCMotor;
const double Period = 10.0;
Stopwatch sw = Stopwatch.StartNew();
// 1 pin mode
// using (DCMotor motor = DCMotor.Create(6))
// using (DCMotor motor = DCMotor.Create(PwmChannel.Create(0, 0, frequency: 50)))
// 2 pin mode
// using (DCMotor motor = DCMotor.Create(27, 22))
// using (DCMotor motor = DCMotor.Create(new SoftwarePwmChannel(27, frequency: 50), 22))
// 2 pin mode with BiDirectional Pin
// using (DCMotor motor = DCMotor.Create(19, 26, null, true, true))
// using (DCMotor motor = DCMotor.Create(PwmChannel.Create(0, 1, 100, 0.0), 26, null, true, true))
// 3 pin mode
// using (DCMotor motor = DCMotor.Create(PwmChannel.Create(0, 0, frequency: 50), 23, 24))
// Start Stop mode - additional methods to disable/enable output regardless of the Speed value
// using (DCMotorWithStartStop motor = new DCMotorWithStartStop(DCMotor.Create( _any version above_ )))
using DCMotor motor = DCMotor.Create(6, 27, 22);
bool done = false;
Console.CancelKeyPress += (o, e) =>
{
done = true;
e.Cancel = true;
};
string? lastSpeedDisp = null;
while (!done)
{
double time = sw.ElapsedMilliseconds / 1000.0;
// Note: range is from -1 .. 1 (for 1 pin setup 0 .. 1)
motor.Speed = Math.Sin(2.0 * Math.PI * time / Period);
string disp = $"Speed = {motor.Speed:0.00}";
if (disp != lastSpeedDisp)
{
lastSpeedDisp = disp;
Console.WriteLine(disp);
}
Thread.Sleep(1);
}