-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAnalogToPowerLevel.java
More file actions
80 lines (70 loc) · 2.08 KB
/
AnalogToPowerLevel.java
File metadata and controls
80 lines (70 loc) · 2.08 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
package com.easternedgerobotics.rov.control;
public final class AnalogToPowerLevel {
private AnalogToPowerLevel() {
}
/**
* Used to scale the value to the [0, 110] range.
*/
private static final float SCALAR = 1.10f;
/**
* Used to shift the value to the [-5, 105] range.
*/
private static final float OFFSET = 0.05f;
/**
* Lower bound for the result.
*/
private static final float MINIMUM = 0;
/**
* Upper bound for the result.
*/
private static final float MAXIMUM = 1.00f;
/**
* Centre of result range.
*/
private static final float MIDDLE = 0.5f;
/**
* Transform an analog pin value into an appropriate power slider value
* with dead-bands at the top and bottom of the slider.
*
* @param analogValue the input.
* @return power slider value.
*/
public static float convert(final float analogValue) {
return clamp(((1 - analogValue) * SCALAR) - OFFSET);
}
/**
* Transform an analog pin value into an appropriate power slider value
* with dead-bands at the top and bottom of the slider, and 0v is max power.
*
* @param analogValue the input.
* @return power slider value.
*/
public static float convertNeg(final float analogValue) {
return 1f - convert(analogValue);
}
/**
* Transform an analog pin value into an appropriate power slider value
* with dead-bands at the top and bottom of the slider, and 0v is max power.
*
* @param analogValue the input.
* @return power slider value.
*/
public static boolean convertBool(final float analogValue) {
return analogValue > MIDDLE;
}
/**
* Clamp the value between the maximum and minimum supported power values.
*
* @param value the power slider value.
* @return clamped value.
*/
private static float clamp(final float value) {
if (value < MINIMUM) {
return MINIMUM;
}
if (value > MAXIMUM) {
return MAXIMUM;
}
return value;
}
}