-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPotentiometer.cpp
More file actions
49 lines (38 loc) · 1.12 KB
/
Potentiometer.cpp
File metadata and controls
49 lines (38 loc) · 1.12 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
/*
* @author:
* @lead: Andrew Horsman
* @other: Mike Noseworthy
* @description: Grabs a value from a potentiometer.
*/
#include "Headers/WPILibrary.h"
class Potentiometer {
AnalogChannel pot;
float range,
lowerBound;
public:
Potentiometer(int portNumber, float lowerBoundVoltage = -1.0, float upperBoundVoltage = -1.0):
pot(portNumber)
{
if (lowerBoundVoltage > upperBoundVoltage)
range = lowerBoundVoltage - upperBoundVoltage;
else if (upperBoundVoltage > lowerBoundVoltage)
range = upperBoundVoltage - lowerBoundVoltage;
lowerBound = lowerBoundVoltage;
}
bool GreaterThanThreshold(float threshold) {
return (pot.GetVoltage() > threshold);
}
bool LessThanThreshold(float threshold) {
return (pot.GetVoltage() < threshold);
}
float GetRawVoltage() {
return pot.GetVoltage();
}
float CalculatePosition() {
float current = GetRawVoltage();
float differenceFromLower = current - lowerBound;
if (differenceFromLower == 0.0) return 0;
float positionPercentile = 100 / (range / differenceFromLower);
return positionPercentile;
}
};