WPILibC++ 2027.0.0-alpha-4
Loading...
Searching...
No Matches
AnalogInput.hpp
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
10#include "wpi/hal/Types.hpp"
13
14namespace wpi {
15
16/**
17 * Analog input class.
18 *
19 * Connected to each analog channel is an averaging and oversampling engine.
20 * This engine accumulates the specified ( by SetAverageBits() and
21 * SetOversampleBits() ) number of samples before returning a new value. This is
22 * not a sliding window average. The only difference between the oversampled
23 * samples and the averaged samples is that the oversampled samples are simply
24 * accumulated effectively increasing the resolution, while the averaged samples
25 * are divided by the number of samples to retain the resolution, but get more
26 * stable values.
27 */
29 public wpi::util::SendableHelper<AnalogInput> {
30 public:
31 /**
32 * Construct an analog input.
33 *
34 * @param channel The channel number on the roboRIO to represent. 0-3 are
35 * on-board 4-7 are on the MXP port.
36 */
37 explicit AnalogInput(int channel);
38
41
42 ~AnalogInput() override = default;
43
44 /**
45 * Get a sample straight from this channel.
46 *
47 * The sample is a 12-bit value representing the 0V to 3.3V range of the A/D
48 * converter in the module. The units are in A/D converter codes. Use
49 * GetVoltage() to get the analog value in calibrated units.
50 *
51 * @return A sample straight from this channel.
52 */
53 int GetValue() const;
54
55 /**
56 * Get a sample from the output of the oversample and average engine for this
57 * channel.
58 *
59 * The sample is 12-bit + the bits configured in SetOversampleBits().
60 * The value configured in SetAverageBits() will cause this value to be
61 * averaged 2**bits number of samples.
62 *
63 * This is not a sliding window. The sample will not change until
64 * 2**(OversampleBits + AverageBits) samples have been acquired from the
65 * module on this channel.
66 *
67 * Use GetAverageVoltage() to get the analog value in calibrated units.
68 *
69 * @return A sample from the oversample and average engine for this channel.
70 */
71 int GetAverageValue() const;
72
73 /**
74 * Get a scaled sample straight from this channel.
75 *
76 * The value is scaled to units of Volts using the calibrated scaling data
77 * from GetLSBWeight() and GetOffset().
78 *
79 * @return A scaled sample straight from this channel.
80 */
81 double GetVoltage() const;
82
83 /**
84 * Get a scaled sample from the output of the oversample and average engine
85 * for this channel.
86 *
87 * The value is scaled to units of Volts using the calibrated scaling data
88 * from GetLSBWeight() and GetOffset().
89 *
90 * Using oversampling will cause this value to be higher resolution, but it
91 * will update more slowly.
92 *
93 * Using averaging will cause this value to be more stable, but it will update
94 * more slowly.
95 *
96 * @return A scaled sample from the output of the oversample and average
97 * engine for this channel.
98 */
99 double GetAverageVoltage() const;
100
101 /**
102 * Get the channel number.
103 *
104 * @return The channel number.
105 */
106 int GetChannel() const;
107
108 /**
109 * Set the number of averaging bits.
110 *
111 * This sets the number of averaging bits. The actual number of averaged
112 * samples is 2^bits.
113 *
114 * Use averaging to improve the stability of your measurement at the expense
115 * of sampling rate. The averaging is done automatically in the FPGA.
116 *
117 * @param bits Number of bits of averaging.
118 */
119 void SetAverageBits(int bits);
120
121 /**
122 * Get the number of averaging bits previously configured.
123 *
124 * This gets the number of averaging bits from the FPGA. The actual number of
125 * averaged samples is 2^bits. The averaging is done automatically in the
126 * FPGA.
127 *
128 * @return Number of bits of averaging previously configured.
129 */
130 int GetAverageBits() const;
131
132 /**
133 * Set the number of oversample bits.
134 *
135 * This sets the number of oversample bits. The actual number of oversampled
136 * values is 2^bits. Use oversampling to improve the resolution of your
137 * measurements at the expense of sampling rate. The oversampling is done
138 * automatically in the FPGA.
139 *
140 * @param bits Number of bits of oversampling.
141 */
142 void SetOversampleBits(int bits);
143
144 /**
145 * Get the number of oversample bits previously configured.
146 *
147 * This gets the number of oversample bits from the FPGA. The actual number of
148 * oversampled values is 2^bits. The oversampling is done automatically in the
149 * FPGA.
150 *
151 * @return Number of bits of oversampling previously configured.
152 */
153 int GetOversampleBits() const;
154
155 /**
156 * Get the factory scaling least significant bit weight constant.
157 *
158 * Volts = ((LSB_Weight * 1e-9) * raw) - (Offset * 1e-9)
159 *
160 * @return Least significant bit weight.
161 */
162 int GetLSBWeight() const;
163
164 /**
165 * Get the factory scaling offset constant.
166 *
167 * Volts = ((LSB_Weight * 1e-9) * raw) - (Offset * 1e-9)
168 *
169 * @return Offset constant.
170 */
171 int GetOffset() const;
172
173 /**
174 * Set the sample rate per channel for all analog channels.
175 *
176 * The maximum rate is 500kS/s divided by the number of channels in use.
177 * This is 62500 samples/s per channel.
178 *
179 * @param samplesPerSecond The number of samples per second.
180 */
181 static void SetSampleRate(double samplesPerSecond);
182
183 /**
184 * Get the current sample rate for all channels
185 *
186 * @return Sample rate.
187 */
188 static double GetSampleRate();
189
190 /**
191 * Indicates this input is used by a simulated device.
192 *
193 * @param device simulated device handle
194 */
196
198
199 private:
200 int m_channel;
202};
203
204} // namespace wpi
int GetAverageValue() const
Get a sample from the output of the oversample and average engine for this channel.
int GetOversampleBits() const
Get the number of oversample bits previously configured.
AnalogInput(int channel)
Construct an analog input.
static void SetSampleRate(double samplesPerSecond)
Set the sample rate per channel for all analog channels.
AnalogInput & operator=(AnalogInput &&)=default
static double GetSampleRate()
Get the current sample rate for all channels.
int GetOffset() const
Get the factory scaling offset constant.
int GetChannel() const
Get the channel number.
double GetAverageVoltage() const
Get a scaled sample from the output of the oversample and average engine for this channel.
void SetOversampleBits(int bits)
Set the number of oversample bits.
int GetValue() const
Get a sample straight from this channel.
~AnalogInput() override=default
void SetSimDevice(HAL_SimDeviceHandle device)
Indicates this input is used by a simulated device.
int GetLSBWeight() const
Get the factory scaling least significant bit weight constant.
double GetVoltage() const
Get a scaled sample straight from this channel.
int GetAverageBits() const
Get the number of averaging bits previously configured.
void SetAverageBits(int bits)
Set the number of averaging bits.
void InitSendable(wpi::util::SendableBuilder &builder) override
Initializes this Sendable object.
AnalogInput(AnalogInput &&)=default
A move-only C++ wrapper around a HAL handle.
Definition Types.hpp:16
Helper class for building Sendable dashboard representations.
Definition SendableBuilder.hpp:21
A helper class for use with objects that add themselves to SendableRegistry.
Definition SendableHelper.hpp:21
Interface for Sendable objects.
Definition Sendable.hpp:16
HAL_Handle HAL_SimDeviceHandle
Definition Types.h:53
Definition CvSource.hpp:15