-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDistance.cs
More file actions
51 lines (41 loc) · 1.68 KB
/
Distance.cs
File metadata and controls
51 lines (41 loc) · 1.68 KB
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
using System;
using GHIElectronics.TinyCLR.Devices.Gpio;
using GHIElectronics.TinyCLR.Devices.Signals;
namespace BrainPad {
public class Distance : IOModule {
PulseFeedback pulseFeedback;
GpioPin distanceTrigger;
GpioPin distanceEcho;
public Distance(double trigger, double echo) {
var triggerPin = Controller.GetGpioFromPin(trigger);
var echoPin = Controller.GetGpioFromPin(echo);
this.Initialize(triggerPin, echoPin);
}
void Initialize(int triggerPin, int echoPin) {
if (triggerPin < 0 || echoPin < 0) {
throw new ArgumentException("trigger or echo pin invalid.");
}
this.distanceTrigger = Controller.Gpio.OpenPin(triggerPin);
this.distanceEcho = Controller.Gpio.OpenPin(echoPin);
this.pulseFeedback = new PulseFeedback(this.distanceTrigger, this.distanceEcho, PulseFeedbackMode.EchoDuration) {
DisableInterrupts = false,
Timeout = TimeSpan.FromSeconds(1),
PulseLength = TimeSpan.FromTicks(100),
PulseValue = GpioPinValue.High,
EchoValue = GpioPinValue.High,
};
}
public override double In() {
var time = this.pulseFeedback.Trigger();
var microsecond = time.TotalMilliseconds * 1000.0;
var distance = microsecond * 0.036 / 2;
return (double)distance;
}
public override void Dispose() {
this.distanceTrigger?.Dispose();
this.distanceEcho?.Dispose();
this.distanceTrigger = null;
this.distanceEcho = null;
}
}
}