-
Notifications
You must be signed in to change notification settings - Fork 22
Expand file tree
/
Copy pathAnalogAbsoluteEncoderSwerve.java
More file actions
142 lines (130 loc) · 3.33 KB
/
AnalogAbsoluteEncoderSwerve.java
File metadata and controls
142 lines (130 loc) · 3.33 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
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
package swervelib.encoders;
import edu.wpi.first.wpilibj.Alert.AlertType;
import edu.wpi.first.wpilibj.AnalogInput;
import edu.wpi.first.wpilibj.RobotController;
import swervelib.telemetry.Alert;
/**
* Swerve Absolute Encoder for Thrifty Encoders and other analog encoders.
*/
public class AnalogAbsoluteEncoderSwerve extends SwerveAbsoluteEncoder
{
// Entire class inspired by 5010
// Source: https://github.com/FRC5010/FRCLibrary/blob/main/FRC5010Example2023/src/main/java/frc/robot/FRC5010/sensors/AnalogInput5010.java
/**
* Encoder as Analog Input.
*/
public AnalogInput encoder;
/**
* Inversion state of the encoder.
*/
private boolean inverted = false;
/**
* An {@link Alert} for if the absolute encoder offset cannot be set.
*/
private Alert cannotSetOffset;
/**
* An {@link Alert} detailing how the analog absolute encoder may not report accurate velocities.
*/
private Alert inaccurateVelocities;
/**
* Construct the Thrifty Encoder as a Swerve Absolute Encoder.
*
* @param encoder Encoder to construct.
*/
public AnalogAbsoluteEncoderSwerve(AnalogInput encoder)
{
this.encoder = encoder;
cannotSetOffset = new Alert(
"Encoders",
"Cannot Set Absolute Encoder Offset of Analog Encoders Channel #" + encoder.getChannel(),
AlertType.kWarning);
inaccurateVelocities = new Alert(
"Encoders",
"The Analog Absolute encoder may not report accurate velocities!",
AlertType.kWarning);
}
@Override
public void close()
{
encoder.close();
}
/**
* Construct the Encoder given the analog input channel.
*
* @param channel Analog Input channel of which the encoder resides.
*/
public AnalogAbsoluteEncoderSwerve(int channel)
{
this(new AnalogInput(channel));
}
/**
* Reset the encoder to factory defaults.
*/
@Override
public void factoryDefault()
{
// Do nothing
}
/**
* Clear sticky faults on the encoder.
*/
@Override
public void clearStickyFaults()
{
// Do nothing
}
/**
* Configure the absolute encoder to read from [0, 360) per second.
*
* @param inverted Whether the encoder is inverted.
*/
@Override
public void configure(boolean inverted)
{
this.inverted = inverted;
}
/**
* Get the absolute position of the encoder.
*
* @return Absolute position in degrees from [0, 360).
*/
@Override
public double getAbsolutePosition()
{
return (inverted ? -1.0 : 1.0) * (encoder.getAverageVoltage() / RobotController.getVoltage5V()) * 360;
}
/**
* Get the instantiated absolute encoder Object.
*
* @return Absolute encoder object.
*/
@Override
public Object getAbsoluteEncoder()
{
return encoder;
}
/**
* Cannot Set the offset of an Analog Absolute Encoder.
*
* @param offset the offset the Absolute Encoder uses as the zero point.
* @return Will always be false as setting the offset is unsupported of an Analog absolute encoder.
*/
@Override
public boolean setAbsoluteEncoderOffset(double offset)
{
//Do Nothing
cannotSetOffset.set(true);
return false;
}
/**
* Get the velocity in degrees/sec.
*
* @return velocity in degrees/sec.
*/
@Override
public double getVelocity()
{
inaccurateVelocities.set(true);
return encoder.getValue() * 360;
}
}