Skip to content

Commit 02bcde0

Browse files
committed
Add encoder example
1 parent eb1e32a commit 02bcde0

File tree

2 files changed

+53
-0
lines changed

2 files changed

+53
-0
lines changed

encoder/robot.py

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
#!/usr/bin/env python3
2+
"""
3+
Sample program displaying the value of a quadrature encoder on the SmartDashboard. Quadrature
4+
Encoders are digital sensors which can detect the amount the encoder has rotated since starting
5+
as well as the direction in which the encoder shaft is rotating. However, encoders can not tell
6+
you the absolute position of the encoder shaft (ie, it considers where it starts to be the zero
7+
position, no matter where it starts), and so can only tell you how much the encoder has rotated
8+
since starting. Depending on the precision of an encoder, it will have fewer or greater ticks per
9+
revolution; the number of ticks per revolution will affect the conversion between ticks and
10+
distance, as specified by DistancePerPulse. One of the most common uses of encoders is in the
11+
drivetrain, so that the distance that the robot drives can be precisely controlled during the
12+
autonomous mode.
13+
"""
14+
15+
import wpilib
16+
17+
import math
18+
19+
20+
class MyRobot(wpilib.TimedRobot):
21+
22+
def robotInit(self):
23+
"""Robot initialization function"""
24+
25+
self.encoder = wpilib.Encoder(1, 2, False, wpilib.Encoder.EncodingType.k4X)
26+
27+
# Defines the number of samples to average when determining the rate.
28+
# On a quadrature encoder, values range from 1-255;
29+
# larger values result in smoother but potentially
30+
# less accurate rates than lower values.
31+
self.encoder.setSamplesToAverage(5)
32+
33+
# Defines how far the mechanism attached to the encoder moves per pulse. In
34+
# this case, we assume that a 360 count encoder is directly
35+
# attached to a 3 inch diameter (1.5inch radius) wheel,
36+
# and that we want to measure distance in inches.
37+
self.encoder.setDistancePerPulse(1.0 / 360.0 * 2.0 * math.pi * 1.5)
38+
39+
# Defines the lowest rate at which the encoder will
40+
# not be considered stopped, for the purposes of
41+
# the GetStopped() method. Units are in distance / second,
42+
# where distance refers to the units of distance
43+
# that you are using, in this case inches.
44+
self.encoder.setMinRate(1.0)
45+
46+
def teleopPeriodic(self):
47+
wpilib.SmartDashboard.putNumber("Encoder Distance", self.encoder.getDistance())
48+
wpilib.SmartDashboard.putNumber("Encoder Rate", self.encoder.getRate())
49+
50+
51+
if __name__ == "__main__":
52+
wpilib.run(MyRobot)

run_tests.sh

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ BASE_TESTS="
2020
elevator-profiled-pid
2121
elevator-simulation
2222
elevator-trapezoid-profile
23+
encoder
2324
game-data
2425
getting-started
2526
gyro

0 commit comments

Comments
 (0)