-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUltraSonicSensor.java
More file actions
90 lines (78 loc) · 1.75 KB
/
Copy pathUltraSonicSensor.java
File metadata and controls
90 lines (78 loc) · 1.75 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
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
package ev3.exercises.library;
import lejos.hardware.port.Port;
import lejos.hardware.sensor.EV3UltrasonicSensor;
import lejos.robotics.RangeFinder;
import lejos.robotics.SampleProvider;
public class UltraSonicSensor implements RangeFinder
{
EV3UltrasonicSensor sensor;
SampleProvider sp;
float [] sample;
/**
* Creates UltraSonicSensor object. This is a wrapper class for EV3UltrasonicSensor.
* @param port SensorPort of EV3UltrasonicSensor device.
*/
public UltraSonicSensor(Port port)
{
sensor = new EV3UltrasonicSensor(port);
sp = sensor.getDistanceMode();
sample = new float[sp.sampleSize()];
}
/**
* Returns the underlying EV3UltrasonicSensor object.
* @return Sensor object reference.
*/
public EV3UltrasonicSensor getSensor()
{
return sensor;
}
/**
* Get range (distance) to object detected by UltraSonic sensor.
* @return Distance in meters.
*/
@Override
public float getRange()
{
sp.fetchSample(sample, 0);
return sample[0];
}
/**
* Get range (distance) to object detected by UltraSonic sensor.
* @return Distance in meters. Only one distance value is returned.
*/
@Override
public float[] getRanges()
{
sp.fetchSample(sample, 0);
return sample;
}
/**
* Determine if UltraSonic sensor is enabled.
* @return True if enabled, false if not.
*/
public boolean isEnabled()
{
return sensor.isEnabled();
}
/**
* Enable UltraSonic sensor.
*/
public void enable()
{
sensor.enable();
}
/**
* Disable UltraSonic sensor.
*/
public void disable()
{
sensor.disable();
}
/**
* Release resources.
*/
public void close()
{
sensor.close();
}
}