Skip to content

Commit d122b0d

Browse files
authored
Use snippets for Digital Inputs (#3028)
1 parent 9da515a commit d122b0d

File tree

1 file changed

+26
-79
lines changed

1 file changed

+26
-79
lines changed

source/docs/software/hardware-apis/sensors/digital-inputs-software.rst

Lines changed: 26 additions & 79 deletions
Original file line numberDiff line numberDiff line change
@@ -14,31 +14,27 @@ A :code:`DigitalInput` can be initialized as follows:
1414

1515
.. tab-set-code::
1616

17-
```java
18-
// Initializes a DigitalInput on DIO 0
19-
DigitalInput input = new DigitalInput(0);
20-
```
17+
.. remoteliteralinclude:: https://raw.githubusercontent.com/wpilibsuite/allwpilib/02de5f710e2579f2079a28944e90b48b34d91cac/wpilibjExamples/src/main/java/edu/wpi/first/wpilibj/snippets/digitalinput/Robot.java
18+
:language: java
19+
:lines: 15-16
2120

22-
```c++
23-
// Initializes a DigitalInput on DIO 0
24-
frc::DigitalInput input{0};
25-
```
21+
.. remoteliteralinclude:: https://raw.githubusercontent.com/wpilibsuite/allwpilib/02de5f710e2579f2079a28944e90b48b34d91cac/wpilibcExamples/src/main/cpp/snippets/DigitalInput/cpp/Robot.cpp
22+
:language: c++
23+
:lines: 21-22
2624

2725
### Reading the value of the DigitalInput
2826

2927
The state of the :code:`DigitalInput` can be polled with the :code:`get` method:
3028

3129
.. tab-set-code::
3230

33-
```java
34-
// Gets the value of the digital input. Returns true if the circuit is open.
35-
input.get();
36-
```
31+
.. remoteliteralinclude:: https://raw.githubusercontent.com/wpilibsuite/allwpilib/02de5f710e2579f2079a28944e90b48b34d91cac/wpilibjExamples/src/main/java/edu/wpi/first/wpilibj/snippets/digitalinput/Robot.java
32+
:language: java
33+
:lines: 20-21
3734

38-
```c++
39-
// Gets the value of the digital input. Returns true if the circuit is open.
40-
input.Get();
41-
```
35+
.. remoteliteralinclude:: https://raw.githubusercontent.com/wpilibsuite/allwpilib/02de5f710e2579f2079a28944e90b48b34d91cac/wpilibcExamples/src/main/cpp/snippets/DigitalInput/cpp/Robot.cpp
36+
:language: c++
37+
:lines: 15-17
4238

4339
## Creating a DigitalInput from an AnalogInput
4440

@@ -50,25 +46,13 @@ An :code:`AnalogTrigger` may be initialized as follows. As with :code:`AnalogPo
5046

5147
.. tab-set-code::
5248

53-
```java
54-
// Initializes an AnalogTrigger on port 0
55-
AnalogTrigger trigger0 = new AnalogTrigger(0);
56-
// Initializes an AnalogInput on port 1 and enables 2-bit oversampling
57-
AnalogInput input = new AnalogInput(1);
58-
input.setAverageBits(2);
59-
// Initializes an AnalogTrigger using the above input
60-
AnalogTrigger trigger1 = new AnalogTrigger(input);
61-
```
62-
63-
```c++
64-
// Initializes an AnalogTrigger on port 0
65-
frc::AnalogTrigger trigger0{0};
66-
// Initializes an AnalogInput on port 1 and enables 2-bit oversampling
67-
frc::AnalogInput input{1};
68-
input.SetAverageBits(2);
69-
// Initializes an AnalogTrigger using the above input
70-
frc::AnalogTrigger trigger1{input};
71-
```
49+
.. remoteliteralinclude:: https://raw.githubusercontent.com/wpilibsuite/allwpilib/02de5f710e2579f2079a28944e90b48b34d91cac/wpilibjExamples/src/main/java/edu/wpi/first/wpilibj/snippets/analogtrigger/Robot.java
50+
:language: java
51+
:lines: 16-22
52+
53+
.. remoteliteralinclude:: https://raw.githubusercontent.com/wpilibsuite/allwpilib/02de5f710e2579f2079a28944e90b48b34d91cac/wpilibcExamples/src/main/cpp/snippets/AnalogTrigger/cpp/Robot.cpp
54+
:language: c++
55+
:lines: 27-33
7256

7357
### Setting the trigger points
7458

@@ -78,58 +62,21 @@ To convert the analog signal to a digital one, it is necessary to specify at wha
7862

7963
.. tab-set-code::
8064

81-
```java
82-
// Sets the trigger to enable at a raw value of 3500, and disable at a value of 1000
83-
trigger.setLimitsRaw(1000, 3500);
84-
// Sets the trigger to enable at a voltage of 4 volts, and disable at a value of 1.5 volts
85-
trigger.setLimitsVoltage(1.5, 4);
86-
```
65+
.. remoteliteralinclude:: https://raw.githubusercontent.com/wpilibsuite/allwpilib/02de5f710e2579f2079a28944e90b48b34d91cac/wpilibjExamples/src/main/java/edu/wpi/first/wpilibj/snippets/analogtrigger/Robot.java
66+
:language: java
67+
:lines: 26-31
8768

88-
```c++
89-
// Sets the trigger to enable at a raw value of 3500, and disable at a value of 1000
90-
trigger.SetLimitsRaw(1000, 3500);
91-
// Sets the trigger to enable at a voltage of 4 volts, and disable at a value of 1.5 volts
92-
trigger.SetLimitsVoltage(1.5, 4);
93-
```
69+
.. remoteliteralinclude:: https://raw.githubusercontent.com/wpilibsuite/allwpilib/02de5f710e2579f2079a28944e90b48b34d91cac/wpilibcExamples/src/main/cpp/snippets/AnalogTrigger/cpp/Robot.cpp
70+
:language: c++
71+
:lines: 16-23
9472

9573
## Using DigitalInputs in code
9674

9775
As almost all switches on the robot will be used through a :code:`DigitalInput`. This class is extremely important for effective robot control.
9876

9977
### Limiting the motion of a mechanism
10078

101-
Nearly all motorized mechanisms (such as arms and elevators) in FRC\ |reg| should be given some form of "limit switch" to prevent them from damaging themselves at the end of their range of motions. A short example is given below:
102-
103-
.. tab-set-code::
104-
105-
```java
106-
Spark spark = new Spark(0);
107-
// Limit switch on DIO 2
108-
DigitalInput limit = new DigitalInput(2);
109-
public void autonomousPeriodic() {
110-
// Runs the motor forwards at half speed, unless the limit is pressed
111-
if(!limit.get()) {
112-
spark.set(.5);
113-
} else {
114-
spark.set(0);
115-
}
116-
}
117-
```
118-
119-
```c++
120-
// Motor for the mechanism
121-
frc::Spark spark{0};
122-
// Limit switch on DIO 2
123-
frc::DigitalInput limit{2};
124-
void AutonomousPeriodic() {
125-
// Runs the motor forwards at half speed, unless the limit is pressed
126-
if(!limit.Get()) {
127-
spark.Set(.5);
128-
} else {
129-
spark.Set(0);
130-
}
131-
}
132-
```
79+
Nearly all motorized mechanisms (such as arms and elevators) in FRC\ |reg| should be given some form of "limit switch" to prevent them from damaging themselves at the end of their range of motions. For an example of this, see :doc:`/docs/software/hardware-apis/sensors/limit-switch`.
13380

13481
### Homing a mechanism
13582

0 commit comments

Comments
 (0)