WPILibC++ 2025.0.0-alpha-1-9-ga2beb75
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/Types.h"
10
11/**
12 * @defgroup hal_analoginput Analog Input Functions
13 * @ingroup hal_capi
14 * @{
15 */
16
17#ifdef __cplusplus
18extern "C" {
19#endif
20
21/**
22 * Initializes the analog input port using the given port object.
23 *
24 * @param[in] portHandle Handle to the port to initialize.
25 * @param[in] allocationLocation the location where the allocation is occurring
26 * (can be null)
27 * @param[out] status the error code, or 0 for success
28 * @return the created analog input handle
29 */
31 HAL_PortHandle portHandle, const char* allocationLocation, int32_t* status);
32
33/**
34 * Frees an analog input port.
35 *
36 * @param[in,out] analogPortHandle Handle to the analog port.
37 */
39
40/**
41 * Checks that the analog module number is valid.
42 *
43 * @param[in] module The analog module number.
44 * @return Analog module is valid and present
45 */
47
48/**
49 * Checks that the analog output channel number is valid.
50 * Verifies that the analog channel number is one of the legal channel numbers.
51 * Channel numbers are 0-based.
52 *
53 * @param[in] channel The analog output channel number.
54 * @return Analog channel is valid
55 */
57
58/**
59 * Indicates the analog input is used by a simulated device.
60 *
61 * @param handle the analog input handle
62 * @param device simulated device handle
63 */
65 HAL_SimDeviceHandle device);
66
67/**
68 * Sets the sample rate.
69 *
70 * This is a global setting for the Athena and effects all channels.
71 *
72 * @param[in] samplesPerSecond The number of samples per channel per second.
73 * @param[out] status the error code, or 0 for success
74 */
75void HAL_SetAnalogSampleRate(double samplesPerSecond, int32_t* status);
76
77/**
78 * Gets the current sample rate.
79 *
80 * This assumes one entry in the scan list.
81 * This is a global setting for the Athena and effects all channels.
82 *
83 * @param[out] status the error code, or 0 for success
84 * @return Sample rate.
85 */
86double HAL_GetAnalogSampleRate(int32_t* status);
87
88/**
89 * Sets the number of averaging bits.
90 *
91 * This sets the number of averaging bits. The actual number of averaged samples
92 * is 2**bits. Use averaging to improve the stability of your measurement at the
93 * expense of sampling rate. The averaging is done automatically in the FPGA.
94 *
95 * @param[in] analogPortHandle Handle to the analog port to configure.
96 * @param[in] bits Number of bits to average.
97 * @param[out] status the error code, or 0 for success
98 */
100 int32_t bits, int32_t* status);
101
102/**
103 * Gets the number of averaging bits.
104 *
105 * This gets the number of averaging bits from the FPGA. The actual number of
106 * averaged samples is 2**bits. The averaging is done automatically in the FPGA.
107 *
108 * @param[in] analogPortHandle Handle to the analog port to use.
109 * @param[out] status the error code, or 0 for success
110 * @return Bits to average.
111 */
113 int32_t* status);
114
115/**
116 * Sets the number of oversample bits.
117 *
118 * This sets the number of oversample bits. The actual number of oversampled
119 * values is 2**bits. Use oversampling to improve the resolution of your
120 * measurements at the expense of sampling rate. The oversampling is done
121 * automatically in the FPGA.
122 *
123 * @param[in] analogPortHandle Handle to the analog port to use.
124 * @param[in] bits Number of bits to oversample.
125 * @param[out] status the error code, or 0 for success
126 */
128 int32_t bits, int32_t* status);
129
130/**
131 * Gets the number of oversample bits.
132 *
133 * This gets the number of oversample bits from the FPGA. The actual number of
134 * oversampled values is 2**bits. The oversampling is done automatically in the
135 * FPGA.
136 *
137 * @param[in] analogPortHandle Handle to the analog port to use.
138 * @param[out] status the error code, or 0 for success
139 * @return Bits to oversample.
140 */
142 int32_t* status);
143
144/**
145 * Gets a sample straight from the channel on this module.
146 *
147 * The sample is a 12-bit value representing the 0V to 5V range of the A/D
148 * converter in the module. The units are in A/D converter codes. Use
149 * GetVoltage() to get the analog value in calibrated units.
150 *
151 * @param[in] analogPortHandle Handle to the analog port to use.
152 * @param[out] status the error code, or 0 for success
153 * @return A sample straight from the channel on this module.
154 */
156 int32_t* status);
157
158/**
159 * Gets a sample from the output of the oversample and average engine for the
160 * channel.
161 *
162 * The sample is 12-bit + the value configured in SetOversampleBits().
163 * The value configured in SetAverageBits() will cause this value to be averaged
164 * 2**bits number of samples. This is not a sliding window. The sample will not
165 * change until 2**(OversampleBits + AverageBits) samples have been acquired
166 * from the module on this channel. Use GetAverageVoltage() to get the analog
167 * value in calibrated units.
168 *
169 * @param[in] analogPortHandle Handle to the analog port to use.
170 * @param[out] status the error code, or 0 for success
171 * @return A sample from the oversample and average engine for the channel.
172 */
174 int32_t* status);
175
176/**
177 * Converts a voltage to a raw value for a specified channel.
178 *
179 * This process depends on the calibration of each channel, so the channel must
180 * be specified.
181 *
182 * @todo This assumes raw values. Oversampling not supported as is.
183 *
184 * @param[in] analogPortHandle Handle to the analog port to use.
185 * @param[in] voltage The voltage to convert.
186 * @param[out] status the error code, or 0 for success
187 * @return The raw value for the channel.
188 */
190 double voltage, int32_t* status);
191
192/**
193 * Gets a scaled sample straight from the channel on this module.
194 *
195 * The value is scaled to units of Volts using the calibrated scaling data from
196 * GetLSBWeight() and GetOffset().
197 *
198 * @param[in] analogPortHandle Handle to the analog port to use.
199 * @param[out] status the error code, or 0 for success
200 * @return A scaled sample straight from the channel on this module.
201 */
203 int32_t* status);
204
205/**
206 * Gets a scaled sample from the output of the oversample and average engine for
207 * the channel.
208 *
209 * The value is scaled to units of Volts using the calibrated scaling data from
210 * GetLSBWeight() and GetOffset(). Using oversampling will cause this value to
211 * be higher resolution, but it will update more slowly. Using averaging will
212 * cause this value to be more stable, but it will update more slowly.
213 *
214 * @param[in] analogPortHandle Handle to the analog port to use.
215 * @param[out] status the error code, or 0 for success
216 * @return A scaled sample from the output of the oversample and average engine
217 * for the channel.
218 */
220 int32_t* status);
221
222/**
223 * Gets the factory scaling least significant bit weight constant.
224 * The least significant bit weight constant for the channel that was calibrated
225 * in manufacturing and stored in an eeprom in the module.
226 *
227 * Volts = ((LSB_Weight * 1e-9) * raw) - (Offset * 1e-9)
228 *
229 * @param[in] analogPortHandle Handle to the analog port to use.
230 * @param[out] status the error code, or 0 for success
231 * @return Least significant bit weight.
232 */
234 int32_t* status);
235
236/**
237 * Gets the factory scaling offset constant.
238 * The offset constant for the channel that was calibrated in manufacturing and
239 * stored in an eeprom in the module.
240 *
241 * Volts = ((LSB_Weight * 1e-9) * raw) - (Offset * 1e-9)
242 *
243 * @param[in] analogPortHandle Handle to the analog port to use.
244 * @param[out] status Error status variable. 0 on success.
245 * @return Offset constant.
246 */
248 int32_t* status);
249
250/**
251 * Get the analog voltage from a raw value.
252 *
253 * @param[in] analogPortHandle Handle to the analog port the values were read
254 * from.
255 * @param[in] rawValue The raw analog value
256 * @param[out] status Error status variable. 0 on success.
257 * @return The voltage relating to the value
258 */
260 int32_t rawValue, int32_t* status);
261#ifdef __cplusplus
262} // extern "C"
263#endif
264/** @} */
int32_t HAL_GetAnalogAverageBits(HAL_AnalogInputHandle analogPortHandle, int32_t *status)
Gets the number of averaging bits.
void HAL_SetAnalogOversampleBits(HAL_AnalogInputHandle analogPortHandle, int32_t bits, int32_t *status)
Sets the number of oversample bits.
double HAL_GetAnalogValueToVolts(HAL_AnalogInputHandle analogPortHandle, int32_t rawValue, int32_t *status)
Get the analog voltage from a raw value.
void HAL_SetAnalogAverageBits(HAL_AnalogInputHandle analogPortHandle, int32_t bits, int32_t *status)
Sets the number of averaging bits.
int32_t HAL_GetAnalogOffset(HAL_AnalogInputHandle analogPortHandle, int32_t *status)
Gets the factory scaling offset constant.
int32_t HAL_GetAnalogAverageValue(HAL_AnalogInputHandle analogPortHandle, int32_t *status)
Gets a sample from the output of the oversample and average engine for the channel.
double HAL_GetAnalogVoltage(HAL_AnalogInputHandle analogPortHandle, int32_t *status)
Gets a scaled sample straight from the channel on this module.
double HAL_GetAnalogAverageVoltage(HAL_AnalogInputHandle analogPortHandle, int32_t *status)
Gets a scaled sample from the output of the oversample and average engine for the channel.
void HAL_SetAnalogSampleRate(double samplesPerSecond, int32_t *status)
Sets the sample rate.
HAL_AnalogInputHandle HAL_InitializeAnalogInputPort(HAL_PortHandle portHandle, const char *allocationLocation, int32_t *status)
Initializes the analog input port using the given port object.
int32_t HAL_GetAnalogValue(HAL_AnalogInputHandle analogPortHandle, int32_t *status)
Gets a sample straight from the channel on this module.
HAL_Bool HAL_CheckAnalogInputChannel(int32_t channel)
Checks that the analog output channel number is valid.
HAL_Bool HAL_CheckAnalogModule(int32_t module)
Checks that the analog module number is valid.
int32_t HAL_GetAnalogVoltsToValue(HAL_AnalogInputHandle analogPortHandle, double voltage, int32_t *status)
Converts a voltage to a raw value for a specified channel.
int32_t HAL_GetAnalogLSBWeight(HAL_AnalogInputHandle analogPortHandle, int32_t *status)
Gets the factory scaling least significant bit weight constant.
void HAL_SetAnalogInputSimDevice(HAL_AnalogInputHandle handle, HAL_SimDeviceHandle device)
Indicates the analog input is used by a simulated device.
int32_t HAL_GetAnalogOversampleBits(HAL_AnalogInputHandle analogPortHandle, int32_t *status)
Gets the number of oversample bits.
void HAL_FreeAnalogInputPort(HAL_AnalogInputHandle analogPortHandle)
Frees an analog input port.
double HAL_GetAnalogSampleRate(int32_t *status)
Gets the current sample rate.
int32_t HAL_Bool
Definition: Types.h:73
HAL_Handle HAL_PortHandle
Definition: Types.h:19
HAL_Handle HAL_SimDeviceHandle
Definition: Types.h:53
HAL_Handle HAL_AnalogInputHandle
Definition: Types.h:21
bits
Definition: data.h:44