Skip to content

Commit 75b907f

Browse files
authored
Update Encoder article to use Encoder snippets (#3017)
Enabled by wpilibsuite/allwpilib#7909
1 parent a861dd9 commit 75b907f

File tree

1 file changed

+54
-96
lines changed

1 file changed

+54
-96
lines changed

source/docs/software/hardware-apis/sensors/encoders-software.rst

Lines changed: 54 additions & 96 deletions
Original file line numberDiff line numberDiff line change
@@ -37,17 +37,13 @@ A quadrature encoder can be instantiated as follows:
3737

3838
.. tab-set-code::
3939

40-
```java
41-
// Initializes an encoder on DIO pins 0 and 1
42-
// Defaults to 4X decoding and non-inverted
43-
Encoder encoder = new Encoder(0, 1);
44-
```
40+
.. remoteliteralinclude:: https://raw.githubusercontent.com/wpilibsuite/allwpilib/b2486633862f86092354df5c1bd92f5db457afcb/wpilibjExamples/src/main/java/edu/wpi/first/wpilibj/snippets/encoder/Robot.java
41+
:language: java
42+
:lines: 16-18
4543

46-
```c++
47-
// Initializes an encoder on DIO pins 0 and 1
48-
// Defaults to 4X decoding and non-inverted
49-
frc::Encoder encoder{0, 1};
50-
```
44+
.. remoteliteralinclude:: https://raw.githubusercontent.com/wpilibsuite/allwpilib/b2486633862f86092354df5c1bd92f5db457afcb/wpilibcExamples/src/main/cpp/snippets/Encoder/cpp/Robot.cpp
45+
:language: c++
46+
:lines: 56-58
5147

5248
#### Decoding Type
5349

@@ -61,17 +57,13 @@ The WPILib :code:`Encoder` class can decode encoder signals in three different m
6157

6258
.. tab-set-code::
6359

64-
```java
65-
// Initializes an encoder on DIO pins 0 and 1
66-
// 2X encoding and non-inverted
67-
Encoder encoder = new Encoder(0, 1, false, Encoder.EncodingType.k2X);
68-
```
60+
.. remoteliteralinclude:: https://raw.githubusercontent.com/wpilibsuite/allwpilib/b2486633862f86092354df5c1bd92f5db457afcb/wpilibjExamples/src/main/java/edu/wpi/first/wpilibj/snippets/encoder/Robot.java
61+
:language: java
62+
:lines: 20-22
6963

70-
```c++
71-
// Initializes an encoder on DIO pins 0 and 1
72-
// 2X encoding and non-inverted
73-
frc::Encoder encoder{0, 1, false, frc::Encoder::EncodingType::k2X};
74-
```
64+
.. remoteliteralinclude:: https://raw.githubusercontent.com/wpilibsuite/allwpilib/b2486633862f86092354df5c1bd92f5db457afcb/wpilibcExamples/src/main/cpp/snippets/Encoder/cpp/Robot.cpp
65+
:language: c++
66+
:lines: 60-62
7567

7668
### Configuring Quadrature Encoder Parameters
7769

@@ -83,35 +75,13 @@ The :code:`Encoder` class offers a number of configuration methods:
8375

8476
.. tab-set-code::
8577

86-
```java
87-
// Configures the encoder to return a distance of 4 for every 256 pulses
88-
// Also changes the units of getRate
89-
encoder.setDistancePerPulse(4.0/256.0);
90-
// Configures the encoder to consider itself stopped after .1 seconds
91-
encoder.setMaxPeriod(0.1);
92-
// Configures the encoder to consider itself stopped when its rate is below 10
93-
encoder.setMinRate(10);
94-
// Reverses the direction of the encoder
95-
encoder.setReverseDirection(true);
96-
// Configures an encoder to average its period measurement over 5 samples
97-
// Can be between 1 and 127 samples
98-
encoder.setSamplesToAverage(5);
99-
```
78+
.. remoteliteralinclude:: https://raw.githubusercontent.com/wpilibsuite/allwpilib/b2486633862f86092354df5c1bd92f5db457afcb/wpilibjExamples/src/main/java/edu/wpi/first/wpilibj/snippets/encoder/Robot.java
79+
:language: java
80+
:lines: 26-37
10081

101-
```c++
102-
// Configures the encoder to return a distance of 4 for every 256 pulses
103-
// Also changes the units of getRate
104-
encoder.SetDistancePerPulse(4.0/256.0);
105-
// Configures the encoder to consider itself stopped after .1 seconds
106-
encoder.SetMaxPeriod(0.1);
107-
// Configures the encoder to consider itself stopped when its rate is below 10
108-
encoder.SetMinRate(10);
109-
// Reverses the direction of the encoder
110-
encoder.SetReverseDirection(true);
111-
// Configures an encoder to average its period measurement over 5 samples
112-
// Can be between 1 and 127 samples
113-
encoder.SetSamplesToAverage(5);
114-
```
82+
.. remoteliteralinclude:: https://raw.githubusercontent.com/wpilibsuite/allwpilib/b2486633862f86092354df5c1bd92f5db457afcb/wpilibcExamples/src/main/cpp/snippets/Encoder/cpp/Robot.cpp
83+
:language: c++
84+
:lines: 20-32
11585

11686
### Reading information from Quadrature Encoders
11787

@@ -125,15 +95,13 @@ Users can obtain the total distance traveled by the encoder with the :code:`getD
12595

12696
.. tab-set-code::
12797

128-
```java
129-
// Gets the distance traveled
130-
encoder.getDistance();
131-
```
98+
.. remoteliteralinclude:: https://raw.githubusercontent.com/wpilibsuite/allwpilib/b2486633862f86092354df5c1bd92f5db457afcb/wpilibjExamples/src/main/java/edu/wpi/first/wpilibj/snippets/encoder/Robot.java
99+
:language: java
100+
:lines: 44-45
132101

133-
```c++
134-
// Gets the distance traveled
135-
encoder.GetDistance();
136-
```
102+
.. remoteliteralinclude:: https://raw.githubusercontent.com/wpilibsuite/allwpilib/b2486633862f86092354df5c1bd92f5db457afcb/wpilibcExamples/src/main/cpp/snippets/Encoder/cpp/Robot.cpp
103+
:language: c++
104+
:lines: 36-37
137105

138106
#### Rate
139107

@@ -143,79 +111,69 @@ Users can obtain the current rate of change of the encoder with the :code:`getRa
143111

144112
.. tab-set-code::
145113

146-
```java
147-
// Gets the current rate of the encoder
148-
encoder.getRate();
149-
```
114+
.. remoteliteralinclude:: https://raw.githubusercontent.com/wpilibsuite/allwpilib/b2486633862f86092354df5c1bd92f5db457afcb/wpilibjExamples/src/main/java/edu/wpi/first/wpilibj/snippets/encoder/Robot.java
115+
:language: java
116+
:lines: 47-48
150117

151-
```c++
152-
// Gets the current rate of the encoder
153-
encoder.GetRate();
154-
```
118+
.. remoteliteralinclude:: https://raw.githubusercontent.com/wpilibsuite/allwpilib/b2486633862f86092354df5c1bd92f5db457afcb/wpilibcExamples/src/main/cpp/snippets/Encoder/cpp/Robot.cpp
119+
:language: c++
120+
:lines: 39-40
155121

156122
#### Stopped
157123

158124
Users can obtain whether the encoder is stationary with the :code:`getStopped()` method:
159125

160126
.. tab-set-code::
161127

162-
```java
163-
// Gets whether the encoder is stopped
164-
encoder.getStopped();
165-
```
128+
.. remoteliteralinclude:: https://raw.githubusercontent.com/wpilibsuite/allwpilib/b2486633862f86092354df5c1bd92f5db457afcb/wpilibjExamples/src/main/java/edu/wpi/first/wpilibj/snippets/encoder/Robot.java
129+
:language: java
130+
:lines: 50-51
166131

167-
```c++
168-
// Gets whether the encoder is stopped
169-
encoder.GetStopped();
170-
```
132+
.. remoteliteralinclude:: https://raw.githubusercontent.com/wpilibsuite/allwpilib/b2486633862f86092354df5c1bd92f5db457afcb/wpilibcExamples/src/main/cpp/snippets/Encoder/cpp/Robot.cpp
133+
:language: c++
134+
:lines: 42-43
171135

172136
#### Direction
173137

174138
Users can obtain the direction in which the encoder last moved with the :code:`getDirection()` method:
175139

176140
.. tab-set-code::
177141

178-
```java
179-
// Gets the last direction in which the encoder moved
180-
encoder.getDirection();
181-
```
142+
.. remoteliteralinclude:: https://raw.githubusercontent.com/wpilibsuite/allwpilib/b2486633862f86092354df5c1bd92f5db457afcb/wpilibjExamples/src/main/java/edu/wpi/first/wpilibj/snippets/encoder/Robot.java
143+
:language: java
144+
:lines: 53-54
182145

183-
```c++
184-
// Gets the last direction in which the encoder moved
185-
encoder.GetDirection();
186-
```
146+
.. remoteliteralinclude:: https://raw.githubusercontent.com/wpilibsuite/allwpilib/b2486633862f86092354df5c1bd92f5db457afcb/wpilibcExamples/src/main/cpp/snippets/Encoder/cpp/Robot.cpp
147+
:language: c++
148+
:lines: 45-46
187149

188150
#### Period
189151

190152
Users can obtain the period of the encoder pulses (in seconds) with the :code:`getPeriod()` method:
191153

192154
.. tab-set-code::
193155

194-
```java
195-
// Gets the current period of the encoder
196-
encoder.getPeriod();
197-
```
156+
.. remoteliteralinclude:: https://raw.githubusercontent.com/wpilibsuite/allwpilib/b2486633862f86092354df5c1bd92f5db457afcb/wpilibjExamples/src/main/java/edu/wpi/first/wpilibj/snippets/encoder/Robot.java
157+
:language: java
158+
:lines: 56-57
198159

199-
```c++
200-
// Gets the current period of the encoder
201-
encoder.GetPeriod();
202-
```
160+
.. remoteliteralinclude:: https://raw.githubusercontent.com/wpilibsuite/allwpilib/b2486633862f86092354df5c1bd92f5db457afcb/wpilibcExamples/src/main/cpp/snippets/Encoder/cpp/Robot.cpp
161+
:language: c++
162+
:lines: 48-49
203163

204164
### Resetting a Quadrature Encoder
205165

206166
To reset a quadrature encoder to a distance reading of zero, call the :code:`reset()` method. This is useful for ensuring that the measured distance corresponds to the actual desired physical measurement, and is often called during a :ref:`homing <docs/software/hardware-apis/sensors/encoders-software:Homing a mechanism>` routine:
207167

208168
.. tab-set-code::
209169

210-
```java
211-
// Resets the encoder to read a distance of zero
212-
encoder.reset();
213-
```
170+
.. remoteliteralinclude:: https://raw.githubusercontent.com/wpilibsuite/allwpilib/b2486633862f86092354df5c1bd92f5db457afcb/wpilibjExamples/src/main/java/edu/wpi/first/wpilibj/snippets/encoder/Robot.java
171+
:language: java
172+
:lines: 59-60
214173

215-
```c++
216-
// Resets the encoder to read a distance of zero
217-
encoder.Reset();
218-
```
174+
.. remoteliteralinclude:: https://raw.githubusercontent.com/wpilibsuite/allwpilib/b2486633862f86092354df5c1bd92f5db457afcb/wpilibcExamples/src/main/cpp/snippets/Encoder/cpp/Robot.cpp
175+
:language: c++
176+
:lines: 51-52
219177

220178
## Duty Cycle Encoders - The :code:`DutyCycleEncoder` class
221179

0 commit comments

Comments
 (0)