-
Notifications
You must be signed in to change notification settings - Fork 78
/
SensorDistanceEx.java
182 lines (149 loc) · 5.03 KB
/
SensorDistanceEx.java
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
package com.arcrobotics.ftclib.hardware;
import com.qualcomm.robotcore.util.Range;
import org.firstinspires.ftc.robotcore.external.navigation.DistanceUnit;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public interface SensorDistanceEx extends SensorDistance {
/**
* Represents a target distance
*/
class DistanceTarget {
/**
* The distance.
*/
private double target;
/**
* The threshold for the actual distance to be in for the target to be reached.
* Essentially, this is the acceptable error range.
*/
private double threshold;
/**
* User-defined name for the target. Optional.
*/
private String name;
/**
* User-defined unit of distance. This unit applies to both the threshold and the target
*/
private DistanceUnit unit;
/**
* Target <b>must</b> be within the range of the Rev 2m Sensor, which is 2 meters
*
* @param unit user-defined unit of distance
* @param target the target distance
*/
public DistanceTarget(DistanceUnit unit, double target) {
this(unit, target, 5);
}
/**
* @param unit user-defined unit of distance
* @param target the target distance
* @param threshold the acceptable error range
*/
public DistanceTarget(DistanceUnit unit, double target, double threshold) {
this(unit, target, threshold, "Distance Target");
}
/**
* @param unit the user-defined unit of distance
* @param target the target distance
* @param threshold the acceptable error range
* @param name the name of the sensor
*/
public DistanceTarget(DistanceUnit unit, double target, double threshold, String name) {
this.unit = unit;
this.target = target;
this.threshold = threshold;
this.name = name;
}
/**
* Determines if the sensor reached the target value (within the threshold error range)
*
* @param currentDistance the current distance to the target
* @return whether the the sensor reached the the target value (within the threshold error range)
*/
public boolean atTarget(double currentDistance) {
currentDistance = unit.fromUnit(unit, currentDistance);
boolean withinRange;
if ((Range.clip(currentDistance, currentDistance - threshold, currentDistance + threshold)) >= currentDistance + threshold)
withinRange = false;
else if (((Range.clip(currentDistance, currentDistance - threshold, currentDistance + threshold)) <= currentDistance + threshold))
withinRange = false;
else withinRange = true;
return withinRange;
}
/**
* Change the target value
*
* @param target the new target
*/
public void setTarget(double target) {
this.target = target;
}
/**
* Changes the unit of measurement
*
* @param unit the new unit value
*/
public void setUnit(DistanceUnit unit) {
this.target = unit.fromUnit(this.unit, target);
this.threshold = unit.fromUnit(this.unit, threshold);
this.unit = unit;
}
/**
* Changes the name of the sensor
*
* @param name the new name of the sensor
*/
public void setName(String name) {
this.name = name;
}
/**
* Gets the unit of distance
*
* @return the unit of distance
*/
public DistanceUnit getUnit() {
return unit;
}
/**
* Gets the acceptable error range
*
* @return the threshold (acceptable error range)
*/
public double getThreshold() {
return threshold;
}
/**
* @return the target distance
*/
public double getTarget() {
return target;
}
/**
* @return the name of the sensor
*/
public String getName() {
return this.name;
}
}
/**
* Returns whether a given DistanceTarget has been reached
*/
boolean targetReached(SensorRevTOFDistance.DistanceTarget target);
/**
* Adds a DistanceTarget.
*/
void addTarget(SensorRevTOFDistance.DistanceTarget target);
/**
* Adds an List of DistanceTargets to the targets associated with this device.
*/
void addTargets(List<SensorRevTOFDistance.DistanceTarget> targets);
/**
* Checks all targets currently associated with this device and returns a {@code Map}
* with the results.
*
* @return The results of the checking.
*/
Map<SensorRevTOFDistance.DistanceTarget, Boolean> checkAllTargets();
}