@@ -37,17 +37,13 @@ A quadrature encoder can be instantiated as follows:
37
37
38
38
.. tab-set-code ::
39
39
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
45
43
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
51
47
52
48
#### Decoding Type
53
49
@@ -61,17 +57,13 @@ The WPILib :code:`Encoder` class can decode encoder signals in three different m
61
57
62
58
.. tab-set-code ::
63
59
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
69
63
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
75
67
76
68
### Configuring Quadrature Encoder Parameters
77
69
@@ -83,35 +75,13 @@ The :code:`Encoder` class offers a number of configuration methods:
83
75
84
76
.. tab-set-code ::
85
77
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
100
81
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
115
85
116
86
### Reading information from Quadrature Encoders
117
87
@@ -125,15 +95,13 @@ Users can obtain the total distance traveled by the encoder with the :code:`getD
125
95
126
96
.. tab-set-code ::
127
97
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
132
101
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
137
105
138
106
#### Rate
139
107
@@ -143,79 +111,69 @@ Users can obtain the current rate of change of the encoder with the :code:`getRa
143
111
144
112
.. tab-set-code ::
145
113
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
150
117
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
155
121
156
122
#### Stopped
157
123
158
124
Users can obtain whether the encoder is stationary with the :code: `getStopped() ` method:
159
125
160
126
.. tab-set-code ::
161
127
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
166
131
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
171
135
172
136
#### Direction
173
137
174
138
Users can obtain the direction in which the encoder last moved with the :code: `getDirection() ` method:
175
139
176
140
.. tab-set-code ::
177
141
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
182
145
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
187
149
188
150
#### Period
189
151
190
152
Users can obtain the period of the encoder pulses (in seconds) with the :code: `getPeriod() ` method:
191
153
192
154
.. tab-set-code ::
193
155
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
198
159
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
203
163
204
164
### Resetting a Quadrature Encoder
205
165
206
166
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:
207
167
208
168
.. tab-set-code ::
209
169
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
214
173
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
219
177
220
178
## Duty Cycle Encoders - The :code: `DutyCycleEncoder ` class
221
179
0 commit comments