Skip to content

Commit 6441cac

Browse files
authored
Update remainder of Encoder article with snippets RLI (#3025)
1 parent 5f6d40f commit 6441cac

File tree

1 file changed

+33
-124
lines changed

1 file changed

+33
-124
lines changed

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

Lines changed: 33 additions & 124 deletions
Original file line numberDiff line numberDiff line change
@@ -269,15 +269,13 @@ An analog encoder can be instantiated as follows:
269269

270270
.. tab-set-code::
271271

272-
```java
273-
// Initializes an analog encoder on Analog Input pin 0
274-
AnalogEncoder encoder = new AnalogEncoder(0);
275-
```
272+
.. remoteliteralinclude:: https://raw.githubusercontent.com/wpilibsuite/allwpilib/40ce42712fb6b4f2ee8b5a6d6cc31fdd262eedec/wpilibjExamples/src/main/java/edu/wpi/first/wpilibj/snippets/analogencoder/Robot.java
273+
:language: java
274+
:lines: 15-16
276275

277-
```c++
278-
// Initializes an analog encoder on Analog Input pin 0
279-
frc::AnalogEncoder encoder{0};
280-
```
276+
.. remoteliteralinclude:: https://raw.githubusercontent.com/wpilibsuite/allwpilib/40ce42712fb6b4f2ee8b5a6d6cc31fdd262eedec/wpilibcExamples/src/main/cpp/snippets/AnalogEncoder/cpp/Robot.cpp
277+
:language: c++
278+
:lines: 24-25
281279

282280
### Configuring Analog Encoder Range and Zero
283281

@@ -289,19 +287,13 @@ The zero position is useful for ensuring that the measured rotation corresponds
289287

290288
.. tab-set-code::
291289

292-
```java
293-
// Initializes an analog encoder on DIO pins 0 to return a value of 4 for
294-
// a full rotation, with the encoder reporting 0 half way through rotation (2
295-
// out of 4)
296-
AnalogEncoder encoder = new AnalogEncoder(0, 4.0, 2.0);
297-
```
290+
.. remoteliteralinclude:: https://raw.githubusercontent.com/wpilibsuite/allwpilib/40ce42712fb6b4f2ee8b5a6d6cc31fdd262eedec/wpilibjExamples/src/main/java/edu/wpi/first/wpilibj/snippets/analogencoder/Robot.java
291+
:language: java
292+
:lines: 18-21
298293

299-
```c++
300-
// Initializes an analog encoder on DIO pins 0 to return a value of 4 for
301-
// a full rotation, with the encoder reporting 0 half way through rotation (2
302-
// out of 4)
303-
frc::AnalogEncoder encoder{0, 4.0, 2.0};
304-
```
294+
.. remoteliteralinclude:: https://raw.githubusercontent.com/wpilibsuite/allwpilib/40ce42712fb6b4f2ee8b5a6d6cc31fdd262eedec/wpilibcExamples/src/main/cpp/snippets/AnalogEncoder/cpp/Robot.cpp
295+
:language: c++
296+
:lines: 27-30
305297

306298
### Reading Rotation from Analog Encoders
307299

@@ -311,15 +303,13 @@ Users can obtain the rotation measured by the encoder with the :code:`get()` met
311303

312304
.. tab-set-code::
313305

314-
```java
315-
// Gets the rotation
316-
encoder.get();
317-
```
306+
.. remoteliteralinclude:: https://raw.githubusercontent.com/wpilibsuite/allwpilib/40ce42712fb6b4f2ee8b5a6d6cc31fdd262eedec/wpilibjExamples/src/main/java/edu/wpi/first/wpilibj/snippets/analogencoder/Robot.java
307+
:language: java
308+
:lines: 28-29
318309

319-
```c++
320-
// Gets the rotation
321-
encoder.Get();
322-
```
310+
.. remoteliteralinclude:: https://raw.githubusercontent.com/wpilibsuite/allwpilib/40ce42712fb6b4f2ee8b5a6d6cc31fdd262eedec/wpilibcExamples/src/main/cpp/snippets/AnalogEncoder/cpp/Robot.cpp
311+
:language: c++
312+
:lines: 19-20
323313

324314
## Using Encoders in Code
325315

@@ -333,69 +323,14 @@ Encoders can be used on a robot drive to create a simple "drive to distance" rou
333323

334324
.. tab-set-code::
335325

336-
```java
337-
// Creates an encoder on DIO ports 0 and 1
338-
Encoder encoder = new Encoder(0, 1);
339-
// Initialize motor controllers and drive
340-
Spark leftLeader = new Spark(0);
341-
Spark leftFollower = new Spark(1);
342-
Spark rightLeader = new Spark(2);
343-
Spark rightFollower = new Spark(3);
344-
DifferentialDrive drive = new DifferentialDrive(leftLeader::set, rightLeader::set);
345-
346-
public Robot() {
347-
// Configures the encoder's distance-per-pulse
348-
// The robot moves forward 1 foot per encoder rotation
349-
// There are 256 pulses per encoder rotation
350-
encoder.setDistancePerPulse(1./256.);
351-
// Invert the right side of the drivetrain. You might have to invert the other side
352-
rightLeader.setInverted(true);
353-
// Configure the followers to follow the leaders
354-
leftLeader.addFollower(leftFollower);
355-
rightLeader.addFollower(rightFollower);
356-
}
357-
358-
@Override
359-
public void autonomousPeriodic() {
360-
// Drives forward at half speed until the robot has moved 5 feet, then stops:
361-
if(encoder.getDistance() < 5) {
362-
drive.tankDrive(0.5, 0.5);
363-
} else {
364-
drive.tankDrive(0, 0);
365-
}
366-
}
367-
```
368-
369-
```c++
370-
// Creates an encoder on DIO ports 0 and 1.
371-
frc::Encoder encoder{0, 1};
372-
// Initialize motor controllers and drive
373-
frc::Spark leftLeader{0};
374-
frc::Spark leftFollower{1};
375-
frc::Spark rightLeader{2};
376-
frc::Spark rightFollower{3};
377-
frc::DifferentialDrive drive{[&](double output) { leftLeader.Set(output); },
378-
[&](double output) { rightLeader.Set(output); }};
379-
Robot::Robot() {
380-
// Configures the encoder's distance-per-pulse
381-
// The robot moves forward 1 foot per encoder rotation
382-
// There are 256 pulses per encoder rotation
383-
encoder.SetDistancePerPulse(1.0/256.0);
384-
// Invert the right side of the drivetrain. You might have to invert the other side
385-
rightLeader.SetInverted(true);
386-
// Configure the followers to follow the leaders
387-
leftLeader.AddFollower(leftFollower);
388-
rightLeader.AddFollower(rightFollower);
389-
}
390-
void Robot::AutonomousPeriodic() {
391-
// Drives forward at half speed until the robot has moved 5 feet, then stops:
392-
if(encoder.GetDistance() < 5) {
393-
drive.TankDrive(0.5, 0.5);
394-
} else {
395-
drive.TankDrive(0, 0);
396-
}
397-
}
398-
```
326+
.. remoteliteralinclude:: https://raw.githubusercontent.com/wpilibsuite/allwpilib/40ce42712fb6b4f2ee8b5a6d6cc31fdd262eedec/wpilibjExamples/src/main/java/edu/wpi/first/wpilibj/snippets/encoderdrive/Robot.java
327+
:language: java
328+
:lines: 17-47
329+
330+
.. remoteliteralinclude:: https://raw.githubusercontent.com/wpilibsuite/allwpilib/40ce42712fb6b4f2ee8b5a6d6cc31fdd262eedec/wpilibcExamples/src/main/cpp/snippets/EncoderDrive/cpp/Robot.cpp
331+
:language: c++
332+
:lines: 41-49, 18-38
333+
399334

400335
### Homing a Mechanism
401336

@@ -405,37 +340,11 @@ Since quadrature encoders measure *relative* distance, it is often important to
405340

406341
.. tab-set-code::
407342

408-
```java
409-
Encoder encoder = new Encoder(0, 1);
410-
Spark spark = new Spark(0);
411-
// Limit switch on DIO 2
412-
DigitalInput limit = new DigitalInput(2);
413-
public void autonomousPeriodic() {
414-
// Runs the motor backwards at half speed until the limit switch is pressed
415-
// then turn off the motor and reset the encoder
416-
if(!limit.get()) {
417-
spark.set(-0.5);
418-
} else {
419-
spark.set(0);
420-
encoder.reset();
421-
}
422-
}
423-
```
424-
425-
```c++
426-
frc::Encoder encoder{0,1};
427-
frc::Spark spark{0};
428-
// Limit switch on DIO 2
429-
frc::DigitalInput limit{2};
430-
void AutonomousPeriodic() {
431-
// Runs the motor backwards at half speed until the limit switch is pressed
432-
// then turn off the motor and reset the encoder
433-
if(!limit.Get()) {
434-
spark.Set(-0.5);
435-
} else {
436-
spark.Set(0);
437-
encoder.Reset();
438-
}
439-
}
440-
```
343+
.. remoteliteralinclude:: https://raw.githubusercontent.com/wpilibsuite/allwpilib/40ce42712fb6b4f2ee8b5a6d6cc31fdd262eedec/wpilibjExamples/src/main/java/edu/wpi/first/wpilibj/snippets/encoderhoming/Robot.java
344+
:language: java
345+
:lines: 17-34
346+
347+
.. remoteliteralinclude:: https://raw.githubusercontent.com/wpilibsuite/allwpilib/40ce42712fb6b4f2ee8b5a6d6cc31fdd262eedec/wpilibcExamples/src/main/cpp/snippets/EncoderHoming/cpp/Robot.cpp
348+
:language: c++
349+
:lines: 30-33, 18-27
441350

0 commit comments

Comments
 (0)