Skip to content

Commit 87fad1c

Browse files
authored
Update AnalogInput article to use snippets (#3031)
1 parent f8306ce commit 87fad1c

File tree

1 file changed

+56
-100
lines changed

1 file changed

+56
-100
lines changed

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

Lines changed: 56 additions & 100 deletions
Original file line numberDiff line numberDiff line change
@@ -18,15 +18,13 @@ An :code:`AnalogInput` may be initialized as follows:
1818

1919
.. tab-set-code::
2020

21-
```java
22-
// Initializes an AnalogInput on port 0
23-
AnalogInput analog = new AnalogInput(0);
24-
```
21+
.. remoteliteralinclude:: https://raw.githubusercontent.com/wpilibsuite/allwpilib/b96264f0421ea4ddce0d7b721d995e60db8ab5ad/wpilibjExamples/src/main/java/edu/wpi/first/wpilibj/snippets/analoginput/Robot.java
22+
:language: java
23+
:lines: 17-18
2524

26-
```c++
27-
// Initializes an AnalogInput on port 0
28-
frc::AnalogInput analog{0};
29-
```
25+
.. remoteliteralinclude:: https://raw.githubusercontent.com/wpilibsuite/allwpilib/b96264f0421ea4ddce0d7b721d995e60db8ab5ad/wpilibcExamples/src/main/cpp/snippets/AnalogInput/cpp/Robot.cpp
26+
:language: c++
27+
:lines: 63-64
3028

3129
### Oversampling and Averaging
3230

@@ -41,37 +39,27 @@ When oversampling is enabled, the FPGA will add multiple consecutive samples tog
4139

4240
.. tab-set-code::
4341

44-
```java
45-
// Sets the AnalogInput to 4-bit oversampling. 16 samples will be added together.
46-
// Thus, the reported values will increase by about a factor of 16, and the update
47-
// rate will decrease by a similar amount.
48-
analog.setOversampleBits(4);
49-
```
42+
.. remoteliteralinclude:: https://raw.githubusercontent.com/wpilibsuite/allwpilib/b96264f0421ea4ddce0d7b721d995e60db8ab5ad/wpilibjExamples/src/main/java/edu/wpi/first/wpilibj/snippets/analoginput/Robot.java
43+
:language: java
44+
:lines: 25-28
5045

51-
```c++
52-
// Sets the AnalogInput to 4-bit oversampling. 16 samples will be added together.
53-
// Thus, the reported values will increase by about a factor of 16, and the update
54-
// rate will decrease by a similar amount.
55-
analog.SetOversampleBits(4);
56-
```
46+
.. remoteliteralinclude:: https://raw.githubusercontent.com/wpilibsuite/allwpilib/b96264f0421ea4ddce0d7b721d995e60db8ab5ad/wpilibcExamples/src/main/cpp/snippets/AnalogInput/cpp/Robot.cpp
47+
:language: c++
48+
:lines: 15-19
5749

5850
#### Averaging
5951

6052
Averaging behaves much like oversampling, except the accumulated values are divided by the number of samples so that the scaling of the returned values does not change. This is often more-convenient, but occasionally the additional roundoff error introduced by the rounding is undesirable.
6153

6254
.. tab-set-code::
6355

64-
```java
65-
// Sets the AnalogInput to 4-bit averaging. 16 samples will be averaged together.
66-
// The update rate will decrease by a factor of 16.
67-
analog.setAverageBits(4);
68-
```
56+
.. remoteliteralinclude:: https://raw.githubusercontent.com/wpilibsuite/allwpilib/b96264f0421ea4ddce0d7b721d995e60db8ab5ad/wpilibjExamples/src/main/java/edu/wpi/first/wpilibj/snippets/analoginput/Robot.java
57+
:language: java
58+
:lines: 30-32
6959

70-
```c++
71-
// Sets the AnalogInput to 4-bit averaging. 16 samples will be averaged together.
72-
// The update rate will decrease by a factor of 16.
73-
analog.SetAverageBits(4);
74-
```
60+
.. remoteliteralinclude:: https://raw.githubusercontent.com/wpilibsuite/allwpilib/b96264f0421ea4ddce0d7b721d995e60db8ab5ad/wpilibcExamples/src/main/cpp/snippets/AnalogInput/cpp/Robot.cpp
61+
:language: c++
62+
:lines: 21-23
7563

7664
.. note:: When oversampling and averaging are used at the same time, the oversampling is applied *first,* and then the oversampled values are averaged. Thus, 2-bit oversampling and 2-bit averaging used at the same time will increase the scale of the returned values by approximately a factor of 2, and decrease the update rate by approximately a factor of 4.
7765

@@ -85,55 +73,55 @@ The :code:`getValue` method returns the raw instantaneous measured value from th
8573

8674
.. tab-set-code::
8775

88-
```java
89-
analog.getValue();
90-
```
76+
.. remoteliteralinclude:: https://raw.githubusercontent.com/wpilibsuite/allwpilib/b96264f0421ea4ddce0d7b721d995e60db8ab5ad/wpilibjExamples/src/main/java/edu/wpi/first/wpilibj/snippets/analoginput/Robot.java
77+
:language: java
78+
:lines: 37-40
9179

92-
```c++
93-
analog.GetValue();
94-
```
80+
.. remoteliteralinclude:: https://raw.githubusercontent.com/wpilibsuite/allwpilib/b96264f0421ea4ddce0d7b721d995e60db8ab5ad/wpilibcExamples/src/main/cpp/snippets/AnalogInput/cpp/Robot.cpp
81+
:language: c++
82+
:lines: 25-28
9583

9684
#### getVoltage
9785

9886
The :code:`getVoltage` method returns the instantaneous measured voltage from the analog input. Oversampling and averaging settings are ignored, but the value is rescaled to represent a voltage. The returned value is a double.
9987

10088
.. tab-set-code::
10189

102-
```java
103-
analog.getVoltage();
104-
```
90+
.. remoteliteralinclude:: https://raw.githubusercontent.com/wpilibsuite/allwpilib/b96264f0421ea4ddce0d7b721d995e60db8ab5ad/wpilibjExamples/src/main/java/edu/wpi/first/wpilibj/snippets/analoginput/Robot.java
91+
:language: java
92+
:lines: 42-44
10593

106-
```c++
107-
analog.GetVoltage();
108-
```
94+
.. remoteliteralinclude:: https://raw.githubusercontent.com/wpilibsuite/allwpilib/b96264f0421ea4ddce0d7b721d995e60db8ab5ad/wpilibcExamples/src/main/cpp/snippets/AnalogInput/cpp/Robot.cpp
95+
:language: c++
96+
:lines: 30-32
10997

11098
#### getAverageValue
11199

112100
The :code:`getAverageValue` method returns the averaged value from the analog input. The value is not rescaled, but oversampling and averaging are both applied. The returned value is an integer.
113101

114102
.. tab-set-code::
115103

116-
```java
117-
analog.getAverageValue();
118-
```
104+
.. remoteliteralinclude:: https://raw.githubusercontent.com/wpilibsuite/allwpilib/b96264f0421ea4ddce0d7b721d995e60db8ab5ad/wpilibjExamples/src/main/java/edu/wpi/first/wpilibj/snippets/analoginput/Robot.java
105+
:language: java
106+
:lines: 46-48
119107

120-
```c++
121-
analog.GetAverageValue();
122-
```
108+
.. remoteliteralinclude:: https://raw.githubusercontent.com/wpilibsuite/allwpilib/b96264f0421ea4ddce0d7b721d995e60db8ab5ad/wpilibcExamples/src/main/cpp/snippets/AnalogInput/cpp/Robot.cpp
109+
:language: c++
110+
:lines: 34-36
123111

124112
#### getAverageVoltage
125113

126114
The :code:`getAverageVoltage` method returns the averaged voltage from the analog input. Rescaling, oversampling, and averaging are all applied. The returned value is a double.
127115

128116
.. tab-set-code::
129117

130-
```java
131-
analog.getAverageVoltage();
132-
```
118+
.. remoteliteralinclude:: https://raw.githubusercontent.com/wpilibsuite/allwpilib/b96264f0421ea4ddce0d7b721d995e60db8ab5ad/wpilibjExamples/src/main/java/edu/wpi/first/wpilibj/snippets/analoginput/Robot.java
119+
:language: java
120+
:lines: 46-48
133121

134-
```c++
135-
analog.GetAverageVoltage();
136-
```
122+
.. remoteliteralinclude:: https://raw.githubusercontent.com/wpilibsuite/allwpilib/b96264f0421ea4ddce0d7b721d995e60db8ab5ad/wpilibcExamples/src/main/cpp/snippets/AnalogInput/cpp/Robot.cpp
123+
:language: c++
124+
:lines: 38-40
137125

138126
### Accumulator
139127

@@ -143,59 +131,27 @@ Analog input channels 0 and 1 additionally support an accumulator, which integra
143131

144132
.. tab-set-code::
145133

146-
```java
147-
// Sets the initial value of the accumulator to 0
148-
// This is the "starting point" from which the value will change over time
149-
analog.setAccumulatorInitialValue(0);
150-
// Sets the "center" of the accumulator to 0. This value is subtracted from
151-
// all measured values prior to accumulation.
152-
analog.setAccumulatorCenter(0);
153-
// Returns the number of accumulated samples since the accumulator was last started/reset
154-
analog.getAccumulatorCount();
155-
// Returns the value of the accumulator. Return type is long.
156-
analog.getAccumulatorValue();
157-
// Resets the accumulator to the initial value
158-
analog.resetAccumulator();
159-
```
160-
161-
```c++
162-
// Sets the initial value of the accumulator to 0
163-
// This is the "starting point" from which the value will change over time
164-
analog.SetAccumulatorInitialValue(0);
165-
// Sets the "center" of the accumulator to 0. This value is subtracted from
166-
// all measured values prior to accumulation.
167-
analog.SetAccumulatorCenter(0);
168-
// Returns the number of accumulated samples since the accumulator was last started/reset
169-
analog.GetAccumulatorCount();
170-
// Returns the value of the accumulator. Return type is long.
171-
analog.GetAccumulatorValue();
172-
// Resets the accumulator to the initial value
173-
analog.ResetAccumulator();
174-
```
134+
.. remoteliteralinclude:: https://raw.githubusercontent.com/wpilibsuite/allwpilib/b96264f0421ea4ddce0d7b721d995e60db8ab5ad/wpilibjExamples/src/main/java/edu/wpi/first/wpilibj/snippets/analoginput/Robot.java
135+
:language: java
136+
:lines: 42-44
137+
138+
.. remoteliteralinclude:: https://raw.githubusercontent.com/wpilibsuite/allwpilib/b96264f0421ea4ddce0d7b721d995e60db8ab5ad/wpilibcExamples/src/main/cpp/snippets/AnalogInput/cpp/Robot.cpp
139+
:language: c++
140+
:lines: 42-54
175141

176142
#### Obtaining synchronized count and value
177143

178144
Sometimes, it is necessarily to obtain matched measurements of the count and the value. This can be done using the :code:`getAccumulatorOutput` method:
179145

180146
.. tab-set-code::
181147

182-
```java
183-
// Instantiate an AccumulatorResult object to hold the matched measurements
184-
AccumulatorResult result = new AccumulatorResult();
185-
// Fill the AccumulatorResult with the matched measurements
186-
analog.getAccumulatorOutput(result);
187-
// Read the values from the AccumulatorResult
188-
long count = result.count;
189-
long value = result.value;
190-
```
191-
192-
```c++
193-
// The count and value variables to fill
194-
int_64t count;
195-
int_64t value;
196-
// Fill the count and value variables with the matched measurements
197-
analog.GetAccumulatorOutput(count, value);
198-
```
148+
.. remoteliteralinclude:: https://raw.githubusercontent.com/wpilibsuite/allwpilib/b96264f0421ea4ddce0d7b721d995e60db8ab5ad/wpilibjExamples/src/main/java/edu/wpi/first/wpilibj/snippets/analoginput/Robot.java
149+
:language: java
150+
:lines: 20-21, 67-71
151+
152+
.. remoteliteralinclude:: https://raw.githubusercontent.com/wpilibsuite/allwpilib/b96264f0421ea4ddce0d7b721d995e60db8ab5ad/wpilibcExamples/src/main/cpp/snippets/AnalogInput/cpp/Robot.cpp
153+
:language: c++
154+
:lines: 66-68, 58-59
199155

200156
## Using analog inputs in code
201157

0 commit comments

Comments
 (0)