WPILibC++ 2026.2.2
Loading...
Searching...
No Matches
AnalogInput.h
Go to the documentation of this file.
1// Copyright (c) FIRST and other WPILib contributors.
2// Open Source Software; you can modify and/or share it under the terms of
3// the WPILib BSD license file in the root directory of this project.
4
5#pragma once
6
7#include <stdint.h>
8
9#include <hal/AnalogInput.h>
10#include <hal/Types.h>
13
14namespace frc {
15
16class DMA;
17class DMASample;
18
19/**
20 * Analog input class.
21 *
22 * Connected to each analog channel is an averaging and oversampling engine.
23 * This engine accumulates the specified ( by SetAverageBits() and
24 * SetOversampleBits() ) number of samples before returning a new value. This is
25 * not a sliding window average. The only difference between the oversampled
26 * samples and the averaged samples is that the oversampled samples are simply
27 * accumulated effectively increasing the resolution, while the averaged samples
28 * are divided by the number of samples to retain the resolution, but get more
29 * stable values.
30 */
32 public wpi::SendableHelper<AnalogInput> {
33 friend class AnalogTrigger;
34 friend class AnalogGyro;
35 friend class DMA;
36 friend class DMASample;
37
38 public:
39 static constexpr int kAccumulatorModuleNumber = 1;
40 static constexpr int kAccumulatorNumChannels = 2;
41 static constexpr int kAccumulatorChannels[kAccumulatorNumChannels] = {0, 1};
42
43 /**
44 * Construct an analog input.
45 *
46 * @param channel The channel number on the roboRIO to represent. 0-3 are
47 * on-board 4-7 are on the MXP port.
48 */
49 explicit AnalogInput(int channel);
50
53
54 ~AnalogInput() override = default;
55
56 /**
57 * Get a sample straight from this channel.
58 *
59 * The sample is a 12-bit value representing the 0V to 5V range of the A/D
60 * converter in the module. The units are in A/D converter codes. Use
61 * GetVoltage() to get the analog value in calibrated units.
62 *
63 * @return A sample straight from this channel.
64 */
65 int GetValue() const;
66
67 /**
68 * Get a sample from the output of the oversample and average engine for this
69 * channel.
70 *
71 * The sample is 12-bit + the bits configured in SetOversampleBits().
72 * The value configured in SetAverageBits() will cause this value to be
73 * averaged 2**bits number of samples.
74 *
75 * This is not a sliding window. The sample will not change until
76 * 2**(OversampleBits + AverageBits) samples have been acquired from the
77 * module on this channel.
78 *
79 * Use GetAverageVoltage() to get the analog value in calibrated units.
80 *
81 * @return A sample from the oversample and average engine for this channel.
82 */
83 int GetAverageValue() const;
84
85 /**
86 * Get a scaled sample straight from this channel.
87 *
88 * The value is scaled to units of Volts using the calibrated scaling data
89 * from GetLSBWeight() and GetOffset().
90 *
91 * @return A scaled sample straight from this channel.
92 */
93 double GetVoltage() const;
94
95 /**
96 * Get a scaled sample from the output of the oversample and average engine
97 * for this channel.
98 *
99 * The value is scaled to units of Volts using the calibrated scaling data
100 * from GetLSBWeight() and GetOffset().
101 *
102 * Using oversampling will cause this value to be higher resolution, but it
103 * will update more slowly.
104 *
105 * Using averaging will cause this value to be more stable, but it will update
106 * more slowly.
107 *
108 * @return A scaled sample from the output of the oversample and average
109 * engine for this channel.
110 */
111 double GetAverageVoltage() const;
112
113 /**
114 * Get the channel number.
115 *
116 * @return The channel number.
117 */
118 int GetChannel() const;
119
120 /**
121 * Set the number of averaging bits.
122 *
123 * This sets the number of averaging bits. The actual number of averaged
124 * samples is 2^bits.
125 *
126 * Use averaging to improve the stability of your measurement at the expense
127 * of sampling rate. The averaging is done automatically in the FPGA.
128 *
129 * @param bits Number of bits of averaging.
130 */
131 void SetAverageBits(int bits);
132
133 /**
134 * Get the number of averaging bits previously configured.
135 *
136 * This gets the number of averaging bits from the FPGA. The actual number of
137 * averaged samples is 2^bits. The averaging is done automatically in the
138 * FPGA.
139 *
140 * @return Number of bits of averaging previously configured.
141 */
142 int GetAverageBits() const;
143
144 /**
145 * Set the number of oversample bits.
146 *
147 * This sets the number of oversample bits. The actual number of oversampled
148 * values is 2^bits. Use oversampling to improve the resolution of your
149 * measurements at the expense of sampling rate. The oversampling is done
150 * automatically in the FPGA.
151 *
152 * @param bits Number of bits of oversampling.
153 */
154 void SetOversampleBits(int bits);
155
156 /**
157 * Get the number of oversample bits previously configured.
158 *
159 * This gets the number of oversample bits from the FPGA. The actual number of
160 * oversampled values is 2^bits. The oversampling is done automatically in the
161 * FPGA.
162 *
163 * @return Number of bits of oversampling previously configured.
164 */
165 int GetOversampleBits() const;
166
167 /**
168 * Get the factory scaling least significant bit weight constant.
169 *
170 * Volts = ((LSB_Weight * 1e-9) * raw) - (Offset * 1e-9)
171 *
172 * @return Least significant bit weight.
173 */
174 int GetLSBWeight() const;
175
176 /**
177 * Get the factory scaling offset constant.
178 *
179 * Volts = ((LSB_Weight * 1e-9) * raw) - (Offset * 1e-9)
180 *
181 * @return Offset constant.
182 */
183 int GetOffset() const;
184
185 /**
186 * Is the channel attached to an accumulator.
187 *
188 * @return The analog input is attached to an accumulator.
189 */
191
192 /**
193 * Initialize the accumulator.
194 */
196
197 /**
198 * Set an initial value for the accumulator.
199 *
200 * This will be added to all values returned to the user.
201 *
202 * @param value The value that the accumulator should start from when reset.
203 */
204 void SetAccumulatorInitialValue(int64_t value);
205
206 /**
207 * Resets the accumulator to the initial value.
208 */
210
211 /**
212 * Set the center value of the accumulator.
213 *
214 * The center value is subtracted from each A/D value before it is added to
215 * the accumulator. This is used for the center value of devices like gyros
216 * and accelerometers to take the device offset into account when integrating.
217 *
218 * This center value is based on the output of the oversampled and averaged
219 * source from the accumulator channel. Because of this, any non-zero
220 * oversample bits will affect the size of the value for this field.
221 */
223
224 /**
225 * Set the accumulator's deadband.
226 */
227 void SetAccumulatorDeadband(int deadband);
228
229 /**
230 * Read the accumulated value.
231 *
232 * Read the value that has been accumulating.
233 * The accumulator is attached after the oversample and average engine.
234 *
235 * @return The 64-bit value accumulated since the last Reset().
236 */
237 int64_t GetAccumulatorValue() const;
238
239 /**
240 * Read the number of accumulated values.
241 *
242 * Read the count of the accumulated values since the accumulator was last
243 * Reset().
244 *
245 * @return The number of times samples from the channel were accumulated.
246 */
247 int64_t GetAccumulatorCount() const;
248
249 /**
250 * Read the accumulated value and the number of accumulated values atomically.
251 *
252 * This function reads the value and count from the FPGA atomically.
253 * This can be used for averaging.
254 *
255 * @param value Reference to the 64-bit accumulated output.
256 * @param count Reference to the number of accumulation cycles.
257 */
258 void GetAccumulatorOutput(int64_t& value, int64_t& count) const;
259
260 /**
261 * Set the sample rate per channel for all analog channels.
262 *
263 * The maximum rate is 500kS/s divided by the number of channels in use.
264 * This is 62500 samples/s per channel.
265 *
266 * @param samplesPerSecond The number of samples per second.
267 */
268 static void SetSampleRate(double samplesPerSecond);
269
270 /**
271 * Get the current sample rate for all channels
272 *
273 * @return Sample rate.
274 */
275 static double GetSampleRate();
276
277 /**
278 * Indicates this input is used by a simulated device.
279 *
280 * @param device simulated device handle
281 */
283
284 void InitSendable(wpi::SendableBuilder& builder) override;
285
286 private:
287 int m_channel;
289 int64_t m_accumulatorOffset;
290};
291
292} // namespace frc
Use a rate gyro to return the robots heading relative to a starting position.
Definition AnalogGyro.h:34
Analog input class.
Definition AnalogInput.h:32
static void SetSampleRate(double samplesPerSecond)
Set the sample rate per channel for all analog channels.
void SetAccumulatorCenter(int center)
Set the center value of the accumulator.
void InitSendable(wpi::SendableBuilder &builder) override
Initializes this Sendable object.
static constexpr int kAccumulatorNumChannels
Definition AnalogInput.h:40
double GetAverageVoltage() const
Get a scaled sample from the output of the oversample and average engine for this channel.
void SetSimDevice(HAL_SimDeviceHandle device)
Indicates this input is used by a simulated device.
int64_t GetAccumulatorValue() const
Read the accumulated value.
static double GetSampleRate()
Get the current sample rate for all channels.
void SetAverageBits(int bits)
Set the number of averaging bits.
bool IsAccumulatorChannel() const
Is the channel attached to an accumulator.
static constexpr int kAccumulatorModuleNumber
Definition AnalogInput.h:39
int GetAverageBits() const
Get the number of averaging bits previously configured.
int GetOffset() const
Get the factory scaling offset constant.
void SetOversampleBits(int bits)
Set the number of oversample bits.
int GetValue() const
Get a sample straight from this channel.
void SetAccumulatorInitialValue(int64_t value)
Set an initial value for the accumulator.
int GetLSBWeight() const
Get the factory scaling least significant bit weight constant.
void GetAccumulatorOutput(int64_t &value, int64_t &count) const
Read the accumulated value and the number of accumulated values atomically.
static constexpr int kAccumulatorChannels[kAccumulatorNumChannels]
Definition AnalogInput.h:41
int GetChannel() const
Get the channel number.
AnalogInput & operator=(AnalogInput &&)=default
~AnalogInput() override=default
int GetAverageValue() const
Get a sample from the output of the oversample and average engine for this channel.
int64_t GetAccumulatorCount() const
Read the number of accumulated values.
AnalogInput(AnalogInput &&)=default
void SetAccumulatorDeadband(int deadband)
Set the accumulator's deadband.
double GetVoltage() const
Get a scaled sample straight from this channel.
int GetOversampleBits() const
Get the number of oversample bits previously configured.
void ResetAccumulator()
Resets the accumulator to the initial value.
AnalogInput(int channel)
Construct an analog input.
void InitAccumulator()
Initialize the accumulator.
Definition AnalogTrigger.h:22
Class for configuring Direct Memory Access (DMA) of FPGA inputs.
Definition DMA.h:23
DMA sample.
Definition DMASample.h:23
A move-only C++ wrapper around a HAL handle.
Definition Types.h:96
Helper class for building Sendable dashboard representations.
Definition SendableBuilder.h:21
A helper class for use with objects that add themselves to SendableRegistry.
Definition SendableHelper.h:21
Interface for Sendable objects.
Definition Sendable.h:16
HAL_Handle HAL_SimDeviceHandle
Definition Types.h:53
Definition CAN.h:11